Deleted Added
full compact
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 * $Id: dmenu.c,v 1.23 1996/07/11 18:37:47 jkh Exp $
7 * $Id: dmenu.c,v 1.24 1996/08/01 10:58:50 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 <sys/types.h>
39
40#define MAX_MENU 15
41
42static Boolean exited;
43
44int
45dmenuDisplayFile(dialogMenuItem *tmp)
46{
47 systemDisplayHelp((char *)tmp->data);
48 return DITEM_SUCCESS | DITEM_RESTORE;
49}
50
51int
52dmenuSubmenu(dialogMenuItem *tmp)
53{
54 return (dmenuOpenSimple((DMenu *)(tmp->data), FALSE) ? DITEM_SUCCESS : DITEM_FAILURE) |
55 DITEM_RECREATE;
56}
57
58int
59dmenuSystemCommand(dialogMenuItem *self)
60{
61 WINDOW *w = NULL; /* Keep lint happy */
62
63 /* If aux is set, the command is known not to produce any screen-spoiling output */
64 if (!self->aux)
65 w = savescr();
66 systemExecute((char *)self->data);
67 if (!self->aux)
68 restorescr(w);
69 return DITEM_SUCCESS;
70}
71
72int
73dmenuSystemCommandBox(dialogMenuItem *tmp)
74{
75 use_helpfile(NULL);
76 use_helpline("Select OK to dismiss this dialog");
77 dialog_prgbox(tmp->title, (char *)tmp->data, 22, 76, 1, 1);
78 return DITEM_SUCCESS | DITEM_RESTORE;
79}
80
81int
82dmenuExit(dialogMenuItem *tmp)
83{
84 exited = TRUE;
85 return DITEM_LEAVE_MENU;
86}
87
88int
89dmenuSetVariable(dialogMenuItem *tmp)
90{
91 variable_set((char *)tmp->data);
92 return DITEM_SUCCESS;
93}
94
95int
96dmenuToggleVariable(dialogMenuItem *tmp)
97{
98 if (!variable_get((char *)tmp->data))
99 variable_set((char *)tmp->data);
100 else
101 variable_unset((char *)tmp->data);
102 return DITEM_SUCCESS;
103}
104
105int
106dmenuSetFlag(dialogMenuItem *tmp)
107{
108 if (*((unsigned int *)tmp->data) & tmp->aux)
109 *((unsigned int *)tmp->data) &= ~tmp->aux;
110 else
111 *((unsigned int *)tmp->data) |= tmp->aux;
112 return DITEM_SUCCESS;
113}
114
115int
116dmenuSetValue(dialogMenuItem *tmp)
117{
118 *((unsigned int *)tmp->data) = tmp->aux;
119 return DITEM_SUCCESS;
120}
121
122/* Traverse menu but give user no control over positioning */
123Boolean
124dmenuOpenSimple(DMenu *menu, Boolean buttons)
125{
126 int choice, scroll, curr, max;
127
128 choice = scroll = curr = max = 0;
129 return dmenuOpen(menu, &choice, &scroll, &curr, &max, buttons);
130}
131
132/* Work functions for the state hook */
133int
134dmenuFlagCheck(dialogMenuItem *item)
135{
136 return (*((unsigned int *)item->data) & item->aux);
137}
138
139int
140dmenuVarCheck(dialogMenuItem *item)
141{
142 char *w, *cp, *cp2, tmp[256];
143
144 w = (char *)item->aux;
145 if (!w)
146 w = (char *)item->data;
147 if (!w)
148 return FALSE;
149 strncpy(tmp, w, 256);
150 if ((cp = index(tmp, '=')) != NULL) {
151 *(cp++) = '\0';
152 cp2 = getenv(tmp);
153 if (cp2)
154 return !strcmp(cp, cp2);
155 else
156 return FALSE;
157 }
158 else
159 return (int)getenv(tmp);
160}
161
162int
163dmenuRadioCheck(dialogMenuItem *item)
164{
165 return (*((unsigned int *)item->data) == item->aux);
166}
167
168static int
169menu_height(DMenu *menu, int n)
170{
171 int max;
172 char *t;
173
174 max = MAX_MENU;
175 if (StatusLine > 24)
176 max += StatusLine - 24;
177 for (t = menu->prompt; *t; t++) {
178 if (*t == '\n')
179 --max;
180 }
181 return n > max ? max : n;
182}
183
184/* Traverse over an internal menu */
185Boolean
186dmenuOpen(DMenu *menu, int *choice, int *scroll, int *curr, int *max, Boolean buttons)
187{
188 int n, rval = 0;
189 dialogMenuItem *items;
190
191 items = menu->items;
192 if (buttons)
193 items += 2;
194 /* Count up all the items */
195 for (n = 0; items[n].title; n++);
196
197 while (1) {
198 char buf[FILENAME_MAX];
199
200 /* Any helpful hints, put 'em up! */
201 use_helpline(menu->helpline);
202 use_helpfile(systemHelpFile(menu->helpfile, buf));
203
204 /* Pop up that dialog! */
205 dialog_clear();
205 dialog_clear_norefresh();
206 if (menu->type & DMENU_NORMAL_TYPE)
207 rval = dialog_menu((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
208 menu_height(menu, n), -n, items, (char *)buttons, choice, scroll);
209
210 else if (menu->type & DMENU_RADIO_TYPE)
211 rval = dialog_radiolist((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
212 menu_height(menu, n), -n, items, (char *)buttons);
213
214 else if (menu->type & DMENU_CHECKLIST_TYPE)
215 rval = dialog_checklist((u_char *)menu->title, (u_char *)menu->prompt, -1, -1,
216 menu_height(menu, n), -n, items, (char *)buttons);
217 else
218 msgFatal("Menu: `%s' is of an unknown type\n", menu->title);
219 clearok(stdscr, TRUE);
220 if (exited) {
221 exited = FALSE;
222 return TRUE;
223 }
224 else if (rval)
225 return FALSE;
226 else if (menu->type & DMENU_SELECTION_RETURNS)
227 return TRUE;
228 }
229}