diff options
-rw-r--r-- | apps/plugins/helloworld.lua | 74 | ||||
-rw-r--r-- | apps/plugins/lua/SOURCES | 1 | ||||
-rw-r--r-- | apps/plugins/lua/liolib.c | 479 | ||||
-rw-r--r-- | apps/plugins/lua/rocklib.c | 100 | ||||
-rw-r--r-- | apps/plugins/lua/rocklua.c | 1 |
5 files changed, 512 insertions, 143 deletions
diff --git a/apps/plugins/helloworld.lua b/apps/plugins/helloworld.lua index ed8726c022..94e55af3cc 100644 --- a/apps/plugins/helloworld.lua +++ b/apps/plugins/helloworld.lua | |||
@@ -21,31 +21,29 @@ | |||
21 | 21 | ||
22 | ]]-- | 22 | ]]-- |
23 | 23 | ||
24 | require("actions") -- Contains rb.actions & rb.contexts | 24 | require("actions") -- Contains rb.actions & rb.contexts |
25 | --require("buttons") -- Contains rb.buttons; not needed in this example | ||
25 | 26 | ||
26 | -- Example function which splashes a message for x seconds | 27 | -- Example function which splashes a message for x seconds |
27 | function sayhello(seconds) | 28 | function sayhello(seconds) |
28 | message = string.format("Hello world from LUA for %d seconds", seconds) | 29 | local message = string.format("Hello world from LUA for %d seconds", seconds) |
29 | rb.splash(seconds * rb.HZ, message) | 30 | rb.splash(seconds * rb.HZ, message) |
30 | end | 31 | end |
31 | 32 | ||
32 | -- Helper function which draws a transparent image at the center of the screen | 33 | -- Helper function which draws a (transparent) image at the center of the screen |
33 | function draw_image(img) | 34 | function draw_image(img) |
34 | local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 | 35 | local x, y = (rb.LCD_WIDTH - img:width()) / 2, (rb.LCD_HEIGHT - img:height()) / 2 |
35 | 36 | ||
36 | local func = rb.lcd_bitmap_transparent_part | 37 | local func = rb.lcd_bitmap_transparent_part |
37 | if(func == nil) then | 38 | or rb.lcd_bitmap_part -- Fallback version for grayscale targets |
38 | func = rb.lcd_bitmap_part -- Fallback version for grayscale targets | 39 | or rb.lcd_mono_bitmap_part -- Fallback version for mono targets |
39 | if(func == nil) then | 40 | |
40 | func = rb.lcd_mono_bitmap_part -- Fallback version for mono targets | ||
41 | end | ||
42 | end | ||
43 | func(img, 0, 0, img:width(), x, y, img:width(), img:height()) | 41 | func(img, 0, 0, img:width(), x, y, img:width(), img:height()) |
44 | rb.lcd_update() | 42 | rb.lcd_update() |
45 | end | 43 | end |
46 | 44 | ||
47 | -- Helper function that acts like a normal printf() would do | 45 | -- Helper function that acts like a normal printf() would do |
48 | line = 0 | 46 | local line = 0 |
49 | function printf(...) | 47 | function printf(...) |
50 | local msg = string.format(...) | 48 | local msg = string.format(...) |
51 | local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) | 49 | local res, w, h = rb.font_getstringsize(msg, rb.FONT_UI) |
@@ -66,37 +64,26 @@ end | |||
66 | 64 | ||
67 | -- Helper function which reads the contents of a file | 65 | -- Helper function which reads the contents of a file |
68 | function file_get_contents(filename) | 66 | function file_get_contents(filename) |
69 | if(not rb.file_exists(filename)) then | 67 | local file = io.open(filename, "r") |
68 | if(file == nil) then | ||
70 | return nil | 69 | return nil |
71 | end | 70 | end |
72 | 71 | ||
73 | local fd = rb.open(filename, rb.O_RDONLY) | 72 | local contents = file:read("*all") -- See Lua manual for more information |
74 | if(fd == -1) then | 73 | file:close() -- GC takes care of this if you would've forgotten it |
75 | return nil | ||
76 | end | ||
77 | |||
78 | local contents = rb.read(fd, rb.filesize(fd)) | ||
79 | rb.close(fd) | ||
80 | 74 | ||
81 | return contents | 75 | return contents |
82 | end | 76 | end |
83 | 77 | ||
84 | -- Helper function which saves contents to a file | 78 | -- Helper function which saves contents to a file |
85 | function file_put_contents(filename, contents) | 79 | function file_put_contents(filename, contents) |
86 | local flags = rb.O_WRONLY | 80 | local file = io.open(filename, "w+") -- See Lua manual for more information |
87 | if(rb.file_exists(filename)) then | 81 | if(file == nil) then |
88 | flags = bit.bor(flags, rb.O_APPEND) -- binary OR O_APPEND if the file exists | ||
89 | else | ||
90 | flags = bit.bor(flags, rb.O_CREAT) -- binary OR O_CREAT if the file doesn't exist | ||
91 | end | ||
92 | |||
93 | local fd = rb.open(filename, flags) | ||
94 | if(fd == -1) then | ||
95 | return false | 82 | return false |
96 | end | 83 | end |
97 | 84 | ||
98 | local ret = rb.write(fd, contents) ~= string.len(contents) | 85 | local ret = file:write(contents) == 1 |
99 | rb.close(fd) | 86 | file:close() -- GC takes care of this if you would've forgotten it |
100 | return ret | 87 | return ret |
101 | end | 88 | end |
102 | 89 | ||
@@ -120,13 +107,10 @@ if(rb.lcd_rgbpack ~= nil) then -- Only do this when we're on a color target, i.e | |||
120 | end | 107 | end |
121 | 108 | ||
122 | -- Load a BMP file in the variable backdrop | 109 | -- Load a BMP file in the variable backdrop |
123 | local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present? | 110 | local backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers.bmp") -- This image should always be present... |
124 | if(backdrop == nil) then | 111 | or rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- ... if not, try using the mono version... |
125 | backdrop = rb.read_bmp_file("/.rockbox/icons/tango_small_viewers_mono.bmp") -- Try using the mono version | 112 | or rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- ... or try using the builtin version |
126 | if(backdrop == nil) then | 113 | |
127 | backdrop = rb.read_bmp_file("/.rockbox/icons/viewers.bmp") -- Try using the builtin version | ||
128 | end | ||
129 | end | ||
130 | -- Draws the image using our own draw_image() function; see up | 114 | -- Draws the image using our own draw_image() function; see up |
131 | draw_image(backdrop) | 115 | draw_image(backdrop) |
132 | 116 | ||
@@ -134,7 +118,7 @@ draw_image(backdrop) | |||
134 | rb.lcd_update() | 118 | rb.lcd_update() |
135 | 119 | ||
136 | -- Sleep for 2 seconds | 120 | -- Sleep for 2 seconds |
137 | seconds = 2 | 121 | local seconds = 2 |
138 | rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second | 122 | rb.sleep(seconds * rb.HZ) -- rb.HZ equals to the amount of ticks that fit in 1 second |
139 | 123 | ||
140 | -- Call the function sayhello() with arguments seconds | 124 | -- Call the function sayhello() with arguments seconds |
@@ -152,14 +136,18 @@ file_put_contents(pathname, "Hello from Lua!") | |||
152 | -- Splash the contents of pathname | 136 | -- Splash the contents of pathname |
153 | printf(file_get_contents(pathname)) | 137 | printf(file_get_contents(pathname)) |
154 | 138 | ||
155 | line = line + 1 -- empty line | 139 | line = line + 1 -- Add empty line |
140 | |||
141 | -- Some examples of how bitlib works | ||
142 | printf("1 | 2 = %d <> 7 & 3 = %d", bit.bor(1, 2), bit.band(7, 3)) | ||
143 | printf("~8 = %d <> 5 ^ 1 = %d", bit.bnot(8), bit.bxor(5, 1)) | ||
144 | |||
145 | line = line + 1 -- Add empty line | ||
156 | 146 | ||
157 | -- Display some long lines | 147 | -- Display some long lines |
158 | local tmp = "" | 148 | for i=0, 4 do |
159 | for i=1, 5 do | 149 | printf("This is a %slong line!", string.rep("very very very ", i)) |
160 | printf("This is a %s long line!", tmp) | 150 | rb.yield() -- Always try to yield to give other threads some breathing space! |
161 | tmp = tmp .. " very very very" | ||
162 | rb.yield() | ||
163 | end | 151 | end |
164 | 152 | ||
165 | -- Loop until the user exits the program | 153 | -- Loop until the user exits the program |
diff --git a/apps/plugins/lua/SOURCES b/apps/plugins/lua/SOURCES index 5755b6c42e..56c029921d 100644 --- a/apps/plugins/lua/SOURCES +++ b/apps/plugins/lua/SOURCES | |||
@@ -8,6 +8,7 @@ ldo.c | |||
8 | ldump.c | 8 | ldump.c |
9 | lfunc.c | 9 | lfunc.c |
10 | lgc.c | 10 | lgc.c |
11 | liolib.c | ||
11 | llex.c | 12 | llex.c |
12 | lmem.c | 13 | lmem.c |
13 | loadlib.c | 14 | loadlib.c |
diff --git a/apps/plugins/lua/liolib.c b/apps/plugins/lua/liolib.c new file mode 100644 index 0000000000..2ea52b854b --- /dev/null +++ b/apps/plugins/lua/liolib.c | |||
@@ -0,0 +1,479 @@ | |||
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 | |||
21 | |||
22 | |||
23 | #define IO_INPUT 1 | ||
24 | #define IO_OUTPUT 2 | ||
25 | |||
26 | |||
27 | static const char *const fnames[] = {"input", "output"}; | ||
28 | |||
29 | |||
30 | static int pushresult (lua_State *L, int i, const char *filename) { | ||
31 | int en = errno; | ||
32 | if (i) { | ||
33 | lua_pushboolean(L, 1); | ||
34 | return 1; | ||
35 | } | ||
36 | else { | ||
37 | lua_pushnil(L); | ||
38 | if (filename) | ||
39 | lua_pushfstring(L, "%s: %s", filename, strerror(en)); | ||
40 | else | ||
41 | lua_pushfstring(L, "%s", strerror(en)); | ||
42 | lua_pushinteger(L, 0); | ||
43 | return 3; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | |||
48 | static void fileerror (lua_State *L, int arg, const char *filename) { | ||
49 | lua_pushfstring(L, "%s: %s", filename, strerror(errno)); | ||
50 | luaL_argerror(L, arg, lua_tostring(L, -1)); | ||
51 | } | ||
52 | |||
53 | |||
54 | static int io_type (lua_State *L) { | ||
55 | void *ud; | ||
56 | luaL_checkany(L, 1); | ||
57 | ud = lua_touserdata(L, 1); | ||
58 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); | ||
59 | if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) | ||
60 | lua_pushnil(L); /* not a file */ | ||
61 | else if (*((int *)ud) < 0) | ||
62 | lua_pushliteral(L, "closed file"); | ||
63 | else | ||
64 | lua_pushliteral(L, "file"); | ||
65 | return 1; | ||
66 | } | ||
67 | |||
68 | |||
69 | static int* tofile (lua_State *L) { | ||
70 | int *f = (int*) luaL_checkudata(L, 1, LUA_FILEHANDLE); | ||
71 | if (*f < 0) | ||
72 | luaL_error(L, "attempt to use a closed file"); | ||
73 | return f; | ||
74 | } | ||
75 | |||
76 | |||
77 | |||
78 | /* | ||
79 | ** When creating file handles, always creates a `closed' file handle | ||
80 | ** before opening the actual file; so, if there is a memory error, the | ||
81 | ** file is not left opened. | ||
82 | */ | ||
83 | static int* newfile (lua_State *L) { | ||
84 | int *pf = (int *)lua_newuserdata(L, sizeof(int)); | ||
85 | *pf = -1; /* file handle is currently `closed' */ | ||
86 | luaL_getmetatable(L, LUA_FILEHANDLE); | ||
87 | lua_setmetatable(L, -2); | ||
88 | return pf; | ||
89 | } | ||
90 | |||
91 | |||
92 | /* | ||
93 | ** function to close regular files | ||
94 | */ | ||
95 | static int io_fclose (lua_State *L) { | ||
96 | int *p = tofile(L); | ||
97 | int ok = (rb->close(*p) == 0); | ||
98 | *p = -1; | ||
99 | return pushresult(L, ok, NULL); | ||
100 | } | ||
101 | |||
102 | |||
103 | static inline int aux_close (lua_State *L) { | ||
104 | return io_fclose(L); | ||
105 | } | ||
106 | |||
107 | |||
108 | static int io_close (lua_State *L) { | ||
109 | if (lua_isnone(L, 1)) | ||
110 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); | ||
111 | tofile(L); /* make sure argument is a file */ | ||
112 | return aux_close(L); | ||
113 | } | ||
114 | |||
115 | |||
116 | static int io_gc (lua_State *L) { | ||
117 | int f = *(int*) luaL_checkudata(L, 1, LUA_FILEHANDLE); | ||
118 | /* ignore closed files */ | ||
119 | if (f >= 0) | ||
120 | aux_close(L); | ||
121 | return 0; | ||
122 | } | ||
123 | |||
124 | |||
125 | static int io_tostring (lua_State *L) { | ||
126 | int f = *(int*) luaL_checkudata(L, 1, LUA_FILEHANDLE); | ||
127 | if (f < 0) | ||
128 | lua_pushliteral(L, "file (closed)"); | ||
129 | else | ||
130 | lua_pushfstring(L, "file (%d)", f); | ||
131 | return 1; | ||
132 | } | ||
133 | |||
134 | |||
135 | static int io_open (lua_State *L) { | ||
136 | const char *filename = luaL_checkstring(L, 1); | ||
137 | const char *mode = luaL_optstring(L, 2, "r"); | ||
138 | int *pf = newfile(L); | ||
139 | int flags = 0; | ||
140 | if(*(mode+1) == '+') { | ||
141 | flags = O_RDWR; | ||
142 | switch(*mode) { | ||
143 | case 'w': | ||
144 | flags |= O_TRUNC; break; | ||
145 | case 'a': | ||
146 | flags |= O_APPEND; break; | ||
147 | } | ||
148 | } | ||
149 | else { | ||
150 | switch(*mode) { | ||
151 | case 'r': | ||
152 | flags = O_RDONLY; break; | ||
153 | case 'w': | ||
154 | flags = O_WRONLY; break; | ||
155 | case 'a': | ||
156 | flags = O_WRONLY | O_APPEND; break; | ||
157 | } | ||
158 | } | ||
159 | if(!rb->file_exists(filename)) | ||
160 | flags |= O_CREAT; | ||
161 | *pf = rb->open(filename, flags); | ||
162 | return (*pf < 0) ? pushresult(L, 0, filename) : 1; | ||
163 | } | ||
164 | |||
165 | |||
166 | static int* getiofile (lua_State *L, int findex) { | ||
167 | int *f; | ||
168 | lua_rawgeti(L, LUA_ENVIRONINDEX, findex); | ||
169 | f = (int *)lua_touserdata(L, -1); | ||
170 | if (f == NULL || *f < 0) | ||
171 | luaL_error(L, "standard %s file is closed", fnames[findex - 1]); | ||
172 | return f; | ||
173 | } | ||
174 | |||
175 | |||
176 | static int g_iofile (lua_State *L, int f, int flags) { | ||
177 | if (!lua_isnoneornil(L, 1)) { | ||
178 | const char *filename = lua_tostring(L, 1); | ||
179 | if (filename) { | ||
180 | int *pf = newfile(L); | ||
181 | *pf = rb->open(filename, flags); | ||
182 | if (*pf < 0) | ||
183 | fileerror(L, 1, filename); | ||
184 | } | ||
185 | else { | ||
186 | tofile(L); /* check that it's a valid file handle */ | ||
187 | lua_pushvalue(L, 1); | ||
188 | } | ||
189 | lua_rawseti(L, LUA_ENVIRONINDEX, f); | ||
190 | } | ||
191 | /* return current value */ | ||
192 | lua_rawgeti(L, LUA_ENVIRONINDEX, f); | ||
193 | return 1; | ||
194 | } | ||
195 | |||
196 | |||
197 | static int io_input (lua_State *L) { | ||
198 | return g_iofile(L, IO_INPUT, O_RDONLY); | ||
199 | } | ||
200 | |||
201 | |||
202 | static int io_output (lua_State *L) { | ||
203 | return g_iofile(L, IO_OUTPUT, O_WRONLY); | ||
204 | } | ||
205 | |||
206 | |||
207 | static int io_readline (lua_State *L); | ||
208 | |||
209 | |||
210 | static void aux_lines (lua_State *L, int idx, int toclose) { | ||
211 | lua_pushvalue(L, idx); | ||
212 | lua_pushboolean(L, toclose); /* close/not close file when finished */ | ||
213 | lua_pushcclosure(L, io_readline, 2); | ||
214 | } | ||
215 | |||
216 | |||
217 | static int f_lines (lua_State *L) { | ||
218 | tofile(L); /* check that it's a valid file handle */ | ||
219 | aux_lines(L, 1, 0); | ||
220 | return 1; | ||
221 | } | ||
222 | |||
223 | |||
224 | static int io_lines (lua_State *L) { | ||
225 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ | ||
226 | /* will iterate over default input */ | ||
227 | lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); | ||
228 | return f_lines(L); | ||
229 | } | ||
230 | else { | ||
231 | const char *filename = luaL_checkstring(L, 1); | ||
232 | int *pf = newfile(L); | ||
233 | *pf = rb->open(filename, O_RDONLY); | ||
234 | if (*pf < 0) | ||
235 | fileerror(L, 1, filename); | ||
236 | aux_lines(L, lua_gettop(L), 1); | ||
237 | return 1; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | |||
242 | /* | ||
243 | ** {====================================================== | ||
244 | ** READ | ||
245 | ** ======================================================= | ||
246 | */ | ||
247 | |||
248 | |||
249 | static int read_number (lua_State *L, int *f) { | ||
250 | char buf[10]; /* Maximum uint32 value is 10 chars long */ | ||
251 | lua_Number d; | ||
252 | int i = 0; | ||
253 | /* Rather hackish, but we don't have fscanf. | ||
254 | Was: fscanf(f, LUA_NUMBER_SCAN, &d); */ | ||
255 | memset(buf, 0, 10); | ||
256 | rb->read(*f, buf, 10); | ||
257 | while(isdigit(buf[i]) && i < 10) | ||
258 | i++; | ||
259 | if(i == 0) return 0; | ||
260 | else { | ||
261 | rb->lseek(*f, i-10, SEEK_CUR); | ||
262 | d = atoi(buf); | ||
263 | lua_pushnumber(L, d); | ||
264 | return 1; | ||
265 | } | ||
266 | } | ||
267 | |||
268 | |||
269 | static int test_eof (lua_State *L, int *f) { | ||
270 | ssize_t s = rb->lseek(*f, 0, SEEK_CUR); | ||
271 | lua_pushlstring(L, NULL, 0); | ||
272 | return s != rb->filesize(*f); | ||
273 | } | ||
274 | |||
275 | |||
276 | /* Rockbox already defines read_line() */ | ||
277 | static int _read_line (lua_State *L, int *f) { | ||
278 | luaL_Buffer b; | ||
279 | luaL_buffinit(L, &b); | ||
280 | for (;;) { | ||
281 | size_t l; | ||
282 | size_t r; | ||
283 | char *p = luaL_prepbuffer(&b); | ||
284 | r = rb->read_line(*f, p, LUAL_BUFFERSIZE); | ||
285 | l = strlen(p); | ||
286 | if (l == 0 || p[l-1] != '\n') | ||
287 | luaL_addsize(&b, l); | ||
288 | else { | ||
289 | luaL_addsize(&b, l - 1); /* do not include `eol' */ | ||
290 | luaL_pushresult(&b); /* close buffer */ | ||
291 | return 1; /* read at least an `eol' */ | ||
292 | } | ||
293 | if (r < LUAL_BUFFERSIZE) { /* eof? */ | ||
294 | luaL_pushresult(&b); /* close buffer */ | ||
295 | return (lua_objlen(L, -1) > 0); /* check whether read something */ | ||
296 | } | ||
297 | } | ||
298 | } | ||
299 | |||
300 | |||
301 | static int read_chars (lua_State *L, int *f, size_t n) { | ||
302 | size_t rlen; /* how much to read */ | ||
303 | size_t nr; /* number of chars actually read */ | ||
304 | luaL_Buffer b; | ||
305 | luaL_buffinit(L, &b); | ||
306 | rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ | ||
307 | do { | ||
308 | char *p = luaL_prepbuffer(&b); | ||
309 | if (rlen > n) rlen = n; /* cannot read more than asked */ | ||
310 | nr = rb->read(*f, p, rlen); | ||
311 | luaL_addsize(&b, nr); | ||
312 | n -= nr; /* still have to read `n' chars */ | ||
313 | } while (n > 0 && nr == rlen); /* until end of count or eof */ | ||
314 | luaL_pushresult(&b); /* close buffer */ | ||
315 | return (n == 0 || lua_objlen(L, -1) > 0); | ||
316 | } | ||
317 | |||
318 | |||
319 | static int g_read (lua_State *L, int *f, int first) { | ||
320 | int nargs = lua_gettop(L) - 1; | ||
321 | int success; | ||
322 | int n; | ||
323 | if (nargs == 0) { /* no arguments? */ | ||
324 | success = _read_line(L, f); | ||
325 | n = first+1; /* to return 1 result */ | ||
326 | } | ||
327 | else { /* ensure stack space for all results and for auxlib's buffer */ | ||
328 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); | ||
329 | success = 1; | ||
330 | for (n = first; nargs-- && success; n++) { | ||
331 | if (lua_type(L, n) == LUA_TNUMBER) { | ||
332 | size_t l = (size_t)lua_tointeger(L, n); | ||
333 | success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); | ||
334 | } | ||
335 | else { | ||
336 | const char *p = lua_tostring(L, n); | ||
337 | luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); | ||
338 | switch (p[1]) { | ||
339 | case 'n': /* number */ | ||
340 | success = read_number(L, f); | ||
341 | break; | ||
342 | case 'l': /* line */ | ||
343 | success = _read_line(L, f); | ||
344 | break; | ||
345 | case 'a': /* file */ | ||
346 | read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ | ||
347 | success = 1; /* always success */ | ||
348 | break; | ||
349 | default: | ||
350 | return luaL_argerror(L, n, "invalid format"); | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | if (!success) { | ||
356 | lua_pop(L, 1); /* remove last result */ | ||
357 | lua_pushnil(L); /* push nil instead */ | ||
358 | } | ||
359 | return n - first; | ||
360 | } | ||
361 | |||
362 | |||
363 | static int io_read (lua_State *L) { | ||
364 | return g_read(L, getiofile(L, IO_INPUT), 1); | ||
365 | } | ||
366 | |||
367 | |||
368 | static int f_read (lua_State *L) { | ||
369 | return g_read(L, tofile(L), 2); | ||
370 | } | ||
371 | |||
372 | |||
373 | static int io_readline (lua_State *L) { | ||
374 | int *f = (int *) lua_touserdata(L, lua_upvalueindex(1)); | ||
375 | int sucess; | ||
376 | if (*f < 0) /* file is already closed? */ | ||
377 | luaL_error(L, "file is already closed"); | ||
378 | sucess = _read_line(L, f); | ||
379 | if (sucess) return 1; | ||
380 | else { /* EOF */ | ||
381 | if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ | ||
382 | lua_settop(L, 0); | ||
383 | lua_pushvalue(L, lua_upvalueindex(1)); | ||
384 | aux_close(L); /* close it */ | ||
385 | } | ||
386 | return 0; | ||
387 | } | ||
388 | } | ||
389 | |||
390 | /* }====================================================== */ | ||
391 | |||
392 | |||
393 | static int g_write (lua_State *L, int *f, int arg) { | ||
394 | int nargs = lua_gettop(L) - 1; | ||
395 | int status = 1; | ||
396 | for (; nargs--; arg++) { | ||
397 | if (lua_type(L, arg) == LUA_TNUMBER) { | ||
398 | /* optimization: could be done exactly as for strings */ | ||
399 | status = status && | ||
400 | rb->fdprintf(*f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; | ||
401 | } | ||
402 | else { | ||
403 | size_t l; | ||
404 | const char *s = luaL_checklstring(L, arg, &l); | ||
405 | status = status && (rb->write(*f, s, l) == (ssize_t)l); | ||
406 | } | ||
407 | } | ||
408 | return pushresult(L, status, NULL); | ||
409 | } | ||
410 | |||
411 | |||
412 | static int io_write (lua_State *L) { | ||
413 | return g_write(L, getiofile(L, IO_OUTPUT), 1); | ||
414 | } | ||
415 | |||
416 | |||
417 | static int f_write (lua_State *L) { | ||
418 | return g_write(L, tofile(L), 2); | ||
419 | } | ||
420 | |||
421 | |||
422 | static int f_seek (lua_State *L) { | ||
423 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | ||
424 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | ||
425 | int f = *tofile(L); | ||
426 | int op = luaL_checkoption(L, 2, "cur", modenames); | ||
427 | long offset = luaL_optlong(L, 3, 0); | ||
428 | op = rb->lseek(f, offset, mode[op]); | ||
429 | if (op) | ||
430 | return pushresult(L, 0, NULL); /* error */ | ||
431 | else { | ||
432 | lua_pushinteger(L, rb->lseek(f, 0, SEEK_CUR)); | ||
433 | return 1; | ||
434 | } | ||
435 | } | ||
436 | |||
437 | |||
438 | static const luaL_Reg iolib[] = { | ||
439 | {"close", io_close}, | ||
440 | {"input", io_input}, | ||
441 | {"lines", io_lines}, | ||
442 | {"open", io_open}, | ||
443 | {"output", io_output}, | ||
444 | {"read", io_read}, | ||
445 | {"type", io_type}, | ||
446 | {"write", io_write}, | ||
447 | {NULL, NULL} | ||
448 | }; | ||
449 | |||
450 | |||
451 | static const luaL_Reg flib[] = { | ||
452 | {"close", io_close}, | ||
453 | {"lines", f_lines}, | ||
454 | {"read", f_read}, | ||
455 | {"seek", f_seek}, | ||
456 | {"write", f_write}, | ||
457 | {"__gc", io_gc}, | ||
458 | {"__tostring", io_tostring}, | ||
459 | {NULL, NULL} | ||
460 | }; | ||
461 | |||
462 | |||
463 | static void createmeta (lua_State *L) { | ||
464 | luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ | ||
465 | lua_pushvalue(L, -1); /* push metatable */ | ||
466 | lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ | ||
467 | luaL_register(L, NULL, flib); /* file methods */ | ||
468 | } | ||
469 | |||
470 | |||
471 | LUALIB_API int luaopen_io (lua_State *L) { | ||
472 | createmeta(L); | ||
473 | lua_replace(L, LUA_ENVIRONINDEX); | ||
474 | /* open library */ | ||
475 | luaL_register(L, LUA_IOLIBNAME, iolib); | ||
476 | /* create (and set) default files */ | ||
477 | lua_pop(L, 1); /* pop environment for default files */ | ||
478 | return 1; | ||
479 | } | ||
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index f22bd01b48..6da57d154e 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c | |||
@@ -685,62 +685,6 @@ RB_WRAP(backlight_set_brightness) | |||
685 | } | 685 | } |
686 | #endif | 686 | #endif |
687 | 687 | ||
688 | RB_WRAP(open) | ||
689 | { | ||
690 | const char* pathname = luaL_checkstring(L, 1); | ||
691 | int flags = luaL_checkint(L, 2); | ||
692 | int result = rb->open(pathname, flags); | ||
693 | lua_pushinteger(L, result); | ||
694 | return 1; | ||
695 | } | ||
696 | |||
697 | RB_WRAP(close) | ||
698 | { | ||
699 | int fd = luaL_checkint(L, 1); | ||
700 | int result = rb->close(fd); | ||
701 | lua_pushinteger(L, result); | ||
702 | return 1; | ||
703 | } | ||
704 | |||
705 | RB_WRAP(read) | ||
706 | { | ||
707 | size_t len, n, result = 0; | ||
708 | luaL_Buffer b; | ||
709 | |||
710 | int fd = luaL_checkint(L, 1); | ||
711 | size_t count = luaL_checkint(L, 2); | ||
712 | |||
713 | luaL_buffinit(L, &b); | ||
714 | len = LUAL_BUFFERSIZE; | ||
715 | do | ||
716 | { | ||
717 | char *p = luaL_prepbuffer(&b); | ||
718 | |||
719 | if (len > count) | ||
720 | len = count; | ||
721 | |||
722 | n = rb->read(fd, p, len); | ||
723 | |||
724 | luaL_addsize(&b, n); | ||
725 | count -= n; | ||
726 | result += n; | ||
727 | } while (count > 0 && n == len); | ||
728 | luaL_pushresult(&b); /* close buffer */ | ||
729 | |||
730 | lua_pushinteger(L, result); | ||
731 | return 2; | ||
732 | } | ||
733 | |||
734 | RB_WRAP(lseek) | ||
735 | { | ||
736 | int fd = luaL_checkint(L, 1); | ||
737 | off_t offset = luaL_checkint(L, 2); | ||
738 | int whence = luaL_checkint(L, 3); | ||
739 | off_t result = rb->lseek(fd, offset, whence); | ||
740 | lua_pushinteger(L, result); | ||
741 | return 1; | ||
742 | } | ||
743 | |||
744 | RB_WRAP(creat) | 688 | RB_WRAP(creat) |
745 | { | 689 | { |
746 | const char* pathname = luaL_checkstring(L, 1); | 690 | const char* pathname = luaL_checkstring(L, 1); |
@@ -749,16 +693,6 @@ RB_WRAP(creat) | |||
749 | return 1; | 693 | return 1; |
750 | } | 694 | } |
751 | 695 | ||
752 | RB_WRAP(write) | ||
753 | { | ||
754 | size_t count; | ||
755 | int fd = luaL_checkint(L, 1); | ||
756 | void* buf = (void*)luaL_checklstring(L, 2, &count); | ||
757 | ssize_t result = rb->write(fd, buf, count); | ||
758 | lua_pushinteger(L, result); | ||
759 | return 1; | ||
760 | } | ||
761 | |||
762 | RB_WRAP(remove) | 696 | RB_WRAP(remove) |
763 | { | 697 | { |
764 | const char* pathname = luaL_checkstring(L, 1); | 698 | const char* pathname = luaL_checkstring(L, 1); |
@@ -776,23 +710,6 @@ RB_WRAP(rename) | |||
776 | return 1; | 710 | return 1; |
777 | } | 711 | } |
778 | 712 | ||
779 | RB_WRAP(ftruncate) | ||
780 | { | ||
781 | int fd = luaL_checkint(L, 1); | ||
782 | off_t length = luaL_checkint(L, 2); | ||
783 | int result = rb->ftruncate(fd, length); | ||
784 | lua_pushinteger(L, result); | ||
785 | return 1; | ||
786 | } | ||
787 | |||
788 | RB_WRAP(filesize) | ||
789 | { | ||
790 | int fd = luaL_checkint(L, 1); | ||
791 | off_t result = rb->filesize(fd); | ||
792 | lua_pushinteger(L, result); | ||
793 | return 1; | ||
794 | } | ||
795 | |||
796 | RB_WRAP(file_exists) | 713 | RB_WRAP(file_exists) |
797 | { | 714 | { |
798 | const char* path = luaL_checkstring(L, 1); | 715 | const char* path = luaL_checkstring(L, 1); |
@@ -940,16 +857,9 @@ static const luaL_Reg rocklib[] = | |||
940 | #endif | 857 | #endif |
941 | 858 | ||
942 | /* File handling */ | 859 | /* File handling */ |
943 | R(open), | ||
944 | R(close), | ||
945 | R(read), | ||
946 | R(lseek), | ||
947 | R(creat), | 860 | R(creat), |
948 | R(write), | ||
949 | R(remove), | 861 | R(remove), |
950 | R(rename), | 862 | R(rename), |
951 | R(ftruncate), | ||
952 | R(filesize), | ||
953 | R(file_exists), | 863 | R(file_exists), |
954 | 864 | ||
955 | /* Kernel */ | 865 | /* Kernel */ |
@@ -1008,16 +918,6 @@ LUALIB_API int luaopen_rock(lua_State *L) | |||
1008 | RB_CONSTANT(LCD_WIDTH); | 918 | RB_CONSTANT(LCD_WIDTH); |
1009 | RB_CONSTANT(LCD_HEIGHT); | 919 | RB_CONSTANT(LCD_HEIGHT); |
1010 | 920 | ||
1011 | RB_CONSTANT(O_RDONLY); | ||
1012 | RB_CONSTANT(O_WRONLY); | ||
1013 | RB_CONSTANT(O_RDWR); | ||
1014 | RB_CONSTANT(O_CREAT); | ||
1015 | RB_CONSTANT(O_APPEND); | ||
1016 | RB_CONSTANT(O_TRUNC); | ||
1017 | RB_CONSTANT(SEEK_SET); | ||
1018 | RB_CONSTANT(SEEK_CUR); | ||
1019 | RB_CONSTANT(SEEK_END); | ||
1020 | |||
1021 | RB_CONSTANT(FONT_SYSFIXED); | 921 | RB_CONSTANT(FONT_SYSFIXED); |
1022 | RB_CONSTANT(FONT_UI); | 922 | RB_CONSTANT(FONT_UI); |
1023 | 923 | ||
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c index cc99f9236e..266fa4305e 100644 --- a/apps/plugins/lua/rocklua.c +++ b/apps/plugins/lua/rocklua.c | |||
@@ -36,6 +36,7 @@ static const luaL_Reg lualibs[] = { | |||
36 | {LUA_OSLIBNAME, luaopen_os}, | 36 | {LUA_OSLIBNAME, luaopen_os}, |
37 | {LUA_ROCKLIBNAME, luaopen_rock}, | 37 | {LUA_ROCKLIBNAME, luaopen_rock}, |
38 | {LUA_BITLIBNAME, luaopen_bit}, | 38 | {LUA_BITLIBNAME, luaopen_bit}, |
39 | {LUA_IOLIBNAME, luaopen_io}, | ||
39 | {LUA_LOADLIBNAME, luaopen_package}, | 40 | {LUA_LOADLIBNAME, luaopen_package}, |
40 | {NULL, NULL} | 41 | {NULL, NULL} |
41 | }; | 42 | }; |