summaryrefslogtreecommitdiff
path: root/clients/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clients/lib')
-rw-r--r--clients/lib/Makefile20
-rw-r--r--clients/lib/iolib.c220
-rw-r--r--clients/lib/mathlib.c182
-rw-r--r--clients/lib/mathlib.h2
-rw-r--r--clients/lib/strlib.c72
-rw-r--r--clients/lib/strlib.h2
6 files changed, 379 insertions, 119 deletions
diff --git a/clients/lib/Makefile b/clients/lib/Makefile
index 4110a62f..b191b750 100644
--- a/clients/lib/Makefile
+++ b/clients/lib/Makefile
@@ -3,17 +3,26 @@
INC= $(LUA)/include
LIB= $(LUA)/lib
+# in SunOs /usr/5include contains prototypes for standard lib
+INCS= -I/usr/5include -I$(INC)
+WARN= -Wall -Wmissing-prototypes -Wshadow -ansi
+
CC= gcc
-CFLAGS= -Wall -O2 -I$(INC) $(DEFS)
+CFLAGS= $(INCS) $(DEFS) $(WARN) -O2
OBJS= iolib.o mathlib.o strlib.o
+
SLIB= $(LIB)/liblualib.a
-DLIB= $(LIB)/liblualib.so.1.1
-libs: $(SLIB) $(DLIB)
+# dynamic libraries only work for SunOs
+DLIB= $(LIB)/liblualib.so.2.1
+
+all: $(SLIB)
+
+dynamic: $(DLIB)
$(SLIB): $(OBJS)
- ar ruvl $@ $(OBJS)
+ ar rcuv $@ $(OBJS)
ranlib $(SLIB)
$(DLIB): $(OBJS)
@@ -21,3 +30,6 @@ $(DLIB): $(OBJS)
clean:
rm -f $(OBJS) $(SLIB) $(DLIB)
+
+co:
+ co -M iolib.c mathlib.c mathlib.h strlib.c strlib.h
diff --git a/clients/lib/iolib.c b/clients/lib/iolib.c
index b972124b..bb3883ab 100644
--- a/clients/lib/iolib.c
+++ b/clients/lib/iolib.c
@@ -3,20 +3,18 @@
** Input/output library to LUA
*/
-char *rcs_iolib="$Id: iolib.c,v 1.4 1994/04/25 20:11:23 celes Exp $";
+char *rcs_iolib="$Id: iolib.c,v 1.21 1995/02/06 19:36:13 roberto Exp $";
-#include <stdlib.h>
-#include <string.h>
#include <stdio.h>
#include <ctype.h>
+#include <sys/types.h>
#include <sys/stat.h>
-#ifdef __GNUC__
-#include <floatingpoint.h>
-#endif
-
-#include "mm.h"
+#include <string.h>
+#include <time.h>
+#include <stdlib.h>
#include "lua.h"
+#include "lualib.h"
static FILE *in=stdin, *out=stdout;
@@ -31,7 +29,7 @@ static FILE *in=stdin, *out=stdout;
static void io_readfrom (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL) /* restore standart input */
+ if (o == LUA_NOOBJECT) /* restore standart input */
{
if (in != stdin)
{
@@ -76,7 +74,7 @@ static void io_readfrom (void)
static void io_writeto (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL) /* restore standart output */
+ if (o == LUA_NOOBJECT) /* restore standart output */
{
if (out != stdout)
{
@@ -122,7 +120,7 @@ static void io_writeto (void)
static void io_appendto (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL) /* restore standart output */
+ if (o == LUA_NOOBJECT) /* restore standart output */
{
if (out != stdout)
{
@@ -179,7 +177,7 @@ static void io_appendto (void)
static void io_read (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL || !lua_isstring(o)) /* free format */
+ if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
{
int c;
char s[256];
@@ -187,7 +185,7 @@ static void io_read (void)
;
if (c == '\"')
{
- int c, n=0;
+ int n=0;
while((c = fgetc(in)) != '\"')
{
if (c == EOF)
@@ -201,7 +199,7 @@ static void io_read (void)
}
else if (c == '\'')
{
- int c, n=0;
+ int n=0;
while((c = fgetc(in)) != '\'')
{
if (c == EOF)
@@ -215,7 +213,6 @@ static void io_read (void)
}
else
{
- char *ptr;
double d;
ungetc (c, in);
if (fscanf (in, "%s", s) != 1)
@@ -223,8 +220,7 @@ static void io_read (void)
lua_pushnil ();
return;
}
- d = strtod (s, &ptr);
- if (!(*ptr))
+ if (sscanf(s, "%lf %*c", &d) == 1)
{
lua_pushnumber (d);
return;
@@ -269,9 +265,9 @@ static void io_read (void)
break;
case 'f': case 'g': case 'e':
{
- float f;
- sscanf (s, "%f", &f);
- lua_pushnumber(f);
+ float fl;
+ sscanf (s, "%f", &fl);
+ lua_pushnumber(fl);
}
break;
default:
@@ -314,6 +310,38 @@ static void io_read (void)
/*
+** Read characters until a given one. The delimiter is not read.
+*/
+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;
+ }
+ if (c != EOF) ungetc(c,in);
+ s[m] = 0;
+ lua_pushstring(s);
+ free(s);
+}
+
+
+
+/*
** Write a variable. On error put 0 on stack, otherwise put 1.
** LUA interface:
** status = write (variable [,format])
@@ -341,39 +369,49 @@ static void io_read (void)
*/
static char *buildformat (char *e, lua_Object o)
{
- static char buffer[512];
+ 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=0, l;
+ 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');
- e++; /* skip point */
+ if (*e == '.') e++; /* skip point */
while (isdigit(*e))
- n = n*10 + (*e++ - '0');
+ 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);
- sprintf(strchr(f,0), "%c", t);
- switch (tolower(t))
+ if (m > 0) sprintf(strchr(f,0),"%d", m);
+ if (n >= 0) sprintf(strchr(f,0),".%d", n);
+ switch (t)
{
- case 'i': t = 'i';
+ 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': t = 'f';
+ 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 's': t = 's';
+ 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;
@@ -383,25 +421,34 @@ static char *buildformat (char *e, lua_Object o)
}
else if (m!=0 && j=='|')
{
+ int k;
int i=l-1;
- while (isspace(string[i])) i--;
- string -= (m-i) / 2;
- i=0;
- while (string[i]==0) string[i++] = ' ';
- string[l] = 0;
+ 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;
}
static void io_write (void)
{
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
- if (o1 == NULL) /* new line */
+ if (o1 == LUA_NOOBJECT) /* new line */
{
fprintf (out, "\n");
lua_pushnumber(1);
}
- else if (o2 == NULL) /* free format */
+ else if (o2 == LUA_NOOBJECT) /* free format */
{
int status=0;
if (lua_isnumber(o1))
@@ -426,10 +473,10 @@ static void io_write (void)
** Execute a executable program using "system".
** Return the result of execution.
*/
-void io_execute (void)
+static void io_execute (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL || !lua_isstring (o))
+ if (o == LUA_NOOBJECT || !lua_isstring (o))
{
lua_error ("incorrect argument to function 'execute`");
lua_pushnumber (0);
@@ -446,10 +493,10 @@ void io_execute (void)
** Remove a file.
** On error put 0 on stack, otherwise put 1.
*/
-void io_remove (void)
+static void io_remove (void)
{
lua_Object o = lua_getparam (1);
- if (o == NULL || !lua_isstring (o))
+ if (o == LUA_NOOBJECT || !lua_isstring (o))
{
lua_error ("incorrect argument to function 'execute`");
lua_pushnumber (0);
@@ -464,6 +511,88 @@ void io_remove (void)
return;
}
+
+/*
+** To get a environment variables
+*/
+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);
+ }
+}
+
+/*
+** Return time: hour, min, sec
+*/
+static void io_time (void)
+{
+ time_t t;
+ struct tm *s;
+
+ time(&t);
+ s = localtime(&t);
+ lua_pushnumber(s->tm_hour);
+ lua_pushnumber(s->tm_min);
+ lua_pushnumber(s->tm_sec);
+}
+
+/*
+** Return date: dd, mm, yyyy
+*/
+static void io_date (void)
+{
+ time_t t;
+ struct tm *s;
+
+ time(&t);
+ s = localtime(&t);
+ lua_pushnumber(s->tm_mday);
+ lua_pushnumber(s->tm_mon+1);
+ lua_pushnumber(s->tm_year+1900);
+}
+
+/*
+** Beep
+*/
+static void io_beep (void)
+{
+ printf("\a");
+}
+
+/*
+** To exit
+*/
+static void io_exit (void)
+{
+ lua_Object o = lua_getparam(1);
+ if (lua_isstring(o))
+ printf("%s\n", lua_getstring(o));
+ exit(1);
+}
+
+/*
+** To debug a lua program. Start a dialog with the user, interpreting
+ lua commands until an 'cont'.
+*/
+static void io_debug (void)
+{
+ while (1)
+ {
+ char buffer[250];
+ fprintf(stderr, "lua_debug> ");
+ if (gets(buffer) == 0) return;
+ if (strcmp(buffer, "cont") == 0) return;
+ lua_dostring(buffer);
+ }
+}
+
/*
** Open io library
*/
@@ -473,7 +602,14 @@ void iolib_open (void)
lua_register ("writeto", io_writeto);
lua_register ("appendto", io_appendto);
lua_register ("read", io_read);
+ lua_register ("readuntil",io_readuntil);
lua_register ("write", io_write);
lua_register ("execute", io_execute);
lua_register ("remove", io_remove);
+ lua_register ("getenv", io_getenv);
+ lua_register ("time", io_time);
+ lua_register ("date", io_date);
+ lua_register ("beep", io_beep);
+ lua_register ("exit", io_exit);
+ lua_register ("debug", io_debug);
}
diff --git a/clients/lib/mathlib.c b/clients/lib/mathlib.c
index c84af8cb..ca5ab780 100644
--- a/clients/lib/mathlib.c
+++ b/clients/lib/mathlib.c
@@ -3,24 +3,26 @@
** Mathematics library to LUA
*/
-char *rcs_mathlib="$Id: mathlib.c,v 1.1 1993/12/17 18:41:19 celes Exp $";
+char *rcs_mathlib="$Id: mathlib.c,v 1.9 1995/02/06 19:36:43 roberto Exp $";
#include <stdio.h> /* NULL */
#include <math.h>
+#include "lualib.h"
#include "lua.h"
-#define TODEGREE(a) ((a)*180.0/3.14159)
-#define TORAD(a) ((a)*3.14159/180.0)
+#define PI 3.14159265358979323846
+#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 == NULL)
- { lua_error ("too few arguments to function `abs'"); return; }
+ if (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `abs'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `abs'"); return; }
+ lua_error ("incorrect arguments to function `abs'");
d = lua_getnumber(o);
if (d < 0) d = -d;
lua_pushnumber (d);
@@ -31,10 +33,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `sin'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `sin'"); return; }
+ lua_error ("incorrect arguments to function `sin'");
d = lua_getnumber(o);
lua_pushnumber (sin(TORAD(d)));
}
@@ -45,10 +47,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `cos'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `cos'"); return; }
+ lua_error ("incorrect arguments to function `cos'");
d = lua_getnumber(o);
lua_pushnumber (cos(TORAD(d)));
}
@@ -59,10 +61,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `tan'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `tan'"); return; }
+ lua_error ("incorrect arguments to function `tan'");
d = lua_getnumber(o);
lua_pushnumber (tan(TORAD(d)));
}
@@ -72,10 +74,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `asin'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `asin'"); return; }
+ lua_error ("incorrect arguments to function `asin'");
d = lua_getnumber(o);
lua_pushnumber (TODEGREE(asin(d)));
}
@@ -85,10 +87,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `acos'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `acos'"); return; }
+ lua_error ("incorrect arguments to function `acos'");
d = lua_getnumber(o);
lua_pushnumber (TODEGREE(acos(d)));
}
@@ -99,10 +101,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `atan'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `atan'"); return; }
+ lua_error ("incorrect arguments to function `atan'");
d = lua_getnumber(o);
lua_pushnumber (TODEGREE(atan(d)));
}
@@ -112,10 +114,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `ceil'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `ceil'"); return; }
+ lua_error ("incorrect arguments to function `ceil'");
d = lua_getnumber(o);
lua_pushnumber (ceil(d));
}
@@ -125,10 +127,10 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `floor'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `floor'"); return; }
+ lua_error ("incorrect arguments to function `floor'");
d = lua_getnumber(o);
lua_pushnumber (floor(d));
}
@@ -139,7 +141,7 @@ static void math_mod (void)
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; }
+ lua_error ("incorrect arguments to function `mod'");
d1 = (int) lua_getnumber(o1);
d2 = (int) lua_getnumber(o2);
lua_pushnumber (d1%d2);
@@ -150,24 +152,36 @@ 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 (o == LUA_NOOBJECT)
+ lua_error ("too few arguments to function `sqrt'");
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `sqrt'"); return; }
+ lua_error ("incorrect arguments to function `sqrt'");
d = lua_getnumber(o);
lua_pushnumber (sqrt(d));
}
+static int old_pow;
+
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));
+ lua_Object op = lua_getparam(3);
+ if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
+ {
+ lua_Object old = lua_getlocked(old_pow);
+ lua_pushobject(o1);
+ lua_pushobject(o2);
+ lua_pushobject(op);
+ if (lua_callfunction(old) != 0)
+ lua_error(NULL);
+ }
+ else
+ {
+ double d1 = lua_getnumber(o1);
+ double d2 = lua_getnumber(o2);
+ lua_pushnumber (pow(d1,d2));
+ }
}
static void math_min (void)
@@ -175,15 +189,15 @@ 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 ((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'"); return; }
+ lua_error ("incorrect arguments to function `min'");
dmin = lua_getnumber (o);
- while ((o = lua_getparam(i++)) != NULL)
+ while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
{
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `min'"); return; }
+ lua_error ("incorrect arguments to function `min'");
d = lua_getnumber (o);
if (d < dmin) dmin = d;
}
@@ -196,15 +210,15 @@ 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 ((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'"); return; }
+ lua_error ("incorrect arguments to function `max'");
dmax = lua_getnumber (o);
- while ((o = lua_getparam(i++)) != NULL)
+ while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
{
if (!lua_isnumber(o))
- { lua_error ("incorrect arguments to function `max'"); return; }
+ lua_error ("incorrect arguments to function `max'");
d = lua_getnumber (o);
if (d > dmax) dmax = d;
}
@@ -212,6 +226,67 @@ static void math_max (void)
}
+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);
+ 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);
+ 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);
+ 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);
+ 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);
+ lua_pushnumber (d/180.*PI);
+}
/*
** Open math library
@@ -229,7 +304,12 @@ void mathlib_open (void)
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);
+ lua_register ("log", math_log);
+ lua_register ("log10", math_log10);
+ lua_register ("exp", math_exp);
+ lua_register ("deg", math_deg);
+ lua_register ("rad", math_rad);
+ old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
}
diff --git a/clients/lib/mathlib.h b/clients/lib/mathlib.h
index bdf2b4b8..7630144a 100644
--- a/clients/lib/mathlib.h
+++ b/clients/lib/mathlib.h
@@ -1,7 +1,7 @@
/*
** Math library to LUA
** TeCGraf - PUC-Rio
-** $Id: mathlib.h,v 1.1 1993/12/17 18:41:19 celes Exp $
+** $Id: mathlib.h,v 1.1 1993/12/17 18:41:19 celes Stab $
*/
diff --git a/clients/lib/strlib.c b/clients/lib/strlib.c
index 037b84be..6b92a30b 100644
--- a/clients/lib/strlib.c
+++ b/clients/lib/strlib.c
@@ -3,34 +3,66 @@
** String library to LUA
*/
-char *rcs_strlib="$Id: strlib.c,v 1.2 1994/03/28 15:14:02 celes Exp $";
+char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
-#include <stdlib.h>
#include <string.h>
+#include <stdlib.h>
#include <ctype.h>
-#include "mm.h"
+#include "lua.h"
+#include "lualib.h"
-#include "lua.h"
+static char *newstring (lua_Object o)
+{
+ char *s = lua_getstring(o);
+ char *ns = (char *)malloc(strlen(s)+1);
+ if (ns == 0)
+ lua_error("not enough memory for new string");
+ strcpy(ns, s);
+ return ns;
+}
+
/*
** Return the position of the first caracter of a substring into a string
** LUA interface:
-** n = strfind (string, substring)
+** n = strfind (string, substring, init, end)
*/
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'"); return; }
+ 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,s2);
+ f = strstr(s1+init,s2);
if (f != NULL)
- lua_pushnumber (f-s1+1);
+ {
+ int pos = f-s1+1;
+ if (o4 == 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)
+ lua_pushnumber (pos);
+ else
+ lua_pushnil();
+ }
else
lua_pushnil();
}
@@ -44,7 +76,7 @@ static void str_len (void)
{
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
- { lua_error ("incorrect arguments to function `strlen'"); return; }
+ lua_error ("incorrect arguments to function `strlen'");
lua_pushnumber(strlen(lua_getstring(o)));
}
@@ -62,20 +94,20 @@ static void str_sub (void)
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);
+ 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 == NULL ? strlen(s) : lua_getnumber (o3);
+ end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
if (end < start || start < 1 || end > strlen(s))
- lua_pushstring("");
+ lua_pushliteral("");
else
{
s[end] = 0;
lua_pushstring (&s[start-1]);
}
- free (s);
+ free(s);
}
/*
@@ -88,8 +120,8 @@ 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));
+ lua_error ("incorrect arguments to function `strlower'");
+ c = s = newstring(o);
while (*c != 0)
{
*c = tolower(*c);
@@ -110,8 +142,8 @@ 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));
+ lua_error ("incorrect arguments to function `strlower'");
+ c = s = newstring(o);
while (*c != 0)
{
*c = toupper(*c);
diff --git a/clients/lib/strlib.h b/clients/lib/strlib.h
index daa1f603..db3d38c5 100644
--- a/clients/lib/strlib.h
+++ b/clients/lib/strlib.h
@@ -1,7 +1,7 @@
/*
** String library to LUA
** TeCGraf - PUC-Rio
-** $Id: strlib.h,v 1.1 1993/12/17 18:41:19 celes Exp $
+** $Id: strlib.h,v 1.1 1993/12/17 18:41:19 celes Stab $
*/