tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | README | LICENSE

os.c (2409B)


      1 /* zlib License
      2  *
      3  * Copyright (c) 2017-2022 Ed van Bruggen
      4  *
      5  * This software is provided 'as-is', without any express or implied
      6  * warranty.  In no event will the authors be held liable for any damages
      7  * arising from the use of this software.
      8  *
      9  * Permission is granted to anyone to use this software for any purpose,
     10  * including commercial applications, and to alter it and redistribute it
     11  * freely, subject to the following restrictions:
     12  *
     13  * 1. The origin of this software must not be misrepresented; you must not
     14  *    claim that you wrote the original software. If you use this software
     15  *    in a product, an acknowledgment in the product documentation would be
     16  *    appreciated but is not required.
     17  * 2. Altered source versions must be plainly marked as such, and must not be
     18  *    misrepresented as being the original software.
     19  * 3. This notice may not be removed or altered from any source distribution.
     20  */
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <time.h>
     24 #include <unistd.h>
     25 #include <limits.h>
     26 
     27 #include "../tisp.h"
     28 
     29 /* TODO sys ls, mv, cp, rm, mkdir, exit */
     30 
     31 /* change to new directory */
     32 static Val
     33 prim_cd(Tsp st, Hash env, Val args)
     34 {
     35 	Val dir;
     36 	tsp_arg_num(args, "cd", 1);
     37 	dir = car(args);
     38 	if (!(dir->t & (TSP_STR|TSP_SYM)))
     39 		tsp_warnf("strlen: expected string or symbol, received %s", type_str(dir->t));
     40 	if (chdir(dir->v.s))
     41 		return perror("; error: cd"), NULL;
     42 	return st->none;
     43 }
     44 
     45 /* TODO rename to cwd ? */
     46 /* return string of current working directory */
     47 static Val
     48 prim_pwd(Tsp st, Hash env, Val args)
     49 {
     50 	tsp_arg_num(args, "pwd", 0);
     51 	char cwd[PATH_MAX];
     52 	if (!getcwd(cwd, sizeof(cwd)) && cwd[0] != '(')
     53 		tsp_warn("pwd: could not get current directory");
     54 	return mk_str(st, cwd);
     55 }
     56 
     57 /* TODO time formating */
     58 /* return number of seconds since 1970 (unix time stamp) */
     59 static Val
     60 prim_now(Tsp st, Hash env, Val args)
     61 {
     62 	tsp_arg_num(args, "now", 0);
     63 	return mk_int(time(NULL));
     64 }
     65 
     66 /* TODO time-avg: run timeit N times and take average */
     67 /* return time in miliseconds taken to run command given */
     68 static Val
     69 form_time(Tsp st, Hash env, Val args)
     70 {
     71 	Val v;
     72 	clock_t t;
     73 	tsp_arg_num(args, "time", 1);
     74 	t = clock();
     75 	if (!(v = tisp_eval(st, env, car(args))))
     76 		return NULL;
     77 	t = clock() - t;
     78 	return mk_dec(((double)t)/CLOCKS_PER_SEC*100);
     79 }
     80 
     81 void
     82 tib_env_os(Tsp st)
     83 {
     84 	tsp_env_name_prim(cd!, cd);
     85 	tsp_env_prim(pwd);
     86 	tsp_env_prim(now);
     87 	tsp_env_form(time);
     88 }