2008-07-09 00:28:26 +02:00
|
|
|
/* $OpenBSD: lex.c,v 1.44 2008/07/03 17:52:08 otto Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2010-01-28 21:58:34 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
|
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"
|
|
|
|
|
2010-03-31 14:20:04 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.112 2010/03/31 12:20:04 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 "${}" */
|
|
|
|
#define SCSPAREN 8 /* inside $() */
|
|
|
|
#define SBQUOTE 9 /* inside `` */
|
|
|
|
#define SASPAREN 10 /* inside $(( )) */
|
|
|
|
#define SHEREDELIM 11 /* parsing <<,<<- delimiter */
|
|
|
|
#define SHEREDQUOTE 12 /* parsing " in <<,<<- delimiter */
|
|
|
|
#define SPATTERN 13 /* parsing *(...|...) pattern (*+?@!) */
|
|
|
|
#define STBRACE 14 /* parsing ${...[#%]...} */
|
|
|
|
#define SLETARRAY 15 /* inside =( ), just copy */
|
|
|
|
#define SADELIM 16 /* like SBASE, looking for delimiter */
|
|
|
|
#define SHERESTRING 17 /* parsing <<< string */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Structure to keep track of the lexing state and the various pieces of info
|
2005-06-08 23:51:22 +02:00
|
|
|
* needed for each particular state. */
|
2005-05-23 05:06:10 +02:00
|
|
|
typedef struct lex_state Lex_state;
|
|
|
|
struct lex_state {
|
|
|
|
int ls_state;
|
|
|
|
union {
|
|
|
|
/* $(...) */
|
|
|
|
struct scsparen_info {
|
|
|
|
int nparen; /* count open parenthesis */
|
|
|
|
int csstate; /* XXX remove */
|
|
|
|
#define ls_scsparen ls_info.u_scsparen
|
|
|
|
} u_scsparen;
|
|
|
|
|
|
|
|
/* $((...)) */
|
|
|
|
struct sasparen_info {
|
|
|
|
int nparen; /* count open parenthesis */
|
|
|
|
int start; /* marks start of $(( in output str */
|
|
|
|
#define ls_sasparen ls_info.u_sasparen
|
|
|
|
} u_sasparen;
|
|
|
|
|
|
|
|
/* ((...)) */
|
|
|
|
struct sletparen_info {
|
|
|
|
int nparen; /* count open parenthesis */
|
|
|
|
#define ls_sletparen ls_info.u_sletparen
|
|
|
|
} u_sletparen;
|
|
|
|
|
|
|
|
/* `...` */
|
|
|
|
struct sbquote_info {
|
|
|
|
int indquotes; /* true if in double quotes: "`...`" */
|
|
|
|
#define ls_sbquote ls_info.u_sbquote
|
|
|
|
} u_sbquote;
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-06-23 01:34:42 +02:00
|
|
|
/* =(...) */
|
|
|
|
struct sletarray_info {
|
|
|
|
int nparen; /* count open parentheses */
|
|
|
|
#define ls_sletarray ls_info.u_sletarray
|
|
|
|
} u_sletarray;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2007-07-06 01:48:53 +02:00
|
|
|
|
2007-07-06 03:53:36 +02:00
|
|
|
/* ADELIM */
|
|
|
|
struct sadelim_info {
|
2007-07-23 16:28:52 +02:00
|
|
|
unsigned char nparen; /* count open parentheses */
|
2007-07-08 00:29:36 +02:00
|
|
|
#define SADELIM_BASH 0
|
|
|
|
#define SADELIM_MAKE 1
|
|
|
|
unsigned char style;
|
2007-07-06 03:53:36 +02:00
|
|
|
unsigned char delimiter;
|
|
|
|
unsigned char num;
|
2007-07-08 00:29:36 +02:00
|
|
|
unsigned char flags; /* ofs. into sadelim_flags[] */
|
2007-07-06 03:53:36 +02:00
|
|
|
#define ls_sadelim ls_info.u_sadelim
|
|
|
|
} u_sadelim;
|
|
|
|
|
2009-09-19 23:54:46 +02:00
|
|
|
/* $'...' */
|
|
|
|
struct sequote_info {
|
|
|
|
bool got_NUL; /* ignore rest of string */
|
|
|
|
#define ls_sequote ls_info.u_sequote
|
|
|
|
} u_sequote;
|
|
|
|
|
2007-07-06 01:48:53 +02:00
|
|
|
Lex_state *base; /* used to point to next state block */
|
2005-05-23 05:06:10 +02:00
|
|
|
} ls_info;
|
|
|
|
};
|
|
|
|
|
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 *);
|
|
|
|
static int getsc__(void);
|
|
|
|
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 *);
|
|
|
|
static int arraysub(char **);
|
2005-05-23 05:06:10 +02:00
|
|
|
static const char *ungetsc(int);
|
2009-04-07 21:06:44 +02:00
|
|
|
static void gethere(bool);
|
2005-05-23 05:06:10 +02:00
|
|
|
static Lex_state *push_state_(State_info *, Lex_state *);
|
|
|
|
static Lex_state *pop_state_(State_info *, Lex_state *);
|
|
|
|
|
2009-03-15 19:30:40 +01:00
|
|
|
static int dopprompt(const char *, int, bool);
|
2006-08-01 16:35:44 +02:00
|
|
|
|
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() */
|
2009-09-23 20:04:58 +02:00
|
|
|
#define _getsc() (*source->str != '\0' && *source->str != '\\' \
|
2007-04-15 12:45:59 +02:00
|
|
|
&& !backslash_skip && !(source->flags & SF_FIRST) \
|
|
|
|
? *source->str++ : getsc_bn())
|
2007-01-12 11:18:22 +01:00
|
|
|
/* optimised getsc__() */
|
2009-09-23 20:04:58 +02:00
|
|
|
#define _getsc_() ((*source->str != '\0') && !(source->flags & SF_FIRST) \
|
2007-04-15 12:45:59 +02:00
|
|
|
? *source->str++ : getsc__())
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
static int getsc(void);
|
|
|
|
static int getsc_(void);
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsc(void)
|
|
|
|
{
|
|
|
|
return (_getsc());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsc_(void)
|
|
|
|
{
|
|
|
|
return (_getsc_());
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* !MKSH_SMALL: use them inline */
|
|
|
|
#define getsc() _getsc()
|
|
|
|
#define getsc_() _getsc_()
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#define STATE_BSIZE 32
|
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
#define PUSH_STATE(s) do { \
|
|
|
|
if (++statep == state_info.end) \
|
|
|
|
statep = push_state_(&state_info, statep); \
|
|
|
|
state = statep->ls_state = (s); \
|
|
|
|
} while (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) \
|
|
|
|
statep = pop_state_(&state_info, statep); \
|
|
|
|
state = statep->ls_state; \
|
|
|
|
} while (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))".
|
|
|
|
* hence the state stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
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:
|
2005-05-23 05:06:10 +02:00
|
|
|
states[0].ls_state = -1;
|
|
|
|
states[0].ls_info.base = NULL;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (cf&ONEWORD)
|
|
|
|
state = SWORD;
|
|
|
|
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;
|
|
|
|
statep->ls_sletparen.nparen = 0;
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-06-23 01:34:42 +02:00
|
|
|
} else if (cf&LETARRAY) {
|
|
|
|
state = SLETARRAY;
|
|
|
|
statep->ls_sletarray.nparen = 0;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
} else { /* normal lexing */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (source->flags & SF_ALIAS) { /* trailing ' ' in alias definition */
|
|
|
|
source->flags &= ~SF_ALIAS;
|
2005-07-04 14:27:28 +02:00
|
|
|
cf |= ALIAS;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initial state: one of SBASE SHEREDELIM SWORD SASPAREN */
|
|
|
|
statep->ls_state = state;
|
|
|
|
|
2008-02-26 21:43:11 +01:00
|
|
|
/* check for here string */
|
|
|
|
if (state == SHEREDELIM) {
|
|
|
|
c = getsc();
|
|
|
|
if (c == '<') {
|
|
|
|
state = SHERESTRING;
|
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 ||
|
2008-02-26 21:43:11 +01:00
|
|
|
((state == SBASE || state == SHEREDELIM || state == SHERESTRING) &&
|
|
|
|
ctype(c, C_LEX1)))) {
|
|
|
|
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 == '(')
|
|
|
|
statep->ls_sadelim.nparen++;
|
|
|
|
else if (c == ')')
|
|
|
|
statep->ls_sadelim.nparen--;
|
|
|
|
else if (statep->ls_sadelim.nparen == 0 &&
|
|
|
|
(c == /*{*/ '}' || c == statep->ls_sadelim.delimiter)) {
|
2007-07-06 03:53:36 +02:00
|
|
|
*wp++ = ADELIM;
|
|
|
|
*wp++ = c;
|
|
|
|
if (c == /*{*/ '}' || --statep->ls_sadelim.num == 0)
|
|
|
|
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))) {
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp = EOS; /* temporary */
|
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 '\'':
|
2010-02-25 21:18:19 +01:00
|
|
|
open_ssquote:
|
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;
|
|
|
|
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 '"':
|
|
|
|
if ((cf & HEREDOC))
|
|
|
|
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 '$':
|
2010-02-25 21:18:19 +01:00
|
|
|
subst_dollar:
|
2005-05-23 05:06:10 +02:00
|
|
|
c = getsc();
|
|
|
|
if (c == '(') /*)*/ {
|
|
|
|
c = getsc();
|
|
|
|
if (c == '(') /*)*/ {
|
|
|
|
PUSH_STATE(SASPAREN);
|
|
|
|
statep->ls_sasparen.nparen = 2;
|
|
|
|
statep->ls_sasparen.start =
|
|
|
|
Xsavepos(ws, wp);
|
|
|
|
*wp++ = EXPRSUB;
|
|
|
|
} else {
|
|
|
|
ungetsc(c);
|
|
|
|
PUSH_STATE(SCSPAREN);
|
|
|
|
statep->ls_scsparen.nparen = 1;
|
|
|
|
statep->ls_scsparen.csstate = 0;
|
|
|
|
*wp++ = COMSUB;
|
|
|
|
}
|
|
|
|
} else if (c == '{') /*}*/ {
|
|
|
|
*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);
|
2007-07-08 00:29:36 +02:00
|
|
|
statep->ls_sadelim.style = SADELIM_BASH;
|
2007-07-06 03:53:36 +02:00
|
|
|
statep->ls_sadelim.delimiter = ':';
|
|
|
|
statep->ls_sadelim.num = 1;
|
2007-07-23 16:28:52 +02:00
|
|
|
statep->ls_sadelim.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 == ' ' ||
|
2007-07-06 03:53:36 +02:00
|
|
|
c == '$' /* XXX what else? */) {
|
|
|
|
/* 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);
|
2007-07-08 00:29:36 +02:00
|
|
|
statep->ls_sadelim.style = SADELIM_BASH;
|
2007-07-06 03:53:36 +02:00
|
|
|
statep->ls_sadelim.delimiter = ':';
|
|
|
|
statep->ls_sadelim.num = 2;
|
2007-07-23 16:28:52 +02:00
|
|
|
statep->ls_sadelim.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);
|
|
|
|
statep->ls_sadelim.style = SADELIM_BASH;
|
|
|
|
statep->ls_sadelim.delimiter = '/';
|
|
|
|
statep->ls_sadelim.num = 1;
|
|
|
|
statep->ls_sadelim.nparen = 0;
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* If this is a trim operation,
|
|
|
|
* treat (,|,) specially in STBRACE.
|
|
|
|
*/
|
2008-02-27 02:00:10 +01:00
|
|
|
if (ctype(c, C_SUBOP2)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
ungetsc(c);
|
|
|
|
PUSH_STATE(STBRACE);
|
|
|
|
} else {
|
|
|
|
ungetsc(c);
|
2010-02-23 22:51:49 +01:00
|
|
|
if (state == SDQUOTE)
|
|
|
|
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';
|
2009-10-04 14:44:19 +02:00
|
|
|
} else if (c == '\'' && (state == SBASE)) {
|
|
|
|
/* XXX which other states are valid? */
|
2009-09-19 23:54:46 +02:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
PUSH_STATE(SEQUOTE);
|
|
|
|
statep->ls_sequote.got_NUL = false;
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
} 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;
|
|
|
|
/* 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
|
|
|
|
* $ ` \.").
|
|
|
|
*/
|
|
|
|
statep->ls_sbquote.indquotes = 0;
|
2005-07-04 14:47:13 +02:00
|
|
|
s2 = statep;
|
|
|
|
base = state_info.base;
|
2005-07-04 14:27:28 +02:00
|
|
|
while (1) {
|
2005-07-04 14:47:13 +02:00
|
|
|
for (; s2 != base; s2--) {
|
|
|
|
if (s2->ls_state == SDQUOTE) {
|
2005-07-04 14:27:28 +02:00
|
|
|
statep->ls_sbquote.indquotes = 1;
|
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;
|
2005-07-04 14:47:13 +02:00
|
|
|
if (!(s2 = s2->ls_info.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)
|
2009-09-19 23:54:46 +02:00
|
|
|
statep->ls_sequote.got_NUL = true;
|
|
|
|
if (!statep->ls_sequote.got_NUL) {
|
|
|
|
char ts[4];
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
if ((unsigned int)c2 < 0x100) {
|
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c2;
|
|
|
|
} else {
|
2009-09-19 23:54:46 +02:00
|
|
|
c = utf_wctomb(ts, c2 - 0x100);
|
|
|
|
ts[c] = 0;
|
2010-01-28 21:52:08 +01:00
|
|
|
for (c = 0; ts[c]; ++c) {
|
|
|
|
*wp++ = QCHAR;
|
2009-09-19 23:54:46 +02:00
|
|
|
*wp++ = ts[c];
|
2010-01-28 21:52:08 +01:00
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
}
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
} else if (!statep->ls_sequote.got_NUL) {
|
|
|
|
*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();
|
|
|
|
*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;
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
case SCSPAREN: /* $( ... ) */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* todo: deal with $(...) quoting properly
|
2009-10-04 14:45:23 +02:00
|
|
|
* kludge to partly fake quoting inside $(...): doesn't
|
|
|
|
* really work because nested $(...) or ${...} inside
|
2005-05-23 05:06:10 +02:00
|
|
|
* double quotes aren't dealt with.
|
|
|
|
*/
|
|
|
|
switch (statep->ls_scsparen.csstate) {
|
2010-01-29 10:34:31 +01:00
|
|
|
case 0: /* normal */
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (c) {
|
|
|
|
case '(':
|
|
|
|
statep->ls_scsparen.nparen++;
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
statep->ls_scsparen.nparen--;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
statep->ls_scsparen.csstate = 1;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
statep->ls_scsparen.csstate = 2;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
statep->ls_scsparen.csstate = 4;
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
case 1: /* backslash in normal mode */
|
|
|
|
case 3: /* backslash in double quotes */
|
2005-05-23 05:06:10 +02:00
|
|
|
--statep->ls_scsparen.csstate;
|
|
|
|
break;
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
case 2: /* double quotes */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '"')
|
|
|
|
statep->ls_scsparen.csstate = 0;
|
|
|
|
else if (c == '\\')
|
|
|
|
statep->ls_scsparen.csstate = 3;
|
|
|
|
break;
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
case 4: /* single quotes */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '\'') {
|
|
|
|
statep->ls_scsparen.csstate = 0;
|
|
|
|
ignore_backslash_newline--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (statep->ls_scsparen.nparen == 0) {
|
|
|
|
POP_STATE();
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = 0; /* end of COMSUB */
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
|
2010-01-29 10:34:31 +01:00
|
|
|
case SASPAREN: /* $(( ... )) */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* XXX should nest using existing state machine
|
2009-10-04 14:45:23 +02:00
|
|
|
* (embed "...", $(...), etc.) */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c == '(')
|
|
|
|
statep->ls_sasparen.nparen++;
|
|
|
|
else if (c == ')') {
|
|
|
|
statep->ls_sasparen.nparen--;
|
|
|
|
if (statep->ls_sasparen.nparen == 1) {
|
|
|
|
/*(*/
|
|
|
|
if ((c2 = getsc()) == ')') {
|
|
|
|
POP_STATE();
|
2010-01-29 10:34:31 +01:00
|
|
|
/* end of EXPRSUB */
|
|
|
|
*wp++ = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
ungetsc(c2);
|
|
|
|
/* mismatched parenthesis -
|
|
|
|
* assume we were really
|
2009-10-04 14:45:23 +02:00
|
|
|
* parsing a $(...) expression
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
s = Xrestpos(ws, wp,
|
|
|
|
statep->ls_sasparen.start);
|
|
|
|
memmove(s + 1, s, wp - s);
|
|
|
|
*s++ = COMSUB;
|
|
|
|
*s = '('; /*)*/
|
|
|
|
wp++;
|
|
|
|
statep->ls_scsparen.nparen = 1;
|
|
|
|
statep->ls_scsparen.csstate = 0;
|
|
|
|
state = statep->ls_state =
|
|
|
|
SCSPAREN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
|
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 == '\'')
|
|
|
|
goto open_ssquote;
|
|
|
|
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 == '$')
|
|
|
|
goto subst_dollar;
|
|
|
|
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;
|
|
|
|
|
|
|
|
case STBRACE:
|
|
|
|
/* Same as SBRACE, except (,|,) treated specially */
|
|
|
|
/*{*/
|
|
|
|
if (c == '}') {
|
|
|
|
POP_STATE();
|
|
|
|
*wp++ = CSUBST;
|
|
|
|
*wp++ = /*{*/ '}';
|
|
|
|
} else if (c == '|') {
|
|
|
|
*wp++ = SPAT;
|
|
|
|
} else if (c == '(') {
|
|
|
|
*wp++ = OPAT;
|
|
|
|
*wp++ = ' '; /* simile for @ */
|
|
|
|
PUSH_STATE(SPATTERN);
|
|
|
|
} else
|
|
|
|
goto Sbase1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBQUOTE:
|
|
|
|
if (c == '`') {
|
|
|
|
*wp++ = 0;
|
|
|
|
POP_STATE();
|
|
|
|
} else if (c == '\\') {
|
|
|
|
switch (c = getsc()) {
|
|
|
|
case '\\':
|
|
|
|
case '$': case '`':
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
if (statep->ls_sbquote.indquotes) {
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
default:
|
2010-01-29 10:34:31 +01:00
|
|
|
if (c) {
|
|
|
|
/* trailing \ is lost */
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = '\\';
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*wp++ = c;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SWORD: /* ONEWORD */
|
|
|
|
goto Subst;
|
|
|
|
|
|
|
|
case SLETPAREN: /* LETEXPR: (( ... )) */
|
|
|
|
/*(*/
|
|
|
|
if (c == ')') {
|
|
|
|
if (statep->ls_sletparen.nparen > 0)
|
2009-06-10 20:12:51 +02:00
|
|
|
--statep->ls_sletparen.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);
|
2009-10-04 14:45:23 +02:00
|
|
|
/* mismatched parenthesis -
|
|
|
|
* assume we were really
|
|
|
|
* parsing a $(...) expression
|
|
|
|
*/
|
|
|
|
*wp = EOS;
|
|
|
|
sp = Xstring(ws, wp);
|
|
|
|
dp = wdstrip(sp, true, false);
|
|
|
|
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 == '(')
|
|
|
|
/* parenthesis inside quotes and backslashes
|
2009-06-11 14:42:21 +02:00
|
|
|
* are lost, but AT&T ksh doesn't count them
|
2005-05-23 05:06:10 +02:00
|
|
|
* either
|
|
|
|
*/
|
|
|
|
++statep->ls_sletparen.nparen;
|
|
|
|
goto Sbase2;
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-06-23 01:34:42 +02:00
|
|
|
case SLETARRAY: /* LETARRAY: =( ... ) */
|
|
|
|
if (c == '('/*)*/)
|
|
|
|
++statep->ls_sletarray.nparen;
|
|
|
|
else if (c == /*(*/')')
|
|
|
|
if (statep->ls_sletarray.nparen-- == 0) {
|
|
|
|
c = 0;
|
|
|
|
goto Done;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
2007-06-23 01:34:42 +02:00
|
|
|
break;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2007-06-23 01:34:42 +02:00
|
|
|
|
2008-02-26 21:43:11 +01:00
|
|
|
case SHERESTRING: /* <<< delimiter */
|
|
|
|
if (c == '\\') {
|
|
|
|
c = getsc();
|
2010-01-29 10:34:31 +01:00
|
|
|
if (c) {
|
|
|
|
/* trailing \ is lost */
|
2008-02-26 21:43:11 +01:00
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2008-02-26 22:08:33 +01:00
|
|
|
/* invoke quoting mode */
|
2008-02-26 21:43:11 +01:00
|
|
|
Xstring(ws, wp)[0] = QCHAR;
|
2009-09-19 23:54:46 +02:00
|
|
|
} else if (c == '$') {
|
|
|
|
if ((c2 = getsc()) == '\'') {
|
|
|
|
PUSH_STATE(SEQUOTE);
|
|
|
|
statep->ls_sequote.got_NUL = false;
|
|
|
|
goto sherestring_quoted;
|
|
|
|
}
|
|
|
|
ungetsc(c2);
|
|
|
|
goto sherestring_regular;
|
2008-02-26 21:43:11 +01:00
|
|
|
} else if (c == '\'') {
|
|
|
|
PUSH_STATE(SSQUOTE);
|
2009-09-19 23:54:46 +02:00
|
|
|
sherestring_quoted:
|
2008-02-26 21:43:11 +01:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
ignore_backslash_newline++;
|
2008-02-26 22:08:33 +01:00
|
|
|
/* invoke quoting mode */
|
2008-02-26 21:43:11 +01:00
|
|
|
Xstring(ws, wp)[0] = QCHAR;
|
|
|
|
} else if (c == '"') {
|
|
|
|
state = statep->ls_state = SHEREDQUOTE;
|
|
|
|
*wp++ = OQUOTE;
|
2008-02-26 22:08:33 +01:00
|
|
|
/* just don't IFS split; no quoting mode */
|
2008-02-26 21:43:11 +01:00
|
|
|
} else {
|
2009-09-19 23:54:46 +02:00
|
|
|
sherestring_regular:
|
2008-02-26 21:43:11 +01:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
case SHEREDELIM: /* <<,<<- delimiter */
|
|
|
|
/* XXX chuck this state (and the next) - use
|
2009-10-04 14:45:23 +02:00
|
|
|
* the existing states ($ and \`...` should be
|
2005-05-23 05:06:10 +02:00
|
|
|
* stripped of their specialness after the
|
|
|
|
* fact).
|
|
|
|
*/
|
|
|
|
/* 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
|
|
|
*/
|
|
|
|
if (c == '\\') {
|
|
|
|
c = getsc();
|
2010-01-29 10:34:31 +01:00
|
|
|
if (c) {
|
|
|
|
/* trailing \ is lost */
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = QCHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
} else if (c == '$') {
|
|
|
|
if ((c2 = getsc()) == '\'') {
|
|
|
|
PUSH_STATE(SEQUOTE);
|
|
|
|
statep->ls_sequote.got_NUL = false;
|
|
|
|
goto sheredelim_quoted;
|
|
|
|
}
|
|
|
|
ungetsc(c2);
|
|
|
|
goto sheredelim_regular;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == '\'') {
|
|
|
|
PUSH_STATE(SSQUOTE);
|
2009-09-19 23:54:46 +02:00
|
|
|
sheredelim_quoted:
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = OQUOTE;
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
} else if (c == '"') {
|
|
|
|
state = statep->ls_state = SHEREDQUOTE;
|
|
|
|
*wp++ = OQUOTE;
|
|
|
|
} else {
|
2009-09-19 23:54:46 +02:00
|
|
|
sheredelim_regular:
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHEREDQUOTE: /* " in <<,<<- delimiter */
|
|
|
|
if (c == '"') {
|
|
|
|
*wp++ = CQUOTE;
|
2008-02-26 21:43:11 +01:00
|
|
|
state = statep->ls_state =
|
2008-02-26 22:08:33 +01:00
|
|
|
/* dp[1] == '<' means here string */
|
2008-02-26 21:43:11 +01:00
|
|
|
Xstring(ws, wp)[1] == '<' ?
|
|
|
|
SHERESTRING : SHEREDELIM;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (c == '\\') {
|
|
|
|
switch (c = getsc()) {
|
|
|
|
case '\\': case '"':
|
|
|
|
case '$': case '`':
|
|
|
|
break;
|
|
|
|
default:
|
2010-01-29 10:34:31 +01:00
|
|
|
if (c) {
|
|
|
|
/* trailing \ lost */
|
2005-05-23 05:06:10 +02:00
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = '\\';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*wp++ = CHAR;
|
|
|
|
*wp++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPATTERN: /* in *(...|...) pattern (*+?@!) */
|
|
|
|
if ( /*(*/ c == ')') {
|
|
|
|
*wp++ = CPAT;
|
|
|
|
POP_STATE();
|
|
|
|
} else if (c == '|') {
|
|
|
|
*wp++ = SPAT;
|
|
|
|
} else if (c == '(') {
|
|
|
|
*wp++ = OPAT;
|
|
|
|
*wp++ = ' '; /* simile for @ */
|
|
|
|
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");
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-06-23 01:34:42 +02:00
|
|
|
if (state == SLETARRAY && statep->ls_sletarray.nparen != -1)
|
2007-08-20 01:12:23 +02:00
|
|
|
yyerror("%s: ')' missing\n", T_synerr);
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2007-06-23 01:34:42 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* This done to avoid tests for SHEREDELIM wherever SBASE tested */
|
2008-02-26 21:43:11 +01:00
|
|
|
if (state == SHEREDELIM || state == SHERESTRING)
|
2005-05-23 05:06:10 +02:00
|
|
|
state = SBASE;
|
|
|
|
|
|
|
|
dp = Xstring(ws, wp);
|
2008-07-09 23:32:45 +02:00
|
|
|
if ((c == '<' || c == '>' || c == '&') && state == SBASE) {
|
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) {
|
|
|
|
if ((c2 = getsc()) == '-')
|
|
|
|
iop->flag |= IOSKIP;
|
|
|
|
else
|
|
|
|
ungetsc(c2);
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
Xfree(ws, wp); /* free word */
|
|
|
|
yylval.iop = iop;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (REDIR);
|
2008-06-29 00:51:56 +02:00
|
|
|
no_iop:
|
|
|
|
;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wp == dp && state == SBASE) {
|
|
|
|
Xfree(ws, wp); /* free word */
|
|
|
|
/* 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;
|
|
|
|
else
|
|
|
|
ungetsc(c2);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
*wp++ = EOS; /* terminate word */
|
|
|
|
yylval.cp = Xclose(ws, wp);
|
2009-09-23 20:04:58 +02:00
|
|
|
if (state == SWORD || state == SLETPAREN
|
|
|
|
/* XXX ONEWORD? */
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|| state == SLETARRAY
|
|
|
|
#endif
|
|
|
|
)
|
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;
|
|
|
|
if ((cf & HEREDELIM) && (sp[1] == '<'))
|
2008-03-05 17:49:22 +01:00
|
|
|
while (dp < ident+IDENT) {
|
2008-02-26 21:43:11 +01:00
|
|
|
if ((c = *sp++) == CHAR)
|
|
|
|
*dp++ = *sp++;
|
2008-03-05 17:40:57 +01:00
|
|
|
else if ((c != OQUOTE) && (c != CQUOTE))
|
2008-02-26 21:43:11 +01:00
|
|
|
break;
|
2008-03-05 17:49:22 +01:00
|
|
|
}
|
2008-02-26 21:43:11 +01:00
|
|
|
else
|
|
|
|
while (dp < ident+IDENT && (c = *sp++) == CHAR)
|
|
|
|
*dp++ = *sp++;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Make sure the ident array stays '\0' padded */
|
|
|
|
memset(dp, 0, (ident+IDENT) - dp + 1);
|
|
|
|
if (c != EOS)
|
|
|
|
*ident = '\0'; /* word is not unquoted */
|
|
|
|
|
|
|
|
if (*ident != '\0' && (cf&(KEYWORD|ALIAS))) {
|
|
|
|
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)) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(!(cf & ESACONLY) || p->val.i == ESAC || p->val.i == '}')) {
|
|
|
|
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 */
|
|
|
|
if (*cp == '(' /*)*/)
|
|
|
|
/*
|
|
|
|
* delete alias upon encountering function
|
|
|
|
* definition
|
|
|
|
*/
|
2009-08-28 22:30:59 +02:00
|
|
|
ktdelete(p);
|
2008-02-24 23:12:36 +01:00
|
|
|
else {
|
2008-12-02 13:39:38 +01:00
|
|
|
Source *s = source;
|
2008-02-24 23:12:36 +01:00
|
|
|
|
2008-12-02 13:39:38 +01:00
|
|
|
while (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++)
|
2008-02-26 21:43:11 +01:00
|
|
|
if (iseof && (*p)->delim[1] != '<')
|
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;
|
|
|
|
char *volatile eof;
|
|
|
|
char *eofp;
|
|
|
|
int skiptabs;
|
|
|
|
XString xs;
|
|
|
|
char *xp;
|
|
|
|
int xpos;
|
|
|
|
|
2008-02-26 21:43:11 +01:00
|
|
|
if (iop->delim[1] == '<') {
|
2008-02-26 22:08:33 +01:00
|
|
|
/* process the here string */
|
2008-02-26 21:43:11 +01:00
|
|
|
xp = iop->heredoc = evalstr(iop->delim, DOBLANK);
|
|
|
|
c = strlen(xp) - 1;
|
|
|
|
memmove(xp, xp + 1, c);
|
|
|
|
xp[c] = '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
eof = evalstr(iop->delim, 0);
|
|
|
|
|
|
|
|
if (!(iop->flag & IOEVAL))
|
|
|
|
ignore_backslash_newline++;
|
|
|
|
|
|
|
|
Xinit(xs, xp, 256, ATEMP);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
eofp = eof;
|
|
|
|
skiptabs = iop->flag & IOSKIP;
|
|
|
|
xpos = Xsavepos(xs, xp);
|
|
|
|
while ((c = getsc()) != 0) {
|
|
|
|
if (skiptabs) {
|
|
|
|
if (c == '\t')
|
|
|
|
continue;
|
|
|
|
skiptabs = 0;
|
|
|
|
}
|
|
|
|
if (c != *eofp)
|
|
|
|
break;
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
Xput(xs, xp, c);
|
|
|
|
eofp++;
|
|
|
|
}
|
|
|
|
/* Allow EOF here so commands with out trailing newlines
|
|
|
|
* will work (eg, ksh -c '...', $(...), etc).
|
|
|
|
*/
|
|
|
|
if (*eofp == '\0' && (c == 0 || c == '\n')) {
|
|
|
|
xp = Xrestpos(xs, xp, xpos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ungetsc(c);
|
|
|
|
while ((c = getsc()) != '\n') {
|
|
|
|
if (c == 0)
|
|
|
|
yyerror("here document '%s' unclosed\n", eof);
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
Xput(xs, xp, c);
|
|
|
|
}
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
Xput(xs, xp, c);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
source->str = null; /* zap pending input */
|
|
|
|
|
|
|
|
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);
|
2005-05-23 05:06:10 +02:00
|
|
|
s->type = type;
|
|
|
|
s->str = null;
|
|
|
|
s->start = NULL;
|
|
|
|
s->line = 0;
|
|
|
|
s->errline = 0;
|
|
|
|
s->file = NULL;
|
|
|
|
s->flags = 0;
|
|
|
|
s->next = NULL;
|
|
|
|
s->areap = areap;
|
2008-03-28 14:46:53 +01:00
|
|
|
if (type == SFILE || type == SSTDIN)
|
|
|
|
XinitN(s->xs, 256, s->areap);
|
|
|
|
else
|
2005-05-23 05:06:10 +02:00
|
|
|
memset(&s->xs, 0, sizeof(s->xs));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (s);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getsc__(void)
|
|
|
|
{
|
|
|
|
Source *s = source;
|
|
|
|
int c;
|
|
|
|
|
2007-04-15 12:45:59 +02:00
|
|
|
getsc_again:
|
2005-05-23 05:06:10 +02:00
|
|
|
while ((c = *s->str++) == 0) {
|
|
|
|
s->str = NULL; /* return 0 for EOF by default */
|
|
|
|
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:
|
|
|
|
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))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
source = s = s->next; /* pop source stack */
|
|
|
|
/* Note that this alias ended with a space,
|
|
|
|
* enabling alias expansion on the following
|
|
|
|
* word.
|
|
|
|
*/
|
|
|
|
s->flags |= SF_ALIAS;
|
|
|
|
} else {
|
|
|
|
/* At this point, we need to keep the current
|
|
|
|
* alias in the source list so recursive
|
|
|
|
* aliases can be detected and we also need
|
2009-06-10 20:12:51 +02:00
|
|
|
* to return the next character. Do this
|
2005-05-23 05:06:10 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
source = s->next; /* pop source stack */
|
|
|
|
source->flags |= s->flags & SF_ALIAS;
|
|
|
|
c = getsc__();
|
|
|
|
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;
|
|
|
|
/* avoid reading eof twice */
|
|
|
|
s->str = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SREREAD:
|
2010-01-29 10:34:31 +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);
|
|
|
|
}
|
|
|
|
}
|
2007-04-15 12:45:59 +02:00
|
|
|
/* check for UTF-8 byte order mark */
|
|
|
|
if (s->flags & SF_FIRST) {
|
|
|
|
s->flags &= ~SF_FIRST;
|
|
|
|
if (((unsigned char)c == 0xEF) &&
|
|
|
|
(((const unsigned char *)(s->str))[0] == 0xBB) &&
|
|
|
|
(((const unsigned char *)(s->str))[1] == 0xBF)) {
|
|
|
|
s->str += 2;
|
2008-12-04 19:11:08 +01:00
|
|
|
UTFMODE = 1;
|
2007-04-15 12:45:59 +02:00
|
|
|
goto getsc_again;
|
|
|
|
}
|
|
|
|
}
|
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;
|
2005-05-23 05:06:10 +02:00
|
|
|
int have_tty = interactive && (s->flags & SF_TTY);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
nread = x_read(xp, LINE);
|
|
|
|
if (nread < 0) /* read error */
|
|
|
|
nread = 0;
|
|
|
|
xp[nread] = '\0';
|
|
|
|
xp += nread;
|
|
|
|
} else {
|
|
|
|
if (interactive) {
|
|
|
|
pprompt(prompt, 0);
|
|
|
|
} else
|
|
|
|
s->line++;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
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 */
|
2010-01-29 10:34:31 +01:00
|
|
|
xp++; /* move past NUL so doubling works... */
|
2005-05-23 05:06:10 +02:00
|
|
|
XcheckN(s->xs, xp, Xlength(s->xs, xp));
|
2010-01-29 10:34:31 +01:00
|
|
|
xp--; /* ...and move back again */
|
2005-05-23 05:06:10 +02: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.
|
2005-05-23 05:06:10 +02:00
|
|
|
* todo: reduce size of shf buffer (~128?) if SSTDIN
|
|
|
|
*/
|
|
|
|
if (s->type == SSTDIN)
|
|
|
|
shf_flush(s->u.shf);
|
|
|
|
}
|
|
|
|
/* XXX: temporary kludge to restore source after a
|
|
|
|
* 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);
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-05-27 21:52:38 +02:00
|
|
|
if (interactive && *cp == '!' && cur_prompt == PS1) {
|
|
|
|
int linelen;
|
|
|
|
|
|
|
|
linelen = Xlength(s->xs, xp);
|
|
|
|
XcheckN(s->xs, xp, fc_e_n + /* NUL */ 1);
|
|
|
|
/* reload after potential realloc */
|
|
|
|
cp = Xstring(s->xs, xp);
|
|
|
|
/* change initial '!' into space */
|
|
|
|
*cp = ' ';
|
|
|
|
/* NUL terminate the current string */
|
|
|
|
*xp = '\0';
|
|
|
|
/* move the actual string forward */
|
|
|
|
memmove(cp + fc_e_n, cp, linelen + /* NUL */ 1);
|
|
|
|
xp += fc_e_n;
|
|
|
|
/* prepend it with "fc -e -" */
|
|
|
|
memcpy(cp, fc_e_, fc_e_n);
|
|
|
|
}
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
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) {
|
2010-01-29 10:34:31 +01:00
|
|
|
case PS1: /* command */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Substitute ! and !! here, before substitutions are done
|
|
|
|
* 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);
|
|
|
|
if (sigsetjmp(e->jbuf, 0)) {
|
|
|
|
prompt = safe_prompt;
|
|
|
|
/* 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;
|
2010-01-29 10:34:31 +01:00
|
|
|
case PS2: /* command continuation */
|
2005-05-23 05:06:10 +02:00
|
|
|
prompt = str_val(global("PS2"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-01 16:35:44 +02:00
|
|
|
static int
|
2009-03-15 19:30:40 +01:00
|
|
|
dopprompt(const char *cp, int ntruncate, bool doprint)
|
2006-08-01 16:10:25 +02:00
|
|
|
{
|
2006-08-02 13:33:37 +02:00
|
|
|
int columns = 0, lines = 0, indelimit = 0;
|
2006-08-01 16:10:25 +02:00
|
|
|
char delimiter = 0;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
if (doprint && (indelimit ||
|
|
|
|
(ntruncate < (x_cols * lines + columns))))
|
|
|
|
shf_write(cp, cp2 - cp, shl_out);
|
|
|
|
cp = cp2 - /* loop increment */ 1;
|
|
|
|
continue;
|
|
|
|
} else
|
2006-08-02 12:42:30 +02:00
|
|
|
columns++;
|
2006-08-02 13:33:37 +02:00
|
|
|
if (doprint && (*cp != delimiter) &&
|
|
|
|
(indelimit || (ntruncate < (x_cols * lines + columns))))
|
|
|
|
shf_putc(*cp, shl_out);
|
2006-08-01 16:10:25 +02:00
|
|
|
}
|
2006-08-01 16:35:44 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pprompt(const char *cp, int ntruncate)
|
|
|
|
{
|
2009-03-15 19:30:40 +01:00
|
|
|
dopprompt(cp, ntruncate, true);
|
2006-08-01 16:35:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-08-02 13:33:37 +02:00
|
|
|
promptlen(const char *cp)
|
2006-08-01 16:35:44 +02:00
|
|
|
{
|
2009-03-15 19:30:40 +01:00
|
|
|
return (dopprompt(cp, 0, false));
|
2006-08-01 16:10:25 +02:00
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Read the variable part of a ${...} expression (ie, up to but not including
|
|
|
|
* the :[-+?=#%] or close-brace.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_brace_var(XString *wsp, char *wp)
|
|
|
|
{
|
|
|
|
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
|
2009-06-10 20:12:51 +02:00
|
|
|
} state;
|
2005-05-23 05:06:10 +02:00
|
|
|
char c;
|
|
|
|
|
|
|
|
state = PS_INITIAL;
|
|
|
|
while (1) {
|
|
|
|
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;
|
|
|
|
else if (ctype(c, C_VAR1))
|
|
|
|
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);
|
2010-01-29 10:34:31 +01:00
|
|
|
c = getsc(); /* the ] */
|
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:
|
|
|
|
*wp++ = '\0'; /* end of variable part */
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
arraysub(char **strp)
|
|
|
|
{
|
|
|
|
XString ws;
|
|
|
|
char *wp;
|
|
|
|
char c;
|
|
|
|
int depth = 1; /* we are just past the initial [ */
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (depth == 0 ? 1 : 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unget a char: handles case when we are already at the start of the buffer */
|
|
|
|
static const char *
|
|
|
|
ungetsc(int c)
|
|
|
|
{
|
|
|
|
if (backslash_skip)
|
|
|
|
backslash_skip--;
|
|
|
|
/* Don't unget eof... */
|
|
|
|
if (source->str == null && c == '\0')
|
2009-06-08 22:06:50 +02:00
|
|
|
return (source->str);
|
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;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (source->str);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Called to get a char that isn't a \newline sequence. */
|
|
|
|
static int
|
|
|
|
getsc_bn(void)
|
|
|
|
{
|
|
|
|
int c, c2;
|
|
|
|
|
|
|
|
if (ignore_backslash_newline)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (getsc_());
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (backslash_skip == 1) {
|
|
|
|
backslash_skip = 2;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (getsc_());
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
backslash_skip = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
c = getsc_();
|
|
|
|
if (c == '\\') {
|
|
|
|
if ((c2 = getsc_()) == '\n')
|
|
|
|
/* ignore the \newline; get the next char... */
|
|
|
|
continue;
|
|
|
|
ungetsc(c2);
|
|
|
|
backslash_skip = 1;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Lex_state *
|
|
|
|
push_state_(State_info *si, Lex_state *old_end)
|
|
|
|
{
|
2009-06-08 22:06:50 +02:00
|
|
|
Lex_state *new = alloc(STATE_BSIZE * sizeof(Lex_state), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
new[0].ls_info.base = old_end;
|
|
|
|
si->base = &new[0];
|
|
|
|
si->end = &new[STATE_BSIZE];
|
2009-06-08 22:06:50 +02:00
|
|
|
return (&new[1]);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Lex_state *
|
|
|
|
pop_state_(State_info *si, Lex_state *old_end)
|
|
|
|
{
|
|
|
|
Lex_state *old_base = si->base;
|
|
|
|
|
|
|
|
si->base = old_end->ls_info.base - STATE_BSIZE;
|
|
|
|
si->end = old_end->ls_info.base;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|