main.c revision 223060
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3420425Sstevestatic char const copyright[] = 351556Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 361556Srgrimes The Regents of the University of California. All rights reserved.\n"; 371556Srgrimes#endif /* not lint */ 381556Srgrimes 391556Srgrimes#ifndef lint 4036150Scharnier#if 0 4136150Scharnierstatic char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95"; 4236150Scharnier#endif 431556Srgrimes#endif /* not lint */ 4499110Sobrien#include <sys/cdefs.h> 4599110Sobrien__FBSDID("$FreeBSD: head/bin/sh/main.c 223060 2011-06-13 21:03:27Z jilles $"); 461556Srgrimes 4717987Speter#include <stdio.h> 481556Srgrimes#include <signal.h> 4917987Speter#include <sys/stat.h> 5017987Speter#include <unistd.h> 511556Srgrimes#include <fcntl.h> 5217525Sache#include <locale.h> 5359214Simp#include <errno.h> 5417525Sache 551556Srgrimes#include "shell.h" 561556Srgrimes#include "main.h" 571556Srgrimes#include "mail.h" 581556Srgrimes#include "options.h" 591556Srgrimes#include "output.h" 601556Srgrimes#include "parser.h" 611556Srgrimes#include "nodes.h" 6217987Speter#include "expand.h" 631556Srgrimes#include "eval.h" 641556Srgrimes#include "jobs.h" 651556Srgrimes#include "input.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "var.h" 6817987Speter#include "show.h" 691556Srgrimes#include "memalloc.h" 701556Srgrimes#include "error.h" 711556Srgrimes#include "init.h" 721556Srgrimes#include "mystring.h" 7317987Speter#include "exec.h" 7420425Ssteve#include "cd.h" 75223060Sjilles#include "builtins.h" 761556Srgrimes 771556Srgrimesint rootpid; 781556Srgrimesint rootshell; 79200998Sjillesstruct jmploc main_handler; 80221669Sjillesint localeisutf8, initial_localeisutf8; 811556Srgrimes 82222957Sjillesstatic void read_profile(char *); 83213811Sobrienstatic char *find_dot_file(char *); 841556Srgrimes 851556Srgrimes/* 861556Srgrimes * Main routine. We initialize things, parse the arguments, execute 871556Srgrimes * profiles if we're a login shell, and then call cmdloop to execute 881556Srgrimes * commands. The setjmp call sets up the location to jump to when an 891556Srgrimes * exception occurs. When an exception occurs the variable "state" 901556Srgrimes * is used to figure out how far we had gotten. 911556Srgrimes */ 921556Srgrimes 9317987Speterint 9490111Simpmain(int argc, char *argv[]) 9517987Speter{ 96222957Sjilles struct stackmark smark, smark2; 971556Srgrimes volatile int state; 981556Srgrimes char *shinit; 991556Srgrimes 10017525Sache (void) setlocale(LC_ALL, ""); 101221669Sjilles initcharset(); 1021556Srgrimes state = 0; 103200998Sjilles if (setjmp(main_handler.loc)) { 10420425Ssteve switch (exception) { 10520425Ssteve case EXEXEC: 10620425Ssteve exitstatus = exerrno; 10720425Ssteve break; 10820425Ssteve 10920425Ssteve case EXERROR: 11020425Ssteve exitstatus = 2; 11120425Ssteve break; 11220425Ssteve 11320425Ssteve default: 11420425Ssteve break; 11520425Ssteve } 11620425Ssteve 117220978Sjilles if (state == 0 || iflag == 0 || ! rootshell || 118220978Sjilles exception == EXEXIT) 119218306Sjilles exitshell(exitstatus); 1201556Srgrimes reset(); 121215567Sjilles if (exception == EXINT) 122215567Sjilles out2fmt_flush("\n"); 1231556Srgrimes popstackmark(&smark); 1241556Srgrimes FORCEINTON; /* enable interrupts */ 1251556Srgrimes if (state == 1) 1261556Srgrimes goto state1; 1271556Srgrimes else if (state == 2) 1281556Srgrimes goto state2; 1291556Srgrimes else if (state == 3) 1301556Srgrimes goto state3; 1311556Srgrimes else 1321556Srgrimes goto state4; 1331556Srgrimes } 134200998Sjilles handler = &main_handler; 1351556Srgrimes#ifdef DEBUG 1361556Srgrimes opentrace(); 1371556Srgrimes trputs("Shell args: "); trargs(argv); 1381556Srgrimes#endif 1391556Srgrimes rootpid = getpid(); 1401556Srgrimes rootshell = 1; 1411556Srgrimes init(); 1421556Srgrimes setstackmark(&smark); 143222957Sjilles setstackmark(&smark2); 1441556Srgrimes procargs(argc, argv); 145206759Sjilles pwd_init(iflag); 146203576Sjilles if (iflag) 147203576Sjilles chkmail(1); 1481556Srgrimes if (argv[0] && argv[0][0] == '-') { 1491556Srgrimes state = 1; 1501556Srgrimes read_profile("/etc/profile"); 1511556Srgrimesstate1: 1521556Srgrimes state = 2; 15319240Ssteve if (privileged == 0) 154223005Sjilles read_profile("${HOME-}/.profile"); 15519240Ssteve else 15619240Ssteve read_profile("/etc/suid_profile"); 1578855Srgrimes } 1581556Srgrimesstate2: 1591556Srgrimes state = 3; 16025471Ssteve if (!privileged && iflag) { 16117987Speter if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 16217987Speter state = 3; 16317987Speter read_profile(shinit); 16417987Speter } 1651556Srgrimes } 1661556Srgrimesstate3: 1671556Srgrimes state = 4; 168222957Sjilles popstackmark(&smark2); 1691556Srgrimes if (minusc) { 170194128Sjilles evalstring(minusc, sflag ? 0 : EV_EXIT); 1711556Srgrimes } 1721556Srgrimes if (sflag || minusc == NULL) { 1731556Srgrimesstate4: /* XXX ??? - why isn't this before the "if" statement */ 1741556Srgrimes cmdloop(1); 1751556Srgrimes } 1761556Srgrimes exitshell(exitstatus); 17717987Speter /*NOTREACHED*/ 17817987Speter return 0; 1791556Srgrimes} 1801556Srgrimes 1811556Srgrimes 1821556Srgrimes/* 1831556Srgrimes * Read and execute commands. "Top" is nonzero for the top level command 1841556Srgrimes * loop; it turns on prompting if the shell is interactive. 1851556Srgrimes */ 1861556Srgrimes 1871556Srgrimesvoid 18890111Simpcmdloop(int top) 18917987Speter{ 1901556Srgrimes union node *n; 1911556Srgrimes struct stackmark smark; 1921556Srgrimes int inter; 1931556Srgrimes int numeof = 0; 1941556Srgrimes 1951556Srgrimes TRACE(("cmdloop(%d) called\n", top)); 1961556Srgrimes setstackmark(&smark); 1971556Srgrimes for (;;) { 1981556Srgrimes if (pendingsigs) 1991556Srgrimes dotrap(); 2001556Srgrimes inter = 0; 2011556Srgrimes if (iflag && top) { 2021556Srgrimes inter++; 203163085Sstefanf showjobs(1, SHOWJOBS_DEFAULT); 2041556Srgrimes chkmail(0); 2051556Srgrimes flushout(&output); 2061556Srgrimes } 2071556Srgrimes n = parsecmd(inter); 2081556Srgrimes /* showtree(n); DEBUG */ 2091556Srgrimes if (n == NEOF) { 2101556Srgrimes if (!top || numeof >= 50) 2111556Srgrimes break; 2121556Srgrimes if (!stoppedjobs()) { 2131556Srgrimes if (!Iflag) 2141556Srgrimes break; 215199629Sjilles out2fmt_flush("\nUse \"exit\" to leave shell.\n"); 2161556Srgrimes } 2171556Srgrimes numeof++; 2181556Srgrimes } else if (n != NULL && nflag == 0) { 2191556Srgrimes job_warning = (job_warning == 2) ? 1 : 0; 2201556Srgrimes numeof = 0; 2211556Srgrimes evaltree(n, 0); 2221556Srgrimes } 2231556Srgrimes popstackmark(&smark); 22464702Scracauer setstackmark(&smark); 225211349Sjilles if (evalskip != 0) { 226211349Sjilles if (evalskip == SKIPFILE) 227211349Sjilles evalskip = 0; 22820425Ssteve break; 22920425Ssteve } 2301556Srgrimes } 23164702Scracauer popstackmark(&smark); 2321556Srgrimes} 2331556Srgrimes 2341556Srgrimes 2351556Srgrimes 2361556Srgrimes/* 2371556Srgrimes * Read /etc/profile or .profile. Return on error. 2381556Srgrimes */ 2391556Srgrimes 240213811Sobrienstatic void 241222957Sjillesread_profile(char *name) 24290111Simp{ 2431556Srgrimes int fd; 244222957Sjilles const char *expandedname; 2451556Srgrimes 246222957Sjilles expandedname = expandstr(name); 247222957Sjilles if (expandedname == NULL) 248222957Sjilles return; 2491556Srgrimes INTOFF; 250222957Sjilles if ((fd = open(expandedname, O_RDONLY)) >= 0) 2511556Srgrimes setinputfd(fd, 1); 2521556Srgrimes INTON; 2531556Srgrimes if (fd < 0) 2541556Srgrimes return; 2551556Srgrimes cmdloop(0); 2561556Srgrimes popfile(); 2571556Srgrimes} 2581556Srgrimes 2591556Srgrimes 2601556Srgrimes 2611556Srgrimes/* 2621556Srgrimes * Read a file containing shell functions. 2631556Srgrimes */ 2641556Srgrimes 2651556Srgrimesvoid 266200956Sjillesreadcmdfile(const char *name) 26717987Speter{ 2681556Srgrimes int fd; 2691556Srgrimes 2701556Srgrimes INTOFF; 2711556Srgrimes if ((fd = open(name, O_RDONLY)) >= 0) 2721556Srgrimes setinputfd(fd, 1); 2731556Srgrimes else 274222684Sjilles error("cannot open %s: %s", name, strerror(errno)); 2751556Srgrimes INTON; 2761556Srgrimes cmdloop(0); 2771556Srgrimes popfile(); 2781556Srgrimes} 2791556Srgrimes 2801556Srgrimes 2811556Srgrimes 2821556Srgrimes/* 28346684Skris * Take commands from a file. To be compatible we should do a path 28417987Speter * search for the file, which is necessary to find sub-commands. 2851556Srgrimes */ 2861556Srgrimes 28717987Speter 288213811Sobrienstatic char * 28990111Simpfind_dot_file(char *basename) 29017987Speter{ 29117987Speter char *fullname; 292200956Sjilles const char *path = pathval(); 29317987Speter struct stat statb; 29417987Speter 29517987Speter /* don't try this for absolute or relative paths */ 29617987Speter if( strchr(basename, '/')) 29717987Speter return basename; 29817987Speter 29917987Speter while ((fullname = padvance(&path, basename)) != NULL) { 300222173Sjilles if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { 301222173Sjilles /* 302222173Sjilles * Don't bother freeing here, since it will 303222173Sjilles * be freed by the caller. 304222173Sjilles */ 305222173Sjilles return fullname; 306222173Sjilles } 30717987Speter stunalloc(fullname); 30817987Speter } 30917987Speter return basename; 31017987Speter} 31117987Speter 31217987Speterint 31390111Simpdotcmd(int argc, char **argv) 31417987Speter{ 315208630Sjilles char *filename, *fullname; 316157414Sstefanf 317157414Sstefanf if (argc < 2) 318157414Sstefanf error("missing filename"); 319157414Sstefanf 3201556Srgrimes exitstatus = 0; 32117987Speter 322208630Sjilles /* 323208630Sjilles * Because we have historically not supported any options, 324208630Sjilles * only treat "--" specially. 325208630Sjilles */ 326208630Sjilles filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1]; 327208630Sjilles 328208630Sjilles fullname = find_dot_file(filename); 329157414Sstefanf setinputfile(fullname, 1); 330157414Sstefanf commandname = fullname; 331157414Sstefanf cmdloop(0); 332157414Sstefanf popfile(); 3331556Srgrimes return exitstatus; 3341556Srgrimes} 3351556Srgrimes 3361556Srgrimes 33717987Speterint 33890111Simpexitcmd(int argc, char **argv) 33917987Speter{ 3401556Srgrimes if (stoppedjobs()) 34117987Speter return 0; 34220425Ssteve if (argc > 1) 343217175Sjilles exitshell(number(argv[1])); 34420425Ssteve else 345217175Sjilles exitshell_savedstatus(); 3461556Srgrimes} 347