Imported from ../lua-3.0.tar.gz.
[lua.git] / clients / lib / iolib.c
blobe518df1b174d0a736cf0d9cc80988b3a5a32c48e
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4 #include <stdlib.h>
5 #include <errno.h>
7 #include "lua.h"
8 #include "auxlib.h"
9 #include "luadebug.h"
10 #include "lualib.h"
13 int lua_tagio;
16 #ifdef POPEN
17 FILE *popen();
18 int pclose();
19 #else
20 #define popen(x,y) NULL /* that is, popen always fails */
21 #define pclose(x) (-1)
22 #endif
25 static void pushresult (int i)
27 if (i)
28 lua_pushuserdata(NULL);
29 else {
30 lua_pushnil();
31 #ifndef NOSTRERROR
32 lua_pushstring(strerror(errno));
33 #else
34 lua_pushstring("O.S. unable to define the error");
35 #endif
41 static FILE *getfile (char *name)
43 lua_Object f = lua_getglobal(name);
44 if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio)
45 luaL_verror("global variable %s is not a file handle", name);
46 return lua_getuserdata(f);
50 static void closefile (char *name)
52 FILE *f = getfile(name);
53 if (f == stdin || f == stdout) return;
54 if (pclose(f) == -1)
55 fclose(f);
59 static void setfile (FILE *f, char *name)
61 lua_pushusertag(f, lua_tagio);
62 lua_setglobal(name);
66 static void setreturn (FILE *f, char *name)
68 setfile(f, name);
69 lua_pushusertag(f, lua_tagio);
73 static void io_readfrom (void)
75 FILE *current;
76 lua_Object f = lua_getparam(1);
77 if (f == LUA_NOOBJECT) {
78 closefile("_INPUT");
79 current = stdin;
81 else if (lua_tag(f) == lua_tagio)
82 current = lua_getuserdata(f);
83 else {
84 char *s = luaL_check_string(1);
85 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
86 if (current == NULL) {
87 pushresult(0);
88 return;
91 setreturn(current, "_INPUT");
95 static void io_writeto (void)
97 FILE *current;
98 lua_Object f = lua_getparam(1);
99 if (f == LUA_NOOBJECT) {
100 closefile("_OUTPUT");
101 current = stdout;
103 else if (lua_tag(f) == lua_tagio)
104 current = lua_getuserdata(f);
105 else {
106 char *s = luaL_check_string(1);
107 current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
108 if (current == NULL) {
109 pushresult(0);
110 return;
113 setreturn(current, "_OUTPUT");
117 static void io_appendto (void)
119 char *s = luaL_check_string(1);
120 FILE *fp = fopen (s, "a");
121 if (fp != NULL)
122 setreturn(fp, "_OUTPUT");
123 else
124 pushresult(0);
128 #define NEED_OTHER (EOF-1) /* just some flag different from EOF */
130 static void io_read (void)
132 FILE *f = getfile("_INPUT");
133 char *buff;
134 char *p = luaL_opt_string(1, "[^\n]*{\n}");
135 int inskip = 0; /* to control {skips} */
136 int c = NEED_OTHER;
137 luaI_emptybuff();
138 while (*p) {
139 if (*p == '{') {
140 inskip++;
141 p++;
143 else if (*p == '}') {
144 if (inskip == 0)
145 lua_error("unbalanced braces in read pattern");
146 inskip--;
147 p++;
149 else {
150 char *ep = luaL_item_end(p); /* get what is next */
151 int m; /* match result */
152 if (c == NEED_OTHER) c = getc(f);
153 m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
154 if (m) {
155 if (inskip == 0) luaI_addchar(c);
156 c = NEED_OTHER;
158 switch (*ep) {
159 case '*': /* repetition */
160 if (!m) p = ep+1; /* else stay in (repeat) the same item */
161 break;
162 case '?': /* optional */
163 p = ep+1; /* continues reading the pattern */
164 break;
165 default:
166 if (m) p = ep; /* continues reading the pattern */
167 else
168 goto break_while; /* pattern fails */
171 } break_while:
172 if (c >= 0) /* not EOF nor NEED_OTHER? */
173 ungetc(c, f);
174 buff = luaI_addchar(0);
175 if (*buff != 0 || *p == 0) /* read something or did not fail? */
176 lua_pushstring(buff);
180 static void io_write (void)
182 FILE *f = getfile("_OUTPUT");
183 int arg = 1;
184 int status = 1;
185 char *s;
186 while ((s = luaL_opt_string(arg++, NULL)) != NULL)
187 status = status && (fputs(s, f) != EOF);
188 pushresult(status);
192 static void io_execute (void)
194 lua_pushnumber(system(luaL_check_string(1)));
198 static void io_remove (void)
200 pushresult(remove(luaL_check_string(1)) == 0);
204 static void io_rename (void)
206 pushresult(rename(luaL_check_string(1),
207 luaL_check_string(2)) == 0);
211 static void io_tmpname (void)
213 lua_pushstring(tmpnam(NULL));
218 static void io_getenv (void)
220 lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
224 static void io_date (void)
226 time_t t;
227 struct tm *tm;
228 char *s = luaL_opt_string(1, "%c");
229 char b[BUFSIZ];
230 time(&t); tm = localtime(&t);
231 if (strftime(b,sizeof(b),s,tm))
232 lua_pushstring(b);
233 else
234 lua_error("invalid `date' format");
238 static void io_exit (void)
240 lua_Object o = lua_getparam(1);
241 exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
245 static void io_debug (void)
247 while (1) {
248 char buffer[250];
249 fprintf(stderr, "lua_debug> ");
250 if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
251 if (strcmp(buffer, "cont\n") == 0) return;
252 lua_dostring(buffer);
257 static void lua_printstack (FILE *f)
259 int level = 1; /* skip level 0 (it's this function) */
260 lua_Object func;
261 while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
262 char *name;
263 int currentline;
264 char *filename;
265 int linedefined;
266 lua_funcinfo(func, &filename, &linedefined);
267 fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
268 switch (*lua_getobjname(func, &name)) {
269 case 'g':
270 fprintf(f, "function %s", name);
271 break;
272 case 't':
273 fprintf(f, "`%s' tag method", name);
274 break;
275 default: {
276 if (linedefined == 0)
277 fprintf(f, "main of %s", filename);
278 else if (linedefined < 0)
279 fprintf(f, "%s", filename);
280 else
281 fprintf(f, "function (%s:%d)", filename, linedefined);
282 filename = NULL;
285 if ((currentline = lua_currentline(func)) > 0)
286 fprintf(f, " at line %d", currentline);
287 if (filename)
288 fprintf(f, " [in file %s]", filename);
289 fprintf(f, "\n");
294 static void errorfb (void)
296 fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
297 lua_printstack(stderr);
302 static struct luaL_reg iolib[] = {
303 {"readfrom", io_readfrom},
304 {"writeto", io_writeto},
305 {"appendto", io_appendto},
306 {"read", io_read},
307 {"write", io_write},
308 {"execute", io_execute},
309 {"remove", io_remove},
310 {"rename", io_rename},
311 {"tmpname", io_tmpname},
312 {"getenv", io_getenv},
313 {"date", io_date},
314 {"exit", io_exit},
315 {"debug", io_debug},
316 {"print_stack", errorfb}
319 void iolib_open (void)
321 lua_tagio = lua_newtag();
322 setfile(stdin, "_INPUT");
323 setfile(stdout, "_OUTPUT");
324 setfile(stdin, "_STDIN");
325 setfile(stdout, "_STDOUT");
326 setfile(stderr, "_STDERR");
327 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
328 lua_pushcfunction(errorfb);
329 lua_seterrormethod();