blob: 97125e396e5fc0720069a08117ef9a6075588b32 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
2 * Copyright (c) 2003-2013,2014 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28/*
29 * $Id: demo_forms.c,v 1.50 2014/10/10 00:38:00 tom Exp $
30 *
31 * Demonstrate a variety of functions from the form library.
32 * Thomas Dickey - 2003/4/26
33 */
34/*
35TYPE_ENUM -
36TYPE_REGEXP -
37dup_field -
38field_init -
39field_just -
40field_term -
41form_init -
42form_opts -
43form_opts_off -
44form_opts_on -
45form_request_by_name -
46form_term -
47form_userptr -
48free_fieldtype -
49link_field -
50link_fieldtype -
51move_field -
52new_page -
53pos_form_cursor -
54set_field_init -
55set_field_term -
56set_fieldtype_arg -
57set_fieldtype_choice -
58set_form_fields -
59set_form_init -
60set_form_opts -
61set_form_page -
62set_form_term -
63set_form_userptr -
64set_max_field -
65*/
66
67#include <test.priv.h>
68
69#if USE_LIBFORM
70
71#include <edit_field.h>
72
73typedef struct {
74 char *name;
75 char *value;
76} MY_DATA;
77
78static MY_DATA *my_data;
79
80static int d_option = 0;
81static int j_value = 0;
82static int m_value = 0;
83static int o_value = 0;
84static char *t_value = 0;
85
86static void
87failed(const char *s)
88{
89 perror(s);
90 ExitProgram(EXIT_FAILURE);
91}
92
93static void
94chomp(char *value)
95{
96 size_t have = strlen(value);
97 while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
98 value[--have] = '\0';
99 }
100}
101
102static int
103trimmed(const char *value)
104{
105 int result = (int) strlen(value);
106 while (result > 0 && isspace(UChar(value[result - 1]))) {
107 --result;
108 }
109 return result;
110}
111
112static char *
113get_data(const char *name)
114{
115 char *result = t_value;
116 if (my_data != 0) {
117 int n;
118 for (n = 0; my_data[n].name != 0; ++n) {
119 if (!strcmp(name, my_data[n].name)) {
120 result = my_data[n].value;
121 break;
122 }
123 }
124 }
125 return result;
126}
127
128/*
129 * Read (possibly) multi-line data with name+value pairs.
130 */
131static void
132read_data(const char *filename)
133{
134 FILE *fp = fopen(filename, "r");
135
136 if (fp != 0) {
137 char buffer[BUFSIZ];
138 char *colon;
139 int more = 0;
140 int item = 0;
141
142 my_data = typeCalloc(MY_DATA, (size_t) 100); /* FIXME */
143 while (fgets(buffer, sizeof(buffer), fp) != 0) {
144 chomp(buffer);
145 if (more) {
146 if (strcmp(buffer, ".")) {
147 char *prior = my_data[more - 1].value;
148 size_t need = strlen(buffer) + 2 + strlen(prior);
149 char *value = typeRealloc(char, need, prior);
150 if (value == 0)
151 failed("realloc");
152 strcat(value, "\n");
153 strcat(value, buffer);
154 my_data[more - 1].value = value;
155 } else {
156 more = 0;
157 }
158 } else if (*buffer == '#') {
159 continue;
160 } else if ((colon = strchr(buffer, ':')) != 0) {
161 char *name;
162 char *value;
163 *colon++ = '\0';
164 name = strdup(buffer);
165 value = strdup(colon);
166 if (name == 0 || value == 0)
167 failed("strdup");
168 my_data[item].name = name;
169 my_data[item].value = value;
170 more = ++item;
171 } else {
172 failed("expected a colon");
173 }
174 }
175 fclose(fp);
176 } else {
177 failed(filename);
178 }
179}
180
181static FIELD *
182make_label(const char *label, int frow, int fcol)
183{
184 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
185
186 if (f) {
187 set_field_buffer(f, 0, label);
188 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
189 }
190 return (f);
191}
192
193/*
194 * Define each field with an extra one, for reflecting "actual" text.
195 */
196static FIELD *
197make_field(const char *label, int frow, int fcol, int rows, int cols)
198{
199 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
200
201 if (f) {
202 set_field_back(f, A_UNDERLINE);
203 /*
204 * If -j and -d options are combined, -j loses. It is documented in
205 * "Character User Interface Programming", page 12-15 that setting
206 * O_STATIC off makes the form library ignore justification.
207 */
208 set_field_just(f, j_value);
209 if (d_option) {
210 if (has_colors()) {
211 set_field_fore(f, (chtype) COLOR_PAIR(2));
212 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
213 } else {
214 set_field_fore(f, A_BOLD);
215 }
216 /*
217 * The field_opts_off() call dumps core with Solaris curses,
218 * but that is a known bug in Solaris' form library -TD
219 */
220 field_opts_off(f, O_STATIC);
221 set_max_field(f, m_value);
222 }
223
224 init_edit_field(f, get_data(label));
225 }
226 return (f);
227}
228
229static void
230display_form(FORM * f)
231{
232 WINDOW *w;
233 int rows, cols;
234
235 scale_form(f, &rows, &cols);
236
237 /*
238 * Put the form at the upper-left corner of the display, with just a box
239 * around it.
240 */
241 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
242 set_form_win(f, w);
243 set_form_sub(f, derwin(w, rows, cols, 1, 2));
244 box(w, 0, 0);
245 keypad(w, TRUE);
246
247 if (post_form(f) != E_OK)
248 wrefresh(w);
249 }
250}
251
252static void
253erase_form(FORM * f)
254{
255 WINDOW *w = form_win(f);
256 WINDOW *s = form_sub(f);
257
258 unpost_form(f);
259 werase(w);
260 wrefresh(w);
261 delwin(s);
262 delwin(w);
263}
264
265static void
266show_insert_mode(bool insert_mode)
267{
268 MvAddStr(5, 57, (insert_mode
269 ? "form_status: insert "
270 : "form_status: overlay"));
271}
272
273#define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
274
275static FIELD *
276another_field(FORM * form, FIELD * field)
277{
278 FIELD **f = form_fields(form);
279 FIELD *result = 0;
280 int n;
281
282 for (n = 0; f[n] != 0; ++n) {
283 if (f[n] != field) {
284 result = f[n];
285 field_opts_on(result, O_SELECTABLE);
286 break;
287 }
288 }
289 return result;
290}
291
292static int
293my_form_driver(FORM * form, int c)
294{
295 static bool insert_mode = TRUE;
296 FIELD *field;
297
298 switch (c) {
299 case MY_QUIT:
300 if (form_driver(form, REQ_VALIDATION) == E_OK)
301 return (TRUE);
302 break;
303 case MY_HELP:
304 help_edit_field();
305 break;
306 case MY_EDT_MODE:
307 if ((field = current_field(form)) != 0) {
308 set_current_field(form, another_field(form, field));
309 if ((unsigned) field_opts(field) & O_EDIT) {
310 field_opts_off(field, O_EDIT);
311 set_field_status(field, 0);
312 } else {
313 field_opts_on(field, O_EDIT);
314 }
315 set_current_field(form, field);
316 }
317 break;
318 case MY_INS_MODE:
319 /* there should be a form_status() function, but there is none */
320 if (!insert_mode) {
321 if (form_driver(form, REQ_INS_MODE) == E_OK) {
322 insert_mode = TRUE;
323 }
324 } else {
325 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
326 insert_mode = FALSE;
327 }
328 }
329 show_insert_mode(insert_mode);
330 refresh();
331 break;
332 default:
333 beep();
334 break;
335 }
336 return (FALSE);
337}
338
339static void
340show_current_field(WINDOW *win, FORM * form)
341{
342 FIELD *field;
343 FIELDTYPE *type;
344 char *buffer;
345 int nbuf;
346 int field_rows, field_cols, field_max;
347 int currow, curcol;
348
349 if (has_colors()) {
350 wbkgd(win, (chtype) COLOR_PAIR(1));
351 }
352 werase(win);
353 form_getyx(form, currow, curcol);
354 wprintw(win, "Cursor: %d,%d", currow, curcol);
355 if (data_ahead(form))
356 waddstr(win, " ahead");
357 if (data_behind(form))
358 waddstr(win, " behind");
359 waddch(win, '\n');
360 if ((field = current_field(form)) != 0) {
361 wprintw(win, "Page %d%s, Field %d/%d%s:",
362 form_page(form),
363 new_page(field) ? "*" : "",
364 field_index(field), field_count(form),
365 field_arg(field) ? "(arg)" : "");
366 if ((type = field_type(field)) != 0) {
367 if (type == TYPE_ALNUM)
368 waddstr(win, "ALNUM");
369 else if (type == TYPE_ALPHA)
370 waddstr(win, "ALPHA");
371 else if (type == TYPE_ENUM)
372 waddstr(win, "ENUM");
373 else if (type == TYPE_INTEGER)
374 waddstr(win, "INTEGER");
375#ifdef NCURSES_VERSION
376 else if (type == TYPE_IPV4)
377 waddstr(win, "IPV4");
378#endif
379 else if (type == TYPE_NUMERIC)
380 waddstr(win, "NUMERIC");
381 else if (type == TYPE_REGEXP)
382 waddstr(win, "REGEXP");
383 else
384 waddstr(win, "other");
385 }
386
387 if ((unsigned) field_opts(field) & O_EDIT)
388 waddstr(win, " editable");
389 else
390 waddstr(win, " readonly");
391
392 if (field_status(field))
393 waddstr(win, " modified");
394
395 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
396 != ERR) {
397 wprintw(win, " size %dx%d (max %d)",
398 field_rows, field_cols, field_max);
399 }
400
401 waddch(win, ' ');
402 (void) wattrset(win, AttrArg(field_fore(field), 0));
403 waddstr(win, "fore");
404 wattroff(win, (int) field_fore(field));
405
406 waddch(win, '/');
407
408 (void) wattrset(win, AttrArg(field_back(field), 0));
409 waddstr(win, "back");
410 wattroff(win, (int) field_back(field));
411
412 wprintw(win, ", pad '%c'", field_pad(field));
413
414 waddstr(win, "\n");
415 for (nbuf = 0; nbuf <= 2; ++nbuf) {
416 if ((buffer = field_buffer(field, nbuf)) != 0) {
417 wprintw(win, "buffer %d:", nbuf);
418 (void) wattrset(win, A_REVERSE);
419 if (nbuf) {
420 waddnstr(win, buffer, trimmed(buffer));
421 } else {
422 waddstr(win, buffer);
423 }
424 wattroff(win, A_REVERSE);
425 waddstr(win, "\n");
426 }
427 }
428 }
429 wrefresh(win);
430}
431
432static void
433demo_forms(void)
434{
435 WINDOW *w;
436 FORM *form;
437 FIELD *f[100]; /* will memset to zero */
438 int finished = 0, c;
439 unsigned n = 0;
440 int pg;
441 WINDOW *also;
442 const char *fname;
443
444#ifdef NCURSES_MOUSE_VERSION
445 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
446#endif
447
448 help_edit_field();
449
450 MvAddStr(4, 57, "Forms Entry Test");
451 show_insert_mode(TRUE);
452
453 refresh();
454
455 /* describe the form */
456 memset(f, 0, sizeof(f));
457 for (pg = 0; pg < 4; ++pg) {
458 char label[80];
459 sprintf(label, "Sample Form Page %d", pg + 1);
460 f[n++] = make_label(label, 0, 15);
461 set_new_page(f[n - 1], TRUE);
462
463 switch (pg) {
464 default:
465 fname = "Last Name";
466 f[n++] = make_label(fname, 2, 0);
467 f[n++] = make_field(fname, 3, 0, 1, 18);
468 set_field_type(f[n - 1], TYPE_ALPHA, 1);
469
470 fname = "First Name";
471 f[n++] = make_label(fname, 2, 20);
472 f[n++] = make_field(fname, 3, 20, 1, 12);
473 set_field_type(f[n - 1], TYPE_ALPHA, 1);
474
475 fname = "Middle Name";
476 f[n++] = make_label(fname, 2, 34);
477 f[n++] = make_field(fname, 3, 34, 1, 12);
478 set_field_type(f[n - 1], TYPE_ALPHA, 1);
479 break;
480 case 1:
481 fname = "Last Name";
482 f[n++] = make_label(fname, 2, 0);
483 f[n++] = make_field(fname, 3, 0, 1, 18);
484 set_field_type(f[n - 1], TYPE_ALPHA, 1);
485
486 fname = "First Name";
487 f[n++] = make_label(fname, 2, 20);
488 f[n++] = make_field(fname, 3, 20, 1, 12);
489 set_field_type(f[n - 1], TYPE_ALPHA, 1);
490
491 fname = "MI";
492 f[n++] = make_label(fname, 2, 34);
493 f[n++] = make_field(fname, 3, 34, 1, 1);
494 set_field_pad(f[n - 1], '?');
495 set_field_type(f[n - 1], TYPE_ALPHA, 1);
496 break;
497 case 2:
498 fname = "Host Name";
499 f[n++] = make_label(fname, 2, 0);
500 f[n++] = make_field(fname, 3, 0, 1, 24);
501 set_field_type(f[n - 1], TYPE_ALNUM, 1);
502
503#ifdef NCURSES_VERSION
504 fname = "IP Address";
505 f[n++] = make_label(fname, 2, 26);
506 f[n++] = make_field(fname, 3, 26, 1, 16);
507 set_field_type(f[n - 1], TYPE_IPV4, 1);
508#endif
509
510 break;
511
512 case 3:
513 fname = "Four digits";
514 f[n++] = make_label(fname, 2, 0);
515 f[n++] = make_field(fname, 3, 0, 1, 18);
516 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
517
518 fname = "Numeric";
519 f[n++] = make_label(fname, 2, 20);
520 f[n++] = make_field(fname, 3, 20, 1, 12);
521 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
522
523 break;
524 }
525
526 fname = "Comments";
527 f[n++] = make_label(fname, 5, 0);
528 f[n++] = make_field(fname, 6, 0, 4, 46);
529 init_edit_field(f[n - 1], get_data(fname));
530 }
531
532 f[n] = (FIELD *) 0;
533
534 if ((form = new_form(f)) != 0) {
535
536 display_form(form);
537
538 w = form_win(form);
539 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
540 show_current_field(also, form);
541
542 while (!finished) {
543 switch (edit_field(form, &c)) {
544 case E_OK:
545 break;
546 case E_UNKNOWN_COMMAND:
547 finished = my_form_driver(form, c);
548 break;
549 default:
550 beep();
551 break;
552 }
553 show_current_field(also, form);
554 }
555
556 erase_form(form);
557
558 free_form(form);
559 }
560 for (c = 0; f[c] != 0; c++) {
561 void *ptr = field_userptr(f[c]);
562 free(ptr);
563 free_field(f[c]);
564 }
565 noraw();
566 nl();
567
568#ifdef NCURSES_MOUSE_VERSION
569 mousemask(0, (mmask_t *) 0);
570#endif
571}
572
573static void
574usage(void)
575{
576 static const char *tbl[] =
577 {
578 "Usage: demo_forms [options] [data file]"
579 ,""
580 ," -d make fields dynamic"
581 ," -j value justify (1=left, 2=center, 3=right)"
582 ," -m value set maximum size of dynamic fields"
583 ," -o value specify number of offscreen rows in new_field()"
584 ," -t value specify text to fill fields initially"
585 };
586 unsigned int j;
587 for (j = 0; j < SIZEOF(tbl); ++j)
588 fprintf(stderr, "%s\n", tbl[j]);
589 exit(EXIT_FAILURE);
590}
591
592int
593main(int argc, char *argv[])
594{
595 int ch;
596
597 setlocale(LC_ALL, "");
598
599 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
600 switch (ch) {
601 case 'd':
602 d_option = TRUE;
603 break;
604 case 'j':
605 j_value = atoi(optarg);
606 if (j_value < NO_JUSTIFICATION
607 || j_value > JUSTIFY_RIGHT)
608 usage();
609 break;
610 case 'm':
611 m_value = atoi(optarg);
612 break;
613 case 'o':
614 o_value = atoi(optarg);
615 break;
616 case 't':
617 t_value = optarg;
618 break;
619 default:
620 usage();
621
622 }
623 }
624 while (optind < argc) {
625 read_data(argv[optind++]);
626 }
627
628 initscr();
629 cbreak();
630 noecho();
631 raw();
632 nonl(); /* lets us read ^M's */
633 intrflush(stdscr, FALSE);
634 keypad(stdscr, TRUE);
635
636 if (has_colors()) {
637 start_color();
638 init_pair(1, COLOR_WHITE, COLOR_BLUE);
639 init_pair(2, COLOR_GREEN, COLOR_BLACK);
640 init_pair(3, COLOR_CYAN, COLOR_BLACK);
641 bkgd((chtype) COLOR_PAIR(1));
642 refresh();
643 }
644
645 demo_forms();
646
647 endwin();
648 ExitProgram(EXIT_SUCCESS);
649}
650
651#else
652int
653main(void)
654{
655 printf("This program requires the curses form library\n");
656 ExitProgram(EXIT_FAILURE);
657}
658#endif