summaryrefslogtreecommitdiff
path: root/clients
diff options
context:
space:
mode:
Diffstat (limited to 'clients')
-rw-r--r--clients/lib/Makefile13
-rw-r--r--clients/lib/iolib.c242
-rw-r--r--clients/lib/mathlib.c64
-rw-r--r--clients/lib/strlib.c210
-rw-r--r--clients/lua/Makefile13
-rw-r--r--clients/lua/lua.c14
6 files changed, 326 insertions, 230 deletions
diff --git a/clients/lib/Makefile b/clients/lib/Makefile
index 70750979..ffe2a63f 100644
--- a/clients/lib/Makefile
+++ b/clients/lib/Makefile
@@ -1,17 +1,10 @@
# makefile for lualib
-INC= $(LUA)/include
-LIB= $(LUA)/lib
+LUA= ../..
-# in SunOs /usr/5include contains prototypes for standard lib
-INCS= -I/usr/5include -I$(INC)
-WARN= -Wall -Wmissing-prototypes -Wshadow -ansi
+include $(LUA)/config
-# if your system does not have popen, remove -DPOPEN from the line below
-DEFS= -DPOPEN
-
-CC= gcc
-CFLAGS= $(INCS) $(DEFS) $(WARN) -O2
+EXTRA_DEFS= $(POPEN)
OBJS= iolib.o mathlib.o strlib.o
SRCS= iolib.c mathlib.c mathlib.h strlib.c strlib.h
diff --git a/clients/lib/iolib.c b/clients/lib/iolib.c
index 01f8b553..9ed7e479 100644
--- a/clients/lib/iolib.c
+++ b/clients/lib/iolib.c
@@ -3,15 +3,14 @@
** Input/output library to LUA
*/
-char *rcs_iolib="$Id: iolib.c,v 1.29 1995/11/10 18:32:59 roberto Exp $";
+char *rcs_iolib="$Id: iolib.c,v 1.44 1996/05/03 20:10:59 roberto Exp $";
#include <stdio.h>
#include <ctype.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
+#include <errno.h>
#include "lua.h"
#include "luadebug.h"
@@ -29,6 +28,14 @@ int pclose();
#endif
+static void pushresult (int i)
+{
+ if (i)
+ lua_pushnumber (1);
+ else
+ lua_pushnil();
+}
+
static void closeread (void)
{
if (in != stdin)
@@ -116,15 +123,12 @@ static void io_writeto (void)
** LUA interface:
** status = appendto (filename)
** where:
-** status = 2 -> success (already exist)
-** status = 1 -> success (new file)
+** status = 1 -> success
** status = nil -> error
*/
static void io_appendto (void)
{
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();
@@ -132,7 +136,7 @@ static void io_appendto (void)
{
if (out != stdout) fclose (out);
out = fp;
- lua_pushnumber (r);
+ lua_pushnumber(1);
}
}
@@ -142,15 +146,14 @@ static char getformat (char *f, int *just, int *m, int *n)
int t;
switch (*f++)
{
+ case 'q': case 'Q':
case 's': case 'S':
- t = 's';
+ case 'i': case 'I':
+ t = tolower(*(f-1));
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)");
@@ -177,32 +180,6 @@ static char getformat (char *f, int *just, int *m, int *n)
}
-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.
** LUA interface:
@@ -222,23 +199,23 @@ static int read_until_char (int del)
{
int c;
while((c = fgetc(in)) != EOF && c != del)
- add_char(c);
+ luaI_addchar(c);
return c;
}
-static int read_until_blank (void)
+static void read_until_blank (void)
{
int c;
while((c = fgetc(in)) != EOF && !isspace(c))
- add_char(c);
- return c;
+ luaI_addchar(c);
+ if (c != EOF) ungetc(c,in);
}
static void read_m (int m)
{
int c;
while (m-- && (c = fgetc(in)) != EOF)
- add_char(c);
+ luaI_addchar(c);
}
@@ -256,21 +233,18 @@ static void read_free (void)
{ /* string */
c = read_until_char(c);
if (c == EOF)
- {
- add_char(0); /* to be ready for next time */
lua_pushnil();
- }
else
- lua_pushstring(add_char(0));
+ lua_pushstring(luaI_addchar(0));
}
else
{
double d;
char dummy;
char *s;
- add_char(c);
+ luaI_addchar(c);
read_until_blank();
- s = add_char(0);
+ s = luaI_addchar(0);
if (sscanf(s, "%lf %c", &d, &dummy) == 1)
lua_pushnumber(d);
else
@@ -281,6 +255,7 @@ static void read_free (void)
static void io_read (void)
{
lua_Object o = lua_getparam (1);
+ luaI_addchar(0); /* initialize buffer */
if (o == LUA_NOOBJECT) /* free format */
read_free();
else /* formatted */
@@ -289,12 +264,19 @@ static void io_read (void)
switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2))
{
case 's':
+ {
+ char *s;
if (m < 0)
read_until_blank();
else
read_m(m);
- lua_pushstring(add_char(0));
+ s = luaI_addchar(0);
+ if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0))
+ lua_pushstring(s);
+ else
+ lua_pushnil();
break;
+ }
case 'i': /* can read as float, since it makes no difference to Lua */
case 'f':
@@ -306,7 +288,7 @@ static void io_read (void)
else
{
read_m(m);
- result = sscanf(add_char(0), "%lf", &d);
+ result = sscanf(luaI_addchar(0), "%lf", &d);
}
if (result == 1)
lua_pushnumber(d);
@@ -314,6 +296,8 @@ static void io_read (void)
lua_pushnil();
break;
}
+ default:
+ lua_arg_error("read (format)");
}
}
}
@@ -324,10 +308,16 @@ static void io_read (void)
*/
static void io_readuntil (void)
{
- int del = *lua_check_string(1, "readuntil");
- int c = read_until_char(del);
+ int del, c;
+ lua_Object p = lua_getparam(1);
+ luaI_addchar(0); /* initialize buffer */
+ if (p == LUA_NOOBJECT || lua_isnil(p))
+ del = EOF;
+ else
+ del = *lua_check_string(1, "readuntil");
+ c = read_until_char(del);
if (c != EOF) ungetc(c,in);
- lua_pushstring(add_char(0));
+ lua_pushstring(luaI_addchar(0));
}
@@ -384,6 +374,13 @@ static int write_string (char *s, int just, int m)
return status;
}
+static int write_quoted (int just, int m)
+{
+ luaI_addchar(0);
+ luaI_addquoted(lua_check_string(1, "write"));
+ return write_string(luaI_addchar(0), just, m);
+}
+
static int write_float (int just, int m, int n)
{
char buffer[100];
@@ -420,15 +417,16 @@ static void io_write (void)
if (lua_getparam (2) == LUA_NOOBJECT) /* free format */
{
lua_Object o1 = lua_getparam(1);
- if (lua_isnumber(o1))
+ int t = lua_type(o1);
+ if (t == LUA_T_NUMBER)
status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
- else if (lua_isstring(o1))
+ else if (t == LUA_T_STRING)
status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
}
else /* formated */
{
int just, m, n;
- switch (getformat (lua_check_string(2, "write"), &just, &m, &n))
+ switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
{
case 's':
{
@@ -439,6 +437,9 @@ static void io_write (void)
status = 0;
break;
}
+ case 'q':
+ status = write_quoted(just, m);
+ break;
case 'f':
status = write_float(just, m, n);
break;
@@ -467,59 +468,54 @@ static void io_execute (void)
*/
static void io_remove (void)
{
- if (remove(lua_check_string(1, "remove")) == 0)
- lua_pushnumber (1);
- else
- lua_pushnil();
+ pushresult(remove(lua_check_string(1, "remove")) == 0);
}
+static void io_rename (void)
+{
+ char *f1 = lua_check_string(1, "rename");
+ char *f2 = lua_check_string(2, "rename");
+ pushresult(rename(f1, f2) == 0);
+}
-/*
-** To get a environment variables
-*/
-static void io_getenv (void)
+static void io_tmpname (void)
{
- char *env = getenv(lua_check_string(1, "getenv"));
- if (env == NULL) lua_pushnil();
- else lua_pushstring(env);
+ lua_pushstring(tmpnam(NULL));
}
-/*
-** Return time: hour, min, sec
-*/
-static void io_time (void)
+static void io_errorno (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);
+/* lua_pushstring(strerror(errno));*/
}
-
+
+
/*
-** Return date: dd, mm, yyyy
+** To get a environment variable
*/
-static void io_date (void)
+static void io_getenv (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);
+ char *env = getenv(lua_check_string(1, "getenv"));
+ lua_pushstring(env); /* if NULL push nil */
}
-
+
/*
-** Beep
+** Return user formatted time stamp
*/
-static void io_beep (void)
+static void io_date (void)
{
- printf("\a");
+ time_t t;
+ struct tm *tm;
+ char *s;
+ char b[BUFSIZ];
+ if (lua_getparam(1) == LUA_NOOBJECT)
+ s = "%c";
+ else
+ s = lua_check_string(1, "date");
+ time(&t); tm = localtime(&t);
+ if (strftime(b,sizeof(b),s,tm))
+ lua_pushstring(b);
+ else
+ lua_error("invalid `date' format");
}
/*
@@ -528,9 +524,8 @@ static void io_beep (void)
static void io_exit (void)
{
lua_Object o = lua_getparam(1);
- if (lua_isstring(o))
- fprintf(stderr, "%s\n", lua_getstring(o));
- exit(1);
+ int code = lua_isnumber(o) ? (int)lua_getnumber(o) : 1;
+ exit(code);
}
/*
@@ -543,14 +538,14 @@ static void io_debug (void)
{
char buffer[250];
fprintf(stderr, "lua_debug> ");
- if (gets(buffer) == 0) return;
+ if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
if (strcmp(buffer, "cont") == 0) return;
lua_dostring(buffer);
}
}
-void lua_printstack (FILE *f)
+static void lua_printstack (FILE *f)
{
int level = 0;
lua_Object func;
@@ -560,13 +555,13 @@ void lua_printstack (FILE *f)
char *name;
int currentline;
fprintf(f, "\t");
- switch (*getobjname(func, &name))
+ switch (*lua_getobjname(func, &name))
{
case 'g':
fprintf(f, "function %s", name);
break;
case 'f':
- fprintf(f, "fallback %s", name);
+ fprintf(f, "`%s' fallback", name);
break;
default:
{
@@ -582,7 +577,7 @@ void lua_printstack (FILE *f)
}
}
if ((currentline = lua_currentline(func)) > 0)
- fprintf(f, " at line %d", currentline);
+ fprintf(f, " at line %d", currentline);
fprintf(f, "\n");
}
}
@@ -597,26 +592,27 @@ static void errorfb (void)
}
-/*
-** Open io library
-*/
+static struct lua_reg iolib[] = {
+{"readfrom", io_readfrom},
+{"writeto", io_writeto},
+{"appendto", io_appendto},
+{"read", io_read},
+{"readuntil",io_readuntil},
+{"write", io_write},
+{"execute", io_execute},
+{"remove", io_remove},
+{"rename", io_rename},
+{"tmpname", io_tmpname},
+{"ioerror", io_errorno},
+{"getenv", io_getenv},
+{"date", io_date},
+{"exit", io_exit},
+{"debug", io_debug},
+{"print_stack", errorfb}
+};
+
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 ("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);
- lua_register ("print_stack", errorfb);
- lua_setfallback("error", errorfb);
+ luaI_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
+ lua_setfallback("error", errorfb);
}
-
diff --git a/clients/lib/mathlib.c b/clients/lib/mathlib.c
index 3f1e57f6..07081e48 100644
--- a/clients/lib/mathlib.c
+++ b/clients/lib/mathlib.c
@@ -3,9 +3,9 @@
** Mathematics library to LUA
*/
-char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp $";
+char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp $";
-#include <stdio.h> /* NULL */
+#include <stdlib.h>
#include <math.h>
#include "lualib.h"
@@ -113,7 +113,7 @@ static void math_pow (void)
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_Object old = lua_getref(old_pow);
lua_pushobject(o1);
lua_pushobject(o2);
lua_pushobject(op);
@@ -184,29 +184,47 @@ static void math_rad (void)
lua_pushnumber (d/180.*PI);
}
+static void math_random (void)
+{
+ lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
+}
+
+static void math_randomseed (void)
+{
+ srand(lua_check_number(1, "randomseed"));
+}
+
+
+static struct lua_reg mathlib[] = {
+{"abs", math_abs},
+{"sin", math_sin},
+{"cos", math_cos},
+{"tan", math_tan},
+{"asin", math_asin},
+{"acos", math_acos},
+{"atan", math_atan},
+{"atan2", math_atan2},
+{"ceil", math_ceil},
+{"floor", math_floor},
+{"mod", math_mod},
+{"sqrt", math_sqrt},
+{"min", math_min},
+{"max", math_max},
+{"log", math_log},
+{"log10", math_log10},
+{"exp", math_exp},
+{"deg", math_deg},
+{"rad", math_rad},
+{"random", math_random},
+{"randomseed", math_randomseed}
+};
+
/*
** 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 ("atan2", math_atan2);
- lua_register ("ceil", math_ceil);
- lua_register ("floor", math_floor);
- lua_register ("mod", math_mod);
- lua_register ("sqrt", math_sqrt);
- 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));
+ luaI_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
+ old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1);
}
+
diff --git a/clients/lib/strlib.c b/clients/lib/strlib.c
index 0f37ab85..6a99871c 100644
--- a/clients/lib/strlib.c
+++ b/clients/lib/strlib.c
@@ -3,12 +3,13 @@
** String library to LUA
*/
-char *rcs_strlib="$Id: strlib.c,v 1.14 1995/11/10 17:54:31 roberto Exp $";
+char *rcs_strlib="$Id: strlib.c,v 1.23 1996/04/30 21:13:55 roberto Exp $";
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
+#include <limits.h>
#include "lua.h"
#include "lualib.h"
@@ -24,12 +25,12 @@ void lua_arg_error(char *funcname)
char *lua_check_string (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
- if (!(lua_isstring(o) || lua_isnumber(o)))
+ if (!lua_isstring(o))
lua_arg_error(funcname);
return lua_getstring(o);
}
-float lua_check_number (int numArg, char *funcname)
+double lua_check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
if (!lua_isnumber(o))
@@ -37,13 +38,36 @@ float lua_check_number (int numArg, char *funcname)
return lua_getnumber(o);
}
-static char *newstring (char *s)
+static int lua_opt_number (int numArg, int def, char *funcname)
{
- char *ns = (char *)malloc(strlen(s)+1);
- if (ns == 0)
- lua_error("not enough memory for new string");
- strcpy(ns, s);
- return ns;
+ return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
+ (int)lua_check_number(numArg, funcname);
+}
+
+char *luaI_addchar (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;
}
@@ -56,15 +80,12 @@ static void str_find (void)
{
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);
+ int init = lua_opt_number(3, 1, "strfind") - 1;
+ char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
if (f != NULL)
{
int pos = f-s1+1;
- if (lua_getparam (4) == LUA_NOOBJECT)
- lua_pushnumber (pos);
- else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
+ if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
lua_pushnumber (pos);
else
lua_pushnil();
@@ -94,34 +115,16 @@ static void str_sub (void)
{
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");
+ int end = lua_opt_number(3, strlen(s), "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;
- }
-}
-
-/*
-** Convert a string according to given function.
-*/
-typedef int (*strfunc)(int s);
-static void str_apply (strfunc f, char *funcname)
-{
- char *s, *c;
- c = s = newstring(lua_check_string(1, funcname));
- while (*c != 0)
- {
- *c = f(*c);
- c++;
+ luaI_addchar(0);
+ while (start <= end)
+ luaI_addchar(s[start++ - 1]);
+ lua_pushstring (luaI_addchar(0));
}
- lua_pushstring(s);
- free(s);
}
/*
@@ -131,7 +134,11 @@ static void str_apply (strfunc f, char *funcname)
*/
static void str_lower (void)
{
- str_apply(tolower, "strlower");
+ char *s = lua_check_string(1, "strlower");
+ luaI_addchar(0);
+ while (*s)
+ luaI_addchar(tolower(*s++));
+ lua_pushstring(luaI_addchar(0));
}
@@ -142,7 +149,11 @@ static void str_lower (void)
*/
static void str_upper (void)
{
- str_apply(toupper, "strupper");
+ char *s = lua_check_string(1, "strupper");
+ luaI_addchar(0);
+ while (*s)
+ luaI_addchar(toupper(*s++));
+ lua_pushstring(luaI_addchar(0));
}
/*
@@ -151,42 +162,115 @@ static void str_upper (void)
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;
+ int pos = lua_opt_number(2, 1, "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)
+void luaI_addquoted (char *s)
+{
+ luaI_addchar('"');
+ for (; *s; s++)
+ {
+ if (*s == '"' || *s == '\\' || *s == '\n')
+ luaI_addchar('\\');
+ luaI_addchar(*s);
+ }
+ luaI_addchar('"');
+}
+
+#define MAX_CONVERTION 2000
+#define MAX_FORMAT 50
+
+static void str_format (void)
{
- char s[maxparams+1];
- int i = 0;
- while (lua_getparam(++i) != LUA_NOOBJECT)
+ int arg = 1;
+ char *strfrmt = lua_check_string(arg++, "format");
+ luaI_addchar(0); /* initialize */
+ while (*strfrmt)
{
- if (i > maxparams)
- lua_error("too many parameters to function `int2str'");
- s[i-1] = (int)lua_check_number(i, "int2str");
+ if (*strfrmt != '%')
+ luaI_addchar(*strfrmt++);
+ else if (*++strfrmt == '%')
+ luaI_addchar(*strfrmt++); /* %% */
+ else
+ { /* format item */
+ char form[MAX_FORMAT]; /* store the format ('%...') */
+ char buff[MAX_CONVERTION]; /* store the formated value */
+ int size = 0;
+ int i = 0;
+ form[i++] = '%';
+ form[i] = *strfrmt++;
+ while (!isalpha(form[i]))
+ {
+ if (isdigit(form[i]))
+ {
+ size = size*10 + form[i]-'0';
+ if (size >= MAX_CONVERTION)
+ lua_error("format size/precision too long in function `format'");
+ }
+ else if (form[i] == '.')
+ size = 0; /* re-start */
+ if (++i >= MAX_FORMAT)
+ lua_error("bad format in function `format'");
+ form[i] = *strfrmt++;
+ }
+ form[i+1] = 0; /* ends string */
+ switch (form[i])
+ {
+ case 'q':
+ luaI_addquoted(lua_check_string(arg++, "format"));
+ buff[0] = '\0'; /* addchar already done */
+ break;
+ case 's':
+ {
+ char *s = lua_check_string(arg++, "format");
+ if (strlen(s) >= MAX_CONVERTION)
+ lua_error("string argument too long in function `format'");
+ sprintf(buff, form, s);
+ break;
+ }
+ case 'c': case 'd': case 'i': case 'o':
+ case 'u': case 'x': case 'X':
+ sprintf(buff, form, (int)lua_check_number(arg++, "format"));
+ break;
+ case 'e': case 'E': case 'f': case 'g':
+ sprintf(buff, form, lua_check_number(arg++, "format"));
+ break;
+ default: /* also treat cases 'pnLlh' */
+ lua_error("invalid format option in function `format'");
+ }
+ for (i=0; buff[i]; i++) /* move formated value to result */
+ luaI_addchar(buff[i]);
+ }
}
- s[i-1] = 0;
- lua_pushstring(s);
+ lua_pushstring(luaI_addchar(0)); /* push the result */
}
+
+void luaI_openlib (struct lua_reg *l, int n)
+{
+ int i;
+ for (i=0; i<n; i++)
+ lua_register(l[i].name, l[i].func);
+}
+
+static struct lua_reg strlib[] = {
+{"strfind", str_find},
+{"strlen", str_len},
+{"strsub", str_sub},
+{"strlower", str_lower},
+{"strupper", str_upper},
+{"ascii", str_ascii},
+{"format", str_format}
+};
+
+
/*
** 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);
- lua_register ("ascii", str_ascii);
- lua_register ("int2str", str_int2str);
+ luaI_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
}
diff --git a/clients/lua/Makefile b/clients/lua/Makefile
index b76dae3c..7bfde7fe 100644
--- a/clients/lua/Makefile
+++ b/clients/lua/Makefile
@@ -1,21 +1,18 @@
# makefile for lua interpreter
-BIN= $(LUA)/bin
-INC= $(LUA)/include
-LIB= $(LUA)/lib
+LUA= ../..
-CC= gcc
-CFLAGS= $(INCS) $(DEFS) $(WARN) -O2
+include $(LUA)/config
-# in SunOs /usr/5include contains prototypes for standard lib
-INCS= -I/usr/5include -I$(INC)
-WARN= -Wall -Wmissing-prototypes -Wshadow -ansi
+EXTRA_DEFS= $(POSIX)
OBJS= lua.o
SRCS= lua.c
T=$(BIN)/lua
+all: $T
+
$T: $(OBJS)
$(CC) -o $@ $(OBJS) -L$(LIB) -llua -llualib -lm
diff --git a/clients/lua/lua.c b/clients/lua/lua.c
index 33c7adcb..bae09e25 100644
--- a/clients/lua/lua.c
+++ b/clients/lua/lua.c
@@ -3,7 +3,7 @@
** Linguagem para Usuarios de Aplicacao
*/
-char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto Exp $";
+char *rcs_lua="$Id: lua.c,v 1.10 1996/05/03 19:20:17 roberto Exp $";
#include <stdio.h>
#include <string.h>
@@ -14,11 +14,15 @@ char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto Exp $";
static int lua_argc;
static char **lua_argv;
+#ifdef POSIX
/*
** although this function is POSIX, there is no standard header file that
** defines it
*/
int isatty (int fd);
+#else
+#define isatty(x) (x==0) /* assume stdin is a tty */
+#endif
/*
%F Allow Lua code to access argv strings.
@@ -41,10 +45,10 @@ static void lua_getargv (void)
static void manual_input (void)
{
- if (isatty(fileno(stdin)))
+ if (isatty(0))
{
char buffer[250];
- while (gets(buffer) != 0)
+ while (fgets(buffer, sizeof(buffer), stdin) != 0)
lua_dostring(buffer);
}
else
@@ -83,7 +87,11 @@ int main (int argc, char *argv[])
printf("%s %s\n(written by %s)\n\n",
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
else
+ {
result = lua_dofile (argv[i]);
+ if (result)
+ fprintf(stderr, "lua: error trying to run file %s\n", argv[i]);
+ }
}
}
return result;