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 make-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 go.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_GO_SPLICE comment in go.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 /* Positional parameter maximum (macro arguments, `vexpr' "splitifs") */
58 #define a_AMV_POSPAR_MAX SI16_MAX
60 /* Special "pseudo macro" that stabs you from the back */
61 #define a_AMV_MACKY_MACK ((struct a_amv_mac*)-1)
63 /* Note: changing the hash function must be reflected in `vexpr' "hash",
64 * because that is used by the hashtable creator scripts! */
65 #define a_AMV_PRIME HSHSIZE
66 #define a_AMV_NAME2HASH(N) n_torek_hash(N)
67 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
71 a_AMV_MF_ACCOUNT
= 1u<<0, /* This macro is an `account' */
72 a_AMV_MF_TYPE_MASK
= a_AMV_MF_ACCOUNT
,
73 a_AMV_MF_UNDEF
= 1u<<1, /* Unlink after lookup */
74 a_AMV_MF_DELETE
= 1u<<7, /* Delete in progress, free once refcnt==0 */
80 a_AMV_LF_SCOPE
= 1u<<0, /* Current scope `localopts' on */
81 a_AMV_LF_SCOPE_FIXATE
= 1u<<1, /* Ditto, but fixated */
82 a_AMV_LF_SCOPE_MASK
= a_AMV_LF_SCOPE
| a_AMV_LF_SCOPE_FIXATE
,
83 a_AMV_LF_CALL
= 1u<<2, /* `localopts' on for `call'ed scopes */
84 a_AMV_LF_CALL_FIXATE
= 1u<<3, /* Ditto, but fixated */
85 a_AMV_LF_CALL_MASK
= a_AMV_LF_CALL
| a_AMV_LF_CALL_FIXATE
,
86 a_AMV_LF_CALL_TO_SCOPE_SHIFT
= 2
89 /* make-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
90 * _IMPORT implies _ENV; it doesn't verify anything... */
93 a_AMV_VF_BOOL
= 1u<<0, /* ok_b_* */
94 a_AMV_VF_VIRT
= 1u<<1, /* "Stateless" automatic variable */
95 a_AMV_VF_NOLOPTS
= 1u<<2, /* May not be tracked by `localopts' */
96 a_AMV_VF_RDONLY
= 1u<<3, /* May not be set by user */
97 a_AMV_VF_NODEL
= 1u<<4, /* May not be deleted */
98 a_AMV_VF_NOTEMPTY
= 1u<<5, /* May not be assigned an empty value */
99 a_AMV_VF_NOCNTRLS
= 1u<<6, /* Value may not contain control characters */
100 a_AMV_VF_NUM
= 1u<<7, /* Value must be a 32-bit number */
101 a_AMV_VF_POSNUM
= 1u<<8, /* Value must be positive 32-bit number */
102 a_AMV_VF_LOWER
= 1u<<9, /* Values will be stored in a lowercase version */
103 a_AMV_VF_VIP
= 1u<<10, /* Wants _var_check_vips() evaluation */
104 a_AMV_VF_IMPORT
= 1u<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
105 a_AMV_VF_ENV
= 1u<<12, /* Update environment on change */
106 a_AMV_VF_I3VAL
= 1u<<13, /* Has an initial value */
107 a_AMV_VF_DEFVAL
= 1u<<14, /* Has a default value */
108 a_AMV_VF_LINKED
= 1u<<15, /* `environ' linked */
109 a_AMV_VF__MASK
= (1u<<(15+1)) - 1
112 /* We support some special parameter names for one(+)-letter variable names;
113 * note these have counterparts in the code that manages shell expansion!
114 * All these special variables are solely backed by n_var_vlook(), and besides
115 * there is only a_amv_var_revlookup() which knows about them */
116 enum a_amv_var_special_category
{
117 a_AMV_VSC_NONE
, /* Normal variable, no special treatment */
118 a_AMV_VSC_GLOBAL
, /* ${[?!]} are specially mapped, but global */
119 a_AMV_VSC_MULTIPLEX
, /* ${^.*} circumflex accent multiplexer */
120 a_AMV_VSC_POSPAR
, /* ${[1-9][0-9]*} positional parameters */
121 a_AMV_VSC_POSPAR_ENV
/* ${[*@#]} positional parameter support variables */
124 enum a_amv_var_special_type
{
126 a_AMV_VST_QM
, /* ? */
127 a_AMV_VST_EM
, /* ! */
129 /* This is special in that it is a multiplex indicator, the ^ is followed by
130 * a normal variable */
131 a_AMV_VST_CACC
, /* ^ (circumflex accent) */
132 /* _VSC_POSPAR_ENV */
133 a_AMV_VST_STAR
, /* * */
134 a_AMV_VST_AT
, /* @ */
135 a_AMV_VST_NOSIGN
/* # */
138 enum a_amv_var_vip_mode
{
145 ui16_t app_maxcount
; /* == slots in .app_dat */
146 ui16_t app_count
; /* Maximum a_AMV_POSPAR_MAX */
147 ui16_t app_idx
; /* `shift' moves this one, decs .app_count */
148 bool_t app_not_heap
; /* .app_dat stuff not on dynamically allocated */
150 char const **app_dat
; /* NULL terminated (for "$@" explosion support) */
152 n_CTA(a_AMV_POSPAR_MAX
<= SI16_MAX
, "Limit exceeds datatype capabilities");
155 struct a_amv_mac
*am_next
;
156 ui32_t am_maxlen
; /* of any line in .am_line_dat */
157 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
158 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
159 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
160 ui32_t am_refcnt
; /* 0-based for `un{account,define}' purposes */
161 ui8_t am_flags
; /* enum a_amv_mac_flags */
162 char am_name
[n_VFIELD_SIZE(3)]; /* of this macro */
164 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
166 struct a_amv_mac_line
{
168 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
169 char aml_dat
[n_VFIELD_SIZE(0)];
172 struct a_amv_mac_call_args
{
173 char const *amca_name
; /* For MACKY_MACK, this is *0*! */
174 struct a_amv_mac
*amca_amp
; /* "const", but for am_refcnt */
175 struct a_amv_var
**amca_unroller
;
176 void (*amca_hook_pre
)(void *);
179 bool_t amca_ps_hook_mask
;
180 bool_t amca_no_xcall
; /* We want n_GO_INPUT_NO_XCALL for this */
182 struct a_amv_pospar amca_pospar
;
185 struct a_amv_lostack
{
186 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
187 struct a_amv_mac_call_args
*as_amcap
;
188 struct a_amv_lostack
*as_up
; /* Outer context */
189 struct a_amv_var
*as_lopts
;
190 ui8_t as_loflags
; /* enum a_amv_mac_loflags */
195 struct a_amv_var
*av_link
;
198 char *av_env
; /* Actively managed putenv(3) memory */
200 ui16_t av_flags
; /* enum a_amv_var_flags */
201 char av_name
[n_VFIELD_SIZE(6)];
203 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
205 struct a_amv_var_map
{
208 ui16_t avm_flags
; /* enum a_amv_var_flags */
210 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
212 struct a_amv_var_virt
{
215 struct a_amv_var
const *avv_var
;
218 struct a_amv_var_defval
{
221 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
224 struct a_amv_var_carrier
{
225 char const *avc_name
;
228 struct a_amv_var
*avc_var
;
229 struct a_amv_var_map
const *avc_map
;
232 ui8_t avc_special_cat
;
233 /* Numerical parameter name if .avc_special_cat=a_AMV_VSC_POSPAR,
234 * otherwise a enum a_amv_var_special_type */
235 ui16_t avc_special_prop
;
238 /* Include constant make-okey-map.pl output, and the generated version data */
239 #include "gen-version.h"
240 #include "gen-okeys.h"
242 /* The currently active account */
243 static struct a_amv_mac
*a_amv_acc_curr
;
245 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
247 /* Unroll list of currently running macro stack */
248 static struct a_amv_lostack
*a_amv_lopts
;
250 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
252 /* Global (i.e., non-local) a_AMV_VSC_POSPAR stack */
253 static struct a_amv_pospar a_amv_pospar
;
255 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
256 * TODO today via a static lostack, it should be a field in mailbox, once that
257 * TODO is a real multi-instance object */
258 static struct a_amv_var
*a_amv_folder_hook_lopts
;
260 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
261 static struct a_amv_var
*a_amv_compose_lopts
;
263 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
264 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
265 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
266 * plain lookup cases or when _UNDEF was performed on a currently active entry
267 * (the entry will have been unlinked, and the _MF_DELETE will be honoured once
268 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
269 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
270 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
272 /* `call', `call_if' (and `xcall' via go.c -> c_call()) */
273 static int a_amv_mac_call(void *v
, bool_t silent_nexist
);
275 /* Execute a macro; amcap must reside in LOFI memory */
276 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
278 static void a_amv_mac__finalize(void *vp
);
280 /* User display helpers */
281 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
283 /* _def() returns error for faulty definitions and already existing * names,
284 * _undef() returns error if a named thing doesn't exist */
285 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
286 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
289 static void a_amv_mac_free(struct a_amv_mac
*amp
);
291 /* Update replay-log */
292 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
293 struct a_amv_var
*oavp
);
294 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
296 /* Special cased value string allocation */
297 static char *a_amv_var_copy(char const *str
);
298 static void a_amv_var_free(char *cp
);
300 /* Check for special housekeeping. _VIP_SET_POST and _VIP_CLEAR do not fail
301 * (or propagate errors), _VIP_SET_PRE may and should case abortion */
302 static bool_t
a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
,
303 enum okeys okey
, char const *val
);
305 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
306 static bool_t
a_amv_var_check_nocntrls(char const *val
);
307 static bool_t
a_amv_var_check_num(char const *val
, bool_t posnum
);
309 /* If a variable name begins with a lowercase-character and contains at
310 * least one '@', it is converted to all-lowercase. This is necessary
311 * for lookups of names based on email addresses.
312 * Following the standard, only the part following the last '@' should
313 * be lower-cased, but practice has established otherwise here */
314 static char const *a_amv_var_canonify(char const *vn
);
316 /* Try to reverse lookup an option name to an enum okeys mapping.
317 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
318 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
321 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
322 * Sets .avc_prime; .avc_var is NULL if not found.
323 * Here it is where we care for _I3VAL and _DEFVAL, too.
324 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
325 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
326 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
327 * return FAL0, then! */
328 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
331 /* Lookup functions for special category variables, _pospar drives all
332 * positional parameter etc. special categories */
333 static char const *a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
);
334 static char const *a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
);
335 static char const *a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
);
337 /* Set var from .avc_(map|name|hash), return success */
338 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
341 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
342 struct a_amv_var
*avp
);
344 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
345 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
347 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
349 /* List all variables */
350 static void a_amv_var_show_all(void);
352 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
354 /* Actually do print one, return number of lines written */
355 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
357 /* Shared c_set() and c_environ():set impl, return success */
358 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
360 static struct a_amv_mac
*
361 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
362 enum a_amv_mac_flags amf
){
363 struct a_amv_mac
*amp
, **ampp
;
365 enum a_amv_mac_flags save_amf
;
369 amf
&= a_AMV_MF_TYPE_MASK
;
370 h
= a_AMV_NAME2HASH(name
);
371 h
= a_AMV_HASH2PRIME(h
);
372 ampp
= &a_amv_macs
[h
];
374 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
375 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
376 !strcmp(amp
->am_name
, name
)){
377 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
380 *ampp
= amp
->am_next
;
382 if(amp
->am_refcnt
> 0){
383 amp
->am_flags
|= a_AMV_MF_DELETE
;
384 if(n_poption
& n_PO_D_V
)
385 n_err(_("Delayed deletion of currently active %s: %s\n"),
386 (amp
->am_flags
& a_AMV_MF_ACCOUNT
? "account" : "define"),
390 amp
= (struct a_amv_mac
*)-1;
397 ampp
= &a_amv_macs
[h
];
398 newamp
->am_next
= *ampp
;
408 a_amv_mac_call(void *v
, bool_t silent_nexist
){
409 struct a_amv_mac
*amp
;
412 struct n_cmd_arg_ctx
*cacp
;
417 if(cacp
->cac_no
== 0){
418 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
419 n_pstate_err_no
= n_ERR_INVAL
;
424 name
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
426 if(n_UNLIKELY(cacp
->cac_no
> a_AMV_POSPAR_MAX
)){
427 n_err(_("Too many arguments to macro `call': %s\n"), name
);
428 n_pstate_err_no
= n_ERR_OVERFLOW
;
430 }else if(n_UNLIKELY((amp
= a_amv_mac_lookup(name
, NULL
, a_AMV_MF_NONE
)
433 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name
, FAL0
));
434 n_pstate_err_no
= n_ERR_NOENT
;
438 struct a_amv_mac_call_args
*amcap
;
441 argc
= cacp
->cac_no
+ 1;
442 amcap
= n_lofi_alloc(sizeof *amcap
+ (argc
* sizeof *argv
));
443 argv
= (void*)&amcap
[1];
445 for(argc
= 0; (cacp
->cac_arg
= cacp
->cac_arg
->ca_next
) != NULL
; ++argc
)
446 argv
[argc
] = cacp
->cac_arg
->ca_arg
.ca_str
.s
;
449 memset(amcap
, 0, sizeof *amcap
);
450 amcap
->amca_name
= name
;
451 amcap
->amca_amp
= amp
;
452 if(a_amv_lopts
!= NULL
)
453 amcap
->amca_loflags
= (a_amv_lopts
->as_loflags
& a_AMV_LF_CALL_MASK
454 ) >> a_AMV_LF_CALL_TO_SCOPE_SHIFT
;
456 amcap
->amca_pospar
.app_count
= (ui16_t
)argc
;
457 amcap
->amca_pospar
.app_not_heap
= TRU1
;
458 amcap
->amca_pospar
.app_dat
= argv
;
461 (void)a_amv_mac_exec(amcap
);
470 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
471 struct a_amv_lostack
*losp
;
472 struct a_amv_mac_line
**amlp
;
473 char **args_base
, **args
;
474 struct a_amv_mac
*amp
;
478 amp
= amcap
->amca_amp
;
479 assert(amp
!= NULL
&& amp
!= a_AMV_MACKY_MACK
);
481 /* XXX Unfortunately we yet need to dup the macro lines! :( */
482 args_base
= args
= n_alloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
483 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
484 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
487 losp
= n_lofi_alloc(sizeof *losp
);
488 losp
->as_global_saved
= a_amv_lopts
;
489 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
490 losp
->as_up
= losp
->as_global_saved
;
491 losp
->as_lopts
= NULL
;
494 losp
->as_lopts
= *amcap
->amca_unroller
;
496 losp
->as_loflags
= amcap
->amca_loflags
;
499 if(amcap
->amca_hook_pre
!= NULL
)
500 n_PS_ROOT_BLOCK((*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
));
501 rv
= n_go_macro((n_GO_INPUT_NONE
|
502 (amcap
->amca_no_xcall
? n_GO_INPUT_NO_XCALL
: 0)),
503 amp
->am_name
, args_base
, &a_amv_mac__finalize
, losp
);
509 a_amv_mac__finalize(void *vp
){
510 struct a_amv_mac
*amp
;
511 struct a_amv_mac_call_args
*amcap
;
512 struct a_amv_lostack
*losp
;
516 a_amv_lopts
= losp
->as_global_saved
;
518 amcap
= losp
->as_amcap
;
520 if(!amcap
->amca_pospar
.app_not_heap
&& amcap
->amca_pospar
.app_maxcount
> 0){
523 for(i
= amcap
->amca_pospar
.app_maxcount
; i
-- != 0;)
524 n_free(n_UNCONST(amcap
->amca_pospar
.app_dat
[i
]));
525 n_free(amcap
->amca_pospar
.app_dat
);
528 if(amcap
->amca_unroller
== NULL
){
529 if(losp
->as_lopts
!= NULL
)
530 a_amv_lopts_unroll(&losp
->as_lopts
);
532 *amcap
->amca_unroller
= losp
->as_lopts
;
534 if(amcap
->amca_ps_hook_mask
)
535 n_pstate
&= ~n_PS_HOOK_MASK
;
537 if((amp
= amcap
->amca_amp
) != a_AMV_MACKY_MACK
&& amp
!= NULL
&&
538 --amp
->am_refcnt
== 0 && (amp
->am_flags
& a_AMV_MF_DELETE
))
547 a_amv_mac_show(enum a_amv_mac_flags amf
){
548 size_t lc
, mc
, ti
, i
;
556 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
558 n_perr(_("Can't create temporary file for `define' or `account' listing"),
563 amf
&= a_AMV_MF_TYPE_MASK
;
564 typestr
= (amf
& a_AMV_MF_ACCOUNT
) ? "account" : "define";
566 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
567 struct a_amv_mac
*amp
;
569 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
570 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
571 struct a_amv_mac_line
**amlpp
;
578 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
579 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
580 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
582 fputs((*amlpp
)->aml_dat
, fp
);
591 page_or_print(fp
, lc
);
593 rv
= (ferror(fp
) == 0);
601 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
603 ui32_t line_cnt
, maxlen
;
605 struct linelist
*ll_next
;
606 struct a_amv_mac_line
*ll_amlp
;
607 } *llp
, *ll_head
, *ll_tail
;
608 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
609 struct a_amv_mac
*amp
;
613 memset(&line
, 0, sizeof line
);
617 /* TODO We should have our input state machine which emits Line events,
618 * TODO and hook different consumers dependent on our content, as stated
619 * TODO in i think lex_input; */
620 /* Read in the lines which form the macro content */
621 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
625 n
.i
= n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, n_empty
,
626 &line
.s
, &line
.l
, NULL
, NULL
);
630 n_err(_("Unterminated %s definition: %s\n"),
631 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
635 /* Trim WS, remember amount of leading spaces for display purposes */
636 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
638 leaspc
= (leaspc
+ 8u) & ~7u;
643 for(; n
.ui
> 0 && spacechar(cp
[n
.ui
- 1]); --n
.ui
)
648 maxlen
= n_MAX(maxlen
, n
.ui
);
651 /* Is is the closing brace? */
655 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
656 struct a_amv_mac_line
*amlp
;
658 llp
= n_autorec_alloc(sizeof *llp
);
662 ll_tail
->ll_next
= llp
;
665 llp
->ll_amlp
= amlp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
667 amlp
->aml_len
= n
.ui
-1;
668 amlp
->aml_prespc
= leaspc
;
669 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
671 n_err(_("Too much content in %s definition: %s\n"),
672 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
677 /* Create the new macro */
678 n
.s
= strlen(name
) +1;
679 amp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
680 memset(amp
, 0, n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
));
681 amp
->am_maxlen
= maxlen
;
682 amp
->am_line_cnt
= line_cnt
;
684 memcpy(amp
->am_name
, name
, n
.s
);
686 struct a_amv_mac_line
**amlpp
;
688 amp
->am_line_dat
= amlpp
= n_alloc(sizeof(*amlpp
) * ++line_cnt
);
689 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
690 *amlpp
++ = llp
->ll_amlp
;
694 /* Create entry, replace a yet existing one as necessary */
695 a_amv_mac_lookup(name
, amp
, amf
| a_AMV_MF_UNDEF
);
704 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
708 * free(amp->am_line_dat);
715 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
716 struct a_amv_mac
*amp
;
722 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
723 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
724 n_err(_("%s not defined: %s\n"),
725 (amf
& a_AMV_MF_ACCOUNT
? "Account" : "Macro"), name
);
729 struct a_amv_mac
**ampp
, *lamp
;
731 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
733 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
734 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
735 /* xxx Expensive but rare: be simple */
736 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
737 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
749 a_amv_mac_free(struct a_amv_mac
*amp
){
750 struct a_amv_mac_line
**amlpp
;
753 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
755 free(amp
->am_line_dat
);
761 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
762 struct a_amv_var
*oavp
){
763 struct a_amv_var
*avp
;
767 /* Propagate unrolling up the stack, as necessary */
770 if(alp
->as_loflags
& a_AMV_LF_SCOPE_MASK
)
772 if((alp
= alp
->as_up
) == NULL
)
776 /* Check whether this variable is handled yet XXX Boost: ID check etc.!! */
777 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
778 if(!strcmp(avp
->av_name
, name
))
781 nl
= strlen(name
) +1;
782 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
783 avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
784 avp
->av_link
= alp
->as_lopts
;
786 memcpy(avp
->av_name
, name
, nl
);
788 avp
->av_value
= NULL
;
794 avp
->av_value
= &avp
->av_name
[nl
];
795 avp
->av_flags
= oavp
->av_flags
;
796 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
798 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
806 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
807 struct a_amv_lostack
*save_alp
;
808 struct a_amv_var
*x
, *avp
;
814 save_alp
= a_amv_lopts
;
819 n_PS_ROOT_BLOCK(n_var_vset(x
->av_name
, (uintptr_t)x
->av_value
));
822 a_amv_lopts
= save_alp
;
827 a_amv_var_copy(char const *str
){
833 news
= n_UNCONST(n_empty
);
834 else if(str
[1] == '\0'){
836 news
= n_UNCONST(n_1
);
837 else if(str
[0] == '0')
838 news
= n_UNCONST(n_0
);
841 }else if(str
[2] == '\0' && str
[0] == '-' && str
[1] == '1')
842 news
= n_UNCONST(n_m1
);
845 len
= strlen(str
) +1;
847 memcpy(news
, str
, len
);
854 a_amv_var_free(char *cp
){
856 if(cp
[0] != '\0' && cp
!= n_0
&& cp
!= n_1
&& cp
!= n_m1
)
862 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
, enum okeys okey
,
869 if(avvm
== a_AMV_VIP_SET_PRE
){
874 /* Note this gets called from main.c during initialization, and they
875 * simply set this to pw_dir as a fallback: don't verify _that_ call.
877 if(!(n_pstate
& n_PS_ROOT
) && !n_is_dir(val
, TRUM1
)){
878 n_err(_("$HOME is not a directory or not accessible: %s\n"),
879 n_shexp_quote_cp(val
, FAL0
));
884 if(!n_is_dir(val
, TRU1
)){
885 n_err(_("$TMPDIR is not a directory or not accessible: %s\n"),
886 n_shexp_quote_cp(val
, FAL0
));
894 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
895 if(uib
& ~0777u){ /* (is valid _VF_POSNUM) */
896 n_err(_("Invalid *umask* setting: %s\n"), val
);
902 }else if(avvm
== a_AMV_VIP_SET_POST
){
907 n_poption
|= n_PO_DEBUG
;
910 /* Invalidate any resolved folder then, too
913 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
918 x_b
= x
= n_autorec_alloc(strlen(val
) +1);
919 while((c
= *val
++) != '\0')
923 n_PS_ROOT_BLOCK(ok_vset(ifs_ws
, x_b
));
925 #ifdef HAVE_SETLOCALE
933 n_poption
|= n_PO_MEMDEBUG
;
935 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
936 if(!(n_pstate
& n_PS_ROOT
))
937 n_PS_ROOT_BLOCK(ok_bset(posix
));
939 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
940 if(!(n_pstate
& n_PS_ROOT
))
941 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT
));
943 case ok_b_skipemptybody
:
944 n_poption
|= n_PO_E_FLAG
;
946 case ok_b_typescript_mode
:
947 ok_bset(colour_disable
);
948 ok_bset(line_editor_disable
);
949 if(!(n_psonce
& n_PSO_STARTED
))
950 ok_bset(termcap_disable
);
956 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
961 n_poption
|= (n_poption
& n_PO_VERB
) ? n_PO_VERBVERB
: n_PO_VERB
;
969 n_poption
&= ~n_PO_DEBUG
;
972 /* Invalidate any resolved folder then, too
975 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
978 n_poption
&= ~n_PO_MEMDEBUG
;
980 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
981 if(!(n_pstate
& n_PS_ROOT
))
982 n_PS_ROOT_BLOCK(ok_bclear(posix
));
984 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
985 if(!(n_pstate
& n_PS_ROOT
))
986 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT
));
988 case ok_b_skipemptybody
:
989 n_poption
&= ~n_PO_E_FLAG
;
992 n_poption
&= ~(n_PO_VERB
| n_PO_VERBVERB
);
1001 a_amv_var_check_nocntrls(char const *val
){
1005 while((c
= *val
++) != '\0')
1013 a_amv_var_check_num(char const *val
, bool_t posnum
){
1014 /* TODO The internal/environment variables which are num= or posnum= should
1015 * TODO gain special lookup functions, or the return should be void* and
1016 * TODO castable to integer; i.e. no more strtoX() should be needed.
1017 * TODO I.e., the result of this function should instead be stored */
1023 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1025 enum n_idec_state ids
;
1027 ids
= n_idec_cp(&uib
, val
, 0,
1028 (posnum
? n_IDEC_MODE_SIGNED_TYPE
: n_IDEC_MODE_NONE
), NULL
);
1029 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1030 ) != n_IDEC_STATE_CONSUMED
)
1032 /* TODO Unless we store integers we need to look and forbid, because
1033 * TODO callee may not be able to swallow, e.g., "-1" */
1034 if(posnum
&& (ids
& n_IDEC_STATE_SEEN_MINUS
))
1042 a_amv_var_canonify(char const *vn
){
1044 if(!upperchar(*vn
)){
1047 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
1049 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
1056 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
1058 struct a_amv_var_map
const *avmp
;
1062 /* It may be a special a.k.a. macro-local or one-letter parameter */
1064 if(n_UNLIKELY(digitchar(c
))){
1065 /* (Inline dec. atoi, ugh) */
1066 for(j
= (ui8_t
)c
- '0', i
= 1;; ++i
){
1071 goto jno_special_param
;
1072 j
= j
* 10 + (ui8_t
)c
- '0';
1074 if(j
<= a_AMV_POSPAR_MAX
){
1075 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR
;
1076 goto jspecial_param
;
1078 }else if(n_UNLIKELY(name
[1] == '\0')){
1082 avcp
->avc_special_cat
= a_AMV_VSC_GLOBAL
;
1083 j
= (c
== '?') ? a_AMV_VST_QM
: a_AMV_VST_EM
;
1084 goto jspecial_param
;
1088 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1090 goto jspecial_param
;
1092 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1094 goto jspecial_param
;
1096 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1097 j
= a_AMV_VST_NOSIGN
;
1098 goto jspecial_param
;
1104 avcp
->avc_special_cat
= a_AMV_VSC_MULTIPLEX
;
1106 goto jspecial_param
;
1109 /* Normal reverse lookup, walk over the hashtable */
1111 avcp
->avc_special_cat
= a_AMV_VSC_NONE
;
1112 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
1113 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
1115 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
1118 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
1121 avmp
= &a_amv_var_map
[x
];
1122 if(avmp
->avm_hash
== hash
&&
1123 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
1124 avcp
->avc_map
= avmp
;
1125 avcp
->avc_okey
= (enum okeys
)x
;
1129 if(++i
== a_AMV_VAR_REV_PRIME
){
1130 #ifdef a_AMV_VAR_REV_WRAPAROUND
1137 avcp
->avc_map
= NULL
;
1141 return (avcp
!= NULL
);
1143 /* All these are mapped to *--special-param* */
1145 avcp
->avc_name
= name
;
1146 avcp
->avc_special_prop
= (ui16_t
)j
;
1147 avmp
= &a_amv_var_map
[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX
];
1148 avcp
->avc_hash
= avmp
->avm_hash
;
1149 avcp
->avc_map
= avmp
;
1150 avcp
->avc_okey
= ok_v___special_param
;
1155 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
1158 struct a_amv_var_map
const *avmp
;
1159 struct a_amv_var
*avp
;
1163 struct a_amv_var
**avpp
, *lavp
;
1165 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
1167 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
1168 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
1169 /* Relink as head, hope it "sorts on usage" over time.
1170 * _clear() relies on this behaviour */
1172 lavp
->av_link
= avp
->av_link
;
1173 avp
->av_link
= *avpp
;
1180 /* If this is not an assembled variable we need to consider some special
1181 * initialization cases and eventually create the variable anew */
1182 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1183 /* Does it have an import-from-environment flag? */
1184 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
1185 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
1186 /* May be better not to use that one, though? */
1187 /* TODO Outsource the tests into a _shared_ test function! */
1188 bool_t isempty
, isbltin
;
1190 isempty
= (*cp
== '\0' &&
1191 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
1192 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
1195 if(n_UNLIKELY(isempty
)){
1196 n_err(_("Environment variable must not be empty: %s\n"),
1200 }else if(n_LIKELY(*cp
!= '\0')){
1201 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
1202 !a_amv_var_check_nocntrls(cp
))){
1203 n_err(_("Ignoring environment, control characters "
1204 "invalid in variable: %s\n"), avcp
->avc_name
);
1207 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1208 !a_amv_var_check_num(cp
, FAL0
))){
1209 n_err(_("Environment variable value not a number "
1210 "or out of range: %s\n"), avcp
->avc_name
);
1213 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1214 !a_amv_var_check_num(cp
, TRU1
))){
1215 n_err(_("Environment variable value not a number, "
1216 "negative or out of range: %s\n"), avcp
->avc_name
);
1225 /* A first-time init switch is to be handled now and here */
1226 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1227 static struct a_amv_var_defval
const **arr
,
1228 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1232 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1234 arr
[i
] = &a_amv_var_i3vals
[i
];
1237 for(i
= 0; arr
[i
] != NULL
; ++i
)
1238 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1239 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1240 : arr
[i
]->avdv_value
;
1241 /* Remove this entry, hope entire block becomes no-op asap */
1243 arr
[i
] = arr
[i
+ 1];
1244 while(arr
[i
++] != NULL
);
1248 if(i3val_nonew
== TRUM1
)
1249 avp
= (struct a_amv_var
*)-1;
1254 /* The virtual variables */
1255 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1256 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1257 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1258 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1264 /* Place this last because once it is set first the variable will never
1265 * be removed again and thus match in the first block above */
1267 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1268 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1269 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1270 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1271 : a_amv_var_defvals
[i
].avdv_value
;
1280 avcp
->avc_var
= avp
;
1282 return (avp
!= NULL
);
1285 /* E.g., $TMPDIR may be set to non-existent, so we need to be able to catch
1286 * that and redirect to a possible default value */
1287 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1288 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, cp
)){
1290 if(avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
))
1291 unsetenv(avcp
->avc_name
);
1293 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0)
1297 struct a_amv_var
**avpp
;
1300 l
= strlen(avcp
->avc_name
) +1;
1302 avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1303 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1305 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1309 avp
->av_flags
= avmp
->avm_flags
;
1310 avp
->av_value
= a_amv_var_copy(cp
);
1312 if(avp
->av_flags
& a_AMV_VF_ENV
)
1313 a_amv_var__putenv(avcp
, avp
);
1314 if(avmp
->avm_flags
& a_AMV_VF_VIP
)
1315 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, cp
);
1321 a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
){
1325 struct a_amv_var_map
const *avmp
;
1328 /* Not function local, TODO but lazy evaluted for now */
1329 if(avcp
->avc_special_prop
== a_AMV_VST_QM
){
1330 avmp
= &a_amv_var_map
[a_AMV_VAR__QM_MAP_IDX
];
1331 avcp
->avc_okey
= ok_v___qm
;
1332 ep
= &n_pstate_ex_no
;
1334 avmp
= &a_amv_var_map
[a_AMV_VAR__EM_MAP_IDX
];
1335 avcp
->avc_okey
= ok_v___em
;
1336 ep
= &n_pstate_err_no
;
1339 /* XXX Oh heaven, we are responsible to ensure that $?/! is up-to-date
1340 * TODO we could num=1 ok_v___[qe]m, but the thing is still a string
1341 * TODO and thus conversion takes places over and over again; also
1342 * TODO for now that would have to occur before we set _that_ value
1343 * TODO so let's special treat it until we store ints as such */
1345 case 0: rv
= n_0
; break;
1346 case 1: rv
= n_1
; break;
1348 snprintf(itoabuf
, sizeof itoabuf
, "%d", *ep
);
1352 n_PS_ROOT_BLOCK(n_var_okset(avcp
->avc_okey
, (uintptr_t)rv
));
1354 avcp
->avc_hash
= avmp
->avm_hash
;
1355 avcp
->avc_map
= avmp
;
1356 rv
= a_amv_var_lookup(avcp
, FAL0
) ? avcp
->avc_var
->av_value
: NULL
;
1362 a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
){
1369 i
= strlen(rv
= &avcp
->avc_name
[1]);
1371 /* ERR, ERRDOC, ERRNAME, plus *-NAME variants */
1372 if(rv
[0] == 'E' && i
>= 3 && rv
[1] == 'R' && rv
[2] == 'R'){
1374 e
= n_pstate_err_no
;
1376 }else if(rv
[3] == '-'){
1377 e
= n_err_from_name(&rv
[4]);
1380 case 0: rv
= n_0
; break;
1381 case 1: rv
= n_1
; break;
1383 snprintf(itoabuf
, sizeof itoabuf
, "%d", e
);
1384 rv
= savestr(itoabuf
); /* XXX yet, cannot do numbers */
1389 if(!memcmp(&rv
[3], "DOC", 3)){
1392 case '\0': e
= n_pstate_err_no
; break;
1393 case '-': e
= n_err_from_name(&rv
[1]); break;
1396 rv
= n_err_to_doc(e
);
1398 }else if(i
>= 7 && !memcmp(&rv
[3], "NAME", 4)){
1401 case '\0': e
= n_pstate_err_no
; break;
1402 case '-': e
= n_err_from_name(&rv
[1]); break;
1405 rv
= n_err_to_name(e
);
1419 a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
){
1422 char const *rv
, **argv
;
1427 /* If in a macro/xy.. */
1428 if(a_amv_lopts
!= NULL
){
1430 struct a_amv_mac_call_args
*amcap
;
1432 amcap
= a_amv_lopts
->as_amcap
;
1433 argc
= amcap
->amca_pospar
.app_count
;
1434 argv
= amcap
->amca_pospar
.app_dat
;
1435 argv
+= amcap
->amca_pospar
.app_idx
;
1437 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
1438 if(!(ismacky
= (amcap
->amca_amp
== a_AMV_MACKY_MACK
)) &&
1439 (amcap
->amca_ps_hook_mask
||
1440 (assert(amcap
->amca_amp
!= NULL
),
1441 (amcap
->amca_amp
->am_flags
& a_AMV_MF_TYPE_MASK
1442 ) == a_AMV_MF_ACCOUNT
)))
1445 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1446 if(avcp
->avc_special_prop
> 0){
1447 if(argc
>= avcp
->avc_special_prop
)
1448 rv
= argv
[avcp
->avc_special_prop
- 1];
1450 rv
= amcap
->amca_name
;
1452 rv
= (a_amv_lopts
->as_up
!= NULL
1453 ? a_amv_lopts
->as_up
->as_amcap
->amca_name
: n_empty
);
1456 /* MACKY_MACK doesn't know about [*@#] */
1457 /*else*/ if(ismacky
){
1458 if(n_poption
& n_PO_D_V
)
1459 n_err(_("Cannot use $*/$@/$# in this context: %s\n"),
1460 n_shexp_quote_cp(avcp
->avc_name
, FAL0
));
1464 argc
= a_amv_pospar
.app_count
;
1465 argv
= a_amv_pospar
.app_dat
;
1466 argv
+= a_amv_pospar
.app_idx
;
1468 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1469 if(avcp
->avc_special_prop
> 0){
1470 if(argc
>= avcp
->avc_special_prop
)
1471 rv
= argv
[avcp
->avc_special_prop
- 1];
1478 switch(avcp
->avc_special_prop
){ /* XXX OPTIMIZE */
1479 case a_AMV_VST_STAR
:
1481 for(i
= j
= 0; i
< argc
; ++i
)
1482 j
+= strlen(argv
[i
]) + 1;
1488 rv
= cp
= n_autorec_alloc(j
);
1489 for(i
= j
= 0; i
< argc
; ++i
){
1490 j
= strlen(argv
[i
]);
1491 memcpy(cp
, argv
[i
], j
);
1498 case a_AMV_VST_NOSIGN
:{
1501 rv
= cp
= n_autorec_alloc(sizeof("65535"));
1502 snprintf(cp
, sizeof("65535"), "%hu", argc
);
1514 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1516 struct a_amv_var
*avp
;
1518 struct a_amv_var_map
const *avmp
;
1523 rv
= a_amv_var_clear(avcp
, force_env
);
1527 if((avmp
= avcp
->avc_map
) != NULL
){
1530 /* Validity checks */
1531 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1532 !(n_pstate
& n_PS_ROOT
))){
1533 value
= N_("Variable is read-only: %s\n");
1536 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1537 value
= N_("Variable must not be empty: %s\n");
1540 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1541 !a_amv_var_check_nocntrls(value
))){
1542 value
= N_("Variable forbids control characters: %s\n");
1545 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1546 !a_amv_var_check_num(value
, FAL0
))){
1547 value
= N_("Variable value not a number or out of range: %s\n");
1550 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1551 !a_amv_var_check_num(value
, TRU1
))){
1552 value
= _("Variable value not a number, negative, "
1553 "or out of range: %s\n");
1556 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1557 !(n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_ROOT
))){
1558 value
= N_("Variable cannot be set in a resource file: %s\n");
1562 /* Any more complicated inter-dependency? */
1563 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1564 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, value
))){
1565 value
= N_("Assignment of variable aborted: %s\n");
1567 n_err(V_(value
), avcp
->avc_name
);
1571 /* Transformations */
1572 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1575 oval
= savestr(value
);
1577 for(; (c
= *oval
) != '\0'; ++oval
)
1578 *oval
= lowerconv(c
);
1583 a_amv_var_lookup(avcp
, TRU1
);
1585 /* Don't care what happens later on, store this in the unroll list */
1586 if(a_amv_lopts
!= NULL
&&
1587 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1588 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1590 if((avp
= avcp
->avc_var
) == NULL
){
1591 struct a_amv_var
**avpp
;
1594 l
= strlen(avcp
->avc_name
) +1;
1595 avcp
->avc_var
= avp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1597 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1602 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1603 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1604 oval
= n_UNCONST(n_empty
);
1606 oval
= avp
->av_value
;
1609 avp
->av_value
= a_amv_var_copy(value
);
1611 /* Via `set' etc. the user may give even boolean options non-boolean
1612 * values, ignore that and force boolean */
1613 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1614 if(!(n_pstate
& n_PS_ROOT
) && (n_poption
& n_PO_D_VV
) &&
1616 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1617 avcp
->avc_name
, value
);
1618 avp
->av_value
= n_UNCONST(n_1
);
1620 avp
->av_value
= a_amv_var_copy(value
);
1623 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1624 avp
->av_flags
|= a_AMV_VF_LINKED
;
1625 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1626 rv
= a_amv_var__putenv(avcp
, avp
);
1627 if(avp
->av_flags
& a_AMV_VF_VIP
)
1628 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, value
);
1629 a_amv_var_free(oval
);
1636 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1644 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1646 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1648 if((rv
= (putenv(cp
) == 0))){
1651 if((ocp
= avp
->av_env
) != NULL
)
1662 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1663 struct a_amv_var
**avpp
, *avp
;
1664 struct a_amv_var_map
const *avmp
;
1670 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1671 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1672 !(n_pstate
& n_PS_ROOT
))){
1673 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1676 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1677 !a_amv_var_check_vips(a_AMV_VIP_CLEAR
, avcp
->avc_okey
, NULL
))){
1678 n_err(_("Clearance of variable aborted: %s\n"), avcp
->avc_name
);
1685 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1688 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1689 }else if(!(n_pstate
& (n_PS_ROOT
| n_PS_ROBOT
)) && (n_poption
& n_PO_D_V
))
1690 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1692 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1693 avcp
->avc_var
= NULL
;
1699 if(a_amv_lopts
!= NULL
&&
1700 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1701 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1703 avp
= avcp
->avc_var
;
1704 avcp
->avc_var
= NULL
;
1705 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1706 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1707 *avpp
= (*avpp
)->av_link
;
1715 envval
= avp
->av_env
;
1717 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1718 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1720 a_amv_var_free(avp
->av_value
);
1723 /* XXX Fun part, extremely simple-minded for now: if this variable has
1724 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1725 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1726 a_amv_var_lookup(avcp
, TRU1
);
1733 a_amv_var__clearenv(char const *name
, char *value
){
1735 extern char **environ
;
1747 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1752 while(*ecpp
++ != NULL
);
1762 a_amv_var_show_all(void){
1763 struct n_string msg
, *msgp
;
1766 struct a_amv_var
*avp
;
1767 char const **vacp
, **cap
;
1770 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1771 n_perr(_("Can't create temporary file for `set' listing"), 0);
1775 /* We need to instantiate first-time-inits and default values here, so that
1776 * they will be regular members of our _vars[] table */
1777 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1778 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1779 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1780 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1782 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1783 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1785 no
+= a_AMV_VAR_VIRTS_CNT
;
1787 vacp
= n_autorec_alloc(no
* sizeof(*vacp
));
1789 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1790 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1791 *cap
++ = avp
->av_name
;
1792 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1793 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1796 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1799 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1800 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1801 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1804 page_or_print(fp
, i
);
1811 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1815 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1821 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1822 struct a_amv_var_carrier avc
;
1828 msgp
= n_string_trunc(msgp
, 0);
1831 a_amv_var_revlookup(&avc
, name
);
1832 isset
= a_amv_var_lookup(&avc
, FAL0
);
1834 if(n_poption
& n_PO_D_V
){
1835 if(avc
.avc_map
== NULL
){
1836 msgp
= n_string_push_cp(msgp
, "#assembled variable with value");
1843 {a_AMV_VF_VIRT
, "virtual"},
1844 {a_AMV_VF_RDONLY
, "read-only"},
1845 {a_AMV_VF_NODEL
, "nodelete"},
1846 {a_AMV_VF_NOTEMPTY
, "notempty"},
1847 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1848 {a_AMV_VF_NUM
, "number"},
1849 {a_AMV_VF_POSNUM
, "positive-number"},
1850 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1851 {a_AMV_VF_ENV
, "sync-environ"},
1852 {a_AMV_VF_I3VAL
, "initial-value"},
1853 {a_AMV_VF_DEFVAL
, "default-value"},
1854 {a_AMV_VF_LINKED
, "`environ' link"}
1857 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1858 if(isset
? (avc
.avc_var
->av_flags
& tp
->flag
)
1859 : (avc
.avc_map
->avm_flags
& tp
->flag
)){
1860 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1861 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1865 msgp
= n_string_push_cp(msgp
, "\n ");
1868 if(!isset
|| (avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)){
1869 msgp
= n_string_push_c(msgp
, *n_ns
);
1871 if(avc
.avc_map
!= NULL
&& (avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
))
1872 msgp
= n_string_push_cp(msgp
, "boolean; ");
1873 msgp
= n_string_push_cp(msgp
, "variable not set: ");
1874 msgp
= n_string_push_cp(msgp
, n_shexp_quote_cp(name
, FAL0
));
1879 n_UNINIT(quote
, NULL
);
1880 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1881 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1882 if(strcmp(quote
, avc
.avc_var
->av_value
))
1883 msgp
= n_string_push_cp(msgp
, "wysh ");
1884 }else if(n_poption
& n_PO_D_V
)
1885 msgp
= n_string_push_cp(msgp
, "wysh ");
1886 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1887 msgp
= n_string_push_cp(msgp
, "environ ");
1888 msgp
= n_string_push_cp(msgp
, "set ");
1889 msgp
= n_string_push_cp(msgp
, name
);
1890 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1891 msgp
= n_string_push_c(msgp
, '=');
1892 msgp
= n_string_push_cp(msgp
, quote
);
1893 }else if(n_poption
& n_PO_D_V
)
1894 msgp
= n_string_push_cp(msgp
, " #boolean");
1897 msgp
= n_string_push_c(msgp
, '\n');
1898 fputs(n_string_cp(msgp
), fp
);
1900 return (i
> 0 ? 2 : 1);
1904 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1905 char *cp
, *cp2
, *varbuf
, c
;
1911 while((cp
= *ap
++) != NULL
){
1913 cp2
= varbuf
= n_autorec_alloc(strlen(cp
) +1);
1915 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1916 if(cntrlchar(c
) || spacechar(c
)){
1917 n_err(_("Variable name with control character ignored: %s\n"),
1926 cp
= n_UNCONST(n_empty
);
1931 n_err(_("Empty variable name ignored\n"));
1934 struct a_amv_var_carrier avc
;
1937 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1938 varbuf
= &varbuf
[2];
1940 a_amv_var_revlookup(&avc
, varbuf
);
1943 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1945 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1960 if((args
= v
)[0] == NULL
){
1961 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1965 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1967 n_err(_("Synopsis: define: <name> {\n"));
1971 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1978 c_undefine(void *v
){
1986 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1987 while(*++args
!= NULL
);
1997 rv
= a_amv_mac_call(vp
, FAL0
);
2003 c_call_if(void *vp
){
2007 rv
= a_amv_mac_call(vp
, TRU1
);
2014 struct a_amv_mac_call_args
*amcap
;
2015 struct a_amv_mac
*amp
;
2017 int rv
, i
, oqf
, nqf
;
2022 if((args
= v
)[0] == NULL
){
2023 rv
= (a_amv_mac_show(a_AMV_MF_ACCOUNT
) == FAL0
);
2027 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
2028 if(args
[2] != NULL
){
2029 n_err(_("Synopsis: account: <name> {\n"));
2032 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
2033 n_err(_("`account': cannot use reserved name: %s\n"),
2037 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACCOUNT
) == FAL0
);
2041 if(n_pstate
& n_PS_HOOK_MASK
){
2042 n_err(_("`account': can't change account from within a hook\n"));
2046 save_mbox_for_possible_quitstuff();
2049 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
2050 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACCOUNT
)) == NULL
){
2051 n_err(_("`account': account does not exist: %s\n"), args
[0]);
2055 oqf
= savequitflags();
2057 if(a_amv_acc_curr
!= NULL
){
2058 if(a_amv_acc_curr
->am_lopts
!= NULL
)
2059 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
2060 /* For accounts this lingers */
2061 --a_amv_acc_curr
->am_refcnt
;
2062 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DELETE
)
2063 a_amv_mac_free(a_amv_acc_curr
);
2066 a_amv_acc_curr
= amp
;
2069 assert(amp
->am_lopts
== NULL
);
2070 amcap
= n_lofi_alloc(sizeof *amcap
);
2071 memset(amcap
, 0, sizeof *amcap
);
2072 amcap
->amca_name
= amp
->am_name
;
2073 amcap
->amca_amp
= amp
;
2074 amcap
->amca_unroller
= &
->am_lopts
;
2075 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2076 amcap
->amca_no_xcall
= TRU1
;
2077 ++amp
->am_refcnt
; /* We may not run 0 to avoid being deleted! */
2078 if(!a_amv_mac_exec(amcap
)){
2079 /* XXX account switch incomplete, unroll? */
2080 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
2085 n_PS_ROOT_BLOCK((amp
!= NULL
? ok_vset(account
, amp
->am_name
)
2086 : ok_vclear(account
)));
2088 if((n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_HOOK_MASK
)){
2089 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
2090 restorequitflags(oqf
);
2091 if((i
= setfile("%", 0)) < 0)
2093 temporary_folder_hook_check(FAL0
);
2094 if(i
> 0 && !ok_blook(emptystart
))
2096 n_folder_announce(n_ANNOUNCE_CHANGE
);
2097 restorequitflags(nqf
);
2106 c_unaccount(void *v
){
2114 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACCOUNT
);
2115 while(*++args
!= NULL
);
2121 c_localopts(void *vp
){
2122 enum a_amv_loflags alf
, alm
;
2129 if(a_amv_lopts
== NULL
){
2130 n_err(_("Cannot use `localopts' in this context "
2131 "(not in `define' or `account', nor special hook)\n"));
2135 if((argv
= vp
)[1] == NULL
|| is_asccaseprefix((++argv
)[-1], "scope"))
2136 alf
= alm
= a_AMV_LF_SCOPE
;
2137 else if(is_asccaseprefix(argv
[-1], "call"))
2138 alf
= a_AMV_LF_CALL
, alm
= a_AMV_LF_CALL_MASK
;
2139 else if(is_asccaseprefix(argv
[-1], "call-fixate"))
2140 alf
= a_AMV_LF_CALL_FIXATE
, alm
= a_AMV_LF_CALL_MASK
;
2142 n_err(_("Synopsis: localopts: [<scope|call|call-fixate>] <boolean>\n"));
2146 if(alf
== a_AMV_LF_SCOPE
&&
2147 (a_amv_lopts
->as_loflags
& a_AMV_LF_SCOPE_FIXATE
)){
2148 if(n_poption
& n_PO_D_V
)
2149 n_err(_("Cannot turn off `localopts', setting is fixated\n"));
2153 a_amv_lopts
->as_loflags
&= ~alm
;
2154 if(boolify(*argv
, UIZ_MAX
, FAL0
) > 0)
2155 a_amv_lopts
->as_loflags
|= alf
;
2164 struct a_amv_pospar
*appp
;
2171 if((vp
= *(char**)vp
) == NULL
)
2176 if((n_idec_si16_cp(&sib
, vp
, 10, NULL
2177 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2178 ) != n_IDEC_STATE_CONSUMED
|| sib
< 0){
2179 n_err(_("`shift': invalid argument: %s\n"), vp
);
2185 /* If in in a macro/xy */
2186 if(a_amv_lopts
!= NULL
){
2187 struct a_amv_mac
const *amp
;
2188 struct a_amv_mac_call_args
*amcap
;
2190 /* Explicitly do allow `vpospar' created things! */
2191 amp
= (amcap
= a_amv_lopts
->as_amcap
)->amca_amp
;
2192 if((amp
== NULL
|| amcap
->amca_ps_hook_mask
||
2193 (amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == a_AMV_MF_ACCOUNT
) &&
2194 amcap
->amca_pospar
.app_not_heap
){
2195 n_err(_("Cannot use `shift' in `account's or hook macros etc.\n"));
2198 appp
= &amcap
->amca_pospar
;
2200 appp
= &a_amv_pospar
;
2202 if(i
> appp
->app_count
){
2203 n_err(_("`shift': cannot shift %hu of %hu parameters\n"),
2204 i
, appp
->app_count
);
2208 appp
->app_count
-= i
;
2217 c_return(void *vp
){ /* TODO the exit status should be m_si64! */
2221 if(a_amv_lopts
!= NULL
){
2224 n_go_input_force_eof();
2225 n_pstate_err_no
= n_ERR_NONE
;
2228 if((argv
= vp
)[0] != NULL
){
2231 if((n_idec_si32_cp(&i
, argv
[0], 10, NULL
2232 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2233 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2236 n_err(_("`return': return value argument is invalid: %s\n"),
2238 n_pstate_err_no
= n_ERR_INVAL
;
2242 if(argv
[1] != NULL
){
2243 if((n_idec_si32_cp(&i
, argv
[1], 10, NULL
2244 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2245 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2246 n_pstate_err_no
= i
;
2248 n_err(_("`return': error number argument is invalid: %s\n"),
2250 n_pstate_err_no
= n_ERR_INVAL
;
2256 n_err(_("Can only use `return' in a macro\n"));
2257 n_pstate_err_no
= n_ERR_OPNOTSUPP
;
2265 temporary_folder_hook_check(bool_t nmail
){ /* TODO temporary, v15: drop */
2266 struct a_amv_mac_call_args
*amcap
;
2267 struct a_amv_mac
*amp
;
2275 var
= n_autorec_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
2277 /* First try the fully resolved path */
2278 snprintf(var
, len
, "folder-hook-%s", mailname
);
2279 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2282 /* If we are under *folder*, try the usual +NAME syntax, too */
2283 if(displayname
[0] == '+'){
2286 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
2288 snprintf(var
, len
, "folder-hook-+%s", x
);
2289 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2295 /* Plain *folder-hook* is our last try */
2296 if((cp
= ok_vlook(folder_hook
)) == NULL
)
2300 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
2301 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
2302 n_shexp_quote_cp(displayname
, FAL0
), cp
);
2307 amcap
= n_lofi_alloc(sizeof *amcap
);
2308 memset(amcap
, 0, sizeof *amcap
);
2309 amcap
->amca_name
= cp
;
2310 amcap
->amca_amp
= amp
;
2311 n_pstate
&= ~n_PS_HOOK_MASK
;
2313 amcap
->amca_unroller
= NULL
;
2314 n_pstate
|= n_PS_HOOK_NEWMAIL
;
2316 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
2317 n_pstate
|= n_PS_HOOK
;
2319 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2320 amcap
->amca_ps_hook_mask
= TRU1
;
2321 amcap
->amca_no_xcall
= TRU1
;
2322 rv
= a_amv_mac_exec(amcap
);
2323 n_pstate
&= ~n_PS_HOOK_MASK
;
2331 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2333 if(a_amv_folder_hook_lopts
!= NULL
){
2334 void *save
= a_amv_lopts
;
2337 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
2338 assert(a_amv_folder_hook_lopts
== NULL
);
2345 temporary_compose_mode_hook_call(char const *macname
,
2346 void (*hook_pre
)(void *), void *hook_arg
){
2347 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_GO_SPLICE
2348 * TODO comment in go.c for the right way of doing things! */
2349 static struct a_amv_lostack
*cmh_losp
;
2350 struct a_amv_mac_call_args
*amcap
;
2351 struct a_amv_mac
*amp
;
2356 if(macname
== (char*)-1){
2357 a_amv_mac__finalize(cmh_losp
);
2359 }else if(macname
!= NULL
&&
2360 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
2361 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
2364 amcap
= n_lofi_alloc(sizeof *amcap
);
2365 memset(amcap
, 0, sizeof *amcap
);
2366 amcap
->amca_name
= (macname
!= NULL
) ? macname
2367 : "*on-compose-splice(-shell)?*";
2368 amcap
->amca_amp
= amp
;
2369 amcap
->amca_unroller
= &a_amv_compose_lopts
;
2370 amcap
->amca_hook_pre
= hook_pre
;
2371 amcap
->amca_hook_arg
= hook_arg
;
2372 amcap
->amca_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2373 amcap
->amca_ps_hook_mask
= TRU1
;
2374 amcap
->amca_no_xcall
= TRU1
;
2375 n_pstate
&= ~n_PS_HOOK_MASK
;
2376 n_pstate
|= n_PS_HOOK
;
2378 a_amv_mac_exec(amcap
);
2380 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
2381 memset(cmh_losp
, 0, sizeof *cmh_losp
);
2382 cmh_losp
->as_global_saved
= a_amv_lopts
;
2383 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
2384 cmh_losp
->as_loflags
= a_AMV_LF_SCOPE_FIXATE
;
2385 a_amv_lopts
= cmh_losp
;
2392 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2394 if(a_amv_compose_lopts
!= NULL
){
2395 void *save
= a_amv_lopts
;
2398 a_amv_lopts_unroll(&a_amv_compose_lopts
);
2399 assert(a_amv_compose_lopts
== NULL
);
2406 n_var_is_user_writable(char const *name
){
2407 struct a_amv_var_carrier avc
;
2408 struct a_amv_var_map
const *avmp
;
2412 a_amv_var_revlookup(&avc
, name
);
2413 if((avmp
= avc
.avc_map
) == NULL
)
2416 rv
= ((avmp
->avm_flags
& (a_AMV_VF_BOOL
| a_AMV_VF_RDONLY
)) == 0);
2422 n_var_oklook(enum okeys okey
){
2423 struct a_amv_var_carrier avc
;
2425 struct a_amv_var_map
const *avmp
;
2428 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2429 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2430 avc
.avc_hash
= avmp
->avm_hash
;
2431 avc
.avc_okey
= okey
;
2433 if(a_amv_var_lookup(&avc
, FAL0
))
2434 rv
= avc
.avc_var
->av_value
;
2442 n_var_okset(enum okeys okey
, uintptr_t val
){
2443 struct a_amv_var_carrier avc
;
2445 struct a_amv_var_map
const *avmp
;
2448 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2449 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2450 avc
.avc_hash
= avmp
->avm_hash
;
2451 avc
.avc_okey
= okey
;
2453 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2459 n_var_okclear(enum okeys okey
){
2460 struct a_amv_var_carrier avc
;
2462 struct a_amv_var_map
const *avmp
;
2465 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2466 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2467 avc
.avc_hash
= avmp
->avm_hash
;
2468 avc
.avc_okey
= okey
;
2470 rv
= a_amv_var_clear(&avc
, FAL0
);
2476 n_var_vlook(char const *vokey
, bool_t try_getenv
){
2477 struct a_amv_var_carrier avc
;
2481 a_amv_var_revlookup(&avc
, vokey
);
2483 switch((enum a_amv_var_special_category
)avc
.avc_special_cat
){
2484 default: /* silence CC */
2485 case a_AMV_VSC_NONE
:
2486 if(a_amv_var_lookup(&avc
, FAL0
))
2487 rv
= avc
.avc_var
->av_value
;
2488 /* Only check the environment for something that is otherwise unknown */
2489 else if(try_getenv
&& avc
.avc_map
== NULL
)
2494 case a_AMV_VSC_GLOBAL
:
2495 rv
= a_amv_var_vsc_global(&avc
);
2497 case a_AMV_VSC_MULTIPLEX
:
2498 rv
= a_amv_var_vsc_multiplex(&avc
);
2500 case a_AMV_VSC_POSPAR
:
2501 case a_AMV_VSC_POSPAR_ENV
:
2502 rv
= a_amv_var_vsc_pospar(&avc
);
2510 n_var_vexplode(void const **cookie
){
2511 struct a_amv_pospar
*appp
;
2514 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
2516 *cookie
= (appp
->app_count
> 0) ? &appp
->app_dat
[appp
->app_idx
] : NULL
;
2518 return (*cookie
!= NULL
);
2522 n_var_vset(char const *vokey
, uintptr_t val
){
2523 struct a_amv_var_carrier avc
;
2527 a_amv_var_revlookup(&avc
, vokey
);
2529 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2535 n_var_vclear(char const *vokey
){
2536 struct a_amv_var_carrier avc
;
2540 a_amv_var_revlookup(&avc
, vokey
);
2542 ok
= a_amv_var_clear(&avc
, FAL0
);
2549 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
2550 enum okey_xlook_mode oxm
){
2551 struct a_amv_var_carrier avc
;
2552 struct str
const *us
;
2555 struct a_amv_var_map
const *avmp
;
2558 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
2560 /* For simplicity: allow this case too */
2561 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
2566 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2567 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2568 avc
.avc_okey
= okey
;
2570 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
2571 nlen
= strlen(avc
.avc_name
);
2572 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
2573 memcpy(nbuf
, avc
.avc_name
, nlen
);
2576 /* One of .url_u_h_p and .url_h_p we test in here */
2577 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2578 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2579 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2580 if(a_amv_var_lookup(&avc
, FAL0
))
2585 us
= &urlp
->url_h_p
;
2586 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2587 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2588 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2589 if(a_amv_var_lookup(&avc
, FAL0
)){
2591 rv
= avc
.avc_var
->av_value
;
2597 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2604 #endif /* HAVE_SOCKETS */
2612 if(*(ap
= v
) == NULL
){
2613 a_amv_var_show_all();
2616 err
= !a_amv_var_c_set(ap
, FAL0
);
2627 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2628 err
|= !n_var_vclear(*ap
);
2638 if(*(ap
= v
) == NULL
)
2641 struct n_string msg
, *msgp
= &msg
;
2643 msgp
= n_string_creat(msgp
);
2644 for(; *ap
!= NULL
; ++ap
)
2645 a_amv_var_show(*ap
, n_stdout
, msgp
);
2649 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2654 struct a_amv_var_carrier avc
;
2658 sighandler_type sigint
;
2661 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2663 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2664 memset(&avc
, 0, sizeof avc
);
2666 a_amv_var_revlookup(&avc
, *argv
);
2668 if(avc
.avc_map
!= NULL
){
2669 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2670 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2674 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2675 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2681 a_amv_var_lookup(&avc
, FAL0
);
2683 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2685 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2688 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2689 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2690 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2697 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2706 if(UICMP(64, l
, >=, UIZ_MAX
-42)){
2707 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2712 varres
= val
= n_autorec_alloc(l
+1);
2713 for(; l
> 0 && (c
= getc(nf
)) != EOF
; --l
)
2717 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2723 if(!a_amv_var_set(&avc
, varres
, FAL0
))
2728 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2734 safe_signal(SIGINT
, sigint
);
2741 struct a_amv_var_carrier avc
;
2747 if((islnk
= is_asccaseprefix(*(ap
= v
), "link")) ||
2748 is_asccaseprefix(*ap
, "unlink")){
2749 for(err
= 0; *++ap
!= NULL
;){
2750 a_amv_var_revlookup(&avc
, *ap
);
2752 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2753 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2755 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2757 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2758 if(n_poption
& n_PO_D_V
)
2759 n_err(_("`environ': link: already established: %s\n"), *ap
);
2762 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2763 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2764 a_amv_var__putenv(&avc
, avc
.avc_var
);
2766 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2769 char const *evp
= getenv(*ap
);
2772 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2774 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2780 }else if(is_asccaseprefix(*ap
, "set"))
2781 err
= !a_amv_var_c_set(++ap
, TRU1
);
2782 else if(is_asccaseprefix(*ap
, "unset")){
2783 for(err
= 0; *++ap
!= NULL
;){
2784 a_amv_var_revlookup(&avc
, *ap
);
2786 if(!a_amv_var_clear(&avc
, TRU1
))
2790 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2798 c_vexpr(void *v
){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2800 enum n_idec_state ids
;
2802 char op
, varbuf
[64 + 64 / 8 +1];
2803 char const **argv
, *varname
, *varres
, *cp
;
2806 a_SOFTOVERFLOW
= 1u<<1,
2808 a_ISDECIMAL
= 1u<<3, /* Print only decimal result */
2809 a_SATURATED
= 1u<<4,
2811 a_UNSIGNED
= 1u<<6, /* Unsigned right shift (share bit ok) */
2818 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2819 n_UNINIT(varres
, n_empty
);
2821 if((cp
= argv
[0])[0] == '\0')
2831 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2834 if(*(cp
= *++argv
) == '\0')
2836 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2837 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2838 ) != n_IDEC_STATE_CONSUMED
){
2839 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2841 f
|= a_SOFTOVERFLOW
;
2858 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
2863 if(*(cp
= *++argv
) == '\0')
2865 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2866 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2867 ) != n_IDEC_STATE_CONSUMED
){
2868 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2870 f
|= a_SOFTOVERFLOW
;
2874 if(*(cp
= *++argv
) == '\0')
2876 else if(((ids
= n_idec_si64_cp(&rhv
, cp
, 0, NULL
)
2877 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2878 ) != n_IDEC_STATE_CONSUMED
){
2879 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2881 f
|= a_SOFTOVERFLOW
;
2891 if(rhv
!= SI64_MIN
){
2896 goto jenum_plusminus
;
2902 if(SI64_MAX
- rhv
< lhv
)
2903 goto jenum_plusminus
;
2908 if(rhv
!= SI64_MIN
){
2913 goto jenum_plusminus
;
2919 if(SI64_MIN
+ rhv
> lhv
){
2921 if(!(f
& a_SATURATED
))
2922 goto jenum_overflow
;
2923 f
|= a_SOFTOVERFLOW
;
2924 lhv
= (lhv
< 0 || xop
== '-') ? SI64_MIN
: SI64_MAX
;
2929 /* Will the result be positive? */
2930 if((lhv
< 0) == (rhv
< 0)){
2935 if(rhv
!= 0 && lhv
!= 0 && SI64_MAX
/ rhv
> lhv
){
2936 if(!(f
& a_SATURATED
))
2937 goto jenum_overflow
;
2938 f
|= a_SOFTOVERFLOW
;
2944 if(lhv
!= 0 && SI64_MIN
/ lhv
< rhv
){
2945 if(!(f
& a_SATURATED
))
2946 goto jenum_overflow
;
2947 f
|= a_SOFTOVERFLOW
;
2952 if(rhv
!= 0 && lhv
!= 0 && SI64_MIN
/ rhv
< lhv
){
2953 if(!(f
& a_SATURATED
))
2954 goto jenum_overflow
;
2955 f
|= a_SOFTOVERFLOW
;
2964 if(!(f
& a_SATURATED
))
2966 f
|= a_SOFTOVERFLOW
;
2973 if(!(f
& a_SATURATED
))
2975 f
|= a_SOFTOVERFLOW
;
2993 if(rhv
> 63){ /* xxx 63? */
2994 if(!(f
& a_SATURATED
))
2995 goto jenum_overflow
;
3000 else if(f
& a_UNSIGNED
)
3001 lhv
= (ui64_t
)lhv
>> (ui8_t
)rhv
;
3011 }else if(cp
[2] == '\0' && cp
[1] == '@'){
3015 }else if(cp
[0] == '<'){
3027 }else if(cp
[0] == '>'){
3043 }else if(is_asccaseprefix(cp
, "length")){
3044 f
|= a_ISNUM
| a_ISDECIMAL
;
3045 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3048 i
= strlen(*++argv
);
3049 if(UICMP(64, i
, >, SI64_MAX
))
3050 goto jestr_overflow
;
3052 }else if(is_asccaseprefix(cp
, "hash")){
3053 f
|= a_ISNUM
| a_ISDECIMAL
;
3054 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3057 i
= n_torek_hash(*++argv
);
3059 }else if(is_asccaseprefix(cp
, "find")){
3060 f
|= a_ISNUM
| a_ISDECIMAL
;
3061 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3064 if((cp
= strstr(argv
[1], argv
[2])) == NULL
)
3066 i
= PTR2SIZE(cp
- argv
[1]);
3067 if(UICMP(64, i
, >, SI64_MAX
))
3068 goto jestr_overflow
;
3070 }else if(is_asccaseprefix(cp
, "ifind")){
3071 f
|= a_ISNUM
| a_ISDECIMAL
;
3072 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3075 if((cp
= asccasestr(argv
[1], argv
[2])) == NULL
)
3077 i
= PTR2SIZE(cp
- argv
[1]);
3078 if(UICMP(64, i
, >, SI64_MAX
))
3079 goto jestr_overflow
;
3081 }else if(is_asccaseprefix(cp
, "substring")){
3082 if(argv
[1] == NULL
|| argv
[2] == NULL
)
3084 if(argv
[3] != NULL
&& argv
[4] != NULL
)
3087 i
= strlen(varres
= *++argv
);
3089 if(*(cp
= *++argv
) == '\0')
3091 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3092 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3093 ) != n_IDEC_STATE_CONSUMED
)
3094 goto jestr_numrange
;
3096 if(UICMP(64, i
, <, -lhv
))
3097 goto jesubstring_off
;
3100 if(UICMP(64, i
, >=, lhv
)){
3105 if(n_poption
& n_PO_D_V
)
3106 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
3107 n_shexp_quote_cp(argv
[-1], FAL0
));
3108 f
|= a_SOFTOVERFLOW
;
3111 if(argv
[1] != NULL
){
3112 if(*(cp
= *++argv
) == '\0')
3114 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3115 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3116 ) != n_IDEC_STATE_CONSUMED
)
3117 goto jestr_numrange
;
3119 if(UICMP(64, i
, <, -lhv
))
3120 goto jesubstring_len
;
3123 if(UICMP(64, i
, >=, lhv
)){
3124 if(UICMP(64, i
, !=, lhv
))
3125 varres
= savestrbuf(varres
, (size_t)lhv
);
3128 if(n_poption
& n_PO_D_V
)
3129 n_err(_("`vexpr': substring: length argument too large: %s\n"),
3130 n_shexp_quote_cp(argv
[-2], FAL0
));
3131 f
|= a_SOFTOVERFLOW
;
3134 }else if(is_asccaseprefix(cp
, "trim")) Jtrim
: {
3136 enum n_str_trim_flags stf
;
3138 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3141 if(is_asccaseprefix(cp
, "trim-front"))
3142 stf
= n_STR_TRIM_FRONT
;
3143 else if(is_asccaseprefix(cp
, "trim-end"))
3144 stf
= n_STR_TRIM_END
;
3146 stf
= n_STR_TRIM_BOTH
;
3148 trim
.l
= strlen(trim
.s
= n_UNCONST(argv
[1]));
3149 (void)n_str_trim(&trim
, stf
);
3150 varres
= savestrbuf(trim
.s
, trim
.l
);
3151 }else if(is_asccaseprefix(cp
, "trim-front"))
3153 else if(is_asccaseprefix(cp
, "trim-end"))
3155 else if(is_asccaseprefix(cp
, "random")){
3156 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3159 if((n_idec_si64_cp(&lhv
, argv
[1], 0, NULL
3160 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3161 ) != n_IDEC_STATE_CONSUMED
|| lhv
< 0 || lhv
> PATH_MAX
)
3162 goto jestr_numrange
;
3165 varres
= n_random_create_cp((size_t)lhv
, NULL
);
3166 }else if(is_asccaseprefix(cp
, "file-expand")){
3167 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3170 if((varres
= fexpand(argv
[1], FEXP_NVAR
| FEXP_NOPROTO
)) == NULL
)
3172 }else if(is_asccaseprefix(cp
, "makeprint")){
3173 struct str sin
, sout
;
3175 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3178 /* XXX using strlen for `vexpr makeprint' is wrong for UTF-16 */
3179 sin
.l
= strlen(sin
.s
= n_UNCONST(argv
[1]));
3180 makeprint(&sin
, &sout
);
3181 varres
= savestrbuf(sout
.s
, sout
.l
);
3183 /* TODO `vexpr': (wide) string length, find, etc!! */
3185 }else if(is_asccaseprefix(cp
, "regex")) Jregex
:{
3186 regmatch_t rema
[1 + n_VEXPR_REGEX_MAX
];
3190 f
|= a_ISNUM
| a_ISDECIMAL
;
3191 if(argv
[1] == NULL
|| argv
[2] == NULL
||
3192 (argv
[3] != NULL
&& argv
[4] != NULL
))
3195 reflrv
= REG_EXTENDED
;
3197 reflrv
|= REG_ICASE
;
3198 if((reflrv
= regcomp(&re
, argv
[2], reflrv
))){
3199 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
3200 n_shexp_quote_cp(argv
[2], FAL0
), n_regex_err_to_doc(&re
, reflrv
));
3202 n_pstate_err_no
= n_ERR_INVAL
;
3205 reflrv
= regexec(&re
, argv
[1], n_NELEM(rema
), rema
, 0);
3207 if(reflrv
== REG_NOMATCH
)
3210 /* Search only? Else replace, which is a bit */
3211 if(argv
[3] == NULL
){
3212 if(UICMP(64, rema
[0].rm_so
, >, SI64_MAX
))
3213 goto jestr_overflow
;
3214 lhv
= (si64_t
)rema
[0].rm_so
;
3216 /* We need to setup some kind of pseudo macro environment for this */
3217 struct a_amv_lostack los
;
3218 struct a_amv_mac_call_args amca
;
3219 char const **reargv
;
3221 memset(&amca
, 0, sizeof amca
);
3222 amca
.amca_name
= savestrbuf(&argv
[1][rema
[0].rm_so
],
3223 rema
[0].rm_eo
- rema
[0].rm_so
);
3224 amca
.amca_amp
= a_AMV_MACKY_MACK
;
3225 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3227 amca
.amca_pospar
.app_count
= (ui32_t
)i
- 1;
3228 amca
.amca_pospar
.app_not_heap
= TRU1
;
3229 amca
.amca_pospar
.app_dat
=
3230 reargv
= n_autorec_alloc(sizeof(char*) * i
);
3231 for(i
= 1; i
< n_NELEM(rema
) && rema
[i
].rm_so
!= -1; ++i
)
3232 *reargv
++ = savestrbuf(&argv
[1][rema
[i
].rm_so
],
3233 rema
[i
].rm_eo
- rema
[i
].rm_so
);
3236 memset(&los
, 0, sizeof los
);
3237 hold_all_sigs(); /* TODO DISLIKE! */
3238 los
.as_global_saved
= a_amv_lopts
;
3239 los
.as_amcap
= &amca
;
3240 los
.as_up
= los
.as_global_saved
;
3245 struct n_string s_b
;
3246 enum n_shexp_state shs
;
3248 templ
.s
= n_UNCONST(argv
[3]);
3250 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
3251 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
3252 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
),
3253 n_string_creat_auto(&s_b
), &templ
, NULL
);
3254 if((shs
& (n_SHEXP_STATE_ERR_MASK
| n_SHEXP_STATE_STOP
)
3255 ) == n_SHEXP_STATE_STOP
){
3256 varres
= n_string_cp(&s_b
);
3257 n_string_drop_ownership(&s_b
);
3262 a_amv_lopts
= los
.as_global_saved
;
3263 rele_all_sigs(); /* TODO DISLIKE! */
3267 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
3269 }else if(is_asccaseprefix(argv
[0], "iregex")){
3272 #endif /* HAVE_REGEX */
3276 n_pstate_err_no
= (f
& a_SOFTOVERFLOW
) ? n_ERR_OVERFLOW
: n_ERR_NONE
;
3279 /* Generate the variable value content for numerics.
3280 * Anticipate in our handling below! (Don't do needless work) */
3282 if((f
& a_ISNUM
) && ((f
& a_ISDECIMAL
) || varname
!= NULL
)){
3283 snprintf(varbuf
, sizeof varbuf
, "%" PRId64
, lhv
);
3287 if(varname
== NULL
){
3288 /* If there was no error and we are printing a numeric result, print some
3289 * more bases for the fun of it */
3290 if((f
& (a_ERR
| a_ISNUM
| a_ISDECIMAL
)) == a_ISNUM
){
3293 for(j
= 1, i
= 0; i
< 64; ++i
){
3294 varbuf
[63 + 64 / 8 -j
- i
] = (lhv
& ((ui64_t
)1 << i
)) ? '1' : '0';
3295 if((i
& 7) == 7 && i
!= 63){
3297 varbuf
[63 + 64 / 8 -j
- i
] = ' ';
3300 varbuf
[64 + 64 / 8 -1] = '\0';
3301 if(fprintf(n_stdout
,
3302 "%s\n0%" PRIo64
" | 0x%" PRIX64
" | %" PRId64
"\n",
3303 varbuf
, lhv
, lhv
, lhv
) < 0){
3304 n_pstate_err_no
= n_err_no
;
3307 }else if(varres
!= NULL
&& fprintf(n_stdout
, "%s\n", varres
) < 0){
3308 n_pstate_err_no
= n_err_no
;
3311 }else if(!n_var_vset(varname
, (uintptr_t)varres
)){
3312 n_pstate_err_no
= n_ERR_NOTSUP
;
3316 return (f
& a_ERR
) ? 1 : 0;
3319 f
= a_ERR
| a_ISNUM
;
3323 n_err(_("`vexpr': invalid subcommand: %s\n"),
3324 n_shexp_quote_cp(*argv
, FAL0
));
3325 n_pstate_err_no
= n_ERR_INVAL
;
3328 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3329 n_pstate_err_no
= n_ERR_INVAL
;
3333 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3334 n_shexp_quote_cp(*argv
, FAL0
));
3335 n_pstate_err_no
= n_ERR_RANGE
;
3338 n_err(_("`vexpr': expression overflows datatype: %" PRId64
" %c %" PRId64
3339 "\n"), lhv
, op
, rhv
);
3340 n_pstate_err_no
= n_ERR_OVERFLOW
;
3344 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3345 n_shexp_quote_cp(*argv
, FAL0
));
3346 n_pstate_err_no
= n_ERR_RANGE
;
3349 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3350 n_pstate_err_no
= n_ERR_OVERFLOW
;
3353 n_pstate_err_no
= n_ERR_NODATA
;
3364 struct n_cmd_arg
*cap
;
3366 struct a_amv_pospar
*appp
;
3375 struct n_cmd_arg_ctx
*cacp
;
3378 n_pstate_err_no
= n_ERR_NONE
;
3379 n_UNINIT(varres
, n_empty
);
3381 cap
= cacp
->cac_arg
;
3383 if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "set"))
3385 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "clear"))
3387 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "quote"))
3390 n_err(_("`vpospar': invalid subcommand: %s\n"),
3391 n_shexp_quote_cp(cap
->ca_arg
.ca_str
.s
, FAL0
));
3392 n_pstate_err_no
= n_ERR_INVAL
;
3398 if((f
& (a_CLEAR
| a_QUOTE
)) && cap
->ca_next
!= NULL
){
3399 n_err(_("`vpospar': `%s': takes no argument\n"), cap
->ca_arg
.ca_str
.s
);
3400 n_pstate_err_no
= n_ERR_INVAL
;
3407 /* If in a macro, we need to overwrite the local instead of global argv */
3408 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
3411 if(f
& (a_SET
| a_CLEAR
)){
3412 if(cacp
->cac_vput
!= NULL
)
3413 n_err(_("`vpospar': `vput' only supported for `quote' subcommand\n"));
3414 if(!appp
->app_not_heap
&& appp
->app_maxcount
> 0){
3415 for(i
= appp
->app_maxcount
; i
-- != 0;)
3416 n_free(n_UNCONST(appp
->app_dat
[i
]));
3417 n_free(appp
->app_dat
);
3419 memset(appp
, 0, sizeof *appp
);
3422 if((i
= cacp
->cac_no
) > a_AMV_POSPAR_MAX
){
3423 n_err(_("`vpospar': overflow: %" PRIuZ
" arguments!\n"), i
);
3424 n_pstate_err_no
= n_ERR_OVERFLOW
;
3429 memset(appp
, 0, sizeof *appp
);
3431 appp
->app_maxcount
= appp
->app_count
= (ui16_t
)i
;
3432 /* XXX Optimize: store it all in one chunk! */
3434 i
*= sizeof *appp
->app_dat
;
3435 appp
->app_dat
= n_alloc(i
);
3437 for(i
= 0; cap
!= NULL
; ++i
, cap
= cap
->ca_next
){
3438 appp
->app_dat
[i
] = n_alloc(cap
->ca_arg
.ca_str
.l
+1);
3439 memcpy(n_UNCONST(appp
->app_dat
[i
]), cap
->ca_arg
.ca_str
.s
,
3440 cap
->ca_arg
.ca_str
.l
+1);
3443 appp
->app_dat
[i
] = NULL
;
3447 if(appp
->app_count
== 0)
3451 struct n_string s
, *sp
;
3454 sp
= n_string_creat_auto(&s
);
3456 sep1
= *ok_vlook(ifs
);
3457 sep2
= *ok_vlook(ifs_ws
);
3463 for(i
= 0; i
< appp
->app_count
; ++i
){
3465 if(!n_string_can_swallow(sp
, 2))
3467 sp
= n_string_push_c(sp
, sep1
);
3469 sp
= n_string_push_c(sp
, sep2
);
3471 in
.l
= strlen(in
.s
= n_UNCONST(appp
->app_dat
[i
+ appp
->app_idx
]));
3473 if(!n_string_can_swallow(sp
, in
.l
)){
3475 n_err(_("`vpospar': overflow: string too long!\n"));
3476 n_pstate_err_no
= n_ERR_OVERFLOW
;
3480 sp
= n_shexp_quote(sp
, &in
, TRU1
);
3483 varres
= n_string_cp(sp
);
3486 if(cacp
->cac_vput
== NULL
){
3487 if(fprintf(n_stdout
, "%s\n", varres
) < 0){
3488 n_pstate_err_no
= n_err_no
;
3491 }else if(!n_var_vset(cacp
->cac_vput
, (uintptr_t)varres
)){
3492 n_pstate_err_no
= n_ERR_NOTSUP
;
3498 return (f
& a_ERR
) ? 1 : 0;