Skip to content

Latest commit

 

History

History
1116 lines (915 loc) · 28.4 KB

gui_www.c

File metadata and controls

1116 lines (915 loc) · 28.4 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 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.
*/
#if !SUPPORT_GUI_WWW
#error Something is wrong in the build system.
#endif
#define BUILDING_EXTERNAL_PLUGIN 1
#include "gui.h"
MOJOGUI_PLUGIN(www)
#if !GUI_STATIC_LINK_WWW
CREATE_MOJOGUI_ENTRY_POINT(www)
#endif
#include <stdarg.h>
#define FREE_AND_NULL(x) { free(x); x = NULL; }
// tapdance between things WinSock and BSD Sockets define differently...
#if PLATFORM_WINDOWS
#include <winsock.h>
typedef int socklen_t;
Sep 25, 2007
Sep 25, 2007
32
#define setprotoent(x) assert(x == 0)
33
34
35
36
37
38
39
40
41
42
43
44
45
#define sockErrno() WSAGetLastError()
#define wouldBlockError(err) (err == WSAEWOULDBLOCK)
#define intrError(err) (err == WSAEINTR)
static inline void setBlocking(SOCKET s, boolean blocking)
{
u_long val = (blocking) ? 0 : 1;
ioctlsocket(s, FIONBIO, &val);
} // setBlocking
static const char *sockStrErrVal(int val)
{
STUBBED("Windows strerror");
Feb 2, 2008
Feb 2, 2008
46
return "sockStrErrVal() is unimplemented.";
47
48
49
50
51
52
53
54
55
} // sockStrErrVal
static boolean initSocketSupport(void)
{
WSADATA data;
int rc = WSAStartup(MAKEWORD(1, 1), &data);
if (rc != 0)
{
Mar 2, 2008
Mar 2, 2008
56
logError("www: WSAStartup() failed: %0", sockStrErrVal(rc));
57
58
59
return false;
} // if
Mar 2, 2008
Mar 2, 2008
60
61
62
63
64
65
66
67
logInfo("www: WinSock initialized (want %0.%1, got %2.%3).",
numstr((int) (LOBYTE(data.wVersion))),
numstr((int) (HIBYTE(data.wVersion))),
numstr((int) (LOBYTE(data.wHighVersion))),
numstr((int) (HIBYTE(data.wHighVersion))));
logInfo("www: WinSock description: %0", data.szDescription);
logInfo("www: WinSock system status: %0", data.szSystemStatus);
logInfo("www: WinSock max sockets: %0", numstr((int) data.iMaxSockets));
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
return true;
} // initSocketSupport
#define deinitSocketSupport() WSACleanup()
#else
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
#include <fcntl.h>
typedef int SOCKET;
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET (-1)
#define closesocket(x) close(x)
#define sockErrno() (errno)
#define sockStrErrVal(val) strerror(val)
#define intrError(err) (err == EINTR)
#define initSocketSupport() (true)
#define deinitSocketSupport()
static inline boolean wouldBlockError(int err)
{
return ((err == EWOULDBLOCK) || (err == EAGAIN));
} // wouldBlockError
static void setBlocking(SOCKET s, boolean blocking)
{
int flags = fcntl(s, F_GETFL, 0);
if (blocking)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
fcntl(s, F_SETFL, flags);
} // setBlocking
#endif
#define sockStrError() (sockStrErrVal(sockErrno()))
typedef struct _S_WebRequest
{
char *key;
char *value;
struct _S_WebRequest *next;
} WebRequest;
static char *output = NULL;
static char *lastProgressType = NULL;
static char *lastComponent = NULL;
static char *baseUrl = NULL;
static WebRequest *webRequest = NULL;
static uint32 percentTicks = 0;
static SOCKET listenSocket = INVALID_SOCKET;
static SOCKET clientSocket = INVALID_SOCKET;
Nov 21, 2007
Nov 21, 2007
134
static uint8 MojoGui_www_priority(boolean istty)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
return MOJOGUI_PRIORITY_TRY_LAST;
} // MojoGui_www_priority
static void freeWebRequest(void)
{
while (webRequest)
{
WebRequest *next = webRequest->next;
free(webRequest->key);
free(webRequest->value);
free(webRequest);
webRequest = next;
} // while
} // freeWebRequest
static void addWebRequest(const char *key, const char *val)
{
if ((key != NULL) && (*key != '\0'))
{
Mar 2, 2008
Mar 2, 2008
157
158
159
WebRequest *req = (WebRequest *) xmalloc(sizeof (WebRequest));
req->key = xstrdup(key);
req->value = xstrdup(val);
160
161
req->next = webRequest;
webRequest = req;
Mar 2, 2008
Mar 2, 2008
162
logDebug("www: request element '%0' = '%1'", key, val);
163
164
165
166
167
168
169
} // if
} // addWebRequest
static int hexVal(char ch)
{
if ((ch >= 'a') && (ch <= 'f'))
Jun 2, 2007
Jun 2, 2007
170
return (ch - 'a') + 10;
171
else if ((ch >= 'A') && (ch <= 'F'))
Jun 2, 2007
Jun 2, 2007
172
return (ch - 'A') + 10;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
else if ((ch >= '0') && (ch <= '9'))
return (ch - '0');
return -1;
} // hexVal
static void unescapeUri(char *uri)
{
char *ptr = uri;
while ((ptr = strchr(ptr, '%')) != NULL)
{
int a, b;
if ((a = hexVal(ptr[1])) != -1)
{
if ((b = hexVal(ptr[2])) != -1)
{
*(ptr++) = (char) ((a * 16) + b);
memmove(ptr, ptr+2, strlen(ptr+1));
} // if
else
{
*(ptr++) = '?';
memmove(ptr, ptr+1, strlen(ptr));
} // else
} // if
else
{
*(ptr++) = '?';
} // else
} // while
} // unescapeUri
static int strAdd(char **ptr, size_t *len, size_t *alloc, const char *fmt, ...)
{
Sep 25, 2007
Sep 25, 2007
208
size_t bw = 0;
209
210
211
212
213
214
215
216
217
218
219
size_t avail = *alloc - *len;
va_list ap;
va_start(ap, fmt);
bw = vsnprintf(*ptr + *len, avail, fmt, ap);
va_end(ap);
if (bw >= avail)
{
const size_t add = (*alloc + (bw + 1)); // double plus the new len.
*alloc += add;
avail += add;
Mar 2, 2008
Mar 2, 2008
220
*ptr = xrealloc(*ptr, *alloc);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
va_start(ap, fmt);
bw = vsnprintf(*ptr + *len, avail, fmt, ap);
va_end(ap);
} // if
*len += bw;
return bw;
} // strAdd
static char *htmlescape(const char *str)
{
size_t len = 0, alloc = 0;
char *retval = NULL;
char ch;
Jun 1, 2007
Jun 1, 2007
237
while ((ch = *(str++)) != '\0')
238
239
240
241
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
275
276
277
278
279
280
281
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
{
switch (ch)
{
case '&': strAdd(&retval, &len, &alloc, "&amp;"); break;
case '<': strAdd(&retval, &len, &alloc, "&lt;"); break;
case '>': strAdd(&retval, &len, &alloc, "&gt;"); break;
case '"': strAdd(&retval, &len, &alloc, "&quot;"); break;
case '\'': strAdd(&retval, &len, &alloc, "&#39;"); break;
default: strAdd(&retval, &len, &alloc, "%c", ch); break;
} // switch
} // while
return retval;
} // htmlescape
static const char *standardResponseHeaders =
"Content-Type: text/html; charset=utf-8\n"
"Accept-Ranges: none\n"
"Cache-Control: no-cache\n"
"Connection: close\n\n";
static void setHtmlString(char **str, int responseCode,
const char *responseString,
const char *title, const char *html)
{
size_t len = 0, alloc = 0;
FREE_AND_NULL(*str);
strAdd(str, &len, &alloc,
"HTTP/1.1 %d %s\n" // responseCode, responseString
"%s" // standardResponseHeaders
"<html>"
"<head>"
"<title>%s</title>" // title
"</head>"
"<body>%s</body>" // html
"</html>\n",
responseCode, responseString,
standardResponseHeaders,
title, html);
} // setHtmlString
static void setHtml(const char *title, const char *html)
{
setHtmlString(&output, 200, "OK", title, html);
} // setHtml
static void sendStringAndDrop(SOCKET *_s, const char *str)
{
SOCKET s = *_s;
int outlen = 0;
if (str == NULL)
str = "";
else
outlen = strlen(str);
setBlocking(s, true);
while (outlen > 0)
{
int rc = send(s, str, outlen, 0);
if (rc != SOCKET_ERROR)
{
str += rc;
outlen -= rc;
} // if
else
{
const int err = sockErrno();
if (!intrError(err))
{
Mar 2, 2008
Mar 2, 2008
312
logError("www: send() failed: %0", sockStrErrVal(err));
313
314
315
316
317
318
319
320
321
322
323
324
break;
} // if
} // else
} // while
closesocket(s);
*_s = INVALID_SOCKET;
} // sendStringAndDrop
static void respond404(SOCKET *s)
{
Mar 2, 2008
Mar 2, 2008
325
char *text = htmlescape(_("Not Found"));
326
327
328
329
330
331
332
333
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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
char *str = NULL;
size_t len = 0, alloc = 0;
char *html = NULL;
strAdd(&html, &len, &alloc, "<center><h1>%s</h1></center>", text);
setHtmlString(&str, 404, text, text, html);
free(html);
free(text);
sendStringAndDrop(s, str);
free(str);
} // respond404
static boolean parseGet(char *get)
{
char *uri = NULL;
char *ver = NULL;
uri = strchr(get, ' ');
if (uri == NULL) return false;
*(uri++) = '\0';
ver = strchr(uri, ' ');
if (ver == NULL) return false;
*(ver++) = '\0';
if (strcmp(get, "GET") != 0) return false;
if (uri[0] != '/') return false;
uri++; // skip dirsep.
// !!! FIXME: we may want to feed stock files (<img> tags, etc)
// !!! FIXME: at some point in the future.
if ((uri[0] != '?') && (uri[0] != '\0')) return false;
if (strncmp(ver, "HTTP/", 5) != 0) return false;
if (*uri == '?')
uri++; // skip initial argsep.
do
{
char *next = strchr(uri, '&');
char *val = NULL;
if (next != NULL)
*(next++) = '\0';
val = strchr(uri, '=');
if (val == NULL)
val = "";
else
*(val++) = '\0';
unescapeUri(uri);
unescapeUri(val);
addWebRequest(uri, val);
uri = next;
} while (uri != NULL);
return true;
} // parseGet
static boolean parseRequest(char *reqstr)
{
do
{
char *next = strchr(reqstr, '\n');
char *val = NULL;
if (next != NULL)
*(next++) = '\0';
val = strchr(reqstr, ':');
if (val == NULL)
val = "";
else
{
*(val++) = '\0';
while (*val == ' ')
val++;
} // else
if (*reqstr != '\0')
{
size_t len = 0, alloc = 0;
char *buf = NULL;
strAdd(&buf, &len, &alloc, "HTTP-%s", reqstr);
addWebRequest(buf, val);
free(buf);
} // if
reqstr = next;
} while (reqstr != NULL);
return true;
} // parseRequest
static WebRequest *servePage(boolean blocking)
{
int newline = 0;
char ch = 0;
struct sockaddr_in addr;
socklen_t addrlen = 0;
int s = 0;
char *reqstr = NULL;
size_t len = 0, alloc = 0;
int err = 0;
freeWebRequest();
if (listenSocket == INVALID_SOCKET)
return NULL;
if (clientSocket != INVALID_SOCKET) // response to feed to client.
sendStringAndDrop(&clientSocket, output);
if (blocking)
setBlocking(listenSocket, true);
do
{
s = accept(listenSocket, (struct sockaddr *) &addr, &addrlen);
err = sockErrno();
} while ( (s == INVALID_SOCKET) && (intrError(err)) );
if (blocking)
setBlocking(listenSocket, false); // reset what we toggled up there.
if (s == INVALID_SOCKET)
{
if (wouldBlockError(err))
assert(!blocking);
else
{
Mar 2, 2008
Mar 2, 2008
460
logError("www: accept() failed: %0", sockStrErrVal(err));
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
closesocket(listenSocket); // make all future i/o fail too.
listenSocket = INVALID_SOCKET;
} // else
return NULL;
} // if
setBlocking(s, true);
// Doing this one char at a time isn't efficient, but it's easy.
while (1)
{
if (recv(s, &ch, 1, 0) == SOCKET_ERROR)
{
const int err = sockErrno();
if (!intrError(err)) // just try again on interrupt.
{
Mar 2, 2008
Mar 2, 2008
478
logError("www: recv() failed: %0", sockStrErrVal(err));
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
FREE_AND_NULL(reqstr);
closesocket(s);
s = INVALID_SOCKET;
break;
} // if
} // if
else if (ch == '\n') // newline
{
if (++newline == 2)
break; // end of request.
strAdd(&reqstr, &len, &alloc, "\n");
} // if
else if (ch != '\r')
{
newline = 0;
strAdd(&reqstr, &len, &alloc, "%c", ch);
} // else if
} // while
if (reqstr != NULL)
{
char *get = NULL;
char *ptr = strchr(reqstr, '\n');
if (ptr != NULL)
{
*ptr = '\0';
ptr++;
} // if
// reqstr is the GET (or whatever) request, ptr is the rest.
Mar 2, 2008
Mar 2, 2008
511
get = xstrdup(reqstr);
512
513
514
515
516
517
518
519
520
521
522
if (ptr == NULL)
{
*ptr = '\0';
len = 0;
} // if
else
{
len = strlen(ptr);
memmove(reqstr, ptr, len+1);
} // else
Mar 2, 2008
Mar 2, 2008
523
logDebug("www: request '%0'", get);
524
525
526
527
// okay, now (get) and (reqptr) are separate strings.
// These parse*() functions update (webRequest).
if ( (parseGet(get)) && (parseRequest(reqstr)) )
Mar 2, 2008
Mar 2, 2008
528
logDebug("www: accepted request");
529
530
else
{
Mar 2, 2008
Mar 2, 2008
531
logError("www: rejected bogus request");
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
freeWebRequest();
respond404(&s);
} // else
free(reqstr);
free(get);
} // if
clientSocket = s;
return webRequest;
} // servePage
static SOCKET create_listen_socket(short portnum)
{
SOCKET s = INVALID_SOCKET;
int protocol = 0; // pray this is right.
struct protoent *prot;
setprotoent(0);
prot = getprotobyname("tcp");
if (prot != NULL)
protocol = prot->p_proto;
s = socket(PF_INET, SOCK_STREAM, protocol);
if (s == INVALID_SOCKET)
Mar 2, 2008
Mar 2, 2008
558
logInfo("www: socket() failed ('%0')", sockStrError());
559
560
561
562
563
564
565
else
{
boolean success = false;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(portnum);
addr.sin_addr.s_addr = INADDR_ANY; // !!! FIXME: bind to localhost.
Jun 1, 2007
Jun 1, 2007
566
567
568
569
570
// So we can bind this socket over and over in debug runs...
#if ((!defined _NDEBUG) && (!defined NDEBUG))
{
int on = 1;
Sep 25, 2007
Sep 25, 2007
571
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof (on));
Jun 1, 2007
Jun 1, 2007
572
573
574
}
#endif
575
if (bind(s, (struct sockaddr *) &addr, sizeof (addr)) == SOCKET_ERROR)
Mar 2, 2008
Mar 2, 2008
576
logError("www: bind() failed ('%0')", sockStrError());
577
else if (listen(s, 5) == SOCKET_ERROR)
Mar 2, 2008
Mar 2, 2008
578
logError("www: listen() failed ('%0')", sockStrError());
579
580
else
{
Mar 2, 2008
Mar 2, 2008
581
582
logInfo("www: socket created on port %0",
numstr(portnum));
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
success = true;
} // else
if (!success)
{
closesocket(s);
s = INVALID_SOCKET;
} // if
} // if
return s;
} // create_listen_socket
static boolean MojoGui_www_init(void)
{
size_t len = 0, alloc = 0;
short portnum = 7341; // !!! FIXME: try some random ports.
percentTicks = 0;
if (!initSocketSupport())
{
Mar 2, 2008
Mar 2, 2008
605
logInfo("www: socket subsystem init failed, use another UI.");
606
607
608
609
610
611
return false;
} // if
listenSocket = create_listen_socket(portnum);
if (listenSocket < 0)
{
Mar 2, 2008
Mar 2, 2008
612
logInfo("www: no listen socket, use another UI.");
613
614
615
616
617
618
619
620
621
622
623
624
625
return false;
} // if
setBlocking(listenSocket, false);
strAdd(&baseUrl, &len, &alloc, "http://localhost:%d/", (int) portnum);
return true;
} // MojoGui_www_init
static void MojoGui_www_deinit(void)
{
// Catch any waiting browser connections...and tell them to buzz off! :)
Mar 2, 2008
Mar 2, 2008
626
627
char *donetitle = htmlescape(_("Shutting down..."));
char *donetext = htmlescape(_("You can close this browser now."));
628
629
630
size_t len = 0, alloc = 0;
char *html = NULL;
Jun 1, 2007
Jun 1, 2007
631
strAdd(&html, &len, &alloc, "<hr><center>%s</center><hr>", donetext);
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
setHtml(donetitle, html);
free(html);
free(donetitle);
free(donetext);
while (servePage(false) != NULL) { /* no-op. */ }
freeWebRequest();
FREE_AND_NULL(output);
FREE_AND_NULL(lastProgressType);
FREE_AND_NULL(lastComponent);
FREE_AND_NULL(baseUrl);
if (clientSocket != INVALID_SOCKET)
{
closesocket(clientSocket);
clientSocket = INVALID_SOCKET;
} // if
if (listenSocket != INVALID_SOCKET)
{
closesocket(listenSocket);
listenSocket = INVALID_SOCKET;
} // if
deinitSocketSupport();
} // MojoGui_www_deinit
Jun 1, 2007
Jun 1, 2007
660
static int doPromptPage(const char *title, const char *text, boolean centertxt,
661
const char *pagename,
Jun 1, 2007
Jun 1, 2007
662
const char **buttons, const char **locButtons,
663
664
665
666
667
668
669
670
int bcount)
{
char *htmltitle = htmlescape(title);
boolean sawPage = false;
int answer = -1;
int i = 0;
char *html = NULL;
size_t len = 0, alloc = 0;
Jun 1, 2007
Jun 1, 2007
671
const char *align = ((centertxt) ? " align='center'" : "");
672
673
674
675
676
677
strAdd(&html, &len, &alloc,
"<center>"
"<form name='form_%s' method='get'>" // pagename
"<input type='hidden' name='page' value='%s'>" // pagename
"<table>"
Jun 1, 2007
Jun 1, 2007
678
"<tr><td%s>%s</td></tr>" // align, text
679
"<tr>"
Jun 1, 2007
Jun 1, 2007
680
"<td align='center'>", pagename, pagename, align, text);
681
682
683
684
for (i = 0; i < bcount; i++)
{
const char *button = buttons[i];
Jun 1, 2007
Jun 1, 2007
685
const char *loc = locButtons[i];
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
strAdd(&html, &len, &alloc,
"<input type='submit' name='%s' value='%s'>", button, loc);
} // for
strAdd(&html, &len, &alloc,
"</td>"
"</tr>"
"</table>"
"</form>"
"</center>");
setHtml(htmltitle, html);
free(htmltitle);
free(html);
while ((!sawPage) || (answer == -1))
{
WebRequest *req = servePage(true);
sawPage = false;
answer = -1;
while (req != NULL)
{
const char *k = req->key;
const char *v = req->value;
if ( (strcmp(k, "page") == 0) && (strcmp(v, pagename) == 0) )
sawPage = true;
else
{
for (i = 0; i < bcount; i++)
{
if (strcmp(k, buttons[i]) == 0)
{
answer = i;
break;
} // if
} // for
} // else
req = req->next;
Jun 2, 2007
Jun 2, 2007
725
} // while
726
727
728
729
730
731
732
733
} // while
return answer;
} // doPromptPage
static void MojoGui_www_msgbox(const char *title, const char *text)
{
Jun 1, 2007
Jun 1, 2007
734
const char *buttons[] = { "ok" };
Mar 2, 2008
Mar 2, 2008
735
const char *locButtons[] = { htmlescape(_("OK")) };
736
char *htmltext = htmlescape(text);
Jun 1, 2007
Jun 1, 2007
737
doPromptPage(title, htmltext, true, "msgbox", buttons, locButtons, 1);
738
free(htmltext);
Jun 1, 2007
Jun 1, 2007
739
free((void *) locButtons[0]);
740
741
742
} // MojoGui_www_msgbox
Jul 2, 2007
Jul 2, 2007
743
744
static boolean MojoGui_www_promptyn(const char *title, const char *text,
boolean defval)
745
{
Jul 2, 2007
Jul 2, 2007
746
747
748
// !!! FIXME:
// We currently ignore defval
749
750
int i, rc;
char *htmltext = htmlescape(text);
Jun 1, 2007
Jun 1, 2007
751
const char *buttons[] = { "no", "yes" };
Mar 2, 2008
Mar 2, 2008
752
753
const char *locButtons[] = { htmlescape(_("No")), htmlescape(_("Yes")) };
Jun 1, 2007
Jun 1, 2007
754
assert(STATICARRAYLEN(buttons) == STATICARRAYLEN(locButtons));
755
Jun 1, 2007
Jun 1, 2007
756
rc = doPromptPage(title, htmltext, true, "promptyn", buttons, locButtons,
757
758
759
STATICARRAYLEN(buttons));
free(htmltext);
Jun 1, 2007
Jun 1, 2007
760
761
for (i = 0; i < STATICARRAYLEN(locButtons); i++)
free((void *) locButtons[i]);
762
763
764
765
766
return (rc == 1);
} // MojoGui_www_promptyn
Jul 2, 2007
Jul 2, 2007
767
768
static MojoGuiYNAN MojoGui_www_promptynan(const char *title, const char *text,
boolean defval)
769
{
Jul 2, 2007
Jul 2, 2007
770
771
772
// !!! FIXME:
// We currently ignore defval
773
774
int i, rc;
char *htmltext = htmlescape(text);
Jun 1, 2007
Jun 1, 2007
775
776
const char *buttons[] = { "no", "yes", "always", "never" };
const char *locButtons[] = {
Mar 2, 2008
Mar 2, 2008
777
778
779
780
htmlescape(_("No")),
htmlescape(_("Yes")),
htmlescape(_("Always")),
htmlescape(_("Never")),
781
};
Jun 1, 2007
Jun 1, 2007
782
assert(STATICARRAYLEN(buttons) == STATICARRAYLEN(locButtons));
783
Jun 1, 2007
Jun 1, 2007
784
rc = doPromptPage(title, htmltext, true, "promptynan", buttons, locButtons,
785
786
787
STATICARRAYLEN(buttons));
free(htmltext);
Jun 1, 2007
Jun 1, 2007
788
789
for (i = 0; i < STATICARRAYLEN(locButtons); i++)
free((void *) locButtons[i]);
790
791
792
793
794
return (MojoGuiYNAN) rc;
} // MojoGui_www_promptynan
Nov 24, 2007
Nov 24, 2007
795
796
static boolean MojoGui_www_start(const char *title,
const MojoGuiSplash *splash)
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
{
return true;
} // MojoGui_www_start
static void MojoGui_www_stop(void)
{
// no-op.
} // MojoGui_www_stop
static int MojoGui_www_readme(const char *name, const uint8 *data,
size_t datalen, boolean can_back,
boolean can_fwd)
{
char *text = NULL;
size_t len = 0, alloc = 0;
Jun 1, 2007
Jun 1, 2007
814
char *htmldata = htmlescape((const char *) data);
815
816
817
818
819
int i, rc;
int cancelbutton = -1;
int backbutton = -1;
int fwdbutton = -1;
int bcount = 0;
Jun 1, 2007
Jun 1, 2007
820
821
822
const char *buttons[4] = { NULL, NULL, NULL, NULL };
const char *locButtons[4] = { NULL, NULL, NULL, NULL };
assert(STATICARRAYLEN(buttons) == STATICARRAYLEN(locButtons));
823
824
cancelbutton = bcount++;
Jun 1, 2007
Jun 1, 2007
825
buttons[cancelbutton] = "cancel";
Mar 2, 2008
Mar 2, 2008
826
locButtons[cancelbutton] = xstrdup(_("Cancel"));
827
828
829
830
831
if (can_back)
{
backbutton = bcount++;
buttons[backbutton] = "back";
Mar 2, 2008
Mar 2, 2008
832
locButtons[backbutton] = xstrdup(_("Back"));
833
834
835
836
837
838
} // if
if (can_fwd)
{
fwdbutton = bcount++;
buttons[fwdbutton] = "next";
Mar 2, 2008
Mar 2, 2008
839
locButtons[fwdbutton] = xstrdup(_("Next"));
840
841
842
843
844
} // if
strAdd(&text, &len, &alloc, "<pre>\n%s\n</pre>", htmldata);
free(htmldata);
Jun 1, 2007
Jun 1, 2007
845
rc = doPromptPage(name, text, false, "readme", buttons, locButtons, bcount);
846
847
free(text);
Jun 1, 2007
Jun 1, 2007
848
849
for (i = 0; i < STATICARRAYLEN(locButtons); i++)
free((void *) locButtons[i]);
850
851
852
853
854
855
856
857
858
859
860
861
if (rc == backbutton)
return -1;
else if (rc == cancelbutton)
return 0;
return 1;
} // MojoGui_www_readme
static int MojoGui_www_options(MojoGuiSetupOptions *opts,
boolean can_back, boolean can_fwd)
{
Jan 15, 2008
Jan 15, 2008
862
863
// !!! FIXME: write me.
STUBBED("www options");
864
865
866
867
868
869
870
871
return 1;
} // MojoGui_www_options
static char *MojoGui_www_destination(const char **recommends, int recnum,
int *command, boolean can_back,
boolean can_fwd)
{
Jun 2, 2007
Jun 2, 2007
872
char *retval = NULL;
Mar 2, 2008
Mar 2, 2008
873
char *title = xstrdup(_("Destination"));
Jun 2, 2007
Jun 2, 2007
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
char *html = NULL;
size_t len = 0, alloc = 0;
boolean checked = true;
int cancelbutton = -1;
int backbutton = -1;
int fwdbutton = -1;
int bcount = 0;
int rc = 0;
int i = 0;
const char *buttons[4] = { NULL, NULL, NULL, NULL };
const char *locButtons[4] = { NULL, NULL, NULL, NULL };
assert(STATICARRAYLEN(buttons) == STATICARRAYLEN(locButtons));
cancelbutton = bcount++;
buttons[cancelbutton] = "cancel";
Mar 2, 2008
Mar 2, 2008
889
locButtons[cancelbutton] = xstrdup(_("Cancel"));
Jun 2, 2007
Jun 2, 2007
890
891
892
893
894
if (can_back)
{
backbutton = bcount++;
buttons[backbutton] = "back";
Mar 2, 2008
Mar 2, 2008
895
locButtons[backbutton] = xstrdup(_("Back"));
Jun 2, 2007
Jun 2, 2007
896
897
898
899
900
901
} // if
if (can_fwd)
{
fwdbutton = bcount++;
buttons[fwdbutton] = "next";
Mar 2, 2008
Mar 2, 2008
902
locButtons[fwdbutton] = xstrdup(_("Next"));
Jun 2, 2007
Jun 2, 2007
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
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
967
968
} // if
strAdd(&html, &len, &alloc,
"<form name='form_destination' method='get'>"
"<table>");
for (i = 0; i < recnum; i++)
{
strAdd(&html, &len, &alloc,
"<tr>"
"<td>"
"<input type='radio' name='dest' %s value='%s'>%s"
"</td>"
"</tr>",
((checked) ? "checked='true'" : ""), recommends[i], recommends[i]);
checked = false;
} // for
strAdd(&html, &len, &alloc,
"<tr>"
"<td>"
"<input type='radio' name='dest' %s value='*'>"
"<input type='text' name='customdest' value=''>"
"</td>"
"</tr>"
"</table>"
"</form>", ((checked) ? "checked='true'" : ""));
rc = doPromptPage(title, html, true, "destination",
buttons, locButtons, bcount);
free(title);
free(html);
for (i = 0; i < STATICARRAYLEN(locButtons); i++)
free((void *) locButtons[i]);
if (rc == backbutton)
*command = -1;
else if (rc == cancelbutton)
*command = 0;
else
{
const char *dest = NULL;
const char *customdest = NULL;
WebRequest *req = webRequest;
while (req != NULL)
{
const char *k = req->key;
const char *v = req->value;
if (strcmp(k, "dest") == 0)
dest = v;
else if (strcmp(k, "customdest") == 0)
customdest = v;
req = req->next;
} // while
if (dest != NULL)
{
if (strcmp(dest, "*") == 0)
dest = customdest;
} // if
if (dest == NULL)
*command = 0; // !!! FIXME: maybe loop with doPromptPage again.
else
{
Mar 2, 2008
Mar 2, 2008
969
retval = xstrdup(dest);
Jun 2, 2007
Jun 2, 2007
970
971
972
973
974
*command = 1;
} // else
} // else
return retval;
975
976
977
} // MojoGui_www_destination
Apr 25, 2009
Apr 25, 2009
978
979
980
981
static int MojoGui_www_productkey(const char *desc, const char *fmt,
char *buf, const int buflen,
boolean can_back, boolean can_fwd)
{
Mar 22, 2010
Mar 22, 2010
982
983
char *prompt = xstrdup(_("Please enter your product key"));
int retval = -1;
Apr 25, 2009
Apr 25, 2009
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
char *html = NULL;
size_t len = 0, alloc = 0;
int cancelbutton = -1;
int backbutton = -1;
int fwdbutton = -1;
int bcount = 0;
int rc = 0;
int i = 0;
const char *buttons[4] = { NULL, NULL, NULL, NULL };
const char *locButtons[4] = { NULL, NULL, NULL, NULL };
assert(STATICARRAYLEN(buttons) == STATICARRAYLEN(locButtons));
cancelbutton = bcount++;
buttons[cancelbutton] = "cancel";
locButtons[cancelbutton] = xstrdup(_("Cancel"));
if (can_back)