2001-06-29 10:27:14 +05:30
|
|
|
/* 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-15 02:51:08 +05:30
|
|
|
* by Erik Andersen <andersen@codepoet.org>
|
2001-06-29 10:27:14 +05:30
|
|
|
*
|
2003-03-14 21:35:59 +05:30
|
|
|
* - backtick expansion did not work properly
|
|
|
|
* Jonas Holmberg <jonas.holmberg@axis.com>
|
|
|
|
* Robert Schwebel <r.schwebel@pengutronix.de>
|
2003-07-15 02:51:08 +05:30
|
|
|
* Erik Andersen <andersen@codepoet.org>
|
2003-03-14 21:35:59 +05:30
|
|
|
*
|
2006-07-13 00:47:55 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-06-29 10:27:14 +05:30
|
|
|
*/
|
|
|
|
|
2007-04-09 08:36:34 +05:30
|
|
|
#include <sys/times.h>
|
|
|
|
#include <setjmp.h>
|
2007-03-25 04:12:29 +05:30
|
|
|
|
2007-03-09 13:55:24 +05:30
|
|
|
#ifdef STANDALONE
|
|
|
|
# ifndef _GNU_SOURCE
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
# endif
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <dirent.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <ctype.h>
|
|
|
|
# include <assert.h>
|
|
|
|
# define bb_dev_null "/dev/null"
|
|
|
|
# define DEFAULT_SHELL "/proc/self/exe"
|
|
|
|
# define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe"
|
2007-06-13 17:57:17 +05:30
|
|
|
# define bb_banner "busybox standalone"
|
2007-04-11 04:33:30 +05:30
|
|
|
# define ENABLE_FEATURE_SH_STANDALONE 0
|
2007-03-09 13:55:24 +05:30
|
|
|
# define bb_msg_memory_exhausted "memory exhausted"
|
|
|
|
# define xmalloc(size) malloc(size)
|
|
|
|
# define msh_main(argc,argv) main(argc,argv)
|
|
|
|
# define safe_read(fd,buf,count) read(fd,buf,count)
|
2008-02-21 03:53:24 +05:30
|
|
|
# define nonblock_safe_read(fd,buf,count) read(fd,buf,count)
|
2007-03-09 13:55:24 +05:30
|
|
|
# define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
|
|
|
|
# define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
|
|
|
|
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
|
2007-11-28 12:19:03 +05:30
|
|
|
static int find_applet_by_name(const char *applet)
|
2007-03-09 13:55:24 +05:30
|
|
|
{
|
2007-11-28 12:19:03 +05:30
|
|
|
return -1;
|
2007-03-09 13:55:24 +05:30
|
|
|
}
|
2007-03-28 03:31:31 +05:30
|
|
|
static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
|
2007-03-09 13:55:24 +05:30
|
|
|
{
|
2007-03-20 17:00:28 +05:30
|
|
|
unsigned i, out, res;
|
|
|
|
assert(sizeof(unsigned) == 4);
|
|
|
|
if (buflen) {
|
|
|
|
out = 0;
|
|
|
|
for (i = 1000000000; i; i /= 10) {
|
|
|
|
res = n / i;
|
|
|
|
if (res || out || i == 1) {
|
2007-05-30 05:59:55 +05:30
|
|
|
if (!--buflen) break;
|
|
|
|
out++;
|
|
|
|
n -= res*i;
|
|
|
|
*buf++ = '0' + res;
|
2007-03-20 17:00:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-28 03:31:31 +05:30
|
|
|
return buf;
|
2007-03-09 13:55:24 +05:30
|
|
|
}
|
2007-03-28 03:31:31 +05:30
|
|
|
static char *itoa_to_buf(int n, char *buf, unsigned buflen)
|
2007-03-09 13:55:24 +05:30
|
|
|
{
|
2007-03-20 17:00:28 +05:30
|
|
|
if (buflen && n < 0) {
|
|
|
|
n = -n;
|
|
|
|
*buf++ = '-';
|
|
|
|
buflen--;
|
|
|
|
}
|
2007-03-28 03:31:31 +05:30
|
|
|
return utoa_to_buf((unsigned)n, buf, buflen);
|
2007-03-09 13:55:24 +05:30
|
|
|
}
|
|
|
|
static char local_buf[12];
|
|
|
|
static char *itoa(int n)
|
|
|
|
{
|
2007-03-28 03:31:31 +05:30
|
|
|
*(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
|
2007-03-20 17:00:28 +05:30
|
|
|
return local_buf;
|
2007-03-09 13:55:24 +05:30
|
|
|
}
|
|
|
|
#else
|
2007-11-28 12:19:03 +05:30
|
|
|
# include "busybox.h" /* for applet_names */
|
2007-03-09 13:55:24 +05:30
|
|
|
#endif
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
//#define MSHDEBUG 4
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
2008-03-02 18:19:39 +05:30
|
|
|
static int mshdbg = MSHDEBUG;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-04-12 06:02:05 +05:30
|
|
|
#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
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
static int mshdbg_rc = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-04-12 06:02:05 +05:30
|
|
|
#define RCPRINTF(x) if (mshdbg_rc) printf x
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DBGPRINTF(x)
|
2007-02-01 07:09:24 +05:30
|
|
|
#define DBGPRINTF0(x) ((void)0)
|
|
|
|
#define DBGPRINTF1(x) ((void)0)
|
|
|
|
#define DBGPRINTF2(x) ((void)0)
|
|
|
|
#define DBGPRINTF3(x) ((void)0)
|
|
|
|
#define DBGPRINTF4(x) ((void)0)
|
|
|
|
#define DBGPRINTF5(x) ((void)0)
|
|
|
|
#define DBGPRINTF6(x) ((void)0)
|
|
|
|
#define DBGPRINTF7(x) ((void)0)
|
|
|
|
#define DBGPRINTF8(x) ((void)0)
|
|
|
|
#define DBGPRINTF9(x) ((void)0)
|
|
|
|
|
|
|
|
#define RCPRINTF(x) ((void)0)
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
#endif /* MSHDEBUG */
|
|
|
|
|
|
|
|
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
|
2006-06-06 11:56:12 +05:30
|
|
|
# define DEFAULT_ROOT_PROMPT "\\u:\\w> "
|
|
|
|
# define DEFAULT_USER_PROMPT "\\u:\\w$ "
|
|
|
|
#else
|
|
|
|
# define DEFAULT_ROOT_PROMPT "# "
|
|
|
|
# define DEFAULT_USER_PROMPT "$ "
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- sh.h -------- */
|
|
|
|
/*
|
|
|
|
* shell
|
|
|
|
*/
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
#define LINELIM 2100
|
|
|
|
#define NPUSH 8 /* limit to input nesting */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2002-12-11 13:12:46 +05:30
|
|
|
#undef NOFILE
|
2004-08-05 00:49:10 +05:30
|
|
|
#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 10:27:14 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* values returned by wait
|
|
|
|
*/
|
2007-04-14 18:52:09 +05:30
|
|
|
#define WAITSIG(s) ((s) & 0177)
|
|
|
|
#define WAITVAL(s) (((s) >> 8) & 0377)
|
|
|
|
#define WAITCORE(s) (((s) & 0200) != 0)
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
/*
|
2004-04-14 23:21:38 +05:30
|
|
|
* library and system definitions
|
2001-06-29 10:27:14 +05:30
|
|
|
*/
|
2008-03-24 07:48:03 +05:30
|
|
|
typedef void xint; /* base type of jmp_buf, for not broken compilers */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* shell components
|
|
|
|
*/
|
|
|
|
#define NOBLOCK ((struct op *)NULL)
|
|
|
|
#define NOWORD ((char *)NULL)
|
|
|
|
#define NOWORDS ((char **)NULL)
|
|
|
|
#define NOPIPE ((int *)NULL)
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
/*
|
|
|
|
* redirection
|
|
|
|
*/
|
|
|
|
struct ioword {
|
2008-03-02 20:03:26 +05:30
|
|
|
smallint io_flag; /* action (below) */
|
|
|
|
int io_fd; /* fd affected */
|
2008-02-12 11:34:06 +05:30
|
|
|
char *io_name; /* file name */
|
2007-02-01 07:13:54 +05:30
|
|
|
};
|
|
|
|
|
2008-02-12 11:34:06 +05:30
|
|
|
#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 /* >&- */
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
#define IODEFAULT (-1) /* "default" IO fd */
|
2007-02-01 07:13:54 +05:30
|
|
|
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* Description of a command or an operation on commands.
|
|
|
|
* Might eventually use a union.
|
|
|
|
*/
|
|
|
|
struct op {
|
2008-03-02 20:03:26 +05:30
|
|
|
smallint op_type; /* operation type, see Txxxx below */
|
2008-03-02 18:19:39 +05:30
|
|
|
char **op_words; /* arguments to a command */
|
2008-02-12 11:34:06 +05:30
|
|
|
struct ioword **ioact; /* IO actions (eg, < > >>) */
|
2001-06-29 10:27:14 +05:30
|
|
|
struct op *left;
|
|
|
|
struct op *right;
|
2008-02-12 11:34:06 +05:30
|
|
|
char *str; /* identifier for case and for */
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
#define TCOM 1 /* command */
|
|
|
|
#define TPAREN 2 /* (c-list) */
|
|
|
|
#define TPIPE 3 /* a | b */
|
|
|
|
#define TLIST 4 /* a [&;] b */
|
|
|
|
#define TOR 5 /* || */
|
|
|
|
#define TAND 6 /* && */
|
|
|
|
#define TFOR 7
|
|
|
|
#define TDO 8
|
|
|
|
#define TCASE 9
|
|
|
|
#define TIF 10
|
|
|
|
#define TWHILE 11
|
|
|
|
#define TUNTIL 12
|
|
|
|
#define TELIF 13
|
|
|
|
#define TPAT 14 /* pattern in case */
|
|
|
|
#define TBRACE 15 /* {c-list} */
|
|
|
|
#define TASYNC 16 /* c & */
|
2004-08-05 00:49:10 +05:30
|
|
|
/* Added to support "." file expansion */
|
2007-12-16 22:51:29 +05:30
|
|
|
#define TDOT 17
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* Strings for names to make debug easier */
|
2005-09-22 20:08:17 +05:30
|
|
|
#ifdef MSHDEBUG
|
2007-01-01 11:30:38 +05:30
|
|
|
static const char *const T_CMD_NAMES[] = {
|
2004-08-05 00:49:10 +05:30
|
|
|
"PLACEHOLDER",
|
|
|
|
"TCOM",
|
|
|
|
"TPAREN",
|
|
|
|
"TPIPE",
|
|
|
|
"TLIST",
|
|
|
|
"TOR",
|
|
|
|
"TAND",
|
|
|
|
"TFOR",
|
|
|
|
"TDO",
|
|
|
|
"TCASE",
|
|
|
|
"TIF",
|
|
|
|
"TWHILE",
|
|
|
|
"TUNTIL",
|
|
|
|
"TELIF",
|
|
|
|
"TPAT",
|
|
|
|
"TBRACE",
|
|
|
|
"TASYNC",
|
|
|
|
"TDOT",
|
|
|
|
};
|
2005-09-22 20:08:17 +05:30
|
|
|
#endif
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-04-14 18:52:09 +05:30
|
|
|
#define AREASIZE (90000)
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* flags to control evaluation of words
|
|
|
|
*/
|
2007-04-14 18:52:09 +05:30
|
|
|
#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 10:27:14 +05:30
|
|
|
|
2007-04-14 18:52:09 +05:30
|
|
|
#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
|
|
|
|
struct brkcon {
|
|
|
|
jmp_buf brkpt;
|
|
|
|
struct brkcon *nextlev;
|
|
|
|
};
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-12 11:34:06 +05:30
|
|
|
static smallint trapset; /* trap pending (signal number) */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-02-12 11:34:06 +05:30
|
|
|
static smallint yynerrs; /* yacc (flag) */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-03-25 04:12:29 +05:30
|
|
|
/* moved to G: static char line[LINELIM]; */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
|
|
|
static char *current_prompt;
|
|
|
|
static line_input_t *line_input_state;
|
|
|
|
#endif
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* other functions
|
|
|
|
*/
|
2007-02-01 07:13:54 +05:30
|
|
|
static const char *rexecve(char *c, char **v, char **envp);
|
2004-08-05 00:46:54 +05:30
|
|
|
static char *evalstr(char *cp, int f);
|
|
|
|
static char *putn(int n);
|
|
|
|
static char *unquote(char *as);
|
|
|
|
static int rlookup(char *n);
|
|
|
|
static struct wdblock *glob(char *cp, struct wdblock *wb);
|
|
|
|
static int my_getc(int ec);
|
2006-06-26 03:38:53 +05:30
|
|
|
static int subgetc(char ec, int quoted);
|
2004-09-03 04:43:10 +05:30
|
|
|
static char **makenv(int all, struct wdblock *wb);
|
2004-08-05 00:46:54 +05:30
|
|
|
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 next(int f);
|
|
|
|
static void setdash(void);
|
|
|
|
static void onecommand(void);
|
|
|
|
static void runtrap(int i);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- area stuff -------- */
|
|
|
|
|
2007-04-14 18:52:09 +05:30
|
|
|
#define REGSIZE sizeof(struct region)
|
|
|
|
#define GROWBY (256)
|
|
|
|
/* #define SHRINKBY (64) */
|
|
|
|
#undef SHRINKBY
|
|
|
|
#define FREE (32767)
|
|
|
|
#define BUSY (0)
|
|
|
|
#define ALIGN (sizeof(int)-1)
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
|
|
|
|
struct region {
|
2004-08-05 00:46:54 +05:30
|
|
|
struct region *next;
|
|
|
|
int area;
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- grammar stuff -------- */
|
|
|
|
typedef union {
|
2004-08-05 00:46:54 +05:30
|
|
|
char *cp;
|
|
|
|
char **wp;
|
|
|
|
int i;
|
|
|
|
struct op *o;
|
2001-06-29 10:27:14 +05:30
|
|
|
} YYSTYPE;
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
#define WORD 256
|
|
|
|
#define LOGAND 257
|
|
|
|
#define LOGOR 258
|
|
|
|
#define BREAK 259
|
|
|
|
#define IF 260
|
|
|
|
#define THEN 261
|
|
|
|
#define ELSE 262
|
|
|
|
#define ELIF 263
|
|
|
|
#define FI 264
|
|
|
|
#define CASE 265
|
|
|
|
#define ESAC 266
|
|
|
|
#define FOR 267
|
|
|
|
#define WHILE 268
|
|
|
|
#define UNTIL 269
|
|
|
|
#define DO 270
|
|
|
|
#define DONE 271
|
|
|
|
#define IN 272
|
2004-08-05 00:49:10 +05:30
|
|
|
/* Added for "." file expansion */
|
2007-02-01 07:13:16 +05:30
|
|
|
#define DOT 273
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
#define YYERRCODE 300
|
|
|
|
|
|
|
|
/* flags to yylex */
|
2007-02-01 07:13:16 +05:30
|
|
|
#define CONTIN 01 /* skip new lines to complete command */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct op *pipeline(int cf);
|
2001-06-29 10:27:14 +05:30
|
|
|
static struct op *andor(void);
|
|
|
|
static struct op *c_list(void);
|
2004-08-05 00:46:54 +05:30
|
|
|
static int synio(int cf);
|
|
|
|
static void musthave(int c, int cf);
|
2001-06-29 10:27:14 +05:30
|
|
|
static struct op *simple(void);
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct op *nested(int type, int mark);
|
|
|
|
static struct op *command(int cf);
|
|
|
|
static struct op *dogroup(int onlydone);
|
2001-06-29 10:27:14 +05:30
|
|
|
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-05 00:46:54 +05:30
|
|
|
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 10:27:14 +05:30
|
|
|
static struct op *newtp(void);
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct op *namelist(struct op *t);
|
2001-06-29 10:27:14 +05:30
|
|
|
static char **copyw(void);
|
2004-08-05 00:46:54 +05:30
|
|
|
static void word(char *cp);
|
2001-06-29 10:27:14 +05:30
|
|
|
static struct ioword **copyio(void);
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct ioword *io(int u, int f, char *cp);
|
|
|
|
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 10:27:14 +05:30
|
|
|
|
|
|
|
/* -------- var.h -------- */
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
struct var {
|
|
|
|
char *value;
|
|
|
|
char *name;
|
|
|
|
struct var *next;
|
|
|
|
char status;
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
2004-08-05 00:46:54 +05:30
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- io.h -------- */
|
|
|
|
/* io buffer */
|
|
|
|
struct iobuf {
|
2007-02-01 07:13:54 +05:30
|
|
|
unsigned id; /* buffer id */
|
|
|
|
char buf[512]; /* buffer */
|
|
|
|
char *bufp; /* pointer into buffer */
|
|
|
|
char *ebufp; /* pointer to end of buffer */
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/* possible arguments to an IO function */
|
|
|
|
struct ioarg {
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *aword;
|
2004-08-05 00:46:54 +05:30
|
|
|
char **awordlist;
|
2007-02-01 07:13:54 +05:30
|
|
|
int afile; /* file descriptor */
|
|
|
|
unsigned afid; /* buffer id */
|
2008-02-12 11:34:06 +05:30
|
|
|
off_t afpos; /* file position */
|
2007-02-01 07:13:54 +05:30
|
|
|
struct iobuf *afbuf; /* buffer for this file */
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* an input generator's state */
|
2004-08-05 00:46:54 +05:30
|
|
|
struct io {
|
|
|
|
int (*iofn) (struct ioarg *, struct io *);
|
|
|
|
struct ioarg *argp;
|
|
|
|
int peekc;
|
2007-02-01 07:13:54 +05:30
|
|
|
char prev; /* previous character read by readc() */
|
|
|
|
char nlcount; /* for `'s */
|
|
|
|
char xchar; /* for `'s */
|
|
|
|
char task; /* reason for pushed IO */
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
2007-12-16 22:51:29 +05:30
|
|
|
/* ->task: */
|
|
|
|
#define XOTHER 0 /* none of the below */
|
|
|
|
#define XDOLL 1 /* expanding ${} */
|
|
|
|
#define XGRAVE 2 /* expanding `'s */
|
|
|
|
#define XIO 3 /* file IO */
|
2007-02-01 07:13:54 +05:30
|
|
|
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* input generators for IO structure
|
|
|
|
*/
|
2004-08-05 00:46:54 +05:30
|
|
|
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 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static int eofc(void);
|
|
|
|
static int readc(void);
|
|
|
|
static void unget(int c);
|
2006-06-26 03:38:53 +05:30
|
|
|
static void ioecho(char c);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* IO control
|
|
|
|
*/
|
2004-08-05 00:46:54 +05:30
|
|
|
static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
|
2007-03-25 04:12:29 +05:30
|
|
|
#define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen)))
|
2004-08-05 00:46:54 +05:30
|
|
|
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 10:27:14 +05:30
|
|
|
/* -------- word.h -------- */
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
#define NSTART 16 /* default number of words to allow for initially */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
struct wdblock {
|
|
|
|
short w_bsize;
|
|
|
|
short w_nword;
|
2001-06-29 10:27:14 +05:30
|
|
|
/* bounds are arbitrary */
|
2004-08-05 00:46:54 +05:30
|
|
|
char *w_words[1];
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct wdblock *addword(char *wd, struct wdblock *wb);
|
|
|
|
static struct wdblock *newword(int nw);
|
|
|
|
static char **getwords(struct wdblock *wb);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
/* -------- misc stuff -------- */
|
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
static int dolabel(struct op *t, char **args);
|
|
|
|
static int dohelp(struct op *t, char **args);
|
|
|
|
static int dochdir(struct op *t, char **args);
|
|
|
|
static int doshift(struct op *t, char **args);
|
|
|
|
static int dologin(struct op *t, char **args);
|
|
|
|
static int doumask(struct op *t, char **args);
|
|
|
|
static int doexec(struct op *t, char **args);
|
|
|
|
static int dodot(struct op *t, char **args);
|
|
|
|
static int dowait(struct op *t, char **args);
|
|
|
|
static int doread(struct op *t, char **args);
|
|
|
|
static int doeval(struct op *t, char **args);
|
|
|
|
static int dotrap(struct op *t, char **args);
|
|
|
|
static int dobreak(struct op *t, char **args);
|
|
|
|
static int doexit(struct op *t, char **args);
|
|
|
|
static int doexport(struct op *t, char **args);
|
|
|
|
static int doreadonly(struct op *t, char **args);
|
|
|
|
static int doset(struct op *t, char **args);
|
|
|
|
static int dotimes(struct op *t, char **args);
|
|
|
|
static int docontinue(struct op *t, char **args);
|
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp);
|
|
|
|
static int execute(struct op *t, int *pin, int *pout, int no_fork);
|
2004-08-05 00:46:54 +05:30
|
|
|
static int iosetup(struct ioword *iop, int pipein, int pipeout);
|
|
|
|
static void brkset(struct brkcon *bc);
|
|
|
|
static int getsig(char *s);
|
|
|
|
static void setsig(int n, sighandler_t f);
|
|
|
|
static int getn(char *as);
|
|
|
|
static int brkcontin(char *cp, int val);
|
|
|
|
static void rdexp(char **wp, void (*f) (struct var *), int key);
|
|
|
|
static void badid(char *s);
|
|
|
|
static void varput(char *s, int out);
|
2007-02-01 07:13:54 +05:30
|
|
|
static int expand(const char *cp, struct wdblock **wbp, int f);
|
2004-08-05 00:46:54 +05:30
|
|
|
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 void readhere(char **name, char *s, int ec);
|
|
|
|
static int xxchar(struct ioarg *ap);
|
|
|
|
|
|
|
|
struct here {
|
|
|
|
char *h_tag;
|
2008-02-12 11:34:06 +05:30
|
|
|
char h_dosub;
|
2004-08-05 00:46:54 +05:30
|
|
|
struct ioword *h_iop;
|
|
|
|
struct here *h_next;
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char *const signame[] = {
|
2001-06-29 10:27:14 +05:30
|
|
|
"Signal 0",
|
|
|
|
"Hangup",
|
2007-02-01 07:09:24 +05:30
|
|
|
NULL, /* interrupt */
|
2001-06-29 10:27:14 +05:30
|
|
|
"Quit",
|
|
|
|
"Illegal instruction",
|
|
|
|
"Trace/BPT trap",
|
|
|
|
"Abort",
|
|
|
|
"Bus error",
|
|
|
|
"Floating Point Exception",
|
|
|
|
"Killed",
|
|
|
|
"SIGUSR1",
|
|
|
|
"SIGSEGV",
|
|
|
|
"SIGUSR2",
|
2007-02-01 07:09:24 +05:30
|
|
|
NULL, /* broken pipe */
|
2001-06-29 10:27:14 +05:30
|
|
|
"Alarm clock",
|
2007-08-13 02:28:27 +05:30
|
|
|
"Terminated"
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2007-06-25 16:25:35 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
typedef int (*builtin_func_ptr)(struct op *, char **);
|
2008-02-12 15:21:03 +05:30
|
|
|
|
2001-07-07 05:35:55 +05:30
|
|
|
struct builtincmd {
|
|
|
|
const char *name;
|
2008-02-12 15:21:03 +05:30
|
|
|
builtin_func_ptr builtinfunc;
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
2008-02-12 15:21:03 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static const struct builtincmd builtincmds[] = {
|
2007-04-09 08:36:34 +05:30
|
|
|
{ "." , 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 },
|
|
|
|
{ NULL , NULL },
|
2001-06-29 10:27:14 +05:30
|
|
|
};
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static struct op *dowholefile(int /*, int*/);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* Globals */
|
2004-08-05 00:46:54 +05:30
|
|
|
static char **dolv;
|
|
|
|
static int dolc;
|
2008-05-31 03:58:32 +05:30
|
|
|
static uint8_t exstat;
|
2008-02-12 11:34:06 +05:30
|
|
|
static smallint gflg; /* (seems to be a parse error indicator) */
|
|
|
|
static smallint interactive; /* Is this an interactive shell */
|
|
|
|
static smallint execflg;
|
|
|
|
static smallint isbreak; /* "break" statement was seen */
|
|
|
|
static int multiline; /* '\n' changed to ';' (counter) */
|
|
|
|
static struct op *outtree; /* result from parser */
|
2004-08-05 00:46:54 +05:30
|
|
|
static xint *failpt;
|
|
|
|
static xint *errpt;
|
|
|
|
static struct brkcon *brklist;
|
|
|
|
static struct wdblock *wdlist;
|
|
|
|
static struct wdblock *iolist;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
static struct var *mshdbg_var;
|
|
|
|
#endif
|
2004-08-05 00:46:54 +05:30
|
|
|
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-05 00:49:10 +05:30
|
|
|
|
2007-04-09 08:36:34 +05:30
|
|
|
static int areanum; /* current allocation area */
|
2008-02-12 11:34:06 +05:30
|
|
|
static smallint intr; /* interrupt pending (bool) */
|
|
|
|
static smallint heedint = 1; /* heed interrupt signals (bool) */
|
2004-08-05 00:46:54 +05:30
|
|
|
static int inparse;
|
2007-04-09 08:36:34 +05:30
|
|
|
static char *null = (char*)""; /* null value for variable */
|
2007-04-17 04:04:39 +05:30
|
|
|
static void (*qflag)(int) = SIG_IGN;
|
2004-08-05 00:46:54 +05:30
|
|
|
static int startl;
|
|
|
|
static int peeksym;
|
|
|
|
static int nlseen;
|
|
|
|
static int iounit = IODEFAULT;
|
|
|
|
static YYSTYPE yylval;
|
2007-03-25 04:12:29 +05:30
|
|
|
static char *elinep; /* done in main(): = line + sizeof(line) - 5 */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
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 */
|
2004-08-05 00:46:54 +05:30
|
|
|
static void *brktop;
|
|
|
|
static void *brkaddr;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
#define AFID_NOBUF (~0)
|
|
|
|
#define AFID_ID 0
|
|
|
|
|
|
|
|
|
2007-03-25 04:12:29 +05:30
|
|
|
/*
|
|
|
|
* parsing & execution environment
|
|
|
|
*/
|
|
|
|
struct env {
|
|
|
|
char *linep;
|
|
|
|
struct io *iobase;
|
|
|
|
struct io *iop;
|
|
|
|
xint *errpt; /* void * */
|
|
|
|
int iofd;
|
|
|
|
struct env *oenv;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct globals {
|
2007-12-16 22:51:29 +05:30
|
|
|
struct env global_env;
|
|
|
|
struct ioarg temparg; // = { .afid = AFID_NOBUF }; /* temporary for PUSHIO */
|
|
|
|
unsigned bufid; // = AFID_ID; /* buffer id counter */
|
2007-03-25 04:12:29 +05:30
|
|
|
char ourtrap[_NSIG + 1];
|
|
|
|
char *trap[_NSIG + 1];
|
|
|
|
struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */
|
|
|
|
struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */
|
|
|
|
struct ioarg ioargstack[NPUSH];
|
2007-12-16 22:51:29 +05:30
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
char flags['z' - 'a' + 1];
|
2007-03-25 04:12:29 +05:30
|
|
|
char filechar_cmdbuf[BUFSIZ];
|
|
|
|
char line[LINELIM];
|
|
|
|
char child_cmd[LINELIM];
|
2007-12-02 14:05:37 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
struct io iostack[NPUSH];
|
|
|
|
|
2007-12-02 14:05:37 +05:30
|
|
|
char grave__var_name[LINELIM];
|
|
|
|
char grave__alt_value[LINELIM];
|
2007-03-25 04:12:29 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
#define G (*ptr_to_globals)
|
2007-12-16 22:51:29 +05:30
|
|
|
#define global_env (G.global_env )
|
|
|
|
#define temparg (G.temparg )
|
|
|
|
#define bufid (G.bufid )
|
2007-03-25 04:12:29 +05:30
|
|
|
#define ourtrap (G.ourtrap )
|
|
|
|
#define trap (G.trap )
|
|
|
|
#define sharedbuf (G.sharedbuf )
|
|
|
|
#define mainbuf (G.mainbuf )
|
|
|
|
#define ioargstack (G.ioargstack )
|
2007-12-16 22:51:29 +05:30
|
|
|
/* this looks weird, but is OK ... we index FLAG with 'a'...'z' */
|
|
|
|
#define FLAG (G.flags - 'a' )
|
2007-03-25 04:12:29 +05:30
|
|
|
#define filechar_cmdbuf (G.filechar_cmdbuf)
|
|
|
|
#define line (G.line )
|
|
|
|
#define child_cmd (G.child_cmd )
|
2007-12-16 22:51:29 +05:30
|
|
|
#define iostack (G.iostack )
|
2007-12-02 14:05:37 +05:30
|
|
|
#define INIT_G() do { \
|
2008-02-28 00:11:59 +05:30
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = line; \
|
|
|
|
global_env.iobase = iostack; \
|
|
|
|
global_env.iop = iostack - 1; \
|
|
|
|
global_env.iofd = FDBASE; \
|
|
|
|
temparg.afid = AFID_NOBUF; \
|
|
|
|
bufid = AFID_ID; \
|
2007-12-02 14:05:37 +05:30
|
|
|
} while (0)
|
2007-03-25 04:12:29 +05:30
|
|
|
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
/* in substitution */
|
|
|
|
#define INSUB() (global_env.iop->task == XGRAVE || global_env.iop->task == XDOLL)
|
|
|
|
|
|
|
|
#define RUN(what, arg, gen) ((temparg.what = (arg)), run(&temparg, (gen)))
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
#ifdef MSHDEBUG
|
2008-03-02 18:19:39 +05:30
|
|
|
static void print_tree(struct op *head)
|
2004-08-05 00:49:10 +05:30
|
|
|
{
|
|
|
|
if (head == NULL) {
|
|
|
|
DBGPRINTF(("PRINT_TREE: no tree\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left,
|
2004-08-05 00:49:10 +05:30
|
|
|
head->right));
|
|
|
|
|
|
|
|
if (head->left)
|
|
|
|
print_tree(head->left);
|
|
|
|
|
|
|
|
if (head->right)
|
|
|
|
print_tree(head->right);
|
|
|
|
}
|
2007-02-01 07:13:39 +05:30
|
|
|
#endif /* MSHDEBUG */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IO functions
|
|
|
|
*/
|
|
|
|
static void prs(const char *s)
|
|
|
|
{
|
|
|
|
if (*s)
|
2008-05-19 15:18:17 +05:30
|
|
|
write(STDERR_FILENO, s, strlen(s));
|
2007-02-01 07:13:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void prn(unsigned u)
|
|
|
|
{
|
|
|
|
prs(itoa(u));
|
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static void echo(char **wp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
prs("+");
|
|
|
|
for (i = 0; wp[i]; i++) {
|
|
|
|
if (i)
|
|
|
|
prs(" ");
|
|
|
|
prs(wp[i]);
|
|
|
|
}
|
|
|
|
prs("\n");
|
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:39 +05:30
|
|
|
static void closef(int i)
|
|
|
|
{
|
|
|
|
if (i > 2)
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void closeall(void)
|
|
|
|
{
|
|
|
|
int u;
|
|
|
|
|
|
|
|
for (u = NUFILE; u < NOFILE;)
|
|
|
|
close(u++);
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* fail but return to process next command */
|
2007-02-01 07:13:39 +05:30
|
|
|
static void fail(void) ATTRIBUTE_NORETURN;
|
2007-02-01 07:13:16 +05:30
|
|
|
static void fail(void)
|
|
|
|
{
|
|
|
|
longjmp(failpt, 1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* abort shell (or fail in subshell) */
|
|
|
|
static void leave(void) ATTRIBUTE_NORETURN;
|
|
|
|
static void leave(void)
|
|
|
|
{
|
|
|
|
DBGPRINTF(("LEAVE: leave called!\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
if (execflg)
|
|
|
|
fail();
|
|
|
|
scraphere();
|
|
|
|
freehere(1);
|
|
|
|
runtrap(0);
|
|
|
|
_exit(exstat);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void warn(const char *s)
|
|
|
|
{
|
|
|
|
if (*s) {
|
|
|
|
prs(s);
|
2008-05-31 03:58:32 +05:30
|
|
|
if (!exstat)
|
|
|
|
exstat = 255;
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
prs("\n");
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['e'])
|
2007-02-01 07:13:16 +05:30
|
|
|
leave();
|
|
|
|
}
|
2007-01-22 12:51:38 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void err(const char *s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
warn(s);
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['n'])
|
2007-02-01 07:13:16 +05:30
|
|
|
return;
|
|
|
|
if (!interactive)
|
|
|
|
leave();
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.errpt)
|
|
|
|
longjmp(global_env.errpt, 1);
|
2007-02-01 07:13:16 +05:30
|
|
|
closeall();
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop = global_env.iobase = iostack;
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:39 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* -------- area.c -------- */
|
2007-01-22 12:51:38 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
#define sbrk(X) ({ \
|
|
|
|
void * __q = (void *)-1; \
|
|
|
|
if (brkaddr + (int)(X) < brktop) { \
|
|
|
|
__q = brkaddr; \
|
|
|
|
brkaddr += (int)(X); \
|
|
|
|
} \
|
|
|
|
__q; \
|
|
|
|
})
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void initarea(void)
|
|
|
|
{
|
|
|
|
brkaddr = xmalloc(AREASIZE);
|
|
|
|
brktop = brkaddr + AREASIZE;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
while ((long) sbrk(0) & ALIGN)
|
|
|
|
sbrk(1);
|
|
|
|
areabot = (struct region *) sbrk(REGSIZE);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
areabot->next = areabot;
|
|
|
|
areabot->area = BUSY;
|
|
|
|
areatop = areabot;
|
|
|
|
areanxt = areabot;
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static char *getcell(unsigned nbytes)
|
|
|
|
{
|
|
|
|
int nregio;
|
|
|
|
struct region *p, *q;
|
|
|
|
int i;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
if (nbytes == 0) {
|
|
|
|
puts("getcell(0)");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
/* silly and defeats the algorithm */
|
|
|
|
/*
|
|
|
|
* round upwards and add administration area
|
|
|
|
*/
|
|
|
|
nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
|
|
|
|
p = areanxt;
|
|
|
|
for (;;) {
|
|
|
|
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;
|
|
|
|
p = (struct region *) sbrk(i * REGSIZE);
|
|
|
|
if (p == (struct region *) -1)
|
|
|
|
return NULL;
|
|
|
|
p--;
|
|
|
|
if (p != areatop) {
|
|
|
|
puts("not contig");
|
|
|
|
abort(); /* allocated areas are contiguous */
|
|
|
|
}
|
|
|
|
q = p + i;
|
|
|
|
p->next = q;
|
|
|
|
p->area = FREE;
|
|
|
|
q->next = areabot;
|
|
|
|
q->area = BUSY;
|
|
|
|
areatop = q;
|
|
|
|
found:
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
if (areanxt + 1 > q) {
|
|
|
|
puts("OOM");
|
|
|
|
abort(); /* insufficient space left for admin */
|
|
|
|
}
|
|
|
|
areanxt->next = q;
|
|
|
|
areanxt->area = FREE;
|
|
|
|
p->next = areanxt;
|
|
|
|
}
|
|
|
|
p->area = areanum;
|
|
|
|
return (char *) (p + 1);
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void freecell(char *cp)
|
|
|
|
{
|
|
|
|
struct region *p;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
p = (struct region *) cp;
|
|
|
|
if (p != NULL) {
|
|
|
|
p--;
|
|
|
|
if (p < areanxt)
|
|
|
|
areanxt = p;
|
|
|
|
p->area = FREE;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
#define DELETE(obj) freecell((char *)obj)
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void freearea(int a)
|
|
|
|
{
|
|
|
|
struct region *p, *top;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
top = areatop;
|
|
|
|
for (p = areabot; p != top; p = p->next)
|
|
|
|
if (p->area >= a)
|
|
|
|
p->area = FREE;
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void setarea(char *cp, int a)
|
|
|
|
{
|
|
|
|
struct region *p;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
p = (struct region *) cp;
|
|
|
|
if (p != NULL)
|
|
|
|
(p - 1)->area = a;
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static int getarea(char *cp)
|
|
|
|
{
|
|
|
|
return ((struct region *) cp - 1)->area;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void garbage(void)
|
|
|
|
{
|
|
|
|
struct region *p, *q, *top;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
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) {
|
|
|
|
brk((char *) (q + 1));
|
|
|
|
q->next = areabot;
|
|
|
|
q->area = BUSY;
|
|
|
|
areatop = q;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
#endif
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
static void *get_space(int n)
|
2007-02-01 07:13:16 +05:30
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = getcell(n);
|
2007-02-01 07:13:54 +05:30
|
|
|
if (cp == NULL)
|
2007-02-01 07:13:16 +05:30
|
|
|
err("out of string space");
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *strsave(const char *s, int a)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
cp = get_space(strlen(s) + 1);
|
2007-02-01 07:13:54 +05:30
|
|
|
if (cp == NULL) {
|
|
|
|
// FIXME: I highly doubt this is good.
|
|
|
|
return (char*)"";
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:54 +05:30
|
|
|
setarea(cp, a);
|
|
|
|
strcpy(cp, s);
|
|
|
|
return cp;
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:39 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* -------- var.c -------- */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static int eqname(const char *n1, const char *n2)
|
|
|
|
{
|
|
|
|
for (; *n1 != '=' && *n1 != '\0'; n1++)
|
|
|
|
if (*n2++ != *n1)
|
|
|
|
return 0;
|
|
|
|
return *n2 == '\0' || *n2 == '=';
|
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static const char *findeq(const char *cp)
|
2007-02-01 07:13:16 +05:30
|
|
|
{
|
|
|
|
while (*cp != '\0' && *cp != '=')
|
|
|
|
cp++;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static struct var *lookup(const char *n)
|
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
// FIXME: dirty hack
|
|
|
|
static struct var dummy;
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
struct var *vp;
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp;
|
|
|
|
char *xp;
|
2007-02-01 07:13:16 +05:30
|
|
|
int c;
|
|
|
|
|
|
|
|
if (isdigit(*n)) {
|
2007-02-01 07:13:54 +05:30
|
|
|
dummy.name = (char*)n;
|
2007-02-01 07:13:16 +05:30
|
|
|
for (c = 0; isdigit(*n) && c < 1000; n++)
|
|
|
|
c = c * 10 + *n - '0';
|
|
|
|
dummy.status = RONLY;
|
|
|
|
dummy.value = (c <= dolc ? dolv[c] : null);
|
|
|
|
return &dummy;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
|
|
|
if (eqname(vp->name, n))
|
|
|
|
return vp;
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
cp = findeq(n);
|
2008-03-02 18:19:39 +05:30
|
|
|
vp = get_space(sizeof(*vp));
|
|
|
|
if (vp == 0 || (vp->name = get_space((int) (cp - n) + 2)) == NULL) {
|
2007-02-01 07:13:54 +05:30
|
|
|
dummy.name = dummy.value = (char*)"";
|
2007-02-01 07:13:16 +05:30
|
|
|
return &dummy;
|
|
|
|
}
|
2007-02-01 07:13:54 +05:30
|
|
|
|
|
|
|
xp = vp->name;
|
|
|
|
while ((*xp = *n++) != '\0' && *xp != '=')
|
|
|
|
xp++;
|
|
|
|
*xp++ = '=';
|
|
|
|
*xp = '\0';
|
2007-02-01 07:13:16 +05:30
|
|
|
setarea((char *) vp, 0);
|
|
|
|
setarea((char *) vp->name, 0);
|
|
|
|
vp->value = null;
|
|
|
|
vp->next = vlist;
|
|
|
|
vp->status = GETCELL;
|
|
|
|
vlist = vp;
|
|
|
|
return vp;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static void nameval(struct var *vp, const char *val, const char *name)
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *xp;
|
|
|
|
int fl;
|
|
|
|
|
|
|
|
if (vp->status & RONLY) {
|
|
|
|
xp = vp->name;
|
|
|
|
while (*xp && *xp != '=')
|
2007-09-27 15:50:47 +05:30
|
|
|
fputc(*xp++, stderr);
|
2007-02-01 07:13:16 +05:30
|
|
|
err(" is read-only");
|
|
|
|
return;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:16 +05:30
|
|
|
fl = 0;
|
|
|
|
if (name == NULL) {
|
2008-03-02 18:19:39 +05:30
|
|
|
xp = get_space(strlen(vp->name) + strlen(val) + 2);
|
2007-02-01 07:13:16 +05:30
|
|
|
if (xp == NULL)
|
|
|
|
return;
|
|
|
|
/* make string: name=value */
|
|
|
|
setarea(xp, 0);
|
|
|
|
name = xp;
|
|
|
|
cp = vp->name;
|
|
|
|
while ((*xp = *cp++) != '\0' && *xp != '=')
|
|
|
|
xp++;
|
|
|
|
*xp++ = '=';
|
|
|
|
strcpy(xp, val);
|
|
|
|
val = xp;
|
|
|
|
fl = GETCELL;
|
|
|
|
}
|
|
|
|
if (vp->status & GETCELL)
|
|
|
|
freecell(vp->name); /* form new string `name=value' */
|
2007-02-01 07:13:54 +05:30
|
|
|
vp->name = (char*)name;
|
|
|
|
vp->value = (char*)val;
|
2007-02-01 07:13:16 +05:30
|
|
|
vp->status |= fl;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/*
|
|
|
|
* give variable at `vp' the value `val'.
|
|
|
|
*/
|
|
|
|
static void setval(struct var *vp, const char *val)
|
|
|
|
{
|
|
|
|
nameval(vp, val, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void export(struct var *vp)
|
|
|
|
{
|
|
|
|
vp->status |= EXPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ronly(struct var *vp)
|
|
|
|
{
|
|
|
|
if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
|
|
|
|
vp->status |= RONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int isassign(const char *s)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s));
|
|
|
|
|
|
|
|
c = *s;
|
2007-03-24 19:36:51 +05:30
|
|
|
/* no isalpha() - we shouldn't use locale */
|
|
|
|
/* c | 0x20 - lowercase (Latin) letters */
|
|
|
|
if (c != '_' && (unsigned)((c|0x20) - 'a') > 25)
|
|
|
|
/* not letter */
|
2007-02-01 07:13:16 +05:30
|
|
|
return 0;
|
2007-03-24 19:36:51 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
while (1) {
|
|
|
|
c = *++s;
|
|
|
|
if (c == '=')
|
|
|
|
return 1;
|
2007-03-24 19:36:51 +05:30
|
|
|
if (c == '\0')
|
|
|
|
return 0;
|
2007-02-01 07:13:16 +05:30
|
|
|
if (c != '_'
|
|
|
|
&& (unsigned)(c - '0') > 9 /* not number */
|
2007-03-24 19:36:51 +05:30
|
|
|
&& (unsigned)((c|0x20) - 'a') > 25 /* not letter */
|
2007-02-01 07:13:16 +05:30
|
|
|
) {
|
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static int assign(const char *s, int cf)
|
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp;
|
2007-02-01 07:13:16 +05:30
|
|
|
struct var *vp;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf));
|
|
|
|
|
|
|
|
if (!isalpha(*s) && *s != '_')
|
|
|
|
return 0;
|
|
|
|
for (cp = s; *cp != '='; cp++)
|
|
|
|
if (*cp == '\0' || (!isalnum(*cp) && *cp != '_'))
|
|
|
|
return 0;
|
|
|
|
vp = lookup(s);
|
|
|
|
nameval(vp, ++cp, cf == COPYV ? NULL : s);
|
|
|
|
if (cf != COPYV)
|
|
|
|
vp->status &= ~GETCELL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int checkname(char *cp)
|
|
|
|
{
|
|
|
|
DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp));
|
|
|
|
|
|
|
|
if (!isalpha(*cp++) && *(cp - 1) != '_')
|
|
|
|
return 0;
|
|
|
|
while (*cp)
|
|
|
|
if (!isalnum(*cp++) && *(cp - 1) != '_')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void putvlist(int f, int out)
|
|
|
|
{
|
|
|
|
struct var *vp;
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
for (vp = vlist; vp; vp = vp->next) {
|
2007-02-01 07:13:16 +05:30
|
|
|
if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
|
|
|
|
if (vp->status & EXPORT)
|
|
|
|
write(out, "export ", 7);
|
|
|
|
if (vp->status & RONLY)
|
|
|
|
write(out, "readonly ", 9);
|
|
|
|
write(out, vp->name, (int) (findeq(vp->name) - vp->name));
|
|
|
|
write(out, "\n", 1);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:54 +05:30
|
|
|
}
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trap handling
|
|
|
|
*/
|
|
|
|
static void sig(int i)
|
|
|
|
{
|
|
|
|
trapset = i;
|
|
|
|
signal(i, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runtrap(int i)
|
|
|
|
{
|
|
|
|
char *trapstr;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
trapstr = trap[i];
|
|
|
|
if (trapstr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
trap[i] = NULL;
|
|
|
|
|
|
|
|
RUN(aword, trapstr, nlchar);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void setdash(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *cp;
|
|
|
|
int c;
|
2004-08-05 00:46:54 +05:30
|
|
|
char m['z' - 'a' + 1];
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
cp = m;
|
2004-08-05 00:46:54 +05:30
|
|
|
for (c = 'a'; c <= 'z'; c++)
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG[c])
|
2001-06-29 10:27:14 +05:30
|
|
|
*cp++ = c;
|
2007-02-01 07:09:24 +05:30
|
|
|
*cp = '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
setval(lookup("-"), m);
|
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int newfile(char *s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int f;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("NEWFILE: opening %s\n", s));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
f = 0;
|
2006-12-17 05:19:13 +05:30
|
|
|
if (NOT_LONE_DASH(s)) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF(("NEWFILE: s is %s\n", s));
|
2007-04-14 18:52:09 +05:30
|
|
|
f = open(s, O_RDONLY);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (f < 0) {
|
|
|
|
prs(s);
|
|
|
|
err(": cannot open");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
next(remap(f));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-04-15 06:47:50 +05:30
|
|
|
#ifdef UNUSED
|
2006-06-26 03:38:53 +05:30
|
|
|
struct op *scantree(struct op *head)
|
2004-08-05 00:49:10 +05:30
|
|
|
{
|
|
|
|
struct op *dotnode;
|
|
|
|
|
|
|
|
if (head == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
if (head->left != NULL) {
|
|
|
|
dotnode = scantree(head->left);
|
|
|
|
if (dotnode)
|
2006-11-27 22:19:55 +05:30
|
|
|
return dotnode;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (head->right != NULL) {
|
|
|
|
dotnode = scantree(head->right);
|
|
|
|
if (dotnode)
|
2006-11-27 22:19:55 +05:30
|
|
|
return dotnode;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
if (head->op_words == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF5(("SCANTREE: checking node %p\n", head));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF5(("SCANTREE: dot found in node %p\n", head));
|
2006-11-27 22:19:55 +05:30
|
|
|
return head;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
2008-04-15 06:47:50 +05:30
|
|
|
#endif
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void onecommand(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2001-06-29 10:27:14 +05:30
|
|
|
jmp_buf m1;
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
while (global_env.oenv)
|
2001-06-29 10:27:14 +05:30
|
|
|
quitenv();
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
areanum = 1;
|
|
|
|
freehere(areanum);
|
|
|
|
freearea(areanum);
|
|
|
|
garbage();
|
2008-03-02 20:03:26 +05:30
|
|
|
wdlist = NULL;
|
|
|
|
iolist = NULL;
|
|
|
|
global_env.errpt = NULL;
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = line;
|
2001-06-29 10:27:14 +05:30
|
|
|
yynerrs = 0;
|
|
|
|
multiline = 0;
|
|
|
|
inparse = 1;
|
|
|
|
intr = 0;
|
|
|
|
execflg = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
failpt = m1;
|
|
|
|
setjmp(failpt); /* Bruce Evans' fix */
|
|
|
|
failpt = m1;
|
|
|
|
if (setjmp(failpt) || yyparse() || intr) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF(("ONECOMMAND: this is not good.\n"));
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
while (global_env.oenv)
|
2001-06-29 10:27:14 +05:30
|
|
|
quitenv();
|
|
|
|
scraphere();
|
|
|
|
if (!interactive && intr)
|
|
|
|
leave();
|
|
|
|
inparse = 0;
|
|
|
|
intr = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
inparse = 0;
|
|
|
|
brklist = 0;
|
|
|
|
intr = 0;
|
|
|
|
execflg = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-04-17 04:04:39 +05:30
|
|
|
if (!FLAG['n']) {
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n",
|
2004-08-05 00:49:10 +05:30
|
|
|
outtree));
|
2008-02-12 15:21:03 +05:30
|
|
|
execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0);
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (!interactive && intr) {
|
|
|
|
execflg = 0;
|
|
|
|
leave();
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
i = trapset;
|
|
|
|
if (i != 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
trapset = 0;
|
|
|
|
runtrap(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int newenv(int f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct env *ep;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (f) {
|
|
|
|
quitenv();
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
ep = get_space(sizeof(*ep));
|
2001-06-29 10:27:14 +05:30
|
|
|
if (ep == NULL) {
|
2007-12-16 22:51:29 +05:30
|
|
|
while (global_env.oenv)
|
2001-06-29 10:27:14 +05:30
|
|
|
quitenv();
|
|
|
|
fail();
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*ep = global_env;
|
|
|
|
global_env.oenv = ep;
|
|
|
|
global_env.errpt = errpt;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void quitenv(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
struct env *ep;
|
|
|
|
int fd;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("QUITENV: global_env.oenv=%p\n", global_env.oenv));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
ep = global_env.oenv;
|
2007-02-01 07:13:16 +05:30
|
|
|
if (ep != NULL) {
|
2007-12-16 22:51:29 +05:30
|
|
|
fd = global_env.iofd;
|
|
|
|
global_env = *ep;
|
2007-02-01 07:13:16 +05:30
|
|
|
/* should close `'d files */
|
|
|
|
DELETE(ep);
|
2007-12-16 22:51:29 +05:30
|
|
|
while (--fd >= global_env.iofd)
|
2007-02-01 07:13:16 +05:30
|
|
|
close(fd);
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/*
|
|
|
|
* Is character c in s?
|
|
|
|
*/
|
|
|
|
static int any(int c, const char *s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
while (*s)
|
|
|
|
if (*s++ == c)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/*
|
|
|
|
* Is any character from s1 in s2?
|
|
|
|
*/
|
|
|
|
static int anys(const char *s1, const char *s2)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
while (*s1)
|
|
|
|
if (any(*s1++, s2))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static char *putn(int n)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
return itoa(n);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
static void next(int f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
PUSHIO(afile, f, filechar);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static void onintr(int s ATTRIBUTE_UNUSED) /* ANSI C requires a parameter */
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:16 +05:30
|
|
|
signal(SIGINT, onintr);
|
|
|
|
intr = 1;
|
|
|
|
if (interactive) {
|
|
|
|
if (inparse) {
|
|
|
|
prs("\n");
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
} else if (heedint) {
|
|
|
|
execflg = 0;
|
|
|
|
leave();
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- gmatch.c -------- */
|
|
|
|
/*
|
|
|
|
* int gmatch(string, pattern)
|
|
|
|
* char *string, *pattern;
|
|
|
|
*
|
|
|
|
* Match a pattern as in sh(1).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CMASK 0377
|
|
|
|
#define QUOTE 0200
|
2007-03-25 04:12:29 +05:30
|
|
|
#define QMASK (CMASK & ~QUOTE)
|
2004-08-05 00:46:54 +05:30
|
|
|
#define NOT '!' /* might use ^ */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static const char *cclass(const char *p, int sub)
|
|
|
|
{
|
|
|
|
int c, d, not, found;
|
|
|
|
|
|
|
|
not = (*p == NOT);
|
|
|
|
if (not != 0)
|
|
|
|
p++;
|
|
|
|
found = not;
|
|
|
|
do {
|
|
|
|
if (*p == '\0')
|
|
|
|
return NULL;
|
|
|
|
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 != ']');
|
|
|
|
return found ? p + 1 : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gmatch(const char *s, const char *p)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int sc, pc;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (s == NULL || p == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
while ((pc = *p++ & CMASK) != '\0') {
|
|
|
|
sc = *s++ & QMASK;
|
|
|
|
switch (pc) {
|
|
|
|
case '[':
|
2007-01-01 11:30:38 +05:30
|
|
|
p = cclass(p, sc);
|
|
|
|
if (p == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
if (sc == 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
s--;
|
|
|
|
do {
|
|
|
|
if (*p == '\0' || gmatch(s, p))
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
} while (*s++ != '\0');
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
default:
|
|
|
|
if (sc != (pc & ~QUOTE))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *s == '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- csyn.c -------- */
|
|
|
|
/*
|
|
|
|
* shell: syntax (C version)
|
|
|
|
*/
|
|
|
|
|
2007-02-01 07:13:39 +05:30
|
|
|
static void yyerror(const char *s) ATTRIBUTE_NORETURN;
|
|
|
|
static void yyerror(const char *s)
|
|
|
|
{
|
2008-02-12 11:34:06 +05:30
|
|
|
yynerrs = 1;
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && global_env.iop <= iostack) {
|
2007-02-01 07:13:39 +05:30
|
|
|
multiline = 0;
|
2008-02-12 11:34:06 +05:30
|
|
|
while (eofc() == 0 && yylex(0) != '\n')
|
|
|
|
continue;
|
2007-02-01 07:13:39 +05:30
|
|
|
}
|
|
|
|
err(s);
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zzerr(void) ATTRIBUTE_NORETURN;
|
|
|
|
static void zzerr(void)
|
|
|
|
{
|
|
|
|
yyerror("syntax error");
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
int yyparse(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF7(("YYPARSE: enter...\n"));
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
startl = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = 0;
|
|
|
|
yynerrs = 0;
|
|
|
|
outtree = c_list();
|
|
|
|
musthave('\n', 0);
|
2008-02-12 11:34:06 +05:30
|
|
|
return yynerrs; /* 0/1 */
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *pipeline(int cf)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t, *p;
|
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = command(cf);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF9(("PIPELINE: t=%p\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (t != NULL) {
|
|
|
|
while ((c = yylex(0)) == '|') {
|
2007-01-01 11:30:38 +05:30
|
|
|
p = command(CONTIN);
|
|
|
|
if (p == NULL) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF8(("PIPELINE: error!\n"));
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t->op_type != TPAREN && t->op_type != TCOM) {
|
2001-06-29 10:27:14 +05:30
|
|
|
/* shell statement */
|
|
|
|
t = block(TPAREN, t, NOBLOCK, NOWORDS);
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
t = block(TPIPE, t, p, NOWORDS);
|
|
|
|
}
|
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("PIPELINE: returning t=%p\n", t));
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *andor(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t, *p;
|
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("ANDOR: enter...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = pipeline(0);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF9(("ANDOR: t=%p\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (t != NULL) {
|
|
|
|
while ((c = yylex(0)) == LOGAND || c == LOGOR) {
|
2007-01-01 11:30:38 +05:30
|
|
|
p = pipeline(CONTIN);
|
|
|
|
if (p == NULL) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF8(("ANDOR: error!\n"));
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
|
2008-03-02 20:03:26 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("ANDOR: returning t=%p\n", t));
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *c_list(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t, *p;
|
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("C_LIST: enter...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = andor();
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (t != NULL) {
|
2007-01-01 11:30:38 +05:30
|
|
|
peeksym = yylex(0);
|
|
|
|
if (peeksym == '&')
|
2001-06-29 10:27:14 +05:30
|
|
|
t = block(TASYNC, t, NOBLOCK, NOWORDS);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
while ((c = yylex(0)) == ';' || c == '&'
|
2008-03-02 18:19:39 +05:30
|
|
|
|| (multiline && c == '\n')
|
|
|
|
) {
|
2007-01-01 11:30:38 +05:30
|
|
|
p = andor();
|
|
|
|
if (p== NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
peeksym = yylex(0);
|
|
|
|
if (peeksym == '&')
|
2001-06-29 10:27:14 +05:30
|
|
|
p = block(TASYNC, p, NOBLOCK, NOWORDS);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
t = list(t, p);
|
2004-08-05 00:49:10 +05:30
|
|
|
} /* WHILE */
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
/* IF */
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("C_LIST: returning t=%p\n", t));
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int synio(int cf)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct ioword *iop;
|
|
|
|
int i;
|
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = yylex(cf);
|
|
|
|
if (c != '<' && c != '>') {
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
i = yylval.i;
|
|
|
|
musthave(WORD, 0);
|
|
|
|
iop = io(iounit, i, yylval.cp);
|
|
|
|
iounit = IODEFAULT;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (i & IOHERE)
|
|
|
|
markhere(yylval.cp, iop);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("SYNIO: returning 1\n"));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void musthave(int c, int cf)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:09:24 +05:30
|
|
|
peeksym = yylex(cf);
|
|
|
|
if (peeksym != c) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF7(("MUSTHAVE: error!\n"));
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = 0;
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *simple(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = NULL;
|
|
|
|
for (;;) {
|
|
|
|
switch (peeksym = yylex(0)) {
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
(void) synio(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WORD:
|
|
|
|
if (t == NULL) {
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TCOM;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
peeksym = 0;
|
|
|
|
word(yylval.cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *nested(int type, int mark)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
multiline++;
|
|
|
|
t = c_list();
|
|
|
|
musthave(mark, 0);
|
|
|
|
multiline--;
|
2006-11-27 22:19:55 +05:30
|
|
|
return block(type, t, NOBLOCK, NOWORDS);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *command(int cf)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
struct wdblock *iosave;
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF(("COMMAND: enter, cf=%d\n", cf));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
iosave = iolist;
|
|
|
|
iolist = NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (multiline)
|
|
|
|
cf |= CONTIN;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
while (synio(cf))
|
|
|
|
cf = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
c = yylex(cf);
|
|
|
|
|
|
|
|
switch (c) {
|
2001-06-29 10:27:14 +05:30
|
|
|
default:
|
|
|
|
peeksym = c;
|
2007-01-01 11:30:38 +05:30
|
|
|
t = simple();
|
|
|
|
if (t == NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (iolist == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TCOM;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
t = nested(TPAREN, ')');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
t = nested(TBRACE, '}');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FOR:
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TFOR;
|
2001-06-29 10:27:14 +05:30
|
|
|
musthave(WORD, 0);
|
|
|
|
startl = 1;
|
|
|
|
t->str = yylval.cp;
|
|
|
|
multiline++;
|
2008-03-02 18:19:39 +05:30
|
|
|
t->op_words = wordlist();
|
2007-01-01 11:30:38 +05:30
|
|
|
c = yylex(0);
|
|
|
|
if (c != '\n' && c != ';')
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
|
|
|
t->left = dogroup(0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WHILE:
|
|
|
|
case UNTIL:
|
|
|
|
multiline++;
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = (c == WHILE ? TWHILE : TUNTIL);
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = c_list();
|
|
|
|
t->right = dogroup(1);
|
2008-03-02 18:19:39 +05:30
|
|
|
/* t->op_words = NULL; - newtp() did this */
|
2001-06-29 10:27:14 +05:30
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CASE:
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TCASE;
|
2001-06-29 10:27:14 +05:30
|
|
|
musthave(WORD, 0);
|
|
|
|
t->str = yylval.cp;
|
|
|
|
startl++;
|
|
|
|
multiline++;
|
|
|
|
musthave(IN, CONTIN);
|
|
|
|
startl++;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = caselist();
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
musthave(ESAC, 0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IF:
|
|
|
|
multiline++;
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TIF;
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = c_list();
|
|
|
|
t->right = thenpart();
|
|
|
|
musthave(FI, 0);
|
|
|
|
multiline--;
|
|
|
|
break;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
case DOT:
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TDOT;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
musthave(WORD, 0); /* gets name of file */
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
|
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
word(yylval.cp); /* add word to wdlist */
|
|
|
|
word(NOWORD); /* terminate wdlist */
|
|
|
|
t->op_words = copyw(); /* dup wdlist */
|
2004-08-05 00:49:10 +05:30
|
|
|
break;
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
while (synio(0))
|
|
|
|
continue;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
t = namelist(t);
|
|
|
|
iolist = iosave;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("COMMAND: returning %p\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static struct op *dowholefile(int type /*, int mark*/)
|
2004-08-05 00:49:10 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
multiline++;
|
|
|
|
t = c_list();
|
|
|
|
multiline--;
|
|
|
|
t = block(type, t, NOBLOCK, NOWORDS);
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t));
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *dogroup(int onlydone)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
|
|
|
struct op *mylist;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
c = yylex(CONTIN);
|
|
|
|
if (c == DONE && onlydone)
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (c != DO)
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2001-06-29 10:27:14 +05:30
|
|
|
mylist = c_list();
|
|
|
|
musthave(DONE, 0);
|
2006-11-27 22:19:55 +05:30
|
|
|
return mylist;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *thenpart(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = yylex(0);
|
|
|
|
if (c != THEN) {
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
/*t->op_type = 0; - newtp() did this */
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = c_list();
|
|
|
|
if (t->left == NULL)
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2001-06-29 10:27:14 +05:30
|
|
|
t->right = elsepart();
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *elsepart(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
switch (c = yylex(0)) {
|
|
|
|
case ELSE:
|
2007-01-01 11:30:38 +05:30
|
|
|
t = c_list();
|
|
|
|
if (t == NULL)
|
2007-02-01 07:13:39 +05:30
|
|
|
zzerr();
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case ELIF:
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TELIF;
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = c_list();
|
|
|
|
t->right = thenpart();
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
default:
|
|
|
|
peeksym = c;
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *caselist(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
while ((peeksym = yylex(CONTIN)) != ESAC) {
|
|
|
|
DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym));
|
2001-06-29 10:27:14 +05:30
|
|
|
t = list(t, casepart());
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("CASELIST, returning t=%p\n", t));
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *casepart(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("CASEPART: enter...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = TPAT;
|
2008-03-02 18:19:39 +05:30
|
|
|
t->op_words = pattern();
|
2001-06-29 10:27:14 +05:30
|
|
|
musthave(')', 0);
|
|
|
|
t->left = c_list();
|
2007-01-01 11:30:38 +05:30
|
|
|
peeksym = yylex(CONTIN);
|
|
|
|
if (peeksym != ESAC)
|
2001-06-29 10:27:14 +05:30
|
|
|
musthave(BREAK, CONTIN);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static char **pattern(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c, cf;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
cf = CONTIN;
|
|
|
|
do {
|
|
|
|
musthave(WORD, cf);
|
|
|
|
word(yylval.cp);
|
|
|
|
cf = 0;
|
2007-01-01 11:30:38 +05:30
|
|
|
c = yylex(0);
|
|
|
|
} while (c == '|');
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
|
|
|
word(NOWORD);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return copyw();
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static char **wordlist(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = yylex(0);
|
|
|
|
if (c != IN) {
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = c;
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
startl = 0;
|
|
|
|
while ((c = yylex(0)) == WORD)
|
|
|
|
word(yylval.cp);
|
|
|
|
word(NOWORD);
|
|
|
|
peeksym = c;
|
2006-11-27 22:19:55 +05:30
|
|
|
return copyw();
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* supporting functions
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static struct op *list(struct op *t1, struct op *t2)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (t1 == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return t2;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (t2 == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return t1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return block(TLIST, t1, t2, NOWORDS);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *block(int type, struct op *t1, struct op *t2, char **wp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t = newtp();
|
2008-03-02 20:03:26 +05:30
|
|
|
t->op_type = type;
|
2001-06-29 10:27:14 +05:30
|
|
|
t->left = t1;
|
|
|
|
t->right = t2;
|
2008-03-02 18:19:39 +05:30
|
|
|
t->op_words = wp;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-12 11:34:06 +05:30
|
|
|
DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, t2));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
/* See if given string is a shell multiline (FOR, IF, etc) */
|
2006-07-01 18:38:46 +05:30
|
|
|
static int rlookup(char *n)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-12-16 22:51:29 +05:30
|
|
|
struct res {
|
|
|
|
char r_name[6];
|
|
|
|
int16_t r_val;
|
|
|
|
};
|
|
|
|
static const struct res restab[] = {
|
|
|
|
{ "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 },
|
|
|
|
{ "{" , '{' },
|
|
|
|
{ "}" , '}' },
|
|
|
|
{ "." , DOT },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
const struct res *rp;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
for (rp = restab; rp->r_name[0]; rp++)
|
2004-08-05 00:49:10 +05:30
|
|
|
if (strcmp(rp->r_name, n) == 0) {
|
|
|
|
DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val));
|
2006-11-27 22:19:55 +05:30
|
|
|
return rp->r_val; /* Return numeric code for shell multiline */
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF7(("RLOOKUP: NO match, returning 0\n"));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0; /* Not a shell multiline */
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct op *newtp(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
t = (struct op *) tree(sizeof(*t));
|
2008-02-12 11:34:06 +05:30
|
|
|
memset(t, 0, sizeof(*t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF3(("NEWTP: allocated %p\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static struct op *namelist(struct op *t)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t,
|
2008-03-02 20:03:26 +05:30
|
|
|
T_CMD_NAMES[t->op_type], iolist));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (iolist) {
|
2004-08-05 00:46:54 +05:30
|
|
|
iolist = addword((char *) NULL, iolist);
|
2001-06-29 10:27:14 +05:30
|
|
|
t->ioact = copyio();
|
|
|
|
} else
|
|
|
|
t->ioact = NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t->op_type != TCOM) {
|
|
|
|
if (t->op_type != TPAREN && t->ioact != NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
t = block(TPAREN, t, NOBLOCK, NOWORDS);
|
|
|
|
t->ioact = t->left->ioact;
|
|
|
|
t->left->ioact = NULL;
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
word(NOWORD);
|
2008-03-02 18:19:39 +05:30
|
|
|
t->op_words = copyw();
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static char **copyw(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char **wd;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
wd = getwords(wdlist);
|
2008-03-02 20:03:26 +05:30
|
|
|
wdlist = NULL;
|
2006-11-27 22:19:55 +05:30
|
|
|
return wd;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void word(char *cp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
wdlist = addword(cp, wdlist);
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct ioword **copyio(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct ioword **iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
iop = (struct ioword **) getwords(iolist);
|
2008-03-02 20:03:26 +05:30
|
|
|
iolist = NULL;
|
2006-11-27 22:19:55 +05:30
|
|
|
return iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct ioword *io(int u, int f, char *cp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct ioword *iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
iop = (struct ioword *) tree(sizeof(*iop));
|
2008-03-02 20:03:26 +05:30
|
|
|
iop->io_fd = u;
|
2001-06-29 10:27:14 +05:30
|
|
|
iop->io_flag = f;
|
|
|
|
iop->io_name = cp;
|
2004-08-05 00:46:54 +05:30
|
|
|
iolist = addword((char *) iop, iolist);
|
2006-11-27 22:19:55 +05:30
|
|
|
return iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int yylex(int cf)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c, c1;
|
2001-06-29 10:27:14 +05:30
|
|
|
int atstart;
|
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = peeksym;
|
|
|
|
if (c > 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
peeksym = 0;
|
|
|
|
if (c == '\n')
|
|
|
|
startl = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
nlseen = 0;
|
|
|
|
atstart = startl;
|
|
|
|
startl = 0;
|
|
|
|
yylval.i = 0;
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = line;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* MALAMO */
|
|
|
|
line[LINELIM - 1] = '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
loop:
|
2004-08-05 00:49:10 +05:30
|
|
|
while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */
|
2008-03-02 18:19:39 +05:30
|
|
|
continue;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
if (any(c, "0123456789")) {
|
2007-02-01 07:09:24 +05:30
|
|
|
c1 = my_getc(0);
|
|
|
|
unget(c1);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (c1 == '<' || c1 == '>') {
|
|
|
|
iounit = c - '0';
|
|
|
|
goto loop;
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
c = c1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
case '#': /* Comment, skip to next newline or End-of-string */
|
2008-03-02 18:19:39 +05:30
|
|
|
while ((c = my_getc(0)) != '\0' && c != '\n')
|
|
|
|
continue;
|
2001-06-29 10:27:14 +05:30
|
|
|
unget(c);
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case 0:
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF5(("YYLEX: return 0, c=%d\n", c));
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case '$':
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF9(("YYLEX: found $\n"));
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2007-02-01 07:09:24 +05:30
|
|
|
c = my_getc(0);
|
|
|
|
if (c == '{') {
|
|
|
|
c = collect(c, '}');
|
|
|
|
if (c != '\0')
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
goto pack;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
2007-02-01 07:09:24 +05:30
|
|
|
c = collect(c, c);
|
|
|
|
if (c != '\0')
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
goto pack;
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
case '&':
|
|
|
|
case ';':
|
|
|
|
startl = 1;
|
2004-08-05 00:49:10 +05:30
|
|
|
/* If more chars process them, else return NULL char */
|
2007-02-01 07:09:24 +05:30
|
|
|
c1 = dual(c);
|
|
|
|
if (c1 != '\0')
|
2006-11-27 22:19:55 +05:30
|
|
|
return c1;
|
2007-02-01 07:09:24 +05:30
|
|
|
return c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
case '^':
|
|
|
|
startl = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return '|';
|
2001-06-29 10:27:14 +05:30
|
|
|
case '>':
|
|
|
|
case '<':
|
|
|
|
diag(c);
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case '\n':
|
|
|
|
nlseen++;
|
|
|
|
gethere();
|
|
|
|
startl = 1;
|
|
|
|
if (multiline || cf & CONTIN) {
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && global_env.iop <= iostack) {
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
2004-08-05 00:46:54 +05:30
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 10:27:14 +05:30
|
|
|
#else
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(cprompt->value);
|
2001-06-29 10:27:14 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (cf & CONTIN)
|
|
|
|
goto loop;
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
startl = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
unget(c);
|
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
pack:
|
|
|
|
while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) {
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.linep >= elinep)
|
2001-06-29 10:27:14 +05:30
|
|
|
err("word too long");
|
|
|
|
else
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2004-08-05 00:49:10 +05:30
|
|
|
};
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
unget(c);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
if (any(c, "\"'`$"))
|
2001-06-29 10:27:14 +05:30
|
|
|
goto loop;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = '\0';
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
if (atstart) {
|
|
|
|
c = rlookup(line);
|
|
|
|
if (c != 0) {
|
|
|
|
startl = 1;
|
|
|
|
return c;
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
yylval.cp = strsave(line, areanum);
|
2006-11-27 22:19:55 +05:30
|
|
|
return WORD;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int collect(int c, int c1)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
char s[2];
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1));
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
while ((c = my_getc(c1)) != c1) {
|
|
|
|
if (c == 0) {
|
|
|
|
unget(c);
|
|
|
|
s[0] = c1;
|
|
|
|
s[1] = 0;
|
2004-08-05 00:46:54 +05:30
|
|
|
prs("no closing ");
|
|
|
|
yyerror(s);
|
2006-11-27 22:19:55 +05:30
|
|
|
return YYERRCODE;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && c == '\n' && global_env.iop <= iostack) {
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
2004-08-05 00:46:54 +05:30
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 10:27:14 +05:30
|
|
|
#else
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(cprompt->value);
|
2001-06-29 10:27:14 +05:30
|
|
|
#endif
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF8(("COLLECT: return 0, line is %s\n", line));
|
|
|
|
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
/* "multiline commands" helper func */
|
|
|
|
/* see if next 2 chars form a shell multiline */
|
2006-07-01 18:38:46 +05:30
|
|
|
static int dual(int c)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
char s[3];
|
2006-07-01 18:38:46 +05:30
|
|
|
char *cp = s;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF8(("DUAL: enter, c=%d\n", c));
|
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
*cp++ = c; /* c is the given "peek" char */
|
|
|
|
*cp++ = my_getc(0); /* get next char of input */
|
|
|
|
*cp = '\0'; /* add EOS marker */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
c = rlookup(s); /* see if 2 chars form a shell multiline */
|
2004-08-05 00:49:10 +05:30
|
|
|
if (c == 0)
|
2007-02-01 07:09:24 +05:30
|
|
|
unget(*--cp); /* String is not a shell multiline, put peek char back */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:09:24 +05:30
|
|
|
return c; /* String is multiline, return numeric multiline (restab) code */
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void diag(int ec)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF8(("DIAG: enter, ec=%d\n", ec));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
c = my_getc(0);
|
|
|
|
if (c == '>' || c == '<') {
|
|
|
|
if (c != ec)
|
|
|
|
zzerr();
|
2007-02-01 07:09:24 +05:30
|
|
|
yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE);
|
2001-06-29 10:27:14 +05:30
|
|
|
c = my_getc(0);
|
|
|
|
} else
|
2007-02-01 07:09:24 +05:30
|
|
|
yylval.i = (ec == '>' ? IOWRITE : IOREAD);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (c != '&' || yylval.i == IOHERE)
|
|
|
|
unget(c);
|
|
|
|
else
|
|
|
|
yylval.i |= IODUP;
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static char *tree(unsigned size)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *t;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
t = getcell(size);
|
|
|
|
if (t == NULL) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF2(("TREE: getcell(%d) failed!\n", size));
|
2001-06-29 10:27:14 +05:30
|
|
|
prs("command line too complicated\n");
|
|
|
|
fail();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return t;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* VARARGS1 */
|
|
|
|
/* ARGSUSED */
|
|
|
|
|
|
|
|
/* -------- exec.c -------- */
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static struct op **find1case(struct op *t, const char *w)
|
|
|
|
{
|
|
|
|
struct op *t1;
|
|
|
|
struct op **tp;
|
|
|
|
char **wp;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (t == NULL) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type,
|
|
|
|
T_CMD_NAMES[t->op_type]));
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t->op_type == TLIST) {
|
2007-02-01 07:13:54 +05:30
|
|
|
tp = find1case(t->left, w);
|
|
|
|
if (tp != NULL) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp));
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
t1 = t->right; /* TPAT */
|
|
|
|
} else
|
|
|
|
t1 = t;
|
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
for (wp = t1->op_words; *wp;) {
|
2007-02-01 07:13:54 +05:30
|
|
|
cp = evalstr(*wp++, DOSUB);
|
|
|
|
if (cp && gmatch(w, cp)) {
|
|
|
|
DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n",
|
|
|
|
&t1->left));
|
|
|
|
return &t1->left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("FIND1CASE: returning NULL\n"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct op *findcase(struct op *t, const char *w)
|
|
|
|
{
|
|
|
|
struct op **tp;
|
|
|
|
|
|
|
|
tp = find1case(t, w);
|
|
|
|
return tp != NULL ? *tp : NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* execute tree
|
|
|
|
*/
|
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
static int execute(struct op *t, int *pin, int *pout, int no_fork)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct op *t1;
|
2001-06-29 10:27:14 +05:30
|
|
|
volatile int i, rv, a;
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp;
|
|
|
|
char **wp, **wp2;
|
2001-06-29 10:27:14 +05:30
|
|
|
struct var *vp;
|
2004-08-05 00:49:10 +05:30
|
|
|
struct op *outtree_save;
|
2001-06-29 10:27:14 +05:30
|
|
|
struct brkcon bc;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) ℘
|
2004-03-15 13:59:22 +05:30
|
|
|
#endif
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
if (t == NULL) {
|
|
|
|
DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n"));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t,
|
|
|
|
t->op_type, T_CMD_NAMES[t->op_type],
|
2008-03-02 18:19:39 +05:30
|
|
|
((t->op_words == NULL) ? "NULL" : t->op_words[0])));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
rv = 0;
|
|
|
|
a = areanum++;
|
2008-03-02 18:19:39 +05:30
|
|
|
wp2 = t->op_words;
|
2008-02-12 11:34:06 +05:30
|
|
|
wp = (wp2 != NULL)
|
2008-03-02 20:03:26 +05:30
|
|
|
? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY)
|
2004-08-05 00:46:54 +05:30
|
|
|
: NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
switch (t->op_type) {
|
2004-08-05 00:49:10 +05:30
|
|
|
case TDOT:
|
|
|
|
DBGPRINTF3(("EXECUTE: TDOT\n"));
|
|
|
|
|
|
|
|
outtree_save = outtree;
|
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
newfile(evalstr(t->op_words[0], DOALL));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
t->left = dowholefile(TLIST /*, 0*/);
|
2004-08-05 00:49:10 +05:30
|
|
|
t->right = NULL;
|
|
|
|
|
|
|
|
outtree = outtree_save;
|
|
|
|
|
|
|
|
if (t->left)
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t->left, pin, pout, /* no_fork: */ 0);
|
2004-08-05 00:49:10 +05:30
|
|
|
if (t->right)
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t->right, pin, pout, /* no_fork: */ 0);
|
2004-08-05 00:49:10 +05:30
|
|
|
break;
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
case TPAREN:
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t->left, pin, pout, /* no_fork: */ 0);
|
2003-03-14 21:35:59 +05:30
|
|
|
break;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
case TCOM:
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = forkexec(t, pin, pout, no_fork, wp);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case TPIPE:
|
|
|
|
{
|
2004-08-05 00:46:54 +05:30
|
|
|
int pv[2];
|
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
rv = openpipe(pv);
|
|
|
|
if (rv < 0)
|
2004-08-05 00:46:54 +05:30
|
|
|
break;
|
|
|
|
pv[0] = remap(pv[0]);
|
|
|
|
pv[1] = remap(pv[1]);
|
2008-02-12 15:21:03 +05:30
|
|
|
(void) execute(t->left, pin, pv, /* no_fork: */ 0);
|
|
|
|
rv = execute(t->right, pv, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TLIST:
|
2008-02-12 15:21:03 +05:30
|
|
|
(void) execute(t->left, pin, pout, /* no_fork: */ 0);
|
|
|
|
rv = execute(t->right, pin, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case TASYNC:
|
2004-08-05 00:46:54 +05:30
|
|
|
{
|
2008-02-12 11:34:06 +05:30
|
|
|
smallint hinteractive = interactive;
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
i = vfork();
|
2007-02-01 07:09:24 +05:30
|
|
|
if (i == 0) { /* child */
|
2004-08-05 00:46:54 +05:30
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
if (interactive)
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
interactive = 0;
|
|
|
|
if (pin == NULL) {
|
|
|
|
close(0);
|
2007-05-10 03:46:08 +05:30
|
|
|
xopen(bb_dev_null, O_RDONLY);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
_exit(execute(t->left, pin, pout, /* no_fork: */ 1));
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
interactive = hinteractive;
|
|
|
|
if (i != -1) {
|
|
|
|
setval(lookup("!"), putn(i));
|
2008-02-11 23:40:06 +05:30
|
|
|
closepipe(pin);
|
2007-02-01 07:09:24 +05:30
|
|
|
if (interactive) {
|
|
|
|
prs(putn(i));
|
|
|
|
prs("\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
rv = -1;
|
|
|
|
setstatus(rv);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOR:
|
|
|
|
case TAND:
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t->left, pin, pout, /* no_fork: */ 0);
|
2007-01-01 11:30:38 +05:30
|
|
|
t1 = t->right;
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t1 != NULL && (rv == 0) == (t->op_type == TAND))
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t1, pin, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case TFOR:
|
|
|
|
if (wp == NULL) {
|
2004-08-05 00:46:54 +05:30
|
|
|
wp = dolv + 1;
|
2007-02-01 07:09:24 +05:30
|
|
|
i = dolc;
|
|
|
|
if (i < 0)
|
2001-06-29 10:27:14 +05:30
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
i = -1;
|
2008-03-02 18:19:39 +05:30
|
|
|
while (*wp++ != NULL)
|
|
|
|
continue;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
vp = lookup(t->str);
|
|
|
|
while (setjmp(bc.brkpt))
|
|
|
|
if (isbreak)
|
|
|
|
goto broken;
|
|
|
|
brkset(&bc);
|
|
|
|
for (t1 = t->left; i-- && *wp != NULL;) {
|
|
|
|
setval(vp, *wp++);
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t1, pin, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
brklist = brklist->nextlev;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TWHILE:
|
|
|
|
case TUNTIL:
|
|
|
|
while (setjmp(bc.brkpt))
|
|
|
|
if (isbreak)
|
|
|
|
goto broken;
|
|
|
|
brkset(&bc);
|
|
|
|
t1 = t->left;
|
2008-03-02 20:03:26 +05:30
|
|
|
while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->op_type == TWHILE))
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t->right, pin, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
brklist = brklist->nextlev;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIF:
|
|
|
|
case TELIF:
|
2004-08-05 00:46:54 +05:30
|
|
|
if (t->right != NULL) {
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = !execute(t->left, pin, pout, /* no_fork: */ 0) ?
|
|
|
|
execute(t->right->left, pin, pout, /* no_fork: */ 0) :
|
|
|
|
execute(t->right->right, pin, pout, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCASE:
|
2007-01-01 11:30:38 +05:30
|
|
|
cp = evalstr(t->str, DOSUB | DOTRIM);
|
2007-02-01 07:09:24 +05:30
|
|
|
if (cp == NULL)
|
2001-06-29 10:27:14 +05:30
|
|
|
cp = "";
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n",
|
|
|
|
((t->str == NULL) ? "NULL" : t->str),
|
|
|
|
((cp == NULL) ? "NULL" : cp)));
|
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
t1 = findcase(t->left, cp);
|
|
|
|
if (t1 != NULL) {
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1));
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t1, pin, pout, /* no_fork: */ 0);
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=%p, t1=%p)...\n", t, t1));
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case TBRACE:
|
|
|
|
/*
|
2007-02-01 07:09:24 +05:30
|
|
|
iopp = t->ioact;
|
|
|
|
if (i)
|
2001-06-29 10:27:14 +05:30
|
|
|
while (*iopp)
|
|
|
|
if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
|
|
|
|
rv = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
2007-02-01 07:09:24 +05:30
|
|
|
if (rv >= 0) {
|
|
|
|
t1 = t->left;
|
|
|
|
if (t1) {
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(t1, pin, pout, /* no_fork: */ 0);
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
};
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-04-14 20:38:41 +05:30
|
|
|
broken:
|
2008-03-02 18:19:39 +05:30
|
|
|
// Restoring op_words is most likely not needed now: see comment in forkexec()
|
|
|
|
// (also take a look at exec builtin (doexec) - it touches t->op_words)
|
|
|
|
t->op_words = wp2;
|
2001-06-29 10:27:14 +05:30
|
|
|
isbreak = 0;
|
|
|
|
freehere(areanum);
|
|
|
|
freearea(areanum);
|
|
|
|
areanum = a;
|
|
|
|
if (interactive && intr) {
|
|
|
|
closeall();
|
|
|
|
fail();
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
i = trapset;
|
|
|
|
if (i != 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
trapset = 0;
|
|
|
|
runtrap(i);
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv));
|
2006-11-27 22:19:55 +05:30
|
|
|
return rv;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-04-14 20:38:41 +05:30
|
|
|
static builtin_func_ptr inbuilt(const char *s)
|
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
const struct builtincmd *bp;
|
|
|
|
|
2007-04-09 08:36:34 +05:30
|
|
|
for (bp = builtincmds; bp->name; bp++)
|
2007-02-01 07:13:54 +05:30
|
|
|
if (strcmp(bp->name, s) == 0)
|
|
|
|
return bp->builtinfunc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2004-08-05 00:49:10 +05:30
|
|
|
pid_t newpid;
|
2008-02-11 23:40:06 +05:30
|
|
|
int i;
|
|
|
|
builtin_func_ptr bltin = NULL;
|
2008-02-12 15:21:03 +05:30
|
|
|
const char *bltin_name = NULL;
|
|
|
|
const char *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
struct ioword **iopp;
|
|
|
|
int resetsig;
|
|
|
|
char **owp;
|
2008-02-12 15:21:03 +05:30
|
|
|
int forked;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
int *hpin = pin;
|
|
|
|
int *hpout = pout;
|
|
|
|
char *hwp;
|
2008-02-12 11:34:06 +05:30
|
|
|
smallint hinteractive;
|
|
|
|
smallint hintr;
|
|
|
|
smallint hexecflg;
|
2004-08-05 00:46:54 +05:30
|
|
|
struct brkcon *hbrklist;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &pin;
|
|
|
|
(void) &pout;
|
|
|
|
(void) ℘
|
2008-02-11 23:40:06 +05:30
|
|
|
(void) &bltin;
|
2001-06-29 10:27:14 +05:30
|
|
|
(void) &cp;
|
|
|
|
(void) &resetsig;
|
|
|
|
(void) &owp;
|
2004-03-15 13:59:22 +05:30
|
|
|
#endif
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, no_fork %d\n", t, pin,
|
|
|
|
pout, no_fork));
|
2008-03-02 18:19:39 +05:30
|
|
|
DBGPRINTF7(("FORKEXEC: t->op_words is %s\n",
|
|
|
|
((t->op_words == NULL) ? "NULL" : t->op_words[0])));
|
2001-06-29 10:27:14 +05:30
|
|
|
owp = wp;
|
|
|
|
resetsig = 0;
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t->op_type == TCOM) {
|
2007-04-14 18:52:09 +05:30
|
|
|
while (*wp++ != NULL)
|
|
|
|
continue;
|
2001-06-29 10:27:14 +05:30
|
|
|
cp = *wp;
|
|
|
|
|
|
|
|
/* strip all initial assignments */
|
2008-02-12 15:21:03 +05:30
|
|
|
/* FIXME: not correct wrt PATH=yyy command etc */
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['x']) {
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n",
|
2004-08-05 00:49:10 +05:30
|
|
|
cp, wp, owp));
|
2004-08-05 00:46:54 +05:30
|
|
|
echo(cp ? wp : owp);
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
if (cp == NULL) {
|
|
|
|
if (t->ioact == NULL) {
|
|
|
|
while ((cp = *owp++) != NULL && assign(cp, COPYV))
|
|
|
|
continue;
|
|
|
|
DBGPRINTF(("FORKEXEC: returning setstatus(0)\n"));
|
|
|
|
return setstatus(0);
|
|
|
|
}
|
|
|
|
} else { /* cp != NULL */
|
|
|
|
bltin_name = cp;
|
2008-02-11 23:40:06 +05:30
|
|
|
bltin = inbuilt(cp);
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
forked = 0;
|
2008-03-02 18:19:39 +05:30
|
|
|
// We were pointing t->op_words to temporary (expanded) arg list:
|
|
|
|
// t->op_words = wp;
|
2008-03-01 15:05:39 +05:30
|
|
|
// and restored it later (in execute()), but "break"
|
2008-03-02 18:19:39 +05:30
|
|
|
// longjmps away (at "Run builtin" below), leaving t->op_words clobbered!
|
2008-03-01 15:05:39 +05:30
|
|
|
// See http://bugs.busybox.net/view.php?id=846.
|
2008-03-02 18:19:39 +05:30
|
|
|
// Now we do not touch t->op_words, but separately pass wp as param list
|
2008-03-24 07:35:58 +05:30
|
|
|
// to builtins
|
2008-02-12 15:21:03 +05:30
|
|
|
DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin,
|
|
|
|
no_fork, owp));
|
|
|
|
/* Don't fork if it is a lone builtin (not in pipe)
|
|
|
|
* OR we are told to _not_ fork */
|
|
|
|
if ((!bltin || pin || pout) /* not lone bltin AND */
|
|
|
|
&& !no_fork /* not told to avoid fork */
|
|
|
|
) {
|
|
|
|
/* Save values in case child alters them after vfork */
|
2001-06-29 10:27:14 +05:30
|
|
|
hpin = pin;
|
|
|
|
hpout = pout;
|
|
|
|
hwp = *wp;
|
|
|
|
hinteractive = interactive;
|
|
|
|
hintr = intr;
|
|
|
|
hbrklist = brklist;
|
|
|
|
hexecflg = execflg;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
|
|
|
|
newpid = vfork();
|
|
|
|
if (newpid == -1) {
|
2006-11-19 03:34:09 +05:30
|
|
|
DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n"));
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
if (newpid > 0) { /* Parent */
|
2004-08-05 00:49:10 +05:30
|
|
|
/* Restore values */
|
2001-06-29 10:27:14 +05:30
|
|
|
pin = hpin;
|
|
|
|
pout = hpout;
|
|
|
|
*wp = hwp;
|
|
|
|
interactive = hinteractive;
|
|
|
|
intr = hintr;
|
|
|
|
brklist = hbrklist;
|
|
|
|
execflg = hexecflg;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-11 23:40:06 +05:30
|
|
|
closepipe(pin);
|
2004-08-05 00:49:10 +05:30
|
|
|
return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-02-11 23:40:06 +05:30
|
|
|
/* Child */
|
2008-02-12 15:21:03 +05:30
|
|
|
DBGPRINTF(("FORKEXEC: child process, bltin=%p (%s)\n", bltin, bltin_name));
|
2001-06-29 10:27:14 +05:30
|
|
|
if (interactive) {
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
resetsig = 1;
|
|
|
|
}
|
|
|
|
interactive = 0;
|
|
|
|
intr = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
forked = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
brklist = 0;
|
|
|
|
execflg = 0;
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-11 23:40:06 +05:30
|
|
|
if (owp)
|
2001-06-29 10:27:14 +05:30
|
|
|
while ((cp = *owp++) != NULL && assign(cp, COPYV))
|
2008-02-11 23:40:06 +05:30
|
|
|
if (!bltin)
|
2001-06-29 10:27:14 +05:30
|
|
|
export(lookup(cp));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-16 18:48:17 +05:30
|
|
|
if (pin) { /* NB: close _first_, then move fds! */
|
|
|
|
close(pin[1]);
|
2007-05-10 03:46:08 +05:30
|
|
|
xmove_fd(pin[0], 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-11 23:40:06 +05:30
|
|
|
if (pout) {
|
2008-02-16 18:48:17 +05:30
|
|
|
close(pout[0]);
|
2007-05-10 03:46:08 +05:30
|
|
|
xmove_fd(pout[1], 1);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
iopp = t->ioact;
|
2008-02-11 23:40:06 +05:30
|
|
|
if (iopp) {
|
|
|
|
if (bltin && bltin != doexec) {
|
2008-02-12 15:21:03 +05:30
|
|
|
prs(bltin_name);
|
2001-06-29 10:27:14 +05:30
|
|
|
err(": cannot redirect shell command");
|
2004-08-05 00:49:10 +05:30
|
|
|
if (forked)
|
|
|
|
_exit(-1);
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
while (*iopp) {
|
2004-08-05 00:49:10 +05:30
|
|
|
if (iosetup(*iopp++, pin != NULL, pout != NULL)) {
|
2008-02-11 23:40:06 +05:30
|
|
|
/* system-detected error */
|
2004-08-05 00:49:10 +05:30
|
|
|
if (forked)
|
2008-02-11 23:40:06 +05:30
|
|
|
_exit(-1);
|
|
|
|
return -1;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
2008-02-11 23:40:06 +05:30
|
|
|
if (bltin) {
|
2008-02-12 15:21:03 +05:30
|
|
|
if (forked || pin || pout) {
|
|
|
|
/* Builtin in pipe: disallowed */
|
|
|
|
/* TODO: allow "exec"? */
|
|
|
|
prs(bltin_name);
|
|
|
|
err(": cannot run builtin as part of pipe");
|
|
|
|
if (forked)
|
|
|
|
_exit(-1);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-03-01 15:05:39 +05:30
|
|
|
/* Run builtin */
|
|
|
|
i = setstatus(bltin(t, wp));
|
2004-08-05 00:49:10 +05:30
|
|
|
if (forked)
|
|
|
|
_exit(i);
|
|
|
|
DBGPRINTF(("FORKEXEC: returning i=%d\n", i));
|
2006-11-27 22:19:55 +05:30
|
|
|
return i;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* should use FIOCEXCL */
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = FDBASE; i < NOFILE; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
close(i);
|
|
|
|
if (resetsig) {
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (t->op_type == TPAREN)
|
2008-02-12 15:21:03 +05:30
|
|
|
_exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1));
|
2001-06-29 10:27:14 +05:30
|
|
|
if (wp[0] == NULL)
|
2008-05-19 14:59:47 +05:30
|
|
|
_exit(EXIT_SUCCESS);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-09-03 04:43:10 +05:30
|
|
|
cp = rexecve(wp[0], wp, makenv(0, NULL));
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(wp[0]);
|
|
|
|
prs(": ");
|
2004-08-05 00:49:10 +05:30
|
|
|
err(cp);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (!execflg)
|
|
|
|
trap[0] = NULL;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-12 15:21:03 +05:30
|
|
|
DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", getpid()));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
leave();
|
|
|
|
/* NOTREACHED */
|
2008-02-11 23:40:06 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 0< 1> are ignored as required
|
|
|
|
* within pipelines.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int iosetup(struct ioword *iop, int pipein, int pipeout)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int u = -1;
|
2007-02-01 07:13:54 +05:30
|
|
|
char *cp = NULL;
|
|
|
|
const char *msg;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop,
|
2004-08-05 00:49:10 +05:30
|
|
|
pipein, pipeout));
|
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (iop->io_fd == IODEFAULT) /* take default */
|
|
|
|
iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (pipein && iop->io_fd == 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 20:03:26 +05:30
|
|
|
if (pipeout && iop->io_fd == 1)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
|
2001-06-29 10:27:14 +05:30
|
|
|
if ((iop->io_flag & IOHERE) == 0) {
|
2007-02-01 07:09:24 +05:30
|
|
|
cp = iop->io_name; /* huh?? */
|
|
|
|
cp = evalstr(cp, DOSUB | DOTRIM);
|
|
|
|
if (cp == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (iop->io_flag & IODUP) {
|
|
|
|
if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
|
|
|
|
prs(cp);
|
|
|
|
err(": illegal >& argument");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
if (*cp == '-')
|
|
|
|
iop->io_flag = IOCLOSE;
|
2004-08-05 00:46:54 +05:30
|
|
|
iop->io_flag &= ~(IOREAD | IOWRITE);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
switch (iop->io_flag) {
|
|
|
|
case IOREAD:
|
2007-04-14 18:52:09 +05:30
|
|
|
u = open(cp, O_RDONLY);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case IOHERE:
|
2004-08-05 00:46:54 +05:30
|
|
|
case IOHERE | IOXHERE:
|
|
|
|
u = herein(iop->io_name, iop->io_flag & IOXHERE);
|
2007-02-01 07:13:54 +05:30
|
|
|
cp = (char*)"here file";
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
case IOWRITE | IOCAT:
|
2007-04-14 18:52:09 +05:30
|
|
|
u = open(cp, O_WRONLY);
|
2007-02-01 07:09:24 +05:30
|
|
|
if (u >= 0) {
|
2006-10-14 07:53:43 +05:30
|
|
|
lseek(u, (long) 0, SEEK_END);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
/* fall through to creation if >>file doesn't exist */
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
case IOWRITE:
|
|
|
|
u = creat(cp, 0666);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IODUP:
|
2008-03-02 20:03:26 +05:30
|
|
|
u = dup2(*cp - '0', iop->io_fd);
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case IOCLOSE:
|
2008-03-02 20:03:26 +05:30
|
|
|
close(iop->io_fd);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-12 15:21:03 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (u < 0) {
|
|
|
|
prs(cp);
|
|
|
|
prs(": cannot ");
|
|
|
|
warn(msg);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2008-03-02 20:03:26 +05:30
|
|
|
xmove_fd(u, iop->io_fd);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enter a new loop level (marked for break/continue).
|
|
|
|
*/
|
2006-06-26 03:38:53 +05:30
|
|
|
static void brkset(struct brkcon *bc)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int waitfor(int lastpid, int canintr)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int pid, rv;
|
2001-06-29 10:27:14 +05:30
|
|
|
int s;
|
2008-02-12 11:34:06 +05:30
|
|
|
smallint oheedint = heedint;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
heedint = 0;
|
|
|
|
rv = 0;
|
|
|
|
do {
|
|
|
|
pid = wait(&s);
|
|
|
|
if (pid == -1) {
|
|
|
|
if (errno != EINTR || canintr)
|
|
|
|
break;
|
|
|
|
} else {
|
2007-01-01 11:30:38 +05:30
|
|
|
rv = WAITSIG(s);
|
|
|
|
if (rv != 0) {
|
2007-06-25 16:25:35 +05:30
|
|
|
if (rv < ARRAY_SIZE(signame)) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (signame[rv] != NULL) {
|
|
|
|
if (pid != lastpid) {
|
|
|
|
prn(pid);
|
|
|
|
prs(": ");
|
|
|
|
}
|
|
|
|
prs(signame[rv]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pid != lastpid) {
|
|
|
|
prn(pid);
|
|
|
|
prs(": ");
|
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
prs("Signal ");
|
|
|
|
prn(rv);
|
|
|
|
prs(" ");
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
if (WAITCORE(s))
|
|
|
|
prs(" - core dumped");
|
2007-06-25 16:25:35 +05:30
|
|
|
if (rv >= ARRAY_SIZE(signame) || signame[rv])
|
2001-06-29 10:27:14 +05:30
|
|
|
prs("\n");
|
2008-05-14 17:21:04 +05:30
|
|
|
rv |= 0x80;
|
2001-06-29 10:27:14 +05:30
|
|
|
} else
|
|
|
|
rv = WAITVAL(s);
|
|
|
|
}
|
|
|
|
} while (pid != lastpid);
|
|
|
|
heedint = oheedint;
|
|
|
|
if (intr) {
|
|
|
|
if (interactive) {
|
|
|
|
if (canintr)
|
|
|
|
intr = 0;
|
|
|
|
} else {
|
2004-08-05 00:46:54 +05:30
|
|
|
if (exstat == 0)
|
|
|
|
exstat = rv;
|
2001-06-29 10:27:14 +05:30
|
|
|
onintr(0);
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return rv;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int setstatus(int s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
exstat = s;
|
|
|
|
setval(lookup("?"), putn(s));
|
2006-11-27 22:19:55 +05:30
|
|
|
return s;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PATH-searching interface to execve.
|
|
|
|
* If getenv("PATH") were kept up-to-date,
|
|
|
|
* execvp might be used.
|
|
|
|
*/
|
2007-02-01 07:13:54 +05:30
|
|
|
static const char *rexecve(char *c, char **v, char **envp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *sp;
|
|
|
|
char *tp;
|
2008-02-11 23:40:06 +05:30
|
|
|
int asis = 0;
|
2001-07-07 05:35:55 +05:30
|
|
|
char *name = c;
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2007-04-11 04:33:30 +05:30
|
|
|
if (ENABLE_FEATURE_SH_STANDALONE) {
|
2007-11-28 12:19:03 +05:30
|
|
|
if (find_applet_by_name(name) >= 0) {
|
2006-08-11 03:16:43 +05:30
|
|
|
/* We have to exec here since we vforked. Running
|
2007-04-11 22:33:19 +05:30
|
|
|
* run_applet_and_exit() won't work and bad things
|
2006-08-11 03:16:43 +05:30
|
|
|
* will happen. */
|
2007-06-08 20:32:55 +05:30
|
|
|
execve(bb_busybox_exec_path, v, envp);
|
2006-08-11 03:16:43 +05:30
|
|
|
}
|
2001-07-07 05:35:55 +05:30
|
|
|
}
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
sp = any('/', c) ? "" : path->value;
|
2007-02-01 07:13:54 +05:30
|
|
|
asis = (*sp == '\0');
|
2001-06-29 10:27:14 +05:30
|
|
|
while (asis || *sp != '\0') {
|
|
|
|
asis = 0;
|
2007-12-16 22:51:29 +05:30
|
|
|
tp = global_env.linep;
|
2007-02-01 07:09:24 +05:30
|
|
|
for (; *sp != '\0'; tp++) {
|
|
|
|
*tp = *sp++;
|
|
|
|
if (*tp == ':') {
|
|
|
|
asis = (*sp == '\0');
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (tp != global_env.linep)
|
2001-06-29 10:27:14 +05:30
|
|
|
*tp++ = '/';
|
2008-03-02 18:19:39 +05:30
|
|
|
strcpy(tp, c);
|
2001-07-07 05:35:55 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
execve(global_env.linep, v, envp);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
switch (errno) {
|
|
|
|
case ENOEXEC:
|
2008-05-31 03:58:32 +05:30
|
|
|
/* File is executable but file format isnt recognized */
|
|
|
|
/* Run it as a shell script */
|
|
|
|
/* (execve above didnt do it itself, unlike execvp) */
|
2007-12-16 22:51:29 +05:30
|
|
|
*v = global_env.linep;
|
2008-03-02 18:19:39 +05:30
|
|
|
v--;
|
|
|
|
tp = *v;
|
2008-05-31 03:58:32 +05:30
|
|
|
*v = (char*)DEFAULT_SHELL;
|
2003-09-02 08:06:18 +05:30
|
|
|
execve(DEFAULT_SHELL, v, envp);
|
2001-06-29 10:27:14 +05:30
|
|
|
*v = tp;
|
2008-03-02 18:19:39 +05:30
|
|
|
return "no shell";
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case ENOMEM:
|
2006-11-27 22:19:55 +05:30
|
|
|
return (char *) bb_msg_memory_exhausted;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
case E2BIG:
|
2006-11-27 22:19:55 +05:30
|
|
|
return "argument list too long";
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
2008-05-31 03:58:32 +05:30
|
|
|
if (errno == ENOENT) {
|
|
|
|
exstat = 127; /* standards require this */
|
|
|
|
return "not found";
|
|
|
|
}
|
|
|
|
exstat = 126; /* mimic bash */
|
|
|
|
return "cannot execute";
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command produced by generator `f'
|
|
|
|
* applied to stream `arg'.
|
|
|
|
*/
|
2004-08-05 00:46:54 +05:30
|
|
|
static int run(struct ioarg *argp, int (*f) (struct ioarg *))
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n",
|
2004-08-05 00:49:10 +05:30
|
|
|
areanum, outtree, failpt));
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
areanum++;
|
|
|
|
swdlist = wdlist;
|
|
|
|
siolist = iolist;
|
|
|
|
otree = outtree;
|
|
|
|
ofail = failpt;
|
|
|
|
rv = -1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
errpt = ev;
|
|
|
|
if (newenv(setjmp(errpt)) == 0) {
|
2008-03-02 20:03:26 +05:30
|
|
|
wdlist = NULL;
|
|
|
|
iolist = NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
pushio(argp, f);
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iobase = global_env.iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
yynerrs = 0;
|
2007-01-01 11:30:38 +05:30
|
|
|
failpt = rt;
|
|
|
|
if (setjmp(failpt) == 0 && yyparse() == 0)
|
2008-02-12 15:21:03 +05:30
|
|
|
rv = execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
quitenv();
|
2004-08-05 00:49:10 +05:30
|
|
|
} else {
|
|
|
|
DBGPRINTF(("RUN: error from newenv()!\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
wdlist = swdlist;
|
|
|
|
iolist = siolist;
|
|
|
|
failpt = ofail;
|
|
|
|
outtree = otree;
|
|
|
|
freearea(areanum--);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return rv;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- do.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* built-in commands: doX
|
|
|
|
*/
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dohelp(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED)
|
2001-07-07 05:35:55 +05:30
|
|
|
{
|
|
|
|
int col;
|
|
|
|
const struct builtincmd *x;
|
|
|
|
|
2006-12-24 20:53:28 +05:30
|
|
|
puts("\nBuilt-in commands:\n"
|
|
|
|
"-------------------");
|
2001-07-07 05:35:55 +05:30
|
|
|
|
2007-04-09 08:36:34 +05:30
|
|
|
col = 0;
|
|
|
|
x = builtincmds;
|
|
|
|
while (x->name) {
|
|
|
|
col += printf("%c%s", ((col == 0) ? '\t' : ' '), x->name);
|
2001-07-07 05:35:55 +05:30
|
|
|
if (col > 60) {
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2001-07-07 05:35:55 +05:30
|
|
|
col = 0;
|
|
|
|
}
|
2007-04-09 08:36:34 +05:30
|
|
|
x++;
|
2001-07-07 05:35:55 +05:30
|
|
|
}
|
2007-04-11 04:33:30 +05:30
|
|
|
#if ENABLE_FEATURE_SH_STANDALONE
|
2001-07-07 05:35:55 +05:30
|
|
|
{
|
2007-11-28 12:19:03 +05:30
|
|
|
const char *applet = applet_names;
|
2001-07-07 05:35:55 +05:30
|
|
|
|
2007-11-28 12:19:03 +05:30
|
|
|
while (*applet) {
|
|
|
|
col += printf("%c%s", ((col == 0) ? '\t' : ' '), applet);
|
2001-07-07 05:35:55 +05:30
|
|
|
if (col > 60) {
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2001-07-07 05:35:55 +05:30
|
|
|
col = 0;
|
|
|
|
}
|
2007-11-28 12:19:03 +05:30
|
|
|
applet += strlen(applet) + 1;
|
2001-07-07 05:35:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-12-24 20:53:28 +05:30
|
|
|
puts("\n");
|
2001-07-07 05:35:55 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dolabel(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dochdir(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp, *er;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-02-01 07:09:24 +05:30
|
|
|
if (cp == NULL) {
|
|
|
|
cp = homedir->value;
|
2007-02-09 23:00:14 +05:30
|
|
|
if (cp != NULL)
|
|
|
|
goto do_cd;
|
|
|
|
er = ": no home directory";
|
|
|
|
} else {
|
|
|
|
do_cd:
|
|
|
|
if (chdir(cp) >= 0)
|
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
er = ": bad directory";
|
2007-02-09 23:00:14 +05:30
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(cp != NULL ? cp : "cd");
|
2001-06-29 10:27:14 +05:30
|
|
|
err(er);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doshift(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int n;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
n = args[1] ? getn(args[1]) : 1;
|
2004-08-05 00:46:54 +05:30
|
|
|
if (dolc < n) {
|
2001-06-29 10:27:14 +05:30
|
|
|
err("nothing to shift");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
dolv[n] = dolv[0];
|
|
|
|
dolv += n;
|
|
|
|
dolc -= n;
|
|
|
|
setval(lookup("#"), putn(dolc));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* execute login and newgrp directly
|
|
|
|
*/
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dologin(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
}
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = rexecve(args[0], args, makenv(0, NULL));
|
|
|
|
prs(args[0]);
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(": ");
|
|
|
|
err(cp);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doumask(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-02-12 15:21:03 +05:30
|
|
|
int i;
|
2006-07-01 18:38:46 +05:30
|
|
|
char *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-01-01 11:30:38 +05:30
|
|
|
if (cp == NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
i = umask(0);
|
|
|
|
umask(i);
|
2008-02-12 15:21:03 +05:30
|
|
|
printf("%04o\n", i);
|
2001-06-29 10:27:14 +05:30
|
|
|
} else {
|
2008-02-12 15:21:03 +05:30
|
|
|
i = bb_strtou(cp, NULL, 8);
|
|
|
|
if (errno) {
|
|
|
|
err("umask: bad octal number");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
umask(i);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
static int doexec(struct op *t, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
jmp_buf ex;
|
|
|
|
xint *ofail;
|
2008-03-01 15:05:39 +05:30
|
|
|
char **sv_words;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
t->ioact = NULL;
|
2008-03-01 15:05:39 +05:30
|
|
|
if (!args[1])
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2008-03-01 15:05:39 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
execflg = 1;
|
|
|
|
ofail = failpt;
|
2007-01-01 11:30:38 +05:30
|
|
|
failpt = ex;
|
2008-03-01 15:05:39 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
sv_words = t->op_words;
|
|
|
|
t->op_words = args + 1;
|
2008-03-01 15:05:39 +05:30
|
|
|
// TODO: test what will happen with "exec break" -
|
2008-03-02 18:19:39 +05:30
|
|
|
// will it leave t->op_words pointing to garbage?
|
2008-03-01 15:05:39 +05:30
|
|
|
// (see http://bugs.busybox.net/view.php?id=846)
|
2007-01-01 11:30:38 +05:30
|
|
|
if (setjmp(failpt) == 0)
|
2008-02-12 15:21:03 +05:30
|
|
|
execute(t, NOPIPE, NOPIPE, /* no_fork: */ 1);
|
2008-03-02 18:19:39 +05:30
|
|
|
t->op_words = sv_words;
|
2008-03-01 15:05:39 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
failpt = ofail;
|
|
|
|
execflg = 0;
|
2008-03-01 15:05:39 +05:30
|
|
|
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dodot(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *sp;
|
|
|
|
char *tp;
|
2001-06-29 10:27:14 +05:30
|
|
|
char *cp;
|
2004-08-05 00:49:10 +05:30
|
|
|
int maltmp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("DODOT: enter, t=%p, tleft %p, tright %p, global_env.linep is %s\n",
|
|
|
|
t, t->left, t->right, ((global_env.linep == NULL) ? "NULL" : global_env.linep)));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-01-01 11:30:38 +05:30
|
|
|
if (cp == NULL) {
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF(("DODOT: bad args, ret 0\n"));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
DBGPRINTF(("DODOT: cp is %s\n", cp));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
sp = any('/', cp) ? ":" : path->value;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("DODOT: sp is %s, global_env.linep is %s\n",
|
2004-08-05 00:49:10 +05:30
|
|
|
((sp == NULL) ? "NULL" : sp),
|
2007-12-16 22:51:29 +05:30
|
|
|
((global_env.linep == NULL) ? "NULL" : global_env.linep)));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
while (*sp) {
|
2007-12-16 22:51:29 +05:30
|
|
|
tp = global_env.linep;
|
2001-06-29 10:27:14 +05:30
|
|
|
while (*sp && (*tp = *sp++) != ':')
|
|
|
|
tp++;
|
2007-12-16 22:51:29 +05:30
|
|
|
if (tp != global_env.linep)
|
2001-06-29 10:27:14 +05:30
|
|
|
*tp++ = '/';
|
2008-03-02 18:19:39 +05:30
|
|
|
strcpy(tp, cp);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* Original code */
|
2007-12-16 22:51:29 +05:30
|
|
|
i = open(global_env.linep, O_RDONLY);
|
2007-01-01 11:30:38 +05:30
|
|
|
if (i >= 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
exstat = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
maltmp = remap(i);
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("DODOT: remap=%d, exstat=%d, global_env.iofd %d, i %d, global_env.linep is %s\n",
|
|
|
|
maltmp, exstat, global_env.iofd, i, global_env.linep));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
next(maltmp); /* Basically a PUSHIO */
|
|
|
|
|
|
|
|
DBGPRINTF(("DODOT: returning exstat=%d\n", exstat));
|
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return exstat;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
} /* while */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
prs(cp);
|
|
|
|
err(": not found");
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dowait(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
|
|
|
char *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-01-01 11:30:38 +05:30
|
|
|
if (cp != NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
i = getn(cp);
|
|
|
|
if (i == 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
} else
|
|
|
|
i = -1;
|
|
|
|
setstatus(waitfor(i, 1));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doread(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *cp, **wp;
|
|
|
|
int nb = 0;
|
|
|
|
int nl = 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
if (args[1] == NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
err("Usage: read name ...");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-03-01 15:05:39 +05:30
|
|
|
for (wp = args + 1; *wp; wp++) {
|
2007-12-16 22:51:29 +05:30
|
|
|
for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) {
|
2008-05-19 15:18:17 +05:30
|
|
|
nb = nonblock_safe_read(STDIN_FILENO, cp, sizeof(*cp));
|
2007-02-01 07:09:24 +05:30
|
|
|
if (nb != sizeof(*cp))
|
|
|
|
break;
|
|
|
|
nl = (*cp == '\n');
|
|
|
|
if (nl || (wp[1] && any(*cp, ifs->value)))
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
2007-01-01 11:30:38 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
*cp = '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
if (nb <= 0)
|
|
|
|
break;
|
2007-12-16 22:51:29 +05:30
|
|
|
setval(lookup(*wp), global_env.linep);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return nb <= 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doeval(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-01 15:05:39 +05:30
|
|
|
return RUN(awordlist, args + 1, wdchar);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dotrap(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int n, i;
|
|
|
|
int resetsig;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
if (args[1] == NULL) {
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = 0; i <= _NSIG; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
if (trap[i]) {
|
|
|
|
prn(i);
|
|
|
|
prs(": ");
|
|
|
|
prs(trap[i]);
|
|
|
|
prs("\n");
|
|
|
|
}
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-03-01 15:05:39 +05:30
|
|
|
resetsig = isdigit(args[1][0]);
|
|
|
|
for (i = resetsig ? 1 : 2; args[i] != NULL; ++i) {
|
|
|
|
n = getsig(args[i]);
|
2001-06-29 10:27:14 +05:30
|
|
|
freecell(trap[n]);
|
|
|
|
trap[n] = 0;
|
|
|
|
if (!resetsig) {
|
2008-03-01 15:05:39 +05:30
|
|
|
if (args[1][0] != '\0') {
|
|
|
|
trap[n] = strsave(args[1], 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
setsig(n, sig);
|
|
|
|
} else
|
|
|
|
setsig(n, SIG_IGN);
|
|
|
|
} else {
|
2007-02-01 07:13:16 +05:30
|
|
|
if (interactive) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (n == SIGINT)
|
|
|
|
setsig(n, onintr);
|
|
|
|
else
|
2004-08-05 00:46:54 +05:30
|
|
|
setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
|
2007-02-01 07:13:16 +05:30
|
|
|
} else
|
2001-06-29 10:27:14 +05:30
|
|
|
setsig(n, SIG_DFL);
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int getsig(char *s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int n;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
n = getn(s);
|
|
|
|
if (n < 0 || n > _NSIG) {
|
2001-06-29 10:27:14 +05:30
|
|
|
err("trap: bad signal number");
|
|
|
|
n = 0;
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return n;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void setsig(int n, sighandler_t f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
|
|
|
|
ourtrap[n] = 1;
|
|
|
|
signal(n, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int getn(char *as)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *s;
|
|
|
|
int n, m;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
s = as;
|
|
|
|
m = 1;
|
|
|
|
if (*s == '-') {
|
|
|
|
m = -1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
for (n = 0; isdigit(*s); s++)
|
2004-08-05 00:46:54 +05:30
|
|
|
n = (n * 10) + (*s - '0');
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*s) {
|
|
|
|
prs(as);
|
|
|
|
err(": bad number");
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return n * m;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dobreak(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-01 15:05:39 +05:30
|
|
|
return brkcontin(args[1], 1);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int docontinue(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-01 15:05:39 +05:30
|
|
|
return brkcontin(args[1], 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int brkcontin(char *cp, int val)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct brkcon *bc;
|
|
|
|
int nl;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
nl = cp == NULL ? 1 : getn(cp);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (nl <= 0)
|
|
|
|
nl = 999;
|
|
|
|
do {
|
2007-02-01 07:09:24 +05:30
|
|
|
bc = brklist;
|
|
|
|
if (bc == NULL)
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
brklist = bc->nextlev;
|
|
|
|
} while (--nl);
|
|
|
|
if (nl) {
|
|
|
|
err("bad break/continue level");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-02-12 11:34:06 +05:30
|
|
|
isbreak = (val != 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
longjmp(bc->brkpt, 1);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doexit(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
execflg = 0;
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-02-01 07:09:24 +05:30
|
|
|
if (cp != NULL)
|
2001-06-29 10:27:14 +05:30
|
|
|
setstatus(getn(cp));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF(("DOEXIT: calling leave(), t=%p\n", t));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
leave();
|
|
|
|
/* NOTREACHED */
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doexport(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-01 15:05:39 +05:30
|
|
|
rdexp(args + 1, export, EXPORT);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doreadonly(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-01 15:05:39 +05:30
|
|
|
rdexp(args + 1, ronly, RONLY);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static void rdexp(char **wp, void (*f) (struct var *), int key)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF6(("RDEXP: enter, wp=%p, func=%p, key=%d\n", wp, f, key));
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF6(("RDEXP: *wp=%s\n", *wp));
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*wp != NULL) {
|
2001-07-13 01:09:59 +05:30
|
|
|
for (; *wp != NULL; wp++) {
|
|
|
|
if (isassign(*wp)) {
|
|
|
|
char *cp;
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2001-07-13 01:09:59 +05:30
|
|
|
assign(*wp, COPYV);
|
2008-03-02 18:19:39 +05:30
|
|
|
for (cp = *wp; *cp != '='; cp++)
|
|
|
|
continue;
|
2001-07-13 01:09:59 +05:30
|
|
|
*cp = '\0';
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
if (checkname(*wp))
|
2004-08-05 00:46:54 +05:30
|
|
|
(*f) (lookup(*wp));
|
2001-06-29 10:27:14 +05:30
|
|
|
else
|
|
|
|
badid(*wp);
|
2001-07-13 01:09:59 +05:30
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
} else
|
|
|
|
putvlist(key, 1);
|
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void badid(char *s)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
prs(s);
|
|
|
|
err(": bad identifier");
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int doset(struct op *t ATTRIBUTE_UNUSED, char **args)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct var *vp;
|
|
|
|
char *cp;
|
|
|
|
int n;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-01 15:05:39 +05:30
|
|
|
cp = args[1];
|
2007-01-01 11:30:38 +05:30
|
|
|
if (cp == NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
|
|
|
varput(vp->name, 1);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
if (*cp == '-') {
|
2008-03-01 15:05:39 +05:30
|
|
|
args++;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*++cp == 0)
|
2007-04-17 04:04:39 +05:30
|
|
|
FLAG['x'] = FLAG['v'] = 0;
|
2007-02-01 07:09:24 +05:30
|
|
|
else {
|
|
|
|
for (; *cp; cp++) {
|
2001-06-29 10:27:14 +05:30
|
|
|
switch (*cp) {
|
|
|
|
case 'e':
|
|
|
|
if (!interactive)
|
2007-04-17 04:04:39 +05:30
|
|
|
FLAG['e']++;
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-08-05 00:46:54 +05:30
|
|
|
if (*cp >= 'a' && *cp <= 'z')
|
2007-04-17 04:04:39 +05:30
|
|
|
FLAG[(int) *cp]++;
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
setdash();
|
|
|
|
}
|
2008-03-01 15:05:39 +05:30
|
|
|
if (args[1]) {
|
|
|
|
args[0] = dolv[0];
|
|
|
|
for (n = 1; args[n]; n++)
|
|
|
|
setarea((char *) args[n], 0);
|
2004-08-05 00:46:54 +05:30
|
|
|
dolc = n - 1;
|
2008-03-01 15:05:39 +05:30
|
|
|
dolv = args;
|
2001-06-29 10:27:14 +05:30
|
|
|
setval(lookup("#"), putn(dolc));
|
2004-08-05 00:46:54 +05:30
|
|
|
setarea((char *) (dolv - 1), 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void varput(char *s, int out)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2001-08-06 19:44:18 +05:30
|
|
|
if (isalnum(*s) || *s == '_') {
|
2001-06-29 10:27:14 +05:30
|
|
|
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.
|
|
|
|
*/
|
2008-03-01 15:05:39 +05:30
|
|
|
static void times_fmt(char *buf, clock_t val, unsigned clk_tck)
|
|
|
|
{
|
|
|
|
unsigned min, sec;
|
|
|
|
if (sizeof(val) > sizeof(int))
|
|
|
|
sec = ((unsigned long)val) / clk_tck;
|
|
|
|
else
|
|
|
|
sec = ((unsigned)val) / clk_tck;
|
|
|
|
min = sec / 60;
|
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
sprintf(buf, "%um%u.%03us", min, (sec - min * 60),
|
|
|
|
/* msec: */ ((unsigned)(val - (clock_t)sec * clk_tck)) * 1000 / clk_tck
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
sprintf(buf, "%um%us", min, (sec - min * 60));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:30:54 +05:30
|
|
|
static int dotimes(struct op *t ATTRIBUTE_UNUSED, char **args ATTRIBUTE_UNUSED)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
struct tms buf;
|
2008-03-01 15:05:39 +05:30
|
|
|
unsigned clk_tck = sysconf(_SC_CLK_TCK);
|
|
|
|
/* How much do we need for "NmN.NNNs" ? */
|
|
|
|
enum { TIMEBUF_SIZE = sizeof(int)*3 + sizeof(int)*3 + 6 };
|
|
|
|
char u[TIMEBUF_SIZE], s[TIMEBUF_SIZE];
|
|
|
|
char cu[TIMEBUF_SIZE], cs[TIMEBUF_SIZE];
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
times(&buf);
|
2008-03-01 15:05:39 +05:30
|
|
|
|
|
|
|
times_fmt(u, buf.tms_utime, clk_tck);
|
|
|
|
times_fmt(s, buf.tms_stime, clk_tck);
|
|
|
|
times_fmt(cu, buf.tms_cutime, clk_tck);
|
|
|
|
times_fmt(cs, buf.tms_cstime, clk_tck);
|
|
|
|
|
|
|
|
printf("%s %s\n%s %s\n", u, s, cu, cs);
|
2001-06-29 10:27:14 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- eval.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ${}
|
|
|
|
* `command`
|
|
|
|
* blank interpretation
|
|
|
|
* quoting
|
|
|
|
* glob
|
|
|
|
*/
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static char **eval(char **ap, int f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
struct wdblock *wb;
|
|
|
|
char **wp;
|
|
|
|
char **wf;
|
|
|
|
jmp_buf ev;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) ℘
|
|
|
|
(void) ≈
|
|
|
|
#endif
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF4(("EVAL: enter, f=%d\n", f));
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
wp = NULL;
|
|
|
|
wb = NULL;
|
|
|
|
wf = NULL;
|
2007-01-01 11:30:38 +05:30
|
|
|
errpt = ev;
|
|
|
|
if (newenv(setjmp(errpt)) == 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
while (*ap && isassign(*ap))
|
|
|
|
expand(*ap++, &wb, f & ~DOGLOB);
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['k']) {
|
2001-06-29 10:27:14 +05:30
|
|
|
for (wf = ap; *wf; wf++) {
|
|
|
|
if (isassign(*wf))
|
|
|
|
expand(*wf, &wb, f & ~DOGLOB);
|
|
|
|
}
|
|
|
|
}
|
2008-02-12 11:34:06 +05:30
|
|
|
for (wb = addword((char *) NULL, wb); *ap; ap++) {
|
2007-04-17 04:04:39 +05:30
|
|
|
if (!FLAG['k'] || !isassign(*ap))
|
2001-06-29 10:27:14 +05:30
|
|
|
expand(*ap, &wb, f & ~DOKEY);
|
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
wb = addword((char *) 0, wb);
|
2001-06-29 10:27:14 +05:30
|
|
|
wp = getwords(wb);
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
gflg = 1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return gflg ? (char **) NULL : wp;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* Make the exported environment from the exported
|
|
|
|
* names in the dictionary. Keyword assignments
|
|
|
|
* will already have been done.
|
|
|
|
*/
|
2004-09-03 04:43:10 +05:30
|
|
|
static char **makenv(int all, struct wdblock *wb)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct var *vp;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF5(("MAKENV: enter, all=%d\n", all));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
for (vp = vlist; vp; vp = vp->next)
|
2004-08-05 00:49:10 +05:30
|
|
|
if (all || vp->status & EXPORT)
|
2001-06-29 10:27:14 +05:30
|
|
|
wb = addword(vp->name, wb);
|
2004-08-05 00:46:54 +05:30
|
|
|
wb = addword((char *) 0, wb);
|
2006-11-27 22:19:55 +05:30
|
|
|
return getwords(wb);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static int expand(const char *cp, struct wdblock **wbp, int f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
jmp_buf ev;
|
2007-02-01 07:13:54 +05:30
|
|
|
char *xp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &cp;
|
|
|
|
#endif
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF3(("EXPAND: enter, f=%d\n", f));
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
gflg = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (cp == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
if (!anys("$`'\"", cp) && !anys(ifs->value, cp)
|
|
|
|
&& ((f & DOGLOB) == 0 || !anys("[*?", cp))
|
|
|
|
) {
|
|
|
|
xp = strsave(cp, areanum);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (f & DOTRIM)
|
2007-02-01 07:13:54 +05:30
|
|
|
unquote(xp);
|
|
|
|
*wbp = addword(xp, *wbp);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
errpt = ev;
|
|
|
|
if (newenv(setjmp(errpt)) == 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
PUSHIO(aword, cp, strchar);
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iobase = global_env.iop;
|
2007-02-01 07:13:54 +05:30
|
|
|
while ((xp = blank(f)) && gflg == 0) {
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = xp;
|
2007-02-01 07:13:54 +05:30
|
|
|
xp = strsave(xp, areanum);
|
2004-08-05 00:46:54 +05:30
|
|
|
if ((f & DOGLOB) == 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (f & DOTRIM)
|
2007-02-01 07:13:54 +05:30
|
|
|
unquote(xp);
|
|
|
|
*wbp = addword(xp, *wbp);
|
2001-06-29 10:27:14 +05:30
|
|
|
} else
|
2007-02-01 07:13:54 +05:30
|
|
|
*wbp = glob(xp, *wbp);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
gflg = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return gflg == 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
static char *evalstr(char *cp, int f)
|
|
|
|
{
|
|
|
|
struct wdblock *wb;
|
|
|
|
|
|
|
|
DBGPRINTF6(("EVALSTR: enter, cp=%p, f=%d\n", cp, f));
|
|
|
|
|
|
|
|
wb = NULL;
|
|
|
|
if (expand(cp, &wb, f)) {
|
|
|
|
if (wb == NULL || wb->w_nword == 0
|
|
|
|
|| (cp = wb->w_words[0]) == NULL
|
|
|
|
) {
|
2007-03-07 15:05:43 +05:30
|
|
|
// TODO: I suspect that
|
2007-02-01 07:13:54 +05:30
|
|
|
// char *evalstr(char *cp, int f) is actually
|
|
|
|
// const char *evalstr(const char *cp, int f)!
|
|
|
|
cp = (char*)"";
|
|
|
|
}
|
|
|
|
DELETE(wb);
|
|
|
|
} else
|
|
|
|
cp = NULL;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* Blank interpretation and quoting
|
|
|
|
*/
|
2006-06-26 03:38:53 +05:30
|
|
|
static char *blank(int f)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c, c1;
|
|
|
|
char *sp;
|
2001-06-29 10:27:14 +05:30
|
|
|
int scanequals, foundequals;
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF3(("BLANK: enter, f=%d\n", f));
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
sp = global_env.linep;
|
2001-06-29 10:27:14 +05:30
|
|
|
scanequals = f & DOKEY;
|
|
|
|
foundequals = 0;
|
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
loop:
|
|
|
|
c = subgetc('"', foundequals);
|
|
|
|
switch (c) {
|
2001-06-29 10:27:14 +05:30
|
|
|
case 0:
|
2007-12-16 22:51:29 +05:30
|
|
|
if (sp == global_env.linep)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = 0;
|
2006-11-27 22:19:55 +05:30
|
|
|
return sp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
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;
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
unget(c);
|
2001-08-06 19:44:18 +05:30
|
|
|
if (!isalpha(c) && c != '_')
|
2001-06-29 10:27:14 +05:30
|
|
|
scanequals = 0;
|
|
|
|
for (;;) {
|
|
|
|
c = subgetc('"', foundequals);
|
|
|
|
if (c == 0 ||
|
2004-08-05 00:46:54 +05:30
|
|
|
f & (DOBLANK && any(c, ifs->value)) ||
|
|
|
|
(!INSUB() && any(c, "\"'"))) {
|
|
|
|
scanequals = 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
unget(c);
|
|
|
|
if (any(c, "\"'"))
|
|
|
|
goto loop;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scanequals) {
|
|
|
|
if (c == '=') {
|
|
|
|
foundequals = 1;
|
2004-08-05 00:46:54 +05:30
|
|
|
scanequals = 0;
|
|
|
|
} else if (!isalnum(c) && c != '_')
|
2001-06-29 10:27:14 +05:30
|
|
|
scanequals = 0;
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = 0;
|
2006-11-27 22:19:55 +05:30
|
|
|
return sp;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get characters, substituting for ` and $
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int subgetc(char ec, int quoted)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
again:
|
2001-06-29 10:27:14 +05:30
|
|
|
c = my_getc(ec);
|
|
|
|
if (!INSUB() && ec != '\'') {
|
|
|
|
if (c == '`') {
|
|
|
|
if (grave(quoted) == 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XGRAVE;
|
2001-06-29 10:27:14 +05:30
|
|
|
goto again;
|
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
if (c == '$') {
|
|
|
|
c = dollar(quoted);
|
|
|
|
if (c == 0) {
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XDOLL;
|
2007-02-01 07:09:24 +05:30
|
|
|
goto again;
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to generate the string returned by ${} substitution.
|
|
|
|
*/
|
2006-06-26 03:38:53 +05:30
|
|
|
static int dollar(int quoted)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
int otask;
|
|
|
|
struct io *oiop;
|
|
|
|
char *dolp;
|
2006-07-01 18:38:46 +05:30
|
|
|
char *s, c, *cp = NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
struct var *vp;
|
|
|
|
|
2004-08-05 00:49:10 +05:30
|
|
|
DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted));
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
c = readc();
|
2007-12-16 22:51:29 +05:30
|
|
|
s = global_env.linep;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (c != '{') {
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep++ = c;
|
2001-08-06 19:44:18 +05:30
|
|
|
if (isalpha(c) || c == '_') {
|
2004-08-05 00:46:54 +05:30
|
|
|
while ((c = readc()) != 0 && (isalnum(c) || c == '_'))
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.linep < elinep)
|
|
|
|
*global_env.linep++ = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
unget(c);
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
} else {
|
2007-12-16 22:51:29 +05:30
|
|
|
oiop = global_env.iop;
|
|
|
|
otask = global_env.iop->task;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XOTHER;
|
2004-08-05 00:46:54 +05:30
|
|
|
while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n')
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.linep < elinep)
|
|
|
|
*global_env.linep++ = c;
|
|
|
|
if (oiop == global_env.iop)
|
|
|
|
global_env.iop->task = otask;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (c != '}') {
|
|
|
|
err("unclosed ${");
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.linep >= elinep) {
|
2001-06-29 10:27:14 +05:30
|
|
|
err("string in ${} too long");
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep -= 10;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
*global_env.linep = 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*s)
|
2004-08-05 00:46:54 +05:30
|
|
|
for (cp = s + 1; *cp; cp++)
|
2001-06-29 10:27:14 +05:30
|
|
|
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 */
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = s;
|
2004-08-05 00:46:54 +05:30
|
|
|
PUSHIO(awordlist, dolv + 1, dolchar);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:46:54 +05:30
|
|
|
} else { /* trap the nasty ${=} */
|
2001-06-29 10:27:14 +05:30
|
|
|
s[0] = '1';
|
2007-02-01 07:09:24 +05:30
|
|
|
s[1] = '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
vp = lookup(s);
|
2007-02-01 07:09:24 +05:30
|
|
|
dolp = vp->value;
|
|
|
|
if (dolp == null) {
|
2001-06-29 10:27:14 +05:30
|
|
|
switch (c) {
|
|
|
|
case '=':
|
|
|
|
if (isdigit(*s)) {
|
|
|
|
err("cannot use ${...=...} with $n");
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
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);
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (c == '+')
|
|
|
|
dolp = strsave(cp, areanum);
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['u'] && dolp == null) {
|
2001-06-29 10:27:14 +05:30
|
|
|
prs("unset variable: ");
|
|
|
|
err(s);
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.linep = s;
|
2001-06-29 10:27:14 +05:30
|
|
|
PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the command in `...` and read its output.
|
|
|
|
*/
|
2003-03-14 21:35:59 +05:30
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int grave(int quoted)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-03-25 04:12:29 +05:30
|
|
|
/* moved to G: static char child_cmd[LINELIM]; */
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *cp;
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2003-03-14 21:35:59 +05:30
|
|
|
int j;
|
2001-06-29 10:27:14 +05:30
|
|
|
int pf[2];
|
2007-02-01 07:13:54 +05:30
|
|
|
const char *src;
|
2003-03-14 21:35:59 +05:30
|
|
|
char *dest;
|
|
|
|
int count;
|
|
|
|
int ignore;
|
|
|
|
int ignore_once;
|
|
|
|
char *argument_list[4];
|
2004-09-03 04:43:10 +05:30
|
|
|
struct wdblock *wb = NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &cp;
|
|
|
|
#endif
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
for (cp = global_env.iop->argp->aword; *cp != '`'; cp++) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*cp == 0) {
|
|
|
|
err("no closing `");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:13:54 +05:30
|
|
|
}
|
2003-03-14 21:35:59 +05:30
|
|
|
|
|
|
|
/* string copy with dollar expansion */
|
2007-12-16 22:51:29 +05:30
|
|
|
src = global_env.iop->argp->aword;
|
2003-03-14 21:35:59 +05:30
|
|
|
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;
|
2007-12-02 14:05:37 +05:30
|
|
|
/* moved to G to reduce stack usage
|
2003-03-14 21:35:59 +05:30
|
|
|
char var_name[LINELIM];
|
|
|
|
char alt_value[LINELIM];
|
2007-12-02 14:05:37 +05:30
|
|
|
*/
|
|
|
|
#define var_name (G.grave__var_name)
|
|
|
|
#define alt_value (G.grave__alt_value)
|
2003-03-14 21:35:59 +05:30
|
|
|
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-21 00:03:12 +05:30
|
|
|
while (isalnum(*src) || *src=='_')
|
2003-03-14 21:35:59 +05:30
|
|
|
var_name[var_index++] = *src++;
|
|
|
|
var_name[var_index] = 0;
|
|
|
|
|
|
|
|
if (braces) {
|
|
|
|
switch (*src) {
|
|
|
|
case '}':
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
case '=':
|
|
|
|
case '+':
|
|
|
|
case '?':
|
2004-08-05 00:46:54 +05:30
|
|
|
operator = * src;
|
2003-03-14 21:35:59 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err("unclosed ${\n");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2003-03-14 21:35:59 +05:30
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
if (operator) {
|
2003-03-14 21:35:59 +05:30
|
|
|
src++;
|
|
|
|
while (*src && (*src != '}')) {
|
|
|
|
alt_value[alt_index++] = *src++;
|
|
|
|
}
|
|
|
|
alt_value[alt_index] = 0;
|
|
|
|
if (*src != '}') {
|
|
|
|
err("unclosed ${\n");
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2003-03-14 21:35:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
2004-09-03 04:43:10 +05:30
|
|
|
if (isalpha(*var_name)) {
|
|
|
|
/* let subshell handle it instead */
|
2003-03-14 21:35:59 +05:30
|
|
|
|
2004-09-03 04:43:10 +05:30
|
|
|
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);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-09-03 04:43:10 +05:30
|
|
|
} 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 21:35:59 +05:30
|
|
|
}
|
2007-12-02 14:05:37 +05:30
|
|
|
#undef var_name
|
|
|
|
#undef alt_value
|
2003-03-14 21:35:59 +05:30
|
|
|
} else {
|
|
|
|
*dest++ = *src++;
|
|
|
|
count++;
|
|
|
|
ignore_once = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (openpipe(pf) < 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
while ((i = vfork()) == -1 && errno == EAGAIN)
|
|
|
|
continue;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF3(("GRAVE: i is %p\n", io));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2003-03-14 21:35:59 +05:30
|
|
|
if (i < 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
closepipe(pf);
|
2004-08-05 00:46:54 +05:30
|
|
|
err((char *) bb_msg_memory_exhausted);
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
if (i != 0) {
|
2008-01-03 01:25:04 +05:30
|
|
|
waitpid(i, NULL, 0); // safe_waitpid?
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->argp->aword = ++cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
close(pf[1]);
|
2004-08-05 00:46:54 +05:30
|
|
|
PUSHIO(afile, remap(pf[0]),
|
2007-02-01 07:13:54 +05:30
|
|
|
(int (*)(struct ioarg *)) ((quoted) ? qgravechar : gravechar));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
/* allow trapped signals */
|
2003-03-14 21:35:59 +05:30
|
|
|
/* XXX - Maybe this signal stuff should go as well? */
|
2004-08-05 00:46:54 +05:30
|
|
|
for (j = 0; j <= _NSIG; j++)
|
2003-03-14 21:35:59 +05:30
|
|
|
if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(j, SIG_DFL);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-05-10 03:46:08 +05:30
|
|
|
/* Testcase where below checks are needed:
|
|
|
|
* close stdout & run this script:
|
|
|
|
* files=`ls`
|
|
|
|
* echo "$files" >zz
|
|
|
|
*/
|
|
|
|
xmove_fd(pf[1], 1);
|
2008-01-29 04:15:43 +05:30
|
|
|
if (pf[0] != 1)
|
|
|
|
close(pf[0]);
|
2003-03-14 21:35:59 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
argument_list[0] = (char *) DEFAULT_SHELL;
|
2007-02-01 07:13:54 +05:30
|
|
|
argument_list[1] = (char *) "-c";
|
2003-03-14 21:35:59 +05:30
|
|
|
argument_list[2] = child_cmd;
|
2007-02-01 07:13:54 +05:30
|
|
|
argument_list[3] = NULL;
|
2003-03-14 21:35:59 +05:30
|
|
|
|
2004-09-03 04:43:10 +05:30
|
|
|
cp = rexecve(argument_list[0], argument_list, makenv(1, wb));
|
2004-08-05 00:49:10 +05:30
|
|
|
prs(argument_list[0]);
|
|
|
|
prs(": ");
|
|
|
|
err(cp);
|
2008-05-19 14:59:47 +05:30
|
|
|
_exit(EXIT_FAILURE);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2003-03-14 21:35:59 +05:30
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static char *unquote(char *as)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *s;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
s = as;
|
|
|
|
if (s != NULL)
|
2001-06-29 10:27:14 +05:30
|
|
|
while (*s)
|
|
|
|
*s++ &= ~QUOTE;
|
2006-11-27 22:19:55 +05:30
|
|
|
return as;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* -------- glob.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* glob
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define scopy(x) strsave((x), areanum)
|
|
|
|
#define BLKSIZ 512
|
|
|
|
#define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static struct wdblock *cl, *nl;
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char spcl[] ALIGN1= "[?*";
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct wdblock *glob(char *cp, struct wdblock *wb)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
|
|
|
char *pp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (cp == 0)
|
2006-11-27 22:19:55 +05:30
|
|
|
return wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
i = 0;
|
|
|
|
for (pp = cp; *pp; pp++)
|
|
|
|
if (any(*pp, spcl))
|
|
|
|
i++;
|
|
|
|
else if (!any(*pp & ~QUOTE, spcl))
|
|
|
|
*pp &= ~QUOTE;
|
|
|
|
if (i != 0) {
|
2007-02-01 07:09:24 +05:30
|
|
|
for (cl = addword(scopy(cp), NULL); anyspcl(cl); cl = nl) {
|
2004-08-05 00:46:54 +05:30
|
|
|
nl = newword(cl->w_nword * 2);
|
|
|
|
for (i = 0; i < cl->w_nword; i++) { /* for each argument */
|
2001-06-29 10:27:14 +05:30
|
|
|
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-05 00:46:54 +05:30
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
DELETE(cl->w_words[i]);
|
|
|
|
DELETE(cl);
|
|
|
|
}
|
|
|
|
if (cl->w_nword) {
|
2008-03-02 18:21:26 +05:30
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
|
|
|
unquote(cl->w_words[i]);
|
|
|
|
qsort_string_vector(cl->w_words, cl->w_nword);
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = 0; i < cl->w_nword; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
wb = addword(cl->w_words[i], wb);
|
|
|
|
DELETE(cl);
|
2006-11-27 22:19:55 +05:30
|
|
|
return wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
wb = addword(unquote(cp), wb);
|
2006-11-27 22:19:55 +05:30
|
|
|
return wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void globname(char *we, char *pp)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *np, *cp;
|
2001-06-29 10:27:14 +05:30
|
|
|
char *name, *gp, *dp;
|
|
|
|
int k;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *de;
|
2004-08-05 00:46:54 +05:30
|
|
|
char dname[NAME_MAX + 1];
|
2001-06-29 10:27:14 +05:30
|
|
|
struct stat dbuf;
|
|
|
|
|
|
|
|
for (np = we; np != pp; pp--)
|
|
|
|
if (pp[-1] == '/')
|
|
|
|
break;
|
2008-03-02 18:21:26 +05:30
|
|
|
dp = cp = get_space((int) (pp - np) + 3);
|
|
|
|
while (np < pp)
|
2001-06-29 10:27:14 +05:30
|
|
|
*cp++ = *np++;
|
|
|
|
*cp++ = '.';
|
|
|
|
*cp = '\0';
|
2008-03-02 18:21:26 +05:30
|
|
|
gp = cp = get_space(strlen(pp) + 1);
|
|
|
|
while (*np && *np != '/')
|
2001-06-29 10:27:14 +05:30
|
|
|
*cp++ = *np++;
|
|
|
|
*cp = '\0';
|
|
|
|
dirp = opendir(dp);
|
|
|
|
if (dirp == 0) {
|
|
|
|
DELETE(dp);
|
|
|
|
DELETE(gp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dname[NAME_MAX] = '\0';
|
2004-08-05 00:46:54 +05:30
|
|
|
while ((de = readdir(dirp)) != NULL) {
|
2001-06-29 10:27:14 +05:30
|
|
|
/* XXX Hmmm... What this could be? (abial) */
|
|
|
|
/*
|
2004-08-05 00:46:54 +05:30
|
|
|
if (ent[j].d_ino == 0)
|
2007-02-01 07:09:24 +05:30
|
|
|
continue;
|
2004-08-05 00:46:54 +05:30
|
|
|
*/
|
2001-06-29 10:27:14 +05:30
|
|
|
strncpy(dname, de->d_name, NAME_MAX);
|
2004-08-05 00:46:54 +05:30
|
|
|
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 10:27:14 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
nl = addword(name, nl);
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
DELETE(dp);
|
|
|
|
DELETE(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate a pathname as below.
|
|
|
|
* start..end1 / middle end
|
|
|
|
* the slashes come for free
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static char *generate(char *start1, char *end1, char *middle, char *end)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
char *p;
|
2006-07-01 18:38:46 +05:30
|
|
|
char *op, *xp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
p = op = get_space((int)(end1 - start1) + strlen(middle) + strlen(end) + 2);
|
|
|
|
xp = start1;
|
|
|
|
while (xp != end1)
|
|
|
|
*op++ = *xp++;
|
|
|
|
xp = middle;
|
|
|
|
while (*xp != '\0')
|
2001-06-29 10:27:14 +05:30
|
|
|
*op++ = *xp++;
|
2008-03-02 18:19:39 +05:30
|
|
|
strcpy(op, end);
|
2006-11-27 22:19:55 +05:30
|
|
|
return p;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int anyspcl(struct wdblock *wb)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
|
|
|
char **wd;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
wd = wb->w_words;
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = 0; i < wb->w_nword; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
if (anys(spcl, *wd++))
|
2006-11-27 22:19:31 +05:30
|
|
|
return 1;
|
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- word.c -------- */
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static struct wdblock *newword(int nw)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct wdblock *wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
wb = get_space(sizeof(*wb) + nw * sizeof(char *));
|
2001-06-29 10:27:14 +05:30
|
|
|
wb->w_bsize = nw;
|
|
|
|
wb->w_nword = 0;
|
2006-11-27 22:19:55 +05:30
|
|
|
return wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static struct wdblock *addword(char *wd, struct wdblock *wb)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct wdblock *wb2;
|
|
|
|
int nw;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (wb == NULL)
|
|
|
|
wb = newword(NSTART);
|
2007-01-01 11:30:38 +05:30
|
|
|
nw = wb->w_nword;
|
|
|
|
if (nw >= wb->w_bsize) {
|
2001-06-29 10:27:14 +05:30
|
|
|
wb2 = newword(nw * 2);
|
2004-08-05 00:46:54 +05:30
|
|
|
memcpy((char *) wb2->w_words, (char *) wb->w_words,
|
|
|
|
nw * sizeof(char *));
|
2001-06-29 10:27:14 +05:30
|
|
|
wb2->w_nword = nw;
|
|
|
|
DELETE(wb);
|
|
|
|
wb = wb2;
|
|
|
|
}
|
|
|
|
wb->w_words[wb->w_nword++] = wd;
|
2006-11-27 22:19:55 +05:30
|
|
|
return wb;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
|
2007-04-14 20:38:41 +05:30
|
|
|
static char **getwords(struct wdblock *wb)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char **wd;
|
|
|
|
int nb;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (wb == NULL)
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (wb->w_nword == 0) {
|
|
|
|
DELETE(wb);
|
2006-11-27 22:19:55 +05:30
|
|
|
return NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2008-03-02 18:19:39 +05:30
|
|
|
nb = sizeof(*wd) * wb->w_nword;
|
|
|
|
wd = get_space(nb);
|
2008-03-02 18:21:26 +05:30
|
|
|
memcpy(wd, wb->w_words, nb);
|
|
|
|
DELETE(wb); /* perhaps should done by caller */
|
2006-11-27 22:19:55 +05:30
|
|
|
return wd;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -------- io.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shell IO
|
|
|
|
*/
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static int my_getc(int ec)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.linep > elinep) {
|
2008-03-02 18:19:39 +05:30
|
|
|
while ((c = readc()) != '\n' && c)
|
|
|
|
continue;
|
2001-06-29 10:27:14 +05:30
|
|
|
err("input line too long");
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
c = readc();
|
2007-12-16 22:51:29 +05:30
|
|
|
if ((ec != '\'') && (ec != '`') && (global_env.iop->task != XGRAVE)) {
|
2004-08-05 00:46:54 +05:30
|
|
|
if (c == '\\') {
|
2001-06-29 10:27:14 +05:30
|
|
|
c = readc();
|
|
|
|
if (c == '\n' && ec != '\"')
|
2006-11-27 22:19:55 +05:30
|
|
|
return my_getc(ec);
|
2001-06-29 10:27:14 +05:30
|
|
|
c |= QUOTE;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void unget(int c)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop >= global_env.iobase)
|
|
|
|
global_env.iop->peekc = c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int eofc(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-12-16 22:51:29 +05:30
|
|
|
return global_env.iop < global_env.iobase || (global_env.iop->peekc == 0 && global_env.iop->prev == 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static int readc(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
RCPRINTF(("READC: global_env.iop %p, global_env.iobase %p\n", global_env.iop, global_env.iobase));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
for (; global_env.iop >= global_env.iobase; global_env.iop--) {
|
|
|
|
RCPRINTF(("READC: global_env.iop %p, peekc 0x%x\n", global_env.iop, global_env.iop->peekc));
|
|
|
|
c = global_env.iop->peekc;
|
2007-01-01 11:30:38 +05:30
|
|
|
if (c != '\0') {
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->peekc = 0;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop->prev != 0) {
|
|
|
|
c = (*global_env.iop->iofn)(global_env.iop->argp, global_env.iop);
|
2007-02-01 07:09:24 +05:30
|
|
|
if (c != '\0') {
|
|
|
|
if (c == -1) {
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop++;
|
2007-02-01 07:09:24 +05:30
|
|
|
continue;
|
2004-08-05 00:46:54 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop == iostack)
|
2007-02-01 07:09:24 +05:30
|
|
|
ioecho(c);
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->prev = c;
|
2008-03-02 20:03:26 +05:30
|
|
|
return c;
|
2004-08-05 00:46:54 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop->task == XIO && global_env.iop->prev != '\n') {
|
|
|
|
global_env.iop->prev = 0;
|
|
|
|
if (global_env.iop == iostack)
|
2007-02-01 07:09:24 +05:30
|
|
|
ioecho('\n');
|
|
|
|
return '\n';
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop->task == XIO) {
|
2007-02-01 07:09:24 +05:30
|
|
|
if (multiline) {
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->prev = 0;
|
2008-03-02 20:03:26 +05:30
|
|
|
return 0;
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && global_env.iop == iostack + 1) {
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
2007-02-01 07:09:24 +05:30
|
|
|
current_prompt = prompt->value;
|
2001-06-29 10:27:14 +05:30
|
|
|
#else
|
2007-02-01 07:09:24 +05:30
|
|
|
prs(prompt->value);
|
2001-06-29 10:27:14 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
} /* FOR */
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop >= iostack) {
|
|
|
|
RCPRINTF(("READC: return 0, global_env.iop %p\n", global_env.iop));
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("READC: leave()...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
leave();
|
|
|
|
/* NOTREACHED */
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void ioecho(char c)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-04-17 04:04:39 +05:30
|
|
|
if (FLAG['v'])
|
2008-05-19 15:18:17 +05:30
|
|
|
write(STDERR_FILENO, &c, sizeof c);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("PUSHIO: argp %p, argp->afid 0x%x, global_env.iop %p\n", argp,
|
|
|
|
argp->afid, global_env.iop));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* Set env ptr for io source to next array spot and check for array overflow */
|
2007-12-16 22:51:29 +05:30
|
|
|
if (++global_env.iop >= &iostack[NPUSH]) {
|
|
|
|
global_env.iop--;
|
2001-06-29 10:27:14 +05:30
|
|
|
err("Shell input nested too deeply");
|
2008-02-12 11:34:06 +05:30
|
|
|
gflg = 1;
|
2001-06-29 10:27:14 +05:30
|
|
|
return;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* We did not overflow the NPUSH array spots so setup data structs */
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn; /* Store data source func ptr */
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (argp->afid != AFID_NOBUF)
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->argp = argp;
|
2001-06-29 10:27:14 +05:30
|
|
|
else {
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->argp = ioargstack + (global_env.iop - iostack); /* MAL - index into stack */
|
|
|
|
*global_env.iop->argp = *argp; /* copy data from temp area into stack spot */
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop == &iostack[0])
|
|
|
|
global_env.iop->argp->afbuf = &mainbuf;
|
2004-08-05 00:49:10 +05:30
|
|
|
else
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->argp->afbuf = &sharedbuf;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
/* 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 */
|
2007-12-16 22:51:29 +05:30
|
|
|
if ((isatty(global_env.iop->argp->afile) == 0)
|
|
|
|
&& (global_env.iop == &iostack[0]
|
|
|
|
|| lseek(global_env.iop->argp->afile, 0L, SEEK_CUR) != -1)) {
|
2004-08-05 00:49:10 +05:30
|
|
|
if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */
|
|
|
|
bufid = AFID_ID; /* AFID_ID = 0 */
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->argp->afid = bufid; /* assign buffer id */
|
2004-08-05 00:46:54 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("PUSHIO: iostack %p, global_env.iop %p, afbuf %p\n",
|
|
|
|
iostack, global_env.iop, global_env.iop->argp->afbuf));
|
|
|
|
DBGPRINTF(("PUSHIO: mbuf %p, sbuf %p, bid %d, global_env.iop %p\n",
|
|
|
|
&mainbuf, &sharedbuf, bufid, global_env.iop));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->prev = ~'\n';
|
|
|
|
global_env.iop->peekc = 0;
|
|
|
|
global_env.iop->xchar = 0;
|
|
|
|
global_env.iop->nlcount = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (fn == filechar || fn == linechar)
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XIO;
|
2004-08-05 00:46:54 +05:30
|
|
|
else if (fn == (int (*)(struct ioarg *)) gravechar
|
2008-03-02 20:03:26 +05:30
|
|
|
|| fn == (int (*)(struct ioarg *)) qgravechar)
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XGRAVE;
|
2001-06-29 10:27:14 +05:30
|
|
|
else
|
2007-12-16 22:51:29 +05:30
|
|
|
global_env.iop->task = XOTHER;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static struct io *setbase(struct io *ip)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct io *xp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
xp = global_env.iobase;
|
|
|
|
global_env.iobase = ip;
|
2006-11-27 22:19:55 +05:30
|
|
|
return xp;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input generating functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2008-03-02 20:03:26 +05:30
|
|
|
* Produce the characters of a string, then a newline, then NUL.
|
2001-06-29 10:27:14 +05:30
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int nlchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2008-03-02 20:03:26 +05:30
|
|
|
char c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (ap->aword == NULL)
|
2008-03-02 20:03:26 +05:30
|
|
|
return '\0';
|
2007-02-01 07:09:24 +05:30
|
|
|
c = *ap->aword++;
|
2008-03-02 20:03:26 +05:30
|
|
|
if (c == '\0') {
|
2001-06-29 10:27:14 +05:30
|
|
|
ap->aword = NULL;
|
2006-11-27 22:19:55 +05:30
|
|
|
return '\n';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a list of words, produce the characters
|
|
|
|
* in them, with a space after each word.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int wdchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char c;
|
|
|
|
char **wl;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
wl = ap->awordlist;
|
|
|
|
if (wl == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*wl != NULL) {
|
2007-01-01 11:30:38 +05:30
|
|
|
c = *(*wl)++;
|
|
|
|
if (c != 0)
|
2006-11-27 22:19:55 +05:30
|
|
|
return c & 0177;
|
2001-06-29 10:27:14 +05:30
|
|
|
ap->awordlist++;
|
2006-11-27 22:19:55 +05:30
|
|
|
return ' ';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
ap->awordlist = NULL;
|
2006-11-27 22:19:55 +05:30
|
|
|
return '\n';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters of a list of words,
|
|
|
|
* producing a space between them.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int dolchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
char *wp;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
wp = *ap->awordlist++;
|
|
|
|
if (wp != NULL) {
|
2004-08-05 00:46:54 +05:30
|
|
|
PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar);
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int xxchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (ap->aword == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-01-01 11:30:38 +05:30
|
|
|
c = *ap->aword++;
|
|
|
|
if (c == '\0') {
|
2001-06-29 10:27:14 +05:30
|
|
|
ap->aword = NULL;
|
2006-11-27 22:19:55 +05:30
|
|
|
return ' ';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce the characters from a single word (string).
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int strchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2007-01-01 11:30:38 +05:30
|
|
|
if (ap->aword == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-01-01 11:30:38 +05:30
|
|
|
return *ap->aword++;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Produce quoted characters from a single word (string).
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int qstrchar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 23:48:04 +05:30
|
|
|
if (ap->aword == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return 0;
|
2007-01-01 23:48:04 +05:30
|
|
|
c = *ap->aword++;
|
|
|
|
if (c)
|
|
|
|
c |= QUOTE;
|
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters from a file.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int filechar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2001-06-29 10:27:14 +05:30
|
|
|
char c;
|
|
|
|
struct iobuf *bp = ap->afbuf;
|
|
|
|
|
|
|
|
if (ap->afid != AFID_NOBUF) {
|
2007-01-01 11:30:38 +05:30
|
|
|
i = (ap->afid != bp->id);
|
|
|
|
if (i || bp->bufp == bp->ebufp) {
|
2004-08-05 00:46:54 +05:30
|
|
|
if (i)
|
2006-10-14 07:53:43 +05:30
|
|
|
lseek(ap->afile, ap->afpos, SEEK_SET);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2008-02-21 03:53:24 +05:30
|
|
|
i = nonblock_safe_read(ap->afile, bp->buf, sizeof(bp->buf));
|
2004-08-05 00:46:54 +05:30
|
|
|
if (i <= 0) {
|
|
|
|
closef(ap->afile);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
bp->id = ap->afid;
|
2007-01-01 11:30:38 +05:30
|
|
|
bp->bufp = bp->buf;
|
|
|
|
bp->ebufp = bp->bufp + i;
|
2004-08-05 00:46:54 +05:30
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
ap->afpos++;
|
|
|
|
return *bp->bufp++ & 0177;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
2003-03-14 21:35:59 +05:30
|
|
|
if (interactive && isatty(ap->afile)) {
|
2007-03-25 04:12:29 +05:30
|
|
|
/* moved to G: static char filechar_cmdbuf[BUFSIZ]; */
|
2004-08-05 00:46:54 +05:30
|
|
|
static int position = 0, size = 0;
|
|
|
|
|
|
|
|
while (size == 0 || position >= size) {
|
2008-02-03 00:20:50 +05:30
|
|
|
size = read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state);
|
|
|
|
if (size < 0) /* Error/EOF */
|
2008-05-19 14:59:47 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2004-08-05 00:46:54 +05:30
|
|
|
position = 0;
|
2008-02-03 00:20:50 +05:30
|
|
|
/* if Ctrl-C, size == 0 and loop will repeat */
|
2004-08-05 00:46:54 +05:30
|
|
|
}
|
2007-03-25 04:12:29 +05:30
|
|
|
c = filechar_cmdbuf[position];
|
2004-08-05 00:46:54 +05:30
|
|
|
position++;
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
#endif
|
2008-02-21 03:53:24 +05:30
|
|
|
i = nonblock_safe_read(ap->afile, &c, sizeof(c));
|
2007-02-01 07:09:24 +05:30
|
|
|
return i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters from a here temp file.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int herechar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
2008-02-21 03:53:24 +05:30
|
|
|
if (nonblock_safe_read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
|
2001-06-29 10:27:14 +05:30
|
|
|
close(ap->afile);
|
2007-02-01 07:09:24 +05:30
|
|
|
c = '\0';
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the characters produced by a process (`...`).
|
|
|
|
* Quote them if required, and remove any trailing newline characters.
|
|
|
|
*/
|
2006-06-26 03:38:53 +05:30
|
|
|
static int gravechar(struct ioarg *ap, struct io *iop)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = qgravechar(ap, iop) & ~QUOTE;
|
|
|
|
if (c == '\n')
|
2001-06-29 10:27:14 +05:30
|
|
|
c = ' ';
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int qgravechar(struct ioarg *ap, struct io *iop)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF3(("QGRAVECHAR: enter, ap=%p, iop=%p\n", ap, iop));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
if (iop->xchar) {
|
|
|
|
if (iop->nlcount) {
|
|
|
|
iop->nlcount--;
|
2006-11-27 22:19:55 +05:30
|
|
|
return '\n' | QUOTE;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
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)
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
iop->nlcount--;
|
|
|
|
c = '\n';
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c != 0 ? c | QUOTE : 0;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a single command (usually the first line) from a file.
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int linechar(struct ioarg *ap)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
c = filechar(ap);
|
|
|
|
if (c == '\n') {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (!multiline) {
|
|
|
|
closef(ap->afile);
|
2004-08-05 00:46:54 +05:30
|
|
|
ap->afile = -1; /* illegal value */
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:19:55 +05:30
|
|
|
return c;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remap fd into Shell's fd space
|
|
|
|
*/
|
2006-07-01 18:38:46 +05:30
|
|
|
static int remap(int fd)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2001-06-29 10:27:14 +05:30
|
|
|
int map[NOFILE];
|
2004-08-05 00:49:10 +05:30
|
|
|
int newfd;
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("REMAP: fd=%d, global_env.iofd=%d\n", fd, global_env.iofd));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
if (fd < global_env.iofd) {
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = 0; i < NOFILE; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
map[i] = 0;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
do {
|
|
|
|
map[fd] = 1;
|
2004-08-05 00:49:10 +05:30
|
|
|
newfd = dup(fd);
|
|
|
|
fd = newfd;
|
2007-12-16 22:51:29 +05:30
|
|
|
} while (fd >= 0 && fd < global_env.iofd);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2004-08-05 00:46:54 +05:30
|
|
|
for (i = 0; i < NOFILE; i++)
|
2001-06-29 10:27:14 +05:30
|
|
|
if (map[i])
|
|
|
|
close(i);
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (fd < 0)
|
|
|
|
err("too many files open in shell");
|
|
|
|
}
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-11-27 22:19:55 +05:30
|
|
|
return fd;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static int openpipe(int *pv)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int i;
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2007-01-01 11:30:38 +05:30
|
|
|
i = pipe(pv);
|
|
|
|
if (i < 0)
|
2001-06-29 10:27:14 +05:30
|
|
|
err("can't create pipe - try again");
|
2006-11-27 22:19:55 +05:30
|
|
|
return i;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void closepipe(int *pv)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
if (pv != NULL) {
|
2008-02-11 23:40:06 +05:30
|
|
|
close(pv[0]);
|
|
|
|
close(pv[1]);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-01 07:13:54 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/* -------- here.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* here documents
|
|
|
|
*/
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void markhere(char *s, struct ioword *iop)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct here *h, *lh;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("MARKHERE: enter, s=%p\n", s));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
2008-03-02 18:19:39 +05:30
|
|
|
h = get_space(sizeof(struct here));
|
2007-02-01 07:09:24 +05:30
|
|
|
if (h == NULL)
|
2001-06-29 10:27:14 +05:30
|
|
|
return;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
h->h_tag = evalstr(s, DOSUB);
|
|
|
|
if (h->h_tag == 0)
|
|
|
|
return;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
h->h_iop = iop;
|
|
|
|
iop->io_name = 0;
|
|
|
|
h->h_next = NULL;
|
|
|
|
if (inhere == 0)
|
|
|
|
inhere = h;
|
2007-02-01 07:09:24 +05:30
|
|
|
else {
|
|
|
|
for (lh = inhere; lh != NULL; lh = lh->h_next) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (lh->h_next == 0) {
|
|
|
|
lh->h_next = h;
|
|
|
|
break;
|
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
|
|
|
}
|
2004-08-05 00:46:54 +05:30
|
|
|
iop->io_flag |= IOHERE | IOXHERE;
|
2007-02-01 07:09:24 +05:30
|
|
|
for (s = h->h_tag; *s; s++) {
|
2001-06-29 10:27:14 +05:30
|
|
|
if (*s & QUOTE) {
|
2004-08-05 00:46:54 +05:30
|
|
|
iop->io_flag &= ~IOXHERE;
|
|
|
|
*s &= ~QUOTE;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
2008-02-12 11:34:06 +05:30
|
|
|
h->h_dosub = ((iop->io_flag & IOXHERE) ? '\0' : '\'');
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void gethere(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct here *h, *hp;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("GETHERE: enter...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
/* Scan here files first leaving inhere list in place */
|
|
|
|
for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
|
2008-02-12 11:34:06 +05:30
|
|
|
readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub /* NUL or ' */);
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
/* Make inhere list active - keep list intact for scraphere */
|
|
|
|
if (hp != NULL) {
|
2004-08-05 00:46:54 +05:30
|
|
|
hp->h_next = acthere;
|
|
|
|
acthere = inhere;
|
|
|
|
inhere = NULL;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-01 18:38:46 +05:30
|
|
|
static void readhere(char **name, char *s, int ec)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
|
|
|
int tf;
|
|
|
|
char tname[30] = ".msh_XXXXXX";
|
2006-07-01 18:38:46 +05:30
|
|
|
int c;
|
2001-06-29 10:27:14 +05:30
|
|
|
jmp_buf ev;
|
2004-08-05 00:46:54 +05:30
|
|
|
char myline[LINELIM + 1];
|
2001-06-29 10:27:14 +05:30
|
|
|
char *thenext;
|
|
|
|
|
2006-05-06 02:02:31 +05:30
|
|
|
DBGPRINTF7(("READHERE: enter, name=%p, s=%p\n", name, s));
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
tf = mkstemp(tname);
|
|
|
|
if (tf < 0)
|
|
|
|
return;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
*name = strsave(tname, areanum);
|
2007-01-01 11:30:38 +05:30
|
|
|
errpt = ev;
|
|
|
|
if (newenv(setjmp(errpt)) != 0)
|
2001-06-29 10:27:14 +05:30
|
|
|
unlink(tname);
|
|
|
|
else {
|
2007-12-16 22:51:29 +05:30
|
|
|
pushio(global_env.iop->argp, (int (*)(struct ioarg *)) global_env.iop->iofn);
|
|
|
|
global_env.iobase = global_env.iop;
|
2001-06-29 10:27:14 +05:30
|
|
|
for (;;) {
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && global_env.iop <= iostack) {
|
2007-01-22 14:33:07 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
2004-08-05 00:46:54 +05:30
|
|
|
current_prompt = cprompt->value;
|
2001-06-29 10:27:14 +05:30
|
|
|
#else
|
2004-08-05 00:46:54 +05:30
|
|
|
prs(cprompt->value);
|
2001-06-29 10:27:14 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
thenext = myline;
|
|
|
|
while ((c = my_getc(ec)) != '\n' && c) {
|
|
|
|
if (ec == '\'')
|
2004-08-05 00:46:54 +05:30
|
|
|
c &= ~QUOTE;
|
2001-06-29 10:27:14 +05:30
|
|
|
if (thenext >= &myline[LINELIM]) {
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*thenext++ = c;
|
|
|
|
}
|
|
|
|
*thenext = 0;
|
|
|
|
if (strcmp(s, myline) == 0 || c == 0)
|
|
|
|
break;
|
|
|
|
*thenext++ = '\n';
|
2004-08-05 00:46:54 +05:30
|
|
|
write(tf, myline, (int) (thenext - myline));
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
if (c == 0) {
|
2004-08-05 00:46:54 +05:30
|
|
|
prs("here document `");
|
|
|
|
prs(s);
|
|
|
|
err("' unclosed");
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
}
|
|
|
|
close(tf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open here temp file.
|
|
|
|
* if unquoted here, expand here temp file into second temp file.
|
|
|
|
*/
|
2006-06-26 03:38:53 +05:30
|
|
|
static int herein(char *hname, int xdoll)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
int hf;
|
2001-06-29 10:27:14 +05:30
|
|
|
int tf;
|
|
|
|
|
|
|
|
#if __GNUC__
|
|
|
|
/* Avoid longjmp clobbering */
|
|
|
|
(void) &tf;
|
|
|
|
#endif
|
2004-08-05 00:49:10 +05:30
|
|
|
if (hname == NULL)
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
|
|
|
|
|
2007-04-14 18:52:09 +05:30
|
|
|
hf = open(hname, O_RDONLY);
|
2001-06-29 10:27:14 +05:30
|
|
|
if (hf < 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
if (xdoll) {
|
|
|
|
char c;
|
|
|
|
char tname[30] = ".msh_XXXXXX";
|
|
|
|
jmp_buf ev;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
tf = mkstemp(tname);
|
|
|
|
if (tf < 0)
|
2006-11-27 22:19:31 +05:30
|
|
|
return -1;
|
2007-02-01 07:09:24 +05:30
|
|
|
errpt = ev;
|
|
|
|
if (newenv(setjmp(errpt)) == 0) {
|
2001-06-29 10:27:14 +05:30
|
|
|
PUSHIO(afile, hf, herechar);
|
2007-12-16 22:51:29 +05:30
|
|
|
setbase(global_env.iop);
|
2001-06-29 10:27:14 +05:30
|
|
|
while ((c = subgetc(0, 0)) != 0) {
|
2004-08-05 00:46:54 +05:30
|
|
|
c &= ~QUOTE;
|
2001-06-29 10:27:14 +05:30
|
|
|
write(tf, &c, sizeof c);
|
|
|
|
}
|
|
|
|
quitenv();
|
|
|
|
} else
|
|
|
|
unlink(tname);
|
|
|
|
close(tf);
|
2007-04-14 18:52:09 +05:30
|
|
|
tf = open(tname, O_RDONLY);
|
2001-06-29 10:27:14 +05:30
|
|
|
unlink(tname);
|
2006-11-27 22:19:55 +05:30
|
|
|
return tf;
|
2007-02-01 07:09:24 +05:30
|
|
|
}
|
|
|
|
return hf;
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
2006-06-26 03:38:53 +05:30
|
|
|
static void scraphere(void)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct here *h;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF7(("SCRAPHERE: enter...\n"));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
for (h = inhere; h != NULL; h = h->h_next) {
|
|
|
|
if (h->h_iop && h->h_iop->io_name)
|
2004-08-05 00:46:54 +05:30
|
|
|
unlink(h->h_iop->io_name);
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
inhere = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlink here temp files before a freearea(area) */
|
2006-06-26 03:38:53 +05:30
|
|
|
static void freehere(int area)
|
2001-06-29 10:27:14 +05:30
|
|
|
{
|
2006-07-01 18:38:46 +05:30
|
|
|
struct here *h, *hl;
|
2004-08-05 00:49:10 +05:30
|
|
|
|
|
|
|
DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
|
2001-06-29 10:27:14 +05:30
|
|
|
|
|
|
|
hl = NULL;
|
2008-03-02 20:03:26 +05:30
|
|
|
for (h = acthere; h != NULL; h = h->h_next) {
|
2001-06-29 10:27:14 +05:30
|
|
|
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;
|
2008-03-02 20:03:26 +05:30
|
|
|
} else {
|
2001-06-29 10:27:14 +05:30
|
|
|
hl = h;
|
2008-03-02 20:03:26 +05:30
|
|
|
}
|
|
|
|
}
|
2001-06-29 10:27:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
/* -------- sh.c -------- */
|
|
|
|
/*
|
|
|
|
* shell
|
|
|
|
*/
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2007-02-01 07:13:16 +05:30
|
|
|
int msh_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int f;
|
|
|
|
char *s;
|
|
|
|
int cflag;
|
|
|
|
char *name, **ap;
|
|
|
|
int (*iof) (struct ioarg *);
|
|
|
|
|
2007-12-02 14:05:37 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2007-03-25 04:12:29 +05:30
|
|
|
sharedbuf.id = AFID_NOBUF;
|
|
|
|
mainbuf.id = AFID_NOBUF;
|
|
|
|
elinep = line + sizeof(line) - 5;
|
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
|
|
|
line_input_state = new_line_input_t(FOR_SHELL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DBGPRINTF(("MSH_MAIN: argc %d, environ %p\n", argc, environ));
|
|
|
|
|
|
|
|
initarea();
|
|
|
|
ap = environ;
|
|
|
|
if (ap != NULL) {
|
|
|
|
while (*ap)
|
|
|
|
assign(*ap++, !COPYV);
|
|
|
|
for (ap = environ; *ap;)
|
|
|
|
export(lookup(*ap++));
|
|
|
|
}
|
|
|
|
closeall();
|
|
|
|
areanum = 1;
|
|
|
|
|
|
|
|
shell = lookup("SHELL");
|
|
|
|
if (shell->value == null)
|
|
|
|
setval(shell, (char *)DEFAULT_SHELL);
|
|
|
|
export(shell);
|
|
|
|
|
|
|
|
homedir = lookup("HOME");
|
|
|
|
if (homedir->value == null)
|
|
|
|
setval(homedir, "/");
|
|
|
|
export(homedir);
|
|
|
|
|
|
|
|
setval(lookup("$"), putn(getpid()));
|
|
|
|
|
|
|
|
path = lookup("PATH");
|
|
|
|
if (path->value == null) {
|
2007-04-14 18:52:09 +05:30
|
|
|
/* Can be merged with same string elsewhere in bbox */
|
2007-02-01 07:13:16 +05:30
|
|
|
if (geteuid() == 0)
|
2007-06-13 04:05:19 +05:30
|
|
|
setval(path, bb_default_root_path);
|
2007-02-01 07:13:16 +05:30
|
|
|
else
|
2007-06-13 04:05:19 +05:30
|
|
|
setval(path, bb_default_path);
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
export(path);
|
|
|
|
|
|
|
|
ifs = lookup("IFS");
|
|
|
|
if (ifs->value == null)
|
|
|
|
setval(ifs, " \t\n");
|
|
|
|
|
|
|
|
#ifdef MSHDEBUG
|
|
|
|
mshdbg_var = lookup("MSHDEBUG");
|
|
|
|
if (mshdbg_var->value == null)
|
|
|
|
setval(mshdbg_var, "0");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
prompt = lookup("PS1");
|
|
|
|
#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
|
|
|
|
if (prompt->value == null)
|
|
|
|
#endif
|
|
|
|
setval(prompt, DEFAULT_USER_PROMPT);
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
setval(prompt, DEFAULT_ROOT_PROMPT);
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
}
|
|
|
|
cprompt = lookup("PS2");
|
|
|
|
#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
|
|
|
|
if (cprompt->value == null)
|
|
|
|
#endif
|
|
|
|
setval(cprompt, "> ");
|
|
|
|
|
|
|
|
iof = filechar;
|
|
|
|
cflag = 0;
|
|
|
|
name = *argv++;
|
|
|
|
if (--argc >= 1) {
|
|
|
|
if (argv[0][0] == '-' && argv[0][1] != '\0') {
|
|
|
|
for (s = argv[0] + 1; *s; s++)
|
|
|
|
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;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
qflag = SIG_DFL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
/* standard input */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
setval(prompt, "");
|
|
|
|
iof = linechar;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2008-02-12 11:34:06 +05:30
|
|
|
interactive = 1;
|
2007-02-01 07:13:16 +05:30
|
|
|
default:
|
|
|
|
if (*s >= 'a' && *s <= 'z')
|
2007-04-17 04:04:39 +05:30
|
|
|
FLAG[(int) *s]++;
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argv--;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iof == filechar && --argc > 0) {
|
|
|
|
setval(prompt, "");
|
|
|
|
setval(cprompt, "");
|
|
|
|
prompt->status &= ~EXPORT;
|
|
|
|
cprompt->status &= ~EXPORT;
|
|
|
|
|
|
|
|
/* 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"));
|
|
|
|
|
|
|
|
name = *++argv;
|
|
|
|
if (newfile(name))
|
2008-05-19 14:59:47 +05:30
|
|
|
exit(EXIT_FAILURE); /* Exit on error */
|
2007-02-01 07:13:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setdash();
|
|
|
|
|
|
|
|
/* This won't be true if PUSHIO has been called, say from newfile() above */
|
2007-12-16 22:51:29 +05:30
|
|
|
if (global_env.iop < iostack) {
|
2007-02-01 07:13:16 +05:30
|
|
|
PUSHIO(afile, 0, iof);
|
|
|
|
if (isatty(0) && isatty(1) && !cflag) {
|
2008-02-12 11:34:06 +05:30
|
|
|
interactive = 1;
|
2007-02-01 07:13:16 +05:30
|
|
|
#if !ENABLE_FEATURE_SH_EXTRA_QUIET
|
|
|
|
#ifdef MSHDEBUG
|
2007-06-13 17:57:17 +05:30
|
|
|
printf("\n\n%s built-in shell (msh with debug)\n", bb_banner);
|
2007-02-01 07:13:16 +05:30
|
|
|
#else
|
2007-06-13 17:57:17 +05:30
|
|
|
printf("\n\n%s built-in shell (msh)\n", bb_banner);
|
2007-02-01 07:13:16 +05:30
|
|
|
#endif
|
|
|
|
printf("Enter 'help' for a list of built-in commands.\n\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGQUIT, qflag);
|
|
|
|
if (name && name[0] == '-') {
|
2008-02-12 11:34:06 +05:30
|
|
|
interactive = 1;
|
2007-04-14 18:52:09 +05:30
|
|
|
f = open(".profile", O_RDONLY);
|
2007-02-01 07:13:16 +05:30
|
|
|
if (f >= 0)
|
|
|
|
next(remap(f));
|
2007-04-14 18:52:09 +05:30
|
|
|
f = open("/etc/profile", O_RDONLY);
|
2007-02-01 07:13:16 +05:30
|
|
|
if (f >= 0)
|
|
|
|
next(remap(f));
|
|
|
|
}
|
|
|
|
if (interactive)
|
|
|
|
signal(SIGTERM, sig);
|
|
|
|
|
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGINT, onintr);
|
2008-02-12 15:42:18 +05:30
|
|
|
|
|
|
|
/* Handle "msh SCRIPT VAR=val params..." */
|
|
|
|
/* Disabled: bash does not do it! */
|
|
|
|
#if 0
|
|
|
|
argv++;
|
|
|
|
/* skip leading args of the form VAR=val */
|
|
|
|
while (*argv && assign(*argv, !COPYV)) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
argv--;
|
|
|
|
#endif
|
2007-02-01 07:13:16 +05:30
|
|
|
dolv = argv;
|
|
|
|
dolc = argc;
|
|
|
|
dolv[0] = name;
|
2008-02-12 15:42:18 +05:30
|
|
|
|
2007-02-01 07:13:16 +05:30
|
|
|
setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
|
|
|
|
|
2007-12-16 22:51:29 +05:30
|
|
|
DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, global_env.iop %p, iostack %p\n", interactive, global_env.iop, iostack));
|
2007-02-01 07:13:16 +05:30
|
|
|
|
|
|
|
for (;;) {
|
2007-12-16 22:51:29 +05:30
|
|
|
if (interactive && global_env.iop <= iostack) {
|
2007-02-01 07:13:16 +05:30
|
|
|
#if ENABLE_FEATURE_EDITING
|
|
|
|
current_prompt = prompt->value;
|
|
|
|
#else
|
|
|
|
prs(prompt->value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
onecommand();
|
|
|
|
/* Ensure that getenv("PATH") stays current */
|
|
|
|
setenv("PATH", path->value, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBGPRINTF(("MSH_MAIN: returning.\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-29 10:27:14 +05:30
|
|
|
/*
|
|
|
|
* Copyright (c) 1987,1997, Prentice Hall
|
|
|
|
* All rights reserved.
|
2004-03-15 13:59:22 +05:30
|
|
|
*
|
2001-06-29 10:27:14 +05:30
|
|
|
* 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 13:59:22 +05:30
|
|
|
*
|
2001-06-29 10:27:14 +05:30
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-03-15 13:59:22 +05:30
|
|
|
*
|
2001-06-29 10:27:14 +05:30
|
|
|
* 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 13:59:22 +05:30
|
|
|
*
|
2001-06-29 10:27:14 +05:30
|
|
|
* 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 13:59:22 +05:30
|
|
|
*
|
2001-06-29 10:27:14 +05:30
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|