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_EXT_LINKED
= 1u<<24, /* `environ' link'ed */
117 a_AMV_VF_EXT_FROZEN
= 1u<<25, /* Has been set by -S,.. */
118 a_AMV_VF_EXT_FROZEN_UNSET
= 1u<<26, /* ..and was used to unset a variable */
119 a_AMV_VF_EXT__FROZEN_MASK
= a_AMV_VF_EXT_FROZEN
| a_AMV_VF_EXT_FROZEN_UNSET
,
120 a_AMV_VF_EXT__MASK
= (1u<<(26+1)) - 1
123 /* We support some special parameter names for one(+)-letter variable names;
124 * note these have counterparts in the code that manages shell expansion!
125 * All these special variables are solely backed by n_var_vlook(), and besides
126 * there is only a_amv_var_revlookup() which knows about them */
127 enum a_amv_var_special_category
{
128 a_AMV_VSC_NONE
, /* Normal variable, no special treatment */
129 a_AMV_VSC_GLOBAL
, /* ${[?!]} are specially mapped, but global */
130 a_AMV_VSC_MULTIPLEX
, /* ${^.*} circumflex accent multiplexer */
131 a_AMV_VSC_POSPAR
, /* ${[1-9][0-9]*} positional parameters */
132 a_AMV_VSC_POSPAR_ENV
/* ${[*@#]} positional parameter support variables */
135 enum a_amv_var_special_type
{
137 a_AMV_VST_QM
, /* ? */
138 a_AMV_VST_EM
, /* ! */
140 /* This is special in that it is a multiplex indicator, the ^ is followed by
141 * a normal variable */
142 a_AMV_VST_CACC
, /* ^ (circumflex accent) */
143 /* _VSC_POSPAR_ENV */
144 a_AMV_VST_STAR
, /* * */
145 a_AMV_VST_AT
, /* @ */
146 a_AMV_VST_NOSIGN
/* # */
149 enum a_amv_var_vip_mode
{
156 ui16_t app_maxcount
; /* == slots in .app_dat */
157 ui16_t app_count
; /* Maximum a_AMV_POSPAR_MAX */
158 ui16_t app_idx
; /* `shift' moves this one, decs .app_count */
159 bool_t app_not_heap
; /* .app_dat stuff not on dynamically allocated */
161 char const **app_dat
; /* NULL terminated (for "$@" explosion support) */
163 n_CTA(a_AMV_POSPAR_MAX
<= SI16_MAX
, "Limit exceeds datatype capabilities");
166 struct a_amv_mac
*am_next
;
167 ui32_t am_maxlen
; /* of any line in .am_line_dat */
168 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
169 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
170 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
171 ui32_t am_refcnt
; /* 0-based for `un{account,define}' purposes */
172 ui8_t am_flags
; /* enum a_amv_mac_flags */
173 char am_name
[n_VFIELD_SIZE(3)]; /* of this macro */
175 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
177 struct a_amv_mac_line
{
179 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
180 char aml_dat
[n_VFIELD_SIZE(0)];
183 struct a_amv_mac_call_args
{
184 char const *amca_name
; /* For MACKY_MACK, this is *0*! */
185 struct a_amv_mac
*amca_amp
; /* "const", but for am_refcnt */
186 struct a_amv_var
**amca_unroller
;
187 void (*amca_hook_pre
)(void *);
190 bool_t amca_ps_hook_mask
;
191 bool_t amca_no_xcall
; /* We want n_GO_INPUT_NO_XCALL for this */
193 struct a_amv_pospar amca_pospar
;
196 struct a_amv_lostack
{
197 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
198 struct a_amv_mac_call_args
*as_amcap
;
199 struct a_amv_lostack
*as_up
; /* Outer context */
200 struct a_amv_var
*as_lopts
;
201 ui8_t as_loflags
; /* enum a_amv_mac_loflags */
206 struct a_amv_var
*av_link
;
209 char *av_env
; /* Actively managed putenv(3) memory, or NULL */
211 ui32_t av_flags
; /* enum a_amv_var_flags inclusive extended bits */
212 char av_name
[n_VFIELD_SIZE(4)];
214 n_CTA(a_AMV_VF_EXT__MASK
<= UI32_MAX
, "Enumeration excesses storage datatype");
216 struct a_amv_var_map
{
219 ui16_t avm_flags
; /* enum a_amv_var_flags without extended bits */
221 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
223 struct a_amv_var_virt
{
226 struct a_amv_var
const *avv_var
;
229 struct a_amv_var_defval
{
232 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
235 struct a_amv_var_carrier
{
236 char const *avc_name
;
239 struct a_amv_var
*avc_var
;
240 struct a_amv_var_map
const *avc_map
;
243 ui8_t avc_special_cat
;
244 /* Numerical parameter name if .avc_special_cat=a_AMV_VSC_POSPAR,
245 * otherwise a enum a_amv_var_special_type */
246 ui16_t avc_special_prop
;
249 /* Include constant make-okey-map.pl output, and the generated version data */
250 #include <gen-version.h>
251 #include <gen-okeys.h>
253 /* The currently active account */
254 static struct a_amv_mac
*a_amv_acc_curr
;
256 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
258 /* Unroll list of currently running macro stack */
259 static struct a_amv_lostack
*a_amv_lopts
;
261 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
263 /* Global (i.e., non-local) a_AMV_VSC_POSPAR stack */
264 static struct a_amv_pospar a_amv_pospar
;
266 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
267 * TODO today via a static lostack, it should be a field in mailbox, once that
268 * TODO is a real multi-instance object */
269 static struct a_amv_var
*a_amv_folder_hook_lopts
;
271 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
272 static struct a_amv_var
*a_amv_compose_lopts
;
274 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
275 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
276 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
277 * plain lookup cases or when _UNDEF was performed on a currently active entry
278 * (the entry will have been unlinked, and the _MF_DELETE will be honoured once
279 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
280 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
281 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
283 /* `call', `call_if' (and `xcall' via go.c -> c_call()) */
284 static int a_amv_mac_call(void *v
, bool_t silent_nexist
);
286 /* Execute a macro; amcap must reside in LOFI memory */
287 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
289 static void a_amv_mac__finalize(void *vp
);
291 /* User display helpers */
292 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
294 /* _def() returns error for faulty definitions and already existing * names,
295 * _undef() returns error if a named thing doesn't exist */
296 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
297 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
300 static void a_amv_mac_free(struct a_amv_mac
*amp
);
302 /* Update replay-log */
303 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
304 struct a_amv_var
*oavp
);
305 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
307 /* Special cased value string allocation */
308 static char *a_amv_var_copy(char const *str
);
309 static void a_amv_var_free(char *cp
);
311 /* Check for special housekeeping. _VIP_SET_POST and _VIP_CLEAR do not fail
312 * (or propagate errors), _VIP_SET_PRE may and should case abortion */
313 static bool_t
a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
,
314 enum okeys okey
, char const *val
);
316 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
317 static bool_t
a_amv_var_check_nocntrls(char const *val
);
318 static bool_t
a_amv_var_check_num(char const *val
, bool_t posnum
);
320 /* If a variable name begins with a lowercase-character and contains at
321 * least one '@', it is converted to all-lowercase. This is necessary
322 * for lookups of names based on email addresses.
323 * Following the standard, only the part following the last '@' should
324 * be lower-cased, but practice has established otherwise here */
325 static char const *a_amv_var_canonify(char const *vn
);
327 /* Try to reverse lookup an option name to an enum okeys mapping.
328 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
329 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
332 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
333 * Sets .avc_prime; .avc_var is NULL if not found.
334 * Here it is where we care for _I3VAL and _DEFVAL.
335 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
336 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
337 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
338 * return FAL0, then! */
339 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
342 /* Lookup functions for special category variables, _pospar drives all
343 * positional parameter etc. special categories */
344 static char const *a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
);
345 static char const *a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
);
346 static char const *a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
);
348 /* Set var from .avc_(map|name|hash), return success */
349 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
352 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
353 struct a_amv_var
*avp
);
355 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
356 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
358 static bool_t
a_amv_var__clearenv(char const *name
, struct a_amv_var
*avp
);
360 /* List all variables */
361 static void a_amv_var_show_all(void);
363 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
365 /* Actually do print one, return number of lines written */
366 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
368 /* Shared c_set() and c_environ():set impl, return success */
369 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
371 static struct a_amv_mac
*
372 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
373 enum a_amv_mac_flags amf
){
374 struct a_amv_mac
*amp
, **ampp
;
376 enum a_amv_mac_flags save_amf
;
380 amf
&= a_AMV_MF_TYPE_MASK
;
381 h
= a_AMV_NAME2HASH(name
);
382 h
= a_AMV_HASH2PRIME(h
);
383 ampp
= &a_amv_macs
[h
];
385 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
386 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
387 !strcmp(amp
->am_name
, name
)){
388 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
391 *ampp
= amp
->am_next
;
393 if(amp
->am_refcnt
> 0){
394 amp
->am_flags
|= a_AMV_MF_DELETE
;
395 if(n_poption
& n_PO_D_V
)
396 n_err(_("Delayed deletion of currently active %s: %s\n"),
397 (amp
->am_flags
& a_AMV_MF_ACCOUNT
? "account" : "define"),
401 amp
= (struct a_amv_mac
*)-1;
408 ampp
= &a_amv_macs
[h
];
409 newamp
->am_next
= *ampp
;
419 a_amv_mac_call(void *v
, bool_t silent_nexist
){
420 struct a_amv_mac
*amp
;
423 struct n_cmd_arg_ctx
*cacp
;
428 if(cacp
->cac_no
== 0){
429 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
430 n_pstate_err_no
= n_ERR_INVAL
;
435 name
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
437 if(n_UNLIKELY(cacp
->cac_no
> a_AMV_POSPAR_MAX
)){
438 n_err(_("Too many arguments to macro `call': %s\n"), name
);
439 n_pstate_err_no
= n_ERR_OVERFLOW
;
441 }else if(n_UNLIKELY((amp
= a_amv_mac_lookup(name
, NULL
, a_AMV_MF_NONE
)
444 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name
, FAL0
));
445 n_pstate_err_no
= n_ERR_NOENT
;
449 struct a_amv_mac_call_args
*amcap
;
452 argc
= cacp
->cac_no
+ 1;
453 amcap
= n_lofi_alloc(sizeof *amcap
+ (argc
* sizeof *argv
));
454 argv
= (void*)&amcap
[1];
456 for(argc
= 0; (cacp
->cac_arg
= cacp
->cac_arg
->ca_next
) != NULL
; ++argc
)
457 argv
[argc
] = cacp
->cac_arg
->ca_arg
.ca_str
.s
;
460 memset(amcap
, 0, sizeof *amcap
);
461 amcap
->amca_name
= name
;
462 amcap
->amca_amp
= amp
;
463 if(a_amv_lopts
!= NULL
)
464 amcap
->amca_loflags
= (a_amv_lopts
->as_loflags
& a_AMV_LF_CALL_MASK
465 ) >> a_AMV_LF_CALL_TO_SCOPE_SHIFT
;
467 amcap
->amca_pospar
.app_count
= (ui16_t
)argc
;
468 amcap
->amca_pospar
.app_not_heap
= TRU1
;
469 amcap
->amca_pospar
.app_dat
= argv
;
472 (void)a_amv_mac_exec(amcap
);
481 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
482 struct a_amv_lostack
*losp
;
483 struct a_amv_mac_line
**amlp
;
484 char **args_base
, **args
;
485 struct a_amv_mac
*amp
;
489 amp
= amcap
->amca_amp
;
490 assert(amp
!= NULL
&& amp
!= a_AMV_MACKY_MACK
);
492 /* XXX Unfortunately we yet need to dup the macro lines! :( */
493 args_base
= args
= n_alloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
494 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
495 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
498 losp
= n_lofi_alloc(sizeof *losp
);
499 losp
->as_global_saved
= a_amv_lopts
;
500 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
501 losp
->as_up
= losp
->as_global_saved
;
502 losp
->as_lopts
= NULL
;
505 losp
->as_lopts
= *amcap
->amca_unroller
;
507 losp
->as_loflags
= amcap
->amca_loflags
;
510 if(amcap
->amca_hook_pre
!= NULL
)
511 n_PS_ROOT_BLOCK((*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
));
512 rv
= n_go_macro((n_GO_INPUT_NONE
|
513 (amcap
->amca_no_xcall
? n_GO_INPUT_NO_XCALL
: 0)),
514 amp
->am_name
, args_base
, &a_amv_mac__finalize
, losp
);
520 a_amv_mac__finalize(void *vp
){
521 struct a_amv_mac
*amp
;
522 struct a_amv_mac_call_args
*amcap
;
523 struct a_amv_lostack
*losp
;
527 a_amv_lopts
= losp
->as_global_saved
;
529 amcap
= losp
->as_amcap
;
531 if(!amcap
->amca_pospar
.app_not_heap
&& amcap
->amca_pospar
.app_maxcount
> 0){
534 for(i
= amcap
->amca_pospar
.app_maxcount
; i
-- != 0;)
535 n_free(n_UNCONST(amcap
->amca_pospar
.app_dat
[i
]));
536 n_free(amcap
->amca_pospar
.app_dat
);
539 if(amcap
->amca_unroller
== NULL
){
540 if(losp
->as_lopts
!= NULL
)
541 a_amv_lopts_unroll(&losp
->as_lopts
);
543 *amcap
->amca_unroller
= losp
->as_lopts
;
545 if(amcap
->amca_ps_hook_mask
)
546 n_pstate
&= ~n_PS_HOOK_MASK
;
548 if((amp
= amcap
->amca_amp
) != a_AMV_MACKY_MACK
&& amp
!= NULL
&&
549 --amp
->am_refcnt
== 0 && (amp
->am_flags
& a_AMV_MF_DELETE
))
558 a_amv_mac_show(enum a_amv_mac_flags amf
){
559 size_t lc
, mc
, ti
, i
;
567 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
569 n_perr(_("`define' or `account' list: cannot create temporary file"), 0);
573 amf
&= a_AMV_MF_TYPE_MASK
;
574 typestr
= (amf
& a_AMV_MF_ACCOUNT
) ? "account" : "define";
576 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
577 struct a_amv_mac
*amp
;
579 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
580 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
581 struct a_amv_mac_line
**amlpp
;
588 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
589 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
590 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
592 fputs((*amlpp
)->aml_dat
, fp
);
601 page_or_print(fp
, lc
);
603 rv
= (ferror(fp
) == 0);
611 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
613 ui32_t line_cnt
, maxlen
;
615 struct linelist
*ll_next
;
616 struct a_amv_mac_line
*ll_amlp
;
617 } *llp
, *ll_head
, *ll_tail
;
618 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
619 struct a_amv_mac
*amp
;
623 memset(&line
, 0, sizeof line
);
627 /* TODO We should have our input state machine which emits Line events,
628 * TODO and hook different consumers dependent on our content, as stated
629 * TODO in i think lex_input; */
630 /* Read in the lines which form the macro content */
631 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
635 n
.i
= n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, n_empty
,
636 &line
.s
, &line
.l
, NULL
, NULL
);
640 n_err(_("Unterminated %s definition: %s\n"),
641 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
645 /* Trim WS, remember amount of leading spaces for display purposes */
646 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
648 leaspc
= (leaspc
+ 8u) & ~7u;
653 for(; n
.ui
> 0 && spacechar(cp
[n
.ui
- 1]); --n
.ui
)
658 maxlen
= n_MAX(maxlen
, n
.ui
);
661 /* Is is the closing brace? */
665 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
666 struct a_amv_mac_line
*amlp
;
668 llp
= n_autorec_alloc(sizeof *llp
);
672 ll_tail
->ll_next
= llp
;
675 llp
->ll_amlp
= amlp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
677 amlp
->aml_len
= n
.ui
-1;
678 amlp
->aml_prespc
= leaspc
;
679 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
681 n_err(_("Too much content in %s definition: %s\n"),
682 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
687 /* Create the new macro */
688 n
.s
= strlen(name
) +1;
689 amp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
690 memset(amp
, 0, n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
));
691 amp
->am_maxlen
= maxlen
;
692 amp
->am_line_cnt
= line_cnt
;
694 memcpy(amp
->am_name
, name
, n
.s
);
696 struct a_amv_mac_line
**amlpp
;
698 amp
->am_line_dat
= amlpp
= n_alloc(sizeof(*amlpp
) * ++line_cnt
);
699 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
700 *amlpp
++ = llp
->ll_amlp
;
704 /* Create entry, replace a yet existing one as necessary */
705 a_amv_mac_lookup(name
, amp
, amf
| a_AMV_MF_UNDEF
);
714 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
715 n_free(llp
->ll_amlp
);
718 * n_free(amp->am_line_dat);
725 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
726 struct a_amv_mac
*amp
;
732 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
733 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
734 n_err(_("%s not defined: %s\n"),
735 (amf
& a_AMV_MF_ACCOUNT
? "Account" : "Macro"), name
);
739 struct a_amv_mac
**ampp
, *lamp
;
741 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
743 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
744 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
745 /* xxx Expensive but rare: be simple */
746 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
747 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
759 a_amv_mac_free(struct a_amv_mac
*amp
){
760 struct a_amv_mac_line
**amlpp
;
763 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
765 n_free(amp
->am_line_dat
);
771 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
772 struct a_amv_var
*oavp
){
773 struct a_amv_var
*avp
;
777 /* Propagate unrolling up the stack, as necessary */
780 if(alp
->as_loflags
& a_AMV_LF_SCOPE_MASK
)
782 if((alp
= alp
->as_up
) == NULL
)
786 /* Check whether this variable is handled yet XXX Boost: ID check etc.!! */
787 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
788 if(!strcmp(avp
->av_name
, name
))
791 nl
= strlen(name
) +1;
792 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
793 avp
= n_calloc(1, n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
794 avp
->av_link
= alp
->as_lopts
;
797 avp
->av_value
= &avp
->av_name
[nl
];
798 avp
->av_flags
= oavp
->av_flags
;
799 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
801 if(oavp
->av_env
!= NULL
)
802 avp
->av_env
= sstrdup(oavp
->av_env
);
803 assert(avp
->av_env
== NULL
||
804 (avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_EXT_LINKED
)));
807 memcpy(avp
->av_name
, name
, nl
);
813 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
814 struct a_amv_lostack
*save_alp
;
815 struct a_amv_var
*x
, *avp
;
821 save_alp
= a_amv_lopts
;
826 n_PS_ROOT_BLOCK(n_var_vset(x
->av_name
, (uintptr_t)x
->av_value
));
829 a_amv_lopts
= save_alp
;
834 a_amv_var_copy(char const *str
){
840 news
= n_UNCONST(n_empty
);
841 else if(str
[1] == '\0'){
843 news
= n_UNCONST(n_1
);
844 else if(str
[0] == '0')
845 news
= n_UNCONST(n_0
);
848 }else if(str
[2] == '\0' && str
[0] == '-' && str
[1] == '1')
849 news
= n_UNCONST(n_m1
);
852 len
= strlen(str
) +1;
854 memcpy(news
, str
, len
);
861 a_amv_var_free(char *cp
){
863 if(cp
[0] != '\0' && cp
!= n_0
&& cp
!= n_1
&& cp
!= n_m1
)
869 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
, enum okeys okey
,
876 if(avvm
== a_AMV_VIP_SET_PRE
){
881 /* Note this gets called from main.c during initialization, and they
882 * simply set this to pw_dir as a fallback: don't verify _that_ call.
884 if(!(n_pstate
& n_PS_ROOT
) && !n_is_dir(val
, TRUM1
)){
885 n_err(_("$HOME is not a directory or not accessible: %s\n"),
886 n_shexp_quote_cp(val
, FAL0
));
891 if(!n_is_dir(val
, TRU1
)){
892 n_err(_("$TMPDIR is not a directory or not accessible: %s\n"),
893 n_shexp_quote_cp(val
, FAL0
));
901 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
902 if(uib
& ~0777u){ /* (is valid _VF_POSNUM) */
903 n_err(_("Invalid *umask* setting: %s\n"), val
);
909 }else if(avvm
== a_AMV_VIP_SET_POST
){
917 n_poption
|= n_PO_DEBUG
;
920 /* Invalidate any resolved folder then, too
923 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
928 x_b
= x
= n_autorec_alloc(strlen(val
) +1);
929 while((c
= *val
++) != '\0')
933 n_PS_ROOT_BLOCK(ok_vset(ifs_ws
, x_b
));
935 #ifdef HAVE_SETLOCALE
943 n_poption
|= n_PO_MEMDEBUG
;
945 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
946 if(!(n_pstate
& n_PS_ROOT
))
947 n_PS_ROOT_BLOCK(ok_bset(posix
));
949 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
950 if(!(n_pstate
& n_PS_ROOT
))
951 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT
));
953 case ok_b_skipemptybody
:
954 n_poption
|= n_PO_E_FLAG
;
956 case ok_b_typescript_mode
:
957 ok_bset(colour_disable
);
958 ok_bset(line_editor_disable
);
959 if(!(n_psonce
& n_PSO_STARTED
))
960 ok_bset(termcap_disable
);
966 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
971 n_poption
|= (n_poption
& n_PO_VERB
) ? n_PO_VERBVERB
: n_PO_VERB
;
982 n_poption
&= ~n_PO_DEBUG
;
985 /* Invalidate any resolved folder then, too
988 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
991 n_poption
&= ~n_PO_MEMDEBUG
;
993 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
994 if(!(n_pstate
& n_PS_ROOT
))
995 n_PS_ROOT_BLOCK(ok_bclear(posix
));
997 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
998 if(!(n_pstate
& n_PS_ROOT
))
999 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT
));
1001 case ok_b_skipemptybody
:
1002 n_poption
&= ~n_PO_E_FLAG
;
1005 n_poption
&= ~(n_PO_VERB
| n_PO_VERBVERB
);
1014 a_amv_var_check_nocntrls(char const *val
){
1018 while((c
= *val
++) != '\0')
1026 a_amv_var_check_num(char const *val
, bool_t posnum
){
1027 /* TODO The internal/environment variables which are num= or posnum= should
1028 * TODO gain special lookup functions, or the return should be void* and
1029 * TODO castable to integer; i.e. no more strtoX() should be needed.
1030 * TODO I.e., the result of this function should instead be stored */
1036 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1038 enum n_idec_state ids
;
1040 ids
= n_idec_cp(&uib
, val
, 0,
1041 (posnum
? n_IDEC_MODE_SIGNED_TYPE
: n_IDEC_MODE_NONE
), NULL
);
1042 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1043 ) != n_IDEC_STATE_CONSUMED
)
1045 /* TODO Unless we store integers we need to look and forbid, because
1046 * TODO callee may not be able to swallow, e.g., "-1" */
1047 if(posnum
&& (ids
& n_IDEC_STATE_SEEN_MINUS
))
1055 a_amv_var_canonify(char const *vn
){
1057 if(!upperchar(*vn
)){
1060 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
1062 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
1069 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
1071 struct a_amv_var_map
const *avmp
;
1075 /* It may be a special a.k.a. macro-local or one-letter parameter */
1077 if(n_UNLIKELY(digitchar(c
))){
1078 /* (Inline dec. atoi, ugh) */
1079 for(j
= (ui8_t
)c
- '0', i
= 1;; ++i
){
1084 goto jno_special_param
;
1085 j
= j
* 10 + (ui8_t
)c
- '0';
1087 if(j
<= a_AMV_POSPAR_MAX
){
1088 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR
;
1089 goto jspecial_param
;
1091 }else if(n_UNLIKELY(name
[1] == '\0')){
1095 avcp
->avc_special_cat
= a_AMV_VSC_GLOBAL
;
1096 j
= (c
== '?') ? a_AMV_VST_QM
: a_AMV_VST_EM
;
1097 goto jspecial_param
;
1101 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1103 goto jspecial_param
;
1105 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1107 goto jspecial_param
;
1109 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1110 j
= a_AMV_VST_NOSIGN
;
1111 goto jspecial_param
;
1117 avcp
->avc_special_cat
= a_AMV_VSC_MULTIPLEX
;
1119 goto jspecial_param
;
1122 /* Normal reverse lookup, walk over the hashtable */
1124 avcp
->avc_special_cat
= a_AMV_VSC_NONE
;
1125 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
1126 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
1128 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
1131 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
1134 avmp
= &a_amv_var_map
[x
];
1135 if(avmp
->avm_hash
== hash
&&
1136 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
1137 avcp
->avc_map
= avmp
;
1138 avcp
->avc_okey
= (enum okeys
)x
;
1142 if(++i
== a_AMV_VAR_REV_PRIME
){
1143 #ifdef a_AMV_VAR_REV_WRAPAROUND
1150 avcp
->avc_map
= NULL
;
1154 return (avcp
!= NULL
);
1156 /* All these are mapped to *--special-param* */
1158 avcp
->avc_name
= name
;
1159 avcp
->avc_special_prop
= (ui16_t
)j
;
1160 avmp
= &a_amv_var_map
[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX
];
1161 avcp
->avc_hash
= avmp
->avm_hash
;
1162 avcp
->avc_map
= avmp
;
1163 avcp
->avc_okey
= ok_v___special_param
;
1168 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
1171 struct a_amv_var_map
const *avmp
;
1172 struct a_amv_var
*avp
;
1176 struct a_amv_var
**avpp
, *lavp
;
1178 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
1180 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
1181 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
1182 /* Relink as head, hope it "sorts on usage" over time.
1183 * The code relies on this behaviour! */
1185 lavp
->av_link
= avp
->av_link
;
1186 avp
->av_link
= *avpp
;
1190 /* If this setting has been established via -S and we still have
1191 * not reached the _STARTED_CONFIG program state, it may have been
1192 * an explicit "clearance" that is to be treated as unset.
1193 * Because that is a special condition that (has been hacked in
1194 * later and) needs to be encapsulated in lower levels, but not
1195 * of interest if _set() or _clear() called us */
1196 switch(avp
->av_flags
& a_AMV_VF_EXT__FROZEN_MASK
){
1197 case a_AMV_VF_EXT_FROZEN
| a_AMV_VF_EXT_FROZEN_UNSET
:
1199 avcp
->avc_var
= avp
;
1211 /* If this is not an assembled variable we need to consider some special
1212 * initialization cases and eventually create the variable anew */
1213 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1214 /* Does it have an import-from-environment flag? */
1215 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
1216 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
1217 /* May be better not to use that one, though? */
1218 /* TODO Outsource the tests into a _shared_ test function! */
1219 bool_t isempty
, isbltin
;
1221 isempty
= (*cp
== '\0' &&
1222 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
1223 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
1226 if(n_UNLIKELY(isempty
)){
1227 n_err(_("Environment variable must not be empty: %s\n"),
1231 }else if(n_LIKELY(*cp
!= '\0')){
1232 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
1233 !a_amv_var_check_nocntrls(cp
))){
1234 n_err(_("Ignoring environment, control characters "
1235 "invalid in variable: %s\n"), avcp
->avc_name
);
1238 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1239 !a_amv_var_check_num(cp
, FAL0
))){
1240 n_err(_("Environment variable value not a number "
1241 "or out of range: %s\n"), avcp
->avc_name
);
1244 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1245 !a_amv_var_check_num(cp
, TRU1
))){
1246 n_err(_("Environment variable value not a number, "
1247 "negative or out of range: %s\n"), avcp
->avc_name
);
1256 /* A first-time init switch is to be handled now and here */
1257 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1258 static struct a_amv_var_defval
const **arr
,
1259 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1263 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1265 arr
[i
] = &a_amv_var_i3vals
[i
];
1268 for(i
= 0; arr
[i
] != NULL
; ++i
)
1269 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1270 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_1
1271 : arr
[i
]->avdv_value
;
1272 /* Remove this entry, hope entire block becomes no-op asap */
1274 arr
[i
] = arr
[i
+ 1];
1275 while(arr
[i
++] != NULL
);
1279 if(i3val_nonew
== TRUM1
)
1280 avp
= (struct a_amv_var
*)-1;
1287 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1288 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1289 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1290 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_1
1291 : a_amv_var_defvals
[i
].avdv_value
;
1296 /* The virtual variables */
1297 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1298 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1299 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1300 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1310 avcp
->avc_var
= avp
;
1313 return (avp
!= NULL
);
1316 /* E.g., $TMPDIR may be set to non-existent, so we need to be able to catch
1317 * that and redirect to a possible default value */
1318 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1319 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, cp
)){
1321 if(avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
))
1322 unsetenv(avcp
->avc_name
);
1324 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0)
1328 struct a_amv_var
**avpp
;
1331 l
= strlen(avcp
->avc_name
) +1;
1333 avp
= n_calloc(1, n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1334 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1336 avp
->av_flags
= avmp
->avm_flags
;
1337 avp
->av_value
= a_amv_var_copy(cp
);
1338 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1340 if(avp
->av_flags
& a_AMV_VF_ENV
)
1341 a_amv_var__putenv(avcp
, avp
);
1342 if(avmp
->avm_flags
& a_AMV_VF_VIP
)
1343 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, cp
);
1349 a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
){
1353 struct a_amv_var_map
const *avmp
;
1356 /* Not function local, TODO but lazy evaluted for now */
1357 if(avcp
->avc_special_prop
== a_AMV_VST_QM
){
1358 avmp
= &a_amv_var_map
[a_AMV_VAR__QM_MAP_IDX
];
1359 avcp
->avc_okey
= ok_v___qm
;
1360 ep
= &n_pstate_ex_no
;
1362 avmp
= &a_amv_var_map
[a_AMV_VAR__EM_MAP_IDX
];
1363 avcp
->avc_okey
= ok_v___em
;
1364 ep
= &n_pstate_err_no
;
1367 /* XXX Oh heaven, we are responsible to ensure that $?/! is up-to-date
1368 * TODO we could num=1 ok_v___[qe]m, but the thing is still a string
1369 * TODO and thus conversion takes places over and over again; also
1370 * TODO for now that would have to occur before we set _that_ value
1371 * TODO so let's special treat it until we store ints as such */
1373 case 0: rv
= n_0
; break;
1374 case 1: rv
= n_1
; break;
1376 snprintf(itoabuf
, sizeof itoabuf
, "%d", *ep
);
1380 n_PS_ROOT_BLOCK(n_var_okset(avcp
->avc_okey
, (uintptr_t)rv
));
1382 avcp
->avc_hash
= avmp
->avm_hash
;
1383 avcp
->avc_map
= avmp
;
1384 rv
= a_amv_var_lookup(avcp
, FAL0
) ? avcp
->avc_var
->av_value
: NULL
;
1390 a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
){
1397 i
= strlen(rv
= &avcp
->avc_name
[1]);
1399 /* ERR, ERRDOC, ERRNAME, plus *-NAME variants */
1400 if(rv
[0] == 'E' && i
>= 3 && rv
[1] == 'R' && rv
[2] == 'R'){
1402 e
= n_pstate_err_no
;
1404 }else if(rv
[3] == '-'){
1405 e
= n_err_from_name(&rv
[4]);
1408 case 0: rv
= n_0
; break;
1409 case 1: rv
= n_1
; break;
1411 snprintf(itoabuf
, sizeof itoabuf
, "%d", e
);
1412 rv
= savestr(itoabuf
); /* XXX yet, cannot do numbers */
1417 if(!memcmp(&rv
[3], "DOC", 3)){
1420 case '\0': e
= n_pstate_err_no
; break;
1421 case '-': e
= n_err_from_name(&rv
[1]); break;
1424 rv
= n_err_to_doc(e
);
1426 }else if(i
>= 7 && !memcmp(&rv
[3], "NAME", 4)){
1429 case '\0': e
= n_pstate_err_no
; break;
1430 case '-': e
= n_err_from_name(&rv
[1]); break;
1433 rv
= n_err_to_name(e
);
1447 a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
){
1450 char const *rv
, **argv
;
1455 /* If in a macro/xy.. */
1456 if(a_amv_lopts
!= NULL
){
1458 struct a_amv_mac_call_args
*amcap
;
1460 amcap
= a_amv_lopts
->as_amcap
;
1461 argc
= amcap
->amca_pospar
.app_count
;
1462 argv
= amcap
->amca_pospar
.app_dat
;
1463 argv
+= amcap
->amca_pospar
.app_idx
;
1465 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
1466 if(!(ismacky
= (amcap
->amca_amp
== a_AMV_MACKY_MACK
)) &&
1467 (amcap
->amca_ps_hook_mask
||
1468 (assert(amcap
->amca_amp
!= NULL
),
1469 (amcap
->amca_amp
->am_flags
& a_AMV_MF_TYPE_MASK
1470 ) == a_AMV_MF_ACCOUNT
)))
1473 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1474 if(avcp
->avc_special_prop
> 0){
1475 if(argc
>= avcp
->avc_special_prop
)
1476 rv
= argv
[avcp
->avc_special_prop
- 1];
1478 rv
= amcap
->amca_name
;
1480 rv
= (a_amv_lopts
->as_up
!= NULL
1481 ? a_amv_lopts
->as_up
->as_amcap
->amca_name
: n_empty
);
1484 /* MACKY_MACK doesn't know about [*@#] */
1485 /*else*/ if(ismacky
){
1486 if(n_poption
& n_PO_D_V
)
1487 n_err(_("Cannot use $*/$@/$# in this context: %s\n"),
1488 n_shexp_quote_cp(avcp
->avc_name
, FAL0
));
1492 argc
= a_amv_pospar
.app_count
;
1493 argv
= a_amv_pospar
.app_dat
;
1494 argv
+= a_amv_pospar
.app_idx
;
1496 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1497 if(avcp
->avc_special_prop
> 0){
1498 if(argc
>= avcp
->avc_special_prop
)
1499 rv
= argv
[avcp
->avc_special_prop
- 1];
1506 switch(avcp
->avc_special_prop
){ /* XXX OPTIMIZE */
1507 case a_AMV_VST_STAR
:
1509 for(i
= j
= 0; i
< argc
; ++i
)
1510 j
+= strlen(argv
[i
]) + 1;
1516 rv
= cp
= n_autorec_alloc(j
);
1517 for(i
= j
= 0; i
< argc
; ++i
){
1518 j
= strlen(argv
[i
]);
1519 memcpy(cp
, argv
[i
], j
);
1526 case a_AMV_VST_NOSIGN
:{
1529 rv
= cp
= n_autorec_alloc(sizeof("65535"));
1530 snprintf(cp
, sizeof("65535"), "%hu", argc
);
1542 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1544 struct a_amv_var
*avp
;
1546 struct a_amv_var_map
const *avmp
;
1551 rv
= a_amv_var_clear(avcp
, force_env
);
1555 if((avmp
= avcp
->avc_map
) != NULL
){
1558 /* Validity checks */
1559 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1560 !(n_pstate
& n_PS_ROOT
))){
1561 value
= N_("Variable is read-only: %s\n");
1564 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1565 value
= N_("Variable must not be empty: %s\n");
1568 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1569 !a_amv_var_check_nocntrls(value
))){
1570 value
= N_("Variable forbids control characters: %s\n");
1573 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1574 !a_amv_var_check_num(value
, FAL0
))){
1575 value
= N_("Variable value not a number or out of range: %s\n");
1578 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1579 !a_amv_var_check_num(value
, TRU1
))){
1580 value
= _("Variable value not a number, negative, "
1581 "or out of range: %s\n");
1584 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1585 !(n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_ROOT
))){
1586 value
= N_("Variable cannot be set in a resource file: %s\n");
1590 /* Any more complicated inter-dependency? */
1591 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1592 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, value
))){
1593 value
= N_("Assignment of variable aborted: %s\n");
1595 n_err(V_(value
), avcp
->avc_name
);
1599 /* Transformations */
1600 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1603 oval
= savestr(value
);
1605 for(; (c
= *oval
) != '\0'; ++oval
)
1606 *oval
= lowerconv(c
);
1611 a_amv_var_lookup(avcp
, TRU1
);
1612 avp
= avcp
->avc_var
;
1614 /* If this setting had been established via -S and we still have not reached
1615 * the _STARTED_CONFIG program state, silently ignore request! */
1616 if(n_UNLIKELY(avp
!= NULL
) &&
1617 n_UNLIKELY((avp
->av_flags
& a_AMV_VF_EXT__FROZEN_MASK
) != 0)){
1618 if(!(n_psonce
& n_PSO_STARTED_CONFIG
)){
1619 if((n_pstate
& n_PS_ROOT
) ||
1620 (!(n_psonce
& n_PSO_STARTED_GETOPT
) &&
1621 (n_poption
& n_PO_S_FLAG_TEMPORARY
)))
1623 if(n_poption
& n_PO_D_VV
)
1624 n_err(_("Temporarily frozen by -S, not `set'ing: %s\n"),
1629 /* Otherwise, if -S freezing was an `unset' request, be very simple and
1630 * avoid tampering with that very special case we are not really prepared
1631 * for just one more line of code: throw the old thing away! */
1632 if(!(avp
->av_flags
& a_AMV_VF_EXT_FROZEN_UNSET
))
1633 avp
->av_flags
&= ~a_AMV_VF_EXT__FROZEN_MASK
;
1635 assert(avp
->av_value
== n_empty
);
1636 a_amv_vars
[avcp
->avc_prime
] = avp
->av_link
;
1638 avcp
->avc_var
= avp
= NULL
;
1642 if(n_UNLIKELY(a_amv_lopts
!= NULL
) &&
1643 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1644 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1648 oval
= avp
->av_value
;
1650 struct a_amv_var
**avpp
;
1653 l
= strlen(avcp
->avc_name
) +1;
1654 avcp
->avc_var
= avp
= n_calloc(1,
1655 n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1656 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1658 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1659 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1660 oval
= n_UNCONST(n_empty
);
1664 avp
->av_value
= a_amv_var_copy(value
);
1666 /* Via `set' etc. the user may give even boolean options non-boolean
1667 * values, ignore that and force boolean */
1668 if(!(avp
->av_flags
& a_AMV_VF_BOOL
))
1669 avp
->av_value
= a_amv_var_copy(value
);
1671 if(!(n_pstate
& n_PS_ROOT
) && (n_poption
& n_PO_D_VV
) &&
1673 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1674 avcp
->avc_name
, value
);
1675 avp
->av_value
= n_UNCONST(n_1
);
1679 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1680 avp
->av_flags
|= a_AMV_VF_EXT_LINKED
;
1681 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_EXT_LINKED
))
1682 rv
= a_amv_var__putenv(avcp
, avp
);
1683 if(avp
->av_flags
& a_AMV_VF_VIP
)
1684 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, value
);
1686 avp
->av_flags
&= ~a_AMV_VF_EXT__FROZEN_MASK
;
1687 if(!(n_psonce
& n_PSO_STARTED_GETOPT
) &&
1688 (n_poption
& n_PO_S_FLAG_TEMPORARY
) != 0)
1689 avp
->av_flags
|= a_AMV_VF_EXT_FROZEN
;
1691 a_amv_var_free(oval
);
1698 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1706 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1708 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1710 if((rv
= (putenv(cp
) == 0))){
1726 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1727 struct a_amv_var
**avpp
, *avp
;
1728 struct a_amv_var_map
const *avmp
;
1734 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1735 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1736 !(n_pstate
& n_PS_ROOT
))){
1737 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1740 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1741 !a_amv_var_check_vips(a_AMV_VIP_CLEAR
, avcp
->avc_okey
, NULL
))){
1742 n_err(_("Clearance of variable aborted: %s\n"), avcp
->avc_name
);
1749 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1750 assert(avcp
->avc_var
== NULL
);
1751 /* This may be a clearance request from the command line, via -S, and we
1752 * need to keep track of that! Unfortunately we are not prepared for
1753 * this, really, so we need to create a fake entry that is known and
1754 * handled correctly by the lowermost variable layer! */
1755 if(n_UNLIKELY(!(n_psonce
& n_PSO_STARTED_GETOPT
)) &&
1756 (n_poption
& n_PO_S_FLAG_TEMPORARY
)) Jfreeze
:{
1759 l
= strlen(avcp
->avc_name
) +1;
1760 avp
= n_calloc(1, n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1761 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1763 avp
->av_value
= n_UNCONST(n_empty
); /* Sth. covered by _var_free()! */
1764 avp
->av_flags
= (avmp
!= NULL
? avmp
->avm_flags
: 0) |
1765 a_AMV_VF_EXT_FROZEN
| a_AMV_VF_EXT_FROZEN_UNSET
;
1766 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1768 if(force_env
|| (avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_ENV
)))
1770 }else if(force_env
){
1772 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1773 }else if(!(n_pstate
& (n_PS_ROOT
| n_PS_ROBOT
)) && (n_poption
& n_PO_D_V
))
1774 n_err(_("Cannot unset undefined variable: %s\n"), avcp
->avc_name
);
1776 }else if((avp
= avcp
->avc_var
) == (struct a_amv_var
*)-1){
1777 /* Clearance request from command line, via -S? As above.. */
1778 if(n_UNLIKELY(!(n_psonce
& n_PSO_STARTED_GETOPT
) &&
1779 (n_poption
& n_PO_S_FLAG_TEMPORARY
) != 0))
1781 avcp
->avc_var
= NULL
;
1787 /* If this setting has been established via -S and we still have not reached
1788 * the _STARTED_CONFIG program state, silently ignore request!
1789 * XXX All this is very complicated for the tenth of a second */
1791 n_UNLIKELY((avp
->av_flags
& a_AMV_VF_EXT__FROZEN_MASK
) != 0)){
1792 if(!(n_psonce
& n_PSO_STARTED_CONFIG
)){
1793 if((n_pstate
& n_PS_ROOT
) ||
1794 (!(n_psonce
& n_PSO_STARTED_GETOPT
) &&
1795 (n_poption
& n_PO_S_FLAG_TEMPORARY
))){
1796 /* Be aware this may turn a set into an unset! */
1797 if(!(avp
->av_flags
& a_AMV_VF_EXT_FROZEN_UNSET
)){
1798 if(avp
->av_flags
& a_AMV_VF_DEFVAL
)
1800 a_amv_var_free(avp
->av_value
);
1801 avp
->av_flags
|= a_AMV_VF_EXT_FROZEN_UNSET
;
1802 avp
->av_value
= n_UNCONST(n_empty
); /* _var_free() covered */
1803 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_EXT_LINKED
))
1808 if(n_poption
& n_PO_D_VV
)
1809 n_err(_("Temporarily frozen by -S, not `unset'ting: %s\n"),
1813 avp
->av_flags
&= ~a_AMV_VF_EXT__FROZEN_MASK
;
1816 if(n_UNLIKELY(a_amv_lopts
!= NULL
) &&
1817 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1818 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1821 avp
= avcp
->avc_var
;
1822 avcp
->avc_var
= NULL
;
1823 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1824 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1825 *avpp
= (*avpp
)->av_link
;
1827 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_EXT_LINKED
))
1828 rv
= a_amv_var__clearenv(avp
->av_name
, avp
);
1829 a_amv_var_free(avp
->av_value
);
1832 /* XXX Fun part, extremely simple-minded for now: if this variable has
1833 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1834 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0)){
1835 a_amv_var_lookup(avcp
, TRU1
);
1836 if(n_UNLIKELY(!(n_psonce
& n_PSO_STARTED_GETOPT
)) &&
1837 (n_poption
& n_PO_S_FLAG_TEMPORARY
))
1838 avp
->av_flags
|= a_AMV_VF_EXT_FROZEN
;
1846 a_amv_var__clearenv(char const *name
, struct a_amv_var
*avp
){
1848 extern char **environ
;
1860 /* TODO a_amv_var__clearenv without HAVE_SETENV does not handle yet
1861 * TODO existing (non-linked) environment variables! */
1863 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1864 if(*ecpp
== avp
->av_env
){
1867 while(*ecpp
++ != NULL
);
1868 n_free(avp
->av_env
);
1873 #endif /* HAVE_SETENV */
1879 a_amv_var_show_all(void){
1880 struct n_string msg
, *msgp
;
1883 struct a_amv_var
*avp
;
1884 char const **vacp
, **cap
;
1887 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1888 n_perr(_("`set' list: cannot create temporary file"), 0);
1892 /* We need to instantiate first-time-inits and default values here, so that
1893 * they will be regular members of our _vars[] table */
1894 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1895 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1896 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1897 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1899 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1900 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1902 no
+= a_AMV_VAR_VIRTS_CNT
;
1904 vacp
= n_autorec_alloc(no
* sizeof(*vacp
));
1906 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1907 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1908 *cap
++ = avp
->av_name
;
1909 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1910 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1913 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1916 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1917 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1918 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1921 page_or_print(fp
, i
);
1928 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1932 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1938 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1939 struct a_amv_var_carrier avc
;
1941 struct a_amv_var
*avp
;
1946 msgp
= n_string_trunc(msgp
, 0);
1949 a_amv_var_revlookup(&avc
, name
);
1950 isset
= a_amv_var_lookup(&avc
, FAL0
);
1953 if(n_poption
& n_PO_D_V
){
1954 if(avc
.avc_map
== NULL
){
1955 msgp
= n_string_push_cp(msgp
, "#assembled variable with value");
1962 {a_AMV_VF_VIRT
, "virtual"},
1963 {a_AMV_VF_CHAIN
, "variable chain"},
1964 {a_AMV_VF_RDONLY
, "read-only"},
1965 {a_AMV_VF_NODEL
, "nodelete"},
1966 {a_AMV_VF_I3VAL
, "initial-value"},
1967 {a_AMV_VF_DEFVAL
, "default-value"},
1968 {a_AMV_VF_IMPORT
, "import-environ-first\0"}, /* assert NUL in max */
1969 {a_AMV_VF_ENV
, "sync-environ"},
1970 {a_AMV_VF_NOLOPTS
, "no-localopts"},
1971 {a_AMV_VF_NOTEMPTY
, "notempty"},
1972 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1973 {a_AMV_VF_NUM
, "number"},
1974 {a_AMV_VF_POSNUM
, "positive-number"},
1977 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1978 if(isset
? (avp
->av_flags
& tp
->flag
)
1979 : (avc
.avc_map
->avm_flags
& tp
->flag
)){
1980 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1981 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1985 if(avp
!= NULL
&& (avp
->av_flags
& a_AMV_VF_EXT_FROZEN
)){
1986 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1987 msgp
= n_string_push_cp(msgp
, "(un)?set via -S");
1991 msgp
= n_string_push_cp(msgp
, "\n ");
1994 /* (Read-only variables are generally shown via comments..) */
1995 if(!isset
|| (avp
->av_flags
& a_AMV_VF_RDONLY
)){
1996 msgp
= n_string_push_c(msgp
, n_ns
[0]);
1998 if(avc
.avc_map
!= NULL
&& (avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
))
1999 msgp
= n_string_push_cp(msgp
, "boolean; ");
2000 msgp
= n_string_push_cp(msgp
, "variable not set: ");
2001 msgp
= n_string_push_cp(msgp
, n_shexp_quote_cp(name
, FAL0
));
2006 n_UNINIT(quote
, NULL
);
2007 if(!(avp
->av_flags
& a_AMV_VF_BOOL
)){
2008 quote
= n_shexp_quote_cp(avp
->av_value
, TRU1
);
2009 if(strcmp(quote
, avp
->av_value
))
2010 msgp
= n_string_push_cp(msgp
, "wysh ");
2011 }else if(n_poption
& n_PO_D_V
)
2012 msgp
= n_string_push_cp(msgp
, "wysh "); /* (for shell-style comment) */
2013 if(avp
->av_flags
& a_AMV_VF_EXT_LINKED
)
2014 msgp
= n_string_push_cp(msgp
, "environ ");
2015 msgp
= n_string_push_cp(msgp
, "set ");
2016 msgp
= n_string_push_cp(msgp
, name
);
2017 if(!(avp
->av_flags
& a_AMV_VF_BOOL
)){
2018 msgp
= n_string_push_c(msgp
, '=');
2019 msgp
= n_string_push_cp(msgp
, quote
);
2020 }else if(n_poption
& n_PO_D_V
)
2021 msgp
= n_string_push_cp(msgp
, " #boolean");
2024 msgp
= n_string_push_c(msgp
, '\n');
2025 fputs(n_string_cp(msgp
), fp
);
2027 return (i
> 0 ? 2 : 1);
2031 a_amv_var_c_set(char **ap
, bool_t issetenv
){
2032 char *cp
, *cp2
, *varbuf
, c
;
2038 while((cp
= *ap
++) != NULL
){
2040 cp2
= varbuf
= n_autorec_alloc(strlen(cp
) +1);
2042 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
2043 if(cntrlchar(c
) || spacechar(c
)){
2044 n_err(_("Variable name with control or space character ignored: "
2053 cp
= n_UNCONST(n_empty
);
2058 n_err(_("Empty variable name ignored\n"));
2061 struct a_amv_var_carrier avc
;
2064 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
2065 varbuf
= &varbuf
[2];
2067 a_amv_var_revlookup(&avc
, varbuf
);
2070 errs
+= !a_amv_var_clear(&avc
, issetenv
);
2072 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
2087 if((args
= v
)[0] == NULL
){
2088 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
2092 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
2094 n_err(_("Synopsis: define: <name> {\n"));
2098 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
2105 c_undefine(void *v
){
2113 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
2114 while(*++args
!= NULL
);
2124 rv
= a_amv_mac_call(vp
, FAL0
);
2130 c_call_if(void *vp
){
2134 rv
= a_amv_mac_call(vp
, TRU1
);
2141 struct a_amv_mac_call_args
*amcap
;
2142 struct a_amv_mac
*amp
;
2144 int rv
, i
, oqf
, nqf
;
2149 if((args
= v
)[0] == NULL
){
2150 rv
= (a_amv_mac_show(a_AMV_MF_ACCOUNT
) == FAL0
);
2154 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
2155 if(args
[2] != NULL
){
2156 n_err(_("Synopsis: account: <name> {\n"));
2159 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
2160 n_err(_("`account': cannot use reserved name: %s\n"),
2164 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACCOUNT
) == FAL0
);
2168 if(n_pstate
& n_PS_HOOK_MASK
){
2169 n_err(_("`account': cannot change account from within a hook\n"));
2173 save_mbox_for_possible_quitstuff();
2176 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
2177 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACCOUNT
)) == NULL
){
2178 n_err(_("`account': account does not exist: %s\n"), args
[0]);
2182 oqf
= savequitflags();
2184 /* Shutdown the active account */
2185 if(a_amv_acc_curr
!= NULL
){
2189 /* Is there a cleanup hook? */
2190 var
= savecat("on-account-cleanup-", a_amv_acc_curr
->am_name
);
2191 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
||
2192 (cp
= ok_vlook(on_account_cleanup
)) != NULL
){
2193 struct a_amv_mac
*amphook
;
2195 if((amphook
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) != NULL
){
2196 amcap
= n_lofi_alloc(sizeof *amcap
);
2197 memset(amcap
, 0, sizeof *amcap
);
2198 amcap
->amca_name
= cp
;
2199 amcap
->amca_amp
= amphook
;
2200 amcap
->amca_unroller
= &a_amv_acc_curr
->am_lopts
;
2201 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2202 amcap
->amca_no_xcall
= TRU1
;
2203 n_pstate
|= n_PS_HOOK
;
2204 rv
= a_amv_mac_exec(amcap
);
2205 n_pstate
&= ~n_PS_HOOK_MASK
;
2207 n_err(_("*on-account-leave* hook %s does not exist\n"),
2208 n_shexp_quote_cp(cp
, FAL0
));
2212 if(a_amv_acc_curr
->am_lopts
!= NULL
)
2213 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
2215 /* For accounts this lingers */
2216 --a_amv_acc_curr
->am_refcnt
;
2217 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DELETE
)
2218 a_amv_mac_free(a_amv_acc_curr
);
2221 a_amv_acc_curr
= amp
;
2223 /* And switch to any non-"null" account */
2225 assert(amp
->am_lopts
== NULL
);
2226 amcap
= n_lofi_alloc(sizeof *amcap
);
2227 memset(amcap
, 0, sizeof *amcap
);
2228 amcap
->amca_name
= amp
->am_name
;
2229 amcap
->amca_amp
= amp
;
2230 amcap
->amca_unroller
= &
->am_lopts
;
2231 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2232 amcap
->amca_no_xcall
= TRU1
;
2233 ++amp
->am_refcnt
; /* We may not run 0 to avoid being deleted! */
2234 if(!a_amv_mac_exec(amcap
)){
2235 /* XXX account switch incomplete, unroll? */
2236 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
2241 n_PS_ROOT_BLOCK((amp
!= NULL
? ok_vset(account
, amp
->am_name
)
2242 : ok_vclear(account
)));
2244 if(n_psonce
& n_PSO_STARTED
){
2245 assert(!(n_pstate
& n_PS_HOOK_MASK
));
2246 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
2247 restorequitflags(oqf
);
2248 if((i
= setfile("%", 0)) < 0)
2250 temporary_folder_hook_check(FAL0
);
2251 if(i
> 0 && !ok_blook(emptystart
))
2253 n_folder_announce(n_ANNOUNCE_CHANGE
);
2254 restorequitflags(nqf
);
2263 c_unaccount(void *v
){
2271 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACCOUNT
);
2272 while(*++args
!= NULL
);
2278 c_localopts(void *vp
){
2279 enum a_amv_loflags alf
, alm
;
2286 if(a_amv_lopts
== NULL
){
2287 n_err(_("Cannot use `localopts' in this context "
2288 "(not in `define' or `account', nor special hook)\n"));
2292 if((argv
= vp
)[1] == NULL
|| is_asccaseprefix((++argv
)[-1], "scope"))
2293 alf
= alm
= a_AMV_LF_SCOPE
;
2294 else if(is_asccaseprefix(argv
[-1], "call"))
2295 alf
= a_AMV_LF_CALL
, alm
= a_AMV_LF_CALL_MASK
;
2296 else if(is_asccaseprefix(argv
[-1], "call-fixate"))
2297 alf
= a_AMV_LF_CALL_FIXATE
, alm
= a_AMV_LF_CALL_MASK
;
2299 n_err(_("Synopsis: localopts: [<scope|call|call-fixate>] <boolean>\n"));
2303 if(alf
== a_AMV_LF_SCOPE
&&
2304 (a_amv_lopts
->as_loflags
& a_AMV_LF_SCOPE_FIXATE
)){
2305 if(n_poption
& n_PO_D_V
)
2306 n_err(_("Cannot turn off `localopts', setting is fixated\n"));
2310 a_amv_lopts
->as_loflags
&= ~alm
;
2311 if(boolify(*argv
, UIZ_MAX
, FAL0
) > 0)
2312 a_amv_lopts
->as_loflags
|= alf
;
2321 struct a_amv_pospar
*appp
;
2328 if((vp
= *(char**)vp
) == NULL
)
2333 if((n_idec_si16_cp(&sib
, vp
, 10, NULL
2334 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2335 ) != n_IDEC_STATE_CONSUMED
|| sib
< 0){
2336 n_err(_("`shift': invalid argument: %s\n"), vp
);
2342 /* If in in a macro/xy */
2343 if(a_amv_lopts
!= NULL
){
2344 struct a_amv_mac
const *amp
;
2345 struct a_amv_mac_call_args
*amcap
;
2347 /* Explicitly do allow `vpospar' created things! */
2348 amp
= (amcap
= a_amv_lopts
->as_amcap
)->amca_amp
;
2349 if((amp
== NULL
|| amcap
->amca_ps_hook_mask
||
2350 (amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == a_AMV_MF_ACCOUNT
) &&
2351 amcap
->amca_pospar
.app_not_heap
){
2352 n_err(_("Cannot use `shift' in `account's or hook macros etc.\n"));
2355 appp
= &amcap
->amca_pospar
;
2357 appp
= &a_amv_pospar
;
2359 if(i
> appp
->app_count
){
2360 n_err(_("`shift': cannot shift %hu of %hu parameters\n"),
2361 i
, appp
->app_count
);
2365 appp
->app_count
-= i
;
2374 c_return(void *vp
){ /* TODO the exit status should be m_si64! */
2378 if(a_amv_lopts
!= NULL
){
2381 n_go_input_force_eof();
2382 n_pstate_err_no
= n_ERR_NONE
;
2385 if((argv
= vp
)[0] != NULL
){
2388 if((n_idec_si32_cp(&i
, argv
[0], 10, NULL
2389 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2390 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2393 n_err(_("`return': return value argument is invalid: %s\n"),
2395 n_pstate_err_no
= n_ERR_INVAL
;
2399 if(argv
[1] != NULL
){
2400 if((n_idec_si32_cp(&i
, argv
[1], 10, NULL
2401 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2402 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2403 n_pstate_err_no
= i
;
2405 n_err(_("`return': error number argument is invalid: %s\n"),
2407 n_pstate_err_no
= n_ERR_INVAL
;
2413 n_err(_("Can only use `return' in a macro\n"));
2414 n_pstate_err_no
= n_ERR_OPNOTSUPP
;
2422 temporary_folder_hook_check(bool_t nmail
){ /* TODO temporary, v15: drop */
2423 struct a_amv_mac_call_args
*amcap
;
2424 struct a_amv_mac
*amp
;
2432 var
= n_autorec_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
2434 /* First try the fully resolved path */
2435 snprintf(var
, len
, "folder-hook-%s", mailname
);
2436 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2439 /* If we are under *folder*, try the usual +NAME syntax, too */
2440 if(displayname
[0] == '+'){
2443 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
2445 snprintf(var
, len
, "folder-hook-+%s", x
);
2446 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2452 /* Plain *folder-hook* is our last try */
2453 if((cp
= ok_vlook(folder_hook
)) == NULL
)
2457 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
2458 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
2459 n_shexp_quote_cp(displayname
, FAL0
), cp
);
2464 amcap
= n_lofi_alloc(sizeof *amcap
);
2465 memset(amcap
, 0, sizeof *amcap
);
2466 amcap
->amca_name
= cp
;
2467 amcap
->amca_amp
= amp
;
2468 n_pstate
&= ~n_PS_HOOK_MASK
;
2470 amcap
->amca_unroller
= NULL
;
2471 n_pstate
|= n_PS_HOOK_NEWMAIL
;
2473 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
2474 n_pstate
|= n_PS_HOOK
;
2476 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2477 amcap
->amca_ps_hook_mask
= TRU1
;
2478 amcap
->amca_no_xcall
= TRU1
;
2479 rv
= a_amv_mac_exec(amcap
);
2480 n_pstate
&= ~n_PS_HOOK_MASK
;
2488 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2490 if(a_amv_folder_hook_lopts
!= NULL
){
2491 void *save
= a_amv_lopts
;
2494 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
2495 assert(a_amv_folder_hook_lopts
== NULL
);
2502 temporary_compose_mode_hook_call(char const *macname
,
2503 void (*hook_pre
)(void *), void *hook_arg
){
2504 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_GO_SPLICE
2505 * TODO comment in go.c for the right way of doing things! */
2506 static struct a_amv_lostack
*cmh_losp
;
2507 struct a_amv_mac_call_args
*amcap
;
2508 struct a_amv_mac
*amp
;
2513 if(macname
== (char*)-1){
2514 a_amv_mac__finalize(cmh_losp
);
2516 }else if(macname
!= NULL
&&
2517 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
2518 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
2521 amcap
= n_lofi_alloc(sizeof *amcap
);
2522 memset(amcap
, 0, sizeof *amcap
);
2523 amcap
->amca_name
= (macname
!= NULL
) ? macname
2524 : "*on-compose-splice(-shell)?*";
2525 amcap
->amca_amp
= amp
;
2526 amcap
->amca_unroller
= &a_amv_compose_lopts
;
2527 amcap
->amca_hook_pre
= hook_pre
;
2528 amcap
->amca_hook_arg
= hook_arg
;
2529 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2530 amcap
->amca_ps_hook_mask
= TRU1
;
2531 amcap
->amca_no_xcall
= TRU1
;
2532 n_pstate
&= ~n_PS_HOOK_MASK
;
2533 n_pstate
|= n_PS_HOOK
;
2535 a_amv_mac_exec(amcap
);
2537 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
2538 memset(cmh_losp
, 0, sizeof *cmh_losp
);
2539 cmh_losp
->as_global_saved
= a_amv_lopts
;
2540 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
2541 cmh_losp
->as_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2542 a_amv_lopts
= cmh_losp
;
2549 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2551 if(a_amv_compose_lopts
!= NULL
){
2552 void *save
= a_amv_lopts
;
2555 a_amv_lopts_unroll(&a_amv_compose_lopts
);
2556 assert(a_amv_compose_lopts
== NULL
);
2563 n_var_is_user_writable(char const *name
){
2564 struct a_amv_var_carrier avc
;
2565 struct a_amv_var_map
const *avmp
;
2569 a_amv_var_revlookup(&avc
, name
);
2570 if((avmp
= avc
.avc_map
) == NULL
)
2573 rv
= ((avmp
->avm_flags
& (a_AMV_VF_BOOL
| a_AMV_VF_RDONLY
)) == 0);
2579 n_var_oklook(enum okeys okey
){
2580 struct a_amv_var_carrier avc
;
2582 struct a_amv_var_map
const *avmp
;
2585 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2586 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2587 avc
.avc_hash
= avmp
->avm_hash
;
2588 avc
.avc_okey
= okey
;
2590 if(a_amv_var_lookup(&avc
, FAL0
))
2591 rv
= avc
.avc_var
->av_value
;
2599 n_var_okset(enum okeys okey
, uintptr_t val
){
2600 struct a_amv_var_carrier avc
;
2602 struct a_amv_var_map
const *avmp
;
2605 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2606 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2607 avc
.avc_hash
= avmp
->avm_hash
;
2608 avc
.avc_okey
= okey
;
2610 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2616 n_var_okclear(enum okeys okey
){
2617 struct a_amv_var_carrier avc
;
2619 struct a_amv_var_map
const *avmp
;
2622 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2623 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2624 avc
.avc_hash
= avmp
->avm_hash
;
2625 avc
.avc_okey
= okey
;
2627 rv
= a_amv_var_clear(&avc
, FAL0
);
2633 n_var_vlook(char const *vokey
, bool_t try_getenv
){
2634 struct a_amv_var_carrier avc
;
2638 a_amv_var_revlookup(&avc
, vokey
);
2640 switch((enum a_amv_var_special_category
)avc
.avc_special_cat
){
2641 default: /* silence CC */
2642 case a_AMV_VSC_NONE
:
2643 if(a_amv_var_lookup(&avc
, FAL0
))
2644 rv
= avc
.avc_var
->av_value
;
2645 /* Only check the environment for something that is otherwise unknown */
2646 else if(try_getenv
&& avc
.avc_map
== NULL
)
2651 case a_AMV_VSC_GLOBAL
:
2652 rv
= a_amv_var_vsc_global(&avc
);
2654 case a_AMV_VSC_MULTIPLEX
:
2655 rv
= a_amv_var_vsc_multiplex(&avc
);
2657 case a_AMV_VSC_POSPAR
:
2658 case a_AMV_VSC_POSPAR_ENV
:
2659 rv
= a_amv_var_vsc_pospar(&avc
);
2667 n_var_vexplode(void const **cookie
){
2668 struct a_amv_pospar
*appp
;
2671 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
2673 *cookie
= (appp
->app_count
> 0) ? &appp
->app_dat
[appp
->app_idx
] : NULL
;
2675 return (*cookie
!= NULL
);
2679 n_var_vset(char const *vokey
, uintptr_t val
){
2680 struct a_amv_var_carrier avc
;
2684 a_amv_var_revlookup(&avc
, vokey
);
2686 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2692 n_var_vclear(char const *vokey
){
2693 struct a_amv_var_carrier avc
;
2697 a_amv_var_revlookup(&avc
, vokey
);
2699 ok
= a_amv_var_clear(&avc
, FAL0
);
2706 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
2707 enum okey_xlook_mode oxm
){
2708 struct a_amv_var_carrier avc
;
2709 struct str
const *us
;
2712 struct a_amv_var_map
const *avmp
;
2715 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
2717 /* For simplicity: allow this case too */
2718 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
2723 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2724 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2725 avc
.avc_okey
= okey
;
2727 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
2728 nlen
= strlen(avc
.avc_name
);
2729 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
2730 memcpy(nbuf
, avc
.avc_name
, nlen
);
2733 /* One of .url_u_h_p and .url_h_p we test in here */
2734 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2735 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2736 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2737 if(a_amv_var_lookup(&avc
, FAL0
))
2742 us
= &urlp
->url_h_p
;
2743 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2744 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2745 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2746 if(a_amv_var_lookup(&avc
, FAL0
)){
2748 rv
= avc
.avc_var
->av_value
;
2754 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2761 #endif /* HAVE_SOCKETS */
2769 if(*(ap
= v
) == NULL
){
2770 a_amv_var_show_all();
2773 err
= !a_amv_var_c_set(ap
, FAL0
);
2784 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2785 err
|= !n_var_vclear(*ap
);
2795 if(*(ap
= v
) == NULL
)
2798 struct n_string msg
, *msgp
= &msg
;
2800 msgp
= n_string_creat(msgp
);
2801 for(; *ap
!= NULL
; ++ap
)
2802 a_amv_var_show(*ap
, n_stdout
, msgp
);
2806 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2811 struct a_amv_var_carrier avc
;
2815 sighandler_type sigint
;
2818 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2820 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2821 memset(&avc
, 0, sizeof avc
);
2823 a_amv_var_revlookup(&avc
, *argv
);
2825 if(avc
.avc_map
!= NULL
){
2826 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2827 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2831 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2832 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2838 a_amv_var_lookup(&avc
, FAL0
);
2840 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2842 n_perr(_("`varedit': cannot create temporary file"), 0);
2845 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2846 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2847 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2854 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2863 if(UICMP(64, l
, >=, UIZ_MAX
-42)){
2864 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2869 varres
= val
= n_autorec_alloc(l
+1);
2870 for(; l
> 0 && (c
= getc(nf
)) != EOF
; --l
)
2874 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2880 if(!a_amv_var_set(&avc
, varres
, FAL0
))
2885 n_err(_("`varedit': cannot start $EDITOR\n"));
2891 safe_signal(SIGINT
, sigint
);
2898 struct a_amv_var_carrier avc
;
2904 if((islnk
= is_asccaseprefix(*(ap
= v
), "link")) ||
2905 is_asccaseprefix(*ap
, "unlink")){
2906 for(err
= 0; *++ap
!= NULL
;){
2907 a_amv_var_revlookup(&avc
, *ap
);
2909 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2910 (avc
.avc_var
->av_flags
& a_AMV_VF_EXT_LINKED
))){
2912 avc
.avc_var
->av_flags
&= ~a_AMV_VF_EXT_LINKED
;
2914 }else if(avc
.avc_var
->av_flags
&
2915 (a_AMV_VF_ENV
| a_AMV_VF_EXT_LINKED
)){
2916 if(n_poption
& n_PO_D_V
)
2917 n_err(_("`environ': link: already established: %s\n"), *ap
);
2920 avc
.avc_var
->av_flags
|= a_AMV_VF_EXT_LINKED
;
2921 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2922 a_amv_var__putenv(&avc
, avc
.avc_var
);
2924 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2927 char const *evp
= getenv(*ap
);
2930 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2932 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2938 }else if(is_asccaseprefix(*ap
, "set"))
2939 err
= !a_amv_var_c_set(++ap
, TRU1
);
2940 else if(is_asccaseprefix(*ap
, "unset")){
2941 for(err
= 0; *++ap
!= NULL
;){
2942 a_amv_var_revlookup(&avc
, *ap
);
2944 if(!a_amv_var_clear(&avc
, TRU1
))
2948 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2956 c_vexpr(void *v
){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2958 enum n_idec_state ids
;
2960 char op
, varbuf
[64 + 64 / 8 +1];
2961 char const **argv
, *varname
, *varres
, *cp
;
2964 a_SOFTOVERFLOW
= 1u<<1,
2966 a_ISDECIMAL
= 1u<<3, /* Print only decimal result */
2967 a_SATURATED
= 1u<<4,
2969 a_UNSIGNED
= 1u<<6, /* Unsigned right shift (share bit ok) */
2976 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2977 n_UNINIT(varres
, n_empty
);
2979 if((cp
= argv
[0])[0] == '\0')
2989 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2992 if(*(cp
= *++argv
) == '\0')
2994 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2995 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2996 ) != n_IDEC_STATE_CONSUMED
){
2997 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2999 f
|= a_SOFTOVERFLOW
;
3016 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3021 if(*(cp
= *++argv
) == '\0')
3023 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
3024 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3025 ) != n_IDEC_STATE_CONSUMED
){
3026 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
3028 f
|= a_SOFTOVERFLOW
;
3032 if(*(cp
= *++argv
) == '\0')
3034 else if(((ids
= n_idec_si64_cp(&rhv
, cp
, 0, NULL
)
3035 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3036 ) != n_IDEC_STATE_CONSUMED
){
3037 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
3039 f
|= a_SOFTOVERFLOW
;
3049 if(rhv
!= SI64_MIN
){
3054 goto jenum_plusminus
;
3060 if(SI64_MAX
- rhv
< lhv
)
3061 goto jenum_plusminus
;
3066 if(rhv
!= SI64_MIN
){
3071 goto jenum_plusminus
;
3077 if(SI64_MIN
+ rhv
> lhv
){
3079 if(!(f
& a_SATURATED
))
3080 goto jenum_overflow
;
3081 f
|= a_SOFTOVERFLOW
;
3082 lhv
= (lhv
< 0 || xop
== '-') ? SI64_MIN
: SI64_MAX
;
3087 /* Will the result be positive? */
3088 if((lhv
< 0) == (rhv
< 0)){
3093 if(rhv
!= 0 && lhv
!= 0 && SI64_MAX
/ rhv
> lhv
){
3094 if(!(f
& a_SATURATED
))
3095 goto jenum_overflow
;
3096 f
|= a_SOFTOVERFLOW
;
3102 if(lhv
!= 0 && SI64_MIN
/ lhv
< rhv
){
3103 if(!(f
& a_SATURATED
))
3104 goto jenum_overflow
;
3105 f
|= a_SOFTOVERFLOW
;
3110 if(rhv
!= 0 && lhv
!= 0 && SI64_MIN
/ rhv
< lhv
){
3111 if(!(f
& a_SATURATED
))
3112 goto jenum_overflow
;
3113 f
|= a_SOFTOVERFLOW
;
3122 if(!(f
& a_SATURATED
))
3124 f
|= a_SOFTOVERFLOW
;
3131 if(!(f
& a_SATURATED
))
3133 f
|= a_SOFTOVERFLOW
;
3151 if(rhv
> 63){ /* xxx 63? */
3152 if(!(f
& a_SATURATED
))
3153 goto jenum_overflow
;
3158 else if(f
& a_UNSIGNED
)
3159 lhv
= (ui64_t
)lhv
>> (ui8_t
)rhv
;
3169 }else if(cp
[2] == '\0' && cp
[1] == '@'){
3173 }else if(cp
[0] == '<'){
3185 }else if(cp
[0] == '>'){
3201 }else if(is_asccaseprefix(cp
, "length")){
3202 f
|= a_ISNUM
| a_ISDECIMAL
;
3203 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3206 i
= strlen(*++argv
);
3207 if(UICMP(64, i
, >, SI64_MAX
))
3208 goto jestr_overflow
;
3210 }else if(is_asccaseprefix(cp
, "hash")){
3211 f
|= a_ISNUM
| a_ISDECIMAL
;
3212 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3215 i
= n_torek_hash(*++argv
);
3217 }else if(is_asccaseprefix(cp
, "find")){
3218 f
|= a_ISNUM
| a_ISDECIMAL
;
3219 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3222 if((cp
= strstr(argv
[1], argv
[2])) == NULL
)
3224 i
= PTR2SIZE(cp
- argv
[1]);
3225 if(UICMP(64, i
, >, SI64_MAX
))
3226 goto jestr_overflow
;
3228 }else if(is_asccaseprefix(cp
, "ifind")){
3229 f
|= a_ISNUM
| a_ISDECIMAL
;
3230 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3233 if((cp
= asccasestr(argv
[1], argv
[2])) == NULL
)
3235 i
= PTR2SIZE(cp
- argv
[1]);
3236 if(UICMP(64, i
, >, SI64_MAX
))
3237 goto jestr_overflow
;
3239 }else if(is_asccaseprefix(cp
, "substring")){
3240 if(argv
[1] == NULL
|| argv
[2] == NULL
)
3242 if(argv
[3] != NULL
&& argv
[4] != NULL
)
3245 i
= strlen(varres
= *++argv
);
3247 if(*(cp
= *++argv
) == '\0')
3249 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3250 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3251 ) != n_IDEC_STATE_CONSUMED
)
3252 goto jestr_numrange
;
3254 if(UICMP(64, i
, <, -lhv
))
3255 goto jesubstring_off
;
3258 if(UICMP(64, i
, >=, lhv
)){
3263 if(n_poption
& n_PO_D_V
)
3264 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
3265 n_shexp_quote_cp(argv
[-1], FAL0
));
3266 f
|= a_SOFTOVERFLOW
;
3269 if(argv
[1] != NULL
){
3270 if(*(cp
= *++argv
) == '\0')
3272 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3273 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3274 ) != n_IDEC_STATE_CONSUMED
)
3275 goto jestr_numrange
;
3277 if(UICMP(64, i
, <, -lhv
))
3278 goto jesubstring_len
;
3281 if(UICMP(64, i
, >=, lhv
)){
3282 if(UICMP(64, i
, !=, lhv
))
3283 varres
= savestrbuf(varres
, (size_t)lhv
);
3286 if(n_poption
& n_PO_D_V
)
3287 n_err(_("`vexpr': substring: length argument too large: %s\n"),
3288 n_shexp_quote_cp(argv
[-2], FAL0
));
3289 f
|= a_SOFTOVERFLOW
;
3292 }else if(is_asccaseprefix(cp
, "trim")) Jtrim
: {
3294 enum n_str_trim_flags stf
;
3296 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3299 if(is_asccaseprefix(cp
, "trim-front"))
3300 stf
= n_STR_TRIM_FRONT
;
3301 else if(is_asccaseprefix(cp
, "trim-end"))
3302 stf
= n_STR_TRIM_END
;
3304 stf
= n_STR_TRIM_BOTH
;
3306 trim
.l
= strlen(trim
.s
= n_UNCONST(argv
[1]));
3307 (void)n_str_trim(&trim
, stf
);
3308 varres
= savestrbuf(trim
.s
, trim
.l
);
3309 }else if(is_asccaseprefix(cp
, "trim-front"))
3311 else if(is_asccaseprefix(cp
, "trim-end"))
3313 else if(is_asccaseprefix(cp
, "random")){
3314 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3317 if((n_idec_si64_cp(&lhv
, argv
[1], 0, NULL
3318 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3319 ) != n_IDEC_STATE_CONSUMED
|| lhv
< 0 || lhv
> PATH_MAX
)
3320 goto jestr_numrange
;
3323 varres
= n_random_create_cp((size_t)lhv
, NULL
);
3324 }else if(is_asccaseprefix(cp
, "file-expand")){
3325 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3328 if((varres
= fexpand(argv
[1], FEXP_NVAR
| FEXP_NOPROTO
)) == NULL
)
3330 }else if(is_asccaseprefix(cp
, "makeprint")){
3331 struct str sin
, sout
;
3333 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3336 /* XXX using strlen for `vexpr makeprint' is wrong for UTF-16 */
3337 sin
.l
= strlen(sin
.s
= n_UNCONST(argv
[1]));
3338 makeprint(&sin
, &sout
);
3339 varres
= savestrbuf(sout
.s
, sout
.l
);
3341 /* TODO `vexpr': (wide) string length, find, etc!! */
3343 }else if(is_asccaseprefix(cp
, "regex")) Jregex
:{
3344 regmatch_t rema
[1 + n_VEXPR_REGEX_MAX
];
3348 f
|= a_ISNUM
| a_ISDECIMAL
;
3349 if(argv
[1] == NULL
|| argv
[2] == NULL
||
3350 (argv
[3] != NULL
&& argv
[4] != NULL
))
3353 reflrv
= REG_EXTENDED
;
3355 reflrv
|= REG_ICASE
;
3356 if((reflrv
= regcomp(&re
, argv
[2], reflrv
))){
3357 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
3358 n_shexp_quote_cp(argv
[2], FAL0
), n_regex_err_to_doc(&re
, reflrv
));
3360 n_pstate_err_no
= n_ERR_INVAL
;
3363 reflrv
= regexec(&re
, argv
[1], n_NELEM(rema
), rema
, 0);
3365 if(reflrv
== REG_NOMATCH
)
3368 /* Search only? Else replace, which is a bit */
3369 if(argv
[3] == NULL
){
3370 if(UICMP(64, rema
[0].rm_so
, >, SI64_MAX
))
3371 goto jestr_overflow
;
3372 lhv
= (si64_t
)rema
[0].rm_so
;
3374 /* We need to setup some kind of pseudo macro environment for this */
3375 struct a_amv_lostack los
;
3376 struct a_amv_mac_call_args amca
;
3377 char const **reargv
;
3379 memset(&amca
, 0, sizeof amca
);
3380 amca
.amca_name
= savestrbuf(&argv
[1][rema
[0].rm_so
],
3381 rema
[0].rm_eo
- rema
[0].rm_so
);
3382 amca
.amca_amp
= a_AMV_MACKY_MACK
;
3383 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3385 amca
.amca_pospar
.app_count
= (ui32_t
)i
- 1;
3386 amca
.amca_pospar
.app_not_heap
= TRU1
;
3387 amca
.amca_pospar
.app_dat
=
3388 reargv
= n_autorec_alloc(sizeof(char*) * i
);
3389 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3390 *reargv
++ = savestrbuf(&argv
[1][rema
[i
].rm_so
],
3391 rema
[i
].rm_eo
- rema
[i
].rm_so
);
3394 memset(&los
, 0, sizeof los
);
3395 hold_all_sigs(); /* TODO DISLIKE! */
3396 los
.as_global_saved
= a_amv_lopts
;
3397 los
.as_amcap
= &amca
;
3398 los
.as_up
= los
.as_global_saved
;
3403 struct n_string s_b
;
3404 enum n_shexp_state shs
;
3406 templ
.s
= n_UNCONST(argv
[3]);
3408 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
3409 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
3410 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
),
3411 n_string_creat_auto(&s_b
), &templ
, NULL
);
3412 if((shs
& (n_SHEXP_STATE_ERR_MASK
| n_SHEXP_STATE_STOP
)
3413 ) == n_SHEXP_STATE_STOP
){
3414 varres
= n_string_cp(&s_b
);
3415 n_string_drop_ownership(&s_b
);
3420 a_amv_lopts
= los
.as_global_saved
;
3421 rele_all_sigs(); /* TODO DISLIKE! */
3425 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
3427 }else if(is_asccaseprefix(argv
[0], "iregex")){
3430 #endif /* HAVE_REGEX */
3434 n_pstate_err_no
= (f
& a_SOFTOVERFLOW
) ? n_ERR_OVERFLOW
: n_ERR_NONE
;
3437 /* Generate the variable value content for numerics.
3438 * Anticipate in our handling below! (Don't do needless work) */
3440 if((f
& a_ISNUM
) && ((f
& a_ISDECIMAL
) || varname
!= NULL
)){
3441 snprintf(varbuf
, sizeof varbuf
, "%" PRId64
, lhv
);
3445 if(varname
== NULL
){
3446 /* If there was no error and we are printing a numeric result, print some
3447 * more bases for the fun of it */
3448 if((f
& (a_ERR
| a_ISNUM
| a_ISDECIMAL
)) == a_ISNUM
){
3451 for(j
= 1, i
= 0; i
< 64; ++i
){
3452 varbuf
[63 + 64 / 8 -j
- i
] = (lhv
& ((ui64_t
)1 << i
)) ? '1' : '0';
3453 if((i
& 7) == 7 && i
!= 63){
3455 varbuf
[63 + 64 / 8 -j
- i
] = ' ';
3458 varbuf
[64 + 64 / 8 -1] = '\0';
3459 if(fprintf(n_stdout
,
3460 "%s\n0%" PRIo64
" | 0x%" PRIX64
" | %" PRId64
"\n",
3461 varbuf
, lhv
, lhv
, lhv
) < 0){
3462 n_pstate_err_no
= n_err_no
;
3465 }else if(varres
!= NULL
&& fprintf(n_stdout
, "%s\n", varres
) < 0){
3466 n_pstate_err_no
= n_err_no
;
3469 }else if(!n_var_vset(varname
, (uintptr_t)varres
)){
3470 n_pstate_err_no
= n_ERR_NOTSUP
;
3474 return (f
& a_ERR
) ? 1 : 0;
3477 f
= a_ERR
| a_ISNUM
;
3481 n_err(_("`vexpr': invalid subcommand: %s\n"),
3482 n_shexp_quote_cp(*argv
, FAL0
));
3483 n_pstate_err_no
= n_ERR_INVAL
;
3486 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3487 n_pstate_err_no
= n_ERR_INVAL
;
3491 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3492 n_shexp_quote_cp(*argv
, FAL0
));
3493 n_pstate_err_no
= n_ERR_RANGE
;
3496 n_err(_("`vexpr': expression overflows datatype: %" PRId64
" %c %" PRId64
3497 "\n"), lhv
, op
, rhv
);
3498 n_pstate_err_no
= n_ERR_OVERFLOW
;
3502 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3503 n_shexp_quote_cp(*argv
, FAL0
));
3504 n_pstate_err_no
= n_ERR_RANGE
;
3507 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3508 n_pstate_err_no
= n_ERR_OVERFLOW
;
3511 n_pstate_err_no
= n_ERR_NODATA
;
3522 struct n_cmd_arg
*cap
;
3524 struct a_amv_pospar
*appp
;
3533 struct n_cmd_arg_ctx
*cacp
;
3536 n_pstate_err_no
= n_ERR_NONE
;
3537 n_UNINIT(varres
, n_empty
);
3539 cap
= cacp
->cac_arg
;
3541 if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "set"))
3543 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "clear"))
3545 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "quote"))
3548 n_err(_("`vpospar': invalid subcommand: %s\n"),
3549 n_shexp_quote_cp(cap
->ca_arg
.ca_str
.s
, FAL0
));
3550 n_pstate_err_no
= n_ERR_INVAL
;
3556 if((f
& (a_CLEAR
| a_QUOTE
)) && cap
->ca_next
!= NULL
){
3557 n_err(_("`vpospar': `%s': takes no argument\n"), cap
->ca_arg
.ca_str
.s
);
3558 n_pstate_err_no
= n_ERR_INVAL
;
3565 /* If in a macro, we need to overwrite the local instead of global argv */
3566 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
3569 if(f
& (a_SET
| a_CLEAR
)){
3570 if(cacp
->cac_vput
!= NULL
)
3571 n_err(_("`vpospar': `vput' only supported for `quote' subcommand\n"));
3572 if(!appp
->app_not_heap
&& appp
->app_maxcount
> 0){
3573 for(i
= appp
->app_maxcount
; i
-- != 0;)
3574 n_free(n_UNCONST(appp
->app_dat
[i
]));
3575 n_free(appp
->app_dat
);
3577 memset(appp
, 0, sizeof *appp
);
3580 if((i
= cacp
->cac_no
) > a_AMV_POSPAR_MAX
){
3581 n_err(_("`vpospar': overflow: %" PRIuZ
" arguments!\n"), i
);
3582 n_pstate_err_no
= n_ERR_OVERFLOW
;
3587 memset(appp
, 0, sizeof *appp
);
3589 appp
->app_maxcount
= appp
->app_count
= (ui16_t
)i
;
3590 /* XXX Optimize: store it all in one chunk! */
3592 i
*= sizeof *appp
->app_dat
;
3593 appp
->app_dat
= n_alloc(i
);
3595 for(i
= 0; cap
!= NULL
; ++i
, cap
= cap
->ca_next
){
3596 appp
->app_dat
[i
] = n_alloc(cap
->ca_arg
.ca_str
.l
+1);
3597 memcpy(n_UNCONST(appp
->app_dat
[i
]), cap
->ca_arg
.ca_str
.s
,
3598 cap
->ca_arg
.ca_str
.l
+1);
3601 appp
->app_dat
[i
] = NULL
;
3605 if(appp
->app_count
== 0)
3609 struct n_string s
, *sp
;
3612 sp
= n_string_creat_auto(&s
);
3614 sep1
= *ok_vlook(ifs
);
3615 sep2
= *ok_vlook(ifs_ws
);
3621 for(i
= 0; i
< appp
->app_count
; ++i
){
3623 if(!n_string_can_swallow(sp
, 2))
3625 sp
= n_string_push_c(sp
, sep1
);
3627 sp
= n_string_push_c(sp
, sep2
);
3629 in
.l
= strlen(in
.s
= n_UNCONST(appp
->app_dat
[i
+ appp
->app_idx
]));
3631 if(!n_string_can_swallow(sp
, in
.l
)){
3633 n_err(_("`vpospar': overflow: string too long!\n"));
3634 n_pstate_err_no
= n_ERR_OVERFLOW
;
3638 sp
= n_shexp_quote(sp
, &in
, TRU1
);
3641 varres
= n_string_cp(sp
);
3644 if(cacp
->cac_vput
== NULL
){
3645 if(fprintf(n_stdout
, "%s\n", varres
) < 0){
3646 n_pstate_err_no
= n_err_no
;
3649 }else if(!n_var_vset(cacp
->cac_vput
, (uintptr_t)varres
)){
3650 n_pstate_err_no
= n_ERR_NOTSUP
;
3656 return (f
& a_ERR
) ? 1 : 0;