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 Otherwise, the `localopts' should be an attribute of the lex_input.c
9 *@ TODO command context, so that it belongs to the execution context
10 *@ TODO we are running in, instead of being global data. See, e.g.,
11 *@ TODO the a_LEX_SLICE comment in lex_input.c.
12 *@ TODO . undefining and overwriting a macro should always be possible:
13 *@ TODO simply place the thing in a delete-later list and replace the
14 *@ TODO accessible entry! (instant delete if on top recursion level.)
15 *@ TODO . Likewise, overwriting an existing should be like delete+create
16 *@ TODO . once we can have non-fatal !0 returns for commands, we should
17 *@ TODO return error if "(environ)? unset" goes for non-existent.
19 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
20 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
23 * Copyright (c) 1980, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 #define n_FILE accmacvar
53 #ifndef HAVE_AMALGAMATION
57 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
58 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
61 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
62 #define a_AMV_PRIME HSHSIZE
63 #define a_AMV_NAME2HASH(N) torek_hash(N)
64 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
68 a_AMV_MF_ACC
= 1<<0, /* This macro is an `account' */
69 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACC
,
70 a_AMV_MF_UNDEF
= 1<<1, /* Unlink after lookup */
71 a_AMV_MF_DEL
= 1<<7, /* Current `account': deleted while active */
75 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
76 * _IMPORT implies _ENV; it doesn't verify anything... */
79 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
80 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
81 a_AMV_VF_NOLOPTS
= 1<<2, /* May not be tracked by `localopts' */
82 a_AMV_VF_RDONLY
= 1<<3, /* May not be set by user */
83 a_AMV_VF_NODEL
= 1<<4, /* May not be deleted */
84 a_AMV_VF_NOTEMPTY
= 1<<5, /* May not be assigned an empty value */
85 a_AMV_VF_NOCNTRLS
= 1<<6, /* Value may not contain control characters */
86 a_AMV_VF_NUM
= 1<<7, /* Value must be a 32-bit number */
87 a_AMV_VF_POSNUM
= 1<<8, /* Value must be positive 32-bit number */
88 a_AMV_VF_LOWER
= 1<<9, /* Values will be stored in a lowercase version */
89 a_AMV_VF_VIP
= 1<<10, /* Wants _var_check_vips() evaluation */
90 a_AMV_VF_IMPORT
= 1<<11, /* Import ONLY from environ (before PS_STARTED) */
91 a_AMV_VF_ENV
= 1<<12, /* Update environment on change */
92 a_AMV_VF_I3VAL
= 1<<13, /* Has an initial value */
93 a_AMV_VF_DEFVAL
= 1<<14, /* Has a default value */
94 a_AMV_VF_LINKED
= 1<<15, /* `environ' linked */
95 a_AMV_VF__MASK
= (1<<(15+1)) - 1
99 struct a_amv_mac
*am_next
;
100 ui32_t am_maxlen
; /* of any line in .am_line_dat */
101 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
102 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
103 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
104 ui8_t am_flags
; /* enum a_amv_mac_flags */
105 char am_name
[n_VFIELD_SIZE(7)]; /* of this macro */
107 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
109 struct a_amv_mac_line
{
111 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
112 char aml_dat
[n_VFIELD_SIZE(0)];
115 struct a_amv_mac_call_args
{
116 char const *amca_name
;
117 struct a_amv_mac
const *amca_amp
;
118 struct a_amv_var
**amca_unroller
;
119 void (*amca_hook_pre
)(void *);
121 bool_t amca_lopts_on
;
122 bool_t amca_ps_hook_mask
;
126 struct a_amv_lostack
{
127 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
128 struct a_amv_mac_call_args
*as_amcap
;
129 struct a_amv_lostack
*as_up
; /* Outer context */
130 struct a_amv_var
*as_lopts
;
131 bool_t as_unroll
; /* Unrolling enabled? */
136 struct a_amv_var
*av_link
;
139 char *av_env
; /* Actively managed putenv(3) memory */
141 ui16_t av_flags
; /* enum a_amv_var_flags */
142 char av_name
[n_VFIELD_SIZE(6)];
144 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
146 struct a_amv_var_map
{
149 ui16_t avm_flags
; /* enum a_amv_var_flags */
151 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
153 struct a_amv_var_virt
{
156 struct a_amv_var
const *avv_var
;
159 struct a_amv_var_defval
{
162 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
165 struct a_amv_var_carrier
{
166 char const *avc_name
;
169 struct a_amv_var
*avc_var
;
170 struct a_amv_var_map
const *avc_map
;
175 /* Include the constant mk-okey-map.pl output */
179 /* True boolean visualization: this string will not be copied to heap memory
180 * in a_amv_var_copy(), but we must avoid confusion with identical user data.
181 * While here, add a special "0" one and speed up *.exit-status* assignments! */
182 static char const a_amv_var_1
[] = "1";
183 static char const a_amv_var_0
[] = "0";
185 /* The currently active account */
186 static struct a_amv_mac
*a_amv_acc_curr
;
188 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
190 /* Unroll list of currently running macro stack */
191 static struct a_amv_lostack
*a_amv_lopts
;
193 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
195 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
196 * TODO today via a static lostack, it should be a field in mailbox, once that
197 * TODO is a real multi-instance object */
198 static struct a_amv_var
*a_amv_folder_hook_lopts
;
200 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
201 static struct a_amv_var
*a_amv_compose_lopts
;
203 /* Does cp consist solely of WS and a } */
204 static bool_t
a_amv_mac_is_closing_angle(char const *cp
);
206 /* Lookup for macros/accounts */
207 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
208 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
210 /* Execute a macro; amcap must reside in LOFI memory */
211 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
213 static void a_amv_mac__finalize(void *vp
);
215 /* User display helpers */
216 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
218 /* _def() returns error for faulty definitions and already existing * names,
219 * _undef() returns error if a named thing doesn't exist */
220 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
221 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
224 static void a_amv_mac_free(struct a_amv_mac
*amp
);
226 /* Update replay-log */
227 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
228 struct a_amv_var
*oavp
);
229 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
231 /* Special cased value string allocation */
232 static char *a_amv_var_copy(char const *str
);
233 static void a_amv_var_free(char *cp
);
235 /* Check for special housekeeping */
236 static bool_t
a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
);
238 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
239 static bool_t
a_amv_var_check_nocntrls(char const *val
);
240 static bool_t
a_amv_var_check_num(char const *val
, bool_t pos
);
242 /* If a variable name begins with a lowercase-character and contains at
243 * least one '@', it is converted to all-lowercase. This is necessary
244 * for lookups of names based on email addresses.
245 * Following the standard, only the part following the last '@' should
246 * be lower-cased, but practice has established otherwise here */
247 static char const *a_amv_var_canonify(char const *vn
);
249 /* Try to reverse lookup an option name to an enum okeys mapping.
250 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
251 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
254 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
255 * Sets .avc_prime; .avc_var is NULL if not found.
256 * Here it is where we care for _I3VAL and _DEFVAL, too.
257 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
258 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
259 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
260 * return FAL0, then! */
261 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
264 /* Set var from .avc_(map|name|hash), return success */
265 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
268 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
269 struct a_amv_var
*avp
);
271 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
272 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
274 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
276 /* List all variables */
277 static void a_amv_var_show_all(void);
279 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
281 /* Actually do print one, return number of lines written */
282 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
284 /* Shared c_set() and c_environ():set impl, return success */
285 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
288 a_amv_mac_is_closing_angle(char const *cp
){
292 while(spacechar(*cp
))
295 if((rv
= (*cp
++ == '}'))){
296 while(spacechar(*cp
))
304 static struct a_amv_mac
*
305 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
306 enum a_amv_mac_flags amf
){
307 struct a_amv_mac
*amp
, **ampp
;
309 enum a_amv_mac_flags save_amf
;
313 amf
&= a_AMV_MF_TYPE_MASK
;
314 h
= a_AMV_NAME2HASH(name
);
315 h
= a_AMV_HASH2PRIME(h
);
316 ampp
= &a_amv_macs
[h
];
318 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
319 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
320 !strcmp(amp
->am_name
, name
)){
321 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
324 *ampp
= amp
->am_next
;
326 if((amf
& a_AMV_MF_ACC
) &&
327 account_name
!= NULL
&& !strcmp(account_name
, name
)){
328 amp
->am_flags
|= a_AMV_MF_DEL
;
329 n_err(_("Delayed deletion of active account: %s\n"), name
);
332 amp
= (struct a_amv_mac
*)-1;
339 ampp
= &a_amv_macs
[h
];
340 newamp
->am_next
= *ampp
;
350 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
351 struct a_amv_lostack
*losp
;
352 struct a_amv_mac_line
**amlp
;
353 char **args_base
, **args
;
354 struct a_amv_mac
const *amp
;
358 amp
= amcap
->amca_amp
;
359 /* XXX Unfortunately we yet need to dup the macro lines! :( */
360 args_base
= args
= smalloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
361 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
362 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
365 losp
= n_lofi_alloc(sizeof *losp
);
366 losp
->as_global_saved
= a_amv_lopts
;
367 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
368 losp
->as_up
= losp
->as_global_saved
;
369 losp
->as_lopts
= NULL
;
372 losp
->as_lopts
= *amcap
->amca_unroller
;
374 losp
->as_unroll
= amcap
->amca_lopts_on
;
377 if(amcap
->amca_hook_pre
!= NULL
)
378 (*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
);
379 rv
= n_source_macro(n_LEXINPUT_NONE
, amp
->am_name
, args_base
,
380 &a_amv_mac__finalize
, losp
);
386 a_amv_mac__finalize(void *vp
){
387 struct a_amv_mac_call_args
*amcap
;
388 struct a_amv_lostack
*losp
;
392 a_amv_lopts
= losp
->as_global_saved
;
394 if((amcap
= losp
->as_amcap
)->amca_unroller
== NULL
){
395 if(losp
->as_lopts
!= NULL
)
396 a_amv_lopts_unroll(&losp
->as_lopts
);
398 *amcap
->amca_unroller
= losp
->as_lopts
;
400 if(amcap
->amca_ps_hook_mask
)
401 pstate
&= ~PS_HOOK_MASK
;
409 a_amv_mac_show(enum a_amv_mac_flags amf
){
410 size_t lc
, mc
, ti
, i
;
418 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
420 n_perr(_("Can't create temporary file for `define' or `account' listing"),
425 amf
&= a_AMV_MF_TYPE_MASK
;
426 typestr
= (amf
& a_AMV_MF_ACC
) ? "account" : "define";
428 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
429 struct a_amv_mac
*amp
;
431 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
432 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
433 struct a_amv_mac_line
**amlpp
;
440 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
441 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
442 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
444 fputs((*amlpp
)->aml_dat
, fp
);
453 page_or_print(fp
, lc
);
455 rv
= (ferror(fp
) == 0);
463 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
465 ui32_t line_cnt
, maxlen
;
467 struct linelist
*ll_next
;
468 struct a_amv_mac_line
*ll_amlp
;
469 } *llp
, *ll_head
, *ll_tail
;
470 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
471 struct a_amv_mac
*amp
;
475 memset(&line
, 0, sizeof line
);
479 /* Read in the lines which form the macro content */
480 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
484 n
.i
= n_lex_input(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_NL_ESC
, n_empty
,
485 &line
.s
, &line
.l
, NULL
);
489 n_err(_("Unterminated %s definition: %s\n"),
490 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
493 if(a_amv_mac_is_closing_angle(line
.s
))
496 /* Trim WS, remember amount of leading spaces for display purposes */
497 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
499 leaspc
= (leaspc
+ 8u) & ~7u;
504 for(; n
.ui
> 0 && whitechar(cp
[n
.ui
- 1]); --n
.ui
)
509 maxlen
= n_MAX(maxlen
, n
.ui
);
512 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
513 struct a_amv_mac_line
*amlp
;
515 llp
= salloc(sizeof *llp
);
519 ll_tail
->ll_next
= llp
;
522 llp
->ll_amlp
= amlp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
524 amlp
->aml_len
= n
.ui
-1;
525 amlp
->aml_prespc
= leaspc
;
526 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
528 n_err(_("Too much content in %s definition: %s\n"),
529 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
534 /* Create the new macro */
535 n
.s
= strlen(name
) +1;
536 amp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
538 amp
->am_maxlen
= maxlen
;
539 amp
->am_line_cnt
= line_cnt
;
541 amp
->am_lopts
= NULL
;
542 memcpy(amp
->am_name
, name
, n
.s
);
544 struct a_amv_mac_line
**amlpp
;
546 amp
->am_line_dat
= amlpp
= smalloc(sizeof(*amlpp
) * ++line_cnt
);
547 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
548 *amlpp
++ = llp
->ll_amlp
;
552 /* Finally check whether such a macro already exists, in which case we throw
553 * it all away again. At least we know it would have worked */
554 if(a_amv_mac_lookup(name
, amp
, amf
) != NULL
){
555 n_err(_("There is already a %s of name: %s\n"),
556 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
568 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
571 free(amp
->am_line_dat
);
578 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
579 struct a_amv_mac
*amp
;
585 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
586 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
587 n_err(_("%s not defined: %s\n"),
588 (amf
& a_AMV_MF_ACC
? "Account" : "Macro"), name
);
592 struct a_amv_mac
**ampp
, *lamp
;
594 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
596 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
597 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
598 /* xxx Expensive but rare: be simple */
599 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
600 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
612 a_amv_mac_free(struct a_amv_mac
*amp
){
613 struct a_amv_mac_line
**amlpp
;
616 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
618 free(amp
->am_line_dat
);
624 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
625 struct a_amv_var
*oavp
){
626 struct a_amv_var
*avp
;
630 /* Propagate unrolling up the stack, as necessary */
635 if((alp
= alp
->as_up
) == NULL
)
639 /* Check whether this variable is handled yet */
640 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
641 if(!strcmp(avp
->av_name
, name
))
644 nl
= strlen(name
) +1;
645 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
646 avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
647 avp
->av_link
= alp
->as_lopts
;
649 memcpy(avp
->av_name
, name
, nl
);
651 avp
->av_value
= NULL
;
657 avp
->av_value
= &avp
->av_name
[nl
];
658 avp
->av_flags
= oavp
->av_flags
;
659 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
661 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
669 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
670 struct a_amv_lostack
*save_alp
;
672 struct a_amv_var
*x
, *avp
;
677 reset
= !(pstate
& PS_ROOT
);
679 save_alp
= a_amv_lopts
;
685 vok_vset(x
->av_name
, x
->av_value
);
690 a_amv_lopts
= save_alp
;
695 a_amv_var_copy(char const *str
){
701 news
= n_UNCONST(n_empty
);
702 else if(str
[1] == '\0'){
704 news
= n_UNCONST(a_amv_var_1
);
705 else if(str
[0] == '0')
706 news
= n_UNCONST(a_amv_var_0
);
711 len
= strlen(str
) +1;
713 memcpy(news
, str
, len
);
720 a_amv_var_free(char *cp
){
722 if(cp
[0] != '\0' && cp
!= a_amv_var_1
&& cp
!= a_amv_var_0
)
728 a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
){
741 /* Invalidate any resolved folder then, too
744 ok
= !(pstate
& PS_ROOT
);
746 ok_vclear(_folder_resolved
);
758 case ok_b_POSIXLY_CORRECT
:
759 if(!(pstate
& PS_ROOT
)){
760 bool_t reset
= !(pstate
& PS_ROOT
);
772 if(!(pstate
& PS_ROOT
)){
773 bool_t reset
= !(pstate
& PS_ROOT
);
777 ok_bset(POSIXLY_CORRECT
);
779 ok_bclear(POSIXLY_CORRECT
);
784 case ok_b_skipemptybody
:
787 case ok_b_typescript_mode
:
789 ok_bset(colour_disable
);
790 ok_bset(line_editor_disable
);
791 if(!(pstate
& PS_STARTED
))
792 ok_bset(termcap_disable
);
799 if((ul
= strtoul(*val
, NULL
, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
800 n_err(_("Invalid *umask* setting: %s\n"), *val
);
807 flag
= (enable
&& !(options
& OPT_VERB
))
808 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
811 DBG( n_err("Implementation error: never heard of %u\n", ok
); )
826 a_amv_var_check_nocntrls(char const *val
){
830 while((c
= *val
++) != '\0')
838 a_amv_var_check_num(char const *val
, bool_t pos
){ /* TODO intmax_t anywhere! */
839 /* TODO The internal/environment variables which are num= or posnum= should
840 * TODO gain special lookup functions, or the return should be void* and
841 * TODO castable to integer; i.e. no more strtoX() should be needed.
842 * TODO I.e., the result of this function should instead be stored.
843 * TODO Use intmax_t IF that is sizeof(void*) only? */
849 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
851 union {long s
; unsigned long u
;} i
;
854 i
.s
= strtol(val
, &eptr
, 0); /* TODO strtoimax() */
857 ((i
.s
== LONG_MIN
|| i
.s
== LONG_MAX
) && errno
== ERANGE
))
859 #if INT_MIN != LONG_MIN
860 else if(i
.s
< INT_MIN
)
863 #if INT_MAX != LONG_MAX
864 else if(i
.s
> INT_MAX
)
868 i
.u
= strtoul(val
, &eptr
, 0); /* TODO strtoumax() */
870 if(*eptr
!= '\0' || (i
.u
== ULONG_MAX
&& errno
== ERANGE
))
872 #if UINT_MAX != ULONG_MAX
873 else if(i
.u
> UINT_MAX
)
883 a_amv_var_canonify(char const *vn
){
888 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
890 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
897 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
899 struct a_amv_var_map
const *avmp
;
902 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
903 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
905 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
908 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
911 avmp
= &a_amv_var_map
[x
];
912 if(avmp
->avm_hash
== hash
&&
913 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
914 avcp
->avc_map
= avmp
;
915 avcp
->avc_okey
= (enum okeys
)x
;
919 if(++i
== a_AMV_VAR_REV_PRIME
){
920 #ifdef a_AMV_VAR_REV_WRAPAROUND
927 avcp
->avc_map
= NULL
;
931 return (avcp
!= NULL
);
935 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
938 struct a_amv_var_map
const *avmp
;
939 struct a_amv_var
*avp
;
943 struct a_amv_var
**avpp
, *lavp
;
945 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
947 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
948 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
949 /* Relink as head, hope it "sorts on usage" over time.
950 * _clear() relies on this behaviour */
952 lavp
->av_link
= avp
->av_link
;
953 avp
->av_link
= *avpp
;
960 /* If this is not an assembled variable we need to consider some special
961 * initialization cases and eventually create the variable anew */
962 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
963 /* Does it have an import-from-environment flag? */
964 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
965 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
966 /* May be better not to use that one, though? */
967 bool_t isempty
, isbltin
;
969 isempty
= (*cp
== '\0' &&
970 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
971 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
974 if(n_UNLIKELY(isempty
)){
977 }else if(n_LIKELY(*cp
!= '\0')){
978 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
979 !a_amv_var_check_nocntrls(cp
))){
980 n_err(_("Ignoring environment, control characters "
981 "invalid in variable: %s\n"), avcp
->avc_name
);
984 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
985 !a_amv_var_check_num(cp
, FAL0
))){
986 n_err(_("Environment variable value not a number "
987 "or out of range: %s\n"), avcp
->avc_name
);
990 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
991 !a_amv_var_check_num(cp
, TRU1
))){
992 n_err(_("Environment variable value not a number, "
993 "negative or out of range: %s\n"), avcp
->avc_name
);
1002 /* A first-time init switch is to be handled now and here */
1003 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1004 static struct a_amv_var_defval
const **arr
,
1005 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1009 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1011 arr
[i
] = &a_amv_var_i3vals
[i
];
1014 for(i
= 0; arr
[i
] != NULL
; ++i
)
1015 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1016 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1017 : arr
[i
]->avdv_value
;
1018 /* Remove this entry, hope entire block becomes no-op asap */
1020 arr
[i
] = arr
[i
+ 1];
1021 while(arr
[i
++] != NULL
);
1025 if(i3val_nonew
== TRUM1
)
1026 avp
= (struct a_amv_var
*)-1;
1031 /* The virtual variables */
1032 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1033 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1034 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1035 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1041 /* Place this last because once it is set first the variable will never
1042 * be removed again and thus match in the first block above */
1043 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1044 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1045 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1046 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1047 : a_amv_var_defvals
[i
].avdv_value
;
1056 avcp
->avc_var
= avp
;
1058 return (avp
!= NULL
);
1061 struct a_amv_var
**avpp
;
1064 l
= strlen(avcp
->avc_name
) +1;
1065 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1067 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1069 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1070 avp
->av_value
= a_amv_var_copy(cp
);
1074 avp
->av_flags
= avmp
->avm_flags
;
1076 if(avp
->av_flags
& a_AMV_VF_VIP
)
1077 a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
);
1078 if(avp
->av_flags
& a_AMV_VF_ENV
)
1079 a_amv_var__putenv(avcp
, avp
);
1085 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1087 struct a_amv_var
*avp
;
1089 struct a_amv_var_map
const *avmp
;
1094 rv
= a_amv_var_clear(avcp
, force_env
);
1098 if((avmp
= avcp
->avc_map
) != NULL
){
1101 /* Validity checks */
1102 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1103 !(pstate
& PS_ROOT
))){
1104 value
= N_("Variable is readonly: %s\n");
1107 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1108 value
= N_("Variable must not be empty: %s\n");
1111 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1112 !a_amv_var_check_nocntrls(value
))){
1113 value
= N_("Variable forbids control characters: %s\n");
1116 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1117 !a_amv_var_check_num(value
, FAL0
))){
1118 value
= N_("Variable value not a number or out of range: %s\n");
1121 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1122 !a_amv_var_check_num(value
, TRU1
))){
1123 value
= _("Variable value not a number, negative, "
1124 "or out of range: %s\n");
1127 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1128 !(pstate
& (PS_ROOT
| PS_STARTED
)))){
1129 value
= N_("Variable cannot be set in a resource file: %s\n");
1131 n_err(V_(value
), avcp
->avc_name
);
1135 /* Transformations */
1136 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1139 oval
= savestr(value
);
1141 for(; (c
= *oval
) != '\0'; ++oval
)
1142 *oval
= lowerconv(c
);
1147 a_amv_var_lookup(avcp
, TRU1
);
1149 /* Don't care what happens later on, store this in the unroll list */
1150 if(a_amv_lopts
!= NULL
&&
1151 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1152 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1154 if((avp
= avcp
->avc_var
) == NULL
){
1155 struct a_amv_var
**avpp
;
1158 l
= strlen(avcp
->avc_name
) +1;
1159 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1161 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1166 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1167 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1168 oval
= n_UNCONST(n_empty
);
1170 oval
= avp
->av_value
;
1173 avp
->av_value
= a_amv_var_copy(value
);
1175 /* Via `set' etc. the user may give even boolean options non-boolean
1176 * values, ignore that and force boolean */
1177 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1178 if(!(pstate
& PS_ROOT
) && (options
& OPT_D_VV
) && *value
!= '\0')
1179 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1180 avcp
->avc_name
, value
);
1181 avp
->av_value
= n_UNCONST(a_amv_var_1
);
1183 avp
->av_value
= a_amv_var_copy(value
);
1185 /* Check if update allowed XXX wasteful on error! */
1186 if((avp
->av_flags
& a_AMV_VF_VIP
) &&
1187 !(rv
= a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
))){
1188 char *cp
= avp
->av_value
;
1190 avp
->av_value
= oval
;
1195 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1196 avp
->av_flags
|= a_AMV_VF_LINKED
;
1197 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1198 rv
= a_amv_var__putenv(avcp
, avp
);
1200 a_amv_var_free(oval
);
1207 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1215 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1217 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1219 if((rv
= (putenv(cp
) == 0))){
1222 if((ocp
= avp
->av_env
) != NULL
)
1233 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1234 struct a_amv_var
**avpp
, *avp
;
1235 struct a_amv_var_map
const *avmp
;
1241 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1242 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1243 !(pstate
& PS_ROOT
))){
1244 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1247 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1248 !a_amv_var_check_vips(avcp
->avc_okey
, FAL0
, NULL
))
1254 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1257 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1258 }else if(!(pstate
& (PS_ROOT
| PS_ROBOT
)) && (options
& OPT_D_V
))
1259 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1261 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1262 avcp
->avc_var
= NULL
;
1268 if(a_amv_lopts
!= NULL
&&
1269 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1270 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1272 avp
= avcp
->avc_var
;
1273 avcp
->avc_var
= NULL
;
1274 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1275 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1276 *avpp
= (*avpp
)->av_link
;
1280 char *envval
= NULL
;
1282 char *envval
= avp
->av_env
;
1284 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1285 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1287 a_amv_var_free(avp
->av_value
);
1290 /* XXX Fun part, extremely simple-minded for now: if this variable has
1291 * XXX a default value, immediately reinstantiate it! */
1292 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1293 a_amv_var_lookup(avcp
, TRU1
);
1300 a_amv_var__clearenv(char const *name
, char *value
){
1302 extern char **environ
;
1314 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1319 while(*ecpp
++ != NULL
);
1329 a_amv_var_show_all(void){
1330 struct n_string msg
, *msgp
;
1333 struct a_amv_var
*avp
;
1334 char const **vacp
, **cap
;
1337 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1338 n_perr(_("Can't create temporary file for `set' listing"), 0);
1342 /* We need to instantiate first-time-inits and default values here, so that
1343 * they will be regular members of our _vars[] table */
1344 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1345 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1346 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1347 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1349 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1350 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1352 no
+= a_AMV_VAR_VIRTS_CNT
;
1354 vacp
= salloc(no
* sizeof(*vacp
));
1356 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1357 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1358 *cap
++ = avp
->av_name
;
1359 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1360 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1363 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1366 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1367 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1368 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1371 page_or_print(fp
, i
);
1378 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1382 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1388 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1389 struct a_amv_var_carrier avc
;
1394 msgp
= n_string_trunc(msgp
, 0);
1397 a_amv_var_revlookup(&avc
, name
);
1398 if(!a_amv_var_lookup(&avc
, FAL0
)){
1401 msgp
= n_string_assign_cp(msgp
, _("No such variable: "));
1402 s
.s
= n_UNCONST(name
);
1404 msgp
= n_shexp_quote(msgp
, &s
, FAL0
);
1408 if(options
& OPT_D_V
){
1409 if(avc
.avc_map
== NULL
){
1410 msgp
= n_string_push_c(msgp
, '#');
1411 msgp
= n_string_push_cp(msgp
, "assembled");
1419 {a_AMV_VF_VIRT
, "virtual"},
1420 {a_AMV_VF_RDONLY
, "readonly"},
1421 {a_AMV_VF_NODEL
, "nodelete"},
1422 {a_AMV_VF_NOTEMPTY
, "notempty"},
1423 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1424 {a_AMV_VF_NUM
, "number"},
1425 {a_AMV_VF_POSNUM
, "positive-number"},
1426 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1427 {a_AMV_VF_ENV
, "sync-environ"},
1428 {a_AMV_VF_I3VAL
, "initial-value"},
1429 {a_AMV_VF_DEFVAL
, "default-value"},
1430 {a_AMV_VF_LINKED
, "`environ' link"}
1433 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1434 if(avc
.avc_var
->av_flags
& tp
->flags
){
1435 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1436 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1441 msgp
= n_string_push_cp(msgp
, "\n ");
1444 if(avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)
1445 msgp
= n_string_push_cp(msgp
, "# ");
1446 n_UNINIT(quote
, NULL
);
1447 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1448 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1449 if(strcmp(quote
, avc
.avc_var
->av_value
))
1450 msgp
= n_string_push_cp(msgp
, "wysh ");
1452 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1453 msgp
= n_string_push_cp(msgp
, "environ ");
1454 msgp
= n_string_push_cp(msgp
, "set ");
1455 msgp
= n_string_push_cp(msgp
, name
);
1456 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1457 msgp
= n_string_push_c(msgp
, '=');
1458 msgp
= n_string_push_cp(msgp
, quote
);
1462 msgp
= n_string_push_c(msgp
, '\n');
1463 fputs(n_string_cp(msgp
), fp
);
1465 return (i
> 0 ? 2 : 1);
1469 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1470 char *cp
, *cp2
, *varbuf
, c
;
1476 while((cp
= *ap
++) != NULL
){
1478 cp2
= varbuf
= salloc(strlen(cp
) +1);
1480 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1481 if(cntrlchar(c
) || whitechar(c
)){
1482 n_err(_("Variable name with control character ignored: %s\n"),
1491 cp
= n_UNCONST(n_empty
);
1496 n_err(_("Empty variable name ignored\n"));
1499 struct a_amv_var_carrier avc
;
1502 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1503 varbuf
= &varbuf
[2];
1505 a_amv_var_revlookup(&avc
, varbuf
);
1508 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1510 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1525 if((args
= v
)[0] == NULL
){
1526 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1530 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1532 n_err(_("Synopsis: define: <name> {\n"));
1536 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1543 c_undefine(void *v
){
1551 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1552 while(*++args
!= NULL
);
1559 struct a_amv_mac_call_args
*amcap
;
1560 char const *errs
, *name
;
1561 struct a_amv_mac
*amp
;
1568 if((args
= v
)[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)){
1569 errs
= _("Synopsis: call: <%s>\n");
1574 if((amp
= a_amv_mac_lookup(name
= *args
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1575 errs
= _("Undefined macro `call'ed: %s\n");
1579 amcap
= n_lofi_alloc(sizeof *amcap
);
1580 memset(amcap
, 0, sizeof *amcap
);
1581 amcap
->amca_name
= name
;
1582 amcap
->amca_amp
= amp
;
1583 rv
= (a_amv_mac_exec(amcap
) == FAL0
);
1593 check_folder_hook(bool_t nmail
){ /* TODO temporary, v15: drop */
1594 struct a_amv_mac_call_args
*amcap
;
1595 struct a_amv_mac
*amp
;
1602 var
= salloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1604 /* First try the fully resolved path */
1605 snprintf(var
, len
, "folder-hook-%s", mailname
);
1606 if((cp
= vok_vlook(var
)) != NULL
)
1609 /* If we are under *folder*, try the usual +NAME syntax, too */
1610 if(displayname
[0] == '+'){
1613 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
1615 snprintf(var
, len
, "folder-hook-+%s", x
);
1616 if((cp
= vok_vlook(var
)) != NULL
)
1622 /* Plain *folder-hook* is our last try */
1623 if((cp
= ok_vlook(folder_hook
)) == NULL
)
1627 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1628 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1629 n_shexp_quote_cp(displayname
, FAL0
), cp
);
1634 amcap
= n_lofi_alloc(sizeof *amcap
);
1635 memset(amcap
, 0, sizeof *amcap
);
1636 amcap
->amca_name
= cp
;
1637 amcap
->amca_amp
= amp
;
1638 pstate
&= ~PS_HOOK_MASK
;
1640 amcap
->amca_unroller
= NULL
;
1641 pstate
|= PS_HOOK_NEWMAIL
;
1643 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
1646 amcap
->amca_lopts_on
= TRU1
;
1647 amcap
->amca_ps_hook_mask
= TRU1
;
1648 rv
= a_amv_mac_exec(amcap
);
1649 pstate
&= ~PS_HOOK_MASK
;
1658 struct a_amv_mac_call_args
*amcap
;
1659 struct a_amv_mac
*amp
;
1661 int rv
, i
, oqf
, nqf
;
1666 if((args
= v
)[0] == NULL
){
1667 rv
= (a_amv_mac_show(a_AMV_MF_ACC
) == FAL0
);
1671 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
1672 if(args
[2] != NULL
){
1673 n_err(_("Synopsis: account: <name> {\n"));
1676 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
1677 n_err(_("`account': cannot use reserved name: %s\n"),
1681 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACC
) == FAL0
);
1685 if(pstate
& PS_HOOK_MASK
){
1686 n_err(_("`account': can't change account from within a hook\n"));
1690 save_mbox_for_possible_quitstuff();
1693 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1694 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACC
)) == NULL
) {
1695 n_err(_("`account': account does not exist: %s\n"), args
[0]);
1699 oqf
= savequitflags();
1701 if(a_amv_acc_curr
!= NULL
){
1702 if(a_amv_acc_curr
->am_lopts
!= NULL
)
1703 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
1704 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DEL
)
1705 a_amv_mac_free(a_amv_acc_curr
);
1708 account_name
= (amp
!= NULL
) ? amp
->am_name
: NULL
;
1709 a_amv_acc_curr
= amp
;
1713 assert(amp
->am_lopts
== NULL
);
1714 amcap
= n_lofi_alloc(sizeof *amcap
);
1715 memset(amcap
, 0, sizeof *amcap
);
1716 amcap
->amca_name
= amp
->am_name
;
1717 amcap
->amca_amp
= amp
;
1718 amcap
->amca_unroller
= &
->am_lopts
;
1719 amcap
->amca_lopts_on
= TRU1
;
1720 ok
= a_amv_mac_exec(amcap
);
1722 /* XXX account switch incomplete, unroll? */
1723 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
1729 bool_t reset
= !(pstate
& PS_ROOT
);
1733 ok_vset(_account_name
, amp
->am_name
);
1735 ok_vclear(_account_name
);
1740 if((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
){
1741 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1742 restorequitflags(oqf
);
1743 if((i
= setfile("%", 0)) < 0)
1745 check_folder_hook(FAL0
);
1746 if(i
> 0 && !ok_blook(emptystart
))
1748 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1749 restorequitflags(nqf
);
1758 c_unaccount(void *v
){
1766 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACC
);
1767 while(*++args
!= NULL
);
1773 c_localopts(void *v
){
1780 if(a_amv_lopts
== NULL
){
1781 n_err(_("Cannot use `localopts' in this context "
1782 "(not in `define' or `account', nor special hook)\n"));
1786 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
1794 temporary_call_compose_mode_hook(char const *macname
,
1795 void (*hook_pre
)(void *), void *hook_arg
){
1796 /* TODO call_compose_mode_hook() temporary, v15: generalize; see a_LEX_SLICE
1797 * TODO comment in lex_input.c for the right way of doing things! */
1798 static struct a_amv_lostack
*cmh_losp
;
1799 struct a_amv_mac_call_args
*amcap
;
1800 struct a_amv_mac
*amp
;
1805 if(macname
== (char*)-1){
1806 a_amv_mac__finalize(cmh_losp
);
1808 }else if(macname
!= NULL
&&
1809 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
1810 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1813 amcap
= n_lofi_alloc(sizeof *amcap
);
1814 memset(amcap
, 0, sizeof *amcap
);
1815 amcap
->amca_name
= (macname
!= NULL
) ? macname
: "on-compose-done-shell";
1816 amcap
->amca_amp
= amp
;
1817 amcap
->amca_unroller
= &a_amv_compose_lopts
;
1818 amcap
->amca_hook_pre
= hook_pre
;
1819 amcap
->amca_hook_arg
= hook_arg
;
1820 amcap
->amca_lopts_on
= TRU1
;
1821 amcap
->amca_ps_hook_mask
= TRU1
;
1822 pstate
&= ~PS_HOOK_MASK
;
1825 a_amv_mac_exec(amcap
);
1827 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
1828 cmh_losp
->as_global_saved
= a_amv_lopts
;
1829 cmh_losp
->as_up
= NULL
;
1830 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
1831 cmh_losp
->as_unroll
= TRU1
;
1832 a_amv_lopts
= cmh_losp
;
1839 temporary_unroll_compose_mode(void){ /* XXX intermediate hack */
1841 if(a_amv_compose_lopts
!= NULL
){
1842 void *save
= a_amv_lopts
;
1845 a_amv_lopts_unroll(&a_amv_compose_lopts
);
1846 a_amv_compose_lopts
= NULL
;
1853 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1855 if(a_amv_folder_hook_lopts
!= NULL
){
1856 void *save
= a_amv_lopts
;
1859 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
1860 a_amv_folder_hook_lopts
= NULL
;
1867 n_var_oklook(enum okeys okey
){
1868 struct a_amv_var_carrier avc
;
1870 struct a_amv_var_map
const *avmp
;
1873 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1874 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1875 avc
.avc_hash
= avmp
->avm_hash
;
1876 avc
.avc_okey
= okey
;
1878 if(a_amv_var_lookup(&avc
, FAL0
))
1879 rv
= avc
.avc_var
->av_value
;
1887 n_var_okset(enum okeys okey
, uintptr_t val
){
1888 struct a_amv_var_carrier avc
;
1890 struct a_amv_var_map
const *avmp
;
1893 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1894 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1895 avc
.avc_hash
= avmp
->avm_hash
;
1896 avc
.avc_okey
= okey
;
1898 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
1904 n_var_okclear(enum okeys okey
){
1905 struct a_amv_var_carrier avc
;
1907 struct a_amv_var_map
const *avmp
;
1910 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1911 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1912 avc
.avc_hash
= avmp
->avm_hash
;
1913 avc
.avc_okey
= okey
;
1915 rv
= a_amv_var_clear(&avc
, FAL0
);
1921 n_var_voklook(char const *vokey
){
1922 struct a_amv_var_carrier avc
;
1926 a_amv_var_revlookup(&avc
, vokey
);
1928 rv
= a_amv_var_lookup(&avc
, FAL0
) ? avc
.avc_var
->av_value
: NULL
;
1934 n_var_vokset(char const *vokey
, uintptr_t val
){
1935 struct a_amv_var_carrier avc
;
1939 a_amv_var_revlookup(&avc
, vokey
);
1941 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
1947 n_var_vokclear(char const *vokey
){
1948 struct a_amv_var_carrier avc
;
1952 a_amv_var_revlookup(&avc
, vokey
);
1954 ok
= a_amv_var_clear(&avc
, FAL0
);
1961 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
1962 enum okey_xlook_mode oxm
){
1963 struct a_amv_var_carrier avc
;
1964 struct str
const *us
;
1967 struct a_amv_var_map
const *avmp
;
1970 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1972 /* For simplicity: allow this case too */
1973 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
1978 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1979 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1980 avc
.avc_okey
= okey
;
1982 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1983 nlen
= strlen(avc
.avc_name
);
1984 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
1985 memcpy(nbuf
, avc
.avc_name
, nlen
);
1988 /* One of .url_u_h_p and .url_h_p we test in here */
1989 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1990 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1991 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1992 if(a_amv_var_lookup(&avc
, FAL0
))
1997 us
= &urlp
->url_h_p
;
1998 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1999 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2000 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2001 if(a_amv_var_lookup(&avc
, FAL0
)){
2003 rv
= avc
.avc_var
->av_value
;
2009 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2016 #endif /* HAVE_SOCKETS */
2024 if(*(ap
= v
) == NULL
){
2025 a_amv_var_show_all();
2028 err
= !a_amv_var_c_set(ap
, FAL0
);
2039 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2040 err
|= !n_var_vokclear(*ap
);
2050 if(*(ap
= v
) == NULL
)
2053 struct n_string msg
, *msgp
= &msg
;
2055 msgp
= n_string_creat(msgp
);
2056 for(; *ap
!= NULL
; ++ap
)
2057 a_amv_var_show(*ap
, stdout
, msgp
);
2061 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2066 struct a_amv_var_carrier avc
;
2070 sighandler_type sigint
;
2073 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2075 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2076 memset(&avc
, 0, sizeof avc
);
2078 a_amv_var_revlookup(&avc
, *argv
);
2080 if(avc
.avc_map
!= NULL
){
2081 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2082 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2086 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2087 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2093 a_amv_var_lookup(&avc
, FAL0
);
2095 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2097 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2100 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2101 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2102 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2109 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2119 base
= salloc((size_t)l
+1);
2121 for(l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
2122 if(c
== '\n' || c
== '\r'){
2126 *val
= (char)(uc_i
)c
;
2132 if(!a_amv_var_set(&avc
, base
, FAL0
))
2137 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2143 safe_signal(SIGINT
, sigint
);
2150 struct a_amv_var_carrier avc
;
2156 if((islnk
= !strcmp(*(ap
= v
), "link")) || !strcmp(*ap
, "unlink")){
2157 for(err
= 0; *++ap
!= NULL
;){
2158 a_amv_var_revlookup(&avc
, *ap
);
2160 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2161 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2163 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2165 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2166 if(options
& OPT_D_V
)
2167 n_err(_("`environ': link: already established: %s\n"), *ap
);
2170 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2171 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2172 a_amv_var__putenv(&avc
, avc
.avc_var
);
2174 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2177 char const *evp
= getenv(*ap
);
2180 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2182 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2188 }else if(!strcmp(*ap
, "set"))
2189 err
= !a_amv_var_c_set(++ap
, TRU1
);
2190 else if(!strcmp(*ap
, "unset")){
2191 for(err
= 0; *++ap
!= NULL
;){
2192 a_amv_var_revlookup(&avc
, *ap
);
2194 if(!a_amv_var_clear(&avc
, TRU1
))
2198 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));