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 boolean 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_BOOLEAN
= 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
);
203 /* _ma_define() returns error for faulty definitions and already existing
204 * names, _ma_undefine() returns error if a named thing doesn't exist */
205 static bool_t
_ma_define(char const *name
, enum ma_flags mafl
);
206 static bool_t
_ma_undefine(char const *name
, enum ma_flags mafl
);
207 static void _ma_freelines(struct mline
*lp
);
209 /* Update replay-log */
210 static void _localopts_add(struct lostack
*losp
, char const *name
,
212 static void _localopts_unroll(struct var
**vapp
);
215 _var_vcopy(char const *str
)
224 len
= strlen(str
) +1;
226 memcpy(news
, str
, len
);
242 _var_check_specials(enum okeys okey
, bool_t enable
, char **val
)
260 case ok_b_skipemptybody
:
264 flag
= (enable
&& !(options
& OPT_VERB
))
265 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
268 ok
= (val
!= NULL
&& var_folder_updated(*val
, &cp
));
269 if (ok
&& cp
!= NULL
) {
271 /* It's smalloc()ed, but ensure we don't leak */
280 case ok_v_line_editor_cursor_right
:
281 if ((ok
= (val
!= NULL
&& *val
!= NULL
))) {
282 /* Set with no value? TODO very guly */
283 if (*(cp
= *val
) != '\0') {
287 c
= n_shell_expand_escape(&x
, FAL0
);
291 } while (*x
!= '\0');
312 _var_canonify(char const *vn
)
315 if (!upperchar(*vn
)) {
318 for (vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
320 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
327 _var_revlookup(struct var_carrier
*vcp
, char const *name
)
330 struct var_map
const *vmp
;
333 vcp
->vc_name
= name
= _var_canonify(name
);
334 vcp
->vc_hash
= hash
= MA_NAME2HASH(name
);
336 for (i
= hash
% _VAR_REV_PRIME
, j
= 0; j
<= _VAR_REV_LONGEST
; ++j
) {
337 ui32_t x
= _var_revmap
[i
];
338 if (x
== _VAR_REV_ILL
)
341 if (vmp
->vm_hash
== hash
&& !strcmp(_var_keydat
+ vmp
->vm_keyoff
, name
)) {
343 vcp
->vc_okey
= (enum okeys
)x
;
346 if (++i
== _VAR_REV_PRIME
) {
347 #ifdef _VAR_REV_WRAPAROUND
358 return (vcp
!= NULL
);
362 _var_lookup(struct var_carrier
*vcp
)
364 struct var
**vap
, *lvp
, *vp
;
367 /* XXX _So_ unlikely that it should be checked if normal lookup fails! */
368 if (UNLIKELY(vcp
->vc_vmap
!= NULL
&&
369 (vcp
->vc_vmap
->vm_flags
& VM_VIRTUAL
) != 0)) {
370 struct var_virtual
const *vvp
;
372 for (vvp
= _var_virtuals
;
373 PTRCMP(vvp
, <, _var_virtuals
+ NELEM(_var_virtuals
)); ++vvp
)
374 if (vvp
->vv_okey
== vcp
->vc_okey
) {
375 vp
= UNCONST(vvp
->vv_var
);
381 vap
= _vars
+ (vcp
->vc_prime
= MA_HASH2PRIME(vcp
->vc_hash
));
383 for (lvp
= NULL
, vp
= *vap
; vp
!= NULL
; lvp
= vp
, vp
= vp
->v_link
)
384 if (!strcmp(vp
->v_name
, vcp
->vc_name
)) {
385 /* Relink as head, hope it "sorts on usage" over time.
386 * _var_clear() relies on this behaviour */
388 lvp
->v_link
= vp
->v_link
;
402 _var_broadway(struct var_show
*vsp
, char const *name
)
407 memset(vsp
, 0, sizeof *vsp
);
409 vsp
->vs_isasm
= !_var_revlookup(&vsp
->vs_vc
, name
);
411 if ((vsp
->vs_isset
= rv
= _var_lookup(&vsp
->vs_vc
))) {
412 vsp
->vs_value
= vsp
->vs_vc
.vc_var
->v_value
;
413 vsp
->vs_isenv
= FAL0
;
415 vsp
->vs_isenv
= ((vsp
->vs_value
= getenv(vsp
->vs_vc
.vc_name
)) != NULL
);
422 _var_set(struct var_carrier
*vcp
, char const *value
)
430 ok
= _var_clear(vcp
);
436 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_RDONLY
)) {
437 n_err(_("Variable readonly: \"%s\"\n"), vcp
->vc_name
);
442 /* Don't care what happens later on, store this in the unroll list */
443 if (_localopts
!= NULL
)
444 _localopts_add(_localopts
, vcp
->vc_name
, vcp
->vc_var
);
446 if ((vp
= vcp
->vc_var
) == NULL
) {
447 size_t l
= strlen(vcp
->vc_name
) + 1;
450 vp
= smalloc(sizeof(*vp
) - VFIELD_SIZEOF(struct var
, v_name
) + l
);
451 vp
->v_link
= _vars
[vcp
->vc_prime
];
452 _vars
[vcp
->vc_prime
] = vp
;
453 memcpy(vp
->v_name
, vcp
->vc_name
, l
);
458 if (vcp
->vc_vmap
== NULL
)
459 vp
->v_value
= _var_vcopy(value
);
461 /* Via `set' etc. the user may give even boolean options non-boolean
462 * values, ignore that and force boolean xxx error log? */
463 if (vcp
->vc_vmap
->vm_flags
& VM_BOOLEAN
)
465 vp
->v_value
= _var_vcopy(value
);
467 /* Check if update allowed XXX wasteful on error! */
468 if ((vcp
->vc_vmap
->vm_flags
& VM_SPECIAL
) &&
469 !(ok
= _var_check_specials(vcp
->vc_okey
, TRU1
, &vp
->v_value
))) {
470 char *cp
= vp
->v_value
;
484 _var_clear(struct var_carrier
*vcp
)
489 if (!_var_lookup(vcp
)) {
490 if (!(pstate
& PS_IN_LOAD
) && (options
& OPT_D_V
))
491 n_err(_("Variable undefined: \"%s\"\n"), vcp
->vc_name
);
492 } else if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_RDONLY
)) {
493 n_err(_("Variable readonly: \"%s\"\n"), vcp
->vc_name
);
496 if (_localopts
!= NULL
)
497 _localopts_add(_localopts
, vcp
->vc_name
, vcp
->vc_var
);
499 /* Always listhead after _var_lookup() */
500 _vars
[vcp
->vc_prime
] = _vars
[vcp
->vc_prime
]->v_link
;
501 _var_vfree(vcp
->vc_var
->v_value
);
505 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_SPECIAL
))
506 ok
= _var_check_specials(vcp
->vc_okey
, FAL0
, NULL
);
519 char const **vacp
, **cap
;
522 if ((fp
= Ftmp(NULL
, "listvars", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
524 n_perr(_("tmpfile"), 0);
528 for (no
= i
= 0; i
< MA_PRIME
; ++i
)
529 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
531 no
+= NELEM(_var_virtuals
);
533 vacp
= salloc(no
* sizeof(*vacp
));
535 for (cap
= vacp
, i
= 0; i
< MA_PRIME
; ++i
)
536 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
538 for (i
= 0; i
< NELEM(_var_virtuals
); ++i
)
539 *cap
++ = _var_virtuals
[i
].vv_var
->v_name
;
542 qsort(vacp
, no
, sizeof *vacp
, &__var_list_all_cmp
);
544 i
= (ok_blook(bsdcompat
) || ok_blook(bsdset
));
545 for (cap
= vacp
; no
!= 0; ++cap
, --no
) {
546 char const *asmis
, *fmt
;
548 if (!_var_broadway(&vs
, *cap
))
550 if (vs
.vs_value
== NULL
)
553 asmis
= !(options
& OPT_D_VV
) ? ""
554 : vs
.vs_isasm
? "*" : " ";
558 if (vs
.vs_vc
.vc_vmap
!= NULL
&&
559 (vs
.vs_vc
.vc_vmap
->vm_flags
& VM_BOOLEAN
))
562 fmt
= "%sset %s=\"%s\"\n";
563 if (*vs
.vs_value
!= '\0')
564 vs
.vs_value
= __var_simple_quote(vs
.vs_value
);
567 /* Shall a code checker complain on that, i'm in holiday */
568 fprintf(fp
, fmt
, asmis
, *cap
, vs
.vs_value
);
571 page_or_print(fp
, PTR2SIZE(cap
- vacp
));
578 __var_list_all_cmp(void const *s1
, void const *s2
)
583 rv
= strcmp(*(char**)UNCONST(s1
), *(char**)UNCONST(s2
));
589 __var_simple_quote(char const *cp
) /* TODO "unite" with string_quote(), etc.. */
597 for (i
= 0, cp_base
= cp
; (c
= *cp
) != '\0'; ++i
, ++cp
)
602 for (esc
= FAL0
, i
= 0, cp
= cp_base
; (c
= *cp
) != '\0'; rv
[i
++] = c
, ++cp
) {
616 _var_set_env(char **ap
, bool_t issetenv
)
618 char *cp
, *cp2
, *varbuf
, c
;
622 for (; *ap
!= NULL
; ++ap
) {
625 cp2
= varbuf
= ac_alloc(strlen(cp
) +1);
626 for (; (c
= *cp
) != '=' && c
!= '\0'; ++cp
)
634 n_err(_("Non-null variable name required\n"));
639 if (varbuf
[0] == 'n' && varbuf
[1] == 'o') {
640 char const *k
= varbuf
+ 2;
642 if (issetenv
&& (!strcmp(k
, "HOME") || /* TODO generic */
643 !strcmp(k
, "USER") || !strcmp(k
, "TMPDIR"))) {/* TODO approach */
644 if (options
& OPT_D_V
)
645 n_err(_("Cannot `unsetenv' \"%s\"\n"), k
);
650 errs
+= !_var_vokclear(k
);
653 errs
+= (unsetenv(k
) != 0);
659 errs
+= !_var_vokset(varbuf
, (uintptr_t)cp
);
662 static char const *cp_buf
[3];
663 char const **pl
, **pe
;
665 if (!strcmp(varbuf
, "HOME")) /* TODO generic */
666 pl
= cp_buf
+ 0, pe
= &homedir
;
667 else if (!strcmp(varbuf
, "USER")) /* TODO approach */
668 pl
= cp_buf
+ 1, pe
= &myname
;
669 else if (!strcmp(varbuf
, "TMPDIR")) /* TODO also here */
670 pl
= cp_buf
+ 2, pe
= &tempdir
;
676 *pe
= *pl
= sstrdup(cp
);
680 errs
+= (setenv(varbuf
, cp
, 1) != 0);
695 _is_closing_angle(char const *cp
)
700 while (spacechar(*cp
))
704 while (spacechar(*cp
))
712 static struct macro
*
713 _ma_look(char const *name
, struct macro
*data
, enum ma_flags mafl
)
715 enum ma_flags save_mafl
;
717 struct macro
*lmp
, *mp
;
721 mafl
&= MA_TYPE_MASK
;
722 h
= MA_NAME2HASH(name
);
723 h
= MA_HASH2PRIME(h
);
725 for (lmp
= NULL
, mp
= _macros
[h
]; mp
!= NULL
; lmp
= mp
, mp
= mp
->ma_next
) {
726 if ((mp
->ma_flags
& MA_TYPE_MASK
) == mafl
&& !strcmp(mp
->ma_name
, name
)) {
727 if (save_mafl
& MA_UNDEF
) {
729 _macros
[h
] = mp
->ma_next
;
731 lmp
->ma_next
= mp
->ma_next
;
733 /* TODO it should also be possible to delete running macros */
734 if ((mafl
& MA_ACC
) &&
735 account_name
!= NULL
&& !strcmp(account_name
, name
)) {
736 mp
->ma_flags
|= MA_DELETED
;
737 n_err(_("Delayed deletion of active account \"%s\"\n"), name
);
739 _ma_freelines(mp
->ma_contents
);
742 mp
= (struct macro
*)-1;
750 data
->ma_next
= _macros
[h
];
760 _ma_exec(struct macro
const *mp
, struct var
**unroller
)
764 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
; /* FIXME hack (sigman+) */
765 struct mline
const *lp
;
769 los
.s_up
= _localopts
;
770 los
.s_mac
= UNCONST(mp
);
771 los
.s_localopts
= NULL
;
775 x
= salloc(sizeof *x
); /* FIXME intermediate hack (signal man+) */
776 x
->up
= temporary_localopts_store
;
778 temporary_localopts_store
= x
;
780 buf
= ac_alloc(mp
->ma_maxlen
+1);
781 for (lp
= mp
->ma_contents
; lp
; lp
= lp
->l_next
) {
782 memcpy(buf
, lp
->l_line
, lp
->l_length
+1);
783 rv
|= execute(buf
, lp
->l_length
); /* XXX break if != 0 ? */
787 temporary_localopts_store
= x
->up
; /* FIXME intermediate hack */
789 _localopts
= los
.s_up
;
790 if (unroller
== NULL
) {
791 if (los
.s_localopts
!= NULL
)
792 _localopts_unroll(&los
.s_localopts
);
794 *unroller
= los
.s_localopts
;
800 _ma_list(enum ma_flags mafl
)
809 if ((fp
= Ftmp(NULL
, "listmacs", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
811 n_perr(_("tmpfile"), 0);
816 mafl
&= MA_TYPE_MASK
;
817 typestr
= (mafl
& MA_ACC
) ? "account" : "define";
819 for (ti
= mc
= 0; ti
< MA_PRIME
; ++ti
)
820 for (mq
= _macros
[ti
]; mq
; mq
= mq
->ma_next
)
821 if ((mq
->ma_flags
& MA_TYPE_MASK
) == mafl
) {
824 fprintf(fp
, "%s %s {\n", typestr
, mq
->ma_name
);
825 for (lp
= mq
->ma_contents
; lp
!= NULL
; lp
= lp
->l_next
) {
826 for (i
= lp
->l_leadspaces
; i
> 0; --i
)
828 fputs(lp
->l_line
, fp
);
834 page_or_print(fp
, 0);
836 mc
= (ui32_t
)ferror(fp
);
844 _ma_define(char const *name
, enum ma_flags mafl
)
848 struct mline
*lp
, *lst
= NULL
, *lnd
= NULL
;
849 char *linebuf
= NULL
, *cp
;
851 ui32_t maxlen
= 0, leaspc
;
852 union {int i
; ui32_t ui
;} n
;
855 mp
= scalloc(1, sizeof *mp
);
856 mp
->ma_name
= sstrdup(name
);
860 n
.i
= readline_input("", TRU1
, &linebuf
, &linesize
, NULL
);
864 n_err(_("Unterminated %s definition: \"%s\"\n"),
865 (mafl
& MA_ACC
? "account" : "macro"), mp
->ma_name
);
866 if ((pstate
& PS_IN_LOAD
) == PS_SOURCING
)
870 if (_is_closing_angle(linebuf
))
873 /* Trim WS xxx we count tabs as one space here */
874 for (cp
= linebuf
, leaspc
= 0; n
.ui
> 0 && whitechar(*cp
); ++cp
, --n
.ui
)
876 leaspc
= (leaspc
+ 8) & ~7;
881 for (; whitechar(cp
[n
.ui
- 1]); --n
.ui
)
885 maxlen
= MAX(maxlen
, n
.ui
);
887 lp
= scalloc(1, sizeof(*lp
) - VFIELD_SIZEOF(struct mline
, l_line
) + n
.ui
);
888 memcpy(lp
->l_line
, cp
, n
.ui
);
889 lp
->l_length
= --n
.ui
;
890 lp
->l_leadspaces
= leaspc
;
898 mp
->ma_contents
= lst
;
899 mp
->ma_maxlen
= maxlen
;
901 if (_ma_look(mp
->ma_name
, mp
, mafl
) != NULL
) {
902 n_err(_("A %s named \"%s\" already exists\n"),
903 (mafl
& MA_ACC
? "account" : "macro"), mp
->ma_name
);
904 lst
= mp
->ma_contents
;
924 _ma_undefine(char const *name
, enum ma_flags mafl
)
932 if (LIKELY(name
[0] != '*' || name
[1] != '\0')) {
933 if ((mp
= _ma_look(name
, NULL
, mafl
| MA_UNDEF
)) == NULL
) {
934 n_err(_("%s \"%s\" is not defined\n"),
935 (mafl
& MA_ACC
? "Account" : "Macro"), name
);
939 struct macro
**mpp
, *lmp
;
941 for (mpp
= _macros
; PTRCMP(mpp
, <, _macros
+ NELEM(_macros
)); ++mpp
)
942 for (lmp
= NULL
, mp
= *mpp
; mp
!= NULL
;) {
943 if ((mp
->ma_flags
& MA_TYPE_MASK
) == mafl
) {
944 /* xxx Expensive but rare: be simple */
945 _ma_look(mp
->ma_name
, NULL
, mafl
| MA_UNDEF
);
946 mp
= (lmp
== NULL
) ? *mpp
: lmp
->ma_next
;
958 _ma_freelines(struct mline
*lp
)
963 for (lq
= NULL
; lp
!= NULL
; ) {
975 _localopts_add(struct lostack
*losp
, char const *name
, struct var
*ovap
)
981 /* Propagate unrolling up the stack, as necessary */
982 while (!losp
->s_unroll
&& (losp
= losp
->s_up
) != NULL
)
987 /* We've found a level that wants to unroll; check wether it does it yet */
988 for (vap
= losp
->s_localopts
; vap
!= NULL
; vap
= vap
->v_link
)
989 if (!strcmp(vap
->v_name
, name
))
992 nl
= strlen(name
) + 1;
993 vl
= (ovap
!= NULL
) ? strlen(ovap
->v_value
) + 1 : 0;
994 vap
= smalloc(sizeof(*vap
) - VFIELD_SIZEOF(struct var
, v_name
) + nl
+ vl
);
995 vap
->v_link
= losp
->s_localopts
;
996 losp
->s_localopts
= vap
;
997 memcpy(vap
->v_name
, name
, nl
);
1001 vap
->v_value
= vap
->v_name
+ nl
;
1002 memcpy(vap
->v_value
, ovap
->v_value
, vl
);
1009 _localopts_unroll(struct var
**vapp
)
1011 struct lostack
*save_los
;
1012 struct var
*x
, *vap
;
1018 save_los
= _localopts
;
1020 while (vap
!= NULL
) {
1023 vok_vset(x
->v_name
, x
->v_value
);
1026 _localopts
= save_los
;
1031 _var_oklook(enum okeys okey
)
1033 struct var_carrier vc
;
1037 vc
.vc_vmap
= _var_map
+ okey
;
1038 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1039 vc
.vc_hash
= _var_map
[okey
].vm_hash
;
1042 if (!_var_lookup(&vc
)) {
1043 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1045 assert(vc
.vc_var
!= NULL
);
1050 rv
= vc
.vc_var
->v_value
;
1056 _var_okset(enum okeys okey
, uintptr_t val
)
1058 struct var_carrier vc
;
1062 vc
.vc_vmap
= _var_map
+ okey
;
1063 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1064 vc
.vc_hash
= _var_map
[okey
].vm_hash
;
1067 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1073 _var_okclear(enum okeys okey
)
1075 struct var_carrier vc
;
1079 vc
.vc_vmap
= _var_map
+ okey
;
1080 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1081 vc
.vc_hash
= _var_map
[okey
].vm_hash
;
1084 rv
= _var_clear(&vc
);
1090 _var_voklook(char const *vokey
)
1092 struct var_carrier vc
;
1096 _var_revlookup(&vc
, vokey
);
1098 if (!_var_lookup(&vc
)) {
1099 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1101 assert(vc
.vc_var
!= NULL
);
1106 rv
= vc
.vc_var
->v_value
;
1112 _var_vokset(char const *vokey
, uintptr_t val
)
1114 struct var_carrier vc
;
1118 _var_revlookup(&vc
, vokey
);
1120 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1126 _var_vokclear(char const *vokey
)
1128 struct var_carrier vc
;
1132 _var_revlookup(&vc
, vokey
);
1134 err
= !_var_clear(&vc
);
1140 _env_look(char const *envkey
, bool_t envonly
) /* TODO rather dummy yet!! */
1146 rv
= getenv(envkey
); /* TODO rework vars: cache, output a la mimetypes */
1148 rv
= _var_voklook(envkey
);
1155 _var_xoklook(enum okeys okey
, struct url
const *urlp
, enum okey_xlook_mode oxm
)
1157 struct var_carrier vc
;
1158 struct str
const *us
;
1160 char *nbuf
= NULL
/* CC happiness */, *rv
;
1163 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1165 /* For simplicity: allow this case too */
1166 if (!(oxm
& (OXM_H_P
| OXM_U_H_P
)))
1169 vc
.vc_vmap
= _var_map
+ okey
;
1170 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1173 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1174 nlen
= strlen(vc
.vc_name
);
1175 nbuf
= ac_alloc(nlen
+ 1 + us
->l
+1);
1176 memcpy(nbuf
, vc
.vc_name
, nlen
);
1179 /* One of .url_u_h_p and .url_h_p we test in here */
1180 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1181 vc
.vc_name
= _var_canonify(nbuf
);
1182 vc
.vc_hash
= MA_NAME2HASH(vc
.vc_name
);
1183 if (_var_lookup(&vc
))
1187 if (oxm
& OXM_H_P
) {
1188 us
= &urlp
->url_h_p
;
1189 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1190 vc
.vc_name
= _var_canonify(nbuf
);
1191 vc
.vc_hash
= MA_NAME2HASH(vc
.vc_name
);
1192 if (_var_lookup(&vc
)) {
1194 rv
= vc
.vc_var
->v_value
;
1200 rv
= (oxm
& OXM_PLAIN
) ? _var_oklook(okey
) : NULL
;
1202 if (oxm
& (OXM_H_P
| OXM_U_H_P
))
1207 #endif /* HAVE_SOCKETS */
1213 char const **argv
= v
;
1218 else for (; *argv
!= NULL
; ++argv
) {
1219 _var_broadway(&vs
, *argv
);
1220 if (vs
.vs_value
== NULL
)
1221 vs
.vs_value
= "NULL";
1223 if (vs
.vs_vc
.vc_vmap
!= NULL
) {
1224 ui16_t f
= vs
.vs_vc
.vc_vmap
->vm_flags
;
1227 printf(_("\"%s\": (%d) boolean%s%s: set=%d (ENVIRON=%d)\n"),
1228 vs
.vs_vc
.vc_name
, vs
.vs_vc
.vc_okey
,
1229 (f
& VM_RDONLY
? ", read-only" : ""),
1230 (f
& VM_VIRTUAL
? ", virtual" : ""), vs
.vs_isset
, vs
.vs_isenv
);
1232 printf(_("\"%s\": (%d) value%s%s: set=%d (ENVIRON=%d) value<%s>\n"),
1233 vs
.vs_vc
.vc_name
, vs
.vs_vc
.vc_okey
,
1234 (f
& VM_RDONLY
? ", read-only" : ""),
1235 (f
& VM_VIRTUAL
? ", virtual" : ""), vs
.vs_isset
, vs
.vs_isenv
,
1238 printf("\"%s\": (assembled): set=%d (ENVIRON=%d) value<%s>\n",
1239 vs
.vs_vc
.vc_name
, vs
.vs_isset
, vs
.vs_isenv
, vs
.vs_value
);
1242 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1256 err
= !_var_set_env(ap
, FAL0
);
1268 if (!(err
= !(pstate
& PS_STARTED
)))
1269 err
= !_var_set_env(ap
, TRU1
);
1282 err
|= !_var_vokclear(*ap
++);
1293 if (!(err
= !(pstate
& PS_STARTED
))) {
1296 for (ap
= v
; *ap
!= NULL
; ++ap
) {
1299 if (!strcmp(*ap
, "HOME") || !strcmp(*ap
, "USER") || /* TODO generic */
1300 !strcmp(*ap
, "TMPDIR")) { /* TODO approach */
1301 if (options
& OPT_D_V
)
1302 n_err(_("Cannot `unsetenv' \"%s\"\n"), *ap
);
1307 bad
= !_var_vokclear(*ap
);
1310 unsetenv(*ap
) != 0 ||
1324 struct var_carrier vc
;
1325 sighandler_type sigint
;
1327 char *val
, **argv
= v
;
1331 sigint
= safe_signal(SIGINT
, SIG_IGN
);
1333 while (*argv
!= NULL
) {
1334 memset(&vc
, 0, sizeof vc
);
1336 _var_revlookup(&vc
, *argv
++);
1338 if (vc
.vc_vmap
!= NULL
) {
1339 if (vc
.vc_vmap
->vm_flags
& VM_BOOLEAN
) {
1340 n_err(_("`varedit': can't edit boolean variable \"%s\"\n"),
1344 if (vc
.vc_vmap
->vm_flags
& VM_RDONLY
) {
1345 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1353 if ((of
= Ftmp(NULL
, "vared", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1355 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1358 } else if (vc
.vc_var
!= NULL
&& *(val
= vc
.vc_var
->v_value
) != '\0' &&
1359 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)) {
1360 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1367 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
1373 off_t l
= fsize(nf
);
1376 base
= smalloc((size_t)l
+ 1);
1378 for (l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
1379 if (c
== '\n' || c
== '\r') {
1383 *val
= (char)(uc_i
)c
;
1389 if (!vok_vset(vc
.vc_name
, base
))
1395 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
1401 safe_signal(SIGINT
, sigint
);
1413 if (args
[0] == NULL
) {
1414 rv
= _ma_list(MA_NONE
);
1418 if (args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1420 n_err(_("Syntax is: define <name> {"));
1424 rv
= !_ma_define(args
[0], MA_NONE
);
1440 rv
|= !_ma_undefine(*args
, MA_NONE
);
1441 while (*++args
!= NULL
);
1451 char const *errs
, *name
;
1455 if (args
[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)) {
1456 errs
= _("Syntax is: call <%s>\n");
1461 if ((mp
= _ma_look(*args
, NULL
, MA_NONE
)) == NULL
) {
1462 errs
= _("Undefined macro called: \"%s\"\n");
1467 rv
= _ma_exec(mp
, NULL
);
1477 check_folder_hook(bool_t nmail
)
1482 struct var
**unroller
;
1486 var
= ac_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1488 /* First try the fully resolved path */
1489 snprintf(var
, len
, "folder-hook-%s", mailname
);
1490 if ((cp
= vok_vlook(var
)) != NULL
)
1493 /* If we are under *folder*, try the usual +NAME syntax, too */
1494 if (displayname
[0] == '+') {
1495 char *x
= mailname
+ len
;
1497 for (; x
> mailname
; --x
)
1499 snprintf(var
, len
, "folder-hook-+%s", x
);
1500 if ((cp
= vok_vlook(var
)) != NULL
)
1506 /* Plain *folder-hook* is our last try */
1507 if ((cp
= ok_vlook(folder_hook
)) == NULL
)
1511 if ((mp
= _ma_look(cp
, NULL
, MA_NONE
)) == NULL
) {
1512 n_err(_("Cannot call *folder-hook* for \"%s\": "
1513 "macro \"%s\" does not exist\n"), displayname
, cp
);
1518 pstate
&= ~PS_HOOK_MASK
;
1520 pstate
|= PS_HOOK_NEWMAIL
;
1524 unroller
= &_folder_hook_localopts
;
1526 rv
= (_ma_exec(mp
, unroller
) == 0);
1527 pstate
&= ~PS_HOOK_MASK
;
1540 int rv
= 1, i
, oqf
, nqf
;
1543 if (args
[0] == NULL
) {
1544 rv
= _ma_list(MA_ACC
);
1548 if (args
[1] && args
[1][0] == '{' && args
[1][1] == '\0') {
1549 if (args
[2] != NULL
) {
1550 n_err(_("Syntax is: account <name> {\n"));
1553 if (!asccasecmp(args
[0], ACCOUNT_NULL
)) {
1554 n_err(_("Error: \"%s\" is a reserved name\n"), ACCOUNT_NULL
);
1557 rv
= !_ma_define(args
[0], MA_ACC
);
1561 if (pstate
& PS_HOOK_MASK
) {
1562 n_err(_("Cannot change account from within a hook\n"));
1566 save_mbox_for_possible_quitstuff();
1569 if (asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1570 (mp
= _ma_look(args
[0], NULL
, MA_ACC
)) == NULL
) {
1571 n_err(_("Account \"%s\" does not exist\n"), args
[0]);
1575 oqf
= savequitflags();
1577 if (_acc_curr
!= NULL
) {
1578 if (_acc_curr
->ma_localopts
!= NULL
)
1579 _localopts_unroll(&_acc_curr
->ma_localopts
);
1580 if (_acc_curr
->ma_flags
& MA_DELETED
) { /* xxx can be made generic? */
1581 _ma_freelines(_acc_curr
->ma_contents
);
1582 free(_acc_curr
->ma_name
);
1587 account_name
= (mp
!= NULL
) ? mp
->ma_name
: NULL
;
1590 if (mp
!= NULL
&& _ma_exec(mp
, &mp
->ma_localopts
) == CBAD
) {
1591 /* XXX account switch incomplete, unroll? */
1592 n_err(_("Switching to account \"%s\" failed\n"), args
[0]);
1596 if ((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
) {
1597 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1598 restorequitflags(oqf
);
1599 if ((i
= setfile("%", 0)) < 0)
1601 check_folder_hook(FAL0
);
1602 if (i
> 0 && !ok_blook(emptystart
))
1604 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1605 restorequitflags(nqf
);
1614 c_unaccount(void *v
)
1623 rv
|= !_ma_undefine(*args
, MA_ACC
);
1624 while (*++args
!= NULL
);
1630 c_localopts(void *v
)
1636 if (_localopts
== NULL
) {
1637 n_err(_("Cannot use `localopts' but from within a "
1638 "`define' or `account'\n"));
1642 _localopts
->s_unroll
= (boolify(*c
, UIZ_MAX
, FAL0
) > 0);
1650 temporary_localopts_free(void) /* XXX intermediate hack */
1652 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
;
1655 x
= temporary_localopts_store
;
1656 temporary_localopts_store
= NULL
;
1660 struct lostack
*losp
= x
->lo
;
1662 if (losp
->s_localopts
!= NULL
)
1663 _localopts_unroll(&losp
->s_localopts
);
1669 temporary_localopts_folder_hook_unroll(void) /* XXX intermediate hack */
1672 if (_folder_hook_localopts
!= NULL
) {
1673 void *save
= _localopts
;
1675 _localopts_unroll(&_folder_hook_localopts
);