]> git.ipfire.org Git - thirdparty/newt.git/blob - whiptail.c
1) patch from Bruce Perens which makes newt easier to build on debian
[thirdparty/newt.git] / whiptail.c
1 /* a reasonable dialog */
2
3 #include <fcntl.h>
4 #include <popt.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include "dialogboxes.h"
11 #include "newt.h"
12
13 enum mode { MODE_NONE, MODE_INFOBOX, MODE_MSGBOX, MODE_YESNO, MODE_CHECKLIST,
14 MODE_INPUTBOX, MODE_RADIOLIST, MODE_MENU, MODE_GAUGE };
15
16 #define OPT_MSGBOX 1000
17 #define OPT_CHECKLIST 1001
18 #define OPT_YESNO 1002
19 #define OPT_INPUTBOX 1003
20 #define OPT_FULLBUTTONS 1004
21 #define OPT_MENU 1005
22 #define OPT_RADIOLIST 1006
23 #define OPT_GAUGE 1007
24 #define OPT_INFOBOX 1008
25
26 static void usage(void) {
27 fprintf(stderr, "whiptail: bad parametrs (see man dialog(1) for details)\n");
28 exit(1);
29 }
30
31 int main(int argc, char ** argv) {
32 enum mode mode = MODE_NONE;
33 poptContext optCon;
34 int arg;
35 char * optArg;
36 char * text;
37 char * nextArg;
38 char * end;
39 int height;
40 int width;
41 int fd = -1;
42 int needSpace = 0;
43 int noCancel = 0;
44 int noItem = 0;
45 int clear = 0;
46 int scrollText = 0;
47 int rc = 1;
48 int flags = 0;
49 int defaultNo = 0;
50 int separateOutput = 0;
51 char * result;
52 char ** selections, ** next;
53 char * title = NULL;
54 char * backtitle = NULL;
55 struct poptOption optionsTable[] = {
56 { "backtitle", '\0', POPT_ARG_STRING, &backtitle, 0 },
57 { "checklist", '\0', 0, 0, OPT_CHECKLIST },
58 { "clear", '\0', 0, &clear, 0 },
59 { "defaultno", '\0', 0, &defaultNo, 0 },
60 { "inputbox", '\0', 0, 0, OPT_INPUTBOX },
61 { "fb", '\0', 0, 0, OPT_FULLBUTTONS },
62 { "fullbuttons", '\0', 0, 0, OPT_FULLBUTTONS },
63 { "gauge", '\0', 0, 0, OPT_GAUGE },
64 { "infobox", '\0', 0, 0, OPT_INFOBOX },
65 { "menu", '\0', 0, 0, OPT_MENU },
66 { "msgbox", '\0', 0, 0, OPT_MSGBOX },
67 { "nocancel", '\0', 0, &noCancel, 0 },
68 { "noitem", '\0', 0, &noItem, 0 },
69 { "radiolist", '\0', 0, 0, OPT_RADIOLIST },
70 { "scrolltext", '\0', 0, &scrollText, 0 },
71 { "separate-output", '\0', 0, &separateOutput, 0 },
72 { "title", '\0', POPT_ARG_STRING, &title, 0 },
73 { "yesno", '\0', 0, 0, OPT_YESNO },
74 { 0, 0, 0, 0, 0 }
75 };
76
77 optCon = poptGetContext("whiptail", argc, argv, optionsTable, 0);
78
79 while ((arg = poptGetNextOpt(optCon)) > 0) {
80 optArg = poptGetOptArg(optCon);
81
82 switch (arg) {
83 case OPT_INFOBOX:
84 if (mode != MODE_NONE) usage();
85 mode = MODE_INFOBOX;
86 break;
87
88 case OPT_MENU:
89 if (mode != MODE_NONE) usage();
90 mode = MODE_MENU;
91 break;
92
93 case OPT_MSGBOX:
94 if (mode != MODE_NONE) usage();
95 mode = MODE_MSGBOX;
96 break;
97
98 case OPT_RADIOLIST:
99 if (mode != MODE_NONE) usage();
100 mode = MODE_RADIOLIST;
101 break;
102
103 case OPT_CHECKLIST:
104 if (mode != MODE_NONE) usage();
105 mode = MODE_CHECKLIST;
106 break;
107
108 case OPT_FULLBUTTONS:
109 useFullButtons(1);
110 break;
111
112 case OPT_YESNO:
113 if (mode != MODE_NONE) usage();
114 mode = MODE_YESNO;
115 break;
116
117 case OPT_GAUGE:
118 if (mode != MODE_NONE) usage();
119 mode = MODE_GAUGE;
120 break;
121
122 case OPT_INPUTBOX:
123 if (mode != MODE_NONE) usage();
124 mode = MODE_INPUTBOX;
125 break;
126 }
127 }
128
129 if (arg < -1) {
130 fprintf(stderr, "%s: %s\n",
131 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
132 poptStrerror(arg));
133 exit(1);
134 }
135
136 if (mode == MODE_NONE) usage();
137
138 if (!(text = poptGetArg(optCon))) usage();
139
140 if (!(nextArg = poptGetArg(optCon))) usage();
141 height = strtoul(nextArg, &end, 10);
142 if (*end) usage();
143
144 if (!(nextArg = poptGetArg(optCon))) usage();
145 width = strtoul(nextArg, &end, 10);
146 if (*end) usage();
147
148 if (mode == MODE_GAUGE) {
149 fd = dup(0);
150 close(0);
151 if (open("/dev/tty", O_RDWR) != 0) perror("open /dev/tty");
152 }
153
154 newtInit();
155 newtCls();
156 width -= 2;
157 height -= 2;
158 newtOpenWindow((80 - width) / 2, (24 - height) / 2, width, height, title);
159
160 if (backtitle)
161 newtDrawRootText(0, 0, backtitle);
162
163 if (noCancel) flags |= FLAG_NOCANCEL;
164 if (noItem) flags |= FLAG_NOITEM;
165 if (scrollText) flags |= FLAG_SCROLL_TEXT;
166 if (defaultNo) flags |= FLAG_DEFAULT_NO;
167
168 switch (mode) {
169 case MODE_MSGBOX:
170 rc = messageBox(text, height, width, MSGBOX_MSG, flags);
171 break;
172
173 case MODE_INFOBOX:
174 rc = messageBox(text, height, width, MSGBOX_INFO, flags);
175 break;
176
177 case MODE_YESNO:
178 rc = messageBox(text, height, width, MSGBOX_YESNO, flags);
179 break;
180
181 case MODE_INPUTBOX:
182 rc = inputBox(text, height, width, optCon, flags, &result);
183 if (!rc) fprintf(stderr, "%s", result);
184 break;
185
186 case MODE_MENU:
187 rc = listBox(text, height, width, optCon, flags, &result);
188 if (!rc) fprintf(stderr, "%s", result);
189 break;
190
191 case MODE_RADIOLIST:
192 rc = checkList(text, height, width, optCon, 1, flags, &selections);
193 if (!rc) {
194 fprintf(stderr, "%s", selections[0]);
195 free(selections);
196 }
197 break;
198
199 case MODE_CHECKLIST:
200 rc = checkList(text, height, width, optCon, 0, flags, &selections);
201
202 if (!rc) {
203 for (next = selections; *next; next++) {
204 if (!separateOutput) {
205 if (needSpace) putc(' ', stderr);
206 fprintf(stderr, "\"%s\"", *next);
207 needSpace = 1;
208 } else {
209 fprintf(stderr, "%s\n", *next);
210 }
211 }
212
213 free(selections);
214 }
215 break;
216
217 case MODE_GAUGE:
218 rc = gauge(text, height, width, optCon, fd, flags);
219 break;
220
221 default:
222 usage();
223 }
224
225 if (rc == -1) usage();
226
227 if (clear)
228 newtPopWindow();
229 newtFinished();
230
231 return rc;
232 }