lxdialog.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * dialog - Display simple dialog boxes from shell scripts
  3. *
  4. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "dialog.h"
  22. static void Usage (const char *name);
  23. typedef int (jumperFn) (const char *title, int argc, const char * const * argv);
  24. struct Mode {
  25. char *name;
  26. int argmin, argmax, argmod;
  27. jumperFn *jumper;
  28. };
  29. jumperFn j_menu, j_checklist, j_radiolist, j_yesno, j_textbox, j_inputbox;
  30. jumperFn j_msgbox, j_infobox;
  31. static struct Mode modes[] =
  32. {
  33. {"--menu", 9, 0, 3, j_menu},
  34. {"--checklist", 9, 0, 3, j_checklist},
  35. {"--radiolist", 9, 0, 3, j_radiolist},
  36. {"--yesno", 5,5,1, j_yesno},
  37. {"--textbox", 5,5,1, j_textbox},
  38. {"--inputbox", 5, 6, 1, j_inputbox},
  39. {"--msgbox", 5, 5, 1, j_msgbox},
  40. {"--infobox", 5, 5, 1, j_infobox},
  41. {NULL, 0, 0, 0, NULL}
  42. };
  43. static struct Mode *modePtr;
  44. #ifdef LOCALE
  45. #include <locale.h>
  46. #endif
  47. int
  48. main (int argc, const char * const * argv)
  49. {
  50. int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
  51. const char *title = NULL;
  52. #ifdef LOCALE
  53. (void) setlocale (LC_ALL, "");
  54. #endif
  55. #ifdef TRACE
  56. trace(TRACE_CALLS|TRACE_UPDATE);
  57. #endif
  58. if (argc < 2) {
  59. Usage (argv[0]);
  60. exit (-1);
  61. }
  62. while (offset < argc - 1 && !end_common_opts) { /* Common options */
  63. if (!strcmp (argv[offset + 1], "--title")) {
  64. if (argc - offset < 3 || title != NULL) {
  65. Usage (argv[0]);
  66. exit (-1);
  67. } else {
  68. title = argv[offset + 2];
  69. offset += 2;
  70. }
  71. } else if (!strcmp (argv[offset + 1], "--backtitle")) {
  72. if (backtitle != NULL) {
  73. Usage (argv[0]);
  74. exit (-1);
  75. } else {
  76. backtitle = argv[offset + 2];
  77. offset += 2;
  78. }
  79. } else if (!strcmp (argv[offset + 1], "--clear")) {
  80. if (clear_screen) { /* Hey, "--clear" can't appear twice! */
  81. Usage (argv[0]);
  82. exit (-1);
  83. } else if (argc == 2) { /* we only want to clear the screen */
  84. init_dialog ();
  85. refresh (); /* init_dialog() will clear the screen for us */
  86. end_dialog ();
  87. return 0;
  88. } else {
  89. clear_screen = 1;
  90. offset++;
  91. }
  92. } else /* no more common options */
  93. end_common_opts = 1;
  94. }
  95. if (argc - 1 == offset) { /* no more options */
  96. Usage (argv[0]);
  97. exit (-1);
  98. }
  99. /* use a table to look for the requested mode, to avoid code duplication */
  100. for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
  101. if (!strcmp (argv[offset + 1], modePtr->name))
  102. break;
  103. if (!modePtr->name)
  104. Usage (argv[0]);
  105. if (argc - offset < modePtr->argmin)
  106. Usage (argv[0]);
  107. if (modePtr->argmax && argc - offset > modePtr->argmax)
  108. Usage (argv[0]);
  109. init_dialog ();
  110. retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
  111. if (clear_screen) { /* clear screen before exit */
  112. attr_clear (stdscr, LINES, COLS, screen_attr);
  113. refresh ();
  114. }
  115. end_dialog();
  116. exit (retval);
  117. }
  118. /*
  119. * Print program usage
  120. */
  121. static void
  122. Usage (const char *name)
  123. {
  124. fprintf (stderr, "\
  125. \ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
  126. \n patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
  127. \n modified/gutted for use as a Linux kernel config tool by \
  128. \n William Roadcap (roadcapw@cfw.com)\
  129. \n\
  130. \n* Display dialog boxes from shell scripts *\
  131. \n\
  132. \nUsage: %s --clear\
  133. \n %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
  134. \n\
  135. \nBox options:\
  136. \n\
  137. \n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
  138. \n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
  139. \n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
  140. \n --textbox <file> <height> <width>\
  141. \n --inputbox <text> <height> <width> [<init>]\
  142. \n --yesno <text> <height> <width>\
  143. \n", name, name);
  144. exit (-1);
  145. }
  146. /*
  147. * These are the program jumpers
  148. */
  149. int
  150. j_menu (const char *t, int ac, const char * const * av)
  151. {
  152. return dialog_menu (t, av[2], atoi (av[3]), atoi (av[4]),
  153. atoi (av[5]), av[6], (ac - 6) / 2, av + 7);
  154. }
  155. int
  156. j_checklist (const char *t, int ac, const char * const * av)
  157. {
  158. return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
  159. atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK);
  160. }
  161. int
  162. j_radiolist (const char *t, int ac, const char * const * av)
  163. {
  164. return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
  165. atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO);
  166. }
  167. int
  168. j_textbox (const char *t, int ac, const char * const * av)
  169. {
  170. return dialog_textbox (t, av[2], atoi (av[3]), atoi (av[4]));
  171. }
  172. int
  173. j_yesno (const char *t, int ac, const char * const * av)
  174. {
  175. return dialog_yesno (t, av[2], atoi (av[3]), atoi (av[4]));
  176. }
  177. int
  178. j_inputbox (const char *t, int ac, const char * const * av)
  179. {
  180. int ret = dialog_inputbox (t, av[2], atoi (av[3]), atoi (av[4]),
  181. ac == 6 ? av[5] : (char *) NULL);
  182. if (ret == 0)
  183. fprintf(stderr, dialog_input_result);
  184. return ret;
  185. }
  186. int
  187. j_msgbox (const char *t, int ac, const char * const * av)
  188. {
  189. return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 1);
  190. }
  191. int
  192. j_infobox (const char *t, int ac, const char * const * av)
  193. {
  194. return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 0);
  195. }