annotate toys/toysh.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 979c5ab6f0c2
children 1e8b9acdafeb
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 *
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
3 * toysh - toybox shell
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 *
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
7 * The spec for this is at:
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
8 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
198
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
9 * and http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
10 *
198
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
11 * There are also specs for:
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
12 * http://www.opengroup.org/onlinepubs/009695399/utilities/cd.html
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 * http://www.opengroup.org/onlinepubs/009695399/utilities/exit.html
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
14 *
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
15 * 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
16 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
17 * 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
18
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
19 USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
20 USE_TOYSH(NEWTOY(exit, NULL, TOYFLAG_NOFORK))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
21 USE_TOYSH(OLDTOY(sh, toysh, "c:i", TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
22 USE_TOYSH(NEWTOY(toysh, "c:i", TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
23
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
24 config TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
25 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
26 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
27 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
28 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
29
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
30 The toybox command shell. Runs a shell script, or else reads input
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
31 interactively and responds to it.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
32
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
33 -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
34
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
35 config TOYSH_TTY
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
36 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
37 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
38 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
39 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
40 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
41 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
42
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
43 config TOYSH_PROFILE
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 "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
45 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
46 depends on TOYSH_TTY
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 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
49
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
50 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
51
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
52 config TOYSH_JOBCTL
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
53 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
54 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
55 depends on TOYSH_TTY
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
56 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
57 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
58 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
59
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
60 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
61
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
62 config TOYSH_FLOWCTL
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
63 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
64 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
65 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
66 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
67 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
68 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
69
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
70 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
71 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
72 "{ }" 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
73
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
74 config TOYSH_QUOTES
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
75 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
76 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
77 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
78 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
79 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
80 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
81
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
82 config TOYSH_WILDCARDS
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 "Wildcards ( ?*{,} )"
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
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
85 depends on TOYSH_QUOTES
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 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
88 "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
89
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
90 config TOYSH_PROCARGS
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 "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
92 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
93 depends on TOYSH_QUOTES
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 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
96 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
97
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
98 (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
99
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
100 config TOYSH_ENVVARS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
101 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
102 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
103 depends on TOYSH_QUOTES
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
104 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
105 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
106 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
107
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
108 config TOYSH_LOCALS
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 "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
110 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
111 depends on TOYSH_ENVVARS
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 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
114 and $?.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
115
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
116 config TOYSH_ARRAYS
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 "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
118 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
119 depends on TOYSH_LOCALS
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 ${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
122
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
123 config TOYSH_PIPES
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
124 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
125 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
126 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
127 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
128 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
129 | 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
130 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
131 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
132
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
133 config TOYSH_BUILTINS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
134 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
135 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
136 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
137 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
138 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
139 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
140
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
141 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
142 bool
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
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
144 depends on TOYSH
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 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
147
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
148 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
149 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
150
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
151 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
152 bool
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
153 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
154 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
155 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
156 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
157
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
158 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
159
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
160 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
161 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
162 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
163 depends on TOYSH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
164 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
165 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
166
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
167 -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
168 -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
169 */
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
170
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
171 #include "toys.h"
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
172
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
173 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
174 char *command;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
175 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
176
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
177 #define TT this.toysh
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
178
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
179 // 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
180 #define TOYSH_FLAG_EXIT 1
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
181 #define TOYSH_FLAG_SUSPEND 2
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
182 #define TOYSH_FLAG_PIPE 4
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
183 #define TOYSH_FLAG_AND 8
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
184 #define TOYSH_FLAG_OR 16
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
185 #define TOYSH_FLAG_AMP 32
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
186 #define TOYSH_FLAG_SEMI 64
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
187 #define TOYSH_FLAG_PAREN 128
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
188
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
189 // What we know about a single process.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
190 struct command {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
191 struct command *next;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
192 int flags; // exit, suspend, && ||
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
193 int pid; // pid (or exit code)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
194 int argc;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
195 char *argv[0];
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
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
198 // A collection of processes piped into/waiting on each other.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
199 struct pipeline {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
200 struct pipeline *next;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
201 int job_id;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
202 struct command *cmd;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
203 char *cmdline; // Unparsed line for display purposes
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
204 int cmdlinelen; // How long is cmdline?
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
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
207 // 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
208 // to struct command. Handles environment variable substitution and
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
209 // 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
210 // hit an ending token.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
211 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
212 {
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
213 char *end;
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 // Detect end of line (and truncate line at comment)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
216 if (CFG_TOYSH_PIPES && strchr("><&|(;", *start)) return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
217
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
218 // Grab next word. (Add dequote and envvar logic here)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
219 end = start;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
220 while (*end && !isspace(*end)) end++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
221 (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start);
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 // 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
224
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
225 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
226 *cmd=xrealloc(*cmd,
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
227 sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
228 (*cmd)->argv[(*cmd)->argc] = 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
229 return end;
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
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
232 // Parse a line of text into a pipeline.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
233 // Returns a pointer to the next line.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
234
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
235 static char *parse_pipeline(char *cmdline, struct pipeline *line)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
236 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
237 struct command **cmd = &(line->cmd);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
238 char *start = line->cmdline = cmdline;
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 if (!cmdline) return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
241
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
242 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
243
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
244 // Parse command into argv[]
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
245 for (;;) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
246 char *end;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
247
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
248 // Skip leading whitespace and detect end of line.
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
249 while (isspace(*start)) start++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
250 if (!*start || *start=='#') {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
251 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
252 return 0;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
253 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
254
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
255 // Allocate next command structure if necessary
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
256 if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
257
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
258 // Parse next argument and add the results to argv[]
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
259 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
260
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
261 // 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
262 if (!end) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
263 if (CFG_TOYSH_PIPES && *start) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
264 if (*start==';') {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
265 start++;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
266 break;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
267 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
268 // handle | & < > >> << || &&
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
269 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
270 break;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
271 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
272 start = end;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
273 }
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
274
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
275 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
276
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
277 return start;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
278 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
279
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
280 // Execute the commands in a pipeline
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
281 static void run_pipeline(struct pipeline *line)
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 struct toy_list *tl;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
284 struct command *cmd = line->cmd;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
285 if (!cmd || !cmd->argc) return;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
286
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
287 tl = toy_find(cmd->argv[0]);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
288 // 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
289 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
290 struct toy_context temp;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
291
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
292 // 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
293 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
294 memset(&toys, 0, sizeof(struct toy_context));
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
295 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
296 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
297 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
298 free(toys.optargs);
257
951110c37fee Add TOYFLAG_UMASK.
Rob Landley <rob@landley.net>
parents: 237
diff changeset
299 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
300 memcpy(&toys, &temp, sizeof(struct toy_context));
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
301 } else {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
302 int status;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
303
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
304 cmd->pid = vfork();
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
305 if (!cmd->pid) xexec(cmd->argv);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
306 else waitpid(cmd->pid, &status, 0);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
307
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
308 if (CFG_TOYSH_FLOWCTL || CFG_TOYSH_PIPES) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
309 if (WIFEXITED(status)) cmd->pid = WEXITSTATUS(status);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
310 if (WIFSIGNALED(status)) cmd->pid = WTERMSIG(status);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
311 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
312 }
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
313
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
314 return;
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
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
317 // Free the contents of a command structure
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
318 static void free_cmd(void *data)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
319 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
320 struct command *cmd=(struct command *)data;
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 while(cmd->argc) free(cmd->argv[--cmd->argc]);
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
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
325
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
326 // 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
327 static void handle(char *command)
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
328 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
329 struct pipeline line;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
330 char *start = command;
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 // Loop through commands in this line
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
333
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
334 for (;;) {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
335
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
336 // Parse a group of connected commands
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
337
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
338 memset(&line,0,sizeof(struct pipeline));
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
339 start = parse_pipeline(start, &line);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
340 if (!line.cmd) break;
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 // Run those commands
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 run_pipeline(&line);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
345 llist_free(line.cmd, free_cmd);
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
346 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
347 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
348
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
349 void cd_main(void)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
350 {
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
351 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
352 xchdir(dest);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
353 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
354
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
355 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
356 {
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
357 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
358 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
359
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
360 void toysh_main(void)
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 FILE *f;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
363
25
eb46bb5626cb New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents: 7
diff changeset
364 // 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
365 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
366 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
367 }
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 115
diff changeset
368 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
369 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
370 else {
165
ad48dca1f4c5 Zap a warning.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
371 size_t cmdlen = 0;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
372 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
373 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
374 if (!f) xputc('$');
560
979c5ab6f0c2 Replace ?: gcc extension with standard code
Kevin Chase <kevincha99@hotmail.com>
parents: 480
diff changeset
375 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
376 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
377 free(command);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
378 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
379 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 146
diff changeset
380
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 165
diff changeset
381 toys.exitval = 1;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff changeset
382 }