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>.
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
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
36 #define n_FILE accmacvar
38 #ifndef HAVE_AMALGAMATION
39 # define _ACCMACVAR_SOURCE /* For _features[] */
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
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)
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 */
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 */
72 struct macro
*ma_next
;
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' */
83 ui32_t l_leadspaces
; /* Number of leading SPC characters */
84 char l_line
[VFIELD_SIZE(0)];
90 char v_name
[VFIELD_SIZE(0)];
95 struct var
const *vv_var
;
101 ui16_t vm_flags
; /* var_map_flags bits */
109 struct var_map
const *vc_vmap
;
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 */
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 */
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
,
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,
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
,
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
);
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
,
211 static void _localopts_unroll(struct var
**vapp
);
214 _var_vcopy(char const *str
)
223 len
= strlen(str
) +1;
225 memcpy(news
, str
, len
);
241 _var_check_specials(enum okeys okey
, bool_t enable
, char **val
)
259 case ok_b_skipemptybody
:
263 flag
= (enable
&& !(options
& OPT_VERB
))
264 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
267 ok
= (val
!= NULL
&& var_folder_updated(*val
, &cp
));
268 if (ok
&& cp
!= NULL
) {
270 /* It's smalloc()ed, but ensure we don't leak */
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') {
286 c
= expand_shell_escape(&x
, FAL0
);
290 } while (*x
!= '\0');
311 _var_canonify(char const *vn
)
314 if (!upperchar(*vn
)) {
317 for (vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
319 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
326 _var_revlookup(struct var_carrier
*vcp
, char const *name
)
329 struct var_map
const *vmp
;
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
)
340 if (vmp
->vm_hash
== hash
&& !strcmp(_var_keydat
+ vmp
->vm_keyoff
, name
)) {
342 vcp
->vc_okey
= (enum okeys
)x
;
345 if (++i
== _VAR_REV_PRIME
) {
346 #ifdef _VAR_REV_WRAPAROUND
357 return (vcp
!= NULL
);
361 _var_lookup(struct var_carrier
*vcp
)
363 struct var
**vap
, *lvp
, *vp
;
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
);
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 */
387 lvp
->v_link
= vp
->v_link
;
401 _var_broadway(struct var_show
*vsp
, char const *name
)
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
;
414 vsp
->vs_isenv
= ((vsp
->vs_value
= getenv(vsp
->vs_vc
.vc_name
)) != NULL
);
421 _var_set(struct var_carrier
*vcp
, char const *value
)
429 ok
= _var_clear(vcp
);
435 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_RDONLY
)) {
436 n_err(_("Variable readonly: \"%s\"\n"), vcp
->vc_name
);
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;
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
);
457 if (vcp
->vc_vmap
== NULL
)
458 vp
->v_value
= _var_vcopy(value
);
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
)
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
;
483 _var_clear(struct var_carrier
*vcp
)
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
);
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
);
504 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_SPECIAL
))
505 ok
= _var_check_specials(vcp
->vc_okey
, FAL0
, NULL
);
518 char const **vacp
, **cap
;
521 if ((fp
= Ftmp(NULL
, "listvars", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
523 n_perr(_("tmpfile"), 0);
527 for (no
= i
= 0; i
< MA_PRIME
; ++i
)
528 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
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
)
537 for (i
= 0; i
< NELEM(_var_virtuals
); ++i
)
538 *cap
++ = _var_virtuals
[i
].vv_var
->v_name
;
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
))
549 if (vs
.vs_value
== NULL
)
552 asmis
= !(options
& OPT_D_VV
) ? ""
553 : vs
.vs_isasm
? "*" : " ";
557 if (vs
.vs_vc
.vc_vmap
!= NULL
&&
558 (vs
.vs_vc
.vc_vmap
->vm_flags
& VM_BINARY
))
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
));
577 __var_list_all_cmp(void const *s1
, void const *s2
)
582 rv
= strcmp(*(char**)UNCONST(s1
), *(char**)UNCONST(s2
));
588 __var_simple_quote(char const *cp
) /* TODO "unite" with string_quote(), etc.. */
596 for (i
= 0, cp_base
= cp
; (c
= *cp
) != '\0'; ++i
, ++cp
)
601 for (esc
= FAL0
, i
= 0, cp
= cp_base
; (c
= *cp
) != '\0'; rv
[i
++] = c
, ++cp
) {
615 _var_set_env(char **ap
, bool_t issetenv
)
617 char *cp
, *cp2
, *varbuf
, c
;
621 for (; *ap
!= NULL
; ++ap
) {
624 cp2
= varbuf
= ac_alloc(strlen(cp
) +1);
625 for (; (c
= *cp
) != '=' && c
!= '\0'; ++cp
)
633 n_err(_("Non-null variable name required\n"));
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
);
649 errs
+= !_var_vokclear(k
);
652 errs
+= (unsetenv(k
) != 0);
658 errs
+= !_var_vokset(varbuf
, (uintptr_t)cp
);
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
;
675 *pe
= *pl
= sstrdup(cp
);
679 errs
+= (setenv(varbuf
, cp
, 1) != 0);
694 _is_closing_angle(char const *cp
)
699 while (spacechar(*cp
))
703 while (spacechar(*cp
))
711 static struct macro
*
712 _ma_look(char const *name
, struct macro
*data
, enum ma_flags mafl
)
714 enum ma_flags save_mafl
;
716 struct macro
*lmp
, *mp
;
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
) {
728 _macros
[h
] = mp
->ma_next
;
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
);
738 _ma_freelines(mp
->ma_contents
);
741 mp
= (struct macro
*)-1;
749 data
->ma_next
= _macros
[h
];
759 _ma_exec(struct macro
const *mp
, struct var
**unroller
)
763 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
; /* FIXME hack (sigman+) */
764 struct mline
const *lp
;
768 los
.s_up
= _localopts
;
769 los
.s_mac
= UNCONST(mp
);
770 los
.s_localopts
= NULL
;
774 x
= salloc(sizeof *x
); /* FIXME intermediate hack (signal man+) */
775 x
->up
= temporary_localopts_store
;
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 ? */
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
);
793 *unroller
= los
.s_localopts
;
799 _ma_list(enum ma_flags mafl
)
808 if ((fp
= Ftmp(NULL
, "listmacs", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
810 n_perr(_("tmpfile"), 0);
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
) {
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
)
827 fputs(lp
->l_line
, fp
);
833 page_or_print(fp
, 0);
835 mc
= (ui32_t
)ferror(fp
);
843 _ma_define(char const *name
, enum ma_flags mafl
)
847 struct mline
*lp
, *lst
= NULL
, *lnd
= NULL
;
848 char *linebuf
= NULL
, *cp
;
850 ui32_t maxlen
= 0, leaspc
;
851 union {int i
; ui32_t ui
;} n
;
854 mp
= scalloc(1, sizeof *mp
);
855 mp
->ma_name
= sstrdup(name
);
859 n
.i
= readline_input("", TRU1
, &linebuf
, &linesize
, NULL
);
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
)
869 if (_is_closing_angle(linebuf
))
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
)
875 leaspc
= (leaspc
+ 8) & ~7;
880 for (; whitechar(cp
[n
.ui
- 1]); --n
.ui
)
884 maxlen
= MAX(maxlen
, n
.ui
);
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
;
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
;
923 _ma_undefine(char const *name
, enum ma_flags mafl
)
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
);
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
;
951 _ma_freelines(struct mline
*lp
)
956 for (lq
= NULL
; lp
!= NULL
; ) {
968 _localopts_add(struct lostack
*losp
, char const *name
, struct var
*ovap
)
974 /* Propagate unrolling up the stack, as necessary */
975 while (!losp
->s_unroll
&& (losp
= losp
->s_up
) != NULL
)
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
))
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
);
994 vap
->v_value
= vap
->v_name
+ nl
;
995 memcpy(vap
->v_value
, ovap
->v_value
, vl
);
1002 _localopts_unroll(struct var
**vapp
)
1004 struct lostack
*save_los
;
1005 struct var
*x
, *vap
;
1011 save_los
= _localopts
;
1013 while (vap
!= NULL
) {
1016 vok_vset(x
->v_name
, x
->v_value
);
1019 _localopts
= save_los
;
1024 _var_oklook(enum okeys okey
)
1026 struct var_carrier vc
;
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
;
1035 if (!_var_lookup(&vc
)) {
1036 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1038 assert(vc
.vc_var
!= NULL
);
1043 rv
= vc
.vc_var
->v_value
;
1049 _var_okset(enum okeys okey
, uintptr_t val
)
1051 struct var_carrier vc
;
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
;
1060 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1066 _var_okclear(enum okeys okey
)
1068 struct var_carrier vc
;
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
;
1077 rv
= _var_clear(&vc
);
1083 _var_voklook(char const *vokey
)
1085 struct var_carrier vc
;
1089 _var_revlookup(&vc
, vokey
);
1091 if (!_var_lookup(&vc
)) {
1092 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1094 assert(vc
.vc_var
!= NULL
);
1099 rv
= vc
.vc_var
->v_value
;
1105 _var_vokset(char const *vokey
, uintptr_t val
)
1107 struct var_carrier vc
;
1111 _var_revlookup(&vc
, vokey
);
1113 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1119 _var_vokclear(char const *vokey
)
1121 struct var_carrier vc
;
1125 _var_revlookup(&vc
, vokey
);
1127 err
= !_var_clear(&vc
);
1133 _env_look(char const *envkey
, bool_t envonly
) /* TODO rather dummy yet!! */
1139 rv
= getenv(envkey
); /* TODO rework vars: cache, output a la mimetypes */
1141 rv
= _var_voklook(envkey
);
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
;
1153 char *nbuf
= NULL
/* CC happiness */, *rv
;
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
)))
1162 vc
.vc_vmap
= _var_map
+ okey
;
1163 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
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
);
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
))
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
)) {
1187 rv
= vc
.vc_var
->v_value
;
1193 rv
= (oxm
& OXM_PLAIN
) ? _var_oklook(okey
) : NULL
;
1195 if (oxm
& (OXM_H_P
| OXM_U_H_P
))
1200 #endif /* HAVE_SOCKETS */
1206 char const **argv
= v
;
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
;
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
);
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
,
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
);
1235 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1249 err
= !_var_set_env(ap
, FAL0
);
1261 if (!(err
= !(pstate
& PS_STARTED
)))
1262 err
= !_var_set_env(ap
, TRU1
);
1275 err
|= !_var_vokclear(*ap
++);
1286 if (!(err
= !(pstate
& PS_STARTED
))) {
1289 for (ap
= v
; *ap
!= NULL
; ++ap
) {
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
);
1300 bad
= !_var_vokclear(*ap
);
1303 unsetenv(*ap
) != 0 ||
1317 struct var_carrier vc
;
1318 sighandler_type sigint
;
1320 char *val
, **argv
= v
;
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"),
1337 if (vc
.vc_vmap
->vm_flags
& VM_RDONLY
) {
1338 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1346 if ((of
= Ftmp(NULL
, "vared", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1348 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
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);
1360 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
1366 off_t l
= fsize(nf
);
1369 base
= smalloc((size_t)l
+ 1);
1371 for (l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
1372 if (c
== '\n' || c
== '\r') {
1376 *val
= (char)(uc_i
)c
;
1382 if (!vok_vset(vc
.vc_name
, base
))
1388 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
1394 safe_signal(SIGINT
, sigint
);
1406 if (args
[0] == NULL
) {
1407 rv
= _ma_list(MA_NONE
);
1411 if (args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1413 n_err(_("Syntax is: define <name> {"));
1417 rv
= !_ma_define(args
[0], MA_NONE
);
1430 if (*args
== NULL
) {
1431 n_err(_("`undefine': required arguments are missing\n"));
1435 _ma_undefine(*args
, MA_NONE
);
1436 while (*++args
!= NULL
);
1448 char const *errs
, *name
;
1452 if (args
[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)) {
1453 errs
= _("Syntax is: call <%s>\n");
1458 if ((mp
= _ma_look(*args
, NULL
, MA_NONE
)) == NULL
) {
1459 errs
= _("Undefined macro called: \"%s\"\n");
1464 rv
= _ma_exec(mp
, NULL
);
1474 check_folder_hook(bool_t nmail
)
1479 struct var
**unroller
;
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
)
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
)
1496 snprintf(var
, len
, "folder-hook-+%s", x
);
1497 if ((cp
= vok_vlook(var
)) != NULL
)
1503 /* Plain *folder-hook* is our last try */
1504 if ((cp
= ok_vlook(folder_hook
)) == NULL
)
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
);
1515 pstate
&= ~PS_HOOK_MASK
;
1517 pstate
|= PS_HOOK_NEWMAIL
;
1521 unroller
= &_folder_hook_localopts
;
1523 rv
= (_ma_exec(mp
, unroller
) == 0);
1524 pstate
&= ~PS_HOOK_MASK
;
1537 int rv
= 1, i
, oqf
, nqf
;
1540 if (args
[0] == NULL
) {
1541 rv
= _ma_list(MA_ACC
);
1545 if (args
[1] && args
[1][0] == '{' && args
[1][1] == '\0') {
1546 if (args
[2] != NULL
) {
1547 n_err(_("Syntax is: account <name> {\n"));
1550 if (!asccasecmp(args
[0], ACCOUNT_NULL
)) {
1551 n_err(_("Error: \"%s\" is a reserved name\n"), ACCOUNT_NULL
);
1554 rv
= !_ma_define(args
[0], MA_ACC
);
1558 if (pstate
& PS_HOOK_MASK
) {
1559 n_err(_("Cannot change account from within a hook\n"));
1563 save_mbox_for_possible_quitstuff();
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]);
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
);
1584 account_name
= (mp
!= NULL
) ? mp
->ma_name
: NULL
;
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]);
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)
1598 check_folder_hook(FAL0
);
1599 if (i
> 0 && !ok_blook(emptystart
))
1601 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1602 restorequitflags(nqf
);
1611 c_unaccount(void *v
)
1617 if (*args
== NULL
) {
1618 n_err(_("`unaccount': required arguments are missing\n"));
1622 _ma_undefine(*args
, MA_ACC
);
1623 while (*++args
!= NULL
);
1631 c_localopts(void *v
)
1637 if (_localopts
== NULL
) {
1638 n_err(_("Cannot use `localopts' but from within a "
1639 "`define' or `account'\n"));
1643 _localopts
->s_unroll
= (boolify(*c
, UIZ_MAX
, FAL0
) > 0);
1651 temporary_localopts_free(void) /* XXX intermediate hack */
1653 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
;
1656 x
= temporary_localopts_store
;
1657 temporary_localopts_store
= NULL
;
1661 struct lostack
*losp
= x
->lo
;
1663 if (losp
->s_localopts
!= NULL
)
1664 _localopts_unroll(&losp
->s_localopts
);
1670 temporary_localopts_folder_hook_unroll(void) /* XXX intermediate hack */
1673 if (_folder_hook_localopts
!= NULL
) {
1674 void *save
= _localopts
;
1676 _localopts_unroll(&_folder_hook_localopts
);