root/scripts/lxdialog/lxdialog.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. Usage
  3. j_menu
  4. j_checklist
  5. j_radiolist
  6. j_textbox
  7. j_yesno
  8. j_inputbox
  9. j_msgbox
  10. j_infobox

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

/* [previous][next][first][last][top][bottom][index][help] */