diff options
Diffstat (limited to 'clients')
-rw-r--r-- | clients/lib/Makefile | 23 | ||||
-rw-r--r-- | clients/lib/iolib.c | 479 | ||||
-rw-r--r-- | clients/lib/mathlib.c | 235 | ||||
-rw-r--r-- | clients/lib/mathlib.h | 13 | ||||
-rw-r--r-- | clients/lib/strlib.c | 135 | ||||
-rw-r--r-- | clients/lib/strlib.h | 13 | ||||
-rw-r--r-- | clients/lua/Makefile | 17 | ||||
-rw-r--r-- | clients/lua/README | 5 | ||||
-rw-r--r-- | clients/lua/lua.c | 28 |
9 files changed, 948 insertions, 0 deletions
diff --git a/clients/lib/Makefile b/clients/lib/Makefile new file mode 100644 index 00000000..4110a62f --- /dev/null +++ b/clients/lib/Makefile @@ -0,0 +1,23 @@ +# makefile for lualib + +INC= $(LUA)/include +LIB= $(LUA)/lib + +CC= gcc +CFLAGS= -Wall -O2 -I$(INC) $(DEFS) + +OBJS= iolib.o mathlib.o strlib.o +SLIB= $(LIB)/liblualib.a +DLIB= $(LIB)/liblualib.so.1.1 + +libs: $(SLIB) $(DLIB) + +$(SLIB): $(OBJS) + ar ruvl $@ $(OBJS) + ranlib $(SLIB) + +$(DLIB): $(OBJS) + ld -o $@ $(OBJS) + +clean: + rm -f $(OBJS) $(SLIB) $(DLIB) diff --git a/clients/lib/iolib.c b/clients/lib/iolib.c new file mode 100644 index 00000000..b972124b --- /dev/null +++ b/clients/lib/iolib.c @@ -0,0 +1,479 @@ +/* +** iolib.c +** Input/output library to LUA +*/ + +char *rcs_iolib="$Id: iolib.c,v 1.4 1994/04/25 20:11:23 celes Exp $"; + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <ctype.h> +#include <sys/stat.h> +#ifdef __GNUC__ +#include <floatingpoint.h> +#endif + +#include "mm.h" + +#include "lua.h" + +static FILE *in=stdin, *out=stdout; + +/* +** Open a file to read. +** LUA interface: +** status = readfrom (filename) +** where: +** status = 1 -> success +** status = 0 -> error +*/ +static void io_readfrom (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL) /* restore standart input */ + { + if (in != stdin) + { + fclose (in); + in = stdin; + } + lua_pushnumber (1); + } + else + { + if (!lua_isstring (o)) + { + lua_error ("incorrect argument to function 'readfrom`"); + lua_pushnumber (0); + } + else + { + FILE *fp = fopen (lua_getstring(o),"r"); + if (fp == NULL) + { + lua_pushnumber (0); + } + else + { + if (in != stdin) fclose (in); + in = fp; + lua_pushnumber (1); + } + } + } +} + + +/* +** Open a file to write. +** LUA interface: +** status = writeto (filename) +** where: +** status = 1 -> success +** status = 0 -> error +*/ +static void io_writeto (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL) /* restore standart output */ + { + if (out != stdout) + { + fclose (out); + out = stdout; + } + lua_pushnumber (1); + } + else + { + if (!lua_isstring (o)) + { + lua_error ("incorrect argument to function 'writeto`"); + lua_pushnumber (0); + } + else + { + FILE *fp = fopen (lua_getstring(o),"w"); + if (fp == NULL) + { + lua_pushnumber (0); + } + else + { + if (out != stdout) fclose (out); + out = fp; + lua_pushnumber (1); + } + } + } +} + + +/* +** Open a file to write appended. +** LUA interface: +** status = appendto (filename) +** where: +** status = 2 -> success (already exist) +** status = 1 -> success (new file) +** status = 0 -> error +*/ +static void io_appendto (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL) /* restore standart output */ + { + if (out != stdout) + { + fclose (out); + out = stdout; + } + lua_pushnumber (1); + } + else + { + if (!lua_isstring (o)) + { + lua_error ("incorrect argument to function 'appendto`"); + lua_pushnumber (0); + } + else + { + int r; + FILE *fp; + struct stat st; + if (stat(lua_getstring(o), &st) == -1) r = 1; + else r = 2; + fp = fopen (lua_getstring(o),"a"); + if (fp == NULL) + { + lua_pushnumber (0); + } + else + { + if (out != stdout) fclose (out); + out = fp; + lua_pushnumber (r); + } + } + } +} + + + +/* +** Read a variable. On error put nil on stack. +** LUA interface: +** variable = read ([format]) +** +** O formato pode ter um dos seguintes especificadores: +** +** s ou S -> para string +** f ou F, g ou G, e ou E -> para reais +** i ou I -> para inteiros +** +** Estes especificadores podem vir seguidos de numero que representa +** o numero de campos a serem lidos. +*/ +static void io_read (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL || !lua_isstring(o)) /* free format */ + { + int c; + char s[256]; + while (isspace(c=fgetc(in))) + ; + if (c == '\"') + { + int c, n=0; + while((c = fgetc(in)) != '\"') + { + if (c == EOF) + { + lua_pushnil (); + return; + } + s[n++] = c; + } + s[n] = 0; + } + else if (c == '\'') + { + int c, n=0; + while((c = fgetc(in)) != '\'') + { + if (c == EOF) + { + lua_pushnil (); + return; + } + s[n++] = c; + } + s[n] = 0; + } + else + { + char *ptr; + double d; + ungetc (c, in); + if (fscanf (in, "%s", s) != 1) + { + lua_pushnil (); + return; + } + d = strtod (s, &ptr); + if (!(*ptr)) + { + lua_pushnumber (d); + return; + } + } + lua_pushstring (s); + return; + } + else /* formatted */ + { + char *e = lua_getstring(o); + char t; + int m=0; + while (isspace(*e)) e++; + t = *e++; + while (isdigit(*e)) + m = m*10 + (*e++ - '0'); + + if (m > 0) + { + char f[80]; + char s[256]; + sprintf (f, "%%%ds", m); + if (fgets (s, m, in) == NULL) + { + lua_pushnil(); + return; + } + else + { + if (s[strlen(s)-1] == '\n') + s[strlen(s)-1] = 0; + } + switch (tolower(t)) + { + case 'i': + { + long int l; + sscanf (s, "%ld", &l); + lua_pushnumber(l); + } + break; + case 'f': case 'g': case 'e': + { + float f; + sscanf (s, "%f", &f); + lua_pushnumber(f); + } + break; + default: + lua_pushstring(s); + break; + } + } + else + { + switch (tolower(t)) + { + case 'i': + { + long int l; + if (fscanf (in, "%ld", &l) == EOF) + lua_pushnil(); + else lua_pushnumber(l); + } + break; + case 'f': case 'g': case 'e': + { + float f; + if (fscanf (in, "%f", &f) == EOF) + lua_pushnil(); + else lua_pushnumber(f); + } + break; + default: + { + char s[256]; + if (fscanf (in, "%s", s) == EOF) + lua_pushnil(); + else lua_pushstring(s); + } + break; + } + } + } +} + + +/* +** Write a variable. On error put 0 on stack, otherwise put 1. +** LUA interface: +** status = write (variable [,format]) +** +** O formato pode ter um dos seguintes especificadores: +** +** s ou S -> para string +** f ou F, g ou G, e ou E -> para reais +** i ou I -> para inteiros +** +** Estes especificadores podem vir seguidos de: +** +** [?][m][.n] +** +** onde: +** ? -> indica justificacao +** < = esquerda +** | = centro +** > = direita (default) +** m -> numero maximo de campos (se exceder estoura) +** n -> indica precisao para +** reais -> numero de casas decimais +** inteiros -> numero minimo de digitos +** string -> nao se aplica +*/ +static char *buildformat (char *e, lua_Object o) +{ + static char buffer[512]; + static char f[80]; + char *string = &buffer[255]; + char t, j='r'; + int m=0, n=0, l; + while (isspace(*e)) e++; + t = *e++; + if (*e == '<' || *e == '|' || *e == '>') j = *e++; + while (isdigit(*e)) + m = m*10 + (*e++ - '0'); + e++; /* skip point */ + while (isdigit(*e)) + n = n*10 + (*e++ - '0'); + + sprintf(f,"%%"); + if (j == '<' || j == '|') sprintf(strchr(f,0),"-"); + if (m != 0) sprintf(strchr(f,0),"%d", m); + if (n != 0) sprintf(strchr(f,0),".%d", n); + sprintf(strchr(f,0), "%c", t); + switch (tolower(t)) + { + case 'i': t = 'i'; + sprintf (string, f, (long int)lua_getnumber(o)); + break; + case 'f': case 'g': case 'e': t = 'f'; + sprintf (string, f, (float)lua_getnumber(o)); + break; + case 's': t = 's'; + sprintf (string, f, lua_getstring(o)); + break; + default: return ""; + } + l = strlen(string); + if (m!=0 && l>m) + { + int i; + for (i=0; i<m; i++) + string[i] = '*'; + string[i] = 0; + } + else if (m!=0 && j=='|') + { + int i=l-1; + while (isspace(string[i])) i--; + string -= (m-i) / 2; + i=0; + while (string[i]==0) string[i++] = ' '; + string[l] = 0; + } + return string; +} +static void io_write (void) +{ + lua_Object o1 = lua_getparam (1); + lua_Object o2 = lua_getparam (2); + if (o1 == NULL) /* new line */ + { + fprintf (out, "\n"); + lua_pushnumber(1); + } + else if (o2 == NULL) /* free format */ + { + int status=0; + if (lua_isnumber(o1)) + status = fprintf (out, "%g", lua_getnumber(o1)); + else if (lua_isstring(o1)) + status = fprintf (out, "%s", lua_getstring(o1)); + lua_pushnumber(status); + } + else /* formated */ + { + if (!lua_isstring(o2)) + { + lua_error ("incorrect format to function `write'"); + lua_pushnumber(0); + return; + } + lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1))); + } +} + +/* +** Execute a executable program using "system". +** Return the result of execution. +*/ +void io_execute (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL || !lua_isstring (o)) + { + lua_error ("incorrect argument to function 'execute`"); + lua_pushnumber (0); + } + else + { + int res = system(lua_getstring(o)); + lua_pushnumber (res); + } + return; +} + +/* +** Remove a file. +** On error put 0 on stack, otherwise put 1. +*/ +void io_remove (void) +{ + lua_Object o = lua_getparam (1); + if (o == NULL || !lua_isstring (o)) + { + lua_error ("incorrect argument to function 'execute`"); + lua_pushnumber (0); + } + else + { + if (remove(lua_getstring(o)) == 0) + lua_pushnumber (1); + else + lua_pushnumber (0); + } + return; +} + +/* +** Open io library +*/ +void iolib_open (void) +{ + lua_register ("readfrom", io_readfrom); + lua_register ("writeto", io_writeto); + lua_register ("appendto", io_appendto); + lua_register ("read", io_read); + lua_register ("write", io_write); + lua_register ("execute", io_execute); + lua_register ("remove", io_remove); +} diff --git a/clients/lib/mathlib.c b/clients/lib/mathlib.c new file mode 100644 index 00000000..c84af8cb --- /dev/null +++ b/clients/lib/mathlib.c @@ -0,0 +1,235 @@ +/* +** mathlib.c +** Mathematics library to LUA +*/ + +char *rcs_mathlib="$Id: mathlib.c,v 1.1 1993/12/17 18:41:19 celes Exp $"; + +#include <stdio.h> /* NULL */ +#include <math.h> + +#include "lua.h" + +#define TODEGREE(a) ((a)*180.0/3.14159) +#define TORAD(a) ((a)*3.14159/180.0) + +static void math_abs (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `abs'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `abs'"); return; } + d = lua_getnumber(o); + if (d < 0) d = -d; + lua_pushnumber (d); +} + + +static void math_sin (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `sin'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `sin'"); return; } + d = lua_getnumber(o); + lua_pushnumber (sin(TORAD(d))); +} + + + +static void math_cos (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `cos'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `cos'"); return; } + d = lua_getnumber(o); + lua_pushnumber (cos(TORAD(d))); +} + + + +static void math_tan (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `tan'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `tan'"); return; } + d = lua_getnumber(o); + lua_pushnumber (tan(TORAD(d))); +} + + +static void math_asin (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `asin'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `asin'"); return; } + d = lua_getnumber(o); + lua_pushnumber (TODEGREE(asin(d))); +} + + +static void math_acos (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `acos'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `acos'"); return; } + d = lua_getnumber(o); + lua_pushnumber (TODEGREE(acos(d))); +} + + + +static void math_atan (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `atan'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `atan'"); return; } + d = lua_getnumber(o); + lua_pushnumber (TODEGREE(atan(d))); +} + + +static void math_ceil (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `ceil'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `ceil'"); return; } + d = lua_getnumber(o); + lua_pushnumber (ceil(d)); +} + + +static void math_floor (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `floor'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `floor'"); return; } + d = lua_getnumber(o); + lua_pushnumber (floor(d)); +} + +static void math_mod (void) +{ + int d1, d2; + lua_Object o1 = lua_getparam (1); + lua_Object o2 = lua_getparam (2); + if (!lua_isnumber(o1) || !lua_isnumber(o2)) + { lua_error ("incorrect arguments to function `mod'"); return; } + d1 = (int) lua_getnumber(o1); + d2 = (int) lua_getnumber(o2); + lua_pushnumber (d1%d2); +} + + +static void math_sqrt (void) +{ + double d; + lua_Object o = lua_getparam (1); + if (o == NULL) + { lua_error ("too few arguments to function `sqrt'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `sqrt'"); return; } + d = lua_getnumber(o); + lua_pushnumber (sqrt(d)); +} + +static void math_pow (void) +{ + double d1, d2; + lua_Object o1 = lua_getparam (1); + lua_Object o2 = lua_getparam (2); + if (!lua_isnumber(o1) || !lua_isnumber(o2)) + { lua_error ("incorrect arguments to function `pow'"); return; } + d1 = lua_getnumber(o1); + d2 = lua_getnumber(o2); + lua_pushnumber (pow(d1,d2)); +} + +static void math_min (void) +{ + int i=1; + double d, dmin; + lua_Object o; + if ((o = lua_getparam(i++)) == NULL) + { lua_error ("too few arguments to function `min'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `min'"); return; } + dmin = lua_getnumber (o); + while ((o = lua_getparam(i++)) != NULL) + { + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `min'"); return; } + d = lua_getnumber (o); + if (d < dmin) dmin = d; + } + lua_pushnumber (dmin); +} + + +static void math_max (void) +{ + int i=1; + double d, dmax; + lua_Object o; + if ((o = lua_getparam(i++)) == NULL) + { lua_error ("too few arguments to function `max'"); return; } + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `max'"); return; } + dmax = lua_getnumber (o); + while ((o = lua_getparam(i++)) != NULL) + { + if (!lua_isnumber(o)) + { lua_error ("incorrect arguments to function `max'"); return; } + d = lua_getnumber (o); + if (d > dmax) dmax = d; + } + lua_pushnumber (dmax); +} + + + +/* +** Open math library +*/ +void mathlib_open (void) +{ + lua_register ("abs", math_abs); + lua_register ("sin", math_sin); + lua_register ("cos", math_cos); + lua_register ("tan", math_tan); + lua_register ("asin", math_asin); + lua_register ("acos", math_acos); + lua_register ("atan", math_atan); + lua_register ("ceil", math_ceil); + lua_register ("floor", math_floor); + lua_register ("mod", math_mod); + lua_register ("sqrt", math_sqrt); + lua_register ("pow", math_pow); + lua_register ("min", math_min); + lua_register ("max", math_max); +} diff --git a/clients/lib/mathlib.h b/clients/lib/mathlib.h new file mode 100644 index 00000000..bdf2b4b8 --- /dev/null +++ b/clients/lib/mathlib.h @@ -0,0 +1,13 @@ +/* +** Math library to LUA +** TeCGraf - PUC-Rio +** $Id: mathlib.h,v 1.1 1993/12/17 18:41:19 celes Exp $ +*/ + + +#ifndef strlib_h + +void mathlib_open (void); + +#endif + diff --git a/clients/lib/strlib.c b/clients/lib/strlib.c new file mode 100644 index 00000000..037b84be --- /dev/null +++ b/clients/lib/strlib.c @@ -0,0 +1,135 @@ +/* +** strlib.c +** String library to LUA +*/ + +char *rcs_strlib="$Id: strlib.c,v 1.2 1994/03/28 15:14:02 celes Exp $"; + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "mm.h" + + +#include "lua.h" + +/* +** Return the position of the first caracter of a substring into a string +** LUA interface: +** n = strfind (string, substring) +*/ +static void str_find (void) +{ + char *s1, *s2, *f; + lua_Object o1 = lua_getparam (1); + lua_Object o2 = lua_getparam (2); + if (!lua_isstring(o1) || !lua_isstring(o2)) + { lua_error ("incorrect arguments to function `strfind'"); return; } + s1 = lua_getstring(o1); + s2 = lua_getstring(o2); + f = strstr(s1,s2); + if (f != NULL) + lua_pushnumber (f-s1+1); + else + lua_pushnil(); +} + +/* +** Return the string length +** LUA interface: +** n = strlen (string) +*/ +static void str_len (void) +{ + lua_Object o = lua_getparam (1); + if (!lua_isstring(o)) + { lua_error ("incorrect arguments to function `strlen'"); return; } + lua_pushnumber(strlen(lua_getstring(o))); +} + + +/* +** Return the substring of a string, from start to end +** LUA interface: +** substring = strsub (string, start, end) +*/ +static void str_sub (void) +{ + int start, end; + char *s; + lua_Object o1 = lua_getparam (1); + lua_Object o2 = lua_getparam (2); + lua_Object o3 = lua_getparam (3); + if (!lua_isstring(o1) || !lua_isnumber(o2)) + { lua_error ("incorrect arguments to function `strsub'"); return; } + if (o3 != NULL && !lua_isnumber(o3)) + { lua_error ("incorrect third argument to function `strsub'"); return; } + s = lua_copystring(o1); + start = lua_getnumber (o2); + end = o3 == NULL ? strlen(s) : lua_getnumber (o3); + if (end < start || start < 1 || end > strlen(s)) + lua_pushstring(""); + else + { + s[end] = 0; + lua_pushstring (&s[start-1]); + } + free (s); +} + +/* +** Convert a string to lower case. +** LUA interface: +** lowercase = strlower (string) +*/ +static void str_lower (void) +{ + char *s, *c; + lua_Object o = lua_getparam (1); + if (!lua_isstring(o)) + { lua_error ("incorrect arguments to function `strlower'"); return; } + c = s = strdup(lua_getstring(o)); + while (*c != 0) + { + *c = tolower(*c); + c++; + } + lua_pushstring(s); + free(s); +} + + +/* +** Convert a string to upper case. +** LUA interface: +** uppercase = strupper (string) +*/ +static void str_upper (void) +{ + char *s, *c; + lua_Object o = lua_getparam (1); + if (!lua_isstring(o)) + { lua_error ("incorrect arguments to function `strlower'"); return; } + c = s = strdup(lua_getstring(o)); + while (*c != 0) + { + *c = toupper(*c); + c++; + } + lua_pushstring(s); + free(s); +} + + +/* +** Open string library +*/ +void strlib_open (void) +{ + lua_register ("strfind", str_find); + lua_register ("strlen", str_len); + lua_register ("strsub", str_sub); + lua_register ("strlower", str_lower); + lua_register ("strupper", str_upper); +} diff --git a/clients/lib/strlib.h b/clients/lib/strlib.h new file mode 100644 index 00000000..daa1f603 --- /dev/null +++ b/clients/lib/strlib.h @@ -0,0 +1,13 @@ +/* +** String library to LUA +** TeCGraf - PUC-Rio +** $Id: strlib.h,v 1.1 1993/12/17 18:41:19 celes Exp $ +*/ + + +#ifndef strlib_h + +void strlib_open (void); + +#endif + diff --git a/clients/lua/Makefile b/clients/lua/Makefile new file mode 100644 index 00000000..7bb74208 --- /dev/null +++ b/clients/lua/Makefile @@ -0,0 +1,17 @@ +# makefile for lua interpreter + +BIN= $(LUA)/bin +INC= $(LUA)/include +LIB= $(LUA)/lib + +CC= gcc +CFLAGS= -g -Wall -O2 -I$(INC) + +OBJS= lua.o +T=$(BIN)/lua + +$T: $(OBJS) + $(CC) -o $@ $(OBJS) -L$(LIB) -llua -llualib -lm + +clean: + rm -f $T $(OBJS) diff --git a/clients/lua/README b/clients/lua/README new file mode 100644 index 00000000..c529892a --- /dev/null +++ b/clients/lua/README @@ -0,0 +1,5 @@ +This client is a simple lua interpreter. +It either loads and executes the files named on the command line or reads +and executes lines from stdin. +Note that, in the second case, each line must contain a complete statement. +For instance, functions cannot span several lines. diff --git a/clients/lua/lua.c b/clients/lua/lua.c new file mode 100644 index 00000000..c4b83cee --- /dev/null +++ b/clients/lua/lua.c @@ -0,0 +1,28 @@ +/* +** lua.c +** Linguagem para Usuarios de Aplicacao +*/ + +char *rcs_lua="$Id: lua.c,v 1.1 1993/12/17 18:41:19 celes Exp $"; + +#include <stdio.h> + +#include "lua.h" +#include "lualib.h" + +void main (int argc, char *argv[]) +{ + int i; + iolib_open (); + strlib_open (); + mathlib_open (); + if (argc < 2) + { + char buffer[2048]; + while (gets(buffer) != 0) + lua_dostring(buffer); + } + else + for (i=1; i<argc; i++) + lua_dofile (argv[i]); +} |