mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.1 KiB

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