home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / BATQUEST.ZIP / BATQUEST.C next >
Encoding:
C/C++ Source or Header  |  1987-11-09  |  6.6 KB  |  190 lines

  1. /*--------------------------------------------------------------------------
  2.   Name:         BATQUEST
  3.  
  4.   Author:       David Michmerhuizen
  5.  
  6.   Date:         August 1, 1986
  7.  
  8.   Language:     MicroSoft C ver 3.0
  9.  
  10.   Description:  Allows batch files to display a prompt and determine the
  11.                 users response via the DOS ERRORLEVEL.
  12.  
  13.                 A prompt is displayed on the screen and a keystroke is
  14.                 read from the keyboard. The allowable keystrokes can
  15.                 be specified on the command line (see below), and if
  16.                 the keystroke received is not one of those, there is a
  17.                 beep and another keystroke is read. The DOS ERRORLEVEL
  18.                 is set depending on the keystroke read. If no allowable
  19.                 keystroke set was specified, any keystroke results in an
  20.                 errorlevel of 0. Otherwise, the errorlevel is the integer
  21.                 position of the keystroke in the set of allowable keystrokes.
  22.  
  23.   Subroutines:  charpos -- returns integer position of character in string
  24.                 charecho -- echos printable characters on screen.
  25.  
  26.   Call:         BATQUEST <"prompt"> [ <choices> ]
  27.  
  28.                 where:   <prompt>  is a prompt string enclosed in double quote
  29.                                    marks, and is required. However, you may
  30.                                    make it null or blank.
  31.  
  32.                          <choices> is an optional character string whose
  33.                                    characters are the valid responses to the
  34.                                    prompt, in increasing ERRORLEVEL order.
  35.                                    Embedded blanks are not allowed.  Lowercase
  36.                                    letters are translated to uppercase.
  37.                                    <choices> must be less than 40 characters
  38.                                    long, and if any character is duplicated,
  39.                                    only the last occurance will be found.  If
  40.                                    <choices> is not present, then any character
  41.                                    is a valid answer to the prompt.
  42.  
  43.   Example:      BATQUEST "Enter your choice ==> " 123ABCQ
  44.  
  45.                 will display the message:  Enter your choice ==>
  46.                 which can only be answered with one of the characters:  123ABCQ
  47.                 which will return an ERRORLEVEL of 4 if answered with:  A
  48.                 which will return an ERRORLEVEL of 7 if answered with:  Q
  49.  
  50.  
  51. ---------------------------------------------------------------------------*/
  52.  
  53. #include <stdio.h>
  54. #include <conio.h>
  55. #include <string.h>
  56. #include <stdlib.h>
  57.  
  58. #define OR  ||
  59. #define AND &&
  60. #define NOT !
  61. #define NE  !=
  62. #define EQ  ==
  63.  
  64. #define MINARGS 2
  65. #define MAXARGS 3
  66. #define MAXARGLEN 40
  67.  
  68. /*-------------------------------------------------------------------*/
  69.  
  70. main(argc, argv)
  71.  
  72.     int argc;                 /* number of arguments passed */
  73.     char *argv[];             /* array of argument strings */
  74.  
  75.  {    /*** start of main ***/
  76.  
  77.  char *UpperArgs,inchar;
  78.  int  choice;
  79.  
  80.  
  81.  
  82. /*--------------------------------------------------------------------
  83. **  if not enough args
  84. **     say not enough
  85. **     exit
  86. **  endif
  87. ** if too many args
  88. **    say too many
  89. **    exit
  90. ** endif
  91. ** if too many choices
  92. **    say too many
  93. **    exit
  94. ** endif
  95. **/
  96.  
  97.  if (argc < MINARGS) {
  98.     printf("\n");
  99.     printf("Format is:  BATQUEST  <prompt>  [ <choices> ]\n");
  100.     printf("\n");
  101.     printf("where:   <prompt>  is a prompt string enclosed in double quote marks, and\n");
  102.     printf("                   is required. However, you may make it null or blank.\n");
  103.     printf("\n");
  104.     printf("         <choices> is an optional character string whose characters are the\n");
  105.     printf("                   valid responses to the prompt, in increasing ERRORLEVEL\n");
  106.     printf("                   order. Embedded blanks are not allowed.  Lowercase letters\n");
  107.     printf("                   are translated to uppercase. <choices> must be less than\n");
  108.     printf("                   40 characters long, and if any character is duplicated,\n");
  109.     printf("                   only the last occurance will be found.  If <choices> is not\n");
  110.     printf("                   present, then any character is a valid answer to the prompt.\n");
  111.     printf("\n");
  112.     printf("Example.   BATQUEST \"Enter your choice ==> \" 123ABCQ\n");
  113.     printf("\n");
  114.     printf("           will display the message:  Enter your choice ==>\n");
  115.     printf("           which can only be answered with one of the characters:  123ABCQ\n");
  116.     printf("           which will return an ERRORLEVEL of 4 if answered with:  A\n");
  117.     printf("           which will return an ERRORLEVEL of 7 if answered with:  Q\n");
  118.     printf("\n");
  119.     printf("written for the Public Domain by David Michmerhuizen, Oakland CA, 1986\n");
  120.     printf("\n");
  121.     exit(0);
  122.     }
  123.  if (argc > MAXARGS) {
  124.     printf("\007Too many arguments. Type BATQUEST for more information\n");
  125.     exit(0);
  126.     }
  127.  if ((argc == MINARGS) AND (strlen(argv[2]) > MAXARGLEN)) {
  128.     printf("\007Too many choices. Type BATQUEST for more information\n");
  129.     exit(0);
  130.     }
  131.  
  132. /*---------------------------------------------------------------------------
  133. ** translate choices to uppercase
  134. ** write the prompt
  135. ** answer is not valid
  136. ** do while answer is not valid
  137. **    read the answer
  138. **    if it isn't valid
  139. **       beep
  140. **    endif
  141. ** enddo
  142. ** exit with errorlevel code
  143. **/
  144.  
  145.  UpperArgs = strupr(argv[2]);
  146.  printf("%s",argv[1]);
  147.  if (strlen(UpperArgs) == 0) {
  148.     inchar = toupper(getch());
  149.     CharEcho(inchar);
  150.     if (inchar EQ '\0') inchar = getch();
  151.     choice = 0;
  152.     }
  153.  else {
  154.     while( NOT (choice = charpos(inchar = toupper(getch()),UpperArgs)) ) {
  155.        CharEcho(inchar);
  156.        putchar('\007');
  157.        if (inchar EQ '\0') inchar = getch();
  158.        }
  159.     CharEcho(inchar);
  160.     if (inchar EQ '\0') inchar = getch();
  161.     }
  162.  exit(choice);
  163.  }
  164.  
  165. /**************************************************************************/
  166. /**************************************************************************/
  167.  
  168.     int charpos(c,s)
  169.     char c, s[];
  170.        {
  171.        int indx, pos;
  172.        for (indx = 0,pos = 0; s[indx]; ++indx)
  173.           if (c == s[indx]) pos = indx+1;
  174.        return(pos);
  175.        }
  176.  
  177. /*-------------------------------------------------------------------------*/
  178.  
  179. #define LOCHAR '\x20'
  180. #define HICHAR '\x7e'
  181.  
  182.     CharEcho(c)
  183.     char c;
  184.        {
  185.        if ((c >= LOCHAR) AND (c <= HICHAR)) {
  186.           putchar(c);
  187.           putchar('\b');
  188.           }
  189.        }
  190.