summaryrefslogtreecommitdiff
path: root/clients/lib/iolib.c
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1995-02-07 12:00:00 +0000
committerrepogen <>1995-02-07 12:00:00 +0000
commita8b6ba0954edb9e0e669e1f451b9a8f145ce5166 (patch)
tree35e9e9999968c4f13a25a5f647203456f044274a /clients/lib/iolib.c
parent944fc7d7d95575f2b8023c1f3d4ac19e1369fc76 (diff)
downloadlua-github-2.1.tar.gz
Lua 2.12.1
Diffstat (limited to 'clients/lib/iolib.c')
-rw-r--r--clients/lib/iolib.c220
1 files changed, 178 insertions, 42 deletions
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);
}