diff options
Diffstat (limited to 'clients/lib')
| -rw-r--r-- | clients/lib/Makefile | 11 | ||||
| -rw-r--r-- | clients/lib/iolib.c | 695 | ||||
| -rw-r--r-- | clients/lib/mathlib.c | 173 | ||||
| -rw-r--r-- | clients/lib/strlib.c | 159 |
4 files changed, 487 insertions, 551 deletions
diff --git a/clients/lib/Makefile b/clients/lib/Makefile index b191b750..70750979 100644 --- a/clients/lib/Makefile +++ b/clients/lib/Makefile @@ -7,15 +7,19 @@ LIB= $(LUA)/lib INCS= -I/usr/5include -I$(INC) WARN= -Wall -Wmissing-prototypes -Wshadow -ansi +# if your system does not have popen, remove -DPOPEN from the line below +DEFS= -DPOPEN + CC= gcc CFLAGS= $(INCS) $(DEFS) $(WARN) -O2 OBJS= iolib.o mathlib.o strlib.o +SRCS= iolib.c mathlib.c mathlib.h strlib.c strlib.h SLIB= $(LIB)/liblualib.a # dynamic libraries only work for SunOs -DLIB= $(LIB)/liblualib.so.2.1 +DLIB= $(LIB)/liblualib.so.$(VERSION) all: $(SLIB) @@ -32,4 +36,7 @@ clean: rm -f $(OBJS) $(SLIB) $(DLIB) co: - co -M iolib.c mathlib.c mathlib.h strlib.c strlib.h + co -f -M $(SRCS) + +klean: clean + rm -f $(SRCS) diff --git a/clients/lib/iolib.c b/clients/lib/iolib.c index bb3883ab..01f8b553 100644 --- a/clients/lib/iolib.c +++ b/clients/lib/iolib.c @@ -3,7 +3,7 @@ ** Input/output library to LUA */ -char *rcs_iolib="$Id: iolib.c,v 1.21 1995/02/06 19:36:13 roberto Exp $"; +char *rcs_iolib="$Id: iolib.c,v 1.29 1995/11/10 18:32:59 roberto Exp $"; #include <stdio.h> #include <ctype.h> @@ -14,51 +14,68 @@ char *rcs_iolib="$Id: iolib.c,v 1.21 1995/02/06 19:36:13 roberto Exp $"; #include <stdlib.h> #include "lua.h" +#include "luadebug.h" #include "lualib.h" static FILE *in=stdin, *out=stdout; + +#ifdef POPEN +FILE *popen(); +int pclose(); +#else +#define popen(x,y) NULL /* that is, popen always fails */ +#define pclose(x) (-1) +#endif + + +static void closeread (void) +{ + if (in != stdin) + { + if (pclose(in) == -1) + fclose(in); + in = stdin; + } +} + +static void closewrite (void) +{ + if (out != stdout) + { + if (pclose(out) == -1) + fclose(out); + out = stdout; + } +} + /* ** Open a file to read. ** LUA interface: ** status = readfrom (filename) ** where: ** status = 1 -> success -** status = 0 -> error +** status = nil -> error */ static void io_readfrom (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) /* restore standart input */ - { - if (in != stdin) - { - fclose (in); - in = stdin; - } + if (lua_getparam (1) == LUA_NOOBJECT) + { /* restore standart input */ + closeread(); 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"); + char *s = lua_check_string(1, "readfrom"); + FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); if (fp == NULL) - { - lua_pushnumber (0); - } + lua_pushnil(); else { - if (in != stdin) fclose (in); + closeread(); in = fp; lua_pushnumber (1); } - } } } @@ -69,41 +86,27 @@ static void io_readfrom (void) ** status = writeto (filename) ** where: ** status = 1 -> success -** status = 0 -> error +** status = nil -> error */ static void io_writeto (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) /* restore standart output */ + if (lua_getparam (1) == LUA_NOOBJECT) /* restore standart output */ { - if (out != stdout) - { - fclose (out); - out = stdout; - } + closewrite(); 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"); + char *s = lua_check_string(1, "writeto"); + FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); if (fp == NULL) - { - lua_pushnumber (0); - } + lua_pushnil(); else { - if (out != stdout) fclose (out); + closewrite(); out = fp; lua_pushnumber (1); } - } } } @@ -115,50 +118,90 @@ static void io_writeto (void) ** where: ** status = 2 -> success (already exist) ** status = 1 -> success (new file) -** status = 0 -> error +** status = nil -> error */ static void io_appendto (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) /* restore standart output */ + char *s = lua_check_string(1, "appendto"); + struct stat st; + int r = (stat(s, &st) == -1) ? 1 : 2; + FILE *fp = fopen (s, "a"); + if (fp == NULL) + lua_pushnil(); + else { - if (out != stdout) + if (out != stdout) fclose (out); + out = fp; + lua_pushnumber (r); + } +} + + +static char getformat (char *f, int *just, int *m, int *n) +{ + int t; + switch (*f++) { - fclose (out); - out = stdout; + case 's': case 'S': + t = 's'; + break; + case 'f': case 'F': case 'g': case 'G': case 'e': case 'E': + t = 'f'; + break; + case 'i': case 'I': + t = 'i'; + break; + default: + t = 0; /* to avoid compiler warnings */ + lua_arg_error("read/write (format)"); } - lua_pushnumber (1); - } - else - { - if (!lua_isstring (o)) + *just = (*f == '<' || *f == '>' || *f == '|') ? *f++ : '>'; + if (isdigit(*f)) { - lua_error ("incorrect argument to function 'appendto`"); - lua_pushnumber (0); + *m = 0; + while (isdigit(*f)) + *m = *m*10 + (*f++ - '0'); } else + *m = -1; + if (*f == '.') { - 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); - } + f++; /* skip point */ + *n = 0; + while (isdigit(*f)) + *n = *n*10 + (*f++ - '0'); } - } + else + *n = -1; + return t; } +static char *add_char (int c) +{ + static char *buff = NULL; + static int max = 0; + static int n = 0; + if (n >= max) + { + if (max == 0) + { + max = 100; + buff = (char *)malloc(max); + } + else + { + max *= 2; + buff = (char *)realloc(buff, max); + } + if (buff == NULL) + lua_error("memory overflow"); + } + buff[n++] = c; + if (c == 0) + n = 0; /* prepare for next string */ + return buff; +} /* ** Read a variable. On error put nil on stack. @@ -174,138 +217,105 @@ static void io_appendto (void) ** Estes especificadores podem vir seguidos de numero que representa ** o numero de campos a serem lidos. */ -static void io_read (void) + +static int read_until_char (int del) +{ + int c; + while((c = fgetc(in)) != EOF && c != del) + add_char(c); + return c; +} + +static int read_until_blank (void) +{ + int c; + while((c = fgetc(in)) != EOF && !isspace(c)) + add_char(c); + return c; +} + +static void read_m (int m) +{ + int c; + while (m-- && (c = fgetc(in)) != EOF) + add_char(c); +} + + +static void read_free (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */ - { int c; - char s[256]; while (isspace(c=fgetc(in))) ; - if (c == '\"') + if (c == EOF) { - int n=0; - while((c = fgetc(in)) != '\"') - { - if (c == EOF) - { - lua_pushnil (); - return; - } - s[n++] = c; - } - s[n] = 0; + lua_pushnil(); + return; } - else if (c == '\'') - { - int n=0; - while((c = fgetc(in)) != '\'') - { + if (c == '\"' || c == '\'') + { /* string */ + c = read_until_char(c); if (c == EOF) { - lua_pushnil (); - return; + add_char(0); /* to be ready for next time */ + lua_pushnil(); } - s[n++] = c; - } - s[n] = 0; + else + lua_pushstring(add_char(0)); } else { - double d; - ungetc (c, in); - if (fscanf (in, "%s", s) != 1) - { - lua_pushnil (); - return; - } - if (sscanf(s, "%lf %*c", &d) == 1) - { - 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 fl; - sscanf (s, "%f", &fl); - lua_pushnumber(fl); - } - break; - default: - lua_pushstring(s); - break; - } + double d; + char dummy; + char *s; + add_char(c); + read_until_blank(); + s = add_char(0); + if (sscanf(s, "%lf %c", &d, &dummy) == 1) + lua_pushnumber(d); + else + lua_pushstring(s); } - else +} + +static void io_read (void) +{ + lua_Object o = lua_getparam (1); + if (o == LUA_NOOBJECT) /* free format */ + read_free(); + else /* formatted */ { - switch (tolower(t)) - { - case 'i': + int m, dummy1, dummy2; + switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2)) { - 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); + case 's': + if (m < 0) + read_until_blank(); + else + read_m(m); + lua_pushstring(add_char(0)); + break; + + case 'i': /* can read as float, since it makes no difference to Lua */ + case 'f': + { + double d; + int result; + if (m < 0) + result = fscanf(in, "%lf", &d); + else + { + read_m(m); + result = sscanf(add_char(0), "%lf", &d); + } + if (result == 1) + lua_pushnumber(d); + else + lua_pushnil(); + break; + } } - break; - } } - } } @@ -314,33 +324,13 @@ static void io_read (void) */ static void io_readuntil (void) { - int n=255,m=0; - int c,d; - char *s; - lua_Object lo = lua_getparam(1); - if (!lua_isstring(lo)) - d = EOF; - else - d = *lua_getstring(lo); - - s = (char *)malloc(n+1); - while((c = fgetc(in)) != EOF && c != d) - { - if (m==n) - { - n *= 2; - s = (char *)realloc(s, n+1); - } - s[m++] = c; - } + int del = *lua_check_string(1, "readuntil"); + int c = read_until_char(del); if (c != EOF) ungetc(c,in); - s[m] = 0; - lua_pushstring(s); - free(s); + lua_pushstring(add_char(0)); } - /* ** Write a variable. On error put 0 on stack, otherwise put 1. ** LUA interface: @@ -367,106 +357,100 @@ static void io_readuntil (void) ** inteiros -> numero minimo de digitos ** string -> nao se aplica */ -static char *buildformat (char *e, lua_Object o) + +static int write_fill (int n, int c) { - static char buffer[2048]; - static char f[80]; - char *string = &buffer[255]; - char *fstart=e, *fspace, *send; - char t, j='r'; - int m=0, n=-1, l; - while (isspace(*e)) e++; - fspace = e; - t = *e++; - if (*e == '<' || *e == '|' || *e == '>') j = *e++; - while (isdigit(*e)) - m = m*10 + (*e++ - '0'); - if (*e == '.') e++; /* skip point */ - while (isdigit(*e)) - if (n < 0) n = (*e++ - '0'); - else 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); - switch (t) - { - case 'i': case 'I': t = 'd'; - sprintf(strchr(f,0), "%c", t); - sprintf (string, f, (long int)lua_getnumber(o)); - break; - case 'f': case 'g': case 'e': case 'G': case 'E': - sprintf(strchr(f,0), "%c", t); - sprintf (string, f, (float)lua_getnumber(o)); - break; - case 'F': t = 'f'; - sprintf(strchr(f,0), "%c", t); - sprintf (string, f, (float)lua_getnumber(o)); - break; - case 's': case 'S': t = 's'; - sprintf(strchr(f,0), "%c", t); - sprintf (string, f, lua_getstring(o)); - break; - default: return ""; - } - l = strlen(string); - send = string+l; - if (m!=0 && l>m) - { - int i; - for (i=0; i<m; i++) - string[i] = '*'; - string[i] = 0; - } - else if (m!=0 && j=='|') - { - int k; - int i=l-1; - while (isspace(string[i]) || string[i]==0) i--; - string -= (m-i)/2; - for(k=0; k<(m-i)/2; k++) - string[k] = ' '; - } - /* add space characteres */ - while (fspace != fstart) - { - string--; - fspace--; - *string = *fspace; - } - while (isspace(*e)) *send++ = *e++; - *send = 0; - return string; + while (n--) + if (fputc(c, out) == EOF) + return 0; + return 1; +} + +static int write_string (char *s, int just, int m) +{ + int status; + int l = strlen(s); + int pre; /* number of blanks before string */ + if (m < 0) m = l; + else if (l > m) + { + write_fill(m, '*'); + return 0; + } + pre = (just == '<') ? 0 : (just == '>') ? m-l : (m-l)/2; + status = write_fill(pre, ' '); + status = status && fprintf(out, "%s", s) >= 0; + status = status && write_fill(m-(l+pre), ' '); + return status; +} + +static int write_float (int just, int m, int n) +{ + char buffer[100]; + lua_Object p = lua_getparam(1); + float number; + if (!lua_isnumber(p)) return 0; + number = lua_getnumber(p); + if (n >= 0) + sprintf(buffer, "%.*f", n, number); + else + sprintf(buffer, "%g", number); + return write_string(buffer, just, m); } + + +static int write_int (int just, int m, int n) +{ + char buffer[100]; + lua_Object p = lua_getparam(1); + int number; + if (!lua_isnumber(p)) return 0; + number = (int)lua_getnumber(p); + if (n >= 0) + sprintf(buffer, "%.*d", n, number); + else + sprintf(buffer, "%d", number); + return write_string(buffer, just, m); +} + + static void io_write (void) { - lua_Object o1 = lua_getparam (1); - lua_Object o2 = lua_getparam (2); - if (o1 == LUA_NOOBJECT) /* new line */ - { - fprintf (out, "\n"); - lua_pushnumber(1); - } - else if (o2 == LUA_NOOBJECT) /* 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; + int status = 0; + if (lua_getparam (2) == LUA_NOOBJECT) /* free format */ + { + lua_Object o1 = lua_getparam(1); + if (lua_isnumber(o1)) + status = fprintf (out, "%g", lua_getnumber(o1)) >= 0; + else if (lua_isstring(o1)) + status = fprintf (out, "%s", lua_getstring(o1)) >= 0; } - lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1))); - } + else /* formated */ + { + int just, m, n; + switch (getformat (lua_check_string(2, "write"), &just, &m, &n)) + { + case 's': + { + lua_Object p = lua_getparam(1); + if (lua_isstring(p)) + status = write_string(lua_getstring(p), just, m); + else + status = 0; + break; + } + case 'f': + status = write_float(just, m, n); + break; + case 'i': + status = write_int(just, m, n); + break; + } + } + if (status) + lua_pushnumber(status); + else + lua_pushnil(); } /* @@ -475,40 +459,18 @@ static void io_write (void) */ static void io_execute (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT || !lua_isstring (o)) - { - lua_error ("incorrect argument to function 'execute`"); - lua_pushnumber (0); - } - else - { - int res = system(lua_getstring(o)); - lua_pushnumber (res); - } - return; + lua_pushnumber(system(lua_check_string(1, "execute"))); } /* -** Remove a file. -** On error put 0 on stack, otherwise put 1. +** Remove a file. On error return nil. */ static void io_remove (void) { - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT || !lua_isstring (o)) - { - lua_error ("incorrect argument to function 'execute`"); - lua_pushnumber (0); - } - else - { - if (remove(lua_getstring(o)) == 0) + if (remove(lua_check_string(1, "remove")) == 0) lua_pushnumber (1); - else - lua_pushnumber (0); - } - return; + else + lua_pushnil(); } @@ -517,15 +479,9 @@ static void io_remove (void) */ static void io_getenv (void) { - lua_Object s = lua_getparam(1); - if (!lua_isstring(s)) - lua_pushnil(); - else - { - char *env = getenv(lua_getstring(s)); - if (env == NULL) lua_pushnil(); - else lua_pushstring(env); - } + char *env = getenv(lua_check_string(1, "getenv")); + if (env == NULL) lua_pushnil(); + else lua_pushstring(env); } /* @@ -573,7 +529,7 @@ static void io_exit (void) { lua_Object o = lua_getparam(1); if (lua_isstring(o)) - printf("%s\n", lua_getstring(o)); + fprintf(stderr, "%s\n", lua_getstring(o)); exit(1); } @@ -593,6 +549,54 @@ static void io_debug (void) } } + +void lua_printstack (FILE *f) +{ + int level = 0; + lua_Object func; + fprintf(f, "Active Stack:\n"); + while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) + { + char *name; + int currentline; + fprintf(f, "\t"); + switch (*getobjname(func, &name)) + { + case 'g': + fprintf(f, "function %s", name); + break; + case 'f': + fprintf(f, "fallback %s", name); + break; + default: + { + char *filename; + int linedefined; + lua_funcinfo(func, &filename, &linedefined); + if (linedefined == 0) + fprintf(f, "main of %s", filename); + else if (linedefined < 0) + fprintf(f, "%s", filename); + else + fprintf(f, "function (%s:%d)", filename, linedefined); + } + } + if ((currentline = lua_currentline(func)) > 0) + fprintf(f, " at line %d", currentline); + fprintf(f, "\n"); + } +} + + +static void errorfb (void) +{ + lua_Object o = lua_getparam(1); + char *s = lua_isstring(o) ? lua_getstring(o) : "(no messsage)"; + fprintf(stderr, "lua: %s\n", s); + lua_printstack(stderr); +} + + /* ** Open io library */ @@ -612,4 +616,7 @@ void iolib_open (void) lua_register ("beep", io_beep); lua_register ("exit", io_exit); lua_register ("debug", io_debug); + lua_register ("print_stack", errorfb); + lua_setfallback("error", errorfb); } + diff --git a/clients/lib/mathlib.c b/clients/lib/mathlib.c index ca5ab780..3f1e57f6 100644 --- a/clients/lib/mathlib.c +++ b/clients/lib/mathlib.c @@ -3,7 +3,7 @@ ** Mathematics library to LUA */ -char *rcs_mathlib="$Id: mathlib.c,v 1.9 1995/02/06 19:36:43 roberto Exp $"; +char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp $"; #include <stdio.h> /* NULL */ #include <math.h> @@ -11,19 +11,15 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.9 1995/02/06 19:36:43 roberto Exp $"; #include "lualib.h" #include "lua.h" +#ifndef PI #define PI 3.14159265358979323846 +#endif #define TODEGREE(a) ((a)*180.0/PI) #define TORAD(a) ((a)*PI/180.0) static void math_abs (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `abs'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `abs'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "abs"); if (d < 0) d = -d; lua_pushnumber (d); } @@ -31,13 +27,7 @@ static void math_abs (void) static void math_sin (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `sin'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `sin'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "sin"); lua_pushnumber (sin(TORAD(d))); } @@ -45,13 +35,7 @@ static void math_sin (void) static void math_cos (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `cos'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `cos'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "cos"); lua_pushnumber (cos(TORAD(d))); } @@ -59,104 +43,64 @@ static void math_cos (void) static void math_tan (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `tan'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `tan'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "tan"); lua_pushnumber (tan(TORAD(d))); } static void math_asin (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `asin'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `asin'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "asin"); lua_pushnumber (TODEGREE(asin(d))); } static void math_acos (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `acos'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `acos'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "acos"); lua_pushnumber (TODEGREE(acos(d))); } - static void math_atan (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `atan'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `atan'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "atan"); lua_pushnumber (TODEGREE(atan(d))); } +static void math_atan2 (void) +{ + double d1 = lua_check_number(1, "atan2"); + double d2 = lua_check_number(2, "atan2"); + lua_pushnumber (TODEGREE(atan2(d1, d2))); +} + + static void math_ceil (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `ceil'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `ceil'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "ceil"); lua_pushnumber (ceil(d)); } static void math_floor (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `floor'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `floor'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "floor"); 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'"); - d1 = (int) lua_getnumber(o1); - d2 = (int) lua_getnumber(o2); + int d1 = (int)lua_check_number(1, "mod"); + int d2 = (int)lua_check_number(2, "mod"); lua_pushnumber (d1%d2); } static void math_sqrt (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `sqrt'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `sqrt'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "sqrt"); lua_pushnumber (sqrt(d)); } @@ -187,104 +131,56 @@ static void math_pow (void) static void math_min (void) { int i=1; - double d, dmin; - lua_Object o; - if ((o = lua_getparam(i++)) == LUA_NOOBJECT) - lua_error ("too few arguments to function `min'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `min'"); - dmin = lua_getnumber (o); - while ((o = lua_getparam(i++)) != LUA_NOOBJECT) + double dmin = lua_check_number(i, "min"); + while (lua_getparam(++i) != LUA_NOOBJECT) { - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `min'"); - d = lua_getnumber (o); + double d = lua_check_number(i, "min"); 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++)) == LUA_NOOBJECT) - lua_error ("too few arguments to function `max'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `max'"); - dmax = lua_getnumber (o); - while ((o = lua_getparam(i++)) != LUA_NOOBJECT) + double dmax = lua_check_number(i, "max"); + while (lua_getparam(++i) != LUA_NOOBJECT) { - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `max'"); - d = lua_getnumber (o); + double d = lua_check_number(i, "max"); if (d > dmax) dmax = d; } lua_pushnumber (dmax); } - static void math_log (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `log'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `log'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "log"); lua_pushnumber (log(d)); } static void math_log10 (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `log10'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `log10'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "log10"); lua_pushnumber (log10(d)); } static void math_exp (void) { - double d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `exp'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `exp'"); - d = lua_getnumber(o); + double d = lua_check_number(1, "exp"); lua_pushnumber (exp(d)); } static void math_deg (void) { - float d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `deg'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `deg'"); - d = lua_getnumber(o); + float d = lua_check_number(1, "deg"); lua_pushnumber (d*180./PI); } static void math_rad (void) { - float d; - lua_Object o = lua_getparam (1); - if (o == LUA_NOOBJECT) - lua_error ("too few arguments to function `rad'"); - if (!lua_isnumber(o)) - lua_error ("incorrect arguments to function `rad'"); - d = lua_getnumber(o); + float d = lua_check_number(1, "rad"); lua_pushnumber (d/180.*PI); } @@ -300,6 +196,7 @@ void mathlib_open (void) lua_register ("asin", math_asin); lua_register ("acos", math_acos); lua_register ("atan", math_atan); + lua_register ("atan2", math_atan2); lua_register ("ceil", math_ceil); lua_register ("floor", math_floor); lua_register ("mod", math_mod); diff --git a/clients/lib/strlib.c b/clients/lib/strlib.c index 6b92a30b..0f37ab85 100644 --- a/clients/lib/strlib.c +++ b/clients/lib/strlib.c @@ -3,9 +3,10 @@ ** String library to LUA */ -char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; +char *rcs_strlib="$Id: strlib.c,v 1.14 1995/11/10 17:54:31 roberto Exp $"; #include <string.h> +#include <stdio.h> #include <stdlib.h> #include <ctype.h> @@ -13,9 +14,31 @@ char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; #include "lualib.h" -static char *newstring (lua_Object o) +void lua_arg_error(char *funcname) +{ + char buff[100]; + sprintf(buff, "incorrect arguments to function `%s'", funcname); + lua_error(buff); +} + +char *lua_check_string (int numArg, char *funcname) +{ + lua_Object o = lua_getparam(numArg); + if (!(lua_isstring(o) || lua_isnumber(o))) + lua_arg_error(funcname); + return lua_getstring(o); +} + +float lua_check_number (int numArg, char *funcname) +{ + lua_Object o = lua_getparam(numArg); + if (!lua_isnumber(o)) + lua_arg_error(funcname); + return lua_getnumber(o); +} + +static char *newstring (char *s) { - char *s = lua_getstring(o); char *ns = (char *)malloc(strlen(s)+1); if (ns == 0) lua_error("not enough memory for new string"); @@ -31,34 +54,17 @@ static char *newstring (lua_Object o) */ static void str_find (void) { - char *s1, *s2, *f; - int init; - lua_Object o1 = lua_getparam (1); - lua_Object o2 = lua_getparam (2); - lua_Object o3 = lua_getparam (3); - lua_Object o4 = lua_getparam (4); - if (!lua_isstring(o1) || !lua_isstring(o2)) - lua_error ("incorrect arguments to function `strfind'"); - if (o3 == LUA_NOOBJECT) - init = 0; - else if (lua_isnumber(o3)) - init = lua_getnumber(o3)-1; - else - { - lua_error ("incorrect arguments to function `strfind'"); - return; /* to avoid warnings */ - } - s1 = lua_getstring(o1); - s2 = lua_getstring(o2); - f = strstr(s1+init,s2); + char *s1 = lua_check_string(1, "strfind"); + char *s2 = lua_check_string(2, "strfind"); + int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : + (int)lua_check_number(3, "strfind")-1; + char *f = strstr(s1+init,s2); if (f != NULL) { int pos = f-s1+1; - if (o4 == LUA_NOOBJECT) + if (lua_getparam (4) == LUA_NOOBJECT) lua_pushnumber (pos); - else if (!lua_isnumber(o4)) - lua_error ("incorrect arguments to function `strfind'"); - else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1) + else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1) lua_pushnumber (pos); else lua_pushnil(); @@ -74,10 +80,8 @@ static void str_find (void) */ static void str_len (void) { - lua_Object o = lua_getparam (1); - if (!lua_isstring(o)) - lua_error ("incorrect arguments to function `strlen'"); - lua_pushnumber(strlen(lua_getstring(o))); + char *s = lua_check_string(1, "strlen"); + lua_pushnumber(strlen(s)); } @@ -88,48 +92,47 @@ static void str_len (void) */ 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'"); - if (o3 != LUA_NOOBJECT && !lua_isnumber(o3)) - lua_error ("incorrect third argument to function `strsub'"); - s = newstring(o1); - start = lua_getnumber (o2); - end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3); + char *s = lua_check_string(1, "strsub"); + int start = (int)lua_check_number(2, "strsub"); + int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : + (int)lua_check_number(3, "strsub"); if (end < start || start < 1 || end > strlen(s)) lua_pushliteral(""); else { + char temp = s[end]; s[end] = 0; lua_pushstring (&s[start-1]); + s[end] = temp; } - free(s); } /* -** Convert a string to lower case. -** LUA interface: -** lowercase = strlower (string) +** Convert a string according to given function. */ -static void str_lower (void) +typedef int (*strfunc)(int s); +static void str_apply (strfunc f, char *funcname) { char *s, *c; - lua_Object o = lua_getparam (1); - if (!lua_isstring(o)) - lua_error ("incorrect arguments to function `strlower'"); - c = s = newstring(o); + c = s = newstring(lua_check_string(1, funcname)); while (*c != 0) { - *c = tolower(*c); + *c = f(*c); c++; } lua_pushstring(s); free(s); -} +} + +/* +** Convert a string to lower case. +** LUA interface: +** lowercase = strlower (string) +*/ +static void str_lower (void) +{ + str_apply(tolower, "strlower"); +} /* @@ -139,20 +142,40 @@ static void str_lower (void) */ 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'"); - c = s = newstring(o); - while (*c != 0) - { - *c = toupper(*c); - c++; - } - lua_pushstring(s); - free(s); -} + str_apply(toupper, "strupper"); +} +/* +** get ascii value of a character in a string +*/ +static void str_ascii (void) +{ + char *s = lua_check_string(1, "ascii"); + lua_Object o2 = lua_getparam(2); + int pos; + pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1; + if (pos<0 || pos>=strlen(s)) + lua_arg_error("ascii"); + lua_pushnumber(s[pos]); +} + +/* +** converts one or more integers to chars in a string +*/ +#define maxparams 50 +static void str_int2str (void) +{ + char s[maxparams+1]; + int i = 0; + while (lua_getparam(++i) != LUA_NOOBJECT) + { + if (i > maxparams) + lua_error("too many parameters to function `int2str'"); + s[i-1] = (int)lua_check_number(i, "int2str"); + } + s[i-1] = 0; + lua_pushstring(s); +} /* ** Open string library @@ -164,4 +187,6 @@ void strlib_open (void) lua_register ("strsub", str_sub); lua_register ("strlower", str_lower); lua_register ("strupper", str_upper); + lua_register ("ascii", str_ascii); + lua_register ("int2str", str_int2str); } |
