tisp

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

commit 229859f00655a2d6ae49c1c9916eaf7cea80dcf5
parent 7ed8bc653eb91897b89f175c2fee23db3a7d1018
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sun,  4 Oct 2020 22:20:06 -0700

Rename time tib to os and add cd!, pwd functions

Diffstat:
Makefile | 2+-
config.mk | 1+
main.c | 4++--
tib/os.c | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tib/time.c | 54------------------------------------------------------
5 files changed, 90 insertions(+), 57 deletions(-)

diff --git a/Makefile b/Makefile @@ -10,7 +10,7 @@ endif EXE = tisp SRC = tisp.c main.c -TIB = tib/math.c tib/io.c tib/time.c tib/string.c +TIB = tib/math.c tib/io.c tib/os.c tib/string.c OBJ = $(SRC:.c=.o) $(TIB:.c=.o) LIB = tib/libtibmath.so tib/libtibio.so TSP = tib/core.tsp tib/io.tsp tib/math.tsp tib/doc.tsp tib/repl.tsp diff --git a/config.mk b/config.mk @@ -18,6 +18,7 @@ LDFLAGS = -O3 -Wl,-rpath=$(DESTDIR)$(PREFIX)/lib/tisp $(LIBS) DEBUG ?= 0 # do not load tibs statically, use load procedure instead +# TODO include -ldl when on, enable (load) of shared libs # CFLAGS += -DTIB_DYNAMIC # compiler and linker diff --git a/main.c b/main.c @@ -11,7 +11,7 @@ # include "tibs.tsp.h" tsp_include_tib(math); tsp_include_tib(io); - tsp_include_tib(time); + tsp_include_tib(os); tsp_include_tib(string); #endif @@ -25,7 +25,7 @@ main(int argc, char *argv[]) #ifndef TIB_DYNAMIC tib_env_math(st); tib_env_io(st); - tib_env_time(st); + tib_env_os(st); tib_env_string(st); tisp_env_lib(st, tibs); #endif diff --git a/tib/os.c b/tib/os.c @@ -0,0 +1,86 @@ +/* zlib License + * + * Copyright (c) 2017-2020 Ed van Bruggen + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> +#include <limits.h> + +#include "../tisp.h" + +/* change to new directory */ +static Val +prim_cd(Tsp st, Hash env, Val args) +{ + Val v; + tsp_arg_num(args, "cd", 1); + if (!(v = tisp_eval(st, env, car(args)))) + return NULL; + if (!(v->t & (STRING|SYMBOL))) + tsp_warnf("strlen: expected string or symbol, received %s", type_str(v->t)); + if (chdir(v->v.s)) { + perror("; tisp: error: cd"); + return NULL; + } + return st->none; +} + +/* print current working directory */ +static Val +prim_pwd(Tsp st, Hash env, Val args) +{ + tsp_arg_num(args, "pwd", 0); + char cwd[PATH_MAX]; + if (!getcwd(cwd, sizeof(cwd)) && cwd[0] != '(') + tsp_warn("pwd: could not get current directory"); + return mk_str(st, cwd); +} + +/* return number of seconds since 1970 (unix time stamp) */ +static Val +prim_time(Tsp st, Hash env, Val args) +{ + tsp_arg_num(args, "time", 0); + return mk_int(time(NULL)); +} + +/* return time taken to run command given */ +static Val +prim_timeit(Tsp st, Hash env, Val args) +{ + Val v; + clock_t t; + tsp_arg_num(args, "timeit", 1); + t = clock(); + if (!(v = tisp_eval(st, env, car(args)))) + return NULL; + t = clock() - t; + return mk_dec(((double)t)/CLOCKS_PER_SEC); +} + +void +tib_env_os(Tsp st) +{ + tsp_env_name_fn(cd!, cd); + tsp_env_fn(pwd); + tsp_env_fn(time); + tsp_env_fn(timeit); +} diff --git a/tib/time.c b/tib/time.c @@ -1,54 +0,0 @@ -/* zlib License - * - * Copyright (c) 2017-2020 Ed van Bruggen - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ -#include <stdio.h> -#include <stdlib.h> -#include <time.h> - -#include "../tisp.h" - -/* return number of seconds since 1970 (unix time stamp) */ -static Val -prim_time(Tsp st, Hash env, Val args) -{ - tsp_arg_num(args, "time", 0); - return mk_int(time(NULL)); -} - -/* return time taken to run command given */ -static Val -prim_timeit(Tsp st, Hash env, Val args) -{ - Val v; - clock_t t; - tsp_arg_num(args, "timeit", 1); - t = clock(); - if (!(v = tisp_eval(st, env, car(args)))) - return NULL; - t = clock() - t; - return mk_dec(((double)t)/CLOCKS_PER_SEC); -} - -void -tib_env_time(Tsp st) -{ - tsp_env_fn(time); - tsp_env_fn(timeit); -}