[Toc][Index]

Command Parsing


Whenever you type something at the command line and press the Enter key, 
or include a command in a batch file, you have given a command to CMD.EXE, 
which must figure out how to execute your command.  If you understand the 
general process that is used, you will be able to make the best use of the 
commands.  Understanding these steps can be especially helpful when 
working with complex aliases or batch file commands. 
To decide what activity to perform, the command processor goes through 
several steps.  Before it starts, it writes the entire command line (which 
may contain multiple commands) to the history log file if history logging 
has been enabled with the LOG /H command, and the command did not come 
from a batch file.  Then, if the line contains multiple commands, the 
first command is isolated for processing. 
CMD.EXE begins by dividing the command into a command name and a command 
tail.  The command name is the first word in the command; the tail is 
everything that follows the command name.  For example, in the command 
line 


        dir *.txt /2/p/v

the command name is "dir", and the command tail is " *.txt /2/p/v". 
Next 4DOS tries to match the command name against its list of aliases.  If 
it finds a match between the command name and one of the aliases you've 
defined, it replaces the command name with the contents of the alias. 
 (This substitution is done internally and is not normally visible to 
you).  However, you can view a command line with aliases expanded by 
pressing Ctrl-F after entering the command at the prompt. 
If the alias included parameters (%1, %2, etc.), the parameter values are 
filled in from the text on the command line, and any parameters used in 
this process are removed from the command line.  The process of replacing 
a command name that refers to an alias with the contents of the alias, and 
filling in the alias parameters, is called alias expansion. 
This expansion of an alias creates a new command name:  the first word of 
the alias.  This new command name is again tested against the list of 
aliases, and if a match is found the contents of the new alias is expanded 
just like the first alias.  This process, called nested alias expansion, 
continues until the command name no longer refers to an alias. 
Once it has finished with the aliases, CMD.EXE next tries to match the 
command name with its list of internal commands.  If it is unsuccessful, 
the command processor knows that it will have to search for a batch file 
or external program to execute your command. 
The next step is to locate any batch file or alias parameters, environment 
variables, internal variables, or variable functions in the command, and 
replace each one with its value.  This process is called variable 
expansion. 
The variable expansion process is modified for certain internal commands, 
like EXCEPT, IF, and GLOBAL.  These commands are always followed by 
another command, so variable expansion takes place separately for the 
original command and the command that follows it. 
Once all of the aliases and environment variables have been expanded, 
CMD.EXE will echo the complete command to the screen (if command-line echo 
has been enabled) and write it to the log file (if command logging has 
been turned on). 
Before it can actually execute your command, the command processor must 
scan the command tail to see if it includes redirection or piping.  If so, 
the proper internal switches are set to send output to an alternate device 
or to a file, instead of to the screen.  A second process is started at 
this point, if necessary, to receive any piped output. 
Finally, it is time to execute the command.  If the command name matches 
an internal command, CMD.EXE will perform the activities you have 
requested.  Otherwise, the command processor searches for an executable 
(.COM or .EXE) file, a batch file, or a file with an executable extension 
that matches the command name. 
Once the internal command or external program has terminated, the command 
processor saves the result or exit code that the command generated, cleans 
up any redirection that you specified, and then returns to the original 
command line to retrieve the next command.  When all of the commands in a 
command line are finished, the next line is read from the current batch 
file, or if no batch file is active, the prompt is displayed. 
You can disable and re-enable several parts of command parsing (for 
example alias expansion, variable expansion, and redirection) with the 
SETDOS /X command. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs