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>.
8 * SPDX-License-Identifier: BSD-3-Clause TODO ISC
11 * Copyright (c) 1980, 1993
12 * The Regents of the University of California. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #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 * n_autorec_alloc()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 /* _NFOLDER 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 / built-in: %s\n"), cp
);
143 /* Heirloom compatibility: an IMAP *folder* becomes "%" */
145 else if(cp
== NULL
&& !strcmp(user
, ok_vlook(LOGNAME
)) &&
146 which_protocol(cp
= n_folder_query(), FAL0
, FAL0
, NULL
)
148 /* TODO Compat handling of *folder* with IMAP! */
149 n_OBSOLETE("no more expansion of *folder* in \"%\": "
150 "please set *inbox*");
156 if((cp
= ok_vlook(MAIL
)) != NULL
){
165 ul
= strlen(user
) +1;
166 i
= sizeof(VAL_MAIL
) -1 + 1 + ul
;
168 rv
= n_autorec_alloc(i
);
169 memcpy(rv
, VAL_MAIL
, (i
= sizeof(VAL_MAIL
) -1));
171 memcpy(&rv
[++i
], user
, ul
);
179 a_shexp_tilde(char const *s
){
186 if(*(rp
= &s
[1]) == '/' || *rp
== '\0'){
190 if((rp
= strchr(np
= rp
, '/')) != NULL
){
191 nl
= PTR2SIZE(rp
- np
);
192 np
= savestrbuf(np
, nl
);
197 if((pwp
= getpwnam(np
)) == NULL
){
205 rv
= n_autorec_alloc(nl
+ 1 + rl
+1);
208 memcpy(rv
+ nl
, rp
, rl
);
218 a_shexp_globname(char const *name
, enum fexp_mode fexpm
){
220 struct a_shexp_glob_ctx sgc
;
221 struct n_string outer
;
222 struct n_strlist
*slp
;
226 memset(&sgc
, 0, sizeof sgc
);
227 sgc
.sgc_patlen
= strlen(name
);
228 sgc
.sgc_patdat
= savestrbuf(name
, sgc
.sgc_patlen
);
229 sgc
.sgc_outer
= n_string_reserve(n_string_creat(&outer
), sgc
.sgc_patlen
);
230 sgc
.sgc_flags
= ((fexpm
& FEXP_SILENT
) != 0);
232 if(a_shexp__glob(&sgc
, &slp
))
236 n_string_gut(&outer
);
242 cp
= n_UNCONST(N_("File pattern does not match"));
244 }else if(slp
->sl_next
== NULL
)
245 cp
= savestrbuf(slp
->sl_dat
, slp
->sl_len
);
246 else if(fexpm
& FEXP_MULTIOK
){
247 struct n_strlist
**sorta
, *xslp
;
251 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
){
253 l
+= xslp
->sl_len
+ 1;
256 sorta
= n_alloc(sizeof(*sorta
) * no
);
258 for(xslp
= slp
; xslp
!= NULL
; xslp
= xslp
->sl_next
)
260 qsort(sorta
, no
, sizeof *sorta
, &a_shexp__globsort
);
262 cp
= n_autorec_alloc(++l
);
264 for(i
= 0; i
< no
; ++i
){
266 memcpy(&cp
[l
], xslp
->sl_dat
, xslp
->sl_len
);
273 n_pstate
|= n_PS_EXPAND_MULTIRESULT
;
275 cp
= n_UNCONST(N_("File pattern matches multiple results"));
281 struct n_strlist
*tmp
= slp
;
290 if(!(fexpm
& FEXP_SILENT
)){
291 name
= n_shexp_quote_cp(name
, FAL0
);
292 n_err("%s: %s\n", V_(cp
), name
);
297 #else /* HAVE_FNMATCH */
300 if(!(fexpm
& FEXP_SILENT
))
301 n_err(_("No filename pattern (fnmatch(3)) support compiled in\n"));
302 return savestr(name
);
308 a_shexp__glob(struct a_shexp_glob_ctx
*sgcp
, struct n_strlist
**slpp
){
309 enum{a_SILENT
= 1<<0, a_DEEP
=1<<1, a_SALLOC
=1<<2};
311 struct a_shexp_glob_ctx nsgc
;
315 char const *ccp
, *myp
;
318 /* We need some special treatment for the outermost level.
319 * All along our way, normalize path separators */
320 if(!(sgcp
->sgc_flags
& a_DEEP
)){
321 if(sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/'){
322 myp
= n_string_cp(n_string_push_c(sgcp
->sgc_outer
, '/'));
325 while(--sgcp
->sgc_patlen
> 0 && sgcp
->sgc_patdat
[0] == '/');
329 myp
= n_string_cp(sgcp
->sgc_outer
);
330 old_outerlen
= sgcp
->sgc_outer
->s_len
;
332 /* Separate current directory/pattern level from any possible remaining
333 * pattern in order to be able to use it for fnmatch(3) */
334 if((ccp
= memchr(sgcp
->sgc_patdat
, '/', sgcp
->sgc_patlen
)) == NULL
)
338 nsgc
.sgc_flags
|= a_DEEP
;
339 sgcp
->sgc_patlen
= PTR2SIZE((nsgc
.sgc_patdat
= &ccp
[1]) -
340 &sgcp
->sgc_patdat
[0]);
341 nsgc
.sgc_patlen
-= sgcp
->sgc_patlen
;
343 /* Trim solidus, everywhere */
344 if(sgcp
->sgc_patlen
> 0){
345 assert(sgcp
->sgc_patdat
[sgcp
->sgc_patlen
-1] == '/');
346 ((char*)n_UNCONST(sgcp
->sgc_patdat
))[--sgcp
->sgc_patlen
] = '\0';
348 while(nsgc
.sgc_patlen
> 0 && nsgc
.sgc_patdat
[0] == '/'){
354 /* Our current directory level */
355 /* xxx Plenty of room for optimizations, like quickshot lstat(2) which may
356 * xxx be the (sole) result depending on pattern surroundings, etc. */
357 if((dp
= opendir(myp
)) == NULL
){
360 switch((err
= n_err_no
)){
362 ccp
= N_("cannot access paths under non-directory");
365 ccp
= N_("path component of (sub)pattern non-existent");
368 ccp
= N_("file permission for file (sub)pattern denied");
372 ccp
= N_("file descriptor limit reached, cannot open directory");
375 ccp
= N_("cannot open path component as directory");
380 /* As necessary, quote bytes in the current pattern TODO This will not
381 * TODO truly work out in case the user would try to quote a character
382 * TODO class, for example: in "\[a-z]" the "\" would be doubled! For that
383 * TODO to work out, we need the original user input or the shell-expression
384 * TODO parse tree, otherwise we do not know what is desired! */
390 for(need
= FAL0
, i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
392 case '\'': case '"': case '\\': case '$':
403 ncp
= n_autorec_alloc(i
+1);
404 for(i
= 0, myp
= sgcp
->sgc_patdat
; *myp
!= '\0'; ++myp
)
406 case '\'': case '"': case '\\': case '$':
417 myp
= sgcp
->sgc_patdat
;
420 while((dep
= readdir(dp
)) != NULL
){
421 switch(fnmatch(myp
, dep
->d_name
, FNM_PATHNAME
| FNM_PERIOD
)){
423 /* A match expresses the desire to recurse if there is more pattern */
424 if(nsgc
.sgc_patlen
> 0){
427 n_string_push_cp((sgcp
->sgc_outer
->s_len
> 1
428 ? n_string_push_c(sgcp
->sgc_outer
, '/') : sgcp
->sgc_outer
),
432 #ifdef HAVE_DIRENT_TYPE
433 if(dep
->d_type
== DT_DIR
)
435 else if(dep
->d_type
== DT_LNK
|| dep
->d_type
== DT_UNKNOWN
)
440 if(stat(n_string_cp(sgcp
->sgc_outer
), &sb
)){
441 ccp
= N_("I/O error when querying file status");
443 }else if(S_ISDIR(sb
.st_mode
))
447 /* TODO We recurse with current dir FD open, which could E[MN]FILE!
448 * TODO Instead save away a list of such n_string's for later */
449 if(isdir
&& !a_shexp__glob(&nsgc
, slpp
)){
454 n_string_trunc(sgcp
->sgc_outer
, old_outerlen
);
456 struct n_strlist
*slp
;
459 i
= strlen(dep
->d_name
);
460 j
= (old_outerlen
> 0) ? old_outerlen
+ 1 + i
: i
;
461 slp
= n_STRLIST_ALLOC(j
);
463 slpp
= &slp
->sl_next
;
465 if((j
= old_outerlen
) > 0){
466 memcpy(&slp
->sl_dat
[0], sgcp
->sgc_outer
->s_dat
, j
);
467 if(slp
->sl_dat
[j
-1] != '/')
468 slp
->sl_dat
[j
++] = '/';
470 memcpy(&slp
->sl_dat
[j
], dep
->d_name
, i
);
471 slp
->sl_dat
[j
+= i
] = '\0';
478 ccp
= N_("fnmatch(3) cannot handle file (sub)pattern");
488 return (ccp
== NULL
);
491 if(!(sgcp
->sgc_flags
& a_SILENT
)){
494 if(sgcp
->sgc_outer
->s_len
> 0){
495 s2
= n_shexp_quote_cp(n_string_cp(sgcp
->sgc_outer
), FAL0
);
500 n_err("%s: %s%s%s\n", V_(ccp
), s2
, s3
,
501 n_shexp_quote_cp(sgcp
->sgc_patdat
, FAL0
));
507 a_shexp__globsort(void const *cvpa
, void const *cvpb
){
509 struct n_strlist
const * const *slpa
, * const *slpb
;
514 rv
= asccasecmp((*slpa
)->sl_dat
, (*slpb
)->sl_dat
);
518 #endif /* HAVE_FNMATCH */
521 a_shexp__quote(struct a_shexp_quote_ctx
*sqcp
, struct a_shexp_quote_lvl
*sqlp
){
522 /* XXX Because of the problems caused by ISO C multibyte interface we cannot
523 * XXX use the recursive implementation because of stateful encodings.
524 * XXX I.e., if a quoted substring cannot be self-contained - the data after
525 * XXX the quote relies on "the former state", then this doesn't make sense.
526 * XXX Therefore this is not fully programmed out but instead only detects
527 * XXX the "most fancy" quoting necessary, and directly does that.
528 * XXX As a result of this, T_REVSOL and T_DOUBLE are not even considered.
529 * XXX Otherwise we rather have to convert to wide first and act on that,
530 * XXX e.g., call visual_info(n_VISUAL_INFO_WOUT_CREATE) on entire input */
531 #undef a_SHEXP_QUOTE_RECURSE /* XXX (Needs complete revisit, then) */
532 #ifdef a_SHEXP_QUOTE_RECURSE
533 # define jrecurse jrecurse
534 struct a_shexp_quote_lvl sql
;
536 # define jrecurse jstep
538 struct n_visual_info_ctx vic
;
539 union {struct a_shexp_quote_lvl
*head
; struct n_string
*store
;} u
;
542 char const *ib
, *ib_base
;
545 ib_base
= ib
= sqlp
->sql_dat
.s
;
546 il
= sqlp
->sql_dat
.l
;
547 flags
= sqlp
->sql_flags
;
549 /* Iterate over the entire input, classify characters and type of quotes
550 * along the way. Whenever a quote change has to be applied, adjust flags
551 * for the new situation -, setup sql.* and recurse- */
557 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
559 if(c
== '\t' && (flags
& (a_SHEXP_QUOTE_T_REVSOL
|
560 a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOUBLE
)))
562 #ifdef a_SHEXP_QUOTE_RECURSE
563 ++sqcp
->sqc_cnt_dollar
;
565 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
567 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
568 /* Whereas we don't support those, quote them for the sh(1)ell */
569 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
570 c
== '"' || c
== '$'){
571 if(flags
& a_SHEXP_QUOTE_T_MASK
)
573 #ifdef a_SHEXP_QUOTE_RECURSE
574 ++sqcp
->sqc_cnt_single
;
576 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
579 if(flags
& (a_SHEXP_QUOTE_T_MASK
& ~a_SHEXP_QUOTE_T_SINGLE
))
581 #ifdef a_SHEXP_QUOTE_RECURSE
582 ++sqcp
->sqc_cnt_dollar
;
584 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
586 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
587 if(flags
& a_SHEXP_QUOTE_T_MASK
)
589 #ifdef a_SHEXP_QUOTE_RECURSE
590 ++sqcp
->sqc_cnt_single
;
592 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
594 }else if(!asciichar(c
)){
595 /* Need to keep together multibytes */
596 #ifdef a_SHEXP_QUOTE_RECURSE
597 memset(&vic
, 0, sizeof vic
);
601 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
603 /* xxx check whether resulting \u would be ASCII */
604 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
) ||
605 (flags
& a_SHEXP_QUOTE_T_DOLLAR
)){
606 #ifdef a_SHEXP_QUOTE_RECURSE
614 #ifdef a_SHEXP_QUOTE_RECURSE
615 ++sqcp
->sqc_cnt_dollar
;
617 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
623 sqlp
->sql_flags
= flags
;
625 /* Level made the great and completed processing input. Reverse the list of
626 * levels, detect the "most fancy" quote type needed along this way */
627 /* XXX Due to restriction as above very crude */
628 for(flags
= 0, il
= 0, u
.head
= NULL
; sqlp
!= NULL
;){
629 struct a_shexp_quote_lvl
*tmp
;
631 tmp
= sqlp
->sql_link
;
632 sqlp
->sql_link
= u
.head
;
634 il
+= sqlp
->sql_dat
.l
;
635 if(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
)
636 il
+= (sqlp
->sql_dat
.l
>> 1);
637 flags
|= sqlp
->sql_flags
;
642 /* Finally work the substrings in the correct order, adjusting quotes along
643 * the way as necessary. Start off with the "most fancy" quote, so that
644 * the user sees an overall boundary she can orientate herself on.
645 * We do it like that to be able to give the user some "encapsulation
646 * experience", to address what strikes me is a problem of sh(1)ell quoting:
647 * different to, e.g., perl(1), where you see at a glance where a string
648 * starts and ends, sh(1) quoting occurs at the "top level", disrupting the
649 * visual appearance of "a string" as such */
650 u
.store
= n_string_reserve(sqcp
->sqc_store
, il
);
652 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
653 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
654 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOLLAR
;
655 }else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
){
656 u
.store
= n_string_push_c(u
.store
, '"');
657 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_DOUBLE
;
658 }else if(flags
& a_SHEXP_QUOTE_T_SINGLE
){
659 u
.store
= n_string_push_c(u
.store
, '\'');
660 flags
= (flags
& ~a_SHEXP_QUOTE_T_MASK
) | a_SHEXP_QUOTE_T_SINGLE
;
661 }else /*if(flags & a_SHEXP_QUOTE_T_REVSOL)*/
662 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
664 /* Work all the levels */
665 for(; sqlp
!= NULL
; sqlp
= sqlp
->sql_link
){
666 /* As necessary update our mode of quoting */
667 #ifdef a_SHEXP_QUOTE_RECURSE
670 switch(sqlp
->sql_flags
& a_SHEXP_QUOTE_T_MASK
){
671 case a_SHEXP_QUOTE_T_DOLLAR
:
672 if(!(flags
& a_SHEXP_QUOTE_T_DOLLAR
))
673 il
= a_SHEXP_QUOTE_T_DOLLAR
;
675 case a_SHEXP_QUOTE_T_DOUBLE
:
676 if(!(flags
& (a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
677 il
= a_SHEXP_QUOTE_T_DOLLAR
;
679 case a_SHEXP_QUOTE_T_SINGLE
:
680 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
681 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
682 il
= a_SHEXP_QUOTE_T_SINGLE
;
685 case a_SHEXP_QUOTE_T_REVSOL
:
686 if(!(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
687 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)))
688 il
= a_SHEXP_QUOTE_T_REVSOL
;
693 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
694 u
.store
= n_string_push_c(u
.store
, '\'');
695 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
696 u
.store
= n_string_push_c(u
.store
, '"');
697 flags
&= ~a_SHEXP_QUOTE_T_MASK
;
700 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
)
701 u
.store
= n_string_push_buf(u
.store
, "$'", sizeof("$'") -1);
702 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
703 u
.store
= n_string_push_c(u
.store
, '"');
704 else if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
705 u
.store
= n_string_push_c(u
.store
, '\'');
707 #endif /* a_SHEXP_QUOTE_RECURSE */
709 /* Work the level's substring */
710 ib
= sqlp
->sql_dat
.s
;
711 il
= sqlp
->sql_dat
.l
;
719 assert(c
== '\t' || (flags
& a_SHEXP_QUOTE_T_DOLLAR
));
720 assert((flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_SINGLE
|
721 a_SHEXP_QUOTE_T_DOUBLE
| a_SHEXP_QUOTE_T_DOLLAR
)));
723 case 0x07: c
= 'a'; break;
724 case 0x08: c
= 'b'; break;
725 case 0x0A: c
= 'n'; break;
726 case 0x0B: c
= 'v'; break;
727 case 0x0C: c
= 'f'; break;
728 case 0x0D: c
= 'r'; break;
729 case 0x1B: c
= 'E'; break;
732 if(flags
& a_SHEXP_QUOTE_T_DOLLAR
){
736 if(flags
& a_SHEXP_QUOTE_T_REVSOL
)
737 u
.store
= n_string_push_c(u
.store
, '\\');
740 u
.store
= n_string_push_c(u
.store
, '\\');
742 u
.store
= n_string_push_c(u
.store
, 'c');
746 }else if(blankspacechar(c
) || c
== '|' || c
== '&' || c
== ';' ||
747 /* Whereas we don't support those, quote them for the sh(1)ell */
748 c
== '(' || c
== ')' || c
== '<' || c
== '>' ||
749 c
== '"' || c
== '$'){
750 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
752 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
));
753 u
.store
= n_string_push_c(u
.store
, '\\');
756 if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
758 assert(!(flags
& a_SHEXP_QUOTE_T_SINGLE
));
759 u
.store
= n_string_push_c(u
.store
, '\\');
761 }else if(c
== '\\' || (c
== '#' && ib
== ib_base
)){
762 if(flags
& a_SHEXP_QUOTE_T_SINGLE
)
764 assert(flags
& (a_SHEXP_QUOTE_T_REVSOL
| a_SHEXP_QUOTE_T_DOUBLE
|
765 a_SHEXP_QUOTE_T_DOLLAR
));
766 u
.store
= n_string_push_c(u
.store
, '\\');
768 }else if(asciichar(c
)){
769 /* Shorthand: we can simply push that thing out */
771 u
.store
= n_string_push_c(u
.store
, c
);
774 /* Not an ASCII character, take care not to split up multibyte
775 * sequences etc. For the sake of compile testing, don't enwrap in
776 * HAVE_ALWAYS_UNICODE_LOCALE || HAVE_NATCH_CHAR */
777 if(n_psonce
& n_PSO_UNICODE
){
784 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
788 il2
= PTR2SIZE(&ib2
[0] - &ib
[0]);
789 if((flags
& a_SHEXP_QUOTE_ROUNDTRIP
) || uc
== 0xFFFDu
){
790 /* Use padding to make ambiguities impossible */
791 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
792 (uc
> 0xFFFFu
? 'U' : 'u'),
793 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
799 u
.store
= n_string_push_buf(u
.store
, cp
, il3
);
800 ib
+= il2
, il
-= il2
;
805 memset(&vic
, 0, sizeof vic
);
809 n_VISUAL_INFO_ONE_CHAR
| n_VISUAL_INFO_SKIP_ERRORS
);
811 /* Work this substring as sensitive as possible */
813 if(!(flags
& a_SHEXP_QUOTE_ROUNDTRIP
))
814 u
.store
= n_string_push_buf(u
.store
, ib
, il
);
816 else if((vic
.vic_indat
= n_iconv_onetime_cp(n_ICONV_NONE
,
817 "utf-8", ok_vlook(ttycharset
), savestrbuf(ib
, il
))) != NULL
){
822 il2
= strlen(ib2
= vic
.vic_indat
);
823 if((uc
= n_utf8_to_utf32(&ib2
, &il2
)) != UI32_MAX
){
826 il2
= PTR2SIZE(&ib2
[0] - &vic
.vic_indat
[0]);
827 /* Use padding to make ambiguities impossible */
828 il3
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
829 (uc
> 0xFFFFu
? 'U' : 'u'),
830 (int)(uc
> 0xFFFFu
? 8 : 4), uc
);
831 u
.store
= n_string_push_buf(u
.store
, itoa
, il3
);
841 u
.store
= n_string_push_buf(u
.store
, "\\xFF",
843 n_c_to_hex_base16(&u
.store
->s_dat
[u
.store
->s_len
- 2], *ib
++);
852 /* Close an open quote */
853 if(flags
& (a_SHEXP_QUOTE_T_SINGLE
| a_SHEXP_QUOTE_T_DOLLAR
))
854 u
.store
= n_string_push_c(u
.store
, '\'');
855 else if(flags
& a_SHEXP_QUOTE_T_DOUBLE
)
856 u
.store
= n_string_push_c(u
.store
, '"');
857 #ifdef a_SHEXP_QUOTE_RECURSE
863 #ifdef a_SHEXP_QUOTE_RECURSE
865 sqlp
->sql_dat
.l
-= il
;
868 sql
.sql_dat
.s
= n_UNCONST(ib
);
870 sql
.sql_flags
= flags
;
871 a_shexp__quote(sqcp
, &sql
);
876 #undef a_SHEXP_QUOTE_RECURSE
880 fexpand(char const *name
, enum fexp_mode fexpm
) /* TODO in parts: -> URL::!! */
883 char const *res
, *cp
;
884 bool_t dyn
, haveproto
;
887 n_pstate
&= ~n_PS_EXPAND_MULTIRESULT
;
890 /* The order of evaluation is "%" and "#" expand into constants.
891 * "&" can expand into "+". "+" can expand into shell meta characters.
892 * Shell meta characters expand into constants.
893 * This way, we make no recursive expansion */
894 if((fexpm
& FEXP_NSHORTCUT
) || (res
= shortcut_expand(name
)) == NULL
)
895 res
= n_UNCONST(name
);
898 n_UNINIT(proto
.s
, NULL
), n_UNINIT(proto
.l
, 0);
900 for(cp
= res
; *cp
&& *cp
!= ':'; ++cp
)
903 if(cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/'){
905 proto
.s
= n_UNCONST(res
);
907 proto
.l
= PTR2SIZE(cp
- res
);
912 if(!(fexpm
& FEXP_NSPECIAL
)){
917 if(res
[1] == ':' && res
[2] != '\0'){
923 force
= (res
[1] != '\0');
924 res
= a_shexp_findmail((force
? &res
[1] : ok_vlook(LOGNAME
)),
933 if (prevfile
[0] == '\0') {
934 n_err(_("No previous file\n"));
942 res
= ok_vlook(MBOX
);
950 if(res
[0] == '@' && which_protocol(mailname
, FAL0
, FAL0
, NULL
)
952 res
= str_concat_csvl(&s
, protbase(mailname
), "/", &res
[1], NULL
)->s
;
957 /* POSIX: if *folder* unset or null, "+" shall be retained */
958 if(!(fexpm
& FEXP_NFOLDER
) && *res
== '+' &&
959 *(cp
= n_folder_query()) != '\0'){
960 res
= str_concat_csvl(&s
, cp
, &res
[1], NULL
)->s
;
964 /* Do some meta expansions */
965 if((fexpm
& (FEXP_NSHELL
| FEXP_NVAR
)) != FEXP_NVAR
&&
966 ((fexpm
& FEXP_NSHELL
) ? (strchr(res
, '$') != NULL
)
967 : n_anyof_cp("{}[]*?$", res
))){
970 if(fexpm
& FEXP_NOPROTO
)
973 cp
= haveproto
? savecat(savestrbuf(proto
.s
, proto
.l
), res
) : res
;
975 switch(which_protocol(cp
, TRU1
, FAL0
, NULL
)){
988 struct n_string shou
, *shoup
;
990 shin
.s
= n_UNCONST(res
);
992 shoup
= n_string_creat_auto(&shou
);
994 enum n_shexp_state shs
;
996 /* TODO shexp: take care to not include backtick eval once avail! */
997 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG_D_V
|
998 n_SHEXP_PARSE_QUOTE_AUTO_FIXED
| n_SHEXP_PARSE_QUOTE_AUTO_DQ
|
999 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
), shoup
, &shin
, NULL
);
1000 if(shs
& n_SHEXP_STATE_STOP
)
1003 res
= n_string_cp(shoup
);
1004 /*shoup = n_string_drop_ownership(shoup);*/
1008 res
= a_shexp_tilde(res
);
1010 if(!(fexpm
& FEXP_NSHELL
) &&
1011 (res
= a_shexp_globname(res
, fexpm
)) == NULL
)
1014 }/* else no tilde */
1015 }else if(res
[0] == '~'){
1016 res
= a_shexp_tilde(res
);
1021 if(res
!= NULL
&& haveproto
){
1022 res
= savecat(savestrbuf(proto
.s
, proto
.l
), res
);
1026 if(fexpm
& (FEXP_LOCAL
| FEXP_LOCAL_FILE
)){
1027 switch (which_protocol(res
, FAL0
, FAL0
, &cp
)) {
1029 if(!(fexpm
& FEXP_LOCAL_FILE
)){
1032 if(fexpm
& FEXP_LOCAL_FILE
){
1040 n_err(_("Not a local file or directory: %s\n"),
1041 n_shexp_quote_cp(name
, FAL0
));
1048 if(res
!= NULL
&& !dyn
)
1051 return n_UNCONST(res
);
1054 FL
enum n_shexp_state
1055 n_shexp_parse_token(enum n_shexp_parse_flags flags
, struct n_string
*store
,
1056 struct str
*input
, void const **cookie
){
1057 /* TODO shexp_parse_token: WCHAR
1058 * TODO This needs to be rewritten in order to support $(( )) and $( )
1059 * TODO and ${xyYZ} and the possibly infinite recursion they bring along,
1060 * TODO too. We need a carrier struct, then, and can nicely split this
1061 * TODO big big thing up in little pieces!
1062 * TODO This means it should produce a tree of objects, so that callees
1063 * TODO can recognize whether something happened inside single/double etc.
1064 * TODO quotes; e.g., to requote "'[a-z]'" to, e.g., "\[a-z]", etc.! */
1065 ui32_t last_known_meta_trim_len
;
1066 char c2
, c
, quotec
, utf
[8];
1067 enum n_shexp_state rv
;
1069 char const *ifs
, *ifs_ws
, *ib_save
, *ib
;
1072 a_SKIPQ
= 1u<<0, /* Skip rest of this quote (\u0 ..) */
1073 a_SKIPT
= 1u<<1, /* Skip entire token (\c@) */
1074 a_SKIPMASK
= a_SKIPQ
| a_SKIPT
,
1075 a_SURPLUS
= 1u<<2, /* Extended sequence interpretation */
1076 a_NTOKEN
= 1u<<3, /* "New token": e.g., comments are possible */
1077 a_BRACE
= 1u<<4, /* Variable substitution: brace enclosed */
1078 a_DIGIT1
= 1u<<5, /* ..first character was digit */
1079 a_NONDIGIT
= 1u<<6, /* ..has seen any non-digits */
1080 a_VARSUBST_MASK
= n_BITENUM_MASK(4, 6),
1082 a_ROUND_MASK
= a_SKIPT
| (int)~n_BITENUM_MASK(0, 7),
1085 a_CONSUME
= 1u<<10, /* When done, "consume" remaining input */
1090 assert((flags
& n_SHEXP_PARSE_DRYRUN
) || store
!= NULL
);
1091 assert(input
!= NULL
);
1092 assert(input
->l
== 0 || input
->s
!= NULL
);
1093 assert(!(flags
& n_SHEXP_PARSE_LOG
) || !(flags
& n_SHEXP_PARSE_LOG_D_V
));
1094 assert(!(flags
& n_SHEXP_PARSE_IFS_ADD_COMMA
) ||
1095 !(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
));
1096 assert(!(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
) ||
1097 (flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
));
1099 if((flags
& n_SHEXP_PARSE_LOG_D_V
) && (n_poption
& n_PO_D_V
))
1100 flags
|= n_SHEXP_PARSE_LOG
;
1101 if(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)
1102 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
;
1104 if((flags
& n_SHEXP_PARSE_TRUNC
) && store
!= NULL
)
1105 store
= n_string_trunc(store
, 0);
1107 if(flags
& (n_SHEXP_PARSE_IFS_VAR
| n_SHEXP_PARSE_TRIM_IFSSPACE
)){
1108 ifs
= ok_vlook(ifs
);
1109 ifs_ws
= ok_vlook(ifs_ws
);
1111 n_UNINIT(ifs
, n_empty
);
1112 n_UNINIT(ifs_ws
, n_empty
);
1117 if((il
= input
->l
) == UIZ_MAX
)
1118 input
->l
= il
= strlen(ib
);
1121 if(cookie
!= NULL
&& *cookie
!= NULL
){
1122 assert(!(flags
& n_SHEXP_PARSE_DRYRUN
));
1126 rv
= n_SHEXP_STATE_NONE
;
1128 rv
&= n_SHEXP_STATE_WS_LEAD
;
1129 state
&= a_ROUND_MASK
;
1131 /* In cookie mode, the next ARGV entry is the token already, unchanged,
1132 * since it has already been expanded before! */
1133 if(state
& a_COOKIE
){
1134 char const * const *xcookie
, *cp
;
1138 if((store
= n_string_push_cp(store
, *xcookie
))->s_len
> 0)
1139 rv
|= n_SHEXP_STATE_OUTPUT
;
1140 if(*++xcookie
== NULL
){
1143 flags
|= n_SHEXP_PARSE_QUOTE_AUTO_DQ
; /* ..why we are here! */
1145 *cookie
= n_UNCONST(xcookie
);
1147 for(cp
= &n_string_cp(store
)[i
]; (c
= *cp
++) != '\0';)
1149 rv
|= n_SHEXP_STATE_CONTROL
;
1153 /* The last exploded cookie will join with the yielded input token, so
1154 * simply fall through in this case */
1155 if(state
& a_COOKIE
)
1159 if(flags
& n_SHEXP_PARSE_TRIM_SPACE
){
1160 for(; il
> 0; ++ib
, --il
){
1161 if(!blankspacechar(*ib
))
1163 rv
|= n_SHEXP_STATE_WS_LEAD
;
1167 if(flags
& n_SHEXP_PARSE_TRIM_IFSSPACE
){
1168 for(; il
> 0; ++ib
, --il
){
1169 if(strchr(ifs_ws
, *ib
) == NULL
)
1171 rv
|= n_SHEXP_STATE_WS_LEAD
;
1175 input
->s
= n_UNCONST(ib
);
1180 rv
|= n_SHEXP_STATE_STOP
;
1185 store
= n_string_reserve(store
, n_MIN(il
, 32)); /* XXX */
1187 switch(flags
& n__SHEXP_PARSE_QUOTE_AUTO_MASK
){
1188 case n_SHEXP_PARSE_QUOTE_AUTO_SQ
:
1190 rv
|= n_SHEXP_STATE_QUOTE
;
1192 case n_SHEXP_PARSE_QUOTE_AUTO_DQ
:
1195 case n_SHEXP_PARSE_QUOTE_AUTO_DSQ
:
1198 rv
|= n_SHEXP_STATE_QUOTE
;
1207 /* TODO n_SHEXP_PARSE_META_SEMICOLON++, well, hack: we are not the shell,
1208 * TODO we are not a language, and therefore the general *ifs-ws* and normal
1209 * TODO whitespace trimming that input lines undergo (in a_go_evaluate())
1210 * TODO has already happened, our result will be used *as is*, and therefore
1211 * TODO we need to be aware of and remove trailing unquoted WS that would
1212 * TODO otherwise remain, after we have seen a semicolon sequencer.
1213 * By sheer luck we only need to track this in non-quote-mode */
1214 last_known_meta_trim_len
= UI32_MAX
;
1216 while(il
> 0){ /* {{{ */
1219 /* If no quote-mode active.. */
1221 if(c
== '"' || c
== '\''){
1226 state
&= ~a_SURPLUS
;
1228 last_known_meta_trim_len
= UI32_MAX
;
1229 rv
|= n_SHEXP_STATE_QUOTE
;
1234 last_known_meta_trim_len
= UI32_MAX
;
1239 rv
|= n_SHEXP_STATE_QUOTE
;
1244 }else if(c
== '\\'){
1245 /* Outside of quotes this just escapes any next character, but a sole
1246 * <reverse solidus> at EOS is left unchanged */
1250 last_known_meta_trim_len
= UI32_MAX
;
1252 /* A comment may it be if no token has yet started */
1253 else if(c
== '#' && (state
& a_NTOKEN
)){
1254 rv
|= n_SHEXP_STATE_STOP
;
1255 /*last_known_meta_trim_len = UI32_MAX;*/
1258 /* Metacharacters which separate tokens must be turned on explicitly */
1259 else if(c
== '|' && (flags
& n_SHEXP_PARSE_META_VERTBAR
)){
1260 rv
|= n_SHEXP_STATE_META_VERTBAR
;
1262 /* The parsed sequence may be _the_ output, so ensure we don't
1263 * include the metacharacter, then. */
1264 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1266 /*last_known_meta_trim_len = UI32_MAX;*/
1268 }else if(c
== '&' && (flags
& n_SHEXP_PARSE_META_AMPERSAND
)){
1269 rv
|= n_SHEXP_STATE_META_AMPERSAND
;
1271 /* The parsed sequence may be _the_ output, so ensure we don't
1272 * include the metacharacter, then. */
1273 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1275 /*last_known_meta_trim_len = UI32_MAX;*/
1277 }else if(c
== ';' && (flags
& n_SHEXP_PARSE_META_SEMICOLON
)){
1279 n_go_input_inject(n_GO_INPUT_INJECT_COMMIT
, ib
, il
);
1280 rv
|= n_SHEXP_STATE_META_SEMICOLON
| n_SHEXP_STATE_STOP
;
1282 if(!(flags
& n_SHEXP_PARSE_DRYRUN
) && (rv
& n_SHEXP_STATE_OUTPUT
) &&
1283 last_known_meta_trim_len
!= UI32_MAX
)
1284 store
= n_string_trunc(store
, last_known_meta_trim_len
);
1286 /* The parsed sequence may be _the_ output, so ensure we don't
1287 * include the metacharacter, then. */
1288 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1290 /*last_known_meta_trim_len = UI32_MAX;*/
1292 }else if(c
== ',' && (flags
&
1293 (n_SHEXP_PARSE_IFS_ADD_COMMA
| n_SHEXP_PARSE_IFS_IS_COMMA
))){
1294 /* The parsed sequence may be _the_ output, so ensure we don't
1295 * include the metacharacter, then. */
1296 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1298 /*last_known_meta_trim_len = UI32_MAX;*/
1303 blnk
= blankchar(c
) ? 1 : 0;
1304 blnk
|= ((flags
& (n_SHEXP_PARSE_IFS_VAR
|
1305 n_SHEXP_PARSE_TRIM_IFSSPACE
)) &&
1306 strchr(ifs_ws
, c
) != NULL
) ? 2 : 0;
1308 if((!(flags
& n_SHEXP_PARSE_IFS_VAR
) && (blnk
& 1)) ||
1309 ((flags
& n_SHEXP_PARSE_IFS_VAR
) &&
1310 ((blnk
& 2) || strchr(ifs
, c
) != NULL
))){
1311 if(!(flags
& n_SHEXP_PARSE_IFS_IS_COMMA
)){
1312 /* The parsed sequence may be _the_ output, so ensure we don't
1313 * include the metacharacter, then. */
1314 if(flags
& (n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_META_KEEP
))
1316 /*last_known_meta_trim_len = UI32_MAX;*/
1323 if(blnk
&& store
!= NULL
){
1324 if(last_known_meta_trim_len
== UI32_MAX
)
1325 last_known_meta_trim_len
= store
->s_len
;
1327 last_known_meta_trim_len
= UI32_MAX
;
1331 assert(!(state
& a_NTOKEN
));
1332 if(c
== quotec
&& !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_FIXED
)){
1333 state
&= a_ROUND_MASK
;
1335 /* Users may need to recognize the presence of empty quotes */
1336 rv
|= n_SHEXP_STATE_OUTPUT
;
1338 }else if(c
== '\\' && (state
& a_SURPLUS
)){
1340 /* A sole <reverse solidus> at EOS is treated as-is! This is ok
1341 * since the "closing quote" error will occur next, anyway */
1344 else if((c2
= *ib
) == quotec
){
1347 }else if(quotec
== '"'){
1348 /* Double quotes, POSIX says:
1349 * The <backslash> shall retain its special meaning as an
1350 * escape character (see Section 2.2.1) only when followed
1351 * by one of the following characters when considered
1352 * special: $ ` " \ <newline> */
1356 /* case '"': already handled via c2 == quotec */
1365 /* Dollar-single-quote */
1369 /* case '\'': already handled via c2 == quotec */
1374 case 'b': c
= '\b'; break;
1375 case 'f': c
= '\f'; break;
1376 case 'n': c
= '\n'; break;
1377 case 'r': c
= '\r'; break;
1378 case 't': c
= '\t'; break;
1379 case 'v': c
= '\v'; break;
1382 case 'e': c
= '\033'; break;
1384 /* Control character */
1387 goto j_dollar_ungetc
;
1389 if(state
& a_SKIPMASK
)
1391 /* ASCII C0: 0..1F, 7F <- @.._ (+ a-z -> A-Z), ? */
1392 c
= upperconv(c2
) ^ 0x40;
1393 if((ui8_t
)c
> 0x1F && c
!= 0x7F){
1394 if(flags
& n_SHEXP_PARSE_LOG
)
1395 n_err(_("Invalid \\c notation: %.*s: %.*s\n"),
1396 (int)input
->l
, input
->s
,
1397 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1398 rv
|= n_SHEXP_STATE_ERR_CONTROL
;
1400 /* As an implementation-defined extension, support \c@
1401 * EQ printf(1) alike \c */
1408 /* Octal sequence: 1 to 3 octal bytes */
1410 /* As an extension (dependent on where you look, echo(1), or
1411 * awk(1)/tr(1) etc.), allow leading "0" octal indicator */
1412 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1417 case '1': case '2': case '3':
1418 case '4': case '5': case '6': case '7':
1420 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1421 c2
= (c2
<< 3) | (c
- '0');
1424 if(il
> 0 && (c
= *ib
) >= '0' && c
<= '7'){
1425 if(!(state
& a_SKIPMASK
) && (ui8_t
)c2
> 0x1F){
1426 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1428 if(flags
& n_SHEXP_PARSE_LOG
)
1429 n_err(_("\\0 argument exceeds a byte: %.*s: %.*s\n"),
1430 (int)input
->l
, input
->s
,
1431 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1432 /* Write unchanged */
1434 rv
|= n_SHEXP_STATE_OUTPUT
;
1435 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1436 store
= n_string_push_buf(store
, ib_save
,
1437 PTR2SIZE(ib
- ib_save
));
1440 c2
= (c2
<< 3) | (c
-= '0');
1443 if(state
& a_SKIPMASK
)
1445 if((c
= c2
) == '\0'){
1451 /* ISO 10646 / Unicode sequence, 8 or 4 hexadecimal bytes */
1460 goto j_dollar_ungetc
;
1464 /* Hexadecimal sequence, 1 or 2 hexadecimal bytes */
1468 goto j_dollar_ungetc
;
1472 static ui8_t
const hexatoi
[] = { /* XXX uses ASCII */
1473 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1478 for(no
= j
= 0; i
-- > 0; --il
, ++ib
, ++j
){
1482 no
+= hexatoi
[(ui8_t
)((c
) - ((c
) <= '9' ? 48
1483 : ((c
) <= 'F' ? 55 : 87)))];
1485 if(state
& a_SKIPMASK
)
1487 c2
= (c2
== 'U' || c2
== 'u') ? 'u' : 'x';
1488 if(flags
& n_SHEXP_PARSE_LOG
)
1489 n_err(_("Invalid \\%c notation: %.*s: %.*s\n"),
1490 c2
, (int)input
->l
, input
->s
,
1491 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1492 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1498 /* Unicode massage */
1499 if((c2
!= 'U' && c2
!= 'u') || n_uasciichar(no
)){
1500 if((c
= (char)no
) == '\0')
1504 else if(!(state
& a_SKIPMASK
)){
1505 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1506 store
= n_string_reserve(store
, n_MAX(j
, 4));
1508 if(no
> 0x10FFFF){ /* XXX magic; CText */
1509 if(flags
& n_SHEXP_PARSE_LOG
)
1510 n_err(_("\\U argument exceeds 0x10FFFF: %.*s: "
1512 (int)input
->l
, input
->s
,
1513 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1514 rv
|= n_SHEXP_STATE_ERR_NUMBER
;
1515 /* But normalize the output anyway */
1519 j
= n_utf32_to_utf8(no
, utf
);
1521 if(n_psonce
& n_PSO_UNICODE
){
1522 rv
|= n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_UNICODE
;
1523 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1524 store
= n_string_push_buf(store
, utf
, j
);
1531 icp
= n_iconv_onetime_cp(n_ICONV_NONE
,
1534 rv
|= n_SHEXP_STATE_OUTPUT
;
1535 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1536 store
= n_string_push_cp(store
, icp
);
1541 if(!(flags
& n_SHEXP_PARSE_DRYRUN
)) Jerr_uni_norm
:{
1544 rv
|= n_SHEXP_STATE_OUTPUT
|
1545 n_SHEXP_STATE_ERR_UNICODE
;
1546 i
= snprintf(itoa
, sizeof itoa
, "\\%c%0*X",
1547 (no
> 0xFFFFu
? 'U' : 'u'),
1548 (int)(no
> 0xFFFFu
? 8 : 4), (ui32_t
)no
);
1549 store
= n_string_push_buf(store
, itoa
, i
);
1553 if(state
& a_SKIPMASK
)
1558 /* Extension: \$ can be used to expand a variable.
1559 * B(ug|ad) effect: if conversion fails, not written "as-is" */
1562 goto j_dollar_ungetc
;
1567 /* Follow bash(1) behaviour, print sequence unchanged */
1572 }else if(c
== '$' && quotec
== '"' && il
> 0) J_var_expand
:{
1573 state
&= ~a_VARSUBST_MASK
;
1577 /* Scan variable name */
1578 if(!(state
& a_BRACE
) || il
> 1){
1579 char const *cp
, *vp
;
1585 state
&= ~a_EXPLODE
;
1587 for(i
= 0; il
> 0; --il
, ++ib
, ++i
){
1588 /* We have some special cases regarding special parameters,
1589 * so ensure these don't cause failure. This code has
1590 * counterparts in code that manages internal variables! */
1592 if(!a_SHEXP_ISVARC(c
)){
1594 /* Simply skip over multiplexer */
1597 if(c
== '*' || c
== '@' || c
== '#' || c
== '?' ||
1608 }else if(a_SHEXP_ISVARC_BAD1ST(c
)){
1612 state
|= a_NONDIGIT
;
1615 /* In skip mode, be easy and.. skip over */
1616 if(state
& a_SKIPMASK
){
1617 if((state
& a_BRACE
) && il
> 0 && *ib
== '}')
1622 /* Handle the scan error cases */
1623 if((state
& (a_DIGIT1
| a_NONDIGIT
)) == (a_DIGIT1
| a_NONDIGIT
)){
1624 if(state
& a_BRACE
){
1625 if(il
> 0 && *ib
== '}')
1628 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1630 if(flags
& n_SHEXP_PARSE_LOG
)
1631 n_err(_("Invalid identifier for ${}: %.*s: %.*s\n"),
1632 (int)input
->l
, input
->s
,
1633 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1634 rv
|= n_SHEXP_STATE_ERR_IDENTIFIER
;
1637 if(state
& a_BRACE
){
1638 if(il
== 0 || *ib
!= '}'){
1639 if(flags
& n_SHEXP_PARSE_LOG
)
1640 n_err(_("No closing brace for ${}: %.*s: %.*s\n"),
1641 (int)input
->l
, input
->s
,
1642 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1643 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1649 if(flags
& n_SHEXP_PARSE_LOG
)
1650 n_err(_("Bad substitution for ${}: %.*s: %.*s\n"),
1651 (int)input
->l
, input
->s
,
1652 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1653 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1657 /* Simply write dollar as-is? */
1660 if(state
& a_BRACE
){
1661 if(il
== 0 || *ib
!= '}'){
1662 if(flags
& n_SHEXP_PARSE_LOG
)
1663 n_err(_("No closing brace for ${}: %.*s: %.*s\n"),
1664 (int)input
->l
, input
->s
,
1665 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1666 rv
|= n_SHEXP_STATE_ERR_GROUPOPEN
;
1672 if(flags
& n_SHEXP_PARSE_LOG
)
1673 n_err(_("Bad substitution for ${}: %.*s: %.*s\n"),
1674 (int)input
->l
, input
->s
,
1675 (int)PTR2SIZE(ib
- ib_save
), ib_save
);
1676 rv
|= n_SHEXP_STATE_ERR_BADSUB
;
1681 if(flags
& n_SHEXP_PARSE_DRYRUN
)
1684 /* We may shall explode "${@}" to a series of successive,
1685 * properly quoted tokens (instead). The first exploded
1686 * cookie will join with the current token */
1687 if(n_UNLIKELY(state
& a_EXPLODE
) &&
1688 !(flags
& n_SHEXP_PARSE_DRYRUN
) && cookie
!= NULL
){
1689 if(n_var_vexplode(cookie
))
1691 /* On the other hand, if $@ expands to nothing and is the
1692 * sole content of this quote then act like the shell does
1693 * and throw away the entire atxplode construct */
1694 else if(!(rv
& n_SHEXP_STATE_OUTPUT
) &&
1695 il
== 1 && *ib
== '"' &&
1696 ib_save
== &input
->s
[1] && ib_save
[-1] == '"')
1700 input
->s
= n_UNCONST(ib
);
1702 goto jrestart_empty
;
1705 /* Check getenv(3) shall no internal variable exist!
1706 * XXX We have some common idioms, avoid memory for them
1707 * XXX Even better would be var_vlook_buf()! */
1710 case '?': vp
= n_qm
; break;
1711 case '!': vp
= n_em
; break;
1712 case '*': vp
= n_star
; break;
1713 case '@': vp
= n_at
; break;
1714 case '#': vp
= n_ns
; break;
1715 default: goto j_var_look_buf
;
1719 vp
= savestrbuf(vp
, i
);
1721 if((cp
= n_var_vlook(vp
, TRU1
)) != NULL
){
1722 rv
|= n_SHEXP_STATE_OUTPUT
;
1723 store
= n_string_push_cp(store
, cp
);
1724 for(; (c
= *cp
) != '\0'; ++cp
)
1726 rv
|= n_SHEXP_STATE_CONTROL
;
1733 }else if(c
== '`' && quotec
== '"' && il
> 0){ /* TODO shell command */
1738 if(!(state
& a_SKIPMASK
)){
1739 rv
|= n_SHEXP_STATE_OUTPUT
;
1741 rv
|= n_SHEXP_STATE_CONTROL
;
1742 if(!(flags
& n_SHEXP_PARSE_DRYRUN
))
1743 store
= n_string_push_c(store
, c
);
1747 if(quotec
!= '\0' && !(flags
& n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
)){
1748 if(flags
& n_SHEXP_PARSE_LOG
)
1749 n_err(_("No closing quote: %.*s\n"), (int)input
->l
, input
->s
);
1750 rv
|= n_SHEXP_STATE_ERR_QUOTEOPEN
;
1754 assert(!(state
& a_COOKIE
));
1755 if((flags
& n_SHEXP_PARSE_DRYRUN
) && store
!= NULL
){
1756 store
= n_string_push_buf(store
, input
->s
, PTR2SIZE(ib
- input
->s
));
1757 rv
|= n_SHEXP_STATE_OUTPUT
;
1760 if(state
& a_CONSUME
){
1761 input
->s
= n_UNCONST(&ib
[il
]);
1764 if(flags
& n_SHEXP_PARSE_TRIM_SPACE
){
1765 for(; il
> 0; ++ib
, --il
){
1766 if(!blankspacechar(*ib
))
1768 rv
|= n_SHEXP_STATE_WS_TRAIL
;
1772 if(flags
& n_SHEXP_PARSE_TRIM_IFSSPACE
){
1773 for(; il
> 0; ++ib
, --il
){
1774 if(strchr(ifs_ws
, *ib
) == NULL
)
1776 rv
|= n_SHEXP_STATE_WS_TRAIL
;
1781 input
->s
= n_UNCONST(ib
);
1784 if(!(rv
& n_SHEXP_STATE_STOP
)){
1785 if(!(rv
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_META_MASK
)) &&
1786 (flags
& n_SHEXP_PARSE_IGNORE_EMPTY
) && il
> 0)
1787 goto jrestart_empty
;
1788 if(/*!(rv & n_SHEXP_STATE_OUTPUT) &&*/ il
== 0)
1789 rv
|= n_SHEXP_STATE_STOP
;
1792 if((state
& a_SKIPT
) && !(rv
& n_SHEXP_STATE_STOP
) &&
1793 (flags
& n_SHEXP_PARSE_META_MASK
))
1796 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_UNICODE
));
1797 assert((rv
& n_SHEXP_STATE_OUTPUT
) || !(rv
& n_SHEXP_STATE_CONTROL
));
1803 n_shexp_parse_token_cp(enum n_shexp_parse_flags flags
, char const **cp
){
1805 struct n_string sou
, *soup
;
1807 enum n_shexp_state shs
;
1812 input
.s
= n_UNCONST(*cp
);
1814 soup
= n_string_creat_auto(&sou
);
1816 shs
= n_shexp_parse_token(flags
, soup
, &input
, NULL
);
1817 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1818 soup
= n_string_assign_cp(soup
, *cp
);
1823 rv
= n_string_cp(soup
);
1824 /*n_string_gut(n_string_drop_ownership(soup));*/
1829 FL
struct n_string
*
1830 n_shexp_quote(struct n_string
*store
, struct str
const *input
, bool_t rndtrip
){
1831 struct a_shexp_quote_lvl sql
;
1832 struct a_shexp_quote_ctx sqc
;
1835 assert(store
!= NULL
);
1836 assert(input
!= NULL
);
1837 assert(input
->l
== 0 || input
->s
!= NULL
);
1839 memset(&sqc
, 0, sizeof sqc
);
1840 sqc
.sqc_store
= store
;
1841 sqc
.sqc_input
.s
= input
->s
;
1842 if((sqc
.sqc_input
.l
= input
->l
) == UIZ_MAX
)
1843 sqc
.sqc_input
.l
= strlen(input
->s
);
1844 sqc
.sqc_flags
= rndtrip
? a_SHEXP_QUOTE_ROUNDTRIP
: a_SHEXP_QUOTE_NONE
;
1846 if(sqc
.sqc_input
.l
== 0)
1847 store
= n_string_push_buf(store
, "''", sizeof("''") -1);
1849 memset(&sql
, 0, sizeof sql
);
1850 sql
.sql_dat
= sqc
.sqc_input
;
1851 sql
.sql_flags
= sqc
.sqc_flags
;
1852 a_shexp__quote(&sqc
, &sql
);
1859 n_shexp_quote_cp(char const *cp
, bool_t rndtrip
){
1860 struct n_string store
;
1867 input
.s
= n_UNCONST(cp
);
1869 rv
= n_string_cp(n_shexp_quote(n_string_creat_auto(&store
), &input
,
1871 n_string_gut(n_string_drop_ownership(&store
));
1877 n_shexp_is_valid_varname(char const *name
){
1884 for(lc
= '\0'; (c
= *name
++) != '\0'; lc
= c
)
1885 if(!a_SHEXP_ISVARC(c
))
1887 else if(lc
== '\0' && a_SHEXP_ISVARC_BAD1ST(c
))
1889 if(a_SHEXP_ISVARC_BADNST(lc
))
1899 c_shcodec(void *vp
){
1901 struct n_string sou_b
, *soup
;
1905 char const **argv
, *varname
, *act
, *cp
;
1907 soup
= n_string_creat_auto(&sou_b
);
1909 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
1912 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
1914 if((norndtrip
= (*act
== '+')))
1918 alen
= PTR2SIZE(cp
- act
);
1922 in
.l
= strlen(in
.s
= n_UNCONST(cp
));
1925 if(is_ascncaseprefix(act
, "encode", alen
))
1926 soup
= n_shexp_quote(soup
, &in
, !norndtrip
);
1927 else if(!norndtrip
&& is_ascncaseprefix(act
, "decode", alen
)){
1929 enum n_shexp_state shs
;
1931 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
1932 n_SHEXP_PARSE_IGNORE_EMPTY
), soup
, &in
, NULL
);
1933 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1934 soup
= n_string_assign_cp(soup
, cp
);
1935 nerrn
= n_ERR_CANCELED
;
1939 if(shs
& n_SHEXP_STATE_STOP
)
1945 if(varname
!= NULL
){
1946 cp
= n_string_cp(soup
);
1947 if(!n_var_vset(varname
, (uintptr_t)cp
)){
1948 nerrn
= n_ERR_NOTSUP
;
1954 in
.s
= n_string_cp(soup
);
1956 makeprint(&in
, &out
);
1957 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0){
1965 n_pstate_err_no
= nerrn
;
1967 return (vp
!= NULL
? 0 : 1);
1969 n_err(_("Synopsis: shcodec: <[+]e[ncode]|d[ecode]> <rest-of-line>\n"));
1970 nerrn
= n_ERR_INVAL
;