2015-10-09 18:11:19 +02:00
|
|
|
/* $OpenBSD: lex.c,v 1.51 2015/09/10 22:48:58 nicm Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-05-05 00:44:34 +02:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2017-02-18 03:33:15 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015, 2016, 2017
|
2015-10-09 18:11:19 +02:00
|
|
|
* mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2017-10-18 01:45:19 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.240 2017/10/17 23:45:18 tg Exp $");
|
2008-02-26 21:35:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* states while lexing word
|
|
|
|
*/
|
|
|
|
#define SBASE 0 /* outside any lexical constructs */
|
|
|
|
#define SWORD 1 /* implicit quoting for substitute() */
|
|
|
|
#define SLETPAREN 2 /* inside (( )), implicit quoting */
|
|
|
|
#define SSQUOTE 3 /* inside '' */
|
|
|
|
#define SDQUOTE 4 /* inside "" */
|
2010-02-23 22:51:49 +01:00
|
|
|
#define SEQUOTE 5 /* inside $'' */
|
|
|
|
#define SBRACE 6 /* inside ${} */
|
|
|
|
#define SQBRACE 7 /* inside "${}" */
|
2011-03-06 02:25:35 +01:00
|
|
|
#define SBQUOTE 8 /* inside `` */
|
|
|
|
#define SASPAREN 9 /* inside $(( )) */
|
2015-09-06 21:47:01 +02:00
|
|
|
#define SHEREDELIM 10 /* parsing << or <<- delimiter */
|
|
|
|
#define SHEREDQUOTE 11 /* parsing " in << or <<- delimiter */
|
2011-03-06 02:25:35 +01:00
|
|
|
#define SPATTERN 12 /* parsing *(...|...) pattern (*+?@!) */
|
2011-07-26 18:57:28 +02:00
|
|
|
#define SADELIM 13 /* like SBASE, looking for delimiter */
|
2013-01-19 18:20:02 +01:00
|
|
|
#define STBRACEKORN 14 /* parsing ${...[#%]...} !FSH */
|
|
|
|
#define STBRACEBOURNE 15 /* parsing ${...[#%]...} FSH */
|
2011-03-07 21:07:52 +01:00
|
|
|
#define SINVALID 255 /* invalid state */
|
2011-03-06 02:25:35 +01:00
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
struct sretrace_info {
|
|
|
|
struct sretrace_info *next;
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
|
|
|
};
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Structure to keep track of the lexing state and the various pieces of info
|
|
|
|
* needed for each particular state.
|
|
|
|
*/
|
2011-03-07 21:07:52 +01:00
|
|
|
typedef struct lex_state {
|
2005-05-23 05:06:10 +02:00
|
|
|
union {
|
2011-03-07 21:07:52 +01:00
|
|
|
/* point to the next state block */
|
2011-03-07 21:30:41 +01:00
|
|
|
struct lex_state *base;
|
2011-03-13 02:11:58 +01:00
|
|
|
/* marks start of state output in output string */
|
2015-09-06 15:10:48 +02:00
|
|
|
size_t start;
|
2011-03-07 21:07:52 +01:00
|
|
|
/* SBQUOTE: true if in double quotes: "`...`" */
|
|
|
|
/* SEQUOTE: got NUL, ignore rest of string */
|
|
|
|
bool abool;
|
|
|
|
/* SADELIM information */
|
|
|
|
struct {
|
|
|
|
/* character to search for */
|
2007-07-06 03:53:36 +02:00
|
|
|
unsigned char delimiter;
|
2011-03-07 21:07:52 +01:00
|
|
|
/* max. number of delimiters */
|
2007-07-06 03:53:36 +02:00
|
|
|
unsigned char num;
|
2011-03-07 21:07:52 +01:00
|
|
|
} adelim;
|
|
|
|
} u;
|
|
|
|
/* count open parentheses */
|
|
|
|
short nparen;
|
|
|
|
/* type of this state */
|
|
|
|
uint8_t type;
|
|
|
|
} Lex_state;
|
|
|
|
#define ls_base u.base
|
|
|
|
#define ls_start u.start
|
|
|
|
#define ls_bool u.abool
|
|
|
|
#define ls_adelim u.adelim
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-07 21:13:11 +02:00
|
|
|
typedef struct {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
Lex_state *base;
|
|
|
|
Lex_state *end;
|
2009-04-07 21:13:11 +02:00
|
|
|
} State_info;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-07 21:06:44 +02:00
|
|
|
static void readhere(struct ioword *);
|
2011-03-13 02:11:58 +01:00
|
|
|
static void ungetsc(int);
|
2011-10-26 00:36:39 +02:00
|
|
|
static void ungetsc_i(int);
|
2011-09-07 17:24:22 +02:00
|
|
|
static int getsc_uu(void);
|
2009-04-07 21:06:44 +02:00
|
|
|
static void getsc_line(Source *);
|
|
|
|
static int getsc_bn(void);
|
2016-01-20 22:34:13 +01:00
|
|
|
static int getsc_i(void);
|
2009-04-07 21:06:44 +02:00
|
|
|
static char *get_brace_var(XString *, char *);
|
2011-03-07 21:30:41 +01:00
|
|
|
static bool arraysub(char **);
|
2015-09-06 21:47:01 +02:00
|
|
|
static void gethere(void);
|
2011-10-26 00:36:39 +02:00
|
|
|
static Lex_state *push_state_i(State_info *, Lex_state *);
|
|
|
|
static Lex_state *pop_state_i(State_info *, Lex_state *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static int backslash_skip;
|
|
|
|
static int ignore_backslash_newline;
|
|
|
|
|
2007-01-12 11:18:22 +01:00
|
|
|
/* optimised getsc_bn() */
|
2011-09-07 17:24:22 +02:00
|
|
|
#define o_getsc() (*source->str != '\0' && *source->str != '\\' && \
|
2011-03-13 17:03:54 +01:00
|
|
|
!backslash_skip ? *source->str++ : getsc_bn())
|
2011-09-07 17:24:22 +02:00
|
|
|
/* optimised getsc_uu() */
|
|
|
|
#define o_getsc_u() ((*source->str != '\0') ? *source->str++ : getsc_uu())
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
/* retrace helper */
|
2016-01-20 22:34:13 +01:00
|
|
|
#define o_getsc_r(carg) \
|
2011-03-13 02:11:58 +01:00
|
|
|
int cev = (carg); \
|
|
|
|
struct sretrace_info *rp = retrace_info; \
|
|
|
|
\
|
|
|
|
while (rp) { \
|
|
|
|
Xcheck(rp->xs, rp->xp); \
|
|
|
|
*rp->xp++ = cev; \
|
|
|
|
rp = rp->next; \
|
|
|
|
} \
|
|
|
|
\
|
2016-01-20 22:34:13 +01:00
|
|
|
return (cev);
|
2009-09-23 20:04:58 +02:00
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
/* callback */
|
2009-09-23 20:04:58 +02:00
|
|
|
static int
|
2016-01-20 22:34:13 +01:00
|
|
|
getsc_i(void)
|
2009-09-23 20:04:58 +02:00
|
|
|
{
|
2011-09-07 17:24:22 +02:00
|
|
|
o_getsc_r(o_getsc());
|
2009-09-23 20:04:58 +02:00
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
|
|
|
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
|
2017-05-06 00:53:32 +02:00
|
|
|
#define getsc() ord(getsc_i())
|
2011-03-13 02:11:58 +01:00
|
|
|
#else
|
|
|
|
static int getsc_r(int);
|
2009-09-23 20:04:58 +02:00
|
|
|
|
|
|
|
static int
|
2011-03-13 02:11:58 +01:00
|
|
|
getsc_r(int c)
|
2009-09-23 20:04:58 +02:00
|
|
|
{
|
2011-09-07 17:24:22 +02:00
|
|
|
o_getsc_r(c);
|
2009-09-23 20:04:58 +02:00
|
|
|
}
|
2011-03-13 02:11:58 +01:00
|
|
|
|
2017-05-06 00:53:32 +02:00
|
|
|
#define getsc() ord(getsc_r(o_getsc()))
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
|
|
|
|
2011-03-07 21:07:52 +01:00
|
|
|
#define STATE_BSIZE 8
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
#define PUSH_STATE(s) do { \
|
|
|
|
if (++statep == state_info.end) \
|
2011-10-26 00:36:39 +02:00
|
|
|
statep = push_state_i(&state_info, statep); \
|
2011-03-07 21:07:52 +01:00
|
|
|
state = statep->type = (s); \
|
2011-03-13 02:11:58 +01:00
|
|
|
} while (/* CONSTCOND */ 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
#define POP_STATE() do { \
|
|
|
|
if (--statep == state_info.base) \
|
2011-10-26 00:36:39 +02:00
|
|
|
statep = pop_state_i(&state_info, statep); \
|
2011-03-07 21:07:52 +01:00
|
|
|
state = statep->type; \
|
2011-03-13 02:11:58 +01:00
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
2014-06-29 13:28:28 +02:00
|
|
|
#define PUSH_SRETRACE(s) do { \
|
2011-03-13 02:11:58 +01:00
|
|
|
struct sretrace_info *ri; \
|
|
|
|
\
|
2014-06-29 13:28:28 +02:00
|
|
|
PUSH_STATE(s); \
|
2011-03-13 02:11:58 +01:00
|
|
|
statep->ls_start = Xsavepos(ws, wp); \
|
|
|
|
ri = alloc(sizeof(struct sretrace_info), ATEMP); \
|
|
|
|
Xinit(ri->xs, ri->xp, 64, ATEMP); \
|
|
|
|
ri->next = retrace_info; \
|
|
|
|
retrace_info = ri; \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
|
|
|
#define POP_SRETRACE() do { \
|
|
|
|
wp = Xrestpos(ws, wp, statep->ls_start); \
|
|
|
|
*retrace_info->xp = '\0'; \
|
|
|
|
sp = Xstring(retrace_info->xs, retrace_info->xp); \
|
|
|
|
dp = (void *)retrace_info; \
|
|
|
|
retrace_info = retrace_info->next; \
|
|
|
|
afree(dp, ATEMP); \
|
2014-06-29 13:28:28 +02:00
|
|
|
POP_STATE(); \
|
2011-03-13 02:11:58 +01:00
|
|
|
} while (/* CONSTCOND */ 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/**
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
* Lexical analyser
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* tokens are not regular expressions, they are LL(1).
|
|
|
|
* for example, "${var:-${PWD}}", and "$(size $(whence ksh))".
|
2011-03-07 21:32:50 +01:00
|
|
|
* hence the state stack. Note "$(...)" are now parsed recursively.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
yylex(int cf)
|
|
|
|
{
|
2005-07-04 14:47:13 +02:00
|
|
|
Lex_state states[STATE_BSIZE], *statep, *s2, *base;
|
2005-05-23 05:06:10 +02:00
|
|
|
State_info state_info;
|
2009-09-19 23:54:46 +02:00
|
|
|
int c, c2, state;
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t cz;
|
2005-05-23 05:06:10 +02:00
|
|
|
XString ws; /* expandable output word */
|
|
|
|
char *wp; /* output word pointer */
|
|
|
|
char *sp, *dp;
|
|
|
|
|
2006-08-01 15:43:28 +02:00
|
|
|
Again:
|
2011-03-07 21:07:52 +01:00
|
|
|
states[0].type = SINVALID;
|
|
|
|
states[0].ls_base = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
statep = &states[1];
|
|
|
|
state_info.base = states;
|
2008-10-10 23:30:43 +02:00
|
|
|
state_info.end = &state_info.base[STATE_BSIZE];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
Xinit(ws, wp, 64, ATEMP);
|
|
|
|
|
|
|
|
backslash_skip = 0;
|
|
|
|
ignore_backslash_newline = 0;
|
|
|
|
|
2011-03-13 16:57:23 +01:00
|
|
|
if (cf & ONEWORD)
|
2005-05-23 05:06:10 +02:00
|
|
|
state = SWORD;
|
2011-03-13 16:57:23 +01:00
|
|
|
else if (cf & LETEXPR) {
|
2010-01-29 10:34:31 +01:00
|
|
|
/* enclose arguments in (double) quotes */
|
|
|
|
*wp++ = OQUOTE;
|
2005-05-23 05:06:10 +02:00
|
|
|
state = SLETPAREN;
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen = 0;
|
2011-03-06 02:25:35 +01:00
|
|
|
} else {
|
|
|
|
/* normal lexing */
|
2005-05-23 05:06:10 +02:00
|
|
|
state = (cf & HEREDELIM) ? SHEREDELIM : SBASE;
|
2017-04-28 02:38:33 +02:00
|
|
|
while (ctype((c = getsc()), C_BLANK))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
if (c == '#') {
|
|
|
|
ignore_backslash_newline++;
|
2017-04-28 02:38:33 +02:00
|
|
|
while (!ctype((c = getsc()), C_NUL | C_LF))
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
|
|
|
ignore_backslash_newline--;
|
|
|
|
}
|
|
|
|
ungetsc(c);
|
|
|
|
}
|
2011-03-06 02:25:35 +01:00
|
|
|
if (source->flags & SF_ALIAS) {
|
|
|
|
/* trailing ' ' in alias definition */
|
2005-05-23 05:06:10 +02:00
|
|
|
source->flags &= ~SF_ALIAS;
|
2015-10-09 19:48:53 +02:00
|
|
|
/* POSIX: trailing space only counts if parsing simple cmd */
|
|
|
|
if (!Flag(FPOSIX) || (cf & CMDWORD))
|
|
|
|
cf |= ALIAS;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-06-04 18:11:20 +02:00
|
|
|
/* Initial state: one of SWORD SLETPAREN SHEREDELIM SBASE */
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->type = state;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* collect non-special or quoted characters to form word */
|
|
|
|
while (!((c = getsc()) == 0 ||
|
2013-01-19 18:20:02 +01:00
|
|
|
((state == SBASE || state == SHEREDELIM) && ctype(c, C_LEX1)))) {
|
2012-10-22 22:19:18 +02:00
|
|
|
if (state == SBASE &&
|
2017-05-06 00:53:32 +02:00
|
|
|
subshell_nesting_type == ord(/*{*/ '}') &&
|
|
|
|
c == ord(/*{*/ '}'))
|
2012-10-22 22:19:18 +02:00
|
|
|
/* possibly end ${ :;} */
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ws, wp);
|
|
|
|
switch (state) {
|
2007-07-06 03:53:36 +02:00
|
|
|
case SADELIM:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('('))
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen++;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord(')'))
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen--;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (statep->nparen == 0 && (c == ord(/*{*/ '}') ||
|
2012-12-05 20:38:25 +01:00
|
|
|
c == (int)statep->ls_adelim.delimiter)) {
|
2007-07-06 03:53:36 +02:00
|
|
|
*wp++ = ADELIM;
|
|
|
|
*wp++ = c;
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(/*{*/ '}') || --statep->ls_adelim.num == 0)
|
2007-07-06 03:53:36 +02:00
|
|
|
POP_STATE();
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(/*{*/ '}'))
|
2008-03-01 22:10:26 +01:00
|
|
|
POP_STATE();
|
2007-07-06 03:53:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case SBASE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('[') && (cf & CMDASN)) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* temporary */
|
|
|
|
*wp = EOS;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (is_wdvarname(Xstring(ws, wp), false)) {
|
|
|
|
char *p, *tmp;
|
|
|
|
|
|
|
|
if (arraysub(&tmp)) {
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
for (p = tmp; *p; ) {
|
|
|
|
Xcheck(ws, wp);
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = *p++;
|
|
|
|
}
|
|
|
|
afree(tmp, ATEMP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2006-08-01 15:43:28 +02:00
|
|
|
Sbase1: /* includes *(...|...) pattern (*+?@!) */
|
2017-04-28 02:38:33 +02:00
|
|
|
if (ctype(c, C_PATMO)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
c2 = getsc();
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c2 == ord('(' /*)*/)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OPAT;
|
|
|
|
*wp++ = c;
|
|
|
|
PUSH_STATE(SPATTERN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2006-08-01 15:43:28 +02:00
|
|
|
Sbase2: /* doesn't include *(...|...) pattern (*+?@!) */
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\\'):
|
2010-02-25 21:18:19 +01:00
|
|
|
getsc_qchar:
|
|
|
|
if ((c = getsc())) {
|
2010-01-29 10:34:31 +01:00
|
|
|
/* trailing \ is lost */
|
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\''):
|
2013-06-01 02:15:58 +02:00
|
|
|
open_ssquote_unless_heredoc:
|
|
|
|
if ((cf & HEREDOC))
|
|
|
|
goto store_char;
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
PUSH_STATE(SSQUOTE);
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('"'):
|
2010-02-25 21:18:19 +01:00
|
|
|
open_sdquote:
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
PUSH_STATE(SDQUOTE);
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('$'):
|
2013-02-17 07:05:02 +01:00
|
|
|
/*
|
|
|
|
* processing of dollar sign belongs into
|
|
|
|
* Subst, except for those which can open
|
|
|
|
* a string: $'…' and $"…"
|
|
|
|
*/
|
|
|
|
subst_dollar_ex:
|
|
|
|
c = getsc();
|
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('"'):
|
2013-02-17 07:05:02 +01:00
|
|
|
goto open_sdquote;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\''):
|
2013-02-17 07:05:02 +01:00
|
|
|
goto open_sequote;
|
|
|
|
default:
|
|
|
|
goto SubstS;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
|
|
|
goto Subst;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-08-01 15:43:28 +02:00
|
|
|
Subst:
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\\'):
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('"'):
|
2013-02-17 06:40:17 +01:00
|
|
|
if ((cf & HEREDOC))
|
2005-11-22 19:36:20 +01:00
|
|
|
goto heredocquote;
|
2007-06-04 21:25:45 +02:00
|
|
|
/* FALLTHROUGH */
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\\'):
|
|
|
|
case ord('$'):
|
|
|
|
case ord('`'):
|
2010-03-31 14:20:04 +02:00
|
|
|
store_qchar:
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2006-08-01 15:43:28 +02:00
|
|
|
heredocquote:
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ws, wp);
|
2010-01-29 10:34:31 +01:00
|
|
|
if (c) {
|
|
|
|
/* trailing \ is lost */
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = '\\';
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('$'):
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
2013-02-17 07:05:02 +01:00
|
|
|
SubstS:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('(' /*)*/)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('(' /*)*/)) {
|
2011-03-13 02:11:58 +01:00
|
|
|
*wp++ = EXPRSUB;
|
2014-06-29 13:28:28 +02:00
|
|
|
PUSH_SRETRACE(SASPAREN);
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen = 2;
|
2011-03-13 02:11:58 +01:00
|
|
|
*retrace_info->xp++ = '(';
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
ungetsc(c);
|
2011-03-06 02:25:35 +01:00
|
|
|
subst_command:
|
2012-10-22 22:19:18 +02:00
|
|
|
c = COMSUB;
|
|
|
|
subst_command2:
|
|
|
|
sp = yyrecursive(c);
|
2011-08-27 20:06:52 +02:00
|
|
|
cz = strlen(sp) + 1;
|
|
|
|
XcheckN(ws, wp, cz);
|
2012-10-22 22:19:18 +02:00
|
|
|
*wp++ = c;
|
2011-08-27 20:06:52 +02:00
|
|
|
memcpy(wp, sp, cz);
|
|
|
|
wp += cz;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('{' /*}*/)) {
|
|
|
|
if ((c = getsc()) == ord('|')) {
|
2013-05-02 23:59:54 +02:00
|
|
|
/*
|
|
|
|
* non-subenvironment
|
|
|
|
* value substitution
|
|
|
|
*/
|
|
|
|
c = VALSUB;
|
|
|
|
goto subst_command2;
|
|
|
|
} else if (ctype(c, C_IFSWS)) {
|
2012-10-22 22:19:18 +02:00
|
|
|
/*
|
|
|
|
* non-subenvironment
|
|
|
|
* "command" substitution
|
|
|
|
*/
|
|
|
|
c = FUNSUB;
|
|
|
|
goto subst_command2;
|
|
|
|
}
|
|
|
|
ungetsc(c);
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OSUBST;
|
2017-05-06 00:53:32 +02:00
|
|
|
*wp++ = '{' /*}*/;
|
2005-05-23 05:06:10 +02:00
|
|
|
wp = get_brace_var(&ws, wp);
|
|
|
|
c = getsc();
|
|
|
|
/* allow :# and :% (ksh88 compat) */
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(':')) {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(':')) {
|
2007-07-06 04:39:37 +02:00
|
|
|
*wp++ = CHAR;
|
2007-07-06 03:53:36 +02:00
|
|
|
*wp++ = '0';
|
|
|
|
*wp++ = ADELIM;
|
|
|
|
*wp++ = ':';
|
2008-03-01 22:10:26 +01:00
|
|
|
PUSH_STATE(SBRACE);
|
2007-07-06 03:53:36 +02:00
|
|
|
PUSH_STATE(SADELIM);
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->ls_adelim.delimiter = ':';
|
|
|
|
statep->ls_adelim.num = 1;
|
|
|
|
statep->nparen = 0;
|
2007-07-06 03:53:36 +02:00
|
|
|
break;
|
2017-04-27 22:22:28 +02:00
|
|
|
} else if (ctype(c, C_DIGIT | C_DOLAR | C_SPC) ||
|
2011-03-06 02:25:35 +01:00
|
|
|
/*XXX what else? */
|
2017-05-06 00:53:32 +02:00
|
|
|
c == '(' /*)*/) {
|
2007-07-06 03:53:36 +02:00
|
|
|
/* substring subst. */
|
2007-07-06 04:39:37 +02:00
|
|
|
if (c != ' ') {
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = ' ';
|
|
|
|
}
|
|
|
|
ungetsc(c);
|
2008-03-01 22:10:26 +01:00
|
|
|
PUSH_STATE(SBRACE);
|
2007-07-06 03:53:36 +02:00
|
|
|
PUSH_STATE(SADELIM);
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->ls_adelim.delimiter = ':';
|
|
|
|
statep->ls_adelim.num = 2;
|
|
|
|
statep->nparen = 0;
|
2007-07-06 03:53:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-03-01 22:10:26 +01:00
|
|
|
} else if (c == '/') {
|
2016-08-01 23:38:07 +02:00
|
|
|
c2 = ADELIM;
|
|
|
|
parse_adelim_slash:
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c = getsc()) == ord('/')) {
|
2016-08-01 23:38:07 +02:00
|
|
|
*wp++ = c2;
|
2008-03-01 22:10:26 +01:00
|
|
|
*wp++ = c;
|
|
|
|
} else
|
|
|
|
ungetsc(c);
|
|
|
|
PUSH_STATE(SBRACE);
|
|
|
|
PUSH_STATE(SADELIM);
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->ls_adelim.delimiter = '/';
|
|
|
|
statep->ls_adelim.num = 1;
|
|
|
|
statep->nparen = 0;
|
2008-03-01 22:10:26 +01:00
|
|
|
break;
|
2016-08-01 23:38:07 +02:00
|
|
|
} else if (c == '@') {
|
|
|
|
c2 = getsc();
|
|
|
|
ungetsc(c2);
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c2 == ord('/')) {
|
2016-08-01 23:38:07 +02:00
|
|
|
c2 = CHAR;
|
|
|
|
goto parse_adelim_slash;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* If this is a trim operation,
|
2005-05-23 05:06:10 +02:00
|
|
|
* treat (,|,) specially in STBRACE.
|
|
|
|
*/
|
2017-04-27 21:33:53 +02:00
|
|
|
if (ctype(c, C_SUB2)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
ungetsc(c);
|
2011-07-26 18:57:28 +02:00
|
|
|
if (Flag(FSH))
|
|
|
|
PUSH_STATE(STBRACEBOURNE);
|
|
|
|
else
|
|
|
|
PUSH_STATE(STBRACEKORN);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
ungetsc(c);
|
2013-06-01 02:15:58 +02:00
|
|
|
if (state == SDQUOTE ||
|
|
|
|
state == SQBRACE)
|
2010-02-23 22:51:49 +01:00
|
|
|
PUSH_STATE(SQBRACE);
|
|
|
|
else
|
|
|
|
PUSH_STATE(SBRACE);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-04-27 21:33:53 +02:00
|
|
|
} else if (ctype(c, C_ALPHX)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OSUBST;
|
|
|
|
*wp++ = 'X';
|
|
|
|
do {
|
|
|
|
Xcheck(ws, wp);
|
|
|
|
*wp++ = c;
|
|
|
|
c = getsc();
|
2017-04-27 21:33:53 +02:00
|
|
|
} while (ctype(c, C_ALNUX));
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = '\0';
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = 'X';
|
|
|
|
ungetsc(c);
|
2006-11-10 08:52:04 +01:00
|
|
|
} else if (ctype(c, C_VAR1 | C_DIGIT)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ws, wp);
|
|
|
|
*wp++ = OSUBST;
|
|
|
|
*wp++ = 'X';
|
|
|
|
*wp++ = c;
|
|
|
|
*wp++ = '\0';
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = 'X';
|
|
|
|
} else {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = '$';
|
2005-05-23 05:06:10 +02:00
|
|
|
ungetsc(c);
|
|
|
|
}
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('`'):
|
2010-02-25 21:18:19 +01:00
|
|
|
subst_gravis:
|
2005-05-23 05:06:10 +02:00
|
|
|
PUSH_STATE(SBQUOTE);
|
2017-04-06 03:59:58 +02:00
|
|
|
*wp++ = COMASUB;
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
2015-10-09 19:48:53 +02:00
|
|
|
* We need to know whether we are within double
|
2016-03-01 20:22:31 +01:00
|
|
|
* quotes in order to translate \" to " within
|
|
|
|
* "…`…\"…`…" because, unlike for COMSUBs, the
|
|
|
|
* outer double quoteing changes the backslash
|
|
|
|
* meaning for the inside. For more details:
|
|
|
|
* http://austingroupbugs.net/view.php?id=1015
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->ls_bool = false;
|
2005-07-04 14:47:13 +02:00
|
|
|
s2 = statep;
|
|
|
|
base = state_info.base;
|
2011-03-13 02:11:58 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-07-04 14:47:13 +02:00
|
|
|
for (; s2 != base; s2--) {
|
2011-03-07 21:07:52 +01:00
|
|
|
if (s2->type == SDQUOTE) {
|
|
|
|
statep->ls_bool = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2005-07-04 14:27:28 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2005-07-04 14:47:13 +02:00
|
|
|
if (s2 != base)
|
2005-07-04 14:27:28 +02:00
|
|
|
break;
|
2011-03-07 21:07:52 +01:00
|
|
|
if (!(s2 = s2->ls_base))
|
2005-07-04 14:27:28 +02:00
|
|
|
break;
|
2005-07-04 14:47:13 +02:00
|
|
|
base = s2-- - STATE_BSIZE;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2005-05-26 01:31:08 +02:00
|
|
|
case QCHAR:
|
|
|
|
if (cf & LQCHAR) {
|
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = getsc();
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
2010-02-25 21:18:19 +01:00
|
|
|
store_char:
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-09-19 23:54:46 +02:00
|
|
|
case SEQUOTE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('\'')) {
|
2009-09-19 23:54:46 +02:00
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
ignore_backslash_newline--;
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('\\')) {
|
2016-01-20 22:34:13 +01:00
|
|
|
if ((c2 = unbksl(true, getsc_i, ungetsc)) == -1)
|
|
|
|
c2 = getsc();
|
2009-09-20 00:33:10 +02:00
|
|
|
if (c2 == 0)
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->ls_bool = true;
|
|
|
|
if (!statep->ls_bool) {
|
2009-09-19 23:54:46 +02:00
|
|
|
char ts[4];
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
if ((unsigned int)c2 < 0x100) {
|
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c2;
|
|
|
|
} else {
|
2011-08-27 20:06:52 +02:00
|
|
|
cz = utf_wctomb(ts, c2 - 0x100);
|
|
|
|
ts[cz] = 0;
|
2015-10-24 21:46:10 +02:00
|
|
|
cz = 0;
|
|
|
|
do {
|
2010-01-28 21:52:08 +01:00
|
|
|
*wp++ = QCHAR;
|
2011-08-27 20:06:52 +02:00
|
|
|
*wp++ = ts[cz];
|
2015-10-24 21:46:10 +02:00
|
|
|
} while (ts[++cz]);
|
2009-09-19 23:54:46 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-07 21:07:52 +01:00
|
|
|
} else if (!statep->ls_bool) {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
break;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case SSQUOTE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('\'')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
POP_STATE();
|
2013-06-01 02:15:58 +02:00
|
|
|
if ((cf & HEREDOC) || state == SQBRACE)
|
|
|
|
goto store_char;
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CQUOTE;
|
|
|
|
ignore_backslash_newline--;
|
2010-01-29 10:34:31 +01:00
|
|
|
} else {
|
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDQUOTE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('"')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
} else
|
|
|
|
goto Subst;
|
|
|
|
break;
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* $(( ... )) */
|
|
|
|
case SASPAREN:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('('))
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen++;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord(')')) {
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen--;
|
|
|
|
if (statep->nparen == 1) {
|
2011-03-13 00:04:48 +01:00
|
|
|
/* end of EXPRSUB */
|
2011-03-13 02:11:58 +01:00
|
|
|
POP_SRETRACE();
|
2011-03-13 00:04:48 +01:00
|
|
|
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c2 = getsc()) == ord(/*(*/ ')')) {
|
2011-08-27 20:06:52 +02:00
|
|
|
cz = strlen(sp) - 2;
|
|
|
|
XcheckN(ws, wp, cz);
|
|
|
|
memcpy(wp, sp + 1, cz);
|
|
|
|
wp += cz;
|
2011-03-13 02:11:58 +01:00
|
|
|
afree(sp, ATEMP);
|
|
|
|
*wp++ = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
} else {
|
2011-03-06 02:25:35 +01:00
|
|
|
Source *s;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
ungetsc(c2);
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* mismatched parenthesis -
|
2005-05-23 05:06:10 +02:00
|
|
|
* assume we were really
|
2009-10-04 14:45:23 +02:00
|
|
|
* parsing a $(...) expression
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-03-13 02:11:58 +01:00
|
|
|
--wp;
|
2011-03-06 02:25:35 +01:00
|
|
|
s = pushs(SREREAD,
|
|
|
|
source->areap);
|
|
|
|
s->start = s->str =
|
2011-03-13 02:11:58 +01:00
|
|
|
s->u.freeme = sp;
|
2011-03-06 02:25:35 +01:00
|
|
|
s->next = source;
|
|
|
|
source = s;
|
|
|
|
goto subst_command;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-13 17:07:36 +01:00
|
|
|
/* reuse existing state machine */
|
2011-03-13 02:11:58 +01:00
|
|
|
goto Sbase2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-03-27 17:53:17 +01:00
|
|
|
case SQBRACE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('\\')) {
|
2010-03-27 17:53:17 +01:00
|
|
|
/*
|
|
|
|
* perform POSIX "quote removal" if the back-
|
|
|
|
* slash is "special", i.e. same cases as the
|
|
|
|
* {case '\\':} in Subst: plus closing brace;
|
|
|
|
* in mksh code "quote removal" on '\c' means
|
|
|
|
* write QCHAR+c, otherwise CHAR+\+CHAR+c are
|
|
|
|
* emitted (in heredocquote:)
|
|
|
|
*/
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c = getsc()) == ord('"') || c == ord('\\') ||
|
|
|
|
ctype(c, C_DOLAR | C_GRAVE) || c == ord(/*{*/ '}'))
|
2010-03-27 17:53:17 +01:00
|
|
|
goto store_qchar;
|
|
|
|
goto heredocquote;
|
|
|
|
}
|
|
|
|
goto common_SQBRACE;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case SBRACE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('\''))
|
2013-06-01 02:15:58 +02:00
|
|
|
goto open_ssquote_unless_heredoc;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord('\\'))
|
2010-02-25 21:18:19 +01:00
|
|
|
goto getsc_qchar;
|
2010-03-27 17:53:17 +01:00
|
|
|
common_SQBRACE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('"'))
|
2010-03-27 17:53:17 +01:00
|
|
|
goto open_sdquote;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord('$'))
|
2013-02-17 07:05:02 +01:00
|
|
|
goto subst_dollar_ex;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord('`'))
|
2010-02-25 21:18:19 +01:00
|
|
|
goto subst_gravis;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c != ord(/*{*/ '}'))
|
2010-02-25 21:18:19 +01:00
|
|
|
goto store_char;
|
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = /*{*/ '}';
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-07-26 18:57:28 +02:00
|
|
|
/* Same as SBASE, except (,|,) treated specially */
|
|
|
|
case STBRACEKORN:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('|'))
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = SPAT;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord('(')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OPAT;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* simile for @ */
|
|
|
|
*wp++ = ' ';
|
2005-05-23 05:06:10 +02:00
|
|
|
PUSH_STATE(SPATTERN);
|
2011-07-26 18:57:28 +02:00
|
|
|
} else /* FALLTHROUGH */
|
|
|
|
case STBRACEBOURNE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(/*{*/ '}')) {
|
2011-07-26 18:57:28 +02:00
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = /*{*/ '}';
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
goto Sbase1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBQUOTE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord('`')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = 0;
|
|
|
|
POP_STATE();
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('\\')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c = getsc()) {
|
2011-03-13 00:04:48 +01:00
|
|
|
case 0:
|
|
|
|
/* trailing \ is lost */
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('$'):
|
|
|
|
case ord('`'):
|
|
|
|
case ord('\\'):
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = c;
|
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('"'):
|
2011-03-07 21:07:52 +01:00
|
|
|
if (statep->ls_bool) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
2011-03-13 00:04:48 +01:00
|
|
|
*wp++ = '\\';
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* ONEWORD */
|
|
|
|
case SWORD:
|
2005-05-23 05:06:10 +02:00
|
|
|
goto Subst;
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* LETEXPR: (( ... )) */
|
|
|
|
case SLETPAREN:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(/*(*/ ')')) {
|
2011-03-07 21:07:52 +01:00
|
|
|
if (statep->nparen > 0)
|
|
|
|
--statep->nparen;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if ((c2 = getsc()) == ord(/*(*/ ')')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
c = 0;
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
goto Done;
|
2009-10-04 14:45:23 +02:00
|
|
|
} else {
|
|
|
|
Source *s;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
ungetsc(c2);
|
2016-01-14 20:52:20 +01:00
|
|
|
ungetsc(c);
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* mismatched parenthesis -
|
2009-10-04 14:45:23 +02:00
|
|
|
* assume we were really
|
2011-03-06 02:25:35 +01:00
|
|
|
* parsing a (...) expression
|
2009-10-04 14:45:23 +02:00
|
|
|
*/
|
|
|
|
*wp = EOS;
|
|
|
|
sp = Xstring(ws, wp);
|
2015-12-12 20:05:52 +01:00
|
|
|
dp = wdstrip(sp + 1, WDS_TPUTS);
|
2009-10-04 14:45:23 +02:00
|
|
|
s = pushs(SREREAD, source->areap);
|
|
|
|
s->start = s->str = s->u.freeme = dp;
|
|
|
|
s->next = source;
|
|
|
|
source = s;
|
2017-05-06 00:53:32 +02:00
|
|
|
ungetsc('(' /*)*/);
|
|
|
|
return (ord('(' /*)*/));
|
2009-10-04 14:45:23 +02:00
|
|
|
}
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('('))
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
2011-03-07 21:07:52 +01:00
|
|
|
* parentheses inside quotes and
|
2011-03-06 02:25:35 +01:00
|
|
|
* backslashes are lost, but AT&T ksh
|
|
|
|
* doesn't count them either
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-03-07 21:07:52 +01:00
|
|
|
++statep->nparen;
|
2005-05-23 05:06:10 +02:00
|
|
|
goto Sbase2;
|
|
|
|
|
2015-09-06 21:47:01 +02:00
|
|
|
/* << or <<- delimiter */
|
2011-03-06 02:25:35 +01:00
|
|
|
case SHEREDELIM:
|
|
|
|
/*
|
|
|
|
* here delimiters need a special case since
|
2009-10-04 14:45:23 +02:00
|
|
|
* $ and `...` are not to be treated specially
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2013-01-19 19:32:56 +01:00
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\\'):
|
2013-01-19 19:32:56 +01:00
|
|
|
if ((c = getsc())) {
|
2010-01-29 10:34:31 +01:00
|
|
|
/* trailing \ is lost */
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2013-01-19 19:32:56 +01:00
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\''):
|
2013-06-01 02:15:58 +02:00
|
|
|
goto open_ssquote_unless_heredoc;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('$'):
|
|
|
|
if ((c2 = getsc()) == ord('\'')) {
|
2013-02-17 07:05:02 +01:00
|
|
|
open_sequote:
|
2013-01-19 19:32:56 +01:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
ignore_backslash_newline++;
|
2013-02-17 07:05:02 +01:00
|
|
|
PUSH_STATE(SEQUOTE);
|
|
|
|
statep->ls_bool = false;
|
2013-01-19 19:32:56 +01:00
|
|
|
break;
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c2 == ord('"')) {
|
2013-01-19 19:32:56 +01:00
|
|
|
/* FALLTHROUGH */
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('"'):
|
2014-06-29 13:28:28 +02:00
|
|
|
PUSH_SRETRACE(SHEREDQUOTE);
|
2013-01-19 19:32:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
ungetsc(c2);
|
2013-01-19 19:32:56 +01:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-09-06 21:47:01 +02:00
|
|
|
/* " in << or <<- delimiter */
|
2011-03-06 02:25:35 +01:00
|
|
|
case SHEREDQUOTE:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c != ord('"'))
|
2013-01-19 19:32:56 +01:00
|
|
|
goto Subst;
|
|
|
|
POP_SRETRACE();
|
|
|
|
dp = strnul(sp) - 1;
|
|
|
|
/* remove the trailing double quote */
|
|
|
|
*dp = '\0';
|
|
|
|
/* store the quoted string */
|
|
|
|
*wp++ = OQUOTE;
|
2014-11-19 19:44:11 +01:00
|
|
|
XcheckN(ws, wp, (dp - sp) * 2);
|
2013-01-19 19:32:56 +01:00
|
|
|
dp = sp;
|
|
|
|
while ((c = *dp++)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\\') {
|
2013-01-19 19:32:56 +01:00
|
|
|
switch ((c = *dp++)) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\\'):
|
|
|
|
case ord('"'):
|
|
|
|
case ord('$'):
|
|
|
|
case ord('`'):
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2011-03-13 00:04:48 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = '\\';
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2013-01-19 19:32:56 +01:00
|
|
|
afree(sp, ATEMP);
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
state = statep->type = SHEREDELIM;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* in *(...|...) pattern (*+?@!) */
|
|
|
|
case SPATTERN:
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == ord(/*(*/ ')')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CPAT;
|
|
|
|
POP_STATE();
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('|')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = SPAT;
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('(')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OPAT;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* simile for @ */
|
|
|
|
*wp++ = ' ';
|
2005-05-23 05:06:10 +02:00
|
|
|
PUSH_STATE(SPATTERN);
|
|
|
|
} else
|
|
|
|
goto Sbase1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-01 15:43:28 +02:00
|
|
|
Done:
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ws, wp);
|
|
|
|
if (statep != &states[1])
|
|
|
|
/* XXX figure out what is missing */
|
2017-04-06 02:41:42 +02:00
|
|
|
yyerror("no closing quote");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* This done to avoid tests for SHEREDELIM wherever SBASE tested */
|
2013-01-19 18:20:02 +01:00
|
|
|
if (state == SHEREDELIM)
|
2005-05-23 05:06:10 +02:00
|
|
|
state = SBASE;
|
|
|
|
|
|
|
|
dp = Xstring(ws, wp);
|
2013-03-29 18:33:56 +01:00
|
|
|
if (state == SBASE && (
|
|
|
|
(c == '&' && !Flag(FSH) && !Flag(FPOSIX)) ||
|
2017-04-28 02:38:33 +02:00
|
|
|
ctype(c, C_ANGLE)) && ((c2 = Xlength(ws, wp)) == 0 ||
|
2017-04-27 21:33:53 +02:00
|
|
|
(c2 == 2 && dp[0] == CHAR && ctype(dp[1], C_DIGIT)))) {
|
2009-06-08 22:06:50 +02:00
|
|
|
struct ioword *iop = alloc(sizeof(struct ioword), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2016-07-25 23:05:25 +02:00
|
|
|
iop->unit = c2 == 2 ? ksh_numdig(dp[1]) : c == '<' ? 0 : 1;
|
2008-07-09 23:32:45 +02:00
|
|
|
|
2008-06-29 00:51:56 +02:00
|
|
|
if (c == '&') {
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c2 = getsc()) != ord('>')) {
|
2008-06-29 00:51:56 +02:00
|
|
|
ungetsc(c2);
|
|
|
|
goto no_iop;
|
|
|
|
}
|
|
|
|
c = c2;
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag = IOBASH;
|
2008-06-29 00:51:56 +02:00
|
|
|
} else
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
c2 = getsc();
|
|
|
|
/* <<, >>, <> are ok, >< is not */
|
2017-05-06 00:53:32 +02:00
|
|
|
if (c == c2 || (c == ord('<') && c2 == ord('>'))) {
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag |= c == c2 ?
|
2017-05-06 00:53:32 +02:00
|
|
|
(c == ord('>') ? IOCAT : IOHERE) : IORDWR;
|
2015-04-12 00:03:32 +02:00
|
|
|
if (iop->ioflag == IOHERE) {
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c2 = getsc()) == ord('-'))
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag |= IOSKIP;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c2 == ord('<'))
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag |= IOHERESTR;
|
2015-09-06 21:47:01 +02:00
|
|
|
else
|
|
|
|
ungetsc(c2);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c2 == ord('&'))
|
|
|
|
iop->ioflag |= IODUP | (c == ord('<') ? IORDUP : 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
2017-05-06 00:53:32 +02:00
|
|
|
iop->ioflag |= c == ord('>') ? IOWRITE : IOREAD;
|
|
|
|
if (c == ord('>') && c2 == ord('|'))
|
2015-04-12 00:03:32 +02:00
|
|
|
iop->ioflag |= IOCLOB;
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
|
|
|
|
2015-10-09 21:29:50 +02:00
|
|
|
iop->ioname = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
iop->delim = NULL;
|
|
|
|
iop->heredoc = NULL;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* free word */
|
|
|
|
Xfree(ws, wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
yylval.iop = iop;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (REDIR);
|
2008-06-29 00:51:56 +02:00
|
|
|
no_iop:
|
2011-05-05 02:05:01 +02:00
|
|
|
afree(iop, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wp == dp && state == SBASE) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* free word */
|
|
|
|
Xfree(ws, wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* no word, process LEX1 character */
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c == ord('|')) || (c == ord('&')) || (c == ord(';')) ||
|
|
|
|
(c == ord('(' /*)*/))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((c2 = getsc()) == c)
|
2017-05-06 00:53:32 +02:00
|
|
|
c = (c == ord(';')) ? BREAK :
|
|
|
|
(c == ord('|')) ? LOGOR :
|
|
|
|
(c == ord('&')) ? LOGAND :
|
|
|
|
/* c == ord('(' )) */ MDPAREN;
|
|
|
|
else if (c == ord('|') && c2 == ord('&'))
|
2005-05-23 05:06:10 +02:00
|
|
|
c = COPROC;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord(';') && c2 == ord('|'))
|
2011-05-29 04:18:57 +02:00
|
|
|
c = BRKEV;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (c == ord(';') && c2 == ord('&'))
|
2011-05-29 04:18:57 +02:00
|
|
|
c = BRKFT;
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
ungetsc(c2);
|
2011-05-29 08:19:27 +02:00
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
if (c == BREAK) {
|
2017-05-06 00:53:32 +02:00
|
|
|
if ((c2 = getsc()) == ord('&'))
|
2011-05-29 08:19:27 +02:00
|
|
|
c = BRKEV;
|
|
|
|
else
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
|
|
|
#endif
|
2017-05-06 00:53:32 +02:00
|
|
|
} else if (c == ord('\n')) {
|
2015-09-06 21:47:01 +02:00
|
|
|
if (cf & HEREDELIM)
|
|
|
|
ungetsc(c);
|
|
|
|
else {
|
|
|
|
gethere();
|
|
|
|
if (cf & CONTIN)
|
|
|
|
goto Again;
|
|
|
|
}
|
2016-03-01 19:30:05 +01:00
|
|
|
} else if (c == '\0' && !(cf & HEREDELIM)) {
|
|
|
|
struct ioword **p = heres;
|
|
|
|
|
|
|
|
while (p < herep)
|
|
|
|
if ((*p)->ioflag & IOHERESTR)
|
|
|
|
++p;
|
|
|
|
else
|
|
|
|
/* ksh -c 'cat <<EOF' can cause this */
|
2016-07-25 02:04:48 +02:00
|
|
|
yyerror(Tf_heredoc,
|
2016-03-01 19:30:05 +01:00
|
|
|
evalstr((*p)->delim, 0));
|
2015-09-06 21:47:01 +02:00
|
|
|
}
|
2007-05-13 20:07:22 +02:00
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* terminate word */
|
|
|
|
*wp++ = EOS;
|
2005-05-23 05:06:10 +02:00
|
|
|
yylval.cp = Xclose(ws, wp);
|
2009-09-23 20:04:58 +02:00
|
|
|
if (state == SWORD || state == SLETPAREN
|
2011-06-04 18:11:20 +02:00
|
|
|
/* XXX ONEWORD? */)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (LWORD);
|
2008-02-24 23:12:36 +01:00
|
|
|
|
2009-07-06 17:06:25 +02:00
|
|
|
/* unget terminator */
|
|
|
|
ungetsc(c);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* note: the alias-vs-function code below depends on several
|
|
|
|
* interna: starting from here, source->str is not modified;
|
|
|
|
* the way getsc() and ungetsc() operate; etc.
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* copy word to unprefixed string ident */
|
2008-02-26 21:43:11 +01:00
|
|
|
sp = yylval.cp;
|
|
|
|
dp = ident;
|
2015-09-06 21:47:01 +02:00
|
|
|
while ((dp - ident) < IDENT && (c = *sp++) == CHAR)
|
|
|
|
*dp++ = *sp++;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c != EOS)
|
2017-03-12 03:04:15 +01:00
|
|
|
/* word is not unquoted, or space ran out */
|
2015-03-14 06:23:18 +01:00
|
|
|
dp = ident;
|
|
|
|
/* make sure the ident array stays NUL padded */
|
|
|
|
memset(dp, 0, (ident + IDENT) - dp + 1);
|
|
|
|
|
2017-03-12 03:04:15 +01:00
|
|
|
if (*ident != '\0' && (cf & (KEYWORD | ALIAS))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
struct tbl *p;
|
2009-08-28 20:54:01 +02:00
|
|
|
uint32_t h = hash(ident);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-08-28 22:30:59 +02:00
|
|
|
if ((cf & KEYWORD) && (p = ktsearch(&keywords, ident, h)) &&
|
2011-03-06 02:25:35 +01:00
|
|
|
(!(cf & ESACONLY) || p->val.i == ESAC ||
|
2017-05-06 00:53:32 +02:00
|
|
|
p->val.i == ord(/*{*/ '}'))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
afree(yylval.cp, ATEMP);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p->val.i);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-08-28 22:30:59 +02:00
|
|
|
if ((cf & ALIAS) && (p = ktsearch(&aliases, ident, h)) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(p->flag & ISSET)) {
|
2009-07-06 17:06:25 +02:00
|
|
|
/*
|
|
|
|
* this still points to the same character as the
|
|
|
|
* ungetsc'd terminator from above
|
|
|
|
*/
|
|
|
|
const char *cp = source->str;
|
|
|
|
|
|
|
|
/* prefer POSIX but not Korn functions over aliases */
|
2017-04-28 02:38:33 +02:00
|
|
|
while (ctype(*cp, C_BLANK))
|
2009-07-06 17:06:25 +02:00
|
|
|
/*
|
|
|
|
* this is like getsc() without skipping
|
|
|
|
* over Source boundaries (including not
|
|
|
|
* parsing ungetsc'd characters that got
|
|
|
|
* pushed into an SREREAD) which is what
|
|
|
|
* we want here anyway: find out whether
|
|
|
|
* the alias name is followed by a POSIX
|
2015-03-21 00:37:39 +01:00
|
|
|
* function definition
|
2009-07-06 17:06:25 +02:00
|
|
|
*/
|
|
|
|
++cp;
|
|
|
|
/* prefer functions over aliases */
|
2011-05-07 02:51:12 +02:00
|
|
|
if (cp[0] != '(' || cp[1] != ')') {
|
2008-12-02 13:39:38 +01:00
|
|
|
Source *s = source;
|
2008-02-24 23:12:36 +01:00
|
|
|
|
2010-07-21 13:31:16 +02:00
|
|
|
while (s && (s->flags & SF_HASALIAS))
|
2008-02-24 23:12:36 +01:00
|
|
|
if (s->u.tblp == p)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (LWORD);
|
2008-12-02 13:39:38 +01:00
|
|
|
else
|
|
|
|
s = s->next;
|
2008-02-24 23:12:36 +01:00
|
|
|
/* push alias expansion */
|
|
|
|
s = pushs(SALIAS, source->areap);
|
|
|
|
s->start = s->str = p->val.s;
|
|
|
|
s->u.tblp = p;
|
2008-12-02 13:39:38 +01:00
|
|
|
s->flags |= SF_HASALIAS;
|
2017-03-22 01:20:43 +01:00
|
|
|
s->line = source->line;
|
2008-02-24 23:12:36 +01:00
|
|
|
s->next = source;
|
2008-12-02 13:39:38 +01:00
|
|
|
if (source->type == SEOF) {
|
|
|
|
/* prevent infinite recursion at EOS */
|
|
|
|
source->u.tblp = p;
|
|
|
|
source->flags |= SF_HASALIAS;
|
|
|
|
}
|
2008-02-24 23:12:36 +01:00
|
|
|
source = s;
|
|
|
|
afree(yylval.cp, ATEMP);
|
|
|
|
goto Again;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2017-03-12 03:04:15 +01:00
|
|
|
} else if (*ident == '\0') {
|
2015-03-14 06:23:18 +01:00
|
|
|
/* retain typeset et al. even when quoted */
|
2017-03-12 03:04:15 +01:00
|
|
|
struct tbl *tt = get_builtin((dp = wdstrip(yylval.cp, 0)));
|
|
|
|
uint32_t flag = tt ? tt->flag : 0;
|
|
|
|
|
|
|
|
if (flag & (DECL_UTIL | DECL_FWDR))
|
2015-03-14 06:23:18 +01:00
|
|
|
strlcpy(ident, dp, sizeof(ident));
|
|
|
|
afree(dp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (LWORD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-06 21:47:01 +02:00
|
|
|
gethere(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct ioword **p;
|
|
|
|
|
|
|
|
for (p = heres; p < herep; p++)
|
2015-09-06 21:47:01 +02:00
|
|
|
if (!((*p)->ioflag & IOHERESTR))
|
2008-02-26 21:43:11 +01:00
|
|
|
readhere(*p);
|
2005-05-23 05:06:10 +02:00
|
|
|
herep = heres;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read "<<word" text into temp file
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
readhere(struct ioword *iop)
|
|
|
|
{
|
|
|
|
int c;
|
2011-05-05 02:05:01 +02:00
|
|
|
const char *eof, *eofp;
|
2005-05-23 05:06:10 +02:00
|
|
|
XString xs;
|
|
|
|
char *xp;
|
2015-09-06 15:10:48 +02:00
|
|
|
size_t xpos;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-09-06 21:47:01 +02:00
|
|
|
eof = evalstr(iop->delim, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-04-12 00:03:32 +02:00
|
|
|
if (!(iop->ioflag & IOEVAL))
|
2005-05-23 05:06:10 +02:00
|
|
|
ignore_backslash_newline++;
|
|
|
|
|
|
|
|
Xinit(xs, xp, 256, ATEMP);
|
|
|
|
|
2011-03-07 21:32:50 +01:00
|
|
|
heredoc_read_line:
|
|
|
|
/* beginning of line */
|
|
|
|
eofp = eof;
|
|
|
|
xpos = Xsavepos(xs, xp);
|
2015-04-12 00:03:32 +02:00
|
|
|
if (iop->ioflag & IOSKIP) {
|
2011-03-07 21:32:50 +01:00
|
|
|
/* skip over leading tabs */
|
|
|
|
while ((c = getsc()) == '\t')
|
2015-03-01 17:02:48 +01:00
|
|
|
; /* nothing */
|
2011-03-07 21:32:50 +01:00
|
|
|
goto heredoc_parse_char;
|
|
|
|
}
|
|
|
|
heredoc_read_char:
|
|
|
|
c = getsc();
|
|
|
|
heredoc_parse_char:
|
|
|
|
/* compare with here document marker */
|
|
|
|
if (!*eofp) {
|
|
|
|
/* end of here document marker, what to do? */
|
|
|
|
switch (c) {
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord(/*(*/ ')'):
|
2012-10-22 22:19:18 +02:00
|
|
|
if (!subshell_nesting_type)
|
2011-03-26 16:32:37 +01:00
|
|
|
/*-
|
|
|
|
* not allowed outside $(...) or (...)
|
|
|
|
* => mismatch
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-03-26 16:32:37 +01:00
|
|
|
/* allow $(...) or (...) to close here */
|
2011-03-07 21:32:50 +01:00
|
|
|
ungetsc(/*(*/ ')');
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* Allow EOF here to commands without trailing
|
|
|
|
* newlines (mksh -c '...') will work as well.
|
|
|
|
*/
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('\n'):
|
2011-03-07 21:32:50 +01:00
|
|
|
/* Newline terminates here document marker */
|
|
|
|
goto heredoc_found_terminator;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-07 21:32:50 +01:00
|
|
|
} else if (c == *eofp++)
|
|
|
|
/* store; then read and compare next character */
|
|
|
|
goto heredoc_store_and_loop;
|
|
|
|
/* nope, mismatch; read until end of line */
|
|
|
|
while (c != '\n') {
|
|
|
|
if (!c)
|
|
|
|
/* oops, reached EOF */
|
2016-07-25 02:04:48 +02:00
|
|
|
yyerror(Tf_heredoc, eof);
|
2011-03-07 21:32:50 +01:00
|
|
|
/* store character */
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(xs, xp);
|
|
|
|
Xput(xs, xp, c);
|
2011-03-07 21:32:50 +01:00
|
|
|
/* read next character */
|
|
|
|
c = getsc();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-07 21:32:50 +01:00
|
|
|
/* we read a newline as last character */
|
|
|
|
heredoc_store_and_loop:
|
|
|
|
/* store character */
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
Xput(xs, xp, c);
|
|
|
|
if (c == '\n')
|
|
|
|
goto heredoc_read_line;
|
|
|
|
goto heredoc_read_char;
|
|
|
|
|
|
|
|
heredoc_found_terminator:
|
|
|
|
/* jump back to saved beginning of line */
|
|
|
|
xp = Xrestpos(xs, xp, xpos);
|
|
|
|
/* terminate, close and store */
|
2005-05-23 05:06:10 +02:00
|
|
|
Xput(xs, xp, '\0');
|
|
|
|
iop->heredoc = Xclose(xs, xp);
|
|
|
|
|
2015-04-12 00:03:32 +02:00
|
|
|
if (!(iop->ioflag & IOEVAL))
|
2005-05-23 05:06:10 +02:00
|
|
|
ignore_backslash_newline--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
yyerror(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
/* pop aliases and re-reads */
|
|
|
|
while (source->type == SALIAS || source->type == SREREAD)
|
|
|
|
source = source->next;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* zap pending input */
|
|
|
|
source->str = null;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
error_prefix(true);
|
|
|
|
va_start(va, fmt);
|
|
|
|
shf_vfprintf(shl_out, fmt, va);
|
2017-04-06 02:41:42 +02:00
|
|
|
shf_putc('\n', shl_out);
|
2005-05-23 05:06:10 +02:00
|
|
|
va_end(va);
|
2007-07-22 16:01:50 +02:00
|
|
|
errorfz();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input for yylex with alias expansion
|
|
|
|
*/
|
|
|
|
|
|
|
|
Source *
|
2008-12-13 18:02:18 +01:00
|
|
|
pushs(int type, Area *areap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Source *s;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
s = alloc(sizeof(Source), areap);
|
2010-07-04 20:29:40 +02:00
|
|
|
memset(s, 0, sizeof(Source));
|
2005-05-23 05:06:10 +02:00
|
|
|
s->type = type;
|
|
|
|
s->str = null;
|
|
|
|
s->areap = areap;
|
2008-03-28 14:46:53 +01:00
|
|
|
if (type == SFILE || type == SSTDIN)
|
|
|
|
XinitN(s->xs, 256, s->areap);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-09-07 17:24:22 +02:00
|
|
|
getsc_uu(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Source *s = source;
|
|
|
|
int c;
|
|
|
|
|
2017-05-06 00:53:32 +02:00
|
|
|
while ((c = ord(*s->str++)) == 0) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* return 0 for EOF by default */
|
|
|
|
s->str = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (s->type) {
|
|
|
|
case SEOF:
|
|
|
|
s->str = null;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case SSTDIN:
|
|
|
|
case SFILE:
|
|
|
|
getsc_line(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SWSTR:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSTRING:
|
2012-06-28 22:05:11 +02:00
|
|
|
case SSTRINGCMDLINE:
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SWORDS:
|
|
|
|
s->start = s->str = *s->u.strv++;
|
|
|
|
s->type = SWORDSEP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SWORDSEP:
|
|
|
|
if (*s->u.strv == NULL) {
|
2007-07-22 15:34:52 +02:00
|
|
|
s->start = s->str = "\n";
|
2005-05-23 05:06:10 +02:00
|
|
|
s->type = SEOF;
|
|
|
|
} else {
|
2016-07-25 02:04:48 +02:00
|
|
|
s->start = s->str = T1space;
|
2005-05-23 05:06:10 +02:00
|
|
|
s->type = SWORDS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SALIAS:
|
|
|
|
if (s->flags & SF_ALIASEND) {
|
|
|
|
/* pass on an unused SF_ALIAS flag */
|
|
|
|
source = s->next;
|
|
|
|
source->flags |= s->flags & SF_ALIAS;
|
|
|
|
s = source;
|
|
|
|
} else if (*s->u.tblp->val.s &&
|
2017-04-27 22:22:28 +02:00
|
|
|
ctype((c = strnul(s->u.tblp->val.s)[-1]), C_SPACE)) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* pop source stack */
|
|
|
|
source = s = s->next;
|
|
|
|
/*
|
|
|
|
* Note that this alias ended with a
|
|
|
|
* space, enabling alias expansion on
|
|
|
|
* the following word.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
s->flags |= SF_ALIAS;
|
|
|
|
} else {
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* At this point, we need to keep the current
|
2005-05-23 05:06:10 +02:00
|
|
|
* alias in the source list so recursive
|
2011-03-06 02:25:35 +01:00
|
|
|
* aliases can be detected and we also need to
|
|
|
|
* return the next character. Do this by
|
|
|
|
* temporarily popping the alias to get the
|
|
|
|
* next character and then put it back in the
|
|
|
|
* source list with the SF_ALIASEND flag set.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2011-03-06 02:25:35 +01:00
|
|
|
/* pop source stack */
|
|
|
|
source = s->next;
|
2005-05-23 05:06:10 +02:00
|
|
|
source->flags |= s->flags & SF_ALIAS;
|
2011-09-07 17:24:22 +02:00
|
|
|
c = getsc_uu();
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c) {
|
|
|
|
s->flags |= SF_ALIASEND;
|
|
|
|
s->ugbuf[0] = c; s->ugbuf[1] = '\0';
|
|
|
|
s->start = s->str = s->ugbuf;
|
|
|
|
s->next = source;
|
|
|
|
source = s;
|
|
|
|
} else {
|
|
|
|
s = source;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* avoid reading EOF twice */
|
2005-05-23 05:06:10 +02:00
|
|
|
s->str = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SREREAD:
|
2011-03-06 02:25:35 +01:00
|
|
|
if (s->start != s->ugbuf)
|
|
|
|
/* yuck */
|
2005-05-23 05:06:10 +02:00
|
|
|
afree(s->u.freeme, ATEMP);
|
|
|
|
source = s = s->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (s->str == NULL) {
|
|
|
|
s->type = SEOF;
|
|
|
|
s->start = s->str = null;
|
2009-06-08 22:06:50 +02:00
|
|
|
return ('\0');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (s->flags & SF_ECHO) {
|
|
|
|
shf_puts(s->str, shl_out);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
getsc_line(Source *s)
|
|
|
|
{
|
2009-05-27 21:52:38 +02:00
|
|
|
char *xp = Xstring(s->xs, xp), *cp;
|
2010-01-25 17:12:57 +01:00
|
|
|
bool interactive = Flag(FTALKING) && s->type == SSTDIN;
|
2012-11-30 20:02:10 +01:00
|
|
|
bool have_tty = tobool(interactive && (s->flags & SF_TTY));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Done here to ensure nothing odd happens when a timeout occurs */
|
|
|
|
XcheckN(s->xs, xp, LINE);
|
2009-12-05 21:17:59 +01:00
|
|
|
*xp = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
s->start = s->str = xp;
|
|
|
|
|
|
|
|
if (have_tty && ksh_tmout) {
|
|
|
|
ksh_tmout_state = TMOUT_READING;
|
|
|
|
alarm(ksh_tmout);
|
|
|
|
}
|
2016-03-01 19:00:08 +01:00
|
|
|
if (interactive) {
|
2016-04-09 15:55:12 +02:00
|
|
|
if (cur_prompt == PS1)
|
|
|
|
histsave(&s->line, NULL, HIST_FLUSH, true);
|
2010-07-25 13:35:43 +02:00
|
|
|
change_winsz();
|
2016-03-01 19:00:08 +01:00
|
|
|
}
|
2012-05-04 22:49:08 +02:00
|
|
|
#ifndef MKSH_NO_CMDLINE_EDITING
|
2007-06-15 23:55:20 +02:00
|
|
|
if (have_tty && (
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2007-06-15 23:55:20 +02:00
|
|
|
Flag(FVI) ||
|
|
|
|
#endif
|
|
|
|
Flag(FEMACS) || Flag(FGMACS))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
int nread;
|
|
|
|
|
2013-08-10 15:44:33 +02:00
|
|
|
nread = x_read(xp);
|
2011-03-06 02:25:35 +01:00
|
|
|
if (nread < 0)
|
|
|
|
/* read error */
|
2005-05-23 05:06:10 +02:00
|
|
|
nread = 0;
|
|
|
|
xp[nread] = '\0';
|
|
|
|
xp += nread;
|
2012-05-04 22:49:08 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2010-07-25 13:35:43 +02:00
|
|
|
if (interactive)
|
2005-05-23 05:06:10 +02:00
|
|
|
pprompt(prompt, 0);
|
2010-07-25 13:35:43 +02:00
|
|
|
else
|
2005-05-23 05:06:10 +02:00
|
|
|
s->line++;
|
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
char *p = shf_getse(xp, Xnleft(s->xs, xp), s->u.shf);
|
|
|
|
|
|
|
|
if (!p && shf_error(s->u.shf) &&
|
|
|
|
shf_errno(s->u.shf) == EINTR) {
|
|
|
|
shf_clearerr(s->u.shf);
|
|
|
|
if (trap)
|
|
|
|
runtraps(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!p || (xp = p, xp[-1] == '\n'))
|
|
|
|
break;
|
|
|
|
/* double buffer size */
|
2011-03-06 02:25:35 +01:00
|
|
|
/* move past NUL so doubling works... */
|
|
|
|
xp++;
|
2005-05-23 05:06:10 +02:00
|
|
|
XcheckN(s->xs, xp, Xlength(s->xs, xp));
|
2011-03-06 02:25:35 +01:00
|
|
|
/* ...and move back again */
|
|
|
|
xp--;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* flush any unwanted input so other programs/builtins
|
2009-06-10 20:12:51 +02:00
|
|
|
* can read it. Not very optimal, but less error prone
|
2005-05-23 05:06:10 +02:00
|
|
|
* than flushing else where, dealing with redirections,
|
2009-10-04 14:45:23 +02:00
|
|
|
* etc.
|
2011-03-06 02:25:35 +01:00
|
|
|
* TODO: reduce size of shf buffer (~128?) if SSTDIN
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (s->type == SSTDIN)
|
|
|
|
shf_flush(s->u.shf);
|
|
|
|
}
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* XXX: temporary kludge to restore source after a
|
2005-05-23 05:06:10 +02:00
|
|
|
* trap may have been executed.
|
|
|
|
*/
|
|
|
|
source = s;
|
|
|
|
if (have_tty && ksh_tmout) {
|
|
|
|
ksh_tmout_state = TMOUT_EXECUTING;
|
|
|
|
alarm(0);
|
|
|
|
}
|
2009-05-27 21:52:38 +02:00
|
|
|
cp = Xstring(s->xs, xp);
|
2014-01-11 19:09:43 +01:00
|
|
|
rndpush(cp);
|
2009-05-27 21:52:38 +02:00
|
|
|
s->start = s->str = cp;
|
2005-05-23 05:06:10 +02:00
|
|
|
strip_nuls(Xstring(s->xs, xp), Xlength(s->xs, xp));
|
|
|
|
/* Note: if input is all nulls, this is not eof */
|
2010-01-29 10:34:31 +01:00
|
|
|
if (Xlength(s->xs, xp) == 0) {
|
|
|
|
/* EOF */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (s->type == SFILE)
|
|
|
|
shf_fdclose(s->u.shf);
|
|
|
|
s->str = NULL;
|
2015-07-05 21:37:18 +02:00
|
|
|
} else if (interactive && *s->str) {
|
|
|
|
if (cur_prompt != PS1)
|
|
|
|
histsave(&s->line, s->str, HIST_APPEND, true);
|
|
|
|
else if (!ctype(*s->str, C_IFS | C_IFSWS))
|
|
|
|
histsave(&s->line, s->str, HIST_QUEUE, true);
|
2010-01-25 17:12:57 +01:00
|
|
|
#if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
|
2015-07-05 21:37:18 +02:00
|
|
|
else
|
|
|
|
goto check_for_sole_return;
|
2010-01-25 17:12:57 +01:00
|
|
|
} else if (interactive && cur_prompt == PS1) {
|
2015-07-05 21:37:18 +02:00
|
|
|
check_for_sole_return:
|
2010-01-25 17:12:57 +01:00
|
|
|
cp = Xstring(s->xs, xp);
|
2017-04-27 22:22:28 +02:00
|
|
|
while (ctype(*cp, C_IFSWS))
|
2010-01-25 17:12:57 +01:00
|
|
|
++cp;
|
2015-07-05 21:53:46 +02:00
|
|
|
if (!*cp) {
|
|
|
|
histsave(&s->line, NULL, HIST_FLUSH, true);
|
2010-01-25 17:12:57 +01:00
|
|
|
histsync();
|
2015-07-05 21:53:46 +02:00
|
|
|
}
|
2010-01-25 17:12:57 +01:00
|
|
|
#endif
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (interactive)
|
|
|
|
set_prompt(PS2, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_prompt(int to, Source *s)
|
|
|
|
{
|
2015-04-19 20:50:38 +02:00
|
|
|
cur_prompt = (uint8_t)to;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
switch (to) {
|
2011-03-06 02:25:35 +01:00
|
|
|
/* command */
|
|
|
|
case PS1:
|
|
|
|
/*
|
|
|
|
* Substitute ! and !! here, before substitutions are done
|
2005-05-23 05:06:10 +02:00
|
|
|
* so ! in expanded variables are not expanded.
|
2009-06-11 14:42:21 +02:00
|
|
|
* NOTE: this is not what AT&T ksh does (it does it after
|
2005-05-23 05:06:10 +02:00
|
|
|
* substitutions, POSIX doesn't say which is to be done.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct shf *shf;
|
|
|
|
char * volatile ps1;
|
2008-12-13 18:02:18 +01:00
|
|
|
Area *saved_atemp;
|
2015-07-05 15:49:42 +02:00
|
|
|
int saved_lineno;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
ps1 = str_val(global("PS1"));
|
|
|
|
shf = shf_sopen(NULL, strlen(ps1) * 2,
|
|
|
|
SHF_WR | SHF_DYNAMIC, NULL);
|
|
|
|
while (*ps1)
|
|
|
|
if (*ps1 != '!' || *++ps1 == '!')
|
|
|
|
shf_putchar(*ps1++, shf);
|
|
|
|
else
|
2016-07-25 02:04:48 +02:00
|
|
|
shf_fprintf(shf, Tf_lu, s ?
|
2015-04-19 20:50:38 +02:00
|
|
|
(unsigned long)s->line + 1 : 0UL);
|
2005-05-23 05:06:10 +02:00
|
|
|
ps1 = shf_sclose(shf);
|
2015-07-05 15:49:42 +02:00
|
|
|
saved_lineno = current_lineno;
|
|
|
|
if (s)
|
|
|
|
current_lineno = s->line + 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
saved_atemp = ATEMP;
|
|
|
|
newenv(E_ERRH);
|
2012-03-31 19:30:00 +02:00
|
|
|
if (kshsetjmp(e->jbuf)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
prompt = safe_prompt;
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Don't print an error - assume it has already
|
2009-06-10 20:12:51 +02:00
|
|
|
* been printed. Reason is we may have forked
|
2005-05-23 05:06:10 +02:00
|
|
|
* to run a command and the child may be
|
|
|
|
* unwinding its stack through this code as it
|
|
|
|
* exits.
|
|
|
|
*/
|
2008-07-12 18:56:40 +02:00
|
|
|
} else {
|
|
|
|
char *cp = substitute(ps1, 0);
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(prompt, cp, saved_atemp);
|
2008-07-12 18:56:40 +02:00
|
|
|
}
|
2015-07-05 15:49:42 +02:00
|
|
|
current_lineno = saved_lineno;
|
2005-05-23 05:06:10 +02:00
|
|
|
quitenv(NULL);
|
|
|
|
}
|
|
|
|
break;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* command continuation */
|
|
|
|
case PS2:
|
2005-05-23 05:06:10 +02:00
|
|
|
prompt = str_val(global("PS2"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:33:39 +02:00
|
|
|
int
|
|
|
|
pprompt(const char *cp, int ntruncate)
|
2006-08-01 16:10:25 +02:00
|
|
|
{
|
|
|
|
char delimiter = 0;
|
2013-08-16 12:59:03 +02:00
|
|
|
bool doprint = (ntruncate != -1);
|
|
|
|
bool indelimit = false;
|
|
|
|
int columns = 0, lines = 0;
|
2006-08-01 16:10:25 +02:00
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Undocumented AT&T ksh feature:
|
|
|
|
* If the second char in the prompt string is \r then the first
|
|
|
|
* char is taken to be a non-printing delimiter and any chars
|
|
|
|
* between two instances of the delimiter are not considered to
|
|
|
|
* be part of the prompt length
|
2006-08-01 16:10:25 +02:00
|
|
|
*/
|
|
|
|
if (*cp && cp[1] == '\r') {
|
|
|
|
delimiter = *cp;
|
|
|
|
cp += 2;
|
|
|
|
}
|
|
|
|
for (; *cp; cp++) {
|
|
|
|
if (indelimit && *cp != delimiter)
|
|
|
|
;
|
2017-04-28 02:38:33 +02:00
|
|
|
else if (ctype(*cp, C_CR | C_LF)) {
|
2006-08-02 13:33:37 +02:00
|
|
|
lines += columns / x_cols + ((*cp == '\n') ? 1 : 0);
|
2006-08-02 12:42:30 +02:00
|
|
|
columns = 0;
|
2006-08-01 16:10:25 +02:00
|
|
|
} else if (*cp == '\t') {
|
2006-08-02 12:42:30 +02:00
|
|
|
columns = (columns | 7) + 1;
|
2006-08-01 16:10:25 +02:00
|
|
|
} else if (*cp == '\b') {
|
2006-08-02 12:42:30 +02:00
|
|
|
if (columns > 0)
|
|
|
|
columns--;
|
2006-08-01 16:10:25 +02:00
|
|
|
} else if (*cp == delimiter)
|
|
|
|
indelimit = !indelimit;
|
2017-05-05 22:36:03 +02:00
|
|
|
else if (UTFMODE && (rtt2asc(*cp) > 0x7F)) {
|
2006-11-05 16:31:37 +01:00
|
|
|
const char *cp2;
|
|
|
|
columns += utf_widthadj(cp, &cp2);
|
2013-08-16 12:59:03 +02:00
|
|
|
if (doprint && (indelimit ||
|
|
|
|
(ntruncate < (x_cols * lines + columns))))
|
2006-11-05 16:31:37 +01:00
|
|
|
shf_write(cp, cp2 - cp, shl_out);
|
|
|
|
cp = cp2 - /* loop increment */ 1;
|
|
|
|
continue;
|
|
|
|
} else
|
2006-08-02 12:42:30 +02:00
|
|
|
columns++;
|
2013-08-16 12:59:03 +02:00
|
|
|
if (doprint && (*cp != delimiter) &&
|
2006-08-02 13:33:37 +02:00
|
|
|
(indelimit || (ntruncate < (x_cols * lines + columns))))
|
|
|
|
shf_putc(*cp, shl_out);
|
2006-08-01 16:10:25 +02:00
|
|
|
}
|
2013-08-16 12:59:03 +02:00
|
|
|
if (doprint)
|
|
|
|
shf_flush(shl_out);
|
2009-03-15 19:30:40 +01:00
|
|
|
return (x_cols * lines + columns);
|
2006-08-01 16:35:44 +02:00
|
|
|
}
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Read the variable part of a ${...} expression (i.e. up to but not
|
|
|
|
* including the :[-+?=#%] or close-brace).
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_brace_var(XString *wsp, char *wp)
|
|
|
|
{
|
2011-03-26 17:11:43 +01:00
|
|
|
char c;
|
2005-05-23 05:06:10 +02:00
|
|
|
enum parse_state {
|
2016-05-06 00:45:58 +02:00
|
|
|
PS_INITIAL, PS_SAW_PERCENT, PS_SAW_HASH, PS_SAW_BANG,
|
|
|
|
PS_IDENT, PS_NUMBER, PS_VAR1
|
2011-03-26 17:11:43 +01:00
|
|
|
} state = PS_INITIAL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
|
|
|
/* State machine to figure out where the variable part ends. */
|
|
|
|
switch (state) {
|
2016-06-26 01:55:00 +02:00
|
|
|
case PS_SAW_HASH:
|
|
|
|
if (ctype(c, C_VAR1)) {
|
|
|
|
char c2;
|
|
|
|
|
|
|
|
c2 = getsc();
|
|
|
|
ungetsc(c2);
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(c2) != ord(/*{*/ '}')) {
|
2016-06-26 01:55:00 +02:00
|
|
|
ungetsc(c);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto ps_common;
|
2016-05-06 00:45:58 +02:00
|
|
|
case PS_SAW_BANG:
|
2017-05-06 00:53:32 +02:00
|
|
|
switch (ord(c)) {
|
|
|
|
case ord('@'):
|
|
|
|
case ord('#'):
|
|
|
|
case ord('-'):
|
|
|
|
case ord('?'):
|
2016-05-06 00:45:58 +02:00
|
|
|
goto out;
|
2016-06-26 01:55:00 +02:00
|
|
|
}
|
|
|
|
goto ps_common;
|
2005-05-23 05:06:10 +02:00
|
|
|
case PS_INITIAL:
|
2017-05-06 00:53:32 +02:00
|
|
|
switch (ord(c)) {
|
|
|
|
case ord('%'):
|
2016-05-06 00:45:58 +02:00
|
|
|
state = PS_SAW_PERCENT;
|
|
|
|
goto next;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('#'):
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_SAW_HASH;
|
2016-05-06 00:45:58 +02:00
|
|
|
goto next;
|
2017-05-06 00:53:32 +02:00
|
|
|
case ord('!'):
|
2016-05-06 00:45:58 +02:00
|
|
|
state = PS_SAW_BANG;
|
|
|
|
goto next;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2016-05-06 00:45:58 +02:00
|
|
|
case PS_SAW_PERCENT:
|
2016-06-26 01:55:00 +02:00
|
|
|
ps_common:
|
2017-04-27 21:33:53 +02:00
|
|
|
if (ctype(c, C_ALPHX))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_IDENT;
|
2017-04-27 21:33:53 +02:00
|
|
|
else if (ctype(c, C_DIGIT))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_NUMBER;
|
2016-06-26 01:55:00 +02:00
|
|
|
else if (ctype(c, C_VAR1))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_VAR1;
|
|
|
|
else
|
2010-01-29 10:34:31 +01:00
|
|
|
goto out;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case PS_IDENT:
|
2017-04-27 21:33:53 +02:00
|
|
|
if (!ctype(c, C_ALNUX)) {
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(c) == ord('[')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
char *tmp, *p;
|
|
|
|
|
|
|
|
if (!arraysub(&tmp))
|
2017-04-06 02:41:42 +02:00
|
|
|
yyerror("missing ]");
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = c;
|
2017-05-06 00:53:32 +02:00
|
|
|
p = tmp;
|
|
|
|
while (*p) {
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(*wsp, wp);
|
|
|
|
*wp++ = *p++;
|
|
|
|
}
|
|
|
|
afree(tmp, ATEMP);
|
2011-03-06 02:25:35 +01:00
|
|
|
/* the ] */
|
|
|
|
c = getsc();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
goto out;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2016-05-06 00:45:58 +02:00
|
|
|
next:
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case PS_NUMBER:
|
2017-04-27 21:33:53 +02:00
|
|
|
if (!ctype(c, C_DIGIT))
|
2010-01-29 10:34:31 +01:00
|
|
|
goto out;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case PS_VAR1:
|
2010-01-29 10:34:31 +01:00
|
|
|
goto out;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
Xcheck(*wsp, wp);
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
out:
|
2011-03-06 02:25:35 +01:00
|
|
|
/* end of variable part */
|
|
|
|
*wp++ = '\0';
|
2010-01-29 10:34:31 +01:00
|
|
|
ungetsc(c);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save an array subscript - returns true if matching bracket found, false
|
|
|
|
* if eof or newline was found.
|
|
|
|
* (Returned string double null terminated)
|
|
|
|
*/
|
2011-03-07 21:30:41 +01:00
|
|
|
static bool
|
2005-05-23 05:06:10 +02:00
|
|
|
arraysub(char **strp)
|
|
|
|
{
|
|
|
|
XString ws;
|
2011-03-06 02:25:35 +01:00
|
|
|
char *wp, c;
|
|
|
|
/* we are just past the initial [ */
|
2012-10-03 19:24:23 +02:00
|
|
|
unsigned int depth = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
Xinit(ws, wp, 32, ATEMP);
|
|
|
|
|
|
|
|
do {
|
|
|
|
c = getsc();
|
|
|
|
Xcheck(ws, wp);
|
|
|
|
*wp++ = c;
|
2017-05-06 00:53:32 +02:00
|
|
|
if (ord(c) == ord('['))
|
2005-05-23 05:06:10 +02:00
|
|
|
depth++;
|
2017-05-06 00:53:32 +02:00
|
|
|
else if (ord(c) == ord(']'))
|
2005-05-23 05:06:10 +02:00
|
|
|
depth--;
|
|
|
|
} while (depth > 0 && c && c != '\n');
|
|
|
|
|
|
|
|
*wp++ = '\0';
|
|
|
|
*strp = Xclose(ws, wp);
|
|
|
|
|
2011-03-07 21:30:41 +01:00
|
|
|
return (tobool(depth == 0));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unget a char: handles case when we are already at the start of the buffer */
|
2011-03-13 02:11:58 +01:00
|
|
|
static void
|
2005-05-23 05:06:10 +02:00
|
|
|
ungetsc(int c)
|
|
|
|
{
|
2011-03-13 17:20:45 +01:00
|
|
|
struct sretrace_info *rp = retrace_info;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (backslash_skip)
|
|
|
|
backslash_skip--;
|
2011-03-06 02:25:35 +01:00
|
|
|
/* Don't unget EOF... */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (source->str == null && c == '\0')
|
2011-03-13 02:11:58 +01:00
|
|
|
return;
|
2011-03-13 17:20:45 +01:00
|
|
|
while (rp) {
|
|
|
|
if (Xlength(rp->xs, rp->xp))
|
|
|
|
rp->xp--;
|
|
|
|
rp = rp->next;
|
|
|
|
}
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
2011-03-13 02:11:58 +01:00
|
|
|
}
|
|
|
|
static void
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(int c)
|
2011-03-13 02:11:58 +01:00
|
|
|
{
|
2005-05-23 05:06:10 +02:00
|
|
|
if (source->str > source->start)
|
|
|
|
source->str--;
|
|
|
|
else {
|
|
|
|
Source *s;
|
|
|
|
|
|
|
|
s = pushs(SREREAD, source->areap);
|
|
|
|
s->ugbuf[0] = c; s->ugbuf[1] = '\0';
|
|
|
|
s->start = s->str = s->ugbuf;
|
|
|
|
s->next = source;
|
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Called to get a char that isn't a \newline sequence. */
|
|
|
|
static int
|
|
|
|
getsc_bn(void)
|
|
|
|
{
|
|
|
|
int c, c2;
|
|
|
|
|
|
|
|
if (ignore_backslash_newline)
|
2011-09-07 17:24:22 +02:00
|
|
|
return (o_getsc_u());
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (backslash_skip == 1) {
|
|
|
|
backslash_skip = 2;
|
2011-09-07 17:24:22 +02:00
|
|
|
return (o_getsc_u());
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
backslash_skip = 0;
|
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2011-09-07 17:24:22 +02:00
|
|
|
c = o_getsc_u();
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\\') {
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((c2 = o_getsc_u()) == '\n')
|
2005-05-23 05:06:10 +02:00
|
|
|
/* ignore the \newline; get the next char... */
|
|
|
|
continue;
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c2);
|
2005-05-23 05:06:10 +02:00
|
|
|
backslash_skip = 1;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-13 17:03:54 +01:00
|
|
|
void
|
|
|
|
yyskiputf8bom(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2017-05-05 22:36:03 +02:00
|
|
|
if (rtt2asc((c = o_getsc_u())) != 0xEF) {
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
2011-03-13 17:03:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-05 22:36:03 +02:00
|
|
|
if (rtt2asc((c = o_getsc_u())) != 0xBB) {
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
2017-05-05 22:36:03 +02:00
|
|
|
ungetsc_i(asc2rtt(0xEF));
|
2011-03-13 17:03:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-05 22:36:03 +02:00
|
|
|
if (rtt2asc((c = o_getsc_u())) != 0xBF) {
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
2017-05-05 22:36:03 +02:00
|
|
|
ungetsc_i(asc2rtt(0xBB));
|
|
|
|
ungetsc_i(asc2rtt(0xEF));
|
2011-03-13 17:03:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
UTFMODE |= 8;
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static Lex_state *
|
2011-10-26 00:36:39 +02:00
|
|
|
push_state_i(State_info *si, Lex_state *old_end)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-09-14 23:26:19 +02:00
|
|
|
Lex_state *news = alloc2(STATE_BSIZE, sizeof(Lex_state), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-07 21:07:52 +01:00
|
|
|
news[0].ls_base = old_end;
|
2010-07-18 00:09:40 +02:00
|
|
|
si->base = &news[0];
|
|
|
|
si->end = &news[STATE_BSIZE];
|
|
|
|
return (&news[1]);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Lex_state *
|
2011-10-26 00:36:39 +02:00
|
|
|
pop_state_i(State_info *si, Lex_state *old_end)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
Lex_state *old_base = si->base;
|
|
|
|
|
2011-03-07 21:07:52 +01:00
|
|
|
si->base = old_end->ls_base - STATE_BSIZE;
|
|
|
|
si->end = old_end->ls_base;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
afree(old_base, ATEMP);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (si->base + STATE_BSIZE - 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|