| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 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 | |
| 25 | static struct keymap_map *recursiveMap = 0; |
| 26 | static char menubuffer[1024]; |
| 27 | int gtxframe_exitValue = 0; |
| 28 | |
| 29 | int |
| 30 | gtxframe_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 | |
| 42 | int |
| 43 | gtxframe_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; |
| 57 | tp[pos - 1] = 0; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | gtxframe_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 | |
| 74 | int |
| 75 | gtxframe_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 | |
| 87 | int |
| 88 | gtxframe_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; |
| 103 | tp[pos + 1] = 0; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static int |
| 109 | SaveMap(struct gtx_frame *aframe) |
| 110 | { |
| 111 | char tstring[2]; |
| 112 | int i; |
| 113 | |
| 114 | if (!recursiveMap) { |
| 115 | |
| 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 | |
| 148 | static int |
| 149 | RestoreMap(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 | |
| 157 | int |
| 158 | gtxframe_SetFrame(struct gwin *awin, struct gtx_frame *aframe) |
| 159 | { |
| 160 | if (awin->w_frame) { |
| 161 | |
| 162 | awin->w_frame->window = NULL((void *)0); |
| 163 | } |
| 164 | awin->w_frame = aframe; |
| 165 | aframe->window = awin; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | struct gtx_frame * |
| 170 | gtxframe_GetFrame(struct gwin *awin) |
| 171 | { |
| 172 | return awin->w_frame; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | int |
| 177 | gtxframe_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 | |
| 189 | |
| 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 | |
| 201 | |
| 202 | |
| 203 | if (tmenu->cmdString) |
| 204 | free(tmenu->cmdString); |
| 205 | tmenu->cmdString = gtx_CopyString(astring); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | int |
| 211 | gtxframe_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 | |
| 218 | *lm = tm->next; |
| 219 | free(tm->name); |
| 220 | free(tm->cmdString); |
| 221 | free(tm); |
| 222 | return (0); |
| 223 | } |
| 224 | } |
| 225 | return (-1); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | int |
| 230 | gtxframe_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 | |
| 248 | int |
| 249 | gtxframe_AskForString(struct gtx_frame *aframe, char *aprompt, |
| 250 | char *adefault, char *aresult, int aresultSize) |
| 251 | { |
| 252 | int code; |
| 253 | char *tp; |
| 254 | |
| 255 | |
| 256 | SaveMap(aframe); |
| 257 | |
| 258 | |
| 259 | if (aframe->promptLine) |
| |
| 260 | free(aframe->promptLine); |
| 261 | if (aframe->defaultLine) |
| |
| 262 | free(aframe->defaultLine); |
| 263 | aframe->promptLine = gtx_CopyString(aprompt); |
| 264 | tp = aframe->defaultLine = (char *)malloc(1024); |
| 265 | if (tp == NULL((void *)0)) |
| |
| 266 | return (-1); |
| 267 | if (adefault) |
| |
| 268 | strcpy(tp, adefault); |
| 269 | else |
| 270 | *tp = 0; |
| 271 | |
| 272 | |
| 273 | gtx_InputServer(aframe->window); |
| 274 | tp = aframe->defaultLine; |
| 5 | Assigned value is always the same as the existing value |
|
| 275 | |
| 276 | |
| 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 | |
| 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 | |
| 302 | int |
| 303 | gtxframe_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 | |
| 312 | int |
| 313 | gtxframe_ClearMessageLine(struct gtx_frame *aframe) |
| 314 | { |
| 315 | |
| 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 | |
| 324 | static int |
| 325 | ShowMessageLine(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 | |
| 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 | |
| 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 | |
| 361 | int |
| 362 | gtxframe_ExitCmd(void *a_exitValuep, void *arock) |
| 363 | { |
| 364 | |
| 365 | int exitval; |
| 366 | |
| 367 | |
| 368 | gator_cursesgwin_cleanup(&gator_basegwin); |
| 369 | |
| 370 | exitval = *((int *)(a_exitValuep)); |
| 371 | exit(exitval); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | struct gtx_frame * |
| 376 | gtxframe_Create(void) |
| 377 | { |
| 378 | struct gtx_frame *tframe; |
| 379 | struct keymap_map *newkeymap; |
| 380 | struct keymap_state *newkeystate; |
| 381 | |
| 382 | |
| 383 | |
| 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 | |
| 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 | |
| 404 | |
| 405 | free(tframe); |
| 406 | free(newkeymap); |
| 407 | return ((struct gtx_frame *)0); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 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 *)(>xframe_exitValue)); |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | return (tframe); |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | gtxframe_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 | |
| 438 | int |
| 439 | gtxframe_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 | |
| 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 | |
| 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 | |
| 468 | ShowMessageLine(awm->w_frame); |
| 469 | return (0); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | int |
| 474 | gtxframe_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 | |
| 482 | |
| 483 | return (-1); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 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 | |
| 501 | int |
| 502 | gtxframe_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); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | int |
| 519 | gtxframe_ClearList(struct gtx_frame *aframe) |
| 520 | { |
| 521 | struct gtxframe_dlist *tlist, *nlist; |
| 522 | |
| 523 | if (aframe->display != (struct gtxframe_dlist *)0) { |
| 524 | |
| 525 | |
| 526 | |
| 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 | } |