2014-01-05 20:11:46 +01:00
|
|
|
/* $OpenBSD: lex.c,v 1.49 2013/12/17 16:37:06 deraadt 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,
|
2014-01-11 19:09:43 +01:00
|
|
|
* 2011, 2012, 2013, 2014
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2014-12-15 23:50:11 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.195 2014/12/15 22:50:10 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 $(( )) */
|
2013-01-19 18:20:02 +01:00
|
|
|
#define SHEREDELIM 10 /* parsing <<,<<-,<<< delimiter */
|
|
|
|
#define SHEREDQUOTE 11 /* parsing " in <<,<<-,<<< 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 */
|
2011-03-07 21:07:52 +01:00
|
|
|
int start;
|
|
|
|
/* 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);
|
2009-09-19 23:54:46 +02:00
|
|
|
static int s_get(void);
|
|
|
|
static void s_put(int);
|
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 **);
|
2009-04-07 21:06:44 +02:00
|
|
|
static void gethere(bool);
|
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 */
|
2011-09-07 17:24:22 +02: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; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return (cev); \
|
|
|
|
}
|
|
|
|
|
2012-07-01 17:55:00 +02:00
|
|
|
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
|
2009-09-23 20:04:58 +02:00
|
|
|
static int getsc(void);
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsc(void)
|
|
|
|
{
|
2011-09-07 17:24:22 +02:00
|
|
|
o_getsc_r(o_getsc());
|
2009-09-23 20:04:58 +02:00
|
|
|
}
|
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
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
#define getsc() 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;
|
|
|
|
while ((c = getsc()) == ' ' || c == '\t')
|
|
|
|
;
|
|
|
|
if (c == '#') {
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
while ((c = getsc()) != '\0' && c != '\n')
|
|
|
|
;
|
|
|
|
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;
|
2005-07-04 14:27:28 +02:00
|
|
|
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
|
|
|
|
2008-02-26 21:43:11 +01:00
|
|
|
/* check for here string */
|
|
|
|
if (state == SHEREDELIM) {
|
|
|
|
c = getsc();
|
|
|
|
if (c == '<') {
|
2013-01-19 18:20:02 +01:00
|
|
|
state = SHEREDELIM;
|
2008-07-09 22:41:23 +02:00
|
|
|
while ((c = getsc()) == ' ' || c == '\t')
|
|
|
|
;
|
|
|
|
ungetsc(c);
|
|
|
|
c = '<';
|
2008-02-26 21:43:11 +01:00
|
|
|
goto accept_nonword;
|
|
|
|
}
|
|
|
|
ungetsc(c);
|
|
|
|
}
|
|
|
|
|
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 &&
|
|
|
|
subshell_nesting_type == /*{*/ '}' &&
|
|
|
|
c == /*{*/ '}')
|
|
|
|
/* possibly end ${ :;} */
|
|
|
|
break;
|
2008-02-26 21:43:11 +01:00
|
|
|
accept_nonword:
|
2005-05-23 05:06:10 +02:00
|
|
|
Xcheck(ws, wp);
|
|
|
|
switch (state) {
|
2007-07-06 03:53:36 +02:00
|
|
|
case SADELIM:
|
2007-07-23 16:28:52 +02:00
|
|
|
if (c == '(')
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen++;
|
2007-07-23 16:28:52 +02:00
|
|
|
else if (c == ')')
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen--;
|
2012-12-05 20:38:25 +01:00
|
|
|
else if (statep->nparen == 0 && (c == /*{*/ '}' ||
|
|
|
|
c == (int)statep->ls_adelim.delimiter)) {
|
2007-07-06 03:53:36 +02:00
|
|
|
*wp++ = ADELIM;
|
|
|
|
*wp++ = c;
|
2011-03-07 21:07:52 +01:00
|
|
|
if (c == /*{*/ '}' || --statep->ls_adelim.num == 0)
|
2007-07-06 03:53:36 +02:00
|
|
|
POP_STATE();
|
2008-03-01 22:10:26 +01:00
|
|
|
if (c == /*{*/ '}')
|
|
|
|
POP_STATE();
|
2007-07-06 03:53:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case SBASE:
|
|
|
|
if (c == '[' && (cf & (VARASN|ARRAYVAR))) {
|
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;
|
|
|
|
} else {
|
|
|
|
Source *s;
|
|
|
|
|
|
|
|
s = pushs(SREREAD,
|
2009-06-10 20:12:51 +02:00
|
|
|
source->areap);
|
|
|
|
s->start = s->str =
|
|
|
|
s->u.freeme = tmp;
|
2005-05-23 05:06:10 +02:00
|
|
|
s->next = source;
|
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*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 (*+?@!) */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '*' || c == '@' || c == '+' || c == '?' ||
|
|
|
|
c == '!') {
|
|
|
|
c2 = getsc();
|
|
|
|
if (c2 == '(' /*)*/ ) {
|
|
|
|
*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) {
|
|
|
|
case '\\':
|
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;
|
|
|
|
case '\'':
|
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;
|
|
|
|
case '"':
|
2010-02-25 21:18:19 +01:00
|
|
|
open_sdquote:
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
PUSH_STATE(SDQUOTE);
|
|
|
|
break;
|
2013-02-17 07:05:02 +01:00
|
|
|
case '$':
|
|
|
|
/*
|
|
|
|
* processing of dollar sign belongs into
|
|
|
|
* Subst, except for those which can open
|
|
|
|
* a string: $'…' and $"…"
|
|
|
|
*/
|
|
|
|
subst_dollar_ex:
|
|
|
|
c = getsc();
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
goto open_sdquote;
|
|
|
|
case '\'':
|
|
|
|
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) {
|
|
|
|
case '\\':
|
|
|
|
c = getsc();
|
|
|
|
switch (c) {
|
2005-11-22 19:36:20 +01:00
|
|
|
case '"':
|
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 */
|
2005-11-22 19:36:20 +01:00
|
|
|
case '\\':
|
2005-05-23 05:06:10 +02:00
|
|
|
case '$': case '`':
|
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;
|
|
|
|
case '$':
|
|
|
|
c = getsc();
|
2013-02-17 07:05:02 +01:00
|
|
|
SubstS:
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '(') /*)*/ {
|
|
|
|
c = getsc();
|
|
|
|
if (c == '(') /*)*/ {
|
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
|
|
|
}
|
|
|
|
} else if (c == '{') /*}*/ {
|
2013-05-02 23:59:54 +02:00
|
|
|
if ((c = getsc()) == '|') {
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
*wp++ = '{'; /*}*/
|
|
|
|
wp = get_brace_var(&ws, wp);
|
|
|
|
c = getsc();
|
|
|
|
/* allow :# and :% (ksh88 compat) */
|
|
|
|
if (c == ':') {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
2007-07-06 03:53:36 +02:00
|
|
|
if (c == ':') {
|
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;
|
|
|
|
} else if (ksh_isdigit(c) ||
|
2007-07-06 04:22:57 +02:00
|
|
|
c == '('/*)*/ || c == ' ' ||
|
2011-03-06 02:25:35 +01:00
|
|
|
/*XXX what else? */
|
|
|
|
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 == '/') {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2008-03-01 22:10:26 +01:00
|
|
|
if ((c = getsc()) == '/') {
|
|
|
|
*wp++ = ADELIM;
|
|
|
|
*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;
|
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.
|
|
|
|
*/
|
2011-07-26 18:57:28 +02:00
|
|
|
if (ctype(c, C_SUBOP2)) {
|
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
|
|
|
}
|
2006-11-10 08:52:04 +01:00
|
|
|
} else if (ksh_isalphx(c)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OSUBST;
|
|
|
|
*wp++ = 'X';
|
|
|
|
do {
|
|
|
|
Xcheck(ws, wp);
|
|
|
|
*wp++ = c;
|
|
|
|
c = getsc();
|
2006-11-10 08:52:04 +01:00
|
|
|
} while (ksh_isalnux(c));
|
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;
|
|
|
|
case '`':
|
2010-02-25 21:18:19 +01:00
|
|
|
subst_gravis:
|
2005-05-23 05:06:10 +02:00
|
|
|
PUSH_STATE(SBQUOTE);
|
|
|
|
*wp++ = COMSUB;
|
2011-03-06 02:25:35 +01:00
|
|
|
/*
|
|
|
|
* Need to know if we are inside double quotes
|
2009-06-11 14:42:21 +02:00
|
|
|
* since sh/AT&T-ksh translate the \" to " in
|
2009-10-04 14:45:23 +02:00
|
|
|
* "`...\"...`".
|
2009-10-02 20:08:37 +02:00
|
|
|
* This is not done in POSIX mode (section
|
2005-05-23 05:06:10 +02:00
|
|
|
* 3.2.3, Double Quotes: "The backquote shall
|
|
|
|
* retain its special meaning introducing the
|
|
|
|
* other form of command substitution (see
|
|
|
|
* 3.6.3). The portion of the quoted string
|
|
|
|
* from the initial backquote and the
|
|
|
|
* characters up to the next backquote that
|
|
|
|
* is not preceded by a backslash (having
|
|
|
|
* escape characters removed) defines that
|
|
|
|
* command whose output replaces `...` when
|
|
|
|
* the word is expanded."
|
|
|
|
* Section 3.6.3, Command Substitution:
|
|
|
|
* "Within the backquoted style of command
|
|
|
|
* substitution, backslash shall retain its
|
|
|
|
* literal meaning, except when followed by
|
|
|
|
* $ ` \.").
|
|
|
|
*/
|
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:
|
|
|
|
if (c == '\'') {
|
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
ignore_backslash_newline--;
|
|
|
|
} else if (c == '\\') {
|
|
|
|
if ((c2 = unbksl(true, s_get, s_put)) == -1)
|
|
|
|
c2 = s_get();
|
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;
|
|
|
|
for (cz = 0; ts[cz]; ++cz) {
|
2010-01-28 21:52:08 +01:00
|
|
|
*wp++ = QCHAR;
|
2011-08-27 20:06:52 +02:00
|
|
|
*wp++ = ts[cz];
|
2010-01-28 21:52:08 +01:00
|
|
|
}
|
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:
|
|
|
|
if (c == '\'') {
|
|
|
|
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:
|
|
|
|
if (c == '"') {
|
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CQUOTE;
|
|
|
|
} else
|
|
|
|
goto Subst;
|
|
|
|
break;
|
|
|
|
|
2011-03-06 02:25:35 +01:00
|
|
|
/* $(( ... )) */
|
|
|
|
case SASPAREN:
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '(')
|
2011-03-07 21:07:52 +01:00
|
|
|
statep->nparen++;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (c == ')') {
|
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
|
|
|
|
2011-03-13 02:11:58 +01:00
|
|
|
if ((c2 = getsc()) == /*(*/ ')') {
|
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:
|
|
|
|
if (c == '\\') {
|
|
|
|
/*
|
|
|
|
* 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:)
|
|
|
|
*/
|
|
|
|
if ((c = getsc()) == '"' || c == '\\' ||
|
|
|
|
c == '$' || c == '`' || c == /*{*/'}')
|
|
|
|
goto store_qchar;
|
|
|
|
goto heredocquote;
|
|
|
|
}
|
|
|
|
goto common_SQBRACE;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case SBRACE:
|
2010-02-25 21:18:19 +01:00
|
|
|
if (c == '\'')
|
2013-06-01 02:15:58 +02:00
|
|
|
goto open_ssquote_unless_heredoc;
|
2010-02-25 21:18:19 +01:00
|
|
|
else if (c == '\\')
|
|
|
|
goto getsc_qchar;
|
2010-03-27 17:53:17 +01:00
|
|
|
common_SQBRACE:
|
|
|
|
if (c == '"')
|
|
|
|
goto open_sdquote;
|
2010-02-25 21:18:19 +01:00
|
|
|
else if (c == '$')
|
2013-02-17 07:05:02 +01:00
|
|
|
goto subst_dollar_ex;
|
2010-02-25 21:18:19 +01:00
|
|
|
else if (c == '`')
|
|
|
|
goto subst_gravis;
|
|
|
|
else if (c != /*{*/ '}')
|
|
|
|
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:
|
|
|
|
if (c == '|')
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = SPAT;
|
2011-07-26 18:57:28 +02:00
|
|
|
else if (c == '(') {
|
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:
|
|
|
|
if (c == /*{*/ '}') {
|
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = /*{*/ '}';
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
goto Sbase1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBQUOTE:
|
|
|
|
if (c == '`') {
|
|
|
|
*wp++ = 0;
|
|
|
|
POP_STATE();
|
|
|
|
} else if (c == '\\') {
|
|
|
|
switch (c = getsc()) {
|
2011-03-13 00:04:48 +01:00
|
|
|
case 0:
|
|
|
|
/* trailing \ is lost */
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
case '\\':
|
|
|
|
case '$': case '`':
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
case '"':
|
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:
|
|
|
|
if (c == /*(*/ ')') {
|
2011-03-07 21:07:52 +01:00
|
|
|
if (statep->nparen > 0)
|
|
|
|
--statep->nparen;
|
2009-10-04 14:45:23 +02:00
|
|
|
else if ((c2 = getsc()) == /*(*/ ')') {
|
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);
|
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);
|
2011-05-03 00:52:54 +02:00
|
|
|
dp = wdstrip(sp, WDS_KEEPQ);
|
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;
|
|
|
|
return ('('/*)*/);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == '(')
|
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;
|
|
|
|
|
2013-01-19 18:20:02 +01:00
|
|
|
/* <<, <<-, <<< 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) {
|
|
|
|
case '\\':
|
|
|
|
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;
|
2013-02-17 07:05:02 +01:00
|
|
|
case '\'':
|
2013-06-01 02:15:58 +02:00
|
|
|
goto open_ssquote_unless_heredoc;
|
2013-01-19 19:32:56 +01:00
|
|
|
case '$':
|
2009-09-19 23:54:46 +02:00
|
|
|
if ((c2 = getsc()) == '\'') {
|
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;
|
|
|
|
} else if (c2 == '"') {
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '"':
|
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;
|
|
|
|
|
2013-01-19 19:32:56 +01:00
|
|
|
/* " in <<, <<-, <<< delimiter */
|
2011-03-06 02:25:35 +01:00
|
|
|
case SHEREDQUOTE:
|
2013-01-19 19:32:56 +01:00
|
|
|
if (c != '"')
|
|
|
|
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++)) {
|
2011-03-13 00:04:48 +01:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
case '$':
|
|
|
|
case '`':
|
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:
|
|
|
|
if (c == /*(*/ ')') {
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CPAT;
|
|
|
|
POP_STATE();
|
|
|
|
} else if (c == '|') {
|
|
|
|
*wp++ = SPAT;
|
|
|
|
} else if (c == '(') {
|
|
|
|
*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 */
|
|
|
|
yyerror("no closing quote\n");
|
|
|
|
|
|
|
|
/* 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 && (
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
(c == '&' && !Flag(FSH) && !Flag(FPOSIX)) ||
|
|
|
|
#endif
|
|
|
|
c == '<' || c == '>')) {
|
2009-06-08 22:06:50 +02:00
|
|
|
struct ioword *iop = alloc(sizeof(struct ioword), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2008-07-09 23:32:45 +02:00
|
|
|
if (Xlength(ws, wp) == 0)
|
2008-06-29 00:51:56 +02:00
|
|
|
iop->unit = c == '<' ? 0 : 1;
|
2008-07-09 23:32:45 +02:00
|
|
|
else for (iop->unit = 0, c2 = 0; c2 < Xlength(ws, wp); c2 += 2) {
|
|
|
|
if (dp[c2] != CHAR)
|
|
|
|
goto no_iop;
|
|
|
|
if (!ksh_isdigit(dp[c2 + 1]))
|
|
|
|
goto no_iop;
|
|
|
|
iop->unit = (iop->unit * 10) + dp[c2 + 1] - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iop->unit >= FDBASE)
|
|
|
|
goto no_iop;
|
2008-06-29 00:51:56 +02:00
|
|
|
|
|
|
|
if (c == '&') {
|
|
|
|
if ((c2 = getsc()) != '>') {
|
|
|
|
ungetsc(c2);
|
|
|
|
goto no_iop;
|
|
|
|
}
|
|
|
|
c = c2;
|
|
|
|
iop->flag = IOBASH;
|
|
|
|
} else
|
|
|
|
iop->flag = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
c2 = getsc();
|
|
|
|
/* <<, >>, <> are ok, >< is not */
|
|
|
|
if (c == c2 || (c == '<' && c2 == '>')) {
|
2008-06-29 00:51:56 +02:00
|
|
|
iop->flag |= c == c2 ?
|
2005-05-23 05:06:10 +02:00
|
|
|
(c == '>' ? IOCAT : IOHERE) : IORDWR;
|
|
|
|
if (iop->flag == IOHERE) {
|
2011-05-05 02:05:01 +02:00
|
|
|
if ((c2 = getsc()) == '-') {
|
2005-05-23 05:06:10 +02:00
|
|
|
iop->flag |= IOSKIP;
|
2011-05-05 02:05:01 +02:00
|
|
|
c2 = getsc();
|
|
|
|
} else if (c2 == '<')
|
|
|
|
iop->flag |= IOHERESTR;
|
|
|
|
ungetsc(c2);
|
|
|
|
if (c2 == '\n')
|
|
|
|
iop->flag |= IONDELIM;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else if (c2 == '&')
|
2008-06-29 00:51:56 +02:00
|
|
|
iop->flag |= IODUP | (c == '<' ? IORDUP : 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
2008-06-29 00:51:56 +02:00
|
|
|
iop->flag |= c == '>' ? IOWRITE : IOREAD;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '>' && c2 == '|')
|
|
|
|
iop->flag |= IOCLOB;
|
|
|
|
else
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
iop->name = NULL;
|
|
|
|
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 */
|
2007-05-13 20:07:22 +02:00
|
|
|
if ((c == '|') || (c == '&') || (c == ';') || (c == '('/*)*/)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((c2 = getsc()) == c)
|
|
|
|
c = (c == ';') ? BREAK :
|
|
|
|
(c == '|') ? LOGOR :
|
|
|
|
(c == '&') ? LOGAND :
|
2007-06-23 02:05:04 +02:00
|
|
|
/* c == '(' ) */ MDPAREN;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (c == '|' && c2 == '&')
|
|
|
|
c = COPROC;
|
2011-05-29 04:18:57 +02:00
|
|
|
else if (c == ';' && c2 == '|')
|
|
|
|
c = BRKEV;
|
|
|
|
else if (c == ';' && c2 == '&')
|
|
|
|
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) {
|
|
|
|
if ((c2 = getsc()) == '&')
|
|
|
|
c = BRKEV;
|
|
|
|
else
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-13 20:07:22 +02:00
|
|
|
} else if (c == '\n') {
|
2008-02-26 21:43:11 +01:00
|
|
|
gethere(false);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (cf & CONTIN)
|
|
|
|
goto Again;
|
2008-02-26 21:43:11 +01:00
|
|
|
} else if (c == '\0')
|
|
|
|
/* need here strings at EOF */
|
|
|
|
gethere(true);
|
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;
|
2014-12-15 23:50:11 +01:00
|
|
|
if ((cf & HEREDELIM) && (sp[1] == '<')) {
|
|
|
|
herestringloop:
|
|
|
|
switch ((c = *sp++)) {
|
|
|
|
case CHAR:
|
|
|
|
++sp;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case OQUOTE:
|
|
|
|
case CQUOTE:
|
|
|
|
goto herestringloop;
|
|
|
|
default:
|
|
|
|
break;
|
2008-03-05 17:49:22 +01:00
|
|
|
}
|
2014-12-15 23:50:11 +01:00
|
|
|
/* dummy value */
|
|
|
|
*dp++ = 'x';
|
|
|
|
} else
|
2012-10-03 19:24:23 +02:00
|
|
|
while ((dp - ident) < IDENT && (c = *sp++) == CHAR)
|
2008-02-26 21:43:11 +01:00
|
|
|
*dp++ = *sp++;
|
2014-12-15 23:50:11 +01:00
|
|
|
/* make sure the ident array stays NUL padded */
|
2012-10-03 19:24:23 +02:00
|
|
|
memset(dp, 0, (ident + IDENT) - dp + 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c != EOS)
|
2011-03-06 02:25:35 +01:00
|
|
|
/* word is not unquoted */
|
|
|
|
*ident = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-13 16:57:23 +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 ||
|
|
|
|
p->val.i == /*{*/ '}')) {
|
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 */
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* function definition (only the opening
|
|
|
|
* parenthesis is checked though)
|
|
|
|
*/
|
|
|
|
++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;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (LWORD);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-02-26 21:43:11 +01:00
|
|
|
gethere(bool iseof)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct ioword **p;
|
|
|
|
|
|
|
|
for (p = heres; p < herep; p++)
|
2011-05-05 02:05:01 +02:00
|
|
|
if (iseof && !((*p)->flag & IOHERESTR))
|
2008-02-26 22:08:33 +01:00
|
|
|
/* only here strings at EOF */
|
2008-02-26 21:43:11 +01:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
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;
|
|
|
|
int xpos;
|
|
|
|
|
2011-05-05 02:05:01 +02:00
|
|
|
if (iop->flag & IOHERESTR) {
|
2008-02-26 22:08:33 +01:00
|
|
|
/* process the here string */
|
2011-05-05 02:05:01 +02:00
|
|
|
iop->heredoc = xp = evalstr(iop->delim, DOBLANK);
|
2011-08-27 20:06:52 +02:00
|
|
|
xpos = strlen(xp) - 1;
|
|
|
|
memmove(xp, xp + 1, xpos);
|
|
|
|
xp[xpos] = '\n';
|
2008-02-26 21:43:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-05 02:05:01 +02:00
|
|
|
eof = iop->flag & IONDELIM ? "<<" : evalstr(iop->delim, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!(iop->flag & IOEVAL))
|
|
|
|
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);
|
|
|
|
if (iop->flag & IOSKIP) {
|
|
|
|
/* skip over leading tabs */
|
|
|
|
while ((c = getsc()) == '\t')
|
|
|
|
/* nothing */;
|
|
|
|
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) {
|
|
|
|
case /*(*/ ')':
|
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.
|
|
|
|
*/
|
|
|
|
case '\n':
|
|
|
|
/* 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 */
|
|
|
|
yyerror("%s '%s' unclosed\n", "here document", eof);
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
if (!(iop->flag & IOEVAL))
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
|
|
|
while ((c = *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 {
|
2007-07-22 15:34:52 +02:00
|
|
|
s->start = s->str = " ";
|
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 &&
|
2008-10-28 15:32:43 +01:00
|
|
|
(c = strnul(s->u.tblp->val.s)[-1], ksh_isspace(c))) {
|
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);
|
|
|
|
}
|
2010-07-25 13:35:43 +02:00
|
|
|
if (interactive)
|
|
|
|
change_winsz();
|
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;
|
2008-09-30 21:28:12 +02:00
|
|
|
} else if (interactive && *s->str &&
|
2010-01-25 17:12:57 +01:00
|
|
|
(cur_prompt != PS1 || !ctype(*s->str, C_IFS | C_IFSWS))) {
|
2008-09-30 21:28:12 +02:00
|
|
|
histsave(&s->line, s->str, true, true);
|
2010-01-25 17:12:57 +01:00
|
|
|
#if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
|
|
|
|
} else if (interactive && cur_prompt == PS1) {
|
|
|
|
cp = Xstring(s->xs, xp);
|
|
|
|
while (*cp && ctype(*cp, C_IFSWS))
|
|
|
|
++cp;
|
|
|
|
if (!*cp)
|
|
|
|
histsync();
|
|
|
|
#endif
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (interactive)
|
|
|
|
set_prompt(PS2, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_prompt(int to, Source *s)
|
|
|
|
{
|
|
|
|
cur_prompt = to;
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
shf_fprintf(shf, "%d",
|
|
|
|
s ? s->line + 1 : 0);
|
|
|
|
ps1 = shf_sclose(shf);
|
|
|
|
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
|
|
|
}
|
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)
|
|
|
|
;
|
|
|
|
else if (*cp == '\n' || *cp == '\r') {
|
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;
|
2008-12-04 19:11:08 +01:00
|
|
|
else if (UTFMODE && ((unsigned char)*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 {
|
2009-06-10 20:12:51 +02:00
|
|
|
PS_INITIAL, PS_SAW_HASH, PS_IDENT,
|
2010-01-29 10:34:31 +01:00
|
|
|
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) {
|
|
|
|
case PS_INITIAL:
|
2009-11-22 00:23:20 +01:00
|
|
|
if (c == '#' || c == '!' || c == '%') {
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_SAW_HASH;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case PS_SAW_HASH:
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isalphx(c))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_IDENT;
|
2006-11-10 08:52:04 +01:00
|
|
|
else if (ksh_isdigit(c))
|
2005-05-23 05:06:10 +02:00
|
|
|
state = PS_NUMBER;
|
2011-03-26 17:11:43 +01:00
|
|
|
else if (c == '#') {
|
|
|
|
if (state == PS_SAW_HASH) {
|
|
|
|
char c2;
|
|
|
|
|
|
|
|
c2 = getsc();
|
|
|
|
ungetsc(c2);
|
2012-03-29 21:23:01 +02:00
|
|
|
if (c2 != /*{*/ '}') {
|
2011-03-26 17:11:43 +01:00
|
|
|
ungetsc(c);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state = PS_VAR1;
|
|
|
|
} 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:
|
2006-11-10 08:52:04 +01:00
|
|
|
if (!ksh_isalnux(c)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '[') {
|
|
|
|
char *tmp, *p;
|
|
|
|
|
|
|
|
if (!arraysub(&tmp))
|
|
|
|
yyerror("missing ]\n");
|
|
|
|
*wp++ = c;
|
|
|
|
for (p = tmp; *p; ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PS_NUMBER:
|
2006-11-10 08:52:04 +01:00
|
|
|
if (!ksh_isdigit(c))
|
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;
|
|
|
|
if (c == '[')
|
|
|
|
depth++;
|
|
|
|
else if (c == ']')
|
|
|
|
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;
|
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((unsigned char)(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;
|
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((unsigned char)(c = o_getsc_u()) != 0xBB) {
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
|
|
|
ungetsc_i(0xEF);
|
2011-03-13 17:03:54 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
if ((unsigned char)(c = o_getsc_u()) != 0xBF) {
|
2011-10-26 00:36:39 +02:00
|
|
|
ungetsc_i(c);
|
|
|
|
ungetsc_i(0xBB);
|
|
|
|
ungetsc_i(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
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
s_get(void)
|
|
|
|
{
|
|
|
|
return (getsc());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_put(int c)
|
|
|
|
{
|
|
|
|
ungetsc(c);
|
|
|
|
}
|