annotate toys/posix/sh.c @ 655:e235d7d575e5

Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 16:34:08 -0500
parents toys/other/toysh.c@2986aa63a021
children 6df4ccc0acbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
2 *
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
3 * sh.c - toybox shell
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
4 *
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
5 * Copyright 2006 Rob Landley <rob@landley.net>
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
6 *
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
7 * The POSIX-2008/SUSv4 spec for this is at:
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
8 * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
9 * and http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
10 *
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
11 * The first link describes the following shell builtins:
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
12 *
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
13 * break colon continue dot eval exec exit export readonly return set shift
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
14 * times trap unset
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
15 *
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
16 * The second link (the utilities directory) also contains specs for the
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
17 * following shell builtins:
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
18 *
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
19 * alias bg cd command fc fg getopts hash jobs kill read type ulimit
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
20 * umask unalias wait
198
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
21 *
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
22 * Things like the bash man page are good to read too.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
23 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
24 * TODO: // Handle embedded NUL bytes in the command line.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
25
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
26 USE_SH(NEWTOY(cd, NULL, TOYFLAG_NOFORK))
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
27 USE_SH(NEWTOY(exit, NULL, TOYFLAG_NOFORK))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
28
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
29 USE_SH(NEWTOY(sh, "c:i", TOYFLAG_BIN))
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
30 USE_SH(OLDTOY(toysh, sh, "c:i", TOYFLAG_BIN))
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
31
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
32 config SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
33 bool "sh (toysh)"
401
bc435ea49b2c Don't make toysh default to y until it does something useful.
Rob Landley <rob@landley.net>
parents: 292
diff changeset
34 default n
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
35 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
36 usage: sh [-c command] [script]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
37
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
38 Command shell. Runs a shell script, or reads input interactively
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
39 and responds to it.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
40
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
41 -c command line to execute
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
42
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
43 config SH_TTY
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
44 bool "Interactive shell (terminal control)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
45 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
46 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
47 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
48 Add terminal control to toysh. This is necessary for interactive use,
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
49 so the shell isn't killed by CTRL-C.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
50
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
51 config SH_PROFILE
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
52 bool "Profile support"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
53 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
54 depends on SH_TTY
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
55 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
56 Read /etc/profile and ~/.profile when running interactively.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
57
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
58 Also enables the built-in command "source".
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
59
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
60 config SH_JOBCTL
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
61 bool "Job Control (fg, bg, jobs)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
62 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
63 depends on SH_TTY
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
64 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
65 Add job control to toysh. This lets toysh handle CTRL-Z, and enables
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
66 the built-in commands "fg", "bg", and "jobs".
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
67
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
68 With pipe support, enable use of "&" to run background processes.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
69
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
70 config SH_FLOWCTL
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
71 bool "Flow control (if, while, for, functions)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
72 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
73 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
74 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
75 Add flow control to toysh. This enables the if/then/else/fi,
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
76 while/do/done, and for/do/done constructs.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
77
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
78 With pipe support, this enables the ability to define functions
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
79 using the "function name" or "name()" syntax, plus curly brackets
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
80 "{ }" to group commands.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
81
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
82 config SH_QUOTES
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
83 bool "Smarter argument parsing (quotes)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
84 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
85 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
86 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
87 Add support for parsing "" and '' style quotes to the toysh command
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
88 parser, with lets arguments have spaces in them.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
89
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
90 config SH_WILDCARDS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
91 bool "Wildcards ( ?*{,} )"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
92 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
93 depends on SH_QUOTES
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
94 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
95 Expand wildcards in argument names, ala "ls -l *.t?z" and
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
96 "rm subdir/{one,two,three}.txt".
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
97
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
98 config SH_PROCARGS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
99 bool "Executable arguments ( `` and $() )"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
100 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
101 depends on SH_QUOTES
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
102 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
103 Add support for executing arguments contianing $() and ``, using
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
104 the output of the command as the new argument value(s).
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
105
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
106 (Bash calls this "command substitution".)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
107
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
108 config SH_ENVVARS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
109 bool "Environment variable support"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
110 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
111 depends on SH_QUOTES
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
112 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
113 Substitute environment variable values for $VARNAME or ${VARNAME},
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
114 and enable the built-in command "export".
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
115
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
116 config SH_LOCALS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
117 bool "Local variables"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
118 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
119 depends on SH_ENVVARS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
120 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
121 Support for local variables, fancy prompts ($PS1), the "set" command,
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
122 and $?.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
123
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
124 config SH_ARRAYS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
125 bool "Array variables"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
126 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
127 depends on SH_LOCALS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
128 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
129 Support for ${blah[blah]} style array variables.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
130
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
131 config SH_PIPES
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
132 bool "Pipes and redirects ( | > >> < << & && | || () ; )"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
133 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
134 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
135 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
136 Support multiple commands on the same command line. This includes
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
137 | pipes, > >> < redirects, << here documents, || && conditional
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
138 execution, () subshells, ; sequential execution, and (with job
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
139 control) & background processes.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
140
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
141 config SH_BUILTINS
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
142 bool "Builtin commands"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
143 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
144 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
145 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
146 Adds the commands exec, fg, bg, help, jobs, pwd, export, source, set,
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
147 unset, read, alias.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
148
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
149 config EXIT
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
150 bool
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
151 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
152 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
153 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
154 usage: exit [status]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
155
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
156 Exit shell. If no return value supplied on command line, use value
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
157 of most recent command, or 0 if none.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
158
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
159 config CD
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
160 bool
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
161 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
162 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
163 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
164 usage: cd [path]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
165
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
166 Change current directory. With no arguments, go to $HOME.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
167
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
168 config CD_P
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
169 bool # "-P support for cd"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
170 default n
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
171 depends on SH
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
172 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
173 usage: cd [-PL]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
174
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
175 -P Physical path: resolve symlinks in path.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
176 -L Cancel previous -P and restore default behavior.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
177 */
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
178
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
179 #include "toys.h"
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
180
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
181 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
182 char *command;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
183 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
184
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
185 #define TT this.sh
146
99e651512aa4 Get toysh.c to start using the option parsing logic, and some minor cleanup.
Rob Landley <rob@landley.net>
parents: 121
diff changeset
186
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
187 // A single executable, its arguments, and other information we know about it.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
188 #define TOYSH_FLAG_EXIT 1
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
189 #define TOYSH_FLAG_SUSPEND 2
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
190 #define TOYSH_FLAG_PIPE 4
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
191 #define TOYSH_FLAG_AND 8
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
192 #define TOYSH_FLAG_OR 16
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
193 #define TOYSH_FLAG_AMP 32
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
194 #define TOYSH_FLAG_SEMI 64
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
195 #define TOYSH_FLAG_PAREN 128
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
196
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
197 // What we know about a single process.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
198 struct command {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
199 struct command *next;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
200 int flags; // exit, suspend, && ||
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
201 int pid; // pid (or exit code)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
202 int argc;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
203 char *argv[0];
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
204 };
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
205
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
206 // A collection of processes piped into/waiting on each other.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
207 struct pipeline {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
208 struct pipeline *next;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
209 int job_id;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
210 struct command *cmd;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
211 char *cmdline; // Unparsed line for display purposes
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
212 int cmdlinelen; // How long is cmdline?
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
213 };
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
214
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
215 // Parse one word from the command line, appending one or more argv[] entries
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
216 // to struct command. Handles environment variable substitution and
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
217 // substrings. Returns pointer to next used byte, or NULL if it
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
218 // hit an ending token.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
219 static char *parse_word(char *start, struct command **cmd)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
220 {
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
221 char *end;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
222
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
223 // Detect end of line (and truncate line at comment)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
224 if (CFG_TOYSH_PIPES && strchr("><&|(;", *start)) return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
225
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
226 // Grab next word. (Add dequote and envvar logic here)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
227 end = start;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
228 while (*end && !isspace(*end)) end++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
229 (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
230
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
231 // Allocate more space if there's no room for NULL terminator.
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
232
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
233 if (!((*cmd)->argc & 7))
115
19b5567f0a1b Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
Rob Landley <rob@landley.net>
parents: 90
diff changeset
234 *cmd=xrealloc(*cmd,
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
235 sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
236 (*cmd)->argv[(*cmd)->argc] = 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
237 return end;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
238 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
239
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
240 // Parse a line of text into a pipeline.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
241 // Returns a pointer to the next line.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
242
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
243 static char *parse_pipeline(char *cmdline, struct pipeline *line)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
244 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
245 struct command **cmd = &(line->cmd);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
246 char *start = line->cmdline = cmdline;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
247
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
248 if (!cmdline) return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
249
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
250 if (CFG_TOYSH_JOBCTL) line->cmdline = cmdline;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
251
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
252 // Parse command into argv[]
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
253 for (;;) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
254 char *end;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
255
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
256 // Skip leading whitespace and detect end of line.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
257 while (isspace(*start)) start++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
258 if (!*start || *start=='#') {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
259 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
260 return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
261 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
262
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
263 // Allocate next command structure if necessary
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
264 if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
265
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
266 // Parse next argument and add the results to argv[]
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
267 end = parse_word(start, cmd);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
268
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
269 // If we hit the end of this command, how did it end?
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
270 if (!end) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
271 if (CFG_TOYSH_PIPES && *start) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
272 if (*start==';') {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
273 start++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
274 break;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
275 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
276 // handle | & < > >> << || &&
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
277 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
278 break;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
279 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
280 start = end;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
281 }
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
282
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
283 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
284
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
285 return start;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
286 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
287
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
288 // Execute the commands in a pipeline
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
289 static void run_pipeline(struct pipeline *line)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
290 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
291 struct toy_list *tl;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
292 struct command *cmd = line->cmd;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
293 if (!cmd || !cmd->argc) return;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
294
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
295 tl = toy_find(cmd->argv[0]);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
296 // Is this command a builtin that should run in this process?
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
297 if (tl && (tl->flags & TOYFLAG_NOFORK)) {
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
298 struct toy_context temp;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
299
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
300 // This fakes lots of what toybox_main() does.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
301 memcpy(&temp, &toys, sizeof(struct toy_context));
480
f558dce66095 Nathan McSween convinced me compilers that inline memset() can optimize the bzero case pretty well.
Rob Landley <rob@landley.net>
parents: 401
diff changeset
302 memset(&toys, 0, sizeof(struct toy_context));
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
303 toy_init(tl, cmd->argv);
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
304 tl->toy_main();
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
305 cmd->pid = toys.exitval;
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
306 free(toys.optargs);
257
951110c37fee Add TOYFLAG_UMASK.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
307 if (toys.old_umask) umask(toys.old_umask);
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
308 memcpy(&toys, &temp, sizeof(struct toy_context));
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
309 } else {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
310 int status;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
311
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
312 cmd->pid = vfork();
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
313 if (!cmd->pid) xexec(cmd->argv);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
314 else waitpid(cmd->pid, &status, 0);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
315
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
316 if (CFG_TOYSH_FLOWCTL || CFG_TOYSH_PIPES) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
317 if (WIFEXITED(status)) cmd->pid = WEXITSTATUS(status);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
318 if (WIFSIGNALED(status)) cmd->pid = WTERMSIG(status);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
319 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
320 }
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
321
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
322 return;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
323 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
324
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
325 // Free the contents of a command structure
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
326 static void free_cmd(void *data)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
327 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
328 struct command *cmd=(struct command *)data;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
329
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
330 while(cmd->argc) free(cmd->argv[--cmd->argc]);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
331 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
332
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
333
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
334 // Parse a command line and do what it says to do.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
335 static void handle(char *command)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
336 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
337 struct pipeline line;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
338 char *start = command;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
339
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
340 // Loop through commands in this line
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
341
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
342 for (;;) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
343
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
344 // Parse a group of connected commands
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
345
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
346 memset(&line,0,sizeof(struct pipeline));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
347 start = parse_pipeline(start, &line);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
348 if (!line.cmd) break;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
349
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
350 // Run those commands
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
351
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
352 run_pipeline(&line);
624
1e8b9acdafeb Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents: 560
diff changeset
353 llist_traverse(line.cmd, free_cmd);
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
354 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
355 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
356
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
357 void cd_main(void)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
358 {
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
359 char *dest = *toys.optargs ? *toys.optargs : getenv("HOME");
292
b4077be6c746 Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
Rob Landley <rob@landley.net>
parents: 257
diff changeset
360 xchdir(dest);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
361 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
362
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
363 void exit_main(void)
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
364 {
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
365 exit(*toys.optargs ? atoi(*toys.optargs) : 0);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
366 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
367
655
e235d7d575e5 Toysh is our posix sh, so move from "other" to "posix" and use sh as the base command name.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
368 void sh_main(void)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
369 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
370 FILE *f;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
371
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
372 // Set up signal handlers and grab control of this tty.
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
373 if (CFG_TOYSH_TTY) {
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
374 if (isatty(0)) toys.optflags |= 1;
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
375 }
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
376 f = *toys.optargs ? xfopen(*toys.optargs, "r") : NULL;
146
99e651512aa4 Get toysh.c to start using the option parsing logic, and some minor cleanup.
Rob Landley <rob@landley.net>
parents: 121
diff changeset
377 if (TT.command) handle(TT.command);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
378 else {
165
ad48dca1f4c5 Zap a warning.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
379 size_t cmdlen = 0;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
380 for (;;) {
146
99e651512aa4 Get toysh.c to start using the option parsing logic, and some minor cleanup.
Rob Landley <rob@landley.net>
parents: 121
diff changeset
381 char *command = 0;
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
382 if (!f) xputc('$');
560
979c5ab6f0c2 Replace ?: gcc extension with standard code
Kevin Chase <kevincha99@hotmail.com>
parents: 480
diff changeset
383 if (1 > getline(&command, &cmdlen, f ? f : stdin)) break;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
384 handle(command);
146
99e651512aa4 Get toysh.c to start using the option parsing logic, and some minor cleanup.
Rob Landley <rob@landley.net>
parents: 121
diff changeset
385 free(command);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
386 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
387 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
388
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
389 toys.exitval = 1;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
390 }