123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- /*
- ** iolib.c
- ** Input/output library to LUA
- */
- char *rcs_iolib="$Id: iolib.c,v 1.20 1995/02/02 18:54:58 roberto Exp roberto $";
- #include <stdio.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #include "lua.h"
- #include "lualib.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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
- {
- int c;
- char s[256];
- while (isspace(c=fgetc(in)))
- ;
- if (c == '\"')
- {
- int n=0;
- while((c = fgetc(in)) != '\"')
- {
- if (c == EOF)
- {
- lua_pushnil ();
- return;
- }
- s[n++] = c;
- }
- s[n] = 0;
- }
- else if (c == '\'')
- {
- int n=0;
- while((c = fgetc(in)) != '\'')
- {
- if (c == EOF)
- {
- lua_pushnil ();
- return;
- }
- s[n++] = c;
- }
- s[n] = 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;
- }
- }
- 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;
- }
- }
- }
- }
- /*
- ** 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])
- **
- ** 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[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;
- }
- 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;
- }
- lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
- }
- }
- /*
- ** Execute a executable program using "system".
- ** Return the result of execution.
- */
- 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;
- }
- /*
- ** Remove a file.
- ** On error put 0 on stack, otherwise put 1.
- */
- 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)
- lua_pushnumber (1);
- else
- lua_pushnumber (0);
- }
- 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
- */
- 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);
- }
|