# @(#)TOUR 8.1 (Berkeley) 5/31/93
-# $FreeBSD$
+# $FreeBSD: head/bin/sh/TOUR 317882 2017-05-06 13:28:42Z jilles $
NOTE -- This is the original TOUR paper distributed with ash and
does not represent the current state of the shell. It is provided anyway
program input files generates
------- ----------- ---------
- mkbuiltins builtins builtins.h builtins.c
+ mkbuiltins builtins.def builtins.h builtins.c
mknodes nodetypes nodes.h nodes.c
mksyntax - syntax.h syntax.c
mktokens - token.h
word. The text consists of ordinary characters and a number of
special codes defined in parser.h. The special codes are:
- CTLVAR Variable substitution
- CTLENDVAR End of variable substitution
+ CTLVAR Parameter expansion
+ CTLENDVAR End of parameter expansion
CTLBACKQ Command substitution
CTLBACKQ|CTLQUOTE Command substitution inside double quotes
+ CTLARI Arithmetic expansion
+ CTLENDARI End of arithmetic expansion
CTLESC Escape next character
A variable substitution contains the following elements:
VSQUESTION|VSNUL ${var:?text}
VSASSIGN ${var=text}
VSASSIGN|VSNUL ${var:=text}
+ VSTRIMLEFT ${var#text}
+ VSTRIMLEFTMAX ${var##text}
+ VSTRIMRIGHT ${var%text}
+ VSTRIMRIGHTMAX ${var%%text}
+ VSLENGTH ${#var}
+ VSERROR delayed error
In addition, the type field will have the VSQUOTE flag set if the
-variable is enclosed in double quotes. The name of the variable
-comes next, terminated by an equals sign. If the type is not
-VSNORMAL, then the text field in the substitution follows, ter-
-minated by a CTLENDVAR byte.
+variable is enclosed in double quotes and the VSLINENO flag if
+LINENO is being expanded (the parameter name is the decimal line
+number). The parameter's name comes next, terminated by an equals
+sign. If the type is not VSNORMAL (including when it is VSLENGTH),
+then the text field in the substitution follows, terminated by a
+CTLENDVAR byte.
+
+The type VSERROR is used to allow parsing bad substitutions like
+${var[7]} and generate an error when they are expanded.
Commands in back quotes are parsed and stored in a linked list.
The locations of these commands in the string are indicated by
CTLBACKQ and CTLBACKQ+CTLQUOTE characters, depending upon whether
the back quotes were enclosed in double quotes.
+Arithmetic expansion starts with CTLARI and ends with CTLENDARI.
+
The character CTLESC escapes the next character, so that in case
any of the CTL characters mentioned above appear in the input,
they can be passed through transparently. CTLESC is also used to
variable and command substitution, the parser doesn't insert any
CTLESC characters to begin with (so the contents of the text
field can be written without any processing). Other here docu-
-ments, and words which are not subject to splitting and file name
-generation, have the CTLESC characters removed during the vari-
-able and command substitution phase. Words which are subject to
-splitting and file name generation have the CTLESC characters re-
-moved as part of the file name phase.
+ments, and words which are not subject to file name generation,
+have the CTLESC characters removed during the variable and command
+substitution phase. Words which are subject to file name
+generation have the CTLESC characters removed as part of the file
+name phase.
EXECUTION: Command execution is handled by the following files:
eval.c The top level routines.
The routine shellexec is the interface to the exec system call.
-EXPAND.C: Arguments are processed in three passes. The first
-(performed by the routine argstr) performs variable and command
-substitution. The second (ifsbreakup) performs word splitting
-and the third (expandmeta) performs file name generation.
+EXPAND.C: As the routine argstr generates words by parameter
+expansion, command substitution and arithmetic expansion, it
+performs word splitting on the result. As each word is output,
+the routine expandmeta performs file name generation (if enabled).
VAR.C: Variables are stored in a hash table. Probably we should
switch to extensible hashing. The variable name is stored in the
tered throughout the code, depending on which location appears
most appropriate. They can be recognized because their names al-
ways end in "cmd". The mapping from names to procedures is
-specified in the file builtins, which is processed by the mkbuilt-
-ins command.
+specified in the file builtins.def, which is processed by the
+mkbuiltins command.
A builtin command is invoked with argc and argv set up like a
normal program. A builtin command is allowed to overwrite its
ing. This is kind of like getopt, but you don't pass argc and
argv to it. Builtin routines can also call error. This routine
normally terminates the shell (or returns to the main command
-loop if the shell is interactive), but when called from a builtin
-command it causes the builtin command to terminate with an exit
-status of 2.
+loop if the shell is interactive), but when called from a non-
+special builtin command it causes the builtin command to
+terminate with an exit status of 2.
The directory bltins contains commands which can be compiled in-
dependently but can also be built into the shell for efficiency
-reasons. The makefile in this directory compiles these programs
-in the normal fashion (so that they can be run regardless of
-whether the invoker is ash), but also creates a library named
-bltinlib.a which can be linked with ash. The header file bltin.h
-takes care of most of the differences between the ash and the
-stand-alone environment. The user should call the main routine
-"main", and #define main to be the name of the routine to use
-when the program is linked into ash. This #define should appear
-before bltin.h is included; bltin.h will #undef main if the pro-
-gram is to be compiled stand-alone.
+reasons. The header file bltin.h takes care of most of the
+differences between the ash and the stand-alone environment.
+The user should call the main routine "main", and #define main to
+be the name of the routine to use when the program is linked into
+ash. This #define should appear before bltin.h is included;
+bltin.h will #undef main if the program is to be compiled
+stand-alone. A similar approach is used for a few utilities from
+bin and usr.bin.
CD.C: This file defines the cd and pwd builtins.
When an interrupt is caught and no trap has been set for that
signal, the routine "onint" in error.c is called.
-OUTPUT: Ash uses it's own output routines. There are three out-
+OUTPUT: Ash uses its own output routines. There are three out-
put structures allocated. "Output" represents the standard out-
put, "errout" the standard error, and "memout" contains output
which is to be stored in memory. This last is used when a buil-