]>
git.ipfire.org Git - thirdparty/newt.git/blob - whiptail.c
1 /* a reasonable dialog */
10 #include "dialogboxes.h"
13 enum mode
{ MODE_NONE
, MODE_INFOBOX
, MODE_MSGBOX
, MODE_YESNO
, MODE_CHECKLIST
,
14 MODE_INPUTBOX
, MODE_RADIOLIST
, MODE_MENU
, MODE_GAUGE
};
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
22 #define OPT_RADIOLIST 1006
23 #define OPT_GAUGE 1007
24 #define OPT_INFOBOX 1008
26 static void usage(void) {
27 fprintf(stderr
, "whiptail: bad parametrs (see man dialog(1) for details)\n");
31 int main(int argc
, char ** argv
) {
32 enum mode mode
= MODE_NONE
;
50 int separateOutput
= 0;
52 char ** selections
, ** next
;
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
},
77 optCon
= poptGetContext("whiptail", argc
, argv
, optionsTable
, 0);
79 while ((arg
= poptGetNextOpt(optCon
)) > 0) {
80 optArg
= poptGetOptArg(optCon
);
84 if (mode
!= MODE_NONE
) usage();
89 if (mode
!= MODE_NONE
) usage();
94 if (mode
!= MODE_NONE
) usage();
99 if (mode
!= MODE_NONE
) usage();
100 mode
= MODE_RADIOLIST
;
104 if (mode
!= MODE_NONE
) usage();
105 mode
= MODE_CHECKLIST
;
108 case OPT_FULLBUTTONS
:
113 if (mode
!= MODE_NONE
) usage();
118 if (mode
!= MODE_NONE
) usage();
123 if (mode
!= MODE_NONE
) usage();
124 mode
= MODE_INPUTBOX
;
130 fprintf(stderr
, "%s: %s\n",
131 poptBadOption(optCon
, POPT_BADOPTION_NOALIAS
),
136 if (mode
== MODE_NONE
) usage();
138 if (!(text
= poptGetArg(optCon
))) usage();
140 if (!(nextArg
= poptGetArg(optCon
))) usage();
141 height
= strtoul(nextArg
, &end
, 10);
144 if (!(nextArg
= poptGetArg(optCon
))) usage();
145 width
= strtoul(nextArg
, &end
, 10);
148 if (mode
== MODE_GAUGE
) {
151 if (open("/dev/tty", O_RDWR
) != 0) perror("open /dev/tty");
158 newtOpenWindow((80 - width
) / 2, (24 - height
) / 2, width
, height
, title
);
161 newtDrawRootText(0, 0, backtitle
);
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
;
170 rc
= messageBox(text
, height
, width
, MSGBOX_MSG
, flags
);
174 rc
= messageBox(text
, height
, width
, MSGBOX_INFO
, flags
);
178 rc
= messageBox(text
, height
, width
, MSGBOX_YESNO
, flags
);
182 rc
= inputBox(text
, height
, width
, optCon
, flags
, &result
);
183 if (!rc
) fprintf(stderr
, "%s", result
);
187 rc
= listBox(text
, height
, width
, optCon
, flags
, &result
);
188 if (!rc
) fprintf(stderr
, "%s", result
);
192 rc
= checkList(text
, height
, width
, optCon
, 1, flags
, &selections
);
194 fprintf(stderr
, "%s", selections
[0]);
200 rc
= checkList(text
, height
, width
, optCon
, 0, flags
, &selections
);
203 for (next
= selections
; *next
; next
++) {
204 if (!separateOutput
) {
205 if (needSpace
) putc(' ', stderr
);
206 fprintf(stderr
, "\"%s\"", *next
);
209 fprintf(stderr
, "%s\n", *next
);
218 rc
= gauge(text
, height
, width
, optCon
, fd
, flags
);
225 if (rc
== -1) usage();