1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run mk-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . should be recursive environment based
8 *@ TODO . undefining and overwriting a macro should always be possible:
9 *@ TODO simply place the thing in a delete-later list and replace the
10 *@ TODO accessible entry! (instant delete if on top recursion level.)
11 *@ TODO . Likewise, overwriting an existing should be like delete+create
12 *@ TODO . once we can have non-fatal !0 returns for commands, we should
13 *@ TODO return error if "(environ)? unset" goes for non-existent.
15 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
16 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
19 * Copyright (c) 1980, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
53 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
54 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
57 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
58 #define a_AMV_PRIME HSHSIZE
59 #define a_AMV_NAME2HASH(N) torek_hash(N)
60 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
64 a_AMV_MF_ACC
= 1<<0, /* This macro is an `account' */
65 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACC
,
66 a_AMV_MF_UNDEF
= 1<<1, /* Unlink after lookup */
67 a_AMV_MF_DEL
= 1<<7, /* Current `account': deleted while active */
71 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
72 * _IMPORT implies _ENV; it doesn't verify anything... */
75 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
76 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
77 a_AMV_VF_RDONLY
= 1<<2, /* May not be set by user */
78 a_AMV_VF_NODEL
= 1<<3, /* May not be deleted */
79 a_AMV_VF_NOTEMPTY
= 1<<4, /* May not be assigned an empty value */
80 a_AMV_VF_NOCNTRLS
= 1<<5, /* Value may not contain control characters */
81 a_AMV_VF_NUM
= 1<<6, /* Value must be a 32-bit number */
82 a_AMV_VF_POSNUM
= 1<<7, /* Value must be positive 32-bit number */
83 a_AMV_VF_VIP
= 1<<8, /* Wants _var_check_vips() evaluation */
84 a_AMV_VF_IMPORT
= 1<<9, /* Import ONLY from environ (before PS_STARTED) */
85 a_AMV_VF_ENV
= 1<<10, /* Update environment on change */
86 a_AMV_VF_I3VAL
= 1<<11, /* Has an initial value */
87 a_AMV_VF_DEFVAL
= 1<<12, /* Has a default value */
88 a_AMV_VF_LINKED
= 1<<13, /* `environ' linked */
89 a_AMV_VF__MASK
= (1<<(13+1)) - 1
93 struct a_amv_mac
*am_next
;
94 ui32_t am_maxlen
; /* of any line in .am_line_dat */
95 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
96 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
97 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
98 ui8_t am_flags
; /* enum a_amv_mac_flags */
99 char am_name
[VFIELD_SIZE(7)]; /* of this macro */
101 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
103 struct a_amv_mac_line
{
105 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
106 char aml_dat
[VFIELD_SIZE(0)];
109 struct a_amv_mac_call_args
{
110 char const *amca_name
;
111 struct a_amv_mac
const *amca_amp
;
112 struct a_amv_var
**amca_unroller
;
113 void (*amca_hook_pre
)(void *);
114 void (*amca_hook_post
)(void *);
116 bool_t amca_lopts_on
;
120 struct a_amv_lostack
{
121 struct a_amv_lostack
*as_up
; /* Outer context */
122 struct a_amv_mac
*as_mac
; /* Context (`account' or `define') */
123 struct a_amv_var
*as_lopts
;
124 bool_t as_unroll
; /* Unrolling enabled? */
129 struct a_amv_var
*av_link
;
132 char *av_env
; /* Actively managed putenv(3) memory */
134 ui16_t av_flags
; /* enum a_amv_var_flags */
135 char av_name
[VFIELD_SIZE(6)];
137 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
139 struct a_amv_var_map
{
142 ui16_t avm_flags
; /* enum a_amv_var_flags */
144 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
146 struct a_amv_var_virt
{
149 struct a_amv_var
const *avv_var
;
152 struct a_amv_var_defval
{
155 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
158 struct a_amv_var_carrier
{
159 char const *avc_name
;
162 struct a_amv_var
*avc_var
;
163 struct a_amv_var_map
const *avc_map
;
168 /* Include the constant mk-okey-map.pl output */
172 /* The currently active account */
173 static struct a_amv_mac
*a_amv_acc_curr
;
175 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
177 /* Unroll list of currently running macro stack */
178 static struct a_amv_lostack
*a_amv_lopts
;
180 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
182 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
183 * TODO today via a static lostack, it should be a field in mailbox, once that
184 * TODO is a real multi-instance object */
185 static struct a_amv_var
*a_amv_folder_hook_lopts
;
187 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
188 static struct a_amv_var
*a_amv_compose_lopts
;
190 /* Does cp consist solely of WS and a } */
191 static bool_t
a_amv_mac_is_closing_angle(char const *cp
);
193 /* Lookup for macros/accounts */
194 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
195 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
197 /* Execute a macro */
198 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
200 /* User display helpers */
201 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
203 /* _def() returns error for faulty definitions and already existing * names,
204 * _undef() returns error if a named thing doesn't exist */
205 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
206 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
209 static void a_amv_mac_free(struct a_amv_mac
*amp
);
211 /* Update replay-log */
212 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
213 struct a_amv_var
*oavp
);
214 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
216 /* Special cased value string allocation */
217 static char *a_amv_var_copy(char const *str
);
218 static void a_amv_var_free(char *cp
);
220 /* Check for special housekeeping */
221 static bool_t
a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
);
223 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
224 static bool_t
a_amv_var_check_nocntrls(char const *val
);
225 static bool_t
a_amv_var_check_num(char const *val
, bool_t pos
);
227 /* If a variable name begins with a lowercase-character and contains at
228 * least one '@', it is converted to all-lowercase. This is necessary
229 * for lookups of names based on email addresses.
230 * Following the standard, only the part following the last '@' should
231 * be lower-cased, but practice has established otherwise here */
232 static char const *a_amv_var_canonify(char const *vn
);
234 /* Try to reverse lookup an option name to an enum okeys mapping.
235 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
236 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
239 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
240 * Sets .avc_prime; .avc_var is NULL if not found.
241 * Here it is where we care for _I3VAL and _DEFVAL, too.
242 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
243 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
244 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
245 * return FAL0, then! */
246 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
249 /* Set var from .avc_(map|name|hash), return success */
250 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
253 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
254 struct a_amv_var
*avp
);
256 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
257 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
259 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
261 /* List all variables */
262 static void a_amv_var_show_all(void);
264 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
266 /* Actually do print one, return number of lines written */
267 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
269 /* Shared c_set() and c_environ():set impl, return success */
270 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
273 a_amv_mac_is_closing_angle(char const *cp
){
277 while(spacechar(*cp
))
280 if((rv
= (*cp
++ == '}'))){
281 while(spacechar(*cp
))
289 static struct a_amv_mac
*
290 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
291 enum a_amv_mac_flags amf
){
292 struct a_amv_mac
*amp
, **ampp
;
294 enum a_amv_mac_flags save_amf
;
298 amf
&= a_AMV_MF_TYPE_MASK
;
299 h
= a_AMV_NAME2HASH(name
);
300 h
= a_AMV_HASH2PRIME(h
);
301 ampp
= &a_amv_macs
[h
];
303 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
304 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
305 !strcmp(amp
->am_name
, name
)){
306 if(LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
309 *ampp
= amp
->am_next
;
311 if((amf
& a_AMV_MF_ACC
) &&
312 account_name
!= NULL
&& !strcmp(account_name
, name
)){
313 amp
->am_flags
|= a_AMV_MF_DEL
;
314 n_err(_("Delayed deletion of active account: %s\n"), name
);
317 amp
= (struct a_amv_mac
*)-1;
324 ampp
= &a_amv_macs
[h
];
325 newamp
->am_next
= *ampp
;
335 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
336 struct a_amv_lostack los
, *los_save
;
337 struct a_amv_mac_line
**amlp
;
338 char **args_base
, **args
;
339 struct a_amv_mac
const *amp
;
343 amp
= amcap
->amca_amp
;
344 args_base
= args
= smalloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
345 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
346 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
349 los
.as_mac
= UNCONST(amp
); /* But not used.. */
350 los_save
= a_amv_lopts
;
351 if(amcap
->amca_unroller
== NULL
){
352 los
.as_up
= los_save
;
356 los
.as_lopts
= *amcap
->amca_unroller
;
358 los
.as_unroll
= amcap
->amca_lopts_on
;
361 if(amcap
->amca_hook_pre
!= NULL
)
362 (*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
);
363 rv
= n_source_macro(n_LEXINPUT_NONE
, amp
->am_name
, args_base
);
364 if(amcap
->amca_hook_post
!= NULL
)
365 (*amcap
->amca_hook_post
)(amcap
->amca_hook_arg
);
366 a_amv_lopts
= los_save
;
368 if(amcap
->amca_unroller
== NULL
){
369 if(los
.as_lopts
!= NULL
)
370 a_amv_lopts_unroll(&los
.as_lopts
);
372 *amcap
->amca_unroller
= los
.as_lopts
;
378 a_amv_mac_show(enum a_amv_mac_flags amf
){
379 size_t lc
, mc
, ti
, i
;
387 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
389 n_perr(_("Can't create temporary file for `define' or `account' listing"),
394 amf
&= a_AMV_MF_TYPE_MASK
;
395 typestr
= (amf
& a_AMV_MF_ACC
) ? "account" : "define";
397 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
398 struct a_amv_mac
*amp
;
400 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
401 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
402 struct a_amv_mac_line
**amlpp
;
409 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
410 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
411 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
413 fputs((*amlpp
)->aml_dat
, fp
);
422 page_or_print(fp
, lc
);
424 rv
= (ferror(fp
) == 0);
432 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
434 ui32_t line_cnt
, maxlen
;
436 struct linelist
*ll_next
;
437 struct a_amv_mac_line
*ll_amlp
;
438 } *llp
, *ll_head
, *ll_tail
;
439 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
440 struct a_amv_mac
*amp
;
444 memset(&line
, 0, sizeof line
);
448 /* Read in the lines which form the macro content */
449 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
453 n
.i
= n_lex_input(n_LEXINPUT_CTX_BASE
| n_LEXINPUT_NL_ESC
, "",
454 &line
.s
, &line
.l
, NULL
);
458 n_err(_("Unterminated %s definition: %s\n"),
459 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
462 if(a_amv_mac_is_closing_angle(line
.s
))
465 /* Trim WS, remember amount of leading spaces for display purposes */
466 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
468 leaspc
= (leaspc
+ 8u) & ~7u;
473 for(; n
.ui
> 0 && whitechar(cp
[n
.ui
- 1]); --n
.ui
)
478 maxlen
= MAX(maxlen
, n
.ui
);
481 if(LIKELY(++line_cnt
< UI32_MAX
)){
482 struct a_amv_mac_line
*amlp
;
484 llp
= salloc(sizeof *llp
);
488 ll_tail
->ll_next
= llp
;
491 llp
->ll_amlp
= amlp
= smalloc(sizeof(*amlp
) -
492 VFIELD_SIZEOF(struct a_amv_mac_line
, aml_dat
) + n
.ui
);
493 amlp
->aml_len
= n
.ui
-1;
494 amlp
->aml_prespc
= leaspc
;
495 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
497 n_err(_("Too much content in %s definition: %s\n"),
498 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
503 /* Create the new macro */
504 n
.s
= strlen(name
) +1;
505 amp
= smalloc(sizeof(*amp
) - VFIELD_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
507 amp
->am_maxlen
= maxlen
;
508 amp
->am_line_cnt
= line_cnt
;
510 amp
->am_lopts
= NULL
;
511 memcpy(amp
->am_name
, name
, n
.s
);
513 struct a_amv_mac_line
**amlpp
;
515 amp
->am_line_dat
= amlpp
= smalloc(sizeof(*amlpp
) * ++line_cnt
);
516 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
517 *amlpp
++ = llp
->ll_amlp
;
521 /* Finally check whether such a macro already exists, in which case we throw
522 * it all away again. At least we know it would have worked */
523 if(a_amv_mac_lookup(name
, amp
, amf
) != NULL
){
524 n_err(_("There is already a %s of name: %s\n"),
525 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
537 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
540 free(amp
->am_line_dat
);
547 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
548 struct a_amv_mac
*amp
;
554 if(LIKELY(name
[0] != '*' || name
[1] != '\0')){
555 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
556 n_err(_("%s not defined: %s\n"),
557 (amf
& a_AMV_MF_ACC
? "Account" : "Macro"), name
);
561 struct a_amv_mac
**ampp
, *lamp
;
563 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[NELEM(a_amv_macs
)]);
565 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
566 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
567 /* xxx Expensive but rare: be simple */
568 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
569 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
581 a_amv_mac_free(struct a_amv_mac
*amp
){
582 struct a_amv_mac_line
**amlpp
;
585 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
587 free(amp
->am_line_dat
);
593 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
594 struct a_amv_var
*oavp
){
595 struct a_amv_var
*avp
;
599 /* Propagate unrolling up the stack, as necessary */
604 if((alp
= alp
->as_up
) == NULL
)
608 /* Check whether this variable is handled yet */
609 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
610 if(!strcmp(avp
->av_name
, name
))
613 nl
= strlen(name
) +1;
614 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
615 avp
= smalloc(sizeof(*avp
) - VFIELD_SIZEOF(struct a_amv_var
, av_name
) +
617 avp
->av_link
= alp
->as_lopts
;
619 memcpy(avp
->av_name
, name
, nl
);
621 avp
->av_value
= NULL
;
627 avp
->av_value
= avp
->av_name
+ nl
;
628 avp
->av_flags
= oavp
->av_flags
;
629 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
631 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
639 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
640 struct a_amv_lostack
*save_alp
;
642 struct a_amv_var
*x
, *avp
;
647 reset
= !(pstate
& PS_ROOT
);
649 save_alp
= a_amv_lopts
;
655 vok_vset(x
->av_name
, x
->av_value
);
660 a_amv_lopts
= save_alp
;
665 a_amv_var_copy(char const *str
){
673 len
= strlen(str
) +1;
675 memcpy(news
, str
, len
);
682 a_amv_var_free(char *cp
){
690 a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
){
703 /* Invalidate any resolved folder then, too
706 ok
= !(pstate
& PS_ROOT
);
708 ok_vclear(_folder_resolved
);
720 case ok_b_skipemptybody
:
728 if((ul
= strtoul(*val
, NULL
, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
729 n_err(_("Invalid *umask* setting: %s\n"), *val
);
736 flag
= (enable
&& !(options
& OPT_VERB
))
737 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
740 DBG( n_err("Implementation error: never heard of %u\n", ok
); )
755 a_amv_var_check_nocntrls(char const *val
){
759 while((c
= *val
++) != '\0')
767 a_amv_var_check_num(char const *val
, bool_t pos
){ /* TODO intmax_t anywhere! */
768 /* TODO The internal/environment variables which are num= or posnum= should
769 * TODO gain special lookup functions, or the return should be void* and
770 * TODO castable to integer; i.e. no more strtoX() should be needed.
771 * TODO I.e., the result of this function should instead be stored.
772 * TODO Use intmax_t IF that is sizeof(void*) only? */
778 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
780 union {long s
; unsigned long u
;} i
;
783 i
.s
= strtol(val
, &eptr
, 0); /* TODO strtoimax() */
786 ((i
.s
== LONG_MIN
|| i
.s
== LONG_MAX
) && errno
== ERANGE
))
788 #if INT_MIN != LONG_MIN
789 else if(i
.s
< INT_MIN
)
792 #if INT_MAX != LONG_MAX
793 else if(i
.s
> INT_MAX
)
797 i
.u
= strtoul(val
, &eptr
, 0); /* TODO strtoumax() */
799 if(*eptr
!= '\0' || (i
.u
== ULONG_MAX
&& errno
== ERANGE
))
801 #if UINT_MAX != ULONG_MAX
802 else if(i
.u
> UINT_MAX
)
812 a_amv_var_canonify(char const *vn
){
817 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
819 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
826 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
828 struct a_amv_var_map
const *avmp
;
831 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
832 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
834 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
835 ui32_t x
= a_amv_var_revmap
[i
];
837 if(x
== a_AMV_VAR_REV_ILL
)
840 avmp
= &a_amv_var_map
[x
];
841 if(avmp
->avm_hash
== hash
&&
842 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
843 avcp
->avc_map
= avmp
;
844 avcp
->avc_okey
= (enum okeys
)x
;
848 if(++i
== a_AMV_VAR_REV_PRIME
){
849 #ifdef a_AMV_VAR_REV_WRAPAROUND
856 avcp
->avc_map
= NULL
;
860 return (avcp
!= NULL
);
864 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
867 struct a_amv_var_map
const *avmp
;
868 struct a_amv_var
*avp
;
872 struct a_amv_var
**avpp
, *lavp
;
874 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
876 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
877 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
878 /* Relink as head, hope it "sorts on usage" over time.
879 * _clear() relies on this behaviour */
881 lavp
->av_link
= avp
->av_link
;
882 avp
->av_link
= *avpp
;
889 /* If this is not an assembled variable we need to consider some special
890 * initialization cases and eventually create the variable anew */
891 if(LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
892 /* Does it have an import-from-environment flag? */
893 if(UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
894 if(LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
895 /* May be better not to use that one, though? */
896 bool_t isempty
, isbltin
;
898 isempty
= (*cp
== '\0' &&
899 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
900 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
903 if(UNLIKELY(isempty
)){
906 }else if(LIKELY(*cp
!= '\0')){
907 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
908 !a_amv_var_check_nocntrls(cp
))){
909 n_err(_("Ignoring environment, control characters "
910 "invalid in variable: %s\n"), avcp
->avc_name
);
913 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
914 !a_amv_var_check_num(cp
, FAL0
))){
915 n_err(_("Environment variable value not a number "
916 "or out of range: %s\n"), avcp
->avc_name
);
919 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
920 !a_amv_var_check_num(cp
, TRU1
))){
921 n_err(_("Environment variable value not a number, "
922 "negative or out of range: %s\n"), avcp
->avc_name
);
930 /* A first-time init switch is to be handled now and here */
931 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
932 static struct a_amv_var_defval
const **arr
,
933 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
937 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
939 arr
[i
] = &a_amv_var_i3vals
[i
];
942 for(i
= 0; arr
[i
] != NULL
; ++i
)
943 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
944 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? ""
945 : arr
[i
]->avdv_value
;
946 /* Remove this entry, hope entire block becomes no-op asap */
949 while(arr
[i
++] != NULL
);
953 if(i3val_nonew
== TRUM1
)
954 avp
= (struct a_amv_var
*)-1;
959 /* The virtual variables */
960 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
961 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
962 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
963 avp
= UNCONST(a_amv_var_virts
[i
].avv_var
);
969 /* Place this last because once it is set first the variable will never
970 * be removed again and thus match in the first block above */
971 if(UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
972 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
973 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
974 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? ""
975 : a_amv_var_defvals
[i
].avdv_value
;
986 return (avp
!= NULL
);
989 struct a_amv_var
**avpp
;
992 l
= strlen(avcp
->avc_name
) +1;
993 avcp
->avc_var
= avp
= smalloc(sizeof(*avp
) -
994 VFIELD_SIZEOF(struct a_amv_var
, av_name
) + l
);
995 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
997 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
998 avp
->av_value
= a_amv_var_copy(cp
);
1002 avp
->av_flags
= avmp
->avm_flags
;
1004 if(avp
->av_flags
& a_AMV_VF_VIP
)
1005 a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
);
1006 if(avp
->av_flags
& a_AMV_VF_ENV
)
1007 a_amv_var__putenv(avcp
, avp
);
1013 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1015 struct a_amv_var
*avp
;
1017 struct a_amv_var_map
const *avmp
;
1022 rv
= a_amv_var_clear(avcp
, force_env
);
1026 if((avmp
= avcp
->avc_map
) != NULL
){
1029 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1030 !(pstate
& PS_ROOT
))){
1031 n_err(_("Variable is readonly: %s\n"), avcp
->avc_name
);
1034 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1035 n_err(_("Variable must not be empty: %s\n"), avcp
->avc_name
);
1038 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1039 !a_amv_var_check_nocntrls(value
))){
1040 n_err(_("Variable forbids control characters: %s\n"),
1044 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1045 !a_amv_var_check_num(value
, FAL0
))){
1046 n_err(_("Variable value not a number or out of range: %s\n"),
1050 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1051 !a_amv_var_check_num(value
, TRU1
))){
1052 n_err(_("Variable value not a number, negative or out of range: %s\n"),
1056 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1057 !(pstate
& (PS_ROOT
| PS_STARTED
)))){
1058 n_err(_("Variable cannot be set in a resource file: %s\n"),
1065 a_amv_var_lookup(avcp
, TRU1
);
1067 /* Don't care what happens later on, store this in the unroll list */
1068 if(a_amv_lopts
!= NULL
)
1069 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1071 if((avp
= avcp
->avc_var
) == NULL
){
1072 struct a_amv_var
**avpp
;
1073 size_t l
= strlen(avcp
->avc_name
) +1;
1075 avcp
->avc_var
= avp
= smalloc(sizeof(*avp
) -
1076 VFIELD_SIZEOF(struct a_amv_var
, av_name
) + l
);
1077 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1082 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1083 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1086 oval
= avp
->av_value
;
1089 avp
->av_value
= a_amv_var_copy(value
);
1091 /* Via `set' etc. the user may give even boolean options non-boolean
1092 * values, ignore that and force boolean */
1093 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1094 if((options
& OPT_D_VV
) && *value
!= '\0')
1095 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1096 avcp
->avc_name
, value
);
1097 avp
->av_value
= UNCONST(value
= "");
1099 avp
->av_value
= a_amv_var_copy(value
);
1101 /* Check if update allowed XXX wasteful on error! */
1102 if((avp
->av_flags
& a_AMV_VF_VIP
) &&
1103 !(rv
= a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
))){
1104 char *cp
= avp
->av_value
;
1106 avp
->av_value
= oval
;
1111 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1112 avp
->av_flags
|= a_AMV_VF_LINKED
;
1113 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1114 rv
= a_amv_var__putenv(avcp
, avp
);
1116 a_amv_var_free(oval
);
1123 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1131 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1133 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1135 if((rv
= (putenv(cp
) == 0))){
1138 if((ocp
= avp
->av_env
) != NULL
)
1149 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1150 struct a_amv_var
**avpp
, *avp
;
1151 struct a_amv_var_map
const *avmp
;
1157 if(LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1158 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1159 !(pstate
& PS_ROOT
))){
1160 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1163 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1164 !a_amv_var_check_vips(avcp
->avc_okey
, FAL0
, NULL
))
1170 if(UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1173 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1174 }else if(!(pstate
& (PS_ROOT
| PS_ROBOT
)) && (options
& OPT_D_V
))
1175 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1177 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1178 avcp
->avc_var
= NULL
;
1184 if(a_amv_lopts
!= NULL
)
1185 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1187 avp
= avcp
->avc_var
;
1188 avcp
->avc_var
= NULL
;
1189 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1190 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1191 *avpp
= (*avpp
)->av_link
;
1195 char *envval
= NULL
;
1197 char *envval
= avp
->av_env
;
1199 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1200 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1202 a_amv_var_free(avp
->av_value
);
1205 /* XXX Fun part, extremely simple-minded for now: if this variable has
1206 * XXX a default value, immediately reinstantiate it! */
1207 if(UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1208 a_amv_var_lookup(avcp
, TRU1
);
1215 a_amv_var__clearenv(char const *name
, char *value
){
1217 extern char **environ
;
1229 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1234 while(*ecpp
++ != NULL
);
1244 a_amv_var_show_all(void){
1245 struct n_string msg
, *msgp
;
1248 struct a_amv_var
*avp
;
1249 char const **vacp
, **cap
;
1252 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1253 n_perr(_("Can't create temporary file for `set' listing"), 0);
1257 /* We need to instantiate first-time-inits and default values here, so that
1258 * they will be regular members of our _vars[] table */
1259 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1260 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1261 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1262 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1264 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1265 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1267 no
+= a_AMV_VAR_VIRTS_CNT
;
1269 vacp
= salloc(no
* sizeof(*vacp
));
1271 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1272 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1273 *cap
++ = avp
->av_name
;
1274 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1275 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1278 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1281 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1282 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1283 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1286 page_or_print(fp
, i
);
1293 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1297 rv
= strcmp(*(char**)UNCONST(s1
), *(char**)UNCONST(s2
));
1303 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1304 struct a_amv_var_carrier avc
;
1309 msgp
= n_string_trunc(msgp
, 0);
1312 a_amv_var_revlookup(&avc
, name
);
1313 if(!a_amv_var_lookup(&avc
, FAL0
)){
1316 msgp
= n_string_assign_cp(msgp
, _("No such variable: "));
1317 s
.s
= UNCONST(name
);
1319 msgp
= n_shexp_quote(msgp
, &s
, FAL0
);
1323 if(options
& OPT_D_V
){
1324 if(avc
.avc_map
== NULL
){
1325 msgp
= n_string_push_c(msgp
, '#');
1326 msgp
= n_string_push_cp(msgp
, "assembled");
1334 {a_AMV_VF_VIRT
, "virtual"},
1335 {a_AMV_VF_RDONLY
, "readonly"},
1336 {a_AMV_VF_NODEL
, "nodelete"},
1337 {a_AMV_VF_NOTEMPTY
, "notempty"},
1338 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1339 {a_AMV_VF_NUM
, "number"},
1340 {a_AMV_VF_POSNUM
, "positive-number"},
1341 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1342 {a_AMV_VF_ENV
, "sync-environ"},
1343 {a_AMV_VF_I3VAL
, "initial-value"},
1344 {a_AMV_VF_DEFVAL
, "default-value"},
1345 {a_AMV_VF_LINKED
, "`environ' link"}
1348 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[NELEM(tbase
)]); ++tp
)
1349 if(avc
.avc_var
->av_flags
& tp
->flags
){
1350 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1351 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1356 msgp
= n_string_push_cp(msgp
, "\n ");
1359 if(avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)
1360 msgp
= n_string_push_cp(msgp
, "# ");
1361 UNINIT(quote
, NULL
);
1362 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1363 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1364 if(strcmp(quote
, avc
.avc_var
->av_value
))
1365 msgp
= n_string_push_cp(msgp
, "wysh ");
1367 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1368 msgp
= n_string_push_cp(msgp
, "environ ");
1369 msgp
= n_string_push_cp(msgp
, "set ");
1370 msgp
= n_string_push_cp(msgp
, name
);
1371 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1372 msgp
= n_string_push_c(msgp
, '=');
1373 msgp
= n_string_push_cp(msgp
, quote
);
1377 msgp
= n_string_push_c(msgp
, '\n');
1378 fputs(n_string_cp(msgp
), fp
);
1380 return (i
> 0 ? 2 : 1);
1384 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1385 char *cp
, *cp2
, *varbuf
, c
;
1391 while((cp
= *ap
++) != NULL
){
1393 cp2
= varbuf
= salloc(strlen(cp
) +1);
1395 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1396 if(cntrlchar(c
) || whitechar(c
)){
1397 n_err(_("Variable name with control character ignored: %s\n"),
1411 n_err(_("Empty variable name ignored\n"));
1414 struct a_amv_var_carrier avc
;
1417 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1418 varbuf
= &varbuf
[2];
1420 a_amv_var_revlookup(&avc
, varbuf
);
1423 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1425 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1440 if((args
= v
)[0] == NULL
){
1441 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1445 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1447 n_err(_("Synopsis: define: <name> {\n"));
1451 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1458 c_undefine(void *v
){
1466 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1467 while(*++args
!= NULL
);
1474 struct a_amv_mac_call_args amca
;
1475 char const *errs
, *name
;
1476 struct a_amv_mac
*amp
;
1483 if((args
= v
)[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)){
1484 errs
= _("Synopsis: call: <%s>\n");
1489 if((amp
= a_amv_mac_lookup(name
= *args
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1490 errs
= _("Undefined macro `call'ed: %s\n");
1494 memset(&amca
, 0, sizeof amca
);
1495 amca
.amca_name
= name
;
1496 amca
.amca_amp
= amp
;
1497 rv
= (a_amv_mac_exec(&amca
) == FAL0
);
1507 check_folder_hook(bool_t nmail
){ /* TODO temporary, v15: drop */
1508 struct a_amv_mac_call_args amca
;
1509 struct a_amv_mac
*amp
;
1516 var
= salloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1518 /* First try the fully resolved path */
1519 snprintf(var
, len
, "folder-hook-%s", mailname
);
1520 if((cp
= vok_vlook(var
)) != NULL
)
1523 /* If we are under *folder*, try the usual +NAME syntax, too */
1524 if(displayname
[0] == '+'){
1525 char *x
= mailname
+ len
;
1527 for(; x
> mailname
; --x
)
1529 snprintf(var
, len
, "folder-hook-+%s", x
);
1530 if((cp
= vok_vlook(var
)) != NULL
)
1536 /* Plain *folder-hook* is our last try */
1537 if((cp
= ok_vlook(folder_hook
)) == NULL
)
1541 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1542 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1543 n_shexp_quote_cp(displayname
, FAL0
), cp
);
1548 memset(&amca
, 0, sizeof amca
);
1549 amca
.amca_name
= cp
;
1550 amca
.amca_amp
= amp
;
1551 pstate
&= ~PS_HOOK_MASK
;
1553 amca
.amca_unroller
= NULL
;
1554 pstate
|= PS_HOOK_NEWMAIL
;
1556 amca
.amca_unroller
= &a_amv_folder_hook_lopts
;
1559 amca
.amca_lopts_on
= TRU1
;
1560 rv
= a_amv_mac_exec(&amca
);
1561 pstate
&= ~PS_HOOK_MASK
;
1569 call_compose_mode_hook(char const *macname
, /* TODO temporary, v15: drop */
1570 void (*hook_pre
)(void *), void *hook_arg
){
1571 struct a_amv_mac_call_args amca
;
1572 struct a_amv_mac
*amp
;
1575 if((amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
1576 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1579 memset(&amca
, 0, sizeof amca
);
1580 amca
.amca_name
= macname
;
1581 amca
.amca_amp
= amp
;
1582 amca
.amca_unroller
= &a_amv_compose_lopts
;
1583 amca
.amca_hook_pre
= hook_pre
;
1584 amca
.amca_hook_arg
= hook_arg
;
1585 amca
.amca_lopts_on
= TRU1
;
1586 pstate
&= ~PS_HOOK_MASK
;
1588 a_amv_mac_exec(&amca
);
1589 pstate
&= ~PS_HOOK_MASK
;
1596 struct a_amv_mac_call_args amca
;
1597 struct a_amv_mac
*amp
;
1599 int rv
, i
, oqf
, nqf
;
1604 if((args
= v
)[0] == NULL
){
1605 rv
= (a_amv_mac_show(a_AMV_MF_ACC
) == FAL0
);
1609 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
1610 if(args
[2] != NULL
){
1611 n_err(_("Synopsis: account: <name> {\n"));
1614 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
1615 n_err(_("`account': cannot use reserved name: %s\n"),
1619 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACC
) == FAL0
);
1623 if(pstate
& PS_HOOK_MASK
){
1624 n_err(_("`account': can't change account from within a hook\n"));
1628 save_mbox_for_possible_quitstuff();
1631 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1632 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACC
)) == NULL
) {
1633 n_err(_("`account': account does not exist: %s\n"), args
[0]);
1637 oqf
= savequitflags();
1639 if(a_amv_acc_curr
!= NULL
){
1640 if(a_amv_acc_curr
->am_lopts
!= NULL
)
1641 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
1642 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DEL
)
1643 a_amv_mac_free(a_amv_acc_curr
);
1646 account_name
= (amp
!= NULL
) ? amp
->am_name
: NULL
;
1647 a_amv_acc_curr
= amp
;
1650 assert(amp
->am_lopts
== NULL
);
1651 memset(&amca
, 0, sizeof amca
);
1652 amca
.amca_name
= amp
->am_name
;
1653 amca
.amca_amp
= amp
;
1654 amca
.amca_unroller
= &
->am_lopts
;
1655 amca
.amca_lopts_on
= TRU1
;
1656 if(!a_amv_mac_exec(&amca
)){
1657 /* XXX account switch incomplete, unroll? */
1658 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
1663 if((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
){
1664 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1665 restorequitflags(oqf
);
1666 if((i
= setfile("%", 0)) < 0)
1668 check_folder_hook(FAL0
);
1669 if(i
> 0 && !ok_blook(emptystart
))
1671 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1672 restorequitflags(nqf
);
1681 c_unaccount(void *v
){
1689 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACC
);
1690 while(*++args
!= NULL
);
1696 c_localopts(void *v
){
1703 if(a_amv_lopts
== NULL
){
1704 n_err(_("Cannot use `localopts' but from within a "
1705 "`define' or `account'\n"));
1709 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
1717 temporary_localopts_free(void){ /* XXX intermediate hack */
1719 if(a_amv_compose_lopts
!= NULL
){
1720 void *save
= a_amv_lopts
;
1723 a_amv_lopts_unroll(&a_amv_compose_lopts
);
1724 a_amv_compose_lopts
= NULL
;
1731 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1733 if(a_amv_folder_hook_lopts
!= NULL
){
1734 void *save
= a_amv_lopts
;
1737 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
1738 a_amv_folder_hook_lopts
= NULL
;
1745 n_var_oklook(enum okeys okey
){
1746 struct a_amv_var_carrier avc
;
1748 struct a_amv_var_map
const *avmp
;
1751 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1752 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1753 avc
.avc_hash
= avmp
->avm_hash
;
1754 avc
.avc_okey
= okey
;
1756 if(a_amv_var_lookup(&avc
, FAL0
))
1757 rv
= avc
.avc_var
->av_value
;
1765 n_var_okset(enum okeys okey
, uintptr_t val
){
1766 struct a_amv_var_carrier avc
;
1768 struct a_amv_var_map
const *avmp
;
1771 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1772 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1773 avc
.avc_hash
= avmp
->avm_hash
;
1774 avc
.avc_okey
= okey
;
1776 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? "" : (char const*)val
), FAL0
);
1782 n_var_okclear(enum okeys okey
){
1783 struct a_amv_var_carrier avc
;
1785 struct a_amv_var_map
const *avmp
;
1788 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1789 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1790 avc
.avc_hash
= avmp
->avm_hash
;
1791 avc
.avc_okey
= okey
;
1793 rv
= a_amv_var_clear(&avc
, FAL0
);
1799 n_var_voklook(char const *vokey
){
1800 struct a_amv_var_carrier avc
;
1804 a_amv_var_revlookup(&avc
, vokey
);
1806 rv
= a_amv_var_lookup(&avc
, FAL0
) ? avc
.avc_var
->av_value
: NULL
;
1812 n_var_vokset(char const *vokey
, uintptr_t val
){
1813 struct a_amv_var_carrier avc
;
1817 a_amv_var_revlookup(&avc
, vokey
);
1819 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? "" : (char const*)val
), FAL0
);
1825 n_var_vokclear(char const *vokey
){
1826 struct a_amv_var_carrier avc
;
1830 a_amv_var_revlookup(&avc
, vokey
);
1832 ok
= a_amv_var_clear(&avc
, FAL0
);
1839 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
1840 enum okey_xlook_mode oxm
){
1841 struct a_amv_var_carrier avc
;
1842 struct str
const *us
;
1845 struct a_amv_var_map
const *avmp
;
1848 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1850 /* For simplicity: allow this case too */
1851 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
)))
1854 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1855 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1856 avc
.avc_okey
= okey
;
1858 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1859 nlen
= strlen(avc
.avc_name
);
1860 nbuf
= salloc(nlen
+ 1 + us
->l
+1);
1861 memcpy(nbuf
, avc
.avc_name
, nlen
);
1864 /* One of .url_u_h_p and .url_h_p we test in here */
1865 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1866 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1867 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1868 if(a_amv_var_lookup(&avc
, FAL0
))
1873 us
= &urlp
->url_h_p
;
1874 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1875 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1876 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1877 if(a_amv_var_lookup(&avc
, FAL0
)){
1879 rv
= avc
.avc_var
->av_value
;
1885 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
1890 #endif /* HAVE_SOCKETS */
1898 if(*(ap
= v
) == NULL
){
1899 a_amv_var_show_all();
1902 err
= !a_amv_var_c_set(ap
, FAL0
);
1913 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
1914 err
|= !n_var_vokclear(*ap
);
1924 if(*(ap
= v
) == NULL
)
1927 struct n_string msg
, *msgp
= &msg
;
1929 msgp
= n_string_creat(msgp
);
1930 for(; *ap
!= NULL
; ++ap
)
1931 a_amv_var_show(*ap
, stdout
, msgp
);
1935 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1940 struct a_amv_var_carrier avc
;
1944 sighandler_type sigint
;
1947 sigint
= safe_signal(SIGINT
, SIG_IGN
);
1949 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
1950 memset(&avc
, 0, sizeof avc
);
1952 a_amv_var_revlookup(&avc
, *argv
);
1954 if(avc
.avc_map
!= NULL
){
1955 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
1956 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
1960 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
1961 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
1967 a_amv_var_lookup(&avc
, FAL0
);
1969 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1971 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1974 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
1975 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
1976 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1983 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
1989 off_t l
= fsize(nf
);
1992 base
= salloc((size_t)l
+1);
1994 for(l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
1995 if(c
== '\n' || c
== '\r'){
1999 *val
= (char)(uc_i
)c
;
2005 if(!a_amv_var_set(&avc
, base
, FAL0
))
2010 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2016 safe_signal(SIGINT
, sigint
);
2023 struct a_amv_var_carrier avc
;
2029 if((islnk
= !strcmp(*(ap
= v
), "link")) || !strcmp(*ap
, "unlink")){
2030 for(err
= 0; *++ap
!= NULL
;){
2031 a_amv_var_revlookup(&avc
, *ap
);
2033 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2034 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2036 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2038 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2039 if(options
& OPT_D_V
)
2040 n_err(_("`environ': link: already established: %s\n"), *ap
);
2043 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2044 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2045 a_amv_var__putenv(&avc
, avc
.avc_var
);
2047 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2050 char const *evp
= getenv(*ap
);
2053 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2055 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2061 }else if(!strcmp(*ap
, "set"))
2062 err
= !a_amv_var_c_set(++ap
, TRU1
);
2063 else if(!strcmp(*ap
, "unset")){
2064 for(err
= 0; *++ap
!= NULL
;){
2065 a_amv_var_revlookup(&avc
, *ap
);
2067 if(!a_amv_var_clear(&avc
, TRU1
))
2071 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));