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 - 2016 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
);
1001 /* A first-time init switch is to be handled now and here */
1002 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1003 static struct a_amv_var_defval
const **arr
,
1004 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1008 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1010 arr
[i
] = &a_amv_var_i3vals
[i
];
1013 for(i
= 0; arr
[i
] != NULL
; ++i
)
1014 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1015 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1016 : arr
[i
]->avdv_value
;
1017 /* Remove this entry, hope entire block becomes no-op asap */
1019 arr
[i
] = arr
[i
+ 1];
1020 while(arr
[i
++] != NULL
);
1024 if(i3val_nonew
== TRUM1
)
1025 avp
= (struct a_amv_var
*)-1;
1030 /* The virtual variables */
1031 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1032 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1033 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1034 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1040 /* Place this last because once it is set first the variable will never
1041 * be removed again and thus match in the first block above */
1042 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1043 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1044 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1045 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1046 : a_amv_var_defvals
[i
].avdv_value
;
1055 avcp
->avc_var
= avp
;
1057 return (avp
!= NULL
);
1060 struct a_amv_var
**avpp
;
1063 l
= strlen(avcp
->avc_name
) +1;
1064 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1066 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1068 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1069 avp
->av_value
= a_amv_var_copy(cp
);
1073 avp
->av_flags
= avmp
->avm_flags
;
1075 if(avp
->av_flags
& a_AMV_VF_VIP
)
1076 a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
);
1077 if(avp
->av_flags
& a_AMV_VF_ENV
)
1078 a_amv_var__putenv(avcp
, avp
);
1084 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1086 struct a_amv_var
*avp
;
1088 struct a_amv_var_map
const *avmp
;
1093 rv
= a_amv_var_clear(avcp
, force_env
);
1097 if((avmp
= avcp
->avc_map
) != NULL
){
1100 /* Validity checks */
1101 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1102 !(pstate
& PS_ROOT
))){
1103 value
= N_("Variable is readonly: %s\n");
1106 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1107 value
= N_("Variable must not be empty: %s\n");
1110 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1111 !a_amv_var_check_nocntrls(value
))){
1112 value
= N_("Variable forbids control characters: %s\n");
1115 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1116 !a_amv_var_check_num(value
, FAL0
))){
1117 value
= N_("Variable value not a number or out of range: %s\n");
1120 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1121 !a_amv_var_check_num(value
, TRU1
))){
1122 value
= _("Variable value not a number, negative, "
1123 "or out of range: %s\n");
1126 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1127 !(pstate
& (PS_ROOT
| PS_STARTED
)))){
1128 value
= N_("Variable cannot be set in a resource file: %s\n");
1130 n_err(V_(value
), avcp
->avc_name
);
1134 /* Transformations */
1135 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1138 oval
= savestr(value
);
1140 for(; (c
= *oval
) != '\0'; ++oval
)
1141 *oval
= lowerconv(c
);
1146 a_amv_var_lookup(avcp
, TRU1
);
1148 /* Don't care what happens later on, store this in the unroll list */
1149 if(a_amv_lopts
!= NULL
&&
1150 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1151 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1153 if((avp
= avcp
->avc_var
) == NULL
){
1154 struct a_amv_var
**avpp
;
1157 l
= strlen(avcp
->avc_name
) +1;
1158 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1160 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1165 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1166 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1167 oval
= n_UNCONST(n_empty
);
1169 oval
= avp
->av_value
;
1172 avp
->av_value
= a_amv_var_copy(value
);
1174 /* Via `set' etc. the user may give even boolean options non-boolean
1175 * values, ignore that and force boolean */
1176 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1177 if((options
& OPT_D_VV
) && *value
!= '\0')
1178 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1179 avcp
->avc_name
, value
);
1180 avp
->av_value
= n_UNCONST(a_amv_var_1
);
1182 avp
->av_value
= a_amv_var_copy(value
);
1184 /* Check if update allowed XXX wasteful on error! */
1185 if((avp
->av_flags
& a_AMV_VF_VIP
) &&
1186 !(rv
= a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
))){
1187 char *cp
= avp
->av_value
;
1189 avp
->av_value
= oval
;
1194 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1195 avp
->av_flags
|= a_AMV_VF_LINKED
;
1196 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1197 rv
= a_amv_var__putenv(avcp
, avp
);
1199 a_amv_var_free(oval
);
1206 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1214 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1216 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1218 if((rv
= (putenv(cp
) == 0))){
1221 if((ocp
= avp
->av_env
) != NULL
)
1232 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1233 struct a_amv_var
**avpp
, *avp
;
1234 struct a_amv_var_map
const *avmp
;
1240 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1241 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1242 !(pstate
& PS_ROOT
))){
1243 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1246 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1247 !a_amv_var_check_vips(avcp
->avc_okey
, FAL0
, NULL
))
1253 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1256 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1257 }else if(!(pstate
& (PS_ROOT
| PS_ROBOT
)) && (options
& OPT_D_V
))
1258 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1260 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1261 avcp
->avc_var
= NULL
;
1267 if(a_amv_lopts
!= NULL
&&
1268 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1269 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1271 avp
= avcp
->avc_var
;
1272 avcp
->avc_var
= NULL
;
1273 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1274 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1275 *avpp
= (*avpp
)->av_link
;
1279 char *envval
= NULL
;
1281 char *envval
= avp
->av_env
;
1283 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1284 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1286 a_amv_var_free(avp
->av_value
);
1289 /* XXX Fun part, extremely simple-minded for now: if this variable has
1290 * XXX a default value, immediately reinstantiate it! */
1291 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1292 a_amv_var_lookup(avcp
, TRU1
);
1299 a_amv_var__clearenv(char const *name
, char *value
){
1301 extern char **environ
;
1313 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1318 while(*ecpp
++ != NULL
);
1328 a_amv_var_show_all(void){
1329 struct n_string msg
, *msgp
;
1332 struct a_amv_var
*avp
;
1333 char const **vacp
, **cap
;
1336 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1337 n_perr(_("Can't create temporary file for `set' listing"), 0);
1341 /* We need to instantiate first-time-inits and default values here, so that
1342 * they will be regular members of our _vars[] table */
1343 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1344 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1345 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1346 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1348 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1349 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1351 no
+= a_AMV_VAR_VIRTS_CNT
;
1353 vacp
= salloc(no
* sizeof(*vacp
));
1355 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1356 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1357 *cap
++ = avp
->av_name
;
1358 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1359 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1362 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1365 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1366 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1367 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1370 page_or_print(fp
, i
);
1377 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1381 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1387 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1388 struct a_amv_var_carrier avc
;
1393 msgp
= n_string_trunc(msgp
, 0);
1396 a_amv_var_revlookup(&avc
, name
);
1397 if(!a_amv_var_lookup(&avc
, FAL0
)){
1400 msgp
= n_string_assign_cp(msgp
, _("No such variable: "));
1401 s
.s
= n_UNCONST(name
);
1403 msgp
= n_shexp_quote(msgp
, &s
, FAL0
);
1407 if(options
& OPT_D_V
){
1408 if(avc
.avc_map
== NULL
){
1409 msgp
= n_string_push_c(msgp
, '#');
1410 msgp
= n_string_push_cp(msgp
, "assembled");
1418 {a_AMV_VF_VIRT
, "virtual"},
1419 {a_AMV_VF_RDONLY
, "readonly"},
1420 {a_AMV_VF_NODEL
, "nodelete"},
1421 {a_AMV_VF_NOTEMPTY
, "notempty"},
1422 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1423 {a_AMV_VF_NUM
, "number"},
1424 {a_AMV_VF_POSNUM
, "positive-number"},
1425 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1426 {a_AMV_VF_ENV
, "sync-environ"},
1427 {a_AMV_VF_I3VAL
, "initial-value"},
1428 {a_AMV_VF_DEFVAL
, "default-value"},
1429 {a_AMV_VF_LINKED
, "`environ' link"}
1432 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1433 if(avc
.avc_var
->av_flags
& tp
->flags
){
1434 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1435 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1440 msgp
= n_string_push_cp(msgp
, "\n ");
1443 if(avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)
1444 msgp
= n_string_push_cp(msgp
, "# ");
1445 n_UNINIT(quote
, NULL
);
1446 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1447 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1448 if(strcmp(quote
, avc
.avc_var
->av_value
))
1449 msgp
= n_string_push_cp(msgp
, "wysh ");
1451 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1452 msgp
= n_string_push_cp(msgp
, "environ ");
1453 msgp
= n_string_push_cp(msgp
, "set ");
1454 msgp
= n_string_push_cp(msgp
, name
);
1455 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1456 msgp
= n_string_push_c(msgp
, '=');
1457 msgp
= n_string_push_cp(msgp
, quote
);
1461 msgp
= n_string_push_c(msgp
, '\n');
1462 fputs(n_string_cp(msgp
), fp
);
1464 return (i
> 0 ? 2 : 1);
1468 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1469 char *cp
, *cp2
, *varbuf
, c
;
1475 while((cp
= *ap
++) != NULL
){
1477 cp2
= varbuf
= salloc(strlen(cp
) +1);
1479 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1480 if(cntrlchar(c
) || whitechar(c
)){
1481 n_err(_("Variable name with control character ignored: %s\n"),
1490 cp
= n_UNCONST(n_empty
);
1495 n_err(_("Empty variable name ignored\n"));
1498 struct a_amv_var_carrier avc
;
1501 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1502 varbuf
= &varbuf
[2];
1504 a_amv_var_revlookup(&avc
, varbuf
);
1507 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1509 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1524 if((args
= v
)[0] == NULL
){
1525 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1529 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1531 n_err(_("Synopsis: define: <name> {\n"));
1535 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1542 c_undefine(void *v
){
1550 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1551 while(*++args
!= NULL
);
1558 struct a_amv_mac_call_args
*amcap
;
1559 char const *errs
, *name
;
1560 struct a_amv_mac
*amp
;
1567 if((args
= v
)[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)){
1568 errs
= _("Synopsis: call: <%s>\n");
1573 if((amp
= a_amv_mac_lookup(name
= *args
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1574 errs
= _("Undefined macro `call'ed: %s\n");
1578 amcap
= n_lofi_alloc(sizeof *amcap
);
1579 memset(amcap
, 0, sizeof *amcap
);
1580 amcap
->amca_name
= name
;
1581 amcap
->amca_amp
= amp
;
1582 rv
= (a_amv_mac_exec(amcap
) == FAL0
);
1592 check_folder_hook(bool_t nmail
){ /* TODO temporary, v15: drop */
1593 struct a_amv_mac_call_args
*amcap
;
1594 struct a_amv_mac
*amp
;
1601 var
= salloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1603 /* First try the fully resolved path */
1604 snprintf(var
, len
, "folder-hook-%s", mailname
);
1605 if((cp
= vok_vlook(var
)) != NULL
)
1608 /* If we are under *folder*, try the usual +NAME syntax, too */
1609 if(displayname
[0] == '+'){
1612 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
1614 snprintf(var
, len
, "folder-hook-+%s", x
);
1615 if((cp
= vok_vlook(var
)) != NULL
)
1621 /* Plain *folder-hook* is our last try */
1622 if((cp
= ok_vlook(folder_hook
)) == NULL
)
1626 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1627 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1628 n_shexp_quote_cp(displayname
, FAL0
), cp
);
1633 amcap
= n_lofi_alloc(sizeof *amcap
);
1634 memset(amcap
, 0, sizeof *amcap
);
1635 amcap
->amca_name
= cp
;
1636 amcap
->amca_amp
= amp
;
1637 pstate
&= ~PS_HOOK_MASK
;
1639 amcap
->amca_unroller
= NULL
;
1640 pstate
|= PS_HOOK_NEWMAIL
;
1642 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
1645 amcap
->amca_lopts_on
= TRU1
;
1646 amcap
->amca_ps_hook_mask
= TRU1
;
1647 rv
= a_amv_mac_exec(amcap
);
1648 pstate
&= ~PS_HOOK_MASK
;
1657 struct a_amv_mac_call_args
*amcap
;
1658 struct a_amv_mac
*amp
;
1660 int rv
, i
, oqf
, nqf
;
1665 if((args
= v
)[0] == NULL
){
1666 rv
= (a_amv_mac_show(a_AMV_MF_ACC
) == FAL0
);
1670 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
1671 if(args
[2] != NULL
){
1672 n_err(_("Synopsis: account: <name> {\n"));
1675 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
1676 n_err(_("`account': cannot use reserved name: %s\n"),
1680 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACC
) == FAL0
);
1684 if(pstate
& PS_HOOK_MASK
){
1685 n_err(_("`account': can't change account from within a hook\n"));
1689 save_mbox_for_possible_quitstuff();
1692 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1693 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACC
)) == NULL
) {
1694 n_err(_("`account': account does not exist: %s\n"), args
[0]);
1698 oqf
= savequitflags();
1700 if(a_amv_acc_curr
!= NULL
){
1701 if(a_amv_acc_curr
->am_lopts
!= NULL
)
1702 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
1703 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DEL
)
1704 a_amv_mac_free(a_amv_acc_curr
);
1707 account_name
= (amp
!= NULL
) ? amp
->am_name
: NULL
;
1708 a_amv_acc_curr
= amp
;
1712 assert(amp
->am_lopts
== NULL
);
1713 amcap
= n_lofi_alloc(sizeof *amcap
);
1714 memset(amcap
, 0, sizeof *amcap
);
1715 amcap
->amca_name
= amp
->am_name
;
1716 amcap
->amca_amp
= amp
;
1717 amcap
->amca_unroller
= &
->am_lopts
;
1718 amcap
->amca_lopts_on
= TRU1
;
1719 ok
= a_amv_mac_exec(amcap
);
1721 /* XXX account switch incomplete, unroll? */
1722 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
1728 bool_t reset
= !(pstate
& PS_ROOT
);
1732 ok_vset(_account_name
, amp
->am_name
);
1734 ok_vclear(_account_name
);
1739 if((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
){
1740 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1741 restorequitflags(oqf
);
1742 if((i
= setfile("%", 0)) < 0)
1744 check_folder_hook(FAL0
);
1745 if(i
> 0 && !ok_blook(emptystart
))
1747 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1748 restorequitflags(nqf
);
1757 c_unaccount(void *v
){
1765 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACC
);
1766 while(*++args
!= NULL
);
1772 c_localopts(void *v
){
1779 if(a_amv_lopts
== NULL
){
1780 n_err(_("Cannot use `localopts' in this context "
1781 "(not in `define' or `account', nor special hook)\n"));
1785 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
1793 temporary_call_compose_mode_hook(char const *macname
,
1794 void (*hook_pre
)(void *), void *hook_arg
){
1795 /* TODO call_compose_mode_hook() temporary, v15: generalize; see a_LEX_SLICE
1796 * TODO comment in lex_input.c for the right way of doing things! */
1797 static struct a_amv_lostack
*cmh_losp
;
1798 struct a_amv_mac_call_args
*amcap
;
1799 struct a_amv_mac
*amp
;
1804 if(macname
== (char*)-1){
1805 a_amv_mac__finalize(cmh_losp
);
1807 }else if(macname
!= NULL
&&
1808 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
1809 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1812 amcap
= n_lofi_alloc(sizeof *amcap
);
1813 memset(amcap
, 0, sizeof *amcap
);
1814 amcap
->amca_name
= (macname
!= NULL
) ? macname
: "on-compose-done-shell";
1815 amcap
->amca_amp
= amp
;
1816 amcap
->amca_unroller
= &a_amv_compose_lopts
;
1817 amcap
->amca_hook_pre
= hook_pre
;
1818 amcap
->amca_hook_arg
= hook_arg
;
1819 amcap
->amca_lopts_on
= TRU1
;
1820 amcap
->amca_ps_hook_mask
= TRU1
;
1821 pstate
&= ~PS_HOOK_MASK
;
1824 a_amv_mac_exec(amcap
);
1826 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
1827 cmh_losp
->as_global_saved
= a_amv_lopts
;
1828 cmh_losp
->as_up
= NULL
;
1829 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
1830 cmh_losp
->as_unroll
= TRU1
;
1831 a_amv_lopts
= cmh_losp
;
1838 temporary_unroll_compose_mode(void){ /* XXX intermediate hack */
1840 if(a_amv_compose_lopts
!= NULL
){
1841 void *save
= a_amv_lopts
;
1844 a_amv_lopts_unroll(&a_amv_compose_lopts
);
1845 a_amv_compose_lopts
= NULL
;
1852 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1854 if(a_amv_folder_hook_lopts
!= NULL
){
1855 void *save
= a_amv_lopts
;
1858 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
1859 a_amv_folder_hook_lopts
= NULL
;
1866 n_var_oklook(enum okeys okey
){
1867 struct a_amv_var_carrier avc
;
1869 struct a_amv_var_map
const *avmp
;
1872 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1873 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1874 avc
.avc_hash
= avmp
->avm_hash
;
1875 avc
.avc_okey
= okey
;
1877 if(a_amv_var_lookup(&avc
, FAL0
))
1878 rv
= avc
.avc_var
->av_value
;
1886 n_var_okset(enum okeys okey
, uintptr_t val
){
1887 struct a_amv_var_carrier avc
;
1889 struct a_amv_var_map
const *avmp
;
1892 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1893 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1894 avc
.avc_hash
= avmp
->avm_hash
;
1895 avc
.avc_okey
= okey
;
1897 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
1903 n_var_okclear(enum okeys okey
){
1904 struct a_amv_var_carrier avc
;
1906 struct a_amv_var_map
const *avmp
;
1909 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1910 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1911 avc
.avc_hash
= avmp
->avm_hash
;
1912 avc
.avc_okey
= okey
;
1914 rv
= a_amv_var_clear(&avc
, FAL0
);
1920 n_var_voklook(char const *vokey
){
1921 struct a_amv_var_carrier avc
;
1925 a_amv_var_revlookup(&avc
, vokey
);
1927 rv
= a_amv_var_lookup(&avc
, FAL0
) ? avc
.avc_var
->av_value
: NULL
;
1933 n_var_vokset(char const *vokey
, uintptr_t val
){
1934 struct a_amv_var_carrier avc
;
1938 a_amv_var_revlookup(&avc
, vokey
);
1940 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
1946 n_var_vokclear(char const *vokey
){
1947 struct a_amv_var_carrier avc
;
1951 a_amv_var_revlookup(&avc
, vokey
);
1953 ok
= a_amv_var_clear(&avc
, FAL0
);
1960 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
1961 enum okey_xlook_mode oxm
){
1962 struct a_amv_var_carrier avc
;
1963 struct str
const *us
;
1966 struct a_amv_var_map
const *avmp
;
1969 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1971 /* For simplicity: allow this case too */
1972 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
1977 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1978 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1979 avc
.avc_okey
= okey
;
1981 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1982 nlen
= strlen(avc
.avc_name
);
1983 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
1984 memcpy(nbuf
, avc
.avc_name
, nlen
);
1987 /* One of .url_u_h_p and .url_h_p we test in here */
1988 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1989 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1990 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1991 if(a_amv_var_lookup(&avc
, FAL0
))
1996 us
= &urlp
->url_h_p
;
1997 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1998 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1999 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2000 if(a_amv_var_lookup(&avc
, FAL0
)){
2002 rv
= avc
.avc_var
->av_value
;
2008 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2015 #endif /* HAVE_SOCKETS */
2023 if(*(ap
= v
) == NULL
){
2024 a_amv_var_show_all();
2027 err
= !a_amv_var_c_set(ap
, FAL0
);
2038 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2039 err
|= !n_var_vokclear(*ap
);
2049 if(*(ap
= v
) == NULL
)
2052 struct n_string msg
, *msgp
= &msg
;
2054 msgp
= n_string_creat(msgp
);
2055 for(; *ap
!= NULL
; ++ap
)
2056 a_amv_var_show(*ap
, stdout
, msgp
);
2060 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2065 struct a_amv_var_carrier avc
;
2069 sighandler_type sigint
;
2072 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2074 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2075 memset(&avc
, 0, sizeof avc
);
2077 a_amv_var_revlookup(&avc
, *argv
);
2079 if(avc
.avc_map
!= NULL
){
2080 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2081 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2085 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2086 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2092 a_amv_var_lookup(&avc
, FAL0
);
2094 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2096 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2099 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2100 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2101 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2108 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2118 base
= salloc((size_t)l
+1);
2120 for(l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
2121 if(c
== '\n' || c
== '\r'){
2125 *val
= (char)(uc_i
)c
;
2131 if(!a_amv_var_set(&avc
, base
, FAL0
))
2136 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2142 safe_signal(SIGINT
, sigint
);
2149 struct a_amv_var_carrier avc
;
2155 if((islnk
= !strcmp(*(ap
= v
), "link")) || !strcmp(*ap
, "unlink")){
2156 for(err
= 0; *++ap
!= NULL
;){
2157 a_amv_var_revlookup(&avc
, *ap
);
2159 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2160 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2162 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2164 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2165 if(options
& OPT_D_V
)
2166 n_err(_("`environ': link: already established: %s\n"), *ap
);
2169 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2170 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2171 a_amv_var__putenv(&avc
, avc
.avc_var
);
2173 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2176 char const *evp
= getenv(*ap
);
2179 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2181 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2187 }else if(!strcmp(*ap
, "set"))
2188 err
= !a_amv_var_c_set(++ap
, TRU1
);
2189 else if(!strcmp(*ap
, "unset")){
2190 for(err
= 0; *++ap
!= NULL
;){
2191 a_amv_var_revlookup(&avc
, *ap
);
2193 if(!a_amv_var_clear(&avc
, TRU1
))
2197 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));