Merge commit 'origin/vendor/PAM_PASSWDQC'
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / frontends / test / fe_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  * fe_test.c
36  * $Id: fe_test.c,v 1.11 2005/04/05 20:39:22 cpressey Exp $
37  * This code was derived in part from:
38  * $_DragonFly: src/test/caps/server.c,v 1.4 2004/03/06 22:15:00 dillon Exp $
39  * and is therefore also subject to the license conditions on that file.
40  */
41
42 #include <signal.h>
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "dfui/dfui.h"
47 #include "dfui/dump.h"
48
49 volatile sig_atomic_t caught_signal;
50
51 static void
52 signal_handler(int signo)
53 {
54         caught_signal = signo;
55 }
56
57 #define BAR "----------------------------------------------------------------------"
58
59 static void
60 display_form(struct dfui_form *f)
61 {
62         struct dfui_field *fi;
63         struct dfui_action *a;
64
65         printf("," BAR "\n");
66         for (fi = dfui_form_field_get_first(f); fi != NULL;
67              fi = dfui_field_get_next(fi)) {
68                 printf("| %30s %c[ %-30s ]\n",
69                     dfui_info_get_name(dfui_field_get_info(fi)),
70                     dfui_field_property_is(fi, "control", "checkbox") ? '+' : ' ',
71                     "(value)"
72                 );
73         }
74         printf("+" BAR "\n");
75         printf("| Actions: ");
76         for (a = dfui_form_action_get_first(f); a != NULL;
77              a = dfui_action_get_next(a)) {
78                 printf("[ %s ]  ",
79                     dfui_info_get_name(dfui_action_get_info(a))
80                 );
81         }
82         printf("\n");
83         printf("`" BAR "\n");
84 }
85
86 static void
87 present_form(struct dfui_form *f, char *action)
88 {
89         int done = 0;
90         struct dfui_action *a;
91         
92         display_form(f);
93         while (!done) {
94                 printf("Select an action> ");
95                 fgets(action, 80, stdin);
96                 action[strlen(action) - 1] = '\0';
97                 if ((a = dfui_form_action_find(f, action)) != NULL) {
98                         done = 1;
99                 } else {
100                         printf("Invalid option.\n");
101                 }
102         }
103 }
104
105 static void
106 show_progress(struct dfui_progress *pr)
107 {
108         printf("  %d%c done...\n", dfui_progress_get_amount(pr), '%');
109 }
110
111 static void
112 abort_frontend(struct dfui_connection *c)
113 {
114         printf("*** ABORTING.\n");
115         dfui_fe_abort(c);
116         exit(1);
117 }
118
119 /*
120  * dfui frontend example test thing
121  */
122 int
123 main(int argc __unused, char **argv __unused)
124 {
125         struct dfui_connection *c;
126         struct dfui_form *f;
127         struct dfui_response *r;
128         struct dfui_progress *pr;
129         struct dfui_property *gp;
130         void *payload;
131         char action[80];
132
133         int done = 0;
134         char msgtype;
135
136         c = dfui_connection_new(DFUI_TRANSPORT_TCP, "9999");
137
138         printf("Connecting to backend.\n");
139         dfui_fe_connect(c);
140         signal(SIGINT, signal_handler);
141
142         while (!done) {
143                 dfui_fe_receive(c, &msgtype, &payload);
144                 switch (msgtype) {
145                 case DFUI_BE_MSG_PRESENT:
146                         printf("Got a form from the backend.\n");
147                         f = (struct dfui_form *)payload;
148                         present_form(f, action);
149                         if (caught_signal) abort_frontend(c);
150                         r = dfui_response_new(dfui_form_get_id(f), action);
151                         printf("Sending our response to the backend.\n");
152                         dfui_fe_submit(c, r);
153                         dfui_form_free(f);
154                         dfui_response_free(r);
155                         break;
156                 case DFUI_BE_MSG_PROG_BEGIN:
157                         printf("Got a progress bar begin from the backend.\n");
158                         pr = (struct dfui_progress *)payload;
159                         caught_signal = 0;
160                         show_progress(pr);
161                         dfui_fe_progress_continue(c);
162                         dfui_progress_free(pr);
163                         break;
164                 case DFUI_BE_MSG_PROG_UPDATE:
165                         printf("Got a progress bar update from the backend.\n");
166                         pr = (struct dfui_progress *)payload;
167                         show_progress(pr);
168                         dfui_progress_free(pr);
169                         if (caught_signal) {
170                                 printf("* Cancelling ...\n");
171                                 dfui_fe_progress_cancel(c);
172                                 caught_signal = 0;
173                         } else {
174                                 dfui_fe_progress_continue(c);
175                         }
176                         break;
177                 case DFUI_BE_MSG_PROG_END:
178                         printf("Got a progress bar end from the backend.\n");
179                         dfui_fe_progress_continue(c);
180                         break;
181                 case DFUI_BE_MSG_SET_GLOBAL:
182                         gp = (struct dfui_property *)payload;
183                         printf("Got a global setting change from the backend: %s = %s\n",
184                             dfui_property_get_name(gp), dfui_property_get_value(gp));
185                         dfui_fe_confirm_set_global(c);
186                         dfui_property_free(gp);
187                         break;
188                 case DFUI_BE_MSG_STOP:
189                         printf("Got a STOP message from the backend.\n");
190                         dfui_fe_confirm_stop(c);
191                         done = 1;
192                         break;
193                 }
194         }
195         printf("Frontend done.\n");
196         dfui_fe_disconnect(c);
197         
198         signal(SIGINT, SIG_DFL);
199
200         exit(0);
201 }