/
gui_stdio.c
631 lines (521 loc) · 17.3 KB
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.
*/
9
10
11
12
#if !SUPPORT_GUI_STDIO
#error Something is wrong in the build system.
#endif
13
#define BUILDING_EXTERNAL_PLUGIN 1
14
#include "gui.h"
15
16
17
18
19
20
21
MOJOGUI_PLUGIN(stdio)
#if !GUI_STATIC_LINK_STDIO
CREATE_MOJOGUI_ENTRY_POINT(stdio)
#endif
22
23
#include <ctype.h>
24
static char *lastProgressType = NULL;
25
26
27
static char *lastComponent = NULL;
static uint32 percentTicks = 0;
28
29
30
31
32
33
34
35
36
37
38
39
40
static int read_stdin(char *buf, int len)
{
if (fgets(buf, len, stdin) == NULL)
return -1;
len = strlen(buf) - 1;
while ( (len >= 0) && ((buf[len] == '\n') || (buf[len] == '\r')) )
buf[len--] = '\0';
return len+1;
} // read_stdin
41
42
static int readstr(const char *prompt, char *buf, int len,
boolean back, boolean fwd)
43
{
44
45
46
47
// !!! FIXME: if read_stdin() returns -1, we return 0, which makes it
// !!! FIXME: indistinguishable from "user hit enter" ... maybe we should
// !!! FIXME: abort in read_stdin() if i/o fails?
48
int retval = 0;
49
char *backstr = (back) ? xstrdup(_("back")) : NULL;
50
51
52
53
54
55
if (prompt != NULL)
printf("%s\n", prompt);
if (back)
{
56
57
char *fmt = xstrdup(_("Type '%0' to go back."));
char *msg = format(fmt, backstr);
58
59
60
printf("%s\n", msg);
free(msg);
free(fmt);
61
62
} // if
63
64
if (fwd)
{
65
printf("%s", _("Press enter to continue."));
66
67
68
printf("\n");
} // if
69
printf("%s",_("> "));
70
71
72
73
fflush(stdout);
if ((retval = read_stdin(buf, len)) >= 0)
{
74
if ((back) && (strcmp(buf, backstr) == 0)) // !!! FIXME: utf8casecmp?
75
76
77
retval = -1;
} // if
78
free(backstr);
79
return retval;
80
} // readstr
81
82
83
static uint8 MojoGui_stdio_priority(boolean istty)
84
{
85
86
87
88
89
90
91
92
// if not a tty and no other GUI plugins worked out, let the base
// application try to spawn a terminal and try again. If it can't do so,
// it will panic() and thus end the process, so we don't end up blocking
// on some prompt the user can't see.
if (!istty)
return MOJOGUI_PRIORITY_NEVER_TRY;
93
return MOJOGUI_PRIORITY_TRY_ABSOLUTELY_LAST; // always a last resort.
94
95
} // MojoGui_stdio_priority
96
97
static boolean MojoGui_stdio_init(void)
98
{
99
percentTicks = 0;
100
return true; // always succeeds.
101
102
} // MojoGui_stdio_init
103
104
static void MojoGui_stdio_deinit(void)
105
{
106
free(lastProgressType);
107
free(lastComponent);
108
lastProgressType = NULL;
109
lastComponent = NULL;
110
111
} // MojoGui_stdio_deinit
112
113
static void MojoGui_stdio_msgbox(const char *title, const char *text)
114
{
115
char buf[128];
116
117
char *fmt = xstrdup(_("NOTICE: %0\n[hit enter]"));
char *msg = format(fmt, text);
118
119
120
printf("%s\n", msg);
free(msg);
free(fmt);
121
fflush(stdout);
122
read_stdin(buf, sizeof (buf));
123
} // MojoGui_stdio_msgbox
124
125
126
127
static boolean MojoGui_stdio_promptyn(const char *title, const char *text,
boolean defval)
128
{
129
130
boolean retval = false;
if (!feof(stdin))
131
{
132
133
134
135
136
const char *_fmt = ((defval) ? _("%0 [Y/n]: ") : _("%0 [y/N]: "));
char *fmt = xstrdup(_fmt);
char *msg = format(fmt, text);
char *localized_no = xstrdup(_("N"));
char *localized_yes = xstrdup(_("Y"));
137
boolean getout = false;
138
char buf[128];
139
140
while (!getout)
141
{
142
143
144
int rc = 0;
getout = true; // we may reset this later.
145
printf("%s", msg);
146
fflush(stdout);
147
148
149
150
151
152
rc = read_stdin(buf, sizeof (buf));
if (rc < 0)
retval = false;
else if (rc == 0)
retval = defval;
153
else if (strcasecmp(buf, localized_no) == 0)
154
retval = false;
155
else if (strcasecmp(buf, localized_yes) == 0)
156
157
158
retval = true;
else
getout = false; // try again.
159
} // while
160
161
162
163
164
free(localized_yes);
free(localized_no);
free(msg);
free(fmt);
165
} // if
166
167
return retval;
168
} // MojoGui_stdio_promptyn
169
170
171
172
static MojoGuiYNAN MojoGui_stdio_promptynan(const char *title, const char *txt,
boolean defval)
173
174
175
176
{
MojoGuiYNAN retval = MOJOGUI_NO;
if (!feof(stdin))
{
177
178
179
180
181
182
char *fmt = xstrdup(_("%0\n[y/n/Always/Never]: "));
char *msg = format(fmt, txt);
char *localized_no = xstrdup(_("N"));
char *localized_yes = xstrdup(_("Y"));
char *localized_always = xstrdup(_("Always"));
char *localized_never = xstrdup(_("Never"));
183
184
boolean getout = false;
char buf[128];
185
186
187
while (!getout)
{
188
189
190
int rc = 0;
getout = true; // we may reset this later.
191
printf("%s\n", msg);
192
fflush(stdout);
193
194
195
196
197
198
rc = read_stdin(buf, sizeof (buf));
if (rc < 0)
retval = MOJOGUI_NO;
else if (rc == 0)
retval = (defval) ? MOJOGUI_YES : MOJOGUI_NO;
199
else if (strcasecmp(buf, localized_no) == 0)
200
retval = MOJOGUI_NO;
201
202
203
204
205
206
else if (strcasecmp(buf, localized_yes) == 0)
retval = MOJOGUI_YES;
else if (strcasecmp(buf, localized_always) == 0)
retval = MOJOGUI_ALWAYS;
else if (strcasecmp(buf, localized_never) == 0)
retval = MOJOGUI_NEVER;
207
208
else
getout = false; // try again.
209
} // while
210
211
212
213
214
215
216
free(localized_never);
free(localized_always);
free(localized_yes);
free(localized_no);
free(msg);
free(fmt);
217
218
219
220
221
222
} // if
return retval;
} // MojoGui_stdio_promptynan
223
224
static boolean MojoGui_stdio_start(const char *title,
const MojoGuiSplash *splash)
225
226
227
{
printf("%s\n", title);
return true;
228
} // MojoGui_stdio_start
229
230
231
static void MojoGui_stdio_stop(void)
232
233
{
// no-op.
234
235
236
} // MojoGui_stdio_stop
237
238
239
static void dumb_pager(const char *name, const char *data, size_t datalen)
{
const int MAX_PAGE_LINES = 21;
240
char *fmt = xstrdup(_("(%0-%1 of %2 lines, see more?)"));
241
242
243
int i = 0;
int w = 0;
int linecount = 0;
244
boolean getout = false;
245
char **lines = splitText(data, 80, &linecount, &w);
246
247
assert(linecount >= 0);
248
249
250
251
252
253
printf("%s\n", name);
if (lines == NULL) // failed to parse?!
printf("%s\n", data); // just dump it all. Oh well.
else
254
{
255
256
int printed = 0;
do
257
{
258
for (i = 0; (i < MAX_PAGE_LINES) && (printed < linecount); i++)
259
printf("%s", lines[printed++]);
260
261
262
263
264
if (printed >= linecount)
getout = true;
else
{
265
char *msg = NULL;
266
printf("\n");
267
268
msg = format(fmt, numstr((printed-i)+1),
numstr(printed), numstr(linecount));
269
270
getout = !MojoGui_stdio_promptyn("", msg, true);
free(msg);
271
272
273
printf("\n");
} // else
} while (!getout);
274
275
} // while
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
for (i = 0; i < linecount; i++)
free(lines[i]);
free(lines);
free(fmt);
} // dumb_pager
static int MojoGui_stdio_readme(const char *name, const uint8 *_data,
size_t datalen, boolean can_back,
boolean can_fwd)
{
const char *data = (const char *) _data;
char buf[256];
int retval = -1;
boolean failed = true;
292
293
// !!! FIXME: popen() isn't reliable.
#if 0 //PLATFORM_UNIX
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const size_t namelen = strlen(name);
const char *programs[] = { getenv("PAGER"), "more", "less -M", "less" };
int i = 0;
// flush streams, so output doesn't mingle with the popen()'d process.
fflush(stdout);
fflush(stderr);
for (i = 0; i < STATICARRAYLEN(programs); i++)
{
const char *cmd = programs[i];
if (cmd != NULL)
{
FILE *io = popen(cmd, "w");
if (io != NULL)
{
failed = false;
if (!failed) failed = (fwrite("\n", 1, 1, io) != 1);
if (!failed) failed = (fwrite(name, namelen, 1, io) != 1);
if (!failed) failed = (fwrite("\n", 1, 1, io) != 1);
if (!failed) failed = (fwrite(data, datalen, 1, io) != 1);
if (!failed) failed = (fwrite("\n", 1, 1, io) != 1);
failed |= (pclose(io) != 0); // call whether we failed or not.
if (!failed)
break; // it worked, we're done!
} // if
} // if
} // for
#endif // PLATFORM_UNIX
if (failed) // We're not Unix, or none of the pagers worked?
dumb_pager(name, data, datalen);
327
328
329
// Put up the "hit enter to continue (or 'back' to go back)" prompt,
// but only if there's an choice to be made here.
if ((!can_back) || (readstr(NULL, buf, sizeof (buf), can_back, true) >= 0))
330
331
retval = 1;
332
return retval;
333
} // MojoGui_stdio_readme
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
static void toggle_option(MojoGuiSetupOptions *parent,
MojoGuiSetupOptions *opts, int *line, int target)
{
if ((opts != NULL) && (target > *line))
{
if (!opts->is_group_parent)
{
if (++(*line) == target)
{
const boolean toggled = ((opts->value) ? false : true);
// "radio buttons" in a group?
if ((parent) && (parent->is_group_parent))
{
if (toggled) // drop unless we weren't the current toggle.
{
// set all siblings to false...
MojoGuiSetupOptions *i = parent->child;
while (i != NULL)
{
i->value = false;
i = i->next_sibling;
} // while
opts->value = true; // reset us to be true.
} // if
} // if
else // individual "check box" was chosen.
{
opts->value = toggled;
} // else
return; // we found it, bail.
} // if
} // if
if (opts->value) // if option is toggled on, descend to children.
toggle_option(opts, opts->child, line, target);
toggle_option(parent, opts->next_sibling, line, target);
} // if
} // toggle_option
379
380
381
382
383
384
385
static void print_options(MojoGuiSetupOptions *opts, int *line, int level)
{
if (opts != NULL)
{
int i;
int spacing = 1;
if (opts->is_group_parent)
386
spacing += 6;
387
else
388
389
{
(*line)++;
390
printf("%2d [%c]", *line, opts->value ? 'X' : ' ');
391
} // else
392
393
for (i = 0; i < (level + spacing); i++)
394
395
putchar(' ');
396
printf("%s%s\n", opts->description, opts->is_group_parent ? ":" : "");
397
398
if ((opts->value) || (opts->is_group_parent))
399
print_options(opts->child, line, level+1);
400
401
402
403
404
print_options(opts->next_sibling, line, level);
} // if
} // print_options
405
static int MojoGui_stdio_options(MojoGuiSetupOptions *opts,
406
boolean can_back, boolean can_fwd)
407
{
408
409
const char *inst_opts_str = xstrdup(_("Options"));
const char *prompt = xstrdup(_("Choose number to change."));
410
int retval = -1;
411
412
413
414
415
416
417
418
419
boolean getout = false;
char buf[128];
int len = 0;
while (!getout)
{
int line = 0;
printf("\n\n");
420
printf("%s", inst_opts_str);
421
422
423
424
printf("\n");
print_options(opts, &line, 1);
printf("\n");
425
if ((len = readstr(prompt, buf, sizeof (buf), can_back, true)) < 0)
426
427
getout = true;
else if (len == 0)
428
429
430
431
{
getout = true;
retval = 1;
} // else if
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
else
{
char *endptr = NULL;
int target = (int) strtol(buf, &endptr, 10);
if (*endptr == '\0') // complete string was a valid number?
{
line = 0;
toggle_option(NULL, opts, &line, target);
} // if
} // else
} // while
free((void *) inst_opts_str);
free((void *) prompt);
return retval;
448
449
} // MojoGui_stdio_options
450
451
452
453
static char *MojoGui_stdio_destination(const char **recommends, int recnum,
int *command, boolean can_back,
boolean can_fwd)
454
{
455
const char *instdeststr = xstrdup(_("Destination"));
456
457
458
459
const char *prompt = NULL;
char *retval = NULL;
boolean getout = false;
char buf[128];
460
int len = 0;
461
462
int i = 0;
463
464
465
*command = -1;
if (recnum > 0)
466
prompt = xstrdup(_("Choose install destination by number (hit enter for #1), or enter your own."));
467
else
468
prompt = xstrdup(_("Enter path where files will be installed."));
469
470
471
472
while (!getout)
{
printf("\n\n%s\n", instdeststr);
473
for (i = 0; i < recnum; i++)
474
475
476
printf(" %2d %s\n", i+1, recommends[i]);
printf("\n");
477
if ((len = readstr(prompt, buf, sizeof (buf), can_back, false)) < 0)
478
getout = true;
479
480
481
else if ((len == 0) && (recnum > 0)) // default to first in list.
{
482
retval = xstrdup(recommends[0]);
483
484
485
486
*command = 1;
getout = true;
} // else if
487
else if (len > 0)
488
489
490
491
{
char *endptr = NULL;
int target = (int) strtol(buf, &endptr, 10);
// complete string was a valid number?
492
if ((*endptr == '\0') && (target > 0) && (target <= recnum))
493
retval = xstrdup(recommends[target-1]);
494
else
495
retval = xstrdup(buf);
496
497
*command = 1;
498
499
500
501
502
503
504
505
506
507
getout = true;
} // else
} // while
free((void *) prompt);
free((void *) instdeststr);
return retval;
} // MojoGui_stdio_destination
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
static int MojoGui_stdio_productkey(const char *desc, const char *fmt,
char *buf, const int buflen,
boolean can_back, boolean can_fwd)
{
const char *prompt = xstrdup(_("Please enter your product key"));
char *defval = ((*buf) ? xstrdup(buf) : NULL);
boolean getout = false;
int retval = -1;
char *msg = NULL;
if (defval != NULL)
{
char *locfmt = xstrdup(_("(just press enter to use '%0')"));
msg = format(locfmt, defval);
free(locfmt);
} // if
while (!getout)
{
int len;
printf("\n\n%s\n", desc);
if (msg != NULL)
printf("%s\n", msg);
if ((len = readstr(prompt, buf, buflen, can_back, false)) < 0)
getout = true;
else
{
if ((len == 0) && (defval != NULL))
strcpy(buf, defval);
539
if (isValidProductKey(fmt, buf))
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
{
retval = 1;
getout = true;
} // else if
else
{
// We can't check the input character-by-character, so reuse
// the failed-verification localized string.
printf("\n%s\n\n",
_("That key appears to be invalid. Please try again."));
} // else
} // else
} // while
free(msg);
free(defval);
free((void *) prompt);
return retval;
} // MojoGui_stdio_productkey
562
563
564
static boolean MojoGui_stdio_insertmedia(const char *medianame)
{
char buf[32];
565
566
567
char *fmt = xstrdup(_("Please insert '%0'"));
char *msg = format(fmt, medianame);
printf("%s\n", _("Media change"));
568
569
570
printf("%s\n", msg);
free(msg);
free(fmt);
571
return (readstr(NULL, buf, sizeof (buf), false, true) >= 0);
572
573
} // MojoGui_stdio_insertmedia
574
575
576
577
578
579
580
581
static void MojoGui_stdio_progressitem(void)
{
// force new line of output on next call to MojoGui_stdio_progress()
percentTicks = 0;
} // MojoGui_stdio_progressitem
582
static boolean MojoGui_stdio_progress(const char *type, const char *component,
583
584
int percent, const char *item,
boolean can_cancel)
585
{
586
const uint32 now = ticks();
587
588
589
590
591
if ( (lastComponent == NULL) ||
(strcmp(lastComponent, component) != 0) ||
(lastProgressType == NULL) ||
(strcmp(lastProgressType, type) != 0) )
592
{
593
free(lastProgressType);
594
free(lastComponent);
595
596
lastProgressType = xstrdup(type);
lastComponent = xstrdup(component);
597
printf("%s\n%s\n", type, component);
598
599
} // if
600
601
602
// limit update spam... will only write every one second, tops,
// on any given filename, but it writes each filename at least once
// so it doesn't look like we only installed a few things.
603
604
if (percentTicks <= now)
{
605
606
char *fmt = NULL;
char *msg = NULL;
607
percentTicks = now + 1000;
608
if (percent < 0)
609
printf("%s\n", item);
610
else
611
{
612
613
fmt = xstrdup(_("%0 (total progress: %1%%)"));
msg = format(fmt, item, numstr(percent));
614
615
616
617
printf("%s\n", msg);
free(msg);
free(fmt);
} // else
618
} // if
619
620
621
return true;
} // MojoGui_stdio_progress
622
623
624
625
626
627
628
629
static void MojoGui_stdio_final(const char *msg)
{
printf("%s\n\n", msg);
fflush(stdout);
} // MojoGui_stdio_final
630
// end of gui_stdio.c ...