Skip to content

Latest commit

 

History

History
1266 lines (1041 loc) · 34.7 KB

mojosetup.c

File metadata and controls

1266 lines (1041 loc) · 34.7 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Apr 23, 2007
Apr 23, 2007
9
#include <stdarg.h>
10
11
#include "universal.h"
Apr 23, 2007
Apr 23, 2007
12
13
#include "platform.h"
#include "gui.h"
Dec 2, 2006
Dec 2, 2006
14
#include "lua_glue.h"
Apr 23, 2007
Apr 23, 2007
15
16
#include "fileio.h"
Jan 25, 2008
Jan 25, 2008
17
18
19
#define TEST_LAUNCH_BROWSER_CODE 0
int MojoSetup_testLaunchBrowserCode(int argc, char **argv);
May 5, 2007
May 5, 2007
20
#define TEST_ARCHIVE_CODE 0
May 5, 2007
May 5, 2007
21
22
int MojoSetup_testArchiveCode(int argc, char **argv);
May 8, 2007
May 8, 2007
23
#define TEST_NETWORK_CODE 0
May 5, 2007
May 5, 2007
24
int MojoSetup_testNetworkCode(int argc, char **argv);
May 5, 2007
May 5, 2007
25
26
Apr 23, 2007
Apr 23, 2007
27
28
29
30
31
32
33
34
35
36
37
38
uint8 scratchbuf_128k[128 * 1024];
MojoSetupEntryPoints GEntryPoints =
{
xmalloc,
xrealloc,
xstrdup,
xstrncpy,
translate,
logWarning,
logError,
logInfo,
logDebug,
Jan 12, 2008
Jan 12, 2008
39
40
format,
numstr,
May 7, 2007
May 7, 2007
41
MojoPlatform_ticks,
Mar 2, 2008
Mar 2, 2008
42
utf8codepoint,
Mar 3, 2008
Mar 3, 2008
43
44
utf8len,
splitText,
Apr 25, 2009
Apr 25, 2009
45
isValidProductKey,
Apr 23, 2007
Apr 23, 2007
46
47
48
49
50
};
int GArgc = 0;
const char **GArgv = NULL;
May 7, 2007
May 7, 2007
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
static char *crashedmsg = NULL;
static char *termedmsg = NULL;
void MojoSetup_crashed(void)
{
if (crashedmsg == NULL)
panic("BUG: crash at startup");
else
fatal(crashedmsg);
} // MojoSetup_crash
void MojoSetup_terminated(void)
{
if (termedmsg == NULL) // no translation yet.
panic("The installer has been stopped by the system.");
else
fatal(termedmsg);
} // MojoSetup_crash
May 20, 2007
May 20, 2007
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#if !SUPPORT_MULTIARCH
#define trySwitchBinaries()
#else
static void trySwitchBinary(MojoArchive *ar)
{
MojoInput *io = ar->openCurrentEntry(ar);
if (io != NULL)
{
const uint32 imglen = (uint32) io->length(io);
uint8 *img = (uint8 *) xmalloc(imglen);
const uint32 br = io->read(io, img, imglen);
io->close(io);
if (br == imglen)
{
Jan 12, 2008
Jan 12, 2008
86
logInfo("Switching binary with '%0'...", ar->prevEnum.filename);
May 20, 2007
May 20, 2007
87
88
89
90
91
92
93
94
95
96
97
98
99
MojoPlatform_switchBin(img, imglen); // no return on success.
logError("...Switch binary failed.");
} // if
free(img);
} // if
} // trySwitchBinary
static void trySwitchBinaries(void)
{
if (cmdlinestr("nobinswitch", "MOJOSETUP_NOBINSWITCH", NULL) != NULL)
return; // we are already switched or the user is preventing it.
May 21, 2007
May 21, 2007
100
setenv("MOJOSETUP_NOBINSWITCH", "1", 1);
May 20, 2007
May 20, 2007
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
setenv("MOJOSETUP_BASE", GBaseArchivePath, 1);
if (GBaseArchive->enumerate(GBaseArchive))
{
const MojoArchiveEntry *entinfo;
while ((entinfo = GBaseArchive->enumNext(GBaseArchive)) != NULL)
{
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
if (strncmp(entinfo->filename, "arch/", 5) != 0)
continue;
trySwitchBinary(GBaseArchive);
} // while
} // if
} // trySwitchBinaries
#endif
Jul 31, 2009
Jul 31, 2009
122
static boolean trySpawnTerminalGui(void)
Nov 21, 2007
Nov 21, 2007
123
124
{
if (cmdlinestr("notermspawn", "MOJOSETUP_NOTERMSPAWN", NULL) != NULL)
Jul 31, 2009
Jul 31, 2009
125
return false; // we already spawned or the user is preventing it.
Nov 21, 2007
Nov 21, 2007
126
127
if (MojoPlatform_istty()) // maybe we can spawn a terminal for stdio?
Jul 31, 2009
Jul 31, 2009
128
return false; // We're a terminal already, no need to spawn one.
Nov 21, 2007
Nov 21, 2007
129
130
logInfo("No usable GUI found. Trying to spawn a terminal...");
Jul 31, 2009
Jul 31, 2009
131
132
133
if (!MojoPlatform_spawnTerminal())
{
logError("...Terminal spawning failed.");
Mar 22, 2010
Mar 22, 2010
134
return false;
Jul 31, 2009
Jul 31, 2009
135
136
137
138
139
} // if
assert(MojoPlatform_istty());
return (MojoGui_initGuiPlugin() != NULL);
} // trySpawnTerminalGui
Nov 21, 2007
Nov 21, 2007
140
141
May 7, 2007
May 7, 2007
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
static boolean initEverything(void)
{
MojoLog_initLogging();
logInfo("MojoSetup starting up...");
// We have to panic on errors until the GUI is ready. Try to make things
// "succeed" unless they are catastrophic, and report problems later.
// Start with the base archive work, since it might have GUI plugins.
// None of these panic() calls are localized, since localization isn't
// functional until MojoLua_initLua() succeeds.
if (!MojoArchive_initBaseArchive())
panic("Initial setup failed. Cannot continue.");
May 20, 2007
May 20, 2007
157
158
159
trySwitchBinaries(); // may not return.
if (!MojoGui_initGuiPlugin())
Nov 21, 2007
Nov 21, 2007
160
{
Jul 31, 2009
Jul 31, 2009
161
162
// This could terminate the process (and relaunch).
if (!trySpawnTerminalGui())
Dec 18, 2010
Dec 18, 2010
163
panic("Failed to start GUI. Is your download incomplete or corrupt?");
Nov 21, 2007
Nov 21, 2007
164
} // if
May 7, 2007
May 7, 2007
165
166
else if (!MojoLua_initLua())
Nov 21, 2007
Nov 21, 2007
167
{
Dec 18, 2010
Dec 18, 2010
168
169
// (...but if you're the developer: are your files in the wrong place?)
panic("Failed to start. Is your download incomplete or corrupt?");
Nov 21, 2007
Nov 21, 2007
170
} // else if
May 7, 2007
May 7, 2007
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
crashedmsg = xstrdup(_("The installer has crashed due to a bug."));
termedmsg = xstrdup(_("The installer has been stopped by the system."));
return true;
} // initEverything
static void deinitEverything(void)
{
char *tmp = NULL;
logInfo("MojoSetup shutting down...");
MojoLua_deinitLua();
MojoGui_deinitGuiPlugin();
MojoArchive_deinitBaseArchive();
MojoLog_deinitLogging();
tmp = crashedmsg;
crashedmsg = NULL;
free(tmp);
tmp = termedmsg;
termedmsg = NULL;
free(tmp);
} // deinitEverything
Apr 23, 2007
Apr 23, 2007
197
May 27, 2007
May 27, 2007
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
void MojoChecksum_init(MojoChecksumContext *ctx)
{
memset(ctx, '\0', sizeof (MojoChecksumContext));
#if SUPPORT_CRC32
MojoCrc32_init(&ctx->crc32);
#endif
#if SUPPORT_MD5
MojoMd5_init(&ctx->md5);
#endif
#if SUPPORT_SHA1
MojoSha1_init(&ctx->sha1);
#endif
} // MojoChecksum_init
void MojoChecksum_append(MojoChecksumContext *ctx, const uint8 *d, uint32 len)
{
#if SUPPORT_CRC32
MojoCrc32_append(&ctx->crc32, d, len);
#endif
#if SUPPORT_MD5
MojoMd5_append(&ctx->md5, d, len);
#endif
#if SUPPORT_SHA1
MojoSha1_append(&ctx->sha1, d, len);
#endif
} // MojoChecksum_append
void MojoChecksum_finish(MojoChecksumContext *ctx, MojoChecksums *sums)
{
memset(sums, '\0', sizeof (MojoChecksums));
#if SUPPORT_CRC32
MojoCrc32_finish(&ctx->crc32, &sums->crc32);
#endif
#if SUPPORT_MD5
MojoMd5_finish(&ctx->md5, sums->md5);
#endif
#if SUPPORT_SHA1
MojoSha1_finish(&ctx->sha1, sums->sha1);
#endif
} // MojoChecksum_finish
Apr 23, 2007
Apr 23, 2007
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
boolean cmdline(const char *arg)
{
int argc = GArgc;
const char **argv = GArgv;
int i;
if ((arg == NULL) || (argv == NULL))
return false;
while (*arg == '-') // Skip all '-' chars, so "--nosound" == "-nosound"
arg++;
for (i = 1; i < argc; i++)
{
const char *thisarg = argv[i];
if (*thisarg != '-')
continue; // no dash in the string, skip it.
while (*(++thisarg) == '-') { /* keep looping. */ }
if (strcmp(arg, thisarg) == 0)
return true;
} // for
return false;
} // cmdline
const char *cmdlinestr(const char *arg, const char *envr, const char *deflt)
{
uint32 len = 0;
int argc = GArgc;
const char **argv = GArgv;
int i;
Feb 20, 2008
Feb 20, 2008
275
276
277
278
279
280
281
if (envr != NULL)
{
const char *val = getenv(envr);
if (val != NULL)
return val;
} // if
Apr 23, 2007
Apr 23, 2007
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if (arg == NULL)
return deflt;
while (*arg == '-') // Skip all '-' chars, so "--nosound" == "-nosound"
arg++;
len = strlen(arg);
for (i = 1; i < argc; i++)
{
const char *thisarg = argv[i];
if (*thisarg != '-')
continue; // no dash in the string, skip it.
while (*(++thisarg) == '-') { /* keep looping. */ }
if (strncmp(arg, thisarg, len) != 0)
continue; // not us.
thisarg += len; // skip ahead in string to end of match.
if (*thisarg == '=') // --a=b format.
return (thisarg + 1);
else if (*thisarg == '\0') // --a b format.
return ((argv[i+1] == NULL) ? deflt : argv[i+1]);
} // for
return deflt;
} // cmdlinestr
Apr 28, 2007
Apr 28, 2007
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
boolean wildcardMatch(const char *str, const char *pattern)
{
char sch = *(str++);
while (true)
{
const char pch = *(pattern++);
if (pch == '?')
{
if ((sch == '\0') || (sch == '/'))
return false;
sch = *(str++);
} // else if
else if (pch == '*')
{
May 1, 2007
May 1, 2007
327
char nextpch = *pattern;
Apr 30, 2007
Apr 30, 2007
328
if ((nextpch != '?') && (nextpch != '*'))
Apr 28, 2007
Apr 28, 2007
329
{
Apr 30, 2007
Apr 30, 2007
330
while ((sch != '\0') && (sch != nextpch))
Apr 28, 2007
Apr 28, 2007
331
332
333
334
335
336
337
338
339
sch = *(str++);
} // if
} // else if
else
{
if (pch != sch)
return false;
else if (pch == '\0')
Apr 13, 2009
Apr 13, 2009
340
break;
Apr 28, 2007
Apr 28, 2007
341
342
343
344
sch = *(str++);
} // else
} // while
Apr 13, 2009
Apr 13, 2009
345
return true;
Apr 28, 2007
Apr 28, 2007
346
347
348
} // wildcardMatch
Jan 12, 2008
Jan 12, 2008
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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
438
439
440
441
442
443
444
445
446
const char *numstr(int val)
{
static int pos = 0;
char *ptr = ((char *) scratchbuf_128k) + (pos * 128);
snprintf(ptr, 128, "%d", val);
pos = (pos + 1) % 1000;
return ptr;
} // numstr
static char *format_internal(const char *fmt, va_list ap)
{
// This is kinda nasty. String manipulation in C always is.
char *retval = NULL;
const char *strs[10]; // 0 through 9.
const char *ptr = NULL;
char *wptr = NULL;
size_t len = 0;
int maxfmtid = -2;
int i;
// figure out what this format string contains...
for (ptr = fmt; *ptr; ptr++)
{
if (*ptr == '%')
{
const char ch = *(++ptr);
if (ch == '%') // a literal '%'
maxfmtid = (maxfmtid == -2) ? -1 : maxfmtid;
else if ((ch >= '0') && (ch <= '9'))
maxfmtid = ((maxfmtid > (ch - '0')) ? maxfmtid : (ch - '0'));
else
fatal(_("BUG: Invalid format() string"));
} // if
} // while
if (maxfmtid == -2) // no formatters present at all.
return xstrdup(fmt); // just copy it, we're done.
for (i = 0; i <= maxfmtid; i++) // referenced varargs --> linear array.
{
strs[i] = va_arg(ap, const char *);
if (strs[i] == NULL)
strs[i] = "(null)"; // just to match sprintf() behaviour...
} // for
// allocate the string we'll need in one shot, so we don't have to resize.
for (ptr = fmt; *ptr; ptr++)
{
if (*ptr != '%')
len++;
else
{
const char ch = *(++ptr);
if (ch == '%') // a literal '%'
len++; // just want '%' char.
else //if ((ch >= '0') && (ch <= '9'))
len += strlen(strs[ch - '0']);
} // else
} // while
// Now write the formatted string...
wptr = retval = (char *) xmalloc(len+1);
for (ptr = fmt; *ptr; ptr++)
{
const char strch = *ptr;
if (strch != '%')
*(wptr++) = strch;
else
{
const char ch = *(++ptr);
if (ch == '%') // a literal '%'
*(wptr++) = '%';
else //if ((ch >= '0') && (ch <= '9'))
{
const char *str = strs[ch - '0'];
strcpy(wptr, str);
wptr += strlen(str);
} // else
} // else
} // while
*wptr = '\0';
return retval;
} // format_internal
char *format(const char *fmt, ...)
{
char *retval = NULL;
va_list ap;
va_start(ap, fmt);
retval = format_internal(fmt, ap);
va_end(ap);
return retval;
} // format
May 12, 2007
May 12, 2007
447
448
449
#if ((defined _NDEBUG) || (defined NDEBUG))
#define DEFLOGLEV "info"
#else
Apr 23, 2007
Apr 23, 2007
450
#define DEFLOGLEV "everything"
May 12, 2007
May 12, 2007
451
#endif
May 1, 2007
May 1, 2007
452
MojoSetupLogLevel MojoLog_logLevel = MOJOSETUP_LOG_EVERYTHING;
Sep 20, 2007
Sep 20, 2007
453
static void *logFile = NULL;
Apr 23, 2007
Apr 23, 2007
454
455
456
457
void MojoLog_initLogging(void)
{
const char *level = cmdlinestr("loglevel","MOJOSETUP_LOGLEVEL", DEFLOGLEV);
Jun 1, 2007
Jun 1, 2007
458
const char *fname = cmdlinestr("log", "MOJOSETUP_LOG", NULL);
Apr 23, 2007
Apr 23, 2007
459
460
if (strcmp(level, "nothing") == 0)
May 1, 2007
May 1, 2007
461
MojoLog_logLevel = MOJOSETUP_LOG_NOTHING;
Apr 23, 2007
Apr 23, 2007
462
else if (strcmp(level, "errors") == 0)
May 1, 2007
May 1, 2007
463
MojoLog_logLevel = MOJOSETUP_LOG_ERRORS;
Apr 23, 2007
Apr 23, 2007
464
else if (strcmp(level, "warnings") == 0)
May 1, 2007
May 1, 2007
465
MojoLog_logLevel = MOJOSETUP_LOG_WARNINGS;
Apr 23, 2007
Apr 23, 2007
466
else if (strcmp(level, "info") == 0)
May 1, 2007
May 1, 2007
467
MojoLog_logLevel = MOJOSETUP_LOG_INFO;
Apr 23, 2007
Apr 23, 2007
468
else if (strcmp(level, "debug") == 0)
May 1, 2007
May 1, 2007
469
MojoLog_logLevel = MOJOSETUP_LOG_DEBUG;
Apr 23, 2007
Apr 23, 2007
470
else // Unknown string gets everything...that'll teach you.
May 1, 2007
May 1, 2007
471
MojoLog_logLevel = MOJOSETUP_LOG_EVERYTHING;
Apr 23, 2007
Apr 23, 2007
472
Jan 14, 2008
Jan 14, 2008
473
474
475
if ((fname != NULL) && (strcmp(fname, "-") == 0))
logFile = MojoPlatform_stdout();
else if (fname != NULL)
Sep 20, 2007
Sep 20, 2007
476
477
478
479
480
{
const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE;
const uint16 mode = MojoPlatform_defaultFilePerms();
logFile = MojoPlatform_open(fname, flags, mode);
} // if
Apr 23, 2007
Apr 23, 2007
481
482
483
484
485
486
} // MojoLog_initLogging
void MojoLog_deinitLogging(void)
{
if (logFile != NULL)
Sep 20, 2007
Sep 20, 2007
487
488
489
490
{
MojoPlatform_close(logFile);
logFile = NULL;
} // if
Apr 23, 2007
Apr 23, 2007
491
492
493
494
495
496
} // MojoLog_deinitLogging
static inline void addLog(MojoSetupLogLevel level, char levelchar,
const char *fmt, va_list ap)
{
May 1, 2007
May 1, 2007
497
if (level <= MojoLog_logLevel)
Apr 23, 2007
Apr 23, 2007
498
{
Jan 12, 2008
Jan 12, 2008
499
char *str = format_internal(fmt, ap);
Apr 23, 2007
Apr 23, 2007
500
501
502
//int len = vsnprintf(buf + 2, sizeof (buf) - 2, fmt, ap) + 2;
//buf[0] = levelchar;
//buf[1] = ' ';
Jan 12, 2008
Jan 12, 2008
503
504
505
506
int len = strlen(str);
while ( (--len >= 0) && ((str[len] == '\n') || (str[len] == '\r')) ) {}
str[len+1] = '\0'; // delete trailing newline crap.
MojoPlatform_log(str);
Apr 23, 2007
Apr 23, 2007
507
508
if (logFile != NULL)
{
Sep 20, 2007
Sep 20, 2007
509
const char *endl = MOJOPLATFORM_ENDLINE;
Jan 12, 2008
Jan 12, 2008
510
MojoPlatform_write(logFile, str, strlen(str));
Sep 20, 2007
Sep 20, 2007
511
512
MojoPlatform_write(logFile, endl, strlen(endl));
MojoPlatform_flush(logFile);
Apr 23, 2007
Apr 23, 2007
513
} // if
Jan 12, 2008
Jan 12, 2008
514
free(str);
Apr 23, 2007
Apr 23, 2007
515
516
517
518
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
550
551
552
553
554
555
556
557
558
} // if
} // addLog
void logWarning(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
addLog(MOJOSETUP_LOG_WARNINGS, '-', fmt, ap);
va_end(ap);
} // logWarning
void logError(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
addLog(MOJOSETUP_LOG_ERRORS, '!', fmt, ap);
va_end(ap);
} // logError
void logInfo(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
addLog(MOJOSETUP_LOG_INFO, '*', fmt, ap);
va_end(ap);
} // logInfo
void logDebug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
addLog(MOJOSETUP_LOG_DEBUG, '#', fmt, ap);
va_end(ap);
} // logDebug
uint32 profile(const char *what, uint32 start_time)
{
uint32 retval = MojoPlatform_ticks() - start_time;
if (what != NULL)
Jan 12, 2008
Jan 12, 2008
559
logDebug("%0 took %1 ms.", what, numstr((int) retval));
Apr 23, 2007
Apr 23, 2007
560
561
562
563
564
565
return retval;
} // profile_start
int fatal(const char *fmt, ...)
{
May 7, 2007
May 7, 2007
566
static boolean in_fatal = false;
Apr 23, 2007
Apr 23, 2007
567
May 7, 2007
May 7, 2007
568
569
570
571
572
if (in_fatal)
return panic("BUG: fatal() called more than once!");
in_fatal = true;
Jan 12, 2008
Jan 12, 2008
573
574
// may not want to show a message, since we displayed one elsewhere, etc.
if (fmt != NULL)
Apr 23, 2007
Apr 23, 2007
575
{
Jan 12, 2008
Jan 12, 2008
576
char *buf = NULL;
Jan 12, 2008
Jan 12, 2008
577
va_list ap;
Apr 23, 2007
Apr 23, 2007
578
va_start(ap, fmt);
Jan 12, 2008
Jan 12, 2008
579
buf = format_internal(fmt, ap);
Apr 23, 2007
Apr 23, 2007
580
581
va_end(ap);
Jan 12, 2008
Jan 12, 2008
582
logError("FATAL: %0", buf);
Jan 12, 2008
Jan 12, 2008
583
584
585
586
587
if (GGui != NULL)
GGui->msgbox(_("Fatal error"), buf);
free(buf);
Apr 23, 2007
Apr 23, 2007
588
589
} // if
Jan 12, 2008
Jan 12, 2008
590
591
592
// Shouldn't call fatal() before app is initialized!
if ( (GGui == NULL) || (!MojoLua_initialized()) )
panic("fatal() called before app is initialized! Panicking...");
Apr 23, 2007
Apr 23, 2007
593
May 7, 2007
May 7, 2007
594
MojoLua_callProcedure("revertinstall");
Apr 23, 2007
Apr 23, 2007
595
596
597
598
599
600
601
602
603
604
605
606
607
608
deinitEverything();
exit(23);
return 0;
} // fatal
int panic(const char *err)
{
static int panic_runs = 0;
panic_runs++;
if (panic_runs == 1)
{
Jan 12, 2008
Jan 12, 2008
609
logError("PANIC: %0", err);
Apr 23, 2007
Apr 23, 2007
610
611
612
613
614
panic(err);
} // if
else if (panic_runs == 2)
{
May 19, 2007
May 19, 2007
615
616
boolean domsgbox = ((GGui != NULL) && (GGui->msgbox != NULL));
if (domsgbox)
Apr 23, 2007
Apr 23, 2007
617
GGui->msgbox(_("PANIC"), err);
May 19, 2007
May 19, 2007
618
Sep 27, 2007
Sep 27, 2007
619
if ((GGui != NULL) && (GGui->deinit != NULL))
May 19, 2007
May 19, 2007
620
621
622
GGui->deinit();
if (!domsgbox)
Apr 23, 2007
Apr 23, 2007
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
panic(err); /* no GUI plugin...double-panic. */
} // if
else if (panic_runs == 3) // no GUI or panic panicked...write to stderr...
fprintf(stderr, "\n\n\n%s\n %s\n\n\n", _("PANIC"), err);
else // panic is panicking in a loop, terminate without any cleanup...
MojoPlatform_die();
exit(22);
return 0; // shouldn't hit this.
} // panic
char *xstrncpy(char *dst, const char *src, size_t len)
{
snprintf(dst, len, "%s", src);
return dst;
} // xstrncpy
Mar 2, 2008
Mar 2, 2008
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
uint32 utf8codepoint(const char **_str)
{
const char *str = *_str;
uint32 retval = 0;
uint32 octet = (uint32) ((uint8) *str);
uint32 octet2, octet3, octet4;
if (octet == 0) // null terminator, end of string.
return 0;
else if (octet < 128) // one octet char: 0 to 127
{
(*_str)++; // skip to next possible start of codepoint.
return octet;
} // else if
else if ((octet > 127) && (octet < 192)) // bad (starts with 10xxxxxx).
{
// Apparently each of these is supposed to be flagged as a bogus
// char, instead of just resyncing to the next valid codepoint.
(*_str)++; // skip to next possible start of codepoint.
return UNICODE_BOGUS_CHAR_VALUE;
} // else if
else if (octet < 224) // two octets
{
May 23, 2011
May 23, 2011
670
(*_str)++; // advance at least one byte in case of an error
Mar 2, 2008
Mar 2, 2008
671
672
673
674
675
octet -= (128+64);
octet2 = (uint32) ((uint8) *(++str));
if ((octet2 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
May 23, 2011
May 23, 2011
676
*_str += 1; // skip to next possible start of codepoint.
Mar 2, 2008
Mar 2, 2008
677
678
679
680
681
682
683
retval = ((octet << 6) | (octet2 - 128));
if ((retval >= 0x80) && (retval <= 0x7FF))
return retval;
} // else if
else if (octet < 240) // three octets
{
May 23, 2011
May 23, 2011
684
(*_str)++; // advance at least one byte in case of an error
Mar 2, 2008
Mar 2, 2008
685
686
687
688
689
690
691
692
693
octet -= (128+64+32);
octet2 = (uint32) ((uint8) *(++str));
if ((octet2 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet3 = (uint32) ((uint8) *(++str));
if ((octet3 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
May 23, 2011
May 23, 2011
694
*_str += 2; // skip to next possible start of codepoint.
Mar 2, 2008
Mar 2, 2008
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
retval = ( ((octet << 12)) | ((octet2-128) << 6) | ((octet3-128)) );
// There are seven "UTF-16 surrogates" that are illegal in UTF-8.
switch (retval)
{
case 0xD800:
case 0xDB7F:
case 0xDB80:
case 0xDBFF:
case 0xDC00:
case 0xDF80:
case 0xDFFF:
return UNICODE_BOGUS_CHAR_VALUE;
} // switch
// 0xFFFE and 0xFFFF are illegal, too, so we check them at the edge.
if ((retval >= 0x800) && (retval <= 0xFFFD))
return retval;
} // else if
else if (octet < 248) // four octets
{
May 23, 2011
May 23, 2011
717
(*_str)++; // advance at least one byte in case of an error
Mar 2, 2008
Mar 2, 2008
718
719
720
721
722
723
724
725
726
727
728
729
730
octet -= (128+64+32+16);
octet2 = (uint32) ((uint8) *(++str));
if ((octet2 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet3 = (uint32) ((uint8) *(++str));
if ((octet3 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet4 = (uint32) ((uint8) *(++str));
if ((octet4 & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
May 23, 2011
May 23, 2011
731
*_str += 3; // skip to next possible start of codepoint.
Mar 2, 2008
Mar 2, 2008
732
733
734
735
736
737
738
739
740
741
742
743
retval = ( ((octet << 18)) | ((octet2 - 128) << 12) |
((octet3 - 128) << 6) | ((octet4 - 128)) );
if ((retval >= 0x10000) && (retval <= 0x10FFFF))
return retval;
} // else if
// Five and six octet sequences became illegal in rfc3629.
// We throw the codepoint away, but parse them to make sure we move
// ahead the right number of bytes and don't overflow the buffer.
else if (octet < 252) // five octets
{
May 23, 2011
May 23, 2011
744
(*_str)++; // advance at least one byte in case of an error
Mar 2, 2008
Mar 2, 2008
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
May 23, 2011
May 23, 2011
761
*_str += 4; // skip to next possible start of codepoint.
Mar 2, 2008
Mar 2, 2008
762
763
764
765
766
return UNICODE_BOGUS_CHAR_VALUE;
} // else if
else // six octets
{
Jun 1, 2011
Jun 1, 2011
767
(*_str)++; // advance at least one byte in case of an error
Mar 2, 2008
Mar 2, 2008
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
octet = (uint32) ((uint8) *(++str));
if ((octet & (128+64)) != 128) // Format isn't 10xxxxxx?
return UNICODE_BOGUS_CHAR_VALUE;
Jun 1, 2011
Jun 1, 2011
788
*_str += 5; // skip to next possible start of codepoint.
Mar 2, 2008
Mar 2, 2008
789
790
791
792
793
794
795
return UNICODE_BOGUS_CHAR_VALUE;
} // else if
return UNICODE_BOGUS_CHAR_VALUE;
} // utf8codepoint
Mar 3, 2008
Mar 3, 2008
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
int utf8len(const char *str)
{
int retval = 0;
while (utf8codepoint(&str))
retval++;
return retval;
} // utf8len
static char *strfrombuf(const char *text, int len)
{
char *retval = xmalloc(len + 1);
memcpy(retval, text, len);
retval[len] = '\0';
return retval;
} // strfrombuf
char **splitText(const char *text, int scrw, int *_count, int *_w)
{
int i = 0;
int j = 0;
char **retval = NULL;
int count = 0;
int w = 0;
*_count = *_w = 0;
while (*text)
{
const char *utf8text = text;
uint32 ch = 0;
int pos = 0;
int furthest = 0;
for (i = 0; ((ch = utf8codepoint(&utf8text))) && (i < scrw); i++)
{
if ((ch == '\r') || (ch == '\n'))
{
const char nextbyte = *utf8text;
count++;
retval = (char **) xrealloc(retval, count * sizeof (char *));
retval[count-1] = strfrombuf(text, utf8text - text);
if ((ch == '\r') && (nextbyte == '\n')) // DOS endlines!
utf8text++; // skip it.
text = (char *) utf8text; // update to start of new line.
if (i > w)
w = i;
i = -1; // will be zero on next iteration...
} // if
else if ((ch == ' ') || (ch == '\t'))
{
Mar 3, 2008
Mar 3, 2008
848
849
850
851
852
853
854
if (i != 0) // trim spaces from start of line...
furthest = i;
else
{
text++;
i = -1; // it'll be zero on next iteration.
} // else
Mar 3, 2008
Mar 3, 2008
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
} // else if
} // for
// line overflow or end of stream...
pos = (ch) ? furthest : i;
if ((ch) && (furthest == 0)) // uhoh, no split at all...hack it.
{
pos = utf8len(text);
if (pos > scrw) // too big, have to chop a string in the middle.
pos = scrw;
} // if
if (pos > 0)
{
utf8text = text; // adjust pointer by redecoding from start...
for (j = 0; j < pos; j++)
utf8codepoint(&utf8text);
count++;
retval = (char **) xrealloc(retval, count * sizeof (char*));
retval[count-1] = strfrombuf(text, utf8text - text);
text = (char *) utf8text;
if (pos > w)
w = pos;
} // if
} // while
*_count = count;
*_w = w;
return retval;
} // splitText
Apr 23, 2007
Apr 23, 2007
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
static void outOfMemory(void)
{
// Try to translate "out of memory", but not if it causes recursion.
static boolean already_panicked = false;
const char *errstr = "out of memory";
if (!already_panicked)
{
already_panicked = true;
errstr = translate(errstr);
} // if
panic(errstr);
} // outOfMemory
#undef calloc
void *xmalloc(size_t bytes)
{
void *retval = calloc(1, bytes);
if (retval == NULL)
outOfMemory();
return retval;
} // xmalloc
May 9, 2009
May 9, 2009
910
#define calloc(x,y) DO_NOT_CALL_CALLOC__USE_XMALLOC_INSTEAD
Apr 23, 2007
Apr 23, 2007
911
912
913
914
915
916
917
918
919
#undef realloc
void *xrealloc(void *ptr, size_t bytes)
{
void *retval = realloc(ptr, bytes);
if (retval == NULL)
outOfMemory();
return retval;
} // xrealloc
May 9, 2009
May 9, 2009
920
#define realloc(x,y) DO_NOT_CALL_REALLOC__USE_XREALLOC_INSTEAD
Apr 23, 2007
Apr 23, 2007
921
922
923
924
925
926
927
928
929
char *xstrdup(const char *str)
{
char *retval = (char *) xmalloc(strlen(str) + 1);
strcpy(retval, str);
return retval;
} // xstrdup
Sep 26, 2007
Sep 26, 2007
930
931
932
933
// We have to supply this function under certain build types.
#if MOJOSETUP_INTERNAL_BZLIB && BZ_NO_STDIO
void bz_internal_error(int errcode)
{
Feb 2, 2008
Feb 2, 2008
934
fatal(_("bzlib triggered an internal error: %0"), numstr(errcode));
Sep 26, 2007
Sep 26, 2007
935
936
937
938
} // bz_internal_error
#endif
Nov 24, 2007
Nov 24, 2007
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#if SUPPORT_STBIMAGE
unsigned char *stbi_load_from_memory(unsigned char *buffer, int len, int *x,
int *y, int *comp, int req_comp);
#endif
uint8 *decodeImage(const uint8 *data, uint32 size, uint32 *w, uint32 *h)
{
uint8 *retval = MojoPlatform_decodeImage(data, size, w, h);
#if SUPPORT_STBIMAGE
if (retval == NULL) // try our built-in routines.
{
const int siz = (int) size;
unsigned char *buf = (unsigned char *) data;
int x = 0, y = 0, comp = 0;
retval = (uint8 *) stbi_load_from_memory(buf, siz, &x, &y, &comp, 4);
*w = (uint32) x;
*h = (uint32) y;
} // if
#endif
if (retval == NULL)
*w = *h = 0;
return retval;
} // decodeImage
Apr 25, 2009
Apr 25, 2009
967
boolean isValidProductKey(const char *fmt, const char *key)
Apr 25, 2009
Apr 25, 2009
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
if (fmt == NULL)
return true;
else if (key == NULL)
return false;
while (*fmt)
{
const char fmtch = *(fmt++);
const char keych = *(key++);
switch (fmtch)
{
case '-':
case ' ':
if ((keych == ' ') || (keych == '-'))
break;
key--; // user didn't type this, roll back.
break;
case '#':
if ((keych >= '0') && (keych <= '9'))
break;
return false;
case 'X':
if ( ((keych >= 'A') && (keych <= 'Z')) ||
((keych >= 'a') && (keych <= 'z')) )
break;
return false;
case '?':
if ( ((keych >= 'A') && (keych <= 'Z')) ||
((keych >= 'a') && (keych <= 'z')) ||