INSTALL: v14.9.4; and support `citron' a.k.a. "config build install"!
[s-mailx.git] / accmacvar.c
blob4a5323e6b661eacdaf77b6e508899a879f615f48
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
26 * are met:
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
46 * SUCH DAMAGE.
48 #undef n_FILE
49 #define n_FILE accmacvar
51 #ifndef HAVE_AMALGAMATION
52 # include "nail.h"
53 #endif
55 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
56 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
57 #endif
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)
71 enum a_amv_mac_flags{
72 a_AMV_MF_NONE = 0,
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 */
77 a_AMV_MF__MAX = 0xFFu
80 enum a_amv_loflags{
81 a_AMV_LF_NONE = 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... */
93 enum a_amv_var_flags{
94 a_AMV_VF_NONE = 0,
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{
136 /* _VSC_GLOBAL */
137 a_AMV_VST_QM, /* ? */
138 a_AMV_VST_EM, /* ! */
139 /* _VSC_MULTIPLEX */
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{
150 a_AMV_VIP_SET_PRE,
151 a_AMV_VIP_SET_POST,
152 a_AMV_VIP_CLEAR
155 struct a_amv_pospar{
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 */
160 ui8_t app__dummy[1];
161 char const **app_dat; /* NULL terminated (for "$@" explosion support) */
163 n_CTA(a_AMV_POSPAR_MAX <= SI16_MAX, "Limit exceeds datatype capabilities");
165 struct a_amv_mac{
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{
178 ui32_t aml_len;
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 *);
188 void *amca_hook_arg;
189 ui8_t amca_loflags;
190 bool_t amca_ps_hook_mask;
191 bool_t amca_no_xcall; /* We want n_GO_INPUT_NO_XCALL for this */
192 ui8_t amca__pad[5];
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 */
202 ui8_t avs__pad[7];
205 struct a_amv_var{
206 struct a_amv_var *av_link;
207 char *av_value;
208 #ifdef HAVE_PUTENV
209 char *av_env; /* Actively managed putenv(3) memory, or NULL */
210 #endif
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{
217 ui32_t avm_hash;
218 ui16_t avm_keyoff;
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{
224 ui32_t avv_okey;
225 ui8_t avv__dummy[4];
226 struct a_amv_var const *avv_var;
229 struct a_amv_var_defval{
230 ui32_t avdv_okey;
231 ui8_t avdv__pad[4];
232 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
235 struct a_amv_var_carrier{
236 char const *avc_name;
237 ui32_t avc_hash;
238 ui32_t avc_prime;
239 struct a_amv_var *avc_var;
240 struct a_amv_var_map const *avc_map;
241 enum okeys avc_okey;
242 ui8_t avc__pad[1];
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);
299 /* */
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,
330 char const *name);
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,
340 bool_t i3val_nonew);
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,
350 bool_t force_env);
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;
375 ui32_t h;
376 enum a_amv_mac_flags save_amf;
377 NYD2_ENTER;
379 save_amf = 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))
389 goto jleave;
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"),
398 name);
399 }else{
400 a_amv_mac_free(amp);
401 amp = (struct a_amv_mac*)-1;
403 break;
407 if(newamp != NULL){
408 ampp = &a_amv_macs[h];
409 newamp->am_next = *ampp;
410 *ampp = newamp;
411 amp = NULL;
413 jleave:
414 NYD2_LEAVE;
415 return amp;
418 static int
419 a_amv_mac_call(void *v, bool_t silent_nexist){
420 struct a_amv_mac *amp;
421 int rv;
422 char const *name;
423 struct n_cmd_arg_ctx *cacp;
424 NYD_ENTER;
426 cacp = v;
428 if(cacp->cac_no == 0){
429 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
430 n_pstate_err_no = n_ERR_INVAL;
431 rv = 1;
432 goto jleave;
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;
440 rv = 1;
441 }else if(n_UNLIKELY((amp = a_amv_mac_lookup(name, NULL, a_AMV_MF_NONE)
442 ) == NULL)){
443 if(!silent_nexist)
444 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name, FAL0));
445 n_pstate_err_no = n_ERR_NOENT;
446 rv = 1;
447 }else{
448 char const **argv;
449 struct a_amv_mac_call_args *amcap;
450 size_t argc;
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;
458 argv[argc] = NULL;
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;
466 if(argc > 0){
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);
473 rv = n_pstate_ex_no;
475 jleave:
476 NYD_LEAVE;
477 return rv;
480 static bool_t
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;
486 bool_t rv;
487 NYD2_ENTER;
489 amp = amcap->amca_amp;
490 assert(amp != NULL && amp != a_AMV_MACKY_MACK);
491 ++amp->am_refcnt;
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);
496 *args = NULL;
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;
503 }else{
504 losp->as_up = NULL;
505 losp->as_lopts = *amcap->amca_unroller;
507 losp->as_loflags = amcap->amca_loflags;
509 a_amv_lopts = losp;
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);
515 NYD2_LEAVE;
516 return rv;
519 static void
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;
524 NYD2_ENTER;
526 losp = vp;
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){
532 ui16_t i;
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);
542 }else
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))
550 a_amv_mac_free(amp);
552 n_lofi_free(losp);
553 n_lofi_free(amcap);
554 NYD2_LEAVE;
557 static bool_t
558 a_amv_mac_show(enum a_amv_mac_flags amf){
559 size_t lc, mc, ti, i;
560 char const *typestr;
561 FILE *fp;
562 bool_t rv;
563 NYD2_ENTER;
565 rv = FAL0;
567 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
568 NULL){
569 n_perr(_("`define' or `account' list: cannot create temporary file"), 0);
570 goto jleave;
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;
583 if(++mc > 1){
584 putc('\n', fp);
585 ++lc;
587 ++lc;
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)
591 putc(' ', fp);
592 fputs((*amlpp)->aml_dat, fp);
593 putc('\n', fp);
595 fputs("}\n", fp);
596 ++lc;
600 if(mc > 0)
601 page_or_print(fp, lc);
603 rv = (ferror(fp) == 0);
604 Fclose(fp);
605 jleave:
606 NYD2_LEAVE;
607 return rv;
610 static bool_t
611 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
612 struct str line;
613 ui32_t line_cnt, maxlen;
614 struct linelist{
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;
620 bool_t rv;
621 NYD2_ENTER;
623 memset(&line, 0, sizeof line);
624 rv = FAL0;
625 amp = NULL;
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;;){
632 ui32_t leaspc;
633 char *cp;
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);
637 if(n.ui == 0)
638 continue;
639 if(n.i < 0){
640 n_err(_("Unterminated %s definition: %s\n"),
641 (amf & a_AMV_MF_ACCOUNT ? "account" : "macro"), name);
642 goto jerr;
645 /* Trim WS, remember amount of leading spaces for display purposes */
646 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
647 if(*cp == '\t')
648 leaspc = (leaspc + 8u) & ~7u;
649 else if(*cp == ' ')
650 ++leaspc;
651 else
652 break;
653 for(; n.ui > 0 && spacechar(cp[n.ui - 1]); --n.ui)
655 if(n.ui == 0)
656 continue;
658 maxlen = n_MAX(maxlen, n.ui);
659 cp[n.ui++] = '\0';
661 /* Is is the closing brace? */
662 if(*cp == '}')
663 break;
665 if(n_LIKELY(++line_cnt < UI32_MAX)){
666 struct a_amv_mac_line *amlp;
668 llp = n_autorec_alloc(sizeof *llp);
669 if(ll_head == NULL)
670 ll_head = llp;
671 else
672 ll_tail->ll_next = llp;
673 ll_tail = llp;
674 llp->ll_next = NULL;
675 llp->ll_amlp = amlp = n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line,
676 aml_dat) + n.ui);
677 amlp->aml_len = n.ui -1;
678 amlp->aml_prespc = leaspc;
679 memcpy(amlp->aml_dat, cp, n.ui);
680 }else{
681 n_err(_("Too much content in %s definition: %s\n"),
682 (amf & a_AMV_MF_ACCOUNT ? "account" : "macro"), name);
683 goto jerr;
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;
693 amp->am_flags = amf;
694 memcpy(amp->am_name, name, n.s);
695 /* C99 */{
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;
701 *amlpp = NULL;
704 /* Create entry, replace a yet existing one as necessary */
705 a_amv_mac_lookup(name, amp, amf | a_AMV_MF_UNDEF);
706 rv = TRU1;
707 jleave:
708 if(line.s != NULL)
709 n_free(line.s);
710 NYD2_LEAVE;
711 return rv;
713 jerr:
714 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
715 n_free(llp->ll_amlp);
717 * if(amp != NULL){
718 * n_free(amp->am_line_dat);
719 * n_free(amp);
720 *}*/
721 goto jleave;
724 static bool_t
725 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
726 struct a_amv_mac *amp;
727 bool_t rv;
728 NYD2_ENTER;
730 rv = TRU1;
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);
736 rv = FAL0;
738 }else{
739 struct a_amv_mac **ampp, *lamp;
741 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
742 ++ampp)
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;
748 }else{
749 lamp = amp;
750 amp = amp->am_next;
754 NYD2_LEAVE;
755 return rv;
758 static void
759 a_amv_mac_free(struct a_amv_mac *amp){
760 struct a_amv_mac_line **amlpp;
761 NYD2_ENTER;
763 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
764 n_free(*amlpp);
765 n_free(amp->am_line_dat);
766 n_free(amp);
767 NYD2_LEAVE;
770 static void
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;
774 size_t nl, vl;
775 NYD2_ENTER;
777 /* Propagate unrolling up the stack, as necessary */
778 assert(alp != NULL);
779 for(;;){
780 if(alp->as_loflags & a_AMV_LF_SCOPE_MASK)
781 break;
782 if((alp = alp->as_up) == NULL)
783 goto jleave;
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))
789 goto jleave;
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;
795 alp->as_lopts = avp;
796 if(vl != 0){
797 avp->av_value = &avp->av_name[nl];
798 avp->av_flags = oavp->av_flags;
799 memcpy(avp->av_value, oavp->av_value, vl);
800 #ifdef HAVE_PUTENV
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)));
805 #endif
807 memcpy(avp->av_name, name, nl);
808 jleave:
809 NYD2_LEAVE;
812 static void
813 a_amv_lopts_unroll(struct a_amv_var **avpp){
814 struct a_amv_lostack *save_alp;
815 struct a_amv_var *x, *avp;
816 NYD2_ENTER;
818 avp = *avpp;
819 *avpp = NULL;
821 save_alp = a_amv_lopts;
822 a_amv_lopts = NULL;
823 while(avp != NULL){
824 x = avp;
825 avp = avp->av_link;
826 n_PS_ROOT_BLOCK(n_var_vset(x->av_name, (uintptr_t)x->av_value));
827 n_free(x);
829 a_amv_lopts = save_alp;
830 NYD2_LEAVE;
833 static char *
834 a_amv_var_copy(char const *str){
835 char *news;
836 size_t len;
837 NYD2_ENTER;
839 if(*str == '\0')
840 news = n_UNCONST(n_empty);
841 else if(str[1] == '\0'){
842 if(str[0] == '1')
843 news = n_UNCONST(n_1);
844 else if(str[0] == '0')
845 news = n_UNCONST(n_0);
846 else
847 goto jheap;
848 }else if(str[2] == '\0' && str[0] == '-' && str[1] == '1')
849 news = n_UNCONST(n_m1);
850 else{
851 jheap:
852 len = strlen(str) +1;
853 news = n_alloc(len);
854 memcpy(news, str, len);
856 NYD2_LEAVE;
857 return news;
860 static void
861 a_amv_var_free(char *cp){
862 NYD2_ENTER;
863 if(cp[0] != '\0' && cp != n_0 && cp != n_1 && cp != n_m1)
864 n_free(cp);
865 NYD2_LEAVE;
868 static bool_t
869 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm, enum okeys okey,
870 char const *val){
871 bool_t ok;
872 NYD2_ENTER;
874 ok = TRU1;
876 if(avvm == a_AMV_VIP_SET_PRE){
877 switch(okey){
878 default:
879 break;
880 case ok_v_HOME:
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.
883 * See main.c! */
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));
887 ok = FAL0;
889 break;
890 case ok_v_TMPDIR:
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));
894 ok = FAL0;
896 break;
897 case ok_v_umask:
898 if(*val != '\0'){
899 ui64_t uib;
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);
904 ok = FAL0;
907 break;
909 }else if(avvm == a_AMV_VIP_SET_POST){
910 switch(okey){
911 default:
912 break;
913 case ok_b_ask:
914 ok_bset(asksub);
915 break;
916 case ok_b_debug:
917 n_poption |= n_PO_DEBUG;
918 break;
919 case ok_v_HOME:
920 /* Invalidate any resolved folder then, too
921 * FALLTHRU */
922 case ok_v_folder:
923 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved));
924 break;
925 case ok_v_ifs:{
926 char *x_b, *x, c;
928 x_b = x = n_autorec_alloc(strlen(val) +1);
929 while((c = *val++) != '\0')
930 if(spacechar(c))
931 *x++ = c;
932 *x = '\0';
933 n_PS_ROOT_BLOCK(ok_vset(ifs_ws, x_b));
934 } break;
935 #ifdef HAVE_SETLOCALE
936 case ok_v_LANG:
937 case ok_v_LC_ALL:
938 case ok_v_LC_CTYPE:
939 n_locale_init();
940 break;
941 #endif
942 case ok_b_memdebug:
943 n_poption |= n_PO_MEMDEBUG;
944 break;
945 case ok_b_POSIXLY_CORRECT: /* <-> *posix* */
946 if(!(n_pstate & n_PS_ROOT))
947 n_PS_ROOT_BLOCK(ok_bset(posix));
948 break;
949 case ok_b_posix: /* <-> $POSIXLY_CORRECT */
950 if(!(n_pstate & n_PS_ROOT))
951 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT));
952 break;
953 case ok_b_skipemptybody:
954 n_poption |= n_PO_E_FLAG;
955 break;
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);
961 break;
962 case ok_v_umask:
963 if(*val != '\0'){
964 ui64_t uib;
966 n_idec_ui64_cp(&uib, val, 0, NULL);
967 umask((mode_t)uib);
969 break;
970 case ok_b_verbose:
971 n_poption |= (n_poption & n_PO_VERB) ? n_PO_VERBVERB : n_PO_VERB;
972 break;
974 }else{
975 switch(okey){
976 default:
977 break;
978 case ok_b_ask:
979 ok_bclear(asksub);
980 break;
981 case ok_b_debug:
982 n_poption &= ~n_PO_DEBUG;
983 break;
984 case ok_v_HOME:
985 /* Invalidate any resolved folder then, too
986 * FALLTHRU */
987 case ok_v_folder:
988 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved));
989 break;
990 case ok_b_memdebug:
991 n_poption &= ~n_PO_MEMDEBUG;
992 break;
993 case ok_b_POSIXLY_CORRECT: /* <-> *posix* */
994 if(!(n_pstate & n_PS_ROOT))
995 n_PS_ROOT_BLOCK(ok_bclear(posix));
996 break;
997 case ok_b_posix: /* <-> $POSIXLY_CORRECT */
998 if(!(n_pstate & n_PS_ROOT))
999 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT));
1000 break;
1001 case ok_b_skipemptybody:
1002 n_poption &= ~n_PO_E_FLAG;
1003 break;
1004 case ok_b_verbose:
1005 n_poption &= ~(n_PO_VERB | n_PO_VERBVERB);
1006 break;
1009 NYD2_LEAVE;
1010 return ok;
1013 static bool_t
1014 a_amv_var_check_nocntrls(char const *val){
1015 char c;
1016 NYD2_ENTER;
1018 while((c = *val++) != '\0')
1019 if(cntrlchar(c))
1020 break;
1021 NYD2_LEAVE;
1022 return (c == '\0');
1025 static bool_t
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 */
1031 bool_t rv;
1032 NYD2_ENTER;
1034 rv = TRU1;
1036 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1037 ui64_t uib;
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)
1044 rv = FAL0;
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))
1048 rv = FAL0;
1050 NYD2_LEAVE;
1051 return rv;
1054 static char const *
1055 a_amv_var_canonify(char const *vn){
1056 NYD2_ENTER;
1057 if(!upperchar(*vn)){
1058 char const *vp;
1060 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
1062 vn = (*vp == '@') ? i_strdup(vn) : vn;
1064 NYD2_LEAVE;
1065 return vn;
1068 static bool_t
1069 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
1070 ui32_t hash, i, j;
1071 struct a_amv_var_map const *avmp;
1072 char c;
1073 NYD2_ENTER;
1075 /* It may be a special a.k.a. macro-local or one-letter parameter */
1076 c = name[0];
1077 if(n_UNLIKELY(digitchar(c))){
1078 /* (Inline dec. atoi, ugh) */
1079 for(j = (ui8_t)c - '0', i = 1;; ++i){
1080 c = name[i];
1081 if(c == '\0')
1082 break;
1083 if(!digitchar(c))
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')){
1092 switch(c){
1093 case '?':
1094 case '!':
1095 avcp->avc_special_cat = a_AMV_VSC_GLOBAL;
1096 j = (c == '?') ? a_AMV_VST_QM : a_AMV_VST_EM;
1097 goto jspecial_param;
1098 case '^':
1099 goto jmultiplex;
1100 case '*':
1101 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1102 j = a_AMV_VST_STAR;
1103 goto jspecial_param;
1104 case '@':
1105 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1106 j = a_AMV_VST_AT;
1107 goto jspecial_param;
1108 case '#':
1109 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1110 j = a_AMV_VST_NOSIGN;
1111 goto jspecial_param;
1112 default:
1113 break;
1115 }else if(c == '^'){
1116 jmultiplex:
1117 avcp->avc_special_cat = a_AMV_VSC_MULTIPLEX;
1118 j = a_AMV_VST_CACC;
1119 goto jspecial_param;
1122 /* Normal reverse lookup, walk over the hashtable */
1123 jno_special_param:
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){
1129 ui32_t x;
1131 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
1132 break;
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;
1139 goto jleave;
1142 if(++i == a_AMV_VAR_REV_PRIME){
1143 #ifdef a_AMV_VAR_REV_WRAPAROUND
1144 i = 0;
1145 #else
1146 break;
1147 #endif
1150 avcp->avc_map = NULL;
1151 avcp = NULL;
1152 jleave:
1153 NYD2_LEAVE;
1154 return (avcp != NULL);
1156 /* All these are mapped to *--special-param* */
1157 jspecial_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;
1164 goto jleave;
1167 static bool_t
1168 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
1169 size_t i;
1170 char const *cp;
1171 struct a_amv_var_map const *avmp;
1172 struct a_amv_var *avp;
1173 NYD2_ENTER;
1175 /* C99 */{
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! */
1184 if(lavp != NULL){
1185 lavp->av_link = avp->av_link;
1186 avp->av_link = *avpp;
1187 *avpp = avp;
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:
1198 if(!i3val_nonew){
1199 avcp->avc_var = avp;
1200 avp = NULL;
1201 goto j_leave;
1203 /* FALLTHRU */
1204 default:
1205 break;
1207 goto jleave;
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)
1224 ) != 0);
1226 if(n_UNLIKELY(isempty)){
1227 n_err(_("Environment variable must not be empty: %s\n"),
1228 avcp->avc_name);
1229 if(!isbltin)
1230 goto jerr;
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);
1236 goto jerr;
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);
1242 goto jerr;
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);
1248 goto jerr;
1250 goto jnewval;
1251 }else
1252 goto jnewval;
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];
1261 if(arr == NULL){
1262 arr = &arr_base[0];
1263 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1264 while(i-- > 0)
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);
1277 if(!i3val_nonew)
1278 goto jnewval;
1279 if(i3val_nonew == TRUM1)
1280 avp = (struct a_amv_var*)-1;
1281 goto jleave;
1285 /* */
1286 jdefval:
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;
1292 goto jnewval;
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);
1301 goto jleave;
1303 /* Not reached */
1307 jerr:
1308 avp = NULL;
1309 jleave:
1310 avcp->avc_var = avp;
1311 j_leave:
1312 NYD2_LEAVE;
1313 return (avp != NULL);
1315 jnewval:
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)){
1320 #ifdef HAVE_SETENV
1321 if(avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV))
1322 unsetenv(avcp->avc_name);
1323 #endif
1324 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0)
1325 goto jdefval;
1326 goto jerr;
1327 }else{
1328 struct a_amv_var **avpp;
1329 size_t l;
1331 l = strlen(avcp->avc_name) +1;
1332 avcp->avc_var =
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]);
1335 *avpp = avp;
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);
1344 goto jleave;
1348 static char const *
1349 a_amv_var_vsc_global(struct a_amv_var_carrier *avcp){
1350 char itoabuf[32];
1351 char const *rv;
1352 si32_t *ep;
1353 struct a_amv_var_map const *avmp;
1354 NYD2_ENTER;
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;
1361 }else{
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 */
1372 switch(*ep){
1373 case 0: rv = n_0; break;
1374 case 1: rv = n_1; break;
1375 default:
1376 snprintf(itoabuf, sizeof itoabuf, "%d", *ep);
1377 rv = itoabuf;
1378 break;
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;
1385 NYD2_LEAVE;
1386 return rv;
1389 static char const *
1390 a_amv_var_vsc_multiplex(struct a_amv_var_carrier *avcp){
1391 char itoabuf[32];
1392 si32_t e;
1393 size_t i;
1394 char const *rv;
1395 NYD2_ENTER;
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'){
1401 if(i == 3){
1402 e = n_pstate_err_no;
1403 goto jeno;
1404 }else if(rv[3] == '-'){
1405 e = n_err_from_name(&rv[4]);
1406 jeno:
1407 switch(e){
1408 case 0: rv = n_0; break;
1409 case 1: rv = n_1; break;
1410 default:
1411 snprintf(itoabuf, sizeof itoabuf, "%d", e);
1412 rv = savestr(itoabuf); /* XXX yet, cannot do numbers */
1413 break;
1415 goto jleave;
1416 }else if(i >= 6){
1417 if(!memcmp(&rv[3], "DOC", 3)){
1418 rv += 6;
1419 switch(*rv){
1420 case '\0': e = n_pstate_err_no; break;
1421 case '-': e = n_err_from_name(&rv[1]); break;
1422 default: goto jerr;
1424 rv = n_err_to_doc(e);
1425 goto jleave;
1426 }else if(i >= 7 && !memcmp(&rv[3], "NAME", 4)){
1427 rv += 7;
1428 switch(*rv){
1429 case '\0': e = n_pstate_err_no; break;
1430 case '-': e = n_err_from_name(&rv[1]); break;
1431 default: goto jerr;
1433 rv = n_err_to_name(e);
1434 goto jleave;
1439 jerr:
1440 rv = NULL;
1441 jleave:
1442 NYD2_LEAVE;
1443 return rv;
1446 static char const *
1447 a_amv_var_vsc_pospar(struct a_amv_var_carrier *avcp){
1448 size_t i, j;
1449 ui16_t argc;
1450 char const *rv, **argv;
1451 NYD2_ENTER;
1453 rv = NULL;
1455 /* If in a macro/xy.. */
1456 if(a_amv_lopts != NULL){
1457 bool_t ismacky;
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)))
1471 goto jleave;
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];
1477 }else if(ismacky)
1478 rv = amcap->amca_name;
1479 else
1480 rv = (a_amv_lopts->as_up != NULL
1481 ? a_amv_lopts->as_up->as_amcap->amca_name : n_empty);
1482 goto jleave;
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));
1489 goto jleave;
1491 }else{
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];
1500 }else
1501 rv = n_progname;
1502 goto jleave;
1506 switch(avcp->avc_special_prop){ /* XXX OPTIMIZE */
1507 case a_AMV_VST_STAR:
1508 case a_AMV_VST_AT:
1509 for(i = j = 0; i < argc; ++i)
1510 j += strlen(argv[i]) + 1;
1511 if(j == 0)
1512 rv = n_empty;
1513 else{
1514 char *cp;
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);
1520 cp += j;
1521 *cp++ = ' ';
1523 *--cp = '\0';
1525 break;
1526 case a_AMV_VST_NOSIGN:{
1527 char *cp;
1529 rv = cp = n_autorec_alloc(sizeof("65535"));
1530 snprintf(cp, sizeof("65535"), "%hu", argc);
1531 } break;
1532 default:
1533 rv = n_empty;
1534 break;
1536 jleave:
1537 NYD2_LEAVE;
1538 return rv;
1541 static bool_t
1542 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1543 bool_t force_env){
1544 struct a_amv_var *avp;
1545 char *oval;
1546 struct a_amv_var_map const *avmp;
1547 bool_t rv;
1548 NYD2_ENTER;
1550 if(value == NULL){
1551 rv = a_amv_var_clear(avcp, force_env);
1552 goto jleave;
1555 if((avmp = avcp->avc_map) != NULL){
1556 rv = FAL0;
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");
1562 goto jeavmp;
1564 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1565 value = N_("Variable must not be empty: %s\n");
1566 goto jeavmp;
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");
1571 goto jeavmp;
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");
1576 goto jeavmp;
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");
1582 goto jeavmp;
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");
1587 goto jeavmp;
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");
1594 jeavmp:
1595 n_err(V_(value), avcp->avc_name);
1596 goto jleave;
1599 /* Transformations */
1600 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_LOWER)){
1601 char c;
1603 oval = savestr(value);
1604 value = oval;
1605 for(; (c = *oval) != '\0'; ++oval)
1606 *oval = lowerconv(c);
1610 rv = TRU1;
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)))
1622 goto joval_and_go;
1623 if(n_poption & n_PO_D_VV)
1624 n_err(_("Temporarily frozen by -S, not `set'ing: %s\n"),
1625 avcp->avc_name);
1626 goto jleave;
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;
1634 else{
1635 assert(avp->av_value == n_empty);
1636 a_amv_vars[avcp->avc_prime] = avp->av_link;
1637 n_free(avp);
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);
1646 if(avp != NULL)
1647 joval_and_go:
1648 oval = avp->av_value;
1649 else{
1650 struct a_amv_var **avpp;
1651 size_t l;
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]);
1657 *avpp = avp;
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);
1663 if(avmp == NULL)
1664 avp->av_value = a_amv_var_copy(value);
1665 else{
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);
1670 else{
1671 if(!(n_pstate & n_PS_ROOT) && (n_poption & n_PO_D_VV) &&
1672 *value != '\0')
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);
1692 jleave:
1693 NYD2_LEAVE;
1694 return rv;
1697 static bool_t
1698 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1699 #ifndef HAVE_SETENV
1700 char *cp;
1701 #endif
1702 bool_t rv;
1703 NYD2_ENTER;
1705 #ifdef HAVE_SETENV
1706 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1707 #else
1708 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1710 if((rv = (putenv(cp) == 0))){
1711 char *ocp;
1713 ocp = avp->av_env;
1714 avp->av_env = cp;
1715 cp = ocp;
1718 if(cp != NULL)
1719 n_free(cp);
1720 #endif
1721 NYD2_LEAVE;
1722 return rv;
1725 static bool_t
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;
1729 bool_t rv;
1730 NYD2_ENTER;
1732 rv = FAL0;
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);
1738 goto jleave;
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);
1743 goto jleave;
1747 rv = TRU1;
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:{
1757 size_t l;
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]);
1762 *avpp = avp;
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)))
1769 goto jforce_env;
1770 }else if(force_env){
1771 jforce_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);
1775 goto jleave;
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))
1780 goto Jfreeze;
1781 avcp->avc_var = NULL;
1782 if(force_env)
1783 goto jforce_env;
1784 goto jleave;
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 */
1790 if(avp != NULL &&
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)
1799 goto jdefault_path;
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))
1804 goto jforce_env;
1806 goto jleave;
1808 if(n_poption & n_PO_D_VV)
1809 n_err(_("Temporarily frozen by -S, not `unset'ting: %s\n"),
1810 avcp->avc_name);
1811 goto jleave;
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);
1820 jdefault_path:
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);
1830 n_free(avp);
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;
1840 jleave:
1841 NYD2_LEAVE;
1842 return rv;
1845 static bool_t
1846 a_amv_var__clearenv(char const *name, struct a_amv_var *avp){
1847 #ifndef HAVE_SETENV
1848 extern char **environ;
1849 char **ecpp;
1850 #endif
1851 bool_t rv;
1852 NYD2_ENTER;
1854 #ifdef HAVE_SETENV
1855 n_UNUSED(avp);
1856 unsetenv(name);
1857 rv = TRU1;
1858 #else
1859 n_UNUSED(name);
1860 /* TODO a_amv_var__clearenv without HAVE_SETENV does not handle yet
1861 * TODO existing (non-linked) environment variables! */
1862 if(avp != NULL)
1863 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1864 if(*ecpp == avp->av_env){
1866 ecpp[0] = ecpp[1];
1867 while(*ecpp++ != NULL);
1868 n_free(avp->av_env);
1869 avp->av_env = NULL;
1870 break;
1872 rv = TRU1;
1873 #endif /* HAVE_SETENV */
1874 NYD2_LEAVE;
1875 return rv;
1878 static void
1879 a_amv_var_show_all(void){
1880 struct n_string msg, *msgp;
1881 FILE *fp;
1882 size_t no, i;
1883 struct a_amv_var *avp;
1884 char const **vacp, **cap;
1885 NYD2_ENTER;
1887 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1888 n_perr(_("`set' list: cannot create temporary file"), 0);
1889 goto jleave;
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)
1901 ++no;
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;
1912 if(no > 1)
1913 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1915 msgp = &msg;
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);
1919 n_string_gut(&msg);
1921 page_or_print(fp, i);
1922 Fclose(fp);
1923 jleave:
1924 NYD2_LEAVE;
1927 static int
1928 a_amv_var__show_cmp(void const *s1, void const *s2){
1929 int rv;
1930 NYD2_ENTER;
1932 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1933 NYD2_LEAVE;
1934 return rv;
1937 static size_t
1938 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1939 struct a_amv_var_carrier avc;
1940 char const *quote;
1941 struct a_amv_var *avp;
1942 bool_t isset;
1943 size_t i;
1944 NYD2_ENTER;
1946 msgp = n_string_trunc(msgp, 0);
1947 i = 0;
1949 a_amv_var_revlookup(&avc, name);
1950 isset = a_amv_var_lookup(&avc, FAL0);
1951 avp = avc.avc_var;
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");
1956 i = 1;
1957 }else{
1958 struct{
1959 ui16_t flag;
1960 char msg[22];
1961 } const tbase[] = {
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"},
1975 }, *tp;
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");
1990 if(i > 0)
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]);
1997 if(!isset){
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));
2002 goto jleave;
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");
2023 jleave:
2024 msgp = n_string_push_c(msgp, '\n');
2025 fputs(n_string_cp(msgp), fp);
2026 NYD2_ENTER;
2027 return (i > 0 ? 2 : 1);
2030 static bool_t
2031 a_amv_var_c_set(char **ap, bool_t issetenv){
2032 char *cp, *cp2, *varbuf, c;
2033 size_t errs;
2034 NYD2_ENTER;
2036 errs = 0;
2037 jouter:
2038 while((cp = *ap++) != NULL){
2039 /* Isolate key */
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: "
2045 "%s\n"), ap[-1]);
2046 ++errs;
2047 goto jouter;
2049 *cp2++ = c;
2051 *cp2 = '\0';
2052 if(c == '\0')
2053 cp = n_UNCONST(n_empty);
2054 else
2055 ++cp;
2057 if(varbuf == cp2){
2058 n_err(_("Empty variable name ignored\n"));
2059 ++errs;
2060 }else{
2061 struct a_amv_var_carrier avc;
2062 bool_t isunset;
2064 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
2065 varbuf = &varbuf[2];
2067 a_amv_var_revlookup(&avc, varbuf);
2069 if(isunset)
2070 errs += !a_amv_var_clear(&avc, issetenv);
2071 else
2072 errs += !a_amv_var_set(&avc, cp, issetenv);
2075 NYD2_LEAVE;
2076 return (errs == 0);
2079 FL int
2080 c_define(void *v){
2081 int rv;
2082 char **args;
2083 NYD_ENTER;
2085 rv = 1;
2087 if((args = v)[0] == NULL){
2088 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
2089 goto jleave;
2092 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
2093 args[2] != NULL){
2094 n_err(_("Synopsis: define: <name> {\n"));
2095 goto jleave;
2098 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
2099 jleave:
2100 NYD_LEAVE;
2101 return rv;
2104 FL int
2105 c_undefine(void *v){
2106 int rv;
2107 char **args;
2108 NYD_ENTER;
2110 rv = 0;
2111 args = v;
2113 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
2114 while(*++args != NULL);
2115 NYD_LEAVE;
2116 return rv;
2119 FL int
2120 c_call(void *vp){
2121 int rv;
2122 NYD_ENTER;
2124 rv = a_amv_mac_call(vp, FAL0);
2125 NYD_LEAVE;
2126 return rv;
2129 FL int
2130 c_call_if(void *vp){
2131 int rv;
2132 NYD_ENTER;
2134 rv = a_amv_mac_call(vp, TRU1);
2135 NYD_LEAVE;
2136 return rv;
2139 FL int
2140 c_account(void *v){
2141 struct a_amv_mac_call_args *amcap;
2142 struct a_amv_mac *amp;
2143 char **args;
2144 int rv, i, oqf, nqf;
2145 NYD_ENTER;
2147 rv = 1;
2149 if((args = v)[0] == NULL){
2150 rv = (a_amv_mac_show(a_AMV_MF_ACCOUNT) == FAL0);
2151 goto jleave;
2154 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
2155 if(args[2] != NULL){
2156 n_err(_("Synopsis: account: <name> {\n"));
2157 goto jleave;
2159 if(!asccasecmp(args[0], ACCOUNT_NULL)){
2160 n_err(_("`account': cannot use reserved name: %s\n"),
2161 ACCOUNT_NULL);
2162 goto jleave;
2164 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACCOUNT) == FAL0);
2165 goto jleave;
2168 if(n_pstate & n_PS_HOOK_MASK){
2169 n_err(_("`account': cannot change account from within a hook\n"));
2170 goto jleave;
2173 save_mbox_for_possible_quitstuff();
2175 amp = NULL;
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]);
2179 goto jleave;
2182 oqf = savequitflags();
2184 /* Shutdown the active account */
2185 if(a_amv_acc_curr != NULL){
2186 char const *cp;
2187 char *var;
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;
2206 }else
2207 n_err(_("*on-account-leave* hook %s does not exist\n"),
2208 n_shexp_quote_cp(cp, FAL0));
2211 /* `localopts'? */
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 */
2224 if(amp != NULL){
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 = &amp->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);
2237 goto jleave;
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)
2249 goto jleave;
2250 temporary_folder_hook_check(FAL0);
2251 if(i > 0 && !ok_blook(emptystart))
2252 goto jleave;
2253 n_folder_announce(n_ANNOUNCE_CHANGE);
2254 restorequitflags(nqf);
2256 rv = 0;
2257 jleave:
2258 NYD_LEAVE;
2259 return rv;
2262 FL int
2263 c_unaccount(void *v){
2264 int rv;
2265 char **args;
2266 NYD_ENTER;
2268 rv = 0;
2269 args = v;
2271 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACCOUNT);
2272 while(*++args != NULL);
2273 NYD_LEAVE;
2274 return rv;
2277 FL int
2278 c_localopts(void *vp){
2279 enum a_amv_loflags alf, alm;
2280 char const **argv;
2281 int rv;
2282 NYD_ENTER;
2284 rv = 1;
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"));
2289 goto jleave;
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;
2298 else{
2299 n_err(_("Synopsis: localopts: [<scope|call|call-fixate>] <boolean>\n"));
2300 goto jleave;
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"));
2307 goto jleave;
2310 a_amv_lopts->as_loflags &= ~alm;
2311 if(boolify(*argv, UIZ_MAX, FAL0) > 0)
2312 a_amv_lopts->as_loflags |= alf;
2313 rv = 0;
2314 jleave:
2315 NYD_LEAVE;
2316 return rv;
2319 FL int
2320 c_shift(void *vp){
2321 struct a_amv_pospar *appp;
2322 ui16_t i;
2323 int rv;
2324 NYD_ENTER;
2326 rv = 1;
2328 if((vp = *(char**)vp) == NULL)
2329 i = 1;
2330 else{
2331 si16_t sib;
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);
2337 goto jleave;
2339 i = (ui16_t)sib;
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"));
2353 goto jleave;
2355 appp = &amcap->amca_pospar;
2356 }else
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);
2362 goto jleave;
2363 }else{
2364 appp->app_idx += i;
2365 appp->app_count -= i;
2366 rv = 0;
2368 jleave:
2369 NYD_LEAVE;
2370 return rv;
2373 FL int
2374 c_return(void *vp){ /* TODO the exit status should be m_si64! */
2375 int rv;
2376 NYD_ENTER;
2378 if(a_amv_lopts != NULL){
2379 char const **argv;
2381 n_go_input_force_eof();
2382 n_pstate_err_no = n_ERR_NONE;
2383 rv = 0;
2385 if((argv = vp)[0] != NULL){
2386 si32_t i;
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)
2391 rv = (int)i;
2392 else{
2393 n_err(_("`return': return value argument is invalid: %s\n"),
2394 argv[0]);
2395 n_pstate_err_no = n_ERR_INVAL;
2396 rv = 1;
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;
2404 else{
2405 n_err(_("`return': error number argument is invalid: %s\n"),
2406 argv[1]);
2407 n_pstate_err_no = n_ERR_INVAL;
2408 rv = 1;
2412 }else{
2413 n_err(_("Can only use `return' in a macro\n"));
2414 n_pstate_err_no = n_ERR_OPNOTSUPP;
2415 rv = 1;
2417 NYD_LEAVE;
2418 return rv;
2421 FL bool_t
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;
2425 size_t len;
2426 char const *cp;
2427 char *var;
2428 bool_t rv;
2429 NYD_ENTER;
2431 rv = TRU1;
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)
2437 goto jmac;
2439 /* If we are under *folder*, try the usual +NAME syntax, too */
2440 if(displayname[0] == '+'){
2441 char *x;
2443 for(x = &mailname[len]; x != mailname; --x)
2444 if(x[-1] == '/'){
2445 snprintf(var, len, "folder-hook-+%s", x);
2446 if((cp = n_var_vlook(var, FAL0)) != NULL)
2447 goto jmac;
2448 break;
2452 /* Plain *folder-hook* is our last try */
2453 if((cp = ok_vlook(folder_hook)) == NULL)
2454 goto jleave;
2456 jmac:
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);
2460 rv = FAL0;
2461 goto jleave;
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;
2469 if(nmail){
2470 amcap->amca_unroller = NULL;
2471 n_pstate |= n_PS_HOOK_NEWMAIL;
2472 }else{
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;
2482 jleave:
2483 NYD_LEAVE;
2484 return rv;
2487 FL void
2488 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2489 NYD_ENTER;
2490 if(a_amv_folder_hook_lopts != NULL){
2491 void *save = a_amv_lopts;
2493 a_amv_lopts = NULL;
2494 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
2495 assert(a_amv_folder_hook_lopts == NULL);
2496 a_amv_lopts = save;
2498 NYD_LEAVE;
2501 FL void
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;
2509 NYD_ENTER;
2511 amp = NULL;
2513 if(macname == (char*)-1){
2514 a_amv_mac__finalize(cmh_losp);
2515 cmh_losp = NULL;
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"),
2519 macname);
2520 else{
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;
2534 if(macname != NULL)
2535 a_amv_mac_exec(amcap);
2536 else{
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;
2545 NYD_LEAVE;
2548 FL void
2549 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2550 NYD_ENTER;
2551 if(a_amv_compose_lopts != NULL){
2552 void *save = a_amv_lopts;
2554 a_amv_lopts = NULL;
2555 a_amv_lopts_unroll(&a_amv_compose_lopts);
2556 assert(a_amv_compose_lopts == NULL);
2557 a_amv_lopts = save;
2559 NYD_LEAVE;
2562 FL bool_t
2563 n_var_is_user_writable(char const *name){
2564 struct a_amv_var_carrier avc;
2565 struct a_amv_var_map const *avmp;
2566 bool_t rv;
2567 NYD_ENTER;
2569 a_amv_var_revlookup(&avc, name);
2570 if((avmp = avc.avc_map) == NULL)
2571 rv = TRU1;
2572 else
2573 rv = ((avmp->avm_flags & (a_AMV_VF_BOOL | a_AMV_VF_RDONLY)) == 0);
2574 NYD_LEAVE;
2575 return rv;
2578 FL char *
2579 n_var_oklook(enum okeys okey){
2580 struct a_amv_var_carrier avc;
2581 char *rv;
2582 struct a_amv_var_map const *avmp;
2583 NYD_ENTER;
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;
2592 else
2593 rv = NULL;
2594 NYD_LEAVE;
2595 return rv;
2598 FL bool_t
2599 n_var_okset(enum okeys okey, uintptr_t val){
2600 struct a_amv_var_carrier avc;
2601 bool_t ok;
2602 struct a_amv_var_map const *avmp;
2603 NYD_ENTER;
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);
2611 NYD_LEAVE;
2612 return ok;
2615 FL bool_t
2616 n_var_okclear(enum okeys okey){
2617 struct a_amv_var_carrier avc;
2618 bool_t rv;
2619 struct a_amv_var_map const *avmp;
2620 NYD_ENTER;
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);
2628 NYD_LEAVE;
2629 return rv;
2632 FL char const *
2633 n_var_vlook(char const *vokey, bool_t try_getenv){
2634 struct a_amv_var_carrier avc;
2635 char const *rv;
2636 NYD_ENTER;
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)
2647 rv = getenv(vokey);
2648 else
2649 rv = NULL;
2650 break;
2651 case a_AMV_VSC_GLOBAL:
2652 rv = a_amv_var_vsc_global(&avc);
2653 break;
2654 case a_AMV_VSC_MULTIPLEX:
2655 rv = a_amv_var_vsc_multiplex(&avc);
2656 break;
2657 case a_AMV_VSC_POSPAR:
2658 case a_AMV_VSC_POSPAR_ENV:
2659 rv = a_amv_var_vsc_pospar(&avc);
2660 break;
2662 NYD_LEAVE;
2663 return rv;
2666 FL bool_t
2667 n_var_vexplode(void const **cookie){
2668 struct a_amv_pospar *appp;
2669 NYD_ENTER;
2671 appp = (a_amv_lopts != NULL) ? &a_amv_lopts->as_amcap->amca_pospar
2672 : &a_amv_pospar;
2673 *cookie = (appp->app_count > 0) ? &appp->app_dat[appp->app_idx] : NULL;
2674 NYD_LEAVE;
2675 return (*cookie != NULL);
2678 FL bool_t
2679 n_var_vset(char const *vokey, uintptr_t val){
2680 struct a_amv_var_carrier avc;
2681 bool_t ok;
2682 NYD_ENTER;
2684 a_amv_var_revlookup(&avc, vokey);
2686 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2687 NYD_LEAVE;
2688 return ok;
2691 FL bool_t
2692 n_var_vclear(char const *vokey){
2693 struct a_amv_var_carrier avc;
2694 bool_t ok;
2695 NYD_ENTER;
2697 a_amv_var_revlookup(&avc, vokey);
2699 ok = a_amv_var_clear(&avc, FAL0);
2700 NYD_LEAVE;
2701 return ok;
2704 #ifdef HAVE_SOCKETS
2705 FL char *
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;
2710 size_t nlen;
2711 char *nbuf, *rv;
2712 struct a_amv_var_map const *avmp;
2713 NYD_ENTER;
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))){
2719 nbuf = NULL;
2720 goto jplain;
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);
2731 nbuf[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))
2738 goto jvar;
2740 /* The second */
2741 if(oxm & OXM_H_P){
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)){
2747 jvar:
2748 rv = avc.avc_var->av_value;
2749 goto jleave;
2753 jplain:
2754 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2755 jleave:
2756 if(nbuf != NULL)
2757 n_lofi_free(nbuf);
2758 NYD_LEAVE;
2759 return rv;
2761 #endif /* HAVE_SOCKETS */
2763 FL int
2764 c_set(void *v){
2765 char **ap;
2766 int err;
2767 NYD_ENTER;
2769 if(*(ap = v) == NULL){
2770 a_amv_var_show_all();
2771 err = 0;
2772 }else
2773 err = !a_amv_var_c_set(ap, FAL0);
2774 NYD_LEAVE;
2775 return err;
2778 FL int
2779 c_unset(void *v){
2780 char **ap;
2781 int err;
2782 NYD_ENTER;
2784 for(err = 0, ap = v; *ap != NULL; ++ap)
2785 err |= !n_var_vclear(*ap);
2786 NYD_LEAVE;
2787 return err;
2790 FL int
2791 c_varshow(void *v){
2792 char **ap;
2793 NYD_ENTER;
2795 if(*(ap = v) == NULL)
2796 v = NULL;
2797 else{
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);
2803 n_string_gut(msgp);
2805 NYD_LEAVE;
2806 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2809 FL int
2810 c_varedit(void *v){
2811 struct a_amv_var_carrier avc;
2812 FILE *of, *nf;
2813 char *val, **argv;
2814 int err;
2815 sighandler_type sigint;
2816 NYD_ENTER;
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"),
2828 avc.avc_name);
2829 continue;
2831 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2832 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2833 avc.avc_name);
2834 continue;
2838 a_amv_var_lookup(&avc, FAL0);
2840 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2841 NULL){
2842 n_perr(_("`varedit': cannot create temporary file"), 0);
2843 err = 1;
2844 break;
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);
2848 Fclose(of);
2849 err = 1;
2850 continue;
2853 fflush_rewind(of);
2854 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2855 Fclose(of);
2857 if(nf != NULL){
2858 int c;
2859 char const *varres;
2860 off_t l;
2862 l = fsize(nf);
2863 if(UICMP(64, l, >=, UIZ_MAX -42)){
2864 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2865 avc.avc_name);
2866 varres = n_empty;
2867 err = 1;
2868 }else{
2869 varres = val = n_autorec_alloc(l +1);
2870 for(; l > 0 && (c = getc(nf)) != EOF; --l)
2871 *val++ = c;
2872 *val++ = '\0';
2873 if(l != 0){
2874 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2875 avc.avc_name);
2876 err = 1;
2880 if(!a_amv_var_set(&avc, varres, FAL0))
2881 err = 1;
2883 Fclose(nf);
2884 }else{
2885 n_err(_("`varedit': cannot start $EDITOR\n"));
2886 err = 1;
2887 break;
2891 safe_signal(SIGINT, sigint);
2892 NYD_LEAVE;
2893 return err;
2896 FL int
2897 c_environ(void *v){
2898 struct a_amv_var_carrier avc;
2899 int err;
2900 char **ap;
2901 bool_t islnk;
2902 NYD_ENTER;
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))){
2911 if(!islnk){
2912 avc.avc_var->av_flags &= ~a_AMV_VF_EXT_LINKED;
2913 continue;
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);
2918 continue;
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);
2923 }else if(!islnk){
2924 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2925 err = 1;
2926 }else{
2927 char const *evp = getenv(*ap);
2929 if(evp != NULL)
2930 err |= !a_amv_var_set(&avc, evp, TRU1);
2931 else{
2932 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2933 *ap);
2934 err = 1;
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))
2945 err = 1;
2947 }else{
2948 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2949 err = 1;
2951 NYD_LEAVE;
2952 return err;
2955 FL int
2956 c_vexpr(void *v){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2957 size_t i;
2958 enum n_idec_state ids;
2959 si64_t lhv, rhv;
2960 char op, varbuf[64 + 64 / 8 +1];
2961 char const **argv, *varname, *varres, *cp;
2962 enum{
2963 a_ERR = 1u<<0,
2964 a_SOFTOVERFLOW = 1u<<1,
2965 a_ISNUM = 1u<<2,
2966 a_ISDECIMAL = 1u<<3, /* Print only decimal result */
2967 a_SATURATED = 1u<<4,
2968 a_ICASE = 1u<<5,
2969 a_UNSIGNED = 1u<<6, /* Unsigned right shift (share bit ok) */
2970 a_TMP = 1u<<30
2971 } f;
2972 NYD_ENTER;
2974 f = a_ERR;
2975 argv = v;
2976 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *argv++ : NULL;
2977 n_UNINIT(varres, n_empty);
2979 if((cp = argv[0])[0] == '\0')
2980 goto jesubcmd;
2982 if(cp[1] == '\0'){
2983 op = cp[0];
2984 jnumop:
2985 f |= a_ISNUM;
2986 switch(op){
2987 case '=':
2988 case '~':
2989 if(argv[1] == NULL || argv[2] != NULL)
2990 goto jesynopsis;
2992 if(*(cp = *++argv) == '\0')
2993 lhv = 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))
2998 goto jenum_range;
2999 f |= a_SOFTOVERFLOW;
3000 break;
3002 if(op == '~')
3003 lhv = ~lhv;
3004 break;
3006 case '+':
3007 case '-':
3008 case '*':
3009 case '/':
3010 case '%':
3011 case '|':
3012 case '&':
3013 case '^':
3014 case '<':
3015 case '>':
3016 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
3017 goto jesynopsis;
3018 else{
3019 char xop;
3021 if(*(cp = *++argv) == '\0')
3022 lhv = 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))
3027 goto jenum_range;
3028 f |= a_SOFTOVERFLOW;
3029 break;
3032 if(*(cp = *++argv) == '\0')
3033 rhv = 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))
3038 goto jenum_range;
3039 f |= a_SOFTOVERFLOW;
3040 lhv = rhv;
3041 break;
3044 xop = op;
3045 jnumop_again:
3046 switch(xop){
3047 case '+':
3048 if(rhv < 0){
3049 if(rhv != SI64_MIN){
3050 rhv = -rhv;
3051 xop = '-';
3052 goto jnumop_again;
3053 }else if(lhv < 0)
3054 goto jenum_plusminus;
3055 else if(lhv == 0){
3056 lhv = rhv;
3057 break;
3060 if(SI64_MAX - rhv < lhv)
3061 goto jenum_plusminus;
3062 lhv += rhv;
3063 break;
3064 case '-':
3065 if(rhv < 0){
3066 if(rhv != SI64_MIN){
3067 rhv = -rhv;
3068 xop = '+';
3069 goto jnumop_again;
3070 }else if(lhv > 0)
3071 goto jenum_plusminus;
3072 else if(lhv == 0){
3073 lhv = rhv;
3074 break;
3077 if(SI64_MIN + rhv > lhv){
3078 jenum_plusminus:
3079 if(!(f & a_SATURATED))
3080 goto jenum_overflow;
3081 f |= a_SOFTOVERFLOW;
3082 lhv = (lhv < 0 || xop == '-') ? SI64_MIN : SI64_MAX;
3083 }else
3084 lhv -= rhv;
3085 break;
3086 case '*':
3087 /* Will the result be positive? */
3088 if((lhv < 0) == (rhv < 0)){
3089 if(lhv > 0){
3090 lhv = -lhv;
3091 rhv = -rhv;
3093 if(rhv != 0 && lhv != 0 && SI64_MAX / rhv > lhv){
3094 if(!(f & a_SATURATED))
3095 goto jenum_overflow;
3096 f |= a_SOFTOVERFLOW;
3097 lhv = SI64_MAX;
3098 }else
3099 lhv *= rhv;
3100 }else{
3101 if(rhv > 0){
3102 if(lhv != 0 && SI64_MIN / lhv < rhv){
3103 if(!(f & a_SATURATED))
3104 goto jenum_overflow;
3105 f |= a_SOFTOVERFLOW;
3106 lhv = SI64_MIN;
3107 }else
3108 lhv *= rhv;
3109 }else{
3110 if(rhv != 0 && lhv != 0 && SI64_MIN / rhv < lhv){
3111 if(!(f & a_SATURATED))
3112 goto jenum_overflow;
3113 f |= a_SOFTOVERFLOW;
3114 lhv = SI64_MIN;
3115 }else
3116 lhv *= rhv;
3119 break;
3120 case '/':
3121 if(rhv == 0){
3122 if(!(f & a_SATURATED))
3123 goto jenum_range;
3124 f |= a_SOFTOVERFLOW;
3125 lhv = SI64_MAX;
3126 }else
3127 lhv /= rhv;
3128 break;
3129 case '%':
3130 if(rhv == 0){
3131 if(!(f & a_SATURATED))
3132 goto jenum_range;
3133 f |= a_SOFTOVERFLOW;
3134 lhv = SI64_MAX;
3135 }else
3136 lhv %= rhv;
3137 break;
3138 case '|':
3139 lhv |= rhv;
3140 break;
3141 case '&':
3142 lhv &= rhv;
3143 break;
3144 case '^':
3145 lhv ^= rhv;
3146 break;
3147 case '<':
3148 case '>':
3149 if(!(f & a_TMP))
3150 goto jesubcmd;
3151 if(rhv > 63){ /* xxx 63? */
3152 if(!(f & a_SATURATED))
3153 goto jenum_overflow;
3154 rhv = 63;
3156 if(op == '<')
3157 lhv <<= (ui8_t)rhv;
3158 else if(f & a_UNSIGNED)
3159 lhv = (ui64_t)lhv >> (ui8_t)rhv;
3160 else
3161 lhv >>= (ui8_t)rhv;
3162 break;
3165 break;
3166 default:
3167 goto jesubcmd;
3169 }else if(cp[2] == '\0' && cp[1] == '@'){
3170 f |= a_SATURATED;
3171 op = cp[0];
3172 goto jnumop;
3173 }else if(cp[0] == '<'){
3174 if(*++cp != '<')
3175 goto jesubcmd;
3176 if(*++cp == '@'){
3177 f |= a_SATURATED;
3178 ++cp;
3180 if(*cp != '\0')
3181 goto jesubcmd;
3182 f |= a_TMP;
3183 op = '<';
3184 goto jnumop;
3185 }else if(cp[0] == '>'){
3186 if(*++cp != '>')
3187 goto jesubcmd;
3188 if(*++cp == '>'){
3189 f |= a_UNSIGNED;
3190 ++cp;
3192 if(*cp == '@'){
3193 f |= a_SATURATED;
3194 ++cp;
3196 if(*cp != '\0')
3197 goto jesubcmd;
3198 f |= a_TMP;
3199 op = '>';
3200 goto jnumop;
3201 }else if(is_asccaseprefix(cp, "length")){
3202 f |= a_ISNUM | a_ISDECIMAL;
3203 if(argv[1] == NULL || argv[2] != NULL)
3204 goto jesynopsis;
3206 i = strlen(*++argv);
3207 if(UICMP(64, i, >, SI64_MAX))
3208 goto jestr_overflow;
3209 lhv = (si64_t)i;
3210 }else if(is_asccaseprefix(cp, "hash")){
3211 f |= a_ISNUM | a_ISDECIMAL;
3212 if(argv[1] == NULL || argv[2] != NULL)
3213 goto jesynopsis;
3215 i = n_torek_hash(*++argv);
3216 lhv = (si64_t)i;
3217 }else if(is_asccaseprefix(cp, "find")){
3218 f |= a_ISNUM | a_ISDECIMAL;
3219 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
3220 goto jesynopsis;
3222 if((cp = strstr(argv[1], argv[2])) == NULL)
3223 goto jestr_nodata;
3224 i = PTR2SIZE(cp - argv[1]);
3225 if(UICMP(64, i, >, SI64_MAX))
3226 goto jestr_overflow;
3227 lhv = (si64_t)i;
3228 }else if(is_asccaseprefix(cp, "ifind")){
3229 f |= a_ISNUM | a_ISDECIMAL;
3230 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
3231 goto jesynopsis;
3233 if((cp = asccasestr(argv[1], argv[2])) == NULL)
3234 goto jestr_nodata;
3235 i = PTR2SIZE(cp - argv[1]);
3236 if(UICMP(64, i, >, SI64_MAX))
3237 goto jestr_overflow;
3238 lhv = (si64_t)i;
3239 }else if(is_asccaseprefix(cp, "substring")){
3240 if(argv[1] == NULL || argv[2] == NULL)
3241 goto jesynopsis;
3242 if(argv[3] != NULL && argv[4] != NULL)
3243 goto jesynopsis;
3245 i = strlen(varres = *++argv);
3247 if(*(cp = *++argv) == '\0')
3248 lhv = 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;
3253 if(lhv < 0){
3254 if(UICMP(64, i, <, -lhv))
3255 goto jesubstring_off;
3256 lhv = i + lhv;
3258 if(UICMP(64, i, >=, lhv)){
3259 i -= lhv;
3260 varres += lhv;
3261 }else{
3262 jesubstring_off:
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')
3271 lhv = 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;
3276 if(lhv < 0){
3277 if(UICMP(64, i, <, -lhv))
3278 goto jesubstring_len;
3279 lhv = i + lhv;
3281 if(UICMP(64, i, >=, lhv)){
3282 if(UICMP(64, i, !=, lhv))
3283 varres = savestrbuf(varres, (size_t)lhv);
3284 }else{
3285 jesubstring_len:
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: {
3293 struct str trim;
3294 enum n_str_trim_flags stf;
3296 if(argv[1] == NULL || argv[2] != NULL)
3297 goto jesynopsis;
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;
3303 else
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"))
3310 goto Jtrim;
3311 else if(is_asccaseprefix(cp, "trim-end"))
3312 goto Jtrim;
3313 else if(is_asccaseprefix(cp, "random")){
3314 if(argv[1] == NULL || argv[2] != NULL)
3315 goto jesynopsis;
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;
3321 if(lhv == 0)
3322 lhv = NAME_MAX;
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)
3326 goto jesynopsis;
3328 if((varres = fexpand(argv[1], FEXP_NVAR | FEXP_NOPROTO)) == NULL)
3329 goto jestr_nodata;
3330 }else if(is_asccaseprefix(cp, "makeprint")){
3331 struct str sin, sout;
3333 if(argv[1] == NULL || argv[2] != NULL)
3334 goto jesynopsis;
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);
3340 n_free(sout.s);
3341 /* TODO `vexpr': (wide) string length, find, etc!! */
3342 #ifdef HAVE_REGEX
3343 }else if(is_asccaseprefix(cp, "regex")) Jregex:{
3344 regmatch_t rema[1 + n_VEXPR_REGEX_MAX];
3345 regex_t re;
3346 int reflrv;
3348 f |= a_ISNUM | a_ISDECIMAL;
3349 if(argv[1] == NULL || argv[2] == NULL ||
3350 (argv[3] != NULL && argv[4] != NULL))
3351 goto jesynopsis;
3353 reflrv = REG_EXTENDED;
3354 if(f & a_ICASE)
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));
3359 assert(f & a_ERR);
3360 n_pstate_err_no = n_ERR_INVAL;
3361 goto jestr;
3363 reflrv = regexec(&re, argv[1], n_NELEM(rema), rema, 0);
3364 regfree(&re);
3365 if(reflrv == REG_NOMATCH)
3366 goto jestr_nodata;
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;
3373 }else{
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);
3392 *reargv = NULL;
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;
3399 a_amv_lopts = &los;
3401 /* C99 */{
3402 struct str templ;
3403 struct n_string s_b;
3404 enum n_shexp_state shs;
3406 templ.s = n_UNCONST(argv[3]);
3407 templ.l = UIZ_MAX;
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);
3416 }else
3417 varres = NULL;
3420 a_amv_lopts = los.as_global_saved;
3421 rele_all_sigs(); /* TODO DISLIKE! */
3423 if(varres == NULL)
3424 goto jestr_nodata;
3425 f &= ~(a_ISNUM | a_ISDECIMAL);
3427 }else if(is_asccaseprefix(argv[0], "iregex")){
3428 f |= a_ICASE;
3429 goto Jregex;
3430 #endif /* HAVE_REGEX */
3431 }else
3432 goto jesubcmd;
3434 n_pstate_err_no = (f & a_SOFTOVERFLOW) ? n_ERR_OVERFLOW : n_ERR_NONE;
3435 f &= ~a_ERR;
3437 /* Generate the variable value content for numerics.
3438 * Anticipate in our handling below! (Don't do needless work) */
3439 jleave:
3440 if((f & a_ISNUM) && ((f & a_ISDECIMAL) || varname != NULL)){
3441 snprintf(varbuf, sizeof varbuf, "%" PRId64 , lhv);
3442 varres = varbuf;
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){
3449 size_t j;
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){
3454 ++j;
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;
3463 f |= a_ERR;
3465 }else if(varres != NULL && fprintf(n_stdout, "%s\n", varres) < 0){
3466 n_pstate_err_no = n_err_no;
3467 f |= a_ERR;
3469 }else if(!n_var_vset(varname, (uintptr_t)varres)){
3470 n_pstate_err_no = n_ERR_NOTSUP;
3471 f |= a_ERR;
3473 NYD_LEAVE;
3474 return (f & a_ERR) ? 1 : 0;
3476 jerr:
3477 f = a_ERR | a_ISNUM;
3478 lhv = -1;
3479 goto jleave;
3480 jesubcmd:
3481 n_err(_("`vexpr': invalid subcommand: %s\n"),
3482 n_shexp_quote_cp(*argv, FAL0));
3483 n_pstate_err_no = n_ERR_INVAL;
3484 goto jerr;
3485 jesynopsis:
3486 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3487 n_pstate_err_no = n_ERR_INVAL;
3488 goto jerr;
3490 jenum_range:
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;
3494 goto jerr;
3495 jenum_overflow:
3496 n_err(_("`vexpr': expression overflows datatype: %" PRId64 " %c %" PRId64
3497 "\n"), lhv, op, rhv);
3498 n_pstate_err_no = n_ERR_OVERFLOW;
3499 goto jerr;
3501 jestr_numrange:
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;
3505 goto jestr;
3506 jestr_overflow:
3507 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3508 n_pstate_err_no = n_ERR_OVERFLOW;
3509 goto jestr;
3510 jestr_nodata:
3511 n_pstate_err_no = n_ERR_NODATA;
3512 /* FALLTHRU */
3513 jestr:
3514 varres = n_empty;
3515 f &= ~a_ISNUM;
3516 f |= a_ERR;
3517 goto jleave;
3520 FL int
3521 c_vpospar(void *v){
3522 struct n_cmd_arg *cap;
3523 size_t i;
3524 struct a_amv_pospar *appp;
3525 enum{
3526 a_NONE = 0,
3527 a_ERR = 1u<<0,
3528 a_SET = 1u<<1,
3529 a_CLEAR = 1u<<2,
3530 a_QUOTE = 1u<<3
3531 } f;
3532 char const *varres;
3533 struct n_cmd_arg_ctx *cacp;
3534 NYD_ENTER;
3536 n_pstate_err_no = n_ERR_NONE;
3537 n_UNINIT(varres, n_empty);
3538 cacp = v;
3539 cap = cacp->cac_arg;
3541 if(is_asccaseprefix(cap->ca_arg.ca_str.s, "set"))
3542 f = a_SET;
3543 else if(is_asccaseprefix(cap->ca_arg.ca_str.s, "clear"))
3544 f = a_CLEAR;
3545 else if(is_asccaseprefix(cap->ca_arg.ca_str.s, "quote"))
3546 f = a_QUOTE;
3547 else{
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;
3551 f = a_ERR;
3552 goto jleave;
3554 --cacp->cac_no;
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;
3559 f = a_ERR;
3560 goto jleave;
3563 cap = cap->ca_next;
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
3567 : &a_amv_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);
3579 if(f & a_SET){
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;
3583 f = a_ERR;
3584 goto jleave;
3587 memset(appp, 0, sizeof *appp);
3588 if(i > 0){
3589 appp->app_maxcount = appp->app_count = (ui16_t)i;
3590 /* XXX Optimize: store it all in one chunk! */
3591 ++i;
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;
3604 }else{
3605 if(appp->app_count == 0)
3606 varres = n_empty;
3607 else{
3608 struct str in;
3609 struct n_string s, *sp;
3610 char sep1, sep2;
3612 sp = n_string_creat_auto(&s);
3614 sep1 = *ok_vlook(ifs);
3615 sep2 = *ok_vlook(ifs_ws);
3616 if(sep1 == sep2)
3617 sep2 = '\0';
3618 if(sep1 == '\0')
3619 sep1 = ' ';
3621 for(i = 0; i < appp->app_count; ++i){
3622 if(sp->s_len){
3623 if(!n_string_can_swallow(sp, 2))
3624 goto jeover;
3625 sp = n_string_push_c(sp, sep1);
3626 if(sep2 != '\0')
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)){
3632 jeover:
3633 n_err(_("`vpospar': overflow: string too long!\n"));
3634 n_pstate_err_no = n_ERR_OVERFLOW;
3635 f = a_ERR;
3636 goto jleave;
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;
3647 f |= a_ERR;
3649 }else if(!n_var_vset(cacp->cac_vput, (uintptr_t)varres)){
3650 n_pstate_err_no = n_ERR_NOTSUP;
3651 f |= a_ERR;
3654 jleave:
3655 NYD_LEAVE;
3656 return (f & a_ERR) ? 1 : 0;
3659 /* s-it-mode */