home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SETLEVEL.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  9KB  |  272 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /********************************************************************
  4.  *                         SETLEVEL                                 *
  5.  *                                                                  *
  6.  * Written by: Lynn R. Lively                                       *
  7.  * Date:       05/15/90                                             *
  8.  * Version:    1.0                                                  *
  9.  * Written in TurboC V2.00                                          *
  10.  *                                                                  *
  11.  * Allows user input in a MSDOS .BAT file. This program asks a      *
  12.  * specified question and sets the errorlevel according to valid    *
  13.  * answer list. Since MSDOS provides no facility for getting user   *
  14.  * input into a .BAT file, and manipulation of enviorinment strings *
  15.  * seems to be unreliable and nonportable, I have written this      *
  16.  * facility as a possible alternative.                              *
  17.  *                                                                  *
  18.  * I hereby place this program into the public domain.              *
  19.  * Users of this utility do so at their own risk. I accept no       *
  20.  * responsibility for the accuracy or useability of this software.  *
  21.  * However, should you have any questions, suggestions, or problems *
  22.  * I would be happy to talk with you (and help if I can).           *
  23.  * My current work number is 713-591-6111                           *
  24.  *  Your Servant,                                                   *
  25.  *       Lynn R. Lively                                             *
  26.  *                                                                  *
  27.  ********************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <conio.h>
  33.  
  34. /*
  35.  * The following are bit mask values for the option_flg.
  36.  */
  37.  
  38. #define CASE_SENSE       1
  39. #define SINGLE_KEY_ENTRY 2
  40.  
  41. /*
  42.  * The following is the structure deffinition for the valid answer
  43.  * linked list.
  44.  */
  45.  
  46. typedef struct v_ans {
  47.       char *         ans_key;
  48.       int            rtn_err_lev;
  49.       struct v_ans * next_ans;
  50. } VALID_ANS;
  51.  
  52. /*
  53.  * Prototypes of functions defined in this module.
  54.  */
  55.  
  56. void usage (char * progname);
  57.  
  58. int main (int argc, char * argv[])
  59. {
  60.       register int i = 0;
  61.       register int j;
  62.  
  63.       int option_flg = 0;
  64.  
  65.       VALID_ANS * answer_tab = NULL;
  66.       VALID_ANS * wk_ans;
  67.  
  68.       char * wk_ptr;
  69.       char * ques_ptr = "?";
  70.       char   wk_str[256];
  71.  
  72.       if (argc > 1)
  73.       {
  74.         /*
  75.          * Step through and parse the argument list.
  76.          */
  77.  
  78.             for (i = 1; i < argc; i++)
  79.             {
  80.                   j = 0;
  81.                   switch (argv[i][j])
  82.                   {
  83.                   case '/':            /* Options are flagged by '/' */
  84.  
  85.                         j++;
  86.                         switch (argv[i][j])
  87.                         {
  88.                         case 'c':
  89.                         case 'C':
  90.  
  91.                               option_flg |= CASE_SENSE;
  92.                               break;
  93.  
  94.                         case '1':
  95.  
  96.                               option_flg |= SINGLE_KEY_ENTRY;
  97.                               break;
  98.  
  99.                         case 'q':
  100.                         case 'Q':
  101.  
  102.                         /*
  103.                          * The question is assumed to be the argument
  104.                          * after the 'q'.
  105.                          */
  106.  
  107.                               ques_ptr = strdup (argv[++i]);
  108.                               break;
  109.  
  110.                         default:
  111.  
  112.                         /*
  113.                          * Oops! Something wrong. Tell them how its
  114.                          * supposed to work.
  115.                          */
  116.  
  117.                               usage (argv[0]);
  118.                               return (-1);
  119.                         }
  120.                         break;
  121.  
  122.                   case '-':         /* Answers are flagged by '-' */
  123.  
  124.                         /*
  125.                          * The answer is assumed to be everything from the
  126.                          * '-' to the '=' characters. This allows for
  127.                          * single letter and word answers.
  128.                          */
  129.  
  130.                         j++;
  131.                         if ((wk_ptr = strtok (argv[i]+j, "=")) != NULL)
  132.                         {
  133.                               /*
  134.                                * Allocate space for the new answer entry and
  135.                                * link in into the list. Answers are linked
  136.                                * into the list in reverse order from their
  137.                                * command line specification since the logic
  138.                                * is considerably simpler and it shouldn't
  139.                                * matter much anyway.
  140.                                */
  141.  
  142.                               if (answer_tab == NULL)
  143.                               {
  144.                                     answer_tab = wk_ans = (VALID_ANS *)
  145.                                           calloc (1, sizeof(VALID_ANS));
  146.                               }
  147.                               else
  148.                               {
  149.                                     wk_ans = (VALID_ANS *)
  150.                                              calloc (1, sizeof(VALID_ANS));
  151.                                     wk_ans->next_ans = answer_tab;
  152.                                     answer_tab = wk_ans;
  153.                               }
  154.  
  155.                               /*
  156.                                * Store the answer string and errorlevel value
  157.                                * into the answer element.
  158.                                */
  159.  
  160.                               wk_ans->ans_key = strdup (wk_ptr);
  161.                               wk_ptr          = strtok (NULL, "=");
  162.                               if (wk_ptr != NULL)
  163.                               {
  164.                                     /*
  165.                                      * Allow only positive returns since -1 is
  166.                                      * and error return from the program.
  167.                                      */
  168.                                     
  169.                                     wk_ans->rtn_err_lev =
  170.                                           abs (atoi (wk_ptr));
  171.                               }
  172.                               else  wk_ans->rtn_err_lev = 0;
  173.                         }
  174.                         }
  175.                         break;
  176.  
  177.                   default:
  178.  
  179.                   /*
  180.                    * Oops! Something wrong. Tell them how its
  181.                    * supposed to work.
  182.                    */
  183.  
  184.                         usage (argv[0]);
  185.                         return (-1);
  186.             }
  187.       }
  188.       else
  189.       {
  190.  
  191.             /*
  192.              * Oops! Something wrong. Tell them how its
  193.              * supposed to work.
  194.              */
  195.  
  196.             usage (argv[0]);
  197.             return (-1);
  198.       }
  199.  
  200.       /*
  201.        * Ask the question and get their answer.
  202.        */
  203.  
  204.       printf ("%s", ques_ptr);
  205.  
  206.       if ((option_flg & SINGLE_KEY_ENTRY) != 0)
  207.       {
  208.             wk_str[0] = getche ();
  209.             wk_str[1] = '\0';
  210.       }
  211.       else  fgets (wk_str, sizeof (wk_str), stdin);
  212.  
  213.       /*
  214.        * See if we can find their answer in our valid answer list.
  215.        */
  216.  
  217.       wk_ans = answer_tab;
  218.       while (wk_ans != (VALID_ANS *) NULL)
  219.       {
  220.             if ((option_flg & CASE_SENSE) != 0)
  221.             {
  222.                   if ((strncmp (wk_str, wk_ans->ans_key,
  223.                         (strlen (wk_ans->ans_key)))) == 0)
  224.                   {
  225.                   /*
  226.                    * If we found the answer return the associated
  227.                    * errorlevel return number.
  228.                    */
  229.  
  230.                         return (wk_ans->rtn_err_lev);
  231.                   }
  232.             }
  233.             else
  234.             {
  235.                   if ((strnicmp (wk_str, wk_ans->ans_key,
  236.                         (strlen (wk_ans->ans_key)))) == 0)
  237.                   {
  238.                   /*
  239.                    * If we found the answer return the associated
  240.                    * errorlevel return number.
  241.                    */
  242.  
  243.                         return (wk_ans->rtn_err_lev);
  244.                   }
  245.             }
  246.  
  247.             wk_ans = wk_ans->next_ans;
  248.       }
  249.  
  250.       /*
  251.        * If the answer wasn't in our table return a -1.
  252.        */
  253.  
  254.       return (-1);
  255. }
  256.  
  257. void usage (char * progname)
  258. {
  259.       printf ("%s usage:\n", progname);
  260.       printf ("%s /[options] -<valid answer>=<errorlevel>\n\n", progname);
  261.       printf ("Where:\n");
  262.       printf ("  options      = c    (Answers are case sensitive)\n");
  263.       printf ("                 1    (Single key entry)\n");
  264.       printf ("                 q\"Question Text\"\n\n");
  265.       printf ("  valid answer = (Single letter or word response)\n");
  266.       printf ("  errorlevel   = (errorlevel to return when answer seen)\n\n");
  267.       printf ("Example:\n%s /q\"Answer Y or N: \" /1 -y=1 -n=2\n\n",
  268.             progname);
  269.       printf ("Note: -1 is returned if answer is not valid or syntax\n");
  270.       printf ("      is incorrect.\n");
  271. }
  272.