How MS-DOS 6.22 Handles Command Whitespace
This article explains how the default MS-DOS 6.22 command
interpreter, COMMAND.COM, processes leading and trailing
whitespace characters in user input. It covers the internal mechanisms
used to read keyboard input, strip extraneous leading spaces and tabs,
isolate the command verb, and package trailing arguments into the
Program Segment Prefix (PSP) command tail for execution.
The Command Line Input Buffer
When you enter a command at the MS-DOS prompt,
COMMAND.COM uses the DOS Interrupt 21h,
Function 0Ah (Buffered Input) to capture the line. This
function populates a memory buffer that records:
- The maximum buffer size allowed.
- The actual number of characters typed.
- The raw string of characters terminated by a Carriage Return (ASCII
0Dh).
At this initial stage, all characters—including leading spaces,
multiple internal spaces, horizontal tabs (ASCII 09h), and
trailing spaces before the Enter key was pressed—are retained verbatim
in the buffer.
Processing Leading Whitespace
Once the user presses Enter, COMMAND.COM initializes an
internal pointer at the start of the text buffer and begins parsing from
left to right.
- Scanning and Skipping: The parser checks each
character against standard DOS whitespace delimiters, specifically the
space character (ASCII
20h) and horizontal tab (ASCII09h). - Pointer Advancement: If a space or tab is detected, the pointer increments to the next byte without storing or acting upon the whitespace.
- Command Verb Detection: The parser continues skipping characters until it encounters the first non-whitespace character. This character marks the beginning of the command name or executable token.
Because COMMAND.COM automatically discards all preceding
spaces and tabs, typing DIR or
[Tab][Tab]DIR is treated identically to typing
DIR.
Isolating the Command Token
After locating the first non-whitespace character, the parser scans forward to locate the end of the command name. The command name terminates as soon as another delimiter is encountered:
- A space (ASCII
20h) or tab (ASCII09h) - A carriage return (ASCII
0Dh) - Standard DOS path or redirection characters (such as
\,/,:,<,>, or|)
The extracted command name is converted to uppercase and checked
against internal commands (such as DIR, COPY,
or CD). If no internal command matches, DOS uses this token
to search the current directory and the system PATH for an
executable file with a .COM, .EXE, or
.BAT extension.
Processing Trailing Whitespace
The handling of trailing whitespace depends on whether the whitespace follows the command verb itself or appears at the very end of the entire command line.
Trailing Whitespace After the Command Verb
Any whitespace immediately following the command verb serves merely as a delimiter to separate the command from its parameters. The parser consumes the delimiter, skips any consecutive spaces or tabs, and establishes the beginning of the parameter string.
Trailing Whitespace at the End of the Line
When invoking an external program via DOS Interrupt 21h,
Function 4Bh (EXEC), COMMAND.COM constructs an
unparsed parameter string known as the command tail and copies it into
the program's Program Segment Prefix (PSP) at offset
80h:
- PSP Format: Offset
80hstores a single byte representing the length of the tail, followed by the text of the parameters starting at offset81h, terminated by a Carriage Return (0Dh). - Preservation of Trailing Characters:
COMMAND.COMdoes not aggressively trim trailing spaces from the end of the argument string. Any spaces or tabs typed after the final parameter up to the terminating carriage return remain in the PSP command tail buffer, and the byte count at offset80hincludes these characters. - Application-Level Parsing: Because the PSP receives the raw parameter string, it is up to the invoked application or its runtime library (such as the C runtime startup code) to parse arguments. Most compilers and standard utilities ignore unquoted trailing whitespace when tokenizing command line arguments.