(BWDIC!) Rewrite variable handling..
[s-mailx.git] / accmacvar.c
blob197278106ba03473b945ff7bb7c93ca27521dc2a
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 . undefining and overwriting a macro should always be possible:
9 *@ TODO simply place the thing in a delete-later list and replace the
10 *@ TODO accessible entry! (instant delete if on top recursion level.)
11 *@ TODO . Likewise, overwriting an existing should be like delete+create
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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 # define _ACCMACVAR_SOURCE /* For _features[] */
51 # include "nail.h"
52 #endif
54 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
55 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
56 #endif
58 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
59 #define a_AMV_PRIME HSHSIZE
60 #define a_AMV_NAME2HASH(N) torek_hash(N)
61 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
63 enum a_amv_mac_flags{
64 a_AMV_MF_NONE = 0,
65 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
66 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
67 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
68 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
69 a_AMV_MF__MAX = 0xFF
72 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
73 * _IMPORT implies _ENV */
74 enum a_amv_var_flags{
75 a_AMV_VF_NONE = 0,
76 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
77 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
78 a_AMV_VF_RDONLY = 1<<2, /* May not be set by user */
79 a_AMV_VF_NODEL = 1<<3, /* May not be deleted */
80 a_AMV_VF_NOTEMPTY = 1<<4, /* May not be assigned an empty value */
81 a_AMV_VF_VIP = 1<<5, /* Wants _var_check_vips() evaluation */
82 a_AMV_VF_IMPORT = 1<<6, /* Import ONLY from environ (before PS_STARTED) */
83 a_AMV_VF_ENV = 1<<7, /* Update environment on change */
84 a_AMV_VF_I3VAL = 1<<8, /* Has an initial value */
85 a_AMV_VF_DEFVAL = 1<<9, /* Has a default value */
86 a_AMV_VF_LINKED = 1<<10, /* `environ' linked */
87 a_AMV_VF__MASK = (1<<(10+1)) - 1
90 struct a_amv_mac{
91 struct a_amv_mac *am_next;
92 ui32_t am_maxlen; /* of any line in .am_line_dat */
93 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
94 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
95 struct a_amv_var *am_lopts; /* `localopts' unroll list */
96 ui8_t am_flags; /* enum a_amv_mac_flags */
97 char am_name[VFIELD_SIZE(7)]; /* of this macro */
99 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
101 struct a_amv_mac_line{
102 ui32_t aml_len;
103 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
104 char aml_dat[VFIELD_SIZE(0)];
107 struct a_amv_lostack{
108 struct a_amv_lostack *as_up; /* Outer context */
109 struct a_amv_mac *as_mac; /* Context (`account' or `define') */
110 struct a_amv_var *as_lopts;
111 bool_t as_unroll; /* Unrolling enabled? */
112 ui8_t avs__pad[7];
115 struct a_amv_var{
116 struct a_amv_var *av_link;
117 char *av_value;
118 #ifdef HAVE_PUTENV
119 char *av_env; /* Actively managed putenv(3) memory */
120 #endif
121 ui16_t av_flags; /* enum a_amv_var_flags */
122 char av_name[VFIELD_SIZE(6)];
124 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
126 struct a_amv_var_map{
127 ui32_t avm_hash;
128 ui16_t avm_keyoff;
129 ui16_t avm_flags; /* enum a_amv_var_flags */
131 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
133 struct a_amv_var_virt{
134 ui32_t avv_okey;
135 struct a_amv_var const *avv_var;
138 struct a_amv_var_defval{
139 ui32_t avdv_okey;
140 ui8_t avdv__pad[4];
141 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
144 struct a_amv_var_carrier{
145 char const *avc_name;
146 ui32_t avc_hash;
147 ui32_t avc_prime;
148 struct a_amv_var *avc_var;
149 struct a_amv_var_map const *avc_map;
150 enum okeys avc_okey;
153 /* Include the constant mk-okey-map.pl output */
154 #include "version.h"
155 #include "okeys.h"
157 /* The currently active account */
158 static struct a_amv_mac *a_amv_acc_curr;
160 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
162 /* Unroll list of currently running macro stack */
163 static struct a_amv_lostack *a_amv_lopts;
165 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
167 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
168 * TODO today via a static lostack, it should be a field in mailbox, once that
169 * TODO is a real multi-instance object */
170 static struct a_amv_var *a_amv_folder_hook_lopts;
172 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
173 static struct a_amv_var *a_amv_compose_lopts;
175 /* Does cp consist solely of WS and a } */
176 static bool_t a_amv_mac_is_closing_angle(char const *cp);
178 /* Lookup for macros/accounts */
179 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
180 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
182 /* Execute a macro */
183 static bool_t a_amv_mac_exec(struct a_amv_mac const *amp,
184 struct a_amv_var **unroller, bool_t lopts_on);
186 /* User display helpers */
187 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
189 /* _def() returns error for faulty definitions and already existing * names,
190 * _undef() returns error if a named thing doesn't exist */
191 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
192 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
194 /* */
195 static void a_amv_mac_free(struct a_amv_mac *amp);
197 /* Update replay-log */
198 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
199 struct a_amv_var *oavp);
200 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
202 /* Special cased value string allocation */
203 static char *a_amv_var_copy(char const *str);
204 static void a_amv_var_free(char *cp);
206 /* Check for special housekeeping */
207 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
209 /* If a variable name begins with a lowercase-character and contains at
210 * least one '@', it is converted to all-lowercase. This is necessary
211 * for lookups of names based on email addresses.
212 * Following the standard, only the part following the last '@' should
213 * be lower-cased, but practice has established otherwise here */
214 static char const *a_amv_var_canonify(char const *vn);
216 /* Try to reverse lookup an option name to an enum okeys mapping.
217 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
218 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
219 char const *name);
221 /* Lookup a variable from .avc_(map|name|hash), return wether it was found.
222 * Sets .avc_prime; .avc_var is NULL if not found.
223 * Here it is where we care for _I3VAL and _DEFVAL, too */
224 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp);
226 /* Set var from .avc_(map|name|hash), return success */
227 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
228 bool_t force_env);
230 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
231 struct a_amv_var *avp);
233 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
234 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
236 static bool_t a_amv_var__clearenv(char const *name, char *value);
238 /* List all variables */
239 static void a_amv_var_show_all(void);
241 static int a_amv_var__show_cmp(void const *s1, void const *s2);
243 /* Actually do print one, return number of lines written */
244 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
246 static char *a_amv_var__simple_quote(char const *cp);
248 /* Shared c_set() and c_environ():set impl, return success */
249 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
251 static bool_t
252 a_amv_mac_is_closing_angle(char const *cp){
253 bool_t rv;
254 NYD2_ENTER;
256 while(spacechar(*cp))
257 ++cp;
259 if((rv = (*cp++ == '}'))){
260 while(spacechar(*cp))
261 ++cp;
262 rv = (*cp == '\0');
264 NYD2_LEAVE;
265 return rv;
268 static struct a_amv_mac *
269 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
270 enum a_amv_mac_flags amf){
271 struct a_amv_mac *amp, **ampp;
272 ui32_t h;
273 enum a_amv_mac_flags save_amf;
274 NYD2_ENTER;
276 save_amf = amf;
277 amf &= a_AMV_MF_TYPE_MASK;
278 h = a_AMV_NAME2HASH(name);
279 h = a_AMV_HASH2PRIME(h);
280 ampp = &a_amv_macs[h];
282 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
283 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
284 !strcmp(amp->am_name, name)){
285 if(LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
286 goto jleave;
288 *ampp = amp->am_next;
290 if((amf & a_AMV_MF_ACC) &&
291 account_name != NULL && !strcmp(account_name, name)){
292 amp->am_flags |= a_AMV_MF_DEL;
293 n_err(_("Delayed deletion of active account \"%s\"\n"), name);
294 }else{
295 a_amv_mac_free(amp);
296 amp = (struct a_amv_mac*)-1;
298 goto jleave;
302 if(newamp != NULL){
303 ampp = &a_amv_macs[h];
304 newamp->am_next = *ampp;
305 *ampp = newamp;
306 amp = NULL;
308 jleave:
309 NYD2_LEAVE;
310 return amp;
313 static bool_t
314 a_amv_mac_exec(struct a_amv_mac const *amp, struct a_amv_var **unroller,
315 bool_t lopts_on){
316 struct a_amv_lostack los;
317 struct a_amv_mac_line **amlp;
318 char **args_base, **args;
319 bool_t rv;
320 NYD2_ENTER;
322 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
323 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
324 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
325 *args = NULL;
327 los.as_up = a_amv_lopts;
328 los.as_mac = UNCONST(amp); /* But not used.. */
329 los.as_lopts = (unroller == NULL) ? NULL : *unroller;
330 los.as_unroll = lopts_on;
332 a_amv_lopts = &los;
333 rv = n_source_macro(amp->am_name, args_base);
334 a_amv_lopts = los.as_up;
336 if(unroller == NULL){
337 if(los.as_lopts != NULL)
338 a_amv_lopts_unroll(&los.as_lopts);
339 }else
340 *unroller = los.as_lopts;
341 NYD2_LEAVE;
342 return rv;
345 static bool_t
346 a_amv_mac_show(enum a_amv_mac_flags amf){
347 size_t lc, mc, ti, i;
348 char const *typestr;
349 FILE *fp;
350 bool_t rv;
351 NYD2_ENTER;
353 rv = FAL0;
355 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
356 NULL){
357 n_perr(_("Can't create temporary file for `define' or `account' listing"),
359 goto jleave;
362 amf &= a_AMV_MF_TYPE_MASK;
363 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
365 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
366 struct a_amv_mac *amp;
368 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
369 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
370 struct a_amv_mac_line **amlpp;
372 if(++mc > 1){
373 putc('\n', fp);
374 ++lc;
376 ++lc;
377 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
378 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
379 for(i = (*amlpp)->aml_prespc; i > 0; --i)
380 putc(' ', fp);
381 fputs((*amlpp)->aml_dat, fp);
382 putc('\n', fp);
384 fputs("}\n", fp);
385 ++lc;
389 if(mc > 0)
390 page_or_print(fp, lc);
392 rv = (ferror(fp) == 0);
393 Fclose(fp);
394 jleave:
395 NYD2_LEAVE;
396 return rv;
399 static bool_t
400 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
401 struct str line;
402 ui32_t line_cnt, maxlen;
403 struct linelist{
404 struct linelist *ll_next;
405 struct a_amv_mac_line *ll_amlp;
406 } *llp, *ll_head, *ll_tail;
407 union {size_t s; int i; ui32_t ui; size_t l;} n;
408 struct a_amv_mac *amp;
409 bool_t rv;
410 NYD2_ENTER;
412 memset(&line, 0, sizeof line);
413 rv = FAL0;
414 amp = NULL;
416 /* Read in the lines which form the macro content */
417 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
418 ui32_t leaspc;
419 char *cp;
421 n.i = n_lex_input("", TRU1, &line.s, &line.l, NULL);
422 if(n.ui == 0)
423 continue;
424 if(n.i < 0){
425 n_err(_("Unterminated %s definition: \"%s\"\n"),
426 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
427 goto jerr;
429 if(a_amv_mac_is_closing_angle(line.s))
430 break;
432 /* Trim WS, remember amount of leading spaces for display purposes */
433 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
434 if(*cp == '\t')
435 leaspc = (leaspc + 8) & ~7;
436 else if(*cp == ' ')
437 ++leaspc;
438 else
439 break;
440 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
442 if(n.ui == 0)
443 continue;
445 maxlen = MAX(maxlen, n.ui);
446 cp[n.ui++] = '\0';
448 if(LIKELY(++line_cnt < UI32_MAX)){
449 struct a_amv_mac_line *amlp;
451 llp = salloc(sizeof *llp);
452 if(ll_head == NULL)
453 ll_head = llp;
454 else
455 ll_tail->ll_next = llp;
456 ll_tail = llp;
457 llp->ll_next = NULL;
458 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
459 VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
460 amlp->aml_len = n.ui -1;
461 amlp->aml_prespc = leaspc;
462 memcpy(amlp->aml_dat, cp, n.ui);
463 }else{
464 n_err(_("Too much content in %s definition: \"%s\"\n"),
465 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
466 goto jerr;
470 /* Create the new macro */
471 n.s = strlen(name) +1;
472 amp = smalloc(sizeof(*amp) - VFIELD_SIZEOF(struct a_amv_mac, am_name) + n.s);
473 amp->am_next = NULL;
474 amp->am_maxlen = maxlen;
475 amp->am_line_cnt = line_cnt;
476 amp->am_flags = amf;
477 amp->am_lopts = NULL;
478 memcpy(amp->am_name, name, n.s);
479 /* C99 */{
480 struct a_amv_mac_line **amlpp;
482 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
483 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
484 *amlpp++ = llp->ll_amlp;
485 *amlpp = NULL;
488 /* Finally check wether such a macro already exists, in which case we throw
489 * it all away again. At least we know it would have worked */
490 if(a_amv_mac_lookup(name, amp, amf) != NULL){
491 n_err(_("A %s named \"%s\" already exists\n"),
492 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
493 goto jerr;
496 rv = TRU1;
497 jleave:
498 if(line.s != NULL)
499 free(line.s);
500 NYD2_LEAVE;
501 return rv;
503 jerr:
504 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
505 free(llp->ll_amlp);
506 if(amp != NULL){
507 free(amp->am_line_dat);
508 free(amp);
510 goto jleave;
513 static bool_t
514 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
515 struct a_amv_mac *amp;
516 bool_t rv;
517 NYD2_ENTER;
519 rv = TRU1;
521 if(LIKELY(name[0] != '*' || name[1] != '\0')){
522 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
523 n_err(_("%s \"%s\" is not defined\n"),
524 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
525 rv = FAL0;
527 }else{
528 struct a_amv_mac **ampp, *lamp;
530 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[NELEM(a_amv_macs)]);
531 ++ampp)
532 for(lamp = NULL, amp = *ampp; amp != NULL;){
533 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
534 /* xxx Expensive but rare: be simple */
535 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
536 amp = (lamp == NULL) ? *ampp : lamp->am_next;
537 }else{
538 lamp = amp;
539 amp = amp->am_next;
543 NYD2_LEAVE;
544 return rv;
547 static void
548 a_amv_mac_free(struct a_amv_mac *amp){
549 struct a_amv_mac_line **amlpp;
550 NYD2_ENTER;
552 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
553 free(*amlpp);
554 free(amp->am_line_dat);
555 free(amp);
556 NYD2_LEAVE;
559 static void
560 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
561 struct a_amv_var *oavp){
562 struct a_amv_var *avp;
563 size_t nl, vl;
564 NYD2_ENTER;
566 /* Propagate unrolling up the stack, as necessary. Store all unroll
567 * information in the uppermost level which declared it wants to unroll, in
568 * order to avoid duplicates and thus needless actions when unrolling */
569 /* C99 */{
570 struct a_amv_lostack *lalp;
572 for(lalp = NULL; alp != NULL; alp = alp->as_up)
573 if(alp->as_unroll)
574 lalp = alp;
575 if((alp = lalp) == NULL)
576 goto jleave;
579 /* Check wether this variable is handled yet */
580 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
581 if(!strcmp(avp->av_name, name))
582 goto jleave;
584 nl = strlen(name) +1;
585 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
586 avp = smalloc(sizeof(*avp) - VFIELD_SIZEOF(struct a_amv_var, av_name) +
587 nl + vl);
588 avp->av_link = alp->as_lopts;
589 alp->as_lopts = avp;
590 memcpy(avp->av_name, name, nl);
591 if(vl == 0){
592 avp->av_value = NULL;
593 avp->av_flags = 0;
594 #ifdef HAVE_PUTENV
595 avp->av_env = NULL;
596 #endif
597 }else{
598 avp->av_value = avp->av_name + nl;
599 avp->av_flags = oavp->av_flags;
600 memcpy(avp->av_value, oavp->av_value, vl);
601 #ifdef HAVE_PUTENV
602 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
603 #endif
605 jleave:
606 NYD2_LEAVE;
609 static void
610 a_amv_lopts_unroll(struct a_amv_var **avpp){
611 struct a_amv_lostack *save_alp;
612 struct a_amv_var *x, *avp;
613 NYD2_ENTER;
615 avp = *avpp;
616 *avpp = NULL;
618 save_alp = a_amv_lopts;
619 a_amv_lopts = NULL;
620 while(avp != NULL){
621 x = avp;
622 avp = avp->av_link;
623 vok_vset(x->av_name, x->av_value);
624 free(x);
626 a_amv_lopts = save_alp;
627 NYD2_LEAVE;
630 static char *
631 a_amv_var_copy(char const *str){
632 char *news;
633 size_t len;
634 NYD2_ENTER;
636 if(*str == '\0')
637 news = UNCONST("");
638 else{
639 len = strlen(str) +1;
640 news = smalloc(len);
641 memcpy(news, str, len);
643 NYD2_LEAVE;
644 return news;
647 static void
648 a_amv_var_free(char *cp){
649 NYD2_ENTER;
650 if(*cp != '\0')
651 free(cp);
652 NYD2_LEAVE;
655 static bool_t
656 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
657 char *cp;
658 int flag;
659 bool_t ok;
660 NYD2_ENTER;
662 ok = TRU1;
663 flag = 0;
664 cp = NULL;
666 switch(okey){
667 case ok_b_debug:
668 flag = OPT_DEBUG;
669 break;
670 case ok_v_folder:
671 ok = (val != NULL && var_folder_updated(*val, &cp));
672 if(ok && cp != NULL){
673 a_amv_var_free(*val);
674 /* Ensure we don't leak later on */
675 if(*cp == '\0'){
676 *val = UNCONST("");
677 free(cp);
678 }else
679 *val = cp;
681 break;
682 case ok_v_HOME:
683 assert(enable);
684 homedir = *val; /* XXX replace users with ok_vlook(HOME) */
685 break;
686 case ok_b_header:
687 flag = OPT_N_FLAG;
688 enable = !enable;
689 break;
690 case ok_v_LOGNAME:
691 assert(enable);
692 myname = *val; /* XXX replace users with ok_vlook(LOGNAME) */
693 break;
694 case ok_b_memdebug:
695 flag = OPT_MEMDEBUG;
696 break;
697 case ok_b_skipemptybody:
698 flag = OPT_E_FLAG;
699 break;
700 case ok_v_TMPDIR:
701 if(enable) /* DEFVAL will soon ensure a value otherwise! */
702 tempdir = *val; /* XXX replace users with ok_vlook(TMPDIR) */
703 break;
704 case ok_v_USER:
705 if(options & OPT_D_V)
706 n_err(_("Redirecting $USER to standardized $LOGNAME environment\n"));
707 assert(enable);
708 ok_vset(LOGNAME, *val);
709 break;
710 case ok_b_verbose:
711 flag = (enable && !(options & OPT_VERB))
712 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
713 break;
714 default:
715 DBG( n_err("Implementation error: never heard of %u\n", ok); )
716 break;
719 if(flag){
720 if(enable)
721 options |= flag;
722 else
723 options &= ~flag;
725 NYD2_LEAVE;
726 return ok;
729 static char const *
730 a_amv_var_canonify(char const *vn){
731 NYD2_ENTER;
732 if(!upperchar(*vn)){
733 char const *vp;
735 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
737 vn = (*vp == '@') ? i_strdup(vn) : vn;
739 NYD2_LEAVE;
740 return vn;
743 static bool_t
744 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
745 ui32_t hash, i, j;
746 struct a_amv_var_map const *avmp;
747 NYD2_ENTER;
749 avcp->avc_name = name = a_amv_var_canonify(name);
750 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
752 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
753 ui32_t x = a_amv_var_revmap[i];
755 if(x == a_AMV_VAR_REV_ILL)
756 break;
758 avmp = &a_amv_var_map[x];
759 if(avmp->avm_hash == hash &&
760 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
761 avcp->avc_map = avmp;
762 avcp->avc_okey = (enum okeys)x;
763 goto jleave;
766 if(++i == a_AMV_VAR_REV_PRIME){
767 #ifdef a_AMV_VAR_REV_WRAPAROUND
768 i = 0;
769 #else
770 break;
771 #endif
774 avcp->avc_map = NULL;
775 avcp = NULL;
776 jleave:
777 NYD2_LEAVE;
778 return (avcp != NULL);
781 static bool_t
782 a_amv_var_lookup(struct a_amv_var_carrier *avcp){
783 size_t i;
784 char const *cp;
785 struct a_amv_var_map const *avmp;
786 struct a_amv_var *avp;
787 NYD2_ENTER;
789 /* C99 */{
790 struct a_amv_var **avpp, *lavp;
792 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
794 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
795 if(!strcmp(avp->av_name, avcp->avc_name)){
796 /* Relink as head, hope it "sorts on usage" over time.
797 * _clear() relies on this behaviour */
798 if(lavp != NULL){
799 lavp->av_link = avp->av_link;
800 avp->av_link = *avpp;
801 *avpp = avp;
803 goto jleave;
807 /* If this is not an assembled variable we need to consider some special
808 * initialization cases and eventually create the variable anew */
809 if(LIKELY((avmp = avcp->avc_map) != NULL)){
810 /* Does it have an import-from-environment flag? */
811 if(UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
812 if((cp = getenv(avcp->avc_name)) != NULL){
813 /* May be better not to use that one, though? */
814 bool_t isempty, isbltin;
816 isempty = (*cp == '\0' &&
817 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
818 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
819 ) != 0);
821 if(isempty){
822 if(!isbltin)
823 goto jerr;
825 goto jnewval;
829 /* A first-time init switch is to be handled now and here */
830 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
831 static struct a_amv_var_defval const **arr,
832 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
834 if(arr == NULL){
835 arr = &arr_base[0];
836 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
837 while(i-- > 0)
838 arr[i] = &a_amv_var_i3vals[i];
841 for(i = 0; arr[i] != NULL; ++i)
842 if(arr[i]->avdv_okey == avcp->avc_okey){
843 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
844 : arr[i]->avdv_value;
845 /* Remove this entry, hope entire block becomes no-op asap */
847 arr[i] = arr[i + 1];
848 while(arr[i++] != NULL);
849 goto jnewval;
853 /* The virtual variables */
854 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
855 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
856 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
857 avp = UNCONST(a_amv_var_virts[i].avv_var);
858 goto jleave;
860 /* Not reached */
863 /* Place this last because once it is set first the variable will never
864 * be removed again and thus match in the first block above */
865 if(UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
866 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
867 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
868 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
869 : a_amv_var_defvals[i].avdv_value;
870 goto jnewval;
875 jerr:
876 avp = NULL;
877 jleave:
878 avcp->avc_var = avp;
879 NYD2_LEAVE;
880 return (avp != NULL);
882 jnewval: /* C99 */{
883 struct a_amv_var **avpp;
884 size_t l = strlen(avcp->avc_name) +1;
886 avcp->avc_var = avp = smalloc(sizeof(*avp) -
887 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
888 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
889 *avpp = avp;
890 memcpy(avp->av_name, avcp->avc_name, l);
891 avp->av_value = a_amv_var_copy(cp);
892 #ifdef HAVE_PUTENV
893 avp->av_env = NULL;
894 #endif
895 avp->av_flags = avmp->avm_flags;
897 if(avp->av_flags & a_AMV_VF_VIP)
898 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
899 if(avp->av_flags & a_AMV_VF_ENV)
900 a_amv_var__putenv(avcp, avp);
901 goto jleave;
905 static bool_t
906 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
907 bool_t force_env){
908 struct a_amv_var *avp;
909 char *oval;
910 struct a_amv_var_map const *avmp;
911 bool_t ok;
912 NYD2_ENTER;
914 if(value == NULL){
915 ok = a_amv_var_clear(avcp, force_env);
916 goto jleave;
919 a_amv_var_lookup(avcp);
921 if((avmp = avcp->avc_map) != NULL){
922 ok = FAL0;
924 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0)){
925 n_err(_("Variable is readonly: \"%s\"\n"), avcp->avc_name);
926 goto jleave;
928 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
929 n_err(_("Variable must not be empty: \"%s\"\n"), avcp->avc_name);
930 goto jleave;
932 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
933 !(pstate & PS_STARTED))){
934 n_err(_("Variable cannot be set in a resource file: \"%s\"\n"),
935 avcp->avc_name);
936 goto jleave;
940 ok = TRU1;
942 /* Don't care what happens later on, store this in the unroll list */
943 if(a_amv_lopts != NULL)
944 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
946 if((avp = avcp->avc_var) == NULL){
947 struct a_amv_var **avpp;
948 size_t l = strlen(avcp->avc_name) +1;
950 avcp->avc_var = avp = smalloc(sizeof(*avp) -
951 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
952 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
953 *avpp = avp;
954 #ifdef HAVE_PUTENV
955 avp->av_env = NULL;
956 #endif
957 memcpy(avp->av_name, avcp->avc_name, l);
958 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
959 oval = UNCONST("");
960 }else
961 oval = avp->av_value;
963 if(avmp == NULL)
964 avp->av_value = a_amv_var_copy(value);
965 else{
966 /* Via `set' etc. the user may give even boolean options non-boolean
967 * values, ignore that and force boolean */
968 if(avp->av_flags & a_AMV_VF_BOOL){
969 if((options & OPT_D_VV) && *value != '\0')
970 n_err(_("\"%s\" is a boolean variable, ignoring value: %s\n"),
971 avcp->avc_name, value);
972 avp->av_value = UNCONST(value = "");
973 }else
974 avp->av_value = a_amv_var_copy(value);
976 /* Check if update allowed XXX wasteful on error! */
977 if((avp->av_flags & a_AMV_VF_VIP) &&
978 !(ok = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
979 char *cp = avp->av_value;
981 avp->av_value = oval;
982 oval = cp;
986 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
987 avp->av_flags |= a_AMV_VF_LINKED;
988 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
989 ok = a_amv_var__putenv(avcp, avp);
991 a_amv_var_free(oval);
992 jleave:
993 NYD2_LEAVE;
994 return ok;
997 static bool_t
998 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
999 #ifndef HAVE_SETENV
1000 char *cp;
1001 #endif
1002 bool_t rv;
1003 NYD2_ENTER;
1005 #ifdef HAVE_SETENV
1006 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1007 #else
1008 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1010 if((rv = (putenv(cp) == 0))){
1011 char *ocp;
1013 if((ocp = avp->av_env) != NULL)
1014 free(ocp);
1015 avp->av_env = cp;
1016 }else
1017 free(cp);
1018 #endif
1019 NYD2_LEAVE;
1020 return rv;
1023 static bool_t
1024 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1025 struct a_amv_var **avpp, *avp;
1026 struct a_amv_var_map const *avmp;
1027 bool_t rv;
1028 NYD2_ENTER;
1030 rv = TRU1;
1032 if(UNLIKELY(!a_amv_var_lookup(avcp))){
1033 if(force_env)
1034 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1035 else if(!(pstate & PS_ROBOT) && (options & OPT_D_V))
1036 n_err(_("Can't unset undefined variable: \"%s\"\n"), avcp->avc_name);
1037 goto jleave;
1040 if(LIKELY((avmp = avcp->avc_map) != NULL)){
1041 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0)){
1042 n_err(_("Variable may not be unset: \"%s\"\n"), avcp->avc_name);
1043 rv = FAL0;
1044 goto jleave;
1046 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1047 !(rv = a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL)))
1048 goto jleave;
1051 if(a_amv_lopts != NULL)
1052 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1054 avp = avcp->avc_var;
1055 avcp->avc_var = NULL;
1056 avpp = &a_amv_vars[avcp->avc_prime];
1057 assert(*avpp == avp); /* (always listhead after lookup()) */
1058 *avpp = (*avpp)->av_link;
1060 /* C99 */{
1061 #ifdef HAVE_SETENV
1062 char *envval = NULL;
1063 #else
1064 char *envval = avp->av_env;
1065 #endif
1066 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1067 rv = a_amv_var__clearenv(avp->av_name, envval);
1069 a_amv_var_free(avp->av_value);
1070 free(avp);
1072 /* XXX Fun part, extremely simple-minded for now: if this variable has
1073 * XXX a default value, immediately reinstantiate it! */
1074 if(UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1075 a_amv_var_lookup(avcp);
1076 jleave:
1077 NYD2_LEAVE;
1078 return rv;
1081 static bool_t
1082 a_amv_var__clearenv(char const *name, char *value){
1083 #ifndef HAVE_SETENV
1084 extern char **environ;
1085 char **ecpp;
1086 #endif
1087 bool_t rv;
1088 NYD2_ENTER;
1089 UNUSED(value);
1091 #ifdef HAVE_SETENV
1092 unsetenv(name);
1093 rv = TRU1;
1094 #else
1095 if(value != NULL)
1096 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1097 if(*ecpp == value){
1098 free(value);
1100 ecpp[0] = ecpp[1];
1101 while(*ecpp++ != NULL);
1102 break;
1104 rv = TRU1;
1105 #endif
1106 NYD2_LEAVE;
1107 return rv;
1110 static void
1111 a_amv_var_show_all(void){
1112 struct n_string msg, *msgp;
1113 FILE *fp;
1114 size_t no, i;
1115 struct a_amv_var *avp;
1116 char const **vacp, **cap;
1117 NYD2_ENTER;
1119 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1120 n_perr(_("Can't create temporary file for `set' listing"), 0);
1121 goto jleave;
1124 /* We need to instantiate first-time-inits and default values here, so that
1125 * they will be regular members of our _vars[] table */
1126 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1127 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1128 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1129 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1131 for(no = i = 0; i < a_AMV_PRIME; ++i)
1132 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1133 ++no;
1134 no += a_AMV_VAR_VIRTS_CNT;
1136 vacp = salloc(no * sizeof(*vacp));
1138 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1139 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1140 *cap++ = avp->av_name;
1141 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1142 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1144 if(no > 1)
1145 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1147 msgp = &msg;
1148 msgp = n_string_reserve(n_string_creat(msgp), 80);
1149 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1150 i += a_amv_var_show(*cap, fp, msgp);
1151 n_string_gut(&msg);
1153 page_or_print(fp, i);
1154 Fclose(fp);
1155 jleave:
1156 NYD2_LEAVE;
1159 static int
1160 a_amv_var__show_cmp(void const *s1, void const *s2){
1161 int rv;
1162 NYD2_ENTER;
1164 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
1165 NYD2_LEAVE;
1166 return rv;
1169 static size_t
1170 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1171 struct a_amv_var_carrier avc;
1172 size_t i;
1173 NYD2_ENTER;
1175 msgp = n_string_trunc(msgp, 0);
1176 i = 0;
1178 a_amv_var_revlookup(&avc, name);
1179 if(!a_amv_var_lookup(&avc)){
1180 msgp = n_string_assign_cp(msgp, _("No such variable: \""));
1181 msgp = n_string_push_cp(msgp, name);
1182 msgp = n_string_push_c(msgp, '"');
1183 goto jleave;
1186 if(options & OPT_D_V){
1187 if(avc.avc_map == NULL){
1188 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1189 msgp = n_string_push_cp(msgp, "assembled");
1191 /* C99 */{
1192 struct{
1193 ui16_t flags;
1194 char msg[22];
1195 } const tbase[] = {
1196 {a_AMV_VF_VIRT, "virtual"},
1197 {a_AMV_VF_RDONLY, "readonly"},
1198 {a_AMV_VF_NODEL, "nodelete"},
1199 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1200 {a_AMV_VF_ENV, "sync-environ"},
1201 {a_AMV_VF_I3VAL, "initial-value"},
1202 {a_AMV_VF_DEFVAL, "default-value"},
1203 {a_AMV_VF_LINKED, "`environ' link"}
1204 }, *tp;
1206 for(tp = tbase; PTRCMP(tp, <, &tbase[NELEM(tbase)]); ++tp)
1207 if(avc.avc_var->av_flags & tp->flags){
1208 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1209 msgp = n_string_push_cp(msgp, tp->msg);
1213 if(i > 0)
1214 msgp = n_string_push_cp(msgp, "\n ");
1217 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1218 msgp = n_string_push_cp(msgp, "# ");
1219 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1220 msgp = n_string_push_cp(msgp, "environ ");
1221 msgp = n_string_push_cp(msgp, "set ");
1222 msgp = n_string_push_cp(msgp, name);
1223 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1224 msgp = n_string_push_buf(msgp, "=\"", 2);
1225 msgp = n_string_push_cp(msgp,
1226 a_amv_var__simple_quote(avc.avc_var->av_value));
1227 msgp = n_string_push_c(msgp, '"');
1230 jleave:
1231 msgp = n_string_push_c(msgp, '\n');
1232 fputs(n_string_cp(msgp), fp);
1233 NYD2_ENTER;
1234 return (i > 0 ? 2 : 1);
1237 static char *
1238 a_amv_var__simple_quote(char const *cp){ /* TODO <> string_quote(), etc.. */
1239 bool_t esc;
1240 size_t i;
1241 char const *cp_base;
1242 char c, *rv;
1243 NYD2_ENTER;
1245 for(i = 0, cp_base = cp; (c = *cp) != '\0'; ++i, ++cp)
1246 switch(c){
1247 case '\n':
1248 case '\t':
1249 case '"':
1250 ++i;
1251 /* FALLTHRU */
1252 default:
1253 break;
1255 rv = salloc(i +1);
1257 for(esc = FAL0, i = 0, cp = cp_base; (c = *cp) != '\0'; rv[i++] = c, ++cp){
1258 if(!esc){
1259 switch(c){
1260 case '\n':
1261 case '\t':
1262 c = (c == '\n' ? 'n' : 't');
1263 /* FALLTHRU */
1264 case '"':
1265 rv[i++] = '\\';
1266 /* FALLTHRU */
1267 default:
1268 break;
1270 esc = (c == '\\');
1271 }else
1272 esc = FAL0;
1274 rv[i] = '\0';
1275 NYD2_LEAVE;
1276 return rv;
1279 static bool_t
1280 a_amv_var_c_set(char **ap, bool_t issetenv){
1281 char *cp, *cp2, *varbuf, c;
1282 size_t errs;
1283 NYD2_ENTER;
1285 errs = 0;
1286 jouter:
1287 while((cp = *ap++) != NULL){
1288 /* Isolate key */
1289 cp2 = varbuf = salloc(strlen(cp) +1);
1291 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1292 if(cntrlchar(c) || whitechar(c)){
1293 n_err(_("Variable name with control or newline character ignored: "
1294 "\"%s\"\n"), ap[-1]);
1295 ++errs;
1296 goto jouter;
1298 *cp2++ = c;
1300 *cp2 = '\0';
1301 if(c == '\0')
1302 cp = UNCONST("");
1303 else
1304 ++cp;
1306 if(varbuf == cp2){
1307 n_err(_("Empty variable name ignored\n"));
1308 ++errs;
1309 }else{
1310 struct a_amv_var_carrier avc;
1311 bool_t isunset;
1313 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1314 varbuf = &varbuf[2];
1316 a_amv_var_revlookup(&avc, varbuf);
1318 if(isunset)
1319 errs += !a_amv_var_clear(&avc, issetenv);
1320 else
1321 errs += !a_amv_var_set(&avc, cp, issetenv);
1324 NYD2_LEAVE;
1325 return (errs == 0);
1328 FL int
1329 c_define(void *v){
1330 int rv;
1331 char **args;
1332 NYD_ENTER;
1334 rv = 1;
1336 if((args = v)[0] == NULL){
1337 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1338 goto jleave;
1341 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1342 args[2] != NULL){
1343 n_err(_("Synopsis: define: <name> {\n"));
1344 goto jleave;
1347 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1348 jleave:
1349 NYD_LEAVE;
1350 return rv;
1353 FL int
1354 c_undefine(void *v){
1355 int rv;
1356 char **args;
1357 NYD_ENTER;
1359 rv = 0;
1360 args = v;
1362 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1363 while(*++args != NULL);
1364 NYD_LEAVE;
1365 return rv;
1368 FL int
1369 c_call(void *v){
1370 int rv;
1371 char **args;
1372 char const *errs, *name;
1373 struct a_amv_mac *amp;
1374 NYD_ENTER;
1376 rv = 1;
1378 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1379 errs = _("Synopsis: call: <%s>\n");
1380 name = "name";
1381 goto jerr;
1384 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1385 errs = _("Undefined macro `call'ed: \"%s\"\n");
1386 goto jerr;
1389 rv = (a_amv_mac_exec(amp, NULL, FAL0) == FAL0);
1390 jleave:
1391 NYD_LEAVE;
1392 return rv;
1393 jerr:
1394 n_err(errs, name);
1395 goto jleave;
1398 FL bool_t
1399 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1400 size_t len;
1401 char *var, *cp;
1402 struct a_amv_mac *amp;
1403 struct a_amv_var **unroller;
1404 bool_t rv = TRU1;
1405 NYD_ENTER;
1407 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1409 /* First try the fully resolved path */
1410 snprintf(var, len, "folder-hook-%s", mailname);
1411 if((cp = vok_vlook(var)) != NULL)
1412 goto jmac;
1414 /* If we are under *folder*, try the usual +NAME syntax, too */
1415 if(displayname[0] == '+'){
1416 char *x = mailname + len;
1418 for(; x > mailname; --x)
1419 if(x[-1] == '/'){
1420 snprintf(var, len, "folder-hook-+%s", x);
1421 if((cp = vok_vlook(var)) != NULL)
1422 goto jmac;
1423 break;
1427 /* Plain *folder-hook* is our last try */
1428 if((cp = ok_vlook(folder_hook)) == NULL)
1429 goto jleave;
1431 jmac:
1432 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1433 n_err(_("Cannot call *folder-hook* for \"%s\": "
1434 "macro \"%s\" does not exist\n"), displayname, cp);
1435 rv = FAL0;
1436 goto jleave;
1439 pstate &= ~PS_HOOK_MASK;
1440 if(nmail){
1441 pstate |= PS_HOOK_NEWMAIL;
1442 unroller = NULL;
1443 }else{
1444 pstate |= PS_HOOK;
1445 unroller = &a_amv_folder_hook_lopts;
1447 rv = a_amv_mac_exec(amp, unroller, TRU1);
1448 pstate &= ~PS_HOOK_MASK;
1450 jleave:
1451 NYD_LEAVE;
1452 return rv;
1455 FL void
1456 call_compose_mode_hook(char const *macname){ /* TODO temporary, v15: drop */
1457 struct a_amv_mac *amp;
1458 NYD_ENTER;
1460 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1461 n_err(_("Cannot call *on-compose-**: macro \"%s\" does not exist\n"),
1462 macname);
1463 else{
1464 pstate &= ~PS_HOOK_MASK;
1465 pstate |= PS_HOOK;
1466 a_amv_mac_exec(amp, &a_amv_compose_lopts, TRU1);
1467 pstate &= ~PS_HOOK_MASK;
1469 NYD_LEAVE;
1472 FL int
1473 c_account(void *v){
1474 char **args;
1475 struct a_amv_mac *amp;
1476 int rv, i, oqf, nqf;
1477 NYD_ENTER;
1479 rv = 1;
1481 if((args = v)[0] == NULL){
1482 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1483 goto jleave;
1486 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1487 if(args[2] != NULL){
1488 n_err(_("Synopsis: account: <name> {\n"));
1489 goto jleave;
1491 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1492 n_err(_("`account': error: \"%s\" is a reserved name\n"),
1493 ACCOUNT_NULL);
1494 goto jleave;
1496 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1497 goto jleave;
1500 if(pstate & PS_HOOK_MASK){
1501 n_err(_("`account': can't change account from within a hook\n"));
1502 goto jleave;
1505 save_mbox_for_possible_quitstuff();
1507 amp = NULL;
1508 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1509 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1510 n_err(_("`account': account \"%s\" does not exist\n"), args[0]);
1511 goto jleave;
1514 oqf = savequitflags();
1516 if(a_amv_acc_curr != NULL){
1517 if(a_amv_acc_curr->am_lopts != NULL)
1518 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1519 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1520 a_amv_mac_free(a_amv_acc_curr);
1523 account_name = (amp != NULL) ? amp->am_name : NULL;
1524 a_amv_acc_curr = amp;
1526 if(amp != NULL){
1527 assert(amp->am_lopts == NULL);
1528 if(!a_amv_mac_exec(amp, &amp->am_lopts, TRU1)){
1529 /* XXX account switch incomplete, unroll? */
1530 n_err(_("`account': switching to account \"%s\" failed\n"), args[0]);
1531 goto jleave;
1535 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1536 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1537 restorequitflags(oqf);
1538 if((i = setfile("%", 0)) < 0)
1539 goto jleave;
1540 check_folder_hook(FAL0);
1541 if(i > 0 && !ok_blook(emptystart))
1542 goto jleave;
1543 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1544 restorequitflags(nqf);
1546 rv = 0;
1547 jleave:
1548 NYD_LEAVE;
1549 return rv;
1552 FL int
1553 c_unaccount(void *v){
1554 int rv;
1555 char **args;
1556 NYD_ENTER;
1558 rv = 0;
1559 args = v;
1561 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1562 while(*++args != NULL);
1563 NYD_LEAVE;
1564 return rv;
1567 FL int
1568 c_localopts(void *v){
1569 char **argv;
1570 int rv;
1571 NYD_ENTER;
1573 rv = 1;
1575 if(a_amv_lopts == NULL){
1576 n_err(_("Cannot use `localopts' but from within a "
1577 "`define' or `account'\n"));
1578 goto jleave;
1581 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1582 rv = 0;
1583 jleave:
1584 NYD_LEAVE;
1585 return rv;
1588 FL void
1589 temporary_localopts_free(void){ /* XXX intermediate hack */
1590 NYD_ENTER;
1591 if(a_amv_compose_lopts != NULL){
1592 void *save = a_amv_lopts;
1594 a_amv_lopts = NULL;
1595 a_amv_lopts_unroll(&a_amv_compose_lopts);
1596 a_amv_compose_lopts = NULL;
1597 a_amv_lopts = save;
1599 NYD_LEAVE;
1602 FL void
1603 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1604 NYD_ENTER;
1605 if(a_amv_folder_hook_lopts != NULL){
1606 void *save = a_amv_lopts;
1608 a_amv_lopts = NULL;
1609 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1610 a_amv_folder_hook_lopts = NULL;
1611 a_amv_lopts = save;
1613 NYD_LEAVE;
1616 FL char *
1617 n_var_oklook(enum okeys okey){
1618 struct a_amv_var_carrier avc;
1619 char *rv;
1620 struct a_amv_var_map const *avmp;
1621 NYD_ENTER;
1623 avc.avc_map = avmp = &a_amv_var_map[okey];
1624 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1625 avc.avc_hash = avmp->avm_hash;
1626 avc.avc_okey = okey;
1628 if(a_amv_var_lookup(&avc))
1629 rv = avc.avc_var->av_value;
1630 else
1631 rv = NULL;
1632 NYD_LEAVE;
1633 return rv;
1636 FL bool_t
1637 n_var_okset(enum okeys okey, uintptr_t val){
1638 struct a_amv_var_carrier avc;
1639 bool_t ok;
1640 struct a_amv_var_map const *avmp;
1641 NYD_ENTER;
1643 avc.avc_map = avmp = &a_amv_var_map[okey];
1644 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1645 avc.avc_hash = avmp->avm_hash;
1646 avc.avc_okey = okey;
1648 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1649 NYD_LEAVE;
1650 return ok;
1653 FL bool_t
1654 n_var_okclear(enum okeys okey){
1655 struct a_amv_var_carrier avc;
1656 bool_t rv;
1657 struct a_amv_var_map const *avmp;
1658 NYD_ENTER;
1660 avc.avc_map = avmp = &a_amv_var_map[okey];
1661 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1662 avc.avc_hash = avmp->avm_hash;
1663 avc.avc_okey = okey;
1665 rv = a_amv_var_clear(&avc, FAL0);
1666 NYD_LEAVE;
1667 return rv;
1670 FL char *
1671 n_var_voklook(char const *vokey){
1672 struct a_amv_var_carrier avc;
1673 char *rv;
1674 NYD_ENTER;
1676 a_amv_var_revlookup(&avc, vokey);
1678 rv = a_amv_var_lookup(&avc) ? avc.avc_var->av_value : NULL;
1679 NYD_LEAVE;
1680 return rv;
1683 FL bool_t
1684 n_var_vokset(char const *vokey, uintptr_t val){
1685 struct a_amv_var_carrier avc;
1686 bool_t ok;
1687 NYD_ENTER;
1689 a_amv_var_revlookup(&avc, vokey);
1691 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1692 NYD_LEAVE;
1693 return ok;
1696 FL bool_t
1697 n_var_vokclear(char const *vokey){
1698 struct a_amv_var_carrier avc;
1699 bool_t ok;
1700 NYD_ENTER;
1702 a_amv_var_revlookup(&avc, vokey);
1704 ok = a_amv_var_clear(&avc, FAL0);
1705 NYD_LEAVE;
1706 return ok;
1709 #ifdef HAVE_SOCKETS
1710 FL char *
1711 n_var_xoklook(enum okeys okey, struct url const *urlp,
1712 enum okey_xlook_mode oxm){
1713 struct a_amv_var_carrier avc;
1714 struct str const *us;
1715 size_t nlen;
1716 char *nbuf, *rv;
1717 struct a_amv_var_map const *avmp;
1718 NYD_ENTER;
1720 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1722 /* For simplicity: allow this case too */
1723 if(!(oxm & (OXM_H_P | OXM_U_H_P)))
1724 goto jplain;
1726 avc.avc_map = avmp = &a_amv_var_map[okey];
1727 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1728 avc.avc_okey = okey;
1730 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1731 nlen = strlen(avc.avc_name);
1732 nbuf = salloc(nlen + 1 + us->l +1);
1733 memcpy(nbuf, avc.avc_name, nlen);
1734 nbuf[nlen++] = '-';
1736 /* One of .url_u_h_p and .url_h_p we test in here */
1737 memcpy(nbuf + nlen, us->s, us->l +1);
1738 avc.avc_name = a_amv_var_canonify(nbuf);
1739 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1740 if(a_amv_var_lookup(&avc))
1741 goto jvar;
1743 /* The second */
1744 if(oxm & OXM_H_P){
1745 us = &urlp->url_h_p;
1746 memcpy(nbuf + nlen, us->s, us->l +1);
1747 avc.avc_name = a_amv_var_canonify(nbuf);
1748 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1749 if(a_amv_var_lookup(&avc)){
1750 jvar:
1751 rv = avc.avc_var->av_value;
1752 goto jleave;
1756 jplain:
1757 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1758 jleave:
1759 NYD_LEAVE;
1760 return rv;
1762 #endif /* HAVE_SOCKETS */
1764 FL int
1765 c_set(void *v){
1766 char **ap;
1767 int err;
1768 NYD_ENTER;
1770 if(*(ap = v) == NULL){
1771 a_amv_var_show_all();
1772 err = 0;
1773 }else
1774 err = !a_amv_var_c_set(ap, FAL0);
1775 NYD_LEAVE;
1776 return err;
1779 FL int
1780 c_unset(void *v){
1781 char **ap;
1782 int err;
1783 NYD_ENTER;
1785 for(err = 0, ap = v; *ap != NULL; ++ap)
1786 err |= !n_var_vokclear(*ap);
1787 NYD_LEAVE;
1788 return err;
1791 FL int
1792 c_varshow(void *v){
1793 char **ap;
1794 NYD_ENTER;
1796 if(*(ap = v) == NULL)
1797 v = NULL;
1798 else{
1799 struct n_string msg, *msgp = &msg;
1801 msgp = n_string_creat(msgp);
1802 for(; *ap != NULL; ++ap)
1803 a_amv_var_show(*ap, stdout, msgp);
1804 n_string_gut(msgp);
1806 NYD_LEAVE;
1807 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1810 FL int
1811 c_varedit(void *v){
1812 struct a_amv_var_carrier avc;
1813 FILE *of, *nf;
1814 char *val, **argv;
1815 int err;
1816 sighandler_type sigint;
1817 NYD_ENTER;
1819 sigint = safe_signal(SIGINT, SIG_IGN);
1821 for(err = 0, argv = v; *argv != NULL; ++argv){
1822 memset(&avc, 0, sizeof avc);
1824 a_amv_var_revlookup(&avc, *argv);
1826 if(avc.avc_map != NULL){
1827 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1828 n_err(_("`varedit': can't edit boolean variable \"%s\"\n"),
1829 avc.avc_name);
1830 continue;
1832 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
1833 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1834 avc.avc_name);
1835 continue;
1839 a_amv_var_lookup(&avc);
1841 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1842 NULL){
1843 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1844 err = 1;
1845 break;
1846 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
1847 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
1848 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1849 Fclose(of);
1850 err = 1;
1851 continue;
1854 fflush_rewind(of);
1855 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1856 Fclose(of);
1858 if(nf != NULL){
1859 int c;
1860 char *base;
1861 off_t l = fsize(nf);
1863 assert(l >= 0);
1864 base = salloc((size_t)l +1);
1866 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
1867 if(c == '\n' || c == '\r'){
1868 *val = ' ';
1869 ++l;
1870 }else{
1871 *val = (char)(uc_i)c;
1872 l = 0;
1874 val -= l;
1875 *val = '\0';
1877 if(!a_amv_var_set(&avc, base, FAL0))
1878 err = 1;
1880 Fclose(nf);
1881 }else{
1882 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
1883 err = 1;
1884 break;
1888 safe_signal(SIGINT, sigint);
1889 NYD_LEAVE;
1890 return err;
1893 FL int
1894 c_environ(void *v){
1895 struct a_amv_var_carrier avc;
1896 int err;
1897 char **ap;
1898 bool_t islnk;
1899 NYD_ENTER;
1901 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
1902 for(err = 0; *++ap != NULL;){
1903 a_amv_var_revlookup(&avc, *ap);
1905 if(a_amv_var_lookup(&avc) && (islnk ||
1906 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
1907 if(!islnk){
1908 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
1909 continue;
1910 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
1911 if(options & OPT_D_V)
1912 n_err(_("`environ': link: already established for \"%s\"\n"),
1913 *ap);
1914 continue;
1916 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
1917 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
1918 a_amv_var__putenv(&avc, avc.avc_var);
1919 }else if(!islnk){
1920 n_err(_("`environ': unlink: no link established: \"%s\"\n"), *ap);
1921 err = 1;
1922 }else{
1923 char const *evp = getenv(*ap);
1925 if(evp != NULL)
1926 err |= !a_amv_var_set(&avc, evp, TRU1);
1927 else{
1928 n_err(_("`environ': link: cannot link non-existent variable "
1929 "\"%s\"\n"), *ap);
1930 err = 1;
1934 }else if(!strcmp(*ap, "set"))
1935 err = !a_amv_var_c_set(++ap, TRU1);
1936 else if(!strcmp(*ap, "unset")){
1937 for(err = 0; *++ap != NULL;){
1938 a_amv_var_revlookup(&avc, *ap);
1940 if(!a_amv_var_clear(&avc, TRU1))
1941 err = 1;
1943 }else{
1944 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
1945 err = 1;
1947 NYD_LEAVE;
1948 return err;
1951 /* s-it-mode */