/*
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley Software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include <locale.h>
#include "sh.h"
#include <fcntl.h>
#include "sh.tconst.h"
#include <pwd.h>
#include <stdlib.h>
#ifdef TRACE
#include <stdio.h>
#endif
/*
* We use these csh(1) private versions of the select macros, (see select(3C))
* so as not to be limited by the size of struct fd_set (ie 1024).
*/
S_NDOThistory /* "~/.history" */, 0 };
int nofile;
bool reenter;
bool nverbose;
bool nexececho;
bool quitit;
bool fast;
bool batch;
bool enterhist = 0;
extern void hupforegnd(void);
void interactive_hup(void);
void interactive_login_hup(void);
void importpath(tchar *);
void srcunit(int, bool, bool);
void rechist(void);
void goodbye(void);
void pintr1(bool);
void process(bool);
void mailchk(void);
void printprompt(void);
void sigwaiting(void);
void siglwp(void);
void initdesc(int, char *[]);
void initdesc_x(int, char *[], int);
void closem(void);
void unsetfd(int);
void phup(void);
#ifdef TRACE
/*
* Trace routines
*/
/*
* Initialize trace file.
* Called from main.
*/
void
trace_init(void)
{
char *p;
}
/*
* write message to trace file
*/
/*VARARGS1*/
void
char *fmt;
{
if (trace) {
}
}
#endif
int
{
int f;
char *c_max_var_len;
int c_max_var_len_size;
/*
* set up the error exit, if there is an error before
* this is done, it will core dump, and we don't
* tolerate core dumps
*/
haderr = 0;
setexit();
if (haderr) {
/*
* if were here, there was an error in the csh
* startup so just punt
*/
printf("csh startup error, csh exiting...\n");
flush();
exitstat();
}
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
/* Copy arguments */
v = strblktotsblk(av, c);
/*
* Initialize paraml list
*/
settimes(); /* Immed. estab. timing base */
quitit = 1;
loginsh = **v == '-';
if (loginsh)
/*
* Move the descriptors to safe places.
* The variable didfds is 0 while we have only FSH* to work with.
* When didfds is true, we have 0,1,2 and prefer to use these.
*
* Also, setup data for csh internal file descriptor book keeping.
*/
/*
* Initialize the shell variables.
* ARGV and PROMPT are initialized later.
* STATUS is also munged in several places.
*/
"%ld", MAX_VAR_LEN);
/* don't do globbing here, just set exact copies */
/* login shell */
fast++; /* No home -> can't read scripts */
else {
fast++;
}
}
/*
* Grab other useful things from the environment.
* Should we grab everything??
*/
else {
/*
* If USER is not defined, set it here.
*/
} else if (loginsh) { /* Give up setting USER variable. */
printf("Warning: USER environment variable could not be set.\n");
}
}
/*
* Re-initialize path if set in environment
*/
else
importpath(cp);
/* restore globbing until the user says otherwise */
/*
* Record the interrupt states from the parent process.
* If the parent is non-interruptible our hand must be forced
* or we (and our children) won't be either.
* Our children inherit termination from our parent.
* We catch it only if we are the login shell.
*/
/* parents interruptibility */
/* parents terminability */
if (loginsh) {
}
/*
* Process the arguments.
*
* Note that processing of -v/-x is actually delayed till after
* script processing.
*/
c--, v++;
do switch (*cp++) {
case 'b': /* -b Next arg is input file */
batch++;
break;
case 'c': /* -c Command input from arg */
if (c == 1)
exit(0);
c--, v++;
arginp = v[0];
prompt = 0;
nofile++;
cflg++;
break;
case 'e': /* -e Exit on any error */
exiterr++;
break;
case 'f': /* -f Fast start */
fast++;
break;
case 'i': /* -i Interactive, even if !intty */
intact++;
nofile++;
break;
case 'n': /* -n Don't execute */
noexec++;
break;
case 'q': /* -q (Undoc'd) ... die on quit */
quitit = 1;
break;
case 's': /* -s Read from std input */
nofile++;
break;
case 't': /* -t Read one line from input */
onelflg = 2;
prompt = 0;
nofile++;
break;
#ifdef TRACE
case 'T': /* -T trace switch on */
trace_init();
break;
#endif
case 'v': /* -v Echo hist expanded input */
break;
case 'x': /* -x Echo just before execution */
break;
case 'V': /* -V Echo hist expanded input */
break;
case 'X': /* -X Echo just before execution */
break;
} while (*cp);
v++, c--;
}
if (quitit) /* With all due haste, for debugging */
/*
* Unless prevented by -c, -i, -s, or -t, if there
* are remaining arguments the first of them is the name
* of a shell file from which to read commands.
*/
child++; /* So this ... */
}
if (nofile == 0 && c > 0) {
if (nofile < 0) {
child++; /* So this ... */
Perror(v[0]); /* ... doesn't return */
}
file = v[0];
prompt = 0;
c--, v++;
}
/*
* Consider input a tty if it really is or we are interactive.
*/
/*
* Decide whether we should play with signals or not.
* If we are explicitly told (via -i, or -) or we are a login
* shell (arg0 starts with -) or the input and output are both
* Note that in only the login shell is it likely that parent
* may have set signals to be ignored
*/
setintr = 1;
#ifdef TELL
settell();
#endif
/*
* Save the remaining arguments in argv.
*/
/*
* Set up the prompt.
*/
if (prompt) {
}
/*
* If we are an interactive shell, then start fiddling
* with the signals; this is a tricky game.
*/
if (setintr) {
**av = '-';
if (!quitit) /* Wary! */
/*
* Explicitly terminate foreground jobs and exit if we are
* interactive shell
*/
if (loginsh) {
} else {
}
/*
* Wait till in foreground, in case someone
* stupidly runs
* csh &
* dont want to try to grab away the tty.
*/
f = FSHDIAG;
f = FSHOUT;
f = OLDSTD;
else
f = -1;
tpgrp != -1) {
void (*old)() = (void (*)())
goto retry;
}
} else {
printf("Warning: no access to tty; thus no job control in this shell...\n");
tpgrp = -1;
}
}
}
setintr++;
/*
* Set SIGCHLD handler, making sure that reads restart after it runs.
*/
/*
* Set an exit here in case of an interrupt or error reading
* the shell start-up scripts.
*/
setexit();
haderr = 0; /* In case second time through */
reenter++;
/*
* If this is a login csh, and /etc/.login exists,
* source /etc/.login first.
*/
if (loginsh) {
}
/* Will have value("home") here because set fast if don't */
/* Hash path */
/*
* Reconstruct the history list now, so that it's
* available from within .login.
*/
if (loginsh) {
}
/*
* To get cdpath hashing $cdpath must have a
* value, not $CDPATH. So if after reading
* the startup files ( .cshrc ), and
* user has specified a value for cdpath, then
* cache $cdpath paths. xhash2 is global array
* for $cdpath caching.
*/
}
/*
* Now are ready for the -v and -x flags
*/
if (nverbose)
if (nexececho)
/*
* All the rest of the world is inside this call.
* The argument to process indicates whether it should
* catch "error unwinds". Thus if we are a interactive shell
* our call here will never return by being blown past on an error.
*/
/*
* Mop-up.
*/
if (loginsh) {
printf("logout\n");
child++;
goodbye();
}
rechist();
exitstat();
}
void
untty(void)
{
if (tpgrp > 0) {
}
}
void
{
int i = 0;
int c;
if (*dp == ':')
i++;
/*
* i+2 where i is the number of colons in the path.
* There are i+1 directories in the path plus we need
* room for a zero terminator.
*/
i = 0;
if (*dp)
for (;;) {
if ((c = *dp) == ':' || c == 0) {
*dp = 0;
if (c) {
*dp = ':';
} else
break;
}
dp++;
}
pv[i] = 0;
}
/*
* Source to the file which is the catenation of the argument names.
*/
void
{
#ifdef INGRES
#else
#endif
}
/*
* Source to the file which is the catenation of the argument names.
* This one does not check the ownership.
*/
void
{
}
/*
* Source to a unit. If onlyown it must be our file or our group or
* we don't chance it. This occurs on ".cshrc"s and the like.
*/
void
{
/* We have to push down a lot of state here */
/* All this could go into a structure */
#ifdef TELL
#endif
/* The (few) real local variables */
if (unit < 0)
return;
if (didfds)
donefds();
if (onlyown) {
return;
}
}
/*
* There is a critical section here while we are pushing down the
* input stream since we have stuff in different structures.
* If we weren't careful an interrupt could corrupt SHIN's Bin
* structure and kill the shell.
*
* We could avoid the critical region by grouping all the stuff
* in a single structure and pointing at it to move it all at
* once. This is less efficient globally on many variable references
* however.
*/
reenter = 0;
if (setintr)
setexit();
reenter++;
if (reenter == 1) {
/* Setup the new values of the state stuff saved above */
if (enterhist)
HIST = '\0';
/*
* Now if we are allowing commands to be interrupted,
* we let ourselves be interrupted.
*/
if (setintr)
(void) sigsetmask(omask);
#ifdef TELL
settell();
#endif
process(0); /* 0 -> blow away on errors */
}
if (setintr)
(void) sigsetmask(omask);
if (oSHIN >= 0) {
int i;
/* We made it to the new state... free up its storage */
/* This code could get run twice but xfree doesn't care */
for (i = 0; i < fblocks; i++)
/* Reset input arena */
if (enterhist)
#ifdef TELL
#endif
}
/*
* If process reset() (effectively an unwind) then
* we must also unwind.
*/
if (reenter >= 2)
}
void
rechist(void)
{
if (!fast) {
return;
if (fp == -1)
return;
didfds = 0;
}
}
void
goodbye(void)
{
if (loginsh) {
setintr = 0; /* No interrupts after "logout" */
}
rechist();
exitstat();
}
void
exitstat(void)
{
#ifdef PROF
monitor(0);
#endif
/*
* Note that if STATUS is corrupted (i.e. getn bombs)
* then error will exit directly because we poke child here.
* Otherwise we might continue unwarrantedly (sic).
*/
child++;
untty();
}
/*
* in the event of a HUP we want to save the history
*/
void
phup(void)
{
rechist();
exit(1);
}
void
interactive_hup(void)
{
hupforegnd();
exit(1);
}
void
interactive_login_hup(void)
{
rechist();
hupforegnd();
exit(1);
}
/*
* Catch an interrupt, e.g. during lexical input.
* If we are an interactive shell, we reset the interrupt catch
* immediately. In any case we drain the shell output,
* and finally go through the normal error mechanism, which
* gets a chance to make the shell go away.
*/
void
pintr(void)
{
pintr1(1);
}
void
{
tchar **v;
int omask;
if (setintr) {
if (pjobs) {
pjobs = 0;
printf("\n");
bferr("Interrupted");
}
}
draino();
/*
* If we have an active "onintr" then we search for the label.
* Note that if one does "onintr -" then we shan't be interruptible
* so we needn't worry about that here.
*/
if (gointr) {
timflg = 0;
if (v = pargv)
if (v = gargv)
reset();
}
/*
* Process is the main driving routine for the shell.
* It runs all command processing, except for those within { ... }
* in expressions (which is run by a routine evalav in sh.exp.c which
* is a stripped down process), and `...` evaluation which is run
* also by a subset of this code in sh.glob.c in the routine backeval.
*
* The code here is a little strange because part of it is interruptible
* and hence freeing of structures appears to occur when none is necessary
* if this is ignored.
*
* Note that if catch is not set then we will unwind on any error.
* If an end-of-file occurs, we return.
*/
void
{
struct command *t;
for (;;) {
pendjob();
t = 0;
setexit();
/*
* Interruptible during interactive reads
*/
if (setintr)
/*
* For the sake of reset()
*/
if (haderr) {
if (!catch) {
/* unwind */
doneinp = 0;
reset();
}
haderr = 0;
/*
* Every error is eventually caught here or
* the shell dies. It is at this
* point that we clean up any left-over open
* files, by closing all but a fixed number
* of pre-defined files. Thus routines don't
* have to worry about leaving files open due
* to deeper errors... they will get closed here.
*/
closem();
continue;
}
if (doneinp) {
doneinp = 0;
break;
}
if (chkstop)
chkstop--;
if (neednote)
pnote();
mailchk();
/*
* If we are at the end of the input buffer
* then we are going to read fresh stuff.
* Otherwise, we are rereading input and don't
* need or want to prompt.
*/
printprompt();
}
err = 0;
/*
* Echo not only on VERBOSE, but also with history expansion.
*/
haderr = 1;
haderr = 0;
}
/*
* The parser may lose space if interrupted.
*/
if (setintr)
/*
* Save input text on the history list if
* reading in old history, or it
* is from the terminal at the top level and not
* in a loop.
*/
/*
* Print lexical error messages, except when sourcing
* history lists.
*/
/*
* If had a history command :p modifier then
* this is as far as we should go
*/
if (justpr)
reset();
/*
* Parse the words of the input into a parse tree.
*/
if (err)
/*
* Execute the parse tree
*/
{
/*
* POSIX requires SIGCHLD to be held
* until all processes have joined the
* process group in order to avoid race
* condition.
*/
int omask;
}
if (err)
/*
* Made it!
*/
}
}
void
{
tchar *f;
int u;
bool hflg = 0;
t++;
if (*++t == NOSTR)
bferr("Too few arguments.");
hflg++;
}
xfree(f);
if (u < 0 && !hflg)
Perror(f);
}
/*
* Check for mail.
* If we are a login shell, then we don't want to tell
* about any mail file unless its been modified
* after the time we started.
* This prevents us from telling the user things he already
* knows, since the login program insists on saying
* "You have mail."
*/
void
mailchk(void)
{
struct varent *v;
time_t t;
bool new;
if (v == 0)
return;
(void) time(&t);
if (intvl < 1)
intvl = 1;
return;
continue;
continue;
if (cnt == 1)
else
}
chktim = t;
}
/*
* Extract a home directory from the password file
* The argument points to a buffer where the name of the
* user whose home directory is sought is currently.
* We write the home directory of the user back there.
*/
int
{
/* getpwname will not be modified, so we need temp. buffer */
if (pp == 0)
return (1);
return (0);
}
#if 0
void
#ifdef PROF
done(int i)
#else
exit(int i)
#endif
{
untty();
_exit(i);
}
#endif
void
printprompt(void)
{
if (!whyles) {
/*
* Print the prompt string
*/
else {
cp++;
}
} else
/*
* Prompt for forward reading loop
* body content.
*/
printf("? ");
flush();
}
/*
* Save char * block.
*/
tchar **
{
while (*v && num--)
*newv = 0;
return (onewv);
}
void
sigwaiting(void)
{
}
void
siglwp(void)
{
}
/*
* Following functions and data are used for csh to do its
* file descriptors book keeping.
*/
/*
* Get the number of files this csh can use.
*
* Move the initial descriptors to their eventual
* resting places, closing all other units.
*
* Also, reserve 0/1/2, so NIS+ routines do not get
* hold of them. And initialize fdinuse list and set
* the current process id.
*
* If this csh was invoked from setuid'ed script file,
* do not close the third argument passed. The file
* (execv() always passes three arguments when it execs a script
*
* If is_reinit is set in initdesc_x(), then we only close the file
* descriptors that we actually opened (as recorded in fdinuse).
*/
void
{
}
void
{
}
/*
* Callback functions for closing all file descriptors.
*/
static int
{
return (0);
}
static int
{
}
return (0);
}
void
{
/*
* Get pid of this shell
*/
/*
* Get the hard limit numbers of descriptors
* this csh can use.
*/
/*
* If this csh was invoked for executing setuid script file,
* the third argument passed is the special file name
* which should not be closed. This special file name is
*/
if (argc >= 3)
script_fd = -1;
else
/* Make sure to close this file on exec. */
nbytesused = sizeof (int) *
}
/*
* Close all files except 0/1/2 to get a clean
* file descritor space.
*/
if (!is_reinit)
else
didfds = 0; /* 0, 1, 2 aren't set up */
/*
* Open 0/1/2 to avoid Nis+ functions to pick them up.
* Now, 0/1/2 are saved, close them and open them.
*/
dup(0);
dup(0);
/*
* Clear fd_set mask
*/
if (!is_reinit)
}
/*
* This routine is called after an error to close up
* any units which may have been left open accidentally.
*
* You only need to remove files in fdinuse list.
* After you have removed the files, you can clear the
* list and max_fd.
*/
void
closem(void)
{
int f;
for (f = 3; f <= max_fd; f++) {
if (CSH_FD_ISSET(f, fdinuse) &&
close(f);
}
max_fd = 0;
}
/*
* Reset my_pid when a new process is created. Only call this
* if you want the process to affect fdinuse (e.g., fork, but
* not vfork).
*/
void
new_process(void)
{
}
/*
* Csh keeps track of its open files. The open files
* are kept in "fdinuse, Fd In Use" list.
*
* When a file descriptor is newly allocated, setfd() is
* used to mark the fact in "fdinuse" list.
* For example,
* fd = open("newfile", 0);
* setfd(fd);
*
* When a file is freed by close() function, unsetfd() is
* used to remove the fd from "fdinuse" list.
* For example,
* close(fd);
* unsetfd(fd);
*/
void
{
/*
* Because you want to avoid
* conflict due to vfork().
*/
return;
return;
}
void
{
int i;
/*
* Because you want to avoid
* conflict due to vfork().
*/
return;
return;
if (CSH_FD_ISSET(i, fdinuse)) {
max_fd = i;
return;
}
max_fd = 0;
}
}