a_main_setscreensize(): no need to test n_PSO_INTERACTIVE
[s-mailx.git] / accmacvar.c
blobd4fe233464db4881e2daf682fb0ca7e9c1c3b3b0
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 mk-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 lex_input.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_LEX_SLICE comment in lex_input.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 /* Special "pseudo macro" that stabs you from the back */
58 #define a_AMV_MACKY_MACK ((struct a_amv_mac*)-1)
60 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
61 #define a_AMV_PRIME HSHSIZE
62 #define a_AMV_NAME2HASH(N) torek_hash(N)
63 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
65 enum a_amv_mac_flags{
66 a_AMV_MF_NONE = 0,
67 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
68 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
69 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
70 a_AMV_MF_DEL = 1<<7, /* Delete in progress, free once refcnt==0 */
71 a_AMV_MF__MAX = 0xFF
74 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
75 * _IMPORT implies _ENV; it doesn't verify anything... */
76 enum a_amv_var_flags{
77 a_AMV_VF_NONE = 0,
78 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
79 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
80 a_AMV_VF_NOLOPTS = 1<<2, /* May not be tracked by `localopts' */
81 a_AMV_VF_RDONLY = 1<<3, /* May not be set by user */
82 a_AMV_VF_NODEL = 1<<4, /* May not be deleted */
83 a_AMV_VF_NOTEMPTY = 1<<5, /* May not be assigned an empty value */
84 a_AMV_VF_NOCNTRLS = 1<<6, /* Value may not contain control characters */
85 a_AMV_VF_NUM = 1<<7, /* Value must be a 32-bit number */
86 a_AMV_VF_POSNUM = 1<<8, /* Value must be positive 32-bit number */
87 a_AMV_VF_LOWER = 1<<9, /* Values will be stored in a lowercase version */
88 a_AMV_VF_VIP = 1<<10, /* Wants _var_check_vips() evaluation */
89 a_AMV_VF_IMPORT = 1<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
90 a_AMV_VF_ENV = 1<<12, /* Update environment on change */
91 a_AMV_VF_I3VAL = 1<<13, /* Has an initial value */
92 a_AMV_VF_DEFVAL = 1<<14, /* Has a default value */
93 a_AMV_VF_LINKED = 1<<15, /* `environ' linked */
94 a_AMV_VF__MASK = (1<<(15+1)) - 1
97 /* We support some special parameter names for one-letter variable names;
98 * note these have counterparts in the code that manages shell expansion! */
99 enum a_amv_var_special_type{
100 a_AMV_VST_STAR, /* * */
101 a_AMV_VST_AT, /* @ */
102 a_AMV_VST_NOSIGN /* # */
103 /* ? These are different in that they are no macro-local variables, but in
104 * fact real ones! */
105 /*a_AMV_VST_QM * ? *
106 *a_AMV_VST_EM * ! */
109 struct a_amv_mac{
110 struct a_amv_mac *am_next;
111 ui32_t am_maxlen; /* of any line in .am_line_dat */
112 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
113 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
114 struct a_amv_var *am_lopts; /* `localopts' unroll list */
115 ui32_t am_refcnt; /* 0-based for `un{account,define}' purposes */
116 ui8_t am_flags; /* enum a_amv_mac_flags */
117 char am_name[n_VFIELD_SIZE(3)]; /* of this macro */
119 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
121 struct a_amv_mac_line{
122 ui32_t aml_len;
123 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
124 char aml_dat[n_VFIELD_SIZE(0)];
127 struct a_amv_mac_call_args{
128 char const *amca_name; /* For MACKY_MACK, this is *0*! */
129 struct a_amv_mac *amca_amp; /* "const", but for am_refcnt */
130 struct a_amv_var **amca_unroller;
131 void (*amca_hook_pre)(void *);
132 void *amca_hook_arg;
133 bool_t amca_lopts_on;
134 bool_t amca_ps_hook_mask;
135 ui8_t amca__pad[4];
136 ui16_t amca_argc; /* Max is SI16_MAX */
137 char const **amca_argv;
140 struct a_amv_lostack{
141 struct a_amv_lostack *as_global_saved; /* Saved global XXX due to jump */
142 struct a_amv_mac_call_args *as_amcap;
143 struct a_amv_lostack *as_up; /* Outer context */
144 struct a_amv_var *as_lopts;
145 bool_t as_unroll; /* Unrolling enabled? */
146 ui8_t avs__pad[7];
149 struct a_amv_var{
150 struct a_amv_var *av_link;
151 char *av_value;
152 #ifdef HAVE_PUTENV
153 char *av_env; /* Actively managed putenv(3) memory */
154 #endif
155 ui16_t av_flags; /* enum a_amv_var_flags */
156 char av_name[n_VFIELD_SIZE(6)];
158 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
160 struct a_amv_var_map{
161 ui32_t avm_hash;
162 ui16_t avm_keyoff;
163 ui16_t avm_flags; /* enum a_amv_var_flags */
165 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
167 struct a_amv_var_virt{
168 ui32_t avv_okey;
169 ui8_t avv__dummy[4];
170 struct a_amv_var const *avv_var;
173 struct a_amv_var_defval{
174 ui32_t avdv_okey;
175 ui8_t avdv__pad[4];
176 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
179 struct a_amv_var_carrier{
180 char const *avc_name;
181 ui32_t avc_hash;
182 ui32_t avc_prime;
183 struct a_amv_var *avc_var;
184 struct a_amv_var_map const *avc_map;
185 enum okeys avc_okey;
186 ui8_t avc__pad[1];
187 bool_t avc_is_special; /* Only if avc_okey == ok_v___special_param */
188 /* Numeric if .avc_is_special==TRUM1, else enum a_amv_var_special_type */
189 ui16_t avc_special_prop;
192 /* Include the constant mk-okey-map.pl output */
193 #include "version.h"
194 #include "okeys.h"
196 /* The currently active account */
197 static struct a_amv_mac *a_amv_acc_curr;
199 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
201 /* Unroll list of currently running macro stack */
202 static struct a_amv_lostack *a_amv_lopts;
204 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
206 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
207 * TODO today via a static lostack, it should be a field in mailbox, once that
208 * TODO is a real multi-instance object */
209 static struct a_amv_var *a_amv_folder_hook_lopts;
211 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
212 static struct a_amv_var *a_amv_compose_lopts;
214 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
215 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
216 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
217 * plain lookup cases or when _UNDEF was performed on a currently active entry
218 * (the entry will have been unlinked, and the _MF_DEL will be honoured once
219 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
220 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
221 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
223 /* `call', `call_if' */
224 static int a_amv_mac_call(void *v, bool_t silent_nexist);
226 /* Execute a macro; amcap must reside in LOFI memory */
227 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
229 static void a_amv_mac__finalize(void *vp);
231 /* User display helpers */
232 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
234 /* _def() returns error for faulty definitions and already existing * names,
235 * _undef() returns error if a named thing doesn't exist */
236 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
237 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
239 /* */
240 static void a_amv_mac_free(struct a_amv_mac *amp);
242 /* Update replay-log */
243 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
244 struct a_amv_var *oavp);
245 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
247 /* Special cased value string allocation */
248 static char *a_amv_var_copy(char const *str);
249 static void a_amv_var_free(char *cp);
251 /* Check for special housekeeping */
252 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
254 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
255 static bool_t a_amv_var_check_nocntrls(char const *val);
256 static bool_t a_amv_var_check_num(char const *val, bool_t posnum);
258 /* If a variable name begins with a lowercase-character and contains at
259 * least one '@', it is converted to all-lowercase. This is necessary
260 * for lookups of names based on email addresses.
261 * Following the standard, only the part following the last '@' should
262 * be lower-cased, but practice has established otherwise here */
263 static char const *a_amv_var_canonify(char const *vn);
265 /* Try to reverse lookup an option name to an enum okeys mapping.
266 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
267 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
268 char const *name);
270 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
271 * Sets .avc_prime; .avc_var is NULL if not found.
272 * Here it is where we care for _I3VAL and _DEFVAL, too.
273 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
274 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
275 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
276 * return FAL0, then! */
277 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
278 bool_t i3val_nonew);
280 /* Set var from .avc_(map|name|hash), return success */
281 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
282 bool_t force_env);
284 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
285 struct a_amv_var *avp);
287 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
288 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
290 static bool_t a_amv_var__clearenv(char const *name, char *value);
292 /* List all variables */
293 static void a_amv_var_show_all(void);
295 static int a_amv_var__show_cmp(void const *s1, void const *s2);
297 /* Actually do print one, return number of lines written */
298 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
300 /* Shared c_set() and c_environ():set impl, return success */
301 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
303 static struct a_amv_mac *
304 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
305 enum a_amv_mac_flags amf){
306 struct a_amv_mac *amp, **ampp;
307 ui32_t h;
308 enum a_amv_mac_flags save_amf;
309 NYD2_ENTER;
311 save_amf = amf;
312 amf &= a_AMV_MF_TYPE_MASK;
313 h = a_AMV_NAME2HASH(name);
314 h = a_AMV_HASH2PRIME(h);
315 ampp = &a_amv_macs[h];
317 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
318 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
319 !strcmp(amp->am_name, name)){
320 if(n_LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
321 goto jleave;
323 *ampp = amp->am_next;
325 if(amp->am_refcnt > 0){
326 amp->am_flags |= a_AMV_MF_DEL;
327 if(n_poption & n_PO_D_V)
328 n_err(_("Delayed deletion of currently active %s: %s\n"),
329 (amp->am_flags & a_AMV_MF_ACC ? "account" : "define"), name);
330 }else{
331 a_amv_mac_free(amp);
332 amp = (struct a_amv_mac*)-1;
334 break;
338 if(newamp != NULL){
339 ampp = &a_amv_macs[h];
340 newamp->am_next = *ampp;
341 *ampp = newamp;
342 amp = NULL;
344 jleave:
345 NYD2_LEAVE;
346 return amp;
349 static int
350 a_amv_mac_call(void *v, bool_t silent_nexist){
351 int rv;
352 struct a_amv_mac *amp;
353 char const *name;
354 NYD_ENTER;
356 name = *(char const**)v;
358 if((amp = a_amv_mac_lookup(name, NULL, a_AMV_MF_NONE)) != NULL){
359 struct a_amv_mac_call_args *amcap;
361 amcap = n_lofi_alloc(sizeof *amcap);
362 memset(amcap, 0, sizeof *amcap);
363 amcap->amca_name = name;
364 amcap->amca_amp = amp;
365 /* C99 */{
366 char const **argv;
367 ui32_t argc;
369 for(argc = 0, argv = v; *++argv != NULL; ++argc)
371 if(argc > 0){
372 amcap->amca_argc = argc;
373 amcap->amca_argv = &(argv = v)[1];
376 rv = (a_amv_mac_exec(amcap) == FAL0);
377 }else if((rv = (silent_nexist == FAL0)))
378 n_err(_("Undefined macro `call'ed: %s\n"), n_shexp_quote_cp(name, FAL0));
379 NYD_LEAVE;
380 return rv;
383 static bool_t
384 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
385 struct a_amv_lostack *losp;
386 struct a_amv_mac_line **amlp;
387 char **args_base, **args;
388 struct a_amv_mac *amp;
389 bool_t rv;
390 NYD2_ENTER;
392 amp = amcap->amca_amp;
393 assert(amp != NULL && amp != a_AMV_MACKY_MACK);
394 ++amp->am_refcnt;
395 /* XXX Unfortunately we yet need to dup the macro lines! :( */
396 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
397 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
398 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
399 *args = NULL;
401 losp = n_lofi_alloc(sizeof *losp);
402 losp->as_global_saved = a_amv_lopts;
403 if((losp->as_amcap = amcap)->amca_unroller == NULL){
404 losp->as_up = losp->as_global_saved;
405 losp->as_lopts = NULL;
406 }else{
407 losp->as_up = NULL;
408 losp->as_lopts = *amcap->amca_unroller;
410 losp->as_unroll = amcap->amca_lopts_on;
412 a_amv_lopts = losp;
413 if(amcap->amca_hook_pre != NULL)
414 n_PS_ROOT_BLOCK((*amcap->amca_hook_pre)(amcap->amca_hook_arg));
415 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base,
416 &a_amv_mac__finalize, losp);
417 NYD2_LEAVE;
418 return rv;
421 static void
422 a_amv_mac__finalize(void *vp){
423 struct a_amv_mac *amp;
424 struct a_amv_mac_call_args *amcap;
425 struct a_amv_lostack *losp;
426 NYD2_ENTER;
428 losp = vp;
429 a_amv_lopts = losp->as_global_saved;
431 if((amcap = losp->as_amcap)->amca_unroller == NULL){
432 if(losp->as_lopts != NULL)
433 a_amv_lopts_unroll(&losp->as_lopts);
434 }else
435 *amcap->amca_unroller = losp->as_lopts;
437 if(amcap->amca_ps_hook_mask)
438 n_pstate &= ~n_PS_HOOK_MASK;
440 if((amp = amcap->amca_amp) != a_AMV_MACKY_MACK && amp != NULL &&
441 --amp->am_refcnt == 0 && (amp->am_flags & a_AMV_MF_DEL))
442 a_amv_mac_free(amp);
444 n_lofi_free(losp);
445 n_lofi_free(amcap);
446 NYD2_LEAVE;
449 static bool_t
450 a_amv_mac_show(enum a_amv_mac_flags amf){
451 size_t lc, mc, ti, i;
452 char const *typestr;
453 FILE *fp;
454 bool_t rv;
455 NYD2_ENTER;
457 rv = FAL0;
459 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
460 NULL){
461 n_perr(_("Can't create temporary file for `define' or `account' listing"),
463 goto jleave;
466 amf &= a_AMV_MF_TYPE_MASK;
467 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
469 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
470 struct a_amv_mac *amp;
472 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
473 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
474 struct a_amv_mac_line **amlpp;
476 if(++mc > 1){
477 putc('\n', fp);
478 ++lc;
480 ++lc;
481 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
482 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
483 for(i = (*amlpp)->aml_prespc; i > 0; --i)
484 putc(' ', fp);
485 fputs((*amlpp)->aml_dat, fp);
486 putc('\n', fp);
488 fputs("}\n", fp);
489 ++lc;
493 if(mc > 0)
494 page_or_print(fp, lc);
496 rv = (ferror(fp) == 0);
497 Fclose(fp);
498 jleave:
499 NYD2_LEAVE;
500 return rv;
503 static bool_t
504 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
505 struct str line;
506 ui32_t line_cnt, maxlen;
507 struct linelist{
508 struct linelist *ll_next;
509 struct a_amv_mac_line *ll_amlp;
510 } *llp, *ll_head, *ll_tail;
511 union {size_t s; int i; ui32_t ui; size_t l;} n;
512 struct a_amv_mac *amp;
513 bool_t rv;
514 NYD2_ENTER;
516 memset(&line, 0, sizeof line);
517 rv = FAL0;
518 amp = NULL;
520 /* TODO We should have our input state machine which emits Line events,
521 * TODO and hook different consumers dependent on our content, as stated
522 * TODO in i think lex_input; */
523 /* Read in the lines which form the macro content */
524 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
525 ui32_t leaspc;
526 char *cp;
528 n.i = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, n_empty,
529 &line.s, &line.l, NULL);
530 if(n.ui == 0)
531 continue;
532 if(n.i < 0){
533 n_err(_("Unterminated %s definition: %s\n"),
534 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
535 goto jerr;
538 /* Trim WS, remember amount of leading spaces for display purposes */
539 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
540 if(*cp == '\t')
541 leaspc = (leaspc + 8u) & ~7u;
542 else if(*cp == ' ')
543 ++leaspc;
544 else
545 break;
546 for(; n.ui > 0 && spacechar(cp[n.ui - 1]); --n.ui)
548 if(n.ui == 0)
549 continue;
551 maxlen = n_MAX(maxlen, n.ui);
552 cp[n.ui++] = '\0';
554 /* Is is the closing brace? */
555 if(*cp == '}')
556 break;
558 if(n_LIKELY(++line_cnt < UI32_MAX)){
559 struct a_amv_mac_line *amlp;
561 llp = salloc(sizeof *llp);
562 if(ll_head == NULL)
563 ll_head = llp;
564 else
565 ll_tail->ll_next = llp;
566 ll_tail = llp;
567 llp->ll_next = NULL;
568 llp->ll_amlp = amlp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line,
569 aml_dat) + n.ui);
570 amlp->aml_len = n.ui -1;
571 amlp->aml_prespc = leaspc;
572 memcpy(amlp->aml_dat, cp, n.ui);
573 }else{
574 n_err(_("Too much content in %s definition: %s\n"),
575 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
576 goto jerr;
580 /* Create the new macro */
581 n.s = strlen(name) +1;
582 amp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac, am_name) + n.s);
583 amp->am_next = NULL;
584 amp->am_maxlen = maxlen;
585 amp->am_line_cnt = line_cnt;
586 amp->am_refcnt = 0;
587 amp->am_flags = amf;
588 amp->am_lopts = NULL;
589 memcpy(amp->am_name, name, n.s);
590 /* C99 */{
591 struct a_amv_mac_line **amlpp;
593 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
594 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
595 *amlpp++ = llp->ll_amlp;
596 *amlpp = NULL;
599 /* Create entry, replace a yet existing one as necessary */
600 a_amv_mac_lookup(name, amp, amf | a_AMV_MF_UNDEF);
601 rv = TRU1;
602 jleave:
603 if(line.s != NULL)
604 free(line.s);
605 NYD2_LEAVE;
606 return rv;
608 jerr:
609 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
610 free(llp->ll_amlp);
611 if(amp != NULL){
612 free(amp->am_line_dat);
613 free(amp);
615 goto jleave;
618 static bool_t
619 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
620 struct a_amv_mac *amp;
621 bool_t rv;
622 NYD2_ENTER;
624 rv = TRU1;
626 if(n_LIKELY(name[0] != '*' || name[1] != '\0')){
627 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
628 n_err(_("%s not defined: %s\n"),
629 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
630 rv = FAL0;
632 }else{
633 struct a_amv_mac **ampp, *lamp;
635 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
636 ++ampp)
637 for(lamp = NULL, amp = *ampp; amp != NULL;){
638 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
639 /* xxx Expensive but rare: be simple */
640 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
641 amp = (lamp == NULL) ? *ampp : lamp->am_next;
642 }else{
643 lamp = amp;
644 amp = amp->am_next;
648 NYD2_LEAVE;
649 return rv;
652 static void
653 a_amv_mac_free(struct a_amv_mac *amp){
654 struct a_amv_mac_line **amlpp;
655 NYD2_ENTER;
657 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
658 free(*amlpp);
659 free(amp->am_line_dat);
660 free(amp);
661 NYD2_LEAVE;
664 static void
665 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
666 struct a_amv_var *oavp){
667 struct a_amv_var *avp;
668 size_t nl, vl;
669 NYD2_ENTER;
671 /* Propagate unrolling up the stack, as necessary */
672 assert(alp != NULL);
673 for(;;){
674 if(alp->as_unroll)
675 break;
676 if((alp = alp->as_up) == NULL)
677 goto jleave;
680 /* Check whether this variable is handled yet */
681 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
682 if(!strcmp(avp->av_name, name))
683 goto jleave;
685 nl = strlen(name) +1;
686 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
687 avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name) + nl + vl);
688 avp->av_link = alp->as_lopts;
689 alp->as_lopts = avp;
690 memcpy(avp->av_name, name, nl);
691 if(vl == 0){
692 avp->av_value = NULL;
693 avp->av_flags = 0;
694 #ifdef HAVE_PUTENV
695 avp->av_env = NULL;
696 #endif
697 }else{
698 avp->av_value = &avp->av_name[nl];
699 avp->av_flags = oavp->av_flags;
700 memcpy(avp->av_value, oavp->av_value, vl);
701 #ifdef HAVE_PUTENV
702 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
703 #endif
705 jleave:
706 NYD2_LEAVE;
709 static void
710 a_amv_lopts_unroll(struct a_amv_var **avpp){
711 struct a_amv_lostack *save_alp;
712 struct a_amv_var *x, *avp;
713 NYD2_ENTER;
715 avp = *avpp;
716 *avpp = NULL;
718 save_alp = a_amv_lopts;
719 a_amv_lopts = NULL;
720 while(avp != NULL){
721 x = avp;
722 avp = avp->av_link;
723 n_PS_ROOT_BLOCK(n_var_vset(x->av_name, (uintptr_t)x->av_value));
724 free(x);
726 a_amv_lopts = save_alp;
727 NYD2_LEAVE;
730 static char *
731 a_amv_var_copy(char const *str){
732 char *news;
733 size_t len;
734 NYD2_ENTER;
736 if(*str == '\0')
737 news = n_UNCONST(n_empty);
738 else if(str[1] == '\0'){
739 if(str[0] == '1')
740 news = n_UNCONST(n_1);
741 else if(str[0] == '0')
742 news = n_UNCONST(n_0);
743 else
744 goto jheap;
745 }else if(str[2] == '\0' && str[0] == '-' && str[1] == '1')
746 news = n_UNCONST(n_m1);
747 else{
748 jheap:
749 len = strlen(str) +1;
750 news = smalloc(len);
751 memcpy(news, str, len);
753 NYD2_LEAVE;
754 return news;
757 static void
758 a_amv_var_free(char *cp){
759 NYD2_ENTER;
760 if(cp[0] != '\0' && cp != n_0 && cp != n_1 && cp != n_m1)
761 free(cp);
762 NYD2_LEAVE;
765 static bool_t
766 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
767 ui32_t flag;
768 bool_t ok;
769 NYD2_ENTER;
771 ok = TRU1;
772 flag = 0;
774 switch(okey){
775 case ok_b_debug:
776 flag = n_PO_DEBUG;
777 break;
778 case ok_v_HOME:
779 /* Invalidate any resolved folder then, too
780 * FALLTHRU */
781 case ok_v_folder:
782 n_PS_ROOT_BLOCK(ok_vclear(_folder_resolved));
783 break;
784 case ok_b_header:
785 flag = n_PO_N_FLAG;
786 enable = !enable;
787 break;
788 case ok_b_memdebug:
789 flag = n_PO_MEMDEBUG;
790 break;
791 case ok_b_POSIXLY_CORRECT:
792 if(!(n_pstate & n_PS_ROOT))
793 n_PS_ROOT_BLOCK(enable ? ok_bset(posix) : ok_bclear(posix));
794 break;
795 case ok_b_posix:
796 if(!(n_pstate & n_PS_ROOT))
797 n_PS_ROOT_BLOCK(enable ? ok_bset(POSIXLY_CORRECT)
798 : ok_bclear(POSIXLY_CORRECT));
799 break;
800 case ok_b_skipemptybody:
801 flag = n_PO_E_FLAG;
802 break;
803 case ok_b_typescript_mode:
804 if(enable){
805 ok_bset(colour_disable);
806 ok_bset(line_editor_disable);
807 if(!(n_psonce & n_PSO_STARTED))
808 ok_bset(termcap_disable);
810 case ok_v_umask:
811 assert(enable);
812 if(**val != '\0'){
813 ui64_t uib;
815 n_idec_ui64_cp(&uib, *val, 0, NULL);
816 if(uib & ~0777u){ /* (is valid _VF_POSNUM) */
817 n_err(_("Invalid *umask* setting: %s\n"), *val);
818 ok = FAL0;
819 }else
820 umask((mode_t)uib);
822 break;
823 case ok_b_verbose:
824 flag = (enable && !(n_poption & n_PO_VERB))
825 ? n_PO_VERB : n_PO_VERB | n_PO_VERBVERB;
826 break;
827 default:
828 DBG( n_err("Implementation error: never heard of %u\n", ok); )
829 break;
832 if(flag){
833 if(enable)
834 n_poption |= flag;
835 else
836 n_poption &= ~flag;
838 NYD2_LEAVE;
839 return ok;
842 static bool_t
843 a_amv_var_check_nocntrls(char const *val){
844 char c;
845 NYD2_ENTER;
847 while((c = *val++) != '\0')
848 if(cntrlchar(c))
849 break;
850 NYD2_LEAVE;
851 return (c == '\0');
854 static bool_t
855 a_amv_var_check_num(char const *val, bool_t posnum){
856 /* TODO The internal/environment variables which are num= or posnum= should
857 * TODO gain special lookup functions, or the return should be void* and
858 * TODO castable to integer; i.e. no more strtoX() should be needed.
859 * TODO I.e., the result of this function should instead be stored */
860 bool_t rv;
861 NYD2_ENTER;
863 rv = TRU1;
865 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
866 ui64_t uib;
867 enum n_idec_state ids;
869 ids = n_idec_cp(&uib, val, 0,
870 (posnum ? n_IDEC_MODE_SIGNED_TYPE : n_IDEC_MODE_NONE), NULL);
871 if((ids & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
872 ) != n_IDEC_STATE_CONSUMED)
873 rv = FAL0;
874 /* TODO Unless we store integers we need to look and forbid, because
875 * TODO callee may not be able to swallow, e.g., "-1" */
876 if(posnum && (ids & n_IDEC_STATE_SEEN_MINUS))
877 rv = FAL0;
879 NYD2_LEAVE;
880 return rv;
883 static char const *
884 a_amv_var_canonify(char const *vn){
885 NYD2_ENTER;
886 if(!upperchar(*vn)){
887 char const *vp;
889 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
891 vn = (*vp == '@') ? i_strdup(vn) : vn;
893 NYD2_LEAVE;
894 return vn;
897 static bool_t
898 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
899 ui32_t hash, i, j;
900 struct a_amv_var_map const *avmp;
901 char c;
902 NYD2_ENTER;
904 /* It may be a special a.k.a. macro-local or one-letter parameter */
905 c = name[0];
906 if(n_UNLIKELY(digitchar(c))){
907 /* (Inline dec. atoi, ugh) */
908 for(j = (ui8_t)c - '0', i = 1;; ++i){
909 c = name[i];
910 if(c == '\0')
911 break;
912 if(!digitchar(c))
913 goto jno_special_param;
914 j = j * 10 + (ui8_t)c - '0';
916 if(j <= SI16_MAX){
917 avcp->avc_is_special = TRUM1;
918 goto jspecial_param_m1;
920 }else if(n_UNLIKELY(name[1] == '\0')){
921 switch(c){
922 case '*':
923 j = a_AMV_VST_STAR;
924 goto jspecial_param;
925 case '@':
926 j = a_AMV_VST_AT;
927 goto jspecial_param;
928 case '#':
929 j = a_AMV_VST_NOSIGN;
930 goto jspecial_param;
931 case '?':
932 case '!':
933 /* Not function local, could also simply look it up, but faster */
934 avcp->avc_name = name;
935 if(c == '?'){
936 avmp = &a_amv_var_map[a_AMV_VAR__QM_MAP_IDX];
937 avcp->avc_okey = ok_v___qm;
938 }else{
939 avmp = &a_amv_var_map[a_AMV_VAR__EM_MAP_IDX];
940 avcp->avc_okey = ok_v___em;
942 avcp->avc_hash = avmp->avm_hash;
943 avcp->avc_map = avmp;
944 avcp->avc_is_special = FAL0;
945 goto jleave;
946 default:
947 break;
951 /* Normal reverse lookup, walk over the hashtable */
952 jno_special_param:
953 avcp->avc_is_special = FAL0;
954 avcp->avc_name = name = a_amv_var_canonify(name);
955 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
957 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
958 ui32_t x;
960 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
961 break;
963 avmp = &a_amv_var_map[x];
964 if(avmp->avm_hash == hash &&
965 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
966 avcp->avc_map = avmp;
967 avcp->avc_okey = (enum okeys)x;
968 goto jleave;
971 if(++i == a_AMV_VAR_REV_PRIME){
972 #ifdef a_AMV_VAR_REV_WRAPAROUND
973 i = 0;
974 #else
975 break;
976 #endif
979 avcp->avc_map = NULL;
980 avcp = NULL;
981 jleave:
982 NYD2_LEAVE;
983 return (avcp != NULL);
985 /* All these are mapped to *--special-param* */
986 jspecial_param:
987 avcp->avc_is_special = TRU1;
988 jspecial_param_m1:
989 avcp->avc_name = name;
990 avcp->avc_special_prop = (ui16_t)j;
991 avmp = &a_amv_var_map[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX];
992 avcp->avc_hash = avmp->avm_hash;
993 avcp->avc_map = avmp;
994 avcp->avc_okey = ok_v___special_param;
995 goto jleave;
998 static bool_t
999 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
1000 size_t i;
1001 char const *cp;
1002 struct a_amv_var_map const *avmp;
1003 struct a_amv_var *avp;
1004 NYD2_ENTER;
1006 /* C99 */{
1007 struct a_amv_var **avpp, *lavp;
1009 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
1011 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
1012 if(!strcmp(avp->av_name, avcp->avc_name)){
1013 /* Relink as head, hope it "sorts on usage" over time.
1014 * _clear() relies on this behaviour */
1015 if(lavp != NULL){
1016 lavp->av_link = avp->av_link;
1017 avp->av_link = *avpp;
1018 *avpp = avp;
1020 goto jleave;
1024 /* If this is not an assembled variable we need to consider some special
1025 * initialization cases and eventually create the variable anew */
1026 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1027 /* Does it have an import-from-environment flag? */
1028 if(n_UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
1029 if(n_LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
1030 /* May be better not to use that one, though? */
1031 bool_t isempty, isbltin;
1033 isempty = (*cp == '\0' &&
1034 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
1035 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
1036 ) != 0);
1038 if(n_UNLIKELY(isempty)){
1039 if(!isbltin)
1040 goto jerr;
1041 }else if(n_LIKELY(*cp != '\0')){
1042 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
1043 !a_amv_var_check_nocntrls(cp))){
1044 n_err(_("Ignoring environment, control characters "
1045 "invalid in variable: %s\n"), avcp->avc_name);
1046 goto jerr;
1048 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1049 !a_amv_var_check_num(cp, FAL0))){
1050 n_err(_("Environment variable value not a number "
1051 "or out of range: %s\n"), avcp->avc_name);
1052 goto jerr;
1054 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1055 !a_amv_var_check_num(cp, TRU1))){
1056 n_err(_("Environment variable value not a number, "
1057 "negative or out of range: %s\n"), avcp->avc_name);
1058 goto jerr;
1060 goto jnewval;
1061 }else
1062 goto jnewval;
1066 /* A first-time init switch is to be handled now and here */
1067 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
1068 static struct a_amv_var_defval const **arr,
1069 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
1071 if(arr == NULL){
1072 arr = &arr_base[0];
1073 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1074 while(i-- > 0)
1075 arr[i] = &a_amv_var_i3vals[i];
1078 for(i = 0; arr[i] != NULL; ++i)
1079 if(arr[i]->avdv_okey == avcp->avc_okey){
1080 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1081 : arr[i]->avdv_value;
1082 /* Remove this entry, hope entire block becomes no-op asap */
1084 arr[i] = arr[i + 1];
1085 while(arr[i++] != NULL);
1087 if(!i3val_nonew)
1088 goto jnewval;
1089 if(i3val_nonew == TRUM1)
1090 avp = (struct a_amv_var*)-1;
1091 goto jleave;
1095 /* The virtual variables */
1096 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
1097 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
1098 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
1099 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
1100 goto jleave;
1102 /* Not reached */
1105 /* Place this last because once it is set first the variable will never
1106 * be removed again and thus match in the first block above */
1107 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
1108 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
1109 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
1110 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1111 : a_amv_var_defvals[i].avdv_value;
1112 goto jnewval;
1117 jerr:
1118 avp = NULL;
1119 jleave:
1120 avcp->avc_var = avp;
1121 NYD2_LEAVE;
1122 return (avp != NULL);
1124 jnewval: /* C99 */{
1125 struct a_amv_var **avpp;
1126 size_t l;
1128 l = strlen(avcp->avc_name) +1;
1129 avcp->avc_var =
1130 avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name) + l);
1131 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1132 *avpp = avp;
1133 memcpy(avp->av_name, avcp->avc_name, l);
1134 avp->av_value = a_amv_var_copy(cp);
1135 #ifdef HAVE_PUTENV
1136 avp->av_env = NULL;
1137 #endif
1138 avp->av_flags = avmp->avm_flags;
1140 if(avp->av_flags & a_AMV_VF_VIP)
1141 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1142 if(avp->av_flags & a_AMV_VF_ENV)
1143 a_amv_var__putenv(avcp, avp);
1144 goto jleave;
1148 static bool_t
1149 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1150 bool_t force_env){
1151 struct a_amv_var *avp;
1152 char *oval;
1153 struct a_amv_var_map const *avmp;
1154 bool_t rv;
1155 NYD2_ENTER;
1157 if(value == NULL){
1158 rv = a_amv_var_clear(avcp, force_env);
1159 goto jleave;
1162 if((avmp = avcp->avc_map) != NULL){
1163 rv = FAL0;
1165 /* Validity checks */
1166 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1167 !(n_pstate & n_PS_ROOT))){
1168 value = N_("Variable is read-only: %s\n");
1169 goto jeavmp;
1171 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1172 value = N_("Variable must not be empty: %s\n");
1173 goto jeavmp;
1175 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1176 !a_amv_var_check_nocntrls(value))){
1177 value = N_("Variable forbids control characters: %s\n");
1178 goto jeavmp;
1180 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1181 !a_amv_var_check_num(value, FAL0))){
1182 value = N_("Variable value not a number or out of range: %s\n");
1183 goto jeavmp;
1185 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1186 !a_amv_var_check_num(value, TRU1))){
1187 value = _("Variable value not a number, negative, "
1188 "or out of range: %s\n");
1189 goto jeavmp;
1191 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1192 !(n_psonce & n_PSO_STARTED) && !(n_pstate & n_PS_ROOT))){
1193 value = N_("Variable cannot be set in a resource file: %s\n");
1194 jeavmp:
1195 n_err(V_(value), avcp->avc_name);
1196 goto jleave;
1199 /* Transformations */
1200 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_LOWER)){
1201 char c;
1203 oval = savestr(value);
1204 value = oval;
1205 for(; (c = *oval) != '\0'; ++oval)
1206 *oval = lowerconv(c);
1210 rv = TRU1;
1211 a_amv_var_lookup(avcp, TRU1);
1213 /* Don't care what happens later on, store this in the unroll list */
1214 if(a_amv_lopts != NULL &&
1215 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1216 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1218 if((avp = avcp->avc_var) == NULL){
1219 struct a_amv_var **avpp;
1220 size_t l;
1222 l = strlen(avcp->avc_name) +1;
1223 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1224 ) + l);
1225 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1226 *avpp = avp;
1227 #ifdef HAVE_PUTENV
1228 avp->av_env = NULL;
1229 #endif
1230 memcpy(avp->av_name, avcp->avc_name, l);
1231 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1232 oval = n_UNCONST(n_empty);
1233 }else
1234 oval = avp->av_value;
1236 if(avmp == NULL)
1237 avp->av_value = a_amv_var_copy(value);
1238 else{
1239 /* Via `set' etc. the user may give even boolean options non-boolean
1240 * values, ignore that and force boolean */
1241 if(avp->av_flags & a_AMV_VF_BOOL){
1242 if(!(n_pstate & n_PS_ROOT) && (n_poption & n_PO_D_VV) &&
1243 *value != '\0')
1244 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1245 avcp->avc_name, value);
1246 avp->av_value = n_UNCONST(n_1);
1247 }else
1248 avp->av_value = a_amv_var_copy(value);
1250 /* Check if update allowed XXX wasteful on error! */
1251 if((avp->av_flags & a_AMV_VF_VIP) &&
1252 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1253 char *cp = avp->av_value;
1255 avp->av_value = oval;
1256 oval = cp;
1260 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1261 avp->av_flags |= a_AMV_VF_LINKED;
1262 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1263 rv = a_amv_var__putenv(avcp, avp);
1265 a_amv_var_free(oval);
1266 jleave:
1267 NYD2_LEAVE;
1268 return rv;
1271 static bool_t
1272 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1273 #ifndef HAVE_SETENV
1274 char *cp;
1275 #endif
1276 bool_t rv;
1277 NYD2_ENTER;
1279 #ifdef HAVE_SETENV
1280 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1281 #else
1282 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1284 if((rv = (putenv(cp) == 0))){
1285 char *ocp;
1287 if((ocp = avp->av_env) != NULL)
1288 free(ocp);
1289 avp->av_env = cp;
1290 }else
1291 free(cp);
1292 #endif
1293 NYD2_LEAVE;
1294 return rv;
1297 static bool_t
1298 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1299 struct a_amv_var **avpp, *avp;
1300 struct a_amv_var_map const *avmp;
1301 bool_t rv;
1302 NYD2_ENTER;
1304 rv = FAL0;
1306 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1307 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1308 !(n_pstate & n_PS_ROOT))){
1309 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1310 goto jleave;
1312 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1313 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1314 goto jleave;
1317 rv = TRU1;
1319 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1320 if(force_env){
1321 jforce_env:
1322 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1323 }else if(!(n_pstate & (n_PS_ROOT | n_PS_ROBOT)) && (n_poption & n_PO_D_V))
1324 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1325 goto jleave;
1326 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1327 avcp->avc_var = NULL;
1328 if(force_env)
1329 goto jforce_env;
1330 goto jleave;
1333 if(a_amv_lopts != NULL &&
1334 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1335 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1337 avp = avcp->avc_var;
1338 avcp->avc_var = NULL;
1339 avpp = &a_amv_vars[avcp->avc_prime];
1340 assert(*avpp == avp); /* (always listhead after lookup()) */
1341 *avpp = (*avpp)->av_link;
1343 /* C99 */{
1344 #ifdef HAVE_SETENV
1345 char *envval = NULL;
1346 #else
1347 char *envval = avp->av_env;
1348 #endif
1349 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1350 rv = a_amv_var__clearenv(avp->av_name, envval);
1352 a_amv_var_free(avp->av_value);
1353 free(avp);
1355 /* XXX Fun part, extremely simple-minded for now: if this variable has
1356 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1357 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1358 a_amv_var_lookup(avcp, TRU1);
1359 jleave:
1360 NYD2_LEAVE;
1361 return rv;
1364 static bool_t
1365 a_amv_var__clearenv(char const *name, char *value){
1366 #ifndef HAVE_SETENV
1367 extern char **environ;
1368 char **ecpp;
1369 #endif
1370 bool_t rv;
1371 NYD2_ENTER;
1372 n_UNUSED(value);
1374 #ifdef HAVE_SETENV
1375 unsetenv(name);
1376 rv = TRU1;
1377 #else
1378 if(value != NULL)
1379 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1380 if(*ecpp == value){
1381 free(value);
1383 ecpp[0] = ecpp[1];
1384 while(*ecpp++ != NULL);
1385 break;
1387 rv = TRU1;
1388 #endif
1389 NYD2_LEAVE;
1390 return rv;
1393 static void
1394 a_amv_var_show_all(void){
1395 struct n_string msg, *msgp;
1396 FILE *fp;
1397 size_t no, i;
1398 struct a_amv_var *avp;
1399 char const **vacp, **cap;
1400 NYD2_ENTER;
1402 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1403 n_perr(_("Can't create temporary file for `set' listing"), 0);
1404 goto jleave;
1407 /* We need to instantiate first-time-inits and default values here, so that
1408 * they will be regular members of our _vars[] table */
1409 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1410 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1411 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1412 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1414 for(no = i = 0; i < a_AMV_PRIME; ++i)
1415 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1416 ++no;
1417 no += a_AMV_VAR_VIRTS_CNT;
1419 vacp = salloc(no * sizeof(*vacp));
1421 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1422 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1423 *cap++ = avp->av_name;
1424 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1425 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1427 if(no > 1)
1428 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1430 msgp = &msg;
1431 msgp = n_string_reserve(n_string_creat(msgp), 80);
1432 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1433 i += a_amv_var_show(*cap, fp, msgp);
1434 n_string_gut(&msg);
1436 page_or_print(fp, i);
1437 Fclose(fp);
1438 jleave:
1439 NYD2_LEAVE;
1442 static int
1443 a_amv_var__show_cmp(void const *s1, void const *s2){
1444 int rv;
1445 NYD2_ENTER;
1447 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1448 NYD2_LEAVE;
1449 return rv;
1452 static size_t
1453 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1454 struct a_amv_var_carrier avc;
1455 char const *quote;
1456 bool_t isset;
1457 size_t i;
1458 NYD2_ENTER;
1460 msgp = n_string_trunc(msgp, 0);
1461 i = 0;
1463 a_amv_var_revlookup(&avc, name);
1464 isset = a_amv_var_lookup(&avc, FAL0);
1466 if(n_poption & n_PO_D_V){
1467 if(avc.avc_map == NULL){
1468 msgp = n_string_push_cp(msgp, "#assembled variable with value");
1469 i = 1;
1470 }else{
1471 struct{
1472 ui16_t flag;
1473 char msg[22];
1474 } const tbase[] = {
1475 {a_AMV_VF_VIRT, "virtual"},
1476 {a_AMV_VF_RDONLY, "read-only"},
1477 {a_AMV_VF_NODEL, "nodelete"},
1478 {a_AMV_VF_NOTEMPTY, "notempty"},
1479 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1480 {a_AMV_VF_NUM, "number"},
1481 {a_AMV_VF_POSNUM, "positive-number"},
1482 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1483 {a_AMV_VF_ENV, "sync-environ"},
1484 {a_AMV_VF_I3VAL, "initial-value"},
1485 {a_AMV_VF_DEFVAL, "default-value"},
1486 {a_AMV_VF_LINKED, "`environ' link"}
1487 }, *tp;
1489 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1490 if(isset ? (avc.avc_var->av_flags & tp->flag)
1491 : (avc.avc_map->avm_flags & tp->flag)){
1492 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1493 msgp = n_string_push_cp(msgp, tp->msg);
1496 if(i > 0)
1497 msgp = n_string_push_cp(msgp, "\n ");
1500 if(!isset || (avc.avc_var->av_flags & a_AMV_VF_RDONLY)){
1501 msgp = n_string_push_cp(msgp, "#");
1502 if(!isset){
1503 if(avc.avc_map != NULL && (avc.avc_map->avm_flags & a_AMV_VF_BOOL))
1504 msgp = n_string_push_cp(msgp, "boolean; ");
1505 msgp = n_string_push_cp(msgp, "variable not set: ");
1506 msgp = n_string_push_cp(msgp, n_shexp_quote_cp(name, FAL0));
1507 goto jleave;
1511 n_UNINIT(quote, NULL);
1512 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1513 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1514 if(strcmp(quote, avc.avc_var->av_value))
1515 msgp = n_string_push_cp(msgp, "wysh ");
1516 }else if(n_poption & n_PO_D_V)
1517 msgp = n_string_push_cp(msgp, "wysh ");
1518 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1519 msgp = n_string_push_cp(msgp, "environ ");
1520 msgp = n_string_push_cp(msgp, "set ");
1521 msgp = n_string_push_cp(msgp, name);
1522 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1523 msgp = n_string_push_c(msgp, '=');
1524 msgp = n_string_push_cp(msgp, quote);
1525 }else if(n_poption & n_PO_D_V)
1526 msgp = n_string_push_cp(msgp, " #boolean");
1528 jleave:
1529 msgp = n_string_push_c(msgp, '\n');
1530 fputs(n_string_cp(msgp), fp);
1531 NYD2_ENTER;
1532 return (i > 0 ? 2 : 1);
1535 static bool_t
1536 a_amv_var_c_set(char **ap, bool_t issetenv){
1537 char *cp, *cp2, *varbuf, c;
1538 size_t errs;
1539 NYD2_ENTER;
1541 errs = 0;
1542 jouter:
1543 while((cp = *ap++) != NULL){
1544 /* Isolate key */
1545 cp2 = varbuf = salloc(strlen(cp) +1);
1547 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1548 if(cntrlchar(c) || spacechar(c)){
1549 n_err(_("Variable name with control character ignored: %s\n"),
1550 ap[-1]);
1551 ++errs;
1552 goto jouter;
1554 *cp2++ = c;
1556 *cp2 = '\0';
1557 if(c == '\0')
1558 cp = n_UNCONST(n_empty);
1559 else
1560 ++cp;
1562 if(varbuf == cp2){
1563 n_err(_("Empty variable name ignored\n"));
1564 ++errs;
1565 }else{
1566 struct a_amv_var_carrier avc;
1567 bool_t isunset;
1569 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1570 varbuf = &varbuf[2];
1572 a_amv_var_revlookup(&avc, varbuf);
1574 if(isunset)
1575 errs += !a_amv_var_clear(&avc, issetenv);
1576 else
1577 errs += !a_amv_var_set(&avc, cp, issetenv);
1580 NYD2_LEAVE;
1581 return (errs == 0);
1584 FL int
1585 c_define(void *v){
1586 int rv;
1587 char **args;
1588 NYD_ENTER;
1590 rv = 1;
1592 if((args = v)[0] == NULL){
1593 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1594 goto jleave;
1597 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1598 args[2] != NULL){
1599 n_err(_("Synopsis: define: <name> {\n"));
1600 goto jleave;
1603 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1604 jleave:
1605 NYD_LEAVE;
1606 return rv;
1609 FL int
1610 c_undefine(void *v){
1611 int rv;
1612 char **args;
1613 NYD_ENTER;
1615 rv = 0;
1616 args = v;
1618 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1619 while(*++args != NULL);
1620 NYD_LEAVE;
1621 return rv;
1624 FL int
1625 c_call(void *v){
1626 int rv;
1627 NYD_ENTER;
1629 rv = a_amv_mac_call(v, FAL0);
1630 NYD_LEAVE;
1631 return rv;
1634 FL int
1635 c_call_if(void *v){
1636 int rv;
1637 NYD_ENTER;
1639 rv = a_amv_mac_call(v, TRU1);
1640 NYD_LEAVE;
1641 return rv;
1644 FL int
1645 c_account(void *v){
1646 struct a_amv_mac_call_args *amcap;
1647 struct a_amv_mac *amp;
1648 char **args;
1649 int rv, i, oqf, nqf;
1650 NYD_ENTER;
1652 rv = 1;
1654 if((args = v)[0] == NULL){
1655 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1656 goto jleave;
1659 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1660 if(args[2] != NULL){
1661 n_err(_("Synopsis: account: <name> {\n"));
1662 goto jleave;
1664 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1665 n_err(_("`account': cannot use reserved name: %s\n"),
1666 ACCOUNT_NULL);
1667 goto jleave;
1669 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1670 goto jleave;
1673 if(n_pstate & n_PS_HOOK_MASK){
1674 n_err(_("`account': can't change account from within a hook\n"));
1675 goto jleave;
1678 save_mbox_for_possible_quitstuff();
1680 amp = NULL;
1681 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1682 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1683 n_err(_("`account': account does not exist: %s\n"), args[0]);
1684 goto jleave;
1687 oqf = savequitflags();
1689 if(a_amv_acc_curr != NULL){
1690 if(a_amv_acc_curr->am_lopts != NULL)
1691 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1692 /* For accounts this lingers */
1693 --a_amv_acc_curr->am_refcnt;
1694 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1695 a_amv_mac_free(a_amv_acc_curr);
1698 a_amv_acc_curr = amp;
1700 if(amp != NULL){
1701 bool_t ok;
1702 assert(amp->am_lopts == NULL);
1703 amcap = n_lofi_alloc(sizeof *amcap);
1704 memset(amcap, 0, sizeof *amcap);
1705 amcap->amca_name = amp->am_name;
1706 amcap->amca_amp = amp;
1707 amcap->amca_unroller = &amp->am_lopts;
1708 amcap->amca_lopts_on = TRU1;
1709 ++amp->am_refcnt; /* We may not run 0 to avoid being deleted! */
1710 ok = a_amv_mac_exec(amcap);
1711 if(!ok){
1712 /* XXX account switch incomplete, unroll? */
1713 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1714 goto jleave;
1718 n_PS_ROOT_BLOCK((amp != NULL ? ok_vset(_account, amp->am_name)
1719 : ok_vclear(_account)));
1721 if((n_psonce & n_PSO_STARTED) && !(n_pstate & n_PS_HOOK_MASK)){
1722 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1723 restorequitflags(oqf);
1724 if((i = setfile("%", 0)) < 0)
1725 goto jleave;
1726 temporary_folder_hook_check(FAL0);
1727 if(i > 0 && !ok_blook(emptystart))
1728 goto jleave;
1729 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1730 restorequitflags(nqf);
1732 rv = 0;
1733 jleave:
1734 NYD_LEAVE;
1735 return rv;
1738 FL int
1739 c_unaccount(void *v){
1740 int rv;
1741 char **args;
1742 NYD_ENTER;
1744 rv = 0;
1745 args = v;
1747 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1748 while(*++args != NULL);
1749 NYD_LEAVE;
1750 return rv;
1753 FL int
1754 c_localopts(void *v){
1755 char **argv;
1756 int rv;
1757 NYD_ENTER;
1759 rv = 1;
1761 if(a_amv_lopts == NULL){
1762 n_err(_("Cannot use `localopts' in this context "
1763 "(not in `define' or `account', nor special hook)\n"));
1764 goto jleave;
1767 rv = 0;
1769 if(n_pstate & (n_PS_HOOK | n_PS_COMPOSE_MODE)){
1770 if(n_poption & n_PO_D_V)
1771 n_err(_("Cannot turn off `localopts' for compose-mode hooks\n"));
1772 goto jleave;
1775 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1776 jleave:
1777 NYD_LEAVE;
1778 return rv;
1781 FL int
1782 c_shift(void *v){
1783 int rv;
1784 NYD_ENTER;
1786 rv = 1;
1788 if(a_amv_lopts != NULL){
1789 ui16_t i, j;
1790 struct a_amv_mac const *amp;
1791 struct a_amv_mac_call_args *amcap;
1793 amp = (amcap = a_amv_lopts->as_amcap)->amca_amp;
1794 if(amp == NULL || amcap->amca_ps_hook_mask ||
1795 (amp->am_flags & a_AMV_MF_TYPE_MASK) == a_AMV_MF_ACC)
1796 goto jerr;
1798 v = *(char**)v;
1799 if(v == NULL)
1800 i = 1;
1801 else{
1802 si16_t sib;
1804 if((n_idec_si16_cp(&sib, v, 10, NULL
1805 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1806 ) != n_IDEC_STATE_CONSUMED || sib < 0){
1807 n_err(_("`shift': invalid argument: %s\n"), v);
1808 goto jleave;
1810 i = (ui16_t)sib;
1813 if(i > (j = amcap->amca_argc)){
1814 n_err(_("`shift': cannot shift %hu of %hu parameters\n"), i, j);
1815 goto jleave;
1816 }else{
1817 rv = 0;
1818 if(j > 0){
1819 j -= i;
1820 amcap->amca_argc = j;
1821 amcap->amca_argv += i;
1824 }else
1825 jerr:
1826 n_err(_("Can only use `shift' in a `call'ed macro\n"));
1827 jleave:
1828 NYD_LEAVE;
1829 return rv;
1832 FL int
1833 c_return(void *v){
1834 int rv;
1835 NYD_ENTER;
1837 rv = 1;
1839 if(a_amv_lopts != NULL){
1840 char const **argv;
1842 n_source_force_eof();
1844 if((argv = v)[0] != NULL){
1845 si32_t i;
1847 if((n_idec_si32_cp(&i, argv[0], 10, NULL
1848 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1849 ) == n_IDEC_STATE_CONSUMED && i >= 0)
1850 n_pstate_var__em = argv[0];
1851 else
1852 n_err(_("`return': argument one is invalid: %s\n"), argv[0]);
1854 if(argv[1] != NULL){
1855 if((n_idec_si32_cp(&i, argv[1], 10, NULL
1856 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1857 ) == n_IDEC_STATE_CONSUMED && i >= 0)
1858 rv = (int)i;
1859 else{
1860 n_err(_("`return': argument two is invalid: %s\n"), argv[1]);
1861 n_pstate_var__em = n_1;
1863 }else
1864 rv = 0;
1865 }else{
1866 rv = 0;
1867 n_pstate_var__em = n_0;
1869 }else
1870 n_err(_("Can only use `return' in a macro\n"));
1871 NYD_LEAVE;
1872 return rv;
1875 FL bool_t
1876 temporary_folder_hook_check(bool_t nmail){ /* TODO temporary, v15: drop */
1877 struct a_amv_mac_call_args *amcap;
1878 struct a_amv_mac *amp;
1879 size_t len;
1880 char const *cp;
1881 char *var;
1882 bool_t rv;
1883 NYD_ENTER;
1885 rv = TRU1;
1886 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1888 /* First try the fully resolved path */
1889 snprintf(var, len, "folder-hook-%s", mailname);
1890 if((cp = n_var_vlook(var, FAL0)) != NULL)
1891 goto jmac;
1893 /* If we are under *folder*, try the usual +NAME syntax, too */
1894 if(displayname[0] == '+'){
1895 char *x;
1897 for(x = &mailname[len]; x != mailname; --x)
1898 if(x[-1] == '/'){
1899 snprintf(var, len, "folder-hook-+%s", x);
1900 if((cp = n_var_vlook(var, FAL0)) != NULL)
1901 goto jmac;
1902 break;
1906 /* Plain *folder-hook* is our last try */
1907 if((cp = ok_vlook(folder_hook)) == NULL)
1908 goto jleave;
1910 jmac:
1911 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1912 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1913 n_shexp_quote_cp(displayname, FAL0), cp);
1914 rv = FAL0;
1915 goto jleave;
1918 amcap = n_lofi_alloc(sizeof *amcap);
1919 memset(amcap, 0, sizeof *amcap);
1920 amcap->amca_name = cp;
1921 amcap->amca_amp = amp;
1922 n_pstate &= ~n_PS_HOOK_MASK;
1923 if(nmail){
1924 amcap->amca_unroller = NULL;
1925 n_pstate |= n_PS_HOOK_NEWMAIL;
1926 }else{
1927 amcap->amca_unroller = &a_amv_folder_hook_lopts;
1928 n_pstate |= n_PS_HOOK;
1930 amcap->amca_lopts_on = TRU1;
1931 amcap->amca_ps_hook_mask = TRU1;
1932 rv = a_amv_mac_exec(amcap);
1933 n_pstate &= ~n_PS_HOOK_MASK;
1935 jleave:
1936 NYD_LEAVE;
1937 return rv;
1940 FL void
1941 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
1942 NYD_ENTER;
1943 if(a_amv_folder_hook_lopts != NULL){
1944 void *save = a_amv_lopts;
1946 a_amv_lopts = NULL;
1947 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1948 a_amv_folder_hook_lopts = NULL;
1949 a_amv_lopts = save;
1951 NYD_LEAVE;
1954 FL void
1955 temporary_compose_mode_hook_call(char const *macname,
1956 void (*hook_pre)(void *), void *hook_arg){
1957 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_LEX_SLICE
1958 * TODO comment in lex_input.c for the right way of doing things! */
1959 static struct a_amv_lostack *cmh_losp;
1960 struct a_amv_mac_call_args *amcap;
1961 struct a_amv_mac *amp;
1962 NYD_ENTER;
1964 amp = NULL;
1966 if(macname == (char*)-1){
1967 a_amv_mac__finalize(cmh_losp);
1968 cmh_losp = NULL;
1969 }else if(macname != NULL &&
1970 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1971 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1972 macname);
1973 else{
1974 amcap = n_lofi_alloc(sizeof *amcap);
1975 memset(amcap, 0, sizeof *amcap);
1976 amcap->amca_name = (macname != NULL) ? macname : "on-compose-done-shell";
1977 amcap->amca_amp = amp;
1978 amcap->amca_unroller = &a_amv_compose_lopts;
1979 amcap->amca_hook_pre = hook_pre;
1980 amcap->amca_hook_arg = hook_arg;
1981 amcap->amca_lopts_on = TRU1;
1982 amcap->amca_ps_hook_mask = TRU1;
1983 n_pstate &= ~n_PS_HOOK_MASK;
1984 n_pstate |= n_PS_HOOK;
1985 if(macname != NULL)
1986 a_amv_mac_exec(amcap);
1987 else{
1988 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
1989 cmh_losp->as_global_saved = a_amv_lopts;
1990 cmh_losp->as_up = NULL;
1991 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
1992 cmh_losp->as_unroll = TRU1;
1993 a_amv_lopts = cmh_losp;
1996 NYD_LEAVE;
1999 FL void
2000 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2001 NYD_ENTER;
2002 if(a_amv_compose_lopts != NULL){
2003 void *save = a_amv_lopts;
2005 a_amv_lopts = NULL;
2006 a_amv_lopts_unroll(&a_amv_compose_lopts);
2007 a_amv_compose_lopts = NULL;
2008 a_amv_lopts = save;
2010 NYD_LEAVE;
2013 FL bool_t
2014 n_var_is_user_writable(char const *name){
2015 struct a_amv_var_carrier avc;
2016 struct a_amv_var_map const *avmp;
2017 bool_t rv;
2018 NYD_ENTER;
2020 a_amv_var_revlookup(&avc, name);
2021 if((avmp = avc.avc_map) == NULL)
2022 rv = TRU1;
2023 else
2024 rv = ((avmp->avm_flags & (a_AMV_VF_BOOL | a_AMV_VF_RDONLY)) == 0);
2025 NYD_LEAVE;
2026 return rv;
2029 FL char *
2030 n_var_oklook(enum okeys okey){
2031 struct a_amv_var_carrier avc;
2032 char *rv;
2033 struct a_amv_var_map const *avmp;
2034 NYD_ENTER;
2036 avc.avc_map = avmp = &a_amv_var_map[okey];
2037 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2038 avc.avc_hash = avmp->avm_hash;
2039 avc.avc_okey = okey;
2041 if(a_amv_var_lookup(&avc, FAL0))
2042 rv = avc.avc_var->av_value;
2043 else
2044 rv = NULL;
2045 NYD_LEAVE;
2046 return rv;
2049 FL bool_t
2050 n_var_okset(enum okeys okey, uintptr_t val){
2051 struct a_amv_var_carrier avc;
2052 bool_t ok;
2053 struct a_amv_var_map const *avmp;
2054 NYD_ENTER;
2056 avc.avc_map = avmp = &a_amv_var_map[okey];
2057 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2058 avc.avc_hash = avmp->avm_hash;
2059 avc.avc_okey = okey;
2061 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2062 NYD_LEAVE;
2063 return ok;
2066 FL bool_t
2067 n_var_okclear(enum okeys okey){
2068 struct a_amv_var_carrier avc;
2069 bool_t rv;
2070 struct a_amv_var_map const *avmp;
2071 NYD_ENTER;
2073 avc.avc_map = avmp = &a_amv_var_map[okey];
2074 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2075 avc.avc_hash = avmp->avm_hash;
2076 avc.avc_okey = okey;
2078 rv = a_amv_var_clear(&avc, FAL0);
2079 NYD_LEAVE;
2080 return rv;
2083 FL char const *
2084 n_var_vlook(char const *vokey, bool_t try_getenv){
2085 struct a_amv_var_carrier avc;
2086 char const *rv;
2087 NYD_ENTER;
2089 a_amv_var_revlookup(&avc, vokey);
2090 rv = NULL;
2092 /* Here, and only here we need to take care for the special macro-local
2093 * parameters... (except for n_var_vexplode()) */
2094 if(n_LIKELY(!avc.avc_is_special)){
2095 if(a_amv_var_lookup(&avc, FAL0))
2096 rv = avc.avc_var->av_value;
2097 /* Only check the environment for something that is otherwise unknown */
2098 else if(try_getenv && avc.avc_map == NULL)
2099 rv = getenv(vokey);
2100 }else{
2101 bool_t ismacky;
2102 struct a_amv_mac_call_args *amcap;
2104 /* These may occur only in a macro.. */
2105 if(n_UNLIKELY(a_amv_lopts == NULL))
2106 goto jmaylog;
2108 amcap = a_amv_lopts->as_amcap;
2110 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
2111 if((ismacky = (amcap->amca_amp == a_AMV_MACKY_MACK)) ||
2112 (!amcap->amca_ps_hook_mask &&
2113 (assert(amcap->amca_amp != NULL), TRU1) &&
2114 (amcap->amca_amp->am_flags & a_AMV_MF_TYPE_MASK) != a_AMV_MF_ACC)){
2115 if(avc.avc_is_special == TRUM1){
2116 if(avc.avc_special_prop > 0){
2117 if(amcap->amca_argc >= avc.avc_special_prop)
2118 rv = amcap->amca_argv[avc.avc_special_prop - 1];
2119 }else if(ismacky)
2120 rv = amcap->amca_name;
2121 else
2122 rv = (a_amv_lopts->as_up != NULL
2123 ? a_amv_lopts->as_up->as_amcap->amca_name : n_empty);
2125 /* MACKY_MACK doesn't know about [*@#] */
2126 else if(ismacky)
2127 goto jmaylog;
2128 else switch(avc.avc_special_prop){
2129 case a_AMV_VST_STAR:
2130 case a_AMV_VST_AT:{
2131 ui32_t i, l;
2133 for(i = l = 0; i < amcap->amca_argc; ++i)
2134 l += strlen(amcap->amca_argv[i]) + 1;
2135 if(l == 0)
2136 rv = n_empty;
2137 else{
2138 char *cp;
2140 rv = cp = salloc(l);
2141 for(i = l = 0; i < amcap->amca_argc; ++i){
2142 l = strlen(amcap->amca_argv[i]);
2143 memcpy(cp, amcap->amca_argv[i], l);
2144 cp += l;
2145 *cp++ = ' ';
2147 *--cp = '\0';
2149 } break;
2150 case a_AMV_VST_NOSIGN:{
2151 char *cp;
2153 rv = cp = salloc(sizeof("65535"));
2154 snprintf(cp, sizeof("65535"), "%hu", amcap->amca_argc);
2155 } break;
2156 default:
2157 rv = n_empty;
2158 break;
2160 }else{
2161 jmaylog:
2162 if(n_poption & n_PO_D_V)
2163 n_err(_("Cannot use macro local variable in this context: %s\n"),
2164 n_shexp_quote_cp(vokey, FAL0));
2167 NYD_LEAVE;
2168 return rv;
2171 FL bool_t
2172 n_var_vexplode(void const **cookie){
2173 NYD_ENTER;
2174 /* These may occur only in a macro.. */
2175 *cookie = (a_amv_lopts != NULL) ? a_amv_lopts->as_amcap->amca_argv : NULL;
2176 NYD_LEAVE;
2177 return (*cookie != NULL);
2180 FL bool_t
2181 n_var_vset(char const *vokey, uintptr_t val){
2182 struct a_amv_var_carrier avc;
2183 bool_t ok;
2184 NYD_ENTER;
2186 a_amv_var_revlookup(&avc, vokey);
2188 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2189 NYD_LEAVE;
2190 return ok;
2193 FL bool_t
2194 n_var_vclear(char const *vokey){
2195 struct a_amv_var_carrier avc;
2196 bool_t ok;
2197 NYD_ENTER;
2199 a_amv_var_revlookup(&avc, vokey);
2201 ok = a_amv_var_clear(&avc, FAL0);
2202 NYD_LEAVE;
2203 return ok;
2206 #ifdef HAVE_SOCKETS
2207 FL char *
2208 n_var_xoklook(enum okeys okey, struct url const *urlp,
2209 enum okey_xlook_mode oxm){
2210 struct a_amv_var_carrier avc;
2211 struct str const *us;
2212 size_t nlen;
2213 char *nbuf, *rv;
2214 struct a_amv_var_map const *avmp;
2215 NYD_ENTER;
2217 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
2219 /* For simplicity: allow this case too */
2220 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
2221 nbuf = NULL;
2222 goto jplain;
2225 avc.avc_map = avmp = &a_amv_var_map[okey];
2226 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2227 avc.avc_okey = okey;
2229 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
2230 nlen = strlen(avc.avc_name);
2231 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
2232 memcpy(nbuf, avc.avc_name, nlen);
2233 nbuf[nlen++] = '-';
2235 /* One of .url_u_h_p and .url_h_p we test in here */
2236 memcpy(nbuf + nlen, us->s, us->l +1);
2237 avc.avc_name = a_amv_var_canonify(nbuf);
2238 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2239 if(a_amv_var_lookup(&avc, FAL0))
2240 goto jvar;
2242 /* The second */
2243 if(oxm & OXM_H_P){
2244 us = &urlp->url_h_p;
2245 memcpy(nbuf + nlen, us->s, us->l +1);
2246 avc.avc_name = a_amv_var_canonify(nbuf);
2247 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2248 if(a_amv_var_lookup(&avc, FAL0)){
2249 jvar:
2250 rv = avc.avc_var->av_value;
2251 goto jleave;
2255 jplain:
2256 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2257 jleave:
2258 if(nbuf != NULL)
2259 n_lofi_free(nbuf);
2260 NYD_LEAVE;
2261 return rv;
2263 #endif /* HAVE_SOCKETS */
2265 FL int
2266 c_set(void *v){
2267 char **ap;
2268 int err;
2269 NYD_ENTER;
2271 if(*(ap = v) == NULL){
2272 a_amv_var_show_all();
2273 err = 0;
2274 }else
2275 err = !a_amv_var_c_set(ap, FAL0);
2276 NYD_LEAVE;
2277 return err;
2280 FL int
2281 c_unset(void *v){
2282 char **ap;
2283 int err;
2284 NYD_ENTER;
2286 for(err = 0, ap = v; *ap != NULL; ++ap)
2287 err |= !n_var_vclear(*ap);
2288 NYD_LEAVE;
2289 return err;
2292 FL int
2293 c_varshow(void *v){
2294 char **ap;
2295 NYD_ENTER;
2297 if(*(ap = v) == NULL)
2298 v = NULL;
2299 else{
2300 struct n_string msg, *msgp = &msg;
2302 msgp = n_string_creat(msgp);
2303 for(; *ap != NULL; ++ap)
2304 a_amv_var_show(*ap, n_stdout, msgp);
2305 n_string_gut(msgp);
2307 NYD_LEAVE;
2308 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2311 FL int
2312 c_varedit(void *v){
2313 struct a_amv_var_carrier avc;
2314 FILE *of, *nf;
2315 char *val, **argv;
2316 int err;
2317 sighandler_type sigint;
2318 NYD_ENTER;
2320 sigint = safe_signal(SIGINT, SIG_IGN);
2322 for(err = 0, argv = v; *argv != NULL; ++argv){
2323 memset(&avc, 0, sizeof avc);
2325 a_amv_var_revlookup(&avc, *argv);
2327 if(avc.avc_map != NULL){
2328 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2329 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2330 avc.avc_name);
2331 continue;
2333 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2334 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2335 avc.avc_name);
2336 continue;
2340 a_amv_var_lookup(&avc, FAL0);
2342 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2343 NULL){
2344 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2345 err = 1;
2346 break;
2347 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2348 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2349 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2350 Fclose(of);
2351 err = 1;
2352 continue;
2355 fflush_rewind(of);
2356 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2357 Fclose(of);
2359 if(nf != NULL){
2360 int c;
2361 char *base;
2362 off_t l;
2364 l = fsize(nf);
2365 assert(l >= 0);
2366 base = salloc((size_t)l +1);
2368 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2369 if(c == '\n' || c == '\r'){
2370 *val = ' ';
2371 ++l;
2372 }else{
2373 *val = (char)(uc_i)c;
2374 l = 0;
2376 val -= l;
2377 *val = '\0';
2379 if(!a_amv_var_set(&avc, base, FAL0))
2380 err = 1;
2382 Fclose(nf);
2383 }else{
2384 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2385 err = 1;
2386 break;
2390 safe_signal(SIGINT, sigint);
2391 NYD_LEAVE;
2392 return err;
2395 FL int
2396 c_environ(void *v){
2397 struct a_amv_var_carrier avc;
2398 int err;
2399 char **ap;
2400 bool_t islnk;
2401 NYD_ENTER;
2403 if((islnk = is_asccaseprefix(*(ap = v), "link")) ||
2404 is_asccaseprefix(*ap, "unlink")){
2405 for(err = 0; *++ap != NULL;){
2406 a_amv_var_revlookup(&avc, *ap);
2408 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2409 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2410 if(!islnk){
2411 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2412 continue;
2413 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2414 if(n_poption & n_PO_D_V)
2415 n_err(_("`environ': link: already established: %s\n"), *ap);
2416 continue;
2418 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2419 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2420 a_amv_var__putenv(&avc, avc.avc_var);
2421 }else if(!islnk){
2422 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2423 err = 1;
2424 }else{
2425 char const *evp = getenv(*ap);
2427 if(evp != NULL)
2428 err |= !a_amv_var_set(&avc, evp, TRU1);
2429 else{
2430 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2431 *ap);
2432 err = 1;
2436 }else if(is_asccaseprefix(*ap, "set"))
2437 err = !a_amv_var_c_set(++ap, TRU1);
2438 else if(is_asccaseprefix(*ap, "unset")){
2439 for(err = 0; *++ap != NULL;){
2440 a_amv_var_revlookup(&avc, *ap);
2442 if(!a_amv_var_clear(&avc, TRU1))
2443 err = 1;
2445 }else{
2446 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2447 err = 1;
2449 NYD_LEAVE;
2450 return err;
2453 FL int
2454 c_vexpr(void *v){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2455 /* This needs to be here because we need to apply MACKY_MACK for
2456 * search+replace regular expression support :( */
2457 size_t i;
2458 enum n_idec_state ids;
2459 si64_t lhv, rhv;
2460 char op, varbuf[64 + 64 / 8 +1];
2461 char const **argv, *varname, *varres, *cp;
2462 enum{
2463 a_ERR = 1<<0,
2464 a_SOFTERR = 1<<1,
2465 a_ISNUM = 1<<2,
2466 a_ISDECIMAL = 1<<3, /* Print only decimal result */
2467 a_SATURATED = 1<<4,
2468 a_ICASE = 1<<5,
2469 a_UNSIGNED = 1<<6, /* Unsigned right shift (share bit ok) */
2470 a_TMP = 1<<30
2471 } f;
2472 NYD_ENTER;
2474 f = a_ERR;
2475 argv = v;
2476 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *argv++ : NULL;
2477 n_UNINIT(varres, n_empty);
2479 if((cp = argv[0])[0] == '\0')
2480 goto jesubcmd;
2482 if(cp[1] == '\0'){
2483 op = cp[0];
2484 jnumop:
2485 f |= a_ISNUM;
2486 switch(op){
2487 case '=':
2488 case '~':
2489 if(argv[1] == NULL || argv[2] != NULL)
2490 goto jesynopsis;
2492 if(*(cp = *++argv) == '\0')
2493 lhv = 0;
2494 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2495 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2496 ) != n_IDEC_STATE_CONSUMED){
2497 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2498 goto jenum_range;
2499 f |= a_SOFTERR;
2500 break;
2502 if(op == '~')
2503 lhv = ~lhv;
2504 break;
2506 case '+':
2507 case '-':
2508 case '*':
2509 case '/':
2510 case '%':
2511 case '|':
2512 case '&':
2513 case '^':
2514 case '<':
2515 case '>':
2516 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2517 goto jesynopsis;
2518 else{
2519 char xop;
2521 if(*(cp = *++argv) == '\0')
2522 lhv = 0;
2523 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2524 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2525 ) != n_IDEC_STATE_CONSUMED){
2526 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2527 goto jenum_range;
2528 f |= a_SOFTERR;
2529 break;
2532 if(*(cp = *++argv) == '\0')
2533 rhv = 0;
2534 else if(((ids = n_idec_si64_cp(&rhv, cp, 0, NULL)
2535 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2536 ) != n_IDEC_STATE_CONSUMED){
2537 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2538 goto jenum_range;
2539 f |= a_SOFTERR;
2540 lhv = rhv;
2541 break;
2544 xop = op;
2545 jnumop_again:
2546 switch(xop){
2547 case '+':
2548 if(rhv < 0){
2549 if(rhv != SI64_MIN){
2550 rhv = -rhv;
2551 xop = '-';
2552 goto jnumop_again;
2553 }else if(lhv < 0)
2554 goto jeplusminus;
2555 else if(lhv == 0){
2556 lhv = rhv;
2557 break;
2560 if(SI64_MAX - rhv < lhv)
2561 goto jeplusminus;
2562 lhv += rhv;
2563 break;
2564 case '-':
2565 if(rhv < 0){
2566 if(rhv != SI64_MIN){
2567 rhv = -rhv;
2568 xop = '+';
2569 goto jnumop_again;
2570 }else if(lhv > 0)
2571 goto jeplusminus;
2572 else if(lhv == 0){
2573 lhv = rhv;
2574 break;
2577 if(SI64_MIN + rhv > lhv){
2578 jeplusminus:
2579 if(!(f & a_SATURATED))
2580 goto jenum_overflow;
2581 f |= a_SOFTERR;
2582 lhv = (lhv < 0 || xop == '-') ? SI64_MIN : SI64_MAX;
2583 }else
2584 lhv -= rhv;
2585 break;
2586 case '*':
2587 /* Will the result be positive? */
2588 if((lhv < 0) == (rhv < 0)){
2589 if(lhv > 0){
2590 lhv = -lhv;
2591 rhv = -rhv;
2593 if(rhv != 0 && lhv != 0 && SI64_MAX / rhv > lhv){
2594 if(!(f & a_SATURATED))
2595 goto jenum_overflow;
2596 f |= a_SOFTERR;
2597 lhv = SI64_MAX;
2598 }else
2599 lhv *= rhv;
2600 }else{
2601 if(rhv > 0){
2602 if(lhv != 0 && SI64_MIN / lhv < rhv){
2603 if(!(f & a_SATURATED))
2604 goto jenum_overflow;
2605 f |= a_SOFTERR;
2606 lhv = SI64_MIN;
2607 }else
2608 lhv *= rhv;
2609 }else{
2610 if(rhv != 0 && lhv != 0 && SI64_MIN / rhv < lhv){
2611 if(!(f & a_SATURATED))
2612 goto jenum_overflow;
2613 f |= a_SOFTERR;
2614 lhv = SI64_MIN;
2615 }else
2616 lhv *= rhv;
2619 break;
2620 case '/':
2621 if(rhv == 0){
2622 if(!(f & a_SATURATED))
2623 goto jenum_range;
2624 f |= a_SOFTERR;
2625 lhv = SI64_MAX;
2626 }else
2627 lhv /= rhv;
2628 break;
2629 case '%':
2630 if(rhv == 0){
2631 if(!(f & a_SATURATED))
2632 goto jenum_range;
2633 f |= a_SOFTERR;
2634 lhv = SI64_MAX;
2635 }else
2636 lhv %= rhv;
2637 break;
2638 case '|':
2639 lhv |= rhv;
2640 break;
2641 case '&':
2642 lhv &= rhv;
2643 break;
2644 case '^':
2645 lhv ^= rhv;
2646 break;
2647 case '<':
2648 case '>':
2649 if(!(f & a_TMP))
2650 goto jesubcmd;
2651 if(rhv > 63){ /* xxx 63? */
2652 if(!(f & a_SATURATED))
2653 goto jenum_overflow;
2654 rhv = 63;
2656 if(op == '<')
2657 lhv <<= (ui8_t)rhv;
2658 else if(f & a_UNSIGNED)
2659 lhv = (ui64_t)lhv >> (ui8_t)rhv;
2660 else
2661 lhv >>= (ui8_t)rhv;
2662 break;
2665 break;
2666 default:
2667 goto jesubcmd;
2669 }else if(cp[2] == '\0' && cp[1] == '@'){
2670 f |= a_SATURATED;
2671 op = cp[0];
2672 goto jnumop;
2673 }else if(cp[0] == '<'){
2674 if(*++cp != '<')
2675 goto jesubcmd;
2676 if(*++cp == '@'){
2677 f |= a_SATURATED;
2678 ++cp;
2680 if(*cp != '\0')
2681 goto jesubcmd;
2682 f |= a_TMP;
2683 op = '<';
2684 goto jnumop;
2685 }else if(cp[0] == '>'){
2686 if(*++cp != '>')
2687 goto jesubcmd;
2688 if(*++cp == '>'){
2689 f |= a_UNSIGNED;
2690 ++cp;
2692 if(*cp == '@'){
2693 f |= a_SATURATED;
2694 ++cp;
2696 if(*cp != '\0')
2697 goto jesubcmd;
2698 f |= a_TMP;
2699 op = '>';
2700 goto jnumop;
2701 }else if(is_asccaseprefix(cp, "length")){
2702 f |= a_ISNUM | a_ISDECIMAL;
2703 if(argv[1] == NULL || argv[2] != NULL)
2704 goto jesynopsis;
2706 i = strlen(*++argv);
2707 if(UICMP(64, i, >, SI64_MAX))
2708 goto jestr_overflow;
2709 lhv = (si64_t)i;
2710 }else if(is_asccaseprefix(cp, "hash")){
2711 f |= a_ISNUM | a_ISDECIMAL;
2712 if(argv[1] == NULL || argv[2] != NULL)
2713 goto jesynopsis;
2715 i = torek_hash(*++argv);
2716 lhv = (si64_t)i;
2717 }else if(is_asccaseprefix(cp, "file-expand")){
2718 if(argv[1] == NULL || argv[2] != NULL)
2719 goto jesynopsis;
2721 if((varres = fexpand(argv[1], FEXP_NVAR | FEXP_NOPROTO)) == NULL)
2722 goto jsofterr;
2723 }else if(is_asccaseprefix(cp, "random")){
2724 if(argv[1] == NULL || argv[2] != NULL)
2725 goto jesynopsis;
2727 if((n_idec_si64_cp(&lhv, argv[1], 0, NULL
2728 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2729 ) != n_IDEC_STATE_CONSUMED || lhv < 0 || lhv > PATH_MAX)
2730 goto jenum_range;
2731 if(lhv == 0)
2732 lhv = NAME_MAX;
2733 varres = getrandstring((size_t)lhv);
2734 }else if(is_asccaseprefix(cp, "find")){
2735 f |= a_ISNUM | a_ISDECIMAL;
2736 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2737 goto jesynopsis;
2739 if((cp = strstr(argv[1], argv[2])) == NULL)
2740 goto jsofterr;
2741 i = PTR2SIZE(cp - argv[1]);
2742 if(UICMP(64, i, >, SI64_MAX))
2743 goto jestr_overflow;
2744 lhv = (si64_t)i;
2745 }else if(is_asccaseprefix(cp, "ifind")){
2746 f |= a_ISNUM | a_ISDECIMAL;
2747 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2748 goto jesynopsis;
2750 if((cp = asccasestr(argv[1], argv[2])) == NULL)
2751 goto jsofterr;
2752 i = PTR2SIZE(cp - argv[1]);
2753 if(UICMP(64, i, >, SI64_MAX))
2754 goto jestr_overflow;
2755 lhv = (si64_t)i;
2756 }else if(is_asccaseprefix(cp, "substring")){
2757 if(argv[1] == NULL || argv[2] == NULL)
2758 goto jesynopsis;
2759 if(argv[3] != NULL && argv[4] != NULL)
2760 goto jesynopsis;
2762 i = strlen(varres = *++argv);
2764 if(*(cp = *++argv) == '\0')
2765 lhv = 0;
2766 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
2767 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2768 ) != n_IDEC_STATE_CONSUMED)
2769 goto jenum_range;
2770 if(UICMP(64, i, >=, lhv)){
2771 i -= lhv;
2772 varres += lhv;
2773 }else{
2774 if(n_poption & n_PO_D_V)
2775 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
2776 n_shexp_quote_cp(argv[-1], FAL0));
2777 f |= a_SOFTERR;
2780 if(argv[1] != NULL){
2781 if(*(cp = *++argv) == '\0')
2782 lhv = 0;
2783 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
2784 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2785 ) != n_IDEC_STATE_CONSUMED)
2786 goto jenum_range;
2787 if(UICMP(64, i, >=, lhv)){
2788 if(UICMP(64, i, !=, lhv))
2789 varres = savestrbuf(varres, (size_t)lhv);
2790 }else{
2791 if(n_poption & n_PO_D_V)
2792 n_err(_("`vexpr': substring: length argument too large: %s\n"),
2793 n_shexp_quote_cp(argv[-2], FAL0));
2794 f |= a_SOFTERR;
2797 #ifdef HAVE_REGEX
2798 }else if(is_asccaseprefix(cp, "regex")) Jregex:{
2799 regmatch_t rema[1 + n_VEXPR_REGEX_MAX];
2800 regex_t re;
2801 int reflrv;
2803 f |= a_ISNUM | a_ISDECIMAL;
2804 if(argv[1] == NULL || argv[2] == NULL ||
2805 (argv[3] != NULL && argv[4] != NULL))
2806 goto jesynopsis;
2808 reflrv = REG_EXTENDED;
2809 if(f & a_ICASE)
2810 reflrv |= REG_ICASE;
2811 if((reflrv = regcomp(&re, argv[2], reflrv))){
2812 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
2813 n_shexp_quote_cp(argv[2], FAL0), n_regex_err_to_str(&re, reflrv));
2814 assert(f & a_ERR);
2815 goto jerr;
2817 reflrv = regexec(&re, argv[1], n_NELEM(rema), rema, 0);
2818 regfree(&re);
2819 if(reflrv == REG_NOMATCH)
2820 goto jsofterr;
2822 /* Search only? Else replace, which is a bit */
2823 if(argv[3] == NULL){
2824 if(UICMP(64, rema[0].rm_so, >, SI64_MAX))
2825 goto jestr_overflow;
2826 lhv = (si64_t)rema[0].rm_so;
2827 }else{
2828 /* We need to setup some kind of pseudo macro environment for this */
2829 struct a_amv_lostack los;
2830 struct a_amv_mac_call_args amca;
2831 char const **reargv;
2833 memset(&amca, 0, sizeof amca);
2834 amca.amca_name = savestrbuf(&argv[1][rema[0].rm_so],
2835 rema[0].rm_eo - rema[0].rm_so);
2836 amca.amca_amp = a_AMV_MACKY_MACK;
2837 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
2839 amca.amca_argc = (ui32_t)i - 1;
2840 amca.amca_argv =
2841 reargv = salloc(sizeof(char*) * i);
2842 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
2843 *reargv++ = savestrbuf(&argv[1][rema[i].rm_so],
2844 rema[i].rm_eo - rema[i].rm_so);
2845 *reargv = NULL;
2847 hold_all_sigs(); /* TODO DISLIKE! */
2848 los.as_global_saved = a_amv_lopts;
2849 los.as_amcap = &amca;
2850 los.as_up = los.as_global_saved;
2851 los.as_lopts = NULL;
2852 los.as_unroll = FAL0;
2853 a_amv_lopts = &los;
2855 /* C99 */{
2856 struct str templ;
2857 struct n_string s_b;
2858 enum n_shexp_state shs;
2860 templ.s = n_UNCONST(argv[3]);
2861 templ.l = UIZ_MAX;
2862 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
2863 n_SHEXP_PARSE_IGNORE_EMPTY | n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
2864 n_SHEXP_PARSE_QUOTE_AUTO_DSQ),
2865 n_string_creat_auto(&s_b), &templ, NULL);
2866 if((shs & (n_SHEXP_STATE_ERR_MASK | n_SHEXP_STATE_STOP)
2867 ) == n_SHEXP_STATE_STOP){
2868 varres = n_string_cp(&s_b);
2869 n_string_drop_ownership(&s_b);
2870 }else
2871 varres = NULL;
2874 a_amv_lopts = los.as_global_saved;
2875 rele_all_sigs(); /* TODO DISLIKE! */
2877 if(varres == NULL)
2878 goto jsofterr;
2879 f &= ~(a_ISNUM | a_ISDECIMAL);
2881 }else if(is_asccaseprefix(argv[0], "iregex")){
2882 f |= a_ICASE;
2883 goto Jregex;
2884 #endif /* HAVE_REGEX */
2885 }else
2886 goto jesubcmd;
2888 f &= ~a_ERR;
2890 /* Generate the variable value content for numerics.
2891 * Anticipate in our handling below! (Don't do needless work) */
2892 jleave:
2893 if((f & a_ISNUM) && ((f & a_ISDECIMAL) || varname != NULL)){
2894 snprintf(varbuf, sizeof varbuf, "%" PRId64 , lhv);
2895 varres = varbuf;
2898 if(varname == NULL){
2899 /* If there was no error and we are printing a numeric result, print some
2900 * more bases for the fun of it */
2901 if((f & (a_ERR | a_ISNUM | a_ISDECIMAL)) == a_ISNUM){
2902 size_t j;
2904 for(j = 1, i = 0; i < 64; ++i){
2905 varbuf[63 + 64 / 8 -j - i] = (lhv & ((ui64_t)1 << i)) ? '1' : '0';
2906 if((i & 7) == 7 && i != 63){
2907 ++j;
2908 varbuf[63 + 64 / 8 -j - i] = ' ';
2911 varbuf[64 + 64 / 8 -1] = '\0';
2912 fprintf(n_stdout, "%s\n0%" PRIo64 " | 0x%" PRIX64 " | %" PRId64 "\n",
2913 varbuf, lhv, lhv, lhv);
2914 }else
2915 fprintf(n_stdout, "%s\n", varres);
2916 }else if(!n_var_vset(varname, (uintptr_t)varres))
2917 f |= a_ERR | a_SOFTERR;
2918 else if(!(f & a_SOFTERR))
2919 n_pstate_var__em = n_0;
2920 NYD_LEAVE;
2921 return (f & a_ERR) ? 1 : 0;
2923 jsofterr:
2924 f &= ~a_ERR;
2925 jerr:
2926 f &= ~(a_ISNUM | a_ISDECIMAL);
2927 f |= a_SOFTERR | a_ISNUM;
2928 lhv = -1;
2929 goto jleave;
2930 jesubcmd:
2931 assert(f & a_ERR);
2932 n_err(_("`vexpr': invalid subcommand: %s\n"),
2933 n_shexp_quote_cp(*argv, FAL0));
2934 goto jerr;
2935 jesynopsis:
2936 assert(f & a_ERR);
2937 n_err(_("Synopsis: vexpr: <target-variable> <operator> <:argument:>\n"));
2938 goto jerr;
2939 jenum_range:
2940 assert(f & a_ERR);
2941 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
2942 n_shexp_quote_cp(*argv, FAL0));
2943 goto jsofterr;
2944 jenum_overflow:
2945 assert(f & a_ERR);
2946 n_err(_("`vexpr': expression overflows datatype: %" PRId64 " %c %" PRId64
2947 "\n"), lhv, op, rhv);
2948 goto jsofterr;
2949 jestr_overflow:
2950 assert(f & a_ERR);
2951 n_err(_("`vexpr': string length or offset overflows datatype\n"));
2952 goto jsofterr;
2955 /* s-it-mode */