1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run mk-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . should be recursive environment based
8 *@ TODO . undefining and overwriting a macro should always be possible:
9 *@ TODO simply place the thing in a delete-later list and replace the
10 *@ TODO accessible entry! (instant delete if on top recursion level.)
11 *@ TODO . Likewise, overwriting an existing should be like delete+create
12 *@ TODO . once we can have non-fatal !0 returns for commands, we should
13 *@ TODO return error if "(environ)? unset" goes for non-existent.
15 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
16 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
19 * Copyright (c) 1980, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
50 # define _ACCMACVAR_SOURCE /* For _features[] */
54 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
55 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
58 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
59 #define a_AMV_PRIME HSHSIZE
60 #define a_AMV_NAME2HASH(N) torek_hash(N)
61 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
65 a_AMV_MF_ACC
= 1<<0, /* This macro is an `account' */
66 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACC
,
67 a_AMV_MF_UNDEF
= 1<<1, /* Unlink after lookup */
68 a_AMV_MF_DEL
= 1<<7, /* Current `account': deleted while active */
72 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
73 * _IMPORT implies _ENV */
76 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
77 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
78 a_AMV_VF_RDONLY
= 1<<2, /* May not be set by user */
79 a_AMV_VF_NODEL
= 1<<3, /* May not be deleted */
80 a_AMV_VF_NOTEMPTY
= 1<<4, /* May not be assigned an empty value */
81 a_AMV_VF_VIP
= 1<<5, /* Wants _var_check_vips() evaluation */
82 a_AMV_VF_IMPORT
= 1<<6, /* Import ONLY from environ (before PS_STARTED) */
83 a_AMV_VF_ENV
= 1<<7, /* Update environment on change */
84 a_AMV_VF_I3VAL
= 1<<8, /* Has an initial value */
85 a_AMV_VF_DEFVAL
= 1<<9, /* Has a default value */
86 a_AMV_VF_LINKED
= 1<<10, /* `environ' linked */
87 a_AMV_VF__MASK
= (1<<(10+1)) - 1
91 struct a_amv_mac
*am_next
;
92 ui32_t am_maxlen
; /* of any line in .am_line_dat */
93 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
94 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
95 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
96 ui8_t am_flags
; /* enum a_amv_mac_flags */
97 char am_name
[VFIELD_SIZE(7)]; /* of this macro */
99 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
101 struct a_amv_mac_line
{
103 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
104 char aml_dat
[VFIELD_SIZE(0)];
107 struct a_amv_lostack
{
108 struct a_amv_lostack
*as_up
; /* Outer context */
109 struct a_amv_mac
*as_mac
; /* Context (`account' or `define') */
110 struct a_amv_var
*as_lopts
;
111 bool_t as_unroll
; /* Unrolling enabled? */
116 struct a_amv_var
*av_link
;
119 char *av_env
; /* Actively managed putenv(3) memory */
121 ui16_t av_flags
; /* enum a_amv_var_flags */
122 char av_name
[VFIELD_SIZE(6)];
124 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
126 struct a_amv_var_map
{
129 ui16_t avm_flags
; /* enum a_amv_var_flags */
131 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
133 struct a_amv_var_virt
{
135 struct a_amv_var
const *avv_var
;
138 struct a_amv_var_defval
{
141 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
144 struct a_amv_var_carrier
{
145 char const *avc_name
;
148 struct a_amv_var
*avc_var
;
149 struct a_amv_var_map
const *avc_map
;
153 /* Include the constant mk-okey-map.pl output */
157 /* The currently active account */
158 static struct a_amv_mac
*a_amv_acc_curr
;
160 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
162 /* Unroll list of currently running macro stack */
163 static struct a_amv_lostack
*a_amv_lopts
;
165 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
167 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
168 * TODO today via a static lostack, it should be a field in mailbox, once that
169 * TODO is a real multi-instance object */
170 static struct a_amv_var
*a_amv_folder_hook_lopts
;
172 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
173 static struct a_amv_var
*a_amv_compose_lopts
;
175 /* Does cp consist solely of WS and a } */
176 static bool_t
a_amv_mac_is_closing_angle(char const *cp
);
178 /* Lookup for macros/accounts */
179 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
180 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
182 /* Execute a macro */
183 static bool_t
a_amv_mac_exec(struct a_amv_mac
const *amp
,
184 struct a_amv_var
**unroller
, bool_t lopts_on
);
186 /* User display helpers */
187 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
189 /* _def() returns error for faulty definitions and already existing * names,
190 * _undef() returns error if a named thing doesn't exist */
191 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
192 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
195 static void a_amv_mac_free(struct a_amv_mac
*amp
);
197 /* Update replay-log */
198 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
199 struct a_amv_var
*oavp
);
200 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
202 /* Special cased value string allocation */
203 static char *a_amv_var_copy(char const *str
);
204 static void a_amv_var_free(char *cp
);
206 /* Check for special housekeeping */
207 static bool_t
a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
);
209 /* If a variable name begins with a lowercase-character and contains at
210 * least one '@', it is converted to all-lowercase. This is necessary
211 * for lookups of names based on email addresses.
212 * Following the standard, only the part following the last '@' should
213 * be lower-cased, but practice has established otherwise here */
214 static char const *a_amv_var_canonify(char const *vn
);
216 /* Try to reverse lookup an option name to an enum okeys mapping.
217 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
218 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
221 /* Lookup a variable from .avc_(map|name|hash), return wether it was found.
222 * Sets .avc_prime; .avc_var is NULL if not found.
223 * Here it is where we care for _I3VAL and _DEFVAL, too */
224 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
);
226 /* Set var from .avc_(map|name|hash), return success */
227 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
230 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
231 struct a_amv_var
*avp
);
233 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
234 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
236 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
238 /* List all variables */
239 static void a_amv_var_show_all(void);
241 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
243 /* Actually do print one, return number of lines written */
244 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
246 static char *a_amv_var__simple_quote(char const *cp
);
248 /* Shared c_set() and c_environ():set impl, return success */
249 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
252 a_amv_mac_is_closing_angle(char const *cp
){
256 while(spacechar(*cp
))
259 if((rv
= (*cp
++ == '}'))){
260 while(spacechar(*cp
))
268 static struct a_amv_mac
*
269 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
270 enum a_amv_mac_flags amf
){
271 struct a_amv_mac
*amp
, **ampp
;
273 enum a_amv_mac_flags save_amf
;
277 amf
&= a_AMV_MF_TYPE_MASK
;
278 h
= a_AMV_NAME2HASH(name
);
279 h
= a_AMV_HASH2PRIME(h
);
280 ampp
= &a_amv_macs
[h
];
282 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
283 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
284 !strcmp(amp
->am_name
, name
)){
285 if(LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
288 *ampp
= amp
->am_next
;
290 if((amf
& a_AMV_MF_ACC
) &&
291 account_name
!= NULL
&& !strcmp(account_name
, name
)){
292 amp
->am_flags
|= a_AMV_MF_DEL
;
293 n_err(_("Delayed deletion of active account \"%s\"\n"), name
);
296 amp
= (struct a_amv_mac
*)-1;
303 ampp
= &a_amv_macs
[h
];
304 newamp
->am_next
= *ampp
;
314 a_amv_mac_exec(struct a_amv_mac
const *amp
, struct a_amv_var
**unroller
,
316 struct a_amv_lostack los
;
317 struct a_amv_mac_line
**amlp
;
318 char **args_base
, **args
;
322 args_base
= args
= smalloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
323 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
324 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
327 los
.as_up
= a_amv_lopts
;
328 los
.as_mac
= UNCONST(amp
); /* But not used.. */
329 los
.as_lopts
= (unroller
== NULL
) ? NULL
: *unroller
;
330 los
.as_unroll
= lopts_on
;
333 rv
= n_source_macro(amp
->am_name
, args_base
);
334 a_amv_lopts
= los
.as_up
;
336 if(unroller
== NULL
){
337 if(los
.as_lopts
!= NULL
)
338 a_amv_lopts_unroll(&los
.as_lopts
);
340 *unroller
= los
.as_lopts
;
346 a_amv_mac_show(enum a_amv_mac_flags amf
){
347 size_t lc
, mc
, ti
, i
;
355 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
357 n_perr(_("Can't create temporary file for `define' or `account' listing"),
362 amf
&= a_AMV_MF_TYPE_MASK
;
363 typestr
= (amf
& a_AMV_MF_ACC
) ? "account" : "define";
365 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
366 struct a_amv_mac
*amp
;
368 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
369 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
370 struct a_amv_mac_line
**amlpp
;
377 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
378 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
379 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
381 fputs((*amlpp
)->aml_dat
, fp
);
390 page_or_print(fp
, lc
);
392 rv
= (ferror(fp
) == 0);
400 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
402 ui32_t line_cnt
, maxlen
;
404 struct linelist
*ll_next
;
405 struct a_amv_mac_line
*ll_amlp
;
406 } *llp
, *ll_head
, *ll_tail
;
407 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
408 struct a_amv_mac
*amp
;
412 memset(&line
, 0, sizeof line
);
416 /* Read in the lines which form the macro content */
417 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
421 n
.i
= n_lex_input("", TRU1
, &line
.s
, &line
.l
, NULL
);
425 n_err(_("Unterminated %s definition: \"%s\"\n"),
426 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
429 if(a_amv_mac_is_closing_angle(line
.s
))
432 /* Trim WS, remember amount of leading spaces for display purposes */
433 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
435 leaspc
= (leaspc
+ 8) & ~7;
440 for(; n
.ui
> 0 && whitechar(cp
[n
.ui
- 1]); --n
.ui
)
445 maxlen
= MAX(maxlen
, n
.ui
);
448 if(LIKELY(++line_cnt
< UI32_MAX
)){
449 struct a_amv_mac_line
*amlp
;
451 llp
= salloc(sizeof *llp
);
455 ll_tail
->ll_next
= llp
;
458 llp
->ll_amlp
= amlp
= smalloc(sizeof(*amlp
) -
459 VFIELD_SIZEOF(struct a_amv_mac_line
, aml_dat
) + n
.ui
);
460 amlp
->aml_len
= n
.ui
-1;
461 amlp
->aml_prespc
= leaspc
;
462 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
464 n_err(_("Too much content in %s definition: \"%s\"\n"),
465 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
470 /* Create the new macro */
471 n
.s
= strlen(name
) +1;
472 amp
= smalloc(sizeof(*amp
) - VFIELD_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
474 amp
->am_maxlen
= maxlen
;
475 amp
->am_line_cnt
= line_cnt
;
477 amp
->am_lopts
= NULL
;
478 memcpy(amp
->am_name
, name
, n
.s
);
480 struct a_amv_mac_line
**amlpp
;
482 amp
->am_line_dat
= amlpp
= smalloc(sizeof(*amlpp
) * ++line_cnt
);
483 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
484 *amlpp
++ = llp
->ll_amlp
;
488 /* Finally check wether such a macro already exists, in which case we throw
489 * it all away again. At least we know it would have worked */
490 if(a_amv_mac_lookup(name
, amp
, amf
) != NULL
){
491 n_err(_("A %s named \"%s\" already exists\n"),
492 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
504 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
507 free(amp
->am_line_dat
);
514 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
515 struct a_amv_mac
*amp
;
521 if(LIKELY(name
[0] != '*' || name
[1] != '\0')){
522 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
523 n_err(_("%s \"%s\" is not defined\n"),
524 (amf
& a_AMV_MF_ACC
? "Account" : "Macro"), name
);
528 struct a_amv_mac
**ampp
, *lamp
;
530 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[NELEM(a_amv_macs
)]);
532 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
533 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
534 /* xxx Expensive but rare: be simple */
535 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
536 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
548 a_amv_mac_free(struct a_amv_mac
*amp
){
549 struct a_amv_mac_line
**amlpp
;
552 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
554 free(amp
->am_line_dat
);
560 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
561 struct a_amv_var
*oavp
){
562 struct a_amv_var
*avp
;
566 /* Propagate unrolling up the stack, as necessary. Store all unroll
567 * information in the uppermost level which declared it wants to unroll, in
568 * order to avoid duplicates and thus needless actions when unrolling */
570 struct a_amv_lostack
*lalp
;
572 for(lalp
= NULL
; alp
!= NULL
; alp
= alp
->as_up
)
575 if((alp
= lalp
) == NULL
)
579 /* Check wether this variable is handled yet */
580 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
581 if(!strcmp(avp
->av_name
, name
))
584 nl
= strlen(name
) +1;
585 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
586 avp
= smalloc(sizeof(*avp
) - VFIELD_SIZEOF(struct a_amv_var
, av_name
) +
588 avp
->av_link
= alp
->as_lopts
;
590 memcpy(avp
->av_name
, name
, nl
);
592 avp
->av_value
= NULL
;
598 avp
->av_value
= avp
->av_name
+ nl
;
599 avp
->av_flags
= oavp
->av_flags
;
600 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
602 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
610 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
611 struct a_amv_lostack
*save_alp
;
612 struct a_amv_var
*x
, *avp
;
618 save_alp
= a_amv_lopts
;
623 vok_vset(x
->av_name
, x
->av_value
);
626 a_amv_lopts
= save_alp
;
631 a_amv_var_copy(char const *str
){
639 len
= strlen(str
) +1;
641 memcpy(news
, str
, len
);
648 a_amv_var_free(char *cp
){
656 a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
){
671 ok
= (val
!= NULL
&& var_folder_updated(*val
, &cp
));
672 if(ok
&& cp
!= NULL
){
673 a_amv_var_free(*val
);
674 /* Ensure we don't leak later on */
684 homedir
= *val
; /* XXX replace users with ok_vlook(HOME) */
692 myname
= *val
; /* XXX replace users with ok_vlook(LOGNAME) */
697 case ok_b_skipemptybody
:
701 if(enable
) /* DEFVAL will soon ensure a value otherwise! */
702 tempdir
= *val
; /* XXX replace users with ok_vlook(TMPDIR) */
705 if(options
& OPT_D_V
)
706 n_err(_("Redirecting $USER to standardized $LOGNAME environment\n"));
708 ok_vset(LOGNAME
, *val
);
711 flag
= (enable
&& !(options
& OPT_VERB
))
712 ? OPT_VERB
: OPT_VERB
| OPT_VERBVERB
;
715 DBG( n_err("Implementation error: never heard of %u\n", ok
); )
730 a_amv_var_canonify(char const *vn
){
735 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
737 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
744 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
746 struct a_amv_var_map
const *avmp
;
749 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
750 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
752 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
753 ui32_t x
= a_amv_var_revmap
[i
];
755 if(x
== a_AMV_VAR_REV_ILL
)
758 avmp
= &a_amv_var_map
[x
];
759 if(avmp
->avm_hash
== hash
&&
760 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
761 avcp
->avc_map
= avmp
;
762 avcp
->avc_okey
= (enum okeys
)x
;
766 if(++i
== a_AMV_VAR_REV_PRIME
){
767 #ifdef a_AMV_VAR_REV_WRAPAROUND
774 avcp
->avc_map
= NULL
;
778 return (avcp
!= NULL
);
782 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
){
785 struct a_amv_var_map
const *avmp
;
786 struct a_amv_var
*avp
;
790 struct a_amv_var
**avpp
, *lavp
;
792 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
794 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
795 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
796 /* Relink as head, hope it "sorts on usage" over time.
797 * _clear() relies on this behaviour */
799 lavp
->av_link
= avp
->av_link
;
800 avp
->av_link
= *avpp
;
807 /* If this is not an assembled variable we need to consider some special
808 * initialization cases and eventually create the variable anew */
809 if(LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
810 /* Does it have an import-from-environment flag? */
811 if(UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
812 if((cp
= getenv(avcp
->avc_name
)) != NULL
){
813 /* May be better not to use that one, though? */
814 bool_t isempty
, isbltin
;
816 isempty
= (*cp
== '\0' &&
817 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
818 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
829 /* A first-time init switch is to be handled now and here */
830 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
831 static struct a_amv_var_defval
const **arr
,
832 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
836 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
838 arr
[i
] = &a_amv_var_i3vals
[i
];
841 for(i
= 0; arr
[i
] != NULL
; ++i
)
842 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
843 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? ""
844 : arr
[i
]->avdv_value
;
845 /* Remove this entry, hope entire block becomes no-op asap */
848 while(arr
[i
++] != NULL
);
853 /* The virtual variables */
854 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
855 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
856 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
857 avp
= UNCONST(a_amv_var_virts
[i
].avv_var
);
863 /* Place this last because once it is set first the variable will never
864 * be removed again and thus match in the first block above */
865 if(UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
866 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
867 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
868 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? ""
869 : a_amv_var_defvals
[i
].avdv_value
;
880 return (avp
!= NULL
);
883 struct a_amv_var
**avpp
;
884 size_t l
= strlen(avcp
->avc_name
) +1;
886 avcp
->avc_var
= avp
= smalloc(sizeof(*avp
) -
887 VFIELD_SIZEOF(struct a_amv_var
, av_name
) + l
);
888 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
890 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
891 avp
->av_value
= a_amv_var_copy(cp
);
895 avp
->av_flags
= avmp
->avm_flags
;
897 if(avp
->av_flags
& a_AMV_VF_VIP
)
898 a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
);
899 if(avp
->av_flags
& a_AMV_VF_ENV
)
900 a_amv_var__putenv(avcp
, avp
);
906 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
908 struct a_amv_var
*avp
;
910 struct a_amv_var_map
const *avmp
;
915 ok
= a_amv_var_clear(avcp
, force_env
);
919 a_amv_var_lookup(avcp
);
921 if((avmp
= avcp
->avc_map
) != NULL
){
924 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0)){
925 n_err(_("Variable is readonly: \"%s\"\n"), avcp
->avc_name
);
928 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
929 n_err(_("Variable must not be empty: \"%s\"\n"), avcp
->avc_name
);
932 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
933 !(pstate
& PS_STARTED
))){
934 n_err(_("Variable cannot be set in a resource file: \"%s\"\n"),
942 /* Don't care what happens later on, store this in the unroll list */
943 if(a_amv_lopts
!= NULL
)
944 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
946 if((avp
= avcp
->avc_var
) == NULL
){
947 struct a_amv_var
**avpp
;
948 size_t l
= strlen(avcp
->avc_name
) +1;
950 avcp
->avc_var
= avp
= smalloc(sizeof(*avp
) -
951 VFIELD_SIZEOF(struct a_amv_var
, av_name
) + l
);
952 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
957 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
958 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
961 oval
= avp
->av_value
;
964 avp
->av_value
= a_amv_var_copy(value
);
966 /* Via `set' etc. the user may give even boolean options non-boolean
967 * values, ignore that and force boolean */
968 if(avp
->av_flags
& a_AMV_VF_BOOL
){
969 if((options
& OPT_D_VV
) && *value
!= '\0')
970 n_err(_("\"%s\" is a boolean variable, ignoring value: %s\n"),
971 avcp
->avc_name
, value
);
972 avp
->av_value
= UNCONST(value
= "");
974 avp
->av_value
= a_amv_var_copy(value
);
976 /* Check if update allowed XXX wasteful on error! */
977 if((avp
->av_flags
& a_AMV_VF_VIP
) &&
978 !(ok
= a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
))){
979 char *cp
= avp
->av_value
;
981 avp
->av_value
= oval
;
986 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
987 avp
->av_flags
|= a_AMV_VF_LINKED
;
988 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
989 ok
= a_amv_var__putenv(avcp
, avp
);
991 a_amv_var_free(oval
);
998 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1006 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1008 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1010 if((rv
= (putenv(cp
) == 0))){
1013 if((ocp
= avp
->av_env
) != NULL
)
1024 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1025 struct a_amv_var
**avpp
, *avp
;
1026 struct a_amv_var_map
const *avmp
;
1032 if(UNLIKELY(!a_amv_var_lookup(avcp
))){
1034 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1035 else if(!(pstate
& PS_ROBOT
) && (options
& OPT_D_V
))
1036 n_err(_("Can't unset undefined variable: \"%s\"\n"), avcp
->avc_name
);
1040 if(LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1041 if(UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0)){
1042 n_err(_("Variable may not be unset: \"%s\"\n"), avcp
->avc_name
);
1046 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1047 !(rv
= a_amv_var_check_vips(avcp
->avc_okey
, FAL0
, NULL
)))
1051 if(a_amv_lopts
!= NULL
)
1052 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1054 avp
= avcp
->avc_var
;
1055 avcp
->avc_var
= NULL
;
1056 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1057 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1058 *avpp
= (*avpp
)->av_link
;
1062 char *envval
= NULL
;
1064 char *envval
= avp
->av_env
;
1066 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1067 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1069 a_amv_var_free(avp
->av_value
);
1072 /* XXX Fun part, extremely simple-minded for now: if this variable has
1073 * XXX a default value, immediately reinstantiate it! */
1074 if(UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1075 a_amv_var_lookup(avcp
);
1082 a_amv_var__clearenv(char const *name
, char *value
){
1084 extern char **environ
;
1096 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1101 while(*ecpp
++ != NULL
);
1111 a_amv_var_show_all(void){
1112 struct n_string msg
, *msgp
;
1115 struct a_amv_var
*avp
;
1116 char const **vacp
, **cap
;
1119 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1120 n_perr(_("Can't create temporary file for `set' listing"), 0);
1124 /* We need to instantiate first-time-inits and default values here, so that
1125 * they will be regular members of our _vars[] table */
1126 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1127 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1128 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1129 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1131 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1132 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1134 no
+= a_AMV_VAR_VIRTS_CNT
;
1136 vacp
= salloc(no
* sizeof(*vacp
));
1138 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1139 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1140 *cap
++ = avp
->av_name
;
1141 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1142 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1145 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1148 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1149 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1150 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1153 page_or_print(fp
, i
);
1160 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1164 rv
= strcmp(*(char**)UNCONST(s1
), *(char**)UNCONST(s2
));
1170 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1171 struct a_amv_var_carrier avc
;
1175 msgp
= n_string_trunc(msgp
, 0);
1178 a_amv_var_revlookup(&avc
, name
);
1179 if(!a_amv_var_lookup(&avc
)){
1180 msgp
= n_string_assign_cp(msgp
, _("No such variable: \""));
1181 msgp
= n_string_push_cp(msgp
, name
);
1182 msgp
= n_string_push_c(msgp
, '"');
1186 if(options
& OPT_D_V
){
1187 if(avc
.avc_map
== NULL
){
1188 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1189 msgp
= n_string_push_cp(msgp
, "assembled");
1196 {a_AMV_VF_VIRT
, "virtual"},
1197 {a_AMV_VF_RDONLY
, "readonly"},
1198 {a_AMV_VF_NODEL
, "nodelete"},
1199 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1200 {a_AMV_VF_ENV
, "sync-environ"},
1201 {a_AMV_VF_I3VAL
, "initial-value"},
1202 {a_AMV_VF_DEFVAL
, "default-value"},
1203 {a_AMV_VF_LINKED
, "`environ' link"}
1206 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[NELEM(tbase
)]); ++tp
)
1207 if(avc
.avc_var
->av_flags
& tp
->flags
){
1208 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1209 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1214 msgp
= n_string_push_cp(msgp
, "\n ");
1217 if(avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)
1218 msgp
= n_string_push_cp(msgp
, "# ");
1219 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1220 msgp
= n_string_push_cp(msgp
, "environ ");
1221 msgp
= n_string_push_cp(msgp
, "set ");
1222 msgp
= n_string_push_cp(msgp
, name
);
1223 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1224 msgp
= n_string_push_buf(msgp
, "=\"", 2);
1225 msgp
= n_string_push_cp(msgp
,
1226 a_amv_var__simple_quote(avc
.avc_var
->av_value
));
1227 msgp
= n_string_push_c(msgp
, '"');
1231 msgp
= n_string_push_c(msgp
, '\n');
1232 fputs(n_string_cp(msgp
), fp
);
1234 return (i
> 0 ? 2 : 1);
1238 a_amv_var__simple_quote(char const *cp
){ /* TODO <> string_quote(), etc.. */
1241 char const *cp_base
;
1245 for(i
= 0, cp_base
= cp
; (c
= *cp
) != '\0'; ++i
, ++cp
)
1257 for(esc
= FAL0
, i
= 0, cp
= cp_base
; (c
= *cp
) != '\0'; rv
[i
++] = c
, ++cp
){
1262 c
= (c
== '\n' ? 'n' : 't');
1280 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1281 char *cp
, *cp2
, *varbuf
, c
;
1287 while((cp
= *ap
++) != NULL
){
1289 cp2
= varbuf
= salloc(strlen(cp
) +1);
1291 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1292 if(cntrlchar(c
) || whitechar(c
)){
1293 n_err(_("Variable name with control or newline character ignored: "
1294 "\"%s\"\n"), ap
[-1]);
1307 n_err(_("Empty variable name ignored\n"));
1310 struct a_amv_var_carrier avc
;
1313 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1314 varbuf
= &varbuf
[2];
1316 a_amv_var_revlookup(&avc
, varbuf
);
1319 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1321 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1336 if((args
= v
)[0] == NULL
){
1337 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1341 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1343 n_err(_("Synopsis: define: <name> {\n"));
1347 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1354 c_undefine(void *v
){
1362 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1363 while(*++args
!= NULL
);
1372 char const *errs
, *name
;
1373 struct a_amv_mac
*amp
;
1378 if((args
= v
)[0] == NULL
|| (args
[1] != NULL
&& args
[2] != NULL
)){
1379 errs
= _("Synopsis: call: <%s>\n");
1384 if((amp
= a_amv_mac_lookup(name
= *args
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1385 errs
= _("Undefined macro `call'ed: \"%s\"\n");
1389 rv
= (a_amv_mac_exec(amp
, NULL
, FAL0
) == FAL0
);
1399 check_folder_hook(bool_t nmail
){ /* TODO temporary, v15: drop */
1402 struct a_amv_mac
*amp
;
1403 struct a_amv_var
**unroller
;
1407 var
= salloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1409 /* First try the fully resolved path */
1410 snprintf(var
, len
, "folder-hook-%s", mailname
);
1411 if((cp
= vok_vlook(var
)) != NULL
)
1414 /* If we are under *folder*, try the usual +NAME syntax, too */
1415 if(displayname
[0] == '+'){
1416 char *x
= mailname
+ len
;
1418 for(; x
> mailname
; --x
)
1420 snprintf(var
, len
, "folder-hook-+%s", x
);
1421 if((cp
= vok_vlook(var
)) != NULL
)
1427 /* Plain *folder-hook* is our last try */
1428 if((cp
= ok_vlook(folder_hook
)) == NULL
)
1432 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1433 n_err(_("Cannot call *folder-hook* for \"%s\": "
1434 "macro \"%s\" does not exist\n"), displayname
, cp
);
1439 pstate
&= ~PS_HOOK_MASK
;
1441 pstate
|= PS_HOOK_NEWMAIL
;
1445 unroller
= &a_amv_folder_hook_lopts
;
1447 rv
= a_amv_mac_exec(amp
, unroller
, TRU1
);
1448 pstate
&= ~PS_HOOK_MASK
;
1456 call_compose_mode_hook(char const *macname
){ /* TODO temporary, v15: drop */
1457 struct a_amv_mac
*amp
;
1460 if((amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
1461 n_err(_("Cannot call *on-compose-**: macro \"%s\" does not exist\n"),
1464 pstate
&= ~PS_HOOK_MASK
;
1466 a_amv_mac_exec(amp
, &a_amv_compose_lopts
, TRU1
);
1467 pstate
&= ~PS_HOOK_MASK
;
1475 struct a_amv_mac
*amp
;
1476 int rv
, i
, oqf
, nqf
;
1481 if((args
= v
)[0] == NULL
){
1482 rv
= (a_amv_mac_show(a_AMV_MF_ACC
) == FAL0
);
1486 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
1487 if(args
[2] != NULL
){
1488 n_err(_("Synopsis: account: <name> {\n"));
1491 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
1492 n_err(_("`account': error: \"%s\" is a reserved name\n"),
1496 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACC
) == FAL0
);
1500 if(pstate
& PS_HOOK_MASK
){
1501 n_err(_("`account': can't change account from within a hook\n"));
1505 save_mbox_for_possible_quitstuff();
1508 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1509 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACC
)) == NULL
) {
1510 n_err(_("`account': account \"%s\" does not exist\n"), args
[0]);
1514 oqf
= savequitflags();
1516 if(a_amv_acc_curr
!= NULL
){
1517 if(a_amv_acc_curr
->am_lopts
!= NULL
)
1518 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
1519 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DEL
)
1520 a_amv_mac_free(a_amv_acc_curr
);
1523 account_name
= (amp
!= NULL
) ? amp
->am_name
: NULL
;
1524 a_amv_acc_curr
= amp
;
1527 assert(amp
->am_lopts
== NULL
);
1528 if(!a_amv_mac_exec(amp
, &
->am_lopts
, TRU1
)){
1529 /* XXX account switch incomplete, unroll? */
1530 n_err(_("`account': switching to account \"%s\" failed\n"), args
[0]);
1535 if((pstate
& (PS_STARTED
| PS_HOOK_MASK
)) == PS_STARTED
){
1536 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1537 restorequitflags(oqf
);
1538 if((i
= setfile("%", 0)) < 0)
1540 check_folder_hook(FAL0
);
1541 if(i
> 0 && !ok_blook(emptystart
))
1543 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1544 restorequitflags(nqf
);
1553 c_unaccount(void *v
){
1561 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACC
);
1562 while(*++args
!= NULL
);
1568 c_localopts(void *v
){
1575 if(a_amv_lopts
== NULL
){
1576 n_err(_("Cannot use `localopts' but from within a "
1577 "`define' or `account'\n"));
1581 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
1589 temporary_localopts_free(void){ /* XXX intermediate hack */
1591 if(a_amv_compose_lopts
!= NULL
){
1592 void *save
= a_amv_lopts
;
1595 a_amv_lopts_unroll(&a_amv_compose_lopts
);
1596 a_amv_compose_lopts
= NULL
;
1603 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1605 if(a_amv_folder_hook_lopts
!= NULL
){
1606 void *save
= a_amv_lopts
;
1609 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
1610 a_amv_folder_hook_lopts
= NULL
;
1617 n_var_oklook(enum okeys okey
){
1618 struct a_amv_var_carrier avc
;
1620 struct a_amv_var_map
const *avmp
;
1623 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1624 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1625 avc
.avc_hash
= avmp
->avm_hash
;
1626 avc
.avc_okey
= okey
;
1628 if(a_amv_var_lookup(&avc
))
1629 rv
= avc
.avc_var
->av_value
;
1637 n_var_okset(enum okeys okey
, uintptr_t val
){
1638 struct a_amv_var_carrier avc
;
1640 struct a_amv_var_map
const *avmp
;
1643 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1644 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1645 avc
.avc_hash
= avmp
->avm_hash
;
1646 avc
.avc_okey
= okey
;
1648 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? "" : (char const*)val
), FAL0
);
1654 n_var_okclear(enum okeys okey
){
1655 struct a_amv_var_carrier avc
;
1657 struct a_amv_var_map
const *avmp
;
1660 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1661 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1662 avc
.avc_hash
= avmp
->avm_hash
;
1663 avc
.avc_okey
= okey
;
1665 rv
= a_amv_var_clear(&avc
, FAL0
);
1671 n_var_voklook(char const *vokey
){
1672 struct a_amv_var_carrier avc
;
1676 a_amv_var_revlookup(&avc
, vokey
);
1678 rv
= a_amv_var_lookup(&avc
) ? avc
.avc_var
->av_value
: NULL
;
1684 n_var_vokset(char const *vokey
, uintptr_t val
){
1685 struct a_amv_var_carrier avc
;
1689 a_amv_var_revlookup(&avc
, vokey
);
1691 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? "" : (char const*)val
), FAL0
);
1697 n_var_vokclear(char const *vokey
){
1698 struct a_amv_var_carrier avc
;
1702 a_amv_var_revlookup(&avc
, vokey
);
1704 ok
= a_amv_var_clear(&avc
, FAL0
);
1711 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
1712 enum okey_xlook_mode oxm
){
1713 struct a_amv_var_carrier avc
;
1714 struct str
const *us
;
1717 struct a_amv_var_map
const *avmp
;
1720 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
1722 /* For simplicity: allow this case too */
1723 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
)))
1726 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
1727 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
1728 avc
.avc_okey
= okey
;
1730 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
1731 nlen
= strlen(avc
.avc_name
);
1732 nbuf
= salloc(nlen
+ 1 + us
->l
+1);
1733 memcpy(nbuf
, avc
.avc_name
, nlen
);
1736 /* One of .url_u_h_p and .url_h_p we test in here */
1737 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1738 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1739 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1740 if(a_amv_var_lookup(&avc
))
1745 us
= &urlp
->url_h_p
;
1746 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
1747 avc
.avc_name
= a_amv_var_canonify(nbuf
);
1748 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
1749 if(a_amv_var_lookup(&avc
)){
1751 rv
= avc
.avc_var
->av_value
;
1757 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
1762 #endif /* HAVE_SOCKETS */
1770 if(*(ap
= v
) == NULL
){
1771 a_amv_var_show_all();
1774 err
= !a_amv_var_c_set(ap
, FAL0
);
1785 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
1786 err
|= !n_var_vokclear(*ap
);
1796 if(*(ap
= v
) == NULL
)
1799 struct n_string msg
, *msgp
= &msg
;
1801 msgp
= n_string_creat(msgp
);
1802 for(; *ap
!= NULL
; ++ap
)
1803 a_amv_var_show(*ap
, stdout
, msgp
);
1807 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
1812 struct a_amv_var_carrier avc
;
1816 sighandler_type sigint
;
1819 sigint
= safe_signal(SIGINT
, SIG_IGN
);
1821 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
1822 memset(&avc
, 0, sizeof avc
);
1824 a_amv_var_revlookup(&avc
, *argv
);
1826 if(avc
.avc_map
!= NULL
){
1827 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
1828 n_err(_("`varedit': can't edit boolean variable \"%s\"\n"),
1832 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
1833 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1839 a_amv_var_lookup(&avc
);
1841 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1843 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1846 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
1847 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
1848 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1855 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
1861 off_t l
= fsize(nf
);
1864 base
= salloc((size_t)l
+1);
1866 for(l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
1867 if(c
== '\n' || c
== '\r'){
1871 *val
= (char)(uc_i
)c
;
1877 if(!a_amv_var_set(&avc
, base
, FAL0
))
1882 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
1888 safe_signal(SIGINT
, sigint
);
1895 struct a_amv_var_carrier avc
;
1901 if((islnk
= !strcmp(*(ap
= v
), "link")) || !strcmp(*ap
, "unlink")){
1902 for(err
= 0; *++ap
!= NULL
;){
1903 a_amv_var_revlookup(&avc
, *ap
);
1905 if(a_amv_var_lookup(&avc
) && (islnk
||
1906 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
1908 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
1910 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
1911 if(options
& OPT_D_V
)
1912 n_err(_("`environ': link: already established for \"%s\"\n"),
1916 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
1917 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
1918 a_amv_var__putenv(&avc
, avc
.avc_var
);
1920 n_err(_("`environ': unlink: no link established: \"%s\"\n"), *ap
);
1923 char const *evp
= getenv(*ap
);
1926 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
1928 n_err(_("`environ': link: cannot link non-existent variable "
1934 }else if(!strcmp(*ap
, "set"))
1935 err
= !a_amv_var_c_set(++ap
, TRU1
);
1936 else if(!strcmp(*ap
, "unset")){
1937 for(err
= 0; *++ap
!= NULL
;){
1938 a_amv_var_revlookup(&avc
, *ap
);
1940 if(!a_amv_var_clear(&avc
, TRU1
))
1944 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));