expand: Support multi-byte characters during field splitting
[dash.git] / src / main.c
blob1e192f8f7909e9ca4602ecf75e030cad34596b56
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <locale.h>
36 #include <stdio.h>
37 #include <signal.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <fcntl.h>
43 #include "shell.h"
44 #include "main.h"
45 #include "mail.h"
46 #include "options.h"
47 #include "output.h"
48 #include "parser.h"
49 #include "nodes.h"
50 #include "expand.h"
51 #include "eval.h"
52 #include "jobs.h"
53 #include "input.h"
54 #include "trap.h"
55 #include "var.h"
56 #include "show.h"
57 #include "memalloc.h"
58 #include "error.h"
59 #include "init.h"
60 #include "mystring.h"
61 #include "exec.h"
62 #include "cd.h"
64 #define PROFILE 0
66 int rootpid;
67 int shlvl;
68 #ifdef __GLIBC__
69 int *dash_errno;
70 #endif
71 #if PROFILE
72 short profile_buf[16384];
73 extern int etext();
74 #endif
75 MKINIT struct jmploc main_handler;
77 STATIC void read_profile(const char *);
78 STATIC char *find_dot_file(char *);
79 static int cmdloop(int);
80 int main(int, char **);
83 * Main routine. We initialize things, parse the arguments, execute
84 * profiles if we're a login shell, and then call cmdloop to execute
85 * commands. The setjmp call sets up the location to jump to when an
86 * exception occurs. When an exception occurs the variable "state"
87 * is used to figure out how far we had gotten.
90 int
91 main(int argc, char **argv)
93 char *shinit;
94 volatile int state;
95 struct stackmark smark;
96 int login;
98 #ifdef __GLIBC__
99 dash_errno = __errno_location();
100 #endif
102 #if PROFILE
103 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
104 #endif
106 setlocale(LC_ALL, "");
108 state = 0;
109 if (unlikely(setjmp(main_handler.loc))) {
110 int e;
111 int s;
113 exitreset();
115 e = exception;
117 s = state;
118 if (e == EXEND || e == EXEXIT || s == 0 || iflag == 0 || shlvl)
119 goto exit;
121 reset();
123 if (e == EXINT
124 #if ATTY
125 && (! attyset() || equal(termval(), "emacs"))
126 #endif
128 out2c('\n');
129 #ifdef FLUSHERR
130 flushout(out2);
131 #endif
133 popstackmark(&smark);
134 FORCEINTON; /* enable interrupts */
135 if (s == 1)
136 goto state1;
137 else if (s == 2)
138 goto state2;
139 else if (s == 3)
140 goto state3;
141 else
142 goto state4;
144 handler = &main_handler;
145 #ifdef DEBUG
146 opentrace();
147 trputs("Shell args: "); trargs(argv);
148 #endif
149 rootpid = getpid();
150 init();
151 setstackmark(&smark);
152 login = procargs(argc, argv);
153 if (login) {
154 state = 1;
155 read_profile("/etc/profile");
156 state1:
157 state = 2;
158 read_profile("$HOME/.profile");
160 state2:
161 state = 3;
162 if (
163 #ifndef linux
164 getuid() == geteuid() && getgid() == getegid() &&
165 #endif
166 iflag
168 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
169 read_profile(shinit);
172 popstackmark(&smark);
173 state3:
174 state = 4;
175 if (minusc)
176 evalstring(minusc, sflag ? 0 : EV_EXIT);
178 if (sflag || minusc == NULL) {
179 state4: /* XXX ??? - why isn't this before the "if" statement */
180 cmdloop(1);
182 exit:
183 #if PROFILE
184 monitor(0);
185 #endif
186 #if GPROF
188 extern void _mcleanup(void);
189 _mcleanup();
191 #endif
192 exitshell();
193 /* NOTREACHED */
198 * Read and execute commands. "Top" is nonzero for the top level command
199 * loop; it turns on prompting if the shell is interactive.
202 static int
203 cmdloop(int top)
205 union node *n;
206 struct stackmark smark;
207 int inter;
208 int status = 0;
209 int numeof = 0;
211 TRACE(("cmdloop(%d) called\n", top));
212 for (;;) {
213 int skip;
215 setstackmark(&smark);
216 if (jobctl)
217 showjobs(out2, SHOW_CHANGED);
218 inter = 0;
219 if (iflag && top) {
220 inter++;
221 chkmail();
223 n = parsecmd(inter);
224 /* showtree(n); DEBUG */
225 if (n == NEOF) {
226 if (!top || numeof >= 50)
227 break;
228 if (!stoppedjobs()) {
229 if (!Iflag) {
230 if (iflag) {
231 out2c('\n');
232 #ifdef FLUSHERR
233 flushout(out2);
234 #endif
236 break;
238 out2str("\nUse \"exit\" to leave shell.\n");
240 numeof++;
241 } else {
242 int i;
244 job_warning = (job_warning == 2) ? 1 : 0;
245 numeof = 0;
246 i = evaltree(n, 0);
247 if (n)
248 status = i;
250 popstackmark(&smark);
252 skip = evalskip;
253 if (skip) {
254 evalskip &= ~(SKIPFUNC | SKIPFUNCDEF);
255 break;
259 return status;
265 * Read /etc/profile or .profile. Return on error.
268 STATIC void
269 read_profile(const char *name)
271 name = expandstr(name);
272 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
273 return;
275 cmdloop(0);
276 popfile();
282 * Read a file containing shell functions.
285 void
286 readcmdfile(char *name)
288 setinputfile(name, INPUT_PUSH_FILE);
289 cmdloop(0);
290 popfile();
296 * Take commands from a file. To be compatible we should do a path
297 * search for the file, which is necessary to find sub-commands.
301 STATIC char *
302 find_dot_file(char *basename)
304 char *fullname;
305 const char *path = pathval();
306 struct stat64 statb;
307 int len;
309 /* don't try this for absolute or relative paths */
310 if (strchr(basename, '/'))
311 return basename;
313 while ((len = padvance(&path, basename)) >= 0) {
314 fullname = stackblock();
315 if ((!pathopt || *pathopt == 'f') &&
316 !stat64(fullname, &statb) && S_ISREG(statb.st_mode)) {
317 /* This will be freed by the caller. */
318 return stalloc(len);
322 /* not found in the PATH */
323 sh_error("%s: not found", basename);
324 /* NOTREACHED */
328 dotcmd(int argc, char **argv)
330 int status = 0;
332 nextopt(nullstr);
333 argv = argptr;
335 if (*argv) {
336 char *fullname;
338 fullname = find_dot_file(*argv);
339 setinputfile(fullname, INPUT_PUSH_FILE);
340 commandname = fullname;
341 status = cmdloop(0);
342 popfile();
345 return status;
350 exitcmd(int argc, char **argv)
352 if (stoppedjobs())
353 return 0;
355 if (argc > 1)
356 savestatus = number(argv[1]);
358 exraise(EXEXIT);
359 /* NOTREACHED */
362 #ifdef mkinit
363 INCLUDE "error.h"
365 FORKRESET {
366 handler = &main_handler;
368 #endif