xref: /freebsd/stand/liblua/lutils.c (revision 74ca7bf1d4c7173d5575ba168bc4b5f6d181ff5a)
1 /*-
2  * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 
33 #include "lua.h"
34 #include "lauxlib.h"
35 #include "lstd.h"
36 #include "lutils.h"
37 #include "bootstrap.h"
38 
39 /*
40  * Like loader.perform, except args are passed already parsed
41  * on the stack.
42  */
43 static int
44 lua_command(lua_State *L)
45 {
46 	int	i;
47 	int	res = 1;
48 	int 	argc = lua_gettop(L);
49 	char	**argv;
50 
51 	argv = malloc(sizeof(char *) * (argc + 1));
52 	if (argv == NULL)
53 		return 0;
54 	for (i = 0; i < argc; i++)
55 		argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1);
56 	argv[argc] = NULL;
57 	res = interp_builtin_cmd(argc, argv);
58 	free(argv);
59 	lua_pushinteger(L, res);
60 
61 	return 1;
62 }
63 
64 static int
65 lua_perform(lua_State *L)
66 {
67 	int	argc;
68 	char	**argv;
69 	int	res = 1;
70 
71 	if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
72 		res = interp_builtin_cmd(argc, argv);
73 		free(argv);
74 	}
75 	lua_pushinteger(L, res);
76 
77 	return 1;
78 }
79 
80 /*
81  * Accepts a space-delimited loader command and runs it through the standard
82  * loader parsing, as if it were executed at the loader prompt by the user.
83  */
84 static int
85 lua_interpret(lua_State *L)
86 {
87 	const char	*interp_string;
88 
89 	if (lua_gettop(L) != 1) {
90 		lua_pushnil(L);
91 		return 1;
92 	}
93 
94 	interp_string = luaL_checkstring(L, 1);
95 	lua_pushinteger(L, interp_run(interp_string));
96 	return 1;
97 }
98 
99 static int
100 lua_parse(lua_State *L)
101 {
102 	int	argc, nargc;
103 	char	**argv;
104 
105 	if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
106 		for (nargc = 0; nargc < argc; ++nargc) {
107 			lua_pushstring(L, argv[nargc]);
108 		}
109 		free(argv);
110 		return nargc;
111 	}
112 
113 	lua_pushnil(L);
114 	return 1;
115 }
116 
117 static int
118 lua_getchar(lua_State *L)
119 {
120 
121 	lua_pushinteger(L, getchar());
122 	return 1;
123 }
124 
125 static int
126 lua_ischar(lua_State *L)
127 {
128 
129 	lua_pushboolean(L, ischar());
130 	return 1;
131 }
132 
133 static int
134 lua_gets(lua_State *L)
135 {
136 	char	buf[129];
137 
138 	ngets(buf, 128);
139 	lua_pushstring(L, buf);
140 	return 1;
141 }
142 
143 static int
144 lua_time(lua_State *L)
145 {
146 
147 	lua_pushinteger(L, time(NULL));
148 	return 1;
149 }
150 
151 static int
152 lua_delay(lua_State *L)
153 {
154 
155 	delay((int)luaL_checknumber(L, 1));
156 	return 0;
157 }
158 
159 static int
160 lua_getenv(lua_State *L)
161 {
162 	lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
163 
164 	return 1;
165 }
166 
167 static int
168 lua_setenv(lua_State *L)
169 {
170 	const char *key, *val;
171 
172 	key = luaL_checkstring(L, 1);
173 	val = luaL_checkstring(L, 2);
174 	lua_pushinteger(L, setenv(key, val, 1));
175 
176 	return 1;
177 }
178 
179 static int
180 lua_unsetenv(lua_State *L)
181 {
182 	const char	*ev;
183 
184 	ev = luaL_checkstring(L, 1);
185 	lua_pushinteger(L, unsetenv(ev));
186 
187 	return 1;
188 }
189 
190 static int
191 lua_printc(lua_State *L)
192 {
193 	ssize_t cur, l;
194 	const char *s = luaL_checklstring(L, 1, &l);
195 
196 	for (cur = 0; cur < l; ++cur)
197 		putchar((unsigned char)*(s++));
198 
199 	return 1;
200 }
201 
202 static int
203 lua_openfile(lua_State *L)
204 {
205 	const char	*mode, *str;
206 	int	nargs;
207 
208 	nargs = lua_gettop(L);
209 	if (nargs < 1 || nargs > 2) {
210 		lua_pushnil(L);
211 		return 1;
212 	}
213 	str = lua_tostring(L, 1);
214 	mode = "r";
215 	if (nargs > 1) {
216 		mode = lua_tostring(L, 2);
217 		if (mode == NULL) {
218 			lua_pushnil(L);
219 			return 1;
220 		}
221 	}
222 	FILE * f = fopen(str, mode);
223 	if (f != NULL) {
224 		FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
225 		*ptr = f;
226 	} else
227 		lua_pushnil(L);
228 	return 1;
229 }
230 
231 static int
232 lua_closefile(lua_State *L)
233 {
234 	FILE ** f;
235 	if (lua_gettop(L) != 1) {
236 		lua_pushboolean(L, 0);
237 		return 1;
238 	}
239 
240 	f = (FILE**)lua_touserdata(L, 1);
241 	if (f != NULL && *f != NULL) {
242 		lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
243 		*f = NULL;
244 	} else
245 		lua_pushboolean(L, 0);
246 
247 	return 1;
248 }
249 
250 static int
251 lua_readfile(lua_State *L)
252 {
253 	FILE	**f;
254 	size_t	size, r;
255 	char * buf;
256 
257 	if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
258 		lua_pushnil(L);
259 		lua_pushinteger(L, 0);
260 		return 2;
261 	}
262 
263 	f = (FILE**)lua_touserdata(L, 1);
264 
265 	if (f == NULL || *f == NULL) {
266 		lua_pushnil(L);
267 		lua_pushinteger(L, 0);
268 		return 2;
269 	}
270 
271 	if (lua_gettop(L) == 2)
272 		size = (size_t)lua_tonumber(L, 2);
273 	else
274 		size = (*f)->size;
275 
276 
277 	buf = (char*)malloc(size);
278 	r = fread(buf, 1, size, *f);
279 	lua_pushlstring(L, buf, r);
280 	free(buf);
281 	lua_pushinteger(L, r);
282 
283 	return 2;
284 }
285 
286 /*
287  * Implements io.write(file, ...)
288  * Any number of string and number arguments may be passed to it,
289  * and it will return the number of bytes written, or nil, an error string, and
290  * the errno.
291  */
292 static int
293 lua_writefile(lua_State *L)
294 {
295 	FILE	**f;
296 	const char	*buf;
297 	int	i, nargs;
298 	size_t	bufsz, w, wrsz;
299 
300 	buf = NULL;
301 	bufsz = 0;
302 	w = 0;
303 	wrsz = 0;
304 	nargs = lua_gettop(L);
305 	if (nargs < 2) {
306 		errno = EINVAL;
307 		return luaL_fileresult(L, 0, NULL);
308 	}
309 
310 	f = (FILE**)lua_touserdata(L, 1);
311 
312 	if (f == NULL || *f == NULL) {
313 		errno = EINVAL;
314 		return luaL_fileresult(L, 0, NULL);
315 	}
316 
317 	/* Do a validation pass first */
318 	for (i = 0; i < nargs - 1; i++) {
319 		/*
320 		 * With Lua's API, lua_isstring really checks if the argument
321 		 * is a string or a number.  The latter will be implicitly
322 		 * converted to a string by our later call to lua_tolstring.
323 		 */
324 		if (!lua_isstring(L, i + 2)) {
325 			errno = EINVAL;
326 			return luaL_fileresult(L, 0, NULL);
327 		}
328 	}
329 	for (i = 0; i < nargs - 1; i++) {
330 		/* We've already validated; there's no chance of failure */
331 		buf = lua_tolstring(L, i + 2, &bufsz);
332 		wrsz = fwrite(buf, 1, bufsz, *f);
333 		if (wrsz < bufsz)
334 			return luaL_fileresult(L, 0, NULL);
335 		w += wrsz;
336 	}
337 	lua_pushinteger(L, w);
338 	return 1;
339 }
340 
341 #define REG_SIMPLE(n)	{ #n, lua_ ## n }
342 static const struct luaL_Reg loaderlib[] = {
343 	REG_SIMPLE(delay),
344 	REG_SIMPLE(command),
345 	REG_SIMPLE(interpret),
346 	REG_SIMPLE(parse),
347 	REG_SIMPLE(getenv),
348 	REG_SIMPLE(perform),
349 	/* Also registered as the global 'printc' */
350 	REG_SIMPLE(printc),
351 	REG_SIMPLE(setenv),
352 	REG_SIMPLE(time),
353 	REG_SIMPLE(unsetenv),
354 	{ NULL, NULL },
355 };
356 
357 static const struct luaL_Reg iolib[] = {
358 	{ "close", lua_closefile },
359 	REG_SIMPLE(getchar),
360 	REG_SIMPLE(gets),
361 	REG_SIMPLE(ischar),
362 	{ "open", lua_openfile },
363 	{ "read", lua_readfile },
364 	{ "write", lua_writefile },
365 	{ NULL, NULL },
366 };
367 #undef REG_SIMPLE
368 
369 int
370 luaopen_loader(lua_State *L)
371 {
372 	luaL_newlib(L, loaderlib);
373 	/* Add loader.machine and loader.machine_arch properties */
374 	lua_pushstring(L, MACHINE);
375 	lua_setfield(L, -2, "machine");
376 	lua_pushstring(L, MACHINE_ARCH);
377 	lua_setfield(L, -2, "machine_arch");
378 	/* Set global printc to loader.printc */
379 	lua_register(L, "printc", lua_printc);
380 	return 1;
381 }
382 
383 int
384 luaopen_io(lua_State *L)
385 {
386 	luaL_newlib(L, iolib);
387 	return 1;
388 }
389