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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #define n_FILE accmacvar
42 #ifndef HAVE_AMALGAMATION
43 # define _ACCMACVAR_SOURCE /* For _features[] */
48 * HOWTO add a new non-dynamic binary or value option:
49 * - add an entry to nail.h:enum okeys
50 * - run create-okey-map.pl
54 /* Note: changing the hash function must be reflected in create-okey-map.pl */
55 #define MA_PRIME HSHSIZE
56 #define MA_NAME2HASH(N) torek_hash(N)
57 #define MA_HASH2PRIME(H) ((H) % MA_PRIME)
62 MA_TYPE_MASK
= MA_ACC
,
63 MA_UNDEF
= 1<<1, /* Unlink after lookup */
64 MA_DELETED
= 1<<13 /* Only for _acc_curr: deleted while active */
69 VM_BINARY
= 1<<0, /* ok_b_* */
70 VM_RDONLY
= 1<<1, /* May not be set by user */
71 VM_SPECIAL
= 1<<2, /* Wants _var_check_specials() evaluation */
72 VM_VIRTUAL
= 1<<3 /* "Stateless": no var* -- implies VM_RDONLY */
76 struct macro
*ma_next
;
78 struct mline
*ma_contents
;
79 size_t ma_maxlen
; /* Maximum line length */
80 enum ma_flags ma_flags
;
81 struct var
*ma_localopts
; /* `account' unroll list, for `localopts' */
87 char l_line
[VFIELD_SIZE(0)];
93 char v_name
[VFIELD_SIZE(0)];
98 struct var
const *vv_var
;
104 ui16_t vm_flags
; /* var_map_flags bits */
112 struct var_map
const *vc_vmap
;
117 struct var_carrier vs_vc
; /* _var_revlookup() */
118 char const *vs_value
; /* Value (from wherever it came) or NULL */
119 bool_t vs_isset
; /* Managed by us and existent */
120 bool_t vs_isenv
; /* Set, but managed by environ */
125 struct lostack
*s_up
; /* Outer context */
126 struct macro
*s_mac
; /* Context (`account' or `define') */
127 struct var
*s_localopts
;
128 bool_t s_unroll
; /* Unroll? */
131 /* Include the constant create-okey-map.pl output */
135 static struct macro
*_acc_curr
; /* Currently active account */
136 static struct lostack
*_localopts
; /* Currently executing macro unroll list */
137 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
138 * TODO today via a static lostack, it should be a field in mailbox, once that
139 * TODO is a real multi-instance object */
140 static struct var
*_folder_hook_localopts
;
142 /* TODO once we have a dynamically sized hashtable we could unite _macros and
143 * TODO _variables into a single hashtable, stripping down fun interface;
144 * TODO also, setting and clearing a variable can be easily joined */
145 static struct var
*_vars
[MA_PRIME
]; /* TODO dynamically spaced */
146 static struct macro
*_macros
[MA_PRIME
]; /* TODO dynamically spaced */
148 /* Special cased value string allocation */
149 static char * _var_vcopy(char const *str
);
150 static void _var_vfree(char *cp
);
152 /* Check for special housekeeping. */
153 static bool_t
_var_check_specials(enum okeys okey
, bool_t enable
,
156 /* If a variable name begins with a lowercase-character and contains at
157 * least one '@', it is converted to all-lowercase. This is necessary
158 * for lookups of names based on email addresses.
159 * Following the standard, only the part following the last '@' should
160 * be lower-cased, but practice has established otherwise here.
161 * Return value may have been placed in string dope (salloc()) */
162 static char const * _var_canonify(char const *vn
);
164 /* Try to reverse lookup an option name to an enum okeys mapping.
165 * Updates vcp.vc_name and vcp.vc_hash; vcp.vc_vmap is NULL if none found */
166 static bool_t
_var_revlookup(struct var_carrier
*vcp
, char const *name
);
168 /* Lookup a variable from vcp.vc_(vmap|name|hash), return wether it was found.
169 * Sets vcp.vc_prime; vcp.vc_var is NULL if not found */
170 static bool_t
_var_lookup(struct var_carrier
*vcp
);
172 /* Completely fill vsp with data for name, return wether it was set as an
173 * internal variable (it may still have been set as an environment variable) */
174 static bool_t
_var_broadway(struct var_show
*vsp
, char const *name
);
176 /* Set variable from vcp.vc_(vmap|name|hash), return success */
177 static bool_t
_var_set(struct var_carrier
*vcp
, char const *value
);
179 /* Clear variable from vcp.vc_(vmap|name|hash); sets vcp.vc_var to NULL,
181 static bool_t
_var_clear(struct var_carrier
*vcp
);
183 /* List all variables */
184 static void _var_list_all(void);
186 static int __var_list_all_cmp(void const *s1
, void const *s2
);
187 static char * __var_simple_quote(char const *cp
);
189 /* Shared c_set() and c_setenv() impl, return success */
190 static bool_t
_var_set_env(char **ap
, bool_t issetenv
);
192 /* Does cp consist solely of WS and a } */
193 static bool_t
_is_closing_angle(char const *cp
);
195 /* Lookup for macros/accounts */
196 static struct macro
*_ma_look(char const *name
, struct macro
*data
,
199 /* Walk all lines of a macro and execute() them */
200 static int _ma_exec(struct macro
const *mp
, struct var
**unroller
);
202 /* User display helpers */
203 static int _ma_list(enum ma_flags mafl
);
206 static bool_t
_ma_define(char const *name
, enum ma_flags mafl
);
207 static void _ma_undefine(char const *name
, enum ma_flags mafl
);
208 static void _ma_freelines(struct mline
*lp
);
210 /* Update replay-log */
211 static void _localopts_add(struct lostack
*losp
, char const *name
,
213 static void _localopts_unroll(struct var
**vapp
);
216 _var_vcopy(char const *str
)
225 len
= strlen(str
) +1;
227 memcpy(news
, str
, len
);
243 _var_check_specials(enum okeys okey
, bool_t enable
, char **val
)
261 case ok_b_skipemptybody
:
265 flag
= (enable
&& !(options
& OPT_VERB
))
266 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
269 ok
= (val
!= NULL
&& var_folder_updated(*val
, &cp
));
270 if (ok
&& cp
!= NULL
) {
272 /* It's smalloc()ed, but ensure we don't leak */
281 case ok_v_line_editor_cursor_right
:
282 if ((ok
= (val
!= NULL
&& *val
!= NULL
))) {
283 /* Set with no value? TODO very guly */
284 if (*(cp
= *val
) != '\0') {
288 c
= expand_shell_escape(&x
, FAL0
);
292 } while (*x
!= '\0');
313 _var_canonify(char const *vn
)
316 if (!upperchar(*vn
)) {
319 for (vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
321 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
328 _var_revlookup(struct var_carrier
*vcp
, char const *name
)
331 struct var_map
const *vmp
;
334 vcp
->vc_name
= name
= _var_canonify(name
);
335 vcp
->vc_hash
= hash
= MA_NAME2HASH(name
);
337 for (i
= hash
% _VAR_REV_PRIME
, j
= 0; j
<= _VAR_REV_LONGEST
; ++j
) {
338 ui32_t x
= _var_revmap
[i
];
339 if (x
== _VAR_REV_ILL
)
342 if (vmp
->vm_hash
== hash
&& !strcmp(_var_keydat
+ vmp
->vm_keyoff
, name
)) {
344 vcp
->vc_okey
= (enum okeys
)x
;
347 if (++i
== _VAR_REV_PRIME
) {
348 #ifdef _VAR_REV_WRAPAROUND
359 return (vcp
!= NULL
);
363 _var_lookup(struct var_carrier
*vcp
)
365 struct var
**vap
, *lvp
, *vp
;
368 /* XXX _So_ unlikely that it should be checked if normal lookup fails! */
369 if (UNLIKELY(vcp
->vc_vmap
!= NULL
&&
370 (vcp
->vc_vmap
->vm_flags
& VM_VIRTUAL
) != 0)) {
371 struct var_virtual
const *vvp
;
373 for (vvp
= _var_virtuals
;
374 PTRCMP(vvp
, <, _var_virtuals
+ NELEM(_var_virtuals
)); ++vvp
)
375 if (vvp
->vv_okey
== vcp
->vc_okey
) {
376 vp
= UNCONST(vvp
->vv_var
);
382 vap
= _vars
+ (vcp
->vc_prime
= MA_HASH2PRIME(vcp
->vc_hash
));
384 for (lvp
= NULL
, vp
= *vap
; vp
!= NULL
; lvp
= vp
, vp
= vp
->v_link
)
385 if (!strcmp(vp
->v_name
, vcp
->vc_name
)) {
386 /* Relink as head, hope it "sorts on usage" over time.
387 * _var_clear() relies on this behaviour */
389 lvp
->v_link
= vp
->v_link
;
403 _var_broadway(struct var_show
*vsp
, char const *name
)
408 memset(vsp
, 0, sizeof *vsp
);
410 _var_revlookup(&vsp
->vs_vc
, name
);
412 if ((vsp
->vs_isset
= rv
= _var_lookup(&vsp
->vs_vc
))) {
413 vsp
->vs_value
= vsp
->vs_vc
.vc_var
->v_value
;
414 vsp
->vs_isenv
= FAL0
;
416 vsp
->vs_isenv
= ((vsp
->vs_value
= getenv(vsp
->vs_vc
.vc_name
)) != NULL
);
423 _var_set(struct var_carrier
*vcp
, char const *value
)
431 ok
= _var_clear(vcp
);
437 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_RDONLY
)) {
438 fprintf(stderr
, _("Variable readonly: \"%s\"\n"), vcp
->vc_name
);
443 /* Don't care what happens later on, store this in the unroll list */
444 if (_localopts
!= NULL
)
445 _localopts_add(_localopts
, vcp
->vc_name
, vcp
->vc_var
);
447 if ((vp
= vcp
->vc_var
) == NULL
) {
448 size_t l
= strlen(vcp
->vc_name
) + 1;
451 vp
= smalloc(sizeof(*vp
) - VFIELD_SIZEOF(struct var
, v_name
) + l
);
452 vp
->v_link
= _vars
[vcp
->vc_prime
];
453 _vars
[vcp
->vc_prime
] = vp
;
454 memcpy(vp
->v_name
, vcp
->vc_name
, l
);
459 if (vcp
->vc_vmap
== NULL
)
460 vp
->v_value
= _var_vcopy(value
);
462 /* Via `set' etc. the user may give even binary options non-binary
463 * values, ignore that and force binary xxx error log? */
464 if (vcp
->vc_vmap
->vm_flags
& VM_BINARY
)
466 vp
->v_value
= _var_vcopy(value
);
468 /* Check if update allowed XXX wasteful on error! */
469 if ((vcp
->vc_vmap
->vm_flags
& VM_SPECIAL
) &&
470 !(ok
= _var_check_specials(vcp
->vc_okey
, TRU1
, &vp
->v_value
))) {
471 char *cp
= vp
->v_value
;
485 _var_clear(struct var_carrier
*vcp
)
490 if (!_var_lookup(vcp
)) {
491 if (!(pstate
& PS_IN_LOAD
) && (options
& OPT_D_V
))
492 fprintf(stderr
, _("Variable undefined: \"%s\"\n"), vcp
->vc_name
);
493 } else if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_RDONLY
)) {
494 fprintf(stderr
, _("Variable readonly: \"%s\"\n"), vcp
->vc_name
);
497 if (_localopts
!= NULL
)
498 _localopts_add(_localopts
, vcp
->vc_name
, vcp
->vc_var
);
500 /* Always listhead after _var_lookup() */
501 _vars
[vcp
->vc_prime
] = _vars
[vcp
->vc_prime
]->v_link
;
502 _var_vfree(vcp
->vc_var
->v_value
);
506 if (vcp
->vc_vmap
!= NULL
&& (vcp
->vc_vmap
->vm_flags
& VM_SPECIAL
))
507 ok
= _var_check_specials(vcp
->vc_okey
, FAL0
, NULL
);
520 char const **vacp
, **cap
;
523 if ((fp
= Ftmp(NULL
, "listvars", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
529 for (no
= i
= 0; i
< MA_PRIME
; ++i
)
530 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
532 no
+= NELEM(_var_virtuals
);
534 vacp
= salloc(no
* sizeof(*vacp
));
536 for (cap
= vacp
, i
= 0; i
< MA_PRIME
; ++i
)
537 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
539 for (i
= 0; i
< NELEM(_var_virtuals
); ++i
)
540 *cap
++ = _var_virtuals
[i
].vv_var
->v_name
;
543 qsort(vacp
, no
, sizeof *vacp
, &__var_list_all_cmp
);
545 i
= (ok_blook(bsdcompat
) || ok_blook(bsdset
));
546 for (cap
= vacp
; no
!= 0; ++cap
, --no
) {
549 if (!_var_broadway(&vs
, *cap
))
551 if (vs
.vs_value
== NULL
)
557 if (vs
.vs_vc
.vc_vmap
!= NULL
&&
558 (vs
.vs_vc
.vc_vmap
->vm_flags
& VM_BINARY
))
561 fmt
= "set %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
, *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.. */
595 for (i
= 0, cp_base
= cp
; (c
= *cp
) != '\0'; ++i
, ++cp
)
600 for (i
= 0, cp
= cp_base
; (c
= *cp
) != '\0'; rv
[i
++] = c
, ++cp
)
609 _var_set_env(char **ap
, bool_t issetenv
)
611 char *cp
, *cp2
, *varbuf
, c
;
615 for (; *ap
!= NULL
; ++ap
) {
618 cp2
= varbuf
= ac_alloc(strlen(cp
) +1);
619 for (; (c
= *cp
) != '=' && c
!= '\0'; ++cp
)
627 fprintf(stderr
, _("Non-null variable name required\n"));
632 if (varbuf
[0] == 'n' && varbuf
[1] == 'o') {
633 char const *k
= varbuf
+ 2;
635 if (issetenv
&& (!strcmp(k
, "HOME") || /* TODO generic */
636 !strcmp(k
, "USER") || !strcmp(k
, "TMPDIR"))) {/* TODO approach */
637 if (options
& OPT_D_V
)
638 fprintf(stderr
, _("Cannot `unsetenv' \"%s\"\n"), k
);
643 errs
+= !_var_vokclear(k
);
646 errs
+= (unsetenv(k
) != 0);
652 errs
+= !_var_vokset(varbuf
, (uintptr_t)cp
);
655 static char const *cp_buf
[3];
656 char const **pl
, **pe
;
658 if (!strcmp(varbuf
, "HOME")) /* TODO generic */
659 pl
= cp_buf
+ 0, pe
= &homedir
;
660 else if (!strcmp(varbuf
, "USER")) /* TODO approach */
661 pl
= cp_buf
+ 1, pe
= &myname
;
662 else if (!strcmp(varbuf
, "TMPDIR")) /* TODO also here */
663 pl
= cp_buf
+ 2, pe
= &tempdir
;
669 *pe
= *pl
= sstrdup(cp
);
673 errs
+= (setenv(varbuf
, cp
, 1) != 0);
688 _is_closing_angle(char const *cp
)
693 while (spacechar(*cp
))
697 while (spacechar(*cp
))
705 static struct macro
*
706 _ma_look(char const *name
, struct macro
*data
, enum ma_flags mafl
)
708 enum ma_flags save_mafl
;
710 struct macro
*lmp
, *mp
;
714 mafl
&= MA_TYPE_MASK
;
715 h
= MA_NAME2HASH(name
);
716 h
= MA_HASH2PRIME(h
);
718 for (lmp
= NULL
, mp
= _macros
[h
]; mp
!= NULL
; lmp
= mp
, mp
= mp
->ma_next
) {
719 if ((mp
->ma_flags
& MA_TYPE_MASK
) == mafl
&& !strcmp(mp
->ma_name
, name
)) {
720 if (save_mafl
& MA_UNDEF
) {
722 _macros
[h
] = mp
->ma_next
;
724 lmp
->ma_next
= mp
->ma_next
;
726 /* TODO it should also be possible to delete running macros */
727 if ((mafl
& MA_ACC
) &&
728 account_name
!= NULL
&& !strcmp(account_name
, name
)) {
729 mp
->ma_flags
|= MA_DELETED
;
730 fprintf(stderr
, _("Delayed deletion of active account \"%s\"\n"),
733 _ma_freelines(mp
->ma_contents
);
736 mp
= (struct macro
*)-1;
744 data
->ma_next
= _macros
[h
];
754 _ma_exec(struct macro
const *mp
, struct var
**unroller
)
758 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
; /* FIXME hack (sigman+) */
759 struct mline
const *lp
;
763 los
.s_up
= _localopts
;
764 los
.s_mac
= UNCONST(mp
);
765 los
.s_localopts
= NULL
;
769 x
= salloc(sizeof *x
); /* FIXME intermediate hack (signal man+) */
770 x
->up
= temporary_localopts_store
;
772 temporary_localopts_store
= x
;
774 buf
= ac_alloc(mp
->ma_maxlen
+1);
775 for (lp
= mp
->ma_contents
; lp
; lp
= lp
->l_next
) {
776 memcpy(buf
, lp
->l_line
, lp
->l_length
+1);
777 rv
|= execute(buf
, 0, lp
->l_length
); /* XXX break if != 0 ? */
781 temporary_localopts_store
= x
->up
; /* FIXME intermediate hack */
783 _localopts
= los
.s_up
;
784 if (unroller
== NULL
) {
785 if (los
.s_localopts
!= NULL
)
786 _localopts_unroll(&los
.s_localopts
);
788 *unroller
= los
.s_localopts
;
794 _ma_list(enum ma_flags mafl
)
803 if ((fp
= Ftmp(NULL
, "listmacs", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
810 mafl
&= MA_TYPE_MASK
;
811 typestr
= (mafl
& MA_ACC
) ? "account" : "define";
813 for (ti
= mc
= 0; ti
< MA_PRIME
; ++ti
)
814 for (mq
= _macros
[ti
]; mq
; mq
= mq
->ma_next
)
815 if ((mq
->ma_flags
& MA_TYPE_MASK
) == mafl
) {
818 fprintf(fp
, "%s %s {\n", typestr
, mq
->ma_name
);
819 for (lp
= mq
->ma_contents
; lp
!= NULL
; lp
= lp
->l_next
)
820 fprintf(fp
, " %s\n", lp
->l_line
);
824 page_or_print(fp
, 0);
826 mc
= (ui32_t
)ferror(fp
);
834 _ma_define(char const *name
, enum ma_flags mafl
)
838 struct mline
*lp
, *lst
= NULL
, *lnd
= NULL
;
839 char *linebuf
= NULL
, *cp
;
840 size_t linesize
= 0, maxlen
= 0;
844 mp
= scalloc(1, sizeof *mp
);
845 mp
->ma_name
= sstrdup(name
);
849 n
= readline_input("", TRU1
, &linebuf
, &linesize
, NULL
);
853 fprintf(stderr
, _("Unterminated %s definition: \"%s\".\n"),
854 (mafl
& MA_ACC
? "account" : "macro"), mp
->ma_name
);
855 if ((pstate
& PS_IN_LOAD
) == PS_SOURCING
)
859 if (_is_closing_angle(linebuf
))
863 for (cp
= linebuf
, i
= 0; i
< n
; ++cp
, ++i
)
869 while (whitechar(cp
[n
- 1]))
875 maxlen
= MAX(maxlen
, (size_t)n
);
878 lp
= scalloc(1, sizeof(*lp
) - VFIELD_SIZEOF(struct mline
, l_line
) + n
);
879 memcpy(lp
->l_line
, cp
, n
);
880 lp
->l_length
= (size_t)--n
;
887 mp
->ma_contents
= lst
;
888 mp
->ma_maxlen
= maxlen
;
890 if (_ma_look(mp
->ma_name
, mp
, mafl
) != NULL
) {
891 fprintf(stderr
, _("A %s named \"%s\" already exists.\n"),
892 (mafl
& MA_ACC
? "account" : "macro"), mp
->ma_name
);
893 lst
= mp
->ma_contents
;
913 _ma_undefine(char const *name
, enum ma_flags mafl
)
918 if (LIKELY(name
[0] != '*' || name
[1] != '\0')) {
919 if ((mp
= _ma_look(name
, NULL
, mafl
| MA_UNDEF
)) == NULL
)
920 fprintf(stderr
, _("%s \"%s\" is not defined\n"),
921 (mafl
& MA_ACC
? "Account" : "Macro"), name
);
923 struct macro
**mpp
, *lmp
;
925 for (mpp
= _macros
; PTRCMP(mpp
, <, _macros
+ NELEM(_macros
)); ++mpp
)
926 for (lmp
= NULL
, mp
= *mpp
; mp
!= NULL
;) {
927 if ((mp
->ma_flags
& MA_TYPE_MASK
) == mafl
) {
928 /* xxx Expensive but rare: be simple */
929 _ma_look(mp
->ma_name
, NULL
, mafl
| MA_UNDEF
);
930 mp
= (lmp
== NULL
) ? *mpp
: lmp
->ma_next
;
941 _ma_freelines(struct mline
*lp
)
946 for (lq
= NULL
; lp
!= NULL
; ) {
958 _localopts_add(struct lostack
*losp
, char const *name
, struct var
*ovap
)
964 /* Propagate unrolling up the stack, as necessary */
965 while (!losp
->s_unroll
&& (losp
= losp
->s_up
) != NULL
)
970 /* We've found a level that wants to unroll; check wether it does it yet */
971 for (vap
= losp
->s_localopts
; vap
!= NULL
; vap
= vap
->v_link
)
972 if (!strcmp(vap
->v_name
, name
))
975 nl
= strlen(name
) + 1;
976 vl
= (ovap
!= NULL
) ? strlen(ovap
->v_value
) + 1 : 0;
977 vap
= smalloc(sizeof(*vap
) - VFIELD_SIZEOF(struct var
, v_name
) + nl
+ vl
);
978 vap
->v_link
= losp
->s_localopts
;
979 losp
->s_localopts
= vap
;
980 memcpy(vap
->v_name
, name
, nl
);
984 vap
->v_value
= vap
->v_name
+ nl
;
985 memcpy(vap
->v_value
, ovap
->v_value
, vl
);
992 _localopts_unroll(struct var
**vapp
)
994 struct lostack
*save_los
;
1001 save_los
= _localopts
;
1003 while (vap
!= NULL
) {
1006 vok_vset(x
->v_name
, x
->v_value
);
1009 _localopts
= save_los
;
1014 _var_oklook(enum okeys okey
)
1016 struct var_carrier vc
;
1020 vc
.vc_vmap
= _var_map
+ okey
;
1021 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1022 vc
.vc_hash
= _var_map
[okey
].vm_hash
;
1025 if (!_var_lookup(&vc
)) {
1026 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1028 assert(vc
.vc_var
!= NULL
);
1033 rv
= vc
.vc_var
->v_value
;
1039 _var_okset(enum okeys okey
, uintptr_t val
)
1041 struct var_carrier vc
;
1045 vc
.vc_vmap
= _var_map
+ okey
;
1046 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1047 vc
.vc_hash
= _var_map
[okey
].vm_hash
;
1050 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1056 _var_okclear(enum okeys okey
)
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 rv
= _var_clear(&vc
);
1073 _var_voklook(char const *vokey
)
1075 struct var_carrier vc
;
1079 _var_revlookup(&vc
, vokey
);
1081 if (!_var_lookup(&vc
)) {
1082 if ((rv
= getenv(vc
.vc_name
)) != NULL
) {
1084 assert(vc
.vc_var
!= NULL
);
1089 rv
= vc
.vc_var
->v_value
;
1095 _var_vokset(char const *vokey
, uintptr_t val
)
1097 struct var_carrier vc
;
1101 _var_revlookup(&vc
, vokey
);
1103 ok
= _var_set(&vc
, (val
== 0x1 ? "" : (char const*)val
));
1109 _var_vokclear(char const *vokey
)
1111 struct var_carrier vc
;
1115 _var_revlookup(&vc
, vokey
);
1117 err
= !_var_clear(&vc
);
1123 _env_look(char const *envkey
, bool_t envonly
) /* TODO rather dummy yet!! */
1129 rv
= getenv(envkey
); /* TODO rework vars: cache, output a la mimetypes */
1131 rv
= _var_voklook(envkey
);
1138 _var_xoklook(enum okeys okey
, struct url
const *urlp
, enum okey_xlook_mode oxm
)
1140 struct var_carrier vc
;
1141 struct str
const *us
;
1143 char *nbuf
= NULL
/* CC happiness */, *rv
;
1146 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1148 /* For simplicity: allow this case too */
1149 if (!(oxm
& (OXM_H_P
| OXM_U_H_P
)))
1152 vc
.vc_vmap
= _var_map
+ okey
;
1153 vc
.vc_name
= _var_keydat
+ _var_map
[okey
].vm_keyoff
;
1156 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1157 nlen
= strlen(vc
.vc_name
);
1158 nbuf
= ac_alloc(nlen
+ 1 + us
->l
+1);
1159 memcpy(nbuf
, vc
.vc_name
, nlen
);
1162 /* One of .url_u_h_p and .url_h_p we test in here */
1163 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1164 vc
.vc_name
= _var_canonify(nbuf
);
1165 vc
.vc_hash
= MA_NAME2HASH(vc
.vc_name
);
1166 if (_var_lookup(&vc
))
1170 if (oxm
& OXM_H_P
) {
1171 us
= &urlp
->url_h_p
;
1172 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1173 vc
.vc_name
= _var_canonify(nbuf
);
1174 vc
.vc_hash
= MA_NAME2HASH(vc
.vc_name
);
1175 if (_var_lookup(&vc
)) {
1177 rv
= vc
.vc_var
->v_value
;
1183 rv
= (oxm
& OXM_PLAIN
) ? _var_oklook(okey
) : NULL
;
1185 if (oxm
& (OXM_H_P
| OXM_U_H_P
))
1190 #endif /* HAVE_SOCKETS */
1196 char const **argv
= v
;
1201 else for (; *argv
!= NULL
; ++argv
) {
1202 _var_broadway(&vs
, *argv
);
1203 if (vs
.vs_value
== NULL
)
1204 vs
.vs_value
= "NULL";
1206 if (vs
.vs_vc
.vc_vmap
!= NULL
) {
1207 ui16_t f
= vs
.vs_vc
.vc_vmap
->vm_flags
;
1210 printf(_("\"%s\": (%d) binary%s%s: set=%d (ENVIRON=%d)\n"),
1211 vs
.vs_vc
.vc_name
, vs
.vs_vc
.vc_okey
,
1212 (f
& VM_RDONLY
? ", read-only" : ""),
1213 (f
& VM_VIRTUAL
? ", virtual" : ""), vs
.vs_isset
, vs
.vs_isenv
);
1215 printf(_("\"%s\": (%d) value%s%s: set=%d (ENVIRON=%d) value<%s>\n"),
1216 vs
.vs_vc
.vc_name
, vs
.vs_vc
.vc_okey
,
1217 (f
& VM_RDONLY
? ", read-only" : ""),
1218 (f
& VM_VIRTUAL
? ", virtual" : ""), vs
.vs_isset
, vs
.vs_isenv
,
1221 printf("\"%s\": (assembled): set=%d (ENVIRON=%d) value<%s>\n",
1222 vs
.vs_vc
.vc_name
, vs
.vs_isset
, vs
.vs_isenv
, vs
.vs_value
);
1225 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1239 err
= !_var_set_env(ap
, FAL0
);
1251 if (!(err
= !(pstate
& PS_STARTED
)))
1252 err
= !_var_set_env(ap
, TRU1
);
1265 err
|= !_var_vokclear(*ap
++);
1276 if (!(err
= !(pstate
& PS_STARTED
))) {
1279 for (ap
= v
; *ap
!= NULL
; ++ap
) {
1282 if (!strcmp(*ap
, "HOME") || !strcmp(*ap
, "USER") || /* TODO generic */
1283 !strcmp(*ap
, "TMPDIR")) { /* TODO approach */
1284 if (options
& OPT_D_V
)
1285 fprintf(stderr
, _("Cannot `unsetenv' \"%s\"\n"), *ap
);
1290 bad
= !_var_vokclear(*ap
);
1293 unsetenv(*ap
) != 0 ||
1307 struct var_carrier vc
;
1308 sighandler_type sigint
;
1310 char *val
, **argv
= v
;
1314 sigint
= safe_signal(SIGINT
, SIG_IGN
);
1316 while (*argv
!= NULL
) {
1317 memset(&vc
, 0, sizeof vc
);
1319 _var_revlookup(&vc
, *argv
++);
1321 if (vc
.vc_vmap
!= NULL
) {
1322 if (vc
.vc_vmap
->vm_flags
& VM_BINARY
) {
1323 fprintf(stderr
, _("`varedit': can't edit binary variable \"%s\"\n"),
1327 if (vc
.vc_vmap
->vm_flags
& VM_RDONLY
) {
1329 _("`varedit': can't edit readonly variable \"%s\"\n"),
1337 if ((of
= Ftmp(NULL
, "vared", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1339 perror(_("`varedit': cannot create temporary file"));
1342 } else if (vc
.vc_var
!= NULL
&& *(val
= vc
.vc_var
->v_value
) != '\0' &&
1343 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)) {
1344 perror(_("`varedit' failed to write an old value to temporary file"));
1351 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
1357 off_t l
= fsize(nf
);
1360 base
= smalloc((size_t)l
+ 1);
1362 for (l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
1363 if (c
== '\n' || c
== '\r') {
1367 *val
= (char)(uc_i
)c
;
1373 if (!vok_vset(vc
.vc_name
, base
))
1381 safe_signal(SIGINT
, sigint
);
1393 if (args
[0] == NULL
) {
1394 rv
= _ma_list(MA_NONE
);
1398 if (args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1400 fprintf(stderr
, _("Syntax is: define <name> {"));
1404 rv
= !_ma_define(args
[0], MA_NONE
);
1417 if (*args
== NULL
) {
1418 fprintf(stderr
, _("`undefine': required arguments are missing\n"));
1422 _ma_undefine(*args
, MA_NONE
);
1423 while (*++args
!= NULL
);
1435 char const *errs
, *name
;
1439 if (args
[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)) {
1440 errs
= _("Syntax is: call <%s>\n");
1445 if ((mp
= _ma_look(*args
, NULL
, MA_NONE
)) == NULL
) {
1446 errs
= _("Undefined macro called: \"%s\"\n");
1451 rv
= _ma_exec(mp
, NULL
);
1456 fprintf(stderr
, errs
, name
);
1461 check_folder_hook(bool_t nmail
)
1466 struct var
**unroller
;
1470 var
= ac_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1472 /* First try the fully resolved path */
1473 snprintf(var
, len
, "folder-hook-%s", mailname
);
1474 if ((cp
= vok_vlook(var
)) != NULL
)
1477 /* If we are under *folder*, try the usual +NAME syntax, too */
1478 if (displayname
[0] == '+') {
1479 char *x
= mailname
+ len
;
1481 for (; x
> mailname
; --x
)
1483 snprintf(var
, len
, "folder-hook-+%s", x
);
1484 if ((cp
= vok_vlook(var
)) != NULL
)
1490 /* Plain *folder-hook* is our last try */
1491 if ((cp
= ok_vlook(folder_hook
)) == NULL
)
1495 if ((mp
= _ma_look(cp
, NULL
, MA_NONE
)) == NULL
) {
1496 fprintf(stderr
, _("Cannot call *folder-hook* for \"%s\": "
1497 "macro \"%s\" does not exist.\n"), displayname
, cp
);
1502 pstate
&= ~PS_HOOK_MASK
;
1504 pstate
|= PS_HOOK_NEWMAIL
;
1508 unroller
= &_folder_hook_localopts
;
1510 rv
= (_ma_exec(mp
, unroller
) == 0);
1511 pstate
&= ~PS_HOOK_MASK
;
1524 int rv
= 1, i
, oqf
, nqf
;
1527 if (args
[0] == NULL
) {
1528 rv
= _ma_list(MA_ACC
);
1532 if (args
[1] && args
[1][0] == '{' && args
[1][1] == '\0') {
1533 if (args
[2] != NULL
) {
1534 fprintf(stderr
, _("Syntax is: account <name> {\n"));
1537 if (!asccasecmp(args
[0], ACCOUNT_NULL
)) {
1538 fprintf(stderr
, _("Error: \"%s\" is a reserved name.\n"),
1542 rv
= !_ma_define(args
[0], MA_ACC
);
1546 if (pstate
& PS_HOOK_MASK
) {
1547 fprintf(stderr
, _("Cannot change account from within a hook.\n"));
1551 save_mbox_for_possible_quitstuff();
1554 if (asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1555 (mp
= _ma_look(args
[0], NULL
, MA_ACC
)) == NULL
) {
1556 fprintf(stderr
, _("Account \"%s\" does not exist.\n"), args
[0]);
1560 oqf
= savequitflags();
1562 if (_acc_curr
!= NULL
) {
1563 if (_acc_curr
->ma_localopts
!= NULL
)
1564 _localopts_unroll(&_acc_curr
->ma_localopts
);
1565 if (_acc_curr
->ma_flags
& MA_DELETED
) { /* xxx can be made generic? */
1566 _ma_freelines(_acc_curr
->ma_contents
);
1567 free(_acc_curr
->ma_name
);
1572 account_name
= (mp
!= NULL
) ? mp
->ma_name
: NULL
;
1575 if (mp
!= NULL
&& _ma_exec(mp
, &mp
->ma_localopts
) == CBAD
) {
1576 /* XXX account switch incomplete, unroll? */
1577 fprintf(stderr
, _("Switching to account \"%s\" failed.\n"), args
[0]);
1581 if ((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
) {
1582 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1583 restorequitflags(oqf
);
1584 if ((i
= setfile("%", 0)) < 0)
1586 check_folder_hook(FAL0
);
1587 if (i
> 0 && !ok_blook(emptystart
))
1589 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1590 restorequitflags(nqf
);
1599 c_unaccount(void *v
)
1605 if (*args
== NULL
) {
1606 fprintf(stderr
, _("`unaccount': required arguments are missing\n"));
1610 _ma_undefine(*args
, MA_ACC
);
1611 while (*++args
!= NULL
);
1619 c_localopts(void *v
)
1625 if (_localopts
== NULL
) {
1627 _("Cannot use `localopts' but from within a `define' or `account'\n"));
1631 _localopts
->s_unroll
= (boolify(*c
, UIZ_MAX
, FAL0
) > 0);
1639 temporary_localopts_free(void) /* XXX intermediate hack */
1641 struct n2
{struct n2
*up
; struct lostack
*lo
;} *x
;
1644 x
= temporary_localopts_store
;
1645 temporary_localopts_store
= NULL
;
1649 struct lostack
*losp
= x
->lo
;
1651 if (losp
->s_localopts
!= NULL
)
1652 _localopts_unroll(&losp
->s_localopts
);
1658 temporary_localopts_folder_hook_unroll(void) /* XXX intermediate hack */
1661 if (_folder_hook_localopts
!= NULL
) {
1662 void *save
= _localopts
;
1664 _localopts_unroll(&_folder_hook_localopts
);