main.c revision 222957
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 222957 2011-06-10 22:42:00Z 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" 751556Srgrimes 761556Srgrimesint rootpid; 771556Srgrimesint rootshell; 78200998Sjillesstruct jmploc main_handler; 79221669Sjillesint localeisutf8, initial_localeisutf8; 801556Srgrimes 81222957Sjillesstatic void read_profile(char *); 82213811Sobrienstatic char *find_dot_file(char *); 831556Srgrimes 841556Srgrimes/* 851556Srgrimes * Main routine. We initialize things, parse the arguments, execute 861556Srgrimes * profiles if we're a login shell, and then call cmdloop to execute 871556Srgrimes * commands. The setjmp call sets up the location to jump to when an 881556Srgrimes * exception occurs. When an exception occurs the variable "state" 891556Srgrimes * is used to figure out how far we had gotten. 901556Srgrimes */ 911556Srgrimes 9217987Speterint 9390111Simpmain(int argc, char *argv[]) 9417987Speter{ 95222957Sjilles struct stackmark smark, smark2; 961556Srgrimes volatile int state; 971556Srgrimes char *shinit; 981556Srgrimes 9917525Sache (void) setlocale(LC_ALL, ""); 100221669Sjilles initcharset(); 1011556Srgrimes state = 0; 102200998Sjilles if (setjmp(main_handler.loc)) { 10320425Ssteve switch (exception) { 10420425Ssteve case EXEXEC: 10520425Ssteve exitstatus = exerrno; 10620425Ssteve break; 10720425Ssteve 10820425Ssteve case EXERROR: 10920425Ssteve exitstatus = 2; 11020425Ssteve break; 11120425Ssteve 11220425Ssteve default: 11320425Ssteve break; 11420425Ssteve } 11520425Ssteve 116220978Sjilles if (state == 0 || iflag == 0 || ! rootshell || 117220978Sjilles exception == EXEXIT) 118218306Sjilles exitshell(exitstatus); 1191556Srgrimes reset(); 120215567Sjilles if (exception == EXINT) 121215567Sjilles out2fmt_flush("\n"); 1221556Srgrimes popstackmark(&smark); 1231556Srgrimes FORCEINTON; /* enable interrupts */ 1241556Srgrimes if (state == 1) 1251556Srgrimes goto state1; 1261556Srgrimes else if (state == 2) 1271556Srgrimes goto state2; 1281556Srgrimes else if (state == 3) 1291556Srgrimes goto state3; 1301556Srgrimes else 1311556Srgrimes goto state4; 1321556Srgrimes } 133200998Sjilles handler = &main_handler; 1341556Srgrimes#ifdef DEBUG 1351556Srgrimes opentrace(); 1361556Srgrimes trputs("Shell args: "); trargs(argv); 1371556Srgrimes#endif 1381556Srgrimes rootpid = getpid(); 1391556Srgrimes rootshell = 1; 1401556Srgrimes init(); 1411556Srgrimes setstackmark(&smark); 142222957Sjilles setstackmark(&smark2); 1431556Srgrimes procargs(argc, argv); 144206759Sjilles pwd_init(iflag); 145203576Sjilles if (iflag) 146203576Sjilles chkmail(1); 1471556Srgrimes if (argv[0] && argv[0][0] == '-') { 1481556Srgrimes state = 1; 1491556Srgrimes read_profile("/etc/profile"); 1501556Srgrimesstate1: 1511556Srgrimes state = 2; 15219240Ssteve if (privileged == 0) 15319240Ssteve read_profile(".profile"); 15419240Ssteve else 15519240Ssteve read_profile("/etc/suid_profile"); 1568855Srgrimes } 1571556Srgrimesstate2: 1581556Srgrimes state = 3; 15925471Ssteve if (!privileged && iflag) { 16017987Speter if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 16117987Speter state = 3; 16217987Speter read_profile(shinit); 16317987Speter } 1641556Srgrimes } 1651556Srgrimesstate3: 1661556Srgrimes state = 4; 167222957Sjilles popstackmark(&smark2); 1681556Srgrimes if (minusc) { 169194128Sjilles evalstring(minusc, sflag ? 0 : EV_EXIT); 1701556Srgrimes } 1711556Srgrimes if (sflag || minusc == NULL) { 1721556Srgrimesstate4: /* XXX ??? - why isn't this before the "if" statement */ 1731556Srgrimes cmdloop(1); 1741556Srgrimes } 1751556Srgrimes exitshell(exitstatus); 17617987Speter /*NOTREACHED*/ 17717987Speter return 0; 1781556Srgrimes} 1791556Srgrimes 1801556Srgrimes 1811556Srgrimes/* 1821556Srgrimes * Read and execute commands. "Top" is nonzero for the top level command 1831556Srgrimes * loop; it turns on prompting if the shell is interactive. 1841556Srgrimes */ 1851556Srgrimes 1861556Srgrimesvoid 18790111Simpcmdloop(int top) 18817987Speter{ 1891556Srgrimes union node *n; 1901556Srgrimes struct stackmark smark; 1911556Srgrimes int inter; 1921556Srgrimes int numeof = 0; 1931556Srgrimes 1941556Srgrimes TRACE(("cmdloop(%d) called\n", top)); 1951556Srgrimes setstackmark(&smark); 1961556Srgrimes for (;;) { 1971556Srgrimes if (pendingsigs) 1981556Srgrimes dotrap(); 1991556Srgrimes inter = 0; 2001556Srgrimes if (iflag && top) { 2011556Srgrimes inter++; 202163085Sstefanf showjobs(1, SHOWJOBS_DEFAULT); 2031556Srgrimes chkmail(0); 2041556Srgrimes flushout(&output); 2051556Srgrimes } 2061556Srgrimes n = parsecmd(inter); 2071556Srgrimes /* showtree(n); DEBUG */ 2081556Srgrimes if (n == NEOF) { 2091556Srgrimes if (!top || numeof >= 50) 2101556Srgrimes break; 2111556Srgrimes if (!stoppedjobs()) { 2121556Srgrimes if (!Iflag) 2131556Srgrimes break; 214199629Sjilles out2fmt_flush("\nUse \"exit\" to leave shell.\n"); 2151556Srgrimes } 2161556Srgrimes numeof++; 2171556Srgrimes } else if (n != NULL && nflag == 0) { 2181556Srgrimes job_warning = (job_warning == 2) ? 1 : 0; 2191556Srgrimes numeof = 0; 2201556Srgrimes evaltree(n, 0); 2211556Srgrimes } 2221556Srgrimes popstackmark(&smark); 22364702Scracauer setstackmark(&smark); 224211349Sjilles if (evalskip != 0) { 225211349Sjilles if (evalskip == SKIPFILE) 226211349Sjilles evalskip = 0; 22720425Ssteve break; 22820425Ssteve } 2291556Srgrimes } 23064702Scracauer popstackmark(&smark); 2311556Srgrimes} 2321556Srgrimes 2331556Srgrimes 2341556Srgrimes 2351556Srgrimes/* 2361556Srgrimes * Read /etc/profile or .profile. Return on error. 2371556Srgrimes */ 2381556Srgrimes 239213811Sobrienstatic void 240222957Sjillesread_profile(char *name) 24190111Simp{ 2421556Srgrimes int fd; 243222957Sjilles const char *expandedname; 2441556Srgrimes 245222957Sjilles expandedname = expandstr(name); 246222957Sjilles if (expandedname == NULL) 247222957Sjilles return; 2481556Srgrimes INTOFF; 249222957Sjilles if ((fd = open(expandedname, O_RDONLY)) >= 0) 2501556Srgrimes setinputfd(fd, 1); 2511556Srgrimes INTON; 2521556Srgrimes if (fd < 0) 2531556Srgrimes return; 2541556Srgrimes cmdloop(0); 2551556Srgrimes popfile(); 2561556Srgrimes} 2571556Srgrimes 2581556Srgrimes 2591556Srgrimes 2601556Srgrimes/* 2611556Srgrimes * Read a file containing shell functions. 2621556Srgrimes */ 2631556Srgrimes 2641556Srgrimesvoid 265200956Sjillesreadcmdfile(const char *name) 26617987Speter{ 2671556Srgrimes int fd; 2681556Srgrimes 2691556Srgrimes INTOFF; 2701556Srgrimes if ((fd = open(name, O_RDONLY)) >= 0) 2711556Srgrimes setinputfd(fd, 1); 2721556Srgrimes else 273222684Sjilles error("cannot open %s: %s", name, strerror(errno)); 2741556Srgrimes INTON; 2751556Srgrimes cmdloop(0); 2761556Srgrimes popfile(); 2771556Srgrimes} 2781556Srgrimes 2791556Srgrimes 2801556Srgrimes 2811556Srgrimes/* 28246684Skris * Take commands from a file. To be compatible we should do a path 28317987Speter * search for the file, which is necessary to find sub-commands. 2841556Srgrimes */ 2851556Srgrimes 28617987Speter 287213811Sobrienstatic char * 28890111Simpfind_dot_file(char *basename) 28917987Speter{ 29017987Speter char *fullname; 291200956Sjilles const char *path = pathval(); 29217987Speter struct stat statb; 29317987Speter 29417987Speter /* don't try this for absolute or relative paths */ 29517987Speter if( strchr(basename, '/')) 29617987Speter return basename; 29717987Speter 29817987Speter while ((fullname = padvance(&path, basename)) != NULL) { 299222173Sjilles if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { 300222173Sjilles /* 301222173Sjilles * Don't bother freeing here, since it will 302222173Sjilles * be freed by the caller. 303222173Sjilles */ 304222173Sjilles return fullname; 305222173Sjilles } 30617987Speter stunalloc(fullname); 30717987Speter } 30817987Speter return basename; 30917987Speter} 31017987Speter 31117987Speterint 31290111Simpdotcmd(int argc, char **argv) 31317987Speter{ 314208630Sjilles char *filename, *fullname; 315157414Sstefanf 316157414Sstefanf if (argc < 2) 317157414Sstefanf error("missing filename"); 318157414Sstefanf 3191556Srgrimes exitstatus = 0; 32017987Speter 321208630Sjilles /* 322208630Sjilles * Because we have historically not supported any options, 323208630Sjilles * only treat "--" specially. 324208630Sjilles */ 325208630Sjilles filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1]; 326208630Sjilles 327208630Sjilles fullname = find_dot_file(filename); 328157414Sstefanf setinputfile(fullname, 1); 329157414Sstefanf commandname = fullname; 330157414Sstefanf cmdloop(0); 331157414Sstefanf popfile(); 3321556Srgrimes return exitstatus; 3331556Srgrimes} 3341556Srgrimes 3351556Srgrimes 33617987Speterint 33790111Simpexitcmd(int argc, char **argv) 33817987Speter{ 3391556Srgrimes if (stoppedjobs()) 34017987Speter return 0; 34120425Ssteve if (argc > 1) 342217175Sjilles exitshell(number(argv[1])); 34320425Ssteve else 344217175Sjilles exitshell_savedstatus(); 3451556Srgrimes} 346