File: whiptcl.c

package info (click to toggle)
newt 0.52.19-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,848 kB
  • ctags: 2,048
  • sloc: ansic: 9,266; python: 713; makefile: 261; sh: 33; xml: 7
file content (304 lines) | stat: -rw-r--r-- 8,280 bytes parent folder | download | duplicates (4)
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
#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 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 */
        char *result = Tcl_Alloc(200);
	sprintf(result, "%s: %s\n", 
		poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
		poptStrerror(arg));
        Tcl_SetResult(interp, result, TCL_DYNAMIC);
	return TCL_ERROR;
    }

    if (mode == MODE_NONE) {
        Tcl_SetResult(interp, "no dialog mode was specified", TCL_STATIC);
	return TCL_ERROR;
    } else if (rc) {
	Tcl_SetResult(interp, "multiple modes were specified", TCL_STATIC);
	return TCL_ERROR;
    }

    if (!(text = poptGetArg(optCon))) {
	Tcl_SetResult(interp, "missing text parameter", TCL_STATIC);
	return TCL_ERROR;
    }

    if (!(nextArg = poptGetArg(optCon))) {
	Tcl_setResult(interp,"height missing", TCL_STATIC);
	return TCL_ERROR;
    }
    height = strtoul(nextArg, &end, 10);
    if (*end) {
	Tcl_SetResult(interp,"height is not a number", TCL_STATIC);
	return TCL_ERROR;
    }

    if (!(nextArg = poptGetArg(optCon))) {
	Tcl_SetResult(interp, "width missing", TCL_STATIC);
	return TCL_ERROR;
    }
    width = strtoul(nextArg, &end, 10);
    if (*end) {
	Tcl_SetResult(interp, "width is not a number", TCL_STATIC);
	return TCL_ERROR;
    }

    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);
        Tcl_SetResult( interp, rc == DLG_OKAY ? "yes " : "no" , TCL_STATIC);
	if (rc == DLG_ERROR) rc = 0;
	break;

      case MODE_INPUTBOX:
	rc = inputBox(text, height, width, optCon, flags, &result);
	if (rc ==DLG_OKAY) {
            Tcl_SetResult( interp, result, TCL_DYNAMIC);
	}
	break;

      case MODE_MENU:
	rc = listBox(text, height, width, optCon, flags, default_item, &result);
	if (rc==DLG_OKAY) {
            Tcl_SetResult(interp, result, TCL_DYNAMIC);
	}
	break;

      case MODE_RADIOLIST:
	rc = checkList(text, height, width, optCon, 1, flags, &selections);
	if (rc==DLG_OKAY) {
	    Tcl_SetResult( interp, selections[0], TCL_DYNAMIC);
	}
	break;

      case MODE_CHECKLIST:
	rc = checkList(text, height, width, 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) {
        Tcl_SetResult(interp, "bad paramter for whiptcl dialog box", TCL_STATIC);
	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;
}