Drop global "image", _outof() -> a_sendout_file_a_pipe()..
[s-mailx.git] / accmacvar.c
blobaed0079dee9375e162e2954c6bd3122734dee3ac
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 readonly: %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 size_t i;
1457 NYD2_ENTER;
1459 msgp = n_string_trunc(msgp, 0);
1460 i = 0;
1462 a_amv_var_revlookup(&avc, name);
1463 if(!a_amv_var_lookup(&avc, FAL0)){
1464 struct str s;
1466 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1467 s.s = n_UNCONST(name);
1468 s.l = UIZ_MAX;
1469 msgp = n_shexp_quote(msgp, &s, FAL0);
1470 goto jleave;
1473 if(n_poption & n_PO_D_V){
1474 if(avc.avc_map == NULL){
1475 msgp = n_string_push_c(msgp, '#');
1476 msgp = n_string_push_cp(msgp, "assembled");
1477 i = 1;
1479 /* C99 */{
1480 struct{
1481 ui16_t flags;
1482 char msg[22];
1483 } const tbase[] = {
1484 {a_AMV_VF_VIRT, "virtual"},
1485 {a_AMV_VF_RDONLY, "readonly"},
1486 {a_AMV_VF_NODEL, "nodelete"},
1487 {a_AMV_VF_NOTEMPTY, "notempty"},
1488 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1489 {a_AMV_VF_NUM, "number"},
1490 {a_AMV_VF_POSNUM, "positive-number"},
1491 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1492 {a_AMV_VF_ENV, "sync-environ"},
1493 {a_AMV_VF_I3VAL, "initial-value"},
1494 {a_AMV_VF_DEFVAL, "default-value"},
1495 {a_AMV_VF_LINKED, "`environ' link"}
1496 }, *tp;
1498 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1499 if(avc.avc_var->av_flags & tp->flags){
1500 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1501 msgp = n_string_push_cp(msgp, tp->msg);
1505 if(i > 0)
1506 msgp = n_string_push_cp(msgp, "\n ");
1509 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1510 msgp = n_string_push_cp(msgp, "# ");
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 ");
1517 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1518 msgp = n_string_push_cp(msgp, "environ ");
1519 msgp = n_string_push_cp(msgp, "set ");
1520 msgp = n_string_push_cp(msgp, name);
1521 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1522 msgp = n_string_push_c(msgp, '=');
1523 msgp = n_string_push_cp(msgp, quote);
1526 jleave:
1527 msgp = n_string_push_c(msgp, '\n');
1528 fputs(n_string_cp(msgp), fp);
1529 NYD2_ENTER;
1530 return (i > 0 ? 2 : 1);
1533 static bool_t
1534 a_amv_var_c_set(char **ap, bool_t issetenv){
1535 char *cp, *cp2, *varbuf, c;
1536 size_t errs;
1537 NYD2_ENTER;
1539 errs = 0;
1540 jouter:
1541 while((cp = *ap++) != NULL){
1542 /* Isolate key */
1543 cp2 = varbuf = salloc(strlen(cp) +1);
1545 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1546 if(cntrlchar(c) || spacechar(c)){
1547 n_err(_("Variable name with control character ignored: %s\n"),
1548 ap[-1]);
1549 ++errs;
1550 goto jouter;
1552 *cp2++ = c;
1554 *cp2 = '\0';
1555 if(c == '\0')
1556 cp = n_UNCONST(n_empty);
1557 else
1558 ++cp;
1560 if(varbuf == cp2){
1561 n_err(_("Empty variable name ignored\n"));
1562 ++errs;
1563 }else{
1564 struct a_amv_var_carrier avc;
1565 bool_t isunset;
1567 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1568 varbuf = &varbuf[2];
1570 a_amv_var_revlookup(&avc, varbuf);
1572 if(isunset)
1573 errs += !a_amv_var_clear(&avc, issetenv);
1574 else
1575 errs += !a_amv_var_set(&avc, cp, issetenv);
1578 NYD2_LEAVE;
1579 return (errs == 0);
1582 FL int
1583 c_define(void *v){
1584 int rv;
1585 char **args;
1586 NYD_ENTER;
1588 rv = 1;
1590 if((args = v)[0] == NULL){
1591 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1592 goto jleave;
1595 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1596 args[2] != NULL){
1597 n_err(_("Synopsis: define: <name> {\n"));
1598 goto jleave;
1601 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1602 jleave:
1603 NYD_LEAVE;
1604 return rv;
1607 FL int
1608 c_undefine(void *v){
1609 int rv;
1610 char **args;
1611 NYD_ENTER;
1613 rv = 0;
1614 args = v;
1616 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1617 while(*++args != NULL);
1618 NYD_LEAVE;
1619 return rv;
1622 FL int
1623 c_call(void *v){
1624 int rv;
1625 NYD_ENTER;
1627 rv = a_amv_mac_call(v, FAL0);
1628 NYD_LEAVE;
1629 return rv;
1632 FL int
1633 c_call_if(void *v){
1634 int rv;
1635 NYD_ENTER;
1637 rv = a_amv_mac_call(v, TRU1);
1638 NYD_LEAVE;
1639 return rv;
1642 FL int
1643 c_account(void *v){
1644 struct a_amv_mac_call_args *amcap;
1645 struct a_amv_mac *amp;
1646 char **args;
1647 int rv, i, oqf, nqf;
1648 NYD_ENTER;
1650 rv = 1;
1652 if((args = v)[0] == NULL){
1653 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1654 goto jleave;
1657 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1658 if(args[2] != NULL){
1659 n_err(_("Synopsis: account: <name> {\n"));
1660 goto jleave;
1662 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1663 n_err(_("`account': cannot use reserved name: %s\n"),
1664 ACCOUNT_NULL);
1665 goto jleave;
1667 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1668 goto jleave;
1671 if(n_pstate & n_PS_HOOK_MASK){
1672 n_err(_("`account': can't change account from within a hook\n"));
1673 goto jleave;
1676 save_mbox_for_possible_quitstuff();
1678 amp = NULL;
1679 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1680 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1681 n_err(_("`account': account does not exist: %s\n"), args[0]);
1682 goto jleave;
1685 oqf = savequitflags();
1687 if(a_amv_acc_curr != NULL){
1688 if(a_amv_acc_curr->am_lopts != NULL)
1689 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1690 /* For accounts this lingers */
1691 --a_amv_acc_curr->am_refcnt;
1692 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1693 a_amv_mac_free(a_amv_acc_curr);
1696 a_amv_acc_curr = amp;
1698 if(amp != NULL){
1699 bool_t ok;
1700 assert(amp->am_lopts == NULL);
1701 amcap = n_lofi_alloc(sizeof *amcap);
1702 memset(amcap, 0, sizeof *amcap);
1703 amcap->amca_name = amp->am_name;
1704 amcap->amca_amp = amp;
1705 amcap->amca_unroller = &amp->am_lopts;
1706 amcap->amca_lopts_on = TRU1;
1707 ++amp->am_refcnt; /* We may not run 0 to avoid being deleted! */
1708 ok = a_amv_mac_exec(amcap);
1709 if(!ok){
1710 /* XXX account switch incomplete, unroll? */
1711 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1712 goto jleave;
1716 n_PS_ROOT_BLOCK((amp != NULL ? ok_vset(_account, amp->am_name)
1717 : ok_vclear(_account)));
1719 if((n_psonce & n_PSO_STARTED) && !(n_pstate & n_PS_HOOK_MASK)){
1720 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1721 restorequitflags(oqf);
1722 if((i = setfile("%", 0)) < 0)
1723 goto jleave;
1724 temporary_folder_hook_check(FAL0);
1725 if(i > 0 && !ok_blook(emptystart))
1726 goto jleave;
1727 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1728 restorequitflags(nqf);
1730 rv = 0;
1731 jleave:
1732 NYD_LEAVE;
1733 return rv;
1736 FL int
1737 c_unaccount(void *v){
1738 int rv;
1739 char **args;
1740 NYD_ENTER;
1742 rv = 0;
1743 args = v;
1745 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1746 while(*++args != NULL);
1747 NYD_LEAVE;
1748 return rv;
1751 FL int
1752 c_localopts(void *v){
1753 char **argv;
1754 int rv;
1755 NYD_ENTER;
1757 rv = 1;
1759 if(a_amv_lopts == NULL){
1760 n_err(_("Cannot use `localopts' in this context "
1761 "(not in `define' or `account', nor special hook)\n"));
1762 goto jleave;
1765 rv = 0;
1767 if(n_pstate & (n_PS_HOOK | n_PS_COMPOSE_MODE)){
1768 if(n_poption & n_PO_D_V)
1769 n_err(_("Cannot turn off `localopts' for compose-mode hooks\n"));
1770 goto jleave;
1773 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1774 jleave:
1775 NYD_LEAVE;
1776 return rv;
1779 FL int
1780 c_shift(void *v){
1781 int rv;
1782 NYD_ENTER;
1784 rv = 1;
1786 if(a_amv_lopts != NULL){
1787 ui16_t i, j;
1788 struct a_amv_mac const *amp;
1789 struct a_amv_mac_call_args *amcap;
1791 amp = (amcap = a_amv_lopts->as_amcap)->amca_amp;
1792 if(amp == NULL || amcap->amca_ps_hook_mask ||
1793 (amp->am_flags & a_AMV_MF_TYPE_MASK) == a_AMV_MF_ACC)
1794 goto jerr;
1796 v = *(char**)v;
1797 if(v == NULL)
1798 i = 1;
1799 else{
1800 si16_t sib;
1802 if((n_idec_si16_cp(&sib, v, 10, NULL
1803 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1804 ) != n_IDEC_STATE_CONSUMED || sib < 0){
1805 n_err(_("`shift': invalid argument: %s\n"), v);
1806 goto jleave;
1808 i = (ui16_t)sib;
1811 if(i > (j = amcap->amca_argc)){
1812 n_err(_("`shift': cannot shift %hu of %hu parameters\n"), i, j);
1813 goto jleave;
1814 }else{
1815 rv = 0;
1816 if(j > 0){
1817 j -= i;
1818 amcap->amca_argc = j;
1819 amcap->amca_argv += i;
1822 }else
1823 jerr:
1824 n_err(_("Can only use `shift' in a `call'ed macro\n"));
1825 jleave:
1826 NYD_LEAVE;
1827 return rv;
1830 FL int
1831 c_return(void *v){
1832 int rv;
1833 NYD_ENTER;
1835 rv = 1;
1837 if(a_amv_lopts != NULL){
1838 char const **argv;
1840 n_source_force_eof();
1842 if((argv = v)[0] != NULL){
1843 si32_t i;
1845 if((n_idec_si32_cp(&i, argv[0], 10, NULL
1846 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1847 ) == n_IDEC_STATE_CONSUMED && i >= 0)
1848 n_pstate_var__em = argv[0];
1849 else
1850 n_err(_("`return': argument one is invalid: %s\n"), argv[0]);
1852 if(argv[1] != NULL){
1853 if((n_idec_si32_cp(&i, argv[1], 10, NULL
1854 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1855 ) == n_IDEC_STATE_CONSUMED && i >= 0)
1856 rv = (int)i;
1857 else{
1858 n_err(_("`return': argument two is invalid: %s\n"), argv[1]);
1859 n_pstate_var__em = n_1;
1861 }else
1862 rv = 0;
1863 }else{
1864 rv = 0;
1865 n_pstate_var__em = n_0;
1867 }else
1868 n_err(_("Can only use `return' in a macro\n"));
1869 NYD_LEAVE;
1870 return rv;
1873 FL bool_t
1874 temporary_folder_hook_check(bool_t nmail){ /* TODO temporary, v15: drop */
1875 struct a_amv_mac_call_args *amcap;
1876 struct a_amv_mac *amp;
1877 size_t len;
1878 char const *cp;
1879 char *var;
1880 bool_t rv;
1881 NYD_ENTER;
1883 rv = TRU1;
1884 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1886 /* First try the fully resolved path */
1887 snprintf(var, len, "folder-hook-%s", mailname);
1888 if((cp = n_var_vlook(var, FAL0)) != NULL)
1889 goto jmac;
1891 /* If we are under *folder*, try the usual +NAME syntax, too */
1892 if(displayname[0] == '+'){
1893 char *x;
1895 for(x = &mailname[len]; x != mailname; --x)
1896 if(x[-1] == '/'){
1897 snprintf(var, len, "folder-hook-+%s", x);
1898 if((cp = n_var_vlook(var, FAL0)) != NULL)
1899 goto jmac;
1900 break;
1904 /* Plain *folder-hook* is our last try */
1905 if((cp = ok_vlook(folder_hook)) == NULL)
1906 goto jleave;
1908 jmac:
1909 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1910 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1911 n_shexp_quote_cp(displayname, FAL0), cp);
1912 rv = FAL0;
1913 goto jleave;
1916 amcap = n_lofi_alloc(sizeof *amcap);
1917 memset(amcap, 0, sizeof *amcap);
1918 amcap->amca_name = cp;
1919 amcap->amca_amp = amp;
1920 n_pstate &= ~n_PS_HOOK_MASK;
1921 if(nmail){
1922 amcap->amca_unroller = NULL;
1923 n_pstate |= n_PS_HOOK_NEWMAIL;
1924 }else{
1925 amcap->amca_unroller = &a_amv_folder_hook_lopts;
1926 n_pstate |= n_PS_HOOK;
1928 amcap->amca_lopts_on = TRU1;
1929 amcap->amca_ps_hook_mask = TRU1;
1930 rv = a_amv_mac_exec(amcap);
1931 n_pstate &= ~n_PS_HOOK_MASK;
1933 jleave:
1934 NYD_LEAVE;
1935 return rv;
1938 FL void
1939 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
1940 NYD_ENTER;
1941 if(a_amv_folder_hook_lopts != NULL){
1942 void *save = a_amv_lopts;
1944 a_amv_lopts = NULL;
1945 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1946 a_amv_folder_hook_lopts = NULL;
1947 a_amv_lopts = save;
1949 NYD_LEAVE;
1952 FL void
1953 temporary_compose_mode_hook_call(char const *macname,
1954 void (*hook_pre)(void *), void *hook_arg){
1955 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_LEX_SLICE
1956 * TODO comment in lex_input.c for the right way of doing things! */
1957 static struct a_amv_lostack *cmh_losp;
1958 struct a_amv_mac_call_args *amcap;
1959 struct a_amv_mac *amp;
1960 NYD_ENTER;
1962 amp = NULL;
1964 if(macname == (char*)-1){
1965 a_amv_mac__finalize(cmh_losp);
1966 cmh_losp = NULL;
1967 }else if(macname != NULL &&
1968 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1969 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1970 macname);
1971 else{
1972 amcap = n_lofi_alloc(sizeof *amcap);
1973 memset(amcap, 0, sizeof *amcap);
1974 amcap->amca_name = (macname != NULL) ? macname : "on-compose-done-shell";
1975 amcap->amca_amp = amp;
1976 amcap->amca_unroller = &a_amv_compose_lopts;
1977 amcap->amca_hook_pre = hook_pre;
1978 amcap->amca_hook_arg = hook_arg;
1979 amcap->amca_lopts_on = TRU1;
1980 amcap->amca_ps_hook_mask = TRU1;
1981 n_pstate &= ~n_PS_HOOK_MASK;
1982 n_pstate |= n_PS_HOOK;
1983 if(macname != NULL)
1984 a_amv_mac_exec(amcap);
1985 else{
1986 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
1987 cmh_losp->as_global_saved = a_amv_lopts;
1988 cmh_losp->as_up = NULL;
1989 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
1990 cmh_losp->as_unroll = TRU1;
1991 a_amv_lopts = cmh_losp;
1994 NYD_LEAVE;
1997 FL void
1998 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
1999 NYD_ENTER;
2000 if(a_amv_compose_lopts != NULL){
2001 void *save = a_amv_lopts;
2003 a_amv_lopts = NULL;
2004 a_amv_lopts_unroll(&a_amv_compose_lopts);
2005 a_amv_compose_lopts = NULL;
2006 a_amv_lopts = save;
2008 NYD_LEAVE;
2011 FL bool_t
2012 n_var_is_user_writable(char const *name){
2013 struct a_amv_var_carrier avc;
2014 struct a_amv_var_map const *avmp;
2015 bool_t rv;
2016 NYD_ENTER;
2018 a_amv_var_revlookup(&avc, name);
2019 if((avmp = avc.avc_map) == NULL)
2020 rv = TRU1;
2021 else
2022 rv = ((avmp->avm_flags & (a_AMV_VF_BOOL | a_AMV_VF_RDONLY)) == 0);
2023 NYD_LEAVE;
2024 return rv;
2027 FL char *
2028 n_var_oklook(enum okeys okey){
2029 struct a_amv_var_carrier avc;
2030 char *rv;
2031 struct a_amv_var_map const *avmp;
2032 NYD_ENTER;
2034 avc.avc_map = avmp = &a_amv_var_map[okey];
2035 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2036 avc.avc_hash = avmp->avm_hash;
2037 avc.avc_okey = okey;
2039 if(a_amv_var_lookup(&avc, FAL0))
2040 rv = avc.avc_var->av_value;
2041 else
2042 rv = NULL;
2043 NYD_LEAVE;
2044 return rv;
2047 FL bool_t
2048 n_var_okset(enum okeys okey, uintptr_t val){
2049 struct a_amv_var_carrier avc;
2050 bool_t ok;
2051 struct a_amv_var_map const *avmp;
2052 NYD_ENTER;
2054 avc.avc_map = avmp = &a_amv_var_map[okey];
2055 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2056 avc.avc_hash = avmp->avm_hash;
2057 avc.avc_okey = okey;
2059 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2060 NYD_LEAVE;
2061 return ok;
2064 FL bool_t
2065 n_var_okclear(enum okeys okey){
2066 struct a_amv_var_carrier avc;
2067 bool_t rv;
2068 struct a_amv_var_map const *avmp;
2069 NYD_ENTER;
2071 avc.avc_map = avmp = &a_amv_var_map[okey];
2072 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2073 avc.avc_hash = avmp->avm_hash;
2074 avc.avc_okey = okey;
2076 rv = a_amv_var_clear(&avc, FAL0);
2077 NYD_LEAVE;
2078 return rv;
2081 FL char const *
2082 n_var_vlook(char const *vokey, bool_t try_getenv){
2083 struct a_amv_var_carrier avc;
2084 char const *rv;
2085 NYD_ENTER;
2087 a_amv_var_revlookup(&avc, vokey);
2088 rv = NULL;
2090 /* Here, and only here we need to take care for the special macro-local
2091 * parameters... (except for n_var_vexplode()) */
2092 if(n_LIKELY(!avc.avc_is_special)){
2093 if(a_amv_var_lookup(&avc, FAL0))
2094 rv = avc.avc_var->av_value;
2095 /* Only check the environment for something that is otherwise unknown */
2096 else if(try_getenv && avc.avc_map == NULL)
2097 rv = getenv(vokey);
2098 }else{
2099 bool_t ismacky;
2100 struct a_amv_mac_call_args *amcap;
2102 /* These may occur only in a macro.. */
2103 if(n_UNLIKELY(a_amv_lopts == NULL))
2104 goto jmaylog;
2106 amcap = a_amv_lopts->as_amcap;
2108 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
2109 if((ismacky = (amcap->amca_amp == a_AMV_MACKY_MACK)) ||
2110 (!amcap->amca_ps_hook_mask &&
2111 (assert(amcap->amca_amp != NULL), TRU1) &&
2112 (amcap->amca_amp->am_flags & a_AMV_MF_TYPE_MASK) != a_AMV_MF_ACC)){
2113 if(avc.avc_is_special == TRUM1){
2114 if(avc.avc_special_prop > 0){
2115 if(amcap->amca_argc >= avc.avc_special_prop)
2116 rv = amcap->amca_argv[avc.avc_special_prop - 1];
2117 }else if(ismacky)
2118 rv = amcap->amca_name;
2119 else
2120 rv = (a_amv_lopts->as_up != NULL
2121 ? a_amv_lopts->as_up->as_amcap->amca_name : n_empty);
2123 /* MACKY_MACK doesn't know about [*@#] */
2124 else if(ismacky)
2125 goto jmaylog;
2126 else switch(avc.avc_special_prop){
2127 case a_AMV_VST_STAR:
2128 case a_AMV_VST_AT:{
2129 ui32_t i, l;
2131 for(i = l = 0; i < amcap->amca_argc; ++i)
2132 l += strlen(amcap->amca_argv[i]) + 1;
2133 if(l == 0)
2134 rv = n_empty;
2135 else{
2136 char *cp;
2138 rv = cp = salloc(l);
2139 for(i = l = 0; i < amcap->amca_argc; ++i){
2140 l = strlen(amcap->amca_argv[i]);
2141 memcpy(cp, amcap->amca_argv[i], l);
2142 cp += l;
2143 *cp++ = ' ';
2145 *--cp = '\0';
2147 } break;
2148 case a_AMV_VST_NOSIGN:{
2149 char *cp;
2151 rv = cp = salloc(sizeof("65535"));
2152 snprintf(cp, sizeof("65535"), "%hu", amcap->amca_argc);
2153 } break;
2154 default:
2155 rv = n_empty;
2156 break;
2158 }else{
2159 jmaylog:
2160 if(n_poption & n_PO_D_V)
2161 n_err(_("Cannot use macro local variable in this context: %s\n"),
2162 n_shexp_quote_cp(vokey, FAL0));
2165 NYD_LEAVE;
2166 return rv;
2169 FL bool_t
2170 n_var_vexplode(void const **cookie){
2171 NYD_ENTER;
2172 /* These may occur only in a macro.. */
2173 *cookie = (a_amv_lopts != NULL) ? a_amv_lopts->as_amcap->amca_argv : NULL;
2174 NYD_LEAVE;
2175 return (*cookie != NULL);
2178 FL bool_t
2179 n_var_vset(char const *vokey, uintptr_t val){
2180 struct a_amv_var_carrier avc;
2181 bool_t ok;
2182 NYD_ENTER;
2184 a_amv_var_revlookup(&avc, vokey);
2186 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
2187 NYD_LEAVE;
2188 return ok;
2191 FL bool_t
2192 n_var_vclear(char const *vokey){
2193 struct a_amv_var_carrier avc;
2194 bool_t ok;
2195 NYD_ENTER;
2197 a_amv_var_revlookup(&avc, vokey);
2199 ok = a_amv_var_clear(&avc, FAL0);
2200 NYD_LEAVE;
2201 return ok;
2204 #ifdef HAVE_SOCKETS
2205 FL char *
2206 n_var_xoklook(enum okeys okey, struct url const *urlp,
2207 enum okey_xlook_mode oxm){
2208 struct a_amv_var_carrier avc;
2209 struct str const *us;
2210 size_t nlen;
2211 char *nbuf, *rv;
2212 struct a_amv_var_map const *avmp;
2213 NYD_ENTER;
2215 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
2217 /* For simplicity: allow this case too */
2218 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
2219 nbuf = NULL;
2220 goto jplain;
2223 avc.avc_map = avmp = &a_amv_var_map[okey];
2224 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
2225 avc.avc_okey = okey;
2227 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
2228 nlen = strlen(avc.avc_name);
2229 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
2230 memcpy(nbuf, avc.avc_name, nlen);
2231 nbuf[nlen++] = '-';
2233 /* One of .url_u_h_p and .url_h_p we test in here */
2234 memcpy(nbuf + nlen, us->s, us->l +1);
2235 avc.avc_name = a_amv_var_canonify(nbuf);
2236 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2237 if(a_amv_var_lookup(&avc, FAL0))
2238 goto jvar;
2240 /* The second */
2241 if(oxm & OXM_H_P){
2242 us = &urlp->url_h_p;
2243 memcpy(nbuf + nlen, us->s, us->l +1);
2244 avc.avc_name = a_amv_var_canonify(nbuf);
2245 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2246 if(a_amv_var_lookup(&avc, FAL0)){
2247 jvar:
2248 rv = avc.avc_var->av_value;
2249 goto jleave;
2253 jplain:
2254 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2255 jleave:
2256 if(nbuf != NULL)
2257 n_lofi_free(nbuf);
2258 NYD_LEAVE;
2259 return rv;
2261 #endif /* HAVE_SOCKETS */
2263 FL int
2264 c_set(void *v){
2265 char **ap;
2266 int err;
2267 NYD_ENTER;
2269 if(*(ap = v) == NULL){
2270 a_amv_var_show_all();
2271 err = 0;
2272 }else
2273 err = !a_amv_var_c_set(ap, FAL0);
2274 NYD_LEAVE;
2275 return err;
2278 FL int
2279 c_unset(void *v){
2280 char **ap;
2281 int err;
2282 NYD_ENTER;
2284 for(err = 0, ap = v; *ap != NULL; ++ap)
2285 err |= !n_var_vclear(*ap);
2286 NYD_LEAVE;
2287 return err;
2290 FL int
2291 c_varshow(void *v){
2292 char **ap;
2293 NYD_ENTER;
2295 if(*(ap = v) == NULL)
2296 v = NULL;
2297 else{
2298 struct n_string msg, *msgp = &msg;
2300 msgp = n_string_creat(msgp);
2301 for(; *ap != NULL; ++ap)
2302 a_amv_var_show(*ap, n_stdout, msgp);
2303 n_string_gut(msgp);
2305 NYD_LEAVE;
2306 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2309 FL int
2310 c_varedit(void *v){
2311 struct a_amv_var_carrier avc;
2312 FILE *of, *nf;
2313 char *val, **argv;
2314 int err;
2315 sighandler_type sigint;
2316 NYD_ENTER;
2318 sigint = safe_signal(SIGINT, SIG_IGN);
2320 for(err = 0, argv = v; *argv != NULL; ++argv){
2321 memset(&avc, 0, sizeof avc);
2323 a_amv_var_revlookup(&avc, *argv);
2325 if(avc.avc_map != NULL){
2326 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2327 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2328 avc.avc_name);
2329 continue;
2331 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2332 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2333 avc.avc_name);
2334 continue;
2338 a_amv_var_lookup(&avc, FAL0);
2340 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2341 NULL){
2342 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2343 err = 1;
2344 break;
2345 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2346 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2347 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2348 Fclose(of);
2349 err = 1;
2350 continue;
2353 fflush_rewind(of);
2354 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2355 Fclose(of);
2357 if(nf != NULL){
2358 int c;
2359 char *base;
2360 off_t l;
2362 l = fsize(nf);
2363 assert(l >= 0);
2364 base = salloc((size_t)l +1);
2366 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2367 if(c == '\n' || c == '\r'){
2368 *val = ' ';
2369 ++l;
2370 }else{
2371 *val = (char)(uc_i)c;
2372 l = 0;
2374 val -= l;
2375 *val = '\0';
2377 if(!a_amv_var_set(&avc, base, FAL0))
2378 err = 1;
2380 Fclose(nf);
2381 }else{
2382 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2383 err = 1;
2384 break;
2388 safe_signal(SIGINT, sigint);
2389 NYD_LEAVE;
2390 return err;
2393 FL int
2394 c_environ(void *v){
2395 struct a_amv_var_carrier avc;
2396 int err;
2397 char **ap;
2398 bool_t islnk;
2399 NYD_ENTER;
2401 if((islnk = is_asccaseprefix(*(ap = v), "link")) ||
2402 is_asccaseprefix(*ap, "unlink")){
2403 for(err = 0; *++ap != NULL;){
2404 a_amv_var_revlookup(&avc, *ap);
2406 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2407 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2408 if(!islnk){
2409 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2410 continue;
2411 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2412 if(n_poption & n_PO_D_V)
2413 n_err(_("`environ': link: already established: %s\n"), *ap);
2414 continue;
2416 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2417 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2418 a_amv_var__putenv(&avc, avc.avc_var);
2419 }else if(!islnk){
2420 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2421 err = 1;
2422 }else{
2423 char const *evp = getenv(*ap);
2425 if(evp != NULL)
2426 err |= !a_amv_var_set(&avc, evp, TRU1);
2427 else{
2428 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2429 *ap);
2430 err = 1;
2434 }else if(is_asccaseprefix(*ap, "set"))
2435 err = !a_amv_var_c_set(++ap, TRU1);
2436 else if(is_asccaseprefix(*ap, "unset")){
2437 for(err = 0; *++ap != NULL;){
2438 a_amv_var_revlookup(&avc, *ap);
2440 if(!a_amv_var_clear(&avc, TRU1))
2441 err = 1;
2443 }else{
2444 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2445 err = 1;
2447 NYD_LEAVE;
2448 return err;
2451 FL int
2452 c_vexpr(void *v){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2453 /* This needs to be here because we need to apply MACKY_MACK for
2454 * search+replace regular expression support :( */
2455 size_t i;
2456 enum n_idec_state ids;
2457 si64_t lhv, rhv;
2458 char op, varbuf[64 + 64 / 8 +1];
2459 char const **argv, *varname, *varres, *cp;
2460 enum{
2461 a_ERR = 1<<0,
2462 a_SOFTERR = 1<<1,
2463 a_ISNUM = 1<<2,
2464 a_ISDECIMAL = 1<<3, /* Print only decimal result */
2465 a_SATURATED = 1<<4,
2466 a_ICASE = 1<<5,
2467 a_UNSIGNED = 1<<6, /* Unsigned right shift (share bit ok) */
2468 a_TMP = 1<<30
2469 } f;
2470 NYD_ENTER;
2472 f = a_ERR;
2473 argv = v;
2474 varname = (n_pstate & n_PS_ARGMOD_VPUT) ? *argv++ : NULL;
2475 n_UNINIT(varres, n_empty);
2477 if((cp = argv[0])[0] == '\0')
2478 goto jesubcmd;
2480 if(cp[1] == '\0'){
2481 op = cp[0];
2482 jnumop:
2483 f |= a_ISNUM;
2484 switch(op){
2485 case '=':
2486 case '~':
2487 if(argv[1] == NULL || argv[2] != NULL)
2488 goto jesynopsis;
2490 if(*(cp = *++argv) == '\0')
2491 lhv = 0;
2492 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2493 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2494 ) != n_IDEC_STATE_CONSUMED){
2495 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2496 goto jenum_range;
2497 f |= a_SOFTERR;
2498 break;
2500 if(op == '~')
2501 lhv = ~lhv;
2502 break;
2504 case '+':
2505 case '-':
2506 case '*':
2507 case '/':
2508 case '%':
2509 case '|':
2510 case '&':
2511 case '^':
2512 case '<':
2513 case '>':
2514 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2515 goto jesynopsis;
2516 else{
2517 char xop;
2519 if(*(cp = *++argv) == '\0')
2520 lhv = 0;
2521 else if(((ids = n_idec_si64_cp(&lhv, cp, 0, NULL)
2522 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2523 ) != n_IDEC_STATE_CONSUMED){
2524 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2525 goto jenum_range;
2526 f |= a_SOFTERR;
2527 break;
2530 if(*(cp = *++argv) == '\0')
2531 rhv = 0;
2532 else if(((ids = n_idec_si64_cp(&rhv, cp, 0, NULL)
2533 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2534 ) != n_IDEC_STATE_CONSUMED){
2535 if(!(ids & n_IDEC_STATE_EOVERFLOW) || !(f & a_SATURATED))
2536 goto jenum_range;
2537 f |= a_SOFTERR;
2538 lhv = rhv;
2539 break;
2542 xop = op;
2543 jnumop_again:
2544 switch(xop){
2545 case '+':
2546 if(rhv < 0){
2547 if(rhv != SI64_MIN){
2548 rhv = -rhv;
2549 xop = '-';
2550 goto jnumop_again;
2551 }else if(lhv < 0)
2552 goto jeplusminus;
2553 else if(lhv == 0){
2554 lhv = rhv;
2555 break;
2558 if(SI64_MAX - rhv < lhv)
2559 goto jeplusminus;
2560 lhv += rhv;
2561 break;
2562 case '-':
2563 if(rhv < 0){
2564 if(rhv != SI64_MIN){
2565 rhv = -rhv;
2566 xop = '+';
2567 goto jnumop_again;
2568 }else if(lhv > 0)
2569 goto jeplusminus;
2570 else if(lhv == 0){
2571 lhv = rhv;
2572 break;
2575 if(SI64_MIN + rhv > lhv){
2576 jeplusminus:
2577 if(!(f & a_SATURATED))
2578 goto jenum_overflow;
2579 f |= a_SOFTERR;
2580 lhv = (lhv < 0 || xop == '-') ? SI64_MIN : SI64_MAX;
2581 }else
2582 lhv -= rhv;
2583 break;
2584 case '*':
2585 /* Will the result be positive? */
2586 if((lhv < 0) == (rhv < 0)){
2587 if(lhv > 0){
2588 lhv = -lhv;
2589 rhv = -rhv;
2591 if(rhv != 0 && lhv != 0 && SI64_MAX / rhv > lhv){
2592 if(!(f & a_SATURATED))
2593 goto jenum_overflow;
2594 f |= a_SOFTERR;
2595 lhv = SI64_MAX;
2596 }else
2597 lhv *= rhv;
2598 }else{
2599 if(rhv > 0){
2600 if(lhv != 0 && SI64_MIN / lhv < rhv){
2601 if(!(f & a_SATURATED))
2602 goto jenum_overflow;
2603 f |= a_SOFTERR;
2604 lhv = SI64_MIN;
2605 }else
2606 lhv *= rhv;
2607 }else{
2608 if(rhv != 0 && lhv != 0 && SI64_MIN / rhv < lhv){
2609 if(!(f & a_SATURATED))
2610 goto jenum_overflow;
2611 f |= a_SOFTERR;
2612 lhv = SI64_MIN;
2613 }else
2614 lhv *= rhv;
2617 break;
2618 case '/':
2619 if(rhv == 0){
2620 if(!(f & a_SATURATED))
2621 goto jenum_range;
2622 f |= a_SOFTERR;
2623 lhv = SI64_MAX;
2624 }else
2625 lhv /= rhv;
2626 break;
2627 case '%':
2628 if(rhv == 0){
2629 if(!(f & a_SATURATED))
2630 goto jenum_range;
2631 f |= a_SOFTERR;
2632 lhv = SI64_MAX;
2633 }else
2634 lhv %= rhv;
2635 break;
2636 case '|':
2637 lhv |= rhv;
2638 break;
2639 case '&':
2640 lhv &= rhv;
2641 break;
2642 case '^':
2643 lhv ^= rhv;
2644 break;
2645 case '<':
2646 case '>':
2647 if(!(f & a_TMP))
2648 goto jesubcmd;
2649 if(rhv > 63){ /* xxx 63? */
2650 if(!(f & a_SATURATED))
2651 goto jenum_overflow;
2652 rhv = 63;
2654 if(op == '<')
2655 lhv <<= (ui8_t)rhv;
2656 else if(f & a_UNSIGNED)
2657 lhv = (ui64_t)lhv >> (ui8_t)rhv;
2658 else
2659 lhv >>= (ui8_t)rhv;
2660 break;
2663 break;
2664 default:
2665 goto jesubcmd;
2667 }else if(cp[2] == '\0' && cp[1] == '@'){
2668 f |= a_SATURATED;
2669 op = cp[0];
2670 goto jnumop;
2671 }else if(cp[0] == '<'){
2672 if(*++cp != '<')
2673 goto jesubcmd;
2674 if(*++cp == '@'){
2675 f |= a_SATURATED;
2676 ++cp;
2678 if(*cp != '\0')
2679 goto jesubcmd;
2680 f |= a_TMP;
2681 op = '<';
2682 goto jnumop;
2683 }else if(cp[0] == '>'){
2684 if(*++cp != '>')
2685 goto jesubcmd;
2686 if(*++cp == '>'){
2687 f |= a_UNSIGNED;
2688 ++cp;
2690 if(*cp == '@'){
2691 f |= a_SATURATED;
2692 ++cp;
2694 if(*cp != '\0')
2695 goto jesubcmd;
2696 f |= a_TMP;
2697 op = '>';
2698 goto jnumop;
2699 }else if(is_asccaseprefix(cp, "length")){
2700 f |= a_ISNUM | a_ISDECIMAL;
2701 if(argv[1] == NULL || argv[2] != NULL)
2702 goto jesynopsis;
2704 i = strlen(*++argv);
2705 if(UICMP(64, i, >, SI64_MAX))
2706 goto jestr_overflow;
2707 lhv = (si64_t)i;
2708 }else if(is_asccaseprefix(cp, "file-expand")){
2709 if(argv[1] == NULL || argv[2] != NULL)
2710 goto jesynopsis;
2712 if((varres = fexpand(argv[1], FEXP_NVAR)) == NULL)
2713 goto jsofterr;
2714 }else if(is_asccaseprefix(cp, "find")){
2715 f |= a_ISNUM | a_ISDECIMAL;
2716 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2717 goto jesynopsis;
2719 if((cp = strstr(argv[1], argv[2])) == NULL)
2720 goto jsofterr;
2721 i = PTR2SIZE(cp - argv[1]);
2722 if(UICMP(64, i, >, SI64_MAX))
2723 goto jestr_overflow;
2724 lhv = (si64_t)i;
2725 }else if(is_asccaseprefix(cp, "ifind")){
2726 f |= a_ISNUM | a_ISDECIMAL;
2727 if(argv[1] == NULL || argv[2] == NULL || argv[3] != NULL)
2728 goto jesynopsis;
2730 if((cp = asccasestr(argv[1], argv[2])) == NULL)
2731 goto jsofterr;
2732 i = PTR2SIZE(cp - argv[1]);
2733 if(UICMP(64, i, >, SI64_MAX))
2734 goto jestr_overflow;
2735 lhv = (si64_t)i;
2736 }else if(is_asccaseprefix(cp, "substring")){
2737 if(argv[1] == NULL || argv[2] == NULL)
2738 goto jesynopsis;
2739 if(argv[3] != NULL && argv[4] != NULL)
2740 goto jesynopsis;
2742 i = strlen(varres = *++argv);
2744 if(*(cp = *++argv) == '\0')
2745 lhv = 0;
2746 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
2747 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2748 ) != n_IDEC_STATE_CONSUMED)
2749 goto jenum_range;
2750 if(UICMP(64, i, >=, lhv)){
2751 i -= lhv;
2752 varres += lhv;
2753 }else{
2754 if(n_poption & n_PO_D_V)
2755 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
2756 n_shexp_quote_cp(argv[-1], FAL0));
2757 f |= a_SOFTERR;
2760 if(argv[1] != NULL){
2761 if(*(cp = *++argv) == '\0')
2762 lhv = 0;
2763 else if((n_idec_si64_cp(&lhv, cp, 0, NULL
2764 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
2765 ) != n_IDEC_STATE_CONSUMED)
2766 goto jenum_range;
2767 if(UICMP(64, i, >=, lhv)){
2768 if(UICMP(64, i, !=, lhv))
2769 varres = savestrbuf(varres, (size_t)lhv);
2770 }else{
2771 if(n_poption & n_PO_D_V)
2772 n_err(_("`vexpr': substring: length argument too large: %s\n"),
2773 n_shexp_quote_cp(argv[-2], FAL0));
2774 f |= a_SOFTERR;
2777 #ifdef HAVE_REGEX
2778 }else if(is_asccaseprefix(cp, "regex")) Jregex:{
2779 regmatch_t rema[1 + n_VEXPR_REGEX_MAX];
2780 regex_t re;
2781 int reflrv;
2783 f |= a_ISNUM | a_ISDECIMAL;
2784 if(argv[1] == NULL || argv[2] == NULL ||
2785 (argv[3] != NULL && argv[4] != NULL))
2786 goto jesynopsis;
2788 reflrv = REG_EXTENDED;
2789 if(f & a_ICASE)
2790 reflrv |= REG_ICASE;
2791 if((reflrv = regcomp(&re, argv[2], reflrv))){
2792 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
2793 n_shexp_quote_cp(argv[2], FAL0), n_regex_err_to_str(&re, reflrv));
2794 assert(f & a_ERR);
2795 goto jerr;
2797 reflrv = regexec(&re, argv[1], n_NELEM(rema), rema, 0);
2798 regfree(&re);
2799 if(reflrv == REG_NOMATCH)
2800 goto jsofterr;
2802 /* Search only? Else replace, which is a bit */
2803 if(argv[3] == NULL){
2804 if(UICMP(64, rema[0].rm_so, >, SI64_MAX))
2805 goto jestr_overflow;
2806 lhv = (si64_t)rema[0].rm_so;
2807 }else{
2808 /* We need to setup some kind of pseudo macro environment for this */
2809 struct a_amv_lostack los;
2810 struct a_amv_mac_call_args amca;
2811 char const **reargv;
2813 memset(&amca, 0, sizeof amca);
2814 amca.amca_name = savestrbuf(&argv[1][rema[0].rm_so],
2815 rema[0].rm_eo - rema[0].rm_so);
2816 amca.amca_amp = a_AMV_MACKY_MACK;
2817 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
2819 amca.amca_argc = (ui32_t)i - 1;
2820 amca.amca_argv =
2821 reargv = salloc(sizeof(char*) * i);
2822 for(i = 1; rema[i].rm_so != -1 && i < n_NELEM(rema); ++i)
2823 *reargv++ = savestrbuf(&argv[1][rema[i].rm_so],
2824 rema[i].rm_eo - rema[i].rm_so);
2825 *reargv = NULL;
2827 hold_all_sigs(); /* TODO DISLIKE! */
2828 los.as_global_saved = a_amv_lopts;
2829 los.as_amcap = &amca;
2830 los.as_up = los.as_global_saved;
2831 los.as_lopts = NULL;
2832 los.as_unroll = FAL0;
2833 a_amv_lopts = &los;
2835 /* C99 */{
2836 struct str templ;
2837 struct n_string s_b;
2838 enum n_shexp_state shs;
2840 templ.s = n_UNCONST(argv[3]);
2841 templ.l = UIZ_MAX;
2842 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
2843 n_SHEXP_PARSE_IGNORE_EMPTY | n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
2844 n_SHEXP_PARSE_QUOTE_AUTO_DSQ),
2845 n_string_creat_auto(&s_b), &templ, NULL);
2846 if((shs & (n_SHEXP_STATE_ERR_MASK | n_SHEXP_STATE_STOP)
2847 ) == n_SHEXP_STATE_STOP){
2848 varres = n_string_cp(&s_b);
2849 n_string_drop_ownership(&s_b);
2850 }else
2851 varres = NULL;
2854 a_amv_lopts = los.as_global_saved;
2855 rele_all_sigs(); /* TODO DISLIKE! */
2857 if(varres == NULL)
2858 goto jsofterr;
2859 f &= ~(a_ISNUM | a_ISDECIMAL);
2861 }else if(is_asccaseprefix(argv[0], "iregex")){
2862 f |= a_ICASE;
2863 goto Jregex;
2864 #endif /* HAVE_REGEX */
2865 }else
2866 goto jesubcmd;
2868 f &= ~a_ERR;
2870 /* Generate the variable value content for numerics.
2871 * Anticipate in our handling below! (Don't do needless work) */
2872 jleave:
2873 if((f & a_ISNUM) && ((f & a_ISDECIMAL) || varname != NULL)){
2874 snprintf(varbuf, sizeof varbuf, "%" PRId64 , lhv);
2875 varres = varbuf;
2878 if(varname == NULL){
2879 /* If there was no error and we are printing a numeric result, print some
2880 * more bases for the fun of it */
2881 if((f & (a_ERR | a_ISNUM | a_ISDECIMAL)) == a_ISNUM){
2882 size_t j;
2884 for(j = 1, i = 0; i < 64; ++i){
2885 varbuf[63 + 64 / 8 -j - i] = (lhv & ((ui64_t)1 << i)) ? '1' : '0';
2886 if((i & 7) == 7 && i != 63){
2887 ++j;
2888 varbuf[63 + 64 / 8 -j - i] = ' ';
2891 varbuf[64 + 64 / 8 -1] = '\0';
2892 fprintf(n_stdout, "%s\n0%" PRIo64 " | 0x%" PRIX64 " | %" PRId64 "\n",
2893 varbuf, lhv, lhv, lhv);
2894 }else
2895 fprintf(n_stdout, "%s\n", varres);
2896 }else if(!n_var_vset(varname, (uintptr_t)varres))
2897 f |= a_ERR | a_SOFTERR;
2898 else if(!(f & a_SOFTERR))
2899 n_pstate_var__em = n_0;
2900 NYD_LEAVE;
2901 return (f & a_ERR) ? 1 : 0;
2903 jsofterr:
2904 f &= ~a_ERR;
2905 jerr:
2906 f &= ~(a_ISNUM | a_ISDECIMAL);
2907 f |= a_SOFTERR | a_ISNUM;
2908 lhv = -1;
2909 goto jleave;
2910 jesubcmd:
2911 assert(f & a_ERR);
2912 n_err(_("`vexpr': invalid subcommand: %s\n"),
2913 n_shexp_quote_cp(*argv, FAL0));
2914 goto jerr;
2915 jesynopsis:
2916 assert(f & a_ERR);
2917 n_err(_("Synopsis: vexpr: <target-variable> <operator> <:argument:>\n"));
2918 goto jerr;
2919 jenum_range:
2920 assert(f & a_ERR);
2921 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
2922 n_shexp_quote_cp(*argv, FAL0));
2923 goto jsofterr;
2924 jenum_overflow:
2925 assert(f & a_ERR);
2926 n_err(_("`vexpr': expression overflows datatype: %" PRId64 " %c %" PRId64
2927 "\n"), lhv, op, rhv);
2928 goto jsofterr;
2929 jestr_overflow:
2930 assert(f & a_ERR);
2931 n_err(_("`vexpr': string length or offset overflows datatype\n"));
2932 goto jsofterr;
2935 /* s-it-mode */