20 #define popen(x,y) NULL /* that is, popen always fails */
21 #define pclose(x) (-1)
25 static void pushresult (int i
)
28 lua_pushuserdata(NULL
);
32 lua_pushstring(strerror(errno
));
34 lua_pushstring("O.S. unable to define the error");
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;
59 static void setfile (FILE *f
, char *name
)
61 lua_pushusertag(f
, lua_tagio
);
66 static void setreturn (FILE *f
, char *name
)
69 lua_pushusertag(f
, lua_tagio
);
73 static void io_readfrom (void)
76 lua_Object f
= lua_getparam(1);
77 if (f
== LUA_NOOBJECT
) {
81 else if (lua_tag(f
) == lua_tagio
)
82 current
= lua_getuserdata(f
);
84 char *s
= luaL_check_string(1);
85 current
= (*s
== '|') ? popen(s
+1, "r") : fopen(s
, "r");
86 if (current
== NULL
) {
91 setreturn(current
, "_INPUT");
95 static void io_writeto (void)
98 lua_Object f
= lua_getparam(1);
99 if (f
== LUA_NOOBJECT
) {
100 closefile("_OUTPUT");
103 else if (lua_tag(f
) == lua_tagio
)
104 current
= lua_getuserdata(f
);
106 char *s
= luaL_check_string(1);
107 current
= (*s
== '|') ? popen(s
+1,"w") : fopen(s
,"w");
108 if (current
== NULL
) {
113 setreturn(current
, "_OUTPUT");
117 static void io_appendto (void)
119 char *s
= luaL_check_string(1);
120 FILE *fp
= fopen (s
, "a");
122 setreturn(fp
, "_OUTPUT");
128 #define NEED_OTHER (EOF-1) /* just some flag different from EOF */
130 static void io_read (void)
132 FILE *f
= getfile("_INPUT");
134 char *p
= luaL_opt_string(1, "[^\n]*{\n}");
135 int inskip
= 0; /* to control {skips} */
143 else if (*p
== '}') {
145 lua_error("unbalanced braces in read pattern");
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
);
155 if (inskip
== 0) luaI_addchar(c
);
159 case '*': /* repetition */
160 if (!m
) p
= ep
+1; /* else stay in (repeat) the same item */
162 case '?': /* optional */
163 p
= ep
+1; /* continues reading the pattern */
166 if (m
) p
= ep
; /* continues reading the pattern */
168 goto break_while
; /* pattern fails */
172 if (c
>= 0) /* not EOF nor NEED_OTHER? */
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");
186 while ((s
= luaL_opt_string(arg
++, NULL
)) != NULL
)
187 status
= status
&& (fputs(s
, f
) != EOF
);
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)
228 char *s
= luaL_opt_string(1, "%c");
230 time(&t
); tm
= localtime(&t
);
231 if (strftime(b
,sizeof(b
),s
,tm
))
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)
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) */
261 while ((func
= lua_stackedfunction(level
++)) != LUA_NOOBJECT
) {
266 lua_funcinfo(func
, &filename
, &linedefined
);
267 fprintf(f
, (level
==2) ? "Active Stack:\n\t" : "\t");
268 switch (*lua_getobjname(func
, &name
)) {
270 fprintf(f
, "function %s", name
);
273 fprintf(f
, "`%s' tag method", name
);
276 if (linedefined
== 0)
277 fprintf(f
, "main of %s", filename
);
278 else if (linedefined
< 0)
279 fprintf(f
, "%s", filename
);
281 fprintf(f
, "function (%s:%d)", filename
, linedefined
);
285 if ((currentline
= lua_currentline(func
)) > 0)
286 fprintf(f
, " at line %d", currentline
);
288 fprintf(f
, " [in file %s]", filename
);
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
},
308 {"execute", io_execute
},
309 {"remove", io_remove
},
310 {"rename", io_rename
},
311 {"tmpname", io_tmpname
},
312 {"getenv", io_getenv
},
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();