openssl.c: some namespace cleanup
[s-mailx.git] / acmava.c
blobb01ba2bfe970bb1e688676f7df08dbf0e865e598
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
45 * HOWTO add a new non-dynamic binary or value option:
46 * - add an entry to nail.h:enum okeys
47 * - run create-okey-map.pl
48 * - update nail.1
51 /* Note: changing the hash function must be reflected in create-okey-map.pl */
52 #define MA_PRIME HSHSIZE
53 #define MA_NAME2HASH(N) torek_hash(N)
54 #define MA_HASH2PRIME(H) ((H) % MA_PRIME)
56 enum ma_flags {
57 MA_NONE = 0,
58 MA_ACC = 1<<0,
59 MA_TYPE_MASK = MA_ACC,
60 MA_UNDEF = 1<<1 /* Unlink after lookup */
63 struct macro {
64 struct macro *ma_next;
65 char *ma_name;
66 struct mline *ma_contents;
67 size_t ma_maxlen; /* Maximum line length */
68 enum ma_flags ma_flags;
69 struct var *ma_localopts; /* `account' unroll list, for `localopts' */
72 struct mline {
73 struct mline *l_next;
74 size_t l_length;
75 char l_line[VFIELD_SIZE(sizeof(size_t))];
78 struct var {
79 struct var *v_link;
80 char *v_value;
81 char v_name[VFIELD_SIZE(sizeof(size_t))];
84 struct var_map {
85 ui32_t vm_hash;
86 ui16_t vm_keyoff;
87 ui8_t vm_binary;
88 ui8_t vm_special;
91 struct var_carrier {
92 char const *vc_name;
93 ui32_t vc_hash;
94 ui32_t vc_prime;
95 struct var *vc_var;
96 struct var_map const *vc_vmap;
97 enum okeys vc_okey;
100 struct lostack {
101 struct lostack *s_up; /* Outer context */
102 struct macro *s_mac; /* Context (`account' or `define') */
103 struct var *s_localopts;
104 bool_t s_unroll; /* Unroll? */
107 /* Include the constant create-okey-map.pl output */
108 #include "okeys.h"
110 static struct macro *_acc_curr; /* Currently active account */
111 static struct lostack *_localopts; /* Currently executing macro unroll list */
113 /* TODO once we have a dynamically sized hashtable we could unite _macros and
114 * TODO _variables into a single hashtable, stripping down fun interface;
115 * TODO also, setting and clearing a variable can be easily joined */
116 static struct var *_vars[MA_PRIME]; /* TODO dynamically spaced */
117 static struct macro *_macros[MA_PRIME]; /* TODO dynamically spaced */
119 /* Special cased value string allocation */
120 static char * _var_vcopy(char const *str);
121 static void _var_vfree(char *cp);
123 /* Check for special housekeeping. */
124 static bool_t _var_check_specials(enum okeys okey, bool_t enable,
125 char **val);
127 /* If a variable name begins with a lowercase-character and contains at
128 * least one '@', it is converted to all-lowercase. This is necessary
129 * for lookups of names based on email addresses.
130 * Following the standard, only the part following the last '@' should
131 * be lower-cased, but practice has established otherwise here.
132 * Return value may have been placed in string dope (salloc()) */
133 static char const * _var_canonify(char const *vn);
135 /* Try to reverse lookup an option name to an enum okeys mapping.
136 * Updates vcp.vc_name and vcp.vc_hash; vcp.vc_vmap is NULL if none found */
137 static bool_t _var_revlookup(struct var_carrier *vcp, char const *name);
139 /* Lookup a variable from vcp.vc_(vmap|name|hash), return wether it was found.
140 * Sets vcp.vc_prime; vcp.vc_var is NULL if not found */
141 static bool_t _var_lookup(struct var_carrier *vcp);
143 /* Set variable from vcp.vc_(vmap|name|hash), return wether error occurred */
144 static bool_t _var_set(struct var_carrier *vcp, char const *value);
146 /* Clear variable from vcp.vc_(vmap|name|hash); sets vcp.vc_var to NULL,
147 * return wether error occurred */
148 static bool_t _var_clear(struct var_carrier *vcp);
150 /* var_list_all(): qsort(3) helper */
151 static int _var_list_all_cmp(void const *s1, void const *s2);
153 /* Shared c_set() and c_setenv() impl, return wether error(s) occurred */
154 static bool_t _var_set_env(char **ap, bool_t issetenv);
156 /* Does cp consist solely of WS and a } */
157 static bool_t _is_closing_angle(char const *cp);
159 /* Lookup for macros/accounts */
160 static struct macro *_ma_look(char const *name, struct macro *data,
161 enum ma_flags mafl);
163 /* Walk all lines of a macro and execute() them */
164 static int _ma_exec(struct macro const *mp, struct var **unroller);
166 /* User display helpers */
167 static int _ma_list(enum ma_flags mafl);
169 /* */
170 static bool_t _ma_define1(char const *name, enum ma_flags mafl);
171 static void _ma_undef1(char const *name, enum ma_flags mafl);
172 static void _ma_freelines(struct mline *lp);
174 /* Update replay-log */
175 static void _localopts_add(struct lostack *losp, char const *name,
176 struct var *ovap);
177 static void _localopts_unroll(struct var **vapp);
179 static char *
180 _var_vcopy(char const *str)
182 char *news;
183 size_t len;
184 NYD_ENTER;
186 if (*str == '\0')
187 news = UNCONST("");
188 else {
189 len = strlen(str) +1;
190 news = smalloc(len);
191 memcpy(news, str, len);
193 NYD_LEAVE;
194 return news;
197 static void
198 _var_vfree(char *cp)
200 NYD_ENTER;
201 if (*cp != '\0')
202 free(cp);
203 NYD_LEAVE;
206 static bool_t
207 _var_check_specials(enum okeys okey, bool_t enable, char **val)
209 char *cp = NULL;
210 bool_t rv = TRU1;
211 int flag = 0;
212 NYD_ENTER;
214 switch (okey) {
215 case ok_b_debug:
216 flag = OPT_DEBUG;
217 break;
218 case ok_b_header:
219 flag = OPT_N_FLAG;
220 enable = !enable;
221 break;
222 case ok_b_skipemptybody:
223 flag = OPT_E_FLAG;
224 break;
225 case ok_b_verbose:
226 flag = OPT_VERBOSE;
227 break;
228 case ok_v_folder:
229 rv = (val == NULL || var_folder_updated(*val, &cp));
230 if (rv && cp != NULL) {
231 _var_vfree(*val);
232 /* It's smalloc()ed, but ensure we don't leak */
233 if (*cp == '\0') {
234 *val = UNCONST("");
235 free(cp);
236 } else
237 *val = cp;
239 break;
240 #ifdef HAVE_NCL
241 case ok_v_line_editor_cursor_right:
242 if ((rv = (val != NULL && *val != NULL))) {
243 /* Set with no value? TODO very guly */
244 if (*(cp = *val) != '\0') {
245 char const *x = cp;
246 int c;
247 do {
248 c = expand_shell_escape(&x, FAL0);
249 if (c < 0)
250 break;
251 *cp++ = (char)c;
252 } while (*x != '\0');
253 *cp = '\0';
256 break;
257 #endif
258 default:
259 break;
262 if (flag) {
263 if (enable)
264 options |= flag;
265 else
266 options &= ~flag;
268 NYD_LEAVE;
269 return rv;
272 static char const *
273 _var_canonify(char const *vn)
275 NYD_ENTER;
276 if (!upperchar(*vn)) {
277 char const *vp;
279 for (vp = vn; *vp != '\0' && *vp != '@'; ++vp)
281 vn = (*vp == '@') ? i_strdup(vn) : vn;
283 NYD_LEAVE;
284 return vn;
287 static bool_t
288 _var_revlookup(struct var_carrier *vcp, char const *name)
290 ui32_t hash, i, j;
291 struct var_map const *vmp;
292 NYD_ENTER;
294 vcp->vc_name = name = _var_canonify(name);
295 vcp->vc_hash = hash = MA_NAME2HASH(name);
297 for (i = hash % _VAR_REV_PRIME, j = 0; j <= _VAR_REV_LONGEST; ++j) {
298 ui32_t x = _var_revmap[i];
299 if (x == _VAR_REV_ILL)
300 break;
301 vmp = _var_map + x;
302 if (vmp->vm_hash == hash && !strcmp(_var_keydat + vmp->vm_keyoff, name)) {
303 vcp->vc_vmap = vmp;
304 vcp->vc_okey = (enum okeys)x;
305 goto jleave;
307 if (++i == _VAR_REV_PRIME) {
308 #ifdef _VAR_REV_WRAPAROUND
309 i = 0;
310 #else
311 break;
312 #endif
315 vcp->vc_vmap = NULL;
316 vcp = NULL;
317 jleave:
318 NYD_LEAVE;
319 return (vcp != NULL);
322 static bool_t
323 _var_lookup(struct var_carrier *vcp)
325 struct var **vap, *lvp, *vp;
326 NYD_ENTER;
328 vap = _vars + (vcp->vc_prime = MA_HASH2PRIME(vcp->vc_hash));
330 for (lvp = NULL, vp = *vap; vp != NULL; lvp = vp, vp = vp->v_link)
331 if (!strcmp(vp->v_name, vcp->vc_name)) {
332 /* Relink as head, hope it "sorts on usage" over time.
333 * _var_clear() relies on this behaviour */
334 if (lvp != NULL) {
335 lvp->v_link = vp->v_link;
336 vp->v_link = *vap;
337 *vap = vp;
339 goto jleave;
341 vp = NULL;
342 jleave:
343 vcp->vc_var = vp;
344 NYD_LEAVE;
345 return (vp != NULL);
348 static bool_t
349 _var_set(struct var_carrier *vcp, char const *value)
351 bool_t err;
352 struct var *vp;
353 char *oval;
354 NYD_ENTER;
356 if (value == NULL) {
357 bool_t tmp = var_clear_allow_undefined;
358 var_clear_allow_undefined = TRU1;
359 err = _var_clear(vcp);
360 var_clear_allow_undefined = tmp;
361 goto jleave;
364 _var_lookup(vcp);
366 /* Don't care what happens later on, store this in the unroll list */
367 if (_localopts != NULL)
368 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
370 if ((vp = vcp->vc_var) == NULL) {
371 size_t l = strlen(vcp->vc_name) + 1;
373 vcp->vc_var =
374 vp = smalloc(sizeof(*vp) - VFIELD_SIZEOF(struct var, v_name) + l);
375 vp->v_link = _vars[vcp->vc_prime];
376 _vars[vcp->vc_prime] = vp;
377 memcpy(vp->v_name, vcp->vc_name, l);
378 oval = UNCONST("");
379 } else
380 oval = vp->v_value;
381 vp->v_value = _var_vcopy(value);
383 /* Check if update allowed XXX wasteful on error! */
384 if (vcp->vc_vmap != NULL && vcp->vc_vmap->vm_special &&
385 (err = !_var_check_specials(vcp->vc_okey, TRU1, &vp->v_value))) {
386 char *cp = vp->v_value;
387 vp->v_value = oval;
388 oval = cp;
390 if (*oval != '\0')
391 _var_vfree(oval);
392 err = FAL0;
393 jleave:
394 NYD_LEAVE;
395 return err;
398 static bool_t
399 _var_clear(struct var_carrier *vcp)
401 bool_t err = TRU1;
402 NYD_ENTER;
404 if (!_var_lookup(vcp)) {
405 if (!sourcing && !var_clear_allow_undefined) {
406 fprintf(stderr, tr(203, "`%s': undefined variable\n"), vcp->vc_name);
407 goto jleave;
409 } else {
410 if (_localopts != NULL)
411 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
413 /* Always listhead after _var_lookup() */
414 _vars[vcp->vc_prime] = _vars[vcp->vc_prime]->v_link;
415 _var_vfree(vcp->vc_var->v_value);
416 free(vcp->vc_var);
417 vcp->vc_var = NULL;
419 if (vcp->vc_vmap != NULL && vcp->vc_vmap->vm_special &&
420 !_var_check_specials(vcp->vc_okey, FAL0, NULL))
421 goto jleave;
423 err = FAL0;
424 jleave:
425 NYD_LEAVE;
426 return err;
429 static int
430 _var_list_all_cmp(void const *s1, void const *s2)
432 int rv;
433 NYD_ENTER;
435 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
436 NYD_LEAVE;
437 return rv;
440 static bool_t
441 _var_set_env(char **ap, bool_t issetenv)
443 char *cp, *cp2, *varbuf, c;
444 size_t errs = 0;
445 NYD_ENTER;
447 for (; *ap != NULL; ++ap) {
448 /* Isolate key */
449 cp = *ap;
450 cp2 = varbuf = ac_alloc(strlen(cp) +1);
451 for (; (c = *cp) != '=' && c != '\0'; ++cp)
452 *cp2++ = c;
453 *cp2 = '\0';
454 if (c == '\0')
455 cp = UNCONST("");
456 else
457 ++cp;
458 if (varbuf == cp2) {
459 fprintf(stderr, tr(41, "Non-null variable name required\n"));
460 ++errs;
461 goto jnext;
464 if (!issetenv && varbuf[0] == 'n' && varbuf[1] == 'o')
465 errs += _var_vokclear(varbuf + 2);
466 else {
467 errs += _var_vokset(varbuf, (uintptr_t)cp);
468 if (issetenv) {
469 #ifdef HAVE_SETENV
470 errs += (setenv(varbuf, cp, 1) != 0);
471 #else
472 ++errs;
473 #endif
476 jnext:
477 ac_free(varbuf);
480 NYD_LEAVE;
481 return (errs != 0);
484 static bool_t
485 _is_closing_angle(char const *cp)
487 bool_t rv = FAL0;
488 NYD_ENTER;
490 while (spacechar(*cp))
491 ++cp;
492 if (*cp++ != '}')
493 goto jleave;
494 while (spacechar(*cp))
495 ++cp;
496 rv = (*cp == '\0');
497 jleave:
498 NYD_LEAVE;
499 return rv;
502 static struct macro *
503 _ma_look(char const *name, struct macro *data, enum ma_flags mafl)
505 enum ma_flags save_mafl;
506 ui32_t h;
507 struct macro *lmp, *mp;
508 NYD_ENTER;
510 save_mafl = mafl;
511 mafl &= MA_TYPE_MASK;
512 h = MA_NAME2HASH(name);
513 h = MA_HASH2PRIME(h);
515 for (lmp = NULL, mp = _macros[h]; mp != NULL; lmp = mp, mp = mp->ma_next) {
516 if ((mp->ma_flags & MA_TYPE_MASK) == mafl && !strcmp(mp->ma_name, name)) {
517 if (save_mafl & MA_UNDEF) {
518 if (lmp == NULL)
519 _macros[h] = mp->ma_next;
520 else
521 lmp->ma_next = mp->ma_next;
523 goto jleave;
527 if (data != NULL) {
528 data->ma_next = _macros[h];
529 _macros[h] = data;
530 mp = NULL;
532 jleave:
533 NYD_LEAVE;
534 return mp;
537 static int
538 _ma_exec(struct macro const *mp, struct var **unroller)
540 struct lostack los;
541 int rv = 0;
542 char *buf;
543 struct mline const *lp;
544 NYD_ENTER;
546 los.s_up = _localopts;
547 los.s_mac = UNCONST(mp);
548 los.s_localopts = NULL;
549 los.s_unroll = FAL0;
550 _localopts = &los;
552 buf = ac_alloc(mp->ma_maxlen +1);
553 for (lp = mp->ma_contents; lp; lp = lp->l_next) {
554 var_clear_allow_undefined = TRU1;
555 memcpy(buf, lp->l_line, lp->l_length +1);
556 temporary_localopts_store = _localopts; /* XXX intermediate hack */
557 /* FIXME if the execute() jumps away (via INT) then our internal state
558 * FIXME is messed up, even though temporary_localopts_free() is likely
559 * FIXME to correct some of that */
560 rv |= execute(buf, 0, lp->l_length); /* XXX break if != 0 ? */
561 temporary_localopts_store = NULL; /* XXX intermediate hack */
562 var_clear_allow_undefined = FAL0;
564 ac_free(buf);
566 _localopts = los.s_up;
567 if (unroller == NULL) {
568 if (los.s_localopts != NULL)
569 _localopts_unroll(&los.s_localopts);
570 } else
571 *unroller = los.s_localopts;
572 NYD_LEAVE;
573 return rv;
576 static int
577 _ma_list(enum ma_flags mafl)
579 FILE *fp;
580 char const *typestr;
581 struct macro *mq;
582 ui32_t ti, mc;
583 struct mline *lp;
584 NYD_ENTER;
586 if ((fp = Ftmp(NULL, "listmacs", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
587 NULL) {
588 perror("tmpfile");
589 mc = 1;
590 goto jleave;
593 mafl &= MA_TYPE_MASK;
594 typestr = (mafl & MA_ACC) ? "account" : "define";
596 for (ti = mc = 0; ti < MA_PRIME; ++ti)
597 for (mq = _macros[ti]; mq; mq = mq->ma_next)
598 if ((mq->ma_flags & MA_TYPE_MASK) == mafl) {
599 if (++mc > 1)
600 fputc('\n', fp);
601 fprintf(fp, "%s %s {\n", typestr, mq->ma_name);
602 for (lp = mq->ma_contents; lp != NULL; lp = lp->l_next)
603 fprintf(fp, " %s\n", lp->l_line);
604 fputs("}\n", fp);
606 if (mc)
607 page_or_print(fp, 0);
609 mc = (ui32_t)ferror(fp);
610 Fclose(fp);
611 jleave:
612 NYD_LEAVE;
613 return (int)mc;
616 static bool_t
617 _ma_define1(char const *name, enum ma_flags mafl)
619 bool_t rv = FAL0;
620 struct macro *mp;
621 struct mline *lp, *lst = NULL, *lnd = NULL;
622 char *linebuf = NULL, *cp;
623 size_t linesize = 0, maxlen = 0;
624 int n, i;
625 NYD_ENTER;
627 mp = scalloc(1, sizeof *mp);
628 mp->ma_name = sstrdup(name);
629 mp->ma_flags = mafl;
631 for (;;) {
632 n = readline_input("", TRU1, &linebuf, &linesize, NULL);
633 if (n == 0)
634 continue;
635 if (n < 0) {
636 fprintf(stderr, tr(75, "Unterminated %s definition: \"%s\".\n"),
637 (mafl & MA_ACC ? "account" : "macro"), mp->ma_name);
638 if (sourcing && !loading)
639 unstack();
640 goto jerr;
642 if (_is_closing_angle(linebuf))
643 break;
645 /* Trim WS */
646 for (cp = linebuf, i = 0; i < n; ++cp, ++i)
647 if (!whitechar(*cp))
648 break;
649 if (i == n)
650 continue;
651 n -= i;
652 while (whitechar(cp[n - 1]))
653 if (--n == 0)
654 break;
655 if (n == 0)
656 continue;
658 maxlen = MAX(maxlen, (size_t)n);
659 cp[n++] = '\0';
661 lp = scalloc(1, sizeof(*lp) - VFIELD_SIZEOF(struct mline, l_line) + n);
662 memcpy(lp->l_line, cp, n);
663 lp->l_length = (size_t)--n;
664 if (lst != NULL) {
665 lnd->l_next = lp;
666 lnd = lp;
667 } else
668 lst = lnd = lp;
670 mp->ma_contents = lst;
671 mp->ma_maxlen = maxlen;
673 if (_ma_look(mp->ma_name, mp, mafl) != NULL) {
674 fprintf(stderr, tr(76, "A %s named `%s' already exists.\n"),
675 (mafl & MA_ACC ? "account" : "macro"), mp->ma_name);
676 lst = mp->ma_contents;
677 goto jerr;
680 rv = TRU1;
681 jleave:
682 if (linebuf != NULL)
683 free(linebuf);
684 NYD_LEAVE;
685 return rv;
687 jerr:
688 if (lst != NULL)
689 _ma_freelines(lst);
690 free(mp->ma_name);
691 free(mp);
692 goto jleave;
695 static void
696 _ma_undef1(char const *name, enum ma_flags mafl)
698 struct macro *mp;
699 NYD_ENTER;
701 if ((mp = _ma_look(name, NULL, mafl | MA_UNDEF)) != NULL) {
702 _ma_freelines(mp->ma_contents);
703 free(mp->ma_name);
704 free(mp);
705 } else
706 fprintf(stderr, tr(571, "%s `%s' is not defined\n"),
707 (mafl & MA_ACC ? "Account" : "Macro"), name);
708 NYD_LEAVE;
711 static void
712 _ma_freelines(struct mline *lp)
714 struct mline *lq;
715 NYD_ENTER;
717 for (lq = NULL; lp != NULL; ) {
718 if (lq != NULL)
719 free(lq);
720 lq = lp;
721 lp = lp->l_next;
723 if (lq)
724 free(lq);
725 NYD_LEAVE;
728 static void
729 _localopts_add(struct lostack *losp, char const *name, struct var *ovap)
731 struct var *vap;
732 size_t nl, vl;
733 NYD_ENTER;
735 /* Propagate unrolling up the stack, as necessary */
736 while (!losp->s_unroll && (losp = losp->s_up) != NULL)
738 if (losp == NULL)
739 goto jleave;
741 /* We've found a level that wants to unroll; check wether it does it yet */
742 for (vap = losp->s_localopts; vap != NULL; vap = vap->v_link)
743 if (!strcmp(vap->v_name, name))
744 goto jleave;
746 nl = strlen(name) + 1;
747 vl = (ovap != NULL) ? strlen(ovap->v_value) + 1 : 0;
748 vap = smalloc(sizeof(*vap) - VFIELD_SIZEOF(struct var, v_name) + nl + vl);
749 vap->v_link = losp->s_localopts;
750 losp->s_localopts = vap;
751 memcpy(vap->v_name, name, nl);
752 if (vl == 0)
753 vap->v_value = NULL;
754 else {
755 vap->v_value = vap->v_name + nl;
756 memcpy(vap->v_value, ovap->v_value, vl);
758 jleave:
759 NYD_LEAVE;
762 static void
763 _localopts_unroll(struct var **vapp)
765 struct lostack *save_los;
766 struct var *x, *vap;
767 NYD_ENTER;
769 vap = *vapp;
770 *vapp = NULL;
772 save_los = _localopts;
773 _localopts = NULL;
774 while (vap != NULL) {
775 x = vap;
776 vap = vap->v_link;
777 vok_vset(x->v_name, x->v_value);
778 free(x);
780 _localopts = save_los;
781 NYD_LEAVE;
784 FL char *
785 _var_oklook(enum okeys okey)
787 struct var_carrier vc;
788 char *rv;
789 NYD_ENTER;
791 vc.vc_vmap = _var_map + okey;
792 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
793 vc.vc_hash = _var_map[okey].vm_hash;
794 vc.vc_okey = okey;
796 if (!_var_lookup(&vc)) {
797 if ((rv = getenv(vc.vc_name)) != NULL) {
798 _var_set(&vc, rv);
799 assert(vc.vc_var != NULL);
800 goto jvar;
802 } else
803 jvar:
804 rv = vc.vc_var->v_value;
805 NYD_LEAVE;
806 return rv;
809 FL bool_t
810 _var_okset(enum okeys okey, uintptr_t val)
812 struct var_carrier vc;
813 bool_t rv;
814 NYD_ENTER;
816 vc.vc_vmap = _var_map + okey;
817 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
818 vc.vc_hash = _var_map[okey].vm_hash;
819 vc.vc_okey = okey;
821 rv = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
822 NYD_LEAVE;
823 return rv;
826 FL bool_t
827 _var_okclear(enum okeys okey)
829 struct var_carrier vc;
830 bool_t rv;
831 NYD_ENTER;
833 vc.vc_vmap = _var_map + okey;
834 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
835 vc.vc_hash = _var_map[okey].vm_hash;
836 vc.vc_okey = okey;
838 rv = _var_clear(&vc);
839 NYD_LEAVE;
840 return rv;
843 FL char *
844 _var_voklook(char const *vokey)
846 struct var_carrier vc;
847 char *rv;
848 NYD_ENTER;
850 _var_revlookup(&vc, vokey);
852 if (!_var_lookup(&vc)) {
853 if ((rv = getenv(vc.vc_name)) != NULL) {
854 _var_set(&vc, rv);
855 assert(vc.vc_var != NULL);
856 goto jvar;
858 } else
859 jvar:
860 rv = vc.vc_var->v_value;
861 NYD_LEAVE;
862 return rv;
865 FL bool_t
866 _var_vokset(char const *vokey, uintptr_t val)
868 struct var_carrier vc;
869 bool_t err;
870 NYD_ENTER;
872 _var_revlookup(&vc, vokey);
874 err = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
875 NYD_LEAVE;
876 return err;
879 FL bool_t
880 _var_vokclear(char const *vokey)
882 struct var_carrier vc;
883 bool_t err;
884 NYD_ENTER;
886 _var_revlookup(&vc, vokey);
888 err = _var_clear(&vc);
889 NYD_LEAVE;
890 return err;
893 FL void
894 var_list_all(void)
896 FILE *fp;
897 char **vacp, **cap;
898 struct var *vp;
899 size_t no, i;
900 char const *fmt;
901 NYD_ENTER;
903 if ((fp = Ftmp(NULL, "listvars", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
904 NULL) {
905 perror("tmpfile");
906 goto jleave;
909 for (no = i = 0; i < MA_PRIME; ++i)
910 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
911 ++no;
912 vacp = salloc(no * sizeof(*vacp));
913 for (cap = vacp, i = 0; i < MA_PRIME; ++i)
914 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
915 *cap++ = vp->v_name;
917 if (no > 1)
918 qsort(vacp, no, sizeof *vacp, &_var_list_all_cmp);
920 i = (ok_blook(bsdcompat) || ok_blook(bsdset));
921 fmt = (i != 0) ? "%s\t%s\n" : "%s=\"%s\"\n";
923 for (cap = vacp; no != 0; ++cap, --no) {
924 char *cp = vok_vlook(*cap); /* XXX when lookup checks val/bin, change */
925 if (cp == NULL)
926 cp = UNCONST("");
927 if (i || *cp != '\0')
928 fprintf(fp, fmt, *cap, cp);
929 else
930 fprintf(fp, "%s\n", *cap);
933 page_or_print(fp, PTR2SIZE(cap - vacp));
934 Fclose(fp);
935 jleave:
936 NYD_LEAVE;
939 FL int
940 c_varshow(void *v)
942 struct var_carrier vc;
943 char **argv = v, *val;
944 bool_t isenv, isset;
945 NYD_ENTER;
947 if (*argv == NULL) {
948 v = NULL;
949 goto jleave;
952 for (; *argv != NULL; ++argv) {
953 memset(&vc, 0, sizeof vc);
954 _var_revlookup(&vc, *argv);
955 if (_var_lookup(&vc)) {
956 val = vc.vc_var->v_value;
957 isenv = FAL0;
958 } else
959 isenv = ((val = getenv(vc.vc_name)) != NULL);
960 if (val == NULL)
961 val = UNCONST("NULL");
962 isset = (vc.vc_var != NULL);
964 if (vc.vc_vmap != NULL) {
965 if (vc.vc_vmap->vm_binary)
966 printf("%s: binary option (%d): isset=%d/environ=%d\n",
967 vc.vc_name, vc.vc_okey, isset, isenv);
968 else
969 printf("%s: value option (%d): isset=%d/environ=%d value<%s>\n",
970 vc.vc_name, vc.vc_okey, isset, isenv, val);
971 } else
972 printf("%s: isset=%d/environ=%d value<%s>\n",
973 vc.vc_name, isset, isenv, val);
975 jleave:
976 NYD_LEAVE;
977 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
980 FL int
981 c_set(void *v)
983 char **ap = v;
984 int err;
985 NYD_ENTER;
987 if (*ap == NULL) {
988 var_list_all();
989 err = 0;
990 } else
991 err = _var_set_env(ap, FAL0);
992 NYD_LEAVE;
993 return err;
996 FL int
997 c_setenv(void *v)
999 char **ap = v;
1000 int err;
1001 NYD_ENTER;
1003 if (!(err = starting))
1004 err = _var_set_env(ap, TRU1);
1005 NYD_LEAVE;
1006 return err;
1009 FL int
1010 c_unset(void *v)
1012 char **ap = v;
1013 int err = 0;
1014 NYD_ENTER;
1016 while (*ap != NULL)
1017 err |= _var_vokclear(*ap++);
1018 NYD_LEAVE;
1019 return err;
1022 FL int
1023 c_unsetenv(void *v)
1025 int err;
1026 NYD_ENTER;
1028 if (!(err = starting)) {
1029 char **ap;
1030 bool_t tmp = var_clear_allow_undefined;
1031 var_clear_allow_undefined = TRU1;
1032 for (ap = v; *ap != NULL; ++ap) {
1033 bool_t bad = _var_vokclear(*ap);
1034 if (
1035 #ifdef HAVE_SETENV
1036 unsetenv(*ap) != 0 &&
1037 #endif
1040 err = 1;
1042 var_clear_allow_undefined = tmp;
1044 NYD_LEAVE;
1045 return err;
1048 FL int
1049 c_define(void *v)
1051 int rv = 1;
1052 char **args = v;
1053 NYD_ENTER;
1055 if (args[0] == NULL) {
1056 rv = _ma_list(MA_NONE);
1057 goto jleave;
1060 if (args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1061 args[2] != NULL) {
1062 fprintf(stderr, tr(505, "Syntax is: define <name> {"));
1063 goto jleave;
1066 rv = !_ma_define1(args[0], MA_NONE);
1067 jleave:
1068 NYD_LEAVE;
1069 return rv;
1072 FL int
1073 c_undefine(void *v)
1075 int rv = 1;
1076 char **args = v;
1077 NYD_ENTER;
1079 if (*args == NULL) {
1080 fprintf(stderr, tr(504, "`%s': required arguments are missing\n"),
1081 "undefine");
1082 goto jleave;
1085 _ma_undef1(*args, MA_NONE);
1086 while (*++args != NULL);
1087 rv = 0;
1088 jleave:
1089 NYD_LEAVE;
1090 return rv;
1093 FL int
1094 c_call(void *v)
1096 int rv = 1;
1097 char **args = v;
1098 char const *errs, *name;
1099 struct macro *mp;
1100 NYD_ENTER;
1102 if (args[0] == NULL || (args[1] != NULL && args[2] != NULL)) {
1103 errs = tr(507, "Syntax is: call <%s>\n");
1104 name = "name";
1105 goto jerr;
1108 if ((mp = _ma_look(*args, NULL, MA_NONE)) == NULL) {
1109 errs = tr(508, "Undefined macro called: `%s'\n");
1110 name = *args;
1111 goto jerr;
1114 rv = _ma_exec(mp, NULL);
1115 jleave:
1116 NYD_LEAVE;
1117 return rv;
1118 jerr:
1119 fprintf(stderr, errs, name);
1120 goto jleave;
1123 FL int
1124 callhook(char const *name, int nmail)
1126 int len, rv;
1127 struct macro *mp;
1128 char *var, *cp;
1129 NYD_ENTER;
1131 var = ac_alloc(len = strlen(name) + 12 +1);
1132 snprintf(var, len, "folder-hook-%s", name);
1133 if ((cp = vok_vlook(var)) == NULL && (cp = ok_vlook(folder_hook)) == NULL) {
1134 rv = 0;
1135 goto jleave;
1137 if ((mp = _ma_look(cp, NULL, MA_NONE)) == NULL) {
1138 fprintf(stderr, tr(49, "Cannot call hook for folder \"%s\": "
1139 "Macro \"%s\" does not exist.\n"), name, cp);
1140 rv = 1;
1141 goto jleave;
1144 inhook = nmail ? 3 : 1; /* XXX enum state machine */
1145 rv = _ma_exec(mp, NULL);
1146 inhook = 0;
1147 jleave:
1148 ac_free(var);
1149 NYD_LEAVE;
1150 return rv;
1153 FL int
1154 c_account(void *v)
1156 char **args = v;
1157 struct macro *mp;
1158 int rv = 1, i, oqf, nqf;
1159 NYD_ENTER;
1161 if (args[0] == NULL) {
1162 rv = _ma_list(MA_ACC);
1163 goto jleave;
1166 if (args[1] && args[1][0] == '{' && args[1][1] == '\0') {
1167 if (args[2] != NULL) {
1168 fprintf(stderr, tr(517, "Syntax is: account <name> {\n"));
1169 goto jleave;
1171 if (!asccasecmp(args[0], ACCOUNT_NULL)) {
1172 fprintf(stderr, tr(521, "Error: `%s' is a reserved name.\n"),
1173 ACCOUNT_NULL);
1174 goto jleave;
1176 rv = !_ma_define1(args[0], MA_ACC);
1177 goto jleave;
1180 if (inhook) {
1181 fprintf(stderr, tr(518, "Cannot change account from within a hook.\n"));
1182 goto jleave;
1185 save_mbox_for_possible_quitstuff();
1187 mp = NULL;
1188 if (asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1189 (mp = _ma_look(args[0], NULL, MA_ACC)) == NULL) {
1190 fprintf(stderr, tr(519, "Account `%s' does not exist.\n"), args[0]);
1191 goto jleave;
1194 oqf = savequitflags();
1195 if (_acc_curr != NULL && _acc_curr->ma_localopts != NULL)
1196 _localopts_unroll(&_acc_curr->ma_localopts);
1197 account_name = (mp != NULL) ? mp->ma_name : NULL;
1198 _acc_curr = mp;
1200 if (mp != NULL && _ma_exec(mp, &mp->ma_localopts) == CBAD) {
1201 /* XXX account switch incomplete, unroll? */
1202 fprintf(stderr, tr(520, "Switching to account `%s' failed.\n"), args[0]);
1203 goto jleave;
1206 if (!starting && !inhook) {
1207 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1208 restorequitflags(oqf);
1209 if ((i = setfile("%", 0)) < 0)
1210 goto jleave;
1211 callhook(mailname, 0);
1212 if (i > 0 && !ok_blook(emptystart))
1213 goto jleave;
1214 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1215 restorequitflags(nqf);
1217 rv = 0;
1218 jleave:
1219 NYD_LEAVE;
1220 return rv;
1223 FL int
1224 c_unaccount(void *v)
1226 int rv = 1;
1227 char **args = v;
1228 NYD_ENTER;
1230 if (*args == NULL) {
1231 fprintf(stderr, tr(504, "`%s': required arguments are missing\n"),
1232 "unaccount");
1233 goto jleave;
1236 rv = 0;
1237 do {
1238 if (account_name != NULL && !strcmp(account_name, *args)) {
1239 fprintf(stderr, tr(506,
1240 "Rejecting deletion of currently active account `%s'\n"), *args);
1241 continue;
1243 _ma_undef1(*args, MA_ACC);
1244 } while (*++args != NULL);
1245 jleave:
1246 NYD_LEAVE;
1247 return rv;
1250 FL int
1251 c_localopts(void *v)
1253 int rv = 1;
1254 char **c = v;
1255 NYD_ENTER;
1257 if (_localopts == NULL) {
1258 fprintf(stderr, tr(522,
1259 "Cannot use `localopts' but from within a `define' or `account'\n"));
1260 goto jleave;
1263 _localopts->s_unroll = (**c == '0') ? FAL0 : TRU1;
1264 rv = 0;
1265 jleave:
1266 NYD_LEAVE;
1267 return rv;
1270 FL void
1271 temporary_localopts_free(void) /* XXX intermediate hack */
1273 struct lostack *losp;
1274 NYD_ENTER;
1276 var_clear_allow_undefined = FAL0;
1277 losp = temporary_localopts_store;
1278 temporary_localopts_store = NULL;
1279 _localopts_unroll(&losp->s_localopts);
1280 NYD_LEAVE;
1283 /* vim:set fenc=utf-8:s-it-mode */