1 | /* |
---|
2 | * dialog - Display simple dialog boxes from shell scripts |
---|
3 | * |
---|
4 | * AUTHOR: Savio Lam (lam836@cs.cuhk.hk) |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License |
---|
8 | * as published by the Free Software Foundation; either version 2 |
---|
9 | * of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "dialog.h" |
---|
22 | |
---|
23 | static void Usage (); |
---|
24 | |
---|
25 | extern int dialog_yesno_with_default(const char *title, const char *prompt, int height, |
---|
26 | int width, int yesno_default); |
---|
27 | int callback_writeerr(GtkWidget *w, gpointer *pt); |
---|
28 | |
---|
29 | static int separate_output = 0; |
---|
30 | |
---|
31 | typedef int (jumperFn) (const char *title, int argc, const char *const *argv); |
---|
32 | |
---|
33 | struct Mode { |
---|
34 | char *name; |
---|
35 | int argmin, argmax, argmod; |
---|
36 | jumperFn *jumper; |
---|
37 | }; |
---|
38 | |
---|
39 | jumperFn j_yesno, j_msgbox, j_infobox, j_textbox, j_menu; |
---|
40 | jumperFn j_checklist, j_radiolist, j_inputbox, j_gauge; |
---|
41 | |
---|
42 | /* |
---|
43 | * All functions are used in the slackware root disk, apart from "gauge" |
---|
44 | */ |
---|
45 | |
---|
46 | static struct Mode modes[] = |
---|
47 | { |
---|
48 | {"--yesno", 5, 6, 1, j_yesno}, |
---|
49 | {"--msgbox", 5, 5, 1, j_msgbox}, |
---|
50 | {"--infobox", 5, 5, 1, j_infobox}, |
---|
51 | {"--textbox", 5, 5, 1, j_textbox}, |
---|
52 | {"--menu", 8, 0, 2, j_menu}, |
---|
53 | {"--checklist", 9, 0, 3, j_checklist}, |
---|
54 | {"--radiolist", 9, 0, 3, j_radiolist}, |
---|
55 | {"--inputbox", 5, 6, 1, j_inputbox}, |
---|
56 | {"--gauge", 6, 6, 1, j_gauge}, |
---|
57 | {NULL, 0, 0, 0, NULL} |
---|
58 | }; |
---|
59 | |
---|
60 | static struct Mode *modePtr; |
---|
61 | |
---|
62 | int gnome_mode; |
---|
63 | |
---|
64 | void add_atk_namedesc(GtkWidget *widget, const gchar *name, const gchar *desc) |
---|
65 | { |
---|
66 | AtkObject *atk_widget; |
---|
67 | |
---|
68 | g_return_if_fail (GTK_IS_WIDGET(widget)); |
---|
69 | atk_widget = gtk_widget_get_accessible(widget); |
---|
70 | |
---|
71 | if (name) |
---|
72 | atk_object_set_name(atk_widget, name); |
---|
73 | if (desc) |
---|
74 | atk_object_set_description(atk_widget, desc); |
---|
75 | } |
---|
76 | |
---|
77 | void add_atk_relation(GtkWidget *obj1, GtkWidget *obj2, AtkRelationType type) |
---|
78 | { |
---|
79 | |
---|
80 | AtkObject *atk_obj1, *atk_obj2; |
---|
81 | AtkRelationSet *relation_set; |
---|
82 | AtkRelation *relation; |
---|
83 | |
---|
84 | g_return_if_fail (GTK_IS_WIDGET(obj1)); |
---|
85 | g_return_if_fail (GTK_IS_WIDGET(obj2)); |
---|
86 | |
---|
87 | atk_obj1 = gtk_widget_get_accessible(obj1); |
---|
88 | atk_obj2 = gtk_widget_get_accessible(obj2); |
---|
89 | |
---|
90 | relation_set = atk_object_ref_relation_set (atk_obj1); |
---|
91 | relation = atk_relation_new(&atk_obj2, 1, type); |
---|
92 | atk_relation_set_add(relation_set, relation); |
---|
93 | g_object_unref(G_OBJECT (relation)); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | int main(int argc, char *argv[]) |
---|
99 | { |
---|
100 | int offset = 0, clear_screen = 0, end_common_opts = 0, retval; |
---|
101 | const char *title = NULL; |
---|
102 | |
---|
103 | bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR); |
---|
104 | bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); |
---|
105 | textdomain(GETTEXT_PACKAGE); |
---|
106 | |
---|
107 | if (argc < 2) { |
---|
108 | Usage(); |
---|
109 | exit(-1); |
---|
110 | } |
---|
111 | |
---|
112 | if(getenv("DISPLAY")) |
---|
113 | { |
---|
114 | gnome_program_init ("gdialog", VERSION, LIBGNOMEUI_MODULE, |
---|
115 | argc, argv,NULL); |
---|
116 | |
---|
117 | gnome_mode=1; |
---|
118 | } |
---|
119 | |
---|
120 | if (!strcmp(argv[1], "--create-rc")) { |
---|
121 | |
---|
122 | #ifndef NO_COLOR_CURSES |
---|
123 | if (argc != 3) { |
---|
124 | Usage(); |
---|
125 | exit(-1); |
---|
126 | } |
---|
127 | create_rc(argv[2]); |
---|
128 | return 0; |
---|
129 | #else |
---|
130 | fprintf(stderr, "This option is currently unsupported " |
---|
131 | "on your system.\n"); |
---|
132 | return -1; |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | while (offset < argc - 1 && !end_common_opts) { /* Common options */ |
---|
137 | if (!strcmp(argv[offset + 1], "--title")) { |
---|
138 | if (argc - offset < 3 || title != NULL) { |
---|
139 | Usage(); |
---|
140 | exit(-1); |
---|
141 | } else { |
---|
142 | title = argv[offset + 2]; |
---|
143 | offset += 2; |
---|
144 | } |
---|
145 | } else if (!strcmp(argv[offset + 1], "--backtitle")) { |
---|
146 | if (backtitle != NULL) { |
---|
147 | Usage(); |
---|
148 | exit(-1); |
---|
149 | } else { |
---|
150 | backtitle = argv[offset + 2]; |
---|
151 | offset += 2; |
---|
152 | } |
---|
153 | } else if (!strcmp(argv[offset + 1], "--separate-output")) { |
---|
154 | separate_output = 1; |
---|
155 | offset++; |
---|
156 | } else if (!strcmp(argv[offset + 1], "--clear")) { |
---|
157 | if (clear_screen) { /* Hey, "--clear" can't appear twice! */ |
---|
158 | Usage(); |
---|
159 | exit(-1); |
---|
160 | } else if (argc == 2) { /* we only want to clear the screen */ |
---|
161 | init_dialog(); |
---|
162 | if(!gnome_mode) refresh(); /* init_dialog() will clear the screen for us */ |
---|
163 | end_dialog(); |
---|
164 | return 0; |
---|
165 | } else { |
---|
166 | clear_screen = 1; |
---|
167 | offset++; |
---|
168 | } |
---|
169 | } else if(!strcmp(argv[offset + 1], "--defaultno")) { |
---|
170 | offset++; |
---|
171 | } else /* no more common options */ |
---|
172 | end_common_opts = 1; |
---|
173 | } |
---|
174 | |
---|
175 | if (argc - 1 == offset) { /* no more options */ |
---|
176 | Usage(); |
---|
177 | exit(-1); |
---|
178 | } |
---|
179 | /* use a table to look for the requested mode, to avoid code duplication */ |
---|
180 | |
---|
181 | for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */ |
---|
182 | if (!strcmp(argv[offset + 1], modePtr->name)) |
---|
183 | break; |
---|
184 | |
---|
185 | if (!modePtr->name) |
---|
186 | Usage(); |
---|
187 | if (argc - offset < modePtr->argmin) |
---|
188 | Usage(); |
---|
189 | if (modePtr->argmax && argc - offset > modePtr->argmax) |
---|
190 | Usage(); |
---|
191 | if ((argc - offset) % modePtr->argmod) |
---|
192 | Usage(); |
---|
193 | |
---|
194 | /* |
---|
195 | * Check the range of values for height & width of dialog box for text mode. |
---|
196 | */ |
---|
197 | if (((atoi(argv[offset+3]) <= 0) || (atoi(argv[offset+3]) > 25) && !gnome_mode ) |
---|
198 | || ((atoi(argv[offset+4]) <= 0) || (atoi(argv[offset+4]) > 80) && !gnome_mode)) { |
---|
199 | fprintf(stderr, "\nError, values for height (1..25) & width (1..80) \n"); |
---|
200 | exit(-1); |
---|
201 | } |
---|
202 | init_dialog(); |
---|
203 | retval = (*(modePtr->jumper)) (title, argc - offset, (const char * const *)argv + offset); |
---|
204 | |
---|
205 | if(retval == -10) |
---|
206 | { |
---|
207 | Usage(); |
---|
208 | retval = !retval; |
---|
209 | } |
---|
210 | |
---|
211 | if (clear_screen) { /* clear screen before exit */ |
---|
212 | attr_clear(stdscr, LINES, COLS, screen_attr); |
---|
213 | if(!gnome_mode) refresh(); |
---|
214 | } |
---|
215 | end_dialog(); |
---|
216 | |
---|
217 | exit(retval); |
---|
218 | } |
---|
219 | |
---|
220 | /* |
---|
221 | * Print program usage |
---|
222 | */ |
---|
223 | static void Usage() |
---|
224 | { |
---|
225 | fprintf(stderr, "\ |
---|
226 | \ndialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).\ |
---|
227 | \n patched to version %s by Stuart Herbert (S.Herbert@shef.ac.uk)\ |
---|
228 | \n\ |
---|
229 | \n* Display dialog boxes from shell scripts *\ |
---|
230 | \n\ |
---|
231 | \nUsage: gdialog --clear\ |
---|
232 | \n gdialog --create-rc <file>\ |
---|
233 | \n gdialog [--title <title>] [--separate-output] [--backtitle <backtitle>] [--clear] <Box options>\ |
---|
234 | \n\ |
---|
235 | \nBox options:\ |
---|
236 | \n\ |
---|
237 | \n [--defaultno] --yesno <text> <height> <width> [--defaultno]\ |
---|
238 | \n --msgbox <text> <height> <width>\ |
---|
239 | \n --infobox <text> <height> <width>\ |
---|
240 | \n --inputbox <text> <height> <width> [<init>]\ |
---|
241 | \n --textbox <file> <height> <width>\ |
---|
242 | \n --menu <text> <height> <width> <menu height> <tag1> <item1>...\ |
---|
243 | \n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\ |
---|
244 | \n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\ |
---|
245 | \n --gauge <text> <height> <width> <percent>\n", |
---|
246 | VERSION); |
---|
247 | exit(-1); |
---|
248 | } |
---|
249 | |
---|
250 | /* |
---|
251 | * These are the program jumpers |
---|
252 | */ |
---|
253 | |
---|
254 | int j_yesno(const char *t, int ac, const char *const *av) |
---|
255 | { |
---|
256 | if(!strcmp(av[0], "--defaultno")) |
---|
257 | { |
---|
258 | return dialog_yesno_with_default(t, av[2], atoi(av[3]), |
---|
259 | atoi(av[4]), 0); |
---|
260 | } |
---|
261 | else if(ac == 5) |
---|
262 | { |
---|
263 | return dialog_yesno_with_default(t, av[2], atoi(av[3]), |
---|
264 | atoi(av[4]), 1); |
---|
265 | } |
---|
266 | else if(!strcmp(av[5], "--defaultno")) |
---|
267 | { |
---|
268 | return dialog_yesno_with_default(t, av[2], atoi(av[3]), |
---|
269 | atoi(av[4]), 0); |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | return -10; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | int j_msgbox(const char *t, int ac, const char *const *av) |
---|
278 | { |
---|
279 | return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 1); |
---|
280 | } |
---|
281 | |
---|
282 | int j_infobox(const char *t, int ac, const char *const *av) |
---|
283 | { |
---|
284 | return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 0); |
---|
285 | } |
---|
286 | |
---|
287 | int j_textbox(const char *t, int ac, const char *const *av) |
---|
288 | { |
---|
289 | return dialog_textbox(t, av[2], atoi(av[3]), atoi(av[4])); |
---|
290 | } |
---|
291 | |
---|
292 | int j_menu(const char *t, int ac, const char *const *av) |
---|
293 | { |
---|
294 | int ret = dialog_menu(t, av[2], atoi(av[3]), atoi(av[4]), |
---|
295 | atoi(av[5]), (ac - 6) / 2, av + 6); |
---|
296 | if (ret >= 0) { |
---|
297 | fprintf(stderr, av[6 + ret * 2]); |
---|
298 | return 0; |
---|
299 | } else if (ret == -2) |
---|
300 | return 1; /* CANCEL */ |
---|
301 | return ret; /* ESC pressed, ret == -1 */ |
---|
302 | } |
---|
303 | |
---|
304 | int j_checklist(const char *t, int ac, const char *const *av) |
---|
305 | { |
---|
306 | return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]), |
---|
307 | atoi(av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK, separate_output); |
---|
308 | } |
---|
309 | |
---|
310 | int j_radiolist(const char *t, int ac, const char *const *av) |
---|
311 | { |
---|
312 | return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]), |
---|
313 | atoi(av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO, separate_output); |
---|
314 | } |
---|
315 | |
---|
316 | int j_inputbox(const char *t, int ac, const char *const *av) |
---|
317 | { |
---|
318 | int ret = dialog_inputbox(t, av[2], atoi(av[3]), atoi(av[4]), |
---|
319 | ac == 6 ? av[5] : (char *) NULL); |
---|
320 | if (ret == 0) |
---|
321 | fprintf(stderr, dialog_input_result); |
---|
322 | return ret; |
---|
323 | } |
---|
324 | |
---|
325 | int j_gauge(const char *t, int ac, const char *const *av) |
---|
326 | { |
---|
327 | return dialog_gauge(t, av[2], atoi(av[3]), atoi(av[4]), |
---|
328 | atoi(av[5])); |
---|
329 | } |
---|
330 | |
---|
331 | #ifdef WITH_GNOME |
---|
332 | |
---|
333 | /* |
---|
334 | * Gnome Callbacks |
---|
335 | */ |
---|
336 | |
---|
337 | int callback_writeerr(GtkWidget *w, gpointer *pt) |
---|
338 | { |
---|
339 | char *p=(char *)pt; |
---|
340 | write(2, p, strlen(p)); |
---|
341 | write(2, "\n", 1); |
---|
342 | gtk_main_quit(); |
---|
343 | |
---|
344 | return 0; |
---|
345 | } |
---|
346 | |
---|
347 | int callback_exitby(GtkWidget *w, gpointer *pt) |
---|
348 | { |
---|
349 | gint i = GPOINTER_TO_INT(pt); |
---|
350 | exit(i); |
---|
351 | } |
---|
352 | |
---|
353 | #endif |
---|