Bug Summary

File:gtx/frame.c
Location:line 274, column 5
Description:Assigned value is always the same as the existing value

Annotated Source Code

1/*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10#include <afsconfig.h>
11#include <afs/param.h>
12#include <afs/stds.h>
13
14#include <roken.h>
15
16#include <lwp.h>
17
18#include "gtxobjects.h"
19#include "gtxwindows.h"
20#include "gtxcurseswin.h"
21#include "gtxinput.h"
22#include "gtxkeymap.h"
23#include "gtxframe.h"
24
25static struct keymap_map *recursiveMap = 0;
26static char menubuffer[1024]; /*Buffer for menu selections */
27int gtxframe_exitValue = 0; /*Program exit value */
28
29int
30gtxframe_CtrlUCmd(void *aparam, void *arock)
31{
32 struct gwin *awindow = (struct gwin *) aparam;
33 struct gtx_frame *tframe;
34
35 tframe = awindow->w_frame;
36 if (!tframe->defaultLine)
37 return -1;
38 *(tframe->defaultLine) = 0;
39 return 0;
40}
41
42int
43gtxframe_CtrlHCmd(void *aparam, void *arock)
44{
45 struct gwin *awindow = (struct gwin *) aparam;
46
47 struct gtx_frame *tframe;
48 char *tp;
49 int pos;
50
51 tframe = awindow->w_frame;
52 if (!(tp = tframe->defaultLine))
53 return -1;
54 pos = strlen(tp);
55 if (pos == 0)
56 return 0; /* rubout at the end of the line */
57 tp[pos - 1] = 0;
58 return 0;
59}
60
61int
62gtxframe_RecursiveEndCmd(void *aparam, void *arock)
63{
64 struct gwin *awindow = (struct gwin *) aparam;
65
66 struct gtx_frame *tframe;
67
68 tframe = awindow->w_frame;
69 tframe->flags |= GTXFRAME_RECURSIVEEND2;
70 tframe->flags &= ~GTXFRAME_RECURSIVEERR4;
71 return 0;
72}
73
74int
75gtxframe_RecursiveErrCmd(void *aparam, void *arock)
76{
77 struct gwin *awindow = (struct gwin *) aparam;
78
79 struct gtx_frame *tframe;
80
81 tframe = awindow->w_frame;
82 tframe->flags |= GTXFRAME_RECURSIVEEND2;
83 tframe->flags |= GTXFRAME_RECURSIVEERR4;
84 return 0;
85}
86
87int
88gtxframe_SelfInsertCmd(void *aparam, void *rockparam)
89{
90 struct gwin *awindow = (struct gwin *) aparam;
91
92 int arock = (intptr_t)rockparam;
93
94 struct gtx_frame *tframe;
95 int pos;
96 char *tp;
97
98 tframe = awindow->w_frame;
99 if (!(tp = tframe->defaultLine))
100 return -1;
101 pos = strlen(tp);
102 tp[pos] = arock; /* arock has char to insert */
103 tp[pos + 1] = 0; /* null-terminate it, too */
104 return 0;
105}
106
107/* save map, setup recursive map and install it */
108static int
109SaveMap(struct gtx_frame *aframe)
110{
111 char tstring[2];
112 int i;
113
114 if (!recursiveMap) {
115 /* setup recursive edit map if not previously done */
116 recursiveMap = keymap_Create();
117 keymap_BindToString(recursiveMap, "\010", gtxframe_CtrlHCmd, NULL((void *)0),
118 NULL((void *)0));
119 keymap_BindToString(recursiveMap, "\177", gtxframe_CtrlHCmd, NULL((void *)0),
120 NULL((void *)0));
121 keymap_BindToString(recursiveMap, "\025", gtxframe_CtrlUCmd, NULL((void *)0),
122 NULL((void *)0));
123 keymap_BindToString(recursiveMap, "\033", gtxframe_RecursiveEndCmd,
124 NULL((void *)0), NULL((void *)0));
125 keymap_BindToString(recursiveMap, "\015", gtxframe_RecursiveEndCmd,
126 NULL((void *)0), NULL((void *)0));
127 keymap_BindToString(recursiveMap, "\012", gtxframe_RecursiveEndCmd,
128 NULL((void *)0), NULL((void *)0));
129 keymap_BindToString(recursiveMap, "\003", gtxframe_RecursiveErrCmd,
130 NULL((void *)0), NULL((void *)0));
131 keymap_BindToString(recursiveMap, "\007", gtxframe_RecursiveErrCmd,
132 NULL((void *)0), NULL((void *)0));
133
134 for (i = 040; i < 0177; i++) {
135 tstring[0] = i;
136 tstring[1] = 0;
137 keymap_BindToString(recursiveMap, tstring, gtxframe_SelfInsertCmd,
138 NULL((void *)0), (void *)(intptr_t)i);
139 }
140 }
141 aframe->savemap = aframe->keymap;
142 aframe->keymap = recursiveMap;
143 keymap_InitState(aframe->keystate, aframe->keymap);
144 return 0;
145}
146
147/* Restore map to previous value */
148static int
149RestoreMap(struct gtx_frame *aframe)
150{
151 aframe->keymap = aframe->savemap;
152 aframe->savemap = (struct keymap_map *)0;
153 keymap_InitState(aframe->keystate, aframe->keymap);
154 return 0;
155}
156
157int
158gtxframe_SetFrame(struct gwin *awin, struct gtx_frame *aframe)
159{
160 if (awin->w_frame) {
161 /* Unthread this frame */
162 awin->w_frame->window = NULL((void *)0);
163 }
164 awin->w_frame = aframe;
165 aframe->window = awin; /* Set frame's window ptr */
166 return 0;
167}
168
169struct gtx_frame *
170gtxframe_GetFrame(struct gwin *awin)
171{
172 return awin->w_frame;
173}
174
175/* Add a menu string to display list */
176int
177gtxframe_AddMenu(struct gtx_frame *aframe, char *alabel, char *astring)
178{
179 struct gtxframe_menu *tmenu;
180
181 if (aframe->menus)
182 for (tmenu = aframe->menus; tmenu; tmenu = tmenu->next) {
183 if (strcmp(alabel, tmenu->name) == 0)
184 break;
185 } else
186 tmenu = (struct gtxframe_menu *)0;
187 if (!tmenu) {
188 /* Handle everything but the command string, which is handled by the
189 * common-case code below */
190 tmenu = (struct gtxframe_menu *)malloc(sizeof(*tmenu));
191 if (tmenu == (struct gtxframe_menu *)0)
192 return (-1);
193 memset(tmenu, 0, sizeof(*tmenu));
194 tmenu->next = aframe->menus;
195 aframe->menus = tmenu;
196 tmenu->name = gtx_CopyString(alabel);
197 }
198
199 /*
200 * Common case: redo the string labels. Note: at this point, tmenu
201 * points to a valid menu.
202 */
203 if (tmenu->cmdString)
204 free(tmenu->cmdString);
205 tmenu->cmdString = gtx_CopyString(astring);
206 return 0;
207}
208
209/* Delete a given menu from a frame*/
210int
211gtxframe_DeleteMenu(struct gtx_frame *aframe, char *alabel)
212{
213 struct gtxframe_menu *tm, **lm;
214
215 for (lm = &aframe->menus, tm = *lm; tm; lm = &tm->next, tm = *lm) {
216 if (strcmp(alabel, tm->name) == 0) {
217 /* found it, remove and return success */
218 *lm = tm->next; /* unthread from list */
219 free(tm->name);
220 free(tm->cmdString);
221 free(tm);
222 return (0);
223 }
224 }
225 return (-1); /* failed to find entry to delete */
226}
227
228/* Function to remove all known menus */
229int
230gtxframe_ClearMenus(struct gtx_frame *aframe)
231{
232
233 struct gtxframe_menu *tm, *nm;
234
235 if (aframe->menus != (struct gtxframe_menu *)0) {
236 for (tm = aframe->menus; tm; tm = nm) {
237 nm = tm->next;
238 free(tm->name);
239 free(tm->cmdString);
240 free(tm);
241 }
242 }
243
244 aframe->menus = (struct gtxframe_menu *)0;
245 return 0;
246}
247
248int
249gtxframe_AskForString(struct gtx_frame *aframe, char *aprompt,
250 char *adefault, char *aresult, int aresultSize)
251{
252 int code;
253 char *tp;
254
255 /* Ensure recursive-edit map is initialized */
256 SaveMap(aframe);
257
258 /* Set up display */
259 if (aframe->promptLine)
1
Taking false branch
260 free(aframe->promptLine);
261 if (aframe->defaultLine)
2
Taking false branch
262 free(aframe->defaultLine);
263 aframe->promptLine = gtx_CopyString(aprompt);
264 tp = aframe->defaultLine = (char *)malloc(1024);
265 if (tp == NULL((void *)0))
3
Taking false branch
266 return (-1);
267 if (adefault)
4
Taking true branch
268 strcpy(tp, adefault);
269 else
270 *tp = 0;
271
272 /* Do recursive edit */
273 gtx_InputServer(aframe->window);
274 tp = aframe->defaultLine; /* In case command reallocated it */
5
Assigned value is always the same as the existing value
275
276 /* Back from recursive edit, check out what's happened */
277 if (aframe->flags & GTXFRAME_RECURSIVEERR4) {
278 code = -1;
279 goto done;
280 }
281 code = strlen(tp);
282 if (code + 1 > aresultSize) {
283 code = -2;
284 goto done;
285 }
286 strcpy(aresult, tp);
287 code = 0;
288
289 /* Fall through to cleanup and return code */
290 done:
291 RestoreMap(aframe);
292 if (aframe->promptLine)
293 free(aframe->promptLine);
294 if (aframe->defaultLine)
295 free(aframe->defaultLine);
296 aframe->defaultLine = aframe->promptLine = NULL((void *)0);
297 if (code)
298 gtxframe_DisplayString(aframe, "[Aborted]");
299 return (code);
300}
301
302int
303gtxframe_DisplayString(struct gtx_frame *aframe, char *amsgLine)
304{
305 if (aframe->messageLine)
306 free(aframe->messageLine);
307 aframe->messageLine = gtx_CopyString(amsgLine);
308 return 0;
309}
310
311/* Called by input processor to try to clear the dude */
312int
313gtxframe_ClearMessageLine(struct gtx_frame *aframe)
314{
315 /* If we haven't shown message long enough yet, just return */
316 if (aframe->flags & GTXFRAME_NEWDISPLAY1)
317 return (0);
318 if (aframe->messageLine)
319 free(aframe->messageLine);
320 aframe->messageLine = NULL((void *)0);
321 return (0);
322}
323
324static int
325ShowMessageLine(struct gtx_frame *aframe)
326{
327 struct gwin_strparams strparms;
328 struct gwin_sizeparams sizeparms;
329 char *tp;
330
331 if (!aframe->window)
332 return -1;
333
334 /* First, find window size */
335 WOP_GETDIMENSIONS(aframe->window, &sizeparms)(aframe->window)->w_op->gw_getdimensions(aframe->
window, &sizeparms)
;
336
337 if (aframe->promptLine) {
338 memset(&strparms, 0, sizeof(strparms));
339 strparms.x = 0;
340 strparms.y = sizeparms.maxy - 1;
341 strparms.highlight = 1;
342 tp = strparms.s = (char *)malloc(1024);
343 strcpy(tp, aframe->promptLine);
344 strcat(tp, aframe->defaultLine);
345 WOP_DRAWSTRING(aframe->window, &strparms)(aframe->window)->w_op->gw_drawstring(aframe->window
, &strparms)
;
346 aframe->flags |= GTXFRAME_NEWDISPLAY1;
347 } else if (aframe->messageLine) {
348 /* Otherwise we're visible, print the message at the bottom */
349 memset(&strparms, 0, sizeof(strparms));
350 strparms.highlight = 1;
351 strparms.x = 0;
352 strparms.y = sizeparms.maxy - 1;
353 strparms.s = aframe->messageLine;
354 WOP_DRAWSTRING(aframe->window, &strparms)(aframe->window)->w_op->gw_drawstring(aframe->window
, &strparms)
;
355 aframe->flags |= GTXFRAME_NEWDISPLAY1;
356 }
357 return (0);
358}
359
360/* Exit function, returning whatever has been put in its argument */
361int
362gtxframe_ExitCmd(void *a_exitValuep, void *arock)
363{ /*gtxframe_ExitCmd */
364
365 int exitval; /*Value we've been asked to exit with */
366
367 /* This next call should be type independent! */
368 gator_cursesgwin_cleanup(&gator_basegwin);
369
370 exitval = *((int *)(a_exitValuep));
371 exit(exitval);
372
373} /*gtxframe_ExitCmd */
374
375struct gtx_frame *
376gtxframe_Create(void)
377{
378 struct gtx_frame *tframe;
379 struct keymap_map *newkeymap;
380 struct keymap_state *newkeystate;
381
382 /*
383 * Allocate all the pieces first: frame, keymap, and key state.
384 */
385 tframe = (struct gtx_frame *)malloc(sizeof(struct gtx_frame));
386 if (tframe == (struct gtx_frame *)0) {
387 return ((struct gtx_frame *)0);
388 }
389
390 newkeymap = keymap_Create();
391 if (newkeymap == (struct keymap_map *)0) {
392 /*
393 * Get rid of the frame before exiting.
394 */
395 free(tframe);
396 return ((struct gtx_frame *)0);
397 }
398
399 newkeystate = (struct keymap_state *)
400 malloc(sizeof(struct keymap_state));
401 if (newkeystate == (struct keymap_state *)0) {
402 /*
403 * Get rid of the frame AND the keymap before exiting.
404 */
405 free(tframe);
406 free(newkeymap);
407 return ((struct gtx_frame *)0);
408 }
409
410 /*
411 * Now that all the pieces exist, fill them in and stick them in
412 * the right places.
413 */
414 memset(tframe, 0, sizeof(struct gtx_frame));
415 tframe->keymap = newkeymap;
416 tframe->keystate = newkeystate;
417 keymap_InitState(tframe->keystate, tframe->keymap);
418 keymap_BindToString(tframe->keymap, "\003", gtxframe_ExitCmd, "ExitCmd",
419 (char *)(&gtxframe_exitValue));
420
421 /*
422 * At this point, we return successfully.
423 */
424 return (tframe);
425}
426
427int
428gtxframe_Delete(struct gtx_frame *aframe)
429{
430 keymap_Delete(aframe->keymap);
431 free(aframe->keystate);
432 if (aframe->messageLine)
433 free(aframe->messageLine);
434 free(aframe);
435 return 0;
436}
437
438int
439gtxframe_Display(struct gtx_frame *aframe, struct gwin *awm)
440{
441 struct gtxframe_dlist *tlist;
442 struct gtxframe_menu *tm;
443 struct gwin_strparams strparms;
444
445 /* Run through the menus, displaying them on the top line */
446 *menubuffer = 0;
447 for (tm = aframe->menus; tm; tm = tm->next) {
448 strcat(menubuffer, tm->name);
449 strcat(menubuffer, ":");
450 strcat(menubuffer, tm->cmdString);
451 strcat(menubuffer, " ");
452 }
453 if (menubuffer[0] != 0) {
454 memset(&strparms, 0, sizeof(strparms));
455 strparms.x = 0;
456 strparms.y = 0;
457 strparms.s = menubuffer;
458 strparms.highlight = 1;
459 WOP_DRAWSTRING(awm, &strparms)(awm)->w_op->gw_drawstring(awm, &strparms);
460 }
461
462 /* Run through the display list, displaying all objects */
463 for (tlist = aframe->display; tlist; tlist = tlist->next) {
464 OOP_DISPLAY(((struct onode *)(tlist->data)))(((struct onode *)(tlist->data)))->o_op->on_display(
((struct onode *)(tlist->data)));
;
465 }
466
467 /* Finally, show the message line */
468 ShowMessageLine(awm->w_frame);
469 return (0);
470}
471
472/* Add an object to a window's display list */
473int
474gtxframe_AddToList(struct gtx_frame *aframe, struct onode *aobj)
475{
476 struct gtxframe_dlist *tlist;
477
478 for (tlist = aframe->display; tlist; tlist = tlist->next) {
479 if (tlist->data == (char *)aobj) {
480 /*
481 * Don't add the same thing twice.
482 */
483 return (-1);
484 }
485 }
486
487 /*
488 * OK, it's not alreadyt there. Create a new list object, fill it
489 * in, and splice it on.
490 */
491 tlist = (struct gtxframe_dlist *)malloc(sizeof(struct gtxframe_dlist));
492 if (tlist == (struct gtxframe_dlist *)0)
493 return (-1);
494 tlist->data = (char *)aobj;
495 tlist->next = aframe->display;
496 aframe->display = tlist;
497 return (0);
498}
499
500/* Remove an object from a display list, if it is already there */
501int
502gtxframe_RemoveFromList(struct gtx_frame *aframe, struct onode *aobj)
503{
504 struct gtxframe_dlist *tlist, **plist;
505
506 plist = &aframe->display;
507 for (tlist = *plist; tlist; plist = &tlist->next, tlist = *plist) {
508 if (tlist->data == (char *)aobj) {
509 *plist = tlist->next;
510 free(tlist);
511 return 0;
512 }
513 }
514 return (-1); /* Item not found */
515}
516
517/* Clear out everything on the display list for the given frame*/
518int
519gtxframe_ClearList(struct gtx_frame *aframe)
520{
521 struct gtxframe_dlist *tlist, *nlist;
522
523 if (aframe->display != (struct gtxframe_dlist *)0) {
524 /*
525 * Throw away each display list structure (we have at least
526 * one).
527 */
528 for (tlist = aframe->display; tlist; tlist = nlist) {
529 nlist = tlist->next;
530 free(tlist);
531 }
532 }
533
534 aframe->display = (struct gtxframe_dlist *)0;
535 return 0;
536}