util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * util.c
  3. *
  4. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <stdarg.h>
  10. #include "dialog.h"
  11. /* Needed in signal handler in mconf.c */
  12. int saved_x, saved_y;
  13. struct dialog_info dlg;
  14. static void set_mono_theme(void)
  15. {
  16. dlg.screen.atr = A_NORMAL;
  17. dlg.shadow.atr = A_NORMAL;
  18. dlg.dialog.atr = A_NORMAL;
  19. dlg.title.atr = A_BOLD;
  20. dlg.border.atr = A_NORMAL;
  21. dlg.button_active.atr = A_REVERSE;
  22. dlg.button_inactive.atr = A_DIM;
  23. dlg.button_key_active.atr = A_REVERSE;
  24. dlg.button_key_inactive.atr = A_BOLD;
  25. dlg.button_label_active.atr = A_REVERSE;
  26. dlg.button_label_inactive.atr = A_NORMAL;
  27. dlg.inputbox.atr = A_NORMAL;
  28. dlg.inputbox_border.atr = A_NORMAL;
  29. dlg.searchbox.atr = A_NORMAL;
  30. dlg.searchbox_title.atr = A_BOLD;
  31. dlg.searchbox_border.atr = A_NORMAL;
  32. dlg.position_indicator.atr = A_BOLD;
  33. dlg.menubox.atr = A_NORMAL;
  34. dlg.menubox_border.atr = A_NORMAL;
  35. dlg.item.atr = A_NORMAL;
  36. dlg.item_selected.atr = A_REVERSE;
  37. dlg.tag.atr = A_BOLD;
  38. dlg.tag_selected.atr = A_REVERSE;
  39. dlg.tag_key.atr = A_BOLD;
  40. dlg.tag_key_selected.atr = A_REVERSE;
  41. dlg.check.atr = A_BOLD;
  42. dlg.check_selected.atr = A_REVERSE;
  43. dlg.uarrow.atr = A_BOLD;
  44. dlg.darrow.atr = A_BOLD;
  45. }
  46. #define DLG_COLOR(dialog, f, b, h) \
  47. do { \
  48. dlg.dialog.fg = (f); \
  49. dlg.dialog.bg = (b); \
  50. dlg.dialog.hl = (h); \
  51. } while (0)
  52. static void set_classic_theme(void)
  53. {
  54. DLG_COLOR(screen, COLOR_CYAN, COLOR_BLUE, true);
  55. DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, true);
  56. DLG_COLOR(dialog, COLOR_BLACK, COLOR_WHITE, false);
  57. DLG_COLOR(title, COLOR_YELLOW, COLOR_WHITE, true);
  58. DLG_COLOR(border, COLOR_WHITE, COLOR_WHITE, true);
  59. DLG_COLOR(button_active, COLOR_WHITE, COLOR_BLUE, true);
  60. DLG_COLOR(button_inactive, COLOR_BLACK, COLOR_WHITE, false);
  61. DLG_COLOR(button_key_active, COLOR_WHITE, COLOR_BLUE, true);
  62. DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_WHITE, false);
  63. DLG_COLOR(button_label_active, COLOR_YELLOW, COLOR_BLUE, true);
  64. DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_WHITE, true);
  65. DLG_COLOR(inputbox, COLOR_BLACK, COLOR_WHITE, false);
  66. DLG_COLOR(inputbox_border, COLOR_BLACK, COLOR_WHITE, false);
  67. DLG_COLOR(searchbox, COLOR_BLACK, COLOR_WHITE, false);
  68. DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_WHITE, true);
  69. DLG_COLOR(searchbox_border, COLOR_WHITE, COLOR_WHITE, true);
  70. DLG_COLOR(position_indicator, COLOR_YELLOW, COLOR_WHITE, true);
  71. DLG_COLOR(menubox, COLOR_BLACK, COLOR_WHITE, false);
  72. DLG_COLOR(menubox_border, COLOR_WHITE, COLOR_WHITE, true);
  73. DLG_COLOR(item, COLOR_BLACK, COLOR_WHITE, false);
  74. DLG_COLOR(item_selected, COLOR_WHITE, COLOR_BLUE, true);
  75. DLG_COLOR(tag, COLOR_YELLOW, COLOR_WHITE, true);
  76. DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_BLUE, true);
  77. DLG_COLOR(tag_key, COLOR_YELLOW, COLOR_WHITE, true);
  78. DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_BLUE, true);
  79. DLG_COLOR(check, COLOR_BLACK, COLOR_WHITE, false);
  80. DLG_COLOR(check_selected, COLOR_WHITE, COLOR_BLUE, true);
  81. DLG_COLOR(uarrow, COLOR_GREEN, COLOR_WHITE, true);
  82. DLG_COLOR(darrow, COLOR_GREEN, COLOR_WHITE, true);
  83. }
  84. static void set_blackbg_theme(void)
  85. {
  86. DLG_COLOR(screen, COLOR_RED, COLOR_BLACK, true);
  87. DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, false);
  88. DLG_COLOR(dialog, COLOR_WHITE, COLOR_BLACK, false);
  89. DLG_COLOR(title, COLOR_RED, COLOR_BLACK, false);
  90. DLG_COLOR(border, COLOR_BLACK, COLOR_BLACK, true);
  91. DLG_COLOR(button_active, COLOR_YELLOW, COLOR_RED, false);
  92. DLG_COLOR(button_inactive, COLOR_YELLOW, COLOR_BLACK, false);
  93. DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_RED, true);
  94. DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_BLACK, false);
  95. DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_RED, false);
  96. DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_BLACK, true);
  97. DLG_COLOR(inputbox, COLOR_YELLOW, COLOR_BLACK, false);
  98. DLG_COLOR(inputbox_border, COLOR_YELLOW, COLOR_BLACK, false);
  99. DLG_COLOR(searchbox, COLOR_YELLOW, COLOR_BLACK, false);
  100. DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_BLACK, true);
  101. DLG_COLOR(searchbox_border, COLOR_BLACK, COLOR_BLACK, true);
  102. DLG_COLOR(position_indicator, COLOR_RED, COLOR_BLACK, false);
  103. DLG_COLOR(menubox, COLOR_YELLOW, COLOR_BLACK, false);
  104. DLG_COLOR(menubox_border, COLOR_BLACK, COLOR_BLACK, true);
  105. DLG_COLOR(item, COLOR_WHITE, COLOR_BLACK, false);
  106. DLG_COLOR(item_selected, COLOR_WHITE, COLOR_RED, false);
  107. DLG_COLOR(tag, COLOR_RED, COLOR_BLACK, false);
  108. DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_RED, true);
  109. DLG_COLOR(tag_key, COLOR_RED, COLOR_BLACK, false);
  110. DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_RED, true);
  111. DLG_COLOR(check, COLOR_YELLOW, COLOR_BLACK, false);
  112. DLG_COLOR(check_selected, COLOR_YELLOW, COLOR_RED, true);
  113. DLG_COLOR(uarrow, COLOR_RED, COLOR_BLACK, false);
  114. DLG_COLOR(darrow, COLOR_RED, COLOR_BLACK, false);
  115. }
  116. static void set_bluetitle_theme(void)
  117. {
  118. set_classic_theme();
  119. DLG_COLOR(title, COLOR_BLUE, COLOR_WHITE, true);
  120. DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_BLUE, true);
  121. DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_BLUE, true);
  122. DLG_COLOR(searchbox_title, COLOR_BLUE, COLOR_WHITE, true);
  123. DLG_COLOR(position_indicator, COLOR_BLUE, COLOR_WHITE, true);
  124. DLG_COLOR(tag, COLOR_BLUE, COLOR_WHITE, true);
  125. DLG_COLOR(tag_key, COLOR_BLUE, COLOR_WHITE, true);
  126. }
  127. /*
  128. * Select color theme
  129. */
  130. static int set_theme(const char *theme)
  131. {
  132. int use_color = 1;
  133. if (!theme)
  134. set_bluetitle_theme();
  135. else if (strcmp(theme, "classic") == 0)
  136. set_classic_theme();
  137. else if (strcmp(theme, "bluetitle") == 0)
  138. set_bluetitle_theme();
  139. else if (strcmp(theme, "blackbg") == 0)
  140. set_blackbg_theme();
  141. else if (strcmp(theme, "mono") == 0)
  142. use_color = 0;
  143. return use_color;
  144. }
  145. static void init_one_color(struct dialog_color *color)
  146. {
  147. static int pair = 0;
  148. pair++;
  149. init_pair(pair, color->fg, color->bg);
  150. if (color->hl)
  151. color->atr = A_BOLD | COLOR_PAIR(pair);
  152. else
  153. color->atr = COLOR_PAIR(pair);
  154. }
  155. static void init_dialog_colors(void)
  156. {
  157. init_one_color(&dlg.screen);
  158. init_one_color(&dlg.shadow);
  159. init_one_color(&dlg.dialog);
  160. init_one_color(&dlg.title);
  161. init_one_color(&dlg.border);
  162. init_one_color(&dlg.button_active);
  163. init_one_color(&dlg.button_inactive);
  164. init_one_color(&dlg.button_key_active);
  165. init_one_color(&dlg.button_key_inactive);
  166. init_one_color(&dlg.button_label_active);
  167. init_one_color(&dlg.button_label_inactive);
  168. init_one_color(&dlg.inputbox);
  169. init_one_color(&dlg.inputbox_border);
  170. init_one_color(&dlg.searchbox);
  171. init_one_color(&dlg.searchbox_title);
  172. init_one_color(&dlg.searchbox_border);
  173. init_one_color(&dlg.position_indicator);
  174. init_one_color(&dlg.menubox);
  175. init_one_color(&dlg.menubox_border);
  176. init_one_color(&dlg.item);
  177. init_one_color(&dlg.item_selected);
  178. init_one_color(&dlg.tag);
  179. init_one_color(&dlg.tag_selected);
  180. init_one_color(&dlg.tag_key);
  181. init_one_color(&dlg.tag_key_selected);
  182. init_one_color(&dlg.check);
  183. init_one_color(&dlg.check_selected);
  184. init_one_color(&dlg.uarrow);
  185. init_one_color(&dlg.darrow);
  186. }
  187. /*
  188. * Setup for color display
  189. */
  190. static void color_setup(const char *theme)
  191. {
  192. int use_color;
  193. use_color = set_theme(theme);
  194. if (use_color && has_colors()) {
  195. start_color();
  196. init_dialog_colors();
  197. } else
  198. set_mono_theme();
  199. }
  200. /*
  201. * Set window to attribute 'attr'
  202. */
  203. void attr_clear(WINDOW * win, int height, int width, chtype attr)
  204. {
  205. int i, j;
  206. wattrset(win, attr);
  207. for (i = 0; i < height; i++) {
  208. wmove(win, i, 0);
  209. for (j = 0; j < width; j++)
  210. waddch(win, ' ');
  211. }
  212. touchwin(win);
  213. }
  214. void dialog_clear(void)
  215. {
  216. int lines, columns;
  217. lines = getmaxy(stdscr);
  218. columns = getmaxx(stdscr);
  219. attr_clear(stdscr, lines, columns, dlg.screen.atr);
  220. /* Display background title if it exists ... - SLH */
  221. if (dlg.backtitle != NULL) {
  222. int i, len = 0, skip = 0;
  223. struct subtitle_list *pos;
  224. wattrset(stdscr, dlg.screen.atr);
  225. mvwaddstr(stdscr, 0, 1, (char *)dlg.backtitle);
  226. for (pos = dlg.subtitles; pos != NULL; pos = pos->next) {
  227. /* 3 is for the arrow and spaces */
  228. len += strlen(pos->text) + 3;
  229. }
  230. wmove(stdscr, 1, 1);
  231. if (len > columns - 2) {
  232. const char *ellipsis = "[...] ";
  233. waddstr(stdscr, ellipsis);
  234. skip = len - (columns - 2 - strlen(ellipsis));
  235. }
  236. for (pos = dlg.subtitles; pos != NULL; pos = pos->next) {
  237. if (skip == 0)
  238. waddch(stdscr, ACS_RARROW);
  239. else
  240. skip--;
  241. if (skip == 0)
  242. waddch(stdscr, ' ');
  243. else
  244. skip--;
  245. if (skip < strlen(pos->text)) {
  246. waddstr(stdscr, pos->text + skip);
  247. skip = 0;
  248. } else
  249. skip -= strlen(pos->text);
  250. if (skip == 0)
  251. waddch(stdscr, ' ');
  252. else
  253. skip--;
  254. }
  255. for (i = len + 1; i < columns - 1; i++)
  256. waddch(stdscr, ACS_HLINE);
  257. }
  258. wnoutrefresh(stdscr);
  259. }
  260. /*
  261. * Do some initialization for dialog
  262. */
  263. int init_dialog(const char *backtitle)
  264. {
  265. int height, width;
  266. initscr(); /* Init curses */
  267. /* Get current cursor position for signal handler in mconf.c */
  268. getyx(stdscr, saved_y, saved_x);
  269. getmaxyx(stdscr, height, width);
  270. if (height < WINDOW_HEIGTH_MIN || width < WINDOW_WIDTH_MIN) {
  271. endwin();
  272. return -ERRDISPLAYTOOSMALL;
  273. }
  274. dlg.backtitle = backtitle;
  275. color_setup(getenv("MENUCONFIG_COLOR"));
  276. keypad(stdscr, TRUE);
  277. cbreak();
  278. noecho();
  279. dialog_clear();
  280. return 0;
  281. }
  282. void set_dialog_backtitle(const char *backtitle)
  283. {
  284. dlg.backtitle = backtitle;
  285. }
  286. void set_dialog_subtitles(struct subtitle_list *subtitles)
  287. {
  288. dlg.subtitles = subtitles;
  289. }
  290. /*
  291. * End using dialog functions.
  292. */
  293. void end_dialog(int x, int y)
  294. {
  295. /* move cursor back to original position */
  296. move(y, x);
  297. refresh();
  298. endwin();
  299. }
  300. /* Print the title of the dialog. Center the title and truncate
  301. * tile if wider than dialog (- 2 chars).
  302. **/
  303. void print_title(WINDOW *dialog, const char *title, int width)
  304. {
  305. if (title) {
  306. int tlen = MIN(width - 2, strlen(title));
  307. wattrset(dialog, dlg.title.atr);
  308. mvwaddch(dialog, 0, (width - tlen) / 2 - 1, ' ');
  309. mvwaddnstr(dialog, 0, (width - tlen)/2, title, tlen);
  310. waddch(dialog, ' ');
  311. }
  312. }
  313. /*
  314. * Print a string of text in a window, automatically wrap around to the
  315. * next line if the string is too long to fit on one line. Newline
  316. * characters '\n' are propperly processed. We start on a new line
  317. * if there is no room for at least 4 nonblanks following a double-space.
  318. */
  319. void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
  320. {
  321. int newl, cur_x, cur_y;
  322. int prompt_len, room, wlen;
  323. char tempstr[MAX_LEN + 1], *word, *sp, *sp2, *newline_separator = 0;
  324. strcpy(tempstr, prompt);
  325. prompt_len = strlen(tempstr);
  326. if (prompt_len <= width - x * 2) { /* If prompt is short */
  327. wmove(win, y, (width - prompt_len) / 2);
  328. waddstr(win, tempstr);
  329. } else {
  330. cur_x = x;
  331. cur_y = y;
  332. newl = 1;
  333. word = tempstr;
  334. while (word && *word) {
  335. sp = strpbrk(word, "\n ");
  336. if (sp && *sp == '\n')
  337. newline_separator = sp;
  338. if (sp)
  339. *sp++ = 0;
  340. /* Wrap to next line if either the word does not fit,
  341. or it is the first word of a new sentence, and it is
  342. short, and the next word does not fit. */
  343. room = width - cur_x;
  344. wlen = strlen(word);
  345. if (wlen > room ||
  346. (newl && wlen < 4 && sp
  347. && wlen + 1 + strlen(sp) > room
  348. && (!(sp2 = strpbrk(sp, "\n "))
  349. || wlen + 1 + (sp2 - sp) > room))) {
  350. cur_y++;
  351. cur_x = x;
  352. }
  353. wmove(win, cur_y, cur_x);
  354. waddstr(win, word);
  355. getyx(win, cur_y, cur_x);
  356. /* Move to the next line if the word separator was a newline */
  357. if (newline_separator) {
  358. cur_y++;
  359. cur_x = x;
  360. newline_separator = 0;
  361. } else
  362. cur_x++;
  363. if (sp && *sp == ' ') {
  364. cur_x++; /* double space */
  365. while (*++sp == ' ') ;
  366. newl = 1;
  367. } else
  368. newl = 0;
  369. word = sp;
  370. }
  371. }
  372. }
  373. /*
  374. * Print a button
  375. */
  376. void print_button(WINDOW * win, const char *label, int y, int x, int selected)
  377. {
  378. int i, temp;
  379. wmove(win, y, x);
  380. wattrset(win, selected ? dlg.button_active.atr
  381. : dlg.button_inactive.atr);
  382. waddstr(win, "<");
  383. temp = strspn(label, " ");
  384. label += temp;
  385. wattrset(win, selected ? dlg.button_label_active.atr
  386. : dlg.button_label_inactive.atr);
  387. for (i = 0; i < temp; i++)
  388. waddch(win, ' ');
  389. wattrset(win, selected ? dlg.button_key_active.atr
  390. : dlg.button_key_inactive.atr);
  391. waddch(win, label[0]);
  392. wattrset(win, selected ? dlg.button_label_active.atr
  393. : dlg.button_label_inactive.atr);
  394. waddstr(win, (char *)label + 1);
  395. wattrset(win, selected ? dlg.button_active.atr
  396. : dlg.button_inactive.atr);
  397. waddstr(win, ">");
  398. wmove(win, y, x + temp + 1);
  399. }
  400. /*
  401. * Draw a rectangular box with line drawing characters
  402. */
  403. void
  404. draw_box(WINDOW * win, int y, int x, int height, int width,
  405. chtype box, chtype border)
  406. {
  407. int i, j;
  408. wattrset(win, 0);
  409. for (i = 0; i < height; i++) {
  410. wmove(win, y + i, x);
  411. for (j = 0; j < width; j++)
  412. if (!i && !j)
  413. waddch(win, border | ACS_ULCORNER);
  414. else if (i == height - 1 && !j)
  415. waddch(win, border | ACS_LLCORNER);
  416. else if (!i && j == width - 1)
  417. waddch(win, box | ACS_URCORNER);
  418. else if (i == height - 1 && j == width - 1)
  419. waddch(win, box | ACS_LRCORNER);
  420. else if (!i)
  421. waddch(win, border | ACS_HLINE);
  422. else if (i == height - 1)
  423. waddch(win, box | ACS_HLINE);
  424. else if (!j)
  425. waddch(win, border | ACS_VLINE);
  426. else if (j == width - 1)
  427. waddch(win, box | ACS_VLINE);
  428. else
  429. waddch(win, box | ' ');
  430. }
  431. }
  432. /*
  433. * Draw shadows along the right and bottom edge to give a more 3D look
  434. * to the boxes
  435. */
  436. void draw_shadow(WINDOW * win, int y, int x, int height, int width)
  437. {
  438. int i;
  439. if (has_colors()) { /* Whether terminal supports color? */
  440. wattrset(win, dlg.shadow.atr);
  441. wmove(win, y + height, x + 2);
  442. for (i = 0; i < width; i++)
  443. waddch(win, winch(win) & A_CHARTEXT);
  444. for (i = y + 1; i < y + height + 1; i++) {
  445. wmove(win, i, x + width);
  446. waddch(win, winch(win) & A_CHARTEXT);
  447. waddch(win, winch(win) & A_CHARTEXT);
  448. }
  449. wnoutrefresh(win);
  450. }
  451. }
  452. /*
  453. * Return the position of the first alphabetic character in a string.
  454. */
  455. int first_alpha(const char *string, const char *exempt)
  456. {
  457. int i, in_paren = 0, c;
  458. for (i = 0; i < strlen(string); i++) {
  459. c = tolower(string[i]);
  460. if (strchr("<[(", c))
  461. ++in_paren;
  462. if (strchr(">])", c) && in_paren > 0)
  463. --in_paren;
  464. if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
  465. return i;
  466. }
  467. return 0;
  468. }
  469. /*
  470. * ncurses uses ESC to detect escaped char sequences. This resutl in
  471. * a small timeout before ESC is actually delivered to the application.
  472. * lxdialog suggest <ESC> <ESC> which is correctly translated to two
  473. * times esc. But then we need to ignore the second esc to avoid stepping
  474. * out one menu too much. Filter away all escaped key sequences since
  475. * keypad(FALSE) turn off ncurses support for escape sequences - and thats
  476. * needed to make notimeout() do as expected.
  477. */
  478. int on_key_esc(WINDOW *win)
  479. {
  480. int key;
  481. int key2;
  482. int key3;
  483. nodelay(win, TRUE);
  484. keypad(win, FALSE);
  485. key = wgetch(win);
  486. key2 = wgetch(win);
  487. do {
  488. key3 = wgetch(win);
  489. } while (key3 != ERR);
  490. nodelay(win, FALSE);
  491. keypad(win, TRUE);
  492. if (key == KEY_ESC && key2 == ERR)
  493. return KEY_ESC;
  494. else if (key != ERR && key != KEY_ESC && key2 == ERR)
  495. ungetch(key);
  496. return -1;
  497. }
  498. /* redraw screen in new size */
  499. int on_key_resize(void)
  500. {
  501. dialog_clear();
  502. return KEY_RESIZE;
  503. }
  504. struct dialog_list *item_cur;
  505. struct dialog_list item_nil;
  506. struct dialog_list *item_head;
  507. void item_reset(void)
  508. {
  509. struct dialog_list *p, *next;
  510. for (p = item_head; p; p = next) {
  511. next = p->next;
  512. free(p);
  513. }
  514. item_head = NULL;
  515. item_cur = &item_nil;
  516. }
  517. void item_make(const char *fmt, ...)
  518. {
  519. va_list ap;
  520. struct dialog_list *p = malloc(sizeof(*p));
  521. if (item_head)
  522. item_cur->next = p;
  523. else
  524. item_head = p;
  525. item_cur = p;
  526. memset(p, 0, sizeof(*p));
  527. va_start(ap, fmt);
  528. vsnprintf(item_cur->node.str, sizeof(item_cur->node.str), fmt, ap);
  529. va_end(ap);
  530. }
  531. void item_add_str(const char *fmt, ...)
  532. {
  533. va_list ap;
  534. size_t avail;
  535. avail = sizeof(item_cur->node.str) - strlen(item_cur->node.str);
  536. va_start(ap, fmt);
  537. vsnprintf(item_cur->node.str + strlen(item_cur->node.str),
  538. avail, fmt, ap);
  539. item_cur->node.str[sizeof(item_cur->node.str) - 1] = '\0';
  540. va_end(ap);
  541. }
  542. void item_set_tag(char tag)
  543. {
  544. item_cur->node.tag = tag;
  545. }
  546. void item_set_data(void *ptr)
  547. {
  548. item_cur->node.data = ptr;
  549. }
  550. void item_set_selected(int val)
  551. {
  552. item_cur->node.selected = val;
  553. }
  554. int item_activate_selected(void)
  555. {
  556. item_foreach()
  557. if (item_is_selected())
  558. return 1;
  559. return 0;
  560. }
  561. void *item_data(void)
  562. {
  563. return item_cur->node.data;
  564. }
  565. char item_tag(void)
  566. {
  567. return item_cur->node.tag;
  568. }
  569. int item_count(void)
  570. {
  571. int n = 0;
  572. struct dialog_list *p;
  573. for (p = item_head; p; p = p->next)
  574. n++;
  575. return n;
  576. }
  577. void item_set(int n)
  578. {
  579. int i = 0;
  580. item_foreach()
  581. if (i++ == n)
  582. return;
  583. }
  584. int item_n(void)
  585. {
  586. int n = 0;
  587. struct dialog_list *p;
  588. for (p = item_head; p; p = p->next) {
  589. if (p == item_cur)
  590. return n;
  591. n++;
  592. }
  593. return 0;
  594. }
  595. const char *item_str(void)
  596. {
  597. return item_cur->node.str;
  598. }
  599. int item_is_selected(void)
  600. {
  601. return (item_cur->node.selected != 0);
  602. }
  603. int item_is_tag(char tag)
  604. {
  605. return (item_cur->node.tag == tag);
  606. }