2001-06-29 04:57:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Minix shell port for busybox
|
|
|
|
*
|
|
|
|
* This version of the Minix shell was adapted for use in busybox
|
2003-07-14 21:21:08 +00:00
|
|
|
* by Erik Andersen <andersen@codepoet.org>
|
2001-06-29 04:57:14 +00:00
|
|
|
*
|
2003-03-14 16:05:59 +00:00
|
|
|
* - backtick expansion did not work properly
|
|
|
|
* Jonas Holmberg <jonas.holmberg@axis.com>
|
|
|
|
* Robert Schwebel <r.schwebel@pengutronix.de>
|
2003-07-14 21:21:08 +00:00
|
|
|
* Erik Andersen <andersen@codepoet.org>
|
2003-03-14 16:05:59 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* Original copyright notice is retained at the end of this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "cmdedit.h"
|
|
|
|
#include "busybox.h"
|
|
|
|
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* Conditional use of "register" keyword */
|
|
|
|
#define REGISTER register
|
|
|
|
|
|
|
|
|
|
|
|
/*#define MSHDEBUG 1*/
|
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
int mshdbg = 0;
|
|
|
|
|
|
|
|
#define DBGPRINTF(x) if(mshdbg>0)printf x
|
|
|
|
#define DBGPRINTF0(x) if(mshdbg>0)printf x
|
|
|
|
#define DBGPRINTF1(x) if(mshdbg>1)printf x
|
|
|
|
#define DBGPRINTF2(x) if(mshdbg>2)printf x
|
|
|
|
#define DBGPRINTF3(x) if(mshdbg>3)printf x
|
|
|
|
#define DBGPRINTF4(x) if(mshdbg>4)printf x
|
|
|
|
#define DBGPRINTF5(x) if(mshdbg>5)printf x
|
|
|
|
#define DBGPRINTF6(x) if(mshdbg>6)printf x
|
|
|
|
#define DBGPRINTF7(x) if(mshdbg>7)printf x
|
|
|
|
#define DBGPRINTF8(x) if(mshdbg>8)printf x
|
|
|
|
#define DBGPRINTF9(x) if(mshdbg>9)printf x
|
|
|
|
|
|
|
|
int mshdbg_rc = 0;
|
|
|
|
|
|
|
|
#define RCPRINTF(x) if(mshdbg_rc)printf x
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DBGPRINTF(x)
|
|
|
|
#define DBGPRINTF0(x)
|
|
|
|
#define DBGPRINTF1(x)
|
|
|
|
#define DBGPRINTF2(x)
|
|
|
|
#define DBGPRINTF3(x)
|
|
|
|
#define DBGPRINTF4(x)
|
|
|
|
#define DBGPRINTF5(x)
|
|
|
|
#define DBGPRINTF6(x)
|
|
|
|
#define DBGPRINTF7(x)
|
|
|
|
#define DBGPRINTF8(x)
|
|
|
|
#define DBGPRINTF9(x)
|
|
|
|
|
|
|
|
#define RCPRINTF(x)
|
|
|
|
|
|
|
|
#endif /* MSHDEBUG */
|
|
|
|
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/* -------- sh.h -------- */
|
|
|
|
/*
|
|
|
|
* shell
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#define LINELIM 2100
|
|
|
|
#define NPUSH 8 /* limit to input nesting */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2002-12-11 07:42:46 +00:00
|
|
|
#undef NOFILE
|
2004-08-04 19:19:10 +00:00
|
|
|
#define NOFILE 20 /* Number of open files */
|
|
|
|
#define NUFILE 10 /* Number of user-accessible files */
|
|
|
|
#define FDBASE 10 /* First file usable by Shell */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* values returned by wait
|
|
|
|
*/
|
2004-08-04 19:19:10 +00:00
|
|
|
#define WAITSIG(s) ((s)&0177)
|
|
|
|
#define WAITVAL(s) (((s)>>8)&0377)
|
2001-06-29 04:57:14 +00:00
|
|
|
#define WAITCORE(s) (((s)&0200)!=0)
|
|
|
|
|
|
|
|
/*
|
2004-04-14 17:51:38 +00:00
|
|
|
* library and system definitions
|
2001-06-29 04:57:14 +00:00
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
typedef void xint; /* base type of jmp_buf, for not broken compilers */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* shell components
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define QUOTE 0200
|
|
|
|
|
|
|
|
#define NOBLOCK ((struct op *)NULL)
|
|
|
|
#define NOWORD ((char *)NULL)
|
|
|
|
#define NOWORDS ((char **)NULL)
|
|
|
|
#define NOPIPE ((int *)NULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Description of a command or an operation on commands.
|
|
|
|
* Might eventually use a union.
|
|
|
|
*/
|
|
|
|
struct op {
|
2004-08-04 19:16:54 +00:00
|
|
|
int type; /* operation type, see below */
|
|
|
|
char **words; /* arguments to a command */
|
|
|
|
struct ioword **ioact; /* IO actions (eg, < > >>) */
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *left;
|
|
|
|
struct op *right;
|
2004-08-04 19:16:54 +00:00
|
|
|
char *str; /* identifier for case and for */
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
#define TCOM 1 /* command */
|
|
|
|
#define TPAREN 2 /* (c-list) */
|
|
|
|
#define TPIPE 3 /* a | b */
|
|
|
|
#define TLIST 4 /* a [&;] b */
|
2004-08-04 19:19:10 +00:00
|
|
|
#define TOR 5 /* || */
|
2004-08-04 19:16:54 +00:00
|
|
|
#define TAND 6 /* && */
|
2001-06-29 04:57:14 +00:00
|
|
|
#define TFOR 7
|
2004-08-04 19:19:10 +00:00
|
|
|
#define TDO 8
|
2001-06-29 04:57:14 +00:00
|
|
|
#define TCASE 9
|
2004-08-04 19:19:10 +00:00
|
|
|
#define TIF 10
|
2001-06-29 04:57:14 +00:00
|
|
|
#define TWHILE 11
|
|
|
|
#define TUNTIL 12
|
|
|
|
#define TELIF 13
|
2004-08-04 19:16:54 +00:00
|
|
|
#define TPAT 14 /* pattern in case */
|
|
|
|
#define TBRACE 15 /* {c-list} */
|
|
|
|
#define TASYNC 16 /* c & */
|
2004-08-04 19:19:10 +00:00
|
|
|
/* Added to support "." file expansion */
|
|
|
|
#define TDOT 17
|
|
|
|
|
|
|
|
/* Strings for names to make debug easier */
|
2005-09-22 14:38:17 +00:00
|
|
|
#ifdef MSHDEBUG
|
|
|
|
static char *T_CMD_NAMES[] = {
|
2004-08-04 19:19:10 +00:00
|
|
|
"PLACEHOLDER",
|
|
|
|
"TCOM",
|
|
|
|
"TPAREN",
|
|
|
|
"TPIPE",
|
|
|
|
"TLIST",
|
|
|
|
"TOR",
|
|
|
|
"TAND",
|
|
|
|
"TFOR",
|
|
|
|
"TDO",
|
|
|
|
"TCASE",
|
|
|
|
"TIF",
|
|
|
|
"TWHILE",
|
|
|
|
"TUNTIL",
|
|
|
|
"TELIF",
|
|
|
|
"TPAT",
|
|
|
|
"TBRACE",
|
|
|
|
"TASYNC",
|
|
|
|
"TDOT",
|
|
|
|
};
|
2005-09-22 14:38:17 +00:00
|
|
|
#endif
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* actions determining the environment of a process
|
|
|
|
*/
|
|
|
|
#define BIT(i) (1<<(i))
|
2004-08-04 19:16:54 +00:00
|
|
|
#define FEXEC BIT(0) /* execute without forking */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#if 0 /* Original value */
|
|
|
|
#define AREASIZE (65000)
|
|
|
|
#else
|
|
|
|
#define AREASIZE (90000)
|
|
|
|
#endif
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* flags to control evaluation of words
|
|
|
|
*/
|
2004-08-04 19:19:10 +00:00
|
|
|
#define DOSUB 1 /* interpret $, `, and quotes */
|
|
|
|
#define DOBLANK 2 /* perform blank interpretation */
|
|
|
|
#define DOGLOB 4 /* interpret [?* */
|
|
|
|
#define DOKEY 8 /* move words with `=' to 2nd arg. list */
|
|
|
|
#define DOTRIM 16 /* trim resulting string */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* PROTOTYPES */
|
2001-06-29 04:57:14 +00:00
|
|
|
static int newfile(char *s);
|
|
|
|
static char *findeq(char *cp);
|
|
|
|
static char *cclass(char *p, int sub);
|
|
|
|
static void initarea(void);
|
2001-08-01 17:21:35 +00:00
|
|
|
extern int msh_main(int argc, char **argv);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
struct brkcon {
|
|
|
|
jmp_buf brkpt;
|
|
|
|
struct brkcon *nextlev;
|
|
|
|
};
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* redirection
|
|
|
|
*/
|
|
|
|
struct ioword {
|
2004-08-04 19:16:54 +00:00
|
|
|
short io_unit; /* unit affected */
|
|
|
|
short io_flag; /* action (below) */
|
|
|
|
char *io_name; /* file name */
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#define IOREAD 1 /* < */
|
|
|
|
#define IOHERE 2 /* << (here file) */
|
|
|
|
#define IOWRITE 4 /* > */
|
|
|
|
#define IOCAT 8 /* >> */
|
|
|
|
#define IOXHERE 16 /* ${}, ` in << */
|
|
|
|
#define IODUP 32 /* >&digit */
|
|
|
|
#define IOCLOSE 64 /* >&- */
|
2004-08-04 19:16:54 +00:00
|
|
|
|
|
|
|
#define IODEFAULT (-1) /* token for default IO unit */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parsing & execution environment
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct env {
|
|
|
|
char *linep;
|
|
|
|
struct io *iobase;
|
|
|
|
struct io *iop;
|
2004-08-04 19:19:10 +00:00
|
|
|
xint *errpt; /* void * */
|
2004-08-04 19:16:54 +00:00
|
|
|
int iofd;
|
|
|
|
struct env *oenv;
|
2001-06-29 04:57:14 +00:00
|
|
|
} e;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* flags:
|
|
|
|
* -e: quit on error
|
|
|
|
* -k: look for name=value everywhere on command line
|
|
|
|
* -n: no execution
|
|
|
|
* -t: exit after reading and executing one command
|
|
|
|
* -v: echo as read
|
|
|
|
* -x: trace
|
|
|
|
* -u: unset variables net diagnostic
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *flag;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *null; /* null value for variable */
|
|
|
|
static int intr; /* interrupt pending */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *trap[_NSIG + 1];
|
|
|
|
static char ourtrap[_NSIG + 1];
|
|
|
|
static int trapset; /* trap pending */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int heedint; /* heed interrupt signals */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int yynerrs; /* yacc */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char line[LINELIM];
|
|
|
|
static char *elinep;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* other functions
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int (*inbuilt(char *s)) (struct op *);
|
|
|
|
|
|
|
|
static char *rexecve(char *c, char **v, char **envp);
|
|
|
|
static char *space(int n);
|
|
|
|
static char *strsave(char *s, int a);
|
|
|
|
static char *evalstr(char *cp, int f);
|
|
|
|
static char *putn(int n);
|
|
|
|
static char *itoa(int n);
|
|
|
|
static char *unquote(char *as);
|
|
|
|
static struct var *lookup(char *n);
|
|
|
|
static int rlookup(char *n);
|
|
|
|
static struct wdblock *glob(char *cp, struct wdblock *wb);
|
|
|
|
static int my_getc(int ec);
|
|
|
|
static int subgetc(int ec, int quoted);
|
2004-09-02 23:13:10 +00:00
|
|
|
static char **makenv(int all, struct wdblock *wb);
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **eval(char **ap, int f);
|
|
|
|
static int setstatus(int s);
|
|
|
|
static int waitfor(int lastpid, int canintr);
|
|
|
|
|
|
|
|
static void onintr(int s); /* SIGINT handler */
|
|
|
|
|
|
|
|
static int newenv(int f);
|
|
|
|
static void quitenv(void);
|
|
|
|
static void err(char *s);
|
|
|
|
static int anys(char *s1, char *s2);
|
|
|
|
static int any(int c, char *s);
|
|
|
|
static void next(int f);
|
|
|
|
static void setdash(void);
|
|
|
|
static void onecommand(void);
|
|
|
|
static void runtrap(int i);
|
|
|
|
static int gmatch(char *s, char *p);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* error handling
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void leave(void); /* abort shell (or fail in subshell) */
|
|
|
|
static void fail(void); /* fail but return to process next command */
|
|
|
|
static void warn(char *s);
|
|
|
|
static void sig(int i); /* default signal handler */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- area stuff -------- */
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#define REGSIZE sizeof(struct region)
|
|
|
|
#define GROWBY (256)
|
|
|
|
/* #define SHRINKBY (64) */
|
2001-06-29 04:57:14 +00:00
|
|
|
#undef SHRINKBY
|
2006-01-25 00:08:53 +00:00
|
|
|
#define FREE (32767)
|
|
|
|
#define BUSY (0)
|
|
|
|
#define ALIGN (sizeof(int)-1)
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct region {
|
2004-08-04 19:16:54 +00:00
|
|
|
struct region *next;
|
|
|
|
int area;
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- grammar stuff -------- */
|
|
|
|
typedef union {
|
2004-08-04 19:16:54 +00:00
|
|
|
char *cp;
|
|
|
|
char **wp;
|
|
|
|
int i;
|
|
|
|
struct op *o;
|
2001-06-29 04:57:14 +00:00
|
|
|
} YYSTYPE;
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
#define WORD 256
|
|
|
|
#define LOGAND 257
|
|
|
|
#define LOGOR 258
|
|
|
|
#define BREAK 259
|
2004-08-04 19:19:10 +00:00
|
|
|
#define IF 260
|
2001-06-29 04:57:14 +00:00
|
|
|
#define THEN 261
|
|
|
|
#define ELSE 262
|
|
|
|
#define ELIF 263
|
2004-08-04 19:19:10 +00:00
|
|
|
#define FI 264
|
2001-06-29 04:57:14 +00:00
|
|
|
#define CASE 265
|
|
|
|
#define ESAC 266
|
2004-08-04 19:19:10 +00:00
|
|
|
#define FOR 267
|
2001-06-29 04:57:14 +00:00
|
|
|
#define WHILE 268
|
|
|
|
#define UNTIL 269
|
2004-08-04 19:19:10 +00:00
|
|
|
#define DO 270
|
2001-06-29 04:57:14 +00:00
|
|
|
#define DONE 271
|
2004-08-04 19:19:10 +00:00
|
|
|
#define IN 272
|
|
|
|
/* Added for "." file expansion */
|
|
|
|
#define DOT 273
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
#define YYERRCODE 300
|
|
|
|
|
|
|
|
/* flags to yylex */
|
2004-08-04 19:16:54 +00:00
|
|
|
#define CONTIN 01 /* skip new lines to complete command */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
#define SYNTAXERR zzerr()
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *pipeline(int cf);
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct op *andor(void);
|
|
|
|
static struct op *c_list(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static int synio(int cf);
|
|
|
|
static void musthave(int c, int cf);
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct op *simple(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *nested(int type, int mark);
|
|
|
|
static struct op *command(int cf);
|
|
|
|
static struct op *dogroup(int onlydone);
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct op *thenpart(void);
|
|
|
|
static struct op *elsepart(void);
|
|
|
|
static struct op *caselist(void);
|
|
|
|
static struct op *casepart(void);
|
|
|
|
static char **pattern(void);
|
|
|
|
static char **wordlist(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *list(struct op *t1, struct op *t2);
|
|
|
|
static struct op *block(int type, struct op *t1, struct op *t2, char **wp);
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct op *newtp(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *namelist(struct op *t);
|
2001-06-29 04:57:14 +00:00
|
|
|
static char **copyw(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static void word(char *cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct ioword **copyio(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct ioword *io(int u, int f, char *cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
static void zzerr(void);
|
2004-08-04 19:16:54 +00:00
|
|
|
static void yyerror(char *s);
|
|
|
|
static int yylex(int cf);
|
|
|
|
static int collect(int c, int c1);
|
|
|
|
static int dual(int c);
|
|
|
|
static void diag(int ec);
|
|
|
|
static char *tree(unsigned size);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/* -------- var.h -------- */
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
struct var {
|
|
|
|
char *value;
|
|
|
|
char *name;
|
|
|
|
struct var *next;
|
|
|
|
char status;
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
2004-08-04 19:16:54 +00:00
|
|
|
|
|
|
|
#define COPYV 1 /* flag to setval, suggesting copy */
|
|
|
|
#define RONLY 01 /* variable is read-only */
|
|
|
|
#define EXPORT 02 /* variable is to be exported */
|
|
|
|
#define GETCELL 04 /* name & value space was got with getcell */
|
|
|
|
|
|
|
|
static int yyparse(void);
|
|
|
|
static struct var *lookup(char *n);
|
|
|
|
static void setval(struct var *vp, char *val);
|
|
|
|
static void nameval(struct var *vp, char *val, char *name);
|
|
|
|
static void export(struct var *vp);
|
|
|
|
static void ronly(struct var *vp);
|
|
|
|
static int isassign(char *s);
|
|
|
|
static int checkname(char *cp);
|
|
|
|
static int assign(char *s, int cf);
|
|
|
|
static void putvlist(int f, int out);
|
|
|
|
static int eqname(char *n1, char *n2);
|
|
|
|
|
|
|
|
static int execute(struct op *t, int *pin, int *pout, int act);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/* -------- io.h -------- */
|
|
|
|
/* io buffer */
|
|
|
|
struct iobuf {
|
2004-08-04 19:16:54 +00:00
|
|
|
unsigned id; /* buffer id */
|
|
|
|
char buf[512]; /* buffer */
|
|
|
|
char *bufp; /* pointer into buffer */
|
|
|
|
char *ebufp; /* pointer to end of buffer */
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* possible arguments to an IO function */
|
|
|
|
struct ioarg {
|
2004-08-04 19:16:54 +00:00
|
|
|
char *aword;
|
|
|
|
char **awordlist;
|
|
|
|
int afile; /* file descriptor */
|
|
|
|
unsigned afid; /* buffer id */
|
|
|
|
long afpos; /* file position */
|
|
|
|
struct iobuf *afbuf; /* buffer for this file */
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
//static struct ioarg ioargstack[NPUSH];
|
|
|
|
#define AFID_NOBUF (~0)
|
|
|
|
#define AFID_ID 0
|
|
|
|
|
|
|
|
/* an input generator's state */
|
2004-08-04 19:16:54 +00:00
|
|
|
struct io {
|
|
|
|
int (*iofn) (struct ioarg *, struct io *);
|
|
|
|
struct ioarg *argp;
|
|
|
|
int peekc;
|
|
|
|
char prev; /* previous character read by readc() */
|
|
|
|
char nlcount; /* for `'s */
|
|
|
|
char xchar; /* for `'s */
|
|
|
|
char task; /* reason for pushed IO */
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
2004-08-04 19:16:54 +00:00
|
|
|
|
|
|
|
//static struct io iostack[NPUSH];
|
|
|
|
#define XOTHER 0 /* none of the below */
|
|
|
|
#define XDOLL 1 /* expanding ${} */
|
|
|
|
#define XGRAVE 2 /* expanding `'s */
|
|
|
|
#define XIO 3 /* file IO */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/* in substitution */
|
|
|
|
#define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* input generators for IO structure
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int nlchar(struct ioarg *ap);
|
|
|
|
static int strchar(struct ioarg *ap);
|
|
|
|
static int qstrchar(struct ioarg *ap);
|
|
|
|
static int filechar(struct ioarg *ap);
|
|
|
|
static int herechar(struct ioarg *ap);
|
|
|
|
static int linechar(struct ioarg *ap);
|
|
|
|
static int gravechar(struct ioarg *ap, struct io *iop);
|
|
|
|
static int qgravechar(struct ioarg *ap, struct io *iop);
|
|
|
|
static int dolchar(struct ioarg *ap);
|
|
|
|
static int wdchar(struct ioarg *ap);
|
|
|
|
static void scraphere(void);
|
|
|
|
static void freehere(int area);
|
|
|
|
static void gethere(void);
|
|
|
|
static void markhere(char *s, struct ioword *iop);
|
|
|
|
static int herein(char *hname, int xdoll);
|
|
|
|
static int run(struct ioarg *argp, int (*f) (struct ioarg *));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* IO functions
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int eofc(void);
|
|
|
|
static int readc(void);
|
|
|
|
static void unget(int c);
|
|
|
|
static void ioecho(int c);
|
2005-10-17 09:48:57 +00:00
|
|
|
static void prs(const char *s);
|
2004-08-04 19:16:54 +00:00
|
|
|
static void prn(unsigned u);
|
|
|
|
static void closef(int i);
|
|
|
|
static void closeall(void);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* IO control
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
|
|
|
|
static int remap(int fd);
|
|
|
|
static int openpipe(int *pv);
|
|
|
|
static void closepipe(int *pv);
|
|
|
|
static struct io *setbase(struct io *ip);
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
#define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
|
|
|
|
#define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
|
|
|
|
|
|
|
|
/* -------- word.h -------- */
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
#define NSTART 16 /* default number of words to allow for initially */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
struct wdblock {
|
|
|
|
short w_bsize;
|
|
|
|
short w_nword;
|
2001-06-29 04:57:14 +00:00
|
|
|
/* bounds are arbitrary */
|
2004-08-04 19:16:54 +00:00
|
|
|
char *w_words[1];
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct wdblock *addword(char *wd, struct wdblock *wb);
|
|
|
|
static struct wdblock *newword(int nw);
|
|
|
|
static char **getwords(struct wdblock *wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/* -------- area.h -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* storage allocation
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *getcell(unsigned nbytes);
|
|
|
|
static void garbage(void);
|
|
|
|
static void setarea(char *cp, int a);
|
|
|
|
static int getarea(char *cp);
|
|
|
|
static void freearea(int a);
|
|
|
|
static void freecell(char *cp);
|
|
|
|
static int areanum; /* current allocation area */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#define NEW(type) (type *)getcell(sizeof(type))
|
2001-06-29 04:57:14 +00:00
|
|
|
#define DELETE(obj) freecell((char *)obj)
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- misc stuff -------- */
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp);
|
2004-08-04 19:16:54 +00:00
|
|
|
static int iosetup(struct ioword *iop, int pipein, int pipeout);
|
|
|
|
static void echo(char **wp);
|
|
|
|
static struct op **find1case(struct op *t, char *w);
|
|
|
|
static struct op *findcase(struct op *t, char *w);
|
|
|
|
static void brkset(struct brkcon *bc);
|
|
|
|
static int dolabel(struct op *t);
|
|
|
|
static int dohelp(struct op *t);
|
|
|
|
static int dochdir(struct op *t);
|
|
|
|
static int doshift(struct op *t);
|
|
|
|
static int dologin(struct op *t);
|
|
|
|
static int doumask(struct op *t);
|
|
|
|
static int doexec(struct op *t);
|
|
|
|
static int dodot(struct op *t);
|
|
|
|
static int dowait(struct op *t);
|
|
|
|
static int doread(struct op *t);
|
|
|
|
static int doeval(struct op *t);
|
|
|
|
static int dotrap(struct op *t);
|
|
|
|
static int getsig(char *s);
|
|
|
|
static void setsig(int n, sighandler_t f);
|
|
|
|
static int getn(char *as);
|
|
|
|
static int dobreak(struct op *t);
|
|
|
|
static int docontinue(struct op *t);
|
|
|
|
static int brkcontin(char *cp, int val);
|
|
|
|
static int doexit(struct op *t);
|
|
|
|
static int doexport(struct op *t);
|
|
|
|
static int doreadonly(struct op *t);
|
|
|
|
static void rdexp(char **wp, void (*f) (struct var *), int key);
|
|
|
|
static void badid(char *s);
|
|
|
|
static int doset(struct op *t);
|
|
|
|
static void varput(char *s, int out);
|
|
|
|
static int dotimes(struct op *t);
|
|
|
|
static int expand(char *cp, struct wdblock **wbp, int f);
|
|
|
|
static char *blank(int f);
|
|
|
|
static int dollar(int quoted);
|
|
|
|
static int grave(int quoted);
|
|
|
|
static void globname(char *we, char *pp);
|
|
|
|
static char *generate(char *start1, char *end1, char *middle, char *end);
|
|
|
|
static int anyspcl(struct wdblock *wb);
|
|
|
|
static int xstrcmp(char *p1, char *p2);
|
|
|
|
static void glob0(char *a0, unsigned int a1, int a2,
|
|
|
|
int (*a3) (char *, char *));
|
|
|
|
static void glob1(char *base, char *lim);
|
|
|
|
static void glob2(char *i, char *j);
|
|
|
|
static void glob3(char *i, char *j, char *k);
|
|
|
|
static void readhere(char **name, char *s, int ec);
|
|
|
|
static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
|
|
|
|
static int xxchar(struct ioarg *ap);
|
|
|
|
|
|
|
|
struct here {
|
|
|
|
char *h_tag;
|
|
|
|
int h_dosub;
|
|
|
|
struct ioword *h_iop;
|
|
|
|
struct here *h_next;
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
2005-10-17 09:48:57 +00:00
|
|
|
static const char * const signame[] = {
|
2001-06-29 04:57:14 +00:00
|
|
|
"Signal 0",
|
|
|
|
"Hangup",
|
2004-08-04 19:16:54 +00:00
|
|
|
(char *) NULL, /* interrupt */
|
2001-06-29 04:57:14 +00:00
|
|
|
"Quit",
|
|
|
|
"Illegal instruction",
|
|
|
|
"Trace/BPT trap",
|
|
|
|
"Abort",
|
|
|
|
"Bus error",
|
|
|
|
"Floating Point Exception",
|
|
|
|
"Killed",
|
|
|
|
"SIGUSR1",
|
|
|
|
"SIGSEGV",
|
|
|
|
"SIGUSR2",
|
2004-08-04 19:16:54 +00:00
|
|
|
(char *) NULL, /* broken pipe */
|
2001-06-29 04:57:14 +00:00
|
|
|
"Alarm clock",
|
|
|
|
"Terminated",
|
|
|
|
};
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
#define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
|
|
|
|
|
|
|
|
struct res {
|
2005-10-17 09:48:57 +00:00
|
|
|
const char *r_name;
|
2004-08-04 19:16:54 +00:00
|
|
|
int r_val;
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
2005-10-17 09:48:57 +00:00
|
|
|
static const struct res restab[] = {
|
2004-08-04 19:16:54 +00:00
|
|
|
{"for", FOR},
|
|
|
|
{"case", CASE},
|
|
|
|
{"esac", ESAC},
|
|
|
|
{"while", WHILE},
|
|
|
|
{"do", DO},
|
|
|
|
{"done", DONE},
|
|
|
|
{"if", IF},
|
|
|
|
{"in", IN},
|
|
|
|
{"then", THEN},
|
|
|
|
{"else", ELSE},
|
|
|
|
{"elif", ELIF},
|
|
|
|
{"until", UNTIL},
|
|
|
|
{"fi", FI},
|
|
|
|
{";;", BREAK},
|
|
|
|
{"||", LOGOR},
|
|
|
|
{"&&", LOGAND},
|
|
|
|
{"{", '{'},
|
|
|
|
{"}", '}'},
|
2004-08-04 19:19:10 +00:00
|
|
|
{".", DOT},
|
2004-08-04 19:16:54 +00:00
|
|
|
{0, 0},
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-07-07 00:05:55 +00:00
|
|
|
struct builtincmd {
|
|
|
|
const char *name;
|
2004-08-04 19:16:54 +00:00
|
|
|
int (*builtinfunc) (struct op * t);
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
2004-08-04 19:16:54 +00:00
|
|
|
static const struct builtincmd builtincmds[] = {
|
|
|
|
{".", dodot},
|
|
|
|
{":", dolabel},
|
|
|
|
{"break", dobreak},
|
|
|
|
{"cd", dochdir},
|
|
|
|
{"continue", docontinue},
|
|
|
|
{"eval", doeval},
|
|
|
|
{"exec", doexec},
|
|
|
|
{"exit", doexit},
|
|
|
|
{"export", doexport},
|
|
|
|
{"help", dohelp},
|
|
|
|
{"login", dologin},
|
|
|
|
{"newgrp", dologin},
|
|
|
|
{"read", doread},
|
|
|
|
{"readonly", doreadonly},
|
|
|
|
{"set", doset},
|
|
|
|
{"shift", doshift},
|
|
|
|
{"times", dotimes},
|
|
|
|
{"trap", dotrap},
|
|
|
|
{"umask", doumask},
|
|
|
|
{"wait", dowait},
|
|
|
|
{0, 0}
|
2001-06-29 04:57:14 +00:00
|
|
|
};
|
|
|
|
|
2005-09-22 14:38:17 +00:00
|
|
|
static struct op *scantree(struct op *);
|
2004-08-04 19:19:10 +00:00
|
|
|
static struct op *dowholefile(int, int);
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/* Globals */
|
2004-08-04 19:16:54 +00:00
|
|
|
extern char **environ; /* environment pointer */
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **dolv;
|
|
|
|
static int dolc;
|
|
|
|
static int exstat;
|
|
|
|
static char gflg;
|
2005-10-17 09:48:57 +00:00
|
|
|
static int interactive; /* Is this an interactive shell */
|
2004-08-04 19:16:54 +00:00
|
|
|
static int execflg;
|
|
|
|
static int multiline; /* \n changed to ; */
|
|
|
|
static struct op *outtree; /* result from parser */
|
|
|
|
static xint *failpt;
|
|
|
|
static xint *errpt;
|
|
|
|
static struct brkcon *brklist;
|
|
|
|
static int isbreak;
|
|
|
|
static struct wdblock *wdlist;
|
|
|
|
static struct wdblock *iolist;
|
|
|
|
static char *trap[_NSIG + 1];
|
|
|
|
static char ourtrap[_NSIG + 1];
|
|
|
|
static int trapset; /* trap pending */
|
|
|
|
static int yynerrs; /* yacc */
|
|
|
|
static char line[LINELIM];
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
static struct var *mshdbg_var;
|
|
|
|
#endif
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct var *vlist; /* dictionary */
|
|
|
|
static struct var *homedir; /* home directory */
|
|
|
|
static struct var *prompt; /* main prompt */
|
|
|
|
static struct var *cprompt; /* continuation prompt */
|
|
|
|
static struct var *path; /* search path for commands */
|
|
|
|
static struct var *shell; /* shell to interpret command files */
|
|
|
|
static struct var *ifs; /* field separators */
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int areanum; /* current allocation area */
|
|
|
|
static int intr;
|
|
|
|
static int inparse;
|
|
|
|
static char flags['z' - 'a' + 1];
|
|
|
|
static char *flag = flags - 'a';
|
|
|
|
static char *null = "";
|
|
|
|
static int heedint = 1;
|
|
|
|
static void (*qflag) (int) = SIG_IGN;
|
|
|
|
static int startl;
|
|
|
|
static int peeksym;
|
|
|
|
static int nlseen;
|
|
|
|
static int iounit = IODEFAULT;
|
|
|
|
static YYSTYPE yylval;
|
2004-08-04 19:19:10 +00:00
|
|
|
static char *elinep = line + sizeof(line) - 5;
|
|
|
|
|
|
|
|
static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */
|
|
|
|
static struct ioarg ioargstack[NPUSH];
|
|
|
|
static struct io iostack[NPUSH];
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct iobuf sharedbuf = { AFID_NOBUF };
|
|
|
|
static struct iobuf mainbuf = { AFID_NOBUF };
|
2001-06-29 04:57:14 +00:00
|
|
|
static unsigned bufid = AFID_ID; /* buffer id counter */
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct here *inhere; /* list of hear docs while parsing */
|
|
|
|
static struct here *acthere; /* list of active here documents */
|
|
|
|
static struct region *areabot; /* bottom of area */
|
|
|
|
static struct region *areatop; /* top of area */
|
|
|
|
static struct region *areanxt; /* starting point of scan */
|
|
|
|
static void *brktop;
|
|
|
|
static void *brkaddr;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
static struct env e = {
|
|
|
|
line, /* linep: char ptr */
|
|
|
|
iostack, /* iobase: struct io ptr */
|
|
|
|
iostack - 1, /* iop: struct io ptr */
|
|
|
|
(xint *) NULL, /* errpt: void ptr for errors? */
|
|
|
|
FDBASE, /* iofd: file desc */
|
|
|
|
(struct env *) NULL /* oenv: struct env ptr */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
void print_t(struct op *t)
|
|
|
|
{
|
|
|
|
DBGPRINTF(("T: t=0x%x, type %s, words=0x%x, IOword=0x%x\n", t,
|
|
|
|
T_CMD_NAMES[t->type], t->words, t->ioact));
|
|
|
|
|
|
|
|
if (t->words) {
|
|
|
|
DBGPRINTF(("T: W1: %s", t->words[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_tree(struct op *head)
|
|
|
|
{
|
|
|
|
if (head == NULL) {
|
|
|
|
DBGPRINTF(("PRINT_TREE: no tree\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("NODE: 0x%x, left 0x%x, right 0x%x\n", head, head->left,
|
|
|
|
head->right));
|
|
|
|
|
|
|
|
if (head->left)
|
|
|
|
print_tree(head->left);
|
|
|
|
|
|
|
|
if (head->right)
|
|
|
|
print_tree(head->right);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* MSHDEBUG */
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *current_prompt;
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* -------- sh.c -------- */
|
|
|
|
/*
|
|
|
|
* shell
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
int msh_main(int argc, char **argv)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int f;
|
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
int cflag;
|
|
|
|
char *name, **ap;
|
2004-08-04 19:16:54 +00:00
|
|
|
int (*iof) (struct ioarg *);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("MSH_MAIN: argc %d, environ 0x%x\n", argc, environ));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
initarea();
|
|
|
|
if ((ap = environ) != NULL) {
|
|
|
|
while (*ap)
|
|
|
|
assign(*ap++, !COPYV);
|
|
|
|
for (ap = environ; *ap;)
|
|
|
|
export(lookup(*ap++));
|
|
|
|
}
|
|
|
|
closeall();
|
|
|
|
areanum = 1;
|
|
|
|
|
|
|
|
shell = lookup("SHELL");
|
|
|
|
if (shell->value == null)
|
2004-08-27 19:55:28 +00:00
|
|
|
setval(shell, (char *)DEFAULT_SHELL);
|
2001-06-29 04:57:14 +00:00
|
|
|
export(shell);
|
|
|
|
|
|
|
|
homedir = lookup("HOME");
|
|
|
|
if (homedir->value == null)
|
|
|
|
setval(homedir, "/");
|
|
|
|
export(homedir);
|
|
|
|
|
2003-03-14 16:05:59 +00:00
|
|
|
setval(lookup("$"), putn(getpid()));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
path = lookup("PATH");
|
2003-03-14 16:05:59 +00:00
|
|
|
if (path->value == null) {
|
|
|
|
if (geteuid() == 0)
|
|
|
|
setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
|
|
|
|
else
|
|
|
|
setval(path, "/bin:/usr/bin");
|
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
export(path);
|
|
|
|
|
|
|
|
ifs = lookup("IFS");
|
|
|
|
if (ifs->value == null)
|
|
|
|
setval(ifs, " \t\n");
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#ifdef MSHDEBUG
|
|
|
|
mshdbg_var = lookup("MSHDEBUG");
|
|
|
|
if (mshdbg_var->value == null)
|
|
|
|
setval(mshdbg_var, "0");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
prompt = lookup("PS1");
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
|
2001-06-29 04:57:14 +00:00
|
|
|
if (prompt->value == null)
|
2001-07-07 00:05:55 +00:00
|
|
|
#endif
|
2001-06-29 04:57:14 +00:00
|
|
|
setval(prompt, "$ ");
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
setval(prompt, "# ");
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
}
|
|
|
|
cprompt = lookup("PS2");
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
|
2001-06-29 04:57:14 +00:00
|
|
|
if (cprompt->value == null)
|
2001-07-07 00:05:55 +00:00
|
|
|
#endif
|
2001-06-29 04:57:14 +00:00
|
|
|
setval(cprompt, "> ");
|
|
|
|
|
|
|
|
iof = filechar;
|
|
|
|
cflag = 0;
|
|
|
|
name = *argv++;
|
|
|
|
if (--argc >= 1) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (argv[0][0] == '-' && argv[0][1] != '\0') {
|
|
|
|
for (s = argv[0] + 1; *s; s++)
|
2001-06-29 04:57:14 +00:00
|
|
|
switch (*s) {
|
|
|
|
case 'c':
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
cprompt->status &= ~EXPORT;
|
|
|
|
setval(prompt, "");
|
|
|
|
setval(cprompt, "");
|
|
|
|
cflag = 1;
|
|
|
|
if (--argc > 0)
|
|
|
|
PUSHIO(aword, *++argv, iof = nlchar);
|
|
|
|
break;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
case 'q':
|
|
|
|
qflag = SIG_DFL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
/* standard input */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
setval(prompt, "");
|
|
|
|
iof = linechar;
|
|
|
|
break;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
case 'i':
|
|
|
|
interactive++;
|
|
|
|
default:
|
2004-08-04 19:16:54 +00:00
|
|
|
if (*s >= 'a' && *s <= 'z')
|
|
|
|
flag[(int) *s]++;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argv--;
|
|
|
|
argc++;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (iof == filechar && --argc > 0) {
|
|
|
|
setval(prompt, "");
|
|
|
|
setval(cprompt, "");
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
cprompt->status &= ~EXPORT;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
/* Shell is non-interactive, activate printf-based debug */
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0');
|
|
|
|
if (mshdbg < 0)
|
|
|
|
mshdbg = 0;
|
|
|
|
#endif
|
|
|
|
DBGPRINTF(("MSH_MAIN: calling newfile()\n"));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (newfile(name = *++argv))
|
2004-08-04 19:19:10 +00:00
|
|
|
exit(1); /* Exit on error */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
setdash();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
/* This won't be true if PUSHIO has been called, say from newfile() above */
|
2001-06-29 04:57:14 +00:00
|
|
|
if (e.iop < iostack) {
|
|
|
|
PUSHIO(afile, 0, iof);
|
2001-07-07 00:05:55 +00:00
|
|
|
if (isatty(0) && isatty(1) && !cflag) {
|
2001-06-29 04:57:14 +00:00
|
|
|
interactive++;
|
2004-03-15 08:29:22 +00:00
|
|
|
#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
|
2004-08-04 19:19:10 +00:00
|
|
|
#ifdef MSHDEBUG
|
2006-02-16 15:40:24 +00:00
|
|
|
printf("\n\n%s Built-in shell (msh with debug)\n", BB_BANNER);
|
2004-08-04 19:19:10 +00:00
|
|
|
#else
|
2006-02-16 15:40:24 +00:00
|
|
|
printf("\n\n%s Built-in shell (msh)\n", BB_BANNER);
|
2004-08-04 19:19:10 +00:00
|
|
|
#endif
|
2004-08-04 19:16:54 +00:00
|
|
|
printf("Enter 'help' for a list of built-in commands.\n\n");
|
2001-10-19 00:22:23 +00:00
|
|
|
#endif
|
2001-07-07 00:05:55 +00:00
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
signal(SIGQUIT, qflag);
|
|
|
|
if (name && name[0] == '-') {
|
|
|
|
interactive++;
|
|
|
|
if ((f = open(".profile", 0)) >= 0)
|
|
|
|
next(remap(f));
|
|
|
|
if ((f = open("/etc/profile", 0)) >= 0)
|
|
|
|
next(remap(f));
|
|
|
|
}
|
|
|
|
if (interactive)
|
|
|
|
signal(SIGTERM, sig);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGINT, onintr);
|
|
|
|
dolv = argv;
|
|
|
|
dolc = argc;
|
|
|
|
dolv[0] = name;
|
|
|
|
if (dolc > 1) {
|
|
|
|
for (ap = ++argv; --argc > 0;) {
|
|
|
|
if (assign(*ap = *argv++, !COPYV)) {
|
2004-08-04 19:16:54 +00:00
|
|
|
dolc--; /* keyword */
|
2001-06-29 04:57:14 +00:00
|
|
|
} else {
|
|
|
|
ap++;
|
|
|
|
}
|
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop 0x%x, iostack 0x%x\n", interactive, e.iop, iostack));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
for (;;) {
|
|
|
|
if (interactive && e.iop <= iostack) {
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
current_prompt = prompt->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
#else
|
|
|
|
prs(prompt->value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
onecommand();
|
2002-04-26 23:40:09 +00:00
|
|
|
/* Ensure that getenv("PATH") stays current */
|
|
|
|
setenv("PATH", path->value, 1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("MSH_MAIN: returning.\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void setdash()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
|
|
|
REGISTER int c;
|
2004-08-04 19:16:54 +00:00
|
|
|
char m['z' - 'a' + 1];
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
cp = m;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (c = 'a'; c <= 'z'; c++)
|
|
|
|
if (flag[(int) c])
|
2001-06-29 04:57:14 +00:00
|
|
|
*cp++ = c;
|
|
|
|
*cp = 0;
|
|
|
|
setval(lookup("-"), m);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int newfile(s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int f;
|
|
|
|
|
|
|
|
DBGPRINTF7(("NEWFILE: opening %s\n", s));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (strcmp(s, "-") != 0) {
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("NEWFILE: s is %s\n", s));
|
2001-06-29 04:57:14 +00:00
|
|
|
f = open(s, 0);
|
|
|
|
if (f < 0) {
|
|
|
|
prs(s);
|
|
|
|
err(": cannot open");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
f = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
next(remap(f));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
struct op *scantree(head)
|
|
|
|
struct op *head;
|
|
|
|
{
|
|
|
|
struct op *dotnode;
|
|
|
|
|
|
|
|
if (head == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (head->left != NULL) {
|
|
|
|
dotnode = scantree(head->left);
|
|
|
|
if (dotnode)
|
|
|
|
return (dotnode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head->right != NULL) {
|
|
|
|
dotnode = scantree(head->right);
|
|
|
|
if (dotnode)
|
|
|
|
return (dotnode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head->words == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
DBGPRINTF5(("SCANTREE: checking node 0x%x\n", head));
|
|
|
|
|
|
|
|
if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) {
|
|
|
|
DBGPRINTF5(("SCANTREE: dot found in node 0x%x\n", head));
|
|
|
|
return (head);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void onecommand()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
jmp_buf m1;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("ONECOMMAND: enter, outtree=0x%x\n", outtree));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
while (e.oenv)
|
|
|
|
quitenv();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
areanum = 1;
|
|
|
|
freehere(areanum);
|
|
|
|
freearea(areanum);
|
|
|
|
garbage();
|
|
|
|
wdlist = 0;
|
|
|
|
iolist = 0;
|
|
|
|
e.errpt = 0;
|
|
|
|
e.linep = line;
|
|
|
|
yynerrs = 0;
|
|
|
|
multiline = 0;
|
|
|
|
inparse = 1;
|
|
|
|
intr = 0;
|
|
|
|
execflg = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
setjmp(failpt = m1); /* Bruce Evans' fix */
|
2001-06-29 04:57:14 +00:00
|
|
|
if (setjmp(failpt = m1) || yyparse() || intr) {
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("ONECOMMAND: this is not good.\n"));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
while (e.oenv)
|
|
|
|
quitenv();
|
|
|
|
scraphere();
|
|
|
|
if (!interactive && intr)
|
|
|
|
leave();
|
|
|
|
inparse = 0;
|
|
|
|
intr = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
inparse = 0;
|
|
|
|
brklist = 0;
|
|
|
|
intr = 0;
|
|
|
|
execflg = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
if (!flag['n']) {
|
|
|
|
DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=0x%x\n",
|
|
|
|
outtree));
|
2001-06-29 04:57:14 +00:00
|
|
|
execute(outtree, NOPIPE, NOPIPE, 0);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (!interactive && intr) {
|
|
|
|
execflg = 0;
|
|
|
|
leave();
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((i = trapset) != 0) {
|
|
|
|
trapset = 0;
|
|
|
|
runtrap(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void fail()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
longjmp(failpt, 1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void leave()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("LEAVE: leave called!\n"));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (execflg)
|
|
|
|
fail();
|
|
|
|
scraphere();
|
|
|
|
freehere(1);
|
|
|
|
runtrap(0);
|
2004-08-04 19:19:10 +00:00
|
|
|
_exit(exstat);
|
2001-06-29 04:57:14 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void warn(s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
if (*s) {
|
2001-06-29 04:57:14 +00:00
|
|
|
prs(s);
|
|
|
|
exstat = -1;
|
|
|
|
}
|
|
|
|
prs("\n");
|
|
|
|
if (flag['e'])
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void err(s)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
warn(s);
|
|
|
|
if (flag['n'])
|
|
|
|
return;
|
|
|
|
if (!interactive)
|
|
|
|
leave();
|
|
|
|
if (e.errpt)
|
|
|
|
longjmp(e.errpt, 1);
|
|
|
|
closeall();
|
|
|
|
e.iop = e.iobase = iostack;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int newenv(f)
|
2001-06-29 04:57:14 +00:00
|
|
|
int f;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct env *ep;
|
|
|
|
|
|
|
|
DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (f) {
|
|
|
|
quitenv();
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
ep = (struct env *) space(sizeof(*ep));
|
|
|
|
if (ep == NULL) {
|
|
|
|
while (e.oenv)
|
|
|
|
quitenv();
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
*ep = e;
|
|
|
|
e.oenv = ep;
|
|
|
|
e.errpt = errpt;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void quitenv()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct env *ep;
|
|
|
|
REGISTER int fd;
|
|
|
|
|
|
|
|
DBGPRINTF(("QUITENV: e.oenv=0x%x\n", e.oenv));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((ep = e.oenv) != NULL) {
|
|
|
|
fd = e.iofd;
|
|
|
|
e = *ep;
|
|
|
|
/* should close `'d files */
|
|
|
|
DELETE(ep);
|
|
|
|
while (--fd >= e.iofd)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is any character from s1 in s2?
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int anys(s1, s2)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s1, *s2;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
while (*s1)
|
|
|
|
if (any(*s1++, s2))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is character c in s?
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int any(c, s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
if (*s++ == c)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *putn(n)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int n;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (itoa(n));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *itoa(n)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int n;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
static char s[20];
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2003-03-14 16:05:59 +00:00
|
|
|
snprintf(s, sizeof(s), "%u", n);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (s);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void next(int f)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
PUSHIO(afile, f, filechar);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void onintr(s)
|
|
|
|
int s; /* ANSI C requires a parameter */
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
signal(SIGINT, onintr);
|
|
|
|
intr = 1;
|
|
|
|
if (interactive) {
|
|
|
|
if (inparse) {
|
|
|
|
prs("\n");
|
|
|
|
fail();
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
} else if (heedint) {
|
2001-06-29 04:57:14 +00:00
|
|
|
execflg = 0;
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *space(n)
|
2001-06-29 04:57:14 +00:00
|
|
|
int n;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((cp = getcell(n)) == 0)
|
|
|
|
err("out of string space");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *strsave(s, a)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
int a;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp, *xp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((cp = space(strlen(s) + 1)) != NULL) {
|
|
|
|
setarea((char *) cp, a);
|
|
|
|
for (xp = cp; (*xp++ = *s++) != '\0';);
|
|
|
|
return (cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return ("");
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trap handling
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void sig(i)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
trapset = i;
|
|
|
|
signal(i, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runtrap(i)
|
|
|
|
int i;
|
|
|
|
{
|
|
|
|
char *trapstr;
|
|
|
|
|
|
|
|
if ((trapstr = trap[i]) == NULL)
|
|
|
|
return;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i == 0)
|
|
|
|
trap[i] = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
RUN(aword, trapstr, nlchar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- var.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the given name in the dictionary
|
|
|
|
* and return its value. If the name was
|
|
|
|
* not previously there, enter it now and
|
|
|
|
* return a null value.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct var *lookup(n)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *n;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct var *vp;
|
|
|
|
REGISTER char *cp;
|
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
static struct var dummy;
|
|
|
|
|
|
|
|
if (isdigit(*n)) {
|
|
|
|
dummy.name = n;
|
|
|
|
for (c = 0; isdigit(*n) && c < 1000; n++)
|
2004-08-04 19:16:54 +00:00
|
|
|
c = c * 10 + *n - '0';
|
2001-06-29 04:57:14 +00:00
|
|
|
dummy.status = RONLY;
|
2004-08-04 19:16:54 +00:00
|
|
|
dummy.value = c <= dolc ? dolv[c] : null;
|
|
|
|
return (&dummy);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
|
|
|
if (eqname(vp->name, n))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (vp);
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = findeq(n);
|
2004-08-04 19:16:54 +00:00
|
|
|
vp = (struct var *) space(sizeof(*vp));
|
|
|
|
if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
dummy.name = dummy.value = "";
|
2004-08-04 19:16:54 +00:00
|
|
|
return (&dummy);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*cp == 0)
|
|
|
|
*cp = '=';
|
|
|
|
*++cp = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
setarea((char *) vp, 0);
|
|
|
|
setarea((char *) vp->name, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
vp->value = null;
|
|
|
|
vp->next = vlist;
|
|
|
|
vp->status = GETCELL;
|
|
|
|
vlist = vp;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (vp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* give variable at `vp' the value `val'.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void setval(vp, val)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct var *vp;
|
|
|
|
char *val;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
nameval(vp, val, (char *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if name is not NULL, it must be
|
|
|
|
* a prefix of the space `val',
|
|
|
|
* and end with `='.
|
|
|
|
* this is all so that exporting
|
|
|
|
* values is reasonably painless.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void nameval(vp, val, name)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct var *vp;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *val, *name;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp, *xp;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *nv;
|
|
|
|
int fl;
|
|
|
|
|
|
|
|
if (vp->status & RONLY) {
|
|
|
|
for (xp = vp->name; *xp && *xp != '=';)
|
|
|
|
putc(*xp++, stderr);
|
|
|
|
err(" is read-only");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fl = 0;
|
|
|
|
if (name == NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
xp = space(strlen(vp->name) + strlen(val) + 2);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (xp == 0)
|
|
|
|
return;
|
|
|
|
/* make string: name=value */
|
2004-08-04 19:16:54 +00:00
|
|
|
setarea((char *) xp, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
name = xp;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*xp++ == 0)
|
|
|
|
xp[-1] = '=';
|
|
|
|
nv = xp;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cp = val; (*xp++ = *cp++) != '\0';);
|
2001-06-29 04:57:14 +00:00
|
|
|
val = nv;
|
|
|
|
fl = GETCELL;
|
|
|
|
}
|
|
|
|
if (vp->status & GETCELL)
|
2004-08-04 19:16:54 +00:00
|
|
|
freecell(vp->name); /* form new string `name=value' */
|
2001-06-29 04:57:14 +00:00
|
|
|
vp->name = name;
|
|
|
|
vp->value = val;
|
|
|
|
vp->status |= fl;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void export(vp)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct var *vp;
|
|
|
|
{
|
|
|
|
vp->status |= EXPORT;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void ronly(vp)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct var *vp;
|
|
|
|
{
|
2001-08-06 14:14:18 +00:00
|
|
|
if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
|
2001-06-29 04:57:14 +00:00
|
|
|
vp->status |= RONLY;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int isassign(s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (!isalpha((int) *s) && *s != '_')
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
for (; *s != '='; s++)
|
2001-08-06 14:14:18 +00:00
|
|
|
if (*s == 0 || (!isalnum(*s) && *s != '_'))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int assign(s, cf)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
int cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct var *vp;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf));
|
|
|
|
|
2001-08-06 14:14:18 +00:00
|
|
|
if (!isalpha(*s) && *s != '_')
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
for (cp = s; *cp != '='; cp++)
|
2001-08-06 14:14:18 +00:00
|
|
|
if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
vp = lookup(s);
|
2004-08-04 19:16:54 +00:00
|
|
|
nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (cf != COPYV)
|
|
|
|
vp->status &= ~GETCELL;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int checkname(cp)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (!isalpha(*cp++) && *(cp - 1) != '_')
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
while (*cp)
|
2004-08-04 19:16:54 +00:00
|
|
|
if (!isalnum(*cp++) && *(cp - 1) != '_')
|
|
|
|
return (0);
|
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void putvlist(f, out)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int f, out;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct var *vp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
2001-08-06 14:14:18 +00:00
|
|
|
if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (vp->status & EXPORT)
|
|
|
|
write(out, "export ", 7);
|
|
|
|
if (vp->status & RONLY)
|
|
|
|
write(out, "readonly ", 9);
|
2004-08-04 19:16:54 +00:00
|
|
|
write(out, vp->name, (int) (findeq(vp->name) - vp->name));
|
2001-06-29 04:57:14 +00:00
|
|
|
write(out, "\n", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int eqname(n1, n2)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *n1, *n2;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
for (; *n1 != '=' && *n1 != 0; n1++)
|
|
|
|
if (*n2++ != *n1)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
|
|
|
return (*n2 == 0 || *n2 == '=');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *findeq(cp)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
while (*cp != '\0' && *cp != '=')
|
|
|
|
cp++;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- gmatch.c -------- */
|
|
|
|
/*
|
|
|
|
* int gmatch(string, pattern)
|
|
|
|
* char *string, *pattern;
|
|
|
|
*
|
|
|
|
* Match a pattern as in sh(1).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CMASK 0377
|
|
|
|
#define QUOTE 0200
|
|
|
|
#define QMASK (CMASK&~QUOTE)
|
2004-08-04 19:16:54 +00:00
|
|
|
#define NOT '!' /* might use ^ */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int gmatch(s, p)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s, *p;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int sc, pc;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (s == NULL || p == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
while ((pc = *p++ & CMASK) != '\0') {
|
|
|
|
sc = *s++ & QMASK;
|
|
|
|
switch (pc) {
|
|
|
|
case '[':
|
|
|
|
if ((p = cclass(p, sc)) == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
if (sc == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
s--;
|
|
|
|
do {
|
|
|
|
if (*p == '\0' || gmatch(s, p))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
} while (*s++ != '\0');
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
default:
|
2004-08-04 19:16:54 +00:00
|
|
|
if (sc != (pc & ~QUOTE))
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (*s == 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *cclass(p, sub)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *p;
|
|
|
|
REGISTER int sub;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c, d, not, found;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((not = *p == NOT) != 0)
|
|
|
|
p++;
|
|
|
|
found = not;
|
|
|
|
do {
|
|
|
|
if (*p == '\0')
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
c = *p & CMASK;
|
|
|
|
if (p[1] == '-' && p[2] != ']') {
|
|
|
|
d = p[2] & CMASK;
|
|
|
|
p++;
|
|
|
|
} else
|
|
|
|
d = c;
|
|
|
|
if (c == sub || (c <= sub && sub <= d))
|
|
|
|
found = !not;
|
|
|
|
} while (*++p != ']');
|
2004-08-04 19:16:54 +00:00
|
|
|
return (found ? p + 1 : (char *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- area.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All memory between (char *)areabot and (char *)(areatop+1) is
|
|
|
|
* exclusively administered by the area management routines.
|
|
|
|
* It is assumed that sbrk() and brk() manipulate the high end.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void initarea()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
brkaddr = malloc(AREASIZE);
|
|
|
|
brktop = brkaddr + AREASIZE;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2006-01-30 22:48:39 +00:00
|
|
|
while ((long) sbrk(0) & ALIGN)
|
2001-06-29 04:57:14 +00:00
|
|
|
sbrk(1);
|
2004-08-04 19:16:54 +00:00
|
|
|
areabot = (struct region *) sbrk(REGSIZE);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
areabot->next = areabot;
|
|
|
|
areabot->area = BUSY;
|
|
|
|
areatop = areabot;
|
|
|
|
areanxt = areabot;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
char *getcell(nbytes)
|
2001-06-29 04:57:14 +00:00
|
|
|
unsigned nbytes;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int nregio;
|
|
|
|
REGISTER struct region *p, *q;
|
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (nbytes == 0) {
|
|
|
|
puts("getcell(0)");
|
|
|
|
abort();
|
2004-08-04 19:16:54 +00:00
|
|
|
}
|
|
|
|
/* silly and defeats the algorithm */
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* round upwards and add administration area
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
|
2001-06-29 04:57:14 +00:00
|
|
|
for (p = areanxt;;) {
|
|
|
|
if (p->area > areanum) {
|
|
|
|
/*
|
|
|
|
* merge free cells
|
|
|
|
*/
|
|
|
|
while ((q = p->next)->area > areanum && q != areanxt)
|
|
|
|
p->next = q->next;
|
|
|
|
/*
|
|
|
|
* exit loop if cell big enough
|
|
|
|
*/
|
|
|
|
if (q >= p + nregio)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
if (p == areanxt)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = nregio >= GROWBY ? nregio : GROWBY;
|
2004-08-04 19:16:54 +00:00
|
|
|
p = (struct region *) sbrk(i * REGSIZE);
|
|
|
|
if (p == (struct region *) -1)
|
|
|
|
return ((char *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
p--;
|
|
|
|
if (p != areatop) {
|
|
|
|
puts("not contig");
|
2004-08-04 19:16:54 +00:00
|
|
|
abort(); /* allocated areas are contiguous */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
q = p + i;
|
|
|
|
p->next = q;
|
|
|
|
p->area = FREE;
|
|
|
|
q->next = areabot;
|
|
|
|
q->area = BUSY;
|
|
|
|
areatop = q;
|
2004-08-04 19:16:54 +00:00
|
|
|
found:
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* we found a FREE area big enough, pointed to by 'p', and up to 'q'
|
|
|
|
*/
|
|
|
|
areanxt = p + nregio;
|
|
|
|
if (areanxt < q) {
|
|
|
|
/*
|
|
|
|
* split into requested area and rest
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
if (areanxt + 1 > q) {
|
2001-06-29 04:57:14 +00:00
|
|
|
puts("OOM");
|
2004-08-04 19:16:54 +00:00
|
|
|
abort(); /* insufficient space left for admin */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
areanxt->next = q;
|
|
|
|
areanxt->area = FREE;
|
|
|
|
p->next = areanxt;
|
|
|
|
}
|
|
|
|
p->area = areanum;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char *) (p + 1));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void freecell(cp)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct region *p;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((p = (struct region *) cp) != NULL) {
|
2001-06-29 04:57:14 +00:00
|
|
|
p--;
|
|
|
|
if (p < areanxt)
|
|
|
|
areanxt = p;
|
|
|
|
p->area = FREE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void freearea(a)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int a;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct region *p, *top;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
top = areatop;
|
|
|
|
for (p = areabot; p != top; p = p->next)
|
|
|
|
if (p->area >= a)
|
|
|
|
p->area = FREE;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void setarea(cp, a)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
|
|
|
int a;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct region *p;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((p = (struct region *) cp) != NULL)
|
|
|
|
(p - 1)->area = a;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
int getarea(cp)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct region *) cp - 1)->area;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void garbage()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct region *p, *q, *top;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
top = areatop;
|
|
|
|
for (p = areabot; p != top; p = p->next) {
|
|
|
|
if (p->area > areanum) {
|
|
|
|
while ((q = p->next)->area > areanum)
|
|
|
|
p->next = q->next;
|
|
|
|
areanxt = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef SHRINKBY
|
|
|
|
if (areatop >= q + SHRINKBY && q->area > areanum) {
|
2004-08-04 19:16:54 +00:00
|
|
|
brk((char *) (q + 1));
|
2001-06-29 04:57:14 +00:00
|
|
|
q->next = areabot;
|
|
|
|
q->area = BUSY;
|
|
|
|
areatop = q;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- csyn.c -------- */
|
|
|
|
/*
|
|
|
|
* shell: syntax (C version)
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
int yyparse()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("YYPARSE: enter...\n"));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
startl = 1;
|
2001-06-29 04:57:14 +00:00
|
|
|
peeksym = 0;
|
|
|
|
yynerrs = 0;
|
|
|
|
outtree = c_list();
|
|
|
|
musthave('\n', 0);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (yynerrs != 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *pipeline(cf)
|
2001-06-29 04:57:14 +00:00
|
|
|
int cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t, *p;
|
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = command(cf);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF9(("PIPELINE: t=0x%x\n", t));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t != NULL) {
|
|
|
|
while ((c = yylex(0)) == '|') {
|
2004-08-04 19:19:10 +00:00
|
|
|
if ((p = command(CONTIN)) == NULL) {
|
|
|
|
DBGPRINTF8(("PIPELINE: error!\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
SYNTAXERR;
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t->type != TPAREN && t->type != TCOM) {
|
|
|
|
/* shell statement */
|
|
|
|
t = block(TPAREN, t, NOBLOCK, NOWORDS);
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
t = block(TPIPE, t, p, NOWORDS);
|
|
|
|
}
|
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("PIPELINE: returning t=0x%x\n", t));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *andor()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t, *p;
|
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF7(("ANDOR: enter...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = pipeline(0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF9(("ANDOR: t=0x%x\n", t));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t != NULL) {
|
|
|
|
while ((c = yylex(0)) == LOGAND || c == LOGOR) {
|
2004-08-04 19:19:10 +00:00
|
|
|
if ((p = pipeline(CONTIN)) == NULL) {
|
|
|
|
DBGPRINTF8(("ANDOR: error!\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
SYNTAXERR;
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
|
2004-08-04 19:19:10 +00:00
|
|
|
} /* WHILE */
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("ANDOR: returning t=0x%x\n", t));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *c_list()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t, *p;
|
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF7(("C_LIST: enter...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = andor();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t != NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((peeksym = yylex(0)) == '&')
|
2001-06-29 04:57:14 +00:00
|
|
|
t = block(TASYNC, t, NOBLOCK, NOWORDS);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((c = yylex(0)) == ';' || c == '&'
|
|
|
|
|| (multiline && c == '\n')) {
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((p = andor()) == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((peeksym = yylex(0)) == '&')
|
2001-06-29 04:57:14 +00:00
|
|
|
p = block(TASYNC, p, NOBLOCK, NOWORDS);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
t = list(t, p);
|
2004-08-04 19:19:10 +00:00
|
|
|
} /* WHILE */
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
/* IF */
|
|
|
|
DBGPRINTF7(("C_LIST: returning t=0x%x\n", t));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int synio(cf)
|
2001-06-29 04:57:14 +00:00
|
|
|
int cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioword *iop;
|
|
|
|
REGISTER int i;
|
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((c = yylex(cf)) != '<' && c != '>') {
|
|
|
|
peeksym = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
i = yylval.i;
|
|
|
|
musthave(WORD, 0);
|
|
|
|
iop = io(iounit, i, yylval.cp);
|
|
|
|
iounit = IODEFAULT;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i & IOHERE)
|
|
|
|
markhere(yylval.cp, iop);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("SYNIO: returning 1\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void musthave(c, cf)
|
2001-06-29 04:57:14 +00:00
|
|
|
int c, cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
if ((peeksym = yylex(cf)) != c) {
|
|
|
|
DBGPRINTF7(("MUSTHAVE: error!\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
SYNTAXERR;
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
peeksym = 0;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *simple()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = NULL;
|
|
|
|
for (;;) {
|
|
|
|
switch (peeksym = yylex(0)) {
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
(void) synio(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WORD:
|
|
|
|
if (t == NULL) {
|
|
|
|
t = newtp();
|
|
|
|
t->type = TCOM;
|
|
|
|
}
|
|
|
|
peeksym = 0;
|
|
|
|
word(yylval.cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *nested(type, mark)
|
2001-06-29 04:57:14 +00:00
|
|
|
int type, mark;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
|
|
|
|
|
|
|
DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
multiline++;
|
|
|
|
t = c_list();
|
|
|
|
musthave(mark, 0);
|
|
|
|
multiline--;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (block(type, t, NOBLOCK, NOWORDS));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *command(cf)
|
2001-06-29 04:57:14 +00:00
|
|
|
int cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct wdblock *iosave;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF(("COMMAND: enter, cf=%d\n", cf));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
iosave = iolist;
|
|
|
|
iolist = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (multiline)
|
|
|
|
cf |= CONTIN;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
while (synio(cf))
|
|
|
|
cf = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
c = yylex(cf);
|
|
|
|
|
|
|
|
switch (c) {
|
2001-06-29 04:57:14 +00:00
|
|
|
default:
|
|
|
|
peeksym = c;
|
|
|
|
if ((t = simple()) == NULL) {
|
|
|
|
if (iolist == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
t = newtp();
|
|
|
|
t->type = TCOM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
t = nested(TPAREN, ')');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
t = nested(TBRACE, '}');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FOR:
|
|
|
|
t = newtp();
|
|
|
|
t->type = TFOR;
|
|
|
|
musthave(WORD, 0);
|
|
|
|
startl = 1;
|
|
|
|
t->str = yylval.cp;
|
|
|
|
multiline++;
|
|
|
|
t->words = wordlist();
|
|
|
|
if ((c = yylex(0)) != '\n' && c != ';')
|
|
|
|
peeksym = c;
|
|
|
|
t->left = dogroup(0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WHILE:
|
|
|
|
case UNTIL:
|
|
|
|
multiline++;
|
|
|
|
t = newtp();
|
2004-08-04 19:16:54 +00:00
|
|
|
t->type = c == WHILE ? TWHILE : TUNTIL;
|
2001-06-29 04:57:14 +00:00
|
|
|
t->left = c_list();
|
|
|
|
t->right = dogroup(1);
|
|
|
|
t->words = NULL;
|
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CASE:
|
|
|
|
t = newtp();
|
|
|
|
t->type = TCASE;
|
|
|
|
musthave(WORD, 0);
|
|
|
|
t->str = yylval.cp;
|
|
|
|
startl++;
|
|
|
|
multiline++;
|
|
|
|
musthave(IN, CONTIN);
|
|
|
|
startl++;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
t->left = caselist();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
musthave(ESAC, 0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IF:
|
|
|
|
multiline++;
|
|
|
|
t = newtp();
|
|
|
|
t->type = TIF;
|
|
|
|
t->left = c_list();
|
|
|
|
t->right = thenpart();
|
|
|
|
musthave(FI, 0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
case DOT:
|
|
|
|
t = newtp();
|
|
|
|
t->type = TDOT;
|
|
|
|
|
|
|
|
musthave(WORD, 0); /* gets name of file */
|
|
|
|
DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
|
|
|
|
|
|
|
|
word(yylval.cp); /* add word to wdlist */
|
|
|
|
word(NOWORD); /* terminate wdlist */
|
|
|
|
t->words = copyw(); /* dup wdlist */
|
|
|
|
break;
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
while (synio(0));
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
t = namelist(t);
|
|
|
|
iolist = iosave;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("COMMAND: returning 0x%x\n", t));
|
|
|
|
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *dowholefile(type, mark)
|
|
|
|
int type;
|
|
|
|
int mark;
|
|
|
|
{
|
|
|
|
REGISTER struct op *t;
|
|
|
|
|
|
|
|
DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark));
|
|
|
|
|
|
|
|
multiline++;
|
|
|
|
t = c_list();
|
|
|
|
multiline--;
|
|
|
|
t = block(type, t, NOBLOCK, NOWORDS);
|
|
|
|
DBGPRINTF(("DOWHOLEFILE: return t=0x%x\n", t));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *dogroup(onlydone)
|
2001-06-29 04:57:14 +00:00
|
|
|
int onlydone;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
REGISTER struct op *mylist;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
c = yylex(CONTIN);
|
|
|
|
if (c == DONE && onlydone)
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (c != DO)
|
|
|
|
SYNTAXERR;
|
|
|
|
mylist = c_list();
|
|
|
|
musthave(DONE, 0);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (mylist);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *thenpart()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((c = yylex(0)) != THEN) {
|
|
|
|
peeksym = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
t = newtp();
|
|
|
|
t->type = 0;
|
|
|
|
t->left = c_list();
|
|
|
|
if (t->left == NULL)
|
|
|
|
SYNTAXERR;
|
|
|
|
t->right = elsepart();
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *elsepart()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
switch (c = yylex(0)) {
|
|
|
|
case ELSE:
|
|
|
|
if ((t = c_list()) == NULL)
|
|
|
|
SYNTAXERR;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case ELIF:
|
|
|
|
t = newtp();
|
|
|
|
t->type = TELIF;
|
|
|
|
t->left = c_list();
|
|
|
|
t->right = thenpart();
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
peeksym = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *caselist()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
while ((peeksym = yylex(CONTIN)) != ESAC) {
|
|
|
|
DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym));
|
2001-06-29 04:57:14 +00:00
|
|
|
t = list(t, casepart());
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("CASELIST, returning t=0x%x\n", t));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *casepart()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
|
|
|
|
|
|
|
DBGPRINTF7(("CASEPART: enter...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = newtp();
|
|
|
|
t->type = TPAT;
|
|
|
|
t->words = pattern();
|
|
|
|
musthave(')', 0);
|
|
|
|
t->left = c_list();
|
|
|
|
if ((peeksym = yylex(CONTIN)) != ESAC)
|
|
|
|
musthave(BREAK, CONTIN);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("CASEPART: made newtp(TPAT, t=0x%x)\n", t));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **pattern()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c, cf;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
cf = CONTIN;
|
|
|
|
do {
|
|
|
|
musthave(WORD, cf);
|
|
|
|
word(yylval.cp);
|
|
|
|
cf = 0;
|
|
|
|
} while ((c = yylex(0)) == '|');
|
|
|
|
peeksym = c;
|
|
|
|
word(NOWORD);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (copyw());
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **wordlist()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((c = yylex(0)) != IN) {
|
|
|
|
peeksym = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char **) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
startl = 0;
|
|
|
|
while ((c = yylex(0)) == WORD)
|
|
|
|
word(yylval.cp);
|
|
|
|
word(NOWORD);
|
|
|
|
peeksym = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (copyw());
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* supporting functions
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *list(t1, t2)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t1, *t2;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("LIST: enter, t1=0x%x, t2=0x%x\n", t1, t2));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t1 == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t2);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t2 == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t1);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (block(TLIST, t1, t2, NOWORDS));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *block(type, t1, t2, wp)
|
2001-06-29 04:57:14 +00:00
|
|
|
int type;
|
|
|
|
struct op *t1, *t2;
|
|
|
|
char **wp;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
|
|
|
|
|
|
|
DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
t = newtp();
|
|
|
|
t->type = type;
|
|
|
|
t->left = t1;
|
|
|
|
t->right = t2;
|
|
|
|
t->words = wp;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("BLOCK: inserted 0x%x between 0x%x and 0x%x\n", t, t1,
|
|
|
|
t2));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* See if given string is a shell multiline (FOR, IF, etc) */
|
2004-08-04 19:16:54 +00:00
|
|
|
static int rlookup(n)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *n;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2005-10-17 09:48:57 +00:00
|
|
|
REGISTER const struct res *rp;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
for (rp = restab; rp->r_name; rp++)
|
2004-08-04 19:19:10 +00:00
|
|
|
if (strcmp(rp->r_name, n) == 0) {
|
|
|
|
DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val));
|
|
|
|
return (rp->r_val); /* Return numeric code for shell multiline */
|
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF7(("RLOOKUP: NO match, returning 0\n"));
|
|
|
|
return (0); /* Not a shell multiline */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *newtp()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
t = (struct op *) tree(sizeof(*t));
|
2001-06-29 04:57:14 +00:00
|
|
|
t->type = 0;
|
|
|
|
t->words = NULL;
|
|
|
|
t->ioact = NULL;
|
|
|
|
t->left = NULL;
|
|
|
|
t->right = NULL;
|
|
|
|
t->str = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF3(("NEWTP: allocated 0x%x\n", t));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *namelist(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("NAMELIST: enter, t=0x%x, type %s, iolist=0x%x\n", t,
|
|
|
|
T_CMD_NAMES[t->type], iolist));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (iolist) {
|
2004-08-04 19:16:54 +00:00
|
|
|
iolist = addword((char *) NULL, iolist);
|
2001-06-29 04:57:14 +00:00
|
|
|
t->ioact = copyio();
|
|
|
|
} else
|
|
|
|
t->ioact = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t->type != TCOM) {
|
|
|
|
if (t->type != TPAREN && t->ioact != NULL) {
|
|
|
|
t = block(TPAREN, t, NOBLOCK, NOWORDS);
|
|
|
|
t->ioact = t->left->ioact;
|
|
|
|
t->left->ioact = NULL;
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
word(NOWORD);
|
|
|
|
t->words = copyw();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **copyw()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char **wd;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
wd = getwords(wdlist);
|
|
|
|
wdlist = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wd);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void word(cp)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
|
|
|
{
|
|
|
|
wdlist = addword(cp, wdlist);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct ioword **copyio()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioword **iop;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
iop = (struct ioword **) getwords(iolist);
|
|
|
|
iolist = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (iop);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct ioword *io(u, f, cp)
|
2001-06-29 04:57:14 +00:00
|
|
|
int u;
|
|
|
|
int f;
|
|
|
|
char *cp;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioword *iop;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
iop = (struct ioword *) tree(sizeof(*iop));
|
|
|
|
iop->io_unit = u;
|
|
|
|
iop->io_flag = f;
|
|
|
|
iop->io_name = cp;
|
2004-08-04 19:16:54 +00:00
|
|
|
iolist = addword((char *) iop, iolist);
|
|
|
|
return (iop);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void zzerr()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
yyerror("syntax error");
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void yyerror(s)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
yynerrs++;
|
|
|
|
if (interactive && e.iop <= iostack) {
|
|
|
|
multiline = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
while (eofc() == 0 && yylex(0) != '\n');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
err(s);
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int yylex(cf)
|
2001-06-29 04:57:14 +00:00
|
|
|
int cf;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c, c1;
|
2001-06-29 04:57:14 +00:00
|
|
|
int atstart;
|
|
|
|
|
|
|
|
if ((c = peeksym) > 0) {
|
|
|
|
peeksym = 0;
|
|
|
|
if (c == '\n')
|
|
|
|
startl = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
nlseen = 0;
|
|
|
|
atstart = startl;
|
|
|
|
startl = 0;
|
|
|
|
yylval.i = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
e.linep = line;
|
|
|
|
|
|
|
|
/* MALAMO */
|
|
|
|
line[LINELIM - 1] = '\0';
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
loop:
|
2004-08-04 19:19:10 +00:00
|
|
|
while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */
|
|
|
|
;
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
if (any(c, "0123456789")) {
|
|
|
|
unget(c1 = my_getc(0));
|
|
|
|
if (c1 == '<' || c1 == '>') {
|
|
|
|
iounit = c - '0';
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
*e.linep++ = c;
|
|
|
|
c = c1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
case '#': /* Comment, skip to next newline or End-of-string */
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((c = my_getc(0)) != 0 && c != '\n');
|
2001-06-29 04:57:14 +00:00
|
|
|
unget(c);
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case 0:
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF5(("YYLEX: return 0, c=%d\n", c));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case '$':
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF9(("YYLEX: found $\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
*e.linep++ = c;
|
|
|
|
if ((c = my_getc(0)) == '{') {
|
|
|
|
if ((c = collect(c, '}')) != '\0')
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
goto pack;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
if ((c = collect(c, c)) != '\0')
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
goto pack;
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
case '&':
|
|
|
|
case ';':
|
|
|
|
startl = 1;
|
2004-08-04 19:19:10 +00:00
|
|
|
/* If more chars process them, else return NULL char */
|
|
|
|
if ((c1 = dual(c)) != '\0')
|
|
|
|
return (c1);
|
|
|
|
else
|
|
|
|
return (c);
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
case '^':
|
|
|
|
startl = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ('|');
|
2001-06-29 04:57:14 +00:00
|
|
|
case '>':
|
|
|
|
case '<':
|
|
|
|
diag(c);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case '\n':
|
|
|
|
nlseen++;
|
|
|
|
gethere();
|
|
|
|
startl = 1;
|
|
|
|
if (multiline || cf & CONTIN) {
|
|
|
|
if (interactive && e.iop <= iostack) {
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
#else
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(cprompt->value);
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (cf & CONTIN)
|
|
|
|
goto loop;
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
startl = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unget(c);
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
pack:
|
2004-08-04 19:19:10 +00:00
|
|
|
while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (e.linep >= elinep)
|
|
|
|
err("word too long");
|
|
|
|
else
|
|
|
|
*e.linep++ = c;
|
2004-08-04 19:19:10 +00:00
|
|
|
};
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
unget(c);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (any(c, "\"'`$"))
|
2001-06-29 04:57:14 +00:00
|
|
|
goto loop;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
*e.linep++ = '\0';
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (atstart && (c = rlookup(line)) != 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
startl = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
yylval.cp = strsave(line, areanum);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (WORD);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int collect(c, c1)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c, c1;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
char s[2];
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
*e.linep++ = c;
|
|
|
|
while ((c = my_getc(c1)) != c1) {
|
|
|
|
if (c == 0) {
|
|
|
|
unget(c);
|
|
|
|
s[0] = c1;
|
|
|
|
s[1] = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
prs("no closing ");
|
|
|
|
yyerror(s);
|
|
|
|
return (YYERRCODE);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (interactive && c == '\n' && e.iop <= iostack) {
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
#else
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(cprompt->value);
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*e.linep++ = c;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
*e.linep++ = c;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF8(("COLLECT: return 0, line is %s\n", line));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* "multiline commands" helper func */
|
|
|
|
/* see if next 2 chars form a shell multiline */
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dual(c)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
char s[3];
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp = s;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF8(("DUAL: enter, c=%d\n", c));
|
|
|
|
|
|
|
|
*cp++ = c; /* c is the given "peek" char */
|
|
|
|
*cp++ = my_getc(0); /* get next char of input */
|
|
|
|
*cp = 0; /* add EOS marker */
|
|
|
|
|
|
|
|
c = rlookup(s); /* see if 2 chars form a shell multiline */
|
|
|
|
if (c == 0)
|
|
|
|
unget(*--cp); /* String is not a shell multiline, put peek char back */
|
|
|
|
|
|
|
|
return (c); /* String is multiline, return numeric multiline (restab) code */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void diag(ec)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int ec;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF8(("DIAG: enter, ec=%d\n", ec));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
c = my_getc(0);
|
|
|
|
if (c == '>' || c == '<') {
|
|
|
|
if (c != ec)
|
|
|
|
zzerr();
|
2004-08-04 19:16:54 +00:00
|
|
|
yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE;
|
2001-06-29 04:57:14 +00:00
|
|
|
c = my_getc(0);
|
|
|
|
} else
|
2004-08-04 19:16:54 +00:00
|
|
|
yylval.i = ec == '>' ? IOWRITE : IOREAD;
|
2001-06-29 04:57:14 +00:00
|
|
|
if (c != '&' || yylval.i == IOHERE)
|
|
|
|
unget(c);
|
|
|
|
else
|
|
|
|
yylval.i |= IODUP;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *tree(size)
|
2001-06-29 04:57:14 +00:00
|
|
|
unsigned size;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((t = getcell(size)) == NULL) {
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF2(("TREE: getcell(%d) failed!\n", size));
|
2001-06-29 04:57:14 +00:00
|
|
|
prs("command line too complicated\n");
|
|
|
|
fail();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (t);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS1 */
|
|
|
|
/* ARGSUSED */
|
|
|
|
|
|
|
|
/* -------- exec.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* execute tree
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int execute(t, pin, pout, act)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
int *pin, *pout;
|
|
|
|
int act;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t1;
|
2001-06-29 04:57:14 +00:00
|
|
|
volatile int i, rv, a;
|
|
|
|
char *cp, **wp, **wp2;
|
|
|
|
struct var *vp;
|
2004-08-04 19:19:10 +00:00
|
|
|
struct op *outtree_save;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct brkcon bc;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) ℘
|
2004-03-15 08:29:22 +00:00
|
|
|
#endif
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
if (t == NULL) {
|
|
|
|
DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("EXECUTE: t=0x%x, t->type=%d (%s), t->words is %s\n", t,
|
|
|
|
t->type, T_CMD_NAMES[t->type],
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[0])));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
rv = 0;
|
|
|
|
a = areanum++;
|
|
|
|
wp = (wp2 = t->words) != NULL
|
2004-08-04 19:16:54 +00:00
|
|
|
? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
|
|
|
|
: NULL;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* Hard to know how many words there are, be careful of garbage pointer values */
|
|
|
|
/* They are likely to cause "PCI bus fault" errors */
|
|
|
|
#if 0
|
|
|
|
DBGPRINTF(("EXECUTE: t->left=0x%x, t->right=0x%x, t->words[1] is %s\n",
|
|
|
|
t->left, t->right,
|
|
|
|
((t->words[1] == NULL) ? "NULL" : t->words[1])));
|
|
|
|
DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n",
|
|
|
|
((t->words[2] == NULL) ? "NULL" : t->words[2]),
|
|
|
|
((t->words[3] == NULL) ? "NULL" : t->words[3])));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
switch (t->type) {
|
2004-08-04 19:19:10 +00:00
|
|
|
case TDOT:
|
|
|
|
DBGPRINTF3(("EXECUTE: TDOT\n"));
|
|
|
|
|
|
|
|
outtree_save = outtree;
|
|
|
|
|
|
|
|
newfile(evalstr(t->words[0], DOALL));
|
|
|
|
|
|
|
|
t->left = dowholefile(TLIST, 0);
|
|
|
|
t->right = NULL;
|
|
|
|
|
|
|
|
outtree = outtree_save;
|
|
|
|
|
|
|
|
if (t->left)
|
|
|
|
rv = execute(t->left, pin, pout, 0);
|
|
|
|
if (t->right)
|
|
|
|
rv = execute(t->right, pin, pout, 0);
|
|
|
|
break;
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
case TPAREN:
|
2003-03-14 16:05:59 +00:00
|
|
|
rv = execute(t->left, pin, pout, 0);
|
|
|
|
break;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
case TCOM:
|
2001-07-07 00:05:55 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
rv = forkexec(t, pin, pout, act, wp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TPIPE:
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
int pv[2];
|
|
|
|
|
|
|
|
if ((rv = openpipe(pv)) < 0)
|
|
|
|
break;
|
|
|
|
pv[0] = remap(pv[0]);
|
|
|
|
pv[1] = remap(pv[1]);
|
|
|
|
(void) execute(t->left, pin, pv, 0);
|
|
|
|
rv = execute(t->right, pv, pout, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TLIST:
|
|
|
|
(void) execute(t->left, pin, pout, 0);
|
|
|
|
rv = execute(t->right, pin, pout, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TASYNC:
|
2004-08-04 19:16:54 +00:00
|
|
|
{
|
|
|
|
int hinteractive = interactive;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
i = vfork();
|
|
|
|
if (i != 0) {
|
|
|
|
interactive = hinteractive;
|
|
|
|
if (i != -1) {
|
|
|
|
setval(lookup("!"), putn(i));
|
|
|
|
if (pin != NULL)
|
|
|
|
closepipe(pin);
|
|
|
|
if (interactive) {
|
|
|
|
prs(putn(i));
|
|
|
|
prs("\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
rv = -1;
|
|
|
|
setstatus(rv);
|
|
|
|
} else {
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
if (interactive)
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
interactive = 0;
|
|
|
|
if (pin == NULL) {
|
|
|
|
close(0);
|
2005-10-12 15:34:25 +00:00
|
|
|
open(bb_dev_null, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
_exit(execute(t->left, pin, pout, FEXEC));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOR:
|
|
|
|
case TAND:
|
|
|
|
rv = execute(t->left, pin, pout, 0);
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND))
|
2001-06-29 04:57:14 +00:00
|
|
|
rv = execute(t1, pin, pout, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFOR:
|
|
|
|
if (wp == NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
wp = dolv + 1;
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((i = dolc) < 0)
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
i = -1;
|
2004-08-04 19:16:54 +00:00
|
|
|
while (*wp++ != NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
vp = lookup(t->str);
|
|
|
|
while (setjmp(bc.brkpt))
|
|
|
|
if (isbreak)
|
|
|
|
goto broken;
|
|
|
|
brkset(&bc);
|
|
|
|
for (t1 = t->left; i-- && *wp != NULL;) {
|
|
|
|
setval(vp, *wp++);
|
|
|
|
rv = execute(t1, pin, pout, 0);
|
|
|
|
}
|
|
|
|
brklist = brklist->nextlev;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TWHILE:
|
|
|
|
case TUNTIL:
|
|
|
|
while (setjmp(bc.brkpt))
|
|
|
|
if (isbreak)
|
|
|
|
goto broken;
|
|
|
|
brkset(&bc);
|
|
|
|
t1 = t->left;
|
|
|
|
while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
|
|
|
|
rv = execute(t->right, pin, pout, 0);
|
|
|
|
brklist = brklist->nextlev;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIF:
|
|
|
|
case TELIF:
|
2004-08-04 19:16:54 +00:00
|
|
|
if (t->right != NULL) {
|
|
|
|
rv = !execute(t->left, pin, pout, 0) ?
|
|
|
|
execute(t->right->left, pin, pout, 0) :
|
|
|
|
execute(t->right->right, pin, pout, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCASE:
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0)
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = "";
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n",
|
|
|
|
((t->str == NULL) ? "NULL" : t->str),
|
|
|
|
((cp == NULL) ? "NULL" : cp)));
|
|
|
|
|
|
|
|
if ((t1 = findcase(t->left, cp)) != NULL) {
|
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=0x%x, t1=0x%x)...\n", t, t1));
|
2001-06-29 04:57:14 +00:00
|
|
|
rv = execute(t1, pin, pout, 0);
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=0x%x, t1=0x%x)...\n", t, t1));
|
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TBRACE:
|
|
|
|
/*
|
|
|
|
if (iopp = t->ioact)
|
|
|
|
while (*iopp)
|
|
|
|
if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
|
|
|
|
rv = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if (rv >= 0 && (t1 = t->left))
|
|
|
|
rv = execute(t1, pin, pout, 0);
|
|
|
|
break;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
};
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
broken:
|
2001-06-29 04:57:14 +00:00
|
|
|
t->words = wp2;
|
|
|
|
isbreak = 0;
|
|
|
|
freehere(areanum);
|
|
|
|
freearea(areanum);
|
|
|
|
areanum = a;
|
|
|
|
if (interactive && intr) {
|
|
|
|
closeall();
|
|
|
|
fail();
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((i = trapset) != 0) {
|
|
|
|
trapset = 0;
|
|
|
|
runtrap(i);
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("EXECUTE: returning from t=0x%x, rv=%d\n", t, rv));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (rv);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-04 19:19:10 +00:00
|
|
|
forkexec(REGISTER struct op *t, int *pin, int *pout, int act, char **wp)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
pid_t newpid;
|
2001-06-29 04:57:14 +00:00
|
|
|
int i, rv;
|
2004-08-04 19:16:54 +00:00
|
|
|
int (*shcom) (struct op *) = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int f;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp = NULL;
|
|
|
|
struct ioword **iopp;
|
|
|
|
int resetsig;
|
|
|
|
char **owp;
|
2004-08-04 19:19:10 +00:00
|
|
|
int forked = 0;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
int *hpin = pin;
|
|
|
|
int *hpout = pout;
|
|
|
|
char *hwp;
|
|
|
|
int hinteractive;
|
|
|
|
int hintr;
|
2004-08-04 19:16:54 +00:00
|
|
|
struct brkcon *hbrklist;
|
2001-06-29 04:57:14 +00:00
|
|
|
int hexecflg;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &pin;
|
|
|
|
(void) &pout;
|
|
|
|
(void) ℘
|
|
|
|
(void) &shcom;
|
|
|
|
(void) &cp;
|
|
|
|
(void) &resetsig;
|
|
|
|
(void) &owp;
|
2004-03-15 08:29:22 +00:00
|
|
|
#endif
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("FORKEXEC: t=0x%x, pin 0x%x, pout 0x%x, act %d\n", t, pin,
|
|
|
|
pout, act));
|
|
|
|
DBGPRINTF7(("FORKEXEC: t->words is %s\n",
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[0])));
|
|
|
|
|
|
|
|
/* Hard to know how many words there are, be careful of garbage pointer values */
|
|
|
|
/* They are likely to cause "PCI bus fault" errors */
|
|
|
|
#if 0
|
|
|
|
DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[0]),
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[1])));
|
|
|
|
DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n",
|
|
|
|
((wp == NULL) ? "NULL" : wp[0]),
|
|
|
|
((wp[1] == NULL) ? "NULL" : wp[1])));
|
|
|
|
DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n",
|
|
|
|
((wp[2] == NULL) ? "NULL" : wp[2]),
|
|
|
|
((wp[3] == NULL) ? "NULL" : wp[3])));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
owp = wp;
|
|
|
|
resetsig = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
rv = -1; /* system-detected error */
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t->type == TCOM) {
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((cp = *wp++) != NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = *wp;
|
|
|
|
|
|
|
|
/* strip all initial assignments */
|
|
|
|
/* not correct wrt PATH=yyy command etc */
|
2004-08-04 19:19:10 +00:00
|
|
|
if (flag['x']) {
|
|
|
|
DBGPRINTF9(("FORKEXEC: echo'ing, cp=0x%x, wp=0x%x, owp=0x%x\n",
|
|
|
|
cp, wp, owp));
|
2004-08-04 19:16:54 +00:00
|
|
|
echo(cp ? wp : owp);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[0]),
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[1])));
|
|
|
|
DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n",
|
|
|
|
((wp == NULL) ? "NULL" : wp[0]),
|
|
|
|
((wp == NULL) ? "NULL" : wp[1])));
|
|
|
|
#endif
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (cp == NULL && t->ioact == NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((cp = *owp++) != NULL && assign(cp, COPYV));
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (setstatus(0));
|
2004-08-04 19:19:10 +00:00
|
|
|
} else if (cp != NULL) {
|
2001-06-29 04:57:14 +00:00
|
|
|
shcom = inbuilt(cp);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
t->words = wp;
|
|
|
|
f = act;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
#if 0
|
|
|
|
DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[0]),
|
|
|
|
((t->words == NULL) ? "NULL" : t->words[1])));
|
|
|
|
#endif
|
|
|
|
DBGPRINTF(("FORKEXEC: shcom 0x%x, f&FEXEC 0x%x, owp 0x%x\n", shcom,
|
|
|
|
f & FEXEC, owp));
|
|
|
|
|
|
|
|
if (shcom == NULL && (f & FEXEC) == 0) {
|
|
|
|
/* Save values in case the child process alters them */
|
2001-06-29 04:57:14 +00:00
|
|
|
hpin = pin;
|
|
|
|
hpout = pout;
|
|
|
|
hwp = *wp;
|
|
|
|
hinteractive = interactive;
|
|
|
|
hintr = intr;
|
|
|
|
hbrklist = brklist;
|
|
|
|
hexecflg = execflg;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
|
|
|
|
|
|
|
|
newpid = vfork();
|
|
|
|
|
|
|
|
if (newpid == -1) {
|
|
|
|
DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n"));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (newpid > 0) { /* Parent */
|
|
|
|
|
|
|
|
/* Restore values */
|
2001-06-29 04:57:14 +00:00
|
|
|
pin = hpin;
|
|
|
|
pout = hpout;
|
|
|
|
*wp = hwp;
|
|
|
|
interactive = hinteractive;
|
|
|
|
intr = hintr;
|
|
|
|
brklist = hbrklist;
|
|
|
|
execflg = hexecflg;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* moved up
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i == -1)
|
2004-08-04 19:19:10 +00:00
|
|
|
return(rv);
|
|
|
|
*/
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (pin != NULL)
|
|
|
|
closepipe(pin);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
/* Must be the child process, pid should be 0 */
|
|
|
|
DBGPRINTF(("FORKEXEC: child process, shcom=0x%x\n", shcom));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (interactive) {
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
resetsig = 1;
|
|
|
|
}
|
|
|
|
interactive = 0;
|
|
|
|
intr = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
forked = 1;
|
2001-06-29 04:57:14 +00:00
|
|
|
brklist = 0;
|
|
|
|
execflg = 0;
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (owp != NULL)
|
|
|
|
while ((cp = *owp++) != NULL && assign(cp, COPYV))
|
|
|
|
if (shcom == NULL)
|
|
|
|
export(lookup(cp));
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
#ifdef COMPIPE
|
|
|
|
if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
|
|
|
|
err("piping to/from shell builtins not yet done");
|
2004-08-04 19:19:10 +00:00
|
|
|
if (forked)
|
|
|
|
_exit(-1);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (-1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (pin != NULL) {
|
|
|
|
dup2(pin[0], 0);
|
|
|
|
closepipe(pin);
|
|
|
|
}
|
|
|
|
if (pout != NULL) {
|
|
|
|
dup2(pout[1], 1);
|
|
|
|
closepipe(pout);
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((iopp = t->ioact) != NULL) {
|
|
|
|
if (shcom != NULL && shcom != doexec) {
|
|
|
|
prs(cp);
|
|
|
|
err(": cannot redirect shell command");
|
2004-08-04 19:19:10 +00:00
|
|
|
if (forked)
|
|
|
|
_exit(-1);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (-1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
while (*iopp)
|
2004-08-04 19:19:10 +00:00
|
|
|
if (iosetup(*iopp++, pin != NULL, pout != NULL)) {
|
|
|
|
if (forked)
|
|
|
|
_exit(rv);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (rv);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shcom) {
|
|
|
|
i = setstatus((*shcom) (t));
|
|
|
|
if (forked)
|
|
|
|
_exit(i);
|
|
|
|
DBGPRINTF(("FORKEXEC: returning i=%d\n", i));
|
|
|
|
return (i);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/* should use FIOCEXCL */
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = FDBASE; i < NOFILE; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
close(i);
|
|
|
|
if (resetsig) {
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t->type == TPAREN)
|
2004-08-04 19:19:10 +00:00
|
|
|
_exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
|
2001-06-29 04:57:14 +00:00
|
|
|
if (wp[0] == NULL)
|
2004-08-04 19:19:10 +00:00
|
|
|
_exit(0);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-09-02 23:13:10 +00:00
|
|
|
cp = rexecve(wp[0], wp, makenv(0, NULL));
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(wp[0]);
|
|
|
|
prs(": ");
|
2004-08-04 19:19:10 +00:00
|
|
|
err(cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (!execflg)
|
|
|
|
trap[0] = NULL;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
leave();
|
|
|
|
/* NOTREACHED */
|
2004-08-04 19:19:10 +00:00
|
|
|
_exit(1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 0< 1> are ignored as required
|
|
|
|
* within pipelines.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int iosetup(iop, pipein, pipeout)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioword *iop;
|
2001-06-29 04:57:14 +00:00
|
|
|
int pipein, pipeout;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int u = -1;
|
2004-08-04 19:16:54 +00:00
|
|
|
char *cp = NULL, *msg;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("IOSETUP: iop 0x%x, pipein 0x%x, pipeout 0x%x\n", iop,
|
|
|
|
pipein, pipeout));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (iop->io_unit == IODEFAULT) /* take default */
|
2004-08-04 19:16:54 +00:00
|
|
|
iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (pipein && iop->io_unit == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (pipeout && iop->io_unit == 1)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((iop->io_flag & IOHERE) == 0) {
|
|
|
|
cp = iop->io_name;
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL)
|
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (iop->io_flag & IODUP) {
|
|
|
|
if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
|
|
|
|
prs(cp);
|
|
|
|
err(": illegal >& argument");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (*cp == '-')
|
|
|
|
iop->io_flag = IOCLOSE;
|
2004-08-04 19:16:54 +00:00
|
|
|
iop->io_flag &= ~(IOREAD | IOWRITE);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
switch (iop->io_flag) {
|
|
|
|
case IOREAD:
|
|
|
|
u = open(cp, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IOHERE:
|
2004-08-04 19:16:54 +00:00
|
|
|
case IOHERE | IOXHERE:
|
|
|
|
u = herein(iop->io_name, iop->io_flag & IOXHERE);
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = "here file";
|
|
|
|
break;
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
case IOWRITE | IOCAT:
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((u = open(cp, 1)) >= 0) {
|
2004-08-04 19:16:54 +00:00
|
|
|
lseek(u, (long) 0, 2);
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOWRITE:
|
|
|
|
u = creat(cp, 0666);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IODUP:
|
2004-08-04 19:16:54 +00:00
|
|
|
u = dup2(*cp - '0', iop->io_unit);
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IOCLOSE:
|
|
|
|
close(iop->io_unit);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (u < 0) {
|
|
|
|
prs(cp);
|
|
|
|
prs(": cannot ");
|
|
|
|
warn(msg);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
} else {
|
|
|
|
if (u != iop->io_unit) {
|
|
|
|
dup2(u, iop->io_unit);
|
|
|
|
close(u);
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void echo(wp)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char **wp;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
prs("+");
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; wp[i]; i++) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i)
|
|
|
|
prs(" ");
|
|
|
|
prs(wp[i]);
|
|
|
|
}
|
|
|
|
prs("\n");
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op **find1case(t, w)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
char *w;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t1;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op **tp;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char **wp, *cp;
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
if (t == NULL) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op **) NULL);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type,
|
|
|
|
T_CMD_NAMES[t->type]));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (t->type == TLIST) {
|
2004-08-04 19:19:10 +00:00
|
|
|
if ((tp = find1case(t->left, w)) != NULL) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=0x%x\n", tp));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (tp);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
t1 = t->right; /* TPAT */
|
2001-06-29 04:57:14 +00:00
|
|
|
} else
|
|
|
|
t1 = t;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
for (wp = t1->words; *wp;)
|
2004-08-04 19:19:10 +00:00
|
|
|
if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: returning &t1->left= 0x%x.\n",
|
|
|
|
&t1->left));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (&t1->left);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("FIND1CASE: returning NULL\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((struct op **) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct op *findcase(t, w)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
char *w;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op **tp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enter a new loop level (marked for break/continue).
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static void brkset(bc)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct brkcon *bc;
|
|
|
|
{
|
|
|
|
bc->nextlev = brklist;
|
|
|
|
brklist = bc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for the last process created.
|
|
|
|
* Print a message for each process found
|
|
|
|
* that was killed by a signal.
|
|
|
|
* Ignore interrupt signals while waiting
|
|
|
|
* unless `canintr' is true.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int waitfor(lastpid, canintr)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int lastpid;
|
2001-06-29 04:57:14 +00:00
|
|
|
int canintr;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int pid, rv;
|
2001-06-29 04:57:14 +00:00
|
|
|
int s;
|
|
|
|
int oheedint = heedint;
|
|
|
|
|
|
|
|
heedint = 0;
|
|
|
|
rv = 0;
|
|
|
|
do {
|
|
|
|
pid = wait(&s);
|
|
|
|
if (pid == -1) {
|
|
|
|
if (errno != EINTR || canintr)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if ((rv = WAITSIG(s)) != 0) {
|
|
|
|
if (rv < NSIGNAL) {
|
|
|
|
if (signame[rv] != NULL) {
|
|
|
|
if (pid != lastpid) {
|
|
|
|
prn(pid);
|
|
|
|
prs(": ");
|
|
|
|
}
|
|
|
|
prs(signame[rv]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pid != lastpid) {
|
|
|
|
prn(pid);
|
|
|
|
prs(": ");
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
prs("Signal ");
|
|
|
|
prn(rv);
|
|
|
|
prs(" ");
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (WAITCORE(s))
|
|
|
|
prs(" - core dumped");
|
|
|
|
if (rv >= NSIGNAL || signame[rv])
|
|
|
|
prs("\n");
|
|
|
|
rv = -1;
|
|
|
|
} else
|
|
|
|
rv = WAITVAL(s);
|
|
|
|
}
|
|
|
|
} while (pid != lastpid);
|
|
|
|
heedint = oheedint;
|
|
|
|
if (intr) {
|
|
|
|
if (interactive) {
|
|
|
|
if (canintr)
|
|
|
|
intr = 0;
|
|
|
|
} else {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (exstat == 0)
|
|
|
|
exstat = rv;
|
2001-06-29 04:57:14 +00:00
|
|
|
onintr(0);
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (rv);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int setstatus(s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
exstat = s;
|
|
|
|
setval(lookup("?"), putn(s));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (s);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PATH-searching interface to execve.
|
|
|
|
* If getenv("PATH") were kept up-to-date,
|
|
|
|
* execvp might be used.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *rexecve(c, v, envp)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *c, **v, **envp;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
|
|
|
REGISTER char *sp, *tp;
|
2001-06-29 04:57:14 +00:00
|
|
|
int eacces = 0, asis = 0;
|
|
|
|
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
|
2001-07-07 00:05:55 +00:00
|
|
|
char *name = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2001-07-07 00:05:55 +00:00
|
|
|
optind = 1;
|
|
|
|
if (find_applet_by_name(name)) {
|
2004-03-15 08:29:22 +00:00
|
|
|
/* We have to exec here since we vforked. Running
|
2001-07-07 00:05:55 +00:00
|
|
|
* run_applet_by_name() won't work and bad things
|
|
|
|
* will happen. */
|
|
|
|
execve("/proc/self/exe", v, envp);
|
|
|
|
execve("busybox", v, envp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("REXECVE: c=0x%x, v=0x%x, envp=0x%x\n", c, v, envp));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
sp = any('/', c) ? "" : path->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
asis = *sp == '\0';
|
|
|
|
while (asis || *sp != '\0') {
|
|
|
|
asis = 0;
|
|
|
|
tp = e.linep;
|
|
|
|
for (; *sp != '\0'; tp++)
|
|
|
|
if ((*tp = *sp++) == ':') {
|
|
|
|
asis = *sp == '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tp != e.linep)
|
|
|
|
*tp++ = '/';
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; (*tp++ = c[i++]) != '\0';);
|
2001-07-07 00:05:55 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
execve(e.linep, v, envp);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
switch (errno) {
|
|
|
|
case ENOEXEC:
|
|
|
|
*v = e.linep;
|
|
|
|
tp = *--v;
|
|
|
|
*v = e.linep;
|
2003-09-02 02:36:18 +00:00
|
|
|
execve(DEFAULT_SHELL, v, envp);
|
2001-06-29 04:57:14 +00:00
|
|
|
*v = tp;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ("no Shell");
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case ENOMEM:
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char *) bb_msg_memory_exhausted);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case E2BIG:
|
2004-08-04 19:16:54 +00:00
|
|
|
return ("argument list too long");
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
case EACCES:
|
|
|
|
eacces++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (errno == ENOENT ? "not found" : "cannot execute");
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command produced by generator `f'
|
|
|
|
* applied to stream `arg'.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int run(struct ioarg *argp, int (*f) (struct ioarg *))
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
struct op *otree;
|
|
|
|
struct wdblock *swdlist;
|
|
|
|
struct wdblock *siolist;
|
|
|
|
jmp_buf ev, rt;
|
|
|
|
xint *ofail;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &rv;
|
|
|
|
#endif
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("RUN: enter, areanum %d, outtree 0x%x, failpt 0x%x\n",
|
|
|
|
areanum, outtree, failpt));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
areanum++;
|
|
|
|
swdlist = wdlist;
|
|
|
|
siolist = iolist;
|
|
|
|
otree = outtree;
|
|
|
|
ofail = failpt;
|
|
|
|
rv = -1;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (newenv(setjmp(errpt = ev)) == 0) {
|
|
|
|
wdlist = 0;
|
|
|
|
iolist = 0;
|
|
|
|
pushio(argp, f);
|
|
|
|
e.iobase = e.iop;
|
|
|
|
yynerrs = 0;
|
|
|
|
if (setjmp(failpt = rt) == 0 && yyparse() == 0)
|
|
|
|
rv = execute(outtree, NOPIPE, NOPIPE, 0);
|
|
|
|
quitenv();
|
2004-08-04 19:19:10 +00:00
|
|
|
} else {
|
|
|
|
DBGPRINTF(("RUN: error from newenv()!\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
wdlist = swdlist;
|
|
|
|
iolist = siolist;
|
|
|
|
failpt = ofail;
|
|
|
|
outtree = otree;
|
|
|
|
freearea(areanum--);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (rv);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- do.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* built-in commands: doX
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dohelp(struct op *t)
|
2001-07-07 00:05:55 +00:00
|
|
|
{
|
|
|
|
int col;
|
|
|
|
const struct builtincmd *x;
|
|
|
|
|
|
|
|
printf("\nBuilt-in commands:\n");
|
|
|
|
printf("-------------------\n");
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) {
|
2001-07-07 00:05:55 +00:00
|
|
|
if (!x->name)
|
|
|
|
continue;
|
|
|
|
col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
|
|
|
|
if (col > 60) {
|
|
|
|
printf("\n");
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
|
2001-07-07 00:05:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const struct BB_applet *applet;
|
|
|
|
extern const struct BB_applet applets[];
|
|
|
|
extern const size_t NUM_APPLETS;
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) {
|
2001-07-07 00:05:55 +00:00
|
|
|
if (!applet->name)
|
|
|
|
continue;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name);
|
2001-07-07 00:05:55 +00:00
|
|
|
if (col > 60) {
|
|
|
|
printf("\n");
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
printf("\n\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dolabel(struct op *t)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dochdir(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp, *er;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
|
|
|
|
er = ": no home directory";
|
2004-08-04 19:16:54 +00:00
|
|
|
else if (chdir(cp) < 0)
|
2001-06-29 04:57:14 +00:00
|
|
|
er = ": bad directory";
|
|
|
|
else
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
|
|
|
prs(cp != NULL ? cp : "cd");
|
2001-06-29 04:57:14 +00:00
|
|
|
err(er);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doshift(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int n;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
n = t->words[1] ? getn(t->words[1]) : 1;
|
|
|
|
if (dolc < n) {
|
2001-06-29 04:57:14 +00:00
|
|
|
err("nothing to shift");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
dolv[n] = dolv[0];
|
|
|
|
dolv += n;
|
|
|
|
dolc -= n;
|
|
|
|
setval(lookup("#"), putn(dolc));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* execute login and newgrp directly
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dologin(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
2004-09-02 23:13:10 +00:00
|
|
|
cp = rexecve(t->words[0], t->words, makenv(0, NULL));
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(t->words[0]);
|
|
|
|
prs(": ");
|
|
|
|
err(cp);
|
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doumask(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i, n;
|
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((cp = t->words[1]) == NULL) {
|
|
|
|
i = umask(0);
|
|
|
|
umask(i);
|
2004-08-04 19:16:54 +00:00
|
|
|
for (n = 3 * 4; (n -= 3) >= 0;)
|
|
|
|
putc('0' + ((i >> n) & 07), stderr);
|
2001-06-29 04:57:14 +00:00
|
|
|
putc('\n', stderr);
|
|
|
|
} else {
|
2004-08-04 19:16:54 +00:00
|
|
|
for (n = 0; *cp >= '0' && *cp <= '9'; cp++)
|
|
|
|
n = n * 8 + (*cp - '0');
|
2001-06-29 04:57:14 +00:00
|
|
|
umask(n);
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doexec(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
jmp_buf ex;
|
|
|
|
xint *ofail;
|
|
|
|
|
|
|
|
t->ioact = NULL;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
execflg = 1;
|
|
|
|
ofail = failpt;
|
|
|
|
if (setjmp(failpt = ex) == 0)
|
|
|
|
execute(t, NOPIPE, NOPIPE, FEXEC);
|
|
|
|
failpt = ofail;
|
|
|
|
execflg = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dodot(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
|
|
|
REGISTER char *sp, *tp;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
2004-08-04 19:19:10 +00:00
|
|
|
int maltmp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("DODOT: enter, t=0x%x, tleft 0x%x, tright 0x%x, e.linep is %s\n", t, t->left, t->right, ((e.linep == NULL) ? "NULL" : e.linep)));
|
|
|
|
|
|
|
|
if ((cp = t->words[1]) == NULL) {
|
|
|
|
DBGPRINTF(("DODOT: bad args, ret 0\n"));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
} else {
|
|
|
|
DBGPRINTF(("DODOT: cp is %s\n", cp));
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
sp = any('/', cp) ? ":" : path->value;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n",
|
|
|
|
((sp == NULL) ? "NULL" : sp),
|
|
|
|
((e.linep == NULL) ? "NULL" : e.linep)));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
while (*sp) {
|
|
|
|
tp = e.linep;
|
|
|
|
while (*sp && (*tp = *sp++) != ':')
|
|
|
|
tp++;
|
|
|
|
if (tp != e.linep)
|
|
|
|
*tp++ = '/';
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; (*tp++ = cp[i++]) != '\0';);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
/* Original code */
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((i = open(e.linep, 0)) >= 0) {
|
|
|
|
exstat = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
maltmp = remap(i);
|
|
|
|
DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep));
|
|
|
|
|
|
|
|
next(maltmp); /* Basically a PUSHIO */
|
|
|
|
|
|
|
|
DBGPRINTF(("DODOT: returning exstat=%d\n", exstat));
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (exstat);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
} /* While */
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
prs(cp);
|
|
|
|
err(": not found");
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (-1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dowait(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((cp = t->words[1]) != NULL) {
|
|
|
|
i = getn(cp);
|
|
|
|
if (i == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
} else
|
|
|
|
i = -1;
|
|
|
|
setstatus(waitfor(i, 1));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doread(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp, **wp;
|
|
|
|
REGISTER int nb = 0;
|
|
|
|
REGISTER int nl = 0;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (t->words[1] == NULL) {
|
|
|
|
err("Usage: read name ...");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
for (wp = t->words + 1; *wp; wp++) {
|
|
|
|
for (cp = e.linep; !nl && cp < elinep - 1; cp++)
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
|
2004-08-04 19:16:54 +00:00
|
|
|
(nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value)))
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
*cp = 0;
|
|
|
|
if (nb <= 0)
|
|
|
|
break;
|
|
|
|
setval(lookup(*wp), e.linep);
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (nb <= 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doeval(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (RUN(awordlist, t->words + 1, wdchar));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dotrap(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int n, i;
|
|
|
|
REGISTER int resetsig;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (t->words[1] == NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i <= _NSIG; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
if (trap[i]) {
|
|
|
|
prn(i);
|
|
|
|
prs(": ");
|
|
|
|
prs(trap[i]);
|
|
|
|
prs("\n");
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
resetsig = isdigit(*t->words[1]);
|
|
|
|
for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
|
|
|
|
n = getsig(t->words[i]);
|
|
|
|
freecell(trap[n]);
|
|
|
|
trap[n] = 0;
|
|
|
|
if (!resetsig) {
|
|
|
|
if (*t->words[1] != '\0') {
|
|
|
|
trap[n] = strsave(t->words[1], 0);
|
|
|
|
setsig(n, sig);
|
|
|
|
} else
|
|
|
|
setsig(n, SIG_IGN);
|
|
|
|
} else {
|
|
|
|
if (interactive)
|
|
|
|
if (n == SIGINT)
|
|
|
|
setsig(n, onintr);
|
|
|
|
else
|
2004-08-04 19:16:54 +00:00
|
|
|
setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
|
2001-06-29 04:57:14 +00:00
|
|
|
else
|
|
|
|
setsig(n, SIG_DFL);
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int getsig(s)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *s;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int n;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((n = getn(s)) < 0 || n > _NSIG) {
|
|
|
|
err("trap: bad signal number");
|
|
|
|
n = 0;
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (n);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
static void setsig(REGISTER int n, sighandler_t f)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
|
|
|
|
ourtrap[n] = 1;
|
|
|
|
signal(n, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int getn(as)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *as;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
|
|
|
REGISTER int n, m;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
s = as;
|
|
|
|
m = 1;
|
|
|
|
if (*s == '-') {
|
|
|
|
m = -1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
for (n = 0; isdigit(*s); s++)
|
2004-08-04 19:16:54 +00:00
|
|
|
n = (n * 10) + (*s - '0');
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*s) {
|
|
|
|
prs(as);
|
|
|
|
err(": bad number");
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (n * m);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dobreak(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (brkcontin(t->words[1], 1));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int docontinue(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (brkcontin(t->words[1], 0));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int brkcontin(cp, val)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
int val;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct brkcon *bc;
|
|
|
|
REGISTER int nl;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
nl = cp == NULL ? 1 : getn(cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (nl <= 0)
|
|
|
|
nl = 999;
|
|
|
|
do {
|
|
|
|
if ((bc = brklist) == NULL)
|
|
|
|
break;
|
|
|
|
brklist = bc->nextlev;
|
|
|
|
} while (--nl);
|
|
|
|
if (nl) {
|
|
|
|
err("bad break/continue level");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
isbreak = val;
|
|
|
|
longjmp(bc->brkpt, 1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doexit(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
execflg = 0;
|
|
|
|
if ((cp = t->words[1]) != NULL)
|
|
|
|
setstatus(getn(cp));
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("DOEXIT: calling leave(), t=0x%x\n", t));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
leave();
|
|
|
|
/* NOTREACHED */
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doexport(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
rdexp(t->words + 1, export, EXPORT);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doreadonly(t)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct op *t;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
rdexp(t->words + 1, ronly, RONLY);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void rdexp(char **wp, void (*f) (struct var *), int key)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF6(("RDEXP: enter, wp=0x%x, func=0x%x, key=%d\n", wp, f, key));
|
|
|
|
DBGPRINTF6(("RDEXP: *wp=%s\n", *wp));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*wp != NULL) {
|
2001-07-12 19:39:59 +00:00
|
|
|
for (; *wp != NULL; wp++) {
|
|
|
|
if (isassign(*wp)) {
|
|
|
|
char *cp;
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2001-07-12 19:39:59 +00:00
|
|
|
assign(*wp, COPYV);
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cp = *wp; *cp != '='; cp++);
|
2001-07-12 19:39:59 +00:00
|
|
|
*cp = '\0';
|
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
if (checkname(*wp))
|
2004-08-04 19:16:54 +00:00
|
|
|
(*f) (lookup(*wp));
|
2001-06-29 04:57:14 +00:00
|
|
|
else
|
|
|
|
badid(*wp);
|
2001-07-12 19:39:59 +00:00
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
} else
|
|
|
|
putvlist(key, 1);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void badid(s)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
prs(s);
|
|
|
|
err(": bad identifier");
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int doset(t)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct op *t;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct var *vp;
|
|
|
|
REGISTER char *cp;
|
|
|
|
REGISTER int n;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((cp = t->words[1]) == NULL) {
|
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
|
|
|
varput(vp->name, 1);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (*cp == '-') {
|
|
|
|
/* bad: t->words++; */
|
2004-08-04 19:16:54 +00:00
|
|
|
for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*++cp == 0)
|
|
|
|
flag['x'] = flag['v'] = 0;
|
|
|
|
else
|
|
|
|
for (; *cp; cp++)
|
|
|
|
switch (*cp) {
|
|
|
|
case 'e':
|
|
|
|
if (!interactive)
|
|
|
|
flag['e']++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-08-04 19:16:54 +00:00
|
|
|
if (*cp >= 'a' && *cp <= 'z')
|
|
|
|
flag[(int) *cp]++;
|
2001-06-29 04:57:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
setdash();
|
|
|
|
}
|
|
|
|
if (t->words[1]) {
|
|
|
|
t->words[0] = dolv[0];
|
2004-08-04 19:16:54 +00:00
|
|
|
for (n = 1; t->words[n]; n++)
|
|
|
|
setarea((char *) t->words[n], 0);
|
|
|
|
dolc = n - 1;
|
2001-06-29 04:57:14 +00:00
|
|
|
dolv = t->words;
|
|
|
|
setval(lookup("#"), putn(dolc));
|
2004-08-04 19:16:54 +00:00
|
|
|
setarea((char *) (dolv - 1), 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void varput(s, out)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
int out;
|
|
|
|
{
|
2001-08-06 14:14:18 +00:00
|
|
|
if (isalnum(*s) || *s == '_') {
|
2001-06-29 04:57:14 +00:00
|
|
|
write(out, s, strlen(s));
|
|
|
|
write(out, "\n", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Herbert Xu <herbert@debian.org>
|
|
|
|
* This file contains code for the times builtin.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dotimes(struct op *t)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
struct tms buf;
|
|
|
|
long int clk_tck = sysconf(_SC_CLK_TCK);
|
|
|
|
|
|
|
|
times(&buf);
|
|
|
|
printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
|
2004-08-04 19:16:54 +00:00
|
|
|
(int) (buf.tms_utime / clk_tck / 60),
|
|
|
|
((double) buf.tms_utime) / clk_tck,
|
|
|
|
(int) (buf.tms_stime / clk_tck / 60),
|
|
|
|
((double) buf.tms_stime) / clk_tck,
|
|
|
|
(int) (buf.tms_cutime / clk_tck / 60),
|
|
|
|
((double) buf.tms_cutime) / clk_tck,
|
|
|
|
(int) (buf.tms_cstime / clk_tck / 60),
|
|
|
|
((double) buf.tms_cstime) / clk_tck);
|
2001-06-29 04:57:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int (*inbuilt(char *s)) (struct op *) {
|
2001-07-07 00:05:55 +00:00
|
|
|
const struct builtincmd *bp;
|
|
|
|
|
|
|
|
for (bp = builtincmds; bp->name != NULL; bp++)
|
|
|
|
if (strcmp(bp->name, s) == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (bp->builtinfunc);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- eval.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ${}
|
|
|
|
* `command`
|
|
|
|
* blank interpretation
|
|
|
|
* quoting
|
|
|
|
* glob
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char **eval(char **ap, int f)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
struct wdblock *wb;
|
|
|
|
char **wp;
|
|
|
|
char **wf;
|
|
|
|
jmp_buf ev;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) ℘
|
|
|
|
(void) ≈
|
|
|
|
#endif
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF4(("EVAL: enter, f=%d\n", f));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
wp = NULL;
|
|
|
|
wb = NULL;
|
|
|
|
wf = NULL;
|
|
|
|
if (newenv(setjmp(errpt = ev)) == 0) {
|
|
|
|
while (*ap && isassign(*ap))
|
|
|
|
expand(*ap++, &wb, f & ~DOGLOB);
|
|
|
|
if (flag['k']) {
|
|
|
|
for (wf = ap; *wf; wf++) {
|
|
|
|
if (isassign(*wf))
|
|
|
|
expand(*wf, &wb, f & ~DOGLOB);
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
for (wb = addword((char *) 0, wb); *ap; ap++) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (!flag['k'] || !isassign(*ap))
|
|
|
|
expand(*ap, &wb, f & ~DOKEY);
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
wb = addword((char *) 0, wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
wp = getwords(wb);
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
gflg = 1;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (gflg ? (char **) NULL : wp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the exported environment from the exported
|
|
|
|
* names in the dictionary. Keyword assignments
|
|
|
|
* will already have been done.
|
|
|
|
*/
|
2004-09-02 23:13:10 +00:00
|
|
|
static char **makenv(int all, struct wdblock *wb)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct var *vp;
|
|
|
|
|
|
|
|
DBGPRINTF5(("MAKENV: enter, all=%d\n", all));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
2004-08-04 19:19:10 +00:00
|
|
|
if (all || vp->status & EXPORT)
|
2001-06-29 04:57:14 +00:00
|
|
|
wb = addword(vp->name, wb);
|
2004-08-04 19:16:54 +00:00
|
|
|
wb = addword((char *) 0, wb);
|
|
|
|
return (getwords(wb));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *evalstr(cp, f)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
int f;
|
|
|
|
{
|
|
|
|
struct wdblock *wb;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF6(("EVALSTR: enter, cp=0x%x, f=%d\n", cp, f));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
wb = NULL;
|
|
|
|
if (expand(cp, &wb, f)) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (wb == NULL || wb->w_nword == 0
|
|
|
|
|| (cp = wb->w_words[0]) == NULL)
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = "";
|
|
|
|
DELETE(wb);
|
|
|
|
} else
|
|
|
|
cp = NULL;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (cp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
static int expand(char *cp, REGISTER struct wdblock **wbp, int f)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
jmp_buf ev;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &cp;
|
|
|
|
#endif
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF3(("EXPAND: enter, f=%d\n", f));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
gflg = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (cp == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (!anys("$`'\"", cp) &&
|
2004-08-04 19:16:54 +00:00
|
|
|
!anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) {
|
2001-06-29 04:57:14 +00:00
|
|
|
cp = strsave(cp, areanum);
|
|
|
|
if (f & DOTRIM)
|
|
|
|
unquote(cp);
|
|
|
|
*wbp = addword(cp, *wbp);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (newenv(setjmp(errpt = ev)) == 0) {
|
|
|
|
PUSHIO(aword, cp, strchar);
|
|
|
|
e.iobase = e.iop;
|
|
|
|
while ((cp = blank(f)) && gflg == 0) {
|
|
|
|
e.linep = cp;
|
|
|
|
cp = strsave(cp, areanum);
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((f & DOGLOB) == 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (f & DOTRIM)
|
|
|
|
unquote(cp);
|
|
|
|
*wbp = addword(cp, *wbp);
|
|
|
|
} else
|
|
|
|
*wbp = glob(cp, *wbp);
|
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
gflg = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (gflg == 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Blank interpretation and quoting
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *blank(f)
|
2001-06-29 04:57:14 +00:00
|
|
|
int f;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c, c1;
|
|
|
|
REGISTER char *sp;
|
2001-06-29 04:57:14 +00:00
|
|
|
int scanequals, foundequals;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF3(("BLANK: enter, f=%d\n", f));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
sp = e.linep;
|
|
|
|
scanequals = f & DOKEY;
|
|
|
|
foundequals = 0;
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
loop:
|
2001-06-29 04:57:14 +00:00
|
|
|
switch (c = subgetc('"', foundequals)) {
|
|
|
|
case 0:
|
|
|
|
if (sp == e.linep)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
*e.linep++ = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (sp);
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (f & DOBLANK && any(c, ifs->value))
|
|
|
|
goto loop;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
scanequals = 0;
|
|
|
|
if (INSUB())
|
|
|
|
break;
|
|
|
|
for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
if (c == '\'' || !any(c, "$`\""))
|
|
|
|
c |= QUOTE;
|
|
|
|
*e.linep++ = c;
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
unget(c);
|
2001-08-06 14:14:18 +00:00
|
|
|
if (!isalpha(c) && c != '_')
|
2001-06-29 04:57:14 +00:00
|
|
|
scanequals = 0;
|
|
|
|
for (;;) {
|
|
|
|
c = subgetc('"', foundequals);
|
|
|
|
if (c == 0 ||
|
2004-08-04 19:16:54 +00:00
|
|
|
f & (DOBLANK && any(c, ifs->value)) ||
|
|
|
|
(!INSUB() && any(c, "\"'"))) {
|
|
|
|
scanequals = 0;
|
2001-06-29 04:57:14 +00:00
|
|
|
unget(c);
|
|
|
|
if (any(c, "\"'"))
|
|
|
|
goto loop;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scanequals) {
|
|
|
|
if (c == '=') {
|
|
|
|
foundequals = 1;
|
2004-08-04 19:16:54 +00:00
|
|
|
scanequals = 0;
|
|
|
|
} else if (!isalnum(c) && c != '_')
|
2001-06-29 04:57:14 +00:00
|
|
|
scanequals = 0;
|
|
|
|
}
|
|
|
|
*e.linep++ = c;
|
|
|
|
}
|
|
|
|
*e.linep++ = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (sp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get characters, substituting for ` and $
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int subgetc(ec, quoted)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char ec;
|
2001-06-29 04:57:14 +00:00
|
|
|
int quoted;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char c;
|
|
|
|
|
|
|
|
DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
again:
|
2001-06-29 04:57:14 +00:00
|
|
|
c = my_getc(ec);
|
|
|
|
if (!INSUB() && ec != '\'') {
|
|
|
|
if (c == '`') {
|
|
|
|
if (grave(quoted) == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iop->task = XGRAVE;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (c == '$' && (c = dollar(quoted)) == 0) {
|
|
|
|
e.iop->task = XDOLL;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to generate the string returned by ${} substitution.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dollar(quoted)
|
2001-06-29 04:57:14 +00:00
|
|
|
int quoted;
|
|
|
|
{
|
|
|
|
int otask;
|
|
|
|
struct io *oiop;
|
|
|
|
char *dolp;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s, c, *cp = NULL;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct var *vp;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
c = readc();
|
|
|
|
s = e.linep;
|
|
|
|
if (c != '{') {
|
|
|
|
*e.linep++ = c;
|
2001-08-06 14:14:18 +00:00
|
|
|
if (isalpha(c) || c == '_') {
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((c = readc()) != 0 && (isalnum(c) || c == '_'))
|
2001-06-29 04:57:14 +00:00
|
|
|
if (e.linep < elinep)
|
|
|
|
*e.linep++ = c;
|
|
|
|
unget(c);
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
} else {
|
|
|
|
oiop = e.iop;
|
|
|
|
otask = e.iop->task;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iop->task = XOTHER;
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n')
|
2001-06-29 04:57:14 +00:00
|
|
|
if (e.linep < elinep)
|
|
|
|
*e.linep++ = c;
|
|
|
|
if (oiop == e.iop)
|
|
|
|
e.iop->task = otask;
|
|
|
|
if (c != '}') {
|
|
|
|
err("unclosed ${");
|
|
|
|
gflg++;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e.linep >= elinep) {
|
|
|
|
err("string in ${} too long");
|
|
|
|
gflg++;
|
|
|
|
e.linep -= 10;
|
|
|
|
}
|
|
|
|
*e.linep = 0;
|
|
|
|
if (*s)
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cp = s + 1; *cp; cp++)
|
2001-06-29 04:57:14 +00:00
|
|
|
if (any(*cp, "=-+?")) {
|
|
|
|
c = *cp;
|
|
|
|
*cp++ = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s[1] == 0 && (*s == '*' || *s == '@')) {
|
|
|
|
if (dolc > 1) {
|
|
|
|
/* currently this does not distinguish $* and $@ */
|
|
|
|
/* should check dollar */
|
|
|
|
e.linep = s;
|
2004-08-04 19:16:54 +00:00
|
|
|
PUSHIO(awordlist, dolv + 1, dolchar);
|
|
|
|
return (0);
|
|
|
|
} else { /* trap the nasty ${=} */
|
2001-06-29 04:57:14 +00:00
|
|
|
s[0] = '1';
|
|
|
|
s[1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vp = lookup(s);
|
|
|
|
if ((dolp = vp->value) == null) {
|
|
|
|
switch (c) {
|
|
|
|
case '=':
|
|
|
|
if (isdigit(*s)) {
|
|
|
|
err("cannot use ${...=...} with $n");
|
|
|
|
gflg++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setval(vp, cp);
|
|
|
|
dolp = vp->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
dolp = strsave(cp, areanum);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
if (*cp == 0) {
|
|
|
|
prs("missing value for ");
|
|
|
|
err(s);
|
|
|
|
} else
|
|
|
|
err(cp);
|
|
|
|
gflg++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (c == '+')
|
|
|
|
dolp = strsave(cp, areanum);
|
|
|
|
if (flag['u'] && dolp == null) {
|
|
|
|
prs("unset variable: ");
|
|
|
|
err(s);
|
|
|
|
gflg++;
|
|
|
|
}
|
|
|
|
e.linep = s;
|
|
|
|
PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command in `...` and read its output.
|
|
|
|
*/
|
2003-03-14 16:05:59 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int grave(quoted)
|
2001-06-29 04:57:14 +00:00
|
|
|
int quoted;
|
|
|
|
{
|
|
|
|
char *cp;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2003-03-14 16:05:59 +00:00
|
|
|
int j;
|
2001-06-29 04:57:14 +00:00
|
|
|
int pf[2];
|
2003-03-14 16:05:59 +00:00
|
|
|
static char child_cmd[LINELIM];
|
|
|
|
char *src;
|
|
|
|
char *dest;
|
|
|
|
int count;
|
|
|
|
int ignore;
|
|
|
|
int ignore_once;
|
|
|
|
char *argument_list[4];
|
2004-09-02 23:13:10 +00:00
|
|
|
struct wdblock *wb = NULL;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &cp;
|
|
|
|
#endif
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
for (cp = e.iop->argp->aword; *cp != '`'; cp++)
|
|
|
|
if (*cp == 0) {
|
|
|
|
err("no closing `");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2003-03-14 16:05:59 +00:00
|
|
|
|
|
|
|
/* string copy with dollar expansion */
|
|
|
|
src = e.iop->argp->aword;
|
|
|
|
dest = child_cmd;
|
|
|
|
count = 0;
|
|
|
|
ignore = 0;
|
|
|
|
ignore_once = 0;
|
|
|
|
while ((*src != '`') && (count < LINELIM)) {
|
|
|
|
if (*src == '\'')
|
|
|
|
ignore = !ignore;
|
|
|
|
if (*src == '\\')
|
|
|
|
ignore_once = 1;
|
|
|
|
if (*src == '$' && !ignore && !ignore_once) {
|
|
|
|
struct var *vp;
|
|
|
|
char var_name[LINELIM];
|
|
|
|
char alt_value[LINELIM];
|
|
|
|
int var_index = 0;
|
|
|
|
int alt_index = 0;
|
|
|
|
char operator = 0;
|
|
|
|
int braces = 0;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
src++;
|
|
|
|
if (*src == '{') {
|
|
|
|
braces = 1;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var_name[var_index++] = *src++;
|
2005-07-20 18:33:12 +00:00
|
|
|
while (isalnum(*src) || *src=='_')
|
2003-03-14 16:05:59 +00:00
|
|
|
var_name[var_index++] = *src++;
|
|
|
|
var_name[var_index] = 0;
|
|
|
|
|
|
|
|
if (braces) {
|
|
|
|
switch (*src) {
|
|
|
|
case '}':
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
case '=':
|
|
|
|
case '+':
|
|
|
|
case '?':
|
2004-08-04 19:16:54 +00:00
|
|
|
operator = * src;
|
2003-03-14 16:05:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err("unclosed ${\n");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2003-03-14 16:05:59 +00:00
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
if (operator) {
|
2003-03-14 16:05:59 +00:00
|
|
|
src++;
|
|
|
|
while (*src && (*src != '}')) {
|
|
|
|
alt_value[alt_index++] = *src++;
|
|
|
|
}
|
|
|
|
alt_value[alt_index] = 0;
|
|
|
|
if (*src != '}') {
|
|
|
|
err("unclosed ${\n");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2003-03-14 16:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
2004-09-02 23:13:10 +00:00
|
|
|
if (isalpha(*var_name)) {
|
|
|
|
/* let subshell handle it instead */
|
2003-03-14 16:05:59 +00:00
|
|
|
|
2004-09-02 23:13:10 +00:00
|
|
|
char *namep = var_name;
|
|
|
|
|
|
|
|
*dest++ = '$';
|
|
|
|
if (braces)
|
|
|
|
*dest++ = '{';
|
|
|
|
while (*namep)
|
|
|
|
*dest++ = *namep++;
|
|
|
|
if (operator) {
|
|
|
|
char *altp = alt_value;
|
|
|
|
*dest++ = operator;
|
|
|
|
while (*altp)
|
|
|
|
*dest++ = *altp++;
|
|
|
|
}
|
|
|
|
if (braces)
|
|
|
|
*dest++ = '}';
|
|
|
|
|
|
|
|
wb = addword(lookup(var_name)->name, wb);
|
|
|
|
} else {
|
|
|
|
/* expand */
|
|
|
|
|
|
|
|
vp = lookup(var_name);
|
|
|
|
if (vp->value != null)
|
|
|
|
value = (operator == '+') ?
|
|
|
|
alt_value : vp->value;
|
|
|
|
else if (operator == '?') {
|
|
|
|
err(alt_value);
|
|
|
|
return (0);
|
|
|
|
} else if (alt_index && (operator != '+')) {
|
|
|
|
value = alt_value;
|
|
|
|
if (operator == '=')
|
|
|
|
setval(vp, value);
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (*value && (count < LINELIM)) {
|
|
|
|
*dest++ = *value++;
|
|
|
|
count++;
|
|
|
|
}
|
2003-03-14 16:05:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*dest++ = *src++;
|
|
|
|
count++;
|
|
|
|
ignore_once = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (openpipe(pf) < 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((i = vfork()) == -1 && errno == EAGAIN);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF3(("GRAVE: i is %d\n", io));
|
|
|
|
|
2003-03-14 16:05:59 +00:00
|
|
|
if (i < 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
closepipe(pf);
|
2004-08-04 19:16:54 +00:00
|
|
|
err((char *) bb_msg_memory_exhausted);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (i != 0) {
|
2003-03-14 16:05:59 +00:00
|
|
|
waitpid(i, NULL, 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iop->argp->aword = ++cp;
|
|
|
|
close(pf[1]);
|
2004-08-04 19:16:54 +00:00
|
|
|
PUSHIO(afile, remap(pf[0]),
|
|
|
|
(int (*)(struct ioarg *)) ((quoted) ? qgravechar :
|
|
|
|
gravechar));
|
|
|
|
return (1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
/* allow trapped signals */
|
2003-03-14 16:05:59 +00:00
|
|
|
/* XXX - Maybe this signal stuff should go as well? */
|
2004-08-04 19:16:54 +00:00
|
|
|
for (j = 0; j <= _NSIG; j++)
|
2003-03-14 16:05:59 +00:00
|
|
|
if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(j, SIG_DFL);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
dup2(pf[1], 1);
|
|
|
|
closepipe(pf);
|
2003-03-14 16:05:59 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
argument_list[0] = (char *) DEFAULT_SHELL;
|
2003-03-14 16:05:59 +00:00
|
|
|
argument_list[1] = "-c";
|
|
|
|
argument_list[2] = child_cmd;
|
|
|
|
argument_list[3] = 0;
|
|
|
|
|
2004-09-02 23:13:10 +00:00
|
|
|
cp = rexecve(argument_list[0], argument_list, makenv(1, wb));
|
2004-08-04 19:19:10 +00:00
|
|
|
prs(argument_list[0]);
|
|
|
|
prs(": ");
|
|
|
|
err(cp);
|
2003-03-14 16:05:59 +00:00
|
|
|
_exit(1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-14 16:05:59 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *unquote(as)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *as;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((s = as) != NULL)
|
|
|
|
while (*s)
|
|
|
|
*s++ &= ~QUOTE;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (as);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- glob.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* glob
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define scopy(x) strsave((x), areanum)
|
|
|
|
#define BLKSIZ 512
|
|
|
|
#define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct wdblock *cl, *nl;
|
|
|
|
static char spcl[] = "[?*";
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct wdblock *glob(cp, wb)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *cp;
|
|
|
|
struct wdblock *wb;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
|
|
|
REGISTER char *pp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (cp == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
i = 0;
|
|
|
|
for (pp = cp; *pp; pp++)
|
|
|
|
if (any(*pp, spcl))
|
|
|
|
i++;
|
|
|
|
else if (!any(*pp & ~QUOTE, spcl))
|
|
|
|
*pp &= ~QUOTE;
|
|
|
|
if (i != 0) {
|
2004-08-04 19:16:54 +00:00
|
|
|
for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl);
|
|
|
|
cl = nl) {
|
|
|
|
nl = newword(cl->w_nword * 2);
|
|
|
|
for (i = 0; i < cl->w_nword; i++) { /* for each argument */
|
2001-06-29 04:57:14 +00:00
|
|
|
for (pp = cl->w_words[i]; *pp; pp++)
|
|
|
|
if (any(*pp, spcl)) {
|
|
|
|
globname(cl->w_words[i], pp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*pp == '\0')
|
|
|
|
nl = addword(scopy(cl->w_words[i]), nl);
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
DELETE(cl->w_words[i]);
|
|
|
|
DELETE(cl);
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
unquote(cl->w_words[i]);
|
2004-08-04 19:16:54 +00:00
|
|
|
glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (cl->w_nword) {
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
wb = addword(cl->w_words[i], wb);
|
|
|
|
DELETE(cl);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wb = addword(unquote(cp), wb);
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void globname(we, pp)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *we;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *pp;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *np, *cp;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *name, *gp, *dp;
|
|
|
|
int k;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *de;
|
2004-08-04 19:16:54 +00:00
|
|
|
char dname[NAME_MAX + 1];
|
2001-06-29 04:57:14 +00:00
|
|
|
struct stat dbuf;
|
|
|
|
|
|
|
|
for (np = we; np != pp; pp--)
|
|
|
|
if (pp[-1] == '/')
|
|
|
|
break;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (dp = cp = space((int) (pp - np) + 3); np < pp;)
|
2001-06-29 04:57:14 +00:00
|
|
|
*cp++ = *np++;
|
|
|
|
*cp++ = '.';
|
|
|
|
*cp = '\0';
|
2004-08-04 19:16:54 +00:00
|
|
|
for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';)
|
2001-06-29 04:57:14 +00:00
|
|
|
*cp++ = *np++;
|
|
|
|
*cp = '\0';
|
|
|
|
dirp = opendir(dp);
|
|
|
|
if (dirp == 0) {
|
|
|
|
DELETE(dp);
|
|
|
|
DELETE(gp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dname[NAME_MAX] = '\0';
|
2004-08-04 19:16:54 +00:00
|
|
|
while ((de = readdir(dirp)) != NULL) {
|
2001-06-29 04:57:14 +00:00
|
|
|
/* XXX Hmmm... What this could be? (abial) */
|
|
|
|
/*
|
2004-08-04 19:16:54 +00:00
|
|
|
if (ent[j].d_ino == 0)
|
|
|
|
continue;
|
|
|
|
*/
|
2001-06-29 04:57:14 +00:00
|
|
|
strncpy(dname, de->d_name, NAME_MAX);
|
2004-08-04 19:16:54 +00:00
|
|
|
if (dname[0] == '.')
|
|
|
|
if (*gp != '.')
|
|
|
|
continue;
|
|
|
|
for (k = 0; k < NAME_MAX; k++)
|
|
|
|
if (any(dname[k], spcl))
|
|
|
|
dname[k] |= QUOTE;
|
|
|
|
if (gmatch(dname, gp)) {
|
|
|
|
name = generate(we, pp, dname, np);
|
|
|
|
if (*np && !anys(np, spcl)) {
|
|
|
|
if (stat(name, &dbuf)) {
|
|
|
|
DELETE(name);
|
2001-06-29 04:57:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
nl = addword(name, nl);
|
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
DELETE(dp);
|
|
|
|
DELETE(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate a pathname as below.
|
|
|
|
* start..end1 / middle end
|
|
|
|
* the slashes come for free
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static char *generate(start1, end1, middle, end)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *start1;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *end1;
|
2001-06-29 04:57:14 +00:00
|
|
|
char *middle, *end;
|
|
|
|
{
|
|
|
|
char *p;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *op, *xp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
p = op =
|
|
|
|
space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2);
|
2001-06-29 04:57:14 +00:00
|
|
|
for (xp = start1; xp != end1;)
|
|
|
|
*op++ = *xp++;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (xp = middle; (*op++ = *xp++) != '\0';);
|
2001-06-29 04:57:14 +00:00
|
|
|
op--;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (xp = end; (*op++ = *xp++) != '\0';);
|
|
|
|
return (p);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int anyspcl(wb)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct wdblock *wb;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
|
|
|
REGISTER char **wd;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
wd = wb->w_words;
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < wb->w_nword; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
if (anys(spcl, *wd++))
|
2004-08-04 19:16:54 +00:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int xstrcmp(p1, p2)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *p1, *p2;
|
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return (strcmp(*(char **) p1, *(char **) p2));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- word.c -------- */
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct wdblock *newword(nw)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int nw;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct wdblock *wb;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *));
|
2001-06-29 04:57:14 +00:00
|
|
|
wb->w_bsize = nw;
|
|
|
|
wb->w_nword = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct wdblock *addword(wd, wb)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *wd;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct wdblock *wb;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct wdblock *wb2;
|
|
|
|
REGISTER int nw;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (wb == NULL)
|
|
|
|
wb = newword(NSTART);
|
|
|
|
if ((nw = wb->w_nword) >= wb->w_bsize) {
|
|
|
|
wb2 = newword(nw * 2);
|
2004-08-04 19:16:54 +00:00
|
|
|
memcpy((char *) wb2->w_words, (char *) wb->w_words,
|
|
|
|
nw * sizeof(char *));
|
2001-06-29 04:57:14 +00:00
|
|
|
wb2->w_nword = nw;
|
|
|
|
DELETE(wb);
|
|
|
|
wb = wb2;
|
|
|
|
}
|
|
|
|
wb->w_words[wb->w_nword++] = wd;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (wb);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
static
|
2004-08-04 19:16:54 +00:00
|
|
|
char **getwords(wb)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct wdblock *wb;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char **wd;
|
|
|
|
REGISTER int nb;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (wb == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char **) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (wb->w_nword == 0) {
|
|
|
|
DELETE(wb);
|
2004-08-04 19:16:54 +00:00
|
|
|
return ((char **) NULL);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
|
2004-08-04 19:16:54 +00:00
|
|
|
memcpy((char *) wd, (char *) wb->w_words, nb);
|
|
|
|
DELETE(wb); /* perhaps should done by caller */
|
|
|
|
return (wd);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2005-09-22 14:38:17 +00:00
|
|
|
static int (*func) (char *, char *);
|
|
|
|
static int globv;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void glob0(a0, a1, a2, a3)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *a0;
|
|
|
|
unsigned a1;
|
|
|
|
int a2;
|
|
|
|
int (*a3) (char *, char *);
|
|
|
|
{
|
|
|
|
func = a3;
|
|
|
|
globv = a2;
|
|
|
|
glob1(a0, a0 + a1 * a2);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void glob1(base, lim)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *base, *lim;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *i, *j;
|
2001-06-29 04:57:14 +00:00
|
|
|
int v2;
|
|
|
|
char *lptr, *hptr;
|
|
|
|
int c;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
|
|
|
|
v2 = globv;
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
top:
|
|
|
|
if ((n = (int) (lim - base)) <= v2)
|
2001-06-29 04:57:14 +00:00
|
|
|
return;
|
2004-08-04 19:16:54 +00:00
|
|
|
n = v2 * (n / (2 * v2));
|
|
|
|
hptr = lptr = base + n;
|
2001-06-29 04:57:14 +00:00
|
|
|
i = base;
|
2004-08-04 19:16:54 +00:00
|
|
|
j = lim - v2;
|
|
|
|
for (;;) {
|
2001-06-29 04:57:14 +00:00
|
|
|
if (i < lptr) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((c = (*func) (i, lptr)) == 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
glob2(i, lptr -= v2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c < 0) {
|
|
|
|
i += v2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
begin:
|
2001-06-29 04:57:14 +00:00
|
|
|
if (j > hptr) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((c = (*func) (hptr, j)) == 0) {
|
2001-06-29 04:57:14 +00:00
|
|
|
glob2(hptr += v2, j);
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
if (c > 0) {
|
|
|
|
if (i == lptr) {
|
|
|
|
glob3(i, hptr += v2, j);
|
|
|
|
i = lptr += v2;
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
glob2(i, j);
|
|
|
|
j -= v2;
|
|
|
|
i += v2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
j -= v2;
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (i == lptr) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (lptr - base >= lim - hptr) {
|
|
|
|
glob1(hptr + v2, lim);
|
2001-06-29 04:57:14 +00:00
|
|
|
lim = lptr;
|
|
|
|
} else {
|
|
|
|
glob1(base, lptr);
|
2004-08-04 19:16:54 +00:00
|
|
|
base = hptr + v2;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
goto top;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glob3(j, lptr -= v2, i);
|
|
|
|
j = hptr -= v2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void glob2(i, j)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *i, *j;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *index1, *index2, c;
|
2001-06-29 04:57:14 +00:00
|
|
|
int m;
|
|
|
|
|
|
|
|
m = globv;
|
|
|
|
index1 = i;
|
|
|
|
index2 = j;
|
|
|
|
do {
|
|
|
|
c = *index1;
|
|
|
|
*index1++ = *index2;
|
|
|
|
*index2++ = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
} while (--m);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void glob3(i, j, k)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *i, *j, *k;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *index1, *index2, *index3;
|
2001-06-29 04:57:14 +00:00
|
|
|
int c;
|
|
|
|
int m;
|
|
|
|
|
|
|
|
m = globv;
|
|
|
|
index1 = i;
|
|
|
|
index2 = j;
|
|
|
|
index3 = k;
|
|
|
|
do {
|
|
|
|
c = *index1;
|
|
|
|
*index1++ = *index3;
|
|
|
|
*index3++ = *index2;
|
|
|
|
*index2++ = c;
|
2004-08-04 19:16:54 +00:00
|
|
|
} while (--m);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- io.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shell IO
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int my_getc(int ec)
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (e.linep > elinep) {
|
|
|
|
while ((c = readc()) != '\n' && c);
|
2001-06-29 04:57:14 +00:00
|
|
|
err("input line too long");
|
|
|
|
gflg++;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
c = readc();
|
2003-03-14 16:05:59 +00:00
|
|
|
if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (c == '\\') {
|
2001-06-29 04:57:14 +00:00
|
|
|
c = readc();
|
|
|
|
if (c == '\n' && ec != '\"')
|
2004-08-04 19:16:54 +00:00
|
|
|
return (my_getc(ec));
|
2001-06-29 04:57:14 +00:00
|
|
|
c |= QUOTE;
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void unget(c)
|
2001-06-29 04:57:14 +00:00
|
|
|
int c;
|
|
|
|
{
|
|
|
|
if (e.iop >= e.iobase)
|
|
|
|
e.iop->peekc = c;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int eofc()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:16:54 +00:00
|
|
|
return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int readc()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
RCPRINTF(("READC: e.iop 0x%x, e.iobase 0x%x\n", e.iop, e.iobase));
|
|
|
|
|
|
|
|
for (; e.iop >= e.iobase; e.iop--) {
|
|
|
|
RCPRINTF(("READC: e.iop 0x%x, peekc 0x%x\n", e.iop, e.iop->peekc));
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((c = e.iop->peekc) != '\0') {
|
|
|
|
e.iop->peekc = 0;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
|
|
|
} else {
|
|
|
|
if (e.iop->prev != 0) {
|
|
|
|
if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') {
|
|
|
|
if (c == -1) {
|
|
|
|
e.iop++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (e.iop == iostack)
|
|
|
|
ioecho(c);
|
|
|
|
return (e.iop->prev = c);
|
|
|
|
} else if (e.iop->task == XIO && e.iop->prev != '\n') {
|
|
|
|
e.iop->prev = 0;
|
|
|
|
if (e.iop == iostack)
|
|
|
|
ioecho('\n');
|
|
|
|
return '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e.iop->task == XIO) {
|
2004-08-04 19:19:10 +00:00
|
|
|
if (multiline) {
|
2004-08-04 19:16:54 +00:00
|
|
|
return e.iop->prev = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
if (interactive && e.iop == iostack + 1) {
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
current_prompt = prompt->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
#else
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(prompt->value);
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
2004-08-04 19:16:54 +00:00
|
|
|
}
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
} /* FOR */
|
|
|
|
|
|
|
|
if (e.iop >= iostack) {
|
|
|
|
RCPRINTF(("READC: return 0, e.iop 0x%x\n", e.iop));
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2004-08-04 19:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("READC: leave()...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
leave();
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/* NOTREACHED */
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void ioecho(c)
|
2001-06-29 04:57:14 +00:00
|
|
|
char c;
|
|
|
|
{
|
|
|
|
if (flag['v'])
|
|
|
|
write(2, &c, sizeof c);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF(("PUSHIO: argp 0x%x, argp->afid 0x%x, e.iop 0x%x\n", argp,
|
|
|
|
argp->afid, e.iop));
|
|
|
|
|
|
|
|
/* Set env ptr for io source to next array spot and check for array overflow */
|
2001-06-29 04:57:14 +00:00
|
|
|
if (++e.iop >= &iostack[NPUSH]) {
|
|
|
|
e.iop--;
|
|
|
|
err("Shell input nested too deeply");
|
|
|
|
gflg++;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
/* We did not overflow the NPUSH array spots so setup data structs */
|
|
|
|
|
|
|
|
e.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn; /* Store data source func ptr */
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (argp->afid != AFID_NOBUF)
|
2004-08-04 19:16:54 +00:00
|
|
|
e.iop->argp = argp;
|
2001-06-29 04:57:14 +00:00
|
|
|
else {
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */
|
|
|
|
*e.iop->argp = *argp; /* copy data from temp area into stack spot */
|
|
|
|
|
|
|
|
/* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */
|
|
|
|
|
|
|
|
if (e.iop == &iostack[0])
|
|
|
|
e.iop->argp->afbuf = &mainbuf;
|
|
|
|
else
|
|
|
|
e.iop->argp->afbuf = &sharedbuf;
|
|
|
|
|
|
|
|
/* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */
|
|
|
|
/* This line appears to be active when running scripts from command line */
|
|
|
|
if ((isatty(e.iop->argp->afile) == 0)
|
|
|
|
&& (e.iop == &iostack[0]
|
|
|
|
|| lseek(e.iop->argp->afile, 0L, 1) != -1)) {
|
|
|
|
if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */
|
|
|
|
bufid = AFID_ID; /* AFID_ID = 0 */
|
|
|
|
|
|
|
|
e.iop->argp->afid = bufid; /* assign buffer id */
|
2004-08-04 19:16:54 +00:00
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF(("PUSHIO: iostack 0x%x, e.iop 0x%x, afbuf 0x%x\n",
|
|
|
|
iostack, e.iop, e.iop->argp->afbuf));
|
|
|
|
DBGPRINTF(("PUSHIO: mbuf 0x%x, sbuf 0x%x, bid %d, e.iop 0x%x\n",
|
|
|
|
&mainbuf, &sharedbuf, bufid, e.iop));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
e.iop->prev = ~'\n';
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iop->peekc = 0;
|
|
|
|
e.iop->xchar = 0;
|
|
|
|
e.iop->nlcount = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (fn == filechar || fn == linechar)
|
|
|
|
e.iop->task = XIO;
|
2004-08-04 19:16:54 +00:00
|
|
|
else if (fn == (int (*)(struct ioarg *)) gravechar
|
|
|
|
|| fn == (int (*)(struct ioarg *)) qgravechar)
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iop->task = XGRAVE;
|
|
|
|
else
|
|
|
|
e.iop->task = XOTHER;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
return;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static struct io *setbase(ip)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct io *ip;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct io *xp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
xp = e.iobase;
|
|
|
|
e.iobase = ip;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (xp);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input generating functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce the characters of a string, then a newline, then EOF.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int nlchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (ap->aword == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((c = *ap->aword++) == 0) {
|
|
|
|
ap->aword = NULL;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ('\n');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a list of words, produce the characters
|
|
|
|
* in them, with a space after each word.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int wdchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char c;
|
|
|
|
REGISTER char **wl;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((wl = ap->awordlist) == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
if (*wl != NULL) {
|
|
|
|
if ((c = *(*wl)++) != 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c & 0177);
|
2001-06-29 04:57:14 +00:00
|
|
|
ap->awordlist++;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (' ');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
ap->awordlist = NULL;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ('\n');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters of a list of words,
|
|
|
|
* producing a space between them.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int dolchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *wp;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((wp = *ap->awordlist++) != NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar);
|
|
|
|
return (-1);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int xxchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (ap->aword == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
2001-06-29 04:57:14 +00:00
|
|
|
if ((c = *ap->aword++) == '\0') {
|
|
|
|
ap->aword = NULL;
|
2004-08-04 19:16:54 +00:00
|
|
|
return (' ');
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce the characters from a single word (string).
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int strchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (ap->aword == NULL || (c = *ap->aword++) == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce quoted characters from a single word (string).
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int qstrchar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (ap->aword == NULL || (c = *ap->aword++) == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (0);
|
|
|
|
return (c | QUOTE);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters from a file.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int filechar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
char c;
|
|
|
|
struct iobuf *bp = ap->afbuf;
|
|
|
|
|
|
|
|
if (ap->afid != AFID_NOBUF) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (i)
|
|
|
|
lseek(ap->afile, ap->afpos, 0);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if (i <= 0) {
|
|
|
|
closef(ap->afile);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
bp->id = ap->afid;
|
|
|
|
bp->ebufp = (bp->bufp = bp->buf) + i;
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
ap->afpos++;
|
|
|
|
return *bp->bufp++ & 0177;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2003-03-14 16:05:59 +00:00
|
|
|
if (interactive && isatty(ap->afile)) {
|
2004-08-04 19:16:54 +00:00
|
|
|
static char mycommand[BUFSIZ];
|
|
|
|
static int position = 0, size = 0;
|
|
|
|
|
|
|
|
while (size == 0 || position >= size) {
|
|
|
|
cmdedit_read_input(current_prompt, mycommand);
|
|
|
|
size = strlen(mycommand);
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
c = mycommand[position];
|
|
|
|
position++;
|
|
|
|
return (c);
|
2004-03-15 08:29:22 +00:00
|
|
|
} else
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2001-07-12 20:26:32 +00:00
|
|
|
i = safe_read(ap->afile, &c, sizeof(c));
|
2004-08-04 19:19:10 +00:00
|
|
|
return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters from a here temp file.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int herechar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
|
|
if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
|
|
|
|
close(ap->afile);
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
return (c);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters produced by a process (`...`).
|
|
|
|
* Quote them if required, and remove any trailing newline characters.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int gravechar(ap, iop)
|
2001-06-29 04:57:14 +00:00
|
|
|
struct ioarg *ap;
|
|
|
|
struct io *iop;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n')
|
2001-06-29 04:57:14 +00:00
|
|
|
c = ' ';
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int qgravechar(ap, iop)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct io *iop;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
|
|
|
|
|
|
|
DBGPRINTF3(("QGRAVECHAR: enter, ap=0x%x, iop=0x%x\n", ap, iop));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (iop->xchar) {
|
|
|
|
if (iop->nlcount) {
|
|
|
|
iop->nlcount--;
|
2004-08-04 19:16:54 +00:00
|
|
|
return ('\n' | QUOTE);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
c = iop->xchar;
|
|
|
|
iop->xchar = 0;
|
|
|
|
} else if ((c = filechar(ap)) == '\n') {
|
|
|
|
iop->nlcount = 1;
|
|
|
|
while ((c = filechar(ap)) == '\n')
|
|
|
|
iop->nlcount++;
|
|
|
|
iop->xchar = c;
|
|
|
|
if (c == 0)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
iop->nlcount--;
|
|
|
|
c = '\n';
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c != 0 ? c | QUOTE : 0);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a single command (usually the first line) from a file.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int linechar(ap)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct ioarg *ap;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((c = filechar(ap)) == '\n') {
|
|
|
|
if (!multiline) {
|
|
|
|
closef(ap->afile);
|
2004-08-04 19:16:54 +00:00
|
|
|
ap->afile = -1; /* illegal value */
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
return (c);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void prs(s)
|
2005-10-17 09:48:57 +00:00
|
|
|
REGISTER const char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
if (*s)
|
|
|
|
write(2, s, strlen(s));
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void prn(u)
|
2001-06-29 04:57:14 +00:00
|
|
|
unsigned u;
|
|
|
|
{
|
2003-03-14 16:05:59 +00:00
|
|
|
prs(itoa(u));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void closef(i)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
if (i > 2)
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void closeall()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int u;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
for (u = NUFILE; u < NOFILE;)
|
2001-06-29 04:57:14 +00:00
|
|
|
close(u++);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
/*
|
|
|
|
* remap fd into Shell's fd space
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int remap(fd)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int fd;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
int map[NOFILE];
|
2004-08-04 19:19:10 +00:00
|
|
|
int newfd;
|
|
|
|
|
|
|
|
|
|
|
|
DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if (fd < e.iofd) {
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < NOFILE; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
map[i] = 0;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
do {
|
|
|
|
map[fd] = 1;
|
2004-08-04 19:19:10 +00:00
|
|
|
newfd = dup(fd);
|
|
|
|
fd = newfd;
|
2001-06-29 04:57:14 +00:00
|
|
|
} while (fd >= 0 && fd < e.iofd);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
for (i = 0; i < NOFILE; i++)
|
2001-06-29 04:57:14 +00:00
|
|
|
if (map[i])
|
|
|
|
close(i);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (fd < 0)
|
|
|
|
err("too many files open in shell");
|
|
|
|
}
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
return (fd);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static int openpipe(pv)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int *pv;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int i;
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
if ((i = pipe(pv)) < 0)
|
|
|
|
err("can't create pipe - try again");
|
2004-08-04 19:16:54 +00:00
|
|
|
return (i);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void closepipe(pv)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int *pv;
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
|
|
|
if (pv != NULL) {
|
|
|
|
close(*pv++);
|
|
|
|
close(*pv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- here.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* here documents
|
|
|
|
*/
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void markhere(s, iop)
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
struct ioword *iop;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct here *h, *lh;
|
|
|
|
|
|
|
|
DBGPRINTF7(("MARKHERE: enter, s=0x%x\n", s));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
h = (struct here *) space(sizeof(struct here));
|
|
|
|
if (h == 0)
|
|
|
|
return;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
h->h_tag = evalstr(s, DOSUB);
|
|
|
|
if (h->h_tag == 0)
|
|
|
|
return;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
h->h_iop = iop;
|
|
|
|
iop->io_name = 0;
|
|
|
|
h->h_next = NULL;
|
|
|
|
if (inhere == 0)
|
|
|
|
inhere = h;
|
|
|
|
else
|
2004-08-04 19:16:54 +00:00
|
|
|
for (lh = inhere; lh != NULL; lh = lh->h_next)
|
2001-06-29 04:57:14 +00:00
|
|
|
if (lh->h_next == 0) {
|
|
|
|
lh->h_next = h;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-04 19:16:54 +00:00
|
|
|
iop->io_flag |= IOHERE | IOXHERE;
|
2001-06-29 04:57:14 +00:00
|
|
|
for (s = h->h_tag; *s; s++)
|
|
|
|
if (*s & QUOTE) {
|
2004-08-04 19:16:54 +00:00
|
|
|
iop->io_flag &= ~IOXHERE;
|
|
|
|
*s &= ~QUOTE;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
h->h_dosub = iop->io_flag & IOXHERE;
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void gethere()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct here *h, *hp;
|
|
|
|
|
|
|
|
DBGPRINTF7(("GETHERE: enter...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/* Scan here files first leaving inhere list in place */
|
|
|
|
for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
|
2004-08-04 19:16:54 +00:00
|
|
|
readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\'');
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
/* Make inhere list active - keep list intact for scraphere */
|
|
|
|
if (hp != NULL) {
|
2004-08-04 19:16:54 +00:00
|
|
|
hp->h_next = acthere;
|
|
|
|
acthere = inhere;
|
|
|
|
inhere = NULL;
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void readhere(name, s, ec)
|
2001-06-29 04:57:14 +00:00
|
|
|
char **name;
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER char *s;
|
2001-06-29 04:57:14 +00:00
|
|
|
int ec;
|
|
|
|
{
|
|
|
|
int tf;
|
|
|
|
char tname[30] = ".msh_XXXXXX";
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int c;
|
2001-06-29 04:57:14 +00:00
|
|
|
jmp_buf ev;
|
2004-08-04 19:16:54 +00:00
|
|
|
char myline[LINELIM + 1];
|
2001-06-29 04:57:14 +00:00
|
|
|
char *thenext;
|
|
|
|
|
2004-08-04 19:19:10 +00:00
|
|
|
DBGPRINTF7(("READHERE: enter, name=0x%x, s=0x%x\n", name, s));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
tf = mkstemp(tname);
|
|
|
|
if (tf < 0)
|
|
|
|
return;
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
*name = strsave(tname, areanum);
|
|
|
|
if (newenv(setjmp(errpt = ev)) != 0)
|
|
|
|
unlink(tname);
|
|
|
|
else {
|
2004-08-04 19:16:54 +00:00
|
|
|
pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn);
|
2001-06-29 04:57:14 +00:00
|
|
|
e.iobase = e.iop;
|
|
|
|
for (;;) {
|
2004-08-04 19:16:54 +00:00
|
|
|
if (interactive && e.iop <= iostack) {
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2004-08-04 19:16:54 +00:00
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 04:57:14 +00:00
|
|
|
#else
|
2004-08-04 19:16:54 +00:00
|
|
|
prs(cprompt->value);
|
2001-06-29 04:57:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
thenext = myline;
|
|
|
|
while ((c = my_getc(ec)) != '\n' && c) {
|
|
|
|
if (ec == '\'')
|
2004-08-04 19:16:54 +00:00
|
|
|
c &= ~QUOTE;
|
2001-06-29 04:57:14 +00:00
|
|
|
if (thenext >= &myline[LINELIM]) {
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*thenext++ = c;
|
|
|
|
}
|
|
|
|
*thenext = 0;
|
|
|
|
if (strcmp(s, myline) == 0 || c == 0)
|
|
|
|
break;
|
|
|
|
*thenext++ = '\n';
|
2004-08-04 19:16:54 +00:00
|
|
|
write(tf, myline, (int) (thenext - myline));
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
if (c == 0) {
|
2004-08-04 19:16:54 +00:00
|
|
|
prs("here document `");
|
|
|
|
prs(s);
|
|
|
|
err("' unclosed");
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
}
|
|
|
|
close(tf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open here temp file.
|
|
|
|
* if unquoted here, expand here temp file into second temp file.
|
|
|
|
*/
|
2004-08-04 19:16:54 +00:00
|
|
|
static int herein(hname, xdoll)
|
2001-06-29 04:57:14 +00:00
|
|
|
char *hname;
|
|
|
|
int xdoll;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER int hf;
|
2001-06-29 04:57:14 +00:00
|
|
|
int tf;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &tf;
|
|
|
|
#endif
|
2004-08-04 19:19:10 +00:00
|
|
|
if (hname == NULL)
|
2004-08-04 19:16:54 +00:00
|
|
|
return (-1);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
|
|
|
DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
|
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
hf = open(hname, 0);
|
|
|
|
if (hf < 0)
|
|
|
|
return (-1);
|
2004-08-04 19:19:10 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
if (xdoll) {
|
|
|
|
char c;
|
|
|
|
char tname[30] = ".msh_XXXXXX";
|
|
|
|
jmp_buf ev;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2001-06-29 04:57:14 +00:00
|
|
|
tf = mkstemp(tname);
|
|
|
|
if (tf < 0)
|
|
|
|
return (-1);
|
|
|
|
if (newenv(setjmp(errpt = ev)) == 0) {
|
|
|
|
PUSHIO(afile, hf, herechar);
|
|
|
|
setbase(e.iop);
|
|
|
|
while ((c = subgetc(0, 0)) != 0) {
|
2004-08-04 19:16:54 +00:00
|
|
|
c &= ~QUOTE;
|
2001-06-29 04:57:14 +00:00
|
|
|
write(tf, &c, sizeof c);
|
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
unlink(tname);
|
|
|
|
close(tf);
|
|
|
|
tf = open(tname, 0);
|
|
|
|
unlink(tname);
|
|
|
|
return (tf);
|
|
|
|
} else
|
|
|
|
return (hf);
|
|
|
|
}
|
|
|
|
|
2004-08-04 19:16:54 +00:00
|
|
|
static void scraphere()
|
2001-06-29 04:57:14 +00:00
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct here *h;
|
|
|
|
|
|
|
|
DBGPRINTF7(("SCRAPHERE: enter...\n"));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
for (h = inhere; h != NULL; h = h->h_next) {
|
|
|
|
if (h->h_iop && h->h_iop->io_name)
|
2004-08-04 19:16:54 +00:00
|
|
|
unlink(h->h_iop->io_name);
|
2001-06-29 04:57:14 +00:00
|
|
|
}
|
|
|
|
inhere = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlink here temp files before a freearea(area) */
|
2004-08-04 19:16:54 +00:00
|
|
|
static void freehere(area)
|
2001-06-29 04:57:14 +00:00
|
|
|
int area;
|
|
|
|
{
|
2004-08-04 19:19:10 +00:00
|
|
|
REGISTER struct here *h, *hl;
|
|
|
|
|
|
|
|
DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
|
2001-06-29 04:57:14 +00:00
|
|
|
|
|
|
|
hl = NULL;
|
|
|
|
for (h = acthere; h != NULL; h = h->h_next)
|
|
|
|
if (getarea((char *) h) >= area) {
|
|
|
|
if (h->h_iop->io_name != NULL)
|
|
|
|
unlink(h->h_iop->io_name);
|
|
|
|
if (hl == NULL)
|
|
|
|
acthere = h->h_next;
|
|
|
|
else
|
|
|
|
hl->h_next = h->h_next;
|
|
|
|
} else
|
|
|
|
hl = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1987,1997, Prentice Hall
|
|
|
|
* All rights reserved.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* Redistribution and use of the MINIX operating system in source and
|
|
|
|
* binary forms, with or without modification, are permitted provided
|
|
|
|
* that the following conditions are met:
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* Neither the name of Prentice Hall nor the names of the software
|
|
|
|
* authors or contributors may be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2001-06-29 04:57:14 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
|
|
|
|
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|