home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / batutl / ask_bat.arc / ASK_BAT.C next >
C/C++ Source or Header  |  1989-02-17  |  15KB  |  334 lines

  1.                      /*********************/
  2.                      /* Program ASK_BAT.C */
  3.                      /*********************/
  4.  
  5. /* Copyright 1989 Floyd D. Pretz Sr.
  6.  *                5834 Spaulding St.
  7.  *                Omaha, NE  68104
  8.  *
  9.  * Purpose: To parse Console input within a batch file and return with
  10.  *          with an appropriate DOS ERRORLEVEL, and/or echo text to the
  11.  *          console with cursor control.
  12.  *
  13.  * Input:  DOS command line parameters.
  14.  *
  15.  * Output: Modified ERRORLEVEL for batch testing.
  16.  *
  17.  * Description: ask_bat was written with the intention of providing
  18.  *              a method to pause for a few seconds within a batch
  19.  *              file (especially autoexec) to get an option from
  20.  *              the operator that would determine the next action
  21.  *              that the batch file would perform.  If the operator
  22.  *              was unavailable, some default option could be assumed
  23.  *              then batch processing continued.
  24.  *
  25.  *  Use:  ask_bat [ 'string' errlev ... ] [ /T:nnn ] [ /H ] [ /F ] [ /P prompt ]
  26.  *
  27.  *        string - any character string to match from the keyboard.
  28.  *        errlev - the ERRORLEVEL to exit with if preceding string selected.
  29.  *        /T:nnn - maximum time to wait for keyboard activity.
  30.  *            /H - hot key switch (On). attempt to match on one keystroke.
  31.  *            /F - force match switch (On).  'string' must be matched.
  32.  *            /P - prompt text follows.
  33.  *        prompt - text to display prior to keyboard parsing.
  34.  *
  35.  *        Notes: - string-errlev pairs, if any, must occur first in the
  36.  *                 parameter list.
  37.  *               - parameters are space delimited.
  38.  *               - switches may occur in any order following the string-errlev
  39.  *                 pairs.
  40.  *               - prompt or string parameters must be enclosed in double
  41.  *                 quotes (") if either contains a space character.
  42. */
  43.  
  44.         #include <stdio.h>
  45.         #include <stdlib.h>
  46.         #include <time.h>
  47.         #include <string.h>
  48.         #include <stdlib.h>
  49.         #include <conio.h>
  50.  
  51.         #define false 0
  52.         #define true 1
  53.  
  54.         void main(int argc, char *argv[])
  55.  
  56.         {
  57.         clock_t start, end, elapsed;            /* define timer variabls */
  58.         char response[129];                     /* storage for keystroks */
  59.         int timed, hotkey, forced;              /* logical flags         */
  60.         int i, indx, lastpair;                  /* miscellaneous         */
  61.         int maxtime, errlev, keys;              /* miscellaneous         */
  62.         void ask_hlp();                         /* define usage routine  */
  63.         void ask_hlp_big();                     /* define help routine   */
  64.  
  65. /*
  66.  *                    
  67.  *  Main Program Body 
  68.  *                    
  69. */
  70.  
  71. /*
  72.  *  Check for "?" parameter or absence of
  73.  *  parameters and give appropriate help message.
  74. */
  75.         if (argc==1) { ask_hlp();               /* if no command args */
  76.                        i=getch();               /* print usage, get a */
  77.                        exit(0);   }             /* keystroke & exit   */
  78.         if (!stricmp("?",argv[1]))              /* if ? command arg   */
  79.                      { ask_hlp_big();           /* print help & exit  */
  80.                        exit(0);   }
  81. /*
  82.  *  Initialize local variables
  83. */
  84.     response[0] = 0;
  85.         start = clock();                        /* get initial time stamp */
  86.         timed = hotkey = forced = false;        /* default options false */
  87.         keys = 0;                               /* Keyboard buffer index */
  88.         errlev=-1;                              /* -1 indicates no match */
  89.         lastpair=argc;                          /* last string-errlev    */
  90.  
  91. /*
  92.  * Process the command line by looping
  93.  * through the argument list looking for 
  94.  * switches and setting logic flags.
  95. */
  96.         for (indx = 1; indx <= argc-1; indx++)
  97.            {                                
  98.             if (!strnicmp("/ ",argv[indx],1)) {        /* switch parameter?  */
  99.                if (lastpair==argc) lastpair=indx-1;    /* set lastpair index */
  100.                if (!stricmp("/H",argv[indx]))                 /* Hot Key ? */
  101.                   hotkey=true;
  102.                else if (!stricmp("/F",argv[indx]))            /* Forced  ? */
  103.                   forced=true;
  104.                else if (!strnicmp("/T:",argv[indx],3))        /* Timed   ? */
  105.                   { timed=true;
  106.                     maxtime=atoi(argv[indx]+3); }
  107.                else if (!stricmp("/P",argv[indx]))            /* Prompt  ? */
  108.                   { if (argc >= indx)
  109.                        printf(argv[++indx]); }
  110.                else
  111.                   printf("Invalid switch '%s'\n",argv[indx]); /* ?????? ?  */
  112.                }
  113.            }
  114. /*
  115.  *  loop indefinitely until time exceeded while gathering keystrokes.
  116.  *  continue looping until parameter list satisfied (errlev != -1).
  117. */
  118.  
  119.         do {
  120.            if (!kbhit()) {                          /* Keystroke waiting?   */
  121.               if (timed) {                          /* No keyboard activity */
  122.                  end=clock();                       /* get & check time     */
  123.                  elapsed=(end-start)/13;            /* CLK_TCK not used     */
  124.                  if (elapsed > maxtime) {           /* Timeout!             */
  125.                     errlev=0;
  126.                     if (lastpair > 0) {             /* assume match on 1st  */
  127.                        printf (argv[1]);            /* string-errlev pair   */
  128.                        errlev=atoi(argv[2]);
  129.                     }
  130.                  }
  131.               }
  132.            }
  133. /*
  134.  *  Key Hit!   - Process non-ascii as a delete key.
  135.  *             - Trap Hot keys
  136.  *             - Buffer other keys
  137.  *             - & reset clock
  138. */
  139.            else {
  140.               i=getch();
  141.               if (!i || i==8) {                 /* if null/delete then  */
  142.                  if (!i) i=getch();             /* process as delete    */
  143.                  if (keys) {
  144.                     --keys;                     /* backup string end    */
  145.                     response[keys]=0;                                     
  146.                     printf("\b \b");
  147.                  }
  148.               }
  149.               else {
  150.                  if (i != 13) {                 /* Buffer keystroke &  */
  151.                     response[keys]=i;           /* display character   */
  152.                     if (!hotkey) {              /* if not hot key mode */
  153.                        keys++;
  154.                        response[keys]=0;        /* string terminator   */
  155.                        putchar(i);
  156.                     }
  157.                  }
  158. /*
  159.  *  Hotkey! or <RETURN> key!
  160. */  
  161.                  if (hotkey||(i==13)) {
  162.                     if (lastpair==0) errlev=0;
  163. /*
  164.  *  Loop through string-errlev pairs looking for a match
  165. */
  166.                     for (indx = 1; indx < lastpair; indx=indx+2) {
  167. /*
  168.  *  Check hotkey
  169. */
  170.                        if (hotkey && !strnicmp(response,argv[indx],1)) {
  171.                           errlev=atoi(argv[indx+1]);
  172.                           printf(argv[indx]); 
  173.                        }
  174. /*
  175.  *  Check non-hotkey strings
  176. */
  177.                        else if (!stricmp(response,argv[indx]))
  178.                           errlev=atoi(argv[indx+1]);
  179.                     }
  180. /*
  181.  *  Process a simple <RETURN> as a default
  182. */
  183.                     if (!forced && errlev==-1) { 
  184.                        if (i==13 && keys==0) {
  185.                           printf(argv[1]);
  186.                           errlev=atoi(argv[2]);
  187.                        }
  188.                     }
  189. /*
  190.  *  Bell (Beep) if no match or miss match
  191. */
  192.                     if (errlev==-1) printf("\a");
  193.                  }
  194.               }
  195.               start=clock(); }
  196. /*
  197.  *  End of keyboard processing loop
  198. */
  199.            } while (errlev < 0 );
  200.         printf("\n");
  201.         exit(errlev);
  202.         }
  203.  
  204. /*                                */
  205. /*        subroutine ask_hlp      */
  206. /*                                */
  207.  
  208.    void ask_hlp()
  209.      {
  210.      char *help;
  211.      help =                                        /* assign help text */
  212.      "Usage: ask_bat [ \"string\" errlev ... ]"
  213.      " [ /T:nnn ] [ /H ] [ /F ] [ /P \"prompt\" ]\n\n"
  214.      "       ask_bat ? (will display an extended help screen)\n\n"
  215.      "Press any key to continue ...";
  216.      printf("%s",help);
  217.      }
  218.  
  219. /*                                */
  220. /*        subroutine ask_hlp_big  */
  221. /*                                */
  222.  
  223.         void ask_hlp_big()
  224.         {
  225.         char *help;
  226.         help =                                        /* assign help text */
  227. "       program: ask_bat\n"
  228. "\n"
  229. "       Copyright 1989 Floyd D. Pretz Sr.\n"
  230. "                      5834 Spaulding St.\n"
  231. "                      Omaha, NE  68104\n"
  232. "\n"
  233. "       Distribution of this program for non-commercial purposes\n"
  234. "       is permitted.  Author assumes no liability for losses due to\n"
  235. "       malfunction of this program.\n"
  236. "\n"
  237. "       Description:  The ask_bat program was designed to allow\n"
  238. "                     for timed response to (Y/N) type prompts\n"
  239. "                     in .BAT files - esp the Autoexec.bat file.\n"
  240. "                     The program was written to satisfy a need\n"
  241. "                     to recover from a system reboot of a BBS\n"
  242. "                     (Bulletin Board System) following the loss\n"
  243. "                     of modem carrier, and still allow the system\n"
  244. "                     operator (if present) to answer typical\n"
  245. "                     configuration 'boot' questions.  Using ask_bat\n"
  246. "                     with timing options, the system will not wait\n"
  247. "                     indefinitely for console response, but will assume\n"
  248. "                     a default response after a specified period of\n"
  249. "                     time.  This also allows you the opportunity to\n"
  250. "                     go fill your coffee cup during the boot process\n"
  251. "                     and not wait around to answer batch prompts.\n"
  252. "\n"
  253.      "Use: ask_bat [ \"string\" errlev ... ]"
  254.      " [ /T:nnn ] [ /H ] [ /F ] [ /P \"prompt\" ]\n"
  255. "\n"
  256. "                where: 'string' is an optional keyboard (console)\n"
  257. "                        response to which the program will exit with\n"
  258. "                        an ERRORLEVEL set to errlev.  You can use as\n"
  259. "                        many of the 'string'-errlev sets as you wish\n"
  260. "                        (DOS command line can not exceed 128 characters).\n"
  261. "                        The absence of any such pairs will function\n"
  262. "                        as 'Press any key to continue' with the\n"
  263. "                        errorlevel set to 0.  'string' is not case\n"
  264. "                        sensitive (i.e.  'Yes', 'YES' & 'yEs' are\n"
  265. "                        treated identically).\n"
  266. "\n"
  267. "                        /T:nnn is an option to time keyboard activity\n"
  268. "                        and exit ask_bat after nnn seconds.  If the\n"
  269. "                        /T parameter is not specified or nnn=0 then\n"
  270. "                        keyboard activity is not timed and ask_bat will\n"
  271. "                        wait indefinitely for console activity.  In the\n"
  272. "                        case of a time out (user did not respond within\n"
  273. "                        nnn seconds) the default is assumed to be the\n"
  274. "                        first 'string'-errlev parameter (if any).\n"
  275. "                        The default is also assumed if the console\n"
  276. "                        response is null (Enter key only).\n"
  277. "\n"
  278. "                        /H is an option to enable a HOT KEY feature\n"
  279. "                        which attempts to match a single keystroke\n"
  280. "                        to one of the 'string' parameters and exit\n"
  281. "                        accordingly.\n"
  282. "\n\f"
  283. "                         /F is an option to force the user to key in\n"
  284. "                        one of the 'strings' in order to exit the program.\n"
  285. "                        the program will 'beep' if an incorrect entry\n"
  286. "                        is detected.\n"
  287. "\n"
  288. "                        /P prompt is an option to display a string on the\n"
  289. "                        screen without forcing a <RETURN>.\n"
  290. "\n"
  291. "                        Invocation without any parameters will function\n"
  292. "                        similar to the DOS PAUSE statement.\n"
  293. "\n"
  294. "             examples:        ask_bat yes 1 no 2 maybe 3 /T:15 /H\n"
  295. "\n"
  296. "                                 will exit with errorlevel=1 if the\n"
  297. "                                 character 'Y' or 'y' is pressed or\n"
  298. "                                 if 15 seconds elapses or only the\n"
  299. "                                 <RETURN> key is pressed.  Also exits with\n"
  300. "                                 errorlevel=2 if 'N' or 'n' is pressed\n"
  301. "                                 and exits with errorlevel=3 if 'M' or\n"
  302. "                                 'm' is pressed.\n"
  303. "\n"
  304. "                        ask_bat /H\n"
  305. "\n"
  306. "                                 Functions similar to the PAUSE command.\n"
  307. "\n"
  308. "                        ask_bat tom 10 dick 20 /F\n"
  309. "\n"
  310. "                                 Will exit with errorlevel=10 if 'TOM',\n"
  311. "                                 'tom', 'Tom', etc is keyed in, or exit\n"
  312. "                                 with errorlevel=20 if 'Dick', etc is keyed\n"
  313. "                                 in. Because of the '/F' parameter one\n"
  314. "                                 of these two responses must be keyed in\n"
  315. "                                 in order for ask_bat to exit.\n"
  316. "\n"
  317. "                        ask_bat yes 3 no 2 /H /T:15 /P \"Continue --> \"\n"
  318. "\n"
  319. "                                 Will display the prompt string and then\n"
  320. "                                 wait up to 15 seconds for a hot key.\n"
  321. "\n"
  322. "                        ask_bat ? > readme.txt\n"
  323. "\n"
  324. "                                 This will create the readme.txt file\n"
  325. "                                 which you can later print. Or ...\n"
  326. "\n"
  327. "                        ask_bat ? | more\n"
  328. "\n"
  329. "                                 Which will force the screen to pause\n"
  330. "                                 while you read the help text.\n";
  331.         printf("%s",help);
  332.         }
  333.  
  334.