-
Notifications
You must be signed in to change notification settings - Fork 10
/
whiptcl.c
333 lines (275 loc) · 8.41 KB
/
whiptcl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include "nls.h"
#include "dialogboxes.h"
#include "newt.h"
#include <popt.h>
#include <tcl.h>
enum mode { MODE_NONE, MODE_MSGBOX, MODE_YESNO, MODE_CHECKLIST, MODE_INPUTBOX,
MODE_RADIOLIST, MODE_MENU };
#define OPT_MSGBOX 1000
#define OPT_CHECKLIST 1001
#define OPT_YESNO 1002
#define OPT_INPUTBOX 1003
#define OPT_MENU 1005
#define OPT_RADIOLIST 1006
static char * setBacktext(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags);
static char * setHelptext(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags);
static char * setFullButtons(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags);
static int wtFinish(ClientData clientData, Tcl_Interp * interp, int argc,
const char ** argv) {
newtFinished();
return TCL_OK;
}
static int wtInit(ClientData clientData, Tcl_Interp * interp, int argc,
const char ** argv) {
newtInit();
newtCls();
newtPushHelpLine("");
Tcl_TraceVar(interp, "whiptcl_backtext",
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY, setBacktext, NULL);
Tcl_TraceVar(interp, "whiptcl_helpline",
TCL_TRACE_WRITES | TCL_TRACE_UNSETS | TCL_GLOBAL_ONLY,
setHelptext, NULL);
Tcl_TraceVar(interp, "whiptcl_fullbuttons",
TCL_TRACE_WRITES | TCL_TRACE_UNSETS | TCL_GLOBAL_ONLY,
setFullButtons, NULL);
Tcl_SetVar(interp, "whiptcl_helpline", "", TCL_GLOBAL_ONLY);
Tcl_SetVar(interp, "whiptcl_fullbuttons", "1", TCL_GLOBAL_ONLY);
return TCL_OK;
}
static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc,
const char ** argv) {
enum mode mode = MODE_NONE;
poptContext optCon;
int arg;
const char * optArg;
const char * text;
const char * nextArg;
char * end;
int height;
int width;
int listHeight;
int noCancel = 0;
int noItem = 0;
int scrollText = 0;
int rc = 0;
int flags = 0;
int defaultNo = 0;
char * result;
char ** selections, ** next;
char * title = NULL;
char *default_item = NULL;
struct poptOption optionsTable[] = {
{ "checklist", '\0', 0, 0, OPT_CHECKLIST },
{ "defaultno", '\0', 0, &defaultNo, 0 },
{ "inputbox", '\0', 0, 0, OPT_INPUTBOX },
{ "menu", '\0', 0, 0, OPT_MENU },
{ "msgbox", '\0', 0, 0, OPT_MSGBOX },
{ "nocancel", '\0', 0, &noCancel, 0 },
{ "noitem", '\0', 0, &noItem, 0 },
{ "radiolist", '\0', 0, 0, OPT_RADIOLIST },
{ "scrolltext", '\0', 0, &scrollText, 0 },
{ "title", '\0', POPT_ARG_STRING, &title, 0 },
{ "default-item", '\0', POPT_ARG_STRING, &default_item, 0 },
{ "yesno", '\0', 0, 0, OPT_YESNO },
{ 0, 0, 0, 0, 0 }
};
#ifdef ENABLE_NLS
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#endif
optCon = poptGetContext("whiptcl", argc, argv, optionsTable, 0);
while ((arg = poptGetNextOpt(optCon)) > 0) {
optArg = poptGetOptArg(optCon);
switch (arg) {
case OPT_MENU:
if (mode != MODE_NONE) rc = -1;
mode = MODE_MENU;
break;
case OPT_MSGBOX:
if (mode != MODE_NONE) rc = -1;
mode = MODE_MSGBOX;
break;
case OPT_RADIOLIST:
if (mode != MODE_NONE) rc = -1;
mode = MODE_RADIOLIST;
break;
case OPT_CHECKLIST:
if (mode != MODE_NONE) rc = -1;
mode = MODE_CHECKLIST;
break;
case OPT_YESNO:
if (mode != MODE_NONE) rc = -1;
mode = MODE_YESNO;
break;
case OPT_INPUTBOX:
if (mode != MODE_NONE) rc = -1;
mode = MODE_INPUTBOX;
break;
}
}
if (arg < -1) {
/* this could buffer oveflow, bug we're not setuid so I don't care */
interp->result = malloc(200);
interp->freeProc = TCL_DYNAMIC;
sprintf(interp->result, "%s: %s\n",
poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
poptStrerror(arg));
return TCL_ERROR;
}
if (mode == MODE_NONE) {
interp->result = "no dialog mode was specified";
return TCL_ERROR;
} else if (rc) {
interp->result = "multiple modes were specified";
return TCL_ERROR;
}
if (!(text = poptGetArg(optCon))) {
interp->result = "missing text parameter";
return TCL_ERROR;
}
if (!(nextArg = poptGetArg(optCon))) {
interp->result = "height missing";
return TCL_ERROR;
}
height = strtoul(nextArg, &end, 10);
if (*end) {
interp->result = "height is not a number";
return TCL_ERROR;
}
if (!(nextArg = poptGetArg(optCon))) {
interp->result = "width missing";
return TCL_ERROR;
}
width = strtoul(nextArg, &end, 10);
if (*end) {
interp->result = "width is not a number";
return TCL_ERROR;
}
switch (mode) {
case MODE_MENU:
case MODE_RADIOLIST:
case MODE_CHECKLIST:
if (!(nextArg = poptGetArg(optCon))) {
interp->result = "list-height missing";
return TCL_ERROR;
}
listHeight = strtoul(nextArg, &end, 10);
if (*end) {
interp->result = "list-height is not a number";
return TCL_ERROR;
}
break;
}
width -= 2;
height -= 2;
newtOpenWindow((80 - width) / 2, (24 - height) / 2, width, height, title);
if (noCancel) flags |= FLAG_NOCANCEL;
if (noItem) flags |= FLAG_NOITEM;
if (scrollText) flags |= FLAG_SCROLL_TEXT;
if (defaultNo) flags |= FLAG_DEFAULT_NO;
switch (mode) {
case MODE_MSGBOX:
rc = messageBox(text, height, width, MSGBOX_MSG, flags);
break;
case MODE_YESNO:
rc = messageBox(text, height, width, MSGBOX_YESNO, flags);
if (rc == DLG_OKAY)
interp->result = "yes";
else
interp->result = "no";
if (rc == DLG_ERROR) rc = 0;
break;
case MODE_INPUTBOX:
rc = inputBox(text, height, width, optCon, flags, &result);
if (rc ==DLG_OKAY) {
interp->result = result;
interp->freeProc = TCL_DYNAMIC;
}
break;
case MODE_MENU:
rc = listBox(text, height, width, listHeight, optCon, flags, default_item,
&result);
if (rc==DLG_OKAY) {
interp->result = result;
interp->freeProc = TCL_DYNAMIC;
}
break;
case MODE_RADIOLIST:
rc = checkList(text, height, width, listHeight, optCon, 1, flags,
&selections);
if (rc==DLG_OKAY) {
interp->result = selections[0];
interp->freeProc = TCL_DYNAMIC;
free(selections);
}
break;
case MODE_CHECKLIST:
rc = checkList(text, height, width, listHeight, optCon, 0, flags,
&selections);
if (rc==DLG_OKAY) {
for (next = selections; *next; next++)
Tcl_AppendElement(interp, *next);
free(selections);
}
break;
case MODE_NONE:
; /* this can't happen */
break;
}
newtPopWindow();
if (rc == DLG_ERROR) {
interp->result = "bad paramter for whiptcl dialog box";
return TCL_ERROR;
}
Tcl_SetVar(interp, "whiptcl_canceled", (rc == DLG_CANCEL) ? "1" : "0",
0);
Tcl_SetVar(interp, "whiptcl_escaped", (rc == DLG_ESCAPE) ? "1" : "0",
0);
return TCL_OK;
}
static char * setBacktext(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags) {
static char blankLine[81] = " "
" ";
newtDrawRootText(0, 0, blankLine);
newtDrawRootText(0, 0, Tcl_GetVar(interp, "whiptcl_backtext",
TCL_GLOBAL_ONLY));
return NULL;
}
static char * setHelptext(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags) {
const char * text = Tcl_GetVar(interp, "whiptcl_helpline", TCL_GLOBAL_ONLY);
if (!text)
text = "";
else if (!strlen(text))
text = NULL;
newtPopHelpLine();
newtPushHelpLine(text);
return NULL;
}
static char * setFullButtons(ClientData data, Tcl_Interp * interp,
const char * name1, const char * name2, int flags) {
const char * val = Tcl_GetVar(interp, "whiptcl_fullbuttons", TCL_GLOBAL_ONLY);
int rc;
int state;
if ((rc = Tcl_ExprBoolean(interp, val, &state))) {
Tcl_FreeResult(interp);
return "whiptcl_fullbuttons may only contain a boolean value";
}
useFullButtons(state);
return NULL;
}
int Whiptcl_Init(Tcl_Interp * interp) {
Tcl_CreateCommand(interp, "whiptcl_finish", wtFinish, NULL, NULL);
Tcl_CreateCommand(interp, "whiptcl_init", wtInit, NULL, NULL);
Tcl_CreateCommand(interp, "whiptcl_cmd", (Tcl_CmdProc *) wtCmd, NULL, NULL);
Tcl_PkgProvide(interp, "Whip", VERSION);
return TCL_OK;
}