rc

[fork] interactive rc shell
Log | Files | Refs | README | LICENSE

commit df8f2d58845819d1973779918d9c97018edc1170
parent 8a03ed3fc54e728f7ccd2fef40b7215e1d072054
Author: tim <tim>
Date:   Tue,  1 Jul 1997 19:57:45 +0000

autoconf

Diffstat:
Mfn.c | 4++--
Mfootobar.c | 6+++---
Mglob.c | 38+++++++++++++++++++++++---------------
Mglom.c | 97++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mhash.c | 4++--
Minput.c | 25+++++++++++++------------
Mlex.c | 8++++----
Mrc.h | 78+++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mvar.c | 2+-
Mwalk.c | 4++--
10 files changed, 155 insertions(+), 111 deletions(-)

diff --git a/fn.c b/fn.c @@ -26,7 +26,7 @@ extern void inithandler() { null.type = nBody; null.u[0].p = null.u[1].p = NULL; for (i = 1; i < NUMOFSIGNALS; i++) -#ifdef NOSIGCLD +#ifndef HAVE_RESTARTABLE_SYSCALLS if (i != SIGCLD) #endif if (sighandlers[i] == SIG_IGN) @@ -148,7 +148,7 @@ extern void fnassign(char *name, Node *def) { new->def = newdef; new->extdef = NULL; if (strncmp(name, "sig", conststrlen("sig")) == 0) { /* slight optimization */ -#ifdef NOSIGCLD /* System V machines treat SIGCLD very specially */ +#ifndef HAVE_RESTARTABLE_SYSCALLS /* System V machines treat SIGCLD very specially */ if (streq(name, "sigcld")) rc_error("can't trap SIGCLD"); #endif diff --git a/footobar.c b/footobar.c @@ -7,7 +7,7 @@ /* protect an exported name from brain-dead shells */ -#ifdef PROTECT_ENV +#if PROTECT_ENV static bool Fconv(Format *f, int ignore) { unsigned const char *s = va_arg(f->args, unsigned const char *); int c; @@ -192,7 +192,7 @@ extern char *get_name(char *s) { case '=': *r++ = '\0'; return result; -#ifdef PROTECT_ENV +#if PROTECT_ENV case '_': if (*s == '_') { static const char hexchar[] = "0123456789abcdef"; @@ -329,7 +329,7 @@ void initprint(void) { fmtinstall('S', Sconv); fmtinstall('T', Tconv); fmtinstall('D', Dconv); -#ifdef PROTECT_ENV +#if PROTECT_ENV fmtinstall('F', Fconv); #else fmtinstall('F', fmtinstall('s', NULL)); diff --git a/glob.c b/glob.c @@ -3,16 +3,28 @@ #include <sys/types.h> #include <sys/stat.h> #include "rc.h" -#ifdef NODIRENT -#include <sys/dir.h> -#define dirent direct /* need to get the struct declaraction right */ + +/* Lifted from autoconf documentation.*/ +#if HAVE_DIRENT_H +# include <dirent.h> +# define NAMLEN(dirent) strlen((dirent)->d_name) #else -#include <dirent.h> +# define dirent direct +# define NAMLEN(dirent) (dirent)->d_namlen +# if HAVE_SYS_NDIR_H +# include <sys/ndir.h> +# endif +# if HAVE_SYS_DIR_H +# include <sys/dir.h> +# endif +# if HAVE_NDIR_H +# include <ndir.h> +# endif #endif static List *dmatch(char *, char *, char *); static List *doglob(char *, char *); -static List *lglob(List *, char *, char *, SIZE_T); +static List *lglob(List *, char *, char *, size_t); static List *sort(List *); /* @@ -90,10 +102,6 @@ static List *dmatch(char *d, char *p, char *m) { static DIR *dirp; static struct dirent *dp; static struct stat s; - /* prototypes for XXXdir functions. comment out if necessary */ - extern DIR *opendir(const char *); - extern struct dirent *readdir(DIR *); - /*extern int closedir(DIR *);*/ int i; /* @@ -151,11 +159,11 @@ static List *dmatch(char *d, char *p, char *m) { matched name. e.g., for matching ////tmp/////foo* */ -static List *lglob(List *s, char *p, char *m, SIZE_T slashcount) { +static List *lglob(List *s, char *p, char *m, size_t slashcount) { List *q, *r, *top, foo; static struct { List l; - SIZE_T size; + size_t size; } slash; if (slashcount+1 > slash.size) { slash.size = 2*(slashcount+1); @@ -191,9 +199,9 @@ static List *lglob(List *s, char *p, char *m, SIZE_T slashcount) { static List *doglob(char *w, char *m) { static char *dir = NULL, *pattern = NULL, *metadir = NULL, *metapattern = NULL; - static SIZE_T dsize = 0; + static size_t dsize = 0; char *d, *p, *md, *mp; - SIZE_T psize; + size_t psize; char *s = w; List firstdir; List *matched; @@ -238,7 +246,7 @@ static List *doglob(char *w, char *m) { matched = dmatch(".", dir, metadir); } do { - SIZE_T slashcount; + size_t slashcount; sigchk(); for (slashcount = 0; *s == '/'; s++, m++) slashcount++; /* skip slashes */ @@ -258,7 +266,7 @@ end: if (matched == NULL) { } static List *sort(List *s) { - SIZE_T nel = listnel(s); + size_t nel = listnel(s); if (nel > 1) { char **a; List *t; diff --git a/glom.c b/glom.c @@ -5,9 +5,6 @@ #include <signal.h> #include <errno.h> #include "rc.h" -#if !defined(S_IFIFO) && !defined(DEVFD) -#define NOCMDARG -#endif static List *backq(Node *, Node *); static List *bqinput(List *, int); @@ -58,9 +55,9 @@ extern List *concat(List *s1, List *s2) { if ((n1 = listnel(s1)) != (n2 = listnel(s2)) && n1 != 1 && n2 != 1) rc_error("bad concatenation"); for (r = top = nnew(List); 1; r = r->n = nnew(List)) { - SIZE_T x = strlen(s1->w); - SIZE_T y = strlen(s2->w); - SIZE_T z = x + y + 1; + size_t x = strlen(s1->w); + size_t y = strlen(s2->w); + size_t z = x + y + 1; r->w = nalloc(z); strcpy(r->w, s1->w); strcat(r->w, s2->w); @@ -115,7 +112,7 @@ extern List *varsub(List *var, List *subs) { extern List *flatten(List *s) { List *r; - SIZE_T step; + size_t step; char *f; if (s == NULL || s->n == NULL) return s; @@ -175,12 +172,12 @@ extern void assign(List *s1, List *s2, bool stack) { who could not stand the incompetence of my own backquote implementation. */ -#define BUFSIZE ((SIZE_T) 1000) +#define BUFSIZE ((size_t) 1000) static List *bqinput(List *ifs, int fd) { char *end, *bufend, *s; List *r, *top, *prev; - SIZE_T remain, bufsize; + size_t remain, bufsize; char isifs[256]; int n, state; /* a simple FSA is used to read in data */ @@ -197,7 +194,7 @@ static List *bqinput(List *ifs, int fd) { while (1) { if (remain == 0) { /* is the string bigger than the buffer? */ - SIZE_T m = end - r->w; + size_t m = end - r->w; char *buf; while (bufsize < m + BUFSIZE) bufsize *= 2; @@ -249,7 +246,8 @@ static List *bqinput(List *ifs, int fd) { } static List *backq(Node *ifs, Node *n) { - int p[2], pid, sp; + int p[2], sp; + pid_t pid; List *bq; if (n == NULL) return NULL; @@ -288,13 +286,44 @@ extern void qredir(Node *n) { next->n = NULL; } -#ifdef NOCMDARG +#if HAVE_DEV_FD || HAVE_PROC_SELF_FD static List *mkcmdarg(Node *n) { - rc_error("named pipes are not supported"); - return NULL; -} + char *name; + List *ret = nnew(List); + Estack *e = nnew(Estack); + Edata efd; + int p[2]; + if (pipe(p) < 0) { + uerror("pipe"); + return NULL; + } + if (rc_fork() == 0) { + setsigdefaults(FALSE); + if (mvfd(p[n->u[0].i == rFrom], n->u[0].i == rFrom) < 0) /* stupid hack */ + exit(1); + close(p[n->u[0].i != rFrom]); + redirq = NULL; + walk(n->u[2].p, FALSE); + exit(getstatus()); + } + +#if HAVE_DEV_FD + name = nprint("/dev/fd/%d", p[n->u[0].i != rFrom]); #else -#ifndef DEVFD + name = nprint("/proc/self/fd/%d", p[n->u[0].i != rFrom]); +#endif + + efd.fd = p[n->u[0].i != rFrom]; + except(eFd, efd, e); + close(p[n->u[0].i == rFrom]); + ret->w = name; + ret->m = NULL; + ret->n = NULL; + return ret; +} + +#elif HAVE_FIFO + static List *mkcmdarg(Node *n) { int fd; char *name; @@ -302,7 +331,7 @@ static List *mkcmdarg(Node *n) { Estack *e = enew(Estack); List *ret = nnew(List); static int fifonumber = 0; - name = nprint("%s/rc%d.%d", TMPDIR, getpid(), fifonumber++); + name = nprint("/tmp/rc%d.%d", getpid(), fifonumber++); if (mknod(name, S_IFIFO | 0666, 0) < 0) { uerror("mknod"); return NULL; @@ -327,37 +356,15 @@ static List *mkcmdarg(Node *n) { ret->n = NULL; return ret; } + #else + static List *mkcmdarg(Node *n) { - char *name; - List *ret = nnew(List); - Estack *e = nnew(Estack); - Edata efd; - int p[2]; - if (pipe(p) < 0) { - uerror("pipe"); - return NULL; - } - if (rc_fork() == 0) { - setsigdefaults(FALSE); - if (mvfd(p[n->u[0].i == rFrom], n->u[0].i == rFrom) < 0) /* stupid hack */ - exit(1); - close(p[n->u[0].i != rFrom]); - redirq = NULL; - walk(n->u[2].p, FALSE); - exit(getstatus()); - } - name = nprint("/dev/fd/%d", p[n->u[0].i != rFrom]); - efd.fd = p[n->u[0].i != rFrom]; - except(eFd, efd, e); - close(p[n->u[0].i == rFrom]); - ret->w = name; - ret->m = NULL; - ret->n = NULL; - return ret; + rc_error("named pipes are not supported"); + return NULL; } -#endif /* DEVFD */ -#endif /* !NOCMDARG */ + +#endif extern List *glom(Node *n) { List *v, *head, *tail; diff --git a/hash.c b/hash.c @@ -275,7 +275,7 @@ extern char **makeenv() { env[ep++] = fnlookup_string(fp[i].name); } env[ep] = NULL; - qsort(env, (SIZE_T) ep, sizeof(char *), starstrcmp); + qsort(env, (size_t) ep, sizeof(char *), starstrcmp); return env; } @@ -294,7 +294,7 @@ extern void whatare_all_vars(bool showfn, bool showvar) { /* fake getenv() for readline() follows: */ -#ifdef READLINE +#if READLINE extern char *getenv(const char *name) { List *s; if (name == NULL || vp == NULL || (s = varlookup((char *) name)) == NULL) diff --git a/input.c b/input.c @@ -18,9 +18,9 @@ typedef struct Input { bool saved, eofread; } Input; -#define BUFSIZE ((SIZE_T) 256) +#define BUFSIZE ((size_t) 256) -#ifdef READLINE +#if READLINE extern char *readline(char *); extern void add_history(char *); static char *rlinebuf; @@ -37,7 +37,7 @@ static void ugdead(int); static void pushcommon(void); static char *inbuf; -static SIZE_T istacksize, chars_out, chars_in; +static size_t istacksize, chars_out, chars_in; static bool eofread = FALSE, save_lineno = TRUE; static Input *istack, *itop; @@ -87,8 +87,8 @@ static int stringgchar() { /* signal-safe readline wrapper */ -#ifdef READLINE -#ifndef SVSIGS +#if READLINE +#ifndef HAVE_RESTARTABLE_SYSCALLS static char *rc_readline(char *prompt) { char *r; interrupt_happened = FALSE; @@ -108,7 +108,7 @@ static char *rc_readline(char *prompt) { } #else #define rc_readline readline -#endif /* SVSIGS */ +#endif /* HAVE_RESTARTABLE_SYSCALLS */ #endif /* READLINE */ /* @@ -119,9 +119,9 @@ static char *rc_readline(char *prompt) { static int fdgchar() { if (chars_out >= chars_in + 2) { /* has the buffer been exhausted? if so, replenish it */ while (1) { -#ifdef READLINE +#if READLINE if (interactive && istack->fd == 0) { - rlinebuf = readline(prompt); + rlinebuf = rc_readline(prompt); if (rlinebuf == NULL) { chars_in = 0; } else { @@ -143,7 +143,7 @@ static int fdgchar() { uerror("read"); rc_exit(1); } - chars_in = (SIZE_T) r; + chars_in = (size_t) r; } break; } @@ -169,7 +169,7 @@ extern void initinput() { /* push an input source onto the stack. set up a new input buffer, and set gchar() */ static void pushcommon() { - SIZE_T idiff; + size_t idiff; istack->index = chars_out; istack->read = chars_in; istack->ibuf = inbuf; @@ -253,6 +253,7 @@ extern Node *doit(bool execit) { Jbwrap j; Estack e1, e2; Edata jerror; + if (dashen) execit = FALSE; setjmp(j.j); @@ -278,7 +279,7 @@ extern Node *doit(bool execit) { funcall(arglist); } if ((s = varlookup("prompt")) != NULL) { -#ifdef READLINE +#if READLINE prompt = s->w; #else fprint(2, "%s", s->w); @@ -322,7 +323,7 @@ extern Node *parseline(char *extdef) { static void history() { List *hist; - SIZE_T a; + size_t a; if (!interactive || (hist = varlookup("history")) == NULL) return; diff --git a/lex.c b/lex.c @@ -20,7 +20,7 @@ it declared in rc.h) */ -#define BUFSIZE ((SIZE_T) 1000) /* malloc hates power of 2 buffers? */ +#define BUFSIZE ((size_t) 1000) /* malloc hates power of 2 buffers? */ #define BUFMAX (8 * BUFSIZE) /* How big the buffer can get before we re-allocate the space at BUFSIZE again. Premature optimization? Maybe. */ @@ -55,7 +55,7 @@ const char dnw[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; -static SIZE_T bufsize = BUFSIZE; +static size_t bufsize = BUFSIZE; static char *realbuf = NULL; static bool newline = FALSE; static bool errset = FALSE; @@ -73,7 +73,7 @@ extern int yylex() { static bool dollar = FALSE; bool saw_meta = FALSE; int c; - SIZE_T i; /* The purpose of all these local assignments is to */ + size_t i; /* The purpose of all these local assignments is to */ const char *meta; /* allow optimizing compilers like gcc to load these */ char *buf = realbuf; /* values into registers. On a sparc this is a */ YYSTYPE *y = &yylval; /* win, in code size *and* execution time */ @@ -338,7 +338,7 @@ extern void inityy() { extern void print_prompt2() { lineno++; -#ifdef READLINE +#if READLINE prompt = prompt2; #else if (interactive) diff --git a/rc.h b/rc.h @@ -1,4 +1,13 @@ #include "config.h" + +#if HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + +#if HAVE_UNISTD_H +#include <unistd.h> +#endif + #include "proto.h" /* datatypes */ @@ -9,6 +18,24 @@ #include <stdarg.h> +#if HAVE_SETPGRP + +#if SETPGRP_VOID +/* Smells like POSIX: should all be ok. */ +#else +/* BSD: fake it. */ +#define setpgid(pid, pgrp) setpgrp(pid, pgrp) +#define tcsetpgrp(fd, pgrp) ioctl((fd), TIOCSPGRP, &(pgrp)) +#endif + +#else /* HAVE_SETPGRP */ + +/* Nothing doing. */ +#define setpgid +#define tcsetpgrp + +#endif /*HAVE_SETPGRP */ + typedef void builtin_t(char **); typedef struct Block Block; typedef struct Dup Dup; @@ -126,20 +153,21 @@ struct Format { /* for the buffer maintainence routines */ char *buf, *bufbegin, *bufend; int flushed; - void (*grow)(Format *, SIZE_T); + void (*grow)(Format *, size_t); union { int n; void *p; } u; }; /* Format->flags values */ enum { - FMT_long = 1, /* %l */ - FMT_short = 2, /* %h */ - FMT_unsigned = 4, /* %u */ - FMT_zeropad = 8, /* %0 */ - FMT_leftside = 16, /* %- */ - FMT_altform = 32, /* %# */ - FMT_f1set = 64, /* %<n> */ - FMT_f2set = 128 /* %.<n> */ + FMT_quad = 1, /* %q */ + FMT_long = 2, /* %l */ + FMT_short = 4, /* %h */ + FMT_unsigned = 8, /* %u */ + FMT_zeropad = 16, /* %0 */ + FMT_leftside = 32, /* %- */ + FMT_altform = 64, /* %# */ + FMT_f1set = 128, /* %<n> */ + FMT_f2set = 256 /* %.<n> */ }; /* macros */ @@ -158,7 +186,7 @@ enum { #define nnew(x) ((x *) nalloc(sizeof(x))) #define ncpy(x) (strcpy((char *) nalloc(strlen(x) + 1), x)) #ifndef offsetof -#define offsetof(t, m) ((SIZE_T) (((char *) &((t *) 0)->m) - (char *)0)) +#define offsetof(t, m) ((size_t) (((char *) &((t *) 0)->m) - (char *)0)) #endif #define streq(x, y) (*(x) == *(y) && strcmp(x, y) == 0) #define conststrlen(x) (sizeof (x) - 1) @@ -170,7 +198,7 @@ extern char *prompt, *prompt2; extern Rq *redirq; extern bool dashdee, dashee, dashvee, dashex, dashell, dasheye, dashen, dashpee, interactive; -extern int rc_pid; +extern pid_t rc_pid; extern int lineno; /* builtins.c */ @@ -278,19 +306,19 @@ extern const char nw[], dnw[]; /* list.c */ extern void listfree(List *); -extern List *listcpy(List *, void *(*)(SIZE_T)); -extern SIZE_T listlen(List *); +extern List *listcpy(List *, void *(*)(size_t)); +extern size_t listlen(List *); extern int listnel(List *); /* match.c */ extern bool match(char *, char *, char *); /* alloc.c */ -extern void *ealloc(SIZE_T); -extern void *erealloc(void *, SIZE_T); +extern void *ealloc(size_t); +extern void *erealloc(void *, size_t); extern void efree(void *); extern Block *newblock(void); -extern void *nalloc(SIZE_T); +extern void *nalloc(size_t); extern void nfree(void); extern void restoreblock(Block *); @@ -306,7 +334,7 @@ extern Conv fmtinstall(int, Conv); extern bool (*fmtinstall(int, bool (*)(Format *, int)))(Format *, int); extern int printfmt(Format *, const char *); extern int fmtprint(Format *, const char *,...); -extern void fmtappend(Format *, const char *, SIZE_T); +extern void fmtappend(Format *, const char *, size_t); extern void fmtcat(Format *, const char *); extern int fprint(int fd, const char *fmt,...); extern char *mprint(const char *fmt,...); @@ -318,7 +346,7 @@ extern char *nprint(const char *fmt,...); */ #define fmtputc(f, c) {\ if ((f)->buf >= (f)->bufend)\ - (*(f)->grow)((f), (SIZE_T)1);\ + (*(f)->grow)((f), (size_t)1);\ *(f)->buf++ = (c);\ } @@ -342,32 +370,32 @@ extern volatile SIG_ATOMIC_T slow, interrupt_happened; extern int istrue(void); extern int getstatus(void); extern void set(bool); -extern void setstatus(int, int); +extern void setstatus(pid_t, int); extern List *sgetstatus(void); extern void setpipestatus(int [], int); -extern void statprint(int, int); +extern void statprint(pid_t, int); extern void ssetstatus(char **); extern char *strstatus(int s); /* tree.c */ extern Node *mk(int /*nodetype*/,...); -extern Node *treecpy(Node *, void *(*)(SIZE_T)); +extern Node *treecpy(Node *, void *(*)(size_t)); extern void treefree(Node *); /* utils.c */ extern bool isabsolute(char *); extern int n2u(char *, unsigned int); -extern int rc_read(int, char *, SIZE_T); +extern int rc_read(int, char *, size_t); extern int mvfd(int, int); extern int starstrcmp(const void *, const void *); extern void pr_error(char *); extern void panic(char *); extern void uerror(char *); -extern void writeall(int, char *, SIZE_T); +extern void writeall(int, char *, size_t); /* wait.c */ -extern int rc_fork(void); -extern int rc_wait4(int, int *, bool); +extern pid_t rc_fork(void); +extern pid_t rc_wait4(pid_t, int *, bool); extern List *sgetapids(void); extern void waitforall(void); extern bool forked; diff --git a/var.c b/var.c @@ -18,7 +18,7 @@ extern void varassign(char *name, List *def, bool stack) { new = get_var_place(name, stack); new->def = newdef; new->extdef = NULL; -#ifdef READLINE /* need to reset readline() every time TERM or TERMCAP changes */ +#if READLINE /* need to reset readline() every time TERM or TERMCAP changes */ if (interactive && (streq(name, "TERM") || streq(name, "TERMCAP"))) { extern void rl_reset_terminal(char *); rl_reset_terminal(NULL); diff --git a/walk.c b/walk.c @@ -43,12 +43,12 @@ top: sigchk(); case nNowait: { int pid; if ((pid = rc_fork()) == 0) { -#if !defined(NOJOB) && defined(SIGTTOU) && defined(SIGTTIN) && defined(SIGTSTP) +#if defined(RC_JOB) && defined(SIGTTOU) && defined(SIGTTIN) && defined(SIGTSTP) setsigdefaults(FALSE); rc_signal(SIGTTOU, SIG_IGN); /* Berkeleyized version: put it in a new pgroup. */ rc_signal(SIGTTIN, SIG_IGN); rc_signal(SIGTSTP, SIG_IGN); - setpgrp(0, getpid()); + setpgid(0, getpid()); #else setsigdefaults(TRUE); /* ignore SIGINT, SIGQUIT, SIGTERM */ #endif