2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
39 * $Id: be_test.c,v 1.39 2005/02/08 22:58:00 cpressey Exp $
52 #include "dfui/lang.h"
53 #define _(String) gettext (String)
54 extern int _nl_msg_cat_cntr;
56 #define _(String) (String)
59 #include "dfui/dfui.h"
60 #include "dfui/dump.h"
61 #include "dfui/system.h"
62 #include "dfui/lang.h"
65 #include "aura/buffer.h"
67 static void usage(char **);
68 static void abort_backend(void);
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 *);
82 static char *show_lang_menu(struct dfui_connection *);
85 struct dfui_dataset *ds = NULL;
86 char mountpoint[16][256];
87 char capacity[16][256];
93 fprintf(stderr, _("Backend was aborted by DFUI_FE_ABORT message from frontend.\n"));
99 show_lang_menu(struct dfui_connection *c)
102 struct dfui_response *r;
105 fprintf(stderr, _("Language menu section entered.\n"));
107 f = dfui_form_create(
109 _("DFUI Test Suite - Language Choice"),
110 _("You can choose localisation from list."),
115 "a", "default", "default",
116 "Do not choose any language", "",
117 "a", "ru", "Russian",
118 "Russian KOI8-R", "",
122 fprintf(stderr, _("Presenting language menu to frontend.\n"));
123 if (!dfui_be_present(c, f, &r))
125 fprintf(stderr, _("Language menu presented.\n"));
127 if (dfui_response_get_action_id(r) == NULL) {
128 fprintf(stderr, _("Invalid response from frontend. Aborting.\n"));
131 id = aura_strdup(dfui_response_get_action_id(r));
133 fprintf(stderr, _("Language menu section finished (%s).\n"), id);
136 dfui_response_free(r);
138 if(strcmp(id, "default") == 0)
141 /* set keymap, scrnmap, fonts */
142 if(!set_lang_syscons(id))
146 if(!set_lang_envars(id))
156 show_streaming_progress_bar(struct dfui_connection *c)
158 struct dfui_progress *pr;
163 fprintf(stderr, _("Streaming progress bar section entered.\n"));
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.)"),
172 dfui_progress_set_streaming(pr, 1);
174 fprintf(stderr, _("Begin streaming progress bar.\n"));
175 if (!dfui_be_progress_begin(c, pr))
178 for (i = 1; i <= 20; i++) {
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))
188 fprintf(stderr, _("Streaming Progress bar was cancelled!\n"));
193 fprintf(stderr, _("End streaming progress bar.\n"));
194 if (!dfui_be_progress_end(c))
196 dfui_progress_free(pr);
198 fprintf(stderr, _("Streaming progress bar section finished.\n"));
203 show_typical_progress_bar(struct dfui_connection *c)
205 struct dfui_progress *pr;
209 fprintf(stderr, _("Typical progress bar section entered.\n"));
211 pr = dfui_progress_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.)"),
219 fprintf(stderr, _("Begin typical progress bar.\n"));
220 if (!dfui_be_progress_begin(c, pr))
223 for (i = 1; i <= 10; i++) {
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))
230 fprintf(stderr, _("Progress bar was cancelled!\n"));
235 fprintf(stderr, _("End typical progress bar.\n"));
236 if (!dfui_be_progress_end(c))
238 dfui_progress_free(pr);
240 fprintf(stderr, _("Typical progress bar section finished.\n"));
245 show_typical_form(struct dfui_connection *c)
248 struct dfui_response *r;
249 struct dfui_dataset *new_ds;
250 struct dfui_celldata *cd;
252 fprintf(stderr, _("Typical form section entered.\n"));
254 f = dfui_form_create(
257 _("This is a typical DFUI form. One field has a hint "
258 "that suggests that it can be rendered as a checkbox."),
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",
267 "f", "logged_in_as", _("Logged in as"),
268 _("A non-editable field"), "",
269 "p", "editable", "false",
271 "f", "password", "Password",
272 _("Enter your mock password here"), "",
273 "p", "obscured", "true",
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"),
280 "a", "ok", _("OK"), _("Accept these values"), "",
281 "a", "cancel",_("Cancel"), _("Cancel and return to previous form"), "",
285 dfui_form_dataset_add(f, dfui_dataset_dup(ds));
287 fprintf(stderr, _("Present typical form.\n"));
288 if (!dfui_be_present(c, f, &r))
290 fprintf(stderr, _("Typical form was presented.\n"));
292 new_ds = dfui_response_dataset_get_first(r);
294 cd = dfui_dataset_celldata_find(new_ds, "system_name");
296 fprintf(stderr, _("system_name WAS NOT PRESENT IN RESPONSE.\n"));
297 dfui_dataset_celldata_add(new_ds, "system_name", "NULL");
299 fprintf(stderr, "system_name = %s\n", dfui_celldata_get_value(cd));
302 cd = dfui_dataset_celldata_find(new_ds, "is_name_server");
304 fprintf(stderr, _("is_name_server WAS NOT PRESENT IN RESPONSE.\n"));
305 dfui_dataset_celldata_add(new_ds, "is_name_server", "N");
307 fprintf(stderr, "is_name_server = %s\n", dfui_celldata_get_value(cd));
310 cd = dfui_dataset_celldata_find(new_ds, "logged_in_as");
312 fprintf(stderr, _("logged_in_as WAS NOT PRESENT IN RESPONSE.\n"));
313 dfui_dataset_celldata_add(new_ds, "logged_in_as", "NULL");
315 fprintf(stderr, "logged_in_as = %s\n", dfui_celldata_get_value(cd));
318 cd = dfui_dataset_celldata_find(new_ds, "password");
320 fprintf(stderr, _("password WAS NOT PRESENT IN RESPONSE.\n"));
321 dfui_dataset_celldata_add(new_ds, "password", "");
323 fprintf(stderr, "password = %s\n", dfui_celldata_get_value(cd));
326 cd = dfui_dataset_celldata_find(new_ds, "machine_role");
328 fprintf(stderr, _("machine_role WAS NOT PRESENT IN RESPONSE.\n"));
329 dfui_dataset_celldata_add(new_ds, "machine_role", _("Server"));
331 fprintf(stderr, "machine_role = %s\n", dfui_celldata_get_value(cd));
334 fprintf(stderr, _("The action that selected was '%s'.\n"),
335 dfui_response_get_action_id(r));
337 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
338 dfui_dataset_free(ds);
339 ds = dfui_dataset_dup(new_ds);
342 fprintf(stderr, _("Typical form section finished.\n"));
344 dfui_response_free(r);
348 show_multi_form(struct dfui_connection *c, int extensible)
351 struct dfui_response *r;
352 struct dfui_dataset *slice_ds;
355 fprintf(stderr, _("Multi-dataset, %s form section entered.\n"),
356 extensible ? "extensible" : "non-extensible");
358 f = dfui_form_create(
360 _("Multi-Dataset Form"),
361 _("This is a form with not just one set of data, but several."),
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"), "",
369 "a", "ok", _("OK"), _("Accept these values"), "",
370 "a", "cancel", _("Cancel"), _("Cancel and return to previous form"), "",
374 dfui_form_set_multiple(f, 1);
375 dfui_form_set_extensible(f, extensible);
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);
386 fprintf(stderr, _("Present multi-dataset form.\n"));
387 if (!dfui_be_present(c, f, &r))
389 fprintf(stderr, _("Multi-dataset form was presented.\n"));
391 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
393 struct dfui_celldata *cd;
396 for (slice_ds = dfui_response_dataset_get_first(r);
398 slice_ds = dfui_dataset_get_next(slice_ds)) {
399 cd = dfui_dataset_celldata_find(slice_ds, "mountpoint");
401 fprintf(stderr, _("Response dataset #%d DID NOT INCLUDE mountpoint\n"), i);
403 value = dfui_celldata_get_value(cd);
404 fprintf(stderr, _("Response dataset #%d, mountpoint=`%s'\n"), i, value);
405 strncpy(mountpoint[i], value, 255);
407 cd = dfui_dataset_celldata_find(slice_ds, "capacity");
409 fprintf(stderr, _("Response dataset #%d DID NOT INCLUDE capacity\n"), i);
411 value = dfui_celldata_get_value(cd);
412 fprintf(stderr, _("Response dataset #%d, capacity=`%s'\n"), i, value);
413 strncpy(capacity[i], value, 255);
423 fprintf(stderr, _("Multi-dataset form section finished.\n"));
425 dfui_response_free(r);
429 show_typical_confirm(struct dfui_connection *c)
432 struct dfui_response *r;
434 fprintf(stderr, _("Typical confirmation section entered.\n"));
436 f = dfui_form_create(
439 _("Are you sure you wish to reset some settings or other "
440 "to the factory defaults?"),
442 "p", "role", "confirm",
444 "a", "yes", _("Yes"), _("Reset settings"), "",
445 "a", "no", _("No"), _("Keep current settings"), "",
449 if (!dfui_be_present(c, f, &r))
452 fprintf(stderr, _("Typical confirmation section finished.\n"));
454 dfui_response_free(r);
458 show_typical_alert(struct dfui_connection *c)
461 struct dfui_response *r;
463 fprintf(stderr, _("Typical alert section entered.\n"));
465 f = dfui_form_create(
467 _("Confirm Self-Injury"),
468 _("Warning: Pistol is loaded! "
469 "Are you SURE you wish to shoot yourself in the foot?"),
471 "p", "role", "alert",
473 "a", "yes", _("Yes"), _("Pull the trigger"), "",
474 "a", "no", _("No"), _("Make the smart choice"), "",
478 if (!dfui_be_present(c, f, &r))
481 fprintf(stderr, _("Typical alert section finished.\n"));
483 dfui_response_free(r);
487 show_typical_menu(struct dfui_connection *c)
490 struct dfui_response *r;
492 fprintf(stderr, _("Typical menu section entered.\n"));
494 f = dfui_form_create(
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."),
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",
516 if (!dfui_be_present(c, f, &r))
519 fprintf(stderr, _("Typical menu section finished; %s was selected.\n"),
520 dfui_response_get_action_id(r));
522 dfui_response_free(r);
526 show_scrolling_form(struct dfui_connection *c)
529 struct dfui_response *r;
530 struct aura_buffer *song;
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);
547 fprintf(stderr, _("Scrolling form section entered (%d bytes.)\n"), size);
549 f = dfui_form_create(
551 _("Sing Along Everybody!"),
552 aura_buffer_buf(song),
555 "a", "ok", _("OK"), _("No more beer"), "",
559 if (!dfui_be_present(c, f, &r))
562 fprintf(stderr, _("Scrolling form section finished.\n"));
564 dfui_response_free(r);
565 aura_buffer_free(song);
570 show_set_global_form(struct dfui_connection *c)
573 struct dfui_response *r;
574 struct dfui_dataset *gds;
576 fprintf(stderr, _("Set-global form section entered.\n"));
578 f = dfui_form_create(
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."),
585 "f", "name", _("Setting's Name"), "", "",
586 "f", "value", _("Setting's new Value"), "", "",
588 "a", "ok", _("OK"), _("Accept these values"), "",
589 "a", "cancel",_("Cancel"), _("Cancel and return to previous form"), "",
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);
598 fprintf(stderr, _("Present set-global form.\n"));
599 if (!dfui_be_present(c, f, &r))
601 fprintf(stderr, _("Set-global form was presented.\n"));
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 = "";
609 rds = dfui_response_dataset_get_first(r);
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);
616 fprintf(stderr, _("Setting global setting '%s'='%s' in frontend.\n"),
618 dfui_be_set_global_setting(c, name, value, &cancelled);
620 fprintf(stderr, _("Global setting was cancelled "
621 "(not supported or non-critical "
624 fprintf(stderr, _("Global setting was set.\n"));
627 fprintf(stderr, _("Cancel selected, no global settings changed.\n"));
630 fprintf(stderr, _("Set-global form section finished.\n"));
632 dfui_response_free(r);
636 show_unimplemented(struct dfui_connection *c)
639 struct dfui_response *r;
641 fprintf(stderr, _("Not yet implemented section entered.\n"));
643 f = dfui_form_create(
645 _("Not Yet Implemented"),
646 _("The feature you have chosen is not yet implemented."),
648 "p", "role", "alert",
650 "a", "ok", _("OK"), _("Proceed"), "",
654 if (!dfui_be_present(c, f, &r))
657 fprintf(stderr, _("Not yet implemented section finished.\n"));
659 dfui_response_free(r);
663 show_main_menu(struct dfui_connection *c)
666 struct dfui_response *r;
670 fprintf(stderr, _("Main menu section entered.\n"));
672 f = dfui_form_create(
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."),
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"), "",
709 fprintf(stderr, _("Presenting main menu to frontend.\n"));
710 if (!dfui_be_present(c, f, &r))
712 fprintf(stderr, _("Main menu presented.\n"));
714 id = dfui_response_get_action_id(r);
716 fprintf(stderr, _("Invalid response from frontend. Aborting.\n"));
720 if (strcmp(id, "exit") == 0)
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);
743 show_unimplemented(c);
745 fprintf(stderr, _("Main menu section finished.\n"));
748 dfui_response_free(r);
756 fprintf(stderr, _("Usage: %s [-r rendezvous] [-t caps|npipe|tcp]\n"), argv[0]);
761 * DFUI Test Suite backend.
764 main(int argc, char **argv)
766 struct dfui_connection *c;
769 char *rendezvous = NULL;
772 const char *lang = "default";
777 * XXX Get the transport from environment var, if available.
781 * Get command-line arguments.
783 while ((opt = getopt(argc, argv, "r:t:")) != -1) {
786 rendezvous = strdup(optarg);
789 transport = user_get_transport(optarg);
800 transport = user_get_transport("tcp");
802 if (rendezvous == NULL) {
803 if (transport == DFUI_TRANSPORT_TCP)
804 rendezvous = strdup("9999");
806 rendezvous = strdup("test");
810 setlocale (LC_ALL, "");
811 bindtextdomain (PACKAGE, LOCALEDIR);
812 textdomain (PACKAGE);
815 fprintf(stderr, _("Creating connection, transport = %d, rendezvous = %s.\n"),
816 transport, rendezvous);
818 if ((c = dfui_connection_new(transport, rendezvous)) == NULL) {
820 err(1, "dfui_connection_new()");
823 fprintf(stderr, _("Waiting for connection from frontend.\n"));
825 if (!dfui_be_start(c)) {
827 err(1, "dfui_be_start()");
830 fprintf(stderr, _("Creating global data.\n"));
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"));
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");
848 if (geteuid() != 0) {
849 fprintf(stderr, _("User UID is not 0, leave `default' language.\n"));
851 lang = show_lang_menu(c);
855 dfui_be_set_global_setting(c, "lang", lang, &cancelled);
857 /* XXX if (!cancelled) ... ? */
859 /* let gettext know about changes */
864 fprintf(stderr, _("Entering main menu loop.\n"));
867 done = show_main_menu(c);
869 fprintf(stderr, _("Main menu loop finished, freeing global data.\n"));
871 dfui_dataset_free(ds);
873 fprintf(stderr, _("Disconnecting from frontend.\n"));
877 fprintf(stderr, _("Exiting.\n"));