Skip to content

Latest commit

 

History

History
996 lines (824 loc) · 29.1 KB

lua_glue.c

File metadata and controls

996 lines (824 loc) · 29.1 KB
 
Dec 2, 2006
Dec 2, 2006
1
#include "universal.h"
Dec 9, 2006
Dec 9, 2006
2
#include "lua_glue.h"
Dec 3, 2006
Dec 3, 2006
3
#include "platform.h"
Dec 2, 2006
Dec 2, 2006
4
5
6
7
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
Dec 6, 2006
Dec 6, 2006
8
#include "gui.h"
Dec 2, 2006
Dec 2, 2006
9
10
11
12
13
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
Dec 9, 2006
Dec 9, 2006
14
15
16
17
18
19
20
21
22
23
24
25
// Allocator interface for internal Lua use.
static void *MojoLua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
if (nsize == 0)
{
free(ptr);
return NULL;
} // if
return xrealloc(ptr, nsize);
} // MojoLua_alloc
Dec 2, 2006
Dec 2, 2006
26
27
28
29
// Read data from a MojoInput when loading Lua code.
static const char *MojoLua_reader(lua_State *L, void *data, size_t *size)
{
MojoInput *in = (MojoInput *) data;
Dec 9, 2006
Dec 9, 2006
30
char *retval = (char *) scratchbuf_128k;
Dec 2, 2006
Dec 2, 2006
31
32
33
34
35
36
37
38
39
40
41
42
int64 br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
if (br <= 0) // eof or error? (lua doesn't care which?!)
{
br = 0;
retval = NULL;
} // if
*size = (size_t) br;
return retval;
} // MojoLua_reader
Dec 10, 2006
Dec 10, 2006
43
44
45
46
47
48
49
50
51
52
53
54
55
static inline int snprintfcat(char **ptr, size_t *len, const char *fmt, ...)
{
int bw = 0;
va_list ap;
va_start(ap, fmt);
bw = vsnprintf(*ptr, *len, fmt, ap);
va_end(ap);
*ptr += bw;
*len -= bw;
return bw;
} // snprintfcat
Dec 10, 2006
Dec 10, 2006
56
57
58
59
60
61
62
static int luahook_stackwalk(lua_State *L)
{
const char *errstr = lua_tostring(L, 1);
lua_Debug ldbg;
int i = 0;
if (errstr != NULL)
Dec 10, 2006
Dec 10, 2006
63
logDebug("%s\n", errstr);
Dec 10, 2006
Dec 10, 2006
64
Dec 10, 2006
Dec 10, 2006
65
logDebug("Lua stack backtrace:");
Dec 10, 2006
Dec 10, 2006
66
67
68
69
// start at 1 to skip this function.
for (i = 1; lua_getstack(L, i, &ldbg); i++)
{
Dec 10, 2006
Dec 10, 2006
70
71
72
char *ptr = (char *) scratchbuf_128k;
size_t len = sizeof (scratchbuf_128k);
int bw = snprintfcat(&ptr, &len, "#%d", i-1);
Dec 10, 2006
Dec 10, 2006
73
74
75
const int maxspacing = 4;
int spacing = maxspacing - bw;
while (spacing-- > 0)
Dec 10, 2006
Dec 10, 2006
76
snprintfcat(&ptr, &len, " ");
Dec 10, 2006
Dec 10, 2006
77
78
79
if (!lua_getinfo(L, "nSl", &ldbg))
{
Dec 10, 2006
Dec 10, 2006
80
81
snprintfcat(&ptr, &len, "???\n");
logDebug((const char *) scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
82
83
84
85
continue;
} // if
if (ldbg.namewhat[0])
Dec 10, 2006
Dec 10, 2006
86
snprintfcat(&ptr, &len, "%s ", ldbg.namewhat);
Dec 10, 2006
Dec 10, 2006
87
88
if ((ldbg.name) && (ldbg.name[0]))
Dec 10, 2006
Dec 10, 2006
89
snprintfcat(&ptr, &len, "function %s ()", ldbg.name);
Dec 10, 2006
Dec 10, 2006
90
91
92
else
{
if (strcmp(ldbg.what, "main") == 0)
Dec 10, 2006
Dec 10, 2006
93
snprintfcat(&ptr, &len, "mainline of chunk");
Dec 10, 2006
Dec 10, 2006
94
else if (strcmp(ldbg.what, "tail") == 0)
Dec 10, 2006
Dec 10, 2006
95
snprintfcat(&ptr, &len, "tail call");
Dec 10, 2006
Dec 10, 2006
96
else
Dec 10, 2006
Dec 10, 2006
97
snprintfcat(&ptr, &len, "unidentifiable function");
Dec 10, 2006
Dec 10, 2006
98
99
} // if
Dec 10, 2006
Dec 10, 2006
100
101
102
103
logDebug((const char *) scratchbuf_128k);
ptr = (char *) scratchbuf_128k;
len = sizeof (scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
104
for (spacing = 0; spacing < maxspacing; spacing++)
Dec 10, 2006
Dec 10, 2006
105
snprintfcat(&ptr, &len, " ");
Dec 10, 2006
Dec 10, 2006
106
107
if (strcmp(ldbg.what, "C") == 0)
Dec 10, 2006
Dec 10, 2006
108
snprintfcat(&ptr, &len, "in native code");
Dec 10, 2006
Dec 10, 2006
109
else if (strcmp(ldbg.what, "tail") == 0)
Dec 10, 2006
Dec 10, 2006
110
snprintfcat(&ptr, &len, "in Lua code");
Dec 10, 2006
Dec 10, 2006
111
112
else if ( (strcmp(ldbg.source, "=?") == 0) && (ldbg.currentline == 0) )
snprintfcat(&ptr, &len, "in Lua code (debug info stripped)");
Dec 10, 2006
Dec 10, 2006
113
114
else
{
Dec 11, 2006
Dec 11, 2006
115
snprintfcat(&ptr, &len, "in Lua code at %s", ldbg.short_src);
Dec 10, 2006
Dec 10, 2006
116
if (ldbg.currentline != -1)
Dec 10, 2006
Dec 10, 2006
117
snprintfcat(&ptr, &len, ":%d", ldbg.currentline);
Dec 10, 2006
Dec 10, 2006
118
} // else
Dec 10, 2006
Dec 10, 2006
119
logDebug((const char *) scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
120
121
122
123
124
125
126
} // for
lua_pushstring(L, errstr ? errstr : "");
return 1;
} // luahook_stackwalk
Dec 11, 2006
Dec 11, 2006
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// This just lets you punch in one-liners and Lua will run them as individual
// chunks, but you can completely access all Lua state, including calling C
// functions and altering tables. At this time, it's more of a "console"
// than a debugger. You can do "p MojoLua_debugger()" from gdb to launch this
// from a breakpoint in native code, or call MojoSetup.debugger() to launch
// it from Lua code (with stacktrace intact, too: type 'bt' to see it).
static int luahook_debugger(lua_State *L)
{
#if DISABLE_LUA_PARSER
logError("Lua debugger is disabled in this build (no parser).");
#else
int origtop;
lua_pushcfunction(luaState, luahook_stackwalk);
origtop = lua_gettop(L);
printf("Quick and dirty Lua debugger. Type 'exit' to quit.\n");
while (true)
{
char *buf = (char *) scratchbuf_128k;
int len = 0;
printf("> ");
fflush(stdout);
if (fgets(buf, sizeof (scratchbuf_128k), stdin) == NULL)
{
printf("\n\n fgets() on stdin failed: ");
break;
} // if
len = (int) (strlen(buf) - 1);
while ( (len >= 0) && ((buf[len] == '\n') || (buf[len] == '\r')) )
buf[len--] = '\0';
if (strcmp(buf, "q") == 0)
break;
else if (strcmp(buf, "exit") == 0)
break;
else if (strcmp(buf, "bt") == 0)
strcpy(buf, "MojoSetup.stackwalk()");
if ( (luaL_loadstring(L, buf) != 0) ||
(lua_pcall(luaState, 0, LUA_MULTRET, -2) != 0) )
{
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
} // if
else
{
printf("Returned %d values.\n", lua_gettop(L) - origtop);
while (lua_gettop(L) != origtop)
{
// !!! FIXME: dump details of values to stdout here.
lua_pop(L, 1);
} // while
printf("\n");
} // else
} // while
lua_pop(L, 1);
printf("exiting debugger...\n");
#endif
Dec 12, 2006
Dec 12, 2006
189
190
return 0;
Dec 11, 2006
Dec 11, 2006
191
192
193
194
195
196
197
198
199
} // luahook_debugger
void MojoLua_debugger(void)
{
luahook_debugger(luaState);
} // MojoLua_debugger
Dec 3, 2006
Dec 3, 2006
200
201
boolean MojoLua_runFile(const char *basefname)
{
Dec 9, 2006
Dec 9, 2006
202
203
MojoArchive *ar = GBaseArchive; // in case we want to generalize later.
const MojoArchiveEntryInfo *entinfo;
Dec 3, 2006
Dec 3, 2006
204
boolean retval = false;
Dec 9, 2006
Dec 9, 2006
205
206
char clua[128]; // compiled filename.
char ulua[128]; // uncompiled filename.
Dec 3, 2006
Dec 3, 2006
207
208
209
int rc = 0;
MojoInput *io = NULL;
Dec 9, 2006
Dec 9, 2006
210
211
if (snprintf(clua, sizeof (clua), "%s.luac", basefname) >= sizeof (clua))
return false;
Dec 3, 2006
Dec 3, 2006
212
Dec 9, 2006
Dec 9, 2006
213
214
215
216
if (snprintf(ulua, sizeof (ulua), "%s.lua", basefname) >= sizeof (ulua))
return false;
if (ar->enumerate(ar, "lua"))
Dec 3, 2006
Dec 3, 2006
217
{
Dec 9, 2006
Dec 9, 2006
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
while ((entinfo = ar->enumNext(ar)) != NULL)
{
boolean match = (strcmp(entinfo->filename, clua) == 0);
#if !DISABLE_LUA_PARSER
if (!match)
match = (strcmp(entinfo->filename, ulua) == 0);
#endif
if (match)
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
io = ar->openCurrentEntry(ar);
break;
} // if
} // while
Dec 3, 2006
Dec 3, 2006
233
234
235
236
} // if
if (io != NULL)
{
Dec 11, 2006
Dec 11, 2006
237
238
char *realfname = xmalloc(strlen(entinfo->filename) + 6);
sprintf(realfname, "@lua/%s", entinfo->filename);
Dec 10, 2006
Dec 10, 2006
239
lua_pushcfunction(luaState, luahook_stackwalk);
Dec 11, 2006
Dec 11, 2006
240
241
rc = lua_load(luaState, MojoLua_reader, io, realfname);
free(realfname);
Dec 3, 2006
Dec 3, 2006
242
243
244
245
246
247
io->close(io);
if (rc != 0)
lua_error(luaState);
else
{
Dec 11, 2006
Dec 11, 2006
248
// Call new chunk on top of the stack (lua_pcall will pop it off).
Dec 10, 2006
Dec 10, 2006
249
250
251
252
if (lua_pcall(luaState, 0, 0, -2) != 0) // retvals are dumped.
lua_error(luaState); // error on stack has debug info.
else
retval = true; // if this didn't panic, we succeeded.
Dec 3, 2006
Dec 3, 2006
253
} // if
Dec 10, 2006
Dec 10, 2006
254
lua_pop(luaState, 1); // dump stackwalker.
Dec 3, 2006
Dec 3, 2006
255
256
257
258
259
260
} // if
return retval;
} // MojoLua_runFile
Dec 3, 2006
Dec 3, 2006
261
262
263
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
Dec 4, 2006
Dec 4, 2006
264
uint32 ticks = 0;
Dec 3, 2006
Dec 3, 2006
265
266
267
268
int pre = 0;
int post = 0;
pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
Dec 10, 2006
Dec 10, 2006
269
logDebug("Collecting garbage (currently using %d bytes).", pre);
Dec 4, 2006
Dec 4, 2006
270
ticks = MojoPlatform_ticks();
Dec 3, 2006
Dec 3, 2006
271
lua_gc (L, LUA_GCCOLLECT, 0);
Dec 10, 2006
Dec 10, 2006
272
profile("Garbage collection", ticks);
Dec 3, 2006
Dec 3, 2006
273
post = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
Dec 10, 2006
Dec 10, 2006
274
logDebug("Now using %d bytes (%d bytes savings).\n", post, pre - post);
Dec 3, 2006
Dec 3, 2006
275
276
277
} // MojoLua_collectGarbage
Dec 11, 2006
Dec 11, 2006
278
279
280
281
282
283
284
285
286
// You can trigger the garbage collector with more control in the standard
// Lua runtime, but this notes profiling and statistics via logDebug().
static int luahook_collectgarbage(lua_State *L)
{
MojoLua_collectGarbage();
return 0;
} // luahook_collectgarbage
Dec 10, 2006
Dec 10, 2006
287
288
289
290
291
292
293
294
295
static inline void pushStringOrNil(lua_State *L, const char *str)
{
if (str != NULL)
lua_pushstring(L, str);
else
lua_pushnil(L);
} // pushStringOrNil
Dec 2, 2006
Dec 2, 2006
296
297
298
299
300
// Since localization is kept in Lua tables, I stuck this in the Lua glue.
const char *translate(const char *str)
{
const char *retval = str;
Dec 6, 2006
Dec 6, 2006
301
if (luaState != NULL) // No translations before Lua is initialized.
Dec 2, 2006
Dec 2, 2006
302
{
Dec 6, 2006
Dec 6, 2006
303
if (lua_checkstack(luaState, 3))
Dec 2, 2006
Dec 2, 2006
304
{
Dec 6, 2006
Dec 6, 2006
305
306
307
int popcount = 0;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
Dec 2, 2006
Dec 2, 2006
308
{
Dec 6, 2006
Dec 6, 2006
309
310
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
Dec 4, 2006
Dec 4, 2006
311
{
Dec 6, 2006
Dec 6, 2006
312
313
314
315
316
const char *tr = NULL;
lua_getfield(luaState, -1, str); popcount++;
tr = lua_tostring(luaState, -1);
if (tr != NULL) // translated for this locale?
{
Dec 8, 2006
Dec 8, 2006
317
318
319
char *dst = (char *) scratchbuf_128k;
xstrncpy(dst, tr, sizeof(scratchbuf_128k));
retval = dst;
Dec 6, 2006
Dec 6, 2006
320
} // if
Dec 4, 2006
Dec 4, 2006
321
} // if
Dec 2, 2006
Dec 2, 2006
322
} // if
Dec 6, 2006
Dec 6, 2006
323
lua_pop(luaState, popcount); // remove our stack salsa.
Dec 2, 2006
Dec 2, 2006
324
325
326
327
328
329
330
} // if
} // if
return retval;
} // translate
Dec 6, 2006
Dec 6, 2006
331
332
333
334
335
336
// Use this instead of Lua's error() function if you don't have a
// programatic error, so you don't get stack callback stuff:
// MojoSetup.fatal("You need the base game to install this expansion pack.")
// Doesn't actually return.
static int luahook_fatal(lua_State *L)
{
Dec 20, 2006
Dec 20, 2006
337
338
339
340
const char *errstr = lua_tostring(L, 1);
if (errstr == NULL)
errstr = _("Unknown error");
return fatal(errstr); // doesn't actually return.
Dec 6, 2006
Dec 6, 2006
341
342
343
const char *err = luaL_checkstring(L, 1);
fatal(err);
return 0;
Dec 10, 2006
Dec 10, 2006
344
} // luahook_fatal
Dec 6, 2006
Dec 6, 2006
345
346
Dec 3, 2006
Dec 3, 2006
347
348
349
350
351
352
353
354
355
356
// Lua interface to MojoLua_runFile(). This is needed instead of Lua's
// require(), since it can access scripts inside an archive.
static int luahook_runfile(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
lua_pushboolean(L, MojoLua_runFile(fname));
return 1;
} // luahook_runfile
Dec 4, 2006
Dec 4, 2006
357
358
359
360
361
362
363
364
365
// Lua interface to translate().
static int luahook_translate(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
lua_pushstring(L, translate(str));
return 1;
} // luahook_translate
Dec 5, 2006
Dec 5, 2006
366
367
368
369
370
371
372
static int luahook_ticks(lua_State *L)
{
lua_pushnumber(L, MojoPlatform_ticks());
return 1;
} // luahook_ticks
Dec 6, 2006
Dec 6, 2006
373
374
375
376
377
378
static int luahook_msgbox(lua_State *L)
{
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
Dec 7, 2006
Dec 7, 2006
379
GGui->msgbox(title, text);
Dec 6, 2006
Dec 6, 2006
380
381
382
383
384
385
386
387
388
389
390
391
} // if
return 0;
} // luahook_msgbox
static int luahook_promptyn(lua_State *L)
{
boolean rc = false;
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
Dec 7, 2006
Dec 7, 2006
392
rc = GGui->promptyn(title, text);
Dec 6, 2006
Dec 6, 2006
393
394
395
396
397
398
399
} // if
lua_pushboolean(L, rc);
return 1;
} // luahook_msgbox
Dec 10, 2006
Dec 10, 2006
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
static int luahook_logwarning(lua_State *L)
{
logWarning(luaL_checkstring(L, 1));
return 0;
} // luahook_logwarning
static int luahook_logerror(lua_State *L)
{
logError(luaL_checkstring(L, 1));
return 0;
} // luahook_logerror
static int luahook_loginfo(lua_State *L)
{
logInfo(luaL_checkstring(L, 1));
return 0;
} // luahook_loginfo
static int luahook_logdebug(lua_State *L)
{
logDebug(luaL_checkstring(L, 1));
return 0;
} // luahook_logdebug
static int luahook_cmdline(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, cmdline(arg));
return 1;
} // luahook_cmdline
static int luahook_cmdlinestr(lua_State *L)
{
Dec 27, 2006
Dec 27, 2006
438
const int argc = lua_gettop(L);
Dec 10, 2006
Dec 10, 2006
439
440
441
442
443
444
445
446
const char *arg = luaL_checkstring(L, 1);
const char *envr = (argc < 2) ? NULL : lua_tostring(L, 2);
const char *deflt = (argc < 3) ? NULL : lua_tostring(L, 3);
pushStringOrNil(L, cmdlinestr(arg, envr, deflt));
return 1;
} // luahook_cmdlinestr
Dec 20, 2006
Dec 20, 2006
447
static int luahook_gui_start(lua_State *L)
Dec 12, 2006
Dec 12, 2006
448
449
450
{
const char *title = luaL_checkstring(L, 1);
const char *splash = lua_tostring(L, 2);
Dec 20, 2006
Dec 20, 2006
451
lua_pushboolean(L, GGui->start(title, splash));
Dec 12, 2006
Dec 12, 2006
452
return 1;
Dec 20, 2006
Dec 20, 2006
453
} // luahook_gui_start
Dec 12, 2006
Dec 12, 2006
454
455
Dec 20, 2006
Dec 20, 2006
456
static const uint8 *loadFile(const char *fname, size_t *len)
Dec 12, 2006
Dec 12, 2006
457
{
Dec 20, 2006
Dec 20, 2006
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
uint8 *retval = NULL;
MojoInput *io = MojoInput_newFromArchivePath(GBaseArchive, fname);
if (io != NULL)
{
int64 len64 = io->length(io);
*len = (size_t) len64;
if (*len == len64)
{
retval = (uint8 *) xmalloc(*len + 1);
if (io->read(io, retval, *len) == *len)
retval[*len] = '\0';
else
{
free(retval);
retval = NULL;
} // else
} // if
io->close(io);
} // if
return retval;
} // loadFile
static inline boolean canGoBack(int thisstage)
{
return (thisstage > 1);
} // canGoBack
static inline boolean canGoForward(int thisstage, int maxstage)
{
return (thisstage < maxstage);
} // canGoForward
static int luahook_gui_readme(lua_State *L)
{
size_t len = 0;
const char *name = luaL_checkstring(L, 1);
const char *fname = luaL_checkstring(L, 2);
const int thisstage = luaL_checkinteger(L, 3);
const int maxstage = luaL_checkinteger(L, 4);
const uint8 *data = loadFile(fname, &len);
const boolean can_go_back = canGoBack(thisstage);
const boolean can_go_fwd = canGoForward(thisstage, maxstage);
if (data == NULL)
fatal(_("failed to load file '%s'"), fname);
lua_pushboolean(L, GGui->readme(name, data, len, can_go_back, can_go_fwd));
free((void *) data);
return 1;
} // luahook_gui_readme
static int luahook_gui_stop(lua_State *L)
{
GGui->stop();
Dec 12, 2006
Dec 12, 2006
515
return 0;
Dec 20, 2006
Dec 20, 2006
516
} // luahook_gui_stop
Dec 12, 2006
Dec 12, 2006
517
518
Dec 27, 2006
Dec 27, 2006
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
typedef MojoGuiSetupOptions GuiOptions; // a little less chatty.
static inline uint64 file_size_from_string(const char *str)
{
// !!! FIXME: this is WRONG
uint64 retval = (uint64) atoi(str);
size_t len = strlen(str);
if (len > 0)
{
const uint64 ui64_1024 = (uint64) 1024;
char ch = str[len-1];
if ((ch >= 'a') && (ch <= 'z'))
ch -= ('a' - 'A');
if (ch == 'K')
retval *= ui64_1024;
else if (ch == 'M')
retval *= ui64_1024 * ui64_1024;
else if (ch == 'G')
retval *= ui64_1024 * ui64_1024 * ui64_1024;
else if (ch == 'T')
retval *= ui64_1024 * ui64_1024 * ui64_1024 * ui64_1024;
} // if
return retval;
} // file_size_from_string
// forward declare this for recursive magic...
Dec 27, 2006
Dec 27, 2006
550
static GuiOptions *build_gui_options(lua_State *L, GuiOptions *parent);
Dec 27, 2006
Dec 27, 2006
551
552
553
554
555
556
// An option table (from Setup.Option{} or Setup.OptionGroup{}) must be at
// the top of the Lua stack.
static GuiOptions *build_one_gui_option(lua_State *L, GuiOptions *opts,
boolean is_option_group)
{
Dec 27, 2006
Dec 27, 2006
557
GuiOptions *newopt = NULL;
Dec 27, 2006
Dec 27, 2006
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
boolean required = false;
boolean skipopt = false;
lua_getfield(L, -1, "required");
if (lua_toboolean(L, -1))
{
lua_pushboolean(L, true);
lua_setfield(L, -3, "value");
required = skipopt = true; // don't pass to GUI.
} // if
lua_pop(L, 1); // remove "required" from stack.
// "disabled=true" trumps "required=true"
lua_getfield(L, -1, "disabled");
if (lua_toboolean(L, -1))
{
if (required)
{
lua_getfield(L, -2, "description");
logWarning("Option '%s' is both required and disabled!",
lua_tostring(L, -1));
lua_pop(L, 1);
} // if
lua_pushboolean(L, false);
lua_setfield(L, -3, "value");
skipopt = true; // don't pass to GUI.
} // if
lua_pop(L, 1); // remove "disabled" from stack.
Dec 27, 2006
Dec 27, 2006
587
if (skipopt) // Skip this option, but look for children in required opts.
Dec 27, 2006
Dec 27, 2006
588
{
Dec 27, 2006
Dec 27, 2006
589
590
if (required)
newopt = build_gui_options(L, opts);
Dec 27, 2006
Dec 27, 2006
591
592
} // if
Dec 27, 2006
Dec 27, 2006
593
else // add this option.
Dec 27, 2006
Dec 27, 2006
594
{
Dec 27, 2006
Dec 27, 2006
595
newopt = (GuiOptions *) xmalloc(sizeof (GuiOptions));
Dec 27, 2006
Dec 27, 2006
596
newopt->is_group_parent = is_option_group;
Dec 27, 2006
Dec 27, 2006
597
newopt->value = true;
Dec 27, 2006
Dec 27, 2006
598
599
600
601
602
603
604
605
606
607
608
609
610
lua_getfield(L, -1, "description");
newopt->description = xstrdup(lua_tostring(L, -1));
lua_pop(L, 1);
if (!is_option_group)
{
lua_getfield(L, -1, "value");
newopt->value = (lua_toboolean(L, -1) ? true : false);
lua_pop(L, 1);
lua_getfield(L, -1, "size");
newopt->size = file_size_from_string(lua_tostring(L, -1));
lua_pop(L, 1);
Dec 27, 2006
Dec 27, 2006
611
612
613
614
615
616
617
618
619
620
621
622
newopt->opaque = ((int) lua_objlen(L, 4)) + 1;
lua_pushinteger(L, newopt->opaque);
lua_pushvalue(L, -2);
lua_settable(L, 4); // position #4 is our local lookup table.
} // if
newopt->child = build_gui_options(L, newopt); // look for children...
if ((is_option_group) && (!newopt->child)) // skip empty groups.
{
free((void *) newopt->description);
free(newopt);
newopt = NULL;
Dec 27, 2006
Dec 27, 2006
623
} // if
Dec 27, 2006
Dec 27, 2006
624
625
626
627
628
629
} // else
if (newopt != NULL)
{
newopt->next_sibling = opts;
opts = newopt; // prepend to list (we'll reverse it later...)
Dec 27, 2006
Dec 27, 2006
630
631
632
633
634
635
} // if
return opts;
} // build_one_gui_option
Dec 27, 2006
Dec 27, 2006
636
637
static inline GuiOptions *cleanup_gui_option_list(GuiOptions *opts,
GuiOptions *parent)
Dec 27, 2006
Dec 27, 2006
638
{
Dec 27, 2006
Dec 27, 2006
639
640
const boolean is_group = ((parent) && (parent->is_group_parent));
GuiOptions *seen_enabled = NULL;
Dec 27, 2006
Dec 27, 2006
641
GuiOptions *prev = NULL;
Dec 27, 2006
Dec 27, 2006
642
643
GuiOptions *tmp = NULL;
Dec 27, 2006
Dec 27, 2006
644
645
while (opts != NULL)
{
Dec 27, 2006
Dec 27, 2006
646
647
648
649
650
651
652
653
654
655
656
657
658
659
if ((is_group) && (opts->value))
{
if (seen_enabled)
{
logWarning("Options '%s' and '%s' are both enabled in group '%s'.",
seen_enabled->description, opts->description,
parent->description);
seen_enabled->value = false;
} // if
seen_enabled = opts;
} // if
// Reverse the linked list, since we added these backwards before...
tmp = opts->next_sibling;
Dec 27, 2006
Dec 27, 2006
660
661
662
663
opts->next_sibling = prev;
prev = opts;
opts = tmp;
} // while
Dec 27, 2006
Dec 27, 2006
664
665
666
667
668
669
670
671
if ((prev) && (is_group) && (!seen_enabled))
{
logWarning("Option group '%s' has no enabled items, choosing first ('%s').",
parent->description, prev->description);
prev->value = true;
} // if
Dec 27, 2006
Dec 27, 2006
672
return prev;
Dec 27, 2006
Dec 27, 2006
673
} // cleanup_gui_option_list
Dec 27, 2006
Dec 27, 2006
674
675
676
// the top of the stack must be the lua table with options/optiongroups.
Dec 28, 2006
Dec 28, 2006
677
// We build onto (opts) "child" field.
Dec 27, 2006
Dec 27, 2006
678
static GuiOptions *build_gui_options(lua_State *L, GuiOptions *parent)
Dec 27, 2006
Dec 27, 2006
679
680
681
{
int i = 0;
GuiOptions *opts = NULL;
Dec 27, 2006
Dec 27, 2006
682
const struct { const char *fieldname; boolean is_group; } opttype[] =
Dec 27, 2006
Dec 27, 2006
683
684
685
686
687
688
689
{
{ "options", false },
{ "optiongroups", true }
};
for (i = 0; i < STATICARRAYLEN(opttype); i++)
{
Dec 27, 2006
Dec 27, 2006
690
const boolean is_group = opttype[i].is_group;
Dec 27, 2006
Dec 27, 2006
691
692
693
694
695
696
lua_getfield(L, -1, opttype[i].fieldname);
if (!lua_isnil(L, -1))
{
lua_pushnil(L); // first key for iteration...
while (lua_next(L, -2)) // replaces key, pushes value.
{
Dec 27, 2006
Dec 27, 2006
697
698
opts = build_one_gui_option(L, opts, is_group);
lua_pop(L, 1); // remove table, keep key for next iteration.
Dec 27, 2006
Dec 27, 2006
699
} // while
Dec 27, 2006
Dec 27, 2006
700
opts = cleanup_gui_option_list(opts, parent);
Dec 27, 2006
Dec 27, 2006
701
} // if
Dec 27, 2006
Dec 27, 2006
702
lua_pop(L, 1); // pop options/optiongroups table.
Dec 27, 2006
Dec 27, 2006
703
704
705
706
707
708
} // for
return opts;
} // build_gui_options
Dec 27, 2006
Dec 27, 2006
709
710
711
// Free the tree of C structs we generated, and update the mirrored Lua tables
// with new values...
static void done_gui_options(lua_State *L, GuiOptions *opts)
Dec 27, 2006
Dec 27, 2006
712
713
714
{
if (opts != NULL)
{
Dec 27, 2006
Dec 27, 2006
715
done_gui_options(L, opts->next_sibling);
Dec 27, 2006
Dec 27, 2006
716
717
718
719
720
721
722
723
724
725
726
727
done_gui_options(L, opts->child);
if (opts->opaque)
{
// Update Lua table for this option...
lua_pushinteger(L, opts->opaque);
lua_gettable(L, 4); // #4 is our local table
lua_pushboolean(L, opts->value);
lua_setfield(L, -2, "value");
lua_pop(L, 1);
} // if
Dec 27, 2006
Dec 27, 2006
728
729
730
free((void *) opts->description);
free(opts);
} // if
Dec 27, 2006
Dec 27, 2006
731
} // done_gui_options
Dec 27, 2006
Dec 27, 2006
732
733
734
735
736
static int luahook_gui_options(lua_State *L)
{
// The options table is arg #1.
Dec 27, 2006
Dec 27, 2006
737
const int argc = lua_gettop(L);
Dec 27, 2006
Dec 27, 2006
738
739
740
741
const int thisstage = luaL_checkint(L, 2);
const int maxstage = luaL_checkint(L, 3);
const boolean can_go_back = canGoBack(thisstage);
const boolean can_go_fwd = canGoForward(thisstage, maxstage);
Dec 27, 2006
Dec 27, 2006
742
boolean rc = true;
Dec 27, 2006
Dec 27, 2006
743
744
GuiOptions *opts = NULL;
Dec 27, 2006
Dec 27, 2006
745
746
747
748
assert(argc == 3);
lua_newtable(L); // we'll use this for updating the tree later.
Dec 27, 2006
Dec 27, 2006
749
750
751
// Now we need to build a tree of C structs from the hierarchical table
// we got from Lua...
lua_pushvalue(L, 1); // get the Lua table onto the top of the stack...
Dec 27, 2006
Dec 27, 2006
752
opts = build_gui_options(L, NULL);
Dec 27, 2006
Dec 27, 2006
753
754
lua_pop(L, 1); // pop the Lua table off the top of the stack...
Dec 27, 2006
Dec 27, 2006
755
756
757
758
759
if (opts != NULL) // if nothing to do, we'll go directly to next stage.
rc = GGui->options(opts, can_go_back, can_go_fwd);
done_gui_options(L, opts); // free C structs, update Lua tables...
lua_pop(L, 1); // pop table we created.
Dec 27, 2006
Dec 27, 2006
760
Dec 27, 2006
Dec 27, 2006
761
lua_pushboolean(L, rc);
Dec 27, 2006
Dec 27, 2006
762
763
764
765
return 1; // returns one boolean value.
} // luahook_gui_options
Dec 28, 2006
Dec 28, 2006
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
static int luahook_gui_destination(lua_State *L)
{
const int thisstage = luaL_checkinteger(L, 2);
const int maxstage = luaL_checkinteger(L, 3);
const boolean can_go_back = canGoBack(thisstage);
const boolean can_go_fwd = canGoForward(thisstage, maxstage);
char **recommend = NULL;
size_t reccount = 0;
char *rc = NULL;
if (lua_istable(L, 1))
{
int i;
reccount = lua_objlen(L, 1);
recommend = (char **) alloca(reccount * sizeof (char *));
for (i = 0; i < reccount; i++)
{
const char *str = NULL;
lua_pushinteger(L, i+1);
lua_gettable(L, 1);
str = lua_tostring(L, -1);
recommend[i] = (char *) alloca(strlen(str) + 1);
strcpy(recommend[i], str);
lua_pop(L, 1);
} // for
} // if
rc = GGui->destination((const char **) recommend, reccount,
can_go_back, can_go_fwd);
if (rc == NULL)
lua_pushnil(L);
else
{
lua_pushstring(L, rc);
free(rc);
} // else
return 1;
} // luahook_gui_destination
Apr 21, 2007
Apr 21, 2007
807
808
809
810
811
812
813
static int luahook_gui_insertmedia(lua_State *L)
{
lua_pushboolean(L, GGui->insertmedia(luaL_checkstring(L, 1)));
return 1;
} // luahook_gui_insertmedia
Dec 2, 2006
Dec 2, 2006
814
815
816
817
818
819
820
821
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_cfunc(lua_State *L, lua_CFunction f, const char *sym)
{
lua_pushcfunction(luaState, f);
lua_setfield(luaState, -2, sym);
} // set_cfunc
Dec 3, 2006
Dec 3, 2006
822
823
824
825
826
827
828
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_string(lua_State *L, const char *str, const char *sym)
{
lua_pushstring(luaState, str);
lua_setfield(luaState, -2, sym);
} // set_string
Dec 9, 2006
Dec 9, 2006
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
static inline void set_string_array(lua_State *L, int argc, const char **argv,
const char *sym)
{
int i;
lua_newtable(luaState);
for (i = 0; i < argc; i++)
{
lua_pushinteger(luaState, i+1); // lua is option base 1!
lua_pushstring(luaState, argv[i]);
lua_settable(luaState, -3);
} // for
lua_setfield(luaState, -2, sym);
} // set_string_array
void MojoLua_setString(const char *str, const char *sym)
{
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
set_string(luaState, str, sym);
lua_pop(luaState, 1);
} // MojoLua_setString
void MojoLua_setStringArray(int argc, const char **argv, const char *sym)
{
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
set_string_array(luaState, argc, argv, sym);
lua_pop(luaState, 1);
} // MojoLua_setStringArray
Dec 3, 2006
Dec 3, 2006
859
Dec 2, 2006
Dec 2, 2006
860
861
boolean MojoLua_initLua(void)
{
Dec 20, 2006
Dec 20, 2006
862
const char *envr = cmdlinestr("locale", "MOJOSETUP_LOCALE", NULL);
Dec 3, 2006
Dec 3, 2006
863
char locale[16];
Dec 4, 2006
Dec 4, 2006
864
865
char ostype[64];
char osversion[64];
Dec 2, 2006
Dec 2, 2006
866
Dec 20, 2006
Dec 20, 2006
867
868
869
if (envr != NULL)
xstrncpy(locale, envr, sizeof (locale));
else if (!MojoPlatform_locale(locale, sizeof (locale)))
Dec 3, 2006
Dec 3, 2006
870
xstrncpy(locale, "???", sizeof (locale));
Dec 20, 2006
Dec 20, 2006
871
Dec 4, 2006
Dec 4, 2006
872
873
874
875
876
877
if (!MojoPlatform_osType(ostype, sizeof (ostype)))
xstrncpy(ostype, "???", sizeof (ostype));
if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
xstrncpy(osversion, "???", sizeof (osversion));
assert(luaState == NULL);
Dec 3, 2006
Dec 3, 2006
878
Dec 9, 2006
Dec 9, 2006
879
luaState = lua_newstate(MojoLua_alloc, NULL);
Dec 2, 2006
Dec 2, 2006
880
881
882
if (luaState == NULL)
return false;
Dec 20, 2006
Dec 20, 2006
883
lua_atpanic(luaState, luahook_fatal);
Dec 2, 2006
Dec 2, 2006
884
885
886
887
888
889
890
891
892
893
894
895
if (!lua_checkstack(luaState, 20)) // Just in case.
{
lua_close(luaState);
luaState = NULL;
return false;
} // if
luaL_openlibs(luaState);
// Build MojoSetup namespace for Lua to access and fill in C bridges...
lua_newtable(luaState);
Dec 3, 2006
Dec 3, 2006
896
// Set up initial C functions, etc we want to expose to Lua code...
Dec 3, 2006
Dec 3, 2006
897
set_cfunc(luaState, luahook_runfile, "runfile");
Dec 4, 2006
Dec 4, 2006
898
set_cfunc(luaState, luahook_translate, "translate");
Dec 5, 2006
Dec 5, 2006
899
set_cfunc(luaState, luahook_ticks, "ticks");
Dec 6, 2006
Dec 6, 2006
900
901
902
set_cfunc(luaState, luahook_fatal, "fatal");
set_cfunc(luaState, luahook_msgbox, "msgbox");
set_cfunc(luaState, luahook_promptyn, "promptyn");
Dec 10, 2006
Dec 10, 2006
903
set_cfunc(luaState, luahook_stackwalk, "stackwalk");
Dec 10, 2006
Dec 10, 2006
904
905
906
907
908
909
set_cfunc(luaState, luahook_logwarning, "logwarning");
set_cfunc(luaState, luahook_logerror, "logerror");
set_cfunc(luaState, luahook_loginfo, "loginfo");
set_cfunc(luaState, luahook_logdebug, "logdebug");
set_cfunc(luaState, luahook_cmdline, "cmdline");
set_cfunc(luaState, luahook_cmdlinestr, "cmdlinestr");
Dec 11, 2006
Dec 11, 2006
910
911
set_cfunc(luaState, luahook_collectgarbage, "collectgarbage");
set_cfunc(luaState, luahook_debugger, "debugger");
Dec 3, 2006
Dec 3, 2006
912
set_string(luaState, locale, "locale");
Dec 4, 2006
Dec 4, 2006
913
914
915
916
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
Dec 20, 2006
Dec 20, 2006
917
set_string(luaState, GGui->name(), "ui");
Dec 7, 2006
Dec 7, 2006
918
set_string(luaState, GBuildVer, "buildver");
Dec 9, 2006
Dec 9, 2006
919
set_string(luaState, GLuaLicense, "lualicense");
Dec 10, 2006
Dec 10, 2006
920
set_string_array(luaState, GArgc, GArgv, "argv");
Dec 20, 2006
Dec 20, 2006
921
922
923
924
925
// Set the GUI functions...
lua_newtable(luaState);
set_cfunc(luaState, luahook_gui_start, "start");
set_cfunc(luaState, luahook_gui_readme, "readme");
Dec 27, 2006
Dec 27, 2006
926
set_cfunc(luaState, luahook_gui_options, "options");
Dec 28, 2006
Dec 28, 2006
927
set_cfunc(luaState, luahook_gui_destination, "destination");
Dec 20, 2006
Dec 20, 2006
928
set_cfunc(luaState, luahook_gui_stop, "stop");
Apr 21, 2007
Apr 21, 2007
929
set_cfunc(luaState, luahook_gui_insertmedia, "insertmedia");
Dec 20, 2006
Dec 20, 2006
930
lua_setfield(luaState, -2, "gui");
Dec 6, 2006
Dec 6, 2006
931
lua_setglobal(luaState, MOJOSETUP_NAMESPACE);
Dec 2, 2006
Dec 2, 2006
932
Dec 3, 2006
Dec 3, 2006
933
// Set up localization table, if possible.
Dec 3, 2006
Dec 3, 2006
934
MojoLua_runFile("localization");
Dec 3, 2006
Dec 3, 2006
935
Dec 3, 2006
Dec 3, 2006
936
937
938
// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;
Dec 2, 2006
Dec 2, 2006
939
Dec 3, 2006
Dec 3, 2006
940
// ...and run the installer-specific config file.
Dec 3, 2006
Dec 3, 2006
941
942
943
if (!MojoLua_runFile("config"))
return false;
Dec 20, 2006
Dec 20, 2006
944
// We don't need the "Setup" namespace anymore. Make it
Dec 3, 2006
Dec 3, 2006
945
// eligible for garbage collection.
Dec 20, 2006
Dec 20, 2006
946
947
lua_pushnil(luaState);
lua_setglobal(luaState, "Setup");
Dec 3, 2006
Dec 3, 2006
948
949
950
MojoLua_collectGarbage(); // get rid of old init crap we don't need.
Dec 2, 2006
Dec 2, 2006
951
952
953
954
return true;
} // MojoLua_initLua
Dec 20, 2006
Dec 20, 2006
955
956
957
958
959
960
boolean MojoLua_initialized(void)
{
return (luaState != NULL);
} // MojoLua_initialized
Dec 2, 2006
Dec 2, 2006
961
962
963
964
965
966
967
968
969
void MojoLua_deinitLua(void)
{
if (luaState != NULL)
{
lua_close(luaState);
luaState = NULL;
} // if
} // MojoLua_deinitLua
Dec 9, 2006
Dec 9, 2006
970
971
const char *GLuaLicense =
Dec 10, 2006
Dec 10, 2006
972
973
"Lua:\n"
"\n"
Dec 9, 2006
Dec 9, 2006
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
"Copyright (C) 1994-2006 Lua.org, PUC-Rio.\n"
"\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy\n"
"of this software and associated documentation files (the \"Software\"), to deal\n"
"in the Software without restriction, including without limitation the rights\n"
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
"copies of the Software, and to permit persons to whom the Software is\n"
"furnished to do so, subject to the following conditions:\n"
"\n"
"The above copyright notice and this permission notice shall be included in\n"
"all copies or substantial portions of the Software.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n"
"THE SOFTWARE.\n"
"\n";
Dec 2, 2006
Dec 2, 2006
995
// end of lua_glue.c ...