base/config/kconfig/lxdialog/lxdialog.c

Go to the documentation of this file.
00001 /*
00002  *  dialog - Display simple dialog boxes from shell scripts
00003  *
00004  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
00005  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; either version 2
00010  *  of the License, or (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "dialog.h"
00023 
00024 static void Usage (const char *name);
00025 
00026 typedef int (jumperFn) (const char *title, int argc, const char * const * argv);
00027 
00028 struct Mode {
00029     char *name;
00030     int argmin, argmax, argmod;
00031     jumperFn *jumper;
00032 };
00033 
00034 jumperFn j_menu, j_checklist, j_radiolist, j_yesno, j_textbox, j_inputbox;
00035 jumperFn j_msgbox, j_infobox;
00036 
00037 static struct Mode modes[] =
00038 {
00039     {"--menu", 9, 0, 3, j_menu},
00040     {"--checklist", 9, 0, 3, j_checklist},
00041     {"--radiolist", 9, 0, 3, j_radiolist},
00042     {"--yesno",    5,5,1, j_yesno},
00043     {"--textbox",  5,5,1, j_textbox},
00044     {"--inputbox", 5, 6, 1, j_inputbox},
00045     {"--msgbox", 5, 5, 1, j_msgbox},
00046     {"--infobox", 5, 5, 1, j_infobox},
00047     {NULL, 0, 0, 0, NULL}
00048 };
00049 
00050 static struct Mode *modePtr;
00051 
00052 #ifdef LOCALE
00053 #include <locale.h>
00054 #endif
00055 
00056 int
00057 main (int argc, const char * const * argv)
00058 {
00059     int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
00060     const char *title = NULL;
00061 
00062 #ifdef LOCALE
00063     (void) setlocale (LC_ALL, "");
00064 #endif
00065 
00066 #ifdef TRACE
00067     trace(TRACE_CALLS|TRACE_UPDATE);
00068 #endif
00069     if (argc < 2) {
00070     Usage (argv[0]);
00071     exit (-1);
00072     }
00073 
00074     while (offset < argc - 1 && !end_common_opts) { /* Common options */
00075     if (!strcmp (argv[offset + 1], "--title")) {
00076         if (argc - offset < 3 || title != NULL) {
00077         Usage (argv[0]);
00078         exit (-1);
00079         } else {
00080         title = argv[offset + 2];
00081         offset += 2;
00082         }
00083         } else if (!strcmp (argv[offset + 1], "--backtitle")) {
00084             if (backtitle != NULL) {
00085                 Usage (argv[0]);
00086                 exit (-1);
00087             } else {
00088                 backtitle = argv[offset + 2];
00089                 offset += 2;
00090             }
00091     } else if (!strcmp (argv[offset + 1], "--clear")) {
00092         if (clear_screen) { /* Hey, "--clear" can't appear twice! */
00093         Usage (argv[0]);
00094         exit (-1);
00095         } else if (argc == 2) { /* we only want to clear the screen */
00096         init_dialog ();
00097         refresh (); /* init_dialog() will clear the screen for us */
00098         end_dialog ();
00099         return 0;
00100         } else {
00101         clear_screen = 1;
00102         offset++;
00103         }
00104     } else          /* no more common options */
00105         end_common_opts = 1;
00106     }
00107 
00108     if (argc - 1 == offset) {   /* no more options */
00109     Usage (argv[0]);
00110     exit (-1);
00111     }
00112     /* use a table to look for the requested mode, to avoid code duplication */
00113 
00114     for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
00115     if (!strcmp (argv[offset + 1], modePtr->name))
00116         break;
00117 
00118     if (!modePtr->name)
00119     Usage (argv[0]);
00120     if (argc - offset < modePtr->argmin)
00121     Usage (argv[0]);
00122     if (modePtr->argmax && argc - offset > modePtr->argmax)
00123     Usage (argv[0]);
00124 
00125 
00126 
00127     init_dialog ();
00128     retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
00129 
00130     if (clear_screen) {     /* clear screen before exit */
00131     attr_clear (stdscr, LINES, COLS, screen_attr);
00132     refresh ();
00133     }
00134     end_dialog();
00135 
00136     exit (retval);
00137 }
00138 
00139 /*
00140  * Print program usage
00141  */
00142 static void
00143 Usage (const char *name)
00144 {
00145     fprintf (stderr, "\
00146 \ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
00147 \n  patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
00148 \n  modified/gutted for use as a Linux kernel config tool by \
00149 \n  William Roadcap (roadcapw@cfw.com)\
00150 \n\
00151 \n* Display dialog boxes from shell scripts *\
00152 \n\
00153 \nUsage: %s --clear\
00154 \n       %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
00155 \n\
00156 \nBox options:\
00157 \n\
00158 \n  --menu      <text> <height> <width> <menu height> <tag1> <item1>...\
00159 \n  --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
00160 \n  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
00161 \n  --textbox   <file> <height> <width>\
00162 \n  --inputbox  <text> <height> <width> [<init>]\
00163 \n  --yesno     <text> <height> <width>\
00164 \n", name, name);
00165     exit (-1);
00166 }
00167 
00168 /*
00169  * These are the program jumpers
00170  */
00171 
00172 int
00173 j_menu (const char *t, int ac, const char * const * av)
00174 {
00175     return dialog_menu (t, av[2], atoi (av[3]), atoi (av[4]),
00176             atoi (av[5]), av[6], (ac - 6) / 2, av + 7);
00177 }
00178 
00179 int
00180 j_checklist (const char *t, int ac, const char * const * av)
00181 {
00182     return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
00183     atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK);
00184 }
00185 
00186 int
00187 j_radiolist (const char *t, int ac, const char * const * av)
00188 {
00189     return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
00190     atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO);
00191 }
00192 
00193 int
00194 j_textbox (const char *t, int ac, const char * const * av)
00195 {
00196     return dialog_textbox (t, av[2], atoi (av[3]), atoi (av[4]));
00197 }
00198 
00199 int
00200 j_yesno (const char *t, int ac, const char * const * av)
00201 {
00202     return dialog_yesno (t, av[2], atoi (av[3]), atoi (av[4]));
00203 }
00204 
00205 int
00206 j_inputbox (const char *t, int ac, const char * const * av)
00207 {
00208     int ret = dialog_inputbox (t, av[2], atoi (av[3]), atoi (av[4]),
00209                             ac == 6 ? av[5] : (char *) NULL);
00210     if (ret == 0)
00211         fprintf(stderr, dialog_input_result);
00212     return ret;
00213 }
00214 
00215 int
00216 j_msgbox (const char *t, int ac, const char * const * av)
00217 {
00218     return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 1);
00219 }
00220 
00221 int
00222 j_infobox (const char *t, int ac, const char * const * av)
00223 {
00224     return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 0);
00225 }
00226 

Generated on Tue Feb 2 17:46:04 2010 for RTAI API by  doxygen 1.4.7