xref: /freebsd/stand/liblua/lutils.c (revision 3982006ed5587cfd83cc1955186e0aa4b92b3fcd)
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 static int
81 lua_getchar(lua_State *L)
82 {
83 
84 	lua_pushinteger(L, getchar());
85 	return 1;
86 }
87 
88 static int
89 lua_ischar(lua_State *L)
90 {
91 
92 	lua_pushboolean(L, ischar());
93 	return 1;
94 }
95 
96 static int
97 lua_gets(lua_State *L)
98 {
99 	char	buf[129];
100 
101 	ngets(buf, 128);
102 	lua_pushstring(L, buf);
103 	return 1;
104 }
105 
106 static int
107 lua_time(lua_State *L)
108 {
109 
110 	lua_pushinteger(L, time(NULL));
111 	return 1;
112 }
113 
114 static int
115 lua_delay(lua_State *L)
116 {
117 
118 	delay((int)luaL_checknumber(L, 1));
119 	return 0;
120 }
121 
122 static int
123 lua_getenv(lua_State *L)
124 {
125 	lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
126 
127 	return 1;
128 }
129 
130 static int
131 lua_setenv(lua_State *L)
132 {
133 	const char *key, *val;
134 
135 	key = luaL_checkstring(L, 1);
136 	val = luaL_checkstring(L, 2);
137 	lua_pushinteger(L, setenv(key, val, 1));
138 
139 	return 1;
140 }
141 
142 static int
143 lua_unsetenv(lua_State *L)
144 {
145 	const char	*ev;
146 
147 	ev = luaL_checkstring(L, 1);
148 	lua_pushinteger(L, unsetenv(ev));
149 
150 	return 1;
151 }
152 
153 static int
154 lua_printc(lua_State *L)
155 {
156 	int status;
157 	ssize_t l;
158 	const char *s = luaL_checklstring(L, 1, &l);
159 
160 	status = (printf("%s", s) == l);
161 
162 	return status;
163 }
164 
165 static int
166 lua_openfile(lua_State *L)
167 {
168 	const char	*str;
169 
170 	if (lua_gettop(L) != 1) {
171 		lua_pushnil(L);
172 		return 1;
173 	}
174 	str = lua_tostring(L, 1);
175 
176 	FILE * f = fopen(str, "r");
177 	if (f != NULL) {
178 		FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
179 		*ptr = f;
180 	} else
181 		lua_pushnil(L);
182 	return 1;
183 }
184 
185 static int
186 lua_closefile(lua_State *L)
187 {
188 	FILE ** f;
189 	if (lua_gettop(L) != 1) {
190 		lua_pushboolean(L, 0);
191 		return 1;
192 	}
193 
194 	f = (FILE**)lua_touserdata(L, 1);
195 	if (f != NULL && *f != NULL) {
196 		lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
197 		*f = NULL;
198 	} else
199 		lua_pushboolean(L, 0);
200 
201 	return 1;
202 }
203 
204 static int
205 lua_readfile(lua_State *L)
206 {
207 	FILE	**f;
208 	size_t	size, r;
209 	char * buf;
210 
211 	if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
212 		lua_pushnil(L);
213 		lua_pushinteger(L, 0);
214 		return 2;
215 	}
216 
217 	f = (FILE**)lua_touserdata(L, 1);
218 
219 	if (f == NULL || *f == NULL) {
220 		lua_pushnil(L);
221 		lua_pushinteger(L, 0);
222 		return 2;
223 	}
224 
225 	if (lua_gettop(L) == 2)
226 		size = (size_t)lua_tonumber(L, 2);
227 	else
228 		size = (*f)->size;
229 
230 
231 	buf = (char*)malloc(size);
232 	r = fread(buf, 1, size, *f);
233 	lua_pushlstring(L, buf, r);
234 	free(buf);
235 	lua_pushinteger(L, r);
236 
237 	return 2;
238 }
239 
240 #define REG_SIMPLE(n)	{ #n, lua_ ## n }
241 static const struct luaL_Reg loaderlib[] = {
242 	REG_SIMPLE(delay),
243 	REG_SIMPLE(command),
244 	REG_SIMPLE(getenv),
245 	REG_SIMPLE(perform),
246 	REG_SIMPLE(printc),
247 	REG_SIMPLE(setenv),
248 	REG_SIMPLE(time),
249 	REG_SIMPLE(unsetenv),
250 	{ NULL, NULL },
251 };
252 
253 static const struct luaL_Reg iolib[] = {
254 	{ "close", lua_closefile },
255 	REG_SIMPLE(getchar),
256 	REG_SIMPLE(gets),
257 	REG_SIMPLE(ischar),
258 	{ "open", lua_openfile },
259 	{ "read", lua_readfile },
260 	{ NULL, NULL },
261 };
262 #undef REG_SIMPLE
263 
264 int
265 luaopen_loader(lua_State *L)
266 {
267 	luaL_newlib(L, loaderlib);
268 	/* Add loader.machine and loader.machine_arch properties */
269 	lua_pushstring(L, MACHINE);
270 	lua_setfield(L, -2, "machine");
271 	lua_pushstring(L, MACHINE_ARCH);
272 	lua_setfield(L, -2, "machine_arch");
273 	return 1;
274 }
275 
276 int
277 luaopen_io(lua_State *L)
278 {
279 	luaL_newlib(L, iolib);
280 	return 1;
281 }
282