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 isYN./S: Enables case-sensitive evaluation./T[:]c,nn: Specifies a default choicecafter a pause ofnnseconds./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:
Character Normalization (Case Handling): By default,
CHOICEis case-insensitive. During initialization, unless the/Sflag is present,CHOICEconverts all defined characters in the/Clist to uppercase. When waiting for input, any ASCII key pressed by the user is similarly converted to uppercase before comparison. If/Sis specified, the input byte is evaluated strictly by its literal ASCII value.Console Polling and Interception:
CHOICEuses DOS keyboard interrupt services (such asINT 21h, Function 08hor BIOSINT 16h) to read a single character directly from standard input without requiring the user to press Enter.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,
CHOICEdiscards the input, sends a standard ASCII Bell character (07h) to the console speaker to alert the user, and resumes waiting for a valid key.
Timeout Parsing (
/T): If the/Tswitch is active,CHOICEmonitors the system timer tick counter (BIOS data area at0040:006Ch). If the specified number of seconds elapses before a key is pressed,CHOICEbypasses 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+CorCtrl+Breakduring execution, the MS-DOS break handler intercepts the execution. If the user confirms program termination,CHOICEexits immediately with an exit code of0. - Execution Errors: If an internal error occurs (such
as invalid parameter syntax or conflicting switch values),
CHOICEexits with an exit code of255.