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-minus "-" (except in last position for ${x[:]-y}).
60 * We support some special parameter names for one-letter(++) variable names;
61 * these have counterparts in the code that manages internal variables,
62 * and some more special treatment below! */
63 #define a_SHEXP_ISVARC(C) (alnumchar(C) || (C) == '_' || (C) == '-')
64 #define a_SHEXP_ISVARC_BAD1ST(C) (digitchar(C)) /* (Actually assumed below!) */
65 #define a_SHEXP_ISVARC_BADNST(C) ((C) == '-')
67 enum a_shexp_quote_flags
{
69 a_SHEXP_QUOTE_ROUNDTRIP
= 1u<<0, /* Result won't be consumed immediately */
71 a_SHEXP_QUOTE_T_REVSOL
= 1u<<8, /* Type: by reverse solidus */
72 a_SHEXP_QUOTE_T_SINGLE
= 1u<<9, /* Type: single-quotes */
73 a_SHEXP_QUOTE_T_DOUBLE
= 1u<<10, /* Type: double-quotes */
74 a_SHEXP_QUOTE_T_DOLLAR
= 1u<<11, /* Type: dollar-single-quotes */
75 a_SHEXP_QUOTE_T_MASK
= a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
76 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
,
78 a_SHEXP_QUOTE__FREESHIFT
= 16u
82 struct a_shexp_glob_ctx
{
83 char const *sgc_patdat
; /* Remaining pattern (at and below level) */
85 struct n_string
*sgc_outer
; /* Resolved path up to this level */
91 struct a_shexp_quote_ctx
{
92 struct n_string
*sqc_store
; /* Result storage */
93 struct str sqc_input
; /* Input data, topmost level */
95 ui32_t sqc_cnt_single
;
96 ui32_t sqc_cnt_double
;
97 ui32_t sqc_cnt_dollar
;
98 enum a_shexp_quote_flags sqc_flags
;
102 struct a_shexp_quote_lvl
{
103 struct a_shexp_quote_lvl
*sql_link
; /* Outer level */
104 struct str sql_dat
; /* This level (has to) handle(d) */
105 enum a_shexp_quote_flags sql_flags
;
109 /* Locate the user's mailbox file (where new, unread mail is queued) */
110 static char *a_shexp_findmail(char const *user
, bool_t force
);
112 /* Expand ^~/? and ^~USER/? constructs.
113 * Returns the completely resolved (maybe empty or identical to input)
114 * salloc()ed string */
115 static char *a_shexp_tilde(char const *s
);
117 /* Perform fnmatch(3). May return NULL on error */
118 static char *a_shexp_globname(char const *name
, enum fexp_mode fexpm
);
120 static bool_t
a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
,
121 struct n_strlist
**slpp
);
122 static int a_shexp__globsort(void const *cvpa
, void const *cvpb
);
125 /* Parse an input string and create a sh(1)ell-quoted result */
126 static void a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
,
127 struct a_shexp_quote_lvl
*sqlp
);
130 a_shexp_findmail(char const *user
, bool_t force
){
136 if((cp
= ok_vlook(inbox
)) != NULL
&& *cp
!= '\0'){
137 /* Folder extra introduced to avoid % recursion loops */
138 if((rv
= fexpand(cp
, FEXP_NSPECIAL
| FEXP_NFOLDER
| FEXP_NSHELL
)
141 n_err(_("*inbox* expansion failed, using $MAIL / builtin: %s\n"), cp
);
144 if((cp
= ok_vlook(MAIL
)) != NULL
){
153 ul
= strlen(user
) +1;
154 i
= sizeof(VAL_MAIL
) -1 + 1 + ul
;
157 memcpy(rv
, VAL_MAIL
, (i
= sizeof(VAL_MAIL
) -1));
159 memcpy(&rv
[++i
], user
, ul
);
167 a_shexp_tilde(char const *s
){
174 if(*(rp
= &s
[1]) == '/' || *rp
== '\0'){
178 if((rp
= strchr(np
= rp
, '/')) != NULL
){
179 nl
= PTR2SIZE(rp
- np
);
180 np
= savestrbuf(np
, nl
);
185 if((pwp
= getpwnam(np
)) == NULL
){
193 rv
= salloc(nl
+ 1 + rl
+1);
196 memcpy(rv
+ nl
, rp
, rl
);
206 a_shexp_globname(char const *name
, enum fexp_mode fexpm
){
208 struct a_shexp_glob_ctx sgc
;
209 struct n_string outer
;
210 struct n_strlist
*slp
;
214 memset(&sgc
, 0, sizeof sgc
);
215 sgc
.sgc_patlen
= strlen(name
);
216 sgc
.sgc_patdat
= savestrbuf(name
, sgc
.sgc_patlen
);
217 sgc
.sgc_outer
= n_string_reserve(n_string_creat(&outer
), sgc
.sgc_patlen
);
218 sgc
.sgc_flags
= ((fexpm
& FEXP_SILENT
) != 0);
220 if(a_shexp__glob(&sgc
, &slp
))
224 n_string_gut(&outer
);
230 cp
= n_UNCONST(N_("File pattern does not match"));
232 }else if(slp
->sl_next
== NULL
)
233 cp
= savestrbuf(slp
->sl_dat
, slp
->sl_len
);
234 else if(fexpm
& FEXP_MULTIOK
){
235 struct n_strlist
**sorta
, *xslp
;
239 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
){
241 l
+= xslp
->sl_len
+ 1;
244 sorta
= smalloc(sizeof(*sorta
) * no
);
246 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
)
248 qsort(sorta
, no
, sizeof *sorta
, &a_shexp__globsort
);
252 for(i
= 0; i
< no
; ++i
){
254 memcpy(&cp
[l
], xslp
->sl_dat
, xslp
->sl_len
);
261 n_pstate
|= n_PS_EXPAND_MULTIRESULT
;
263 cp
= n_UNCONST(N_("File pattern matches multiple results"));
269 struct n_strlist
*tmp
= slp
;
278 if(!(fexpm
& FEXP_SILENT
)){
279 name
= n_shexp_quote_cp(name
, FAL0
);
280 n_err("%s: %s\n", V_(cp
), name
);
285 #else /* HAVE_FNMATCH */
288 if(!(fexpm
& FEXP_SILENT
))
289 n_err(_("No filename pattern (fnmatch(3)) support compiled in\n"));
290 return savestr(name
);
296 a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
, struct n_strlist
**slpp
){
297 enum{a_SILENT
= 1<<0, a_DEEP
=1<<1, a_SALLOC
=1<<2};
299 struct a_shexp_glob_ctx nsgc
;
303 char const *ccp
, *myp
;
306 /* We need some special treatment for the outermost level */
307 if(!(sgcp
->sgc_flags
& a_DEEP
)){
308 if(sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/'){
309 myp
= n_string_cp(n_string_push_c(sgcp
->sgc_outer
, '/'));
315 myp
= n_string_cp(sgcp
->sgc_outer
);
316 old_outerlen
= sgcp
->sgc_outer
->s_len
;
318 /* Separate current directory/pattern level from any possible remaining
319 * pattern in order to be able to use it for fnmatch(3) */
320 if((ccp
= memchr(sgcp
->sgc_patdat
, '/', sgcp
->sgc_patlen
)) == NULL
)
324 nsgc
.sgc_flags
|= a_DEEP
;
325 sgcp
->sgc_patlen
= PTR2SIZE((nsgc
.sgc_patdat
= &ccp
[1]) -
326 &sgcp
->sgc_patdat
[0]);
327 nsgc
.sgc_patlen
-= sgcp
->sgc_patlen
;
329 if(sgcp
->sgc_patlen
> 0){
330 assert(sgcp
->sgc_patdat
[sgcp
->sgc_patlen
-1] == '/');
331 ((char*)n_UNCONST(sgcp
->sgc_patdat
))[--sgcp
->sgc_patlen
] = '\0';
335 /* Our current directory level */
336 /* xxx Plenty of room for optimizations, like quickshot lstat(2) which may
337 * xxx be the (sole) result depending on pattern surroundings, etc. */
338 if((dp
= opendir(myp
)) == NULL
){
341 switch((err
= n_err_no
)){
343 ccp
= N_("cannot access paths under non-directory");
346 ccp
= N_("path component of (sub)pattern non-existent");
349 ccp
= N_("file permission for file (sub)pattern denied");
352 ccp
= N_("cannot handle file (sub)pattern");
357 /* As necessary, quote bytes in the current pattern */
363 for(need
= FAL0
, i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
365 case '\'': case '"': case '\\': case '$':
377 for(i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
379 case '\'': case '"': case '\\': case '$':
390 myp
= sgcp
->sgc_patdat
;
393 while((dep
= readdir(dp
)) != NULL
){
394 switch(fnmatch(myp
, dep
->d_name
, FNM_PATHNAME
| FNM_PERIOD
)){
396 /* A match expresses the desire to recurse if there is more pattern */
397 if(nsgc
.sgc_patlen
> 0){
400 n_string_push_cp((sgcp
->sgc_outer
->s_len
> 1
401 ? n_string_push_c(sgcp
->sgc_outer
, '/') : sgcp
->sgc_outer
),
405 #ifdef HAVE_DIRENT_TYPE
406 if(dep
->d_type
== DT_DIR
)
408 else if(dep
->d_type
== DT_LNK
|| dep
->d_type
== DT_UNKNOWN
)
413 if(stat(n_string_cp(sgcp
->sgc_outer
), &sb
)){
414 ccp
= N_("I/O error when querying file status");
416 }else if(S_ISDIR(sb
.st_mode
))
420 /* TODO We recurse with current dir FD open, which could E[MN]FILE!
421 * TODO Instead save away a list of such n_string's for later */
422 if(isdir
&& !a_shexp__glob(&nsgc
, slpp
)){
427 n_string_trunc(sgcp
->sgc_outer
, old_outerlen
);
429 struct n_strlist
*slp
;
432 i
= strlen(dep
->d_name
);
433 j
= (old_outerlen
> 0) ? old_outerlen
+ 1 + i
: i
;
434 slp
= n_STRLIST_MALLOC(j
);
436 slpp
= &slp
->sl_next
;
438 if((j
= old_outerlen
) > 0){
439 memcpy(&slp
->sl_dat
[0], sgcp
->sgc_outer
->s_dat
, j
);
440 if(slp
->sl_dat
[j
-1] != '/')
441 slp
->sl_dat
[j
++] = '/';
443 memcpy(&slp
->sl_dat
[j
], dep
->d_name
, i
);
444 slp
->sl_dat
[j
+= i
] = '\0';
451 ccp
= N_("fnmatch(3) cannot handle file (sub)pattern");
461 return (ccp
== NULL
);
464 if(!(sgcp
->sgc_flags
& a_SILENT
)){
467 if(sgcp
->sgc_outer
->s_len
> 0){
468 s2
= n_shexp_quote_cp(n_string_cp(sgcp
->sgc_outer
), FAL0
);
473 n_err("%s: %s%s%s\n", V_(ccp
), s2
, s3
,
474 n_shexp_quote_cp(sgcp
->sgc_patdat
, FAL0
));
480 a_shexp__globsort(void const *cvpa
, void const *cvpb
){
482 struct n_strlist
const * const *slpa
, * const *slpb
;
487 rv
= asccasecmp((*slpa
)->sl_dat
, (*slpb
)->sl_dat
);
491 #endif /* HAVE_FNMATCH */
494 a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
, struct a_shexp_quote_lvl
*sqlp
){
495 /* XXX Because of the problems caused by ISO C multibyte interface we cannot
496 * XXX use the recursive implementation because of stateful encodings.
497 * XXX I.e., if a quoted substring cannot be self-contained - the data after
498 * XXX the quote relies on "the former state", then this doesn't make sense.
499 * XXX Therefore this is not fully programmed out but instead only detects
500 * XXX the "most fancy" quoting necessary, and directly does that.
501 * XXX As a result of this, T_REVSOL and T_DOUBLE are not even considered.
502 * XXX Otherwise we rather have to convert to wide first and act on that,
503 * XXX e.g., call visual_info(n_VISUAL_INFO_WOUT_CREATE) on entire input */
504 #undef a_SHEXP_QUOTE_RECURSE /* XXX (Needs complete revisit, then) */
505 #ifdef a_SHEXP_QUOTE_RECURSE
506 # define jrecurse jrecurse
507 struct a_shexp_quote_lvl sql
;
509 # define jrecurse jstep
511 struct n_visual_info_ctx vic
;
512 union {struct a_shexp_quote_lvl
*head
; struct n_string
*store
;} u
;
515 char const *ib
, *ib_base
;
518 ib_base
= ib
= sqlp
->sql_dat
.s
;
519 il
= sqlp
->sql_dat
.l
;
520 flags
= sqlp
->sql_flags
;
522 /* Iterate over the entire input, classify characters and type of quotes
523 * along the way. Whenever a quote change has to be applied, adjust flags
524 * for the new situation -, setup sql.* and recurse- */
530 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
532 if(c
== '\t' && (flags
& (a_SHEXP_QUOTE_T_REVSOL
|
533 a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOUBLE
)))
535 #ifdef a_SHEXP_QUOTE_RECURSE
536 ++sqcp
->sqc_cnt_dollar
;
538 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
540 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
541 /* Whereas we don't support those, quote them for the sh(1)ell */
542 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
543 c
== '"' || c
== '$'){
544 if(flags
& a_SHEXP_QUOTE_T_MASK
)
546 #ifdef a_SHEXP_QUOTE_RECURSE
547 ++sqcp
->sqc_cnt_single
;
549 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
552 if(flags
& (a_SHEXP_QUOTE_T_MASK
& ~a_SHEXP_QUOTE_T_SINGLE
))
554 #ifdef a_SHEXP_QUOTE_RECURSE
555 ++sqcp
->sqc_cnt_dollar
;
557 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
559 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
560 if(flags
& a_SHEXP_QUOTE_T_MASK
)
562 #ifdef a_SHEXP_QUOTE_RECURSE
563 ++sqcp
->sqc_cnt_single
;
565 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
567 }else if(!asciichar(c
)){
568 /* Need to keep together multibytes */
569 #ifdef a_SHEXP_QUOTE_RECURSE
570 memset(&vic
, 0, sizeof vic
);
574 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
576 /* xxx check whether resulting \u would be ASCII */
577 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
) ||
578 (flags
& a_SHEXP_QUOTE_T_DOLLAR
)){
579 #ifdef a_SHEXP_QUOTE_RECURSE
587 #ifdef a_SHEXP_QUOTE_RECURSE
588 ++sqcp
->sqc_cnt_dollar
;
590 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
596 sqlp
->sql_flags
= flags
;
598 /* Level made the great and completed processing input. Reverse the list of
599 * levels, detect the "most fancy" quote type needed along this way */
600 /* XXX Due to restriction as above very crude */
601 for(flags
= 0, il
= 0, u
.head
= NULL
; sqlp
!= NULL
;){
602 struct a_shexp_quote_lvl
*tmp
;
604 tmp
= sqlp
->sql_link
;
605 sqlp
->sql_link
= u
.head
;
607 il
+= sqlp
->sql_dat
.l
;
608 if(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
)
609 il
+= (sqlp
->sql_dat
.l
>> 1);
610 flags
|= sqlp
->sql_flags
;
615 /* Finally work the substrings in the correct order, adjusting quotes along
616 * the way as necessary. Start off with the "most fancy" quote, so that
617 * the user sees an overall boundary she can orientate herself on.
618 * We do it like that to be able to give the user some "encapsulation
619 * experience", to address what strikes me is a problem of sh(1)ell quoting:
620 * different to, e.g., perl(1), where you see at a glance where a string
621 * starts and ends, sh(1) quoting occurs at the "top level", disrupting the
622 * visual appearance of "a string" as such */
623 u
.store
= n_string_reserve(sqcp
->sqc_store
, il
);
625 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
626 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
627 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
628 }else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
){
629 u
.store
= n_string_push_c(u
.store
, '"');
630 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOUBLE
;
631 }else if(flags
& a_SHEXP_QUOTE_T_SINGLE
){
632 u
.store
= n_string_push_c(u
.store
, '\'');
633 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
634 }else /*if(flags & a_SHEXP_QUOTE_T_REVSOL)*/
635 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
637 /* Work all the levels */
638 for(; sqlp
!= NULL
; sqlp
= sqlp
->sql_link
){
639 /* As necessary update our mode of quoting */
640 #ifdef a_SHEXP_QUOTE_RECURSE
643 switch(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
){
644 case a_SHEXP_QUOTE_T_DOLLAR
:
645 if(!(flags
& a_SHEXP_QUOTE_T_DOLLAR
))
646 il
= a_SHEXP_QUOTE_T_DOLLAR
;
648 case a_SHEXP_QUOTE_T_DOUBLE
:
649 if(!(flags
& (a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
650 il
= a_SHEXP_QUOTE_T_DOLLAR
;
652 case a_SHEXP_QUOTE_T_SINGLE
:
653 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
654 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
655 il
= a_SHEXP_QUOTE_T_SINGLE
;
658 case a_SHEXP_QUOTE_T_REVSOL
:
659 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
660 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
661 il
= a_SHEXP_QUOTE_T_REVSOL
;
666 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
667 u
.store
= n_string_push_c(u
.store
, '\'');
668 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
669 u
.store
= n_string_push_c(u
.store
, '"');
670 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
673 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
674 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
675 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
676 u
.store
= n_string_push_c(u
.store
, '"');
677 else if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
678 u
.store
= n_string_push_c(u
.store
, '\'');
680 #endif /* a_SHEXP_QUOTE_RECURSE */
682 /* Work the level's substring */
683 ib
= sqlp
->sql_dat
.s
;
684 il
= sqlp
->sql_dat
.l
;
692 assert(c
== '\t' || (flags
& a_SHEXP_QUOTE_T_DOLLAR
));
693 assert((flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
694 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)));
696 case 0x07: c
= 'a'; break;
697 case 0x08: c
= 'b'; break;
698 case 0x0A: c
= 'n'; break;
699 case 0x0B: c
= 'v'; break;
700 case 0x0C: c
= 'f'; break;
701 case 0x0D: c
= 'r'; break;
702 case 0x1B: c
= 'E'; break;
705 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
709 if(flags
& a_SHEXP_QUOTE_T_REVSOL
)
710 u
.store
= n_string_push_c(u
.store
, '\\');
713 u
.store
= n_string_push_c(u
.store
, '\\');
715 u
.store
= n_string_push_c(u
.store
, 'c');
719 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
720 /* Whereas we don't support those, quote them for the sh(1)ell */
721 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
722 c
== '"' || c
== '$'){
723 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
725 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
));
726 u
.store
= n_string_push_c(u
.store
, '\\');
729 if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
731 assert(!(flags
& a_SHEXP_QUOTE_T_SINGLE
));
732 u
.store
= n_string_push_c(u
.store
, '\\');
734 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
735 if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
737 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
|
738 a_SHEXP_QUOTE_T_DOLLAR
));
739 u
.store
= n_string_push_c(u
.store
, '\\');
741 }else if(asciichar(c
)){
742 /* Shorthand: we can simply push that thing out */
744 u
.store
= n_string_push_c(u
.store
, c
);
747 /* Not an ASCII character, take care not to split up multibyte
748 * sequences etc. For the sake of compile testing, don't enwrap in
749 * HAVE_ALWAYS_UNICODE_LOCALE || HAVE_NATCH_CHAR */
750 if(n_psonce
& n_PSO_UNICODE
){
757 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
761 il2
= PTR2SIZE(&ib2
[0] - &ib
[0]);
762 if((flags
& a_SHEXP_QUOTE_ROUNDTRIP
) || uc
== 0xFFFDu
){
763 /* Use padding to make ambiguities impossible */
764 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
765 (uc
> 0xFFFFu
? 'U' : 'u'),
766 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
772 u
.store
= n_string_push_buf(u
.store
, cp
, il3
);
773 ib
+= il2
, il
-= il2
;
778 memset(&vic
, 0, sizeof vic
);
782 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
784 /* Work this substring as sensitive as possible */
786 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
))
787 u
.store
= n_string_push_buf(u
.store
, ib
, il
);
789 else if((vic
.vic_indat
= n_iconv_onetime_cp(n_ICONV_NONE
,
790 "utf-8", ok_vlook(ttycharset
), savestrbuf(ib
, il
))) != NULL
){
795 il3
= il2
= strlen(ib2
= vic
.vic_indat
);
796 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
799 il2
= PTR2SIZE(&ib2
[0] - &vic
.vic_indat
[0]);
800 /* Use padding to make ambiguities impossible */
801 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
802 (uc
> 0xFFFFu
? 'U' : 'u'),
803 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
804 u
.store
= n_string_push_buf(u
.store
, itoa
, il3
);
814 u
.store
= n_string_push_buf(u
.store
, "\\xFF",
816 n_c_to_hex_base16(&u
.store
->s_dat
[u
.store
->s_len
- 2], *ib
++);
825 /* Close an open quote */
826 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
827 u
.store
= n_string_push_c(u
.store
, '\'');
828 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
829 u
.store
= n_string_push_c(u
.store
, '"');
830 #ifdef a_SHEXP_QUOTE_RECURSE
836 #ifdef a_SHEXP_QUOTE_RECURSE
838 sqlp
->sql_dat
.l
-= il
;
841 sql
.sql_dat
.s
= n_UNCONST(ib
);
843 sql
.sql_flags
= flags
;
844 a_shexp__quote(sqcp
, &sql
);
849 #undef a_SHEXP_QUOTE_RECURSE
853 fexpand(char const *name
, enum fexp_mode fexpm
)
856 char const *cp
, *res
;
860 n_pstate
&= ~n_PS_EXPAND_MULTIRESULT
;
862 /* The order of evaluation is "%" and "#" expand into constants.
863 * "&" can expand into "+". "+" can expand into shell meta characters.
864 * Shell meta characters expand into constants.
865 * This way, we make no recursive expansion */
866 if ((fexpm
& FEXP_NSHORTCUT
) || (res
= shortcut_expand(name
)) == NULL
)
867 res
= n_UNCONST(name
);
869 if(!(fexpm
& FEXP_NSPECIAL
)){
874 if(res
[1] == ':' && res
[2] != '\0')
879 force
= (res
[1] != '\0');
880 res
= a_shexp_findmail((force
? &res
[1] : ok_vlook(LOGNAME
)),
889 if (prevfile
[0] == '\0') {
890 n_err(_("No previous file\n"));
898 res
= ok_vlook(MBOX
);
903 /* POSIX: if *folder* unset or null, "+" shall be retained */
904 if (!(fexpm
& FEXP_NFOLDER
) && *res
== '+' &&
905 *(cp
= folder_query()) != '\0') {
906 res
= str_concat_csvl(&s
, cp
, &res
[1], NULL
)->s
;
909 /* TODO *folder* can't start with %[:], can it!?! */
910 if (res
[0] == '%' && res
[1] == ':') {
916 /* Do some meta expansions */
917 if((fexpm
& (FEXP_NSHELL
| FEXP_NVAR
)) != FEXP_NVAR
&&
918 ((fexpm
& FEXP_NSHELL
) ? (strchr(res
, '$') != NULL
)
919 : anyof(res
, "{}[]*?$"))){
922 if(fexpm
& FEXP_NOPROTO
)
924 else switch(which_protocol(res
)){
936 struct n_string shou
, *shoup
;
938 shin
.s
= n_UNCONST(res
);
940 shoup
= n_string_creat_auto(&shou
);
942 enum n_shexp_state shs
;
944 /* TODO shexp: take care to not include backtick eval once avail! */
945 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG_D_V
|
946 n_SHEXP_PARSE_QUOTE_AUTO_FIXED
| n_SHEXP_PARSE_QUOTE_AUTO_DQ
|
947 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
), shoup
, &shin
, NULL
);
948 if(shs
& n_SHEXP_STATE_STOP
)
951 res
= n_string_cp(shoup
);
952 shoup
= n_string_drop_ownership(shoup
);
956 res
= a_shexp_tilde(res
);
958 if(!(fexpm
& FEXP_NSHELL
) &&
959 (res
= a_shexp_globname(res
, fexpm
)) == NULL
)
963 }else if(res
[0] == '~'){
964 res
= a_shexp_tilde(res
);
969 if (fexpm
& FEXP_LOCAL
)
970 switch (which_protocol(res
)) {
975 n_err(_("Not a local file or directory: %s\n"),
976 n_shexp_quote_cp(name
, FAL0
));
982 if(res
!= NULL
&& !dyn
)
985 return n_UNCONST(res
);
988 FL
enum n_shexp_state
989 n_shexp_parse_token(enum n_shexp_parse_flags flags
, struct n_string
*store
,
990 struct str
*input
, void const **cookie
){
991 /* TODO shexp_parse_token: WCHAR; $IFS (sp20=' '; echo a $sp20 b; ..) */
992 char c2
, c
, quotec
, utf
[8];
993 enum n_shexp_state rv
;
995 char const *ib_save
, *ib
;
998 a_SKIPQ
= 1u<<0, /* Skip rest of this quote (\c0 ..) */
999 a_SURPLUS
= 1u<<1, /* Extended sequence interpretation */
1000 a_NTOKEN
= 1u<<2, /* "New token": e.g., comments are possible */
1001 a_BRACE
= 1u<<3, /* Variable substitution: brace enclosed */
1002 a_DIGIT1
= 1u<<4, /* ..first character was digit */
1003 a_NONDIGIT
= 1u<<5, /* ..has seen any non-digits */
1004 a_VARSUBST_MASK
= n_BITENUM_MASK(3, 5),
1006 a_ROUND_MASK
= (int)~n_BITENUM_MASK(0, 7),
1009 a_CONSUME
= 1u<<10, /* When done, "consume" remaining input */
1014 assert((flags
& n_SHEXP_PARSE_DRYRUN
) || store
!= NULL
);
1015 assert(input
!= NULL
);
1016 assert(input
->l
== 0 || input
->s
!= NULL
);
1017 assert(!(flags
& n_SHEXP_PARSE_LOG
) || !(flags
& n_SHEXP_PARSE_LOG_D_V
));
1018 assert(!(flags
& n_SHEXP_PARSE_IFS_ADD_COMMA
) ||
1019 !(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
));
1020 assert(!(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
) ||
1021 (flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
));
1023 if((flags
& n_SHEXP_PARSE_LOG_D_V
) && (n_poption
& n_PO_D_V
))
1024 flags
|= n_SHEXP_PARSE_LOG
;
1025 if(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)
1026 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
;
1028 if((flags
& n_SHEXP_PARSE_TRUNC
) && store
!= NULL
)
1029 store
= n_string_trunc(store
, 0);
1033 if((il
= input
->l
) == UIZ_MAX
)
1034 input
->l
= il
= strlen(ib
);
1037 if(cookie
!= NULL
&& *cookie
!= NULL
){
1038 assert(!(flags
& n_SHEXP_PARSE_DRYRUN
));
1043 rv
= n_SHEXP_STATE_NONE
;
1044 state
&= a_ROUND_MASK
;
1046 /* In cookie mode, the next ARGV entry is the token already, unchanged,
1047 * since it has already been expanded before! */
1048 if(state
& a_COOKIE
){
1049 char const * const *xcookie
, *cp
;
1053 if((store
= n_string_push_cp(store
, *xcookie
))->s_len
> 0)
1054 rv
|= n_SHEXP_STATE_OUTPUT
;
1055 if(*++xcookie
== NULL
){
1058 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_DQ
; /* ..why we are here! */
1060 *cookie
= n_UNCONST(xcookie
);
1062 for(cp
= &n_string_cp(store
)[i
]; (c
= *cp
++) != '\0';)
1064 rv
|= n_SHEXP_STATE_CONTROL
;
1068 /* The last exploded cookie will join with the yielded input token, so
1069 * simply fall through in this case */
1070 if(state
& a_COOKIE
)
1073 if(flags
& n_SHEXP_PARSE_TRIMSPACE
){
1074 for(; il
> 0; ++ib
, --il
)
1075 if(!blankspacechar(*ib
))
1078 input
->s
= n_UNCONST(ib
);
1083 rv
|= n_SHEXP_STATE_STOP
;
1088 store
= n_string_reserve(store
, n_MIN(il
, 32)); /* XXX */
1090 switch(flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
){
1091 case n_SHEXP_PARSE_QUOTE_AUTO_SQ
:
1094 case n_SHEXP_PARSE_QUOTE_AUTO_DQ
:
1097 case n_SHEXP_PARSE_QUOTE_AUTO_DSQ
:
1111 /* If no quote-mode active.. */
1113 if(c
== '"' || c
== '\''){
1118 state
&= ~a_SURPLUS
;
1132 }else if(c
== '\\'){
1133 /* Outside of quotes this just escapes any next character, but a sole
1134 * <reverse solidus> at EOS is left unchanged */
1139 /* A comment may it be if no token has yet started */
1140 else if(c
== '#' && (state
& a_NTOKEN
)){
1141 rv
|= n_SHEXP_STATE_STOP
;
1144 /* Metacharacters which separate tokens must be turned on explicitly */
1146 rv
|= n_SHEXP_STATE_META_VERTBAR
;
1147 /* The parsed sequence may be _the_ output, so ensure we don't
1148 * include the metacharacter, then. */
1149 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1151 /*else if(flags & n_SHEXP_PARSE_META_VERTBAR)*/
1154 rv
|= n_SHEXP_STATE_META_AMPERSAND
;
1155 /* The parsed sequence may be _the_ output, so ensure we don't
1156 * include the metacharacter, then. */
1157 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1159 /*else if(flags & n_SHEXP_PARSE_META_AMPERSAND)*/
1162 rv
|= n_SHEXP_STATE_META_SEMICOLON
;
1163 /* The parsed sequence may be _the_ output, so ensure we don't
1164 * include the metacharacter, then. */
1165 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1167 else if(flags
& n_SHEXP_PARSE_META_SEMICOLON
){
1169 n_go_input_inject(n_GO_INPUT_INJECT_COMMIT
, ib
, il
);
1171 rv
|= n_SHEXP_STATE_STOP
;
1174 }else if(c
== ',' && (flags
&
1175 (n_SHEXP_PARSE_IFS_ADD_COMMA
| 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
)
1181 }else if(blankchar(c
)){
1182 if(!(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
)){
1183 /* The parsed sequence may be _the_ output, so ensure we don't
1184 * include the metacharacter, then. */
1185 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1194 assert(!(state
& a_NTOKEN
));
1195 if(c
== quotec
&& !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)){
1196 state
&= a_ROUND_MASK
;
1198 /* Users may need to recognize the presence of empty quotes */
1199 rv
|= n_SHEXP_STATE_OUTPUT
;
1201 }else if(c
== '\\' && (state
& a_SURPLUS
)){
1203 /* A sole <reverse solidus> at EOS is treated as-is! This is ok
1204 * since the "closing quote" error will occur next, anyway */
1207 else if((c2
= *ib
) == quotec
){
1210 }else if(quotec
== '"'){
1211 /* Double quotes, POSIX says:
1212 * The <backslash> shall retain its special meaning as an
1213 * escape character (see Section 2.2.1) only when followed
1214 * by one of the following characters when considered
1215 * special: $ ` " \ <newline> */
1219 /* case '"': already handled via c2 == quotec */
1228 /* Dollar-single-quote */
1232 /* case '\'': already handled via c2 == quotec */
1237 case 'b': c
= '\b'; break;
1238 case 'f': c
= '\f'; break;
1239 case 'n': c
= '\n'; break;
1240 case 'r': c
= '\r'; break;
1241 case 't': c
= '\t'; break;
1242 case 'v': c
= '\v'; break;
1245 case 'e': c
= '\033'; break;
1247 /* Control character */
1250 goto j_dollar_ungetc
;
1254 c
= upperconv(c2
) ^ 0x40;
1255 if((ui8_t
)c
> 0x1F && c
!= 0x7F){ /* ASCII C0: 0..1F, 7F */
1256 if(flags
& n_SHEXP_PARSE_LOG
)
1257 n_err(_("Invalid \\c notation: %.*s\n"),
1258 (int)input
->l
, input
->s
);
1259 rv
|= n_SHEXP_STATE_ERR_CONTROL
;
1261 /* As an implementation-defined extension, support \c@
1262 * EQ printf(1) alike \c */
1264 rv
|= n_SHEXP_STATE_STOP
;
1269 /* Octal sequence: 1 to 3 octal bytes */
1271 /* As an extension (dependent on where you look, echo(1), or
1272 * awk(1)/tr(1) etc.), allow leading "0" octal indicator */
1273 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1278 case '1': case '2': case '3':
1279 case '4': case '5': case '6': case '7':
1281 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1282 c2
= (c2
<< 3) | (c
- '0');
1285 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1286 if((ui8_t
)c2
> 0x1F){
1287 if(flags
& n_SHEXP_PARSE_LOG
)
1288 n_err(_("\\0 argument exceeds a byte: %.*s\n"),
1289 (int)input
->l
, input
->s
);
1290 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1292 /* Write unchanged */
1294 rv
|= n_SHEXP_STATE_OUTPUT
;
1295 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1296 store
= n_string_push_buf(store
, ib_save
,
1297 PTR2SIZE(ib
- ib_save
));
1300 c2
= (c2
<< 3) | (c
-= '0');
1303 if((c
= c2
) == '\0')
1309 /* ISO 10646 / Unicode sequence, 8 or 4 hexadecimal bytes */
1318 goto j_dollar_ungetc
;
1322 /* Hexadecimal sequence, 1 or 2 hexadecimal bytes */
1326 goto j_dollar_ungetc
;
1330 static ui8_t
const hexatoi
[] = { /* XXX uses ASCII */
1331 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1336 for(no
= j
= 0; i
-- > 0; --il
, ++ib
, ++j
){
1340 no
+= hexatoi
[(ui8_t
)((c
) - ((c
) <= '9' ? 48
1341 : ((c
) <= 'F' ? 55 : 87)))];
1345 c2
= (c2
== 'U' || c2
== 'u') ? 'u' : 'x';
1346 if(flags
& n_SHEXP_PARSE_LOG
)
1347 n_err(_("Invalid \\%c notation: %.*s\n"),
1348 c2
, (int)input
->l
, input
->s
);
1349 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1355 /* Unicode massage */
1356 if((c2
!= 'U' && c2
!= 'u') || n_uasciichar(no
)){
1357 if((c
= (char)no
) == '\0')
1361 else if(!(state
& a_SKIPQ
)){
1362 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1363 store
= n_string_reserve(store
, n_MAX(j
, 4));
1366 if(no
> 0x10FFFF){ /* XXX magic; CText */
1367 if(flags
& n_SHEXP_PARSE_LOG
)
1368 n_err(_("\\U argument exceeds 0x10FFFF: %.*s\n"),
1369 (int)input
->l
, input
->s
);
1370 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1371 /* But normalize the output anyway */
1375 j
= n_utf32_to_utf8(no
, utf
);
1377 if(n_psonce
& n_PSO_UNICODE
){
1378 rv
|= n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_UNICODE
;
1379 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1380 store
= n_string_push_buf(store
, utf
, j
);
1387 icp
= n_iconv_onetime_cp(n_ICONV_NONE
,
1390 rv
|= n_SHEXP_STATE_OUTPUT
;
1391 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1392 store
= n_string_push_cp(store
, icp
);
1397 if(!(flags
& n_SHEXP_PARSE_DRYRUN
)) Je_uni_norm
:{
1400 rv
|= n_SHEXP_STATE_OUTPUT
|
1401 n_SHEXP_STATE_ERR_UNICODE
;
1402 i
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
1403 (no
> 0xFFFFu
? 'U' : 'u'),
1404 (int)(no
> 0xFFFFu
? 8 : 4), (ui32_t
)no
);
1405 store
= n_string_push_buf(store
, itoa
, i
);
1414 /* Extension: \$ can be used to expand a variable.
1415 * Bug|ad effect: if conversion fails, not written "as-is" */
1418 goto j_dollar_ungetc
;
1423 /* Follow bash(1) behaviour, print sequence unchanged */
1428 }else if(c
== '$' && quotec
== '"' && il
> 0) J_var_expand
:{
1429 state
&= ~a_VARSUBST_MASK
;
1433 if(!(state
& a_BRACE
) || il
> 1){
1434 char const *cp
, *vp
;
1440 state
&= ~a_EXPLODE
;
1442 for(i
= 0; il
> 0; --il
, ++ib
, ++i
){
1443 /* We have some special cases regarding macro-local special
1444 * parameters, so ensure these don't cause failure.
1445 * This has counterparts in the code that manages internal
1448 if(!a_SHEXP_ISVARC(c
)){
1449 if(i
== 0 && (c
== '*' || c
== '@' || c
== '#' ||
1450 c
== '?' || c
== '!' || c
== '^')){
1451 /* Skip over multiplexer */
1462 }else if(a_SHEXP_ISVARC_BAD1ST(c
)){
1466 state
|= a_NONDIGIT
;
1469 if(state
& a_SKIPQ
){
1470 if((state
& a_BRACE
) && il
> 0 && *ib
== '}')
1475 /* XXX Digit in first place is not supported, however we do
1476 * XXX support all digits because these refer to macro-local
1477 * XXX variables; if we would have a notion of whether we're in
1478 * XXX a macro this could be made more fine grained */
1479 if((state
& (a_DIGIT1
| a_NONDIGIT
)) == (a_DIGIT1
| a_NONDIGIT
)){
1480 if(state
& a_BRACE
){
1481 if(il
> 0 && *ib
== '}')
1484 rv
|= n_SHEXP_STATE_ERR_BRACE
;
1486 if(flags
& n_SHEXP_PARSE_LOG
)
1487 n_err(_("Invalid identifier for ${}: %.*s\n"),
1488 (int)input
->l
, input
->s
);
1489 rv
|= n_SHEXP_STATE_ERR_IDENTIFIER
;
1492 if(state
& a_BRACE
){
1493 if(flags
& n_SHEXP_PARSE_LOG
)
1494 n_err(_("Bad substitution for ${}: %.*s\n"),
1495 (int)input
->l
, input
->s
);
1496 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1497 if(il
> 0 && *ib
== '}')
1500 rv
|= n_SHEXP_STATE_ERR_BRACE
;
1505 if(state
& a_BRACE
){
1506 if(il
== 0 || *ib
!= '}'){
1507 if(flags
& n_SHEXP_PARSE_LOG
)
1508 n_err(_("No closing brace for ${}: %.*s\n"),
1509 (int)input
->l
, input
->s
);
1510 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
|
1511 n_SHEXP_STATE_ERR_BRACE
;
1517 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1520 /* We may shall explode "${@}" to a series of successive,
1521 * properly quoted tokens (instead). The first exploded
1522 * cookie will join with the current token */
1523 if((state
& a_EXPLODE
) && !(flags
& n_SHEXP_PARSE_DRYRUN
) &&
1524 cookie
!= NULL
&& n_var_vexplode(cookie
)){
1526 input
->s
= n_UNCONST(ib
);
1528 goto jrestart_empty
;
1531 /* Check getenv(3) shall no internal variable exist!
1532 * XXX We have some common idioms, avoid memory for them
1533 * XXX Even better would be var_vlook_buf()! */
1536 case '?': vp
= n_qm
; break;
1537 case '!': vp
= n_em
; break;
1538 case '*': vp
= n_star
; break;
1539 case '@': vp
= n_at
; break;
1540 case '#': vp
= n_ns
; break;
1541 default: goto j_var_look_buf
;
1545 vp
= savestrbuf(vp
, i
);
1547 if((cp
= n_var_vlook(vp
, TRU1
)) != NULL
){
1548 rv
|= n_SHEXP_STATE_OUTPUT
;
1549 store
= n_string_push_cp(store
, cp
);
1550 for(; (c
= *cp
) != '\0'; ++cp
)
1552 rv
|= n_SHEXP_STATE_CONTROL
;
1559 }else if(c
== '`' && quotec
== '"' && il
> 0){ /* TODO shell command */
1564 if(!(state
& a_SKIPQ
)){
1565 rv
|= n_SHEXP_STATE_OUTPUT
;
1567 rv
|= n_SHEXP_STATE_CONTROL
;
1568 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1569 store
= n_string_push_c(store
, c
);
1573 if(quotec
!= '\0' && !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
)){
1574 if(flags
& n_SHEXP_PARSE_LOG
)
1575 n_err(_("No closing quote: %.*s\n"), (int)input
->l
, input
->s
);
1576 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
;
1580 assert(!(state
& a_COOKIE
));
1581 if((flags
& n_SHEXP_PARSE_DRYRUN
) && store
!= NULL
){
1582 store
= n_string_push_buf(store
, input
->s
, PTR2SIZE(ib
- input
->s
));
1583 rv
|= n_SHEXP_STATE_OUTPUT
;
1586 if(state
& a_CONSUME
){
1587 input
->s
= n_UNCONST(&ib
[il
]);
1590 if(flags
& n_SHEXP_PARSE_TRIMSPACE
){
1591 for(; il
> 0; ++ib
, --il
)
1596 input
->s
= n_UNCONST(ib
);
1599 if(!(rv
& n_SHEXP_STATE_STOP
)){
1600 if(!(rv
& n_SHEXP_STATE_OUTPUT
) && (flags
& n_SHEXP_PARSE_IGNORE_EMPTY
) &&
1602 goto jrestart_empty
;
1603 if(/*!(rv & n_SHEXP_STATE_OUTPUT) &&*/ il
== 0)
1604 rv
|= n_SHEXP_STATE_STOP
;
1607 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_UNICODE
));
1608 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_CONTROL
));
1614 n_shexp_parse_token_cp(enum n_shexp_parse_flags flags
, char const **cp
){
1616 struct n_string sou
, *soup
;
1618 enum n_shexp_state shs
;
1623 input
.s
= n_UNCONST(*cp
);
1625 soup
= n_string_creat_auto(&sou
);
1627 shs
= n_shexp_parse_token(flags
, soup
, &input
, NULL
);
1628 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1629 soup
= n_string_assign_cp(soup
, *cp
);
1634 rv
= n_string_cp(soup
);
1635 /*n_string_gut(n_string_drop_ownership(soup));*/
1640 FL
struct n_string
*
1641 n_shexp_quote(struct n_string
*store
, struct str
const *input
, bool_t rndtrip
){
1642 struct a_shexp_quote_lvl sql
;
1643 struct a_shexp_quote_ctx sqc
;
1646 assert(store
!= NULL
);
1647 assert(input
!= NULL
);
1648 assert(input
->l
== 0 || input
->s
!= NULL
);
1650 memset(&sqc
, 0, sizeof sqc
);
1651 sqc
.sqc_store
= store
;
1652 sqc
.sqc_input
.s
= input
->s
;
1653 if((sqc
.sqc_input
.l
= input
->l
) == UIZ_MAX
)
1654 sqc
.sqc_input
.l
= strlen(input
->s
);
1655 sqc
.sqc_flags
= rndtrip
? a_SHEXP_QUOTE_ROUNDTRIP
: a_SHEXP_QUOTE_NONE
;
1657 if(sqc
.sqc_input
.l
== 0)
1658 store
= n_string_push_buf(store
, "''", sizeof("''") -1);
1660 memset(&sql
, 0, sizeof sql
);
1661 sql
.sql_dat
= sqc
.sqc_input
;
1662 sql
.sql_flags
= sqc
.sqc_flags
;
1663 a_shexp__quote(&sqc
, &sql
);
1670 n_shexp_quote_cp(char const *cp
, bool_t rndtrip
){
1671 struct n_string store
;
1678 input
.s
= n_UNCONST(cp
);
1680 rv
= n_string_cp(n_shexp_quote(n_string_creat_auto(&store
), &input
,
1682 n_string_gut(n_string_drop_ownership(&store
));
1688 n_shexp_is_valid_varname(char const *name
){
1695 for(lc
= '\0'; (c
= *name
++) != '\0'; lc
= c
)
1696 if(!a_SHEXP_ISVARC(c
))
1698 else if(lc
== '\0' && a_SHEXP_ISVARC_BAD1ST(c
))
1700 if(a_SHEXP_ISVARC_BADNST(lc
))
1712 struct n_string sou_b
, *soup
;
1716 char const **argv
, *varname
, *act
, *cp
;
1718 soup
= n_string_creat_auto(&sou_b
);
1720 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1723 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1725 if((norndtrip
= (*act
== '+')))
1729 alen
= PTR2SIZE(cp
- act
);
1733 in
.l
= strlen(in
.s
= n_UNCONST(cp
));
1736 if(is_ascncaseprefix(act
, "encode", alen
))
1737 soup
= n_shexp_quote(soup
, &in
, !norndtrip
);
1738 else if(!norndtrip
&& is_ascncaseprefix(act
, "decode", alen
)){
1740 enum n_shexp_state shs
;
1742 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
1743 n_SHEXP_PARSE_IGNORE_EMPTY
), soup
, &in
, NULL
);
1744 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1745 soup
= n_string_assign_cp(soup
, cp
);
1746 nerrn
= n_ERR_CANCELED
;
1750 if(shs
& n_SHEXP_STATE_STOP
)
1757 if(varname
!= NULL
){
1758 cp
= n_string_cp(soup
);
1759 if(!n_var_vset(varname
, (uintptr_t)cp
)){
1760 nerrn
= n_ERR_NOTSUP
;
1766 in
.s
= n_string_cp(soup
);
1768 makeprint(&in
, &out
);
1769 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0){
1777 n_pstate_err_no
= nerrn
;
1779 return (cp
!= NULL
? 0 : 1);
1781 n_err(_("Synopsis: shcodec: <[+]e[ncode]|d[ecode]> <rest-of-line>\n"));
1782 nerrn
= n_ERR_INVAL
;