acpi: Select proper one shot timer based on CPUs' C3 state.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / test / be_test.c
1 /*
2  * Copyright (c)2004 Cat's Eye Technologies.  All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * 
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  * 
16  *   Neither the name of Cat's Eye Technologies nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission. 
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE. 
32  */
33
34 /*
35  * This code was derived in part from:
36  * $_DragonFly: src/test/caps/client.c,v 1.3 2004/03/31 20:27:34 dillon Exp $
37  * and is therefore also subject to the license conditions on that file.
38  *
39  * $Id: be_test.c,v 1.39 2005/02/08 22:58:00 cpressey Exp $
40  */
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #ifdef ENABLE_NLS
50 #include <libintl.h>
51 #include <locale.h>
52 #include "dfui/lang.h"
53 #define _(String) gettext (String)
54 extern int _nl_msg_cat_cntr;
55 #else
56 #define _(String) (String)
57 #endif
58
59 #include "dfui/dfui.h"
60 #include "dfui/dump.h"
61 #include "dfui/system.h"
62 #include "dfui/lang.h"
63
64 #include "aura/mem.h"
65 #include "aura/buffer.h"
66
67 static void      usage(char **);
68 static void      abort_backend(void);
69
70 static int       show_streaming_progress_bar(struct dfui_connection *);
71 static int       show_typical_progress_bar(struct dfui_connection *);
72 static void      show_typical_form(struct dfui_connection *);
73 static void      show_multi_form(struct dfui_connection *, int);
74 static void      show_typical_confirm(struct dfui_connection *);
75 static void      show_typical_alert(struct dfui_connection *);
76 static void      show_typical_menu(struct dfui_connection *);
77 static void      show_scrolling_form(struct dfui_connection *);
78 static void      show_set_global_form(struct dfui_connection *);
79 static void      show_unimplemented(struct dfui_connection *);
80 static int       show_main_menu(struct dfui_connection *);
81 #ifdef ENABLE_NLS
82 static char     *show_lang_menu(struct dfui_connection *);
83 #endif
84
85 struct dfui_dataset *ds = NULL;
86 char mountpoint[16][256];
87 char capacity[16][256];
88 int rows;
89
90 static void
91 abort_backend(void)
92 {
93         fprintf(stderr, _("Backend was aborted by DFUI_FE_ABORT message from frontend.\n"));
94         exit(1);
95 }
96
97 #ifdef ENABLE_NLS
98 static char *
99 show_lang_menu(struct dfui_connection *c)
100 {
101         struct dfui_form *f;
102         struct dfui_response *r;
103         char *id;
104
105         fprintf(stderr, _("Language menu section entered.\n"));
106
107         f = dfui_form_create(
108             "main_menu",
109             _("DFUI Test Suite - Language Choice"),
110             _("You can choose localisation from list."),
111             "",
112
113             "p", "role", "menu",
114
115             "a", "default", "default",
116             "Do not choose any language", "",
117             "a", "ru", "Russian",
118             "Russian KOI8-R", "",
119             NULL
120         );
121
122         fprintf(stderr, _("Presenting language menu to frontend.\n"));
123         if (!dfui_be_present(c, f, &r))
124                 abort_backend();
125         fprintf(stderr, _("Language menu presented.\n"));
126
127         if (dfui_response_get_action_id(r) == NULL) {
128                 fprintf(stderr, _("Invalid response from frontend. Aborting.\n"));
129                 abort_backend();
130         }
131         id = aura_strdup(dfui_response_get_action_id(r));
132
133         fprintf(stderr, _("Language menu section finished (%s).\n"), id);
134
135         dfui_form_free(f);
136         dfui_response_free(r);
137
138         if(strcmp(id, "default") == 0)
139                 return(id);
140
141         /* set keymap, scrnmap, fonts */
142         if(!set_lang_syscons(id))
143                 return(NULL);
144
145         /* set envars */
146         if(!set_lang_envars(id))
147                 return(NULL);
148
149         return(id);
150 }
151 #endif
152
153 /*** DRIVERS ***/
154
155 static int
156 show_streaming_progress_bar(struct dfui_connection *c)
157 {
158         struct dfui_progress *pr;
159         char msg_text[256];
160         int cancelled = 0;
161         int i;
162         
163         fprintf(stderr, _("Streaming progress bar section entered.\n"));
164
165         pr = dfui_progress_new(dfui_info_new(
166             _("Streaming Progress Indicator"),
167             _("This is a streaming progress indicator.  You may cause it "
168             "to be aborted (which should be acknowledged after a brief delay.)"),
169             ""),
170             0);
171
172         dfui_progress_set_streaming(pr, 1);
173
174         fprintf(stderr, _("Begin streaming progress bar.\n"));
175         if (!dfui_be_progress_begin(c, pr))
176                 abort_backend();
177
178         for (i = 1; i <= 20; i++) {
179                 sleep(1);
180                 dfui_progress_set_amount(pr, i);
181                 fprintf(stderr, _("Update streaming progress bar.\n"));
182                 dfui_info_set_short_desc(dfui_progress_get_info(pr), _("Update streaming progress bar."));
183                 snprintf(msg_text, 256, _("This is line #%d.\n"), i);
184                 dfui_progress_set_msg_line(pr, msg_text);
185                 if (!dfui_be_progress_update(c, pr, &cancelled))
186                         abort_backend();
187                 if (cancelled) {
188                         fprintf(stderr, _("Streaming Progress bar was cancelled!\n"));
189                         break;
190                 }
191         }
192
193         fprintf(stderr, _("End streaming progress bar.\n"));
194         if (!dfui_be_progress_end(c))
195                 abort_backend();
196         dfui_progress_free(pr);
197
198         fprintf(stderr, _("Streaming progress bar section finished.\n"));
199         return(!cancelled);
200 }
201
202 static int
203 show_typical_progress_bar(struct dfui_connection *c)
204 {
205         struct dfui_progress *pr;
206         int i;
207         int cancelled = 0;
208
209         fprintf(stderr, _("Typical progress bar section entered.\n"));
210
211         pr = dfui_progress_new(
212                 dfui_info_new(
213                         _("Typical Progress Indicator"),
214                         _("This is a typical progress indicator.  You may cause it "
215                         "to be aborted (which should be acknowledged after a brief delay.)"),
216                         ""),
217                 0);
218
219         fprintf(stderr, _("Begin typical progress bar.\n"));
220         if (!dfui_be_progress_begin(c, pr))
221                 abort_backend();
222         
223         for (i = 1; i <= 10; i++) {
224                 sleep(1);
225                 dfui_progress_set_amount(pr, i * 10);
226                 fprintf(stderr, _("Update typical progress bar.\n"));
227                 if (!dfui_be_progress_update(c, pr, &cancelled))
228                         abort_backend();
229                 if (cancelled) {
230                         fprintf(stderr, _("Progress bar was cancelled!\n"));
231                         break;
232                 }
233         }
234
235         fprintf(stderr, _("End typical progress bar.\n"));
236         if (!dfui_be_progress_end(c))
237                 abort_backend();
238         dfui_progress_free(pr);
239
240         fprintf(stderr, _("Typical progress bar section finished.\n"));
241         return(!cancelled);
242 }
243
244 static void
245 show_typical_form(struct dfui_connection *c)
246 {
247         struct dfui_form *f;
248         struct dfui_response *r;
249         struct dfui_dataset *new_ds;
250         struct dfui_celldata *cd;
251
252         fprintf(stderr, _("Typical form section entered.\n"));
253
254         f = dfui_form_create(
255             "typical_form",
256             _("Typical Form"),
257             _("This is a typical DFUI form.  One field has a hint "
258             "that suggests that it can be rendered as a checkbox."),
259             "",
260
261             "f", "system_name", _("System Name"),
262             _("Enter the name of this system"), "",
263             "f", "is_name_server", _("Name Server?"),
264             _("Will this system be used as a DNS server?"), "",
265             "p", "control", "checkbox",
266
267             "f", "logged_in_as", _("Logged in as"),
268             _("A non-editable field"), "",
269             "p", "editable", "false",
270
271             "f", "password", "Password",
272             _("Enter your mock password here"), "",
273             "p", "obscured", "true",
274
275             "f", "machine_role", _("Machine Role"),
276             _("Select a general role for this machine to play"), "",
277             "p", "editable", "false",
278             "o", _("Workstation"), "o", _("Server"), "o", _("Laptop"),
279
280             "a", "ok", _("OK"), _("Accept these values"), "",
281             "a", "cancel",_("Cancel"), _("Cancel and return to previous form"), "",
282             NULL
283         );
284
285         dfui_form_dataset_add(f, dfui_dataset_dup(ds));
286
287         fprintf(stderr, _("Present typical form.\n"));
288         if (!dfui_be_present(c, f, &r))
289                 abort_backend();
290         fprintf(stderr, _("Typical form was presented.\n"));
291
292         new_ds = dfui_response_dataset_get_first(r);
293
294         cd = dfui_dataset_celldata_find(new_ds, "system_name");
295         if (cd == NULL) {
296                 fprintf(stderr, _("system_name WAS NOT PRESENT IN RESPONSE.\n"));
297                 dfui_dataset_celldata_add(new_ds, "system_name", "NULL");
298         } else {
299                 fprintf(stderr, "system_name = %s\n", dfui_celldata_get_value(cd));
300         }
301
302         cd = dfui_dataset_celldata_find(new_ds, "is_name_server");
303         if (cd == NULL) {
304                 fprintf(stderr, _("is_name_server WAS NOT PRESENT IN RESPONSE.\n"));
305                 dfui_dataset_celldata_add(new_ds, "is_name_server", "N");
306         } else {
307                 fprintf(stderr, "is_name_server = %s\n", dfui_celldata_get_value(cd));
308         }
309
310         cd = dfui_dataset_celldata_find(new_ds, "logged_in_as");
311         if (cd == NULL) {
312                 fprintf(stderr, _("logged_in_as WAS NOT PRESENT IN RESPONSE.\n"));
313                 dfui_dataset_celldata_add(new_ds, "logged_in_as", "NULL");
314         } else {
315                 fprintf(stderr, "logged_in_as = %s\n", dfui_celldata_get_value(cd));
316         }
317
318         cd = dfui_dataset_celldata_find(new_ds, "password");
319         if (cd == NULL) {
320                 fprintf(stderr, _("password WAS NOT PRESENT IN RESPONSE.\n"));
321                 dfui_dataset_celldata_add(new_ds, "password", "");
322         } else {
323                 fprintf(stderr, "password = %s\n", dfui_celldata_get_value(cd));
324         }
325
326         cd = dfui_dataset_celldata_find(new_ds, "machine_role");
327         if (cd == NULL) {
328                 fprintf(stderr, _("machine_role WAS NOT PRESENT IN RESPONSE.\n"));
329                 dfui_dataset_celldata_add(new_ds, "machine_role", _("Server"));
330         } else {
331                 fprintf(stderr, "machine_role = %s\n", dfui_celldata_get_value(cd));
332         }
333
334         fprintf(stderr, _("The action that selected was '%s'.\n"), 
335             dfui_response_get_action_id(r));
336
337         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
338                 dfui_dataset_free(ds);
339                 ds = dfui_dataset_dup(new_ds);
340         }
341
342         fprintf(stderr, _("Typical form section finished.\n"));
343         dfui_form_free(f);
344         dfui_response_free(r);
345 }
346
347 static void
348 show_multi_form(struct dfui_connection *c, int extensible)
349 {
350         struct dfui_form *f;
351         struct dfui_response *r;
352         struct dfui_dataset *slice_ds;
353         int i;
354
355         fprintf(stderr, _("Multi-dataset, %s form section entered.\n"),
356             extensible ? "extensible" : "non-extensible");
357
358         f = dfui_form_create(
359             "multi_form",
360             _("Multi-Dataset Form"),
361             _("This is a form with not just one set of data, but several."),
362             "",
363
364             "f", "mountpoint", _("Mountpoint"),
365             _("Where this filesystem will be logically located"), "",
366             "f", "capacity", _("Capacity"),
367             _("How many megabytes of data this slice will hold"), "",
368
369             "a", "ok", _("OK"), _("Accept these values"), "",
370             "a", "cancel", _("Cancel"), _("Cancel and return to previous form"), "",
371             NULL
372         );
373
374         dfui_form_set_multiple(f, 1);
375         dfui_form_set_extensible(f, extensible);
376
377         for (i = 0; i < rows; i++) {
378                 slice_ds = dfui_dataset_new();
379                 dfui_dataset_celldata_add(slice_ds,
380                     "mountpoint", mountpoint[i]);
381                 dfui_dataset_celldata_add(slice_ds,
382                     "capacity", capacity[i]);
383                 dfui_form_dataset_add(f, slice_ds);
384         }
385
386         fprintf(stderr, _("Present multi-dataset form.\n"));
387         if (!dfui_be_present(c, f, &r))
388                 abort_backend();
389         fprintf(stderr, _("Multi-dataset form was presented.\n"));
390
391         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
392                 const char *value;
393                 struct dfui_celldata *cd;
394
395                 i = 0;
396                 for (slice_ds = dfui_response_dataset_get_first(r);
397                      slice_ds != NULL;
398                      slice_ds = dfui_dataset_get_next(slice_ds)) {
399                         cd = dfui_dataset_celldata_find(slice_ds, "mountpoint");
400                         if (cd == NULL) {
401                                 fprintf(stderr, _("Response dataset #%d DID NOT INCLUDE mountpoint\n"), i);
402                         } else {
403                                 value = dfui_celldata_get_value(cd);
404                                 fprintf(stderr, _("Response dataset #%d, mountpoint=`%s'\n"), i, value);
405                                 strncpy(mountpoint[i], value, 255);
406                         }
407                         cd = dfui_dataset_celldata_find(slice_ds, "capacity");
408                         if (cd == NULL) {
409                                 fprintf(stderr, _("Response dataset #%d DID NOT INCLUDE capacity\n"), i);
410                         } else {
411                                 value = dfui_celldata_get_value(cd);
412                                 fprintf(stderr, _("Response dataset #%d, capacity=`%s'\n"), i, value);
413                                 strncpy(capacity[i], value, 255);
414                         }
415                         i++;
416                         if (i == 16)
417                                 break;
418                 }
419
420                 rows = i;
421         }
422
423         fprintf(stderr, _("Multi-dataset form section finished.\n"));
424         dfui_form_free(f);
425         dfui_response_free(r);
426 }
427
428 static void
429 show_typical_confirm(struct dfui_connection *c)
430 {
431         struct dfui_form *f;
432         struct dfui_response *r;
433
434         fprintf(stderr, _("Typical confirmation section entered.\n"));
435
436         f = dfui_form_create(
437             "typical_confirm",
438             _("Confirm Reset"),
439             _("Are you sure you wish to reset some settings or other "
440             "to the factory defaults?"),
441             "",
442             "p", "role", "confirm",
443
444             "a", "yes", _("Yes"), _("Reset settings"), "",
445             "a", "no", _("No"), _("Keep current settings"), "",
446                 NULL
447         );
448
449         if (!dfui_be_present(c, f, &r))
450                 abort_backend();
451
452         fprintf(stderr, _("Typical confirmation section finished.\n"));
453         dfui_form_free(f);
454         dfui_response_free(r);
455 }
456
457 static void
458 show_typical_alert(struct dfui_connection *c)
459 {
460         struct dfui_form *f;
461         struct dfui_response *r;
462
463         fprintf(stderr, _("Typical alert section entered.\n"));
464
465         f = dfui_form_create(
466             "typical_alert",
467             _("Confirm Self-Injury"),
468             _("Warning: Pistol is loaded! "
469             "Are you SURE you wish to shoot yourself in the foot?"),
470             "",
471             "p", "role", "alert",
472
473             "a", "yes", _("Yes"), _("Pull the trigger"), "",
474             "a", "no", _("No"), _("Make the smart choice"), "",
475             NULL
476         );
477
478         if (!dfui_be_present(c, f, &r))
479                 abort_backend();
480
481         fprintf(stderr, _("Typical alert section finished.\n"));
482         dfui_form_free(f);
483         dfui_response_free(r);
484 }
485
486 static void
487 show_typical_menu(struct dfui_connection *c)
488 {
489         struct dfui_form *f;
490         struct dfui_response *r;
491
492         fprintf(stderr, _("Typical menu section entered.\n"));
493
494         f = dfui_form_create(
495             "typical_menu",
496             _("Typical Menu"),
497             _("Pick one."),
498             _("Note that each of these menu items is associated with an "
499               "'accelerator', or shortcut key.  Pressing that key should "
500               "automatically select the associated item."),
501
502             "p", "role", "menu",
503
504             "a", "beef", _("Beef"),       "Beef comes from cows", "",
505             "p", "accelerator", "B",
506             "a", "chicken", _("Chicken"), "Chickens come from coops", "",
507             "p", "accelerator", "C",
508             "a", "tofu", _("Tofu"),       "Tofu comes from beans", "",
509             "p", "accelerator", "T",
510             "a", "cancel", _("Cancel"),   "Cancel this selection", "",
511             "p", "accelerator", "ESC",
512
513             NULL
514         );
515
516         if (!dfui_be_present(c, f, &r))
517                 abort_backend();
518
519         fprintf(stderr, _("Typical menu section finished; %s was selected.\n"),
520             dfui_response_get_action_id(r));
521         dfui_form_free(f);
522         dfui_response_free(r);
523 }
524
525 static void
526 show_scrolling_form(struct dfui_connection *c)
527 {
528         struct dfui_form *f;
529         struct dfui_response *r;
530         struct aura_buffer *song;
531         char *one_verse;
532         int bottles;
533         int size = 0;
534
535         song = aura_buffer_new(16384);
536         one_verse = malloc(2048);
537         for (bottles = 99; bottles > 0; bottles--) {
538                 sprintf(one_verse, _("%d bottles of beer on the wall,\n"
539                     "%d bottles of beer,\n"
540                     "Take one down, pass it around,\n"
541                     "%d bottles of beer on the wall.\n\n"),
542                     bottles, bottles, bottles - 1);
543                 size += strlen(one_verse);
544                 aura_buffer_cat(song, one_verse);
545         }
546
547         fprintf(stderr, _("Scrolling form section entered (%d bytes.)\n"), size);
548
549         f = dfui_form_create(
550             "beer",
551             _("Sing Along Everybody!"),
552             aura_buffer_buf(song),
553             "",
554
555             "a", "ok", _("OK"), _("No more beer"), "",
556             NULL
557         );
558
559         if (!dfui_be_present(c, f, &r))
560                 abort_backend();
561
562         fprintf(stderr, _("Scrolling form section finished.\n"));
563         dfui_form_free(f);
564         dfui_response_free(r);
565         aura_buffer_free(song);
566         free(one_verse);
567 }
568
569 static void
570 show_set_global_form(struct dfui_connection *c)
571 {
572         struct dfui_form *f;
573         struct dfui_response *r;
574         struct dfui_dataset *gds;
575
576         fprintf(stderr, _("Set-global form section entered.\n"));
577
578         f = dfui_form_create(
579             "set_global_form",
580             _("Set-Global Form"),
581             _("Please enter the name and new value of the global "
582               "setting of the frontend that you wish to change."),
583             "",
584
585             "f", "name", _("Setting's Name"), "", "",
586             "f", "value", _("Setting's new Value"), "", "",
587
588             "a", "ok", _("OK"), _("Accept these values"), "",
589             "a", "cancel",_("Cancel"), _("Cancel and return to previous form"), "",
590             NULL
591         );
592
593         gds = dfui_dataset_new();
594         dfui_dataset_celldata_add(gds, "name", "lang");
595         dfui_dataset_celldata_add(gds, "value", "ru");
596         dfui_form_dataset_add(f, gds);
597
598         fprintf(stderr, _("Present set-global form.\n"));
599         if (!dfui_be_present(c, f, &r))
600                 abort_backend();
601         fprintf(stderr, _("Set-global form was presented.\n"));
602
603         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
604                 struct dfui_celldata *cd;
605                 struct dfui_dataset *rds;
606                 const char *name = "", *value = "";
607                 int cancelled = 0;
608
609                 rds = dfui_response_dataset_get_first(r);
610
611                 if ((cd = dfui_dataset_celldata_find(rds, "name")) != NULL)
612                         name = dfui_celldata_get_value(cd);
613                 if ((cd = dfui_dataset_celldata_find(rds, "value")) != NULL)
614                         value = dfui_celldata_get_value(cd);
615
616                 fprintf(stderr, _("Setting global setting '%s'='%s' in frontend.\n"),
617                     name, value);
618                 dfui_be_set_global_setting(c, name, value, &cancelled);
619                 if (cancelled) {
620                         fprintf(stderr, _("Global setting was cancelled "
621                                           "(not supported or non-critical "
622                                           "failure.)\n"));
623                 } else {
624                         fprintf(stderr, _("Global setting was set.\n"));
625                 }
626         } else {
627                 fprintf(stderr, _("Cancel selected, no global settings changed.\n"));
628         }
629
630         fprintf(stderr, _("Set-global form section finished.\n"));
631         dfui_form_free(f);
632         dfui_response_free(r);
633 }
634
635 static void
636 show_unimplemented(struct dfui_connection *c)
637 {
638         struct dfui_form *f;
639         struct dfui_response *r;
640
641         fprintf(stderr, _("Not yet implemented section entered.\n"));
642
643         f = dfui_form_create(
644             "nyi",
645             _("Not Yet Implemented"),
646             _("The feature you have chosen is not yet implemented."),
647             "",
648             "p", "role", "alert",
649
650             "a", "ok", _("OK"), _("Proceed"), "",
651             NULL
652         );
653
654         if (!dfui_be_present(c, f, &r))
655                 abort_backend();
656
657         fprintf(stderr, _("Not yet implemented section finished.\n"));
658         dfui_form_free(f);
659         dfui_response_free(r);
660 }
661
662 static int
663 show_main_menu(struct dfui_connection *c)
664 {
665         struct dfui_form *f;
666         struct dfui_response *r;
667         const char *id;
668         int done = 0;
669
670         fprintf(stderr, _("Main menu section entered.\n"));
671
672         f = dfui_form_create(
673             "main_menu",
674             _("DFUI Test Suite - Main Menu"),
675             _("This backend is a test suite for the DFUI abstract user interface "
676             "layer.  It exercises most of the features of the DFUI protocol. "
677             "It also demonstrates how a backend program, containing "
678             "only the application logic, can send an abstract representation "
679             "of a form to the frontend, which can interpret it any way it "
680             "wishes, and send a response back to the backend."),
681             "",
682
683             "p", "role", "menu",
684
685             "a", "menu", _("Typical Menu"),
686             _("Display a typical menu"), "",
687             "a", "confirm", _("Typical Confirmation"),
688             _("Display a typical confirmation dialog box"), "",
689             "a", "alert", _("Typical Alert"),
690             _("Display a typical alert dialog box"), "",
691             "a", "form", _("Typical Form"),
692             _("Display a typical form"), "",
693             "a", "multi", _("Multi-Dataset Form"),
694             _("Display a form with more than one dataset"), "",
695             "a", "extensible", _("Extensible Form"),
696             _("Display a form with variable number of datasets"), "",
697             "a", "progress", _("Progress Bar"),
698             _("Display a typical progress bar"), "",
699             "a", "streaming_progress_bar", _("Streaming Progress Bar"),
700             _("Display a streaming progress bar"), "",
701             "a", "scroll", _("Scrolling Form"),
702             _("Display a form too large to be fully viewed"), "",
703             "a", "set_global", _("Set Frontend Settings"),
704             _("Change global properties of the frontend, from the backend"), "",
705             "a", "exit", _("Exit"), _("Leave this Test Suite"), "",
706             NULL
707         );
708
709         fprintf(stderr, _("Presenting main menu to frontend.\n"));
710         if (!dfui_be_present(c, f, &r))
711                 abort_backend();
712         fprintf(stderr, _("Main menu presented.\n"));
713
714         id = dfui_response_get_action_id(r);
715         if (id == NULL) {
716                 fprintf(stderr, _("Invalid response from frontend. Aborting.\n"));
717                 abort_backend();
718         }
719         
720         if (strcmp(id, "exit") == 0)
721                 done = 1;
722         else if (strcmp(id, "progress") == 0)
723                 show_typical_progress_bar(c);
724         else if (strcmp(id, "form") == 0)
725                 show_typical_form(c);
726         else if (strcmp(id, "multi") == 0)
727                 show_multi_form(c, 0);
728         else if (strcmp(id, "extensible") == 0)
729                 show_multi_form(c, 1);
730         else if (strcmp(id, "confirm") == 0)
731                 show_typical_confirm(c);
732         else if (strcmp(id, "alert") == 0)
733                 show_typical_alert(c);
734         else if (strcmp(id, "menu") == 0)
735                 show_typical_menu(c);
736         else if (strcmp(id, "scroll") == 0)
737                 show_scrolling_form(c);
738         else if (strcmp(id, "streaming_progress_bar") == 0)
739                 show_streaming_progress_bar(c);
740         else if (strcmp(id, "set_global") == 0)
741                 show_set_global_form(c);
742         else
743                 show_unimplemented(c);
744
745         fprintf(stderr, _("Main menu section finished.\n"));
746
747         dfui_form_free(f);
748         dfui_response_free(r);
749         
750         return(done);
751 }
752
753 static void
754 usage(char **argv)
755 {
756         fprintf(stderr, _("Usage: %s [-r rendezvous] [-t caps|npipe|tcp]\n"), argv[0]);
757         exit(1);
758 }
759
760 /*
761  * DFUI Test Suite backend.
762  */
763 int
764 main(int argc, char **argv)
765 {
766         struct dfui_connection *c;
767         int done = 0;
768         int opt;
769         char *rendezvous = NULL;
770         int transport = 0;
771 #ifdef ENABLE_NLS
772         const char *lang = "default";
773         int cancelled = 0;
774 #endif
775
776         /*
777          * XXX Get the transport from environment var, if available.
778          */
779
780         /*
781          * Get command-line arguments.
782          */
783         while ((opt = getopt(argc, argv, "r:t:")) != -1) {
784                 switch(opt) {
785                 case 'r':
786                         rendezvous = strdup(optarg);
787                         break;
788                 case 't':
789                         transport = user_get_transport(optarg);
790                         break;
791                 case '?':
792                 default:
793                         usage(argv);
794                 }
795         }
796         argc -= optind;
797         argv += optind;
798
799         if (!transport)
800                 transport = user_get_transport("tcp");
801
802         if (rendezvous == NULL) {
803                 if (transport == DFUI_TRANSPORT_TCP)
804                         rendezvous = strdup("9999");
805                 else
806                         rendezvous = strdup("test");
807         }
808
809 #ifdef ENABLE_NLS
810         setlocale (LC_ALL, "");
811         bindtextdomain (PACKAGE, LOCALEDIR);
812         textdomain (PACKAGE);
813 #endif
814
815         fprintf(stderr, _("Creating connection, transport = %d, rendezvous = %s.\n"),
816                 transport, rendezvous);
817
818         if ((c = dfui_connection_new(transport, rendezvous)) == NULL) {
819                 free(rendezvous);
820                 err(1, "dfui_connection_new()");
821         }
822
823         fprintf(stderr, _("Waiting for connection from frontend.\n"));
824
825         if (!dfui_be_start(c)) {
826                 free(rendezvous);
827                 err(1, "dfui_be_start()");
828         }
829
830         fprintf(stderr, _("Creating global data.\n"));
831
832         ds = dfui_dataset_new();
833         dfui_dataset_celldata_add(ds, "system_name", "typhoon");
834         dfui_dataset_celldata_add(ds, "is_name_server", "N");
835         dfui_dataset_celldata_add(ds, "logged_in_as", "uucp");
836         dfui_dataset_celldata_add(ds, "password", "uucp");
837         dfui_dataset_celldata_add(ds, "machine_role", _("Workstation"));
838
839         strcpy(mountpoint[0], "/");
840         strcpy(capacity[0], "200M");
841         strcpy(mountpoint[1], "swap");
842         strcpy(capacity[1], "256M");
843         strcpy(mountpoint[2], "/usr");
844         strcpy(capacity[2], "4.5G");
845         rows = 3;
846
847 #ifdef ENABLE_NLS
848         if (geteuid() != 0) {
849                 fprintf(stderr, _("User UID is not 0, leave `default' language.\n"));
850         } else {
851                 lang = show_lang_menu(c);
852         }
853
854         if (lang != NULL) {
855                 dfui_be_set_global_setting(c, "lang", lang, &cancelled);
856
857                 /* XXX if (!cancelled) ... ? */
858
859                 /* let gettext know about changes */
860                 ++_nl_msg_cat_cntr;
861         }
862 #endif
863
864         fprintf(stderr, _("Entering main menu loop.\n"));
865
866         while (!done)
867                 done = show_main_menu(c);
868
869         fprintf(stderr, _("Main menu loop finished, freeing global data.\n"));
870
871         dfui_dataset_free(ds);
872         
873         fprintf(stderr, _("Disconnecting from frontend.\n"));
874
875         dfui_be_stop(c);
876
877         fprintf(stderr, _("Exiting.\n"));
878
879         free(rendezvous);
880         exit(0);
881 }