Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / sysinstall / dmenu.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last attempt in the `sysinstall' line, the next
5  * generation being slated for what's essentially a complete rewrite.
6  *
7  * $FreeBSD: src/release/sysinstall/dmenu.c,v 1.44 2000/03/08 14:20:26 jkh Exp $
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    verbatim and that no modifications are made prior to this
18  *    point in the file.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include "sysinstall.h"
38 #include <errno.h>
39
40 #define MAX_MENU                15
41
42 static Boolean exited;
43
44 int
45 dmenuDisplayFile(dialogMenuItem *tmp)
46 {
47     systemDisplayHelp((char *)tmp->data);
48     return DITEM_SUCCESS;
49 }
50
51 int
52 dmenuSubmenu(dialogMenuItem *tmp)
53 {
54     return (dmenuOpenSimple((DMenu *)(tmp->data), FALSE) ? DITEM_SUCCESS : DITEM_FAILURE);
55 }
56
57 int
58 dmenuSystemCommand(dialogMenuItem *self)
59 {
60     WINDOW *w = NULL;   /* Keep lint happy */
61
62     /* If aux is set, the command is known not to produce any screen-spoiling output */
63     if (!self->aux)
64         w = savescr();
65     systemExecute((char *)self->data);
66     if (!self->aux)
67         restorescr(w);
68     return DITEM_SUCCESS;
69 }
70
71 int
72 dmenuSystemCommandBox(dialogMenuItem *tmp)
73 {
74     WINDOW *w = savescr();
75     
76     use_helpfile(NULL);
77     use_helpline("Select OK to dismiss this dialog");
78     dialog_prgbox(tmp->title, (char *)tmp->data, 22, 76, 1, 1);
79     restorescr(w);
80     return DITEM_SUCCESS;
81 }
82
83 int
84 dmenuExit(dialogMenuItem *tmp)
85 {
86     exited = TRUE;
87     return DITEM_LEAVE_MENU;
88 }
89
90 int
91 dmenuSetVariable(dialogMenuItem *tmp)
92 {
93     variable_set((char *)tmp->data, *((char *)tmp->data) != '_');
94     return DITEM_SUCCESS;
95 }
96
97 int
98 dmenuSetVariables(dialogMenuItem *tmp)
99 {
100     char *cp1, *cp2;
101     char *copy = strdup((char *)tmp->data);
102
103     for (cp1 = copy; cp1 != NULL;) {
104         cp2 = index(cp1, ',');
105         if (cp2 != NULL) *cp2++ = '\0';
106         variable_set(cp1, *cp1 != '_');
107         cp1 = cp2;
108     }
109     free(copy);
110     return DITEM_SUCCESS;
111 }
112
113 int
114 dmenuSetKmapVariable(dialogMenuItem *tmp)
115 {
116     char *lang;
117     int err;
118
119     variable_set((char *)tmp->data, TRUE);
120     lang = variable_get(VAR_KEYMAP);
121     if (lang != NULL)
122     {
123         err = loadKeymap(lang);
124         if (err == -1)
125             msgConfirm("No appropriate keyboard map found, sorry.");
126         else if (err == -2)
127             msgConfirm("Error installing keyboard map, errno = %d.", errno);
128     }
129     return DITEM_SUCCESS;
130 }
131
132 int
133 dmenuToggleVariable(dialogMenuItem *tmp)
134 {
135     char *var, *cp;
136     int status;
137
138     if (!(var = strdup((char *)tmp->data))) {
139         msgConfirm("Incorrect data field for `%s'!", tmp->title);
140         return DITEM_FAILURE;
141     }
142     if (!(cp = index(var, '='))) {
143         msgConfirm("Data field for %s is not in var=value format!", tmp->title);
144         return DITEM_FAILURE;
145     }
146     status = variable_check(var);
147     *cp = '\0';
148     variable_set2(var, status ? "NO" : "YES", *var != '_');
149     free(var);
150     return DITEM_SUCCESS;
151 }
152
153 int
154 dmenuISetVariable(dialogMenuItem *tmp)
155 {
156     char *ans, *var;
157
158     if (!(var = (char *)tmp->data)) {
159         msgConfirm("Incorrect data field for `%s'!", tmp->title);
160         return DITEM_FAILURE;
161     }
162     ans = msgGetInput(variable_get(var), tmp->title, 1);
163     if (!ans)
164         return DITEM_FAILURE;
165     else if (!*ans)
166         variable_unset(var);
167     else
168         variable_set2(var, ans, *var != '_');
169     return DITEM_SUCCESS;
170 }
171
172 int
173 dmenuSetFlag(dialogMenuItem *tmp)
174 {
175     if (*((unsigned int *)tmp->data) & tmp->aux)
176         *((unsigned int *)tmp->data) &= ~tmp->aux;
177     else
178         *((unsigned int *)tmp->data) |= tmp->aux;
179     return DITEM_SUCCESS;
180 }
181
182 int
183 dmenuSetValue(dialogMenuItem *tmp)
184 {
185     *((unsigned int *)tmp->data) = tmp->aux;
186     return DITEM_SUCCESS;
187 }
188
189 /* Traverse menu but give user no control over positioning */
190 Boolean
191 dmenuOpenSimple(DMenu *menu, Boolean buttons)
192 {
193     int choice, scroll, curr, max;
194
195     choice = scroll = curr = max = 0;
196     return dmenuOpen(menu, &choice, &scroll, &curr, &max, buttons);
197 }
198
199 /* Work functions for the state hook */
200 int
201 dmenuFlagCheck(dialogMenuItem *item)
202 {
203     return (*((unsigned int *)item->data) & item->aux);
204 }
205
206 int
207 dmenuVarCheck(dialogMenuItem *item)
208 {
209     char *w;
210
211     w = (char *)item->aux;
212     if (!w)
213         w = (char *)item->data;
214     return variable_check(w);
215 }
216
217 int
218 dmenuVarsCheck(dialogMenuItem *item)
219 {
220     int res, init;
221     char *w, *cp1, *cp2;
222     char *copy;
223
224     w = (char *)item->aux;
225     if (!w)
226         w = (char *)item->data;
227     if (!w)
228         return FALSE;
229     
230     copy = strdup(w);
231     res = TRUE;
232     init = FALSE;
233     for (cp1 = copy; cp1 != NULL;) {
234         init = TRUE;
235         cp2 = index(cp1, ',');
236         if (cp2 != NULL)
237             *cp2++ = '\0';
238         res = res && variable_check(cp1);
239         cp1 = cp2;
240     }
241     free(copy);
242     return res && init;
243 }
244
245 int
246 dmenuRadioCheck(dialogMenuItem *item)
247 {
248     return (*((unsigned int *)item->data) == item->aux);
249 }
250
251 static int
252 menu_height(DMenu *menu, int n)
253 {
254     int max;
255     char *t;
256
257     max = MAX_MENU;
258     if (StatusLine > 24)
259         max += StatusLine - 24;
260     for (t = menu->prompt; *t; t++) {
261         if (*t == '\n')
262             --max;
263     }
264     return n > max ? max : n;
265 }
266
267 /* Traverse over an internal menu */
268 Boolean
269 dmenuOpen(DMenu *menu, int *choice, int *scroll, int *curr, int *max, Boolean buttons)
270 {
271     int n, rval = 0;
272     dialogMenuItem *items;
273
274     items = menu->items;
275     if (buttons)
276         items += 2;
277     /* Count up all the items */
278     for (n = 0; items[n].title; n++);
279
280     while (1) {
281         char buf[FILENAME_MAX];
282         WINDOW *w = savescr();
283
284         /* Any helpful hints, put 'em up! */
285         use_helpline(menu->helpline);
286         use_helpfile(systemHelpFile(menu->helpfile, buf));
287         dialog_clear_norefresh();
288         /* Pop up that dialog! */
289         if (menu->type & DMENU_NORMAL_TYPE)
290             rval = dialog_menu((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
291                                menu_height(menu, n), -n, items, (char *)buttons, choice, scroll);
292
293         else if (menu->type & DMENU_RADIO_TYPE)
294             rval = dialog_radiolist((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
295                                     menu_height(menu, n), -n, items, (char *)buttons);
296
297         else if (menu->type & DMENU_CHECKLIST_TYPE)
298             rval = dialog_checklist((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
299                                     menu_height(menu, n), -n, items, (char *)buttons);
300         else
301             msgFatal("Menu: `%s' is of an unknown type\n", menu->title);
302         if (exited) {
303             exited = FALSE;
304             restorescr(w);
305             return TRUE;
306         }
307         else if (rval) {
308             restorescr(w);
309             return FALSE;
310         }
311         else if (menu->type & DMENU_SELECTION_RETURNS) {
312             restorescr(w);
313             return TRUE;
314         }
315     }
316 }