sopen(): use n_PO_D_V for verbosity, not n_PO_VERB
[s-mailx.git] / accmacvar.c
blobfac13e7701d5ef0b1d69cd7dde5863bfccfd74ef
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 . should be recursive environment based.
8 *@ TODO Otherwise, the `localopts' should be an attribute of the go.c
9 *@ TODO command context, so that it belongs to the execution context
10 *@ TODO we are running in, instead of being global data. See, e.g.,
11 *@ TODO the a_GO_SPLICE comment in go.c.
12 *@ TODO . once we can have non-fatal !0 returns for commands, we should
13 *@ TODO return error if "(environ)? unset" goes for non-existent.
15 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
16 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
19 * Copyright (c) 1980, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
46 #undef n_FILE
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
50 # include "nail.h"
51 #endif
53 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
54 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
55 #endif
57 /* Positional parameter maximum (macro arguments, `vexpr' "splitifs") */
58 #define a_AMV_POSPAR_MAX SI16_MAX
60 /* Special "pseudo macro" that stabs you from the back */
61 #define a_AMV_MACKY_MACK ((struct a_amv_mac*)-1)
63 /* Note: changing the hash function must be reflected in `vexpr' "hash",
64 * because that is used by the hashtable creator scripts! */
65 #define a_AMV_PRIME HSHSIZE
66 #define a_AMV_NAME2HASH(N) n_torek_hash(N)
67 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
69 enum a_amv_mac_flags{
70 a_AMV_MF_NONE = 0,
71 a_AMV_MF_ACCOUNT = 1u<<0, /* This macro is an `account' */
72 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACCOUNT,
73 a_AMV_MF_UNDEF = 1u<<1, /* Unlink after lookup */
74 a_AMV_MF_DELETE = 1u<<7, /* Delete in progress, free once refcnt==0 */
75 a_AMV_MF__MAX = 0xFFu
78 /* make-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
79 * _IMPORT implies _ENV; it doesn't verify anything... */
80 enum a_amv_var_flags{
81 a_AMV_VF_NONE = 0,
82 a_AMV_VF_BOOL = 1u<<0, /* ok_b_* */
83 a_AMV_VF_VIRT = 1u<<1, /* "Stateless" automatic variable */
84 a_AMV_VF_NOLOPTS = 1u<<2, /* May not be tracked by `localopts' */
85 a_AMV_VF_RDONLY = 1u<<3, /* May not be set by user */
86 a_AMV_VF_NODEL = 1u<<4, /* May not be deleted */
87 a_AMV_VF_NOTEMPTY = 1u<<5, /* May not be assigned an empty value */
88 a_AMV_VF_NOCNTRLS = 1u<<6, /* Value may not contain control characters */
89 a_AMV_VF_NUM = 1u<<7, /* Value must be a 32-bit number */
90 a_AMV_VF_POSNUM = 1u<<8, /* Value must be positive 32-bit number */
91 a_AMV_VF_LOWER = 1u<<9, /* Values will be stored in a lowercase version */
92 a_AMV_VF_VIP = 1u<<10, /* Wants _var_check_vips() evaluation */
93 a_AMV_VF_IMPORT = 1u<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
94 a_AMV_VF_ENV = 1u<<12, /* Update environment on change */
95 a_AMV_VF_I3VAL = 1u<<13, /* Has an initial value */
96 a_AMV_VF_DEFVAL = 1u<<14, /* Has a default value */
97 a_AMV_VF_LINKED = 1u<<15, /* `environ' linked */
98 a_AMV_VF__MASK = (1u<<(15+1)) - 1
101 /* We support some special parameter names for one(+)-letter variable names;
102 * note these have counterparts in the code that manages shell expansion!
103 * All these special variables are solely backed by n_var_vlook(), and besides
104 * there is only a_amv_var_revlookup() which knows about them */
105 enum a_amv_var_special_category{
106 a_AMV_VSC_NONE, /* Normal variable, no special treatment */
107 a_AMV_VSC_GLOBAL, /* ${[?!]} are specially mapped, but global */
108 a_AMV_VSC_MULTIPLEX, /* ${^.*} circumflex accent multiplexer */
109 a_AMV_VSC_POSPAR, /* ${[1-9][0-9]*} positional parameters */
110 a_AMV_VSC_POSPAR_ENV /* ${[*@#]} positional parameter support variables */
113 enum a_amv_var_special_type{
114 /* _VSC_GLOBAL */
115 a_AMV_VST_QM, /* ? */
116 a_AMV_VST_EM, /* ! */
117 /* _VSC_MULTIPLEX */
118 /* This is special in that it is a multiplex indicator, the ^ is followed by
119 * a normal variable */
120 a_AMV_VST_CACC, /* ^ (circumflex accent) */
121 /* _VSC_POSPAR_ENV */
122 a_AMV_VST_STAR, /* * */
123 a_AMV_VST_AT, /* @ */
124 a_AMV_VST_NOSIGN /* # */
127 enum a_amv_var_vip_mode{
128 a_AMV_VIP_SET_PRE,
129 a_AMV_VIP_SET_POST,
130 a_AMV_VIP_CLEAR
133 struct a_amv_pospar{
134 ui16_t app_maxcount; /* == slots in .app_dat */
135 ui16_t app_count; /* Maximum a_AMV_POSPAR_MAX */
136 ui16_t app_idx; /* `shift' moves this one, decs .app_count */
137 bool_t app_not_heap; /* .app_dat stuff not on dynamically allocated */
138 ui8_t app__dummy[1];
139 char const **app_dat; /* NULL terminated (for "$@" explosion support) */
141 n_CTA(a_AMV_POSPAR_MAX <= SI16_MAX, "Limit exceeds datatype capabilities");
143 struct a_amv_mac{
144 struct a_amv_mac *am_next;
145 ui32_t am_maxlen; /* of any line in .am_line_dat */
146 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
147 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
148 struct a_amv_var *am_lopts; /* `localopts' unroll list */
149 ui32_t am_refcnt; /* 0-based for `un{account,define}' purposes */
150 ui8_t am_flags; /* enum a_amv_mac_flags */
151 char am_name[n_VFIELD_SIZE(3)]; /* of this macro */
153 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
155 struct a_amv_mac_line{
156 ui32_t aml_len;
157 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
158 char aml_dat[n_VFIELD_SIZE(0)];
161 struct a_amv_mac_call_args{
162 char const *amca_name; /* For MACKY_MACK, this is *0*! */
163 struct a_amv_mac *amca_amp; /* "const", but for am_refcnt */
164 struct a_amv_var **amca_unroller;
165 void (*amca_hook_pre)(void *);
166 void *amca_hook_arg;
167 bool_t amca_lopts_on;
168 bool_t amca_ps_hook_mask;
169 bool_t amca_no_xcall; /* We want n_GO_INPUT_NO_XCALL for this */
170 ui8_t amca__pad[5];
171 struct a_amv_pospar amca_pospar;
174 struct a_amv_lostack{
175 struct a_amv_lostack *as_global_saved; /* Saved global XXX due to jump */
176 struct a_amv_mac_call_args *as_amcap;
177 struct a_amv_lostack *as_up; /* Outer context */
178 struct a_amv_var *as_lopts;
179 bool_t as_unroll; /* Unrolling enabled? */
180 ui8_t avs__pad[7];
183 struct a_amv_var{
184 struct a_amv_var *av_link;
185 char *av_value;
186 #ifdef HAVE_PUTENV
187 char *av_env; /* Actively managed putenv(3) memory */
188 #endif
189 ui16_t av_flags; /* enum a_amv_var_flags */
190 char av_name[n_VFIELD_SIZE(6)];
192 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
194 struct a_amv_var_map{
195 ui32_t avm_hash;
196 ui16_t avm_keyoff;
197 ui16_t avm_flags; /* enum a_amv_var_flags */
199 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
201 struct a_amv_var_virt{
202 ui32_t avv_okey;
203 ui8_t avv__dummy[4];
204 struct a_amv_var const *avv_var;
207 struct a_amv_var_defval{
208 ui32_t avdv_okey;
209 ui8_t avdv__pad[4];
210 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
213 struct a_amv_var_carrier{
214 char const *avc_name;
215 ui32_t avc_hash;
216 ui32_t avc_prime;
217 struct a_amv_var *avc_var;
218 struct a_amv_var_map const *avc_map;
219 enum okeys avc_okey;
220 ui8_t avc__pad[1];
221 ui8_t avc_special_cat;
222 /* Numerical parameter name if .avc_special_cat=a_AMV_VSC_POSPAR,
223 * otherwise a enum a_amv_var_special_type */
224 ui16_t avc_special_prop;
227 /* Include constant make-okey-map.pl output, and the generated version data */
228 #include "gen-version.h"
229 #include "gen-okeys.h"
231 /* The currently active account */
232 static struct a_amv_mac *a_amv_acc_curr;
234 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
236 /* Unroll list of currently running macro stack */
237 static struct a_amv_lostack *a_amv_lopts;
239 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
241 /* Global (i.e., non-local) a_AMV_VSC_POSPAR stack */
242 static struct a_amv_pospar a_amv_pospar;
244 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
245 * TODO today via a static lostack, it should be a field in mailbox, once that
246 * TODO is a real multi-instance object */
247 static struct a_amv_var *a_amv_folder_hook_lopts;
249 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
250 static struct a_amv_var *a_amv_compose_lopts;
252 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
253 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
254 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
255 * plain lookup cases or when _UNDEF was performed on a currently active entry
256 * (the entry will have been unlinked, and the _MF_DELETE will be honoured once
257 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
258 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
259 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
261 /* `call', `call_if' (and `xcall' via go.c -> c_call()) */
262 static int a_amv_mac_call(void *v, bool_t silent_nexist);
264 /* Execute a macro; amcap must reside in LOFI memory */
265 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
267 static void a_amv_mac__finalize(void *vp);
269 /* User display helpers */
270 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
272 /* _def() returns error for faulty definitions and already existing * names,
273 * _undef() returns error if a named thing doesn't exist */
274 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
275 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
277 /* */
278 static void a_amv_mac_free(struct a_amv_mac *amp);
280 /* Update replay-log */
281 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
282 struct a_amv_var *oavp);
283 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
285 /* Special cased value string allocation */
286 static char *a_amv_var_copy(char const *str);
287 static void a_amv_var_free(char *cp);
289 /* Check for special housekeeping. _VIP_SET_POST and _VIP_CLEAR do not fail
290 * (or propagate errors), _VIP_SET_PRE may and should case abortion */
291 static bool_t a_amv_var_check_vips(enum a_amv_var_vip_mode avvm,
292 enum okeys okey, char const *val);
294 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
295 static bool_t a_amv_var_check_nocntrls(char const *val);
296 static bool_t a_amv_var_check_num(char const *val, bool_t posnum);
298 /* If a variable name begins with a lowercase-character and contains at
299 * least one '@', it is converted to all-lowercase. This is necessary
300 * for lookups of names based on email addresses.
301 * Following the standard, only the part following the last '@' should
302 * be lower-cased, but practice has established otherwise here */
303 static char const *a_amv_var_canonify(char const *vn);
305 /* Try to reverse lookup an option name to an enum okeys mapping.
306 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
307 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
308 char const *name);
310 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
311 * Sets .avc_prime; .avc_var is NULL if not found.
312 * Here it is where we care for _I3VAL and _DEFVAL, too.
313 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
314 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
315 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
316 * return FAL0, then! */
317 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
318 bool_t i3val_nonew);
320 /* Lookup functions for special category variables, _pospar drives all
321 * positional parameter etc. special categories */
322 static char const *a_amv_var_vsc_global(struct a_amv_var_carrier *avcp);
323 static char const *a_amv_var_vsc_multiplex(struct a_amv_var_carrier *avcp);
324 static char const *a_amv_var_vsc_pospar(struct a_amv_var_carrier *avcp);
326 /* Set var from .avc_(map|name|hash), return success */
327 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
328 bool_t force_env);
330 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
331 struct a_amv_var *avp);
333 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
334 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
336 static bool_t a_amv_var__clearenv(char const *name, char *value);
338 /* List all variables */
339 static void a_amv_var_show_all(void);
341 static int a_amv_var__show_cmp(void const *s1, void const *s2);
343 /* Actually do print one, return number of lines written */
344 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
346 /* Shared c_set() and c_environ():set impl, return success */
347 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
349 static struct a_amv_mac *
350 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
351 enum a_amv_mac_flags amf){
352 struct a_amv_mac *amp, **ampp;
353 ui32_t h;
354 enum a_amv_mac_flags save_amf;
355 NYD2_ENTER;
357 save_amf = amf;
358 amf &= a_AMV_MF_TYPE_MASK;
359 h = a_AMV_NAME2HASH(name);
360 h = a_AMV_HASH2PRIME(h);
361 ampp = &a_amv_macs[h];
363 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
364 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
365 !strcmp(amp->am_name, name)){
366 if(n_LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
367 goto jleave;
369 *ampp = amp->am_next;
371 if(amp->am_refcnt > 0){
372 amp->am_flags |= a_AMV_MF_DELETE;
373 if(n_poption & n_PO_D_V)
374 n_err(_("Delayed deletion of currently active %s: %s\n"),
375 (amp->am_flags & a_AMV_MF_ACCOUNT ? "account" : "define"),
376 name);
377 }else{
378 a_amv_mac_free(amp);
379 amp = (struct a_amv_mac*)-1;
381 break;
385 if(newamp != NULL){
386 ampp = &a_amv_macs[h];
387 newamp->am_next = *ampp;
388 *ampp = newamp;
389 amp = NULL;
391 jleave:
392 NYD2_LEAVE;
393 return amp;
396 static int
397 a_amv_mac_call(void *v, bool_t silent_nexist){
398 struct a_amv_mac *amp;
399 int rv;
400 char const *name;
401 struct n_cmd_arg_ctx *cacp;
402 NYD_ENTER;
404 cacp = v;
406 if(cacp->cac_no == 0){
407 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
408 n_pstate_err_no = n_ERR_INVAL;
409 rv = 1;
410 goto jleave;
413 name = cacp->cac_arg->ca_arg.ca_str.s;
415 if(n_UNLIKELY(cacp->cac_no > a_AMV_POSPAR_MAX)){
416 n_err(_("Too many arguments to macro `call': %s\n"), name);
417 n_pstate_err_no = n_ERR_OVERFLOW;
418 rv = 1;
419 }else if(n_UNLIKELY((amp = a_amv_mac_lookup(name, NULL, a_AMV_MF_NONE)
420 ) == NULL)){
421 if(!silent_nexist)
422 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name, FAL0));
423 n_pstate_err_no = n_ERR_NOENT;
424 rv = 1;
425 }else{
426 char const **argv;
427 struct a_amv_mac_call_args *amcap;
428 size_t argc;
430 argc = cacp->cac_no + 1;
431 amcap = n_lofi_alloc(sizeof *amcap + (argc * sizeof *argv));
432 argv = (void*)&amcap[1];
434 for(argc = 0; (cacp->cac_arg = cacp->cac_arg->ca_next) != NULL; ++argc)
435 argv[argc] = cacp->cac_arg->ca_arg.ca_str.s;
436 argv[argc] = NULL;
438 memset(amcap, 0, sizeof *amcap);
439 amcap->amca_name = name;
440 amcap->amca_amp = amp;
441 if(argc > 0){
442 amcap->amca_pospar.app_count = (ui16_t)argc;
443 amcap->amca_pospar.app_not_heap = TRU1;
444 amcap->amca_pospar.app_dat = argv;
447 (void)a_amv_mac_exec(amcap);
448 rv = n_pstate_ex_no;
450 jleave:
451 NYD_LEAVE;
452 return rv;
455 static bool_t
456 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
457 struct a_amv_lostack *losp;
458 struct a_amv_mac_line **amlp;
459 char **args_base, **args;
460 struct a_amv_mac *amp;
461 bool_t rv;
462 NYD2_ENTER;
464 amp = amcap->amca_amp;
465 assert(amp != NULL && amp != a_AMV_MACKY_MACK);
466 ++amp->am_refcnt;
467 /* XXX Unfortunately we yet need to dup the macro lines! :( */
468 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
469 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
470 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
471 *args = NULL;
473 losp = n_lofi_alloc(sizeof *losp);
474 losp->as_global_saved = a_amv_lopts;
475 if((losp->as_amcap = amcap)->amca_unroller == NULL){
476 losp->as_up = losp->as_global_saved;
477 losp->as_lopts = NULL;
478 }else{
479 losp->as_up = NULL;
480 losp->as_lopts = *amcap->amca_unroller;
482 losp->as_unroll = amcap->amca_lopts_on;
484 a_amv_lopts = losp;
485 if(amcap->amca_hook_pre != NULL)
486 n_PS_ROOT_BLOCK((*amcap->amca_hook_pre)(amcap->amca_hook_arg));
487 rv = n_go_macro((n_GO_INPUT_NONE |
488 (amcap->amca_no_xcall ? n_GO_INPUT_NO_XCALL : 0)),
489 amp->am_name, args_base, &a_amv_mac__finalize, losp);
490 NYD2_LEAVE;
491 return rv;
494 static void
495 a_amv_mac__finalize(void *vp){
496 struct a_amv_mac *amp;
497 struct a_amv_mac_call_args *amcap;
498 struct a_amv_lostack *losp;
499 NYD2_ENTER;
501 losp = vp;
502 a_amv_lopts = losp->as_global_saved;
504 amcap = losp->as_amcap;
506 if(!amcap->amca_pospar.app_not_heap && amcap->amca_pospar.app_maxcount > 0){
507 ui16_t i;
509 for(i = amcap->amca_pospar.app_maxcount; i-- != 0;)
510 n_free(n_UNCONST(amcap->amca_pospar.app_dat[i]));
511 n_free(amcap->amca_pospar.app_dat);
514 if(amcap->amca_unroller == NULL){
515 if(losp->as_lopts != NULL)
516 a_amv_lopts_unroll(&losp->as_lopts);
517 }else
518 *amcap->amca_unroller = losp->as_lopts;
520 if(amcap->amca_ps_hook_mask)
521 n_pstate &= ~n_PS_HOOK_MASK;
523 if((amp = amcap->amca_amp) != a_AMV_MACKY_MACK && amp != NULL &&
524 --amp->am_refcnt == 0 && (amp->am_flags & a_AMV_MF_DELETE))
525 a_amv_mac_free(amp);
527 n_lofi_free(losp);
528 n_lofi_free(amcap);
529 NYD2_LEAVE;
532 static bool_t
533 a_amv_mac_show(enum a_amv_mac_flags amf){
534 size_t lc, mc, ti, i;
535 char const *typestr;
536 FILE *fp;
537 bool_t rv;
538 NYD2_ENTER;
540 rv = FAL0;
542 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
543 NULL){
544 n_perr(_("Can't create temporary file for `define' or `account' listing"),
546 goto jleave;
549 amf &= a_AMV_MF_TYPE_MASK;
550 typestr = (amf & a_AMV_MF_ACCOUNT) ? "account" : "define";
552 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
553 struct a_amv_mac *amp;
555 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
556 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
557 struct a_amv_mac_line **amlpp;
559 if(++mc > 1){
560 putc('\n', fp);
561 ++lc;
563 ++lc;
564 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
565 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
566 for(i = (*amlpp)->aml_prespc; i > 0; --i)
567 putc(' ', fp);
568 fputs((*amlpp)->aml_dat, fp);
569 putc('\n', fp);
571 fputs("}\n", fp);
572 ++lc;
576 if(mc > 0)
577 page_or_print(fp, lc);
579 rv = (ferror(fp) == 0);
580 Fclose(fp);
581 jleave:
582 NYD2_LEAVE;
583 return rv;
586 static bool_t
587 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
588 struct str line;
589 ui32_t line_cnt, maxlen;
590 struct linelist{
591 struct linelist *ll_next;
592 struct a_amv_mac_line *ll_amlp;
593 } *llp, *ll_head, *ll_tail;
594 union {size_t s; int i; ui32_t ui; size_t l;} n;
595 struct a_amv_mac *amp;
596 bool_t rv;
597 NYD2_ENTER;
599 memset(&line, 0, sizeof line);
600 rv = FAL0;
601 amp = NULL;
603 /* TODO We should have our input state machine which emits Line events,
604 * TODO and hook different consumers dependent on our content, as stated
605 * TODO in i think lex_input; */
606 /* Read in the lines which form the macro content */
607 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
608 ui32_t leaspc;
609 char *cp;
611 n.i = n_go_input(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_NL_ESC, n_empty,
612 &line.s, &line.l, NULL, NULL);
613 if(n.ui == 0)
614 continue;
615 if(n.i < 0){
616 n_err(_("Unterminated %s definition: %s\n"),
617 (amf & a_AMV_MF_ACCOUNT ? "account" : "macro"), name);
618 goto jerr;
621 /* Trim WS, remember amount of leading spaces for display purposes */
622 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
623 if(*cp == '\t')
624 leaspc = (leaspc + 8u) & ~7u;
625 else if(*cp == ' ')
626 ++leaspc;
627 else
628 break;
629 for(; n.ui > 0 && spacechar(cp[n.ui - 1]); --n.ui)
631 if(n.ui == 0)
632 continue;
634 maxlen = n_MAX(maxlen, n.ui);
635 cp[n.ui++] = '\0';
637 /* Is is the closing brace? */
638 if(*cp == '}')
639 break;
641 if(n_LIKELY(++line_cnt < UI32_MAX)){
642 struct a_amv_mac_line *amlp;
644 llp = n_autorec_alloc(sizeof *llp);
645 if(ll_head == NULL)
646 ll_head = llp;
647 else
648 ll_tail->ll_next = llp;
649 ll_tail = llp;
650 llp->ll_next = NULL;
651 llp->ll_amlp = amlp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line,
652 aml_dat) + n.ui);
653 amlp->aml_len = n.ui -1;
654 amlp->aml_prespc = leaspc;
655 memcpy(amlp->aml_dat, cp, n.ui);
656 }else{
657 n_err(_("Too much content in %s definition: %s\n"),
658 (amf & a_AMV_MF_ACCOUNT ? "account" : "macro"), name);
659 goto jerr;
663 /* Create the new macro */
664 n.s = strlen(name) +1;
665 amp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac, am_name) + n.s);
666 amp->am_next = NULL;
667 amp->am_maxlen = maxlen;
668 amp->am_line_cnt = line_cnt;
669 amp->am_refcnt = 0;
670 amp->am_flags = amf;
671 amp->am_lopts = NULL;
672 memcpy(amp->am_name, name, n.s);
673 /* C99 */{
674 struct a_amv_mac_line **amlpp;
676 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
677 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
678 *amlpp++ = llp->ll_amlp;
679 *amlpp = NULL;
682 /* Create entry, replace a yet existing one as necessary */
683 a_amv_mac_lookup(name, amp, amf | a_AMV_MF_UNDEF);
684 rv = TRU1;
685 jleave:
686 if(line.s != NULL)
687 free(line.s);
688 NYD2_LEAVE;
689 return rv;
691 jerr:
692 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
693 free(llp->ll_amlp);
694 if(amp != NULL){
695 free(amp->am_line_dat);
696 free(amp);
698 goto jleave;
701 static bool_t
702 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
703 struct a_amv_mac *amp;
704 bool_t rv;
705 NYD2_ENTER;
707 rv = TRU1;
709 if(n_LIKELY(name[0] != '*' || name[1] != '\0')){
710 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
711 n_err(_("%s not defined: %s\n"),
712 (amf & a_AMV_MF_ACCOUNT ? "Account" : "Macro"), name);
713 rv = FAL0;
715 }else{
716 struct a_amv_mac **ampp, *lamp;
718 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
719 ++ampp)
720 for(lamp = NULL, amp = *ampp; amp != NULL;){
721 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
722 /* xxx Expensive but rare: be simple */
723 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
724 amp = (lamp == NULL) ? *ampp : lamp->am_next;
725 }else{
726 lamp = amp;
727 amp = amp->am_next;
731 NYD2_LEAVE;
732 return rv;
735 static void
736 a_amv_mac_free(struct a_amv_mac *amp){
737 struct a_amv_mac_line **amlpp;
738 NYD2_ENTER;
740 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
741 free(*amlpp);
742 free(amp->am_line_dat);
743 free(amp);
744 NYD2_LEAVE;
747 static void
748 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
749 struct a_amv_var *oavp){
750 struct a_amv_var *avp;
751 size_t nl, vl;
752 NYD2_ENTER;
754 /* Propagate unrolling up the stack, as necessary */
755 assert(alp != NULL);
756 for(;;){
757 if(alp->as_unroll)
758 break;
759 if((alp = alp->as_up) == NULL)
760 goto jleave;
763 /* Check whether this variable is handled yet */
764 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
765 if(!strcmp(avp->av_name, name))
766 goto jleave;
768 nl = strlen(name) +1;
769 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
770 avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name) + nl + vl);
771 avp->av_link = alp->as_lopts;
772 alp->as_lopts = avp;
773 memcpy(avp->av_name, name, nl);
774 if(vl == 0){
775 avp->av_value = NULL;
776 avp->av_flags = 0;
777 #ifdef HAVE_PUTENV
778 avp->av_env = NULL;
779 #endif
780 }else{
781 avp->av_value = &avp->av_name[nl];
782 avp->av_flags = oavp->av_flags;
783 memcpy(avp->av_value, oavp->av_value, vl);
784 #ifdef HAVE_PUTENV
785 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
786 #endif
788 jleave:
789 NYD2_LEAVE;
792 static void
793 a_amv_lopts_unroll(struct a_amv_var **avpp){
794 struct a_amv_lostack *save_alp;
795 struct a_amv_var *x, *avp;
796 NYD2_ENTER;
798 avp = *avpp;
799 *avpp = NULL;
801 save_alp = a_amv_lopts;
802 a_amv_lopts = NULL;
803 while(avp != NULL){
804 x = avp;
805 avp = avp->av_link;
806 n_PS_ROOT_BLOCK(n_var_vset(x->av_name, (uintptr_t)x->av_value));
807 free(x);
809 a_amv_lopts = save_alp;
810 NYD2_LEAVE;
813 static char *
814 a_amv_var_copy(char const *str){
815 char *news;
816 size_t len;
817 NYD2_ENTER;
819 if(*str == '\0')
820 news = n_UNCONST(n_empty);
821 else if(str[1] == '\0'){
822 if(str[0] == '1')
823 news = n_UNCONST(n_1);
824 else if(str[0] == '0')
825 news = n_UNCONST(n_0);
826 else
827 goto jheap;
828 }else if(str[2] == '\0' && str[0] == '-' && str[1] == '1')
829 news = n_UNCONST(n_m1);
830 else{
831 jheap:
832 len = strlen(str) +1;
833 news = smalloc(len);
834 memcpy(news, str, len);
836 NYD2_LEAVE;
837 return news;
840 static void
841 a_amv_var_free(char *cp){
842 NYD2_ENTER;
843 if(cp[0] != '\0' && cp != n_0 && cp != n_1 && cp != n_m1)
844 free(cp);
845 NYD2_LEAVE;
848 static bool_t
849 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm, enum okeys okey,
850 char const *val){
851 bool_t ok;
852 NYD2_ENTER;
854 ok = TRU1;
856 if(avvm == a_AMV_VIP_SET_PRE){
857 switch(okey){
858 default:
859 break;
860 case ok_v_HOME:
861 /* Note this gets called from main.c during initialization, and they
862 * simply set this to pw_dir as a fallback: don't verify _that_ call.
863 * See main.c! */
864 if(!(n_pstate & n_PS_ROOT) && !n_is_dir(val, TRU1)){
865 n_err(_("$HOME is not a directory or not accessible: %s\n"),
866 n_shexp_quote_cp(val, FAL0));
867 ok = FAL0;
868 break;
870 /* FALLTHRU */
871 case ok_v_TMPDIR:
872 if(!n_is_dir(val, TRU1)){
873 n_err(_("$TMPDIR is not a directory or not accessible: %s\n"),
874 n_shexp_quote_cp(val, FAL0));
875 ok = FAL0;
877 break;
878 case ok_v_umask:
879 if(*val != '\0'){
880 ui64_t uib;
882 n_idec_ui64_cp(&uib, val, 0, NULL);
883 if(uib & ~0777u){ /* (is valid _VF_POSNUM) */
884 n_err(_("Invalid *umask* setting: %s\n"), val);
885 ok = FAL0;
888 break;
890 }else if(avvm == a_AMV_VIP_SET_POST){
891 switch(okey){
892 default:
893 break;
894 case ok_b_debug:
895 n_poption |= n_PO_DEBUG;
896 break;
897 case ok_v_HOME:
898 /* Invalidate any resolved folder then, too
899 * FALLTHRU */
900 case ok_v_folder:
901 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved));
902 break;
903 case ok_v_ifs:{
904 char *x_b, *x, c;
906 x_b = x = n_autorec_alloc(strlen(val) +1);
907 while((c = *val++) != '\0')
908 if(spacechar(c))
909 *x++ = c;
910 *x = '\0';
911 n_PS_ROOT_BLOCK(ok_vset(ifs_ws, x_b));
912 } break;
913 #ifdef HAVE_SETLOCALE
914 case ok_v_LANG:
915 case ok_v_LC_ALL:
916 case ok_v_LC_CTYPE:
917 n_locale_init();
918 break;
919 #endif
920 case ok_b_memdebug:
921 n_poption |= n_PO_MEMDEBUG;
922 break;
923 case ok_b_POSIXLY_CORRECT: /* <-> *posix* */
924 if(!(n_pstate & n_PS_ROOT))
925 n_PS_ROOT_BLOCK(ok_bset(posix));
926 break;
927 case ok_b_posix: /* <-> $POSIXLY_CORRECT */
928 if(!(n_pstate & n_PS_ROOT))
929 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT));
930 break;
931 case ok_b_skipemptybody:
932 n_poption |= n_PO_E_FLAG;
933 break;
934 case ok_b_typescript_mode:
935 ok_bset(colour_disable);
936 ok_bset(line_editor_disable);
937 if(!(n_psonce & n_PSO_STARTED))
938 ok_bset(termcap_disable);
939 break;
940 case ok_v_umask:
941 if(*val != '\0'){
942 ui64_t uib;
944 n_idec_ui64_cp(&uib, val, 0, NULL);
945 umask((mode_t)uib);
947 break;
948 case ok_b_verbose:
949 n_poption |= (n_poption & n_PO_VERB) ? n_PO_VERBVERB : n_PO_VERB;
950 break;
952 }else{
953 switch(okey){
954 default:
955 break;
956 case ok_b_debug:
957 n_poption &= ~n_PO_DEBUG;
958 break;
959 case ok_v_HOME:
960 /* Invalidate any resolved folder then, too
961 * FALLTHRU */
962 case ok_v_folder:
963 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved));
964 break;
965 case ok_b_memdebug:
966 n_poption &= ~n_PO_MEMDEBUG;
967 break;
968 case ok_b_POSIXLY_CORRECT: /* <-> *posix* */
969 if(!(n_pstate & n_PS_ROOT))
970 n_PS_ROOT_BLOCK(ok_bclear(posix));
971 break;
972 case ok_b_posix: /* <-> $POSIXLY_CORRECT */
973 if(!(n_pstate & n_PS_ROOT))
974 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT));
975 break;
976 case ok_b_skipemptybody:
977 n_poption &= ~n_PO_E_FLAG;
978 break;
979 case ok_b_verbose:
980 n_poption &= ~(n_PO_VERB | n_PO_VERBVERB);
981 break;
984 NYD2_LEAVE;
985 return ok;
988 static bool_t
989 a_amv_var_check_nocntrls(char const *val){
990 char c;
991 NYD2_ENTER;
993 while((c = *val++) != '\0')
994 if(cntrlchar(c))
995 break;
996 NYD2_LEAVE;
997 return (c == '\0');
1000 static bool_t
1001 a_amv_var_check_num(char const *val, bool_t posnum){
1002 /* TODO The internal/environment variables which are num= or posnum= should
1003 * TODO gain special lookup functions, or the return should be void* and
1004 * TODO castable to integer; i.e. no more strtoX() should be needed.
1005 * TODO I.e., the result of this function should instead be stored */
1006 bool_t rv;
1007 NYD2_ENTER;
1009 rv = TRU1;
1011 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1012 ui64_t uib;
1013 enum n_idec_state ids;
1015 ids = n_idec_cp(&uib, val, 0,
1016 (posnum ? n_IDEC_MODE_SIGNED_TYPE : n_IDEC_MODE_NONE), NULL);
1017 if((ids & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1018 ) != n_IDEC_STATE_CONSUMED)
1019 rv = FAL0;
1020 /* TODO Unless we store integers we need to look and forbid, because
1021 * TODO callee may not be able to swallow, e.g., "-1" */
1022 if(posnum && (ids & n_IDEC_STATE_SEEN_MINUS))
1023 rv = FAL0;
1025 NYD2_LEAVE;
1026 return rv;
1029 static char const *
1030 a_amv_var_canonify(char const *vn){
1031 NYD2_ENTER;
1032 if(!upperchar(*vn)){
1033 char const *vp;
1035 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
1037 vn = (*vp == '@') ? i_strdup(vn) : vn;
1039 NYD2_LEAVE;
1040 return vn;
1043 static bool_t
1044 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
1045 ui32_t hash, i, j;
1046 struct a_amv_var_map const *avmp;
1047 char c;
1048 NYD2_ENTER;
1050 /* It may be a special a.k.a. macro-local or one-letter parameter */
1051 c = name[0];
1052 if(n_UNLIKELY(digitchar(c))){
1053 /* (Inline dec. atoi, ugh) */
1054 for(j = (ui8_t)c - '0', i = 1;; ++i){
1055 c = name[i];
1056 if(c == '\0')
1057 break;
1058 if(!digitchar(c))
1059 goto jno_special_param;
1060 j = j * 10 + (ui8_t)c - '0';
1062 if(j <= a_AMV_POSPAR_MAX){
1063 avcp->avc_special_cat = a_AMV_VSC_POSPAR;
1064 goto jspecial_param;
1066 }else if(n_UNLIKELY(name[1] == '\0')){
1067 switch(c){
1068 case '?':
1069 case '!':
1070 avcp->avc_special_cat = a_AMV_VSC_GLOBAL;
1071 j = (c == '?') ? a_AMV_VST_QM : a_AMV_VST_EM;
1072 goto jspecial_param;
1073 case '^':
1074 goto jmultiplex;
1075 case '*':
1076 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1077 j = a_AMV_VST_STAR;
1078 goto jspecial_param;
1079 case '@':
1080 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1081 j = a_AMV_VST_AT;
1082 goto jspecial_param;
1083 case '#':
1084 avcp->avc_special_cat = a_AMV_VSC_POSPAR_ENV;
1085 j = a_AMV_VST_NOSIGN;
1086 goto jspecial_param;
1087 default:
1088 break;
1090 }else if(c == '^'){
1091 jmultiplex:
1092 avcp->avc_special_cat = a_AMV_VSC_MULTIPLEX;
1093 j = a_AMV_VST_CACC;
1094 goto jspecial_param;
1097 /* Normal reverse lookup, walk over the hashtable */
1098 jno_special_param:
1099 avcp->avc_special_cat = a_AMV_VSC_NONE;
1100 avcp->avc_name = name = a_amv_var_canonify(name);
1101 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
1103 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
1104 ui32_t x;
1106 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
1107 break;
1109 avmp = &a_amv_var_map[x];
1110 if(avmp->avm_hash == hash &&
1111 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
1112 avcp->avc_map = avmp;
1113 avcp->avc_okey = (enum okeys)x;
1114 goto jleave;
1117 if(++i == a_AMV_VAR_REV_PRIME){
1118 #ifdef a_AMV_VAR_REV_WRAPAROUND
1119 i = 0;
1120 #else
1121 break;
1122 #endif
1125 avcp->avc_map = NULL;
1126 avcp = NULL;
1127 jleave:
1128 NYD2_LEAVE;
1129 return (avcp != NULL);
1131 /* All these are mapped to *--special-param* */
1132 jspecial_param:
1133 avcp->avc_name = name;
1134 avcp->avc_special_prop = (ui16_t)j;
1135 avmp = &a_amv_var_map[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX];
1136 avcp->avc_hash = avmp->avm_hash;
1137 avcp->avc_map = avmp;
1138 avcp->avc_okey = ok_v___special_param;
1139 goto jleave;
1142 static bool_t
1143 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
1144 size_t i;
1145 char const *cp;
1146 struct a_amv_var_map const *avmp;
1147 struct a_amv_var *avp;
1148 NYD2_ENTER;
1150 /* C99 */{
1151 struct a_amv_var **avpp, *lavp;
1153 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
1155 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
1156 if(!strcmp(avp->av_name, avcp->avc_name)){
1157 /* Relink as head, hope it "sorts on usage" over time.
1158 * _clear() relies on this behaviour */
1159 if(lavp != NULL){
1160 lavp->av_link = avp->av_link;
1161 avp->av_link = *avpp;
1162 *avpp = avp;
1164 goto jleave;
1168 /* If this is not an assembled variable we need to consider some special
1169 * initialization cases and eventually create the variable anew */
1170 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1171 /* Does it have an import-from-environment flag? */
1172 if(n_UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
1173 if(n_LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
1174 /* May be better not to use that one, though? */
1175 bool_t isempty, isbltin;
1177 isempty = (*cp == '\0' &&
1178 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
1179 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
1180 ) != 0);
1182 if(n_UNLIKELY(isempty)){
1183 if(!isbltin)
1184 goto jerr;
1185 }else if(n_LIKELY(*cp != '\0')){
1186 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
1187 !a_amv_var_check_nocntrls(cp))){
1188 n_err(_("Ignoring environment, control characters "
1189 "invalid in variable: %s\n"), avcp->avc_name);
1190 goto jerr;
1192 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1193 !a_amv_var_check_num(cp, FAL0))){
1194 n_err(_("Environment variable value not a number "
1195 "or out of range: %s\n"), avcp->avc_name);
1196 goto jerr;
1198 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1199 !a_amv_var_check_num(cp, TRU1))){
1200 n_err(_("Environment variable value not a number, "
1201 "negative or out of range: %s\n"), avcp->avc_name);
1202 goto jerr;
1204 goto jnewval;
1205 }else
1206 goto jnewval;
1210 /* A first-time init switch is to be handled now and here */
1211 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
1212 static struct a_amv_var_defval const **arr,
1213 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
1215 if(arr == NULL){
1216 arr = &arr_base[0];
1217 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1218 while(i-- > 0)
1219 arr[i] = &a_amv_var_i3vals[i];
1222 for(i = 0; arr[i] != NULL; ++i)
1223 if(arr[i]->avdv_okey == avcp->avc_okey){
1224 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1225 : arr[i]->avdv_value;
1226 /* Remove this entry, hope entire block becomes no-op asap */
1228 arr[i] = arr[i + 1];
1229 while(arr[i++] != NULL);
1231 if(!i3val_nonew)
1232 goto jnewval;
1233 if(i3val_nonew == TRUM1)
1234 avp = (struct a_amv_var*)-1;
1235 goto jleave;
1239 /* The virtual variables */
1240 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
1241 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
1242 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
1243 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
1244 goto jleave;
1246 /* Not reached */
1249 /* Place this last because once it is set first the variable will never
1250 * be removed again and thus match in the first block above */
1251 jdefval:
1252 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
1253 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
1254 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
1255 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1256 : a_amv_var_defvals[i].avdv_value;
1257 goto jnewval;
1262 jerr:
1263 avp = NULL;
1264 jleave:
1265 avcp->avc_var = avp;
1266 NYD2_LEAVE;
1267 return (avp != NULL);
1269 jnewval:
1270 /* E.g., $TMPDIR may be set to non-existent, so we need to be able to catch
1271 * that and redirect to a possible default value */
1272 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1273 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE, avcp->avc_okey, cp)){
1274 #ifdef HAVE_SETENV
1275 if(avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV))
1276 unsetenv(avcp->avc_name);
1277 #endif
1278 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0)
1279 goto jdefval;
1280 goto jerr;
1281 }else{
1282 struct a_amv_var **avpp;
1283 size_t l;
1285 l = strlen(avcp->avc_name) +1;
1286 avcp->avc_var =
1287 avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name) + l);
1288 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1289 *avpp = avp;
1290 memcpy(avp->av_name, avcp->avc_name, l);
1291 #ifdef HAVE_PUTENV
1292 avp->av_env = NULL;
1293 #endif
1294 avp->av_flags = avmp->avm_flags;
1295 avp->av_value = a_amv_var_copy(cp);
1297 if(avp->av_flags & a_AMV_VF_ENV)
1298 a_amv_var__putenv(avcp, avp);
1299 if(avmp->avm_flags & a_AMV_VF_VIP)
1300 a_amv_var_check_vips(a_AMV_VIP_SET_POST, avcp->avc_okey, cp);
1301 goto jleave;
1305 static char const *
1306 a_amv_var_vsc_global(struct a_amv_var_carrier *avcp){
1307 char itoabuf[32];
1308 char const *rv;
1309 si32_t *ep;
1310 struct a_amv_var_map const *avmp;
1311 NYD2_ENTER;
1313 /* Not function local, TODO but lazy evaluted for now */
1314 if(avcp->avc_special_prop == a_AMV_VST_QM){
1315 avmp = &a_amv_var_map[a_AMV_VAR__QM_MAP_IDX];
1316 avcp->avc_okey = ok_v___qm;
1317 ep = &n_pstate_ex_no;
1318 }else{
1319 avmp = &a_amv_var_map[a_AMV_VAR__EM_MAP_IDX];
1320 avcp->avc_okey = ok_v___em;
1321 ep = &n_pstate_err_no;
1324 /* XXX Oh heaven, we are responsible to ensure that $?/! is up-to-date
1325 * TODO we could num=1 ok_v___[qe]m, but the thing is still a string
1326 * TODO and thus conversion takes places over and over again; also
1327 * TODO for now that would have to occur before we set _that_ value
1328 * TODO so let's special treat it until we store ints as such */
1329 switch(*ep){
1330 case 0: rv = n_0; break;
1331 case 1: rv = n_1; break;
1332 default:
1333 snprintf(itoabuf, sizeof itoabuf, "%d", *ep);
1334 rv = itoabuf;
1335 break;
1337 n_PS_ROOT_BLOCK(n_var_okset(avcp->avc_okey, (uintptr_t)rv));
1339 avcp->avc_hash = avmp->avm_hash;
1340 avcp->avc_map = avmp;
1341 rv = a_amv_var_lookup(avcp, TRU1) ? avcp->avc_var->av_value : NULL;
1342 NYD2_LEAVE;
1343 return rv;
1346 static char const *
1347 a_amv_var_vsc_multiplex(struct a_amv_var_carrier *avcp){
1348 char itoabuf[32];
1349 si32_t e;
1350 size_t i;
1351 char const *rv;
1352 NYD2_ENTER;
1354 i = strlen(rv = &avcp->avc_name[1]);
1356 /* ERR, ERRDOC, ERRNAME, plus *-NAME variants */
1357 if(rv[0] == 'E' && i >= 3 && rv[1] == 'R' && rv[2] == 'R'){
1358 if(i == 3){
1359 e = n_pstate_err_no;
1360 goto jeno;
1361 }else if(rv[3] == '-'){
1362 e = n_err_from_name(&rv[4]);
1363 jeno:
1364 switch(e){
1365 case 0: rv = n_0; break;
1366 case 1: rv = n_1; break;
1367 default:
1368 snprintf(itoabuf, sizeof itoabuf, "%d", e);
1369 rv = savestr(itoabuf); /* XXX yet, cannot do numbers */
1370 break;
1372 goto jleave;
1373 }else if(i >= 6){
1374 if(!memcmp(&rv[3], "DOC", 3)){
1375 rv += 6;
1376 switch(*rv){
1377 case '\0': e = n_pstate_err_no; break;
1378 case '-': e = n_err_from_name(&rv[1]); break;
1379 default: goto jerr;
1381 rv = n_err_to_doc(e);
1382 goto jleave;
1383 }else if(i >= 7 && !memcmp(&rv[3], "NAME", 4)){
1384 rv += 7;
1385 switch(*rv){
1386 case '\0': e = n_pstate_err_no; break;
1387 case '-': e = n_err_from_name(&rv[1]); break;
1388 default: goto jerr;
1390 rv = n_err_to_name(e);
1391 goto jleave;
1396 jerr:
1397 rv = NULL;
1398 jleave:
1399 NYD2_LEAVE;
1400 return rv;
1403 static char const *
1404 a_amv_var_vsc_pospar(struct a_amv_var_carrier *avcp){
1405 size_t i, j;
1406 ui16_t argc;
1407 char const *rv, **argv;
1408 NYD2_ENTER;
1410 rv = NULL;
1412 /* If in a macro/xy.. */
1413 if(a_amv_lopts != NULL){
1414 bool_t ismacky;
1415 struct a_amv_mac_call_args *amcap;
1417 amcap = a_amv_lopts->as_amcap;
1418 argc = amcap->amca_pospar.app_count;
1419 argv = amcap->amca_pospar.app_dat;
1420 argv += amcap->amca_pospar.app_idx;
1422 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
1423 if(!(ismacky = (amcap->amca_amp == a_AMV_MACKY_MACK)) &&
1424 (amcap->amca_ps_hook_mask ||
1425 (assert(amcap->amca_amp != NULL),
1426 (amcap->amca_amp->am_flags & a_AMV_MF_TYPE_MASK
1427 ) == a_AMV_MF_ACCOUNT)))
1428 goto jleave;
1430 if(avcp->avc_special_cat == a_AMV_VSC_POSPAR){
1431 if(avcp->avc_special_prop > 0){
1432 if(argc >= avcp->avc_special_prop)
1433 rv = argv[avcp->avc_special_prop - 1];
1434 }else if(ismacky)
1435 rv = amcap->amca_name;
1436 else
1437 rv = (a_amv_lopts->as_up != NULL
1438 ? a_amv_lopts->as_up->as_amcap->amca_name : n_empty);
1439 goto jleave;
1441 /* MACKY_MACK doesn't know about [*@#] */
1442 /*else*/ if(ismacky){
1443 if(n_poption & n_PO_D_V)
1444 n_err(_("Cannot use $*/$@/$# in this context: %s\n"),
1445 n_shexp_quote_cp(avcp->avc_name, FAL0));
1446 goto jleave;
1448 }else{
1449 argc = a_amv_pospar.app_count;
1450 argv = a_amv_pospar.app_dat;
1451 argv += a_amv_pospar.app_idx;
1453 if(avcp->avc_special_cat == a_AMV_VSC_POSPAR){
1454 if(avcp->avc_special_prop > 0){
1455 if(argc >= avcp->avc_special_prop)
1456 rv = argv[avcp->avc_special_prop - 1];
1457 }else
1458 rv = n_progname;
1459 goto jleave;
1463 switch(avcp->avc_special_prop){ /* XXX OPTIMIZE */
1464 case a_AMV_VST_STAR:
1465 case a_AMV_VST_AT:
1466 for(i = j = 0; i < argc; ++i)
1467 j += strlen(argv[i]) + 1;
1468 if(j == 0)
1469 rv = n_empty;
1470 else{
1471 char *cp;
1473 rv = cp = n_autorec_alloc(j);
1474 for(i = j = 0; i < argc; ++i){
1475 j = strlen(argv[i]);
1476 memcpy(cp, argv[i], j);
1477 cp += j;
1478 *cp++ = ' ';
1480 *--cp = '\0';
1482 break;
1483 case a_AMV_VST_NOSIGN:{
1484 char *cp;
1486 rv = cp = n_autorec_alloc(sizeof("65535"));
1487 snprintf(cp, sizeof("65535"), "%hu", argc);
1488 } break;
1489 default:
1490 rv = n_empty;
1491 break;
1493 jleave:
1494 NYD2_LEAVE;
1495 return rv;
1498 static bool_t
1499 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1500 bool_t force_env){
1501 struct a_amv_var *avp;
1502 char *oval;
1503 struct a_amv_var_map const *avmp;
1504 bool_t rv;
1505 NYD2_ENTER;
1507 if(value == NULL){
1508 rv = a_amv_var_clear(avcp, force_env);
1509 goto jleave;
1512 if((avmp = avcp->avc_map) != NULL){
1513 rv = FAL0;
1515 /* Validity checks */
1516 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1517 !(n_pstate & n_PS_ROOT))){
1518 value = N_("Variable is read-only: %s\n");
1519 goto jeavmp;
1521 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1522 value = N_("Variable must not be empty: %s\n");
1523 goto jeavmp;
1525 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1526 !a_amv_var_check_nocntrls(value))){
1527 value = N_("Variable forbids control characters: %s\n");
1528 goto jeavmp;
1530 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1531 !a_amv_var_check_num(value, FAL0))){
1532 value = N_("Variable value not a number or out of range: %s\n");
1533 goto jeavmp;
1535 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1536 !a_amv_var_check_num(value, TRU1))){
1537 value = _("Variable value not a number, negative, "
1538 "or out of range: %s\n");
1539 goto jeavmp;
1541 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1542 !(n_psonce & n_PSO_STARTED) && !(n_pstate & n_PS_ROOT))){
1543 value = N_("Variable cannot be set in a resource file: %s\n");
1544 goto jeavmp;
1547 /* Any more complicated inter-dependency? */
1548 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIP) != 0 &&
1549 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE, avcp->avc_okey, value))){
1550 value = N_("Assignment of variable aborted: %s\n");
1551 jeavmp:
1552 n_err(V_(value), avcp->avc_name);
1553 goto jleave;
1556 /* Transformations */
1557 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_LOWER)){
1558 char c;
1560 oval = savestr(value);
1561 value = oval;
1562 for(; (c = *oval) != '\0'; ++oval)
1563 *oval = lowerconv(c);
1567 rv = TRU1;
1568 a_amv_var_lookup(avcp, TRU1);
1570 /* Don't care what happens later on, store this in the unroll list */
1571 if(a_amv_lopts != NULL &&
1572 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1573 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1575 if((avp = avcp->avc_var) == NULL){
1576 struct a_amv_var **avpp;
1577 size_t l;
1579 l = strlen(avcp->avc_name) +1;
1580 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1581 ) + l);
1582 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1583 *avpp = avp;
1584 #ifdef HAVE_PUTENV
1585 avp->av_env = NULL;
1586 #endif
1587 memcpy(avp->av_name, avcp->avc_name, l);
1588 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1589 oval = n_UNCONST(n_empty);
1590 }else
1591 oval = avp->av_value;
1593 if(avmp == NULL)
1594 avp->av_value = a_amv_var_copy(value);
1595 else{
1596 /* Via `set' etc. the user may give even boolean options non-boolean
1597 * values, ignore that and force boolean */
1598 if(avp->av_flags & a_AMV_VF_BOOL){
1599 if(!(n_pstate & n_PS_ROOT) && (n_poption & n_PO_D_VV) &&
1600 *value != '\0')
1601 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1602 avcp->avc_name, value);
1603 avp->av_value = n_UNCONST(n_1);
1604 }else
1605 avp->av_value = a_amv_var_copy(value);
1608 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1609 avp->av_flags |= a_AMV_VF_LINKED;
1610 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1611 rv = a_amv_var__putenv(avcp, avp);
1612 if(avp->av_flags & a_AMV_VF_VIP)
1613 a_amv_var_check_vips(a_AMV_VIP_SET_POST, avcp->avc_okey, value);
1614 a_amv_var_free(oval);
1615 jleave:
1616 NYD2_LEAVE;
1617 return rv;
1620 static bool_t
1621 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1622 #ifndef HAVE_SETENV
1623 char *cp;
1624 #endif
1625 bool_t rv;
1626 NYD2_ENTER;
1628 #ifdef HAVE_SETENV
1629 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1630 #else
1631 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1633 if((rv = (putenv(cp) == 0))){
1634 char *ocp;
1636 if((ocp = avp->av_env) != NULL)
1637 free(ocp);
1638 avp->av_env = cp;
1639 }else
1640 free(cp);
1641 #endif
1642 NYD2_LEAVE;
1643 return rv;
1646 static bool_t
1647 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1648 struct a_amv_var **avpp, *avp;
1649 struct a_amv_var_map const *avmp;
1650 bool_t rv;
1651 NYD2_ENTER;
1653 rv = FAL0;
1655 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1656 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1657 !(n_pstate & n_PS_ROOT))){
1658 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1659 goto jleave;
1661 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIP) != 0 &&
1662 !a_amv_var_check_vips(a_AMV_VIP_CLEAR, avcp->avc_okey, NULL))){
1663 n_err(_("Clearance of variable aborted: %s\n"), avcp->avc_name);
1664 goto jleave;
1668 rv = TRU1;
1670 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1671 if(force_env){
1672 jforce_env:
1673 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1674 }else if(!(n_pstate & (n_PS_ROOT | n_PS_ROBOT)) && (n_poption & n_PO_D_V))
1675 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1676 goto jleave;
1677 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1678 avcp->avc_var = NULL;
1679 if(force_env)
1680 goto jforce_env;
1681 goto jleave;
1684 if(a_amv_lopts != NULL &&
1685 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1686 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1688 avp = avcp->avc_var;
1689 avcp->avc_var = NULL;
1690 avpp = &a_amv_vars[avcp->avc_prime];
1691 assert(*avpp == avp); /* (always listhead after lookup()) */
1692 *avpp = (*avpp)->av_link;
1694 /* C99 */{
1695 char *envval;
1697 #ifdef HAVE_SETENV
1698 envval = NULL;
1699 #else
1700 envval = avp->av_env;
1701 #endif
1702 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1703 rv = a_amv_var__clearenv(avp->av_name, envval);
1705 a_amv_var_free(avp->av_value);
1706 free(avp);
1708 /* XXX Fun part, extremely simple-minded for now: if this variable has
1709 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1710 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1711 a_amv_var_lookup(avcp, TRU1);
1712 jleave:
1713 NYD2_LEAVE;
1714 return rv;
1717 static bool_t
1718 a_amv_var__clearenv(char const *name, char *value){
1719 #ifndef HAVE_SETENV
1720 extern char **environ;
1721 char **ecpp;
1722 #endif
1723 bool_t rv;
1724 NYD2_ENTER;
1725 n_UNUSED(value);
1727 #ifdef HAVE_SETENV
1728 unsetenv(name);
1729 rv = TRU1;
1730 #else
1731 if(value != NULL)
1732 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1733 if(*ecpp == value){
1734 free(value);
1736 ecpp[0] = ecpp[1];
1737 while(*ecpp++ != NULL);
1738 break;
1740 rv = TRU1;
1741 #endif
1742 NYD2_LEAVE;
1743 return rv;
1746 static void
1747 a_amv_var_show_all(void){
1748 struct n_string msg, *msgp;
1749 FILE *fp;
1750 size_t no, i;
1751 struct a_amv_var *avp;
1752 char const **vacp, **cap;
1753 NYD2_ENTER;
1755 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1756 n_perr(_("Can't create temporary file for `set' listing"), 0);
1757 goto jleave;
1760 /* We need to instantiate first-time-inits and default values here, so that
1761 * they will be regular members of our _vars[] table */
1762 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1763 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1764 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1765 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1767 for(no = i = 0; i < a_AMV_PRIME; ++i)
1768 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1769 ++no;
1770 no += a_AMV_VAR_VIRTS_CNT;
1772 vacp = n_autorec_alloc(no * sizeof(*vacp));
1774 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1775 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1776 *cap++ = avp->av_name;
1777 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1778 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1780 if(no > 1)
1781 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1783 msgp = &msg;
1784 msgp = n_string_reserve(n_string_creat(msgp), 80);
1785 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1786 i += a_amv_var_show(*cap, fp, msgp);
1787 n_string_gut(&msg);
1789 page_or_print(fp, i);
1790 Fclose(fp);
1791 jleave:
1792 NYD2_LEAVE;
1795 static int
1796 a_amv_var__show_cmp(void const *s1, void const *s2){
1797 int rv;
1798 NYD2_ENTER;
1800 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1801 NYD2_LEAVE;
1802 return rv;
1805 static size_t
1806 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1807 struct a_amv_var_carrier avc;
1808 char const *quote;
1809 bool_t isset;
1810 size_t i;
1811 NYD2_ENTER;
1813 msgp = n_string_trunc(msgp, 0);
1814 i = 0;
1816 a_amv_var_revlookup(&avc, name);
1817 isset = a_amv_var_lookup(&avc, FAL0);
1819 if(n_poption & n_PO_D_V){
1820 if(avc.avc_map == NULL){
1821 msgp = n_string_push_cp(msgp, "#assembled variable with value");
1822 i = 1;
1823 }else{
1824 struct{
1825 ui16_t flag;
1826 char msg[22];
1827 } const tbase[] = {
1828 {a_AMV_VF_VIRT, "virtual"},
1829 {a_AMV_VF_RDONLY, "read-only"},
1830 {a_AMV_VF_NODEL, "nodelete"},
1831 {a_AMV_VF_NOTEMPTY, "notempty"},
1832 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1833 {a_AMV_VF_NUM, "number"},
1834 {a_AMV_VF_POSNUM, "positive-number"},
1835 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1836 {a_AMV_VF_ENV, "sync-environ"},
1837 {a_AMV_VF_I3VAL, "initial-value"},
1838 {a_AMV_VF_DEFVAL, "default-value"},
1839 {a_AMV_VF_LINKED, "`environ' link"}
1840 }, *tp;
1842 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1843 if(isset ? (avc.avc_var->av_flags & tp->flag)
1844 : (avc.avc_map->avm_flags & tp->flag)){
1845 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1846 msgp = n_string_push_cp(msgp, tp->msg);
1849 if(i > 0)
1850 msgp = n_string_push_cp(msgp, "\n ");
1853 if(!isset || (avc.avc_var->av_flags & a_AMV_VF_RDONLY)){
1854 msgp = n_string_push_c(msgp, *n_ns);
1855 if(!isset){
1856 if(avc.avc_map != NULL && (avc.avc_map->avm_flags & a_AMV_VF_BOOL))
1857 msgp = n_string_push_cp(msgp, "boolean; ");
1858 msgp = n_string_push_cp(msgp, "variable not set: ");
1859 msgp = n_string_push_cp(msgp, n_shexp_quote_cp(name, FAL0));
1860 goto jleave;
1864 n_UNINIT(quote, NULL);
1865 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1866 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1867 if(strcmp(quote, avc.avc_var->av_value))
1868 msgp = n_string_push_cp(msgp, "wysh ");
1869 }else if(n_poption & n_PO_D_V)
1870 msgp = n_string_push_cp(msgp, "wysh ");
1871 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1872 msgp = n_string_push_cp(msgp, "environ ");
1873 msgp = n_string_push_cp(msgp, "set ");
1874 msgp = n_string_push_cp(msgp, name);
1875 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1876 msgp = n_string_push_c(msgp, '=');
1877 msgp = n_string_push_cp(msgp, quote);
1878 }else if(n_poption & n_PO_D_V)
1879 msgp = n_string_push_cp(msgp, " #boolean");
1881 jleave:
1882 msgp = n_string_push_c(msgp, '\n');
1883 fputs(n_string_cp(msgp), fp);
1884 NYD2_ENTER;
1885 return (i > 0 ? 2 : 1);
1888 static bool_t
1889 a_amv_var_c_set(char **ap, bool_t issetenv){
1890 char *cp, *cp2, *varbuf, c;
1891 size_t errs;
1892 NYD2_ENTER;
1894 errs = 0;
1895 jouter:
1896 while((cp = *ap++) != NULL){
1897 /* Isolate key */
1898 cp2 = varbuf = n_autorec_alloc(strlen(cp) +1);
1900 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1901 if(cntrlchar(c) || spacechar(c)){
1902 n_err(_("Variable name with control character ignored: %s\n"),
1903 ap[-1]);
1904 ++errs;
1905 goto jouter;
1907 *cp2++ = c;
1909 *cp2 = '\0';
1910 if(c == '\0')
1911 cp = n_UNCONST(n_empty);
1912 else
1913 ++cp;
1915 if(varbuf == cp2){
1916 n_err(_("Empty variable name ignored\n"));
1917 ++errs;
1918 }else{
1919 struct a_amv_var_carrier avc;
1920 bool_t isunset;
1922 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1923 varbuf = &varbuf[2];
1925 a_amv_var_revlookup(&avc, varbuf);
1927 if(isunset)
1928 errs += !a_amv_var_clear(&avc, issetenv);
1929 else
1930 errs += !a_amv_var_set(&avc, cp, issetenv);
1933 NYD2_LEAVE;
1934 return (errs == 0);
1937 FL int
1938 c_define(void *v){
1939 int rv;
1940 char **args;
1941 NYD_ENTER;
1943 rv = 1;
1945 if((args = v)[0] == NULL){
1946 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1947 goto jleave;
1950 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1951 args[2] != NULL){
1952 n_err(_("Synopsis: define: <name> {\n"));
1953 goto jleave;
1956 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1957 jleave:
1958 NYD_LEAVE;
1959 return rv;
1962 FL int
1963 c_undefine(void *v){
1964 int rv;
1965 char **args;
1966 NYD_ENTER;
1968 rv = 0;
1969 args = v;
1971 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1972 while(*++args != NULL);
1973 NYD_LEAVE;
1974 return rv;
1977 FL int
1978 c_call(void *vp){
1979 int rv;
1980 NYD_ENTER;
1982 rv = a_amv_mac_call(vp, FAL0);
1983 NYD_LEAVE;
1984 return rv;
1987 FL int
1988 c_call_if(void *vp){
1989 int rv;
1990 NYD_ENTER;
1992 rv = a_amv_mac_call(vp, TRU1);
1993 NYD_LEAVE;
1994 return rv;
1997 FL int
1998 c_account(void *v){
1999 struct a_amv_mac_call_args *amcap;
2000 struct a_amv_mac *amp;
2001 char **args;
2002 int rv, i, oqf, nqf;
2003 NYD_ENTER;
2005 rv = 1;
2007 if((args = v)[0] == NULL){
2008 rv = (a_amv_mac_show(a_AMV_MF_ACCOUNT) == FAL0);
2009 goto jleave;
2012 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
2013 if(args[2] != NULL){
2014 n_err(_("Synopsis: account: <name> {\n"));
2015 goto jleave;
2017 if(!asccasecmp(args[0], ACCOUNT_NULL)){
2018 n_err(_("`account': cannot use reserved name: %s\n"),
2019 ACCOUNT_NULL);
2020 goto jleave;
2022 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACCOUNT) == FAL0);
2023 goto jleave;
2026 if(n_pstate & n_PS_HOOK_MASK){
2027 n_err(_("`account': can't change account from within a hook\n"));
2028 goto jleave;
2031 save_mbox_for_possible_quitstuff();
2033 amp = NULL;
2034 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
2035 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACCOUNT)) == NULL){
2036 n_err(_("`account': account does not exist: %s\n"), args[0]);
2037 goto jleave;
2040 oqf = savequitflags();
2042 if(a_amv_acc_curr != NULL){
2043 if(a_amv_acc_curr->am_lopts != NULL)
2044 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
2045 /* For accounts this lingers */
2046 --a_amv_acc_curr->am_refcnt;
2047 if(a_amv_acc_curr->am_flags & a_AMV_MF_DELETE)
2048 a_amv_mac_free(a_amv_acc_curr);
2051 a_amv_acc_curr = amp;
2053 if(amp != NULL){
2054 assert(amp->am_lopts == NULL);
2055 amcap = n_lofi_alloc(sizeof *amcap);
2056 memset(amcap, 0, sizeof *amcap);
2057 amcap->amca_name = amp->am_name;
2058 amcap->amca_amp = amp;
2059 amcap->amca_unroller = &amp->am_lopts;
2060 amcap->amca_lopts_on = TRU1;
2061 amcap->amca_no_xcall = TRU1;
2062 ++amp->am_refcnt; /* We may not run 0 to avoid being deleted! */
2063 if(!a_amv_mac_exec(amcap)){
2064 /* XXX account switch incomplete, unroll? */
2065 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
2066 goto jleave;
2070 n_PS_ROOT_BLOCK((amp != NULL ? ok_vset(account, amp->am_name)
2071 : ok_vclear(account)));
2073 if((n_psonce & n_PSO_STARTED) && !(n_pstate & n_PS_HOOK_MASK)){
2074 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
2075 restorequitflags(oqf);
2076 if((i = setfile("%", 0)) < 0)
2077 goto jleave;
2078 temporary_folder_hook_check(FAL0);
2079 if(i > 0 && !ok_blook(emptystart))
2080 goto jleave;
2081 n_folder_announce(n_ANNOUNCE_CHANGE);
2082 restorequitflags(nqf);
2084 rv = 0;
2085 jleave:
2086 NYD_LEAVE;
2087 return rv;
2090 FL int
2091 c_unaccount(void *v){
2092 int rv;
2093 char **args;
2094 NYD_ENTER;
2096 rv = 0;
2097 args = v;
2099 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACCOUNT);
2100 while(*++args != NULL);
2101 NYD_LEAVE;
2102 return rv;
2105 FL int
2106 c_localopts(void *v){
2107 char **argv;
2108 int rv;
2109 NYD_ENTER;
2111 rv = 1;
2113 if(a_amv_lopts == NULL){
2114 n_err(_("Cannot use `localopts' in this context "
2115 "(not in `define' or `account', nor special hook)\n"));
2116 goto jleave;
2119 rv = 0;
2121 if(n_pstate & (n_PS_HOOK | n_PS_COMPOSE_MODE)){
2122 if(n_poption & n_PO_D_V)
2123 n_err(_("Cannot turn off `localopts' for compose-mode hooks\n"));
2124 goto jleave;
2127 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
2128 jleave:
2129 NYD_LEAVE;
2130 return rv;
2133 FL int
2134 c_shift(void *v){
2135 struct a_amv_pospar *appp;
2136 ui16_t i;
2137 int rv;
2138 NYD_ENTER;
2140 rv = 1;
2142 if((v = *(char**)v) == NULL)
2143 i = 1;
2144 else{
2145 si16_t sib;
2147 if((n_idec_si16_cp(&sib, v, 10, NULL
2148 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2149 ) != n_IDEC_STATE_CONSUMED || sib < 0){
2150 n_err(_("`shift': invalid argument: %s\n"), v);
2151 goto jleave;
2153 i = (ui16_t)sib;
2156 /* If in in a macro/xy */
2157 if(a_amv_lopts != NULL){
2158 struct a_amv_mac const *amp;
2159 struct a_amv_mac_call_args *amcap;
2161 /* Explicitly do allow `vpospar' created things! */
2162 amp = (amcap = a_amv_lopts->as_amcap)->amca_amp;
2163 if((amp == NULL || amcap->amca_ps_hook_mask ||
2164 (amp->am_flags & a_AMV_MF_TYPE_MASK) == a_AMV_MF_ACCOUNT) &&
2165 amcap->amca_pospar.app_not_heap){
2166 n_err(_("Cannot use `shift' in `account's or hook macros etc.\n"));
2167 goto jleave;
2169 appp = &amcap->amca_pospar;
2170 }else
2171 appp = &a_amv_pospar;
2173 if(i > appp->app_count){
2174 n_err(_("`shift': cannot shift %hu of %hu parameters\n"),
2175 i, appp->app_count);
2176 goto jleave;
2177 }else{
2178 appp->app_idx += i;
2179 appp->app_count -= i;
2180 rv = 0;
2182 jleave:
2183 NYD_LEAVE;
2184 return rv;
2187 FL int
2188 c_return(void *v){ /* TODO the exit status should be m_si64! */
2189 int rv;
2190 NYD_ENTER;
2192 if(a_amv_lopts != NULL){
2193 char const **argv;
2195 n_go_input_force_eof();
2196 n_pstate_err_no = n_ERR_NONE;
2197 rv = 0;
2199 if((argv = v)[0] != NULL){
2200 si32_t i;
2202 if((n_idec_si32_cp(&i, argv[0], 10, NULL
2203 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2204 ) == n_IDEC_STATE_CONSUMED && i >= 0)
2205 rv = (int)i;
2206 else{
2207 n_err(_("`return': return value argument is invalid: %s\n"),
2208 argv[0]);
2209 n_pstate_err_no = n_ERR_INVAL;
2210 rv = 1;
2213 if(argv[1] != NULL){
2214 if((n_idec_si32_cp(&i, argv[1], 10, NULL
2215 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2216 ) == n_IDEC_STATE_CONSUMED && i >= 0)
2217 n_pstate_err_no = i;
2218 else{
2219 n_err(_("`return': error number argument is invalid: %s\n"),
2220 argv[1]);
2221 n_pstate_err_no = n_ERR_INVAL;
2222 rv = 1;
2226 }else{
2227 n_err(_("Can only use `return' in a macro\n"));
2228 n_pstate_err_no = n_ERR_OPNOTSUPP;
2229 rv = 1;
2231 NYD_LEAVE;
2232 return rv;
2235 FL bool_t
2236 temporary_folder_hook_check(bool_t nmail){ /* TODO temporary, v15: drop */
2237 struct a_amv_mac_call_args *amcap;
2238 struct a_amv_mac *amp;
2239 size_t len;
2240 char const *cp;
2241 char *var;
2242 bool_t rv;
2243 NYD_ENTER;
2245 rv = TRU1;
2246 var = n_autorec_alloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
2248 /* First try the fully resolved path */
2249 snprintf(var, len, "folder-hook-%s", mailname);
2250 if((cp = n_var_vlook(var, FAL0)) != NULL)
2251 goto jmac;
2253 /* If we are under *folder*, try the usual +NAME syntax, too */
2254 if(displayname[0] == '+'){
2255 char *x;
2257 for(x = &mailname[len]; x != mailname; --x)
2258 if(x[-1] == '/'){
2259 snprintf(var, len, "folder-hook-+%s", x);
2260 if((cp = n_var_vlook(var, FAL0)) != NULL)
2261 goto jmac;
2262 break;
2266 /* Plain *folder-hook* is our last try */
2267 if((cp = ok_vlook(folder_hook)) == NULL)
2268 goto jleave;
2270 jmac:
2271 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
2272 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
2273 n_shexp_quote_cp(displayname, FAL0), cp);
2274 rv = FAL0;
2275 goto jleave;
2278 amcap = n_lofi_alloc(sizeof *amcap);
2279 memset(amcap, 0, sizeof *amcap);
2280 amcap->amca_name = cp;
2281 amcap->amca_amp = amp;
2282 n_pstate &= ~n_PS_HOOK_MASK;
2283 if(nmail){
2284 amcap->amca_unroller = NULL;
2285 n_pstate |= n_PS_HOOK_NEWMAIL;
2286 }else{
2287 amcap->amca_unroller = &a_amv_folder_hook_lopts;
2288 n_pstate |= n_PS_HOOK;
2290 amcap->amca_lopts_on = TRU1;
2291 amcap->amca_ps_hook_mask = TRU1;
2292 amcap->amca_no_xcall = TRU1;
2293 rv = a_amv_mac_exec(amcap);
2294 n_pstate &= ~n_PS_HOOK_MASK;
2296 jleave:
2297 NYD_LEAVE;
2298 return rv;
2301 FL void
2302 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2303 NYD_ENTER;
2304 if(a_amv_folder_hook_lopts != NULL){
2305 void *save = a_amv_lopts;
2307 a_amv_lopts = NULL;
2308 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
2309 assert(a_amv_folder_hook_lopts == NULL);
2310 a_amv_lopts = save;
2312 NYD_LEAVE;
2315 FL void
2316 temporary_compose_mode_hook_call(char const *macname,
2317 void (*hook_pre)(void *), void *hook_arg){
2318 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_GO_SPLICE
2319 * TODO comment in go.c for the right way of doing things! */
2320 static struct a_amv_lostack *cmh_losp;
2321 struct a_amv_mac_call_args *amcap;
2322 struct a_amv_mac *amp;
2323 NYD_ENTER;
2325 amp = NULL;
2327 if(macname == (char*)-1){
2328 a_amv_mac__finalize(cmh_losp);
2329 cmh_losp = NULL;
2330 }else if(macname != NULL &&
2331 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
2332 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
2333 macname);
2334 else{
2335 amcap = n_lofi_alloc(sizeof *amcap);
2336 memset(amcap, 0, sizeof *amcap);
2337 amcap->amca_name = (macname != NULL) ? macname
2338 : "*on-compose-splice(-shell)?*";
2339 amcap->amca_amp = amp;
2340 amcap->amca_unroller = &a_amv_compose_lopts;
2341 amcap->amca_hook_pre = hook_pre;
2342 amcap->amca_hook_arg = hook_arg;
2343 amcap->amca_lopts_on = TRU1;
2344 amcap->amca_ps_hook_mask = TRU1;
2345 amcap->amca_no_xcall = TRU1;
2346 n_pstate &= ~n_PS_HOOK_MASK;
2347 n_pstate |= n_PS_HOOK;
2348 if(macname != NULL)
2349 a_amv_mac_exec(amcap);
2350 else{
2351 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
2352 cmh_losp->as_global_saved = a_amv_lopts;
2353 cmh_losp->as_up = NULL;
2354 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
2355 cmh_losp->as_unroll = TRU1;
2356 a_amv_lopts = cmh_losp;
2359 NYD_LEAVE;
2362 FL void
2363 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2364 NYD_ENTER;
2365 if(a_amv_compose_lopts != NULL){
2366 void *save = a_amv_lopts;
2368 a_amv_lopts = NULL;
2369 a_amv_lopts_unroll(&a_amv_compose_lopts);
2370 assert(a_amv_compose_lopts == NULL);
2371 a_amv_lopts = save;
2373 NYD_LEAVE;
2376 FL bool_t
2377 n_var_is_user_writable(char const *name){
2378 struct a_amv_var_carrier avc;
2379 struct a_amv_var_map const *avmp;
2380 bool_t rv;
2381 NYD_ENTER;
2383 a_amv_var_revlookup(&avc, name);
2384 if((avmp = avc.avc_map) == NULL)
2385 rv = TRU1;
2386 else
2387 rv = ((avmp->avm_flags & (a_AMV_VF_BOOL | a_AMV_VF_RDONLY)) == 0);
2388 NYD_LEAVE;
2389 return rv;
2392 FL char *
2393 n_var_oklook(enum okeys okey){
2394 struct a_amv_var_carrier avc;
2395 char *rv;
2396 struct a_amv_var_map const *avmp;
2397 NYD_ENTER;
2399 avc.avc_map = avmp = &a_amv_var_map[okey];
2400 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2401 avc.avc_hash = avmp->avm_hash;
2402 avc.avc_okey = okey;
2404 if(a_amv_var_lookup(&avc, FAL0))
2405 rv = avc.avc_var->av_value;
2406 else
2407 rv = NULL;
2408 NYD_LEAVE;
2409 return rv;
2412 FL bool_t
2413 n_var_okset(enum okeys okey, uintptr_t val){
2414 struct a_amv_var_carrier avc;
2415 bool_t ok;
2416 struct a_amv_var_map const *avmp;
2417 NYD_ENTER;
2419 avc.avc_map = avmp = &a_amv_var_map[okey];
2420 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2421 avc.avc_hash = avmp->avm_hash;
2422 avc.avc_okey = okey;
2424 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2425 NYD_LEAVE;
2426 return ok;
2429 FL bool_t
2430 n_var_okclear(enum okeys okey){
2431 struct a_amv_var_carrier avc;
2432 bool_t rv;
2433 struct a_amv_var_map const *avmp;
2434 NYD_ENTER;
2436 avc.avc_map = avmp = &a_amv_var_map[okey];
2437 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2438 avc.avc_hash = avmp->avm_hash;
2439 avc.avc_okey = okey;
2441 rv = a_amv_var_clear(&avc, FAL0);
2442 NYD_LEAVE;
2443 return rv;
2446 FL char const *
2447 n_var_vlook(char const *vokey, bool_t try_getenv){
2448 struct a_amv_var_carrier avc;
2449 char const *rv;
2450 NYD_ENTER;
2452 a_amv_var_revlookup(&avc, vokey);
2454 switch((enum a_amv_var_special_category)avc.avc_special_cat){
2455 default: /* silence CC */
2456 case a_AMV_VSC_NONE:
2457 if(a_amv_var_lookup(&avc, FAL0))
2458 rv = avc.avc_var->av_value;
2459 /* Only check the environment for something that is otherwise unknown */
2460 else if(try_getenv && avc.avc_map == NULL)
2461 rv = getenv(vokey);
2462 else
2463 rv = NULL;
2464 break;
2465 case a_AMV_VSC_GLOBAL:
2466 rv = a_amv_var_vsc_global(&avc);
2467 break;
2468 case a_AMV_VSC_MULTIPLEX:
2469 rv = a_amv_var_vsc_multiplex(&avc);
2470 break;
2471 case a_AMV_VSC_POSPAR:
2472 case a_AMV_VSC_POSPAR_ENV:
2473 rv = a_amv_var_vsc_pospar(&avc);
2474 break;
2476 NYD_LEAVE;
2477 return rv;
2480 FL bool_t
2481 n_var_vexplode(void const **cookie){
2482 struct a_amv_pospar *appp;
2483 NYD_ENTER;
2485 appp = (a_amv_lopts != NULL) ? &a_amv_lopts->as_amcap->amca_pospar
2486 : &a_amv_pospar;
2487 *cookie = (appp->app_count > 0) ? &appp->app_dat[appp->app_idx] : NULL;
2488 NYD_LEAVE;
2489 return (*cookie != NULL);
2492 FL bool_t
2493 n_var_vset(char const *vokey, uintptr_t val){
2494 struct a_amv_var_carrier avc;
2495 bool_t ok;
2496 NYD_ENTER;
2498 a_amv_var_revlookup(&avc, vokey);
2500 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2501 NYD_LEAVE;
2502 return ok;
2505 FL bool_t
2506 n_var_vclear(char const *vokey){
2507 struct a_amv_var_carrier avc;
2508 bool_t ok;
2509 NYD_ENTER;
2511 a_amv_var_revlookup(&avc, vokey);
2513 ok = a_amv_var_clear(&avc, FAL0);
2514 NYD_LEAVE;
2515 return ok;
2518 #ifdef HAVE_SOCKETS
2519 FL char *
2520 n_var_xoklook(enum okeys okey, struct url const *urlp,
2521 enum okey_xlook_mode oxm){
2522 struct a_amv_var_carrier avc;
2523 struct str const *us;
2524 size_t nlen;
2525 char *nbuf, *rv;
2526 struct a_amv_var_map const *avmp;
2527 NYD_ENTER;
2529 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
2531 /* For simplicity: allow this case too */
2532 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
2533 nbuf = NULL;
2534 goto jplain;
2537 avc.avc_map = avmp = &a_amv_var_map[okey];
2538 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2539 avc.avc_okey = okey;
2541 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
2542 nlen = strlen(avc.avc_name);
2543 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
2544 memcpy(nbuf, avc.avc_name, nlen);
2545 nbuf[nlen++] = '-';
2547 /* One of .url_u_h_p and .url_h_p we test in here */
2548 memcpy(nbuf + nlen, us->s, us->l +1);
2549 avc.avc_name = a_amv_var_canonify(nbuf);
2550 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2551 if(a_amv_var_lookup(&avc, FAL0))
2552 goto jvar;
2554 /* The second */
2555 if(oxm & OXM_H_P){
2556 us = &urlp->url_h_p;
2557 memcpy(nbuf + nlen, us->s, us->l +1);
2558 avc.avc_name = a_amv_var_canonify(nbuf);
2559 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2560 if(a_amv_var_lookup(&avc, FAL0)){
2561 jvar:
2562 rv = avc.avc_var->av_value;
2563 goto jleave;
2567 jplain:
2568 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2569 jleave:
2570 if(nbuf != NULL)
2571 n_lofi_free(nbuf);
2572 NYD_LEAVE;
2573 return rv;
2575 #endif /* HAVE_SOCKETS */
2577 FL int
2578 c_set(void *v){
2579 char **ap;
2580 int err;
2581 NYD_ENTER;
2583 if(*(ap = v) == NULL){
2584 a_amv_var_show_all();
2585 err = 0;
2586 }else
2587 err = !a_amv_var_c_set(ap, FAL0);
2588 NYD_LEAVE;
2589 return err;
2592 FL int
2593 c_unset(void *v){
2594 char **ap;
2595 int err;
2596 NYD_ENTER;
2598 for(err = 0, ap = v; *ap != NULL; ++ap)
2599 err |= !n_var_vclear(*ap);
2600 NYD_LEAVE;
2601 return err;
2604 FL int
2605 c_varshow(void *v){
2606 char **ap;
2607 NYD_ENTER;
2609 if(*(ap = v) == NULL)
2610 v = NULL;
2611 else{
2612 struct n_string msg, *msgp = &msg;
2614 msgp = n_string_creat(msgp);
2615 for(; *ap != NULL; ++ap)
2616 a_amv_var_show(*ap, n_stdout, msgp);
2617 n_string_gut(msgp);
2619 NYD_LEAVE;
2620 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2623 FL int
2624 c_varedit(void *v){
2625 struct a_amv_var_carrier avc;
2626 FILE *of, *nf;
2627 char *val, **argv;
2628 int err;
2629 sighandler_type sigint;
2630 NYD_ENTER;
2632 sigint = safe_signal(SIGINT, SIG_IGN);
2634 for(err = 0, argv = v; *argv != NULL; ++argv){
2635 memset(&avc, 0, sizeof avc);
2637 a_amv_var_revlookup(&avc, *argv);
2639 if(avc.avc_map != NULL){
2640 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2641 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2642 avc.avc_name);
2643 continue;
2645 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2646 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2647 avc.avc_name);
2648 continue;
2652 a_amv_var_lookup(&avc, FAL0);
2654 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2655 NULL){
2656 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2657 err = 1;
2658 break;
2659 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2660 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2661 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2662 Fclose(of);
2663 err = 1;
2664 continue;
2667 fflush_rewind(of);
2668 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2669 Fclose(of);
2671 if(nf != NULL){
2672 int c;
2673 char const *varres;
2674 off_t l;
2676 l = fsize(nf);
2677 if(UICMP(64, l, >=, UIZ_MAX -42)){
2678 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2679 avc.avc_name);
2680 varres = n_empty;
2681 err = 1;
2682 }else{
2683 varres = val = n_autorec_alloc(l +1);
2684 for(; l > 0 && (c = getc(nf)) != EOF; --l)
2685 *val++ = c;
2686 *val++ = '\0';
2687 if(l != 0){
2688 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2689 avc.avc_name);
2690 err = 1;
2694 if(!a_amv_var_set(&avc, varres, FAL0))
2695 err = 1;
2697 Fclose(nf);
2698 }else{
2699 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2700 err = 1;
2701 break;
2705 safe_signal(SIGINT, sigint);
2706 NYD_LEAVE;
2707 return err;
2710 FL int
2711 c_environ(void *v){
2712 struct a_amv_var_carrier avc;
2713 int err;
2714 char **ap;
2715 bool_t islnk;
2716 NYD_ENTER;
2718 if((islnk = is_asccaseprefix(*(ap = v), "link")) ||
2719 is_asccaseprefix(*ap, "unlink")){
2720 for(err = 0; *++ap != NULL;){
2721 a_amv_var_revlookup(&avc, *ap);
2723 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2724 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2725 if(!islnk){
2726 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2727 continue;
2728 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2729 if(n_poption & n_PO_D_V)
2730 n_err(_("`environ': link: already established: %s\n"), *ap);
2731 continue;
2733 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2734 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2735 a_amv_var__putenv(&avc, avc.avc_var);
2736 }else if(!islnk){
2737 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2738 err = 1;
2739 }else{
2740 char const *evp = getenv(*ap);
2742 if(evp != NULL)
2743 err |= !a_amv_var_set(&avc, evp, TRU1);
2744 else{
2745 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2746 *ap);
2747 err = 1;
2751 }else if(is_asccaseprefix(*ap, "set"))
2752 err = !a_amv_var_c_set(++ap, TRU1);
2753 else if(is_asccaseprefix(*ap, "unset")){
2754 for(err = 0; *++ap != NULL;){
2755 a_amv_var_revlookup(&avc, *ap);
2757 if(!a_amv_var_clear(&avc, TRU1))
2758 err = 1;
2760 }else{
2761 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2762 err = 1;
2764 NYD_LEAVE;
2765 return err;
2768 FL int
2769 c_vexpr(void *v){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2770 size_t i;
2771 enum n_idec_state ids;
2772 si64_t lhv, rhv;
2773 char op, varbuf[64 + 64 / 8 +1];
2774 char const **argv, *varname, *varres, *cp;
2775 enum{
2776 a_ERR = 1u<<0,
2777 a_SOFTOVERFLOW = 1u<<1,
2778 a_ISNUM = 1u<<2,
2779 a_ISDECIMAL = 1u<<3, /* Print only decimal result */
2780 a_SATURATED = 1u<<4,
2781 a_ICASE = 1u<<5,
2782 a_UNSIGNED = 1u<<6, /* Unsigned right shift (share bit ok) */
2783 a_TMP = 1u<<30
2784 } f;
2785 NYD_ENTER;
2787 f = a_ERR;
2788 argv = v;
2789 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *argv++ : NULL;
2790 n_UNINIT(varres, n_empty);
2792 if((cp = argv[0])[0] == '\0')
2793 goto jesubcmd;
2795 if(cp[1] == '\0'){
2796 op = cp[0];
2797 jnumop:
2798 f |= a_ISNUM;
2799 switch(op){
2800 case '=':
2801 case '~':
2802 if(argv[1] == NULL || argv[2] != NULL)
2803 goto jesynopsis;
2805 if(*(cp = *++argv) == '\0')
2806 lhv = 0;
2807 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2808 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2809 ) != n_IDEC_STATE_CONSUMED){
2810 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2811 goto jenum_range;
2812 f |= a_SOFTOVERFLOW;
2813 break;
2815 if(op == '~')
2816 lhv = ~lhv;
2817 break;
2819 case '+':
2820 case '-':
2821 case '*':
2822 case '/':
2823 case '%':
2824 case '|':
2825 case '&':
2826 case '^':
2827 case '<':
2828 case '>':
2829 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2830 goto jesynopsis;
2831 else{
2832 char xop;
2834 if(*(cp = *++argv) == '\0')
2835 lhv = 0;
2836 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2837 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2838 ) != n_IDEC_STATE_CONSUMED){
2839 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2840 goto jenum_range;
2841 f |= a_SOFTOVERFLOW;
2842 break;
2845 if(*(cp = *++argv) == '\0')
2846 rhv = 0;
2847 else if(((ids = n_idec_si64_cp(&rhv, cp, 0, NULL)
2848 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2849 ) != n_IDEC_STATE_CONSUMED){
2850 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2851 goto jenum_range;
2852 f |= a_SOFTOVERFLOW;
2853 lhv = rhv;
2854 break;
2857 xop = op;
2858 jnumop_again:
2859 switch(xop){
2860 case '+':
2861 if(rhv < 0){
2862 if(rhv != SI64_MIN){
2863 rhv = -rhv;
2864 xop = '-';
2865 goto jnumop_again;
2866 }else if(lhv < 0)
2867 goto jenum_plusminus;
2868 else if(lhv == 0){
2869 lhv = rhv;
2870 break;
2873 if(SI64_MAX - rhv < lhv)
2874 goto jenum_plusminus;
2875 lhv += rhv;
2876 break;
2877 case '-':
2878 if(rhv < 0){
2879 if(rhv != SI64_MIN){
2880 rhv = -rhv;
2881 xop = '+';
2882 goto jnumop_again;
2883 }else if(lhv > 0)
2884 goto jenum_plusminus;
2885 else if(lhv == 0){
2886 lhv = rhv;
2887 break;
2890 if(SI64_MIN + rhv > lhv){
2891 jenum_plusminus:
2892 if(!(f & a_SATURATED))
2893 goto jenum_overflow;
2894 f |= a_SOFTOVERFLOW;
2895 lhv = (lhv < 0 || xop == '-') ? SI64_MIN : SI64_MAX;
2896 }else
2897 lhv -= rhv;
2898 break;
2899 case '*':
2900 /* Will the result be positive? */
2901 if((lhv < 0) == (rhv < 0)){
2902 if(lhv > 0){
2903 lhv = -lhv;
2904 rhv = -rhv;
2906 if(rhv != 0 && lhv != 0 && SI64_MAX / rhv > lhv){
2907 if(!(f & a_SATURATED))
2908 goto jenum_overflow;
2909 f |= a_SOFTOVERFLOW;
2910 lhv = SI64_MAX;
2911 }else
2912 lhv *= rhv;
2913 }else{
2914 if(rhv > 0){
2915 if(lhv != 0 && SI64_MIN / lhv < rhv){
2916 if(!(f & a_SATURATED))
2917 goto jenum_overflow;
2918 f |= a_SOFTOVERFLOW;
2919 lhv = SI64_MIN;
2920 }else
2921 lhv *= rhv;
2922 }else{
2923 if(rhv != 0 && lhv != 0 && SI64_MIN / rhv < lhv){
2924 if(!(f & a_SATURATED))
2925 goto jenum_overflow;
2926 f |= a_SOFTOVERFLOW;
2927 lhv = SI64_MIN;
2928 }else
2929 lhv *= rhv;
2932 break;
2933 case '/':
2934 if(rhv == 0){
2935 if(!(f & a_SATURATED))
2936 goto jenum_range;
2937 f |= a_SOFTOVERFLOW;
2938 lhv = SI64_MAX;
2939 }else
2940 lhv /= rhv;
2941 break;
2942 case '%':
2943 if(rhv == 0){
2944 if(!(f & a_SATURATED))
2945 goto jenum_range;
2946 f |= a_SOFTOVERFLOW;
2947 lhv = SI64_MAX;
2948 }else
2949 lhv %= rhv;
2950 break;
2951 case '|':
2952 lhv |= rhv;
2953 break;
2954 case '&':
2955 lhv &= rhv;
2956 break;
2957 case '^':
2958 lhv ^= rhv;
2959 break;
2960 case '<':
2961 case '>':
2962 if(!(f & a_TMP))
2963 goto jesubcmd;
2964 if(rhv > 63){ /* xxx 63? */
2965 if(!(f & a_SATURATED))
2966 goto jenum_overflow;
2967 rhv = 63;
2969 if(op == '<')
2970 lhv <<= (ui8_t)rhv;
2971 else if(f & a_UNSIGNED)
2972 lhv = (ui64_t)lhv >> (ui8_t)rhv;
2973 else
2974 lhv >>= (ui8_t)rhv;
2975 break;
2978 break;
2979 default:
2980 goto jesubcmd;
2982 }else if(cp[2] == '\0' && cp[1] == '@'){
2983 f |= a_SATURATED;
2984 op = cp[0];
2985 goto jnumop;
2986 }else if(cp[0] == '<'){
2987 if(*++cp != '<')
2988 goto jesubcmd;
2989 if(*++cp == '@'){
2990 f |= a_SATURATED;
2991 ++cp;
2993 if(*cp != '\0')
2994 goto jesubcmd;
2995 f |= a_TMP;
2996 op = '<';
2997 goto jnumop;
2998 }else if(cp[0] == '>'){
2999 if(*++cp != '>')
3000 goto jesubcmd;
3001 if(*++cp == '>'){
3002 f |= a_UNSIGNED;
3003 ++cp;
3005 if(*cp == '@'){
3006 f |= a_SATURATED;
3007 ++cp;
3009 if(*cp != '\0')
3010 goto jesubcmd;
3011 f |= a_TMP;
3012 op = '>';
3013 goto jnumop;
3014 }else if(is_asccaseprefix(cp, "length")){
3015 f |= a_ISNUM | a_ISDECIMAL;
3016 if(argv[1] == NULL || argv[2] != NULL)
3017 goto jesynopsis;
3019 i = strlen(*++argv);
3020 if(UICMP(64, i, >, SI64_MAX))
3021 goto jestr_overflow;
3022 lhv = (si64_t)i;
3023 }else if(is_asccaseprefix(cp, "hash")){
3024 f |= a_ISNUM | a_ISDECIMAL;
3025 if(argv[1] == NULL || argv[2] != NULL)
3026 goto jesynopsis;
3028 i = n_torek_hash(*++argv);
3029 lhv = (si64_t)i;
3030 }else if(is_asccaseprefix(cp, "find")){
3031 f |= a_ISNUM | a_ISDECIMAL;
3032 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
3033 goto jesynopsis;
3035 if((cp = strstr(argv[1], argv[2])) == NULL)
3036 goto jestr_nodata;
3037 i = PTR2SIZE(cp - argv[1]);
3038 if(UICMP(64, i, >, SI64_MAX))
3039 goto jestr_overflow;
3040 lhv = (si64_t)i;
3041 }else if(is_asccaseprefix(cp, "ifind")){
3042 f |= a_ISNUM | a_ISDECIMAL;
3043 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
3044 goto jesynopsis;
3046 if((cp = asccasestr(argv[1], argv[2])) == NULL)
3047 goto jestr_nodata;
3048 i = PTR2SIZE(cp - argv[1]);
3049 if(UICMP(64, i, >, SI64_MAX))
3050 goto jestr_overflow;
3051 lhv = (si64_t)i;
3052 }else if(is_asccaseprefix(cp, "substring")){
3053 if(argv[1] == NULL || argv[2] == NULL)
3054 goto jesynopsis;
3055 if(argv[3] != NULL && argv[4] != NULL)
3056 goto jesynopsis;
3058 i = strlen(varres = *++argv);
3060 if(*(cp = *++argv) == '\0')
3061 lhv = 0;
3062 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
3063 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
3064 ) != n_IDEC_STATE_CONSUMED)
3065 goto jestr_numrange;
3066 if(UICMP(64, i, >=, lhv)){
3067 i -= lhv;
3068 varres += lhv;
3069 }else{
3070 if(n_poption & n_PO_D_V)
3071 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
3072 n_shexp_quote_cp(argv[-1], FAL0));
3073 f |= a_SOFTOVERFLOW;
3076 if(argv[1] != NULL){
3077 if(*(cp = *++argv) == '\0')
3078 lhv = 0;
3079 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
3080 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
3081 ) != n_IDEC_STATE_CONSUMED)
3082 goto jestr_numrange;
3083 if(UICMP(64, i, >=, lhv)){
3084 if(UICMP(64, i, !=, lhv))
3085 varres = savestrbuf(varres, (size_t)lhv);
3086 }else{
3087 if(n_poption & n_PO_D_V)
3088 n_err(_("`vexpr': substring: length argument too large: %s\n"),
3089 n_shexp_quote_cp(argv[-2], FAL0));
3090 f |= a_SOFTOVERFLOW;
3093 }else if(is_asccaseprefix(cp, "random")){
3094 if(argv[1] == NULL || argv[2] != NULL)
3095 goto jesynopsis;
3097 if((n_idec_si64_cp(&lhv, argv[1], 0, NULL
3098 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
3099 ) != n_IDEC_STATE_CONSUMED || lhv < 0 || lhv > PATH_MAX)
3100 goto jestr_numrange;
3101 if(lhv == 0)
3102 lhv = NAME_MAX;
3103 varres = n_random_create_cp((size_t)lhv, NULL);
3104 }else if(is_asccaseprefix(cp, "file-expand")){
3105 if(argv[1] == NULL || argv[2] != NULL)
3106 goto jesynopsis;
3108 if((varres = fexpand(argv[1], FEXP_NVAR | FEXP_NOPROTO)) == NULL)
3109 goto jestr_nodata;
3110 }else if(is_asccaseprefix(cp, "makeprint")){
3111 struct str sin, sout;
3113 if(argv[1] == NULL || argv[2] != NULL)
3114 goto jesynopsis;
3116 /* XXX using strlen for `vexpr makeprint' is wrong for UTF-16 */
3117 sin.l = strlen(sin.s = n_UNCONST(argv[1]));
3118 makeprint(&sin, &sout);
3119 varres = savestrbuf(sout.s, sout.l);
3120 n_free(sout.s);
3121 /* TODO `vexpr': (wide) string length, find, etc!! */
3122 #ifdef HAVE_REGEX
3123 }else if(is_asccaseprefix(cp, "regex")) Jregex:{
3124 regmatch_t rema[1 + n_VEXPR_REGEX_MAX];
3125 regex_t re;
3126 int reflrv;
3128 f |= a_ISNUM | a_ISDECIMAL;
3129 if(argv[1] == NULL || argv[2] == NULL ||
3130 (argv[3] != NULL && argv[4] != NULL))
3131 goto jesynopsis;
3133 reflrv = REG_EXTENDED;
3134 if(f & a_ICASE)
3135 reflrv |= REG_ICASE;
3136 if((reflrv = regcomp(&re, argv[2], reflrv))){
3137 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
3138 n_shexp_quote_cp(argv[2], FAL0), n_regex_err_to_doc(&re, reflrv));
3139 assert(f & a_ERR);
3140 n_pstate_err_no = n_ERR_INVAL;
3141 goto jestr;
3143 reflrv = regexec(&re, argv[1], n_NELEM(rema), rema, 0);
3144 regfree(&re);
3145 if(reflrv == REG_NOMATCH)
3146 goto jestr_nodata;
3148 /* Search only? Else replace, which is a bit */
3149 if(argv[3] == NULL){
3150 if(UICMP(64, rema[0].rm_so, >, SI64_MAX))
3151 goto jestr_overflow;
3152 lhv = (si64_t)rema[0].rm_so;
3153 }else{
3154 /* We need to setup some kind of pseudo macro environment for this */
3155 struct a_amv_lostack los;
3156 struct a_amv_mac_call_args amca;
3157 char const **reargv;
3159 memset(&amca, 0, sizeof amca);
3160 amca.amca_name = savestrbuf(&argv[1][rema[0].rm_so],
3161 rema[0].rm_eo - rema[0].rm_so);
3162 amca.amca_amp = a_AMV_MACKY_MACK;
3163 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
3165 amca.amca_pospar.app_count = (ui32_t)i - 1;
3166 amca.amca_pospar.app_not_heap = TRU1;
3167 amca.amca_pospar.app_dat =
3168 reargv = n_autorec_alloc(sizeof(char*) * i);
3169 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
3170 *reargv++ = savestrbuf(&argv[1][rema[i].rm_so],
3171 rema[i].rm_eo - rema[i].rm_so);
3172 *reargv = NULL;
3174 hold_all_sigs(); /* TODO DISLIKE! */
3175 los.as_global_saved = a_amv_lopts;
3176 los.as_amcap = &amca;
3177 los.as_up = los.as_global_saved;
3178 los.as_lopts = NULL;
3179 los.as_unroll = FAL0;
3180 a_amv_lopts = &los;
3182 /* C99 */{
3183 struct str templ;
3184 struct n_string s_b;
3185 enum n_shexp_state shs;
3187 templ.s = n_UNCONST(argv[3]);
3188 templ.l = UIZ_MAX;
3189 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
3190 n_SHEXP_PARSE_IGNORE_EMPTY | n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
3191 n_SHEXP_PARSE_QUOTE_AUTO_DSQ),
3192 n_string_creat_auto(&s_b), &templ, NULL);
3193 if((shs & (n_SHEXP_STATE_ERR_MASK | n_SHEXP_STATE_STOP)
3194 ) == n_SHEXP_STATE_STOP){
3195 varres = n_string_cp(&s_b);
3196 n_string_drop_ownership(&s_b);
3197 }else
3198 varres = NULL;
3201 a_amv_lopts = los.as_global_saved;
3202 rele_all_sigs(); /* TODO DISLIKE! */
3204 if(varres == NULL)
3205 goto jestr_nodata;
3206 f &= ~(a_ISNUM | a_ISDECIMAL);
3208 }else if(is_asccaseprefix(argv[0], "iregex")){
3209 f |= a_ICASE;
3210 goto Jregex;
3211 #endif /* HAVE_REGEX */
3212 }else
3213 goto jesubcmd;
3215 n_pstate_err_no = (f & a_SOFTOVERFLOW) ? n_ERR_OVERFLOW : n_ERR_NONE;
3216 f &= ~a_ERR;
3218 /* Generate the variable value content for numerics.
3219 * Anticipate in our handling below! (Don't do needless work) */
3220 jleave:
3221 if((f & a_ISNUM) && ((f & a_ISDECIMAL) || varname != NULL)){
3222 snprintf(varbuf, sizeof varbuf, "%" PRId64 , lhv);
3223 varres = varbuf;
3226 if(varname == NULL){
3227 /* If there was no error and we are printing a numeric result, print some
3228 * more bases for the fun of it */
3229 if((f & (a_ERR | a_ISNUM | a_ISDECIMAL)) == a_ISNUM){
3230 size_t j;
3232 for(j = 1, i = 0; i < 64; ++i){
3233 varbuf[63 + 64 / 8 -j - i] = (lhv & ((ui64_t)1 << i)) ? '1' : '0';
3234 if((i & 7) == 7 && i != 63){
3235 ++j;
3236 varbuf[63 + 64 / 8 -j - i] = ' ';
3239 varbuf[64 + 64 / 8 -1] = '\0';
3240 if(fprintf(n_stdout,
3241 "%s\n0%" PRIo64 " | 0x%" PRIX64 " | %" PRId64 "\n",
3242 varbuf, lhv, lhv, lhv) < 0){
3243 n_pstate_err_no = n_err_no;
3244 f |= a_ERR;
3246 }else if(varres != NULL && fprintf(n_stdout, "%s\n", varres) < 0){
3247 n_pstate_err_no = n_err_no;
3248 f |= a_ERR;
3250 }else if(!n_var_vset(varname, (uintptr_t)varres)){
3251 n_pstate_err_no = n_ERR_NOTSUP;
3252 f |= a_ERR;
3254 NYD_LEAVE;
3255 return (f & a_ERR) ? 1 : 0;
3257 jerr:
3258 f = a_ERR | a_ISNUM;
3259 lhv = -1;
3260 goto jleave;
3261 jesubcmd:
3262 n_err(_("`vexpr': invalid subcommand: %s\n"),
3263 n_shexp_quote_cp(*argv, FAL0));
3264 n_pstate_err_no = n_ERR_INVAL;
3265 goto jerr;
3266 jesynopsis:
3267 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3268 n_pstate_err_no = n_ERR_INVAL;
3269 goto jerr;
3271 jenum_range:
3272 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3273 n_shexp_quote_cp(*argv, FAL0));
3274 n_pstate_err_no = n_ERR_RANGE;
3275 goto jerr;
3276 jenum_overflow:
3277 n_err(_("`vexpr': expression overflows datatype: %" PRId64 " %c %" PRId64
3278 "\n"), lhv, op, rhv);
3279 n_pstate_err_no = n_ERR_OVERFLOW;
3280 goto jerr;
3282 jestr_numrange:
3283 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3284 n_shexp_quote_cp(*argv, FAL0));
3285 n_pstate_err_no = n_ERR_RANGE;
3286 goto jestr;
3287 jestr_overflow:
3288 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3289 n_pstate_err_no = n_ERR_OVERFLOW;
3290 goto jestr;
3291 jestr_nodata:
3292 n_pstate_err_no = n_ERR_NODATA;
3293 /* FALLTHRU */
3294 jestr:
3295 varres = n_empty;
3296 f &= ~a_ISNUM;
3297 f |= a_ERR;
3298 goto jleave;
3301 FL int
3302 c_vpospar(void *v){
3303 struct n_cmd_arg *cap;
3304 size_t i;
3305 struct a_amv_pospar *appp;
3306 enum{
3307 a_NONE = 0,
3308 a_ERR = 1u<<0,
3309 a_SET = 1u<<1,
3310 a_CLEAR = 1u<<2,
3311 a_QUOTE = 1u<<3
3312 } f;
3313 char const *varres;
3314 struct n_cmd_arg_ctx *cacp;
3315 NYD_ENTER;
3317 n_pstate_err_no = n_ERR_NONE;
3318 n_UNINIT(varres, n_empty);
3319 cacp = v;
3320 cap = cacp->cac_arg;
3322 if(is_asccaseprefix(cap->ca_arg.ca_str.s, "set"))
3323 f = a_SET;
3324 else if(is_asccaseprefix(cap->ca_arg.ca_str.s, "clear"))
3325 f = a_CLEAR;
3326 else if(is_asccaseprefix(cap->ca_arg.ca_str.s, "quote"))
3327 f = a_QUOTE;
3328 else{
3329 n_err(_("`vpospar': invalid subcommand: %s\n"),
3330 n_shexp_quote_cp(cap->ca_arg.ca_str.s, FAL0));
3331 n_pstate_err_no = n_ERR_INVAL;
3332 f = a_ERR;
3333 goto jleave;
3335 --cacp->cac_no;
3337 if((f & (a_CLEAR | a_QUOTE)) && cap->ca_next != NULL){
3338 n_err(_("`vpospar': `%s': takes no argument\n"), cap->ca_arg.ca_str.s);
3339 n_pstate_err_no = n_ERR_INVAL;
3340 f = a_ERR;
3341 goto jleave;
3344 cap = cap->ca_next;
3346 /* If in a macro, we need to overwrite the local instead of global argv */
3347 appp = (a_amv_lopts != NULL) ? &a_amv_lopts->as_amcap->amca_pospar
3348 : &a_amv_pospar;
3350 if(f & (a_SET | a_CLEAR)){
3351 if(cacp->cac_vput != NULL && (n_poption & n_PO_D_V))
3352 n_err(_("`vpospar': `vput' only supported for `quote' subcommand\n"));
3353 if(!appp->app_not_heap && appp->app_maxcount > 0){
3354 for(i = appp->app_maxcount; i-- != 0;)
3355 n_free(n_UNCONST(appp->app_dat[i]));
3356 n_free(appp->app_dat);
3358 memset(appp, 0, sizeof *appp);
3360 if(f & a_SET){
3361 if((i = cacp->cac_no) > a_AMV_POSPAR_MAX){
3362 n_err(_("`vpospar': overflow: %" PRIuZ " arguments!\n"), i);
3363 n_pstate_err_no = n_ERR_OVERFLOW;
3364 f = a_ERR;
3365 goto jleave;
3368 memset(appp, 0, sizeof *appp);
3369 if(i > 0){
3370 appp->app_maxcount = appp->app_count = (ui16_t)i;
3371 /* XXX Optimize: store it all in one chunk! */
3372 ++i;
3373 i *= sizeof *appp->app_dat;
3374 appp->app_dat = n_alloc(i);
3376 for(i = 0; cap != NULL; ++i, cap = cap->ca_next){
3377 appp->app_dat[i] = n_alloc(cap->ca_arg.ca_str.l +1);
3378 memcpy(n_UNCONST(appp->app_dat[i]), cap->ca_arg.ca_str.s,
3379 cap->ca_arg.ca_str.l +1);
3382 appp->app_dat[i] = NULL;
3385 }else{
3386 if(appp->app_count == 0)
3387 varres = n_empty;
3388 else{
3389 struct str in;
3390 struct n_string s, *sp;
3391 char sep1, sep2;
3393 sp = n_string_creat_auto(&s);
3395 sep1 = *ok_vlook(ifs);
3396 sep2 = *ok_vlook(ifs_ws);
3397 if(sep1 == sep2)
3398 sep2 = '\0';
3399 if(sep1 == '\0')
3400 sep1 = ' ';
3402 for(i = 0; i < appp->app_count; ++i){
3403 if(sp->s_len){
3404 if(!n_string_can_swallow(sp, 2))
3405 goto jeover;
3406 sp = n_string_push_c(sp, sep1);
3407 if(sep2 != '\0')
3408 sp = n_string_push_c(sp, sep2);
3410 in.l = strlen(in.s = n_UNCONST(appp->app_dat[i + appp->app_idx]));
3412 if(!n_string_can_swallow(sp, in.l)){
3413 jeover:
3414 n_err(_("`vpospar': overflow: string too long!\n"));
3415 n_pstate_err_no = n_ERR_OVERFLOW;
3416 f = a_ERR;
3417 goto jleave;
3419 sp = n_shexp_quote(sp, &in, TRU1);
3422 varres = n_string_cp(sp);
3425 if(cacp->cac_vput == NULL){
3426 if(fprintf(n_stdout, "%s\n", varres) < 0){
3427 n_pstate_err_no = n_err_no;
3428 f |= a_ERR;
3430 }else if(!n_var_vset(cacp->cac_vput, (uintptr_t)varres)){
3431 n_pstate_err_no = n_ERR_NOTSUP;
3432 f |= a_ERR;
3435 jleave:
3436 NYD_LEAVE;
3437 return (f & a_ERR) ? 1 : 0;
3440 /* s-it-mode */