Mercurial > hg > toybox
annotate toys/toysh.c @ 115:19b5567f0a1b
Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
compiler so much.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 29 Apr 2007 19:55:21 -0400 |
parents | 7c77c6ec17ee |
children | 933766b0bd4b |
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: |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
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 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
9 * |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
10 * Although things like the bash man page are good to read too. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
11 */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
12 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
13 // Handle embedded NUL bytes in the command line. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
14 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
15 #include "toys.h" |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
16 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
17 // 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
|
18 #define TOYSH_FLAG_EXIT 1 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
19 #define TOYSH_FLAG_SUSPEND 2 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
20 #define TOYSH_FLAG_PIPE 4 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
21 #define TOYSH_FLAG_AND 8 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
22 #define TOYSH_FLAG_OR 16 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
23 #define TOYSH_FLAG_AMP 32 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
24 #define TOYSH_FLAG_SEMI 64 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
25 #define TOYSH_FLAG_PAREN 128 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
26 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
27 // What we know about a single process. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
28 struct command { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
29 struct command *next; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
30 int flags; // exit, suspend, && || |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
31 int pid; // pid (or exit code) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
32 int argc; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
33 char *argv[0]; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
34 }; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
35 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
36 // A collection of processes piped into/waiting on each other. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
37 struct pipeline { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
38 struct pipeline *next; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
39 int job_id; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
40 struct command *cmd; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
41 char *cmdline; // Unparsed line for display purposes |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
42 int cmdlinelen; // How long is cmdline? |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
43 }; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
44 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
45 // 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
|
46 // to struct command. Handles environment variable substitution and |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
47 // 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
|
48 // hit an ending token. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
49 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
|
50 { |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
51 char *end; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
52 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
53 // Detect end of line (and truncate line at comment) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
54 if (CFG_TOYSH_PIPES && strchr("><&|(;", *start)) return 0; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
55 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
56 // Grab next word. (Add dequote and envvar logic here) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
57 end = start; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
58 while (*end && !isspace(*end)) end++; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
59 (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
60 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
61 // 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
|
62 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
63 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
|
64 *cmd=xrealloc(*cmd, |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
65 sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *)); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
66 (*cmd)->argv[(*cmd)->argc] = 0; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
67 return end; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
68 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
69 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
70 // Parse a line of text into a pipeline. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
71 // Returns a pointer to the next line. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
72 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
73 static char *parse_pipeline(char *cmdline, struct pipeline *line) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
74 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
75 struct command **cmd = &(line->cmd); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
76 char *start = line->cmdline = cmdline; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
77 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
78 if (!cmdline) return 0; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
79 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
80 if (CFG_TOYSH_JOBCTL) line->cmdline = cmdline; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
81 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
82 // Parse command into argv[] |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
83 for (;;) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
84 char *end; |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
85 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
86 // Skip leading whitespace and detect end of line. |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
87 while (isspace(*start)) start++; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
88 if (!*start || *start=='#') { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
89 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
90 return 0; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
91 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
92 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
93 // Allocate next command structure if necessary |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
94 if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *)); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
95 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
96 // Parse next argument and add the results to argv[] |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
97 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
|
98 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
99 // 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
|
100 if (!end) { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
101 if (CFG_TOYSH_PIPES && *start) { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
102 if (*start==';') { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
103 start++; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
104 break; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
105 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
106 // handle | & < > >> << || && |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
107 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
108 break; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
109 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
110 start = end; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
111 } |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
112 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
113 if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
114 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
115 return start; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
116 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
117 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
118 // Execute the commands in a pipeline |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
119 static void run_pipeline(struct pipeline *line) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
120 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
121 struct toy_list *tl; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
122 struct command *cmd = line->cmd; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
123 if (!cmd || !cmd->argc) return; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
124 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
125 tl = toy_find(cmd->argv[0]); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
126 // 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
|
127 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
|
128 struct toy_context temp; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
129 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
7
diff
changeset
|
130 // 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
|
131 memcpy(&temp, &toys, sizeof(struct toy_context)); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
7
diff
changeset
|
132 bzero(&toys, sizeof(struct toy_context)); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
133 toy_init(tl, cmd->argv); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
134 cmd->pid = tl->toy_main(); |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
7
diff
changeset
|
135 free(toys.optargs); |
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
7
diff
changeset
|
136 memcpy(&toys, &temp, sizeof(struct toy_context)); |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
137 } else { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
138 int status; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
139 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
140 cmd->pid = vfork(); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
141 if (!cmd->pid) xexec(cmd->argv); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
142 else waitpid(cmd->pid, &status, 0); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
143 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
144 if (CFG_TOYSH_FLOWCTL || CFG_TOYSH_PIPES) { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
145 if (WIFEXITED(status)) cmd->pid = WEXITSTATUS(status); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
146 if (WIFSIGNALED(status)) cmd->pid = WTERMSIG(status); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
147 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
148 } |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
149 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
150 return; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
151 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
152 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
153 // Free the contents of a command structure |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
154 static void free_cmd(void *data) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
155 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
156 struct command *cmd=(struct command *)data; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
157 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
158 while(cmd->argc) free(cmd->argv[--cmd->argc]); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
159 } |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
160 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
161 |
3
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
162 // 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
|
163 static void handle(char *command) |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
164 { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
165 struct pipeline line; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
166 char *start = command; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
167 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
168 // Loop through commands in this line |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
169 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
170 for (;;) { |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
171 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
172 // Parse a group of connected commands |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
173 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
174 memset(&line,0,sizeof(struct pipeline)); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
175 start = parse_pipeline(start, &line); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
176 if (!line.cmd) break; |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
177 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
178 // Run those commands |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
179 |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
180 run_pipeline(&line); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
181 llist_free(line.cmd, free_cmd); |
266a462ed18c
Next drop of toysh, plus more infratructure.
landley@driftwood
parents:
2
diff
changeset
|
182 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
183 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
184 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
185 int cd_main(void) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
186 { |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
3
diff
changeset
|
187 char *dest = toys.argv[1] ? toys.argv[1]: getenv("HOME"); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
188 if (chdir(dest)) error_exit("chdir %s",dest); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
189 return 0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
190 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
191 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
192 int exit_main(void) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
193 { |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
3
diff
changeset
|
194 exit(toys.argv[1] ? atoi(toys.argv[1]) : 0); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
195 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
196 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
197 int toysh_main(void) |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
198 { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
199 char *command=NULL; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
200 FILE *f; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
201 |
25
eb46bb5626cb
New option parsing infrastructure (doesn't use getopt). Hook it up to
Rob Landley <rob@landley.net>
parents:
7
diff
changeset
|
202 // 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
|
203 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
|
204 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
|
205 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
206 f = toys.argv[1] ? xfopen(toys.argv[1], "r") : NULL; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
207 if (command) handle(command); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
208 else { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
209 unsigned cmdlen=0; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
210 for (;;) { |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
211 if (!f) putchar('$'); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
212 if (1 > getline(&command, &cmdlen, f ? : stdin)) break; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
213 handle(command); |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
214 } |
90
7c77c6ec17ee
Add "make defconfig". Modify global options to start with CONFIG_TOYBOX_.
Rob Landley <rob@landley.net>
parents:
25
diff
changeset
|
215 if (CFG_TOYBOX_FREE) free(command); |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
216 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
217 |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
218 return 1; |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
diff
changeset
|
219 } |