FixFix IMAP "+" expansion and more (Predrag Punosevac)
[s-mailx.git] / accmacvar.c
blob35350a8736237564129b86f1553ea3c268e45438
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 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE accmacvar
38 #ifndef HAVE_AMALGAMATION
39 # define _ACCMACVAR_SOURCE /* For _features[] */
40 # include "nail.h"
41 #endif
44 * HOWTO add a new non-dynamic binary or value option:
45 * - add an entry to nail.h:enum okeys
46 * - run create-okey-map.pl
47 * - update nail.1
50 /* Note: changing the hash function must be reflected in create-okey-map.pl */
51 #define MA_PRIME HSHSIZE
52 #define MA_NAME2HASH(N) torek_hash(N)
53 #define MA_HASH2PRIME(H) ((H) % MA_PRIME)
55 enum ma_flags {
56 MA_NONE = 0,
57 MA_ACC = 1<<0,
58 MA_TYPE_MASK = MA_ACC,
59 MA_UNDEF = 1<<1, /* Unlink after lookup */
60 MA_DELETED = 1<<13 /* Only for _acc_curr: deleted while active */
63 enum var_map_flags {
64 VM_NONE = 0,
65 VM_BINARY = 1<<0, /* ok_b_* */
66 VM_RDONLY = 1<<1, /* May not be set by user */
67 VM_SPECIAL = 1<<2, /* Wants _var_check_specials() evaluation */
68 VM_VIRTUAL = 1<<3 /* "Stateless": no var* -- implies VM_RDONLY */
71 struct macro {
72 struct macro *ma_next;
73 char *ma_name;
74 struct mline *ma_contents;
75 ui32_t ma_maxlen; /* Maximum line length */
76 enum ma_flags ma_flags;
77 struct var *ma_localopts; /* `account' unroll list, for `localopts' */
80 struct mline {
81 struct mline *l_next;
82 ui32_t l_length;
83 ui32_t l_leadspaces; /* Number of leading SPC characters */
84 char l_line[VFIELD_SIZE(0)];
87 struct var {
88 struct var *v_link;
89 char *v_value;
90 char v_name[VFIELD_SIZE(0)];
93 struct var_virtual {
94 ui32_t vv_okey;
95 struct var const *vv_var;
98 struct var_map {
99 ui32_t vm_hash;
100 ui16_t vm_keyoff;
101 ui16_t vm_flags; /* var_map_flags bits */
104 struct var_carrier {
105 char const *vc_name;
106 ui32_t vc_hash;
107 ui32_t vc_prime;
108 struct var *vc_var;
109 struct var_map const *vc_vmap;
110 enum okeys vc_okey;
113 struct var_show {
114 struct var_carrier vs_vc; /* _var_revlookup() */
115 char const *vs_value; /* Value (from wherever it came) or NULL */
116 bool_t vs_isset; /* Managed by us and existent */
117 bool_t vs_isenv; /* Set, but managed by environ */
118 bool_t vs_isasm; /* Is an assembled variable */
119 ui8_t __pad[5];
122 struct lostack {
123 struct lostack *s_up; /* Outer context */
124 struct macro *s_mac; /* Context (`account' or `define') */
125 struct var *s_localopts;
126 bool_t s_unroll; /* Unroll? */
129 /* Include the constant create-okey-map.pl output */
130 #include "version.h"
131 #include "okeys.h"
133 static struct macro *_acc_curr; /* Currently active account */
134 static struct lostack *_localopts; /* Currently executing macro unroll list */
135 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
136 * TODO today via a static lostack, it should be a field in mailbox, once that
137 * TODO is a real multi-instance object */
138 static struct var *_folder_hook_localopts;
140 /* TODO once we have a dynamically sized hashtable we could unite _macros and
141 * TODO _variables into a single hashtable, stripping down fun interface;
142 * TODO also, setting and clearing a variable can be easily joined */
143 static struct var *_vars[MA_PRIME]; /* TODO dynamically spaced */
144 static struct macro *_macros[MA_PRIME]; /* TODO dynamically spaced */
146 /* Special cased value string allocation */
147 static char * _var_vcopy(char const *str);
148 static void _var_vfree(char *cp);
150 /* Check for special housekeeping. */
151 static bool_t _var_check_specials(enum okeys okey, bool_t enable,
152 char **val);
154 /* If a variable name begins with a lowercase-character and contains at
155 * least one '@', it is converted to all-lowercase. This is necessary
156 * for lookups of names based on email addresses.
157 * Following the standard, only the part following the last '@' should
158 * be lower-cased, but practice has established otherwise here.
159 * Return value may have been placed in string dope (salloc()) */
160 static char const * _var_canonify(char const *vn);
162 /* Try to reverse lookup an option name to an enum okeys mapping.
163 * Updates vcp.vc_name and vcp.vc_hash; vcp.vc_vmap is NULL if none found */
164 static bool_t _var_revlookup(struct var_carrier *vcp, char const *name);
166 /* Lookup a variable from vcp.vc_(vmap|name|hash), return wether it was found.
167 * Sets vcp.vc_prime; vcp.vc_var is NULL if not found */
168 static bool_t _var_lookup(struct var_carrier *vcp);
170 /* Completely fill vsp with data for name, return wether it was set as an
171 * internal variable (it may still have been set as an environment variable) */
172 static bool_t _var_broadway(struct var_show *vsp, char const *name);
174 /* Set variable from vcp.vc_(vmap|name|hash), return success */
175 static bool_t _var_set(struct var_carrier *vcp, char const *value);
177 /* Clear variable from vcp.vc_(vmap|name|hash); sets vcp.vc_var to NULL,
178 * return success */
179 static bool_t _var_clear(struct var_carrier *vcp);
181 /* List all variables */
182 static void _var_list_all(void);
184 static int __var_list_all_cmp(void const *s1, void const *s2);
185 static char * __var_simple_quote(char const *cp);
187 /* Shared c_set() and c_setenv() impl, return success */
188 static bool_t _var_set_env(char **ap, bool_t issetenv);
190 /* Does cp consist solely of WS and a } */
191 static bool_t _is_closing_angle(char const *cp);
193 /* Lookup for macros/accounts */
194 static struct macro *_ma_look(char const *name, struct macro *data,
195 enum ma_flags mafl);
197 /* Walk all lines of a macro and execute() them */
198 static int _ma_exec(struct macro const *mp, struct var **unroller);
200 /* User display helpers */
201 static int _ma_list(enum ma_flags mafl);
203 /* */
204 static bool_t _ma_define(char const *name, enum ma_flags mafl);
205 static void _ma_undefine(char const *name, enum ma_flags mafl);
206 static void _ma_freelines(struct mline *lp);
208 /* Update replay-log */
209 static void _localopts_add(struct lostack *losp, char const *name,
210 struct var *ovap);
211 static void _localopts_unroll(struct var **vapp);
213 static char *
214 _var_vcopy(char const *str)
216 char *news;
217 size_t len;
218 NYD2_ENTER;
220 if (*str == '\0')
221 news = UNCONST("");
222 else {
223 len = strlen(str) +1;
224 news = smalloc(len);
225 memcpy(news, str, len);
227 NYD2_LEAVE;
228 return news;
231 static void
232 _var_vfree(char *cp)
234 NYD2_ENTER;
235 if (*cp != '\0')
236 free(cp);
237 NYD2_LEAVE;
240 static bool_t
241 _var_check_specials(enum okeys okey, bool_t enable, char **val)
243 char *cp = NULL;
244 bool_t ok = TRU1;
245 int flag = 0;
246 NYD2_ENTER;
248 switch (okey) {
249 case ok_b_debug:
250 flag = OPT_DEBUG;
251 break;
252 case ok_b_header:
253 flag = OPT_N_FLAG;
254 enable = !enable;
255 break;
256 case ok_b_memdebug:
257 flag = OPT_MEMDEBUG;
258 break;
259 case ok_b_skipemptybody:
260 flag = OPT_E_FLAG;
261 break;
262 case ok_b_verbose:
263 flag = (enable && !(options & OPT_VERB))
264 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
265 break;
266 case ok_v_folder:
267 ok = (val != NULL && var_folder_updated(*val, &cp));
268 if (ok && cp != NULL) {
269 _var_vfree(*val);
270 /* It's smalloc()ed, but ensure we don't leak */
271 if (*cp == '\0') {
272 *val = UNCONST("");
273 free(cp);
274 } else
275 *val = cp;
277 break;
278 #ifdef HAVE_NCL
279 case ok_v_line_editor_cursor_right:
280 if ((ok = (val != NULL && *val != NULL))) {
281 /* Set with no value? TODO very guly */
282 if (*(cp = *val) != '\0') {
283 char const *x = cp;
284 int c;
285 do {
286 c = expand_shell_escape(&x, FAL0);
287 if (c < 0)
288 break;
289 *cp++ = (char)c;
290 } while (*x != '\0');
291 *cp = '\0';
294 break;
295 #endif
296 default:
297 break;
300 if (flag) {
301 if (enable)
302 options |= flag;
303 else
304 options &= ~flag;
306 NYD2_LEAVE;
307 return ok;
310 static char const *
311 _var_canonify(char const *vn)
313 NYD2_ENTER;
314 if (!upperchar(*vn)) {
315 char const *vp;
317 for (vp = vn; *vp != '\0' && *vp != '@'; ++vp)
319 vn = (*vp == '@') ? i_strdup(vn) : vn;
321 NYD2_LEAVE;
322 return vn;
325 static bool_t
326 _var_revlookup(struct var_carrier *vcp, char const *name)
328 ui32_t hash, i, j;
329 struct var_map const *vmp;
330 NYD2_ENTER;
332 vcp->vc_name = name = _var_canonify(name);
333 vcp->vc_hash = hash = MA_NAME2HASH(name);
335 for (i = hash % _VAR_REV_PRIME, j = 0; j <= _VAR_REV_LONGEST; ++j) {
336 ui32_t x = _var_revmap[i];
337 if (x == _VAR_REV_ILL)
338 break;
339 vmp = _var_map + x;
340 if (vmp->vm_hash == hash && !strcmp(_var_keydat + vmp->vm_keyoff, name)) {
341 vcp->vc_vmap = vmp;
342 vcp->vc_okey = (enum okeys)x;
343 goto jleave;
345 if (++i == _VAR_REV_PRIME) {
346 #ifdef _VAR_REV_WRAPAROUND
347 i = 0;
348 #else
349 break;
350 #endif
353 vcp->vc_vmap = NULL;
354 vcp = NULL;
355 jleave:
356 NYD2_LEAVE;
357 return (vcp != NULL);
360 static bool_t
361 _var_lookup(struct var_carrier *vcp)
363 struct var **vap, *lvp, *vp;
364 NYD2_ENTER;
366 /* XXX _So_ unlikely that it should be checked if normal lookup fails! */
367 if (UNLIKELY(vcp->vc_vmap != NULL &&
368 (vcp->vc_vmap->vm_flags & VM_VIRTUAL) != 0)) {
369 struct var_virtual const *vvp;
371 for (vvp = _var_virtuals;
372 PTRCMP(vvp, <, _var_virtuals + NELEM(_var_virtuals)); ++vvp)
373 if (vvp->vv_okey == vcp->vc_okey) {
374 vp = UNCONST(vvp->vv_var);
375 goto jleave;
377 assert(0);
380 vap = _vars + (vcp->vc_prime = MA_HASH2PRIME(vcp->vc_hash));
382 for (lvp = NULL, vp = *vap; vp != NULL; lvp = vp, vp = vp->v_link)
383 if (!strcmp(vp->v_name, vcp->vc_name)) {
384 /* Relink as head, hope it "sorts on usage" over time.
385 * _var_clear() relies on this behaviour */
386 if (lvp != NULL) {
387 lvp->v_link = vp->v_link;
388 vp->v_link = *vap;
389 *vap = vp;
391 goto jleave;
393 vp = NULL;
394 jleave:
395 vcp->vc_var = vp;
396 NYD2_LEAVE;
397 return (vp != NULL);
400 static bool_t
401 _var_broadway(struct var_show *vsp, char const *name)
403 bool_t rv;
404 NYD2_ENTER;
406 memset(vsp, 0, sizeof *vsp);
408 vsp->vs_isasm = !_var_revlookup(&vsp->vs_vc, name);
410 if ((vsp->vs_isset = rv = _var_lookup(&vsp->vs_vc))) {
411 vsp->vs_value = vsp->vs_vc.vc_var->v_value;
412 vsp->vs_isenv = FAL0;
413 } else
414 vsp->vs_isenv = ((vsp->vs_value = getenv(vsp->vs_vc.vc_name)) != NULL);
416 NYD2_LEAVE;
417 return rv;
420 static bool_t
421 _var_set(struct var_carrier *vcp, char const *value)
423 struct var *vp;
424 char *oval;
425 bool_t ok = TRU1;
426 NYD2_ENTER;
428 if (value == NULL) {
429 ok = _var_clear(vcp);
430 goto jleave;
433 _var_lookup(vcp);
435 if (vcp->vc_vmap != NULL && (vcp->vc_vmap->vm_flags & VM_RDONLY)) {
436 n_err(_("Variable readonly: \"%s\"\n"), vcp->vc_name);
437 ok = FAL0;
438 goto jleave;
441 /* Don't care what happens later on, store this in the unroll list */
442 if (_localopts != NULL)
443 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
445 if ((vp = vcp->vc_var) == NULL) {
446 size_t l = strlen(vcp->vc_name) + 1;
448 vcp->vc_var =
449 vp = smalloc(sizeof(*vp) - VFIELD_SIZEOF(struct var, v_name) + l);
450 vp->v_link = _vars[vcp->vc_prime];
451 _vars[vcp->vc_prime] = vp;
452 memcpy(vp->v_name, vcp->vc_name, l);
453 oval = UNCONST("");
454 } else
455 oval = vp->v_value;
457 if (vcp->vc_vmap == NULL)
458 vp->v_value = _var_vcopy(value);
459 else {
460 /* Via `set' etc. the user may give even binary options non-binary
461 * values, ignore that and force binary xxx error log? */
462 if (vcp->vc_vmap->vm_flags & VM_BINARY)
463 value = UNCONST("");
464 vp->v_value = _var_vcopy(value);
466 /* Check if update allowed XXX wasteful on error! */
467 if ((vcp->vc_vmap->vm_flags & VM_SPECIAL) &&
468 !(ok = _var_check_specials(vcp->vc_okey, TRU1, &vp->v_value))) {
469 char *cp = vp->v_value;
470 vp->v_value = oval;
471 oval = cp;
475 if (*oval != '\0')
476 _var_vfree(oval);
477 jleave:
478 NYD2_LEAVE;
479 return ok;
482 static bool_t
483 _var_clear(struct var_carrier *vcp)
485 bool_t ok = TRU1;
486 NYD2_ENTER;
488 if (!_var_lookup(vcp)) {
489 if (!(pstate & PS_IN_LOAD) && (options & OPT_D_V))
490 n_err(_("Variable undefined: \"%s\"\n"), vcp->vc_name);
491 } else if (vcp->vc_vmap != NULL && (vcp->vc_vmap->vm_flags & VM_RDONLY)) {
492 n_err(_("Variable readonly: \"%s\"\n"), vcp->vc_name);
493 ok = FAL0;
494 } else {
495 if (_localopts != NULL)
496 _localopts_add(_localopts, vcp->vc_name, vcp->vc_var);
498 /* Always listhead after _var_lookup() */
499 _vars[vcp->vc_prime] = _vars[vcp->vc_prime]->v_link;
500 _var_vfree(vcp->vc_var->v_value);
501 free(vcp->vc_var);
502 vcp->vc_var = NULL;
504 if (vcp->vc_vmap != NULL && (vcp->vc_vmap->vm_flags & VM_SPECIAL))
505 ok = _var_check_specials(vcp->vc_okey, FAL0, NULL);
507 NYD2_LEAVE;
508 return ok;
511 static void
512 _var_list_all(void)
514 struct var_show vs;
515 FILE *fp;
516 size_t no, i;
517 struct var *vp;
518 char const **vacp, **cap;
519 NYD2_ENTER;
521 if ((fp = Ftmp(NULL, "listvars", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
522 NULL) {
523 n_perr(_("tmpfile"), 0);
524 goto jleave;
527 for (no = i = 0; i < MA_PRIME; ++i)
528 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
529 ++no;
530 no += NELEM(_var_virtuals);
532 vacp = salloc(no * sizeof(*vacp));
534 for (cap = vacp, i = 0; i < MA_PRIME; ++i)
535 for (vp = _vars[i]; vp != NULL; vp = vp->v_link)
536 *cap++ = vp->v_name;
537 for (i = 0; i < NELEM(_var_virtuals); ++i)
538 *cap++ = _var_virtuals[i].vv_var->v_name;
540 if (no > 1)
541 qsort(vacp, no, sizeof *vacp, &__var_list_all_cmp);
543 i = (ok_blook(bsdcompat) || ok_blook(bsdset));
544 for (cap = vacp; no != 0; ++cap, --no) {
545 char const *asmis, *fmt;
547 if (!_var_broadway(&vs, *cap))
548 continue;
549 if (vs.vs_value == NULL)
550 vs.vs_value = "";
552 asmis = !(options & OPT_D_VV) ? ""
553 : vs.vs_isasm ? "*" : " ";
554 if (i)
555 fmt = "%s%s\t%s\n";
556 else {
557 if (vs.vs_vc.vc_vmap != NULL &&
558 (vs.vs_vc.vc_vmap->vm_flags & VM_BINARY))
559 fmt = "%sset %s\n";
560 else {
561 fmt = "%sset %s=\"%s\"\n";
562 if (*vs.vs_value != '\0')
563 vs.vs_value = __var_simple_quote(vs.vs_value);
566 /* Shall a code checker complain on that, i'm in holiday */
567 fprintf(fp, fmt, asmis, *cap, vs.vs_value);
570 page_or_print(fp, PTR2SIZE(cap - vacp));
571 Fclose(fp);
572 jleave:
573 NYD2_LEAVE;
576 static int
577 __var_list_all_cmp(void const *s1, void const *s2)
579 int rv;
580 NYD2_ENTER;
582 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
583 NYD2_LEAVE;
584 return rv;
587 static char *
588 __var_simple_quote(char const *cp) /* TODO "unite" with string_quote(), etc.. */
590 bool_t esc;
591 size_t i;
592 char const *cp_base;
593 char c, *rv;
594 NYD2_ENTER;
596 for (i = 0, cp_base = cp; (c = *cp) != '\0'; ++i, ++cp)
597 if (c == '"')
598 ++i;
599 rv = salloc(i +1);
601 for (esc = FAL0, i = 0, cp = cp_base; (c = *cp) != '\0'; rv[i++] = c, ++cp) {
602 if (!esc) {
603 if (c == '"')
604 rv[i++] = '\\';
605 esc = (c == '\\');
606 } else
607 esc = FAL0;
609 rv[i] = '\0';
610 NYD2_LEAVE;
611 return rv;
614 static bool_t
615 _var_set_env(char **ap, bool_t issetenv)
617 char *cp, *cp2, *varbuf, c;
618 size_t errs = 0;
619 NYD2_ENTER;
621 for (; *ap != NULL; ++ap) {
622 /* Isolate key */
623 cp = *ap;
624 cp2 = varbuf = ac_alloc(strlen(cp) +1);
625 for (; (c = *cp) != '=' && c != '\0'; ++cp)
626 *cp2++ = c;
627 *cp2 = '\0';
628 if (c == '\0')
629 cp = UNCONST("");
630 else
631 ++cp;
632 if (varbuf == cp2) {
633 n_err(_("Non-null variable name required\n"));
634 ++errs;
635 goto jnext;
638 if (varbuf[0] == 'n' && varbuf[1] == 'o') {
639 char const *k = varbuf + 2;
641 if (issetenv && (!strcmp(k, "HOME") || /* TODO generic */
642 !strcmp(k, "USER") || !strcmp(k, "TMPDIR"))) {/* TODO approach */
643 if (options & OPT_D_V)
644 n_err(_("Cannot `unsetenv' \"%s\"\n"), k);
645 ++errs;
646 goto jnext;
649 errs += !_var_vokclear(k);
650 if (issetenv) {
651 #ifdef HAVE_SETENV
652 errs += (unsetenv(k) != 0);
653 #else
654 ++errs;
655 #endif
657 } else {
658 errs += !_var_vokset(varbuf, (uintptr_t)cp);
659 if (issetenv) {
660 do {
661 static char const *cp_buf[3];
662 char const **pl, **pe;
664 if (!strcmp(varbuf, "HOME")) /* TODO generic */
665 pl = cp_buf + 0, pe = &homedir;
666 else if (!strcmp(varbuf, "USER")) /* TODO approach */
667 pl = cp_buf + 1, pe = &myname;
668 else if (!strcmp(varbuf, "TMPDIR")) /* TODO also here */
669 pl = cp_buf + 2, pe = &tempdir;
670 else
671 break;
673 if (*pl != NULL)
674 free(UNCONST(*pl));
675 *pe = *pl = sstrdup(cp);
676 } while (0);
678 #ifdef HAVE_SETENV
679 errs += (setenv(varbuf, cp, 1) != 0);
680 #else
681 ++errs;
682 #endif
685 jnext:
686 ac_free(varbuf);
689 NYD2_LEAVE;
690 return (errs == 0);
693 static bool_t
694 _is_closing_angle(char const *cp)
696 bool_t rv = FAL0;
697 NYD2_ENTER;
699 while (spacechar(*cp))
700 ++cp;
701 if (*cp++ != '}')
702 goto jleave;
703 while (spacechar(*cp))
704 ++cp;
705 rv = (*cp == '\0');
706 jleave:
707 NYD2_LEAVE;
708 return rv;
711 static struct macro *
712 _ma_look(char const *name, struct macro *data, enum ma_flags mafl)
714 enum ma_flags save_mafl;
715 ui32_t h;
716 struct macro *lmp, *mp;
717 NYD2_ENTER;
719 save_mafl = mafl;
720 mafl &= MA_TYPE_MASK;
721 h = MA_NAME2HASH(name);
722 h = MA_HASH2PRIME(h);
724 for (lmp = NULL, mp = _macros[h]; mp != NULL; lmp = mp, mp = mp->ma_next) {
725 if ((mp->ma_flags & MA_TYPE_MASK) == mafl && !strcmp(mp->ma_name, name)) {
726 if (save_mafl & MA_UNDEF) {
727 if (lmp == NULL)
728 _macros[h] = mp->ma_next;
729 else
730 lmp->ma_next = mp->ma_next;
732 /* TODO it should also be possible to delete running macros */
733 if ((mafl & MA_ACC) &&
734 account_name != NULL && !strcmp(account_name, name)) {
735 mp->ma_flags |= MA_DELETED;
736 n_err(_("Delayed deletion of active account \"%s\"\n"), name);
737 } else {
738 _ma_freelines(mp->ma_contents);
739 free(mp->ma_name);
740 free(mp);
741 mp = (struct macro*)-1;
744 goto jleave;
748 if (data != NULL) {
749 data->ma_next = _macros[h];
750 _macros[h] = data;
751 mp = NULL;
753 jleave:
754 NYD2_LEAVE;
755 return mp;
758 static int
759 _ma_exec(struct macro const *mp, struct var **unroller)
761 struct lostack los;
762 char *buf;
763 struct n2 {struct n2 *up; struct lostack *lo;} *x; /* FIXME hack (sigman+) */
764 struct mline const *lp;
765 int rv = 0;
766 NYD2_ENTER;
768 los.s_up = _localopts;
769 los.s_mac = UNCONST(mp);
770 los.s_localopts = NULL;
771 los.s_unroll = FAL0;
772 _localopts = &los;
774 x = salloc(sizeof *x); /* FIXME intermediate hack (signal man+) */
775 x->up = temporary_localopts_store;
776 x->lo = _localopts;
777 temporary_localopts_store = x;
779 buf = ac_alloc(mp->ma_maxlen +1);
780 for (lp = mp->ma_contents; lp; lp = lp->l_next) {
781 memcpy(buf, lp->l_line, lp->l_length +1);
782 rv |= execute(buf, lp->l_length); /* XXX break if != 0 ? */
784 ac_free(buf);
786 temporary_localopts_store = x->up; /* FIXME intermediate hack */
788 _localopts = los.s_up;
789 if (unroller == NULL) {
790 if (los.s_localopts != NULL)
791 _localopts_unroll(&los.s_localopts);
792 } else
793 *unroller = los.s_localopts;
794 NYD2_LEAVE;
795 return rv;
798 static int
799 _ma_list(enum ma_flags mafl)
801 FILE *fp;
802 char const *typestr;
803 struct macro *mq;
804 ui32_t ti, mc, i;
805 struct mline *lp;
806 NYD2_ENTER;
808 if ((fp = Ftmp(NULL, "listmacs", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
809 NULL) {
810 n_perr(_("tmpfile"), 0);
811 mc = 1;
812 goto jleave;
815 mafl &= MA_TYPE_MASK;
816 typestr = (mafl & MA_ACC) ? "account" : "define";
818 for (ti = mc = 0; ti < MA_PRIME; ++ti)
819 for (mq = _macros[ti]; mq; mq = mq->ma_next)
820 if ((mq->ma_flags & MA_TYPE_MASK) == mafl) {
821 if (++mc > 1)
822 putc('\n', fp);
823 fprintf(fp, "%s %s {\n", typestr, mq->ma_name);
824 for (lp = mq->ma_contents; lp != NULL; lp = lp->l_next) {
825 for (i = lp->l_leadspaces; i > 0; --i)
826 putc(' ', fp);
827 fputs(lp->l_line, fp);
828 putc('\n', fp);
830 fputs("}\n", fp);
832 if (mc)
833 page_or_print(fp, 0);
835 mc = (ui32_t)ferror(fp);
836 Fclose(fp);
837 jleave:
838 NYD2_LEAVE;
839 return (int)mc;
842 static bool_t
843 _ma_define(char const *name, enum ma_flags mafl)
845 bool_t rv = FAL0;
846 struct macro *mp;
847 struct mline *lp, *lst = NULL, *lnd = NULL;
848 char *linebuf = NULL, *cp;
849 size_t linesize = 0;
850 ui32_t maxlen = 0, leaspc;
851 union {int i; ui32_t ui;} n;
852 NYD2_ENTER;
854 mp = scalloc(1, sizeof *mp);
855 mp->ma_name = sstrdup(name);
856 mp->ma_flags = mafl;
858 for (;;) {
859 n.i = readline_input("", TRU1, &linebuf, &linesize, NULL);
860 if (n.ui == 0)
861 continue;
862 if (n.i < 0) {
863 n_err(_("Unterminated %s definition: \"%s\"\n"),
864 (mafl & MA_ACC ? "account" : "macro"), mp->ma_name);
865 if ((pstate & PS_IN_LOAD) == PS_SOURCING)
866 unstack();
867 goto jerr;
869 if (_is_closing_angle(linebuf))
870 break;
872 /* Trim WS xxx we count tabs as one space here */
873 for (cp = linebuf, leaspc = 0; n.ui > 0 && whitechar(*cp); ++cp, --n.ui)
874 if (*cp == '\t')
875 leaspc = (leaspc + 8) & ~7;
876 else
877 ++leaspc;
878 if (n.ui == 0)
879 continue;
880 for (; whitechar(cp[n.ui - 1]); --n.ui)
881 assert(n.ui > 0);
882 assert(n.ui > 0);
884 maxlen = MAX(maxlen, n.ui);
885 cp[n.ui++] = '\0';
886 lp = scalloc(1, sizeof(*lp) - VFIELD_SIZEOF(struct mline, l_line) + n.ui);
887 memcpy(lp->l_line, cp, n.ui);
888 lp->l_length = --n.ui;
889 lp->l_leadspaces = leaspc;
891 if (lst != NULL) {
892 lnd->l_next = lp;
893 lnd = lp;
894 } else
895 lst = lnd = lp;
897 mp->ma_contents = lst;
898 mp->ma_maxlen = maxlen;
900 if (_ma_look(mp->ma_name, mp, mafl) != NULL) {
901 n_err(_("A %s named \"%s\" already exists\n"),
902 (mafl & MA_ACC ? "account" : "macro"), mp->ma_name);
903 lst = mp->ma_contents;
904 goto jerr;
907 rv = TRU1;
908 jleave:
909 if (linebuf != NULL)
910 free(linebuf);
911 NYD2_LEAVE;
912 return rv;
914 jerr:
915 if (lst != NULL)
916 _ma_freelines(lst);
917 free(mp->ma_name);
918 free(mp);
919 goto jleave;
922 static void
923 _ma_undefine(char const *name, enum ma_flags mafl)
925 struct macro *mp;
926 NYD2_ENTER;
928 if (LIKELY(name[0] != '*' || name[1] != '\0')) {
929 if ((mp = _ma_look(name, NULL, mafl | MA_UNDEF)) == NULL)
930 n_err(_("%s \"%s\" is not defined\n"),
931 (mafl & MA_ACC ? "Account" : "Macro"), name);
932 } else {
933 struct macro **mpp, *lmp;
935 for (mpp = _macros; PTRCMP(mpp, <, _macros + NELEM(_macros)); ++mpp)
936 for (lmp = NULL, mp = *mpp; mp != NULL;) {
937 if ((mp->ma_flags & MA_TYPE_MASK) == mafl) {
938 /* xxx Expensive but rare: be simple */
939 _ma_look(mp->ma_name, NULL, mafl | MA_UNDEF);
940 mp = (lmp == NULL) ? *mpp : lmp->ma_next;
941 } else {
942 lmp = mp;
943 mp = mp->ma_next;
947 NYD2_LEAVE;
950 static void
951 _ma_freelines(struct mline *lp)
953 struct mline *lq;
954 NYD2_ENTER;
956 for (lq = NULL; lp != NULL; ) {
957 if (lq != NULL)
958 free(lq);
959 lq = lp;
960 lp = lp->l_next;
962 if (lq)
963 free(lq);
964 NYD2_LEAVE;
967 static void
968 _localopts_add(struct lostack *losp, char const *name, struct var *ovap)
970 struct var *vap;
971 size_t nl, vl;
972 NYD2_ENTER;
974 /* Propagate unrolling up the stack, as necessary */
975 while (!losp->s_unroll && (losp = losp->s_up) != NULL)
977 if (losp == NULL)
978 goto jleave;
980 /* We've found a level that wants to unroll; check wether it does it yet */
981 for (vap = losp->s_localopts; vap != NULL; vap = vap->v_link)
982 if (!strcmp(vap->v_name, name))
983 goto jleave;
985 nl = strlen(name) + 1;
986 vl = (ovap != NULL) ? strlen(ovap->v_value) + 1 : 0;
987 vap = smalloc(sizeof(*vap) - VFIELD_SIZEOF(struct var, v_name) + nl + vl);
988 vap->v_link = losp->s_localopts;
989 losp->s_localopts = vap;
990 memcpy(vap->v_name, name, nl);
991 if (vl == 0)
992 vap->v_value = NULL;
993 else {
994 vap->v_value = vap->v_name + nl;
995 memcpy(vap->v_value, ovap->v_value, vl);
997 jleave:
998 NYD2_LEAVE;
1001 static void
1002 _localopts_unroll(struct var **vapp)
1004 struct lostack *save_los;
1005 struct var *x, *vap;
1006 NYD2_ENTER;
1008 vap = *vapp;
1009 *vapp = NULL;
1011 save_los = _localopts;
1012 _localopts = NULL;
1013 while (vap != NULL) {
1014 x = vap;
1015 vap = vap->v_link;
1016 vok_vset(x->v_name, x->v_value);
1017 free(x);
1019 _localopts = save_los;
1020 NYD2_LEAVE;
1023 FL char *
1024 _var_oklook(enum okeys okey)
1026 struct var_carrier vc;
1027 char *rv;
1028 NYD_ENTER;
1030 vc.vc_vmap = _var_map + okey;
1031 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
1032 vc.vc_hash = _var_map[okey].vm_hash;
1033 vc.vc_okey = okey;
1035 if (!_var_lookup(&vc)) {
1036 if ((rv = getenv(vc.vc_name)) != NULL) {
1037 _var_set(&vc, rv);
1038 assert(vc.vc_var != NULL);
1039 goto jvar;
1041 } else
1042 jvar:
1043 rv = vc.vc_var->v_value;
1044 NYD_LEAVE;
1045 return rv;
1048 FL bool_t
1049 _var_okset(enum okeys okey, uintptr_t val)
1051 struct var_carrier vc;
1052 bool_t ok;
1053 NYD_ENTER;
1055 vc.vc_vmap = _var_map + okey;
1056 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
1057 vc.vc_hash = _var_map[okey].vm_hash;
1058 vc.vc_okey = okey;
1060 ok = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
1061 NYD_LEAVE;
1062 return ok;
1065 FL bool_t
1066 _var_okclear(enum okeys okey)
1068 struct var_carrier vc;
1069 bool_t rv;
1070 NYD_ENTER;
1072 vc.vc_vmap = _var_map + okey;
1073 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
1074 vc.vc_hash = _var_map[okey].vm_hash;
1075 vc.vc_okey = okey;
1077 rv = _var_clear(&vc);
1078 NYD_LEAVE;
1079 return rv;
1082 FL char *
1083 _var_voklook(char const *vokey)
1085 struct var_carrier vc;
1086 char *rv;
1087 NYD_ENTER;
1089 _var_revlookup(&vc, vokey);
1091 if (!_var_lookup(&vc)) {
1092 if ((rv = getenv(vc.vc_name)) != NULL) {
1093 _var_set(&vc, rv);
1094 assert(vc.vc_var != NULL);
1095 goto jvar;
1097 } else
1098 jvar:
1099 rv = vc.vc_var->v_value;
1100 NYD_LEAVE;
1101 return rv;
1104 FL bool_t
1105 _var_vokset(char const *vokey, uintptr_t val)
1107 struct var_carrier vc;
1108 bool_t ok;
1109 NYD_ENTER;
1111 _var_revlookup(&vc, vokey);
1113 ok = _var_set(&vc, (val == 0x1 ? "" : (char const*)val));
1114 NYD_LEAVE;
1115 return ok;
1118 FL bool_t
1119 _var_vokclear(char const *vokey)
1121 struct var_carrier vc;
1122 bool_t err;
1123 NYD_ENTER;
1125 _var_revlookup(&vc, vokey);
1127 err = !_var_clear(&vc);
1128 NYD_LEAVE;
1129 return !err;
1132 FL char *
1133 _env_look(char const *envkey, bool_t envonly) /* TODO rather dummy yet!! */
1135 char *rv;
1136 NYD_ENTER;
1138 if (envonly)
1139 rv = getenv(envkey); /* TODO rework vars: cache, output a la mimetypes */
1140 else
1141 rv = _var_voklook(envkey);
1142 NYD_LEAVE;
1143 return rv;
1146 #ifdef HAVE_SOCKETS
1147 FL char *
1148 _var_xoklook(enum okeys okey, struct url const *urlp, enum okey_xlook_mode oxm)
1150 struct var_carrier vc;
1151 struct str const *us;
1152 size_t nlen;
1153 char *nbuf = NULL /* CC happiness */, *rv;
1154 NYD_ENTER;
1156 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1158 /* For simplicity: allow this case too */
1159 if (!(oxm & (OXM_H_P | OXM_U_H_P)))
1160 goto jplain;
1162 vc.vc_vmap = _var_map + okey;
1163 vc.vc_name = _var_keydat + _var_map[okey].vm_keyoff;
1164 vc.vc_okey = okey;
1166 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1167 nlen = strlen(vc.vc_name);
1168 nbuf = ac_alloc(nlen + 1 + us->l +1);
1169 memcpy(nbuf, vc.vc_name, nlen);
1170 nbuf[nlen++] = '-';
1172 /* One of .url_u_h_p and .url_h_p we test in here */
1173 memcpy(nbuf + nlen, us->s, us->l +1);
1174 vc.vc_name = _var_canonify(nbuf);
1175 vc.vc_hash = MA_NAME2HASH(vc.vc_name);
1176 if (_var_lookup(&vc))
1177 goto jvar;
1179 /* The second */
1180 if (oxm & OXM_H_P) {
1181 us = &urlp->url_h_p;
1182 memcpy(nbuf + nlen, us->s, us->l +1);
1183 vc.vc_name = _var_canonify(nbuf);
1184 vc.vc_hash = MA_NAME2HASH(vc.vc_name);
1185 if (_var_lookup(&vc)) {
1186 jvar:
1187 rv = vc.vc_var->v_value;
1188 goto jleave;
1192 jplain:
1193 rv = (oxm & OXM_PLAIN) ? _var_oklook(okey) : NULL;
1194 jleave:
1195 if (oxm & (OXM_H_P | OXM_U_H_P))
1196 ac_free(nbuf);
1197 NYD_LEAVE;
1198 return rv;
1200 #endif /* HAVE_SOCKETS */
1202 FL int
1203 c_varshow(void *v)
1205 struct var_show vs;
1206 char const **argv = v;
1207 NYD_ENTER;
1209 if (*argv == NULL)
1210 v = NULL;
1211 else for (; *argv != NULL; ++argv) {
1212 _var_broadway(&vs, *argv);
1213 if (vs.vs_value == NULL)
1214 vs.vs_value = "NULL";
1216 if (vs.vs_vc.vc_vmap != NULL) {
1217 ui16_t f = vs.vs_vc.vc_vmap->vm_flags;
1219 if (f & VM_BINARY)
1220 printf(_("\"%s\": (%d) binary%s%s: set=%d (ENVIRON=%d)\n"),
1221 vs.vs_vc.vc_name, vs.vs_vc.vc_okey,
1222 (f & VM_RDONLY ? ", read-only" : ""),
1223 (f & VM_VIRTUAL ? ", virtual" : ""), vs.vs_isset, vs.vs_isenv);
1224 else
1225 printf(_("\"%s\": (%d) value%s%s: set=%d (ENVIRON=%d) value<%s>\n"),
1226 vs.vs_vc.vc_name, vs.vs_vc.vc_okey,
1227 (f & VM_RDONLY ? ", read-only" : ""),
1228 (f & VM_VIRTUAL ? ", virtual" : ""), vs.vs_isset, vs.vs_isenv,
1229 vs.vs_value);
1230 } else
1231 printf("\"%s\": (assembled): set=%d (ENVIRON=%d) value<%s>\n",
1232 vs.vs_vc.vc_name, vs.vs_isset, vs.vs_isenv, vs.vs_value);
1234 NYD_LEAVE;
1235 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1238 FL int
1239 c_set(void *v)
1241 char **ap = v;
1242 int err;
1243 NYD_ENTER;
1245 if (*ap == NULL) {
1246 _var_list_all();
1247 err = 0;
1248 } else
1249 err = !_var_set_env(ap, FAL0);
1250 NYD_LEAVE;
1251 return err;
1254 FL int
1255 c_setenv(void *v)
1257 char **ap = v;
1258 int err;
1259 NYD_ENTER;
1261 if (!(err = !(pstate & PS_STARTED)))
1262 err = !_var_set_env(ap, TRU1);
1263 NYD_LEAVE;
1264 return err;
1267 FL int
1268 c_unset(void *v)
1270 char **ap = v;
1271 int err = 0;
1272 NYD_ENTER;
1274 while (*ap != NULL)
1275 err |= !_var_vokclear(*ap++);
1276 NYD_LEAVE;
1277 return err;
1280 FL int
1281 c_unsetenv(void *v)
1283 int err;
1284 NYD_ENTER;
1286 if (!(err = !(pstate & PS_STARTED))) {
1287 char **ap;
1289 for (ap = v; *ap != NULL; ++ap) {
1290 bool_t bad;
1292 if (!strcmp(*ap, "HOME") || !strcmp(*ap, "USER") || /* TODO generic */
1293 !strcmp(*ap, "TMPDIR")) { /* TODO approach */
1294 if (options & OPT_D_V)
1295 n_err(_("Cannot `unsetenv' \"%s\"\n"), *ap);
1296 err = 1;
1297 continue;
1300 bad = !_var_vokclear(*ap);
1301 if (
1302 #ifdef HAVE_SETENV
1303 unsetenv(*ap) != 0 ||
1304 #endif
1307 err = 1;
1310 NYD_LEAVE;
1311 return err;
1314 FL int
1315 c_varedit(void *v)
1317 struct var_carrier vc;
1318 sighandler_type sigint;
1319 FILE *of, *nf;
1320 char *val, **argv = v;
1321 int err = 0;
1322 NYD_ENTER;
1324 sigint = safe_signal(SIGINT, SIG_IGN);
1326 while (*argv != NULL) {
1327 memset(&vc, 0, sizeof vc);
1329 _var_revlookup(&vc, *argv++);
1331 if (vc.vc_vmap != NULL) {
1332 if (vc.vc_vmap->vm_flags & VM_BINARY) {
1333 n_err(_("`varedit': can't edit binary variable \"%s\"\n"),
1334 vc.vc_name);
1335 continue;
1337 if (vc.vc_vmap->vm_flags & VM_RDONLY) {
1338 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1339 vc.vc_name);
1340 continue;
1344 _var_lookup(&vc);
1346 if ((of = Ftmp(NULL, "vared", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
1347 NULL) {
1348 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1349 err = 1;
1350 break;
1351 } else if (vc.vc_var != NULL && *(val = vc.vc_var->v_value) != '\0' &&
1352 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)) {
1353 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1354 Fclose(of);
1355 err = 1;
1356 continue;
1359 fflush_rewind(of);
1360 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1361 Fclose(of);
1363 if (nf != NULL) {
1364 int c;
1365 char *base;
1366 off_t l = fsize(nf);
1368 assert(l >= 0);
1369 base = smalloc((size_t)l + 1);
1371 for (l = 0, val = base; (c = getc(nf)) != EOF; ++val)
1372 if (c == '\n' || c == '\r') {
1373 *val = ' ';
1374 ++l;
1375 } else {
1376 *val = (char)(uc_i)c;
1377 l = 0;
1379 val -= l;
1380 *val = '\0';
1382 if (!vok_vset(vc.vc_name, base))
1383 err = 1;
1385 free(base);
1386 Fclose(nf);
1387 } else {
1388 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
1389 err = 1;
1390 break;
1394 safe_signal(SIGINT, sigint);
1395 NYD_LEAVE;
1396 return err;
1399 FL int
1400 c_define(void *v)
1402 int rv = 1;
1403 char **args = v;
1404 NYD_ENTER;
1406 if (args[0] == NULL) {
1407 rv = _ma_list(MA_NONE);
1408 goto jleave;
1411 if (args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1412 args[2] != NULL) {
1413 n_err(_("Syntax is: define <name> {"));
1414 goto jleave;
1417 rv = !_ma_define(args[0], MA_NONE);
1418 jleave:
1419 NYD_LEAVE;
1420 return rv;
1423 FL int
1424 c_undefine(void *v)
1426 int rv = 1;
1427 char **args = v;
1428 NYD_ENTER;
1430 if (*args == NULL) {
1431 n_err(_("`undefine': required arguments are missing\n"));
1432 goto jleave;
1435 _ma_undefine(*args, MA_NONE);
1436 while (*++args != NULL);
1437 rv = 0;
1438 jleave:
1439 NYD_LEAVE;
1440 return rv;
1443 FL int
1444 c_call(void *v)
1446 int rv = 1;
1447 char **args = v;
1448 char const *errs, *name;
1449 struct macro *mp;
1450 NYD_ENTER;
1452 if (args[0] == NULL || (args[1] != NULL && args[2] != NULL)) {
1453 errs = _("Syntax is: call <%s>\n");
1454 name = "name";
1455 goto jerr;
1458 if ((mp = _ma_look(*args, NULL, MA_NONE)) == NULL) {
1459 errs = _("Undefined macro called: \"%s\"\n");
1460 name = *args;
1461 goto jerr;
1464 rv = _ma_exec(mp, NULL);
1465 jleave:
1466 NYD_LEAVE;
1467 return rv;
1468 jerr:
1469 n_err(errs, name);
1470 goto jleave;
1473 FL bool_t
1474 check_folder_hook(bool_t nmail)
1476 size_t len;
1477 char *var, *cp;
1478 struct macro *mp;
1479 struct var **unroller;
1480 bool_t rv = TRU1;
1481 NYD_ENTER;
1483 var = ac_alloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1485 /* First try the fully resolved path */
1486 snprintf(var, len, "folder-hook-%s", mailname);
1487 if ((cp = vok_vlook(var)) != NULL)
1488 goto jmac;
1490 /* If we are under *folder*, try the usual +NAME syntax, too */
1491 if (displayname[0] == '+') {
1492 char *x = mailname + len;
1494 for (; x > mailname; --x)
1495 if (x[-1] == '/') {
1496 snprintf(var, len, "folder-hook-+%s", x);
1497 if ((cp = vok_vlook(var)) != NULL)
1498 goto jmac;
1499 break;
1503 /* Plain *folder-hook* is our last try */
1504 if ((cp = ok_vlook(folder_hook)) == NULL)
1505 goto jleave;
1507 jmac:
1508 if ((mp = _ma_look(cp, NULL, MA_NONE)) == NULL) {
1509 n_err(_("Cannot call *folder-hook* for \"%s\": "
1510 "macro \"%s\" does not exist\n"), displayname, cp);
1511 rv = FAL0;
1512 goto jleave;
1515 pstate &= ~PS_HOOK_MASK;
1516 if (nmail) {
1517 pstate |= PS_HOOK_NEWMAIL;
1518 unroller = NULL;
1519 } else {
1520 pstate |= PS_HOOK;
1521 unroller = &_folder_hook_localopts;
1523 rv = (_ma_exec(mp, unroller) == 0);
1524 pstate &= ~PS_HOOK_MASK;
1526 jleave:
1527 ac_free(var);
1528 NYD_LEAVE;
1529 return rv;
1532 FL int
1533 c_account(void *v)
1535 char **args = v;
1536 struct macro *mp;
1537 int rv = 1, i, oqf, nqf;
1538 NYD_ENTER;
1540 if (args[0] == NULL) {
1541 rv = _ma_list(MA_ACC);
1542 goto jleave;
1545 if (args[1] && args[1][0] == '{' && args[1][1] == '\0') {
1546 if (args[2] != NULL) {
1547 n_err(_("Syntax is: account <name> {\n"));
1548 goto jleave;
1550 if (!asccasecmp(args[0], ACCOUNT_NULL)) {
1551 n_err(_("Error: \"%s\" is a reserved name\n"), ACCOUNT_NULL);
1552 goto jleave;
1554 rv = !_ma_define(args[0], MA_ACC);
1555 goto jleave;
1558 if (pstate & PS_HOOK_MASK) {
1559 n_err(_("Cannot change account from within a hook\n"));
1560 goto jleave;
1563 save_mbox_for_possible_quitstuff();
1565 mp = NULL;
1566 if (asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1567 (mp = _ma_look(args[0], NULL, MA_ACC)) == NULL) {
1568 n_err(_("Account \"%s\" does not exist\n"), args[0]);
1569 goto jleave;
1572 oqf = savequitflags();
1574 if (_acc_curr != NULL) {
1575 if (_acc_curr->ma_localopts != NULL)
1576 _localopts_unroll(&_acc_curr->ma_localopts);
1577 if (_acc_curr->ma_flags & MA_DELETED) { /* xxx can be made generic? */
1578 _ma_freelines(_acc_curr->ma_contents);
1579 free(_acc_curr->ma_name);
1580 free(_acc_curr);
1584 account_name = (mp != NULL) ? mp->ma_name : NULL;
1585 _acc_curr = mp;
1587 if (mp != NULL && _ma_exec(mp, &mp->ma_localopts) == CBAD) {
1588 /* XXX account switch incomplete, unroll? */
1589 n_err(_("Switching to account \"%s\" failed\n"), args[0]);
1590 goto jleave;
1593 if ((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED) {
1594 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1595 restorequitflags(oqf);
1596 if ((i = setfile("%", 0)) < 0)
1597 goto jleave;
1598 check_folder_hook(FAL0);
1599 if (i > 0 && !ok_blook(emptystart))
1600 goto jleave;
1601 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1602 restorequitflags(nqf);
1604 rv = 0;
1605 jleave:
1606 NYD_LEAVE;
1607 return rv;
1610 FL int
1611 c_unaccount(void *v)
1613 int rv = 1;
1614 char **args = v;
1615 NYD_ENTER;
1617 if (*args == NULL) {
1618 n_err(_("`unaccount': required arguments are missing\n"));
1619 goto jleave;
1622 _ma_undefine(*args, MA_ACC);
1623 while (*++args != NULL);
1624 rv = 0;
1625 jleave:
1626 NYD_LEAVE;
1627 return rv;
1630 FL int
1631 c_localopts(void *v)
1633 int rv = 1;
1634 char **c = v;
1635 NYD_ENTER;
1637 if (_localopts == NULL) {
1638 n_err(_("Cannot use `localopts' but from within a "
1639 "`define' or `account'\n"));
1640 goto jleave;
1643 _localopts->s_unroll = (boolify(*c, UIZ_MAX, FAL0) > 0);
1644 rv = 0;
1645 jleave:
1646 NYD_LEAVE;
1647 return rv;
1650 FL void
1651 temporary_localopts_free(void) /* XXX intermediate hack */
1653 struct n2 {struct n2 *up; struct lostack *lo;} *x;
1654 NYD_ENTER;
1656 x = temporary_localopts_store;
1657 temporary_localopts_store = NULL;
1658 _localopts = NULL;
1660 while (x != NULL) {
1661 struct lostack *losp = x->lo;
1662 x = x->up;
1663 if (losp->s_localopts != NULL)
1664 _localopts_unroll(&losp->s_localopts);
1666 NYD_LEAVE;
1669 FL void
1670 temporary_localopts_folder_hook_unroll(void) /* XXX intermediate hack */
1672 NYD_ENTER;
1673 if (_folder_hook_localopts != NULL) {
1674 void *save = _localopts;
1675 _localopts = NULL;
1676 _localopts_unroll(&_folder_hook_localopts);
1677 _localopts = save;
1679 NYD_LEAVE;
1682 /* s-it-mode */