MS-DOS 6.22 CHOICE Command Parameter Parsing

This article explains how the MS-DOS 6.22 CHOICE command captures, parses, and evaluates user input against its specified parameter list. It details the internal validation loop, case-sensitivity handling, and how user keystrokes are mapped directly to batch file exit codes (ERRORLEVEL).

Command Parameter Structure

In MS-DOS 6.22, CHOICE is an external command implemented via CHOICE.COM. Its syntax allows users to define the acceptable response characters using command-line switches:

CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
  • /C[:]choices: Defines the allowed keys. If omitted, the default choice list is YN.
  • /S: Enables case-sensitive evaluation.
  • /T[:]c,nn: Specifies a default choice c after a pause of nn seconds.
  • /N: Suppresses the display of the choice prompt (e.g., [Y,N]?).
  • text: Displays an optional prompt string prior to the choice list.

Input Capture and Parsing Mechanism

When executed, CHOICE parses its command-line arguments using standard MS-DOS Program Segment Prefix (PSP) parsing routines. It builds an internal array of valid character bytes based on the string provided after the /C switch. If no /C switch is present, the array defaults to ['Y', 'N'].

The response parsing follows a strict operational sequence:

  1. Character Normalization (Case Handling): By default, CHOICE is case-insensitive. During initialization, unless the /S flag is present, CHOICE converts all defined characters in the /C list to uppercase. When waiting for input, any ASCII key pressed by the user is similarly converted to uppercase before comparison. If /S is specified, the input byte is evaluated strictly by its literal ASCII value.

  2. Console Polling and Interception: CHOICE uses DOS keyboard interrupt services (such as INT 21h, Function 08h or BIOS INT 16h) to read a single character directly from standard input without requiring the user to press Enter.

  3. Comparison Loop: Upon receiving a keystroke, the program scans the configured choice array sequentially from left to right:

    • Match Found: If the keystroke matches an entry in the allowed array, the evaluation loop immediately terminates.
    • No Match: If the key is not in the list, CHOICE discards the input, sends a standard ASCII Bell character (07h) to the console speaker to alert the user, and resumes waiting for a valid key.
  4. Timeout Parsing (/T): If the /T switch is active, CHOICE monitors the system timer tick counter (BIOS data area at 0040:006Ch). If the specified number of seconds elapses before a key is pressed, CHOICE bypasses user input entirely and feeds the designated default character into the parsing routine.

Exit Code (ERRORLEVEL) Generation

CHOICE does not return the ASCII value of the selected key to the operating system. Instead, it converts the user's selection into a 1-based index corresponding to the key's position in the /C parameter list:

  • Pressing the first character in the list terminates the program with an exit code of 1.
  • Pressing the second character returns 2.
  • Pressing the n-th character returns n.

Batch files evaluate this response using the IF ERRORLEVEL construct. Because IF ERRORLEVEL n evaluates to true for values greater than or equal to n, batch scripts must process these exit codes in descending numerical order.

Special Exceptions and Signal Handling

  • Ctrl+C / Ctrl+Break: If the user presses Ctrl+C or Ctrl+Break during execution, the MS-DOS break handler intercepts the execution. If the user confirms program termination, CHOICE exits immediately with an exit code of 0.
  • Execution Errors: If an internal error occurs (such as invalid parameter syntax or conflicting switch values), CHOICE exits with an exit code of 255.