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 - 2013 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
43 * TODO in general it would be nice if it would be possible to define "macros"
44 * TODO etc. inside of other "macros"
47 #define MA_PRIME HSHSIZE
48 #define MA_HASH(S) (strhash(S) % MA_PRIME)
53 MA_TYPE_MASK
= MA_ACC
,
54 MA_UNDEF
= 1<<1 /* Unlink after lookup */
58 struct macro
*ma_next
;
60 struct line
*ma_contents
;
61 size_t ma_maxlen
; /* Maximum line length */
62 enum ma_flags ma_flags
;
63 struct var
*ma_localopts
; /* `account' unroll list, for `localopts' */
69 char l_line
[VFIELD_SIZE(sizeof(size_t))];
79 struct lostack
*s_up
; /* Outer context */
80 struct macro
*s_mac
; /* Context (`account' or `define') */
81 struct var
*s_localopts
;
82 bool_t s_unroll
; /* Unroll? */
85 static struct macro
*_acc_curr
; /* Currently active account */
86 static struct lostack
*_localopts
; /* Currently executing macro unroll list */
88 /* TODO once we have a dynamically sized hashtable we could unite _macros and
89 * TODO _variables into a single hashtable, stripping down fun interface;
90 * TODO also, setting and clearing a variable can be easily joined */
91 static struct macro
*_macros
[MA_PRIME
]; /* TODO dynamically spaced */
92 static struct var
*_vars
[MA_PRIME
]; /* TODO dynamically spaced */
94 /* Special cased value string allocation */
95 static char * _vcopy(char const *str
);
96 static void _vfree(char *cp
);
98 /* Check for special housekeeping. */
99 static bool_t
_check_special_vars(char const *name
, bool_t enable
,
102 /* If a variable name begins with a lowercase-character and contains at
103 * least one '@', it is converted to all-lowercase. This is necessary
104 * for lookups of names based on email addresses.
106 * Following the standard, only the part following the last '@' should
107 * be lower-cased, but practice has established otherwise here */
108 static char const * _canonify(char const *vn
);
110 /* Locate a variable and return its variable node */
111 static struct var
* _lookup(char const *name
, ui_it h
, bool_t hisset
);
113 /* Line *cp* consists solely of WS and a } */
114 static bool_t
_is_closing_angle(char const *cp
);
116 /* Lookup for macros/accounts */
117 static struct macro
*_malook(char const *name
, struct macro
*data
,
120 /* Walk all lines of a macro and execute() them */
121 static int _maexec(struct macro
const *mp
, struct var
**unroll_store
);
123 /* User display helpers */
124 static int _list_macros(enum ma_flags mafl
);
127 static bool_t
_define1(char const *name
, enum ma_flags mafl
);
128 static void _undef1(char const *name
, enum ma_flags mafl
);
129 static void _freelines(struct line
*lp
);
131 /* qsort(3) helper */
132 static int __var_list_all_cmp(void const *s1
, void const *s2
);
134 /* Update replay-log */
135 static void _localopts_add(struct lostack
*losp
, char const *name
,
137 static void _localopts_unroll(struct var
**vapp
);
140 _vcopy(char const *str
)
148 len
= strlen(str
) + 1;
150 memcpy(news
, str
, len
);
163 _check_special_vars(char const *name
, bool_t enable
, char **val
)
165 /* TODO _check_special_vars --> value cache */
170 if (strcmp(name
, "debug") == 0)
172 else if (strcmp(name
, "header") == 0)
173 flag
= OPT_N_FLAG
, enable
= ! enable
;
174 else if (strcmp(name
, "skipemptybody") == 0)
176 else if (strcmp(name
, "verbose") == 0)
178 else if (strcmp(name
, "prompt") == 0)
179 flag
= OPT_NOPROMPT
, enable
= ! enable
;
180 else if (strcmp(name
, "folder") == 0) {
181 rv
= var_folder_updated(*val
, &cp
);
182 if (rv
&& cp
!= NULL
) {
184 /* It's smalloc()ed, but ensure we don't leak */
192 #if ! defined HAVE_READLINE && ! defined HAVE_EDITLINE &&\
193 defined HAVE_LINE_EDITOR
194 else if (strcmp(name
, "line-editor-cursor-right") == 0) {
195 char const *x
= cp
= *val
;
198 c
= expand_shell_escape(&x
, FAL0
);
217 _canonify(char const *vn
)
219 if (! upperchar(*vn
)) {
222 for (vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
224 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
230 _lookup(char const *name
, ui_it h
, bool_t hisset
)
232 struct var
**vap
, *lvp
, *vp
;
238 for (lvp
= NULL
, vp
= *vap
; vp
!= NULL
; lvp
= vp
, vp
= vp
->v_link
)
239 if (*vp
->v_name
== *name
&& strcmp(vp
->v_name
, name
) == 0) {
240 /* Relink as head, hope it "sorts on usage" over time */
242 lvp
->v_link
= vp
->v_link
;
254 _is_closing_angle(char const *cp
)
257 while (spacechar(*cp
))
261 while (spacechar(*cp
))
268 static struct macro
*
269 _malook(char const *name
, struct macro
*data
, enum ma_flags mafl
)
271 enum ma_flags save_mafl
;
273 struct macro
*lmp
, *mp
;
276 mafl
&= MA_TYPE_MASK
;
279 for (lmp
= NULL
, mp
= _macros
[h
]; mp
!= NULL
; lmp
= mp
, mp
= mp
->ma_next
) {
280 if ((mp
->ma_flags
& MA_TYPE_MASK
) == mafl
&&
281 strcmp(mp
->ma_name
, name
) == 0) {
282 if (save_mafl
& MA_UNDEF
) {
284 _macros
[h
] = mp
->ma_next
;
286 lmp
->ma_next
= mp
->ma_next
;
293 data
->ma_next
= _macros
[h
];
302 _maexec(struct macro
const *mp
, struct var
**unroll_store
)
306 struct line
const *lp
;
307 char *buf
= ac_alloc(mp
->ma_maxlen
+ 1);
309 los
.s_up
= _localopts
;
310 los
.s_mac
= UNCONST(mp
);
311 los
.s_localopts
= NULL
;
315 for (lp
= mp
->ma_contents
; lp
; lp
= lp
->l_next
) {
316 unset_allow_undefined
= TRU1
;
317 memcpy(buf
, lp
->l_line
, lp
->l_length
+ 1);
318 rv
|= execute(buf
, 0, lp
->l_length
); /* XXX break if != 0 ? */
319 unset_allow_undefined
= FAL0
;
322 _localopts
= los
.s_up
;
323 if (unroll_store
== NULL
)
324 _localopts_unroll(&los
.s_localopts
);
326 *unroll_store
= los
.s_localopts
;
333 _list_macros(enum ma_flags mafl
)
342 if ((fp
= Ftemp(&cp
, "Ra", "w+", 0600, 1)) == NULL
) {
349 mafl
&= MA_TYPE_MASK
;
350 typestr
= (mafl
& MA_ACC
) ? "account" : "define";
352 for (ti
= mc
= 0; ti
< MA_PRIME
; ++ti
)
353 for (mq
= _macros
[ti
]; mq
; mq
= mq
->ma_next
)
354 if ((mq
->ma_flags
& MA_TYPE_MASK
) == mafl
) {
357 fprintf(fp
, "%s %s {\n", typestr
, mq
->ma_name
);
358 for (lp
= mq
->ma_contents
; lp
; lp
= lp
->l_next
)
359 fprintf(fp
, " %s\n", lp
->l_line
);
363 page_or_print(fp
, 0);
365 mc
= (ui_it
)ferror(fp
);
371 _define1(char const *name
, enum ma_flags mafl
)
375 struct line
*lp
, *lst
= NULL
, *lnd
= NULL
;
376 char *linebuf
= NULL
, *cp
;
377 size_t linesize
= 0, maxlen
= 0;
380 mp
= scalloc(1, sizeof *mp
);
381 mp
->ma_name
= sstrdup(name
);
385 n
= readline_input(LNED_LF_ESC
, "", &linebuf
, &linesize
);
387 fprintf(stderr
, tr(75, "Unterminated %s definition: \"%s\".\n"),
388 (mafl
& MA_ACC
? "account" : "macro"), mp
->ma_name
);
393 if (_is_closing_angle(linebuf
))
397 for (cp
= linebuf
, i
= 0; i
< n
; ++cp
, ++i
)
398 if (! whitechar(*cp
))
403 while (whitechar(cp
[n
- 1]))
409 maxlen
= MAX(maxlen
, (size_t)n
);
412 lp
= scalloc(1, sizeof(*lp
) - VFIELD_SIZEOF(struct line
, l_line
) + n
);
413 memcpy(lp
->l_line
, cp
, n
);
414 lp
->l_length
= (size_t)--n
;
421 mp
->ma_contents
= lst
;
422 mp
->ma_maxlen
= maxlen
;
424 if (_malook(mp
->ma_name
, mp
, mafl
) != NULL
) {
425 if (! (mafl
& MA_ACC
)) {
426 fprintf(stderr
, tr(76, "A macro named \"%s\" already exists.\n"),
428 lst
= mp
->ma_contents
;
431 _undef1(mp
->ma_name
, MA_ACC
);
432 _malook(mp
->ma_name
, mp
, MA_ACC
);
449 _undef1(char const *name
, enum ma_flags mafl
)
453 if ((mp
= _malook(name
, NULL
, mafl
| MA_UNDEF
)) != NULL
) {
454 _freelines(mp
->ma_contents
);
461 _freelines(struct line
*lp
)
465 for (lq
= NULL
; lp
!= NULL
; ) {
476 __var_list_all_cmp(void const *s1
, void const *s2
)
478 return strcmp(*(char**)UNCONST(s1
), *(char**)UNCONST(s2
));
482 _localopts_add(struct lostack
*losp
, char const *name
, struct var
*ovap
)
487 /* Propagate unrolling up the stack, as necessary */
488 while (! losp
->s_unroll
&& (losp
= losp
->s_up
) != NULL
)
493 /* We have found a level that wants to unroll; check wether it does it yet */
494 for (vap
= losp
->s_localopts
; vap
!= NULL
; vap
= vap
->v_link
)
495 if (strcmp(vap
->v_name
, name
) == 0)
498 nl
= strlen(name
) + 1;
499 vl
= (ovap
!= NULL
) ? strlen(ovap
->v_value
) + 1 : 0;
500 vap
= smalloc(sizeof(*vap
) + nl
+ vl
);
501 vap
->v_link
= losp
->s_localopts
;
502 losp
->s_localopts
= vap
;
503 vap
->v_name
= (char*)(vap
+ 1);
504 memcpy(vap
->v_name
, name
, nl
);
508 vap
->v_value
= (char*)(vap
+ 1) + nl
;
509 memcpy(vap
->v_value
, ovap
->v_value
, vl
);
516 _localopts_unroll(struct var
**vapp
)
518 struct lostack
*save_los
;
524 save_los
= _localopts
;
526 while (vap
!= NULL
) {
529 var_assign(x
->v_name
, x
->v_value
);
532 _localopts
= save_los
;
536 var_assign(char const *name
, char const *val
)
543 bool_t tmp
= unset_allow_undefined
;
544 unset_allow_undefined
= TRU1
;
546 unset_allow_undefined
= tmp
;
550 name
= _canonify(name
);
552 vp
= _lookup(name
, h
, TRU1
);
554 /* Don't care what happens later on, store this in the unroll list */
555 if (_localopts
!= NULL
)
556 _localopts_add(_localopts
, name
, vp
);
559 vp
= (struct var
*)scalloc(1, sizeof *vp
);
560 vp
->v_name
= _vcopy(name
);
561 vp
->v_link
= _vars
[h
];
566 vp
->v_value
= _vcopy(val
);
568 /* Check if update allowed XXX wasteful on error! */
569 if (! _check_special_vars(name
, TRU1
, &vp
->v_value
)) {
570 char *cp
= vp
->v_value
;
581 var_unset(char const *name
)
587 name
= _canonify(name
);
589 vp
= _lookup(name
, h
, TRU1
);
592 if (! sourcing
&& ! unset_allow_undefined
) {
593 fprintf(stderr
, tr(203, "\"%s\": undefined variable\n"), name
);
597 if (_localopts
!= NULL
)
598 _localopts_add(_localopts
, name
, vp
);
600 /* Always listhead after _lookup() */
601 _vars
[h
] = _vars
[h
]->v_link
;
606 _check_special_vars(name
, FAL0
, NULL
);
614 var_lookup(char const *name
, bool_t look_environ
)
619 name
= _canonify(name
);
620 if ((vp
= _lookup(name
, 0, FAL0
)) != NULL
)
622 else if (! look_environ
)
624 else if ((rv
= getenv(name
)) != NULL
&& *rv
!= '\0')
633 char *cp
, **vacp
, **cap
;
638 if ((fp
= Ftemp(&cp
, "Ra", "w+", 0600, 1)) == NULL
) {
645 for (no
= i
= 0; i
< MA_PRIME
; ++i
)
646 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
648 vacp
= salloc(no
* sizeof(*vacp
));
649 for (cap
= vacp
, i
= 0; i
< MA_PRIME
; ++i
)
650 for (vp
= _vars
[i
]; vp
!= NULL
; vp
= vp
->v_link
)
654 qsort(vacp
, no
, sizeof *vacp
, &__var_list_all_cmp
);
656 i
= (boption("bsdcompat") || boption("bsdset"));
657 fmt
= (i
!= 0) ? "%s\t%s\n" : "%s=\"%s\"\n";
659 for (cap
= vacp
; no
!= 0; ++cap
, --no
) {
660 cp
= value(*cap
); /* TODO internal lookup; binary? value? */
663 if (i
|| *cp
!= '\0')
664 fprintf(fp
, fmt
, *cap
, cp
);
666 fprintf(fp
, "%s\n", *cap
);
669 page_or_print(fp
, (size_t)(cap
- vacp
));
682 if (args
[0] == NULL
) {
683 errs
= tr(504, "Missing macro name to `define'");
686 if (args
[1] == NULL
|| strcmp(args
[1], "{") || args
[2] != NULL
) {
687 errs
= tr(505, "Syntax is: define <name> {");
690 rv
= ! _define1(args
[0], MA_NONE
);
694 fprintf(stderr
, "%s\n", errs
);
705 fprintf(stderr
, tr(506, "Missing macro name to `undef'\n"));
709 _undef1(*args
, MA_NONE
);
721 char const *errs
, *name
;
724 if (args
[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)) {
725 errs
= tr(507, "Syntax is: call <%s>\n");
730 if ((mp
= _malook(*args
, NULL
, MA_NONE
)) == NULL
) {
731 errs
= tr(508, "Undefined macro called: \"%s\"\n");
736 rv
= _maexec(mp
, NULL
);
740 fprintf(stderr
, errs
, name
);
745 callhook(char const *name
, int nmail
)
751 var
= ac_alloc(len
= strlen(name
) + 13);
752 snprintf(var
, len
, "folder-hook-%s", name
);
753 if ((cp
= value(var
)) == NULL
&& (cp
= value("folder-hook")) == NULL
) {
757 if ((mp
= _malook(cp
, NULL
, MA_NONE
)) == NULL
) {
758 fprintf(stderr
, tr(49, "Cannot call hook for folder \"%s\": "
759 "Macro \"%s\" does not exist.\n"), name
, cp
);
764 inhook
= nmail
? 3 : 1;
765 rv
= _maexec(mp
, NULL
);
776 return _list_macros(MA_NONE
);
782 char **args
= v
, *cp
;
784 int rv
= 1, i
, oqf
, nqf
;
786 if (args
[0] == NULL
) {
787 rv
= _list_macros(MA_ACC
);
791 if (args
[1] && args
[1][0] == '{' && args
[1][1] == '\0') {
792 if (args
[2] != NULL
) {
793 fprintf(stderr
, tr(517, "Syntax is: account <name> {\n"));
796 if (asccasecmp(args
[0], ACCOUNT_NULL
) == 0) {
797 fprintf(stderr
, tr(521, "Error: `%s' is a reserved name.\n"),
801 rv
= ! _define1(args
[0], MA_ACC
);
806 fprintf(stderr
, tr(518, "Cannot change account from within a hook.\n"));
810 if ((cp
= expand("&")) == NULL
)
812 n_strlcpy(mboxname
, cp
, sizeof mboxname
);
815 if (asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
816 (mp
= _malook(args
[0], NULL
, MA_ACC
)) == NULL
) {
817 fprintf(stderr
, tr(519, "Account `%s' does not exist.\n"), args
[0]);
821 oqf
= savequitflags();
822 if (_acc_curr
!= NULL
)
823 _localopts_unroll(&_acc_curr
->ma_localopts
);
824 account_name
= (mp
!= NULL
) ? mp
->ma_name
: NULL
;
827 if (mp
!= NULL
&& _maexec(mp
, &mp
->ma_localopts
) == CBAD
) {
828 /* XXX account switch incomplete, unroll? */
829 fprintf(stderr
, tr(520, "Switching to account `%s' failed.\n"), args
[0]);
833 if (! starting
&& ! inhook
) {
834 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
835 restorequitflags(oqf
);
836 if ((i
= setfile("%", 0)) < 0)
838 callhook(mailname
, 0);
839 if (i
> 0 && ! boption("emptystart"))
841 announce(boption("bsdcompat") || boption("bsdannounce"));
842 restorequitflags(nqf
);
855 if (_localopts
== NULL
) {
856 fprintf(stderr
, tr(522,
857 "Cannot use `localopts' but from within a `define' or `account'\n"));
861 _localopts
->s_unroll
= (**c
== '0') ? FAL0
: TRU1
;
867 /* vim:set fenc=utf-8:s-it-mode */