2 * lex.c - lexical analysis
4 * This file is part of zsh, the Z shell.
6 * Copyright (c) 1992-1997 Paul Falstad
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and to distribute modified versions of this software for any
12 * purpose, provided that the above copyright notice and the following
13 * two paragraphs appear in all copies of this software.
15 * In no event shall Paul Falstad or the Zsh Development Group be liable
16 * to any party for direct, indirect, special, incidental, or consequential
17 * damages arising out of the use of this software and its documentation,
18 * even if Paul Falstad and the Zsh Development Group have been advised of
19 * the possibility of such damage.
21 * Paul Falstad and the Zsh Development Group specifically disclaim any
22 * warranties, including, but not limited to, the implied warranties of
23 * merchantability and fitness for a particular purpose. The software
24 * provided hereunder is on an "as is" basis, and Paul Falstad and the
25 * Zsh Development Group have no obligation to provide maintenance,
26 * support, updates, enhancements, or modifications.
36 mod_export
char ztokens
[] = "#$^*()$=|{}[]`<>>?~`,'\"\\\\";
38 /* parts of the current token */
43 mod_export
char *tokstr
;
50 * Line number at which the first character of a token was found.
51 * We always set this in gettok(), which is always called from
52 * zshlex() unless we have reached an error. So it is always
53 * valid when parsing. It is not useful during execution
54 * of the parsed structure.
60 /* lexical analyzer error flag */
63 mod_export
int lexstop
;
65 /* if != 0, this is the first line of the command */
68 mod_export
int isfirstln
;
70 /* if != 0, this is the first char of the command (not including white space) */
75 /* flag that an alias should be expanded after expansion ending in space */
81 * Don't do spelling correction.
82 * Bit 1 is only valid for the current word. It's
83 * set when we detect a lookahead that stops the word from
91 * Cursor position and line length in zle when the line is
92 * metafied for access from the main shell.
96 mod_export
int zlemetacs
, zlemetall
;
98 /* inwhat says what exactly we are in *
99 * (its value is one of the IN_* things). */
102 mod_export
int inwhat
;
104 /* 1 if x added to complete in a blank between words */
107 mod_export
int addedx
;
109 /* wb and we hold the beginning/end position of the word we are completing. */
112 mod_export
int wb
, we
;
114 /* 1 if aliases should not be expanded */
117 mod_export
int noaliases
;
119 /* we are parsing a line sent to use by the editor */
122 mod_export
int zleparse
;
125 mod_export
int wordbeg
;
128 mod_export
int parbegin
;
131 mod_export
int parend
;
133 /* don't recognize comments */
136 mod_export
int nocomments
;
138 /* text of punctuation tokens */
141 mod_export
char *tokstrings
[WHILE
+ 1] = {
142 NULL
, /* NULLTOK 0 */
153 ">|", /* OUTANGBANG */
155 ">>|", /* DOUTANGBANG */
157 "<>", /* INOUTANG 15 */
159 "<<-", /* DINANGDASH */
161 ">&", /* OUTANGAMP */
162 "&>", /* AMPOUTANG 20 */
163 "&>|", /* OUTANGAMPBANG */
164 ">>&", /* DOUTANGAMP */
165 ">>&|", /* DOUTANGAMPBANG */
172 "&|", /* AMPERBANG 30 */
180 static int len
= 0, bsiz
= 256;
184 struct lexstack
*next
;
210 struct heredocs
*hdocs
;
211 int (*hgetc
) _((void));
212 void (*hungetc
) _((int));
213 void (*hwaddc
) _((int));
214 void (*hwbegin
) _((int));
215 void (*hwend
) _((void));
216 void (*addtoline
) _((int));
218 int eclen
, ecused
, ecnpats
;
221 int ecsoffs
, ecssub
, ecnfunc
;
223 unsigned char *cstack
;
228 static struct lexstack
*lstack
= NULL
;
230 /* save the lexical state */
232 /* is this a hack or what? */
240 ls
= (struct lexstack
*)malloc(sizeof(struct lexstack
));
242 ls
->incmdpos
= incmdpos
;
244 ls
->incasepat
= incasepat
;
245 ls
->dbparens
= dbparens
;
246 ls
->isfirstln
= isfirstln
;
247 ls
->isfirstch
= isfirstch
;
248 ls
->histactive
= histactive
;
249 ls
->histdone
= histdone
;
250 ls
->stophist
= stophist
;
253 ls
->hlinesz
= hlinesz
;
254 ls
->cstack
= cmdstack
;
256 cmdstack
= (unsigned char *)zalloc(CMDSTACKSZ
);
258 ls
->isnewlin
= isnewlin
;
260 ls
->zshlextext
= zshlextext
;
264 ls
->chwords
= chwords
;
265 ls
->chwordlen
= chwordlen
;
266 ls
->chwordpos
= chwordpos
;
267 ls
->hwgetword
= hwgetword
;
268 ls
->lexstop
= lexstop
;
271 ls
->hungetc
= hungetc
;
273 ls
->hwbegin
= hwbegin
;
275 ls
->addtoline
= addtoline
;
278 ls
->ecnpats
= ecnpats
;
281 ls
->ecsoffs
= ecsoffs
;
283 ls
->ecnfunc
= ecnfunc
;
284 ls
->toklineno
= toklineno
;
295 /* restore lexical state */
303 DPUTS(!lstack
, "BUG: lexrestore() without lexsave()");
304 incmdpos
= lstack
->incmdpos
;
305 incond
= lstack
->incond
;
306 incasepat
= lstack
->incasepat
;
307 dbparens
= lstack
->dbparens
;
308 isfirstln
= lstack
->isfirstln
;
309 isfirstch
= lstack
->isfirstch
;
310 histactive
= lstack
->histactive
;
311 histdone
= lstack
->histdone
;
312 stophist
= lstack
->stophist
;
313 chline
= lstack
->hline
;
317 cmdstack
= lstack
->cstack
;
320 isnewlin
= lstack
->isnewlin
;
321 tokstr
= lstack
->tokstr
;
322 zshlextext
= lstack
->zshlextext
;
326 chwords
= lstack
->chwords
;
327 chwordlen
= lstack
->chwordlen
;
328 chwordpos
= lstack
->chwordpos
;
329 hwgetword
= lstack
->hwgetword
;
330 lexstop
= lstack
->lexstop
;
331 hdocs
= lstack
->hdocs
;
332 hgetc
= lstack
->hgetc
;
333 hungetc
= lstack
->hungetc
;
334 hwaddc
= lstack
->hwaddc
;
335 hwbegin
= lstack
->hwbegin
;
336 hwend
= lstack
->hwend
;
337 addtoline
= lstack
->addtoline
;
340 eclen
= lstack
->eclen
;
341 ecused
= lstack
->ecused
;
342 ecnpats
= lstack
->ecnpats
;
343 ecbuf
= lstack
->ecbuf
;
344 ecstrs
= lstack
->ecstrs
;
345 ecsoffs
= lstack
->ecsoffs
;
346 ecssub
= lstack
->ecssub
;
347 ecnfunc
= lstack
->ecnfunc
;
348 hlinesz
= lstack
->hlinesz
;
349 toklineno
= lstack
->toklineno
;
365 while (tok
!= ENDINPUT
&& exalias());
367 if (tok
== NEWLIN
|| tok
== ENDINPUT
) {
369 struct heredocs
*next
= hdocs
->next
;
373 cmdpush(hdocs
->type
== REDIR_HEREDOC
? CS_HEREDOC
: CS_HEREDOCD
);
375 name
= gethere(hdocs
->str
, hdocs
->type
);
380 zerr("here document too large");
383 zfree(hdocs
, sizeof(struct heredocs
));
389 setheredoc(hdocs
->pc
, REDIR_HERESTR
, name
);
390 zfree(hdocs
, sizeof(struct heredocs
));
397 isnewlin
= (inbufct
) ? -1 : 1;
398 if (tok
== SEMI
|| tok
== NEWLIN
)
433 /* case ENVSTRING: */
442 infor
= tok
== FOR
? 2 : 0;
443 if (IS_REDIROP(tok
) || tok
== FOR
|| tok
== FOREACH
|| tok
== SELECT
) {
447 } else if (inredir
) {
453 #define LX1_BKSLASH 0
454 #define LX1_COMMENT 1
462 #define LX1_OUTANG 14
469 #define LX2_INBRACK 4
470 #define LX2_OUTBRACK 5
473 #define LX2_INBRACE 8
474 #define LX2_OUTBRACE 9
475 #define LX2_OUTANG 10
477 #define LX2_EQUALS 12
478 #define LX2_BKSLASH 13
480 #define LX2_DQUOTE 15
481 #define LX2_BQUOTE 16
486 static unsigned char lexact1
[256], lexact2
[256], lextok2
[256];
493 static char *lx1
= "\\q\n;!&|(){}[]<>";
494 static char *lx2
= ";)|$[]~({}><=\\\'\"`,";
496 for (t0
= 0; t0
!= 256; t0
++) {
497 lexact1
[t0
] = LX1_OTHER
;
498 lexact2
[t0
] = LX2_OTHER
;
501 for (t0
= 0; lx1
[t0
]; t0
++)
502 lexact1
[(int)lx1
[t0
]] = t0
;
503 for (t0
= 0; lx2
[t0
]; t0
++)
504 lexact2
[(int)lx2
[t0
]] = t0
;
505 lexact2
['&'] = LX2_BREAK
;
506 lexact2
[STOUC(Meta
)] = LX2_META
;
508 lextok2
['?'] = Quest
;
509 lextok2
['{'] = Inbrace
;
510 lextok2
['['] = Inbrack
;
511 lextok2
['$'] = String
;
512 lextok2
['~'] = Tilde
;
513 lextok2
['#'] = Pound
;
517 /* initialize lexical state */
523 incond
= incasepat
= nocorrect
=
524 infor
= dbparens
= lexstop
= 0;
529 /* add a char to the string buffer */
541 while (newbsiz
< inbufct
)
543 bptr
= len
+ (tokstr
= (char *)hrealloc(tokstr
, bsiz
, newbsiz
));
547 int newbsiz
= bsiz
* 2;
549 if (newbsiz
> inbufct
&& inbufct
> bsiz
)
552 bptr
= len
+ (tokstr
= (char *)hrealloc(tokstr
, bsiz
, newbsiz
));
557 #define SETPARBEGIN {if (zleparse && !(inbufflags & INP_ALIAS) && zlemetacs >= zlemetall+1-inbufct) parbegin = inbufct;}
559 if (zleparse && !(inbufflags & INP_ALIAS) && parbegin != -1 && parend == -1) {\
560 if (zlemetacs >= zlemetall + 1 - inbufct)\
566 * Return 1 for math, 0 for a command, 2 for an error. If it couldn't be
567 * parsed as math, but there was no gross error, it's a command.
571 cmd_or_math(int cs_type
)
577 c
= dquote_parse(')', 0);
581 /* Successfully parsed, see if it was math */
588 } else if (lexstop
) {
589 /* we haven't got anything to unget */
592 /* else unsuccessful: unget the whole thing */
595 while (len
> oldlen
) {
597 hungetc(itok(*--bptr
) ? ztokens
[*bptr
- Pound
] : *bptr
);
605 * Parse either a $(( ... )) or a $(...)
606 * Return 0 on success, 1 on failure.
609 cmd_or_math_sub(void)
611 int c
= hgetc(), ret
;
616 if ((ret
= cmd_or_math(CS_MATHSUBST
)) == 1) {
631 /* Check whether we're looking at valid numeric globbing syntax *
632 * (/\<[0-9]*-[0-9]*\>/). Call pointing just after the opening "<". *
633 * Leaves the input in the same place, returning 0 or 1. */
639 int c
, ec
= '-', ret
= 0;
640 int tbs
= 256, n
= 0;
641 char *tbuf
= (char *)zalloc(tbs
);
660 tbuf
= (char *)realloc(tbuf
, tbs
*= 2);
673 int peekfd
= -1, peek
;
677 while (iblank(c
= hgetc()) && !lexstop
);
680 return (errflag
) ? LEXERR
: ENDINPUT
;
682 wordbeg
= inbufct
- (qbang
&& c
== bangchar
);
683 hwbegin(-1-(qbang
&& c
== bangchar
));
684 /* word includes the last character read and possibly \ before ! */
687 bptr
= tokstr
= (char *) hcalloc(bsiz
= 32);
690 c
= dquote_parse(infor
? ';' : ')', 0);
697 if (c
|| (c
= hgetc()) != ')') {
703 } else if (idigit(c
)) { /* handle 1< foo */
716 } else if (d
== '>' || d
== '<') {
725 /* chars in initial position in word */
727 if (c
== hashchar
&& !nocomments
&&
728 (isset(INTERACTIVECOMMENTS
) ||
729 (!zleparse
&& !expanding
&&
730 (!interact
|| unset(SHINSTDIN
) || strin
)))) {
731 /* History is handled here to prevent extra *
732 * newlines being inserted into the history. */
734 while ((c
= ingetc()) != '\n' && !lexstop
) {
750 switch (lexact1
[STOUC(c
)]) {
775 else if (d
== '!' || d
== '|')
780 if (d
== '!' || d
== '|')
781 return OUTANGAMPBANG
;
784 if (d
== '!' || d
== '|')
785 return DOUTANGAMPBANG
;
815 bptr
= tokstr
= (char *) hcalloc(bsiz
= 32);
816 switch (cmd_or_math(CS_MATH
)) {
831 if (!(incond
== 1 || incmdpos
))
850 } else if (d
== '<') {
866 } else if (d
== '&') {
881 } else if (d
== '&') {
883 if (d
== '!' || d
== '|')
884 peek
= OUTANGAMPBANG
;
890 } else if (d
== '!' || d
== '|')
896 if (d
== '!' || d
== '|')
897 peek
= DOUTANGAMPBANG
;
903 } else if (d
== '!' || d
== '|')
913 if (isset(HISTALLOWCLOBBER
))
920 if (!incond
&& isset(HISTALLOWCLOBBER
))
927 /* we've started a string, now get the *
928 * rest of it, performing tokenization */
929 return gettokstr(c
, 0);
933 * Get the remains of a token string. This has two uses.
934 * When called from gettok(), with sub = 0, we have already identified
935 * any interesting initial character and want to get the rest of
936 * what we now know is a string. However, the string may still include
937 * metacharacters and potentially substitutions.
939 * When called from parse_subst_string() with sub = 1, we are not
940 * fully parsing a command line, merely tokenizing a string.
941 * In this case we always add characters to the parsed string
942 * unless there is a parse error.
947 gettokstr(int c
, int sub
)
949 int bct
= 0, pct
= 0, brct
= 0, fdpar
= 0;
950 int intpos
= 1, in_brace_param
= 0;
951 int peek
, inquote
, unmatched
= 0;
959 bptr
= tokstr
= (char *) hcalloc(bsiz
= 32);
964 int inbl
= inblank(c
);
966 if (fdpar
&& !inbl
&& c
!= ')')
969 if (inbl
&& !in_brace_param
&& !pct
)
972 act
= lexact2
[STOUC(c
)];
973 c
= lextok2
[STOUC(c
)];
977 if (!in_brace_param
&& !sub
)
984 fputs("BUG: input terminated by Meta\n", stderr
);
993 /* this is a single word `( )', treat as INOUTPAR */
998 if ((sub
|| in_brace_param
) && isset(SHGLOB
))
1000 if (!in_brace_param
&& !pct
--) {
1010 if (!pct
&& !in_brace_param
) {
1016 if (unset(SHGLOB
) || (!sub
&& !in_brace_param
))
1022 cmdpush(CS_MATHSUBST
);
1025 c
= dquote_parse(']', sub
);
1032 } else if (e
== '(') {
1034 c
= cmd_or_math_sub();
1045 cmdpush(CS_BRACEPAR
);
1046 if (!in_brace_param
)
1047 in_brace_param
= bct
;
1055 if (!in_brace_param
)
1060 if (!in_brace_param
)
1067 if (isset(SHGLOB
)) {
1068 if (sub
|| in_brace_param
)
1070 if (incasepat
&& !len
)
1073 if (!in_brace_param
) {
1078 /* For command words, parentheses are only
1079 * special at the start. But now we're tokenising
1080 * the remaining string. So I don't see what
1081 * the old incmdpos test here is for.
1086 * is a valid function definition in [k]sh. The best
1087 * thing we can do, without really nasty lookahead tricks,
1088 * is break if we find a blank after a parenthesis. At
1089 * least this can't happen inside braces or brackets. We
1090 * only allow this with SHGLOB (set for both sh and ksh).
1092 * Things like `print @( |foo)' should still
1093 * work, because [k]sh don't allow multiple words
1094 * in a function definition, so we only do this
1095 * in command position.
1098 if (e
== ')' || (isset(SHGLOB
) && inblank(e
) && !bct
&&
1099 !brct
&& !intpos
&& incmdpos
)) {
1101 * Either a () token, or a command word with
1102 * something suspiciously like a ksh function
1104 * The current word isn't spellcheckable.
1111 * This also handles the [k]sh `foo( )' function definition.
1112 * Maintain a variable fdpar, set as long as a single set of
1113 * parentheses contains only space. Then if we get to the
1114 * closing parenthesis and it is still set, we can assume we
1115 * have a function definition. Only do this at the start of
1116 * the word, since the (...) must be a separate token.
1118 if (!pct
++ && isset(SHGLOB
) && intpos
&& !bct
&& !brct
)
1124 if (isset(IGNOREBRACES
) || sub
)
1127 if (!len
&& incmdpos
) {
1132 if (in_brace_param
) {
1139 if ((isset(IGNOREBRACES
) || sub
) && !in_brace_param
)
1143 if (in_brace_param
) {
1146 if (bct
-- == in_brace_param
)
1151 if (unset(IGNOREBRACES
) && !sub
&& bct
> in_brace_param
)
1155 if (in_brace_param
|| sub
)
1171 if (isset(SHGLOB
) && sub
)
1174 if (!(in_brace_param
|| sub
) && e
== '(') {
1186 while ((c
= hgetc()) != '>')
1192 if (in_brace_param
|| sub
)
1210 } else if (!sub
&& peek
!= ENVSTRING
&&
1211 incmdpos
&& !bct
&& !brct
) {
1214 while (++t
< bptr
&& idigit(*t
));
1218 t
= itype_end(t
, IIDENT
, 0);
1220 skipparens(Inbrack
, Outbrack
, &t
);
1229 if (e
== '(' && incmdpos
) {
1254 int strquote
= (len
&& bptr
[-1] == String
);
1260 while ((c
= hgetc()) != '\'' && !lexstop
) {
1261 if (strquote
&& c
== '\\') {
1266 * Mostly we don't need to do anything special
1267 * with escape backslashes or closing quotes
1268 * inside $'...'; however in completion we
1269 * need to be able to strip multiple backslashes
1272 if (c
== '\\' || c
== '\'')
1276 } else if (!sub
&& isset(CSHJUNKIEQUOTES
) && c
== '\n') {
1277 if (bptr
[-1] == '\\')
1292 if (e
!= '\'' || unset(RCQUOTES
) || strquote
)
1305 c
= dquote_parse('"', sub
);
1319 while ((c
= hgetc()) != '`' && !lexstop
) {
1323 add(c
== '`' || c
== '\\' || c
== '$' ? Bnull
: '\\');
1326 else if (!sub
&& isset(CSHJUNKIEQUOTES
))
1329 if (!sub
&& isset(CSHJUNKIEQUOTES
) && c
== '\n') {
1334 if ((inquote
= !inquote
))
1363 zerr("unmatched %c", unmatched
);
1364 if (in_brace_param
) {
1365 while(bct
-- >= in_brace_param
)
1367 zerr("closing brace expected");
1368 } else if (unset(IGNOREBRACES
) && !sub
&& len
> 1 &&
1369 peek
== STRING
&& bptr
[-1] == '}' && bptr
[-2] != Bnull
) {
1370 /* hack to get {foo} command syntax work */
1377 DPUTS(cmdsp
!= ocmdsp
, "BUG: gettok: cmdstack changed.");
1382 /* Return non-zero for error (character to unget), else zero */
1386 dquote_parse(char endchar
, int sub
)
1388 int pct
= 0, brct
= 0, bct
= 0, intick
= 0, err
= 0;
1390 int math
= endchar
== ')' || endchar
== ']';
1391 int zlemath
= math
&& zlemetacs
> zlemetall
+ addedx
- inbufct
;
1393 while (((c
= hgetc()) != endchar
|| bct
||
1394 (math
&& ((pct
> 0) || (brct
> 0))) ||
1395 intick
) && !lexstop
) {
1401 if (c
== '$' || c
== '\\' || (c
== '}' && !intick
&& bct
) ||
1402 c
== endchar
|| c
== '`' ||
1403 (endchar
== ']' && (c
== '[' || c
== ']' ||
1404 c
== '(' || c
== ')' ||
1405 c
== '{' || c
== '}' ||
1406 (c
== '"' && sub
))))
1409 /* lexstop is implicitly handled here */
1413 } else if (sub
|| unset(CSHJUNKIEQUOTES
) || endchar
!= '"')
1417 err
= !sub
&& isset(CSHJUNKIEQUOTES
) && endchar
== '"';
1425 err
= cmd_or_math_sub();
1427 } else if (c
== '[') {
1430 cmdpush(CS_MATHSUBST
);
1431 err
= dquote_parse(']', sub
);
1434 } else if (c
== '{') {
1437 cmdpush(CS_BRACEPAR
);
1439 } else if (c
== '$')
1458 if ((intick
= !intick
)) {
1470 intick
= 2, STOPHIST
1472 intick
= 1, ALLOWHIST
1480 err
= (!pct
-- && math
);
1488 err
= (!brct
-- && math
);
1491 if (intick
|| ((endchar
== ']' || !endchar
) && !bct
))
1496 err
= dquote_parse('"', sub
);
1515 err
= intick
|| endchar
|| err
;
1516 else if (err
== 1) {
1518 * TODO: as far as I can see, this hack is used in gettokstr()
1519 * to hungetc() a character on an error. However, I don't
1520 * understand what that actually gets us, and we can't guarantee
1521 * it's a character anyway, because of the previous test.
1523 * We use the same feature in cmd_or_math where we actually do
1524 * need to unget if we decide it's really a command substitution.
1525 * We try to handle the other case by testing for lexstop.
1529 if (zlemath
&& zlemetacs
<= zlemetall
+ 1 - inbufct
)
1534 /* Tokenize a string given in s. Parsing is done as in double *
1535 * quotes. This is usually called before singsub(). */
1543 if ((err
= parsestrnoerr(s
))) {
1545 if (err
> 32 && err
< 127)
1546 zerr("parse error near `%c'", err
);
1548 zerr("parse error");
1555 parsestrnoerr(char *s
)
1557 int l
= strlen(s
), err
;
1561 inpush(dupstring(s
), 0, NULL
);
1566 err
= dquote_parse('\0', 1);
1570 DPUTS(cmdsp
, "BUG: parsestr: cmdstack not empty.");
1577 parse_subscript(char *s
, int sub
)
1579 int l
= strlen(s
), err
;
1582 if (!*s
|| *s
== ']')
1585 untokenize(t
= dupstring(s
));
1591 err
= dquote_parse(']', sub
);
1602 DPUTS(cmdsp
, "BUG: parse_subscript: cmdstack not empty.");
1607 /* Tokenize a string given in s. Parsing is done as if s were a normal *
1608 * command-line argument but it may contain separators. This is used *
1609 * to parse the right-hand side of ${...%...} substitutions. */
1613 parse_subst_string(char *s
)
1615 int c
, l
= strlen(s
), err
, olen
, lexstop_ret
;
1618 if (!*s
|| !strcmp(s
, nulstring
))
1622 inpush(dupstring(s
), 0, NULL
);
1628 lexstop_ret
= lexstop
;
1629 c
= gettokstr(c
, 1);
1633 DPUTS(cmdsp
, "BUG: parse_subst_string: cmdstack not empty.");
1643 * Historical note: we used to check here for olen == l, but
1644 * that's not necessarily the case if we stripped an RCQUOTE.
1646 if (c
!= STRING
|| (errflag
&& !noerrs
)) {
1647 fprintf(stderr
, "Oops. Bug in parse_subst_string: %s\n",
1648 errflag
? "errflag" : "c != STRING");
1654 /* Check for $'...' quoting. This needs special handling. */
1655 for (ptr
= s
; *ptr
; )
1657 if (*ptr
== String
&& ptr
[1] == Snull
)
1660 int len
, tlen
, diff
;
1661 t
= getkeystring(ptr
+ 2, &len
, GETKEYS_DOLLARS_QUOTE
, NULL
);
1667 * parse_subst_string() currently handles strings in-place.
1668 * That's not so easy to fix without knowing whether
1669 * additional memory should come off the heap or
1670 * otherwise. So we cheat by copying the unquoted string
1671 * into place, unless it's too long. That's not the
1672 * normal case, but I'm worried there are are pathological
1673 * cases with converting metafied multibyte strings.
1674 * If someone can prove there aren't I will be very happy.
1677 DPUTS(1, "$'...' subst too long: fix get_parse_string()");
1680 memcpy(ptr
, t
, tlen
);
1684 char *sptr
= ptr
+ diff
;
1685 while ((*dptr
++ = *sptr
++))
1694 /* Called below to report word positions. */
1700 we
= zlemetall
+ 1 - inbufct
+ (addedx
== 2 ? 1 : 0);
1701 if (zlemetacs
<= we
) {
1702 wb
= zlemetall
- wordbeg
+ addedx
;
1707 /* expand aliases and reserved words */
1717 if (interact
&& isset(SHINSTDIN
) && !strin
&& !incasepat
&&
1718 tok
== STRING
&& !nocorrect
&& !(inbufflags
& INP_ALIAS
) &&
1719 (isset(CORRECTALL
) || (isset(CORRECT
) && incmdpos
)))
1720 spckword(&tokstr
, 1, incmdpos
, 1);
1723 zshlextext
= tokstrings
[tok
];
1727 VARARR(char, copy
, (strlen(tokstr
) + 1));
1729 if (has_token(tokstr
)) {
1732 zshlextext
= p
= copy
;
1734 (*p
++ = itok(*t
) ? ztokens
[*t
++ - Pound
] : *t
++););
1736 zshlextext
= tokstr
;
1738 if (zleparse
&& !(inbufflags
& INP_ALIAS
)) {
1742 if (zp
== 1 && !zleparse
) {
1743 if (zshlextext
== copy
)
1744 zshlextext
= tokstr
;
1749 if (tok
== STRING
) {
1750 /* Check for an alias */
1751 if (!noaliases
&& isset(ALIASESOPT
) &&
1752 (!isset(POSIXALIASES
) ||
1753 !reswdtab
->getnode(reswdtab
, zshlextext
))) {
1756 an
= (Alias
) aliastab
->getnode(aliastab
, zshlextext
);
1757 if (an
&& !an
->inuse
&&
1758 ((an
->node
.flags
& ALIAS_GLOBAL
) || incmdpos
|| inalmore
)) {
1759 inpush(an
->text
, INP_ALIAS
, an
);
1760 if (an
->text
[0] == ' ')
1763 if (zshlextext
== copy
)
1764 zshlextext
= tokstr
;
1767 if ((suf
= strrchr(zshlextext
, '.')) && suf
[1] &&
1768 suf
> zshlextext
&& suf
[-1] != Meta
&&
1769 (an
= (Alias
)sufaliastab
->getnode(sufaliastab
, suf
+1)) &&
1770 !an
->inuse
&& incmdpos
) {
1771 inpush(dupstring(zshlextext
), INP_ALIAS
, NULL
);
1772 inpush(" ", INP_ALIAS
, NULL
);
1773 inpush(an
->text
, INP_ALIAS
, an
);
1775 if (zshlextext
== copy
)
1776 zshlextext
= tokstr
;
1781 /* Then check for a reserved word */
1783 (unset(IGNOREBRACES
) && zshlextext
[0] == '}' && !zshlextext
[1])) &&
1784 (rw
= (Reswd
) reswdtab
->getnode(reswdtab
, zshlextext
))) {
1786 if (tok
== DINBRACK
)
1788 } else if (incond
&& !strcmp(zshlextext
, "]]")) {
1791 } else if (incond
== 1 && zshlextext
[0] == '!' && !zshlextext
[1])
1795 if (zshlextext
== copy
)
1796 zshlextext
= tokstr
;
1807 int pct
= 1, c
, start
= 1;
1809 cmdpush(CS_CMDSUBST
);
1816 if (itok(c
) || lexstop
)
1818 iswhite
= inblank(c
);
1831 int strquote
= bptr
[-1] == '$';
1834 while ((c
= hgetc()) != '\'' && !lexstop
) {
1835 if (c
== '\\' && strquote
) {
1846 while ((c
= hgetc()) != '\"' && !lexstop
)
1855 while ((c
= hgetc()) != '`' && !lexstop
)
1857 add(c
), add(hgetc());
1864 while ((c
= hgetc()) != '\n' && !lexstop
)