INSTALL: update for v14.6
[s-mailx.git] / acmava.c
blob22549f54ef1b4b4fdd9bb72797f78b74e1be5087
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 macro *_macros[MA_PRIME]; /* TODO dynamically spaced */
117 static struct var *_vars[MA_PRIME]; /* TODO dynamically spaced */
119 /* Special cased value string allocation */
120 static char * _vcopy(char const *str);
121 static void _vfree(char *cp);
123 /* Check for special housekeeping. */
124 static bool_t _check_special_vars(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.
131 * Following the standard, only the part following the last '@' should
132 * be lower-cased, but practice has established otherwise here */
133 static char const * _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 on "error" */
137 static bool_t _var_revlookup(struct var_carrier *vcp, char const *name);
139 /* Lookup a variable from vcp.vc_(vmap|name|hash).
140 * Sets vcp.vc_prime; vcp.vc_var is NULL on "error" */
141 static bool_t _var_lookup(struct var_carrier *vcp);
143 /* Set variable from vcp.vc_(vmap|name|hash); sets vcp.vc_var if NULL */
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 static bool_t _var_clear(struct var_carrier *vcp);
149 /* Line *cp* consists solely of WS and a } */
150 static bool_t _is_closing_angle(char const *cp);
152 /* Lookup for macros/accounts */
153 static struct macro *_malook(char const *name, struct macro *data,
154 enum ma_flags mafl);
156 /* Walk all lines of a macro and execute() them */
157 static int _maexec(struct macro const *mp, struct var **unroller);
159 /* User display helpers */
160 static int _list_macros(enum ma_flags mafl);
162 /* */
163 static bool_t _define1(char const *name, enum ma_flags mafl);
164 static void _undef1(char const *name, enum ma_flags mafl);
165 static void _freelines(struct mline *lp);
167 /* var_list_all(): qsort(3) helper */
168 static int _var_list_all_cmp(void const *s1, void const *s2);
170 /* Update replay-log */
171 static void _localopts_add(struct lostack *losp, char const *name,
172 struct var *ovap);
173 static void _localopts_unroll(struct var **vapp);
175 static char *
176 _vcopy(char const *str)
178 char *news;
179 size_t len;
180 NYD_ENTER;
182 if (*str == '\0')
183 news = UNCONST("");
184 else {
185 len = strlen(str) + 1;
186 news = smalloc(len);
187 memcpy(news, str, len);
189 NYD_LEAVE;
190 return news;
193 static void
194 _vfree(char *cp)
196 NYD_ENTER;
197 if (*cp != '\0')
198 free(cp);
199 NYD_LEAVE;
202 static bool_t
203 _check_special_vars(enum okeys okey, bool_t enable, char **val)
205 char *cp = NULL;
206 bool_t rv = TRU1;
207 int flag = 0;
208 NYD_ENTER;
210 switch (okey) {
211 case ok_b_debug:
212 flag = OPT_DEBUG;
213 break;
214 case ok_b_header:
215 flag = OPT_N_FLAG;
216 enable = !enable;
217 break;
218 case ok_b_skipemptybody:
219 flag = OPT_E_FLAG;
220 break;
221 case ok_b_verbose:
222 flag = OPT_VERBOSE;
223 break;
224 case ok_v_folder:
225 rv = (val == NULL || var_folder_updated(*val, &cp));
226 if (rv && cp != NULL) {
227 _vfree(*val);
228 /* It's smalloc()ed, but ensure we don't leak */
229 if (*cp == '\0') {
230 *val = UNCONST("");
231 free(cp);
232 } else
233 *val = cp;
235 break;
236 #ifdef HAVE_NCL
237 case ok_v_line_editor_cursor_right:
238 if ((rv = (val != NULL && *val != NULL))) {
239 /* Set with no value? TODO very guly */
240 if (*(cp = *val) != '\0') {
241 char const *x = cp;
242 int c;
243 do {
244 c = expand_shell_escape(&x, FAL0);
245 if (c < 0)
246 break;
247 *cp++ = (char)c;
248 } while (*x != '\0');
249 *cp++ = '\0';
252 break;
253 #endif
254 default:
255 break;
258 if (flag) {
259 if (enable)
260 options |= flag;
261 else
262 options &= ~flag;
264 NYD_LEAVE;
265 return rv;
268 static char const *
269 _canonify(char const *vn)
271 NYD_ENTER;
272 if (!upperchar(*vn)) {
273 char const *vp;
275 for (vp = vn; *vp != '\0' && *vp != '@'; ++vp)
277 vn = (*vp == '@') ? i_strdup(vn) : vn;
279 NYD_LEAVE;
280 return vn;
283 static bool_t
284 _var_revlookup(struct var_carrier *vcp, char const *name)
286 ui32_t hash, i, j;
287 struct var_map const *vmp;
288 NYD_ENTER;
290 vcp->vc_name = name = _canonify(name);
291 vcp->vc_hash = hash = MA_NAME2HASH(name);
293 for (i = hash % _VAR_REV_PRIME, j = 0; j <= _VAR_REV_LONGEST; ++j) {
294 ui32_t x = _var_revmap[i];
295 if (x == _VAR_REV_ILL)
296 break;
297 vmp = _var_map + x;
298 if (vmp->vm_hash == hash &&
299 !strcmp(_var_keydat + vmp->vm_keyoff, name)) {
300 vcp->vc_vmap = vmp;
301 vcp->vc_okey = (enum okeys)x;
302 goto jleave;
304 if (++i == _VAR_REV_PRIME) {
305 #ifdef _VAR_REV_WRAPAROUND
306 i = 0;
307 #else
308 break;
309 #endif
312 vcp->vc_vmap = NULL;
313 vcp = NULL;
314 jleave:
315 NYD_LEAVE;
316 return (vcp != NULL);
319 static bool_t
320 _var_lookup(struct var_carrier *vcp)
322 struct var **vap, *lvp, *vp;
323 NYD_ENTER;
325 vap = _vars + (vcp->vc_prime = MA_HASH2PRIME(vcp->vc_hash));
327 for (lvp = NULL, vp = *vap; vp != NULL; lvp = vp, vp = vp->v_link)
328 if (!strcmp(vp->v_name, vcp->vc_name)) {
329 /* Relink as head, hope it "sorts on usage" over time */
330 if (lvp != NULL) {
331 lvp->v_link = vp->v_link;
332 vp->v_link = *vap;
333 *vap = vp;
335 goto jleave;
337 vp = NULL;
338 jleave:
339 vcp->vc_var = vp;
340 NYD_LEAVE;
341 return (vp != NULL);
344 static bool_t
345 _var_set(struct var_carrier *vcp, char const *value)
347 bool_t err;
348 struct var *vp;
349 char *oval;
350 NYD_ENTER;
352 if (value == NULL) {
353 bool_t tmp = var_clear_allow_undefined;
354 var_clear_allow_undefined = TRU1;
355 err = _var_clear(vcp);
356 var_clear_allow_undefined = tmp;
357 goto jleave;
360 _var_lookup(vcp);
362 /* Don't care what happens later on, store this in the unroll list */
363 if (_localopts != NULL)
364 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
366 if ((vp = vcp->vc_var) == NULL) {
367 size_t l = strlen(vcp->vc_name) + 1;
369 vcp->vc_var =
370 vp = smalloc(sizeof(*vp) - VFIELD_SIZEOF(struct var, v_name) + l);
371 vp->v_link = _vars[vcp->vc_prime];
372 _vars[vcp->vc_prime] = vp;
373 memcpy(vp->v_name, vcp->vc_name, l);
374 oval = UNCONST("");
375 } else
376 oval = vp->v_value;
377 vp->v_value = _vcopy(value);
379 /* Check if update allowed XXX wasteful on error! */
380 if (vcp->vc_vmap != NULL && vcp->vc_vmap->vm_special &&
381 (err = !_check_special_vars(vcp->vc_okey, TRU1, &vp->v_value))) {
382 char *cp = vp->v_value;
383 vp->v_value = oval;
384 oval = cp;
386 if (*oval != '\0')
387 _vfree(oval);
388 err = FAL0;
389 jleave:
390 NYD_LEAVE;
391 return err;
394 static bool_t
395 _var_clear(struct var_carrier *vcp)
397 bool_t err = TRU1;
398 NYD_ENTER;
400 if (!_var_lookup(vcp)) {
401 if (!sourcing && !var_clear_allow_undefined) {
402 fprintf(stderr, tr(203, "\"%s\": undefined variable\n"),
403 vcp->vc_name);
404 goto jleave;
406 } else {
407 if (_localopts != NULL)
408 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
410 /* Always listhead after _var_lookup() */
411 _vars[vcp->vc_prime] = _vars[vcp->vc_prime]->v_link;
412 _vfree(vcp->vc_var->v_value);
413 free(vcp->vc_var);
414 vcp->vc_var = NULL;
416 if (vcp->vc_vmap != NULL && vcp->vc_vmap->vm_special &&
417 !_check_special_vars(vcp->vc_okey, FAL0, NULL))
418 goto jleave;
420 err = FAL0;
421 jleave:
422 NYD_LEAVE;
423 return err;
426 static bool_t
427 _is_closing_angle(char const *cp)
429 bool_t rv = FAL0;
430 NYD_ENTER;
432 while (spacechar(*cp))
433 ++cp;
434 if (*cp++ != '}')
435 goto jleave;
436 while (spacechar(*cp))
437 ++cp;
438 rv = (*cp == '\0');
439 jleave:
440 NYD_LEAVE;
441 return rv;
444 static struct macro *
445 _malook(char const *name, struct macro *data, enum ma_flags mafl)
447 enum ma_flags save_mafl;
448 ui32_t h;
449 struct macro *lmp, *mp;
450 NYD_ENTER;
452 save_mafl = mafl;
453 mafl &= MA_TYPE_MASK;
454 h = MA_NAME2HASH(name);
455 h = MA_HASH2PRIME(h);
457 for (lmp = NULL, mp = _macros[h]; mp != NULL; lmp = mp, mp = mp->ma_next) {
458 if ((mp->ma_flags & MA_TYPE_MASK) == mafl &&
459 !strcmp(mp->ma_name, name)) {
460 if (save_mafl & MA_UNDEF) {
461 if (lmp == NULL)
462 _macros[h] = mp->ma_next;
463 else
464 lmp->ma_next = mp->ma_next;
466 goto jleave;
470 if (data != NULL) {
471 data->ma_next = _macros[h];
472 _macros[h] = data;
473 mp = NULL;
475 jleave:
476 NYD_LEAVE;
477 return mp;
480 static int
481 _maexec(struct macro const *mp, struct var **unroller)
483 struct lostack los;
484 int rv = 0;
485 char *buf;
486 struct mline const *lp;
487 NYD_ENTER;
489 los.s_up = _localopts;
490 los.s_mac = UNCONST(mp);
491 los.s_localopts = NULL;
492 los.s_unroll = FAL0;
493 _localopts = &los;
495 buf = ac_alloc(mp->ma_maxlen + 1);
496 for (lp = mp->ma_contents; lp; lp = lp->l_next) {
497 var_clear_allow_undefined = TRU1;
498 memcpy(buf, lp->l_line, lp->l_length + 1);
499 rv |= execute(buf, 0, lp->l_length); /* XXX break if != 0 ? */
500 var_clear_allow_undefined = FAL0;
502 ac_free(buf);
504 _localopts = los.s_up;
505 if (unroller == NULL)
506 _localopts_unroll(&los.s_localopts);
507 else
508 *unroller = los.s_localopts;
509 NYD_LEAVE;
510 return rv;
513 static int
514 _list_macros(enum ma_flags mafl)
516 FILE *fp;
517 char const *typestr;
518 struct macro *mq;
519 ui32_t ti, mc;
520 struct mline *lp;
521 NYD_ENTER;
523 if ((fp = Ftmp(NULL, "listmacs", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
524 NULL) {
525 perror("tmpfile");
526 mc = 1;
527 goto jleave;
530 mafl &= MA_TYPE_MASK;
531 typestr = (mafl & MA_ACC) ? "account" : "define";
533 for (ti = mc = 0; ti < MA_PRIME; ++ti)
534 for (mq = _macros[ti]; mq; mq = mq->ma_next)
535 if ((mq->ma_flags & MA_TYPE_MASK) == mafl) {
536 if (++mc > 1)
537 fputc('\n', fp);
538 fprintf(fp, "%s %s {\n", typestr, mq->ma_name);
539 for (lp = mq->ma_contents; lp; lp = lp->l_next)
540 fprintf(fp, " %s\n", lp->l_line);
541 fputs("}\n", fp);
543 if (mc)
544 page_or_print(fp, 0);
546 mc = (ui32_t)ferror(fp);
547 Fclose(fp);
548 jleave:
549 NYD_LEAVE;
550 return (int)mc;
553 static bool_t
554 _define1(char const *name, enum ma_flags mafl)
556 bool_t rv = FAL0;
557 struct macro *mp;
558 struct mline *lp, *lst = NULL, *lnd = NULL;
559 char *linebuf = NULL, *cp;
560 size_t linesize = 0, maxlen = 0;
561 int n, i;
562 NYD_ENTER;
564 mp = scalloc(1, sizeof *mp);
565 mp->ma_name = sstrdup(name);
566 mp->ma_flags = mafl;
568 for (;;) {
569 n = readline_input("", TRU1, &linebuf, &linesize, NULL);
570 if (n <= 0) {
571 fprintf(stderr, tr(75, "Unterminated %s definition: \"%s\".\n"),
572 (mafl & MA_ACC ? "account" : "macro"), mp->ma_name);
573 if (sourcing)
574 unstack();
575 goto jerr;
577 if (_is_closing_angle(linebuf))
578 break;
580 /* Trim WS */
581 for (cp = linebuf, i = 0; i < n; ++cp, ++i)
582 if (!whitechar(*cp))
583 break;
584 if (i == n)
585 continue;
586 n -= i;
587 while (whitechar(cp[n - 1]))
588 if (--n == 0)
589 break;
590 if (n == 0)
591 continue;
593 maxlen = MAX(maxlen, (size_t)n);
594 cp[n++] = '\0';
596 lp = scalloc(1, sizeof(*lp) - VFIELD_SIZEOF(struct mline, l_line) + n);
597 memcpy(lp->l_line, cp, n);
598 lp->l_length = (size_t)--n;
599 if (lst != NULL) {
600 lnd->l_next = lp;
601 lnd = lp;
602 } else
603 lst = lnd = lp;
605 mp->ma_contents = lst;
606 mp->ma_maxlen = maxlen;
608 if (_malook(mp->ma_name, mp, mafl) != NULL) {
609 if (!(mafl & MA_ACC)) {
610 fprintf(stderr, tr(76, "A macro named \"%s\" already exists.\n"),
611 mp->ma_name);
612 lst = mp->ma_contents;
613 goto jerr;
615 _undef1(mp->ma_name, MA_ACC);
616 _malook(mp->ma_name, mp, MA_ACC);
619 rv = TRU1;
620 jleave:
621 if (linebuf != NULL)
622 free(linebuf);
623 NYD_LEAVE;
624 return rv;
626 jerr:
627 if (lst != NULL)
628 _freelines(lst);
629 free(mp->ma_name);
630 free(mp);
631 goto jleave;
634 static void
635 _undef1(char const *name, enum ma_flags mafl)
637 struct macro *mp;
638 NYD_ENTER;
640 if ((mp = _malook(name, NULL, mafl | MA_UNDEF)) != NULL) {
641 _freelines(mp->ma_contents);
642 free(mp->ma_name);
643 free(mp);
645 NYD_LEAVE;
648 static void
649 _freelines(struct mline *lp)
651 struct mline *lq;
652 NYD_ENTER;
654 for (lq = NULL; lp != NULL; ) {
655 if (lq != NULL)
656 free(lq);
657 lq = lp;
658 lp = lp->l_next;
660 if (lq)
661 free(lq);
662 NYD_LEAVE;
665 static int
666 _var_list_all_cmp(void const *s1, void const *s2)
668 int rv;
669 NYD_ENTER;
671 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
672 NYD_LEAVE;
673 return rv;
676 static void
677 _localopts_add(struct lostack *losp, char const *name, struct var *ovap)
679 struct var *vap;
680 size_t nl, vl;
681 NYD_ENTER;
683 /* Propagate unrolling up the stack, as necessary */
684 while (!losp->s_unroll && (losp = losp->s_up) != NULL)
686 if (losp == NULL)
687 goto jleave;
689 /* We've found a level that wants to unroll; check wether it does it yet */
690 for (vap = losp->s_localopts; vap != NULL; vap = vap->v_link)
691 if (!strcmp(vap->v_name, name))
692 goto jleave;
694 nl = strlen(name) + 1;
695 vl = (ovap != NULL) ? strlen(ovap->v_value) + 1 : 0;
696 vap = smalloc(sizeof(*vap) - VFIELD_SIZEOF(struct var, v_name) + nl + vl);
697 vap->v_link = losp->s_localopts;
698 losp->s_localopts = vap;
699 memcpy(vap->v_name, name, nl);
700 if (vl == 0)
701 vap->v_value = NULL;
702 else {
703 vap->v_value = vap->v_name + nl;
704 memcpy(vap->v_value, ovap->v_value, vl);
706 jleave:
707 NYD_LEAVE;
710 static void
711 _localopts_unroll(struct var **vapp)
713 struct lostack *save_los;
714 struct var *x, *vap;
715 NYD_ENTER;
717 vap = *vapp;
718 *vapp = NULL;
720 save_los = _localopts;
721 _localopts = NULL;
722 while (vap != NULL) {
723 x = vap;
724 vap = vap->v_link;
725 vok_vset(x->v_name, x->v_value);
726 free(x);
728 _localopts = save_los;
729 NYD_LEAVE;
732 FL char *
733 _var_oklook(enum okeys okey)
735 struct var_carrier vc;
736 char *rv;
737 NYD_ENTER;
739 vc.vc_vmap = _var_map + okey;
740 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
741 vc.vc_hash = _var_map[okey].vm_hash;
742 vc.vc_okey = okey;
744 if (!_var_lookup(&vc)) {
745 if ((rv = getenv(vc.vc_name)) != NULL) {
746 _var_set(&vc, rv);
747 assert(vc.vc_var != NULL);
748 goto jvar;
750 } else
751 jvar:
752 rv = vc.vc_var->v_value;
753 NYD_LEAVE;
754 return rv;
757 FL bool_t
758 _var_okset(enum okeys okey, uintptr_t val)
760 struct var_carrier vc;
761 bool_t rv;
762 NYD_ENTER;
764 vc.vc_vmap = _var_map + okey;
765 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
766 vc.vc_hash = _var_map[okey].vm_hash;
767 vc.vc_okey = okey;
769 rv = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
770 NYD_LEAVE;
771 return rv;
774 FL bool_t
775 _var_okclear(enum okeys okey)
777 struct var_carrier vc;
778 bool_t rv;
779 NYD_ENTER;
781 vc.vc_vmap = _var_map + okey;
782 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
783 vc.vc_hash = _var_map[okey].vm_hash;
784 vc.vc_okey = okey;
786 rv = _var_clear(&vc);
787 NYD_LEAVE;
788 return rv;
791 FL char *
792 _var_voklook(char const *vokey)
794 struct var_carrier vc;
795 char *rv;
796 NYD_ENTER;
798 _var_revlookup(&vc, vokey);
800 if (!_var_lookup(&vc)) {
801 if ((rv = getenv(vc.vc_name)) != NULL) {
802 _var_set(&vc, rv);
803 assert(vc.vc_var != NULL);
804 goto jvar;
806 } else
807 jvar:
808 rv = vc.vc_var->v_value;
809 NYD_LEAVE;
810 return rv;
813 FL bool_t
814 _var_vokset(char const *vokey, uintptr_t val)
816 struct var_carrier vc;
817 bool_t rv;
818 NYD_ENTER;
820 _var_revlookup(&vc, vokey);
822 rv = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
823 NYD_LEAVE;
824 return rv;
827 FL bool_t
828 _var_vokclear(char const *vokey)
830 struct var_carrier vc;
831 bool_t rv;
832 NYD_ENTER;
834 _var_revlookup(&vc, vokey);
836 rv = _var_clear(&vc);
837 NYD_LEAVE;
838 return rv;
841 FL void
842 var_list_all(void)
844 FILE *fp;
845 char **vacp, **cap;
846 struct var *vp;
847 size_t no, i;
848 char const *fmt;
849 NYD_ENTER;
851 if ((fp = Ftmp(NULL, "listvars", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
852 NULL) {
853 perror("tmpfile");
854 goto jleave;
857 for (no = i = 0; i < MA_PRIME; ++i)
858 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
859 ++no;
860 vacp = salloc(no * sizeof(*vacp));
861 for (cap = vacp, i = 0; i < MA_PRIME; ++i)
862 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
863 *cap++ = vp->v_name;
865 if (no > 1)
866 qsort(vacp, no, sizeof *vacp, &_var_list_all_cmp);
868 i = (ok_blook(bsdcompat) || ok_blook(bsdset));
869 fmt = (i != 0) ? "%s\t%s\n" : "%s=\"%s\"\n";
871 for (cap = vacp; no != 0; ++cap, --no) {
872 char *cp = vok_vlook(*cap); /* XXX when lookup checks val/bin, change */
873 if (cp == NULL)
874 cp = UNCONST("");
875 if (i || *cp != '\0')
876 fprintf(fp, fmt, *cap, cp);
877 else
878 fprintf(fp, "%s\n", *cap);
881 page_or_print(fp, PTR2SIZE(cap - vacp));
882 Fclose(fp);
883 jleave:
884 NYD_LEAVE;
887 FL int
888 c_var_inspect(void *v)
890 struct var_carrier vc;
891 char **argv = v, *val;
892 bool_t isenv, isset;
893 NYD_ENTER;
895 if (*argv == NULL) {
896 v = NULL;
897 goto jleave;
900 for (; *argv != NULL; ++argv) {
901 memset(&vc, 0, sizeof vc);
902 _var_revlookup(&vc, *argv);
903 if (_var_lookup(&vc)) {
904 val = vc.vc_var->v_value;
905 isenv = FAL0;
906 } else
907 isenv = ((val = getenv(vc.vc_name)) != NULL);
908 if (val == NULL)
909 val = UNCONST("NULL");
910 isset = (vc.vc_var != NULL);
912 if (vc.vc_vmap != NULL) {
913 if (vc.vc_vmap->vm_binary)
914 printf("%s: binary option (%d): isset=%d/environ=%d\n",
915 vc.vc_name, vc.vc_okey, isset, isenv);
916 else
917 printf("%s: value option (%d): isset=%d/environ=%d value<%s>\n",
918 vc.vc_name, vc.vc_okey, isset, isenv, val);
919 } else
920 printf("%s: isset=%d/environ=%d value<%s>\n",
921 vc.vc_name, isset, isenv, val);
923 jleave:
924 NYD_LEAVE;
925 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
928 FL int
929 c_define(void *v)
931 int rv = 1;
932 char **args = v;
933 char const *errs;
934 NYD_ENTER;
936 if (args[0] == NULL) {
937 errs = tr(504, "Missing macro name to `define'");
938 goto jerr;
940 if (args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
941 args[2] != NULL) {
942 errs = tr(505, "Syntax is: define <name> {");
943 goto jerr;
946 rv = !_define1(args[0], MA_NONE);
947 jleave:
948 NYD_LEAVE;
949 return rv;
950 jerr:
951 fprintf(stderr, "%s\n", errs);
952 goto jleave;
955 FL int
956 c_undef(void *v)
958 int rv = 1;
959 char **args = v;
960 NYD_ENTER;
962 if (*args == NULL) {
963 fprintf(stderr, tr(506, "Missing macro name to `undef'\n"));
964 goto jleave;
967 _undef1(*args, MA_NONE);
968 while (*++args != NULL);
969 rv = 0;
970 jleave:
971 NYD_LEAVE;
972 return rv;
975 FL int
976 c_call(void *v)
978 int rv = 1;
979 char **args = v;
980 char const *errs, *name;
981 struct macro *mp;
982 NYD_ENTER;
984 if (args[0] == NULL || (args[1] != NULL && args[2] != NULL)) {
985 errs = tr(507, "Syntax is: call <%s>\n");
986 name = "name";
987 goto jerr;
990 if ((mp = _malook(*args, NULL, MA_NONE)) == NULL) {
991 errs = tr(508, "Undefined macro called: \"%s\"\n");
992 name = *args;
993 goto jerr;
996 rv = _maexec(mp, NULL);
997 jleave:
998 NYD_LEAVE;
999 return rv;
1000 jerr:
1001 fprintf(stderr, errs, name);
1002 goto jleave;
1005 FL int
1006 callhook(char const *name, int nmail)
1008 int len, rv;
1009 struct macro *mp;
1010 char *var, *cp;
1011 NYD_ENTER;
1013 var = ac_alloc(len = strlen(name) + 13);
1014 snprintf(var, len, "folder-hook-%s", name);
1015 if ((cp = vok_vlook(var)) == NULL && (cp = ok_vlook(folder_hook)) == NULL) {
1016 rv = 0;
1017 goto jleave;
1019 if ((mp = _malook(cp, NULL, MA_NONE)) == NULL) {
1020 fprintf(stderr, tr(49, "Cannot call hook for folder \"%s\": "
1021 "Macro \"%s\" does not exist.\n"), name, cp);
1022 rv = 1;
1023 goto jleave;
1026 inhook = nmail ? 3 : 1; /* XXX enum state machine */
1027 rv = _maexec(mp, NULL);
1028 inhook = 0;
1029 jleave:
1030 ac_free(var);
1031 NYD_LEAVE;
1032 return rv;
1035 FL int
1036 c_defines(void *v)
1038 int rv;
1039 NYD_ENTER;
1040 UNUSED(v);
1042 rv = _list_macros(MA_NONE);
1043 NYD_LEAVE;
1044 return rv;
1047 FL int
1048 c_account(void *v)
1050 char **args = v;
1051 struct macro *mp;
1052 int rv = 1, i, oqf, nqf;
1053 NYD_ENTER;
1055 if (args[0] == NULL) {
1056 rv = _list_macros(MA_ACC);
1057 goto jleave;
1060 if (args[1] && args[1][0] == '{' && args[1][1] == '\0') {
1061 if (args[2] != NULL) {
1062 fprintf(stderr, tr(517, "Syntax is: account <name> {\n"));
1063 goto jleave;
1065 if (!asccasecmp(args[0], ACCOUNT_NULL)) {
1066 fprintf(stderr, tr(521, "Error: `%s' is a reserved name.\n"),
1067 ACCOUNT_NULL);
1068 goto jleave;
1070 rv = !_define1(args[0], MA_ACC);
1071 goto jleave;
1074 if (inhook) {
1075 fprintf(stderr, tr(518, "Cannot change account from within a hook.\n"));
1076 goto jleave;
1079 save_mbox_for_possible_quitstuff();
1081 mp = NULL;
1082 if (asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1083 (mp = _malook(args[0], NULL, MA_ACC)) == NULL) {
1084 fprintf(stderr, tr(519, "Account `%s' does not exist.\n"), args[0]);
1085 goto jleave;
1088 oqf = savequitflags();
1089 if (_acc_curr != NULL)
1090 _localopts_unroll(&_acc_curr->ma_localopts);
1091 account_name = (mp != NULL) ? mp->ma_name : NULL;
1092 _acc_curr = mp;
1094 if (mp != NULL && _maexec(mp, &mp->ma_localopts) == CBAD) {
1095 /* XXX account switch incomplete, unroll? */
1096 fprintf(stderr, tr(520, "Switching to account `%s' failed.\n"), args[0]);
1097 goto jleave;
1100 if (!starting && !inhook) {
1101 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1102 restorequitflags(oqf);
1103 if ((i = setfile("%", 0)) < 0)
1104 goto jleave;
1105 callhook(mailname, 0);
1106 if (i > 0 && !ok_blook(emptystart))
1107 goto jleave;
1108 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1109 restorequitflags(nqf);
1111 rv = 0;
1112 jleave:
1113 NYD_LEAVE;
1114 return rv;
1117 FL int
1118 c_localopts(void *v)
1120 int rv = 1;
1121 char **c = v;
1122 NYD_ENTER;
1124 if (_localopts == NULL) {
1125 fprintf(stderr, tr(522,
1126 "Cannot use `localopts' but from within a `define' or `account'\n"));
1127 goto jleave;
1130 _localopts->s_unroll = (**c == '0') ? FAL0 : TRU1;
1131 rv = 0;
1132 jleave:
1133 NYD_LEAVE;
1134 return rv;
1137 /* vim:set fenc=utf-8:s-it-mode */