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.
4 *@ TODO "Magic solidus" used as path separator.
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
52 * Environment variable names used by the utilities in the Shell and
53 * Utilities volume of POSIX.1-2008 consist solely of uppercase
54 * letters, digits, and the <underscore> ('_') from the characters
55 * defined in Portable Character Set and do not begin with a digit.
56 * Other characters may be permitted by an implementation;
57 * applications shall tolerate the presence of such names.
58 * We do support the hyphen-minus "-" (except in last position for ${x[:]-y}).
59 * We support some special parameter names for one-letter(++) variable names;
60 * these have counterparts in the code that manages internal variables,
61 * and some more special treatment below! */
62 #define a_SHEXP_ISVARC(C) (alnumchar(C) || (C) == '_' || (C) == '-')
63 #define a_SHEXP_ISVARC_BAD1ST(C) (digitchar(C)) /* (Actually assumed below!) */
64 #define a_SHEXP_ISVARC_BADNST(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 /* _NFOLDER 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 / built-in: %s\n"), cp
);
142 /* Heirloom compatibility: an IMAP *folder* becomes "%" */
144 else if(cp
== NULL
&& !strcmp(user
, ok_vlook(LOGNAME
)) &&
145 which_protocol(cp
= n_folder_query(), FAL0
, FAL0
, NULL
)
147 /* TODO Compat handling of *folder* with IMAP! */
148 n_OBSOLETE("no more expansion of *folder* in \"%\": "
149 "please set *inbox*");
155 if((cp
= ok_vlook(MAIL
)) != NULL
){
164 ul
= strlen(user
) +1;
165 i
= sizeof(VAL_MAIL
) -1 + 1 + ul
;
168 memcpy(rv
, VAL_MAIL
, (i
= sizeof(VAL_MAIL
) -1));
170 memcpy(&rv
[++i
], user
, ul
);
178 a_shexp_tilde(char const *s
){
185 if(*(rp
= &s
[1]) == '/' || *rp
== '\0'){
189 if((rp
= strchr(np
= rp
, '/')) != NULL
){
190 nl
= PTR2SIZE(rp
- np
);
191 np
= savestrbuf(np
, nl
);
196 if((pwp
= getpwnam(np
)) == NULL
){
204 rv
= salloc(nl
+ 1 + rl
+1);
207 memcpy(rv
+ nl
, rp
, rl
);
217 a_shexp_globname(char const *name
, enum fexp_mode fexpm
){
219 struct a_shexp_glob_ctx sgc
;
220 struct n_string outer
;
221 struct n_strlist
*slp
;
225 memset(&sgc
, 0, sizeof sgc
);
226 sgc
.sgc_patlen
= strlen(name
);
227 sgc
.sgc_patdat
= savestrbuf(name
, sgc
.sgc_patlen
);
228 sgc
.sgc_outer
= n_string_reserve(n_string_creat(&outer
), sgc
.sgc_patlen
);
229 sgc
.sgc_flags
= ((fexpm
& FEXP_SILENT
) != 0);
231 if(a_shexp__glob(&sgc
, &slp
))
235 n_string_gut(&outer
);
241 cp
= n_UNCONST(N_("File pattern does not match"));
243 }else if(slp
->sl_next
== NULL
)
244 cp
= savestrbuf(slp
->sl_dat
, slp
->sl_len
);
245 else if(fexpm
& FEXP_MULTIOK
){
246 struct n_strlist
**sorta
, *xslp
;
250 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
){
252 l
+= xslp
->sl_len
+ 1;
255 sorta
= smalloc(sizeof(*sorta
) * no
);
257 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
)
259 qsort(sorta
, no
, sizeof *sorta
, &a_shexp__globsort
);
263 for(i
= 0; i
< no
; ++i
){
265 memcpy(&cp
[l
], xslp
->sl_dat
, xslp
->sl_len
);
272 n_pstate
|= n_PS_EXPAND_MULTIRESULT
;
274 cp
= n_UNCONST(N_("File pattern matches multiple results"));
280 struct n_strlist
*tmp
= slp
;
289 if(!(fexpm
& FEXP_SILENT
)){
290 name
= n_shexp_quote_cp(name
, FAL0
);
291 n_err("%s: %s\n", V_(cp
), name
);
296 #else /* HAVE_FNMATCH */
299 if(!(fexpm
& FEXP_SILENT
))
300 n_err(_("No filename pattern (fnmatch(3)) support compiled in\n"));
301 return savestr(name
);
307 a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
, struct n_strlist
**slpp
){
308 enum{a_SILENT
= 1<<0, a_DEEP
=1<<1, a_SALLOC
=1<<2};
310 struct a_shexp_glob_ctx nsgc
;
314 char const *ccp
, *myp
;
317 /* We need some special treatment for the outermost level.
318 * All along our way, normalize path separators */
319 if(!(sgcp
->sgc_flags
& a_DEEP
)){
320 if(sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/'){
321 myp
= n_string_cp(n_string_push_c(sgcp
->sgc_outer
, '/'));
324 while(--sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/');
328 myp
= n_string_cp(sgcp
->sgc_outer
);
329 old_outerlen
= sgcp
->sgc_outer
->s_len
;
331 /* Separate current directory/pattern level from any possible remaining
332 * pattern in order to be able to use it for fnmatch(3) */
333 if((ccp
= memchr(sgcp
->sgc_patdat
, '/', sgcp
->sgc_patlen
)) == NULL
)
337 nsgc
.sgc_flags
|= a_DEEP
;
338 sgcp
->sgc_patlen
= PTR2SIZE((nsgc
.sgc_patdat
= &ccp
[1]) -
339 &sgcp
->sgc_patdat
[0]);
340 nsgc
.sgc_patlen
-= sgcp
->sgc_patlen
;
342 /* Trim solidus, everywhere */
343 if(sgcp
->sgc_patlen
> 0){
344 assert(sgcp
->sgc_patdat
[sgcp
->sgc_patlen
-1] == '/');
345 ((char*)n_UNCONST(sgcp
->sgc_patdat
))[--sgcp
->sgc_patlen
] = '\0';
347 while(nsgc
.sgc_patlen
> 0 && nsgc
.sgc_patdat
[0] == '/'){
353 /* Our current directory level */
354 /* xxx Plenty of room for optimizations, like quickshot lstat(2) which may
355 * xxx be the (sole) result depending on pattern surroundings, etc. */
356 if((dp
= opendir(myp
)) == NULL
){
359 switch((err
= n_err_no
)){
361 ccp
= N_("cannot access paths under non-directory");
364 ccp
= N_("path component of (sub)pattern non-existent");
367 ccp
= N_("file permission for file (sub)pattern denied");
371 ccp
= N_("file descriptor limit reached, cannot open directory");
374 ccp
= N_("cannot open path component as directory");
379 /* As necessary, quote bytes in the current pattern */
385 for(need
= FAL0
, i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
387 case '\'': case '"': case '\\': case '$':
399 for(i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
401 case '\'': case '"': case '\\': case '$':
412 myp
= sgcp
->sgc_patdat
;
415 while((dep
= readdir(dp
)) != NULL
){
416 switch(fnmatch(myp
, dep
->d_name
, FNM_PATHNAME
| FNM_PERIOD
)){
418 /* A match expresses the desire to recurse if there is more pattern */
419 if(nsgc
.sgc_patlen
> 0){
422 n_string_push_cp((sgcp
->sgc_outer
->s_len
> 1
423 ? n_string_push_c(sgcp
->sgc_outer
, '/') : sgcp
->sgc_outer
),
427 #ifdef HAVE_DIRENT_TYPE
428 if(dep
->d_type
== DT_DIR
)
430 else if(dep
->d_type
== DT_LNK
|| dep
->d_type
== DT_UNKNOWN
)
435 if(stat(n_string_cp(sgcp
->sgc_outer
), &sb
)){
436 ccp
= N_("I/O error when querying file status");
438 }else if(S_ISDIR(sb
.st_mode
))
442 /* TODO We recurse with current dir FD open, which could E[MN]FILE!
443 * TODO Instead save away a list of such n_string's for later */
444 if(isdir
&& !a_shexp__glob(&nsgc
, slpp
)){
449 n_string_trunc(sgcp
->sgc_outer
, old_outerlen
);
451 struct n_strlist
*slp
;
454 i
= strlen(dep
->d_name
);
455 j
= (old_outerlen
> 0) ? old_outerlen
+ 1 + i
: i
;
456 slp
= n_STRLIST_ALLOC(j
);
458 slpp
= &slp
->sl_next
;
460 if((j
= old_outerlen
) > 0){
461 memcpy(&slp
->sl_dat
[0], sgcp
->sgc_outer
->s_dat
, j
);
462 if(slp
->sl_dat
[j
-1] != '/')
463 slp
->sl_dat
[j
++] = '/';
465 memcpy(&slp
->sl_dat
[j
], dep
->d_name
, i
);
466 slp
->sl_dat
[j
+= i
] = '\0';
473 ccp
= N_("fnmatch(3) cannot handle file (sub)pattern");
483 return (ccp
== NULL
);
486 if(!(sgcp
->sgc_flags
& a_SILENT
)){
489 if(sgcp
->sgc_outer
->s_len
> 0){
490 s2
= n_shexp_quote_cp(n_string_cp(sgcp
->sgc_outer
), FAL0
);
495 n_err("%s: %s%s%s\n", V_(ccp
), s2
, s3
,
496 n_shexp_quote_cp(sgcp
->sgc_patdat
, FAL0
));
502 a_shexp__globsort(void const *cvpa
, void const *cvpb
){
504 struct n_strlist
const * const *slpa
, * const *slpb
;
509 rv
= asccasecmp((*slpa
)->sl_dat
, (*slpb
)->sl_dat
);
513 #endif /* HAVE_FNMATCH */
516 a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
, struct a_shexp_quote_lvl
*sqlp
){
517 /* XXX Because of the problems caused by ISO C multibyte interface we cannot
518 * XXX use the recursive implementation because of stateful encodings.
519 * XXX I.e., if a quoted substring cannot be self-contained - the data after
520 * XXX the quote relies on "the former state", then this doesn't make sense.
521 * XXX Therefore this is not fully programmed out but instead only detects
522 * XXX the "most fancy" quoting necessary, and directly does that.
523 * XXX As a result of this, T_REVSOL and T_DOUBLE are not even considered.
524 * XXX Otherwise we rather have to convert to wide first and act on that,
525 * XXX e.g., call visual_info(n_VISUAL_INFO_WOUT_CREATE) on entire input */
526 #undef a_SHEXP_QUOTE_RECURSE /* XXX (Needs complete revisit, then) */
527 #ifdef a_SHEXP_QUOTE_RECURSE
528 # define jrecurse jrecurse
529 struct a_shexp_quote_lvl sql
;
531 # define jrecurse jstep
533 struct n_visual_info_ctx vic
;
534 union {struct a_shexp_quote_lvl
*head
; struct n_string
*store
;} u
;
537 char const *ib
, *ib_base
;
540 ib_base
= ib
= sqlp
->sql_dat
.s
;
541 il
= sqlp
->sql_dat
.l
;
542 flags
= sqlp
->sql_flags
;
544 /* Iterate over the entire input, classify characters and type of quotes
545 * along the way. Whenever a quote change has to be applied, adjust flags
546 * for the new situation -, setup sql.* and recurse- */
552 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
554 if(c
== '\t' && (flags
& (a_SHEXP_QUOTE_T_REVSOL
|
555 a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOUBLE
)))
557 #ifdef a_SHEXP_QUOTE_RECURSE
558 ++sqcp
->sqc_cnt_dollar
;
560 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
562 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
563 /* Whereas we don't support those, quote them for the sh(1)ell */
564 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
565 c
== '"' || c
== '$'){
566 if(flags
& a_SHEXP_QUOTE_T_MASK
)
568 #ifdef a_SHEXP_QUOTE_RECURSE
569 ++sqcp
->sqc_cnt_single
;
571 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
574 if(flags
& (a_SHEXP_QUOTE_T_MASK
& ~a_SHEXP_QUOTE_T_SINGLE
))
576 #ifdef a_SHEXP_QUOTE_RECURSE
577 ++sqcp
->sqc_cnt_dollar
;
579 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
581 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
582 if(flags
& a_SHEXP_QUOTE_T_MASK
)
584 #ifdef a_SHEXP_QUOTE_RECURSE
585 ++sqcp
->sqc_cnt_single
;
587 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
589 }else if(!asciichar(c
)){
590 /* Need to keep together multibytes */
591 #ifdef a_SHEXP_QUOTE_RECURSE
592 memset(&vic
, 0, sizeof vic
);
596 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
598 /* xxx check whether resulting \u would be ASCII */
599 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
) ||
600 (flags
& a_SHEXP_QUOTE_T_DOLLAR
)){
601 #ifdef a_SHEXP_QUOTE_RECURSE
609 #ifdef a_SHEXP_QUOTE_RECURSE
610 ++sqcp
->sqc_cnt_dollar
;
612 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
618 sqlp
->sql_flags
= flags
;
620 /* Level made the great and completed processing input. Reverse the list of
621 * levels, detect the "most fancy" quote type needed along this way */
622 /* XXX Due to restriction as above very crude */
623 for(flags
= 0, il
= 0, u
.head
= NULL
; sqlp
!= NULL
;){
624 struct a_shexp_quote_lvl
*tmp
;
626 tmp
= sqlp
->sql_link
;
627 sqlp
->sql_link
= u
.head
;
629 il
+= sqlp
->sql_dat
.l
;
630 if(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
)
631 il
+= (sqlp
->sql_dat
.l
>> 1);
632 flags
|= sqlp
->sql_flags
;
637 /* Finally work the substrings in the correct order, adjusting quotes along
638 * the way as necessary. Start off with the "most fancy" quote, so that
639 * the user sees an overall boundary she can orientate herself on.
640 * We do it like that to be able to give the user some "encapsulation
641 * experience", to address what strikes me is a problem of sh(1)ell quoting:
642 * different to, e.g., perl(1), where you see at a glance where a string
643 * starts and ends, sh(1) quoting occurs at the "top level", disrupting the
644 * visual appearance of "a string" as such */
645 u
.store
= n_string_reserve(sqcp
->sqc_store
, il
);
647 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
648 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
649 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
650 }else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
){
651 u
.store
= n_string_push_c(u
.store
, '"');
652 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOUBLE
;
653 }else if(flags
& a_SHEXP_QUOTE_T_SINGLE
){
654 u
.store
= n_string_push_c(u
.store
, '\'');
655 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
656 }else /*if(flags & a_SHEXP_QUOTE_T_REVSOL)*/
657 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
659 /* Work all the levels */
660 for(; sqlp
!= NULL
; sqlp
= sqlp
->sql_link
){
661 /* As necessary update our mode of quoting */
662 #ifdef a_SHEXP_QUOTE_RECURSE
665 switch(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
){
666 case a_SHEXP_QUOTE_T_DOLLAR
:
667 if(!(flags
& a_SHEXP_QUOTE_T_DOLLAR
))
668 il
= a_SHEXP_QUOTE_T_DOLLAR
;
670 case a_SHEXP_QUOTE_T_DOUBLE
:
671 if(!(flags
& (a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
672 il
= a_SHEXP_QUOTE_T_DOLLAR
;
674 case a_SHEXP_QUOTE_T_SINGLE
:
675 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
676 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
677 il
= a_SHEXP_QUOTE_T_SINGLE
;
680 case a_SHEXP_QUOTE_T_REVSOL
:
681 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
682 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
683 il
= a_SHEXP_QUOTE_T_REVSOL
;
688 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
689 u
.store
= n_string_push_c(u
.store
, '\'');
690 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
691 u
.store
= n_string_push_c(u
.store
, '"');
692 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
695 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
696 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
697 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
698 u
.store
= n_string_push_c(u
.store
, '"');
699 else if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
700 u
.store
= n_string_push_c(u
.store
, '\'');
702 #endif /* a_SHEXP_QUOTE_RECURSE */
704 /* Work the level's substring */
705 ib
= sqlp
->sql_dat
.s
;
706 il
= sqlp
->sql_dat
.l
;
714 assert(c
== '\t' || (flags
& a_SHEXP_QUOTE_T_DOLLAR
));
715 assert((flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
716 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)));
718 case 0x07: c
= 'a'; break;
719 case 0x08: c
= 'b'; break;
720 case 0x0A: c
= 'n'; break;
721 case 0x0B: c
= 'v'; break;
722 case 0x0C: c
= 'f'; break;
723 case 0x0D: c
= 'r'; break;
724 case 0x1B: c
= 'E'; break;
727 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
731 if(flags
& a_SHEXP_QUOTE_T_REVSOL
)
732 u
.store
= n_string_push_c(u
.store
, '\\');
735 u
.store
= n_string_push_c(u
.store
, '\\');
737 u
.store
= n_string_push_c(u
.store
, 'c');
741 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
742 /* Whereas we don't support those, quote them for the sh(1)ell */
743 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
744 c
== '"' || c
== '$'){
745 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
747 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
));
748 u
.store
= n_string_push_c(u
.store
, '\\');
751 if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
753 assert(!(flags
& a_SHEXP_QUOTE_T_SINGLE
));
754 u
.store
= n_string_push_c(u
.store
, '\\');
756 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
757 if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
759 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
|
760 a_SHEXP_QUOTE_T_DOLLAR
));
761 u
.store
= n_string_push_c(u
.store
, '\\');
763 }else if(asciichar(c
)){
764 /* Shorthand: we can simply push that thing out */
766 u
.store
= n_string_push_c(u
.store
, c
);
769 /* Not an ASCII character, take care not to split up multibyte
770 * sequences etc. For the sake of compile testing, don't enwrap in
771 * HAVE_ALWAYS_UNICODE_LOCALE || HAVE_NATCH_CHAR */
772 if(n_psonce
& n_PSO_UNICODE
){
779 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
783 il2
= PTR2SIZE(&ib2
[0] - &ib
[0]);
784 if((flags
& a_SHEXP_QUOTE_ROUNDTRIP
) || uc
== 0xFFFDu
){
785 /* Use padding to make ambiguities impossible */
786 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
787 (uc
> 0xFFFFu
? 'U' : 'u'),
788 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
794 u
.store
= n_string_push_buf(u
.store
, cp
, il3
);
795 ib
+= il2
, il
-= il2
;
800 memset(&vic
, 0, sizeof vic
);
804 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
806 /* Work this substring as sensitive as possible */
808 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
))
809 u
.store
= n_string_push_buf(u
.store
, ib
, il
);
811 else if((vic
.vic_indat
= n_iconv_onetime_cp(n_ICONV_NONE
,
812 "utf-8", ok_vlook(ttycharset
), savestrbuf(ib
, il
))) != NULL
){
817 il2
= strlen(ib2
= vic
.vic_indat
);
818 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
821 il2
= PTR2SIZE(&ib2
[0] - &vic
.vic_indat
[0]);
822 /* Use padding to make ambiguities impossible */
823 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
824 (uc
> 0xFFFFu
? 'U' : 'u'),
825 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
826 u
.store
= n_string_push_buf(u
.store
, itoa
, il3
);
836 u
.store
= n_string_push_buf(u
.store
, "\\xFF",
838 n_c_to_hex_base16(&u
.store
->s_dat
[u
.store
->s_len
- 2], *ib
++);
847 /* Close an open quote */
848 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
849 u
.store
= n_string_push_c(u
.store
, '\'');
850 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
851 u
.store
= n_string_push_c(u
.store
, '"');
852 #ifdef a_SHEXP_QUOTE_RECURSE
858 #ifdef a_SHEXP_QUOTE_RECURSE
860 sqlp
->sql_dat
.l
-= il
;
863 sql
.sql_dat
.s
= n_UNCONST(ib
);
865 sql
.sql_flags
= flags
;
866 a_shexp__quote(sqcp
, &sql
);
871 #undef a_SHEXP_QUOTE_RECURSE
875 fexpand(char const *name
, enum fexp_mode fexpm
) /* TODO in parts: -> URL::!! */
878 char const *res
, *cp
;
879 bool_t dyn
, haveproto
;
882 n_pstate
&= ~n_PS_EXPAND_MULTIRESULT
;
885 /* The order of evaluation is "%" and "#" expand into constants.
886 * "&" can expand into "+". "+" can expand into shell meta characters.
887 * Shell meta characters expand into constants.
888 * This way, we make no recursive expansion */
889 if((fexpm
& FEXP_NSHORTCUT
) || (res
= shortcut_expand(name
)) == NULL
)
890 res
= n_UNCONST(name
);
893 n_UNINIT(proto
.s
, NULL
), n_UNINIT(proto
.l
, 0);
895 for(cp
= res
; *cp
&& *cp
!= ':'; ++cp
)
898 if(cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/'){
900 proto
.s
= n_UNCONST(res
);
902 proto
.l
= PTR2SIZE(cp
- res
);
907 if(!(fexpm
& FEXP_NSPECIAL
)){
912 if(res
[1] == ':' && res
[2] != '\0'){
918 force
= (res
[1] != '\0');
919 res
= a_shexp_findmail((force
? &res
[1] : ok_vlook(LOGNAME
)),
928 if (prevfile
[0] == '\0') {
929 n_err(_("No previous file\n"));
937 res
= ok_vlook(MBOX
);
945 if(res
[0] == '@' && which_protocol(mailname
, FAL0
, FAL0
, NULL
)
947 res
= str_concat_csvl(&s
, protbase(mailname
), "/", &res
[1], NULL
)->s
;
952 /* POSIX: if *folder* unset or null, "+" shall be retained */
953 if(!(fexpm
& FEXP_NFOLDER
) && *res
== '+' &&
954 *(cp
= n_folder_query()) != '\0'){
955 res
= str_concat_csvl(&s
, cp
, &res
[1], NULL
)->s
;
959 /* Do some meta expansions */
960 if((fexpm
& (FEXP_NSHELL
| FEXP_NVAR
)) != FEXP_NVAR
&&
961 ((fexpm
& FEXP_NSHELL
) ? (strchr(res
, '$') != NULL
)
962 : n_anyof_cp("{}[]*?$", res
))){
965 if(fexpm
& FEXP_NOPROTO
)
968 cp
= haveproto
? savecat(savestrbuf(proto
.s
, proto
.l
), res
) : res
;
970 switch(which_protocol(cp
, TRU1
, FAL0
, NULL
)){
983 struct n_string shou
, *shoup
;
985 shin
.s
= n_UNCONST(res
);
987 shoup
= n_string_creat_auto(&shou
);
989 enum n_shexp_state shs
;
991 /* TODO shexp: take care to not include backtick eval once avail! */
992 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG_D_V
|
993 n_SHEXP_PARSE_QUOTE_AUTO_FIXED
| n_SHEXP_PARSE_QUOTE_AUTO_DQ
|
994 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
), shoup
, &shin
, NULL
);
995 if(shs
& n_SHEXP_STATE_STOP
)
998 res
= n_string_cp(shoup
);
999 /*shoup = n_string_drop_ownership(shoup);*/
1003 res
= a_shexp_tilde(res
);
1005 if(!(fexpm
& FEXP_NSHELL
) &&
1006 (res
= a_shexp_globname(res
, fexpm
)) == NULL
)
1009 }/* else no tilde */
1010 }else if(res
[0] == '~'){
1011 res
= a_shexp_tilde(res
);
1016 if(res
!= NULL
&& haveproto
){
1017 res
= savecat(savestrbuf(proto
.s
, proto
.l
), res
);
1021 if(fexpm
& FEXP_LOCAL
){
1022 switch (which_protocol(res
, FAL0
, FAL0
, NULL
)) {
1024 case PROTO_MAILDIR
: /* Cannot happen since we don't stat(2), but.. */
1027 n_err(_("Not a local file or directory: %s\n"),
1028 n_shexp_quote_cp(name
, FAL0
));
1035 if(res
!= NULL
&& !dyn
)
1038 return n_UNCONST(res
);
1041 FL
enum n_shexp_state
1042 n_shexp_parse_token(enum n_shexp_parse_flags flags
, struct n_string
*store
,
1043 struct str
*input
, void const **cookie
){
1044 /* TODO shexp_parse_token: WCHAR */
1045 ui32_t last_known_meta_trim_len
;
1046 char c2
, c
, quotec
, utf
[8];
1047 enum n_shexp_state rv
;
1049 char const *ifs
, *ifs_ws
, *ib_save
, *ib
;
1052 a_SKIPQ
= 1u<<0, /* Skip rest of this quote (\u0 ..) */
1053 a_SKIPT
= 1u<<1, /* Skip entire token (\c@) */
1054 a_SKIPMASK
= a_SKIPQ
| a_SKIPT
,
1055 a_SURPLUS
= 1u<<2, /* Extended sequence interpretation */
1056 a_NTOKEN
= 1u<<3, /* "New token": e.g., comments are possible */
1057 a_BRACE
= 1u<<4, /* Variable substitution: brace enclosed */
1058 a_DIGIT1
= 1u<<5, /* ..first character was digit */
1059 a_NONDIGIT
= 1u<<6, /* ..has seen any non-digits */
1060 a_VARSUBST_MASK
= n_BITENUM_MASK(4, 6),
1062 a_ROUND_MASK
= a_SKIPT
| (int)~n_BITENUM_MASK(0, 7),
1065 a_CONSUME
= 1u<<10, /* When done, "consume" remaining input */
1070 assert((flags
& n_SHEXP_PARSE_DRYRUN
) || store
!= NULL
);
1071 assert(input
!= NULL
);
1072 assert(input
->l
== 0 || input
->s
!= NULL
);
1073 assert(!(flags
& n_SHEXP_PARSE_LOG
) || !(flags
& n_SHEXP_PARSE_LOG_D_V
));
1074 assert(!(flags
& n_SHEXP_PARSE_IFS_ADD_COMMA
) ||
1075 !(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
));
1076 assert(!(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
) ||
1077 (flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
));
1079 if((flags
& n_SHEXP_PARSE_LOG_D_V
) && (n_poption
& n_PO_D_V
))
1080 flags
|= n_SHEXP_PARSE_LOG
;
1081 if(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)
1082 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
;
1084 if((flags
& n_SHEXP_PARSE_TRUNC
) && store
!= NULL
)
1085 store
= n_string_trunc(store
, 0);
1087 if(flags
& (n_SHEXP_PARSE_IFS_VAR
| n_SHEXP_PARSE_TRIM_IFSSPACE
)){
1088 ifs
= ok_vlook(ifs
);
1089 ifs_ws
= ok_vlook(ifs_ws
);
1091 n_UNINIT(ifs
, n_empty
);
1092 n_UNINIT(ifs_ws
, n_empty
);
1097 if((il
= input
->l
) == UIZ_MAX
)
1098 input
->l
= il
= strlen(ib
);
1101 if(cookie
!= NULL
&& *cookie
!= NULL
){
1102 assert(!(flags
& n_SHEXP_PARSE_DRYRUN
));
1106 rv
= n_SHEXP_STATE_NONE
;
1108 rv
&= n_SHEXP_STATE_WS_LEAD
;
1109 state
&= a_ROUND_MASK
;
1111 /* In cookie mode, the next ARGV entry is the token already, unchanged,
1112 * since it has already been expanded before! */
1113 if(state
& a_COOKIE
){
1114 char const * const *xcookie
, *cp
;
1118 if((store
= n_string_push_cp(store
, *xcookie
))->s_len
> 0)
1119 rv
|= n_SHEXP_STATE_OUTPUT
;
1120 if(*++xcookie
== NULL
){
1123 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_DQ
; /* ..why we are here! */
1125 *cookie
= n_UNCONST(xcookie
);
1127 for(cp
= &n_string_cp(store
)[i
]; (c
= *cp
++) != '\0';)
1129 rv
|= n_SHEXP_STATE_CONTROL
;
1133 /* The last exploded cookie will join with the yielded input token, so
1134 * simply fall through in this case */
1135 if(state
& a_COOKIE
)
1139 if(flags
& n_SHEXP_PARSE_TRIM_SPACE
){
1140 for(; il
> 0; ++ib
, --il
){
1141 if(!blankspacechar(*ib
))
1143 rv
|= n_SHEXP_STATE_WS_LEAD
;
1147 if(flags
& n_SHEXP_PARSE_TRIM_IFSSPACE
){
1148 for(; il
> 0; ++ib
, --il
){
1149 if(strchr(ifs_ws
, *ib
) == NULL
)
1151 rv
|= n_SHEXP_STATE_WS_LEAD
;
1155 input
->s
= n_UNCONST(ib
);
1160 rv
|= n_SHEXP_STATE_STOP
;
1165 store
= n_string_reserve(store
, n_MIN(il
, 32)); /* XXX */
1167 switch(flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
){
1168 case n_SHEXP_PARSE_QUOTE_AUTO_SQ
:
1170 rv
|= n_SHEXP_STATE_QUOTE
;
1172 case n_SHEXP_PARSE_QUOTE_AUTO_DQ
:
1175 case n_SHEXP_PARSE_QUOTE_AUTO_DSQ
:
1178 rv
|= n_SHEXP_STATE_QUOTE
;
1187 /* TODO n_SHEXP_PARSE_META_SEMICOLON++, well, hack: we are not the shell,
1188 * TODO we are not a language, and therefore the general *ifs-ws* and normal
1189 * TODO whitespace trimming that input lines undergo (in a_go_evaluate())
1190 * TODO has already happened, our result will be used *as is*, and therefore
1191 * TODO we need to be aware of and remove trailing unquoted WS that would
1192 * TODO otherwise remain, after we have seen a semicolon sequencer.
1193 * By sheer luck we only need to track this in non-quote-mode */
1194 last_known_meta_trim_len
= UI32_MAX
;
1199 /* If no quote-mode active.. */
1201 if(c
== '"' || c
== '\''){
1206 state
&= ~a_SURPLUS
;
1208 last_known_meta_trim_len
= UI32_MAX
;
1209 rv
|= n_SHEXP_STATE_QUOTE
;
1214 last_known_meta_trim_len
= UI32_MAX
;
1219 rv
|= n_SHEXP_STATE_QUOTE
;
1224 }else if(c
== '\\'){
1225 /* Outside of quotes this just escapes any next character, but a sole
1226 * <reverse solidus> at EOS is left unchanged */
1230 last_known_meta_trim_len
= UI32_MAX
;
1232 /* A comment may it be if no token has yet started */
1233 else if(c
== '#' && (state
& a_NTOKEN
)){
1234 rv
|= n_SHEXP_STATE_STOP
;
1235 /*last_known_meta_trim_len = UI32_MAX;*/
1238 /* Metacharacters which separate tokens must be turned on explicitly */
1239 else if(c
== '|' && (flags
& n_SHEXP_PARSE_META_VERTBAR
)){
1240 rv
|= n_SHEXP_STATE_META_VERTBAR
;
1242 /* The parsed sequence may be _the_ output, so ensure we don't
1243 * include the metacharacter, then. */
1244 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1246 /*last_known_meta_trim_len = UI32_MAX;*/
1248 }else if(c
== '&' && (flags
& n_SHEXP_PARSE_META_AMPERSAND
)){
1249 rv
|= n_SHEXP_STATE_META_AMPERSAND
;
1251 /* The parsed sequence may be _the_ output, so ensure we don't
1252 * include the metacharacter, then. */
1253 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1255 /*last_known_meta_trim_len = UI32_MAX;*/
1257 }else if(c
== ';' && (flags
& n_SHEXP_PARSE_META_SEMICOLON
)){
1259 n_go_input_inject(n_GO_INPUT_INJECT_COMMIT
, ib
, il
);
1260 rv
|= n_SHEXP_STATE_META_SEMICOLON
| n_SHEXP_STATE_STOP
;
1262 if(!(flags
& n_SHEXP_PARSE_DRYRUN
) && (rv
& n_SHEXP_STATE_OUTPUT
) &&
1263 last_known_meta_trim_len
!= UI32_MAX
)
1264 store
= n_string_trunc(store
, last_known_meta_trim_len
);
1266 /* The parsed sequence may be _the_ output, so ensure we don't
1267 * include the metacharacter, then. */
1268 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1270 /*last_known_meta_trim_len = UI32_MAX;*/
1272 }else if(c
== ',' && (flags
&
1273 (n_SHEXP_PARSE_IFS_ADD_COMMA
| n_SHEXP_PARSE_IFS_IS_COMMA
))){
1274 /* The parsed sequence may be _the_ output, so ensure we don't
1275 * include the metacharacter, then. */
1276 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1278 /*last_known_meta_trim_len = UI32_MAX;*/
1283 blnk
= blankchar(c
) ? 1 : 0;
1284 blnk
|= ((flags
& (n_SHEXP_PARSE_IFS_VAR
|
1285 n_SHEXP_PARSE_TRIM_IFSSPACE
)) &&
1286 strchr(ifs_ws
, c
) != NULL
) ? 2 : 0;
1288 if((!(flags
& n_SHEXP_PARSE_IFS_VAR
) && (blnk
& 1)) ||
1289 ((flags
& n_SHEXP_PARSE_IFS_VAR
) &&
1290 ((blnk
& 2) || strchr(ifs
, c
) != NULL
))){
1291 if(!(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
)){
1292 /* The parsed sequence may be _the_ output, so ensure we don't
1293 * include the metacharacter, then. */
1294 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1296 /*last_known_meta_trim_len = UI32_MAX;*/
1303 if(blnk
&& store
!= NULL
){
1304 if(last_known_meta_trim_len
== UI32_MAX
)
1305 last_known_meta_trim_len
= store
->s_len
;
1307 last_known_meta_trim_len
= UI32_MAX
;
1311 assert(!(state
& a_NTOKEN
));
1312 if(c
== quotec
&& !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)){
1313 state
&= a_ROUND_MASK
;
1315 /* Users may need to recognize the presence of empty quotes */
1316 rv
|= n_SHEXP_STATE_OUTPUT
;
1318 }else if(c
== '\\' && (state
& a_SURPLUS
)){
1320 /* A sole <reverse solidus> at EOS is treated as-is! This is ok
1321 * since the "closing quote" error will occur next, anyway */
1324 else if((c2
= *ib
) == quotec
){
1327 }else if(quotec
== '"'){
1328 /* Double quotes, POSIX says:
1329 * The <backslash> shall retain its special meaning as an
1330 * escape character (see Section 2.2.1) only when followed
1331 * by one of the following characters when considered
1332 * special: $ ` " \ <newline> */
1336 /* case '"': already handled via c2 == quotec */
1345 /* Dollar-single-quote */
1349 /* case '\'': already handled via c2 == quotec */
1354 case 'b': c
= '\b'; break;
1355 case 'f': c
= '\f'; break;
1356 case 'n': c
= '\n'; break;
1357 case 'r': c
= '\r'; break;
1358 case 't': c
= '\t'; break;
1359 case 'v': c
= '\v'; break;
1362 case 'e': c
= '\033'; break;
1364 /* Control character */
1367 goto j_dollar_ungetc
;
1369 if(state
& a_SKIPMASK
)
1371 /* ASCII C0: 0..1F, 7F <- @.._ (+ a-z -> A-Z), ? */
1372 c
= upperconv(c2
) ^ 0x40;
1373 if((ui8_t
)c
> 0x1F && c
!= 0x7F){
1374 if(flags
& n_SHEXP_PARSE_LOG
)
1375 n_err(_("Invalid \\c notation: %.*s: %.*s\n"),
1376 (int)input
->l
, input
->s
,
1377 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1378 rv
|= n_SHEXP_STATE_ERR_CONTROL
;
1380 /* As an implementation-defined extension, support \c@
1381 * EQ printf(1) alike \c */
1388 /* Octal sequence: 1 to 3 octal bytes */
1390 /* As an extension (dependent on where you look, echo(1), or
1391 * awk(1)/tr(1) etc.), allow leading "0" octal indicator */
1392 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1397 case '1': case '2': case '3':
1398 case '4': case '5': case '6': case '7':
1400 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1401 c2
= (c2
<< 3) | (c
- '0');
1404 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1405 if(!(state
& a_SKIPMASK
) && (ui8_t
)c2
> 0x1F){
1406 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1408 if(flags
& n_SHEXP_PARSE_LOG
)
1409 n_err(_("\\0 argument exceeds a byte: %.*s: %.*s\n"),
1410 (int)input
->l
, input
->s
,
1411 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1412 /* Write unchanged */
1414 rv
|= n_SHEXP_STATE_OUTPUT
;
1415 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1416 store
= n_string_push_buf(store
, ib_save
,
1417 PTR2SIZE(ib
- ib_save
));
1420 c2
= (c2
<< 3) | (c
-= '0');
1423 if(state
& a_SKIPMASK
)
1425 if((c
= c2
) == '\0'){
1431 /* ISO 10646 / Unicode sequence, 8 or 4 hexadecimal bytes */
1440 goto j_dollar_ungetc
;
1444 /* Hexadecimal sequence, 1 or 2 hexadecimal bytes */
1448 goto j_dollar_ungetc
;
1452 static ui8_t
const hexatoi
[] = { /* XXX uses ASCII */
1453 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1458 for(no
= j
= 0; i
-- > 0; --il
, ++ib
, ++j
){
1462 no
+= hexatoi
[(ui8_t
)((c
) - ((c
) <= '9' ? 48
1463 : ((c
) <= 'F' ? 55 : 87)))];
1465 if(state
& a_SKIPMASK
)
1467 c2
= (c2
== 'U' || c2
== 'u') ? 'u' : 'x';
1468 if(flags
& n_SHEXP_PARSE_LOG
)
1469 n_err(_("Invalid \\%c notation: %.*s: %.*s\n"),
1470 c2
, (int)input
->l
, input
->s
,
1471 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1472 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1478 /* Unicode massage */
1479 if((c2
!= 'U' && c2
!= 'u') || n_uasciichar(no
)){
1480 if((c
= (char)no
) == '\0')
1484 else if(!(state
& a_SKIPMASK
)){
1485 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1486 store
= n_string_reserve(store
, n_MAX(j
, 4));
1488 if(no
> 0x10FFFF){ /* XXX magic; CText */
1489 if(flags
& n_SHEXP_PARSE_LOG
)
1490 n_err(_("\\U argument exceeds 0x10FFFF: %.*s: "
1492 (int)input
->l
, input
->s
,
1493 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1494 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1495 /* But normalize the output anyway */
1499 j
= n_utf32_to_utf8(no
, utf
);
1501 if(n_psonce
& n_PSO_UNICODE
){
1502 rv
|= n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_UNICODE
;
1503 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1504 store
= n_string_push_buf(store
, utf
, j
);
1511 icp
= n_iconv_onetime_cp(n_ICONV_NONE
,
1514 rv
|= n_SHEXP_STATE_OUTPUT
;
1515 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1516 store
= n_string_push_cp(store
, icp
);
1521 if(!(flags
& n_SHEXP_PARSE_DRYRUN
)) Jerr_uni_norm
:{
1524 rv
|= n_SHEXP_STATE_OUTPUT
|
1525 n_SHEXP_STATE_ERR_UNICODE
;
1526 i
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
1527 (no
> 0xFFFFu
? 'U' : 'u'),
1528 (int)(no
> 0xFFFFu
? 8 : 4), (ui32_t
)no
);
1529 store
= n_string_push_buf(store
, itoa
, i
);
1533 if(state
& a_SKIPMASK
)
1538 /* Extension: \$ can be used to expand a variable.
1539 * B(ug|ad) effect: if conversion fails, not written "as-is" */
1542 goto j_dollar_ungetc
;
1547 /* Follow bash(1) behaviour, print sequence unchanged */
1552 }else if(c
== '$' && quotec
== '"' && il
> 0) J_var_expand
:{
1553 state
&= ~a_VARSUBST_MASK
;
1557 /* Scan variable name */
1558 if(!(state
& a_BRACE
) || il
> 1){
1559 char const *cp
, *vp
;
1565 state
&= ~a_EXPLODE
;
1567 for(i
= 0; il
> 0; --il
, ++ib
, ++i
){
1568 /* We have some special cases regarding special parameters,
1569 * so ensure these don't cause failure. This code has
1570 * counterparts in code that manages internal variables! */
1572 if(!a_SHEXP_ISVARC(c
)){
1574 /* Simply skip over multiplexer */
1577 if(c
== '*' || c
== '@' || c
== '#' || c
== '?' ||
1588 }else if(a_SHEXP_ISVARC_BAD1ST(c
)){
1592 state
|= a_NONDIGIT
;
1595 /* In skip mode, be easy and.. skip over */
1596 if(state
& a_SKIPMASK
){
1597 if((state
& a_BRACE
) && il
> 0 && *ib
== '}')
1602 /* Handle the scan error cases */
1603 if((state
& (a_DIGIT1
| a_NONDIGIT
)) == (a_DIGIT1
| a_NONDIGIT
)){
1604 if(state
& a_BRACE
){
1605 if(il
> 0 && *ib
== '}')
1608 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1610 if(flags
& n_SHEXP_PARSE_LOG
)
1611 n_err(_("Invalid identifier for ${}: %.*s: %.*s\n"),
1612 (int)input
->l
, input
->s
,
1613 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1614 rv
|= n_SHEXP_STATE_ERR_IDENTIFIER
;
1617 if(state
& a_BRACE
){
1618 if(il
== 0 || *ib
!= '}'){
1619 if(flags
& n_SHEXP_PARSE_LOG
)
1620 n_err(_("No closing brace for ${}: %.*s: %.*s\n"),
1621 (int)input
->l
, input
->s
,
1622 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1623 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1629 if(flags
& n_SHEXP_PARSE_LOG
)
1630 n_err(_("Bad substitution for ${}: %.*s: %.*s\n"),
1631 (int)input
->l
, input
->s
,
1632 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1633 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1637 /* Simply write dollar as-is? */
1640 if(state
& a_BRACE
){
1641 if(il
== 0 || *ib
!= '}'){
1642 if(flags
& n_SHEXP_PARSE_LOG
)
1643 n_err(_("No closing brace for ${}: %.*s: %.*s\n"),
1644 (int)input
->l
, input
->s
,
1645 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1646 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1652 if(flags
& n_SHEXP_PARSE_LOG
)
1653 n_err(_("Bad substitution for ${}: %.*s: %.*s\n"),
1654 (int)input
->l
, input
->s
,
1655 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1656 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1661 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1664 /* We may shall explode "${@}" to a series of successive,
1665 * properly quoted tokens (instead). The first exploded
1666 * cookie will join with the current token */
1667 if(n_UNLIKELY(state
& a_EXPLODE
) &&
1668 !(flags
& n_SHEXP_PARSE_DRYRUN
) && cookie
!= NULL
){
1669 if(n_var_vexplode(cookie
))
1671 /* On the other hand, if $@ expands to nothing and is the
1672 * sole content of this quote then act like the shell does
1673 * and throw away the entire atxplode construct */
1674 else if(!(rv
& n_SHEXP_STATE_OUTPUT
) &&
1675 il
== 1 && *ib
== '"' &&
1676 ib_save
== &input
->s
[1] && ib_save
[-1] == '"')
1680 input
->s
= n_UNCONST(ib
);
1682 goto jrestart_empty
;
1685 /* Check getenv(3) shall no internal variable exist!
1686 * XXX We have some common idioms, avoid memory for them
1687 * XXX Even better would be var_vlook_buf()! */
1690 case '?': vp
= n_qm
; break;
1691 case '!': vp
= n_em
; break;
1692 case '*': vp
= n_star
; break;
1693 case '@': vp
= n_at
; break;
1694 case '#': vp
= n_ns
; break;
1695 default: goto j_var_look_buf
;
1699 vp
= savestrbuf(vp
, i
);
1701 if((cp
= n_var_vlook(vp
, TRU1
)) != NULL
){
1702 rv
|= n_SHEXP_STATE_OUTPUT
;
1703 store
= n_string_push_cp(store
, cp
);
1704 for(; (c
= *cp
) != '\0'; ++cp
)
1706 rv
|= n_SHEXP_STATE_CONTROL
;
1713 }else if(c
== '`' && quotec
== '"' && il
> 0){ /* TODO shell command */
1718 if(!(state
& a_SKIPMASK
)){
1719 rv
|= n_SHEXP_STATE_OUTPUT
;
1721 rv
|= n_SHEXP_STATE_CONTROL
;
1722 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1723 store
= n_string_push_c(store
, c
);
1727 if(quotec
!= '\0' && !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
)){
1728 if(flags
& n_SHEXP_PARSE_LOG
)
1729 n_err(_("No closing quote: %.*s\n"), (int)input
->l
, input
->s
);
1730 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
;
1734 assert(!(state
& a_COOKIE
));
1735 if((flags
& n_SHEXP_PARSE_DRYRUN
) && store
!= NULL
){
1736 store
= n_string_push_buf(store
, input
->s
, PTR2SIZE(ib
- input
->s
));
1737 rv
|= n_SHEXP_STATE_OUTPUT
;
1740 if(state
& a_CONSUME
){
1741 input
->s
= n_UNCONST(&ib
[il
]);
1744 if(flags
& n_SHEXP_PARSE_TRIM_SPACE
){
1745 for(; il
> 0; ++ib
, --il
){
1746 if(!blankspacechar(*ib
))
1748 rv
|= n_SHEXP_STATE_WS_TRAIL
;
1752 if(flags
& n_SHEXP_PARSE_TRIM_IFSSPACE
){
1753 for(; il
> 0; ++ib
, --il
){
1754 if(strchr(ifs_ws
, *ib
) == NULL
)
1756 rv
|= n_SHEXP_STATE_WS_TRAIL
;
1761 input
->s
= n_UNCONST(ib
);
1764 if(!(rv
& n_SHEXP_STATE_STOP
)){
1765 if(!(rv
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_META_MASK
)) &&
1766 (flags
& n_SHEXP_PARSE_IGNORE_EMPTY
) && il
> 0)
1767 goto jrestart_empty
;
1768 if(/*!(rv & n_SHEXP_STATE_OUTPUT) &&*/ il
== 0)
1769 rv
|= n_SHEXP_STATE_STOP
;
1772 if((state
& a_SKIPT
) && !(rv
& n_SHEXP_STATE_STOP
) &&
1773 (flags
& n_SHEXP_PARSE_META_MASK
))
1776 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_UNICODE
));
1777 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_CONTROL
));
1783 n_shexp_parse_token_cp(enum n_shexp_parse_flags flags
, char const **cp
){
1785 struct n_string sou
, *soup
;
1787 enum n_shexp_state shs
;
1792 input
.s
= n_UNCONST(*cp
);
1794 soup
= n_string_creat_auto(&sou
);
1796 shs
= n_shexp_parse_token(flags
, soup
, &input
, NULL
);
1797 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1798 soup
= n_string_assign_cp(soup
, *cp
);
1803 rv
= n_string_cp(soup
);
1804 /*n_string_gut(n_string_drop_ownership(soup));*/
1809 FL
struct n_string
*
1810 n_shexp_quote(struct n_string
*store
, struct str
const *input
, bool_t rndtrip
){
1811 struct a_shexp_quote_lvl sql
;
1812 struct a_shexp_quote_ctx sqc
;
1815 assert(store
!= NULL
);
1816 assert(input
!= NULL
);
1817 assert(input
->l
== 0 || input
->s
!= NULL
);
1819 memset(&sqc
, 0, sizeof sqc
);
1820 sqc
.sqc_store
= store
;
1821 sqc
.sqc_input
.s
= input
->s
;
1822 if((sqc
.sqc_input
.l
= input
->l
) == UIZ_MAX
)
1823 sqc
.sqc_input
.l
= strlen(input
->s
);
1824 sqc
.sqc_flags
= rndtrip
? a_SHEXP_QUOTE_ROUNDTRIP
: a_SHEXP_QUOTE_NONE
;
1826 if(sqc
.sqc_input
.l
== 0)
1827 store
= n_string_push_buf(store
, "''", sizeof("''") -1);
1829 memset(&sql
, 0, sizeof sql
);
1830 sql
.sql_dat
= sqc
.sqc_input
;
1831 sql
.sql_flags
= sqc
.sqc_flags
;
1832 a_shexp__quote(&sqc
, &sql
);
1839 n_shexp_quote_cp(char const *cp
, bool_t rndtrip
){
1840 struct n_string store
;
1847 input
.s
= n_UNCONST(cp
);
1849 rv
= n_string_cp(n_shexp_quote(n_string_creat_auto(&store
), &input
,
1851 n_string_gut(n_string_drop_ownership(&store
));
1857 n_shexp_is_valid_varname(char const *name
){
1864 for(lc
= '\0'; (c
= *name
++) != '\0'; lc
= c
)
1865 if(!a_SHEXP_ISVARC(c
))
1867 else if(lc
== '\0' && a_SHEXP_ISVARC_BAD1ST(c
))
1869 if(a_SHEXP_ISVARC_BADNST(lc
))
1879 c_shcodec(void *vp
){
1881 struct n_string sou_b
, *soup
;
1885 char const **argv
, *varname
, *act
, *cp
;
1887 soup
= n_string_creat_auto(&sou_b
);
1889 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1892 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1894 if((norndtrip
= (*act
== '+')))
1898 alen
= PTR2SIZE(cp
- act
);
1902 in
.l
= strlen(in
.s
= n_UNCONST(cp
));
1905 if(is_ascncaseprefix(act
, "encode", alen
))
1906 soup
= n_shexp_quote(soup
, &in
, !norndtrip
);
1907 else if(!norndtrip
&& is_ascncaseprefix(act
, "decode", alen
)){
1909 enum n_shexp_state shs
;
1911 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
1912 n_SHEXP_PARSE_IGNORE_EMPTY
), soup
, &in
, NULL
);
1913 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1914 soup
= n_string_assign_cp(soup
, cp
);
1915 nerrn
= n_ERR_CANCELED
;
1919 if(shs
& n_SHEXP_STATE_STOP
)
1925 if(varname
!= NULL
){
1926 cp
= n_string_cp(soup
);
1927 if(!n_var_vset(varname
, (uintptr_t)cp
)){
1928 nerrn
= n_ERR_NOTSUP
;
1934 in
.s
= n_string_cp(soup
);
1936 makeprint(&in
, &out
);
1937 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0){
1945 n_pstate_err_no
= nerrn
;
1947 return (vp
!= NULL
? 0 : 1);
1949 n_err(_("Synopsis: shcodec: <[+]e[ncode]|d[ecode]> <rest-of-line>\n"));
1950 nerrn
= n_ERR_INVAL
;