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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/*-
* Copyright (c) 1995
* Paul Richards. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* verbatim and that no modifications are made prior to this
* point in the file.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Paul Richards.
* 4. The name Paul Richards may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <hash.h>
#include <ctype.h>
#include <err.h>
#include <ncurses.h>
#include <forms.h>
#include <string.h>
#include <stdlib.h>
#include "internal.h"
extern hash_table *global_bindings;
struct attr_cmd {
char *name;
int attr;
};
static struct attr_cmd attr_cmds[] = {
{ "standout", 0x00010000},
{ "underline", 0x00020000},
{ "reverse", 0x00040000},
{ "blink", 0x00080000},
{ "bold", 0x00200000 }
};
int done=0;
int
init_field(char *key, void *data, void *arg)
{
struct Tuple *tuple = (struct Tuple *)data;
struct Tuple *def_tuple;
struct Field *def, *field;
int i;
int len, lim;
int strwidth;
field = (struct Field *)tuple->addr;
/* Field definitions are global, at least for now */
def_tuple = form_get_tuple(global_bindings, field->defname, FT_FIELD_DEF);
if (!def_tuple) {
warnx("Field definition not found -- skipping field");
return (-1);
}
def = (struct Field *)def_tuple->addr;
field->height = def->height;
field->width = def->width;
field->attr = def->attr;
field->selattr = def->selattr;
field->type = def->type;
switch (field->type) {
case FF_INPUT:
field->field.input = malloc(sizeof (struct InputField));
if (!field->field.input) {
warnx("Couldn't allocate memory for input field");
return (-1);
}
field->field.input->limit = def->field.input->limit;
/* Force height to one regardless, at least for now :-) */
field->height = 1;
if (!field->width && !field->field.input->limit) {
field->width = calc_string_width(def->field.input->label);
field->field.input->limit = field->width;
} else if (!field->width)
field->width = field->field.input->limit;
else if (!field->field.input->limit)
field->field.input->limit = field->width;
if (field->field.input->limit < field->width)
field->width = field->field.input->limit;
strwidth = strlen(def->field.input->label);
field->field.input->input = malloc(strwidth + 1);
if (!field->field.input->input) {
warnx("Couldn't allocate memory for input field text");
return (-1);
}
field->field.input->label = malloc(strwidth + 1);
if (!field->field.input->label) {
warnx("Couldn't allocate memory for input field label");
return (-1);
}
strncpy(field->field.input->label,
def->field.input->label,
strwidth + 1);
field->field.input->lbl_flag = def->field.input->lbl_flag;
/*
* If it's a label then clear the input string
* otherwise copy the default there.
*/
if (field->field.input->lbl_flag)
field->field.input->input[0] = '\0';
else if (field->field.input->label) {
strncpy(field->field.input->input,
field->field.input->label,
strwidth + 1);
field->field.input->input[strwidth] = 0;
}
break;
case FF_TEXT:
field->field.text = malloc(sizeof (struct TextField));
if (!field->field.text) {
warnx("Couldn't allocate memory for text field");
return (FS_ERROR);
}
strwidth = strlen(def->field.text->text);
if (!field->width)
field->width = calc_string_width(def->field.text->text);
field->field.text->text = malloc(strwidth + 1);
if (!field->field.text->text) {
warnx("Couldn't allocate memory for text field text");
return (FS_ERROR);
} else
strncpy(field->field.text->text,
def->field.text->text,
strwidth + 1);
if (!field->height)
calc_field_height(field, field->field.text->text);
break;
case FF_MENU:
field->field.menu = malloc(sizeof (struct MenuField));
if (!field->field.menu) {
warnx("Couldn't allocate memory for menu field");
return (FS_ERROR);
}
field->field.menu->no_options = 0;
field->height = 1;
lim = 0;
for (i=0; i < def->field.menu->no_options; i++) {
field->field.menu->no_options =
add_menu_option(field->field.menu,
def->field.menu->options[i]);
if (!field->field.menu->no_options) {
warnx("Couldn't add menu option");
return (FS_ERROR);
}
len = calc_string_width(def->field.menu->options[i]);
if (len > lim)
lim = len;
}
if (!field->width)
field->width = lim;
break;
case FF_ACTION:
field->field.action = malloc(sizeof (struct ActionField));
if (!field->field.action) {
warnx("Couldn't allocate memory for action field");
return (FS_ERROR);
}
if (!field->width)
field->width = calc_string_width(def->field.action->text);
strwidth = strlen(def->field.action->text);
field->field.action->text = malloc(strwidth + 1);
if (!field->field.action->text) {
warnx("Couldn't allocate memory for text field text");
return (FS_ERROR);
} else
strncpy(field->field.action->text,
def->field.action->text,
strwidth + 1);
if (!field->height)
calc_field_height(field, field->field.action->text);
field->field.action->fn = def->field.action->fn;
break;
default:
break;
}
return (1);
}
void
display_field(WINDOW *window, struct Field *field)
{
wattrset(window, field->attr);
wmove(window, field->y, field->x);
switch (field->type) {
case FF_TEXT:
display_text(window, field);
break;
case FF_MENU:
display_menu(window, field);
break;
case FF_INPUT:
display_input(window, field);
break;
case FF_ACTION:
display_action(window, field);
break;
case FF_UNKNOWN:
default:
break;
}
wattrset(window, 0);
wrefresh(window);
}
void
display_text(WINDOW *window, struct Field *field)
{
if (print_string(window, field->y, field->x, field->height,
field->width, field->field.text->text) == ERR)
print_status("Illegal scroll in print_string");
}
void
display_input(WINDOW *window, struct Field *field)
{
if (field->field.input->lbl_flag) {
if (print_string(window, field->y, field->x, field->height,
field->width, field->field.input->label) == ERR)
print_status("Illegal scroll in print_string");
} else
if (print_string(window, field->y, field->x, field->height,
field->width, field->field.input->input) == ERR)
print_status("Illegal scroll in print_string");
}
void
display_menu(WINDOW *window, struct Field *field)
{
if (print_string(window, field->y, field->x,
field->height, field->width,
field->field.menu->options[field->field.menu->selected]) == ERR)
print_status("Illegal scroll in print_string");
}
void
display_action(WINDOW *window, struct Field *field)
{
if (print_string(window, field->y, field->x, field->height,
field->width,
field->field.action->text) == ERR)
print_status("Illegal scroll in print_string");
}
int
do_action(struct Form *form)
{
struct Field *field = form->current_field;
struct Tuple *tuple;
int ch;
void (* fn)();
display_action(form->window, field);
wmove(form->window, field->y, field->x);
for (;;) {
ch = wgetch(form->window);
if (ch == FK_ACCEPT) {
tuple = form_get_tuple(form->bindings, field->field.action->fn, FT_FUNC);
if (!tuple) {
print_status("No function bound to action");
beep();
continue;
} else {
fn = tuple->addr;
(*fn)(form);
return (FS_OK);
}
} else
ch = do_key_bind(form, ch);
if (ch == FS_OK)
return (FS_OK);
else if (ch == FS_ERROR)
continue;
}
}
int
do_menu(struct Form *form)
{
struct Field *field = form->current_field;
int ch;
for (;;) {
display_menu(form->window, field);
wmove(form->window, field->y, field->x);
ch = wgetch(form->window);
switch (ch) {
case ' ':
print_status("");
field->field.menu->selected++;
if (field->field.menu->selected >= field->field.menu->no_options)
field->field.menu->selected = 0;
ch = FS_OK;
break;
default:
ch = do_key_bind(form, ch);
break;
}
if (ch == FS_OK)
return (FS_OK);
else if (ch == FS_ERROR) {
beep();
continue;
} else {
print_status("Hit the space bar to toggle through options");
beep();
continue;
}
}
}
int
do_field(struct Form *form)
{
struct Tuple *tuple;
struct Field *field = form->current_field;
void (* fn)();
int status;
/* Do field entry tasks */
if (field->enter) {
tuple = form_get_tuple(form->bindings, field->enter, FT_FUNC);
if (tuple) {
fn = tuple->addr;
(*fn)(form);
}
}
switch (field->type) {
case FF_TEXT:
status = FS_OK;
display_text(form->window, field);
break;
case FF_INPUT:
status = do_input(form);
break;
case FF_MENU:
status = do_menu(form);
break;
case FF_ACTION:
status = do_action(form);
break;
default:
status = FF_UNKNOWN;
beep();
print_status("Unknown field type");
form->current_field = form->prev_field;
break;
}
/* Do field leave tasks */
if (field->leave) {
tuple = form_get_tuple(form->bindings, field->leave, FT_FUNC);
if (tuple) {
fn = tuple->addr;
(*fn)(form);
}
}
return (status);
}
int
parse_attr(WINDOW *window, char *string)
{
int inc = 0;
struct attr_cmd *attr;
if (*(string) == '\\')
return (1);
while (!isspace(*(string + inc))) {
inc++;
}
for (attr = attr_cmds; attr->name; attr++) {
if (strncmp(attr->name, string, inc))
continue;
else {
wattron(window, attr->attr);
break;
}
}
/* Skip trailing space after the attribute string */
while (isspace(*(string + inc)))
inc++;
return (inc);
}
int
print_string(WINDOW *window, int y, int x,
int height, int fwidth, char *string)
{
int len, skip;
int width;
if (!string)
len = -1;
len = strlen(string);
if (wmove(window, y, x) == ERR)
return (ERR);
while (height--) {
width = fwidth;
while (width--) {
if (len-- > 0) {
if (*string == '\\') {
string++;
len--;
skip = parse_attr(window, string);
len -= skip;
string += skip;
}
if (waddch(window, *string++) == ERR)
return (ERR);
} else if (waddch(window, ' ') == ERR)
return (ERR);
}
if (wmove(window, ++y, x) == ERR)
return (ERR);
}
return (OK);
}
void
print_status(char *msg)
{
if (wmove(stdscr, LINES-1, 0) == ERR) {
endwin();
exit(1);
}
wclrtoeol(stdscr);
wstandout(stdscr);
if (wprintw(stdscr, "%s",
msg) == ERR) {
endwin();
exit(1);
}
wstandend(stdscr);
wrefresh(stdscr);
}
int
do_input(struct Form *form)
{
struct Field *field = form->current_field;
int len;
int disp_off=0, abspos=0, cursor = 0;
unsigned int ch;
#define DISPOFF ((len < field->width) ? 0 : len - field->width)
#define CURSPOS ((len < field->width) ? len : field->width)
len = strlen(field->field.input->input);
display_input(form->window, field);
cursor = CURSPOS;
abspos = cursor;
for (;;) {
wmove(form->window, field->y, field->x+cursor);
wrefresh(form->window);
ch = wgetch(form->window);
ch = do_key_bind(form, ch);
/*
* If there was a valid motion command then we've
* moved to a new field so just return. If the motion
* command was invalid then just go around and get another
* keystroke. Otherwise, it was not a motion command.
*/
if (ch == FS_OK)
return (FS_OK);
else if (ch == FS_ERROR)
continue;
print_status("");
if (field->field.input->lbl_flag) {
field->field.input->lbl_flag = 0;
}
if ((ch == FK_CHOME) || (ch == '')) {
disp_off = 0;
cursor = 0;
abspos = 0;
} else if ((ch == FK_CEND) || (ch == '')) {
disp_off = DISPOFF;
abspos = len;
cursor = CURSPOS;
} else if (ch == FK_CDEL) {
if (!(len-abspos))
beep();
else {
bcopy(field->field.input->input+abspos+1,
field->field.input->input+abspos,
len - abspos);
--len;
}
} else if ((ch == FK_CLEFT) || (ch == FK_CBS) || (ch == '')) {
if (!abspos)
beep();
else {
if (ch == FK_CBS) {
bcopy(field->field.input->input+abspos,
field->field.input->input+abspos-1,
len-abspos+1);
--len;
}
--abspos;
--cursor;
if ((disp_off) && (cursor < 0)) {
--disp_off;
++cursor;
}
}
} else if ((ch == FK_CRIGHT) || (ch == '')) {
if (abspos == len)
beep();
else {
++abspos;
if (++cursor >= field->width) {
++disp_off;
--cursor;
}
}
} else if ((isprint(ch)) && (len < field->field.input->limit)){
bcopy(field->field.input->input+abspos,
field->field.input->input+abspos+1, len-abspos+1);
field->field.input->input[abspos++] = ch;
len++;
if (++cursor > field->width) {
++disp_off;
--cursor;
}
} else
beep();
print_string(form->window, field->y, field->x, field->height,
field->width, field->field.input->input+disp_off);
}
}
/*
* Calculate length of printable part of the string,
* stripping out the attribute modifiers.
*/
int
calc_string_width(char *string)
{
int len, width=0;
if (!string)
return (0);
len = strlen(string);
while (len) {
if (*string != '\\') {
width++;
len--;
string++;
continue;
} else {
string++;
len--;
if (*string == '\\') {
string++;
width++;
len--;
continue;
} else {
while (!isspace(*string)) {
string++;
len--;
}
while (isspace(*string)) {
string ++;
len--;
}
}
}
}
return (width);
}
/* Calculate a default height for a field */
void
calc_field_height(struct Field *field, char *string)
{
int len;
len = calc_string_width(string);
if (!field->width) {
/*
* This is a failsafe, this routine shouldn't be called
* with a width of 0, the width should be determined
* first.
*/
field->height = 1;
return;
}
if (len < field->width) {
field->height = 1;
return;
} else
field->height = len / field->width;
if ((field->height*field->width) < len)
field->height++;
return;
}
void
exit_form(struct Form *form)
{
form->status = FS_EXIT;
}
void
cancel_form(struct Form *form)
{
form->status = FS_CANCEL;
}
|