demo_forms.c 15 KB

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