TOUR (50471) | TOUR (157789) |
---|---|
1# @(#)TOUR 8.1 (Berkeley) 5/31/93 | 1# @(#)TOUR 8.1 (Berkeley) 5/31/93 |
2# $FreeBSD: head/bin/sh/TOUR 50471 1999-08-27 23:15:48Z peter $ | 2# $FreeBSD: head/bin/sh/TOUR 157789 2006-04-16 11:54:01Z schweikh $ |
3 4NOTE -- This is the original TOUR paper distributed with ash and 5does not represent the current state of the shell. It is provided anyway 6since it provides helpful information for how the shell is structured, 7but be warned that things have changed -- the current shell is 8still under development. 9 10================================================================ --- 6 unchanged lines hidden (view full) --- 17DIRECTORIES: The subdirectory bltin contains commands which can 18be compiled stand-alone. The rest of the source is in the main 19ash directory. 20 21SOURCE CODE GENERATORS: Files whose names begin with "mk" are 22programs that generate source code. A complete list of these 23programs is: 24 | 3 4NOTE -- This is the original TOUR paper distributed with ash and 5does not represent the current state of the shell. It is provided anyway 6since it provides helpful information for how the shell is structured, 7but be warned that things have changed -- the current shell is 8still under development. 9 10================================================================ --- 6 unchanged lines hidden (view full) --- 17DIRECTORIES: The subdirectory bltin contains commands which can 18be compiled stand-alone. The rest of the source is in the main 19ash directory. 20 21SOURCE CODE GENERATORS: Files whose names begin with "mk" are 22programs that generate source code. A complete list of these 23programs is: 24 |
25 program intput files generates 26 ------- ------------ --------- | 25 program input files generates 26 ------- ----------- --------- |
27 mkbuiltins builtins builtins.h builtins.c 28 mkinit *.c init.c 29 mknodes nodetypes nodes.h nodes.c 30 mksignames - signames.h signames.c 31 mksyntax - syntax.h syntax.c 32 mktokens - token.h 33 bltin/mkexpr unary_op binary_op operators.h operators.c 34 --- 51 unchanged lines hidden (view full) --- 86with other exceptions. After these cleanup actions, the shell 87can interpret a shell procedure itself without exec'ing a new 88copy of the shell. 89 90INTERRUPTS: In an interactive shell, an interrupt will cause an 91EXINT exception to return to the main command loop. (Exception: 92EXINT is not raised if the user traps interrupts using the trap 93command.) The INTOFF and INTON macros (defined in exception.h) | 27 mkbuiltins builtins builtins.h builtins.c 28 mkinit *.c init.c 29 mknodes nodetypes nodes.h nodes.c 30 mksignames - signames.h signames.c 31 mksyntax - syntax.h syntax.c 32 mktokens - token.h 33 bltin/mkexpr unary_op binary_op operators.h operators.c 34 --- 51 unchanged lines hidden (view full) --- 86with other exceptions. After these cleanup actions, the shell 87can interpret a shell procedure itself without exec'ing a new 88copy of the shell. 89 90INTERRUPTS: In an interactive shell, an interrupt will cause an 91EXINT exception to return to the main command loop. (Exception: 92EXINT is not raised if the user traps interrupts using the trap 93command.) The INTOFF and INTON macros (defined in exception.h) |
94provide uninterruptable critical sections. Between the execution | 94provide uninterruptible critical sections. Between the execution |
95of INTOFF and the execution of INTON, interrupt signals will be 96held for later delivery. INTOFF and INTON can be nested. 97 98MEMALLOC.C: Memalloc.c defines versions of malloc and realloc 99which call error when there is no memory left. It also defines a 100stack oriented memory allocation scheme. Allocating off a stack 101is probably more efficient than allocation using malloc, but the 102big advantage is that when an exception occurs all we have to do 103to free up the memory in use at the time of the exception is to 104restore the stack pointer. The stack is implemented using a 105linked list of blocks. 106 107STPUTC: If the stack were contiguous, it would be easy to store 108strings on the stack without knowing in advance how long the 109string was going to be: 110 p = stackptr; 111 *p++ = c; /* repeated as many times as needed */ 112 stackptr = p; | 95of INTOFF and the execution of INTON, interrupt signals will be 96held for later delivery. INTOFF and INTON can be nested. 97 98MEMALLOC.C: Memalloc.c defines versions of malloc and realloc 99which call error when there is no memory left. It also defines a 100stack oriented memory allocation scheme. Allocating off a stack 101is probably more efficient than allocation using malloc, but the 102big advantage is that when an exception occurs all we have to do 103to free up the memory in use at the time of the exception is to 104restore the stack pointer. The stack is implemented using a 105linked list of blocks. 106 107STPUTC: If the stack were contiguous, it would be easy to store 108strings on the stack without knowing in advance how long the 109string was going to be: 110 p = stackptr; 111 *p++ = c; /* repeated as many times as needed */ 112 stackptr = p; |
113The folloing three macros (defined in memalloc.h) perform these | 113The following three macros (defined in memalloc.h) perform these |
114operations, but grow the stack if you run off the end: 115 STARTSTACKSTR(p); 116 STPUTC(c, p); /* repeated as many times as needed */ 117 grabstackstr(p); 118 119We now start a top-down look at the code: 120 121MAIN.C: The main routine performs some initialization, executes | 114operations, but grow the stack if you run off the end: 115 STARTSTACKSTR(p); 116 STPUTC(c, p); /* repeated as many times as needed */ 117 grabstackstr(p); 118 119We now start a top-down look at the code: 120 121MAIN.C: The main routine performs some initialization, executes |
122the user's profile if necessary, and calls cmdloop. Cmdloop is | 122the user's profile if necessary, and calls cmdloop. Cmdloop |
123repeatedly parses and executes commands. 124 125OPTIONS.C: This file contains the option processing code. It is 126called from main to parse the shell arguments when the shell is 127invoked, and it also contains the set builtin. The -i and -j op- 128tions (the latter turns on job control) require changes in signal 129handling. The routines setjobctl (in jobs.c) and setinteractive 130(in trap.c) are called to handle changes to these options. --- 42 unchanged lines hidden (view full) --- 173 VSNORMAL $var 174 VSMINUS ${var-text} 175 VSMINUS|VSNUL ${var:-text} 176 VSPLUS ${var+text} 177 VSPLUS|VSNUL ${var:+text} 178 VSQUESTION ${var?text} 179 VSQUESTION|VSNUL ${var:?text} 180 VSASSIGN ${var=text} | 123repeatedly parses and executes commands. 124 125OPTIONS.C: This file contains the option processing code. It is 126called from main to parse the shell arguments when the shell is 127invoked, and it also contains the set builtin. The -i and -j op- 128tions (the latter turns on job control) require changes in signal 129handling. The routines setjobctl (in jobs.c) and setinteractive 130(in trap.c) are called to handle changes to these options. --- 42 unchanged lines hidden (view full) --- 173 VSNORMAL $var 174 VSMINUS ${var-text} 175 VSMINUS|VSNUL ${var:-text} 176 VSPLUS ${var+text} 177 VSPLUS|VSNUL ${var:+text} 178 VSQUESTION ${var?text} 179 VSQUESTION|VSNUL ${var:?text} 180 VSASSIGN ${var=text} |
181 VSASSIGN|VSNUL ${var=text} | 181 VSASSIGN|VSNUL ${var:=text} |
182 183In addition, the type field will have the VSQUOTE flag set if the 184variable is enclosed in double quotes. The name of the variable 185comes next, terminated by an equals sign. If the type is not 186VSNORMAL, then the text field in the substitution follows, ter- 187minated by a CTLENDVAR byte. 188 189Commands in back quotes are parsed and stored in a linked list. --- 9 unchanged lines hidden (view full) --- 199 200CTLESC characters have proved to be particularly tricky to get 201right. In the case of here documents which are not subject to 202variable and command substitution, the parser doesn't insert any 203CTLESC characters to begin with (so the contents of the text 204field can be written without any processing). Other here docu- 205ments, and words which are not subject to splitting and file name 206generation, have the CTLESC characters removed during the vari- | 182 183In addition, the type field will have the VSQUOTE flag set if the 184variable is enclosed in double quotes. The name of the variable 185comes next, terminated by an equals sign. If the type is not 186VSNORMAL, then the text field in the substitution follows, ter- 187minated by a CTLENDVAR byte. 188 189Commands in back quotes are parsed and stored in a linked list. --- 9 unchanged lines hidden (view full) --- 199 200CTLESC characters have proved to be particularly tricky to get 201right. In the case of here documents which are not subject to 202variable and command substitution, the parser doesn't insert any 203CTLESC characters to begin with (so the contents of the text 204field can be written without any processing). Other here docu- 205ments, and words which are not subject to splitting and file name 206generation, have the CTLESC characters removed during the vari- |
207able and command substitution phase. Words which are subject | 207able and command substitution phase. Words which are subject to |
208splitting and file name generation have the CTLESC characters re- 209moved as part of the file name phase. 210 211EXECUTION: Command execution is handled by the following files: 212 eval.c The top level routines. 213 redir.c Code to handle redirection of input and output. 214 jobs.c Code to handle forking, waiting, and job control. | 208splitting and file name generation have the CTLESC characters re- 209moved as part of the file name phase. 210 211EXECUTION: Command execution is handled by the following files: 212 eval.c The top level routines. 213 redir.c Code to handle redirection of input and output. 214 jobs.c Code to handle forking, waiting, and job control. |
215 exec.c Code to to path searches and the actual exec sys call. | 215 exec.c Code to do path searches and the actual exec sys call. |
216 expand.c Code to evaluate arguments. 217 var.c Maintains the variable symbol table. Called from expand.c. 218 219EVAL.C: Evaltree recursively executes a parse tree. The exit 220status is returned in the global variable exitstatus. The alter- 221native entry evalbackcmd is called to evaluate commands in back 222quotes. It saves the result in memory if the command is a buil- 223tin; otherwise it forks off a child to execute the command and 224connects the standard output of the child to a pipe. 225 226JOBS.C: To create a process, you call makejob to return a job 227structure, and then call forkshell (passing the job structure as 228an argument) to create the process. Waitforjob waits for a job 229to complete. These routines take care of process groups if job 230control is defined. 231 232REDIR.C: Ash allows file descriptors to be redirected and then 233restored without forking off a child process. This is accom- 234plished by duplicating the original file descriptors. The redir- | 216 expand.c Code to evaluate arguments. 217 var.c Maintains the variable symbol table. Called from expand.c. 218 219EVAL.C: Evaltree recursively executes a parse tree. The exit 220status is returned in the global variable exitstatus. The alter- 221native entry evalbackcmd is called to evaluate commands in back 222quotes. It saves the result in memory if the command is a buil- 223tin; otherwise it forks off a child to execute the command and 224connects the standard output of the child to a pipe. 225 226JOBS.C: To create a process, you call makejob to return a job 227structure, and then call forkshell (passing the job structure as 228an argument) to create the process. Waitforjob waits for a job 229to complete. These routines take care of process groups if job 230control is defined. 231 232REDIR.C: Ash allows file descriptors to be redirected and then 233restored without forking off a child process. This is accom- 234plished by duplicating the original file descriptors. The redir- |
235tab structure records where the file descriptors have be dupli- | 235tab structure records where the file descriptors have been dupli- |
236cated to. 237 238EXEC.C: The routine find_command locates a command, and enters 239the command in the hash table if it is not already there. The 240third argument specifies whether it is to print an error message 241if the command is not found. (When a pipeline is set up, 242find_command is called for all the commands in the pipeline be- 243fore any forking is done, so to get the commands into the hash --- 34 unchanged lines hidden (view full) --- 278out of the table and replaced with strings obtained from malloc, 279since the former will automatically be freed when the stack (see 280the entry on memalloc.c) is emptied. 281 282BUILTIN COMMANDS: The procedures for handling these are scat- 283tered throughout the code, depending on which location appears 284most appropriate. They can be recognized because their names al- 285ways end in "cmd". The mapping from names to procedures is | 236cated to. 237 238EXEC.C: The routine find_command locates a command, and enters 239the command in the hash table if it is not already there. The 240third argument specifies whether it is to print an error message 241if the command is not found. (When a pipeline is set up, 242find_command is called for all the commands in the pipeline be- 243fore any forking is done, so to get the commands into the hash --- 34 unchanged lines hidden (view full) --- 278out of the table and replaced with strings obtained from malloc, 279since the former will automatically be freed when the stack (see 280the entry on memalloc.c) is emptied. 281 282BUILTIN COMMANDS: The procedures for handling these are scat- 283tered throughout the code, depending on which location appears 284most appropriate. They can be recognized because their names al- 285ways end in "cmd". The mapping from names to procedures is |
286specified in the file builtins, which is processed by the mkbuil- 287tins command. | 286specified in the file builtins, which is processed by the mkbuilt- 287ins command. |
288 289A builtin command is invoked with argc and argv set up like a 290normal program. A builtin command is allowed to overwrite its 291arguments. Builtin routines can call nextopt to do option pars- 292ing. This is kind of like getopt, but you don't pass argc and 293argv to it. Builtin routines can also call error. This routine 294normally terminates the shell (or returns to the main command 295loop if the shell is interactive), but when called from a builtin --- 62 unchanged lines hidden --- | 288 289A builtin command is invoked with argc and argv set up like a 290normal program. A builtin command is allowed to overwrite its 291arguments. Builtin routines can call nextopt to do option pars- 292ing. This is kind of like getopt, but you don't pass argc and 293argv to it. Builtin routines can also call error. This routine 294normally terminates the shell (or returns to the main command 295loop if the shell is interactive), but when called from a builtin --- 62 unchanged lines hidden --- |