first version of system-cmd

This commit is contained in:
VaclavT 2021-02-14 13:02:11 +01:00
parent e1465ae786
commit 5145f5d664
3 changed files with 89 additions and 12 deletions

View File

@ -1,6 +1,5 @@
### TODO ### TODO
- construct list for parse-csv in csvparse.cpp instead of ml.cpp
- support for strings with " included - support for strings with " included
- documentation - documentation
- add url of source/inspiration to clib/*.cpp - add url of source/inspiration to clib/*.cpp
@ -10,13 +9,16 @@
- prejmenovat ivaluize - prejmenovat ivaluize
- add some debug support?? - add some debug support??
- add instrumentation (time, nr of evals, debug info, debug environment etc) - add instrumentation (time, nr of evals, debug info, debug environment etc)
- in cating functions (as_string..) better exception description
- add fuctions asecond,third, fourth...
- add better print (syntaxhighligting)
#### Functionality #### Functionality
- readline - readline
- execute system command - execute system command
- printf - printf
- env - env
- support for including lib - support for including lisp lib
- date support - date support
- file functions - file functions
- name it here - name it here

View File

@ -1,24 +1,57 @@
(print "Debug starts") (print "Debug starts")
(define json_list (read-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}")) ; define a function `fact` that takes an argument `n`
(print json_list) ; (defun fact (n)
(for x json_list ; (if (<= n 1)
(print x)) ; 1
; (* n (fact (- n 1)))
; ))
; (print (fact 5))
; (define json_list (read-json "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]}"))
; (print json_list)
; (for x json_list
; (print x))
; nefunguje - api.nasdaq.com
;(define nasdaq_headers (quote (("Accept" "application/json, text/plain, */*") ("Connection" "keep-alive") ("Origin" "https://www.nasdaq.com/") ("User-Agent" "Mozilla/5.0 (Windows NT 10.0)"))))
;(define json_list2 (read-json (read-url "https://api.nasdaq.com/api/calendar/dividends/?date=2021-02-01" nasdaq_headers)))
; (for x json_list2
; (print x))
;; (include "tmp/example.lisp") ;; (include "tmp/example.lisp")
;; (print "sorted: " (qs '(10 9 8 7 6 5 4 3 2 1))) ;; (print "sorted: " (qs '(10 9 8 7 6 5 4 3 2 1)))
;; (define csv (read-file "tmp/data.csv"))
;; ; (print csv) ; (define csv (read-file "tmp/data.csv"))
;; (define csv_list (parse-csv csv)) ; (define csv_list (parse-csv csv))
;; ; (print csv_list) ; (for x csv_list
;; (for x csv_list ; (print x))
;; (print x))
;; (define web_page (read-url "https://query1.finance.yahoo.com/v7/finance/download/FDX?period1=1581272585&period2=1612894985&interval=1d&events=history&includeAdjustedClose=true")) ;; (define web_page (read-url "https://query1.finance.yahoo.com/v7/finance/download/FDX?period1=1581272585&period2=1612894985&interval=1d&events=history&includeAdjustedClose=true"))
;; (print web_page) ;; (print web_page)
;; (define fdx_list (parse-csv (index web_page 1))) ;; (define fdx_list (parse-csv (index web_page 1)))
;; (print fdx_list) ;; (print fdx_list)
(define curl "curl -H \"Accept: application/json, text/plain, */*\" -H \"Origin: https://www.nasdaq.com/\" -H \"User-Agent: Mozilla/5.0 (Windows NT 10.0)\" https://api.nasdaq.com/api/calendar/dividends/?date=2021-02-01 2>/dev/null")
(define curl_out (system-cmd curl))
(define json_list (read-json (last curl_out)))
;(print json_list)
(define data (first json_list))
;(print data)
;(print (index data 1)) ; druhy prvek
(define calendar (index data 1))
;(print calendar)
(define calendar_data (index (index calendar 0) 1))
;(print calendar_data)
(define header (index (index calendar_data 0) 1))
;(print header)
(define rows (index (index calendar_data 1) 1))
;(print rows)
(for x rows
(print x))
(print "Debug ends") (print "Debug ends")

42
ml.cpp
View File

@ -1213,6 +1213,47 @@ namespace builtin {
return json.ivalualize(); return json.ivalualize();
} }
// Execute system command
MlValue system_cmd(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
// TODO add support for more params constructing options as one string
// TODO add support for stderr
if (args.size() != 1)
throw MlError(MlValue("system-cmd", system_cmd), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::string cmd = args[0].as_string();
std::string cmd_output = "";
int stat;
// TODO better parameter here
// TODO handle reading of stderr
// https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
// https://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/
char buffer[128];
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe)
throw std::runtime_error("popen() failed!");
try {
while (!std::feof(pipe)) {
if (std::fgets(buffer, 128, pipe) != nullptr)
cmd_output += buffer;
}
} catch (...) {
stat = pclose(pipe);
throw;
}
stat = pclose(pipe);
int cmd_retval = WEXITSTATUS(stat);
// TODO add helper function for this
std::vector<MlValue> lst;
lst.push_back(MlValue(cmd_retval));
lst.push_back(MlValue::string(cmd_output));
return lst;
}
// Read a file and execute its code // Read a file and execute its code
MlValue include(std::vector<MlValue> args, MlEnvironment &env) { MlValue include(std::vector<MlValue> args, MlEnvironment &env) {
@ -1738,6 +1779,7 @@ MlValue MlEnvironment::get(const std::string& name) const {
if (name == "write-file") return MlValue("write-file", builtin::write_file); if (name == "write-file") return MlValue("write-file", builtin::write_file);
if (name == "read-url") return MlValue("read-url", builtin::read_url); if (name == "read-url") return MlValue("read-url", builtin::read_url);
if (name == "read-json") return MlValue("read-json", builtin::read_json); if (name == "read-json") return MlValue("read-json", builtin::read_json);
if (name == "system-cmd") return MlValue("system-cmd", builtin::system_cmd);
#endif #endif
// String operations // String operations