1
0

util.c 17 KB

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