1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run make-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . Improve support for chains so that we can apply the property checks
8 *@ TODO of the base variable to -HOST and -USER@HOST variants! Add some!!
9 *@ TODO . once we can have non-fatal !0 returns for commands, we should
10 *@ TODO return error if "(environ)? unset" goes for non-existent.
11 *@ TODO . should be recursive environment based.
12 *@ TODO Otherwise, the `localopts' should be an attribute of the go.c
13 *@ TODO command context, so that it belongs to the execution context
14 *@ TODO we are running in, instead of being global data. See, e.g.,
15 *@ TODO the a_GO_SPLICE comment in go.c.
17 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
18 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
21 * Copyright (c) 1980, 1993
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 #define n_FILE accmacvar
51 #ifndef HAVE_AMALGAMATION
55 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
56 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
59 /* Positional parameter maximum (macro arguments, `vexpr' "splitifs") */
60 #define a_AMV_POSPAR_MAX SI16_MAX
62 /* Special "pseudo macro" that stabs you from the back */
63 #define a_AMV_MACKY_MACK ((struct a_amv_mac*)-1)
65 /* Note: changing the hash function must be reflected in `vexpr' "hash",
66 * because that is used by the hashtable creator scripts! */
67 #define a_AMV_PRIME HSHSIZE
68 #define a_AMV_NAME2HASH(N) n_torek_hash(N)
69 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
73 a_AMV_MF_ACCOUNT
= 1u<<0, /* This macro is an `account' */
74 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACCOUNT
,
75 a_AMV_MF_UNDEF
= 1u<<1, /* Unlink after lookup */
76 a_AMV_MF_DELETE
= 1u<<7, /* Delete in progress, free once refcnt==0 */
82 a_AMV_LF_SCOPE
= 1u<<0, /* Current scope `localopts' on */
83 a_AMV_LF_SCOPE_FIXATE
= 1u<<1, /* Ditto, but fixated */
84 a_AMV_LF_SCOPE_MASK
= a_AMV_LF_SCOPE
| a_AMV_LF_SCOPE_FIXATE
,
85 a_AMV_LF_CALL
= 1u<<2, /* `localopts' on for `call'ed scopes */
86 a_AMV_LF_CALL_FIXATE
= 1u<<3, /* Ditto, but fixated */
87 a_AMV_LF_CALL_MASK
= a_AMV_LF_CALL
| a_AMV_LF_CALL_FIXATE
,
88 a_AMV_LF_CALL_TO_SCOPE_SHIFT
= 2
91 /* make-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
92 * _IMPORT implies _ENV; it doesn't verify anything... */
96 /* The basic set of flags, also present in struct a_amv_var_map.avm_flags */
97 a_AMV_VF_BOOL
= 1u<<0, /* ok_b_* */
98 a_AMV_VF_VIRT
= 1u<<1, /* "Stateless" automatic variable */
99 a_AMV_VF_CHAIN
= 1u<<2, /* Is a variable chain (-USER{,@HOST} variants) */
100 a_AMV_VF_VIP
= 1u<<3, /* Wants _var_check_vips() evaluation */
101 a_AMV_VF_RDONLY
= 1u<<4, /* May not be set by user */
102 a_AMV_VF_NODEL
= 1u<<5, /* May not be deleted */
103 a_AMV_VF_I3VAL
= 1u<<6, /* Has an initial value */
104 a_AMV_VF_DEFVAL
= 1u<<7, /* Has a default value */
105 a_AMV_VF_IMPORT
= 1u<<8, /* Import ONLY from environ (pre n_PSO_STARTED) */
106 a_AMV_VF_ENV
= 1u<<9, /* Update environment on change */
107 a_AMV_VF_NOLOPTS
= 1u<<10, /* May not be tracked by `localopts' */
108 a_AMV_VF_NOTEMPTY
= 1u<<11, /* May not be assigned an empty value */
109 a_AMV_VF_NOCNTRLS
= 1u<<12, /* Value may not contain control characters */
110 a_AMV_VF_NUM
= 1u<<13, /* Value must be a 32-bit number */
111 a_AMV_VF_POSNUM
= 1u<<14, /* Value must be positive 32-bit number */
112 a_AMV_VF_LOWER
= 1u<<15, /* Values will be stored in a lowercase version */
113 a_AMV_VF__MASK
= (1u<<(15+1)) - 1,
115 /* Extended flags, not part of struct a_amv_var_map.avm_flags */
116 a_AMV_VF_E_LINKED
= 1u<<24, /* `environ' link'ed */
117 a_AMV_VF_E_FROZEN
= 1u<<25, /* Has been set by -S TODO NOT IMPLEMENTED! */
118 a_AMV_VF_E__MASK
= (1u<<(25+1)) - 1
121 /* We support some special parameter names for one(+)-letter variable names;
122 * note these have counterparts in the code that manages shell expansion!
123 * All these special variables are solely backed by n_var_vlook(), and besides
124 * there is only a_amv_var_revlookup() which knows about them */
125 enum a_amv_var_special_category
{
126 a_AMV_VSC_NONE
, /* Normal variable, no special treatment */
127 a_AMV_VSC_GLOBAL
, /* ${[?!]} are specially mapped, but global */
128 a_AMV_VSC_MULTIPLEX
, /* ${^.*} circumflex accent multiplexer */
129 a_AMV_VSC_POSPAR
, /* ${[1-9][0-9]*} positional parameters */
130 a_AMV_VSC_POSPAR_ENV
/* ${[*@#]} positional parameter support variables */
133 enum a_amv_var_special_type
{
135 a_AMV_VST_QM
, /* ? */
136 a_AMV_VST_EM
, /* ! */
138 /* This is special in that it is a multiplex indicator, the ^ is followed by
139 * a normal variable */
140 a_AMV_VST_CACC
, /* ^ (circumflex accent) */
141 /* _VSC_POSPAR_ENV */
142 a_AMV_VST_STAR
, /* * */
143 a_AMV_VST_AT
, /* @ */
144 a_AMV_VST_NOSIGN
/* # */
147 enum a_amv_var_vip_mode
{
154 ui16_t app_maxcount
; /* == slots in .app_dat */
155 ui16_t app_count
; /* Maximum a_AMV_POSPAR_MAX */
156 ui16_t app_idx
; /* `shift' moves this one, decs .app_count */
157 bool_t app_not_heap
; /* .app_dat stuff not on dynamically allocated */
159 char const **app_dat
; /* NULL terminated (for "$@" explosion support) */
161 n_CTA(a_AMV_POSPAR_MAX
<= SI16_MAX
, "Limit exceeds datatype capabilities");
164 struct a_amv_mac
*am_next
;
165 ui32_t am_maxlen
; /* of any line in .am_line_dat */
166 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
167 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
168 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
169 ui32_t am_refcnt
; /* 0-based for `un{account,define}' purposes */
170 ui8_t am_flags
; /* enum a_amv_mac_flags */
171 char am_name
[n_VFIELD_SIZE(3)]; /* of this macro */
173 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
175 struct a_amv_mac_line
{
177 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
178 char aml_dat
[n_VFIELD_SIZE(0)];
181 struct a_amv_mac_call_args
{
182 char const *amca_name
; /* For MACKY_MACK, this is *0*! */
183 struct a_amv_mac
*amca_amp
; /* "const", but for am_refcnt */
184 struct a_amv_var
**amca_unroller
;
185 void (*amca_hook_pre
)(void *);
188 bool_t amca_ps_hook_mask
;
189 bool_t amca_no_xcall
; /* We want n_GO_INPUT_NO_XCALL for this */
191 struct a_amv_pospar amca_pospar
;
194 struct a_amv_lostack
{
195 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
196 struct a_amv_mac_call_args
*as_amcap
;
197 struct a_amv_lostack
*as_up
; /* Outer context */
198 struct a_amv_var
*as_lopts
;
199 ui8_t as_loflags
; /* enum a_amv_mac_loflags */
204 struct a_amv_var
*av_link
;
207 char *av_env
; /* Actively managed putenv(3) memory */
209 ui32_t av_flags
; /* enum a_amv_var_flags */
210 char av_name
[n_VFIELD_SIZE(4)];
212 n_CTA(a_AMV_VF_E__MASK
<= UI32_MAX
, "Enumeration excesses storage datatype");
214 struct a_amv_var_map
{
217 ui16_t avm_flags
; /* enum a_amv_var_flags */
219 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
221 struct a_amv_var_virt
{
224 struct a_amv_var
const *avv_var
;
227 struct a_amv_var_defval
{
230 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
233 struct a_amv_var_carrier
{
234 char const *avc_name
;
237 struct a_amv_var
*avc_var
;
238 struct a_amv_var_map
const *avc_map
;
241 ui8_t avc_special_cat
;
242 /* Numerical parameter name if .avc_special_cat=a_AMV_VSC_POSPAR,
243 * otherwise a enum a_amv_var_special_type */
244 ui16_t avc_special_prop
;
247 /* Include constant make-okey-map.pl output, and the generated version data */
248 #include "gen-version.h"
249 #include "gen-okeys.h"
251 /* The currently active account */
252 static struct a_amv_mac
*a_amv_acc_curr
;
254 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
256 /* Unroll list of currently running macro stack */
257 static struct a_amv_lostack
*a_amv_lopts
;
259 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
261 /* Global (i.e., non-local) a_AMV_VSC_POSPAR stack */
262 static struct a_amv_pospar a_amv_pospar
;
264 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
265 * TODO today via a static lostack, it should be a field in mailbox, once that
266 * TODO is a real multi-instance object */
267 static struct a_amv_var
*a_amv_folder_hook_lopts
;
269 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
270 static struct a_amv_var
*a_amv_compose_lopts
;
272 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
273 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
274 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
275 * plain lookup cases or when _UNDEF was performed on a currently active entry
276 * (the entry will have been unlinked, and the _MF_DELETE will be honoured once
277 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
278 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
279 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
281 /* `call', `call_if' (and `xcall' via go.c -> c_call()) */
282 static int a_amv_mac_call(void *v
, bool_t silent_nexist
);
284 /* Execute a macro; amcap must reside in LOFI memory */
285 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
287 static void a_amv_mac__finalize(void *vp
);
289 /* User display helpers */
290 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
292 /* _def() returns error for faulty definitions and already existing * names,
293 * _undef() returns error if a named thing doesn't exist */
294 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
295 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
298 static void a_amv_mac_free(struct a_amv_mac
*amp
);
300 /* Update replay-log */
301 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
302 struct a_amv_var
*oavp
);
303 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
305 /* Special cased value string allocation */
306 static char *a_amv_var_copy(char const *str
);
307 static void a_amv_var_free(char *cp
);
309 /* Check for special housekeeping. _VIP_SET_POST and _VIP_CLEAR do not fail
310 * (or propagate errors), _VIP_SET_PRE may and should case abortion */
311 static bool_t
a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
,
312 enum okeys okey
, char const *val
);
314 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
315 static bool_t
a_amv_var_check_nocntrls(char const *val
);
316 static bool_t
a_amv_var_check_num(char const *val
, bool_t posnum
);
318 /* If a variable name begins with a lowercase-character and contains at
319 * least one '@', it is converted to all-lowercase. This is necessary
320 * for lookups of names based on email addresses.
321 * Following the standard, only the part following the last '@' should
322 * be lower-cased, but practice has established otherwise here */
323 static char const *a_amv_var_canonify(char const *vn
);
325 /* Try to reverse lookup an option name to an enum okeys mapping.
326 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
327 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
330 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
331 * Sets .avc_prime; .avc_var is NULL if not found.
332 * Here it is where we care for _I3VAL and _DEFVAL, as well as _E_STARTUP.
333 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
334 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
335 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
336 * return FAL0, then! */
337 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
340 /* Lookup functions for special category variables, _pospar drives all
341 * positional parameter etc. special categories */
342 static char const *a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
);
343 static char const *a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
);
344 static char const *a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
);
346 /* Set var from .avc_(map|name|hash), return success */
347 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
350 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
351 struct a_amv_var
*avp
);
353 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
354 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
356 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
358 /* List all variables */
359 static void a_amv_var_show_all(void);
361 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
363 /* Actually do print one, return number of lines written */
364 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
366 /* Shared c_set() and c_environ():set impl, return success */
367 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
369 static struct a_amv_mac
*
370 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
371 enum a_amv_mac_flags amf
){
372 struct a_amv_mac
*amp
, **ampp
;
374 enum a_amv_mac_flags save_amf
;
378 amf
&= a_AMV_MF_TYPE_MASK
;
379 h
= a_AMV_NAME2HASH(name
);
380 h
= a_AMV_HASH2PRIME(h
);
381 ampp
= &a_amv_macs
[h
];
383 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
384 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
385 !strcmp(amp
->am_name
, name
)){
386 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
389 *ampp
= amp
->am_next
;
391 if(amp
->am_refcnt
> 0){
392 amp
->am_flags
|= a_AMV_MF_DELETE
;
393 if(n_poption
& n_PO_D_V
)
394 n_err(_("Delayed deletion of currently active %s: %s\n"),
395 (amp
->am_flags
& a_AMV_MF_ACCOUNT
? "account" : "define"),
399 amp
= (struct a_amv_mac
*)-1;
406 ampp
= &a_amv_macs
[h
];
407 newamp
->am_next
= *ampp
;
417 a_amv_mac_call(void *v
, bool_t silent_nexist
){
418 struct a_amv_mac
*amp
;
421 struct n_cmd_arg_ctx
*cacp
;
426 if(cacp
->cac_no
== 0){
427 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
428 n_pstate_err_no
= n_ERR_INVAL
;
433 name
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
435 if(n_UNLIKELY(cacp
->cac_no
> a_AMV_POSPAR_MAX
)){
436 n_err(_("Too many arguments to macro `call': %s\n"), name
);
437 n_pstate_err_no
= n_ERR_OVERFLOW
;
439 }else if(n_UNLIKELY((amp
= a_amv_mac_lookup(name
, NULL
, a_AMV_MF_NONE
)
442 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name
, FAL0
));
443 n_pstate_err_no
= n_ERR_NOENT
;
447 struct a_amv_mac_call_args
*amcap
;
450 argc
= cacp
->cac_no
+ 1;
451 amcap
= n_lofi_alloc(sizeof *amcap
+ (argc
* sizeof *argv
));
452 argv
= (void*)&amcap
[1];
454 for(argc
= 0; (cacp
->cac_arg
= cacp
->cac_arg
->ca_next
) != NULL
; ++argc
)
455 argv
[argc
] = cacp
->cac_arg
->ca_arg
.ca_str
.s
;
458 memset(amcap
, 0, sizeof *amcap
);
459 amcap
->amca_name
= name
;
460 amcap
->amca_amp
= amp
;
461 if(a_amv_lopts
!= NULL
)
462 amcap
->amca_loflags
= (a_amv_lopts
->as_loflags
& a_AMV_LF_CALL_MASK
463 ) >> a_AMV_LF_CALL_TO_SCOPE_SHIFT
;
465 amcap
->amca_pospar
.app_count
= (ui16_t
)argc
;
466 amcap
->amca_pospar
.app_not_heap
= TRU1
;
467 amcap
->amca_pospar
.app_dat
= argv
;
470 (void)a_amv_mac_exec(amcap
);
479 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
480 struct a_amv_lostack
*losp
;
481 struct a_amv_mac_line
**amlp
;
482 char **args_base
, **args
;
483 struct a_amv_mac
*amp
;
487 amp
= amcap
->amca_amp
;
488 assert(amp
!= NULL
&& amp
!= a_AMV_MACKY_MACK
);
490 /* XXX Unfortunately we yet need to dup the macro lines! :( */
491 args_base
= args
= n_alloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
492 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
493 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
496 losp
= n_lofi_alloc(sizeof *losp
);
497 losp
->as_global_saved
= a_amv_lopts
;
498 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
499 losp
->as_up
= losp
->as_global_saved
;
500 losp
->as_lopts
= NULL
;
503 losp
->as_lopts
= *amcap
->amca_unroller
;
505 losp
->as_loflags
= amcap
->amca_loflags
;
508 if(amcap
->amca_hook_pre
!= NULL
)
509 n_PS_ROOT_BLOCK((*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
));
510 rv
= n_go_macro((n_GO_INPUT_NONE
|
511 (amcap
->amca_no_xcall
? n_GO_INPUT_NO_XCALL
: 0)),
512 amp
->am_name
, args_base
, &a_amv_mac__finalize
, losp
);
518 a_amv_mac__finalize(void *vp
){
519 struct a_amv_mac
*amp
;
520 struct a_amv_mac_call_args
*amcap
;
521 struct a_amv_lostack
*losp
;
525 a_amv_lopts
= losp
->as_global_saved
;
527 amcap
= losp
->as_amcap
;
529 if(!amcap
->amca_pospar
.app_not_heap
&& amcap
->amca_pospar
.app_maxcount
> 0){
532 for(i
= amcap
->amca_pospar
.app_maxcount
; i
-- != 0;)
533 n_free(n_UNCONST(amcap
->amca_pospar
.app_dat
[i
]));
534 n_free(amcap
->amca_pospar
.app_dat
);
537 if(amcap
->amca_unroller
== NULL
){
538 if(losp
->as_lopts
!= NULL
)
539 a_amv_lopts_unroll(&losp
->as_lopts
);
541 *amcap
->amca_unroller
= losp
->as_lopts
;
543 if(amcap
->amca_ps_hook_mask
)
544 n_pstate
&= ~n_PS_HOOK_MASK
;
546 if((amp
= amcap
->amca_amp
) != a_AMV_MACKY_MACK
&& amp
!= NULL
&&
547 --amp
->am_refcnt
== 0 && (amp
->am_flags
& a_AMV_MF_DELETE
))
556 a_amv_mac_show(enum a_amv_mac_flags amf
){
557 size_t lc
, mc
, ti
, i
;
565 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
567 n_perr(_("Can't create temporary file for `define' or `account' listing"),
572 amf
&= a_AMV_MF_TYPE_MASK
;
573 typestr
= (amf
& a_AMV_MF_ACCOUNT
) ? "account" : "define";
575 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
576 struct a_amv_mac
*amp
;
578 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
579 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
580 struct a_amv_mac_line
**amlpp
;
587 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
588 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
589 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
591 fputs((*amlpp
)->aml_dat
, fp
);
600 page_or_print(fp
, lc
);
602 rv
= (ferror(fp
) == 0);
610 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
612 ui32_t line_cnt
, maxlen
;
614 struct linelist
*ll_next
;
615 struct a_amv_mac_line
*ll_amlp
;
616 } *llp
, *ll_head
, *ll_tail
;
617 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
618 struct a_amv_mac
*amp
;
622 memset(&line
, 0, sizeof line
);
626 /* TODO We should have our input state machine which emits Line events,
627 * TODO and hook different consumers dependent on our content, as stated
628 * TODO in i think lex_input; */
629 /* Read in the lines which form the macro content */
630 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
634 n
.i
= n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, n_empty
,
635 &line
.s
, &line
.l
, NULL
, NULL
);
639 n_err(_("Unterminated %s definition: %s\n"),
640 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
644 /* Trim WS, remember amount of leading spaces for display purposes */
645 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
647 leaspc
= (leaspc
+ 8u) & ~7u;
652 for(; n
.ui
> 0 && spacechar(cp
[n
.ui
- 1]); --n
.ui
)
657 maxlen
= n_MAX(maxlen
, n
.ui
);
660 /* Is is the closing brace? */
664 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
665 struct a_amv_mac_line
*amlp
;
667 llp
= n_autorec_alloc(sizeof *llp
);
671 ll_tail
->ll_next
= llp
;
674 llp
->ll_amlp
= amlp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
676 amlp
->aml_len
= n
.ui
-1;
677 amlp
->aml_prespc
= leaspc
;
678 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
680 n_err(_("Too much content in %s definition: %s\n"),
681 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
686 /* Create the new macro */
687 n
.s
= strlen(name
) +1;
688 amp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
689 memset(amp
, 0, n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
));
690 amp
->am_maxlen
= maxlen
;
691 amp
->am_line_cnt
= line_cnt
;
693 memcpy(amp
->am_name
, name
, n
.s
);
695 struct a_amv_mac_line
**amlpp
;
697 amp
->am_line_dat
= amlpp
= n_alloc(sizeof(*amlpp
) * ++line_cnt
);
698 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
699 *amlpp
++ = llp
->ll_amlp
;
703 /* Create entry, replace a yet existing one as necessary */
704 a_amv_mac_lookup(name
, amp
, amf
| a_AMV_MF_UNDEF
);
713 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
714 n_free(llp
->ll_amlp
);
717 * n_free(amp->am_line_dat);
724 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
725 struct a_amv_mac
*amp
;
731 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
732 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
733 n_err(_("%s not defined: %s\n"),
734 (amf
& a_AMV_MF_ACCOUNT
? "Account" : "Macro"), name
);
738 struct a_amv_mac
**ampp
, *lamp
;
740 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
742 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
743 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
744 /* xxx Expensive but rare: be simple */
745 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
746 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
758 a_amv_mac_free(struct a_amv_mac
*amp
){
759 struct a_amv_mac_line
**amlpp
;
762 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
764 n_free(amp
->am_line_dat
);
770 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
771 struct a_amv_var
*oavp
){
772 struct a_amv_var
*avp
;
776 /* Propagate unrolling up the stack, as necessary */
779 if(alp
->as_loflags
& a_AMV_LF_SCOPE_MASK
)
781 if((alp
= alp
->as_up
) == NULL
)
785 /* Check whether this variable is handled yet XXX Boost: ID check etc.!! */
786 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
787 if(!strcmp(avp
->av_name
, name
))
790 nl
= strlen(name
) +1;
791 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
792 avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
793 avp
->av_link
= alp
->as_lopts
;
795 memcpy(avp
->av_name
, name
, nl
);
797 avp
->av_value
= NULL
;
803 avp
->av_value
= &avp
->av_name
[nl
];
804 avp
->av_flags
= oavp
->av_flags
;
805 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
807 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
815 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
816 struct a_amv_lostack
*save_alp
;
817 struct a_amv_var
*x
, *avp
;
823 save_alp
= a_amv_lopts
;
828 n_PS_ROOT_BLOCK(n_var_vset(x
->av_name
, (uintptr_t)x
->av_value
));
831 a_amv_lopts
= save_alp
;
836 a_amv_var_copy(char const *str
){
842 news
= n_UNCONST(n_empty
);
843 else if(str
[1] == '\0'){
845 news
= n_UNCONST(n_1
);
846 else if(str
[0] == '0')
847 news
= n_UNCONST(n_0
);
850 }else if(str
[2] == '\0' && str
[0] == '-' && str
[1] == '1')
851 news
= n_UNCONST(n_m1
);
854 len
= strlen(str
) +1;
856 memcpy(news
, str
, len
);
863 a_amv_var_free(char *cp
){
865 if(cp
[0] != '\0' && cp
!= n_0
&& cp
!= n_1
&& cp
!= n_m1
)
871 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
, enum okeys okey
,
878 if(avvm
== a_AMV_VIP_SET_PRE
){
883 /* Note this gets called from main.c during initialization, and they
884 * simply set this to pw_dir as a fallback: don't verify _that_ call.
886 if(!(n_pstate
& n_PS_ROOT
) && !n_is_dir(val
, TRUM1
)){
887 n_err(_("$HOME is not a directory or not accessible: %s\n"),
888 n_shexp_quote_cp(val
, FAL0
));
893 if(!n_is_dir(val
, TRU1
)){
894 n_err(_("$TMPDIR is not a directory or not accessible: %s\n"),
895 n_shexp_quote_cp(val
, FAL0
));
903 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
904 if(uib
& ~0777u){ /* (is valid _VF_POSNUM) */
905 n_err(_("Invalid *umask* setting: %s\n"), val
);
911 }else if(avvm
== a_AMV_VIP_SET_POST
){
919 n_poption
|= n_PO_DEBUG
;
922 /* Invalidate any resolved folder then, too
925 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
930 x_b
= x
= n_autorec_alloc(strlen(val
) +1);
931 while((c
= *val
++) != '\0')
935 n_PS_ROOT_BLOCK(ok_vset(ifs_ws
, x_b
));
937 #ifdef HAVE_SETLOCALE
945 n_poption
|= n_PO_MEMDEBUG
;
947 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
948 if(!(n_pstate
& n_PS_ROOT
))
949 n_PS_ROOT_BLOCK(ok_bset(posix
));
951 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
952 if(!(n_pstate
& n_PS_ROOT
))
953 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT
));
955 case ok_b_skipemptybody
:
956 n_poption
|= n_PO_E_FLAG
;
958 case ok_b_typescript_mode
:
959 ok_bset(colour_disable
);
960 ok_bset(line_editor_disable
);
961 if(!(n_psonce
& n_PSO_STARTED
))
962 ok_bset(termcap_disable
);
968 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
973 n_poption
|= (n_poption
& n_PO_VERB
) ? n_PO_VERBVERB
: n_PO_VERB
;
984 n_poption
&= ~n_PO_DEBUG
;
987 /* Invalidate any resolved folder then, too
990 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
993 n_poption
&= ~n_PO_MEMDEBUG
;
995 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
996 if(!(n_pstate
& n_PS_ROOT
))
997 n_PS_ROOT_BLOCK(ok_bclear(posix
));
999 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
1000 if(!(n_pstate
& n_PS_ROOT
))
1001 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT
));
1003 case ok_b_skipemptybody
:
1004 n_poption
&= ~n_PO_E_FLAG
;
1007 n_poption
&= ~(n_PO_VERB
| n_PO_VERBVERB
);
1016 a_amv_var_check_nocntrls(char const *val
){
1020 while((c
= *val
++) != '\0')
1028 a_amv_var_check_num(char const *val
, bool_t posnum
){
1029 /* TODO The internal/environment variables which are num= or posnum= should
1030 * TODO gain special lookup functions, or the return should be void* and
1031 * TODO castable to integer; i.e. no more strtoX() should be needed.
1032 * TODO I.e., the result of this function should instead be stored */
1038 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1040 enum n_idec_state ids
;
1042 ids
= n_idec_cp(&uib
, val
, 0,
1043 (posnum
? n_IDEC_MODE_SIGNED_TYPE
: n_IDEC_MODE_NONE
), NULL
);
1044 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1045 ) != n_IDEC_STATE_CONSUMED
)
1047 /* TODO Unless we store integers we need to look and forbid, because
1048 * TODO callee may not be able to swallow, e.g., "-1" */
1049 if(posnum
&& (ids
& n_IDEC_STATE_SEEN_MINUS
))
1057 a_amv_var_canonify(char const *vn
){
1059 if(!upperchar(*vn
)){
1062 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
1064 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
1071 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
1073 struct a_amv_var_map
const *avmp
;
1077 /* It may be a special a.k.a. macro-local or one-letter parameter */
1079 if(n_UNLIKELY(digitchar(c
))){
1080 /* (Inline dec. atoi, ugh) */
1081 for(j
= (ui8_t
)c
- '0', i
= 1;; ++i
){
1086 goto jno_special_param
;
1087 j
= j
* 10 + (ui8_t
)c
- '0';
1089 if(j
<= a_AMV_POSPAR_MAX
){
1090 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR
;
1091 goto jspecial_param
;
1093 }else if(n_UNLIKELY(name
[1] == '\0')){
1097 avcp
->avc_special_cat
= a_AMV_VSC_GLOBAL
;
1098 j
= (c
== '?') ? a_AMV_VST_QM
: a_AMV_VST_EM
;
1099 goto jspecial_param
;
1103 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1105 goto jspecial_param
;
1107 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1109 goto jspecial_param
;
1111 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1112 j
= a_AMV_VST_NOSIGN
;
1113 goto jspecial_param
;
1119 avcp
->avc_special_cat
= a_AMV_VSC_MULTIPLEX
;
1121 goto jspecial_param
;
1124 /* Normal reverse lookup, walk over the hashtable */
1126 avcp
->avc_special_cat
= a_AMV_VSC_NONE
;
1127 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
1128 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
1130 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
1133 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
1136 avmp
= &a_amv_var_map
[x
];
1137 if(avmp
->avm_hash
== hash
&&
1138 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
1139 avcp
->avc_map
= avmp
;
1140 avcp
->avc_okey
= (enum okeys
)x
;
1144 if(++i
== a_AMV_VAR_REV_PRIME
){
1145 #ifdef a_AMV_VAR_REV_WRAPAROUND
1152 avcp
->avc_map
= NULL
;
1156 return (avcp
!= NULL
);
1158 /* All these are mapped to *--special-param* */
1160 avcp
->avc_name
= name
;
1161 avcp
->avc_special_prop
= (ui16_t
)j
;
1162 avmp
= &a_amv_var_map
[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX
];
1163 avcp
->avc_hash
= avmp
->avm_hash
;
1164 avcp
->avc_map
= avmp
;
1165 avcp
->avc_okey
= ok_v___special_param
;
1170 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
1173 struct a_amv_var_map
const *avmp
;
1174 struct a_amv_var
*avp
;
1178 struct a_amv_var
**avpp
, *lavp
;
1180 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
1182 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
1183 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
1184 /* Relink as head, hope it "sorts on usage" over time.
1185 * _clear() relies on this behaviour */
1187 lavp
->av_link
= avp
->av_link
;
1188 avp
->av_link
= *avpp
;
1195 /* If this is not an assembled variable we need to consider some special
1196 * initialization cases and eventually create the variable anew */
1197 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1198 /* Does it have an import-from-environment flag? */
1199 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
1200 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
1201 /* May be better not to use that one, though? */
1202 /* TODO Outsource the tests into a _shared_ test function! */
1203 bool_t isempty
, isbltin
;
1205 isempty
= (*cp
== '\0' &&
1206 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
1207 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
1210 if(n_UNLIKELY(isempty
)){
1211 n_err(_("Environment variable must not be empty: %s\n"),
1215 }else if(n_LIKELY(*cp
!= '\0')){
1216 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
1217 !a_amv_var_check_nocntrls(cp
))){
1218 n_err(_("Ignoring environment, control characters "
1219 "invalid in variable: %s\n"), avcp
->avc_name
);
1222 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1223 !a_amv_var_check_num(cp
, FAL0
))){
1224 n_err(_("Environment variable value not a number "
1225 "or out of range: %s\n"), avcp
->avc_name
);
1228 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1229 !a_amv_var_check_num(cp
, TRU1
))){
1230 n_err(_("Environment variable value not a number, "
1231 "negative or out of range: %s\n"), avcp
->avc_name
);
1240 /* A first-time init switch is to be handled now and here */
1241 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1242 static struct a_amv_var_defval
const **arr
,
1243 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1247 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1249 arr
[i
] = &a_amv_var_i3vals
[i
];
1252 for(i
= 0; arr
[i
] != NULL
; ++i
)
1253 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1254 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1255 : arr
[i
]->avdv_value
;
1256 /* Remove this entry, hope entire block becomes no-op asap */
1258 arr
[i
] = arr
[i
+ 1];
1259 while(arr
[i
++] != NULL
);
1263 if(i3val_nonew
== TRUM1
)
1264 avp
= (struct a_amv_var
*)-1;
1269 /* The virtual variables */
1270 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1271 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1272 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1273 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1279 /* Place this last because once it is set first the variable will never
1280 * be removed again and thus match in the first block above */
1282 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1283 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1284 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1285 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1286 : a_amv_var_defvals
[i
].avdv_value
;
1295 avcp
->avc_var
= avp
;
1297 return (avp
!= NULL
);
1300 /* E.g., $TMPDIR may be set to non-existent, so we need to be able to catch
1301 * that and redirect to a possible default value */
1302 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1303 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, cp
)){
1305 if(avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
))
1306 unsetenv(avcp
->avc_name
);
1308 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0)
1312 struct a_amv_var
**avpp
;
1315 l
= strlen(avcp
->avc_name
) +1;
1317 avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1318 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1320 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1324 avp
->av_flags
= avmp
->avm_flags
;
1325 avp
->av_value
= a_amv_var_copy(cp
);
1327 if(avp
->av_flags
& a_AMV_VF_ENV
)
1328 a_amv_var__putenv(avcp
, avp
);
1329 if(avmp
->avm_flags
& a_AMV_VF_VIP
)
1330 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, cp
);
1336 a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
){
1340 struct a_amv_var_map
const *avmp
;
1343 /* Not function local, TODO but lazy evaluted for now */
1344 if(avcp
->avc_special_prop
== a_AMV_VST_QM
){
1345 avmp
= &a_amv_var_map
[a_AMV_VAR__QM_MAP_IDX
];
1346 avcp
->avc_okey
= ok_v___qm
;
1347 ep
= &n_pstate_ex_no
;
1349 avmp
= &a_amv_var_map
[a_AMV_VAR__EM_MAP_IDX
];
1350 avcp
->avc_okey
= ok_v___em
;
1351 ep
= &n_pstate_err_no
;
1354 /* XXX Oh heaven, we are responsible to ensure that $?/! is up-to-date
1355 * TODO we could num=1 ok_v___[qe]m, but the thing is still a string
1356 * TODO and thus conversion takes places over and over again; also
1357 * TODO for now that would have to occur before we set _that_ value
1358 * TODO so let's special treat it until we store ints as such */
1360 case 0: rv
= n_0
; break;
1361 case 1: rv
= n_1
; break;
1363 snprintf(itoabuf
, sizeof itoabuf
, "%d", *ep
);
1367 n_PS_ROOT_BLOCK(n_var_okset(avcp
->avc_okey
, (uintptr_t)rv
));
1369 avcp
->avc_hash
= avmp
->avm_hash
;
1370 avcp
->avc_map
= avmp
;
1371 rv
= a_amv_var_lookup(avcp
, FAL0
) ? avcp
->avc_var
->av_value
: NULL
;
1377 a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
){
1384 i
= strlen(rv
= &avcp
->avc_name
[1]);
1386 /* ERR, ERRDOC, ERRNAME, plus *-NAME variants */
1387 if(rv
[0] == 'E' && i
>= 3 && rv
[1] == 'R' && rv
[2] == 'R'){
1389 e
= n_pstate_err_no
;
1391 }else if(rv
[3] == '-'){
1392 e
= n_err_from_name(&rv
[4]);
1395 case 0: rv
= n_0
; break;
1396 case 1: rv
= n_1
; break;
1398 snprintf(itoabuf
, sizeof itoabuf
, "%d", e
);
1399 rv
= savestr(itoabuf
); /* XXX yet, cannot do numbers */
1404 if(!memcmp(&rv
[3], "DOC", 3)){
1407 case '\0': e
= n_pstate_err_no
; break;
1408 case '-': e
= n_err_from_name(&rv
[1]); break;
1411 rv
= n_err_to_doc(e
);
1413 }else if(i
>= 7 && !memcmp(&rv
[3], "NAME", 4)){
1416 case '\0': e
= n_pstate_err_no
; break;
1417 case '-': e
= n_err_from_name(&rv
[1]); break;
1420 rv
= n_err_to_name(e
);
1434 a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
){
1437 char const *rv
, **argv
;
1442 /* If in a macro/xy.. */
1443 if(a_amv_lopts
!= NULL
){
1445 struct a_amv_mac_call_args
*amcap
;
1447 amcap
= a_amv_lopts
->as_amcap
;
1448 argc
= amcap
->amca_pospar
.app_count
;
1449 argv
= amcap
->amca_pospar
.app_dat
;
1450 argv
+= amcap
->amca_pospar
.app_idx
;
1452 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
1453 if(!(ismacky
= (amcap
->amca_amp
== a_AMV_MACKY_MACK
)) &&
1454 (amcap
->amca_ps_hook_mask
||
1455 (assert(amcap
->amca_amp
!= NULL
),
1456 (amcap
->amca_amp
->am_flags
& a_AMV_MF_TYPE_MASK
1457 ) == a_AMV_MF_ACCOUNT
)))
1460 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1461 if(avcp
->avc_special_prop
> 0){
1462 if(argc
>= avcp
->avc_special_prop
)
1463 rv
= argv
[avcp
->avc_special_prop
- 1];
1465 rv
= amcap
->amca_name
;
1467 rv
= (a_amv_lopts
->as_up
!= NULL
1468 ? a_amv_lopts
->as_up
->as_amcap
->amca_name
: n_empty
);
1471 /* MACKY_MACK doesn't know about [*@#] */
1472 /*else*/ if(ismacky
){
1473 if(n_poption
& n_PO_D_V
)
1474 n_err(_("Cannot use $*/$@/$# in this context: %s\n"),
1475 n_shexp_quote_cp(avcp
->avc_name
, FAL0
));
1479 argc
= a_amv_pospar
.app_count
;
1480 argv
= a_amv_pospar
.app_dat
;
1481 argv
+= a_amv_pospar
.app_idx
;
1483 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1484 if(avcp
->avc_special_prop
> 0){
1485 if(argc
>= avcp
->avc_special_prop
)
1486 rv
= argv
[avcp
->avc_special_prop
- 1];
1493 switch(avcp
->avc_special_prop
){ /* XXX OPTIMIZE */
1494 case a_AMV_VST_STAR
:
1496 for(i
= j
= 0; i
< argc
; ++i
)
1497 j
+= strlen(argv
[i
]) + 1;
1503 rv
= cp
= n_autorec_alloc(j
);
1504 for(i
= j
= 0; i
< argc
; ++i
){
1505 j
= strlen(argv
[i
]);
1506 memcpy(cp
, argv
[i
], j
);
1513 case a_AMV_VST_NOSIGN
:{
1516 rv
= cp
= n_autorec_alloc(sizeof("65535"));
1517 snprintf(cp
, sizeof("65535"), "%hu", argc
);
1529 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1531 struct a_amv_var
*avp
;
1533 struct a_amv_var_map
const *avmp
;
1538 rv
= a_amv_var_clear(avcp
, force_env
);
1542 if((avmp
= avcp
->avc_map
) != NULL
){
1545 /* Validity checks */
1546 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1547 !(n_pstate
& n_PS_ROOT
))){
1548 value
= N_("Variable is read-only: %s\n");
1551 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1552 value
= N_("Variable must not be empty: %s\n");
1555 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1556 !a_amv_var_check_nocntrls(value
))){
1557 value
= N_("Variable forbids control characters: %s\n");
1560 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1561 !a_amv_var_check_num(value
, FAL0
))){
1562 value
= N_("Variable value not a number or out of range: %s\n");
1565 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1566 !a_amv_var_check_num(value
, TRU1
))){
1567 value
= _("Variable value not a number, negative, "
1568 "or out of range: %s\n");
1571 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1572 !(n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_ROOT
))){
1573 value
= N_("Variable cannot be set in a resource file: %s\n");
1577 /* Any more complicated inter-dependency? */
1578 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1579 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, value
))){
1580 value
= N_("Assignment of variable aborted: %s\n");
1582 n_err(V_(value
), avcp
->avc_name
);
1586 /* Transformations */
1587 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1590 oval
= savestr(value
);
1592 for(; (c
= *oval
) != '\0'; ++oval
)
1593 *oval
= lowerconv(c
);
1598 a_amv_var_lookup(avcp
, TRU1
);
1600 /* Don't care what happens later on, store this in the unroll list */
1601 if(a_amv_lopts
!= NULL
&&
1602 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1603 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1605 if((avp
= avcp
->avc_var
) == NULL
){
1606 struct a_amv_var
**avpp
;
1609 l
= strlen(avcp
->avc_name
) +1;
1610 avcp
->avc_var
= avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1612 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1617 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1618 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1619 oval
= n_UNCONST(n_empty
);
1621 oval
= avp
->av_value
;
1624 avp
->av_value
= a_amv_var_copy(value
);
1626 /* Via `set' etc. the user may give even boolean options non-boolean
1627 * values, ignore that and force boolean */
1628 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1629 if(!(n_pstate
& n_PS_ROOT
) && (n_poption
& n_PO_D_VV
) &&
1631 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1632 avcp
->avc_name
, value
);
1633 avp
->av_value
= n_UNCONST(n_1
);
1635 avp
->av_value
= a_amv_var_copy(value
);
1638 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1639 avp
->av_flags
|= a_AMV_VF_E_LINKED
;
1640 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_E_LINKED
))
1641 rv
= a_amv_var__putenv(avcp
, avp
);
1642 if(avp
->av_flags
& a_AMV_VF_VIP
)
1643 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, value
);
1644 a_amv_var_free(oval
);
1651 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1659 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1661 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1663 if((rv
= (putenv(cp
) == 0))){
1666 if((ocp
= avp
->av_env
) != NULL
)
1677 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1678 struct a_amv_var
**avpp
, *avp
;
1679 struct a_amv_var_map
const *avmp
;
1685 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1686 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1687 !(n_pstate
& n_PS_ROOT
))){
1688 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1691 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1692 !a_amv_var_check_vips(a_AMV_VIP_CLEAR
, avcp
->avc_okey
, NULL
))){
1693 n_err(_("Clearance of variable aborted: %s\n"), avcp
->avc_name
);
1700 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1703 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1704 }else if(!(n_pstate
& (n_PS_ROOT
| n_PS_ROBOT
)) && (n_poption
& n_PO_D_V
))
1705 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1707 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1708 avcp
->avc_var
= NULL
;
1714 if(a_amv_lopts
!= NULL
&&
1715 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1716 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1718 avp
= avcp
->avc_var
;
1719 avcp
->avc_var
= NULL
;
1720 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1721 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1722 *avpp
= (*avpp
)->av_link
;
1730 envval
= avp
->av_env
;
1732 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_E_LINKED
)) || envval
!= NULL
)
1733 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1735 a_amv_var_free(avp
->av_value
);
1738 /* XXX Fun part, extremely simple-minded for now: if this variable has
1739 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1740 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1741 a_amv_var_lookup(avcp
, TRU1
);
1748 a_amv_var__clearenv(char const *name
, char *value
){
1750 extern char **environ
;
1762 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1767 while(*ecpp
++ != NULL
);
1777 a_amv_var_show_all(void){
1778 struct n_string msg
, *msgp
;
1781 struct a_amv_var
*avp
;
1782 char const **vacp
, **cap
;
1785 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1786 n_perr(_("Can't create temporary file for `set' listing"), 0);
1790 /* We need to instantiate first-time-inits and default values here, so that
1791 * they will be regular members of our _vars[] table */
1792 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1793 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1794 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1795 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1797 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1798 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1800 no
+= a_AMV_VAR_VIRTS_CNT
;
1802 vacp
= n_autorec_alloc(no
* sizeof(*vacp
));
1804 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1805 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1806 *cap
++ = avp
->av_name
;
1807 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1808 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1811 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1814 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1815 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1816 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1819 page_or_print(fp
, i
);
1826 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1830 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1836 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1837 struct a_amv_var_carrier avc
;
1843 msgp
= n_string_trunc(msgp
, 0);
1846 a_amv_var_revlookup(&avc
, name
);
1847 isset
= a_amv_var_lookup(&avc
, FAL0
);
1849 if(n_poption
& n_PO_D_V
){
1850 if(avc
.avc_map
== NULL
){
1851 msgp
= n_string_push_cp(msgp
, "#assembled variable with value");
1858 {a_AMV_VF_VIRT
, "virtual"},
1859 {a_AMV_VF_CHAIN
, "variable chain"},
1860 {a_AMV_VF_RDONLY
, "read-only"},
1861 {a_AMV_VF_NODEL
, "nodelete"},
1862 {a_AMV_VF_I3VAL
, "initial-value"},
1863 {a_AMV_VF_DEFVAL
, "default-value"},
1864 {a_AMV_VF_IMPORT
, "import-environ-first\0"}, /* assert NUL in max */
1865 {a_AMV_VF_ENV
, "sync-environ"},
1866 {a_AMV_VF_NOLOPTS
, "no-localopts"},
1867 {a_AMV_VF_NOTEMPTY
, "notempty"},
1868 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1869 {a_AMV_VF_NUM
, "number"},
1870 {a_AMV_VF_POSNUM
, "positive-number"},
1873 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1874 if(isset
? (avc
.avc_var
->av_flags
& tp
->flag
)
1875 : (avc
.avc_map
->avm_flags
& tp
->flag
)){
1876 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1877 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1881 msgp
= n_string_push_cp(msgp
, "\n ");
1884 if(!isset
|| (avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)){
1885 msgp
= n_string_push_c(msgp
, *n_ns
);
1887 if(avc
.avc_map
!= NULL
&& (avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
))
1888 msgp
= n_string_push_cp(msgp
, "boolean; ");
1889 msgp
= n_string_push_cp(msgp
, "variable not set: ");
1890 msgp
= n_string_push_cp(msgp
, n_shexp_quote_cp(name
, FAL0
));
1895 n_UNINIT(quote
, NULL
);
1896 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1897 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1898 if(strcmp(quote
, avc
.avc_var
->av_value
))
1899 msgp
= n_string_push_cp(msgp
, "wysh ");
1900 }else if(n_poption
& n_PO_D_V
)
1901 msgp
= n_string_push_cp(msgp
, "wysh ");
1902 if(avc
.avc_var
->av_flags
& a_AMV_VF_E_LINKED
)
1903 msgp
= n_string_push_cp(msgp
, "environ ");
1904 msgp
= n_string_push_cp(msgp
, "set ");
1905 msgp
= n_string_push_cp(msgp
, name
);
1906 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1907 msgp
= n_string_push_c(msgp
, '=');
1908 msgp
= n_string_push_cp(msgp
, quote
);
1909 }else if(n_poption
& n_PO_D_V
)
1910 msgp
= n_string_push_cp(msgp
, " #boolean");
1913 msgp
= n_string_push_c(msgp
, '\n');
1914 fputs(n_string_cp(msgp
), fp
);
1916 return (i
> 0 ? 2 : 1);
1920 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1921 char *cp
, *cp2
, *varbuf
, c
;
1927 while((cp
= *ap
++) != NULL
){
1929 cp2
= varbuf
= n_autorec_alloc(strlen(cp
) +1);
1931 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1932 if(cntrlchar(c
) || spacechar(c
)){
1933 n_err(_("Variable name with control character ignored: %s\n"),
1942 cp
= n_UNCONST(n_empty
);
1947 n_err(_("Empty variable name ignored\n"));
1950 struct a_amv_var_carrier avc
;
1953 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1954 varbuf
= &varbuf
[2];
1956 a_amv_var_revlookup(&avc
, varbuf
);
1959 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1961 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1976 if((args
= v
)[0] == NULL
){
1977 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1981 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1983 n_err(_("Synopsis: define: <name> {\n"));
1987 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1994 c_undefine(void *v
){
2002 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
2003 while(*++args
!= NULL
);
2013 rv
= a_amv_mac_call(vp
, FAL0
);
2019 c_call_if(void *vp
){
2023 rv
= a_amv_mac_call(vp
, TRU1
);
2030 struct a_amv_mac_call_args
*amcap
;
2031 struct a_amv_mac
*amp
;
2033 int rv
, i
, oqf
, nqf
;
2038 if((args
= v
)[0] == NULL
){
2039 rv
= (a_amv_mac_show(a_AMV_MF_ACCOUNT
) == FAL0
);
2043 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
2044 if(args
[2] != NULL
){
2045 n_err(_("Synopsis: account: <name> {\n"));
2048 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
2049 n_err(_("`account': cannot use reserved name: %s\n"),
2053 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACCOUNT
) == FAL0
);
2057 if(n_pstate
& n_PS_HOOK_MASK
){
2058 n_err(_("`account': can't change account from within a hook\n"));
2062 save_mbox_for_possible_quitstuff();
2065 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
2066 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACCOUNT
)) == NULL
){
2067 n_err(_("`account': account does not exist: %s\n"), args
[0]);
2071 oqf
= savequitflags();
2073 /* Shutdown the active account */
2074 if(a_amv_acc_curr
!= NULL
){
2078 /* Is there a cleanup hook? */
2079 var
= savecat("on-account-cleanup-", a_amv_acc_curr
->am_name
);
2080 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
||
2081 (cp
= ok_vlook(on_account_cleanup
)) != NULL
){
2082 struct a_amv_mac
*amphook
;
2084 if((amphook
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) != NULL
){
2085 amcap
= n_lofi_alloc(sizeof *amcap
);
2086 memset(amcap
, 0, sizeof *amcap
);
2087 amcap
->amca_name
= cp
;
2088 amcap
->amca_amp
= amphook
;
2089 amcap
->amca_unroller
= &a_amv_acc_curr
->am_lopts
;
2090 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2091 amcap
->amca_no_xcall
= TRU1
;
2092 n_pstate
|= n_PS_HOOK
;
2093 rv
= a_amv_mac_exec(amcap
);
2094 n_pstate
&= ~n_PS_HOOK_MASK
;
2096 n_err(_("*on-account-leave* hook %s does not exist\n"),
2097 n_shexp_quote_cp(cp
, FAL0
));
2101 if(a_amv_acc_curr
->am_lopts
!= NULL
)
2102 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
2104 /* For accounts this lingers */
2105 --a_amv_acc_curr
->am_refcnt
;
2106 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DELETE
)
2107 a_amv_mac_free(a_amv_acc_curr
);
2110 a_amv_acc_curr
= amp
;
2112 /* And switch to any non-"null" account */
2114 assert(amp
->am_lopts
== NULL
);
2115 amcap
= n_lofi_alloc(sizeof *amcap
);
2116 memset(amcap
, 0, sizeof *amcap
);
2117 amcap
->amca_name
= amp
->am_name
;
2118 amcap
->amca_amp
= amp
;
2119 amcap
->amca_unroller
= &
->am_lopts
;
2120 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2121 amcap
->amca_no_xcall
= TRU1
;
2122 ++amp
->am_refcnt
; /* We may not run 0 to avoid being deleted! */
2123 if(!a_amv_mac_exec(amcap
)){
2124 /* XXX account switch incomplete, unroll? */
2125 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
2130 n_PS_ROOT_BLOCK((amp
!= NULL
? ok_vset(account
, amp
->am_name
)
2131 : ok_vclear(account
)));
2133 if(n_psonce
& n_PSO_STARTED
){
2134 assert(!(n_pstate
& n_PS_HOOK_MASK
));
2135 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
2136 restorequitflags(oqf
);
2137 if((i
= setfile("%", 0)) < 0)
2139 temporary_folder_hook_check(FAL0
);
2140 if(i
> 0 && !ok_blook(emptystart
))
2142 n_folder_announce(n_ANNOUNCE_CHANGE
);
2143 restorequitflags(nqf
);
2152 c_unaccount(void *v
){
2160 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACCOUNT
);
2161 while(*++args
!= NULL
);
2167 c_localopts(void *vp
){
2168 enum a_amv_loflags alf
, alm
;
2175 if(a_amv_lopts
== NULL
){
2176 n_err(_("Cannot use `localopts' in this context "
2177 "(not in `define' or `account', nor special hook)\n"));
2181 if((argv
= vp
)[1] == NULL
|| is_asccaseprefix((++argv
)[-1], "scope"))
2182 alf
= alm
= a_AMV_LF_SCOPE
;
2183 else if(is_asccaseprefix(argv
[-1], "call"))
2184 alf
= a_AMV_LF_CALL
, alm
= a_AMV_LF_CALL_MASK
;
2185 else if(is_asccaseprefix(argv
[-1], "call-fixate"))
2186 alf
= a_AMV_LF_CALL_FIXATE
, alm
= a_AMV_LF_CALL_MASK
;
2188 n_err(_("Synopsis: localopts: [<scope|call|call-fixate>] <boolean>\n"));
2192 if(alf
== a_AMV_LF_SCOPE
&&
2193 (a_amv_lopts
->as_loflags
& a_AMV_LF_SCOPE_FIXATE
)){
2194 if(n_poption
& n_PO_D_V
)
2195 n_err(_("Cannot turn off `localopts', setting is fixated\n"));
2199 a_amv_lopts
->as_loflags
&= ~alm
;
2200 if(boolify(*argv
, UIZ_MAX
, FAL0
) > 0)
2201 a_amv_lopts
->as_loflags
|= alf
;
2210 struct a_amv_pospar
*appp
;
2217 if((vp
= *(char**)vp
) == NULL
)
2222 if((n_idec_si16_cp(&sib
, vp
, 10, NULL
2223 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2224 ) != n_IDEC_STATE_CONSUMED
|| sib
< 0){
2225 n_err(_("`shift': invalid argument: %s\n"), vp
);
2231 /* If in in a macro/xy */
2232 if(a_amv_lopts
!= NULL
){
2233 struct a_amv_mac
const *amp
;
2234 struct a_amv_mac_call_args
*amcap
;
2236 /* Explicitly do allow `vpospar' created things! */
2237 amp
= (amcap
= a_amv_lopts
->as_amcap
)->amca_amp
;
2238 if((amp
== NULL
|| amcap
->amca_ps_hook_mask
||
2239 (amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == a_AMV_MF_ACCOUNT
) &&
2240 amcap
->amca_pospar
.app_not_heap
){
2241 n_err(_("Cannot use `shift' in `account's or hook macros etc.\n"));
2244 appp
= &amcap
->amca_pospar
;
2246 appp
= &a_amv_pospar
;
2248 if(i
> appp
->app_count
){
2249 n_err(_("`shift': cannot shift %hu of %hu parameters\n"),
2250 i
, appp
->app_count
);
2254 appp
->app_count
-= i
;
2263 c_return(void *vp
){ /* TODO the exit status should be m_si64! */
2267 if(a_amv_lopts
!= NULL
){
2270 n_go_input_force_eof();
2271 n_pstate_err_no
= n_ERR_NONE
;
2274 if((argv
= vp
)[0] != NULL
){
2277 if((n_idec_si32_cp(&i
, argv
[0], 10, NULL
2278 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2279 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2282 n_err(_("`return': return value argument is invalid: %s\n"),
2284 n_pstate_err_no
= n_ERR_INVAL
;
2288 if(argv
[1] != NULL
){
2289 if((n_idec_si32_cp(&i
, argv
[1], 10, NULL
2290 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2291 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2292 n_pstate_err_no
= i
;
2294 n_err(_("`return': error number argument is invalid: %s\n"),
2296 n_pstate_err_no
= n_ERR_INVAL
;
2302 n_err(_("Can only use `return' in a macro\n"));
2303 n_pstate_err_no
= n_ERR_OPNOTSUPP
;
2311 temporary_folder_hook_check(bool_t nmail
){ /* TODO temporary, v15: drop */
2312 struct a_amv_mac_call_args
*amcap
;
2313 struct a_amv_mac
*amp
;
2321 var
= n_autorec_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
2323 /* First try the fully resolved path */
2324 snprintf(var
, len
, "folder-hook-%s", mailname
);
2325 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2328 /* If we are under *folder*, try the usual +NAME syntax, too */
2329 if(displayname
[0] == '+'){
2332 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
2334 snprintf(var
, len
, "folder-hook-+%s", x
);
2335 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2341 /* Plain *folder-hook* is our last try */
2342 if((cp
= ok_vlook(folder_hook
)) == NULL
)
2346 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
2347 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
2348 n_shexp_quote_cp(displayname
, FAL0
), cp
);
2353 amcap
= n_lofi_alloc(sizeof *amcap
);
2354 memset(amcap
, 0, sizeof *amcap
);
2355 amcap
->amca_name
= cp
;
2356 amcap
->amca_amp
= amp
;
2357 n_pstate
&= ~n_PS_HOOK_MASK
;
2359 amcap
->amca_unroller
= NULL
;
2360 n_pstate
|= n_PS_HOOK_NEWMAIL
;
2362 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
2363 n_pstate
|= n_PS_HOOK
;
2365 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2366 amcap
->amca_ps_hook_mask
= TRU1
;
2367 amcap
->amca_no_xcall
= TRU1
;
2368 rv
= a_amv_mac_exec(amcap
);
2369 n_pstate
&= ~n_PS_HOOK_MASK
;
2377 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2379 if(a_amv_folder_hook_lopts
!= NULL
){
2380 void *save
= a_amv_lopts
;
2383 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
2384 assert(a_amv_folder_hook_lopts
== NULL
);
2391 temporary_compose_mode_hook_call(char const *macname
,
2392 void (*hook_pre
)(void *), void *hook_arg
){
2393 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_GO_SPLICE
2394 * TODO comment in go.c for the right way of doing things! */
2395 static struct a_amv_lostack
*cmh_losp
;
2396 struct a_amv_mac_call_args
*amcap
;
2397 struct a_amv_mac
*amp
;
2402 if(macname
== (char*)-1){
2403 a_amv_mac__finalize(cmh_losp
);
2405 }else if(macname
!= NULL
&&
2406 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
2407 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
2410 amcap
= n_lofi_alloc(sizeof *amcap
);
2411 memset(amcap
, 0, sizeof *amcap
);
2412 amcap
->amca_name
= (macname
!= NULL
) ? macname
2413 : "*on-compose-splice(-shell)?*";
2414 amcap
->amca_amp
= amp
;
2415 amcap
->amca_unroller
= &a_amv_compose_lopts
;
2416 amcap
->amca_hook_pre
= hook_pre
;
2417 amcap
->amca_hook_arg
= hook_arg
;
2418 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2419 amcap
->amca_ps_hook_mask
= TRU1
;
2420 amcap
->amca_no_xcall
= TRU1
;
2421 n_pstate
&= ~n_PS_HOOK_MASK
;
2422 n_pstate
|= n_PS_HOOK
;
2424 a_amv_mac_exec(amcap
);
2426 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
2427 memset(cmh_losp
, 0, sizeof *cmh_losp
);
2428 cmh_losp
->as_global_saved
= a_amv_lopts
;
2429 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
2430 cmh_losp
->as_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2431 a_amv_lopts
= cmh_losp
;
2438 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2440 if(a_amv_compose_lopts
!= NULL
){
2441 void *save
= a_amv_lopts
;
2444 a_amv_lopts_unroll(&a_amv_compose_lopts
);
2445 assert(a_amv_compose_lopts
== NULL
);
2452 n_var_is_user_writable(char const *name
){
2453 struct a_amv_var_carrier avc
;
2454 struct a_amv_var_map
const *avmp
;
2458 a_amv_var_revlookup(&avc
, name
);
2459 if((avmp
= avc
.avc_map
) == NULL
)
2462 rv
= ((avmp
->avm_flags
& (a_AMV_VF_BOOL
| a_AMV_VF_RDONLY
)) == 0);
2468 n_var_oklook(enum okeys okey
){
2469 struct a_amv_var_carrier avc
;
2471 struct a_amv_var_map
const *avmp
;
2474 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2475 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2476 avc
.avc_hash
= avmp
->avm_hash
;
2477 avc
.avc_okey
= okey
;
2479 if(a_amv_var_lookup(&avc
, FAL0
))
2480 rv
= avc
.avc_var
->av_value
;
2488 n_var_okset(enum okeys okey
, uintptr_t val
){
2489 struct a_amv_var_carrier avc
;
2491 struct a_amv_var_map
const *avmp
;
2494 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2495 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2496 avc
.avc_hash
= avmp
->avm_hash
;
2497 avc
.avc_okey
= okey
;
2499 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2505 n_var_okclear(enum okeys okey
){
2506 struct a_amv_var_carrier avc
;
2508 struct a_amv_var_map
const *avmp
;
2511 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2512 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2513 avc
.avc_hash
= avmp
->avm_hash
;
2514 avc
.avc_okey
= okey
;
2516 rv
= a_amv_var_clear(&avc
, FAL0
);
2522 n_var_vlook(char const *vokey
, bool_t try_getenv
){
2523 struct a_amv_var_carrier avc
;
2527 a_amv_var_revlookup(&avc
, vokey
);
2529 switch((enum a_amv_var_special_category
)avc
.avc_special_cat
){
2530 default: /* silence CC */
2531 case a_AMV_VSC_NONE
:
2532 if(a_amv_var_lookup(&avc
, FAL0
))
2533 rv
= avc
.avc_var
->av_value
;
2534 /* Only check the environment for something that is otherwise unknown */
2535 else if(try_getenv
&& avc
.avc_map
== NULL
)
2540 case a_AMV_VSC_GLOBAL
:
2541 rv
= a_amv_var_vsc_global(&avc
);
2543 case a_AMV_VSC_MULTIPLEX
:
2544 rv
= a_amv_var_vsc_multiplex(&avc
);
2546 case a_AMV_VSC_POSPAR
:
2547 case a_AMV_VSC_POSPAR_ENV
:
2548 rv
= a_amv_var_vsc_pospar(&avc
);
2556 n_var_vexplode(void const **cookie
){
2557 struct a_amv_pospar
*appp
;
2560 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
2562 *cookie
= (appp
->app_count
> 0) ? &appp
->app_dat
[appp
->app_idx
] : NULL
;
2564 return (*cookie
!= NULL
);
2568 n_var_vset(char const *vokey
, uintptr_t val
){
2569 struct a_amv_var_carrier avc
;
2573 a_amv_var_revlookup(&avc
, vokey
);
2575 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2581 n_var_vclear(char const *vokey
){
2582 struct a_amv_var_carrier avc
;
2586 a_amv_var_revlookup(&avc
, vokey
);
2588 ok
= a_amv_var_clear(&avc
, FAL0
);
2595 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
2596 enum okey_xlook_mode oxm
){
2597 struct a_amv_var_carrier avc
;
2598 struct str
const *us
;
2601 struct a_amv_var_map
const *avmp
;
2604 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
2606 /* For simplicity: allow this case too */
2607 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
2612 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2613 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2614 avc
.avc_okey
= okey
;
2616 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
2617 nlen
= strlen(avc
.avc_name
);
2618 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
2619 memcpy(nbuf
, avc
.avc_name
, nlen
);
2622 /* One of .url_u_h_p and .url_h_p we test in here */
2623 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2624 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2625 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2626 if(a_amv_var_lookup(&avc
, FAL0
))
2631 us
= &urlp
->url_h_p
;
2632 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2633 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2634 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2635 if(a_amv_var_lookup(&avc
, FAL0
)){
2637 rv
= avc
.avc_var
->av_value
;
2643 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2650 #endif /* HAVE_SOCKETS */
2658 if(*(ap
= v
) == NULL
){
2659 a_amv_var_show_all();
2662 err
= !a_amv_var_c_set(ap
, FAL0
);
2673 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2674 err
|= !n_var_vclear(*ap
);
2684 if(*(ap
= v
) == NULL
)
2687 struct n_string msg
, *msgp
= &msg
;
2689 msgp
= n_string_creat(msgp
);
2690 for(; *ap
!= NULL
; ++ap
)
2691 a_amv_var_show(*ap
, n_stdout
, msgp
);
2695 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2700 struct a_amv_var_carrier avc
;
2704 sighandler_type sigint
;
2707 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2709 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2710 memset(&avc
, 0, sizeof avc
);
2712 a_amv_var_revlookup(&avc
, *argv
);
2714 if(avc
.avc_map
!= NULL
){
2715 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2716 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2720 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2721 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2727 a_amv_var_lookup(&avc
, FAL0
);
2729 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2731 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2734 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2735 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2736 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2743 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2752 if(UICMP(64, l
, >=, UIZ_MAX
-42)){
2753 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2758 varres
= val
= n_autorec_alloc(l
+1);
2759 for(; l
> 0 && (c
= getc(nf
)) != EOF
; --l
)
2763 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2769 if(!a_amv_var_set(&avc
, varres
, FAL0
))
2774 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2780 safe_signal(SIGINT
, sigint
);
2787 struct a_amv_var_carrier avc
;
2793 if((islnk
= is_asccaseprefix(*(ap
= v
), "link")) ||
2794 is_asccaseprefix(*ap
, "unlink")){
2795 for(err
= 0; *++ap
!= NULL
;){
2796 a_amv_var_revlookup(&avc
, *ap
);
2798 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2799 (avc
.avc_var
->av_flags
& a_AMV_VF_E_LINKED
))){
2801 avc
.avc_var
->av_flags
&= ~a_AMV_VF_E_LINKED
;
2803 }else if(avc
.avc_var
->av_flags
&
2804 (a_AMV_VF_ENV
| a_AMV_VF_E_LINKED
)){
2805 if(n_poption
& n_PO_D_V
)
2806 n_err(_("`environ': link: already established: %s\n"), *ap
);
2809 avc
.avc_var
->av_flags
|= a_AMV_VF_E_LINKED
;
2810 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2811 a_amv_var__putenv(&avc
, avc
.avc_var
);
2813 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2816 char const *evp
= getenv(*ap
);
2819 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2821 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2827 }else if(is_asccaseprefix(*ap
, "set"))
2828 err
= !a_amv_var_c_set(++ap
, TRU1
);
2829 else if(is_asccaseprefix(*ap
, "unset")){
2830 for(err
= 0; *++ap
!= NULL
;){
2831 a_amv_var_revlookup(&avc
, *ap
);
2833 if(!a_amv_var_clear(&avc
, TRU1
))
2837 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2845 c_vexpr(void *v
){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2847 enum n_idec_state ids
;
2849 char op
, varbuf
[64 + 64 / 8 +1];
2850 char const **argv
, *varname
, *varres
, *cp
;
2853 a_SOFTOVERFLOW
= 1u<<1,
2855 a_ISDECIMAL
= 1u<<3, /* Print only decimal result */
2856 a_SATURATED
= 1u<<4,
2858 a_UNSIGNED
= 1u<<6, /* Unsigned right shift (share bit ok) */
2865 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2866 n_UNINIT(varres
, n_empty
);
2868 if((cp
= argv
[0])[0] == '\0')
2878 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2881 if(*(cp
= *++argv
) == '\0')
2883 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2884 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2885 ) != n_IDEC_STATE_CONSUMED
){
2886 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2888 f
|= a_SOFTOVERFLOW
;
2905 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
2910 if(*(cp
= *++argv
) == '\0')
2912 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2913 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2914 ) != n_IDEC_STATE_CONSUMED
){
2915 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2917 f
|= a_SOFTOVERFLOW
;
2921 if(*(cp
= *++argv
) == '\0')
2923 else if(((ids
= n_idec_si64_cp(&rhv
, cp
, 0, NULL
)
2924 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2925 ) != n_IDEC_STATE_CONSUMED
){
2926 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2928 f
|= a_SOFTOVERFLOW
;
2938 if(rhv
!= SI64_MIN
){
2943 goto jenum_plusminus
;
2949 if(SI64_MAX
- rhv
< lhv
)
2950 goto jenum_plusminus
;
2955 if(rhv
!= SI64_MIN
){
2960 goto jenum_plusminus
;
2966 if(SI64_MIN
+ rhv
> lhv
){
2968 if(!(f
& a_SATURATED
))
2969 goto jenum_overflow
;
2970 f
|= a_SOFTOVERFLOW
;
2971 lhv
= (lhv
< 0 || xop
== '-') ? SI64_MIN
: SI64_MAX
;
2976 /* Will the result be positive? */
2977 if((lhv
< 0) == (rhv
< 0)){
2982 if(rhv
!= 0 && lhv
!= 0 && SI64_MAX
/ rhv
> lhv
){
2983 if(!(f
& a_SATURATED
))
2984 goto jenum_overflow
;
2985 f
|= a_SOFTOVERFLOW
;
2991 if(lhv
!= 0 && SI64_MIN
/ lhv
< rhv
){
2992 if(!(f
& a_SATURATED
))
2993 goto jenum_overflow
;
2994 f
|= a_SOFTOVERFLOW
;
2999 if(rhv
!= 0 && lhv
!= 0 && SI64_MIN
/ rhv
< lhv
){
3000 if(!(f
& a_SATURATED
))
3001 goto jenum_overflow
;
3002 f
|= a_SOFTOVERFLOW
;
3011 if(!(f
& a_SATURATED
))
3013 f
|= a_SOFTOVERFLOW
;
3020 if(!(f
& a_SATURATED
))
3022 f
|= a_SOFTOVERFLOW
;
3040 if(rhv
> 63){ /* xxx 63? */
3041 if(!(f
& a_SATURATED
))
3042 goto jenum_overflow
;
3047 else if(f
& a_UNSIGNED
)
3048 lhv
= (ui64_t
)lhv
>> (ui8_t
)rhv
;
3058 }else if(cp
[2] == '\0' && cp
[1] == '@'){
3062 }else if(cp
[0] == '<'){
3074 }else if(cp
[0] == '>'){
3090 }else if(is_asccaseprefix(cp
, "length")){
3091 f
|= a_ISNUM
| a_ISDECIMAL
;
3092 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3095 i
= strlen(*++argv
);
3096 if(UICMP(64, i
, >, SI64_MAX
))
3097 goto jestr_overflow
;
3099 }else if(is_asccaseprefix(cp
, "hash")){
3100 f
|= a_ISNUM
| a_ISDECIMAL
;
3101 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3104 i
= n_torek_hash(*++argv
);
3106 }else if(is_asccaseprefix(cp
, "find")){
3107 f
|= a_ISNUM
| a_ISDECIMAL
;
3108 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3111 if((cp
= strstr(argv
[1], argv
[2])) == NULL
)
3113 i
= PTR2SIZE(cp
- argv
[1]);
3114 if(UICMP(64, i
, >, SI64_MAX
))
3115 goto jestr_overflow
;
3117 }else if(is_asccaseprefix(cp
, "ifind")){
3118 f
|= a_ISNUM
| a_ISDECIMAL
;
3119 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3122 if((cp
= asccasestr(argv
[1], argv
[2])) == NULL
)
3124 i
= PTR2SIZE(cp
- argv
[1]);
3125 if(UICMP(64, i
, >, SI64_MAX
))
3126 goto jestr_overflow
;
3128 }else if(is_asccaseprefix(cp
, "substring")){
3129 if(argv
[1] == NULL
|| argv
[2] == NULL
)
3131 if(argv
[3] != NULL
&& argv
[4] != NULL
)
3134 i
= strlen(varres
= *++argv
);
3136 if(*(cp
= *++argv
) == '\0')
3138 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3139 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3140 ) != n_IDEC_STATE_CONSUMED
)
3141 goto jestr_numrange
;
3143 if(UICMP(64, i
, <, -lhv
))
3144 goto jesubstring_off
;
3147 if(UICMP(64, i
, >=, lhv
)){
3152 if(n_poption
& n_PO_D_V
)
3153 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
3154 n_shexp_quote_cp(argv
[-1], FAL0
));
3155 f
|= a_SOFTOVERFLOW
;
3158 if(argv
[1] != NULL
){
3159 if(*(cp
= *++argv
) == '\0')
3161 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3162 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3163 ) != n_IDEC_STATE_CONSUMED
)
3164 goto jestr_numrange
;
3166 if(UICMP(64, i
, <, -lhv
))
3167 goto jesubstring_len
;
3170 if(UICMP(64, i
, >=, lhv
)){
3171 if(UICMP(64, i
, !=, lhv
))
3172 varres
= savestrbuf(varres
, (size_t)lhv
);
3175 if(n_poption
& n_PO_D_V
)
3176 n_err(_("`vexpr': substring: length argument too large: %s\n"),
3177 n_shexp_quote_cp(argv
[-2], FAL0
));
3178 f
|= a_SOFTOVERFLOW
;
3181 }else if(is_asccaseprefix(cp
, "trim")) Jtrim
: {
3183 enum n_str_trim_flags stf
;
3185 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3188 if(is_asccaseprefix(cp
, "trim-front"))
3189 stf
= n_STR_TRIM_FRONT
;
3190 else if(is_asccaseprefix(cp
, "trim-end"))
3191 stf
= n_STR_TRIM_END
;
3193 stf
= n_STR_TRIM_BOTH
;
3195 trim
.l
= strlen(trim
.s
= n_UNCONST(argv
[1]));
3196 (void)n_str_trim(&trim
, stf
);
3197 varres
= savestrbuf(trim
.s
, trim
.l
);
3198 }else if(is_asccaseprefix(cp
, "trim-front"))
3200 else if(is_asccaseprefix(cp
, "trim-end"))
3202 else if(is_asccaseprefix(cp
, "random")){
3203 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3206 if((n_idec_si64_cp(&lhv
, argv
[1], 0, NULL
3207 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3208 ) != n_IDEC_STATE_CONSUMED
|| lhv
< 0 || lhv
> PATH_MAX
)
3209 goto jestr_numrange
;
3212 varres
= n_random_create_cp((size_t)lhv
, NULL
);
3213 }else if(is_asccaseprefix(cp
, "file-expand")){
3214 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3217 if((varres
= fexpand(argv
[1], FEXP_NVAR
| FEXP_NOPROTO
)) == NULL
)
3219 }else if(is_asccaseprefix(cp
, "makeprint")){
3220 struct str sin
, sout
;
3222 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3225 /* XXX using strlen for `vexpr makeprint' is wrong for UTF-16 */
3226 sin
.l
= strlen(sin
.s
= n_UNCONST(argv
[1]));
3227 makeprint(&sin
, &sout
);
3228 varres
= savestrbuf(sout
.s
, sout
.l
);
3230 /* TODO `vexpr': (wide) string length, find, etc!! */
3232 }else if(is_asccaseprefix(cp
, "regex")) Jregex
:{
3233 regmatch_t rema
[1 + n_VEXPR_REGEX_MAX
];
3237 f
|= a_ISNUM
| a_ISDECIMAL
;
3238 if(argv
[1] == NULL
|| argv
[2] == NULL
||
3239 (argv
[3] != NULL
&& argv
[4] != NULL
))
3242 reflrv
= REG_EXTENDED
;
3244 reflrv
|= REG_ICASE
;
3245 if((reflrv
= regcomp(&re
, argv
[2], reflrv
))){
3246 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
3247 n_shexp_quote_cp(argv
[2], FAL0
), n_regex_err_to_doc(&re
, reflrv
));
3249 n_pstate_err_no
= n_ERR_INVAL
;
3252 reflrv
= regexec(&re
, argv
[1], n_NELEM(rema
), rema
, 0);
3254 if(reflrv
== REG_NOMATCH
)
3257 /* Search only? Else replace, which is a bit */
3258 if(argv
[3] == NULL
){
3259 if(UICMP(64, rema
[0].rm_so
, >, SI64_MAX
))
3260 goto jestr_overflow
;
3261 lhv
= (si64_t
)rema
[0].rm_so
;
3263 /* We need to setup some kind of pseudo macro environment for this */
3264 struct a_amv_lostack los
;
3265 struct a_amv_mac_call_args amca
;
3266 char const **reargv
;
3268 memset(&amca
, 0, sizeof amca
);
3269 amca
.amca_name
= savestrbuf(&argv
[1][rema
[0].rm_so
],
3270 rema
[0].rm_eo
- rema
[0].rm_so
);
3271 amca
.amca_amp
= a_AMV_MACKY_MACK
;
3272 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3274 amca
.amca_pospar
.app_count
= (ui32_t
)i
- 1;
3275 amca
.amca_pospar
.app_not_heap
= TRU1
;
3276 amca
.amca_pospar
.app_dat
=
3277 reargv
= n_autorec_alloc(sizeof(char*) * i
);
3278 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3279 *reargv
++ = savestrbuf(&argv
[1][rema
[i
].rm_so
],
3280 rema
[i
].rm_eo
- rema
[i
].rm_so
);
3283 memset(&los
, 0, sizeof los
);
3284 hold_all_sigs(); /* TODO DISLIKE! */
3285 los
.as_global_saved
= a_amv_lopts
;
3286 los
.as_amcap
= &amca
;
3287 los
.as_up
= los
.as_global_saved
;
3292 struct n_string s_b
;
3293 enum n_shexp_state shs
;
3295 templ
.s
= n_UNCONST(argv
[3]);
3297 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
3298 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
3299 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
),
3300 n_string_creat_auto(&s_b
), &templ
, NULL
);
3301 if((shs
& (n_SHEXP_STATE_ERR_MASK
| n_SHEXP_STATE_STOP
)
3302 ) == n_SHEXP_STATE_STOP
){
3303 varres
= n_string_cp(&s_b
);
3304 n_string_drop_ownership(&s_b
);
3309 a_amv_lopts
= los
.as_global_saved
;
3310 rele_all_sigs(); /* TODO DISLIKE! */
3314 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
3316 }else if(is_asccaseprefix(argv
[0], "iregex")){
3319 #endif /* HAVE_REGEX */
3323 n_pstate_err_no
= (f
& a_SOFTOVERFLOW
) ? n_ERR_OVERFLOW
: n_ERR_NONE
;
3326 /* Generate the variable value content for numerics.
3327 * Anticipate in our handling below! (Don't do needless work) */
3329 if((f
& a_ISNUM
) && ((f
& a_ISDECIMAL
) || varname
!= NULL
)){
3330 snprintf(varbuf
, sizeof varbuf
, "%" PRId64
, lhv
);
3334 if(varname
== NULL
){
3335 /* If there was no error and we are printing a numeric result, print some
3336 * more bases for the fun of it */
3337 if((f
& (a_ERR
| a_ISNUM
| a_ISDECIMAL
)) == a_ISNUM
){
3340 for(j
= 1, i
= 0; i
< 64; ++i
){
3341 varbuf
[63 + 64 / 8 -j
- i
] = (lhv
& ((ui64_t
)1 << i
)) ? '1' : '0';
3342 if((i
& 7) == 7 && i
!= 63){
3344 varbuf
[63 + 64 / 8 -j
- i
] = ' ';
3347 varbuf
[64 + 64 / 8 -1] = '\0';
3348 if(fprintf(n_stdout
,
3349 "%s\n0%" PRIo64
" | 0x%" PRIX64
" | %" PRId64
"\n",
3350 varbuf
, lhv
, lhv
, lhv
) < 0){
3351 n_pstate_err_no
= n_err_no
;
3354 }else if(varres
!= NULL
&& fprintf(n_stdout
, "%s\n", varres
) < 0){
3355 n_pstate_err_no
= n_err_no
;
3358 }else if(!n_var_vset(varname
, (uintptr_t)varres
)){
3359 n_pstate_err_no
= n_ERR_NOTSUP
;
3363 return (f
& a_ERR
) ? 1 : 0;
3366 f
= a_ERR
| a_ISNUM
;
3370 n_err(_("`vexpr': invalid subcommand: %s\n"),
3371 n_shexp_quote_cp(*argv
, FAL0
));
3372 n_pstate_err_no
= n_ERR_INVAL
;
3375 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3376 n_pstate_err_no
= n_ERR_INVAL
;
3380 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3381 n_shexp_quote_cp(*argv
, FAL0
));
3382 n_pstate_err_no
= n_ERR_RANGE
;
3385 n_err(_("`vexpr': expression overflows datatype: %" PRId64
" %c %" PRId64
3386 "\n"), lhv
, op
, rhv
);
3387 n_pstate_err_no
= n_ERR_OVERFLOW
;
3391 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3392 n_shexp_quote_cp(*argv
, FAL0
));
3393 n_pstate_err_no
= n_ERR_RANGE
;
3396 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3397 n_pstate_err_no
= n_ERR_OVERFLOW
;
3400 n_pstate_err_no
= n_ERR_NODATA
;
3411 struct n_cmd_arg
*cap
;
3413 struct a_amv_pospar
*appp
;
3422 struct n_cmd_arg_ctx
*cacp
;
3425 n_pstate_err_no
= n_ERR_NONE
;
3426 n_UNINIT(varres
, n_empty
);
3428 cap
= cacp
->cac_arg
;
3430 if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "set"))
3432 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "clear"))
3434 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "quote"))
3437 n_err(_("`vpospar': invalid subcommand: %s\n"),
3438 n_shexp_quote_cp(cap
->ca_arg
.ca_str
.s
, FAL0
));
3439 n_pstate_err_no
= n_ERR_INVAL
;
3445 if((f
& (a_CLEAR
| a_QUOTE
)) && cap
->ca_next
!= NULL
){
3446 n_err(_("`vpospar': `%s': takes no argument\n"), cap
->ca_arg
.ca_str
.s
);
3447 n_pstate_err_no
= n_ERR_INVAL
;
3454 /* If in a macro, we need to overwrite the local instead of global argv */
3455 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
3458 if(f
& (a_SET
| a_CLEAR
)){
3459 if(cacp
->cac_vput
!= NULL
)
3460 n_err(_("`vpospar': `vput' only supported for `quote' subcommand\n"));
3461 if(!appp
->app_not_heap
&& appp
->app_maxcount
> 0){
3462 for(i
= appp
->app_maxcount
; i
-- != 0;)
3463 n_free(n_UNCONST(appp
->app_dat
[i
]));
3464 n_free(appp
->app_dat
);
3466 memset(appp
, 0, sizeof *appp
);
3469 if((i
= cacp
->cac_no
) > a_AMV_POSPAR_MAX
){
3470 n_err(_("`vpospar': overflow: %" PRIuZ
" arguments!\n"), i
);
3471 n_pstate_err_no
= n_ERR_OVERFLOW
;
3476 memset(appp
, 0, sizeof *appp
);
3478 appp
->app_maxcount
= appp
->app_count
= (ui16_t
)i
;
3479 /* XXX Optimize: store it all in one chunk! */
3481 i
*= sizeof *appp
->app_dat
;
3482 appp
->app_dat
= n_alloc(i
);
3484 for(i
= 0; cap
!= NULL
; ++i
, cap
= cap
->ca_next
){
3485 appp
->app_dat
[i
] = n_alloc(cap
->ca_arg
.ca_str
.l
+1);
3486 memcpy(n_UNCONST(appp
->app_dat
[i
]), cap
->ca_arg
.ca_str
.s
,
3487 cap
->ca_arg
.ca_str
.l
+1);
3490 appp
->app_dat
[i
] = NULL
;
3494 if(appp
->app_count
== 0)
3498 struct n_string s
, *sp
;
3501 sp
= n_string_creat_auto(&s
);
3503 sep1
= *ok_vlook(ifs
);
3504 sep2
= *ok_vlook(ifs_ws
);
3510 for(i
= 0; i
< appp
->app_count
; ++i
){
3512 if(!n_string_can_swallow(sp
, 2))
3514 sp
= n_string_push_c(sp
, sep1
);
3516 sp
= n_string_push_c(sp
, sep2
);
3518 in
.l
= strlen(in
.s
= n_UNCONST(appp
->app_dat
[i
+ appp
->app_idx
]));
3520 if(!n_string_can_swallow(sp
, in
.l
)){
3522 n_err(_("`vpospar': overflow: string too long!\n"));
3523 n_pstate_err_no
= n_ERR_OVERFLOW
;
3527 sp
= n_shexp_quote(sp
, &in
, TRU1
);
3530 varres
= n_string_cp(sp
);
3533 if(cacp
->cac_vput
== NULL
){
3534 if(fprintf(n_stdout
, "%s\n", varres
) < 0){
3535 n_pstate_err_no
= n_err_no
;
3538 }else if(!n_var_vset(cacp
->cac_vput
, (uintptr_t)varres
)){
3539 n_pstate_err_no
= n_ERR_NOTSUP
;
3545 return (f
& a_ERR
) ? 1 : 0;