A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
3** Standard I/O (and system) library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define liolib_c
13#define LUA_LIB
14
15#include "lua.h"
16
17#include "lauxlib.h"
18#include "lualib.h"
19#include "rocklibc.h"
20#include "rocklib.h"
21
22#include "llimits.h"
23
24#define IO_INPUT 1
25#define IO_OUTPUT 2
26
27
28static const char *const fnames[] = {"input", "output"};
29
30
31static int pushresult (lua_State *L, int i, const char *filename) {
32 int en = errno;
33 if (i) {
34 lua_pushboolean(L, 1);
35 return 1;
36 }
37 else {
38 lua_pushnil(L);
39 if (filename)
40 lua_pushfstring(L, "%s: %s", filename, strerror(en));
41 else
42 lua_pushfstring(L, "%s", strerror(en));
43 lua_pushinteger(L, en);
44 return 3;
45 }
46}
47
48
49static void fileerror (lua_State *L, int arg, const char *filename) {
50 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
51 luaL_argerror(L, arg, lua_tostring(L, -1));
52}
53
54#define tofilep(L) ((int*) luaL_checkudata(L, 1, LUA_FILEHANDLE))
55
56static int io_type (lua_State *L) {
57 void *ud;
58 luaL_checkany(L, 1);
59 ud = lua_touserdata(L, 1);
60 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
61 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
62 lua_pushnil(L); /* not a file */
63 else if (*((int *)ud) < 0)
64 lua_pushliteral(L, "closed file");
65 else
66 lua_pushliteral(L, "file");
67 return 1;
68}
69
70
71static int* tofile (lua_State *L) {
72 int *f = tofilep(L);
73 if (*f < 0)
74 luaL_error(L, "attempt to use a closed file");
75 return f;
76}
77
78
79
80/*
81** When creating file handles, always creates a `closed' file handle
82** before opening the actual file; so, if there is a memory error, the
83** file is not left opened.
84*/
85static int* newfile (lua_State *L) {
86 int *pf = (int *)lua_newuserdata(L, sizeof(int));
87 *pf = -1; /* file handle is currently `closed' */
88 luaL_getmetatable(L, LUA_FILEHANDLE);
89 lua_setmetatable(L, -2);
90 return pf;
91}
92
93
94/*
95** function to close regular files
96*/
97static int io_fclose (lua_State *L) {
98 int *p = tofile(L);
99 int ok = (rb->close(*p) == 0);
100 *p = -1;
101 return pushresult(L, ok, NULL);
102}
103
104
105static inline int aux_close (lua_State *L) {
106 return io_fclose(L);
107}
108
109
110static int io_close (lua_State *L) {
111 if (lua_isnone(L, 1))
112 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
113 tofile(L); /* make sure argument is a file */
114 return aux_close(L);
115}
116
117
118static int io_gc (lua_State *L) {
119 int *f = tofilep(L);
120 /* ignore closed files */
121 if (*f >= 0)
122 aux_close(L);
123 return 0;
124}
125
126
127static int io_tostring (lua_State *L) {
128 int *f = tofilep(L);
129 if (*f < 0)
130 lua_pushliteral(L, "file (closed)");
131 else
132 lua_pushfstring(L, "file (%d)", *f);
133 return 1;
134}
135
136
137static int io_open (lua_State *L) {
138 const char *filename = luaL_checkstring(L, 1);
139 const char *mode = luaL_optstring(L, 2, "r");
140 int *pf = newfile(L);
141 int flags, wrmode;
142
143 switch(*mode) {
144 case 'r':
145 flags = O_RDONLY;
146 wrmode = 0;
147 break;
148 case 'w':
149 flags = O_WRONLY;
150 wrmode = O_CREAT | O_TRUNC;
151 break;
152 case 'a':
153 flags = O_WRONLY;
154 wrmode = O_CREAT | O_APPEND;
155 break;
156 default:
157 flags = 0;
158 wrmode = 0;
159 return luaL_error(L, "invalid option " LUA_QL("%c") " to "
160 LUA_QL("open"), *mode);
161 }
162
163 if(*(mode+1) == '+')
164 flags = O_RDWR;
165
166 flags |= wrmode;
167
168 *pf = rb->open(filename, flags, 0666);
169 return (*pf < 0) ? pushresult(L, 0, filename) : 1;
170}
171
172
173static int* getiofile (lua_State *L, int findex) {
174 int *f;
175 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
176 f = (int *)lua_touserdata(L, -1);
177 if (f == NULL || *f < 0)
178 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
179 return f;
180}
181
182
183static int g_iofile (lua_State *L, int f, int flags) {
184 if (!lua_isnoneornil(L, 1)) {
185 const char *filename = lua_tostring(L, 1);
186 if (filename) {
187 int *pf = newfile(L);
188 *pf = rb->open(filename, flags);
189 if (*pf < 0)
190 fileerror(L, 1, filename);
191 }
192 else {
193 tofile(L); /* check that it's a valid file handle */
194 lua_pushvalue(L, 1);
195 }
196 lua_rawseti(L, LUA_ENVIRONINDEX, f);
197 }
198 /* return current value */
199 lua_rawgeti(L, LUA_ENVIRONINDEX, f);
200 return 1;
201}
202
203
204static int io_input (lua_State *L) {
205 return g_iofile(L, IO_INPUT, O_RDONLY);
206}
207
208
209static int io_output (lua_State *L) {
210 return g_iofile(L, IO_OUTPUT, O_WRONLY);
211}
212
213
214static int io_readline (lua_State *L);
215
216
217static void aux_lines (lua_State *L, int idx, int toclose) {
218 lua_pushvalue(L, idx);
219 lua_pushboolean(L, toclose); /* close/not close file when finished */
220 lua_pushcclosure(L, io_readline, 2);
221}
222
223
224static int f_lines (lua_State *L) {
225 tofile(L); /* check that it's a valid file handle */
226 aux_lines(L, 1, 0);
227 return 1;
228}
229
230
231static int io_lines (lua_State *L) {
232 if (lua_isnoneornil(L, 1)) { /* no arguments? */
233 /* will iterate over default input */
234 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
235 return f_lines(L);
236 }
237 else {
238 const char *filename = luaL_checkstring(L, 1);
239 int *pf = newfile(L);
240 *pf = rb->open(filename, O_RDONLY);
241 if (*pf < 0)
242 fileerror(L, 1, filename);
243 aux_lines(L, lua_gettop(L), 1);
244 return 1;
245 }
246}
247
248
249/*
250** {======================================================
251** READ
252** =======================================================
253*/
254
255static int read_number (lua_State *L, int *f) {
256 lua_Number d;
257 if (filetol(*f, &d) == 1) { /* was fscanf(f, LUA_NUMBER_SCAN, &d)*/
258 lua_pushnumber(L, d);
259 return 1;
260 }
261 else {
262 lua_pushnil(L); /* "result" to be removed */
263 return 0; /* read fails */
264 }
265}
266
267
268static int test_eof (lua_State *L, int *f) {
269 off_t s = rb->lseek(*f, 0, SEEK_CUR);
270 lua_pushlstring(L, NULL, 0);
271 return s < rb->filesize(*f);
272}
273
274
275/* Rockbox already defines read_line() */
276static int _read_line (lua_State *L, int *f) {
277 luaL_Buffer b;
278 luaL_buffinit(L, &b);
279 for (;;) {
280 size_t l;
281 char *p = luaL_prepbuffer(&b);
282 off_t r = rb->read_line(*f, p, LUAL_BUFFERSIZE); /* does not include `eol' */
283 if (r <= 0) { /* eof? */
284 luaL_pushresult(&b); /* close buffer */
285 return (lua_objlen(L, -1) > 0); /* check whether read something */
286 }
287 l = strlen(p);
288 luaL_addsize(&b, l);
289 if(r >= LUAL_BUFFERSIZE - 1)
290 continue; /* more to read */
291
292 luaL_pushresult(&b); /* close buffer */
293 return 1; /* we read at least 1 character */
294 }
295}
296
297
298static int read_chars (lua_State *L, int *f, size_t n) {
299 size_t rlen; /* how much to read */
300 ssize_t nr; /* number of chars actually read */
301 luaL_Buffer b;
302 luaL_buffinit(L, &b);
303 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
304 do {
305 char *p = luaL_prepbuffer(&b);
306 if (rlen > n) rlen = n; /* cannot read more than asked */
307 nr = rb->read(*f, p, rlen);
308 if (nr < 0)
309 luaL_error(L, "error reading file");
310 luaL_addsize(&b, nr);
311 n -= nr; /* still have to read `n' chars */
312 } while (n > 0 && nr == (ssize_t) rlen); /* until end of count or eof */
313 luaL_pushresult(&b); /* close buffer */
314 return (n == 0 || lua_objlen(L, -1) > 0);
315}
316
317
318static int g_read (lua_State *L, int *f, int first) {
319 int nargs = lua_gettop(L) - 1;
320 int success;
321 int n;
322 if (nargs == 0) { /* no arguments? */
323 success = _read_line(L, f);
324 n = first+1; /* to return 1 result */
325 }
326 else { /* ensure stack space for all results and for auxlib's buffer */
327 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
328 success = 1;
329 for (n = first; nargs-- && success; n++) {
330 if (lua_type(L, n) == LUA_TNUMBER) {
331 size_t l = (size_t)lua_tointeger(L, n);
332 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
333 }
334 else {
335 const char *p = lua_tostring(L, n);
336 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
337 switch (p[1]) {
338 case 'n': /* number */
339 success = read_number(L, f);
340 break;
341 case 'l': /* line */
342 success = _read_line(L, f);
343 break;
344 case 'a': /* file */
345 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
346 success = 1; /* always success */
347 break;
348 default:
349 return luaL_argerror(L, n, "invalid format");
350 }
351 }
352 }
353 }
354 if (!success) {
355 lua_pop(L, 1); /* remove last result */
356 lua_pushnil(L); /* push nil instead */
357 }
358 return n - first;
359}
360
361
362static int io_read (lua_State *L) {
363 return g_read(L, getiofile(L, IO_INPUT), 1);
364}
365
366
367static int f_read (lua_State *L) {
368 return g_read(L, tofile(L), 2);
369}
370
371
372static int io_readline (lua_State *L) {
373 int *f = (int *) lua_touserdata(L, lua_upvalueindex(1));
374 int sucess;
375 if (*f < 0) /* file is already closed? */
376 luaL_error(L, "file is already closed");
377 sucess = _read_line(L, f);
378 if (sucess) return 1;
379 else { /* EOF */
380 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
381 lua_settop(L, 0);
382 lua_pushvalue(L, lua_upvalueindex(1));
383 aux_close(L); /* close it */
384 }
385 return 0;
386 }
387}
388
389/* }====================================================== */
390
391
392static int g_write (lua_State *L, int *f, int arg) {
393 int nargs = lua_gettop(L) - 1;
394 int status = 1;
395 for (; nargs--; arg++) {
396 if (lua_type(L, arg) == LUA_TNUMBER) {
397 /* optimization: could be done exactly as for strings */
398 status = status &&
399 rb->fdprintf(*f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
400 }
401 else {
402 size_t l;
403 const char *s = luaL_checklstring(L, arg, &l);
404 status = status && (rb->write(*f, s, l) == (ssize_t)l);
405 }
406 }
407 return pushresult(L, status, NULL);
408}
409
410
411static int io_write (lua_State *L) {
412 return g_write(L, getiofile(L, IO_OUTPUT), 1);
413}
414
415
416static int f_write (lua_State *L) {
417 return g_write(L, tofile(L), 2);
418}
419
420
421static int f_seek (lua_State *L) {
422 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
423 static const char *const modenames[] = {"set", "cur", "end", NULL};
424 int *f = tofile(L);
425 int op = luaL_checkoption(L, 2, "cur", modenames);
426 long offset = luaL_optlong(L, 3, 0);
427 off_t size = rb->lseek(*f, offset, mode[op]);
428 if (size < 0 || size > MAX_INT) /* signed limit */
429 return pushresult(L, 0, NULL); /* error */
430 else {
431 lua_pushinteger(L, (LUA_INTEGER) size );
432 return 1;
433 }
434}
435
436
437static const luaL_Reg iolib[] = {
438 {"close", io_close},
439 {"input", io_input},
440 {"lines", io_lines},
441 {"open", io_open},
442 {"output", io_output},
443 {"read", io_read},
444 {"type", io_type},
445 {"write", io_write},
446 {NULL, NULL}
447};
448
449
450static const luaL_Reg flib[] = {
451 {"close", io_close},
452 {"lines", f_lines},
453 {"read", f_read},
454 {"seek", f_seek},
455 {"write", f_write},
456 {"__gc", io_gc},
457 {"__tostring", io_tostring},
458 {NULL, NULL}
459};
460
461
462static void createmeta (lua_State *L) {
463 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
464 lua_pushvalue(L, -1); /* push metatable */
465 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
466 luaL_register(L, NULL, flib); /* file methods */
467}
468
469
470LUALIB_API int luaopen_io (lua_State *L) {
471 createmeta(L);
472 lua_replace(L, LUA_ENVIRONINDEX);
473 /* open library */
474 luaL_register(L, LUA_IOLIBNAME, iolib);
475 /* create (and set) default files */
476 lua_pop(L, 1); /* pop environment for default files */
477 return 1;
478}