1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Shell "word", file- and other name expansions, incl. file globbing.
3 *@ TODO v15: peek signal states while opendir/readdir/etc.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #ifndef HAVE_AMALGAMATION
53 * Environment variable names used by the utilities in the Shell and
54 * Utilities volume of POSIX.1-2008 consist solely of uppercase
55 * letters, digits, and the <underscore> ('_') from the characters
56 * defined in Portable Character Set and do not begin with a digit.
57 * Other characters may be permitted by an implementation;
58 * applications shall tolerate the presence of such names.
59 * We do support the hyphen "-" because it is common for mailx.
60 * We support some special parameter names for one-letter variable names;
61 * note these have counterparts in the code that manages internal variables! */
62 #define a_SHEXP_ISVARC(C) (alnumchar(C) || (C) == '_' || (C) == '-')
63 #define a_SHEXP_ISVARC_SPECIAL1(C) \
64 ((C) == '*' || (C) == '@' || (C) == '#' || (C) == '?' || (C) == '!')
66 enum a_shexp_quote_flags
{
68 a_SHEXP_QUOTE_ROUNDTRIP
= 1u<<0, /* Result won't be consumed immediately */
70 a_SHEXP_QUOTE_T_REVSOL
= 1u<<8, /* Type: by reverse solidus */
71 a_SHEXP_QUOTE_T_SINGLE
= 1u<<9, /* Type: single-quotes */
72 a_SHEXP_QUOTE_T_DOUBLE
= 1u<<10, /* Type: double-quotes */
73 a_SHEXP_QUOTE_T_DOLLAR
= 1u<<11, /* Type: dollar-single-quotes */
74 a_SHEXP_QUOTE_T_MASK
= a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
75 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
,
77 a_SHEXP_QUOTE__FREESHIFT
= 16u
81 struct a_shexp_glob_ctx
{
82 char const *sgc_patdat
; /* Remaining pattern (at and below level) */
84 struct n_string
*sgc_outer
; /* Resolved path up to this level */
90 struct a_shexp_quote_ctx
{
91 struct n_string
*sqc_store
; /* Result storage */
92 struct str sqc_input
; /* Input data, topmost level */
94 ui32_t sqc_cnt_single
;
95 ui32_t sqc_cnt_double
;
96 ui32_t sqc_cnt_dollar
;
97 enum a_shexp_quote_flags sqc_flags
;
101 struct a_shexp_quote_lvl
{
102 struct a_shexp_quote_lvl
*sql_link
; /* Outer level */
103 struct str sql_dat
; /* This level (has to) handle(d) */
104 enum a_shexp_quote_flags sql_flags
;
108 /* Locate the user's mailbox file (where new, unread mail is queued) */
109 static char *a_shexp_findmail(char const *user
, bool_t force
);
111 /* Expand ^~/? and ^~USER/? constructs.
112 * Returns the completely resolved (maybe empty or identical to input)
113 * salloc()ed string */
114 static char *a_shexp_tilde(char const *s
);
116 /* Perform fnmatch(3). May return NULL on error */
117 static char *a_shexp_globname(char const *name
, enum fexp_mode fexpm
);
119 static bool_t
a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
,
120 struct n_strlist
**slpp
);
121 static int a_shexp__globsort(void const *cvpa
, void const *cvpb
);
124 /* Parse an input string and create a sh(1)ell-quoted result */
125 static void a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
,
126 struct a_shexp_quote_lvl
*sqlp
);
129 a_shexp_findmail(char const *user
, bool_t force
){
135 if((cp
= ok_vlook(inbox
)) != NULL
&& *cp
!= '\0'){
136 /* Folder extra introduced to avoid % recursion loops */
137 if((rv
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
140 n_err(_("*inbox* expansion failed, using $MAIL / builtin: %s\n"), cp
);
143 if((cp
= ok_vlook(MAIL
)) != NULL
){
152 ul
= strlen(user
) +1;
153 i
= sizeof(VAL_MAIL
) -1 + 1 + ul
;
156 memcpy(rv
, VAL_MAIL
, (i
= sizeof(VAL_MAIL
) -1));
158 memcpy(&rv
[++i
], user
, ul
);
166 a_shexp_tilde(char const *s
){
173 if(*(rp
= &s
[1]) == '/' || *rp
== '\0'){
177 if((rp
= strchr(np
= rp
, '/')) != NULL
){
178 nl
= PTR2SIZE(rp
- np
);
179 np
= savestrbuf(np
, nl
);
184 if((pwp
= getpwnam(np
)) == NULL
){
192 rv
= salloc(nl
+ 1 + rl
+1);
195 memcpy(rv
+ nl
, rp
, rl
);
205 a_shexp_globname(char const *name
, enum fexp_mode fexpm
){
207 struct a_shexp_glob_ctx sgc
;
208 struct n_string outer
;
209 struct n_strlist
*slp
;
213 memset(&sgc
, 0, sizeof sgc
);
214 sgc
.sgc_patlen
= strlen(name
);
215 sgc
.sgc_patdat
= savestrbuf(name
, sgc
.sgc_patlen
);
216 sgc
.sgc_outer
= n_string_reserve(n_string_creat(&outer
), sgc
.sgc_patlen
);
217 sgc
.sgc_flags
= ((fexpm
& FEXP_SILENT
) != 0);
219 if(a_shexp__glob(&sgc
, &slp
))
223 n_string_gut(&outer
);
229 cp
= n_UNCONST(N_("File pattern does not match"));
231 }else if(slp
->sl_next
== NULL
)
232 cp
= savestrbuf(slp
->sl_dat
, slp
->sl_len
);
233 else if(fexpm
& FEXP_MULTIOK
){
234 struct n_strlist
**sorta
, *xslp
;
238 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
){
240 l
+= xslp
->sl_len
+ 1;
243 sorta
= smalloc(sizeof(*sorta
) * no
);
245 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
)
247 qsort(sorta
, no
, sizeof *sorta
, &a_shexp__globsort
);
251 for(i
= 0; i
< no
; ++i
){
253 memcpy(&cp
[l
], xslp
->sl_dat
, xslp
->sl_len
);
260 n_pstate
|= n_PS_EXPAND_MULTIRESULT
;
262 cp
= n_UNCONST(N_("File pattern matches multiple results"));
268 struct n_strlist
*tmp
= slp
;
277 if(!(fexpm
& FEXP_SILENT
)){
278 name
= n_shexp_quote_cp(name
, FAL0
);
279 n_err("%s: %s\n", V_(cp
), name
);
284 #else /* HAVE_FNMATCH */
287 if(!(fexpm
& FEXP_SILENT
))
288 n_err(_("No filename pattern (fnmatch(3)) support compiled in\n"));
289 return savestr(name
);
295 a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
, struct n_strlist
**slpp
){
296 enum{a_SILENT
= 1<<0, a_DEEP
=1<<1, a_SALLOC
=1<<2};
298 struct a_shexp_glob_ctx nsgc
;
302 char const *ccp
, *myp
;
305 /* We need some special treatment for the outermost level */
306 if(!(sgcp
->sgc_flags
& a_DEEP
)){
307 if(sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/'){
308 myp
= n_string_cp(n_string_push_c(sgcp
->sgc_outer
, '/'));
314 myp
= n_string_cp(sgcp
->sgc_outer
);
315 old_outerlen
= sgcp
->sgc_outer
->s_len
;
317 /* Separate current directory/pattern level from any possible remaining
318 * pattern in order to be able to use it for fnmatch(3) */
319 if((ccp
= memchr(sgcp
->sgc_patdat
, '/', sgcp
->sgc_patlen
)) == NULL
)
323 nsgc
.sgc_flags
|= a_DEEP
;
324 sgcp
->sgc_patlen
= PTR2SIZE((nsgc
.sgc_patdat
= &ccp
[1]) -
325 &sgcp
->sgc_patdat
[0]);
326 nsgc
.sgc_patlen
-= sgcp
->sgc_patlen
;
328 if(sgcp
->sgc_patlen
> 0){
329 assert(sgcp
->sgc_patdat
[sgcp
->sgc_patlen
-1] == '/');
330 ((char*)n_UNCONST(sgcp
->sgc_patdat
))[--sgcp
->sgc_patlen
] = '\0';
334 /* Our current directory level */
335 /* xxx Plenty of room for optimizations, like quickshot lstat(2) which may
336 * xxx be the (sole) result depending on pattern surroundings, etc. */
337 if((dp
= opendir(myp
)) == NULL
){
340 switch((err
= errno
)){
342 ccp
= N_("cannot access paths under non-directory");
345 ccp
= N_("path component of (sub)pattern non-existent");
348 ccp
= N_("file permission for file (sub)pattern denied");
351 ccp
= N_("cannot handle file (sub)pattern");
356 /* As necessary, quote bytes in the current pattern */
362 for(need
= FAL0
, i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
364 case '\'': case '"': case '\\': case '$':
376 for(i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
378 case '\'': case '"': case '\\': case '$':
389 myp
= sgcp
->sgc_patdat
;
392 while((dep
= readdir(dp
)) != NULL
){
393 switch(fnmatch(myp
, dep
->d_name
, FNM_PATHNAME
| FNM_PERIOD
)){
395 /* A match expresses the desire to recurse if there is more pattern */
396 if(nsgc
.sgc_patlen
> 0){
399 n_string_push_cp((sgcp
->sgc_outer
->s_len
> 1
400 ? n_string_push_c(sgcp
->sgc_outer
, '/') : sgcp
->sgc_outer
),
404 #ifdef HAVE_DIRENT_TYPE
405 if(dep
->d_type
== DT_DIR
)
407 else if(dep
->d_type
== DT_LNK
|| dep
->d_type
== DT_UNKNOWN
)
412 if(stat(n_string_cp(sgcp
->sgc_outer
), &sb
)){
413 ccp
= N_("I/O error when querying file status");
415 }else if(S_ISDIR(sb
.st_mode
))
419 /* TODO We recurse with current dir FD open, which could E[MN]FILE!
420 * TODO Instead save away a list of such n_string's for later */
421 if(isdir
&& !a_shexp__glob(&nsgc
, slpp
)){
426 n_string_trunc(sgcp
->sgc_outer
, old_outerlen
);
428 struct n_strlist
*slp
;
431 i
= strlen(dep
->d_name
);
432 j
= (old_outerlen
> 0) ? old_outerlen
+ 1 + i
: i
;
433 slp
= n_STRLIST_MALLOC(j
);
435 slpp
= &slp
->sl_next
;
437 if((j
= old_outerlen
) > 0){
438 memcpy(&slp
->sl_dat
[0], sgcp
->sgc_outer
->s_dat
, j
);
439 if(slp
->sl_dat
[j
-1] != '/')
440 slp
->sl_dat
[j
++] = '/';
442 memcpy(&slp
->sl_dat
[j
], dep
->d_name
, i
);
443 slp
->sl_dat
[j
+= i
] = '\0';
450 ccp
= N_("fnmatch(3) cannot handle file (sub)pattern");
460 return (ccp
== NULL
);
463 if(!(sgcp
->sgc_flags
& a_SILENT
)){
466 if(sgcp
->sgc_outer
->s_len
> 0){
467 s2
= n_shexp_quote_cp(n_string_cp(sgcp
->sgc_outer
), FAL0
);
472 n_err("%s: %s%s%s\n", V_(ccp
), s2
, s3
,
473 n_shexp_quote_cp(sgcp
->sgc_patdat
, FAL0
));
479 a_shexp__globsort(void const *cvpa
, void const *cvpb
){
481 struct n_strlist
const * const *slpa
, * const *slpb
;
486 rv
= asccasecmp((*slpa
)->sl_dat
, (*slpb
)->sl_dat
);
490 #endif /* HAVE_FNMATCH */
493 a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
, struct a_shexp_quote_lvl
*sqlp
){
494 /* XXX Because of the problems caused by ISO C multibyte interface we cannot
495 * XXX use the recursive implementation because of stateful encodings.
496 * XXX I.e., if a quoted substring cannot be self-contained - the data after
497 * XXX the quote relies on "the former state", then this doesn't make sense.
498 * XXX Therefore this is not fully programmed out but instead only detects
499 * XXX the "most fancy" quoting necessary, and directly does that.
500 * XXX As a result of this, T_REVSOL and T_DOUBLE are not even considered.
501 * XXX Otherwise we rather have to convert to wide first and act on that,
502 * XXX e.g., call visual_info(n_VISUAL_INFO_WOUT_CREATE) on entire input */
503 #undef a_SHEXP_QUOTE_RECURSE /* XXX (Needs complete revisit, then) */
504 #ifdef a_SHEXP_QUOTE_RECURSE
505 # define jrecurse jrecurse
506 struct a_shexp_quote_lvl sql
;
508 # define jrecurse jstep
510 struct n_visual_info_ctx vic
;
511 union {struct a_shexp_quote_lvl
*head
; struct n_string
*store
;} u
;
514 char const *ib
, *ib_base
;
517 ib_base
= ib
= sqlp
->sql_dat
.s
;
518 il
= sqlp
->sql_dat
.l
;
519 flags
= sqlp
->sql_flags
;
521 /* Iterate over the entire input, classify characters and type of quotes
522 * along the way. Whenever a quote change has to be applied, adjust flags
523 * for the new situation -, setup sql.* and recurse- */
529 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
531 if(c
== '\t' && (flags
& (a_SHEXP_QUOTE_T_REVSOL
|
532 a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOUBLE
)))
534 #ifdef a_SHEXP_QUOTE_RECURSE
535 ++sqcp
->sqc_cnt_dollar
;
537 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
539 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
540 /* Whereas we don't support those, quote them for the sh(1)ell */
541 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
542 c
== '"' || c
== '$'){
543 if(flags
& a_SHEXP_QUOTE_T_MASK
)
545 #ifdef a_SHEXP_QUOTE_RECURSE
546 ++sqcp
->sqc_cnt_single
;
548 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
551 if(flags
& (a_SHEXP_QUOTE_T_MASK
& ~a_SHEXP_QUOTE_T_SINGLE
))
553 #ifdef a_SHEXP_QUOTE_RECURSE
554 ++sqcp
->sqc_cnt_dollar
;
556 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
558 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
559 if(flags
& a_SHEXP_QUOTE_T_MASK
)
561 #ifdef a_SHEXP_QUOTE_RECURSE
562 ++sqcp
->sqc_cnt_single
;
564 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
566 }else if(!asciichar(c
)){
567 /* Need to keep together multibytes */
568 #ifdef a_SHEXP_QUOTE_RECURSE
569 memset(&vic
, 0, sizeof vic
);
573 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
575 /* xxx check whether resulting \u would be ASCII */
576 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
) ||
577 (flags
& a_SHEXP_QUOTE_T_DOLLAR
)){
578 #ifdef a_SHEXP_QUOTE_RECURSE
586 #ifdef a_SHEXP_QUOTE_RECURSE
587 ++sqcp
->sqc_cnt_dollar
;
589 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
595 sqlp
->sql_flags
= flags
;
597 /* Level made the great and completed processing input. Reverse the list of
598 * levels, detect the "most fancy" quote type needed along this way */
599 /* XXX Due to restriction as above very crude */
600 for(flags
= 0, il
= 0, u
.head
= NULL
; sqlp
!= NULL
;){
601 struct a_shexp_quote_lvl
*tmp
;
603 tmp
= sqlp
->sql_link
;
604 sqlp
->sql_link
= u
.head
;
606 il
+= sqlp
->sql_dat
.l
;
607 if(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
)
608 il
+= (sqlp
->sql_dat
.l
>> 1);
609 flags
|= sqlp
->sql_flags
;
614 /* Finally work the substrings in the correct order, adjusting quotes along
615 * the way as necessary. Start off with the "most fancy" quote, so that
616 * the user sees an overall boundary she can orientate herself on.
617 * We do it like that to be able to give the user some "encapsulation
618 * experience", to address what strikes me is a problem of sh(1)ell quoting:
619 * different to, e.g., perl(1), where you see at a glance where a string
620 * starts and ends, sh(1) quoting occurs at the "top level", disrupting the
621 * visual appearance of "a string" as such */
622 u
.store
= n_string_reserve(sqcp
->sqc_store
, il
);
624 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
625 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
626 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
627 }else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
){
628 u
.store
= n_string_push_c(u
.store
, '"');
629 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOUBLE
;
630 }else if(flags
& a_SHEXP_QUOTE_T_SINGLE
){
631 u
.store
= n_string_push_c(u
.store
, '\'');
632 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
633 }else /*if(flags & a_SHEXP_QUOTE_T_REVSOL)*/
634 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
636 /* Work all the levels */
637 for(; sqlp
!= NULL
; sqlp
= sqlp
->sql_link
){
638 /* As necessary update our mode of quoting */
639 #ifdef a_SHEXP_QUOTE_RECURSE
642 switch(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
){
643 case a_SHEXP_QUOTE_T_DOLLAR
:
644 if(!(flags
& a_SHEXP_QUOTE_T_DOLLAR
))
645 il
= a_SHEXP_QUOTE_T_DOLLAR
;
647 case a_SHEXP_QUOTE_T_DOUBLE
:
648 if(!(flags
& (a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
649 il
= a_SHEXP_QUOTE_T_DOLLAR
;
651 case a_SHEXP_QUOTE_T_SINGLE
:
652 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
653 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
654 il
= a_SHEXP_QUOTE_T_SINGLE
;
657 case a_SHEXP_QUOTE_T_REVSOL
:
658 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
659 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
660 il
= a_SHEXP_QUOTE_T_REVSOL
;
665 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
666 u
.store
= n_string_push_c(u
.store
, '\'');
667 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
668 u
.store
= n_string_push_c(u
.store
, '"');
669 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
672 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
673 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
674 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
675 u
.store
= n_string_push_c(u
.store
, '"');
676 else if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
677 u
.store
= n_string_push_c(u
.store
, '\'');
679 #endif /* a_SHEXP_QUOTE_RECURSE */
681 /* Work the level's substring */
682 ib
= sqlp
->sql_dat
.s
;
683 il
= sqlp
->sql_dat
.l
;
691 assert(c
== '\t' || (flags
& a_SHEXP_QUOTE_T_DOLLAR
));
692 assert((flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
693 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)));
695 case 0x07: c
= 'a'; break;
696 case 0x08: c
= 'b'; break;
697 case 0x0A: c
= 'n'; break;
698 case 0x0B: c
= 'v'; break;
699 case 0x0C: c
= 'f'; break;
700 case 0x0D: c
= 'r'; break;
701 case 0x1B: c
= 'E'; break;
704 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
708 if(flags
& a_SHEXP_QUOTE_T_REVSOL
)
709 u
.store
= n_string_push_c(u
.store
, '\\');
712 u
.store
= n_string_push_c(u
.store
, '\\');
714 u
.store
= n_string_push_c(u
.store
, 'c');
718 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
719 /* Whereas we don't support those, quote them for the sh(1)ell */
720 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
721 c
== '"' || c
== '$'){
722 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
724 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
));
725 u
.store
= n_string_push_c(u
.store
, '\\');
728 if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
730 assert(!(flags
& a_SHEXP_QUOTE_T_SINGLE
));
731 u
.store
= n_string_push_c(u
.store
, '\\');
733 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
734 if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
736 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
|
737 a_SHEXP_QUOTE_T_DOLLAR
));
738 u
.store
= n_string_push_c(u
.store
, '\\');
740 }else if(asciichar(c
)){
741 /* Shorthand: we can simply push that thing out */
743 u
.store
= n_string_push_c(u
.store
, c
);
746 /* Not an ASCII character, take care not to split up multibyte
747 * sequences etc. For the sake of compile testing, don't enwrap in
748 * HAVE_ALWAYS_UNICODE_LOCALE || HAVE_NATCH_CHAR */
749 if(n_psonce
& n_PSO_UNICODE
){
756 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
760 il2
= PTR2SIZE(&ib2
[0] - &ib
[0]);
761 if((flags
& a_SHEXP_QUOTE_ROUNDTRIP
) || uc
== 0xFFFDu
){
762 /* Use padding to make ambiguities impossible */
763 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
764 (uc
> 0xFFFFu
? 'U' : 'u'),
765 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
771 u
.store
= n_string_push_buf(u
.store
, cp
, il3
);
772 ib
+= il2
, il
-= il2
;
777 memset(&vic
, 0, sizeof vic
);
781 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
783 /* Work this substring as sensitive as possible */
785 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
))
786 u
.store
= n_string_push_buf(u
.store
, ib
, il
);
788 else if((vic
.vic_indat
= n_iconv_onetime_cp(n_ICONV_NONE
,
789 "utf-8", ok_vlook(ttycharset
), savestrbuf(ib
, il
))) != NULL
){
794 il3
= il2
= strlen(ib2
= vic
.vic_indat
);
795 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
798 il2
= PTR2SIZE(&ib2
[0] - &vic
.vic_indat
[0]);
799 /* Use padding to make ambiguities impossible */
800 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
801 (uc
> 0xFFFFu
? 'U' : 'u'),
802 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
803 u
.store
= n_string_push_buf(u
.store
, itoa
, il3
);
813 u
.store
= n_string_push_buf(u
.store
, "\\xFF",
815 n_c_to_hex_base16(&u
.store
->s_dat
[u
.store
->s_len
- 2], *ib
++);
824 /* Close an open quote */
825 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
826 u
.store
= n_string_push_c(u
.store
, '\'');
827 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
828 u
.store
= n_string_push_c(u
.store
, '"');
829 #ifdef a_SHEXP_QUOTE_RECURSE
835 #ifdef a_SHEXP_QUOTE_RECURSE
837 sqlp
->sql_dat
.l
-= il
;
840 sql
.sql_dat
.s
= n_UNCONST(ib
);
842 sql
.sql_flags
= flags
;
843 a_shexp__quote(sqcp
, &sql
);
848 #undef a_SHEXP_QUOTE_RECURSE
852 fexpand(char const *name
, enum fexp_mode fexpm
)
855 char const *cp
, *res
;
859 n_pstate
&= ~n_PS_EXPAND_MULTIRESULT
;
861 /* The order of evaluation is "%" and "#" expand into constants.
862 * "&" can expand into "+". "+" can expand into shell meta characters.
863 * Shell meta characters expand into constants.
864 * This way, we make no recursive expansion */
865 if ((fexpm
& FEXP_NSHORTCUT
) || (res
= shortcut_expand(name
)) == NULL
)
866 res
= n_UNCONST(name
);
868 if(!(fexpm
& FEXP_NSPECIAL
)){
873 if(res
[1] == ':' && res
[2] != '\0')
878 force
= (res
[1] != '\0');
879 res
= a_shexp_findmail((force
? &res
[1] : ok_vlook(LOGNAME
)),
888 if (prevfile
[0] == '\0') {
889 n_err(_("No previous file\n"));
897 res
= ok_vlook(MBOX
);
902 /* POSIX: if *folder* unset or null, "+" shall be retained */
903 if (!(fexpm
& FEXP_NFOLDER
) && *res
== '+' &&
904 *(cp
= folder_query()) != '\0') {
905 res
= str_concat_csvl(&s
, cp
, &res
[1], NULL
)->s
;
908 /* TODO *folder* can't start with %[:], can it!?! */
909 if (res
[0] == '%' && res
[1] == ':') {
915 /* Do some meta expansions */
916 if((fexpm
& (FEXP_NSHELL
| FEXP_NVAR
)) != FEXP_NVAR
&&
917 ((fexpm
& FEXP_NSHELL
) ? (strchr(res
, '$') != NULL
)
918 : anyof(res
, "{}[]*?$"))){
921 if(fexpm
& FEXP_NOPROTO
)
923 else switch(which_protocol(res
)){
935 struct n_string shou
, *shoup
;
937 shin
.s
= n_UNCONST(res
);
939 shoup
= n_string_creat_auto(&shou
);
941 enum n_shexp_state shs
;
943 /* TODO shexp: take care to not include backtick eval once avail! */
944 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG_D_V
|
945 n_SHEXP_PARSE_QUOTE_AUTO_FIXED
| n_SHEXP_PARSE_QUOTE_AUTO_DQ
|
946 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
), shoup
, &shin
, NULL
);
947 if(shs
& n_SHEXP_STATE_STOP
)
950 res
= n_string_cp(shoup
);
951 shoup
= n_string_drop_ownership(shoup
);
955 res
= a_shexp_tilde(res
);
957 if(!(fexpm
& FEXP_NSHELL
) &&
958 (res
= a_shexp_globname(res
, fexpm
)) == NULL
)
962 }else if(res
[0] == '~'){
963 res
= a_shexp_tilde(res
);
968 if (fexpm
& FEXP_LOCAL
)
969 switch (which_protocol(res
)) {
974 n_err(_("Not a local file or directory: %s\n"),
975 n_shexp_quote_cp(name
, FAL0
));
981 if(res
!= NULL
&& !dyn
)
984 return n_UNCONST(res
);
987 FL
enum n_shexp_state
988 n_shexp_parse_token(enum n_shexp_parse_flags flags
, struct n_string
*store
,
989 struct str
*input
, void const **cookie
){
990 /* TODO shexp_parse_token: WCHAR; $IFS (sp20=' '; echo a $sp20 b; ..) */
991 char c2
, c
, quotec
, utf
[8];
992 enum n_shexp_state rv
;
994 char const *ib_save
, *ib
;
997 a_SKIPQ
= 1<<0, /* Skip rest of this quote (\c0 ..) */
998 a_SURPLUS
= 1<<1, /* Extended sequence interpretation */
999 a_NTOKEN
= 1<<2, /* "New token": e.g., comments are possible */
1000 a_ROUND_MASK
= ~((1<<8) - 1),
1003 a_CONSUME
= 1<<10 /* When done, "consume" remaining input */
1007 assert((flags
& n_SHEXP_PARSE_DRYRUN
) || store
!= NULL
);
1008 assert(input
!= NULL
);
1009 assert(input
->l
== 0 || input
->s
!= NULL
);
1010 assert(!(flags
& n_SHEXP_PARSE_LOG
) || !(flags
& n_SHEXP_PARSE_LOG_D_V
));
1011 assert(!(flags
& n_SHEXP_PARSE_IFS_ADD_COMMA
) ||
1012 !(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
));
1013 assert(!(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
) ||
1014 (flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
));
1016 if((flags
& n_SHEXP_PARSE_LOG_D_V
) && (n_poption
& n_PO_D_V
))
1017 flags
|= n_SHEXP_PARSE_LOG
;
1018 if(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)
1019 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
;
1021 if((flags
& n_SHEXP_PARSE_TRUNC
) && store
!= NULL
)
1022 store
= n_string_trunc(store
, 0);
1026 if((il
= input
->l
) == UIZ_MAX
)
1027 input
->l
= il
= strlen(ib
);
1030 if(cookie
!= NULL
&& *cookie
!= NULL
){
1031 assert(!(flags
& n_SHEXP_PARSE_DRYRUN
));
1036 rv
= n_SHEXP_STATE_NONE
;
1037 state
&= a_ROUND_MASK
;
1039 /* In cookie mode, the next ARGV entry is the token already, unchanged,
1040 * since it has already been expanded before! */
1041 if(state
& a_COOKIE
){
1042 char const * const *xcookie
, *cp
;
1046 if((store
= n_string_push_cp(store
, *xcookie
))->s_len
> 0)
1047 rv
|= n_SHEXP_STATE_OUTPUT
;
1048 if(*++xcookie
== NULL
){
1051 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_DQ
; /* ..why we are here! */
1053 *cookie
= n_UNCONST(xcookie
);
1055 for(cp
= &n_string_cp(store
)[i
]; (c
= *cp
++) != '\0';)
1057 rv
|= n_SHEXP_STATE_CONTROL
;
1061 /* The last exploded cookie will join with the yielded input token, so
1062 * simply fall through in this case */
1063 if(state
& a_COOKIE
)
1066 if(flags
& n_SHEXP_PARSE_TRIMSPACE
){
1067 for(; il
> 0; ++ib
, --il
)
1068 if(!blankspacechar(*ib
))
1071 input
->s
= n_UNCONST(ib
);
1076 rv
|= n_SHEXP_STATE_STOP
;
1081 store
= n_string_reserve(store
, n_MIN(il
, 32)); /* XXX */
1083 switch(flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
){
1084 case n_SHEXP_PARSE_QUOTE_AUTO_SQ
:
1087 case n_SHEXP_PARSE_QUOTE_AUTO_DQ
:
1090 case n_SHEXP_PARSE_QUOTE_AUTO_DSQ
:
1104 /* If no quote-mode active.. */
1106 if(c
== '"' || c
== '\''){
1111 state
&= ~a_SURPLUS
;
1125 }else if(c
== '\\'){
1126 /* Outside of quotes this just escapes any next character, but a sole
1127 * <backslash> at EOS is left unchanged */
1132 /* A comment may it be if no token has yet started */
1133 else if(c
== '#' && (state
& a_NTOKEN
)){
1134 rv
|= n_SHEXP_STATE_STOP
;
1137 /* Metacharacters which separate tokens must be turned on explicitly */
1139 rv
|= n_SHEXP_STATE_META_VERTBAR
;
1140 /* The parsed sequence may be _the_ output, so ensure we don't
1141 * include the metacharacter, then. */
1142 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1144 /*else if(flags & n_SHEXP_PARSE_META_VERTBAR)*/
1147 rv
|= n_SHEXP_STATE_META_AMPERSAND
;
1148 /* The parsed sequence may be _the_ output, so ensure we don't
1149 * include the metacharacter, then. */
1150 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1152 /*else if(flags & n_SHEXP_PARSE_META_AMPERSAND)*/
1155 rv
|= n_SHEXP_STATE_META_SEMICOLON
;
1156 /* The parsed sequence may be _the_ output, so ensure we don't
1157 * include the metacharacter, then. */
1158 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1160 else if(flags
& n_SHEXP_PARSE_META_SEMICOLON
){
1162 n_source_inject_input(n_INPUT_INJECT_COMMIT
, ib
, il
);
1164 rv
|= n_SHEXP_STATE_STOP
;
1167 }else if(c
== ',' && (flags
&
1168 (n_SHEXP_PARSE_IFS_ADD_COMMA
| n_SHEXP_PARSE_IFS_IS_COMMA
))){
1169 /* The parsed sequence may be _the_ output, so ensure we don't
1170 * include the metacharacter, then. */
1171 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1174 }else if(blankchar(c
)){
1175 if(!(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
)){
1176 /* The parsed sequence may be _the_ output, so ensure we don't
1177 * include the metacharacter, then. */
1178 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1187 assert(!(state
& a_NTOKEN
));
1188 if(c
== quotec
&& !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)){
1189 state
&= a_ROUND_MASK
;
1191 /* Users may need to recognize the presence of empty quotes */
1192 rv
|= n_SHEXP_STATE_OUTPUT
;
1194 }else if(c
== '\\' && (state
& a_SURPLUS
)){
1196 /* A sole <backslash> at EOS is treated as-is! This is ok since
1197 * the "closing quote" error will occur next, anyway */
1200 else if((c2
= *ib
) == quotec
){
1203 }else if(quotec
== '"'){
1205 * The <backslash> shall retain its special meaning as an
1206 * escape character (see Section 2.2.1) only when followed
1207 * by one of the following characters when considered
1208 * special: $ ` " \ <newline> */
1212 /* case '"': already handled via c2 == quotec */
1221 /* Dollar-single-quote */
1225 /* case '\'': already handled via c2 == quotec */
1230 case 'b': c
= '\b'; break;
1231 case 'f': c
= '\f'; break;
1232 case 'n': c
= '\n'; break;
1233 case 'r': c
= '\r'; break;
1234 case 't': c
= '\t'; break;
1235 case 'v': c
= '\v'; break;
1238 case 'e': c
= '\033'; break;
1240 /* Control character */
1243 goto j_dollar_ungetc
;
1247 c
= upperconv(c2
) ^ 0x40;
1248 if((ui8_t
)c
> 0x1F && c
!= 0x7F){ /* ASCII C0: 0..1F, 7F */
1249 if(flags
& n_SHEXP_PARSE_LOG
)
1250 n_err(_("Invalid \\c notation: %.*s\n"),
1251 (int)input
->l
, input
->s
);
1252 rv
|= n_SHEXP_STATE_ERR_CONTROL
;
1254 /* As an implementation-defined extension, support \c@
1255 * EQ printf(1) alike \c */
1257 rv
|= n_SHEXP_STATE_STOP
;
1262 /* Octal sequence: 1 to 3 octal bytes */
1264 /* As an extension (dependent on where you look, echo(1), or
1265 * awk(1)/tr(1) etc.), allow leading "0" octal indicator */
1266 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1271 case '1': case '2': case '3':
1272 case '4': case '5': case '6': case '7':
1274 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1275 c2
= (c2
<< 3) | (c
- '0');
1278 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1279 if((ui8_t
)c2
> 0x1F){
1280 if(flags
& n_SHEXP_PARSE_LOG
)
1281 n_err(_("\\0 argument exceeds a byte: %.*s\n"),
1282 (int)input
->l
, input
->s
);
1283 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1285 /* Write unchanged */
1287 rv
|= n_SHEXP_STATE_OUTPUT
;
1288 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1289 store
= n_string_push_buf(store
, ib_save
,
1290 PTR2SIZE(ib
- ib_save
));
1293 c2
= (c2
<< 3) | (c
-= '0');
1296 if((c
= c2
) == '\0')
1302 /* ISO 10646 / Unicode sequence, 8 or 4 hexadecimal bytes */
1311 goto j_dollar_ungetc
;
1315 /* Hexadecimal sequence, 1 or 2 hexadecimal bytes */
1319 goto j_dollar_ungetc
;
1323 static ui8_t
const hexatoi
[] = { /* XXX uses ASCII */
1324 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1329 for(no
= j
= 0; i
-- > 0; --il
, ++ib
, ++j
){
1333 no
+= hexatoi
[(ui8_t
)((c
) - ((c
) <= '9' ? 48
1334 : ((c
) <= 'F' ? 55 : 87)))];
1338 c2
= (c2
== 'U' || c2
== 'u') ? 'u' : 'x';
1339 if(flags
& n_SHEXP_PARSE_LOG
)
1340 n_err(_("Invalid \\%c notation: %.*s\n"),
1341 c2
, (int)input
->l
, input
->s
);
1342 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1348 /* Unicode massage */
1349 if((c2
!= 'U' && c2
!= 'u') || n_uasciichar(no
)){
1350 if((c
= (char)no
) == '\0')
1354 else if(!(state
& a_SKIPQ
)){
1355 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1356 store
= n_string_reserve(store
, n_MAX(j
, 4));
1359 if(no
> 0x10FFFF){ /* XXX magic; CText */
1360 if(flags
& n_SHEXP_PARSE_LOG
)
1361 n_err(_("\\U argument exceeds 0x10FFFF: %.*s\n"),
1362 (int)input
->l
, input
->s
);
1363 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1364 /* But normalize the output anyway */
1368 j
= n_utf32_to_utf8(no
, utf
);
1370 if(n_psonce
& n_PSO_UNICODE
){
1371 rv
|= n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_UNICODE
;
1372 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1373 store
= n_string_push_buf(store
, utf
, j
);
1380 icp
= n_iconv_onetime_cp(n_ICONV_NONE
,
1383 rv
|= n_SHEXP_STATE_OUTPUT
;
1384 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1385 store
= n_string_push_cp(store
, icp
);
1390 if(!(flags
& n_SHEXP_PARSE_DRYRUN
)) Je_uni_norm
:{
1393 rv
|= n_SHEXP_STATE_OUTPUT
|
1394 n_SHEXP_STATE_ERR_UNICODE
;
1395 i
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
1396 (no
> 0xFFFFu
? 'U' : 'u'),
1397 (int)(no
> 0xFFFFu
? 8 : 4), (ui32_t
)no
);
1398 store
= n_string_push_buf(store
, itoa
, i
);
1407 /* Extension: \$ can be used to expand a variable.
1408 * Bug|ad effect: if conversion fails, not written "as-is" */
1411 goto j_dollar_ungetc
;
1416 /* Follow bash(1) behaviour, print sequence unchanged */
1421 }else if(c
== '$' && quotec
== '"' && il
> 0) J_var_expand
:{
1424 if(!(brace
= (*ib
== '{')) || il
> 1){
1425 char const *cp
, *vp
;
1430 state
&= ~a_EXPLODE
;
1432 for(i
= 0; il
> 0; --il
, ++ib
, ++i
){
1433 /* We have some special cases regarding macro-local special
1434 * parameters, so ensure these don't cause failure */
1436 if(!a_SHEXP_ISVARC(c
)){
1437 if(i
== 0 && a_SHEXP_ISVARC_SPECIAL1(c
)){
1438 if(c
== '@' && quotec
== '"')
1447 if(state
& a_SKIPQ
){
1448 if(brace
&& il
> 0 && *ib
== '}')
1455 if(flags
& n_SHEXP_PARSE_LOG
)
1456 n_err(_("Bad substitution (${}): %.*s\n Near %.*s\n"),
1457 (int)input
->l
, input
->s
, (int)il
, ib
);
1458 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1464 if(il
== 0 || *ib
!= '}'){
1465 if(flags
& n_SHEXP_PARSE_LOG
)
1466 n_err(_("Missing closing brace for ${VAR}: %.*s\n"
1468 (int)input
->l
, input
->s
, (int)il
, ib
);
1469 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
|
1470 n_SHEXP_STATE_ERR_BRACE
;
1476 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1479 /* We may shall explode "${@}" to a series of successive,
1480 * properly quoted tokens (instead). The first exploded
1481 * cookie will join with the current token */
1482 if((state
& a_EXPLODE
) && !(flags
& n_SHEXP_PARSE_DRYRUN
) &&
1483 cookie
!= NULL
&& n_var_vexplode(cookie
)){
1485 input
->s
= n_UNCONST(ib
);
1487 goto jrestart_empty
;
1490 /* Check getenv(3) shall no internal variable exist! */
1491 vp
= savestrbuf(vp
, i
);
1492 if((cp
= n_var_vlook(vp
, TRU1
)) != NULL
){
1493 rv
|= n_SHEXP_STATE_OUTPUT
;
1494 store
= n_string_push_cp(store
, cp
);
1495 for(; (c
= *cp
) != '\0'; ++cp
)
1497 rv
|= n_SHEXP_STATE_CONTROL
;
1504 }else if(c
== '`' && quotec
== '"' && il
> 0){ /* TODO shell command */
1509 if(!(state
& a_SKIPQ
)){
1510 rv
|= n_SHEXP_STATE_OUTPUT
;
1512 rv
|= n_SHEXP_STATE_CONTROL
;
1513 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1514 store
= n_string_push_c(store
, c
);
1518 if(quotec
!= '\0' && !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
)){
1519 if(flags
& n_SHEXP_PARSE_LOG
)
1520 n_err(_("no closing quote: %.*s\n"), (int)input
->l
, input
->s
);
1521 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
;
1525 assert(!(state
& a_COOKIE
));
1526 if((flags
& n_SHEXP_PARSE_DRYRUN
) && store
!= NULL
){
1527 store
= n_string_push_buf(store
, input
->s
, PTR2SIZE(ib
- input
->s
));
1528 rv
|= n_SHEXP_STATE_OUTPUT
;
1531 if(state
& a_CONSUME
){
1532 input
->s
= n_UNCONST(&ib
[il
]);
1535 if(flags
& n_SHEXP_PARSE_TRIMSPACE
){
1536 for(; il
> 0; ++ib
, --il
)
1541 input
->s
= n_UNCONST(ib
);
1544 if(!(rv
& n_SHEXP_STATE_STOP
)){
1545 if(!(rv
& n_SHEXP_STATE_OUTPUT
) && (flags
& n_SHEXP_PARSE_IGNORE_EMPTY
) &&
1547 goto jrestart_empty
;
1548 if(/*!(rv & n_SHEXP_STATE_OUTPUT) &&*/ il
== 0)
1549 rv
|= n_SHEXP_STATE_STOP
;
1552 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_UNICODE
));
1553 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_CONTROL
));
1558 FL
struct n_string
*
1559 n_shexp_quote(struct n_string
*store
, struct str
const *input
, bool_t rndtrip
){
1560 struct a_shexp_quote_lvl sql
;
1561 struct a_shexp_quote_ctx sqc
;
1564 assert(store
!= NULL
);
1565 assert(input
!= NULL
);
1566 assert(input
->l
== 0 || input
->s
!= NULL
);
1568 memset(&sqc
, 0, sizeof sqc
);
1569 sqc
.sqc_store
= store
;
1570 sqc
.sqc_input
.s
= input
->s
;
1571 if((sqc
.sqc_input
.l
= input
->l
) == UIZ_MAX
)
1572 sqc
.sqc_input
.l
= strlen(input
->s
);
1573 sqc
.sqc_flags
= rndtrip
? a_SHEXP_QUOTE_ROUNDTRIP
: a_SHEXP_QUOTE_NONE
;
1575 if(sqc
.sqc_input
.l
== 0)
1576 store
= n_string_push_buf(store
, "''", sizeof("''") -1);
1578 memset(&sql
, 0, sizeof sql
);
1579 sql
.sql_dat
= sqc
.sqc_input
;
1580 sql
.sql_flags
= sqc
.sqc_flags
;
1581 a_shexp__quote(&sqc
, &sql
);
1588 n_shexp_quote_cp(char const *cp
, bool_t rndtrip
){
1589 struct n_string store
;
1596 input
.s
= n_UNCONST(cp
);
1598 rv
= n_string_cp(n_shexp_quote(n_string_creat_auto(&store
), &input
,
1600 n_string_gut(n_string_drop_ownership(&store
));
1606 n_shexp_is_valid_varname(char const *name
){
1611 for(rv
= TRU1
; (c
= *name
++) != '\0';)
1612 if(!a_SHEXP_ISVARC(c
)){