lxdialog.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_radiolist, j_yesno, j_textbox, j_inputbox;
  30. jumperFn j_msgbox, j_infobox;
  31. static struct Mode modes[] = {
  32. {"--menu", 9, 0, 3, j_menu},
  33. {"--radiolist", 9, 0, 3, j_radiolist},
  34. {"--yesno", 5, 5, 1, j_yesno},
  35. {"--textbox", 5, 5, 1, j_textbox},
  36. {"--inputbox", 5, 6, 1, j_inputbox},
  37. {"--msgbox", 5, 5, 1, j_msgbox},
  38. {"--infobox", 5, 5, 1, j_infobox},
  39. {NULL, 0, 0, 0, NULL}
  40. };
  41. static struct Mode *modePtr;
  42. #ifdef LOCALE
  43. #include <locale.h>
  44. #endif
  45. int main(int argc, const char *const *argv)
  46. {
  47. int offset = 0, opt_clear = 0, end_common_opts = 0, retval;
  48. const char *title = NULL;
  49. #ifdef LOCALE
  50. (void)setlocale(LC_ALL, "");
  51. #endif
  52. #ifdef TRACE
  53. trace(TRACE_CALLS | TRACE_UPDATE);
  54. #endif
  55. if (argc < 2) {
  56. Usage(argv[0]);
  57. exit(-1);
  58. }
  59. while (offset < argc - 1 && !end_common_opts) { /* Common options */
  60. if (!strcmp(argv[offset + 1], "--title")) {
  61. if (argc - offset < 3 || title != NULL) {
  62. Usage(argv[0]);
  63. exit(-1);
  64. } else {
  65. title = argv[offset + 2];
  66. offset += 2;
  67. }
  68. } else if (!strcmp(argv[offset + 1], "--backtitle")) {
  69. if (backtitle != NULL) {
  70. Usage(argv[0]);
  71. exit(-1);
  72. } else {
  73. backtitle = argv[offset + 2];
  74. offset += 2;
  75. }
  76. } else if (!strcmp(argv[offset + 1], "--clear")) {
  77. if (opt_clear) { /* Hey, "--clear" can't appear twice! */
  78. Usage(argv[0]);
  79. exit(-1);
  80. } else if (argc == 2) { /* we only want to clear the screen */
  81. init_dialog();
  82. refresh(); /* init_dialog() will clear the screen for us */
  83. end_dialog();
  84. return 0;
  85. } else {
  86. opt_clear = 1;
  87. offset++;
  88. }
  89. } else /* no more common options */
  90. end_common_opts = 1;
  91. }
  92. if (argc - 1 == offset) { /* no more options */
  93. Usage(argv[0]);
  94. exit(-1);
  95. }
  96. /* use a table to look for the requested mode, to avoid code duplication */
  97. for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
  98. if (!strcmp(argv[offset + 1], modePtr->name))
  99. break;
  100. if (!modePtr->name)
  101. Usage(argv[0]);
  102. if (argc - offset < modePtr->argmin)
  103. Usage(argv[0]);
  104. if (modePtr->argmax && argc - offset > modePtr->argmax)
  105. Usage(argv[0]);
  106. init_dialog();
  107. retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
  108. if (opt_clear) { /* clear screen before exit */
  109. attr_clear(stdscr, LINES, COLS, screen_attr);
  110. refresh();
  111. }
  112. end_dialog();
  113. exit(retval);
  114. }
  115. /*
  116. * Print program usage
  117. */
  118. static void Usage(const char *name)
  119. {
  120. fprintf(stderr, "\
  121. \ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
  122. \n patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
  123. \n modified/gutted for use as a Linux kernel config tool by \
  124. \n William Roadcap (roadcapw@cfw.com)\
  125. \n\
  126. \n* Display dialog boxes from shell scripts *\
  127. \n\
  128. \nUsage: %s --clear\
  129. \n %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
  130. \n\
  131. \nBox options:\
  132. \n\
  133. \n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
  134. \n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
  135. \n --textbox <file> <height> <width>\
  136. \n --inputbox <text> <height> <width> [<init>]\
  137. \n --yesno <text> <height> <width>\
  138. \n", name, name);
  139. exit(-1);
  140. }
  141. /*
  142. * These are the program jumpers
  143. */
  144. int j_menu(const char *t, int ac, const char *const *av)
  145. {
  146. return dialog_menu(t, av[2], atoi(av[3]), atoi(av[4]),
  147. atoi(av[5]), av[6], (ac - 6) / 2, av + 7);
  148. }
  149. int j_radiolist(const char *t, int ac, const char *const *av)
  150. {
  151. return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]),
  152. atoi(av[5]), (ac - 6) / 3, av + 6);
  153. }
  154. int j_textbox(const char *t, int ac, const char *const *av)
  155. {
  156. return dialog_textbox(t, av[2], atoi(av[3]), atoi(av[4]));
  157. }
  158. int j_yesno(const char *t, int ac, const char *const *av)
  159. {
  160. return dialog_yesno(t, av[2], atoi(av[3]), atoi(av[4]));
  161. }
  162. int j_inputbox(const char *t, int ac, const char *const *av)
  163. {
  164. int ret = dialog_inputbox(t, av[2], atoi(av[3]), atoi(av[4]),
  165. ac == 6 ? av[5] : (char *)NULL);
  166. if (ret == 0)
  167. fprintf(stderr, "%s", dialog_input_result);
  168. return ret;
  169. }
  170. int j_msgbox(const char *t, int ac, const char *const *av)
  171. {
  172. return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 1);
  173. }
  174. int j_infobox(const char *t, int ac, const char *const *av)
  175. {
  176. return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 0);
  177. }