From 8293f85c5b2c0edbfcbe908deca989623c2f4a1a Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 27 Aug 2022 18:12:32 +0800 Subject: [PATCH] :rocket: add lib function `exit()` and add test file `watchdog.nas` to run the nasal file when it is changed. --- lib.nas | 5 +++++ nasal_builtin.h | 6 ++++++ stl/lib.nas | 5 +++++ test/calc.nas | 1 + test/md5compare.nas | 12 ++++++------ test/watchdog.nas | 47 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 test/watchdog.nas diff --git a/lib.nas b/lib.nas index 83569df..9669f67 100644 --- a/lib.nas +++ b/lib.nas @@ -61,6 +61,11 @@ var floor=func(val){ return __floor(val); } +# exit using std::exit +var exit=func(val=-1){ + return __exit(val); +} + # abort using std::abort var abort=func(){ __abort(); diff --git a/nasal_builtin.h b/nasal_builtin.h index d25ec3c..a801782 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -56,6 +56,11 @@ nas_ref builtin_println(nas_ref* local,nasal_gc& gc) std::cout<"); +} + +var argv=runtime.argv(); +if(size(argv)!=1){ + println(os_time(),err_hd(),"need correct file path to watch"); + usage(); + exit(-1); +} +var filename=argv[0]; +if(!io.exists(filename)){ + println(os_time(),err_hd(),"file <",filename,"> does not exist"); + usage(); + exit(-1); +} + +var modified_time=fstat(filename).st_mtime; +println(os_time(),watching_hd(),filename," .."); +while(1){ + unix.sleep(1); + if(!io.exists(filename)){ + println(os_time(),err_hd(),"file <",filename,"> does not exist"); + break; + } + var latest_modified_time=fstat(filename).st_mtime; + if(latest_modified_time!=modified_time){ + modified_time=latest_modified_time; + println(os_time(),modified_hd(),filename); + var ret=system((os.platform()=="windows"?"":"./")~"nasal "~filename); + if(ret!=0){ + println(os_time(),err_hd(),"process returned value ",ret); + } + } +} \ No newline at end of file