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 . 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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
19 * Copyright (c) 1980, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
53 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
54 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
57 /* Special "pseudo macro" that stabs you from the back */
58 #define a_AMV_MACKY_MACK ((struct a_amv_mac*)-1)
60 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
61 #define a_AMV_PRIME HSHSIZE
62 #define a_AMV_NAME2HASH(N) torek_hash(N)
63 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
67 a_AMV_MF_ACC
= 1<<0, /* This macro is an `account' */
68 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACC
,
69 a_AMV_MF_UNDEF
= 1<<1, /* Unlink after lookup */
70 a_AMV_MF_DEL
= 1<<7, /* Delete in progress, free once refcnt==0 */
74 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
75 * _IMPORT implies _ENV; it doesn't verify anything... */
78 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
79 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
80 a_AMV_VF_NOLOPTS
= 1<<2, /* May not be tracked by `localopts' */
81 a_AMV_VF_RDONLY
= 1<<3, /* May not be set by user */
82 a_AMV_VF_NODEL
= 1<<4, /* May not be deleted */
83 a_AMV_VF_NOTEMPTY
= 1<<5, /* May not be assigned an empty value */
84 a_AMV_VF_NOCNTRLS
= 1<<6, /* Value may not contain control characters */
85 a_AMV_VF_NUM
= 1<<7, /* Value must be a 32-bit number */
86 a_AMV_VF_POSNUM
= 1<<8, /* Value must be positive 32-bit number */
87 a_AMV_VF_LOWER
= 1<<9, /* Values will be stored in a lowercase version */
88 a_AMV_VF_VIP
= 1<<10, /* Wants _var_check_vips() evaluation */
89 a_AMV_VF_IMPORT
= 1<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
90 a_AMV_VF_ENV
= 1<<12, /* Update environment on change */
91 a_AMV_VF_I3VAL
= 1<<13, /* Has an initial value */
92 a_AMV_VF_DEFVAL
= 1<<14, /* Has a default value */
93 a_AMV_VF_LINKED
= 1<<15, /* `environ' linked */
94 a_AMV_VF__MASK
= (1<<(15+1)) - 1
97 /* We support some special parameter names for one-letter variable names;
98 * note these have counterparts in the code that manages shell expansion! */
99 enum a_amv_var_special_type
{
100 a_AMV_VST_STAR
, /* * */
101 a_AMV_VST_AT
, /* @ */
102 a_AMV_VST_NOSIGN
/* # */
103 /* ? These are different in that they are no macro-local variables, but in
110 struct a_amv_mac
*am_next
;
111 ui32_t am_maxlen
; /* of any line in .am_line_dat */
112 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
113 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
114 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
115 ui32_t am_refcnt
; /* 0-based for `un{account,define}' purposes */
116 ui8_t am_flags
; /* enum a_amv_mac_flags */
117 char am_name
[n_VFIELD_SIZE(3)]; /* of this macro */
119 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
121 struct a_amv_mac_line
{
123 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
124 char aml_dat
[n_VFIELD_SIZE(0)];
127 struct a_amv_mac_call_args
{
128 char const *amca_name
; /* For MACKY_MACK, this is *0*! */
129 struct a_amv_mac
*amca_amp
; /* "const", but for am_refcnt */
130 struct a_amv_var
**amca_unroller
;
131 void (*amca_hook_pre
)(void *);
133 bool_t amca_lopts_on
;
134 bool_t amca_ps_hook_mask
;
136 ui16_t amca_argc
; /* Max is SI16_MAX */
137 char const **amca_argv
;
140 struct a_amv_lostack
{
141 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
142 struct a_amv_mac_call_args
*as_amcap
;
143 struct a_amv_lostack
*as_up
; /* Outer context */
144 struct a_amv_var
*as_lopts
;
145 bool_t as_unroll
; /* Unrolling enabled? */
150 struct a_amv_var
*av_link
;
153 char *av_env
; /* Actively managed putenv(3) memory */
155 ui16_t av_flags
; /* enum a_amv_var_flags */
156 char av_name
[n_VFIELD_SIZE(6)];
158 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
160 struct a_amv_var_map
{
163 ui16_t avm_flags
; /* enum a_amv_var_flags */
165 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
167 struct a_amv_var_virt
{
170 struct a_amv_var
const *avv_var
;
173 struct a_amv_var_defval
{
176 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
179 struct a_amv_var_carrier
{
180 char const *avc_name
;
183 struct a_amv_var
*avc_var
;
184 struct a_amv_var_map
const *avc_map
;
187 bool_t avc_is_special
; /* Only if avc_okey == ok_v___special_param */
188 /* Numeric if .avc_is_special==TRUM1, else enum a_amv_var_special_type */
189 ui16_t avc_special_prop
;
192 /* Include the constant mk-okey-map.pl output */
196 /* The currently active account */
197 static struct a_amv_mac
*a_amv_acc_curr
;
199 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
201 /* Unroll list of currently running macro stack */
202 static struct a_amv_lostack
*a_amv_lopts
;
204 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
206 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
207 * TODO today via a static lostack, it should be a field in mailbox, once that
208 * TODO is a real multi-instance object */
209 static struct a_amv_var
*a_amv_folder_hook_lopts
;
211 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
212 static struct a_amv_var
*a_amv_compose_lopts
;
214 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
215 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
216 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
217 * plain lookup cases or when _UNDEF was performed on a currently active entry
218 * (the entry will have been unlinked, and the _MF_DEL will be honoured once
219 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
220 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
221 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
223 /* `call', `call_if' */
224 static int a_amv_mac_call(void *v
, bool_t silent_nexist
);
226 /* Execute a macro; amcap must reside in LOFI memory */
227 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
229 static void a_amv_mac__finalize(void *vp
);
231 /* User display helpers */
232 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
234 /* _def() returns error for faulty definitions and already existing * names,
235 * _undef() returns error if a named thing doesn't exist */
236 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
237 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
240 static void a_amv_mac_free(struct a_amv_mac
*amp
);
242 /* Update replay-log */
243 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
244 struct a_amv_var
*oavp
);
245 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
247 /* Special cased value string allocation */
248 static char *a_amv_var_copy(char const *str
);
249 static void a_amv_var_free(char *cp
);
251 /* Check for special housekeeping */
252 static bool_t
a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
);
254 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
255 static bool_t
a_amv_var_check_nocntrls(char const *val
);
256 static bool_t
a_amv_var_check_num(char const *val
, bool_t posnum
);
258 /* If a variable name begins with a lowercase-character and contains at
259 * least one '@', it is converted to all-lowercase. This is necessary
260 * for lookups of names based on email addresses.
261 * Following the standard, only the part following the last '@' should
262 * be lower-cased, but practice has established otherwise here */
263 static char const *a_amv_var_canonify(char const *vn
);
265 /* Try to reverse lookup an option name to an enum okeys mapping.
266 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
267 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
270 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
271 * Sets .avc_prime; .avc_var is NULL if not found.
272 * Here it is where we care for _I3VAL and _DEFVAL, too.
273 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
274 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
275 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
276 * return FAL0, then! */
277 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
280 /* Set var from .avc_(map|name|hash), return success */
281 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
284 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
285 struct a_amv_var
*avp
);
287 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
288 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
290 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
292 /* List all variables */
293 static void a_amv_var_show_all(void);
295 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
297 /* Actually do print one, return number of lines written */
298 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
300 /* Shared c_set() and c_environ():set impl, return success */
301 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
303 static struct a_amv_mac
*
304 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
305 enum a_amv_mac_flags amf
){
306 struct a_amv_mac
*amp
, **ampp
;
308 enum a_amv_mac_flags save_amf
;
312 amf
&= a_AMV_MF_TYPE_MASK
;
313 h
= a_AMV_NAME2HASH(name
);
314 h
= a_AMV_HASH2PRIME(h
);
315 ampp
= &a_amv_macs
[h
];
317 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
318 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
319 !strcmp(amp
->am_name
, name
)){
320 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
323 *ampp
= amp
->am_next
;
325 if(amp
->am_refcnt
> 0){
326 amp
->am_flags
|= a_AMV_MF_DEL
;
327 if(n_poption
& n_PO_D_V
)
328 n_err(_("Delayed deletion of currently active %s: %s\n"),
329 (amp
->am_flags
& a_AMV_MF_ACC
? "account" : "define"), name
);
332 amp
= (struct a_amv_mac
*)-1;
339 ampp
= &a_amv_macs
[h
];
340 newamp
->am_next
= *ampp
;
350 a_amv_mac_call(void *v
, bool_t silent_nexist
){
352 struct a_amv_mac
*amp
;
356 name
= *(char const**)v
;
358 if((amp
= a_amv_mac_lookup(name
, NULL
, a_AMV_MF_NONE
)) != NULL
){
359 struct a_amv_mac_call_args
*amcap
;
361 amcap
= n_lofi_alloc(sizeof *amcap
);
362 memset(amcap
, 0, sizeof *amcap
);
363 amcap
->amca_name
= name
;
364 amcap
->amca_amp
= amp
;
369 for(argc
= 0, argv
= v
; *++argv
!= NULL
; ++argc
)
372 amcap
->amca_argc
= argc
;
373 amcap
->amca_argv
= &(argv
= v
)[1];
376 rv
= (a_amv_mac_exec(amcap
) == FAL0
);
377 }else if((rv
= (silent_nexist
== FAL0
)))
378 n_err(_("Undefined macro `call'ed: %s\n"), n_shexp_quote_cp(name
, FAL0
));
384 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
385 struct a_amv_lostack
*losp
;
386 struct a_amv_mac_line
**amlp
;
387 char **args_base
, **args
;
388 struct a_amv_mac
*amp
;
392 amp
= amcap
->amca_amp
;
393 assert(amp
!= NULL
&& amp
!= a_AMV_MACKY_MACK
);
395 /* XXX Unfortunately we yet need to dup the macro lines! :( */
396 args_base
= args
= smalloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
397 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
398 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
401 losp
= n_lofi_alloc(sizeof *losp
);
402 losp
->as_global_saved
= a_amv_lopts
;
403 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
404 losp
->as_up
= losp
->as_global_saved
;
405 losp
->as_lopts
= NULL
;
408 losp
->as_lopts
= *amcap
->amca_unroller
;
410 losp
->as_unroll
= amcap
->amca_lopts_on
;
413 if(amcap
->amca_hook_pre
!= NULL
)
414 n_PS_ROOT_BLOCK((*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
));
415 rv
= n_source_macro(n_LEXINPUT_NONE
, amp
->am_name
, args_base
,
416 &a_amv_mac__finalize
, losp
);
422 a_amv_mac__finalize(void *vp
){
423 struct a_amv_mac
*amp
;
424 struct a_amv_mac_call_args
*amcap
;
425 struct a_amv_lostack
*losp
;
429 a_amv_lopts
= losp
->as_global_saved
;
431 if((amcap
= losp
->as_amcap
)->amca_unroller
== NULL
){
432 if(losp
->as_lopts
!= NULL
)
433 a_amv_lopts_unroll(&losp
->as_lopts
);
435 *amcap
->amca_unroller
= losp
->as_lopts
;
437 if(amcap
->amca_ps_hook_mask
)
438 n_pstate
&= ~n_PS_HOOK_MASK
;
440 if((amp
= amcap
->amca_amp
) != a_AMV_MACKY_MACK
&& amp
!= NULL
&&
441 --amp
->am_refcnt
== 0 && (amp
->am_flags
& a_AMV_MF_DEL
))
450 a_amv_mac_show(enum a_amv_mac_flags amf
){
451 size_t lc
, mc
, ti
, i
;
459 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
461 n_perr(_("Can't create temporary file for `define' or `account' listing"),
466 amf
&= a_AMV_MF_TYPE_MASK
;
467 typestr
= (amf
& a_AMV_MF_ACC
) ? "account" : "define";
469 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
470 struct a_amv_mac
*amp
;
472 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
473 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
474 struct a_amv_mac_line
**amlpp
;
481 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
482 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
483 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
485 fputs((*amlpp
)->aml_dat
, fp
);
494 page_or_print(fp
, lc
);
496 rv
= (ferror(fp
) == 0);
504 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
506 ui32_t line_cnt
, maxlen
;
508 struct linelist
*ll_next
;
509 struct a_amv_mac_line
*ll_amlp
;
510 } *llp
, *ll_head
, *ll_tail
;
511 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
512 struct a_amv_mac
*amp
;
516 memset(&line
, 0, sizeof line
);
520 /* TODO We should have our input state machine which emits Line events,
521 * TODO and hook different consumers dependent on our content, as stated
522 * TODO in i think lex_input; */
523 /* Read in the lines which form the macro content */
524 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
528 n
.i
= n_lex_input(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_NL_ESC
, n_empty
,
529 &line
.s
, &line
.l
, NULL
);
533 n_err(_("Unterminated %s definition: %s\n"),
534 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
538 /* Trim WS, remember amount of leading spaces for display purposes */
539 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
541 leaspc
= (leaspc
+ 8u) & ~7u;
546 for(; n
.ui
> 0 && spacechar(cp
[n
.ui
- 1]); --n
.ui
)
551 maxlen
= n_MAX(maxlen
, n
.ui
);
554 /* Is is the closing brace? */
558 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
559 struct a_amv_mac_line
*amlp
;
561 llp
= salloc(sizeof *llp
);
565 ll_tail
->ll_next
= llp
;
568 llp
->ll_amlp
= amlp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
570 amlp
->aml_len
= n
.ui
-1;
571 amlp
->aml_prespc
= leaspc
;
572 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
574 n_err(_("Too much content in %s definition: %s\n"),
575 (amf
& a_AMV_MF_ACC
? "account" : "macro"), name
);
580 /* Create the new macro */
581 n
.s
= strlen(name
) +1;
582 amp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
584 amp
->am_maxlen
= maxlen
;
585 amp
->am_line_cnt
= line_cnt
;
588 amp
->am_lopts
= NULL
;
589 memcpy(amp
->am_name
, name
, n
.s
);
591 struct a_amv_mac_line
**amlpp
;
593 amp
->am_line_dat
= amlpp
= smalloc(sizeof(*amlpp
) * ++line_cnt
);
594 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
595 *amlpp
++ = llp
->ll_amlp
;
599 /* Create entry, replace a yet existing one as necessary */
600 a_amv_mac_lookup(name
, amp
, amf
| a_AMV_MF_UNDEF
);
609 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
612 free(amp
->am_line_dat
);
619 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
620 struct a_amv_mac
*amp
;
626 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
627 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
628 n_err(_("%s not defined: %s\n"),
629 (amf
& a_AMV_MF_ACC
? "Account" : "Macro"), name
);
633 struct a_amv_mac
**ampp
, *lamp
;
635 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
637 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
638 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
639 /* xxx Expensive but rare: be simple */
640 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
641 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
653 a_amv_mac_free(struct a_amv_mac
*amp
){
654 struct a_amv_mac_line
**amlpp
;
657 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
659 free(amp
->am_line_dat
);
665 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
666 struct a_amv_var
*oavp
){
667 struct a_amv_var
*avp
;
671 /* Propagate unrolling up the stack, as necessary */
676 if((alp
= alp
->as_up
) == NULL
)
680 /* Check whether this variable is handled yet */
681 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
682 if(!strcmp(avp
->av_name
, name
))
685 nl
= strlen(name
) +1;
686 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
687 avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
688 avp
->av_link
= alp
->as_lopts
;
690 memcpy(avp
->av_name
, name
, nl
);
692 avp
->av_value
= NULL
;
698 avp
->av_value
= &avp
->av_name
[nl
];
699 avp
->av_flags
= oavp
->av_flags
;
700 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
702 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
710 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
711 struct a_amv_lostack
*save_alp
;
712 struct a_amv_var
*x
, *avp
;
718 save_alp
= a_amv_lopts
;
723 n_PS_ROOT_BLOCK(n_var_vset(x
->av_name
, (uintptr_t)x
->av_value
));
726 a_amv_lopts
= save_alp
;
731 a_amv_var_copy(char const *str
){
737 news
= n_UNCONST(n_empty
);
738 else if(str
[1] == '\0'){
740 news
= n_UNCONST(n_1
);
741 else if(str
[0] == '0')
742 news
= n_UNCONST(n_0
);
745 }else if(str
[2] == '\0' && str
[0] == '-' && str
[1] == '1')
746 news
= n_UNCONST(n_m1
);
749 len
= strlen(str
) +1;
751 memcpy(news
, str
, len
);
758 a_amv_var_free(char *cp
){
760 if(cp
[0] != '\0' && cp
!= n_0
&& cp
!= n_1
&& cp
!= n_m1
)
766 a_amv_var_check_vips(enum okeys okey
, bool_t enable
, char **val
){
779 /* Invalidate any resolved folder then, too
782 n_PS_ROOT_BLOCK(ok_vclear(_folder_resolved
));
789 flag
= n_PO_MEMDEBUG
;
791 case ok_b_POSIXLY_CORRECT
:
792 if(!(n_pstate
& n_PS_ROOT
))
793 n_PS_ROOT_BLOCK(enable
? ok_bset(posix
) : ok_bclear(posix
));
796 if(!(n_pstate
& n_PS_ROOT
))
797 n_PS_ROOT_BLOCK(enable
? ok_bset(POSIXLY_CORRECT
)
798 : ok_bclear(POSIXLY_CORRECT
));
800 case ok_b_skipemptybody
:
803 case ok_b_typescript_mode
:
805 ok_bset(colour_disable
);
806 ok_bset(line_editor_disable
);
807 if(!(n_psonce
& n_PSO_STARTED
))
808 ok_bset(termcap_disable
);
815 n_idec_ui64_cp(&uib
, *val
, 0, NULL
);
816 if(uib
& ~0777u){ /* (is valid _VF_POSNUM) */
817 n_err(_("Invalid *umask* setting: %s\n"), *val
);
824 flag
= (enable
&& !(n_poption
& n_PO_VERB
))
825 ? n_PO_VERB
: n_PO_VERB
| n_PO_VERBVERB
;
828 DBG( n_err("Implementation error: never heard of %u\n", ok
); )
843 a_amv_var_check_nocntrls(char const *val
){
847 while((c
= *val
++) != '\0')
855 a_amv_var_check_num(char const *val
, bool_t posnum
){
856 /* TODO The internal/environment variables which are num= or posnum= should
857 * TODO gain special lookup functions, or the return should be void* and
858 * TODO castable to integer; i.e. no more strtoX() should be needed.
859 * TODO I.e., the result of this function should instead be stored */
865 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
867 enum n_idec_state ids
;
869 ids
= n_idec_cp(&uib
, val
, 0,
870 (posnum
? n_IDEC_MODE_SIGNED_TYPE
: n_IDEC_MODE_NONE
), NULL
);
871 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
872 ) != n_IDEC_STATE_CONSUMED
)
874 /* TODO Unless we store integers we need to look and forbid, because
875 * TODO callee may not be able to swallow, e.g., "-1" */
876 if(posnum
&& (ids
& n_IDEC_STATE_SEEN_MINUS
))
884 a_amv_var_canonify(char const *vn
){
889 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
891 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
898 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
900 struct a_amv_var_map
const *avmp
;
904 /* It may be a special a.k.a. macro-local or one-letter parameter */
906 if(n_UNLIKELY(digitchar(c
))){
907 /* (Inline dec. atoi, ugh) */
908 for(j
= (ui8_t
)c
- '0', i
= 1;; ++i
){
913 goto jno_special_param
;
914 j
= j
* 10 + (ui8_t
)c
- '0';
917 avcp
->avc_is_special
= TRUM1
;
918 goto jspecial_param_m1
;
920 }else if(n_UNLIKELY(name
[1] == '\0')){
929 j
= a_AMV_VST_NOSIGN
;
933 /* Not function local, could also simply look it up, but faster */
934 avcp
->avc_name
= name
;
936 avmp
= &a_amv_var_map
[a_AMV_VAR_QM_MAP_IDX
];
937 avcp
->avc_okey
= ok_v___qm
;
939 avmp
= &a_amv_var_map
[a_AMV_VAR_EM_MAP_IDX
];
940 avcp
->avc_okey
= ok_v___em
;
942 avcp
->avc_hash
= avmp
->avm_hash
;
943 avcp
->avc_map
= avmp
;
944 avcp
->avc_is_special
= FAL0
;
951 /* Normal reverse lookup, walk over the hashtable */
953 avcp
->avc_is_special
= FAL0
;
954 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
955 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
957 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
960 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
963 avmp
= &a_amv_var_map
[x
];
964 if(avmp
->avm_hash
== hash
&&
965 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
966 avcp
->avc_map
= avmp
;
967 avcp
->avc_okey
= (enum okeys
)x
;
971 if(++i
== a_AMV_VAR_REV_PRIME
){
972 #ifdef a_AMV_VAR_REV_WRAPAROUND
979 avcp
->avc_map
= NULL
;
983 return (avcp
!= NULL
);
985 /* All these are mapped to *--special-param* */
987 avcp
->avc_is_special
= TRU1
;
989 avcp
->avc_name
= name
;
990 avcp
->avc_special_prop
= (ui16_t
)j
;
991 avmp
= &a_amv_var_map
[a_AMV_VAR_SPECIAL_PARAM_MAP_IDX
];
992 avcp
->avc_hash
= avmp
->avm_hash
;
993 avcp
->avc_map
= avmp
;
994 avcp
->avc_okey
= ok_v___special_param
;
999 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
1002 struct a_amv_var_map
const *avmp
;
1003 struct a_amv_var
*avp
;
1007 struct a_amv_var
**avpp
, *lavp
;
1009 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
1011 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
1012 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
1013 /* Relink as head, hope it "sorts on usage" over time.
1014 * _clear() relies on this behaviour */
1016 lavp
->av_link
= avp
->av_link
;
1017 avp
->av_link
= *avpp
;
1024 /* If this is not an assembled variable we need to consider some special
1025 * initialization cases and eventually create the variable anew */
1026 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1027 /* Does it have an import-from-environment flag? */
1028 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
1029 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
1030 /* May be better not to use that one, though? */
1031 bool_t isempty
, isbltin
;
1033 isempty
= (*cp
== '\0' &&
1034 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
1035 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
1038 if(n_UNLIKELY(isempty
)){
1041 }else if(n_LIKELY(*cp
!= '\0')){
1042 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
1043 !a_amv_var_check_nocntrls(cp
))){
1044 n_err(_("Ignoring environment, control characters "
1045 "invalid in variable: %s\n"), avcp
->avc_name
);
1048 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1049 !a_amv_var_check_num(cp
, FAL0
))){
1050 n_err(_("Environment variable value not a number "
1051 "or out of range: %s\n"), avcp
->avc_name
);
1054 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1055 !a_amv_var_check_num(cp
, TRU1
))){
1056 n_err(_("Environment variable value not a number, "
1057 "negative or out of range: %s\n"), avcp
->avc_name
);
1066 /* A first-time init switch is to be handled now and here */
1067 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1068 static struct a_amv_var_defval
const **arr
,
1069 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1073 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1075 arr
[i
] = &a_amv_var_i3vals
[i
];
1078 for(i
= 0; arr
[i
] != NULL
; ++i
)
1079 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1080 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1081 : arr
[i
]->avdv_value
;
1082 /* Remove this entry, hope entire block becomes no-op asap */
1084 arr
[i
] = arr
[i
+ 1];
1085 while(arr
[i
++] != NULL
);
1089 if(i3val_nonew
== TRUM1
)
1090 avp
= (struct a_amv_var
*)-1;
1095 /* The virtual variables */
1096 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1097 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1098 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1099 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1105 /* Place this last because once it is set first the variable will never
1106 * be removed again and thus match in the first block above */
1107 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1108 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1109 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1110 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1111 : a_amv_var_defvals
[i
].avdv_value
;
1120 avcp
->avc_var
= avp
;
1122 return (avp
!= NULL
);
1125 struct a_amv_var
**avpp
;
1128 l
= strlen(avcp
->avc_name
) +1;
1130 avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1131 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1133 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1134 avp
->av_value
= a_amv_var_copy(cp
);
1138 avp
->av_flags
= avmp
->avm_flags
;
1140 if(avp
->av_flags
& a_AMV_VF_VIP
)
1141 a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
);
1142 if(avp
->av_flags
& a_AMV_VF_ENV
)
1143 a_amv_var__putenv(avcp
, avp
);
1149 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1151 struct a_amv_var
*avp
;
1153 struct a_amv_var_map
const *avmp
;
1158 rv
= a_amv_var_clear(avcp
, force_env
);
1162 if((avmp
= avcp
->avc_map
) != NULL
){
1165 /* Validity checks */
1166 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1167 !(n_pstate
& n_PS_ROOT
))){
1168 value
= N_("Variable is readonly: %s\n");
1171 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1172 value
= N_("Variable must not be empty: %s\n");
1175 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1176 !a_amv_var_check_nocntrls(value
))){
1177 value
= N_("Variable forbids control characters: %s\n");
1180 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1181 !a_amv_var_check_num(value
, FAL0
))){
1182 value
= N_("Variable value not a number or out of range: %s\n");
1185 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1186 !a_amv_var_check_num(value
, TRU1
))){
1187 value
= _("Variable value not a number, negative, "
1188 "or out of range: %s\n");
1191 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1192 !(n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_ROOT
))){
1193 value
= N_("Variable cannot be set in a resource file: %s\n");
1195 n_err(V_(value
), avcp
->avc_name
);
1199 /* Transformations */
1200 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1203 oval
= savestr(value
);
1205 for(; (c
= *oval
) != '\0'; ++oval
)
1206 *oval
= lowerconv(c
);
1211 a_amv_var_lookup(avcp
, TRU1
);
1213 /* Don't care what happens later on, store this in the unroll list */
1214 if(a_amv_lopts
!= NULL
&&
1215 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1216 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1218 if((avp
= avcp
->avc_var
) == NULL
){
1219 struct a_amv_var
**avpp
;
1222 l
= strlen(avcp
->avc_name
) +1;
1223 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1225 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1230 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1231 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1232 oval
= n_UNCONST(n_empty
);
1234 oval
= avp
->av_value
;
1237 avp
->av_value
= a_amv_var_copy(value
);
1239 /* Via `set' etc. the user may give even boolean options non-boolean
1240 * values, ignore that and force boolean */
1241 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1242 if(!(n_pstate
& n_PS_ROOT
) && (n_poption
& n_PO_D_VV
) &&
1244 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1245 avcp
->avc_name
, value
);
1246 avp
->av_value
= n_UNCONST(n_1
);
1248 avp
->av_value
= a_amv_var_copy(value
);
1250 /* Check if update allowed XXX wasteful on error! */
1251 if((avp
->av_flags
& a_AMV_VF_VIP
) &&
1252 !(rv
= a_amv_var_check_vips(avcp
->avc_okey
, TRU1
, &avp
->av_value
))){
1253 char *cp
= avp
->av_value
;
1255 avp
->av_value
= oval
;
1260 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1261 avp
->av_flags
|= a_AMV_VF_LINKED
;
1262 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1263 rv
= a_amv_var__putenv(avcp
, avp
);
1265 a_amv_var_free(oval
);
1272 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1280 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1282 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1284 if((rv
= (putenv(cp
) == 0))){
1287 if((ocp
= avp
->av_env
) != NULL
)
1298 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1299 struct a_amv_var
**avpp
, *avp
;
1300 struct a_amv_var_map
const *avmp
;
1306 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1307 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1308 !(n_pstate
& n_PS_ROOT
))){
1309 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1312 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1313 !a_amv_var_check_vips(avcp
->avc_okey
, FAL0
, NULL
))
1319 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1322 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1323 }else if(!(n_pstate
& (n_PS_ROOT
| n_PS_ROBOT
)) && (n_poption
& n_PO_D_V
))
1324 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1326 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1327 avcp
->avc_var
= NULL
;
1333 if(a_amv_lopts
!= NULL
&&
1334 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1335 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1337 avp
= avcp
->avc_var
;
1338 avcp
->avc_var
= NULL
;
1339 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1340 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1341 *avpp
= (*avpp
)->av_link
;
1345 char *envval
= NULL
;
1347 char *envval
= avp
->av_env
;
1349 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1350 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1352 a_amv_var_free(avp
->av_value
);
1355 /* XXX Fun part, extremely simple-minded for now: if this variable has
1356 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1357 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1358 a_amv_var_lookup(avcp
, TRU1
);
1365 a_amv_var__clearenv(char const *name
, char *value
){
1367 extern char **environ
;
1379 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1384 while(*ecpp
++ != NULL
);
1394 a_amv_var_show_all(void){
1395 struct n_string msg
, *msgp
;
1398 struct a_amv_var
*avp
;
1399 char const **vacp
, **cap
;
1402 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1403 n_perr(_("Can't create temporary file for `set' listing"), 0);
1407 /* We need to instantiate first-time-inits and default values here, so that
1408 * they will be regular members of our _vars[] table */
1409 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1410 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1411 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1412 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1414 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1415 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1417 no
+= a_AMV_VAR_VIRTS_CNT
;
1419 vacp
= salloc(no
* sizeof(*vacp
));
1421 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1422 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1423 *cap
++ = avp
->av_name
;
1424 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1425 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1428 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1431 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1432 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1433 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1436 page_or_print(fp
, i
);
1443 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1447 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1453 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1454 struct a_amv_var_carrier avc
;
1459 msgp
= n_string_trunc(msgp
, 0);
1462 a_amv_var_revlookup(&avc
, name
);
1463 if(!a_amv_var_lookup(&avc
, FAL0
)){
1466 msgp
= n_string_assign_cp(msgp
, _("No such variable: "));
1467 s
.s
= n_UNCONST(name
);
1469 msgp
= n_shexp_quote(msgp
, &s
, FAL0
);
1473 if(n_poption
& n_PO_D_V
){
1474 if(avc
.avc_map
== NULL
){
1475 msgp
= n_string_push_c(msgp
, '#');
1476 msgp
= n_string_push_cp(msgp
, "assembled");
1484 {a_AMV_VF_VIRT
, "virtual"},
1485 {a_AMV_VF_RDONLY
, "readonly"},
1486 {a_AMV_VF_NODEL
, "nodelete"},
1487 {a_AMV_VF_NOTEMPTY
, "notempty"},
1488 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1489 {a_AMV_VF_NUM
, "number"},
1490 {a_AMV_VF_POSNUM
, "positive-number"},
1491 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1492 {a_AMV_VF_ENV
, "sync-environ"},
1493 {a_AMV_VF_I3VAL
, "initial-value"},
1494 {a_AMV_VF_DEFVAL
, "default-value"},
1495 {a_AMV_VF_LINKED
, "`environ' link"}
1498 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1499 if(avc
.avc_var
->av_flags
& tp
->flags
){
1500 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1501 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1506 msgp
= n_string_push_cp(msgp
, "\n ");
1509 if(avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)
1510 msgp
= n_string_push_cp(msgp
, "# ");
1511 n_UNINIT(quote
, NULL
);
1512 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1513 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1514 if(strcmp(quote
, avc
.avc_var
->av_value
))
1515 msgp
= n_string_push_cp(msgp
, "wysh ");
1517 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1518 msgp
= n_string_push_cp(msgp
, "environ ");
1519 msgp
= n_string_push_cp(msgp
, "set ");
1520 msgp
= n_string_push_cp(msgp
, name
);
1521 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1522 msgp
= n_string_push_c(msgp
, '=');
1523 msgp
= n_string_push_cp(msgp
, quote
);
1527 msgp
= n_string_push_c(msgp
, '\n');
1528 fputs(n_string_cp(msgp
), fp
);
1530 return (i
> 0 ? 2 : 1);
1534 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1535 char *cp
, *cp2
, *varbuf
, c
;
1541 while((cp
= *ap
++) != NULL
){
1543 cp2
= varbuf
= salloc(strlen(cp
) +1);
1545 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1546 if(cntrlchar(c
) || spacechar(c
)){
1547 n_err(_("Variable name with control character ignored: %s\n"),
1556 cp
= n_UNCONST(n_empty
);
1561 n_err(_("Empty variable name ignored\n"));
1564 struct a_amv_var_carrier avc
;
1567 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1568 varbuf
= &varbuf
[2];
1570 a_amv_var_revlookup(&avc
, varbuf
);
1573 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1575 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1590 if((args
= v
)[0] == NULL
){
1591 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1595 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1597 n_err(_("Synopsis: define: <name> {\n"));
1601 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1608 c_undefine(void *v
){
1616 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1617 while(*++args
!= NULL
);
1627 rv
= a_amv_mac_call(v
, FAL0
);
1637 rv
= a_amv_mac_call(v
, TRU1
);
1644 struct a_amv_mac_call_args
*amcap
;
1645 struct a_amv_mac
*amp
;
1647 int rv
, i
, oqf
, nqf
;
1652 if((args
= v
)[0] == NULL
){
1653 rv
= (a_amv_mac_show(a_AMV_MF_ACC
) == FAL0
);
1657 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
1658 if(args
[2] != NULL
){
1659 n_err(_("Synopsis: account: <name> {\n"));
1662 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
1663 n_err(_("`account': cannot use reserved name: %s\n"),
1667 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACC
) == FAL0
);
1671 if(n_pstate
& n_PS_HOOK_MASK
){
1672 n_err(_("`account': can't change account from within a hook\n"));
1676 save_mbox_for_possible_quitstuff();
1679 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
1680 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACC
)) == NULL
) {
1681 n_err(_("`account': account does not exist: %s\n"), args
[0]);
1685 oqf
= savequitflags();
1687 if(a_amv_acc_curr
!= NULL
){
1688 if(a_amv_acc_curr
->am_lopts
!= NULL
)
1689 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
1690 /* For accounts this lingers */
1691 --a_amv_acc_curr
->am_refcnt
;
1692 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DEL
)
1693 a_amv_mac_free(a_amv_acc_curr
);
1696 a_amv_acc_curr
= amp
;
1700 assert(amp
->am_lopts
== NULL
);
1701 amcap
= n_lofi_alloc(sizeof *amcap
);
1702 memset(amcap
, 0, sizeof *amcap
);
1703 amcap
->amca_name
= amp
->am_name
;
1704 amcap
->amca_amp
= amp
;
1705 amcap
->amca_unroller
= &
->am_lopts
;
1706 amcap
->amca_lopts_on
= TRU1
;
1707 ++amp
->am_refcnt
; /* We may not run 0 to avoid being deleted! */
1708 ok
= a_amv_mac_exec(amcap
);
1710 /* XXX account switch incomplete, unroll? */
1711 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
1716 n_PS_ROOT_BLOCK((amp
!= NULL
? ok_vset(_account
, amp
->am_name
)
1717 : ok_vclear(_account
)));
1719 if((n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_HOOK_MASK
)){
1720 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1721 restorequitflags(oqf
);
1722 if((i
= setfile("%", 0)) < 0)
1724 temporary_folder_hook_check(FAL0
);
1725 if(i
> 0 && !ok_blook(emptystart
))
1727 announce(ok_blook(bsdcompat
) || ok_blook(bsdannounce
));
1728 restorequitflags(nqf
);
1737 c_unaccount(void *v
){
1745 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACC
);
1746 while(*++args
!= NULL
);
1752 c_localopts(void *v
){
1759 if(a_amv_lopts
== NULL
){
1760 n_err(_("Cannot use `localopts' in this context "
1761 "(not in `define' or `account', nor special hook)\n"));
1767 if(n_pstate
& (n_PS_HOOK
| n_PS_COMPOSE_MODE
)){
1768 if(n_poption
& n_PO_D_V
)
1769 n_err(_("Cannot turn off `localopts' for compose-mode hooks\n"));
1773 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
1786 if(a_amv_lopts
!= NULL
){
1788 struct a_amv_mac
const *amp
;
1789 struct a_amv_mac_call_args
*amcap
;
1791 amp
= (amcap
= a_amv_lopts
->as_amcap
)->amca_amp
;
1792 if(amp
== NULL
|| amcap
->amca_ps_hook_mask
||
1793 (amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == a_AMV_MF_ACC
)
1802 if((n_idec_si16_cp(&sib
, v
, 10, NULL
1803 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1804 ) != n_IDEC_STATE_CONSUMED
|| sib
< 0){
1805 n_err(_("`shift': invalid argument: %s\n"), v
);
1811 if(i
> (j
= amcap
->amca_argc
)){
1812 n_err(_("`shift': cannot shift %hu of %hu parameters\n"), i
, j
);
1818 amcap
->amca_argc
= j
;
1819 amcap
->amca_argv
+= i
;
1824 n_err(_("Can only use `shift' in a `call'ed macro\n"));
1837 if(a_amv_lopts
!= NULL
){
1838 char const **argv
, *emv
;
1840 n_source_force_eof();
1842 if((argv
= v
)[0] != NULL
){
1845 if((n_idec_si32_cp(&i
, argv
[0], 10, NULL
1846 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1847 ) != n_IDEC_STATE_CONSUMED
|| i
< 0){
1848 n_err(_("`return': argument one is invalid: %s\n"), argv
[0]);
1853 if(argv
[1] != NULL
){
1854 if((n_idec_si32_cp(&i
, argv
[1], 10, NULL
1855 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1856 ) != n_IDEC_STATE_CONSUMED
|| i
< 0){
1857 n_err(_("`return': argument two is invalid: %s\n"), argv
[1]);
1870 n_err(_("Can only use `return' in a macro\n"));
1876 temporary_folder_hook_check(bool_t nmail
){ /* TODO temporary, v15: drop */
1877 struct a_amv_mac_call_args
*amcap
;
1878 struct a_amv_mac
*amp
;
1886 var
= salloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
1888 /* First try the fully resolved path */
1889 snprintf(var
, len
, "folder-hook-%s", mailname
);
1890 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
1893 /* If we are under *folder*, try the usual +NAME syntax, too */
1894 if(displayname
[0] == '+'){
1897 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
1899 snprintf(var
, len
, "folder-hook-+%s", x
);
1900 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
1906 /* Plain *folder-hook* is our last try */
1907 if((cp
= ok_vlook(folder_hook
)) == NULL
)
1911 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
1912 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1913 n_shexp_quote_cp(displayname
, FAL0
), cp
);
1918 amcap
= n_lofi_alloc(sizeof *amcap
);
1919 memset(amcap
, 0, sizeof *amcap
);
1920 amcap
->amca_name
= cp
;
1921 amcap
->amca_amp
= amp
;
1922 n_pstate
&= ~n_PS_HOOK_MASK
;
1924 amcap
->amca_unroller
= NULL
;
1925 n_pstate
|= n_PS_HOOK_NEWMAIL
;
1927 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
1928 n_pstate
|= n_PS_HOOK
;
1930 amcap
->amca_lopts_on
= TRU1
;
1931 amcap
->amca_ps_hook_mask
= TRU1
;
1932 rv
= a_amv_mac_exec(amcap
);
1933 n_pstate
&= ~n_PS_HOOK_MASK
;
1941 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
1943 if(a_amv_folder_hook_lopts
!= NULL
){
1944 void *save
= a_amv_lopts
;
1947 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
1948 a_amv_folder_hook_lopts
= NULL
;
1955 temporary_compose_mode_hook_call(char const *macname
,
1956 void (*hook_pre
)(void *), void *hook_arg
){
1957 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_LEX_SLICE
1958 * TODO comment in lex_input.c for the right way of doing things! */
1959 static struct a_amv_lostack
*cmh_losp
;
1960 struct a_amv_mac_call_args
*amcap
;
1961 struct a_amv_mac
*amp
;
1966 if(macname
== (char*)-1){
1967 a_amv_mac__finalize(cmh_losp
);
1969 }else if(macname
!= NULL
&&
1970 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
1971 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1974 amcap
= n_lofi_alloc(sizeof *amcap
);
1975 memset(amcap
, 0, sizeof *amcap
);
1976 amcap
->amca_name
= (macname
!= NULL
) ? macname
: "on-compose-done-shell";
1977 amcap
->amca_amp
= amp
;
1978 amcap
->amca_unroller
= &a_amv_compose_lopts
;
1979 amcap
->amca_hook_pre
= hook_pre
;
1980 amcap
->amca_hook_arg
= hook_arg
;
1981 amcap
->amca_lopts_on
= TRU1
;
1982 amcap
->amca_ps_hook_mask
= TRU1
;
1983 n_pstate
&= ~n_PS_HOOK_MASK
;
1984 n_pstate
|= n_PS_HOOK
;
1986 a_amv_mac_exec(amcap
);
1988 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
1989 cmh_losp
->as_global_saved
= a_amv_lopts
;
1990 cmh_losp
->as_up
= NULL
;
1991 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
1992 cmh_losp
->as_unroll
= TRU1
;
1993 a_amv_lopts
= cmh_losp
;
2000 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2002 if(a_amv_compose_lopts
!= NULL
){
2003 void *save
= a_amv_lopts
;
2006 a_amv_lopts_unroll(&a_amv_compose_lopts
);
2007 a_amv_compose_lopts
= NULL
;
2014 n_var_is_user_writable(char const *name
){
2015 struct a_amv_var_carrier avc
;
2016 struct a_amv_var_map
const *avmp
;
2020 a_amv_var_revlookup(&avc
, name
);
2021 if((avmp
= avc
.avc_map
) == NULL
)
2024 rv
= ((avmp
->avm_flags
& a_AMV_VF_RDONLY
) == 0);
2030 n_var_oklook(enum okeys okey
){
2031 struct a_amv_var_carrier avc
;
2033 struct a_amv_var_map
const *avmp
;
2036 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2037 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2038 avc
.avc_hash
= avmp
->avm_hash
;
2039 avc
.avc_okey
= okey
;
2041 if(a_amv_var_lookup(&avc
, FAL0
))
2042 rv
= avc
.avc_var
->av_value
;
2050 n_var_okset(enum okeys okey
, uintptr_t val
){
2051 struct a_amv_var_carrier avc
;
2053 struct a_amv_var_map
const *avmp
;
2056 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2057 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2058 avc
.avc_hash
= avmp
->avm_hash
;
2059 avc
.avc_okey
= okey
;
2061 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2067 n_var_okclear(enum okeys okey
){
2068 struct a_amv_var_carrier avc
;
2070 struct a_amv_var_map
const *avmp
;
2073 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2074 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2075 avc
.avc_hash
= avmp
->avm_hash
;
2076 avc
.avc_okey
= okey
;
2078 rv
= a_amv_var_clear(&avc
, FAL0
);
2084 n_var_vlook(char const *vokey
, bool_t try_getenv
){
2085 struct a_amv_var_carrier avc
;
2089 a_amv_var_revlookup(&avc
, vokey
);
2092 /* Here, and only here we need to take care for the special macro-local
2093 * parameters... (except for n_var_vexplode()) */
2094 if(n_LIKELY(!avc
.avc_is_special
)){
2095 if(a_amv_var_lookup(&avc
, FAL0
))
2096 rv
= avc
.avc_var
->av_value
;
2097 /* Only check the environment for something that is otherwise unknown */
2098 else if(try_getenv
&& avc
.avc_map
== NULL
)
2102 struct a_amv_mac_call_args
*amcap
;
2104 /* These may occur only in a macro.. */
2105 if(n_UNLIKELY(a_amv_lopts
== NULL
))
2108 amcap
= a_amv_lopts
->as_amcap
;
2110 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
2111 if((ismacky
= (amcap
->amca_amp
== a_AMV_MACKY_MACK
)) ||
2112 (!amcap
->amca_ps_hook_mask
&&
2113 (assert(amcap
->amca_amp
!= NULL
), TRU1
) &&
2114 (amcap
->amca_amp
->am_flags
& a_AMV_MF_TYPE_MASK
) != a_AMV_MF_ACC
)){
2115 if(avc
.avc_is_special
== TRUM1
){
2116 if(avc
.avc_special_prop
> 0){
2117 if(amcap
->amca_argc
>= avc
.avc_special_prop
)
2118 rv
= amcap
->amca_argv
[avc
.avc_special_prop
- 1];
2120 rv
= amcap
->amca_name
;
2122 rv
= (a_amv_lopts
->as_up
!= NULL
2123 ? a_amv_lopts
->as_up
->as_amcap
->amca_name
: n_empty
);
2125 /* MACKY_MACK doesn't know about [*@#] */
2128 else switch(avc
.avc_special_prop
){
2129 case a_AMV_VST_STAR
:
2133 for(i
= l
= 0; i
< amcap
->amca_argc
; ++i
)
2134 l
+= strlen(amcap
->amca_argv
[i
]) + 1;
2140 rv
= cp
= salloc(l
);
2141 for(i
= l
= 0; i
< amcap
->amca_argc
; ++i
){
2142 l
= strlen(amcap
->amca_argv
[i
]);
2143 memcpy(cp
, amcap
->amca_argv
[i
], l
);
2150 case a_AMV_VST_NOSIGN
:{
2153 rv
= cp
= salloc(sizeof("65535"));
2154 snprintf(cp
, sizeof("65535"), "%hu", amcap
->amca_argc
);
2162 if(n_poption
& n_PO_D_V
)
2163 n_err(_("Cannot use macro local variable in this context: %s\n"),
2164 n_shexp_quote_cp(vokey
, FAL0
));
2172 n_var_vexplode(void const **cookie
){
2174 /* These may occur only in a macro.. */
2175 *cookie
= (a_amv_lopts
!= NULL
) ? a_amv_lopts
->as_amcap
->amca_argv
: NULL
;
2177 return (*cookie
!= NULL
);
2181 n_var_vset(char const *vokey
, uintptr_t val
){
2182 struct a_amv_var_carrier avc
;
2186 a_amv_var_revlookup(&avc
, vokey
);
2188 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2194 n_var_vclear(char const *vokey
){
2195 struct a_amv_var_carrier avc
;
2199 a_amv_var_revlookup(&avc
, vokey
);
2201 ok
= a_amv_var_clear(&avc
, FAL0
);
2208 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
2209 enum okey_xlook_mode oxm
){
2210 struct a_amv_var_carrier avc
;
2211 struct str
const *us
;
2214 struct a_amv_var_map
const *avmp
;
2217 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
2219 /* For simplicity: allow this case too */
2220 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
2225 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2226 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2227 avc
.avc_okey
= okey
;
2229 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
2230 nlen
= strlen(avc
.avc_name
);
2231 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
2232 memcpy(nbuf
, avc
.avc_name
, nlen
);
2235 /* One of .url_u_h_p and .url_h_p we test in here */
2236 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2237 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2238 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2239 if(a_amv_var_lookup(&avc
, FAL0
))
2244 us
= &urlp
->url_h_p
;
2245 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2246 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2247 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2248 if(a_amv_var_lookup(&avc
, FAL0
)){
2250 rv
= avc
.avc_var
->av_value
;
2256 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2263 #endif /* HAVE_SOCKETS */
2271 if(*(ap
= v
) == NULL
){
2272 a_amv_var_show_all();
2275 err
= !a_amv_var_c_set(ap
, FAL0
);
2286 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2287 err
|= !n_var_vclear(*ap
);
2297 if(*(ap
= v
) == NULL
)
2300 struct n_string msg
, *msgp
= &msg
;
2302 msgp
= n_string_creat(msgp
);
2303 for(; *ap
!= NULL
; ++ap
)
2304 a_amv_var_show(*ap
, n_stdout
, msgp
);
2308 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2313 struct a_amv_var_carrier avc
;
2317 sighandler_type sigint
;
2320 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2322 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2323 memset(&avc
, 0, sizeof avc
);
2325 a_amv_var_revlookup(&avc
, *argv
);
2327 if(avc
.avc_map
!= NULL
){
2328 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2329 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2333 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2334 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2340 a_amv_var_lookup(&avc
, FAL0
);
2342 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2344 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2347 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2348 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2349 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2356 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2366 base
= salloc((size_t)l
+1);
2368 for(l
= 0, val
= base
; (c
= getc(nf
)) != EOF
; ++val
)
2369 if(c
== '\n' || c
== '\r'){
2373 *val
= (char)(uc_i
)c
;
2379 if(!a_amv_var_set(&avc
, base
, FAL0
))
2384 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2390 safe_signal(SIGINT
, sigint
);
2397 struct a_amv_var_carrier avc
;
2403 if((islnk
= is_asccaseprefix(*(ap
= v
), "link")) ||
2404 is_asccaseprefix(*ap
, "unlink")){
2405 for(err
= 0; *++ap
!= NULL
;){
2406 a_amv_var_revlookup(&avc
, *ap
);
2408 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2409 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2411 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2413 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2414 if(n_poption
& n_PO_D_V
)
2415 n_err(_("`environ': link: already established: %s\n"), *ap
);
2418 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2419 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2420 a_amv_var__putenv(&avc
, avc
.avc_var
);
2422 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2425 char const *evp
= getenv(*ap
);
2428 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2430 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2436 }else if(is_asccaseprefix(*ap
, "set"))
2437 err
= !a_amv_var_c_set(++ap
, TRU1
);
2438 else if(is_asccaseprefix(*ap
, "unset")){
2439 for(err
= 0; *++ap
!= NULL
;){
2440 a_amv_var_revlookup(&avc
, *ap
);
2442 if(!a_amv_var_clear(&avc
, TRU1
))
2446 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2454 c_vexpr(void *v
){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2455 /* This needs to be here because we need to apply MACKY_MACK for
2456 * search+replace regular expression support :( */
2458 enum n_idec_state ids
;
2460 char op
, varbuf
[64 + 64 / 8 +1];
2461 char const *emv
, **argv
, *varname
, *varres
, *cp
;
2465 a_ISDECIMAL
= 1<<2, /* Print only decimal result */
2468 a_UNSIGNED
= 1<<4, /* Unsigned right shift (share bit ok) */
2476 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2477 n_UNINIT(varres
, n_empty
);
2479 if((cp
= argv
[0])[0] == '\0')
2489 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2492 if(*(cp
= *++argv
) == '\0')
2494 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2495 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2496 ) != n_IDEC_STATE_CONSUMED
){
2497 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2516 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
2521 if(*(cp
= *++argv
) == '\0')
2523 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2524 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2525 ) != n_IDEC_STATE_CONSUMED
){
2526 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2532 if(*(cp
= *++argv
) == '\0')
2534 else if(((ids
= n_idec_si64_cp(&rhv
, cp
, 0, NULL
)
2535 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2536 ) != n_IDEC_STATE_CONSUMED
){
2537 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2549 if(rhv
!= SI64_MIN
){
2560 if(SI64_MAX
- rhv
< lhv
)
2566 if(rhv
!= SI64_MIN
){
2577 if(SI64_MIN
+ rhv
> lhv
){
2579 if(!(f
& a_SATURATED
))
2580 goto jenum_overflow
;
2582 lhv
= (lhv
< 0 || xop
== '-') ? SI64_MIN
: SI64_MAX
;
2587 /* Will the result be positive? */
2588 if((lhv
< 0) == (rhv
< 0)){
2593 if(rhv
!= 0 && lhv
!= 0 && SI64_MAX
/ rhv
> lhv
){
2594 if(!(f
& a_SATURATED
))
2595 goto jenum_overflow
;
2602 if(lhv
!= 0 && SI64_MIN
/ lhv
< rhv
){
2603 if(!(f
& a_SATURATED
))
2604 goto jenum_overflow
;
2610 if(rhv
!= 0 && lhv
!= 0 && SI64_MIN
/ rhv
< lhv
){
2611 if(!(f
& a_SATURATED
))
2612 goto jenum_overflow
;
2622 if(!(f
& a_SATURATED
))
2631 if(!(f
& a_SATURATED
))
2651 if(rhv
> 63){ /* xxx 63? */
2652 if(!(f
& a_SATURATED
))
2653 goto jenum_overflow
;
2658 else if(f
& a_UNSIGNED
)
2659 lhv
= (ui64_t
)lhv
>> (ui8_t
)rhv
;
2669 }else if(cp
[2] == '\0' && cp
[1] == '@'){
2673 }else if(cp
[0] == '<'){
2685 }else if(cp
[0] == '>'){
2701 }else if(is_asccaseprefix(cp
, "length")){
2702 f
|= a_ISNUM
| a_ISDECIMAL
;
2703 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2706 i
= strlen(*++argv
);
2707 if(UICMP(64, i
, >, SI64_MAX
))
2708 goto jestr_overflow
;
2710 }else if(is_asccaseprefix(cp
, "file-expand")){
2711 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2714 if((varres
= fexpand(argv
[1], FEXP_NVAR
)) == NULL
)
2716 }else if(is_asccaseprefix(cp
, "find")){
2717 f
|= a_ISNUM
| a_ISDECIMAL
;
2718 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
2721 if((cp
= strstr(argv
[1], argv
[2])) == NULL
)
2723 i
= PTR2SIZE(cp
- argv
[1]);
2724 if(UICMP(64, i
, >, SI64_MAX
))
2725 goto jestr_overflow
;
2727 }else if(is_asccaseprefix(cp
, "ifind")){
2728 f
|= a_ISNUM
| a_ISDECIMAL
;
2729 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
2732 if((cp
= asccasestr(argv
[1], argv
[2])) == NULL
)
2734 i
= PTR2SIZE(cp
- argv
[1]);
2735 if(UICMP(64, i
, >, SI64_MAX
))
2736 goto jestr_overflow
;
2738 }else if(is_asccaseprefix(cp
, "substring")){
2739 if(argv
[1] == NULL
|| argv
[2] == NULL
)
2741 if(argv
[3] != NULL
&& argv
[4] != NULL
)
2744 i
= strlen(varres
= *++argv
);
2746 if(*(cp
= *++argv
) == '\0')
2748 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
2749 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2750 ) != n_IDEC_STATE_CONSUMED
)
2752 if(UICMP(64, i
, >=, lhv
)){
2756 if(n_poption
& n_PO_D_V
)
2757 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
2758 n_shexp_quote_cp(argv
[-1], FAL0
));
2762 if(argv
[1] != NULL
){
2763 if(*(cp
= *++argv
) == '\0')
2765 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
2766 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2767 ) != n_IDEC_STATE_CONSUMED
)
2769 if(UICMP(64, i
, >=, lhv
)){
2770 if(UICMP(64, i
, !=, lhv
))
2771 varres
= savestrbuf(varres
, (size_t)lhv
);
2773 if(n_poption
& n_PO_D_V
)
2774 n_err(_("`vexpr': substring: length argument too large: %s\n"),
2775 n_shexp_quote_cp(argv
[-2], FAL0
));
2780 }else if(is_asccaseprefix(cp
, "regex")) Jregex
:{
2781 regmatch_t rema
[1 + n_VEXPR_REGEX_MAX
];
2785 f
|= a_ISNUM
| a_ISDECIMAL
;
2786 if(argv
[1] == NULL
|| argv
[2] == NULL
||
2787 (argv
[3] != NULL
&& argv
[4] != NULL
))
2790 reflrv
= REG_EXTENDED
;
2792 reflrv
|= REG_ICASE
;
2793 if((reflrv
= regcomp(&re
, argv
[2], reflrv
))){
2794 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
2795 n_shexp_quote_cp(argv
[2], FAL0
), n_regex_err_to_str(&re
, reflrv
));
2799 reflrv
= regexec(&re
, argv
[1], n_NELEM(rema
), rema
, 0);
2801 if(reflrv
== REG_NOMATCH
)
2804 /* Search only? Else replace, which is a bit */
2805 if(argv
[3] == NULL
){
2806 if(UICMP(64, rema
[0].rm_so
, >, SI64_MAX
))
2807 goto jestr_overflow
;
2808 lhv
= (si64_t
)rema
[0].rm_so
;
2810 /* We need to setup some kind of pseudo macro environment for this */
2811 struct a_amv_lostack los
;
2812 struct a_amv_mac_call_args amca
;
2813 char const **reargv
;
2815 memset(&amca
, 0, sizeof amca
);
2816 amca
.amca_name
= savestrbuf(&argv
[1][rema
[0].rm_so
],
2817 rema
[0].rm_eo
- rema
[0].rm_so
);
2818 amca
.amca_amp
= a_AMV_MACKY_MACK
;
2819 for(i
= 1; rema
[i
].rm_so
!= -1 && i
< n_NELEM(rema
); ++i
)
2821 amca
.amca_argc
= (ui32_t
)i
- 1;
2823 reargv
= salloc(sizeof(char*) * i
);
2824 for(i
= 1; rema
[i
].rm_so
!= -1 && i
< n_NELEM(rema
); ++i
)
2825 *reargv
++ = savestrbuf(&argv
[1][rema
[i
].rm_so
],
2826 rema
[i
].rm_eo
- rema
[i
].rm_so
);
2829 hold_all_sigs(); /* TODO DISLIKE! */
2830 los
.as_global_saved
= a_amv_lopts
;
2831 los
.as_amcap
= &amca
;
2832 los
.as_up
= los
.as_global_saved
;
2833 los
.as_lopts
= NULL
;
2834 los
.as_unroll
= FAL0
;
2839 struct n_string s_b
;
2840 enum n_shexp_state shs
;
2842 templ
.s
= n_UNCONST(argv
[3]);
2844 shs
= n_shexp_parse_token(n_string_creat_auto(&s_b
), &templ
, NULL
,
2845 (n_SHEXP_PARSE_LOG
| n_SHEXP_PARSE_IGNORE_EMPTY
|
2846 n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
2847 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
));
2848 if((shs
& (n_SHEXP_STATE_ERR_MASK
| n_SHEXP_STATE_STOP
)
2849 ) == n_SHEXP_STATE_STOP
){
2850 varres
= n_string_cp(&s_b
);
2851 n_string_drop_ownership(&s_b
);
2856 a_amv_lopts
= los
.as_global_saved
;
2857 rele_all_sigs(); /* TODO DISLIKE! */
2861 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
2863 }else if(is_asccaseprefix(argv
[0], "iregex")){
2866 #endif /* HAVE_REGEX */
2872 /* Generate the variable value content for numerics.
2873 * Anticipate in our handling below! (Don't do needless work) */
2875 if((f
& a_ISNUM
) && ((f
& a_ISDECIMAL
) || varname
!= NULL
)){
2876 snprintf(varbuf
, sizeof varbuf
, "%" PRId64
, lhv
);
2880 if(varname
== NULL
){
2881 /* If there was no error and we are printing a numeric result, print some
2882 * more bases for the fun of it */
2883 if((f
& (a_ERR
| a_ISNUM
| a_ISDECIMAL
)) == a_ISNUM
){
2886 for(j
= 1, i
= 0; i
< 64; ++i
){
2887 varbuf
[63 + 64 / 8 -j
- i
] = (lhv
& ((ui64_t
)1 << i
)) ? '1' : '0';
2888 if((i
& 7) == 7 && i
!= 63){
2890 varbuf
[63 + 64 / 8 -j
- i
] = ' ';
2893 varbuf
[64 + 64 / 8 -1] = '\0';
2894 fprintf(n_stdout
, "%s\n0%" PRIo64
" | 0x%" PRIX64
" | %" PRId64
"\n",
2895 varbuf
, lhv
, lhv
, lhv
);
2897 fprintf(n_stdout
, "%s\n", varres
);
2898 }else if(!n_var_vset(varname
, (uintptr_t)varres
)){
2904 return (f
& a_ERR
) ? 1 : 0;
2909 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
2916 n_err(_("`vexpr': invalid subcommand: %s\n"),
2917 n_shexp_quote_cp(*argv
, FAL0
));
2921 n_err(_("Synopsis: vexpr: <target-variable> <operator> <:argument:>\n"));
2925 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
2926 n_shexp_quote_cp(*argv
, FAL0
));
2930 n_err(_("`vexpr': expression overflows datatype: %" PRId64
" %c %" PRId64
2931 "\n"), lhv
, op
, rhv
);
2935 n_err(_("`vexpr': string length or offset overflows datatype\n"));