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 */
78 /* make-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
79 * _IMPORT implies _ENV; it doesn't verify anything... */
82 a_AMV_VF_BOOL
= 1u<<0, /* ok_b_* */
83 a_AMV_VF_VIRT
= 1u<<1, /* "Stateless" automatic variable */
84 a_AMV_VF_NOLOPTS
= 1u<<2, /* May not be tracked by `localopts' */
85 a_AMV_VF_RDONLY
= 1u<<3, /* May not be set by user */
86 a_AMV_VF_NODEL
= 1u<<4, /* May not be deleted */
87 a_AMV_VF_NOTEMPTY
= 1u<<5, /* May not be assigned an empty value */
88 a_AMV_VF_NOCNTRLS
= 1u<<6, /* Value may not contain control characters */
89 a_AMV_VF_NUM
= 1u<<7, /* Value must be a 32-bit number */
90 a_AMV_VF_POSNUM
= 1u<<8, /* Value must be positive 32-bit number */
91 a_AMV_VF_LOWER
= 1u<<9, /* Values will be stored in a lowercase version */
92 a_AMV_VF_VIP
= 1u<<10, /* Wants _var_check_vips() evaluation */
93 a_AMV_VF_IMPORT
= 1u<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
94 a_AMV_VF_ENV
= 1u<<12, /* Update environment on change */
95 a_AMV_VF_I3VAL
= 1u<<13, /* Has an initial value */
96 a_AMV_VF_DEFVAL
= 1u<<14, /* Has a default value */
97 a_AMV_VF_LINKED
= 1u<<15, /* `environ' linked */
98 a_AMV_VF__MASK
= (1u<<(15+1)) - 1
101 /* We support some special parameter names for one(+)-letter variable names;
102 * note these have counterparts in the code that manages shell expansion!
103 * All these special variables are solely backed by n_var_vlook(), and besides
104 * there is only a_amv_var_revlookup() which knows about them */
105 enum a_amv_var_special_category
{
106 a_AMV_VSC_NONE
, /* Normal variable, no special treatment */
107 a_AMV_VSC_GLOBAL
, /* ${[?!]} are specially mapped, but global */
108 a_AMV_VSC_MULTIPLEX
, /* ${^.*} circumflex accent multiplexer */
109 a_AMV_VSC_POSPAR
, /* ${[1-9][0-9]*} positional parameters */
110 a_AMV_VSC_POSPAR_ENV
/* ${[*@#]} positional parameter support variables */
113 enum a_amv_var_special_type
{
115 a_AMV_VST_QM
, /* ? */
116 a_AMV_VST_EM
, /* ! */
118 /* This is special in that it is a multiplex indicator, the ^ is followed by
119 * a normal variable */
120 a_AMV_VST_CACC
, /* ^ (circumflex accent) */
121 /* _VSC_POSPAR_ENV */
122 a_AMV_VST_STAR
, /* * */
123 a_AMV_VST_AT
, /* @ */
124 a_AMV_VST_NOSIGN
/* # */
127 enum a_amv_var_vip_mode
{
134 ui16_t app_maxcount
; /* == slots in .app_dat */
135 ui16_t app_count
; /* Maximum a_AMV_POSPAR_MAX */
136 ui16_t app_idx
; /* `shift' moves this one, decs .app_count */
137 bool_t app_not_heap
; /* .app_dat stuff not on dynamically allocated */
139 char const **app_dat
; /* NULL terminated (for "$@" explosion support) */
141 n_CTA(a_AMV_POSPAR_MAX
<= SI16_MAX
, "Limit exceeds datatype capabilities");
144 struct a_amv_mac
*am_next
;
145 ui32_t am_maxlen
; /* of any line in .am_line_dat */
146 ui32_t am_line_cnt
; /* of *.am_line_dat (but NULL terminated) */
147 struct a_amv_mac_line
**am_line_dat
; /* TODO use deque? */
148 struct a_amv_var
*am_lopts
; /* `localopts' unroll list */
149 ui32_t am_refcnt
; /* 0-based for `un{account,define}' purposes */
150 ui8_t am_flags
; /* enum a_amv_mac_flags */
151 char am_name
[n_VFIELD_SIZE(3)]; /* of this macro */
153 n_CTA(a_AMV_MF__MAX
<= UI8_MAX
, "Enumeration excesses storage datatype");
155 struct a_amv_mac_line
{
157 ui32_t aml_prespc
; /* Number of leading SPC, for display purposes */
158 char aml_dat
[n_VFIELD_SIZE(0)];
161 struct a_amv_mac_call_args
{
162 char const *amca_name
; /* For MACKY_MACK, this is *0*! */
163 struct a_amv_mac
*amca_amp
; /* "const", but for am_refcnt */
164 struct a_amv_var
**amca_unroller
;
165 void (*amca_hook_pre
)(void *);
167 bool_t amca_lopts_on
;
168 bool_t amca_ps_hook_mask
;
169 bool_t amca_no_xcall
; /* We want n_GO_INPUT_NO_XCALL for this */
171 struct a_amv_pospar amca_pospar
;
174 struct a_amv_lostack
{
175 struct a_amv_lostack
*as_global_saved
; /* Saved global XXX due to jump */
176 struct a_amv_mac_call_args
*as_amcap
;
177 struct a_amv_lostack
*as_up
; /* Outer context */
178 struct a_amv_var
*as_lopts
;
179 bool_t as_unroll
; /* Unrolling enabled? */
184 struct a_amv_var
*av_link
;
187 char *av_env
; /* Actively managed putenv(3) memory */
189 ui16_t av_flags
; /* enum a_amv_var_flags */
190 char av_name
[n_VFIELD_SIZE(6)];
192 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
194 struct a_amv_var_map
{
197 ui16_t avm_flags
; /* enum a_amv_var_flags */
199 n_CTA(a_AMV_VF__MASK
<= UI16_MAX
, "Enumeration excesses storage datatype");
201 struct a_amv_var_virt
{
204 struct a_amv_var
const *avv_var
;
207 struct a_amv_var_defval
{
210 char const *avdv_value
; /* Only for !BOOL (otherwise plain existence) */
213 struct a_amv_var_carrier
{
214 char const *avc_name
;
217 struct a_amv_var
*avc_var
;
218 struct a_amv_var_map
const *avc_map
;
221 ui8_t avc_special_cat
;
222 /* Numerical parameter name if .avc_special_cat=a_AMV_VSC_POSPAR,
223 * otherwise a enum a_amv_var_special_type */
224 ui16_t avc_special_prop
;
227 /* Include constant make-okey-map.pl output, and the generated version data */
228 #include "gen-version.h"
229 #include "gen-okeys.h"
231 /* The currently active account */
232 static struct a_amv_mac
*a_amv_acc_curr
;
234 static struct a_amv_mac
*a_amv_macs
[a_AMV_PRIME
]; /* TODO dynamically spaced */
236 /* Unroll list of currently running macro stack */
237 static struct a_amv_lostack
*a_amv_lopts
;
239 static struct a_amv_var
*a_amv_vars
[a_AMV_PRIME
]; /* TODO dynamically spaced */
241 /* Global (i.e., non-local) a_AMV_VSC_POSPAR stack */
242 static struct a_amv_pospar a_amv_pospar
;
244 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
245 * TODO today via a static lostack, it should be a field in mailbox, once that
246 * TODO is a real multi-instance object */
247 static struct a_amv_var
*a_amv_folder_hook_lopts
;
249 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
250 static struct a_amv_var
*a_amv_compose_lopts
;
252 /* Lookup for macros/accounts: if newamp is not NULL it will be linked in the
253 * map, if _MF_UNDEF is set a possibly existing entry will be removed (first).
254 * Returns NULL if a lookup failed, or if newamp was set, the found entry in
255 * plain lookup cases or when _UNDEF was performed on a currently active entry
256 * (the entry will have been unlinked, and the _MF_DELETE will be honoured once
257 * the reference count reaches 0), and (*)-1 if an _UNDEF was performed */
258 static struct a_amv_mac
*a_amv_mac_lookup(char const *name
,
259 struct a_amv_mac
*newamp
, enum a_amv_mac_flags amf
);
261 /* `call', `call_if' (and `xcall' via go.c -> c_call()) */
262 static int a_amv_mac_call(void *v
, bool_t silent_nexist
);
264 /* Execute a macro; amcap must reside in LOFI memory */
265 static bool_t
a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
);
267 static void a_amv_mac__finalize(void *vp
);
269 /* User display helpers */
270 static bool_t
a_amv_mac_show(enum a_amv_mac_flags amf
);
272 /* _def() returns error for faulty definitions and already existing * names,
273 * _undef() returns error if a named thing doesn't exist */
274 static bool_t
a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
);
275 static bool_t
a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
);
278 static void a_amv_mac_free(struct a_amv_mac
*amp
);
280 /* Update replay-log */
281 static void a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
282 struct a_amv_var
*oavp
);
283 static void a_amv_lopts_unroll(struct a_amv_var
**avpp
);
285 /* Special cased value string allocation */
286 static char *a_amv_var_copy(char const *str
);
287 static void a_amv_var_free(char *cp
);
289 /* Check for special housekeeping. _VIP_SET_POST and _VIP_CLEAR do not fail
290 * (or propagate errors), _VIP_SET_PRE may and should case abortion */
291 static bool_t
a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
,
292 enum okeys okey
, char const *val
);
294 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
295 static bool_t
a_amv_var_check_nocntrls(char const *val
);
296 static bool_t
a_amv_var_check_num(char const *val
, bool_t posnum
);
298 /* If a variable name begins with a lowercase-character and contains at
299 * least one '@', it is converted to all-lowercase. This is necessary
300 * for lookups of names based on email addresses.
301 * Following the standard, only the part following the last '@' should
302 * be lower-cased, but practice has established otherwise here */
303 static char const *a_amv_var_canonify(char const *vn
);
305 /* Try to reverse lookup an option name to an enum okeys mapping.
306 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
307 static bool_t
a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
,
310 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
311 * Sets .avc_prime; .avc_var is NULL if not found.
312 * Here it is where we care for _I3VAL and _DEFVAL, too.
313 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
314 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
315 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
316 * return FAL0, then! */
317 static bool_t
a_amv_var_lookup(struct a_amv_var_carrier
*avcp
,
320 /* Lookup functions for special category variables, _pospar drives all
321 * positional parameter etc. special categories */
322 static char const *a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
);
323 static char const *a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
);
324 static char const *a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
);
326 /* Set var from .avc_(map|name|hash), return success */
327 static bool_t
a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
330 static bool_t
a_amv_var__putenv(struct a_amv_var_carrier
*avcp
,
331 struct a_amv_var
*avp
);
333 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
334 static bool_t
a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
);
336 static bool_t
a_amv_var__clearenv(char const *name
, char *value
);
338 /* List all variables */
339 static void a_amv_var_show_all(void);
341 static int a_amv_var__show_cmp(void const *s1
, void const *s2
);
343 /* Actually do print one, return number of lines written */
344 static size_t a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
);
346 /* Shared c_set() and c_environ():set impl, return success */
347 static bool_t
a_amv_var_c_set(char **ap
, bool_t issetenv
);
349 static struct a_amv_mac
*
350 a_amv_mac_lookup(char const *name
, struct a_amv_mac
*newamp
,
351 enum a_amv_mac_flags amf
){
352 struct a_amv_mac
*amp
, **ampp
;
354 enum a_amv_mac_flags save_amf
;
358 amf
&= a_AMV_MF_TYPE_MASK
;
359 h
= a_AMV_NAME2HASH(name
);
360 h
= a_AMV_HASH2PRIME(h
);
361 ampp
= &a_amv_macs
[h
];
363 for(amp
= *ampp
; amp
!= NULL
; ampp
= &(*ampp
)->am_next
, amp
= amp
->am_next
){
364 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
&&
365 !strcmp(amp
->am_name
, name
)){
366 if(n_LIKELY((save_amf
& a_AMV_MF_UNDEF
) == 0))
369 *ampp
= amp
->am_next
;
371 if(amp
->am_refcnt
> 0){
372 amp
->am_flags
|= a_AMV_MF_DELETE
;
373 if(n_poption
& n_PO_D_V
)
374 n_err(_("Delayed deletion of currently active %s: %s\n"),
375 (amp
->am_flags
& a_AMV_MF_ACCOUNT
? "account" : "define"),
379 amp
= (struct a_amv_mac
*)-1;
386 ampp
= &a_amv_macs
[h
];
387 newamp
->am_next
= *ampp
;
397 a_amv_mac_call(void *v
, bool_t silent_nexist
){
398 struct a_amv_mac
*amp
;
401 struct n_cmd_arg_ctx
*cacp
;
406 if(cacp
->cac_no
== 0){
407 n_err(_("Synopsis: call(_if)?: name [:<arguments>:]\n"));
408 n_pstate_err_no
= n_ERR_INVAL
;
413 name
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
415 if(n_UNLIKELY(cacp
->cac_no
> a_AMV_POSPAR_MAX
)){
416 n_err(_("Too many arguments to macro `call': %s\n"), name
);
417 n_pstate_err_no
= n_ERR_OVERFLOW
;
419 }else if(n_UNLIKELY((amp
= a_amv_mac_lookup(name
, NULL
, a_AMV_MF_NONE
)
422 n_err(_("Undefined macro called: %s\n"), n_shexp_quote_cp(name
, FAL0
));
423 n_pstate_err_no
= n_ERR_NOENT
;
427 struct a_amv_mac_call_args
*amcap
;
430 argc
= cacp
->cac_no
+ 1;
431 amcap
= n_lofi_alloc(sizeof *amcap
+ (argc
* sizeof *argv
));
432 argv
= (void*)&amcap
[1];
434 for(argc
= 0; (cacp
->cac_arg
= cacp
->cac_arg
->ca_next
) != NULL
; ++argc
)
435 argv
[argc
] = cacp
->cac_arg
->ca_arg
.ca_str
.s
;
438 memset(amcap
, 0, sizeof *amcap
);
439 amcap
->amca_name
= name
;
440 amcap
->amca_amp
= amp
;
442 amcap
->amca_pospar
.app_count
= (ui16_t
)argc
;
443 amcap
->amca_pospar
.app_not_heap
= TRU1
;
444 amcap
->amca_pospar
.app_dat
= argv
;
447 (void)a_amv_mac_exec(amcap
);
456 a_amv_mac_exec(struct a_amv_mac_call_args
*amcap
){
457 struct a_amv_lostack
*losp
;
458 struct a_amv_mac_line
**amlp
;
459 char **args_base
, **args
;
460 struct a_amv_mac
*amp
;
464 amp
= amcap
->amca_amp
;
465 assert(amp
!= NULL
&& amp
!= a_AMV_MACKY_MACK
);
467 /* XXX Unfortunately we yet need to dup the macro lines! :( */
468 args_base
= args
= smalloc(sizeof(*args
) * (amp
->am_line_cnt
+1));
469 for(amlp
= amp
->am_line_dat
; *amlp
!= NULL
; ++amlp
)
470 *(args
++) = sbufdup((*amlp
)->aml_dat
, (*amlp
)->aml_len
);
473 losp
= n_lofi_alloc(sizeof *losp
);
474 losp
->as_global_saved
= a_amv_lopts
;
475 if((losp
->as_amcap
= amcap
)->amca_unroller
== NULL
){
476 losp
->as_up
= losp
->as_global_saved
;
477 losp
->as_lopts
= NULL
;
480 losp
->as_lopts
= *amcap
->amca_unroller
;
482 losp
->as_unroll
= amcap
->amca_lopts_on
;
485 if(amcap
->amca_hook_pre
!= NULL
)
486 n_PS_ROOT_BLOCK((*amcap
->amca_hook_pre
)(amcap
->amca_hook_arg
));
487 rv
= n_go_macro((n_GO_INPUT_NONE
|
488 (amcap
->amca_no_xcall
? n_GO_INPUT_NO_XCALL
: 0)),
489 amp
->am_name
, args_base
, &a_amv_mac__finalize
, losp
);
495 a_amv_mac__finalize(void *vp
){
496 struct a_amv_mac
*amp
;
497 struct a_amv_mac_call_args
*amcap
;
498 struct a_amv_lostack
*losp
;
502 a_amv_lopts
= losp
->as_global_saved
;
504 amcap
= losp
->as_amcap
;
506 if(!amcap
->amca_pospar
.app_not_heap
&& amcap
->amca_pospar
.app_maxcount
> 0){
509 for(i
= amcap
->amca_pospar
.app_maxcount
; i
-- != 0;)
510 n_free(n_UNCONST(amcap
->amca_pospar
.app_dat
[i
]));
511 n_free(amcap
->amca_pospar
.app_dat
);
514 if(amcap
->amca_unroller
== NULL
){
515 if(losp
->as_lopts
!= NULL
)
516 a_amv_lopts_unroll(&losp
->as_lopts
);
518 *amcap
->amca_unroller
= losp
->as_lopts
;
520 if(amcap
->amca_ps_hook_mask
)
521 n_pstate
&= ~n_PS_HOOK_MASK
;
523 if((amp
= amcap
->amca_amp
) != a_AMV_MACKY_MACK
&& amp
!= NULL
&&
524 --amp
->am_refcnt
== 0 && (amp
->am_flags
& a_AMV_MF_DELETE
))
533 a_amv_mac_show(enum a_amv_mac_flags amf
){
534 size_t lc
, mc
, ti
, i
;
542 if((fp
= Ftmp(NULL
, "deflist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
544 n_perr(_("Can't create temporary file for `define' or `account' listing"),
549 amf
&= a_AMV_MF_TYPE_MASK
;
550 typestr
= (amf
& a_AMV_MF_ACCOUNT
) ? "account" : "define";
552 for(lc
= mc
= ti
= 0; ti
< a_AMV_PRIME
; ++ti
){
553 struct a_amv_mac
*amp
;
555 for(amp
= a_amv_macs
[ti
]; amp
!= NULL
; amp
= amp
->am_next
){
556 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
557 struct a_amv_mac_line
**amlpp
;
564 fprintf(fp
, "%s %s {\n", typestr
, amp
->am_name
);
565 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++lc
, ++amlpp
){
566 for(i
= (*amlpp
)->aml_prespc
; i
> 0; --i
)
568 fputs((*amlpp
)->aml_dat
, fp
);
577 page_or_print(fp
, lc
);
579 rv
= (ferror(fp
) == 0);
587 a_amv_mac_def(char const *name
, enum a_amv_mac_flags amf
){
589 ui32_t line_cnt
, maxlen
;
591 struct linelist
*ll_next
;
592 struct a_amv_mac_line
*ll_amlp
;
593 } *llp
, *ll_head
, *ll_tail
;
594 union {size_t s
; int i
; ui32_t ui
; size_t l
;} n
;
595 struct a_amv_mac
*amp
;
599 memset(&line
, 0, sizeof line
);
603 /* TODO We should have our input state machine which emits Line events,
604 * TODO and hook different consumers dependent on our content, as stated
605 * TODO in i think lex_input; */
606 /* Read in the lines which form the macro content */
607 for(ll_tail
= ll_head
= NULL
, line_cnt
= maxlen
= 0;;){
611 n
.i
= n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, n_empty
,
612 &line
.s
, &line
.l
, NULL
, NULL
);
616 n_err(_("Unterminated %s definition: %s\n"),
617 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
621 /* Trim WS, remember amount of leading spaces for display purposes */
622 for(cp
= line
.s
, leaspc
= 0; n
.ui
> 0; ++cp
, --n
.ui
)
624 leaspc
= (leaspc
+ 8u) & ~7u;
629 for(; n
.ui
> 0 && spacechar(cp
[n
.ui
- 1]); --n
.ui
)
634 maxlen
= n_MAX(maxlen
, n
.ui
);
637 /* Is is the closing brace? */
641 if(n_LIKELY(++line_cnt
< UI32_MAX
)){
642 struct a_amv_mac_line
*amlp
;
644 llp
= n_autorec_alloc(sizeof *llp
);
648 ll_tail
->ll_next
= llp
;
651 llp
->ll_amlp
= amlp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line
,
653 amlp
->aml_len
= n
.ui
-1;
654 amlp
->aml_prespc
= leaspc
;
655 memcpy(amlp
->aml_dat
, cp
, n
.ui
);
657 n_err(_("Too much content in %s definition: %s\n"),
658 (amf
& a_AMV_MF_ACCOUNT
? "account" : "macro"), name
);
663 /* Create the new macro */
664 n
.s
= strlen(name
) +1;
665 amp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac
, am_name
) + n
.s
);
667 amp
->am_maxlen
= maxlen
;
668 amp
->am_line_cnt
= line_cnt
;
671 amp
->am_lopts
= NULL
;
672 memcpy(amp
->am_name
, name
, n
.s
);
674 struct a_amv_mac_line
**amlpp
;
676 amp
->am_line_dat
= amlpp
= smalloc(sizeof(*amlpp
) * ++line_cnt
);
677 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
678 *amlpp
++ = llp
->ll_amlp
;
682 /* Create entry, replace a yet existing one as necessary */
683 a_amv_mac_lookup(name
, amp
, amf
| a_AMV_MF_UNDEF
);
692 for(llp
= ll_head
; llp
!= NULL
; llp
= llp
->ll_next
)
695 free(amp
->am_line_dat
);
702 a_amv_mac_undef(char const *name
, enum a_amv_mac_flags amf
){
703 struct a_amv_mac
*amp
;
709 if(n_LIKELY(name
[0] != '*' || name
[1] != '\0')){
710 if((amp
= a_amv_mac_lookup(name
, NULL
, amf
| a_AMV_MF_UNDEF
)) == NULL
){
711 n_err(_("%s not defined: %s\n"),
712 (amf
& a_AMV_MF_ACCOUNT
? "Account" : "Macro"), name
);
716 struct a_amv_mac
**ampp
, *lamp
;
718 for(ampp
= a_amv_macs
; PTRCMP(ampp
, <, &a_amv_macs
[n_NELEM(a_amv_macs
)]);
720 for(lamp
= NULL
, amp
= *ampp
; amp
!= NULL
;){
721 if((amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == amf
){
722 /* xxx Expensive but rare: be simple */
723 a_amv_mac_lookup(amp
->am_name
, NULL
, amf
| a_AMV_MF_UNDEF
);
724 amp
= (lamp
== NULL
) ? *ampp
: lamp
->am_next
;
736 a_amv_mac_free(struct a_amv_mac
*amp
){
737 struct a_amv_mac_line
**amlpp
;
740 for(amlpp
= amp
->am_line_dat
; *amlpp
!= NULL
; ++amlpp
)
742 free(amp
->am_line_dat
);
748 a_amv_lopts_add(struct a_amv_lostack
*alp
, char const *name
,
749 struct a_amv_var
*oavp
){
750 struct a_amv_var
*avp
;
754 /* Propagate unrolling up the stack, as necessary */
759 if((alp
= alp
->as_up
) == NULL
)
763 /* Check whether this variable is handled yet */
764 for(avp
= alp
->as_lopts
; avp
!= NULL
; avp
= avp
->av_link
)
765 if(!strcmp(avp
->av_name
, name
))
768 nl
= strlen(name
) +1;
769 vl
= (oavp
!= NULL
) ? strlen(oavp
->av_value
) +1 : 0;
770 avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + nl
+ vl
);
771 avp
->av_link
= alp
->as_lopts
;
773 memcpy(avp
->av_name
, name
, nl
);
775 avp
->av_value
= NULL
;
781 avp
->av_value
= &avp
->av_name
[nl
];
782 avp
->av_flags
= oavp
->av_flags
;
783 memcpy(avp
->av_value
, oavp
->av_value
, vl
);
785 avp
->av_env
= (oavp
->av_env
== NULL
) ? NULL
: sstrdup(oavp
->av_env
);
793 a_amv_lopts_unroll(struct a_amv_var
**avpp
){
794 struct a_amv_lostack
*save_alp
;
795 struct a_amv_var
*x
, *avp
;
801 save_alp
= a_amv_lopts
;
806 n_PS_ROOT_BLOCK(n_var_vset(x
->av_name
, (uintptr_t)x
->av_value
));
809 a_amv_lopts
= save_alp
;
814 a_amv_var_copy(char const *str
){
820 news
= n_UNCONST(n_empty
);
821 else if(str
[1] == '\0'){
823 news
= n_UNCONST(n_1
);
824 else if(str
[0] == '0')
825 news
= n_UNCONST(n_0
);
828 }else if(str
[2] == '\0' && str
[0] == '-' && str
[1] == '1')
829 news
= n_UNCONST(n_m1
);
832 len
= strlen(str
) +1;
834 memcpy(news
, str
, len
);
841 a_amv_var_free(char *cp
){
843 if(cp
[0] != '\0' && cp
!= n_0
&& cp
!= n_1
&& cp
!= n_m1
)
849 a_amv_var_check_vips(enum a_amv_var_vip_mode avvm
, enum okeys okey
,
856 if(avvm
== a_AMV_VIP_SET_PRE
){
861 /* Note this gets called from main.c during initialization, and they
862 * simply set this to pw_dir as a fallback: don't verify _that_ call.
864 if(!(n_pstate
& n_PS_ROOT
) && !n_is_dir(val
, TRU1
)){
865 n_err(_("$HOME is not a directory or not accessible: %s\n"),
866 n_shexp_quote_cp(val
, FAL0
));
872 if(!n_is_dir(val
, TRU1
)){
873 n_err(_("$TMPDIR is not a directory or not accessible: %s\n"),
874 n_shexp_quote_cp(val
, FAL0
));
882 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
883 if(uib
& ~0777u){ /* (is valid _VF_POSNUM) */
884 n_err(_("Invalid *umask* setting: %s\n"), val
);
890 }else if(avvm
== a_AMV_VIP_SET_POST
){
895 n_poption
|= n_PO_DEBUG
;
898 /* Invalidate any resolved folder then, too
901 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
906 x_b
= x
= n_autorec_alloc(strlen(val
) +1);
907 while((c
= *val
++) != '\0')
911 n_PS_ROOT_BLOCK(ok_vset(ifs_ws
, x_b
));
913 #ifdef HAVE_SETLOCALE
921 n_poption
|= n_PO_MEMDEBUG
;
923 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
924 if(!(n_pstate
& n_PS_ROOT
))
925 n_PS_ROOT_BLOCK(ok_bset(posix
));
927 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
928 if(!(n_pstate
& n_PS_ROOT
))
929 n_PS_ROOT_BLOCK(ok_bset(POSIXLY_CORRECT
));
931 case ok_b_skipemptybody
:
932 n_poption
|= n_PO_E_FLAG
;
934 case ok_b_typescript_mode
:
935 ok_bset(colour_disable
);
936 ok_bset(line_editor_disable
);
937 if(!(n_psonce
& n_PSO_STARTED
))
938 ok_bset(termcap_disable
);
944 n_idec_ui64_cp(&uib
, val
, 0, NULL
);
949 n_poption
|= (n_poption
& n_PO_VERB
) ? n_PO_VERBVERB
: n_PO_VERB
;
957 n_poption
&= ~n_PO_DEBUG
;
960 /* Invalidate any resolved folder then, too
963 n_PS_ROOT_BLOCK(ok_vclear(folder_resolved
));
966 n_poption
&= ~n_PO_MEMDEBUG
;
968 case ok_b_POSIXLY_CORRECT
: /* <-> *posix* */
969 if(!(n_pstate
& n_PS_ROOT
))
970 n_PS_ROOT_BLOCK(ok_bclear(posix
));
972 case ok_b_posix
: /* <-> $POSIXLY_CORRECT */
973 if(!(n_pstate
& n_PS_ROOT
))
974 n_PS_ROOT_BLOCK(ok_bclear(POSIXLY_CORRECT
));
976 case ok_b_skipemptybody
:
977 n_poption
&= ~n_PO_E_FLAG
;
980 n_poption
&= ~(n_PO_VERB
| n_PO_VERBVERB
);
989 a_amv_var_check_nocntrls(char const *val
){
993 while((c
= *val
++) != '\0')
1001 a_amv_var_check_num(char const *val
, bool_t posnum
){
1002 /* TODO The internal/environment variables which are num= or posnum= should
1003 * TODO gain special lookup functions, or the return should be void* and
1004 * TODO castable to integer; i.e. no more strtoX() should be needed.
1005 * TODO I.e., the result of this function should instead be stored */
1011 if(*val
!= '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
1013 enum n_idec_state ids
;
1015 ids
= n_idec_cp(&uib
, val
, 0,
1016 (posnum
? n_IDEC_MODE_SIGNED_TYPE
: n_IDEC_MODE_NONE
), NULL
);
1017 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1018 ) != n_IDEC_STATE_CONSUMED
)
1020 /* TODO Unless we store integers we need to look and forbid, because
1021 * TODO callee may not be able to swallow, e.g., "-1" */
1022 if(posnum
&& (ids
& n_IDEC_STATE_SEEN_MINUS
))
1030 a_amv_var_canonify(char const *vn
){
1032 if(!upperchar(*vn
)){
1035 for(vp
= vn
; *vp
!= '\0' && *vp
!= '@'; ++vp
)
1037 vn
= (*vp
== '@') ? i_strdup(vn
) : vn
;
1044 a_amv_var_revlookup(struct a_amv_var_carrier
*avcp
, char const *name
){
1046 struct a_amv_var_map
const *avmp
;
1050 /* It may be a special a.k.a. macro-local or one-letter parameter */
1052 if(n_UNLIKELY(digitchar(c
))){
1053 /* (Inline dec. atoi, ugh) */
1054 for(j
= (ui8_t
)c
- '0', i
= 1;; ++i
){
1059 goto jno_special_param
;
1060 j
= j
* 10 + (ui8_t
)c
- '0';
1062 if(j
<= a_AMV_POSPAR_MAX
){
1063 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR
;
1064 goto jspecial_param
;
1066 }else if(n_UNLIKELY(name
[1] == '\0')){
1070 avcp
->avc_special_cat
= a_AMV_VSC_GLOBAL
;
1071 j
= (c
== '?') ? a_AMV_VST_QM
: a_AMV_VST_EM
;
1072 goto jspecial_param
;
1076 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1078 goto jspecial_param
;
1080 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1082 goto jspecial_param
;
1084 avcp
->avc_special_cat
= a_AMV_VSC_POSPAR_ENV
;
1085 j
= a_AMV_VST_NOSIGN
;
1086 goto jspecial_param
;
1092 avcp
->avc_special_cat
= a_AMV_VSC_MULTIPLEX
;
1094 goto jspecial_param
;
1097 /* Normal reverse lookup, walk over the hashtable */
1099 avcp
->avc_special_cat
= a_AMV_VSC_NONE
;
1100 avcp
->avc_name
= name
= a_amv_var_canonify(name
);
1101 avcp
->avc_hash
= hash
= a_AMV_NAME2HASH(name
);
1103 for(i
= hash
% a_AMV_VAR_REV_PRIME
, j
= 0; j
<= a_AMV_VAR_REV_LONGEST
; ++j
){
1106 if((x
= a_amv_var_revmap
[i
]) == a_AMV_VAR_REV_ILL
)
1109 avmp
= &a_amv_var_map
[x
];
1110 if(avmp
->avm_hash
== hash
&&
1111 !strcmp(&a_amv_var_names
[avmp
->avm_keyoff
], name
)){
1112 avcp
->avc_map
= avmp
;
1113 avcp
->avc_okey
= (enum okeys
)x
;
1117 if(++i
== a_AMV_VAR_REV_PRIME
){
1118 #ifdef a_AMV_VAR_REV_WRAPAROUND
1125 avcp
->avc_map
= NULL
;
1129 return (avcp
!= NULL
);
1131 /* All these are mapped to *--special-param* */
1133 avcp
->avc_name
= name
;
1134 avcp
->avc_special_prop
= (ui16_t
)j
;
1135 avmp
= &a_amv_var_map
[a_AMV_VAR__SPECIAL_PARAM_MAP_IDX
];
1136 avcp
->avc_hash
= avmp
->avm_hash
;
1137 avcp
->avc_map
= avmp
;
1138 avcp
->avc_okey
= ok_v___special_param
;
1143 a_amv_var_lookup(struct a_amv_var_carrier
*avcp
, bool_t i3val_nonew
){
1146 struct a_amv_var_map
const *avmp
;
1147 struct a_amv_var
*avp
;
1151 struct a_amv_var
**avpp
, *lavp
;
1153 avpp
= &a_amv_vars
[avcp
->avc_prime
= a_AMV_HASH2PRIME(avcp
->avc_hash
)];
1155 for(lavp
= NULL
, avp
= *avpp
; avp
!= NULL
; lavp
= avp
, avp
= avp
->av_link
)
1156 if(!strcmp(avp
->av_name
, avcp
->avc_name
)){
1157 /* Relink as head, hope it "sorts on usage" over time.
1158 * _clear() relies on this behaviour */
1160 lavp
->av_link
= avp
->av_link
;
1161 avp
->av_link
= *avpp
;
1168 /* If this is not an assembled variable we need to consider some special
1169 * initialization cases and eventually create the variable anew */
1170 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1171 /* Does it have an import-from-environment flag? */
1172 if(n_UNLIKELY((avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
)) != 0)){
1173 if(n_LIKELY((cp
= getenv(avcp
->avc_name
)) != NULL
)){
1174 /* May be better not to use that one, though? */
1175 bool_t isempty
, isbltin
;
1177 isempty
= (*cp
== '\0' &&
1178 (avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) != 0);
1179 isbltin
= ((avmp
->avm_flags
& (a_AMV_VF_I3VAL
| a_AMV_VF_DEFVAL
)
1182 if(n_UNLIKELY(isempty
)){
1185 }else if(n_LIKELY(*cp
!= '\0')){
1186 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) &&
1187 !a_amv_var_check_nocntrls(cp
))){
1188 n_err(_("Ignoring environment, control characters "
1189 "invalid in variable: %s\n"), avcp
->avc_name
);
1192 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1193 !a_amv_var_check_num(cp
, FAL0
))){
1194 n_err(_("Environment variable value not a number "
1195 "or out of range: %s\n"), avcp
->avc_name
);
1198 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1199 !a_amv_var_check_num(cp
, TRU1
))){
1200 n_err(_("Environment variable value not a number, "
1201 "negative or out of range: %s\n"), avcp
->avc_name
);
1210 /* A first-time init switch is to be handled now and here */
1211 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_I3VAL
) != 0)){
1212 static struct a_amv_var_defval
const **arr
,
1213 *arr_base
[a_AMV_VAR_I3VALS_CNT
+1];
1217 arr
[i
= a_AMV_VAR_I3VALS_CNT
] = NULL
;
1219 arr
[i
] = &a_amv_var_i3vals
[i
];
1222 for(i
= 0; arr
[i
] != NULL
; ++i
)
1223 if(arr
[i
]->avdv_okey
== avcp
->avc_okey
){
1224 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1225 : arr
[i
]->avdv_value
;
1226 /* Remove this entry, hope entire block becomes no-op asap */
1228 arr
[i
] = arr
[i
+ 1];
1229 while(arr
[i
++] != NULL
);
1233 if(i3val_nonew
== TRUM1
)
1234 avp
= (struct a_amv_var
*)-1;
1239 /* The virtual variables */
1240 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIRT
) != 0)){
1241 for(i
= 0; i
< a_AMV_VAR_VIRTS_CNT
; ++i
)
1242 if(a_amv_var_virts
[i
].avv_okey
== avcp
->avc_okey
){
1243 avp
= n_UNCONST(a_amv_var_virts
[i
].avv_var
);
1249 /* Place this last because once it is set first the variable will never
1250 * be removed again and thus match in the first block above */
1252 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0){
1253 for(i
= 0; i
< a_AMV_VAR_DEFVALS_CNT
; ++i
)
1254 if(a_amv_var_defvals
[i
].avdv_okey
== avcp
->avc_okey
){
1255 cp
= (avmp
->avm_flags
& a_AMV_VF_BOOL
) ? n_empty
1256 : a_amv_var_defvals
[i
].avdv_value
;
1265 avcp
->avc_var
= avp
;
1267 return (avp
!= NULL
);
1270 /* E.g., $TMPDIR may be set to non-existent, so we need to be able to catch
1271 * that and redirect to a possible default value */
1272 if((avmp
->avm_flags
& a_AMV_VF_VIP
) &&
1273 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, cp
)){
1275 if(avmp
->avm_flags
& (a_AMV_VF_IMPORT
| a_AMV_VF_ENV
))
1276 unsetenv(avcp
->avc_name
);
1278 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0)
1282 struct a_amv_var
**avpp
;
1285 l
= strlen(avcp
->avc_name
) +1;
1287 avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
) + l
);
1288 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1290 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1294 avp
->av_flags
= avmp
->avm_flags
;
1295 avp
->av_value
= a_amv_var_copy(cp
);
1297 if(avp
->av_flags
& a_AMV_VF_ENV
)
1298 a_amv_var__putenv(avcp
, avp
);
1299 if(avmp
->avm_flags
& a_AMV_VF_VIP
)
1300 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, cp
);
1306 a_amv_var_vsc_global(struct a_amv_var_carrier
*avcp
){
1310 struct a_amv_var_map
const *avmp
;
1313 /* Not function local, TODO but lazy evaluted for now */
1314 if(avcp
->avc_special_prop
== a_AMV_VST_QM
){
1315 avmp
= &a_amv_var_map
[a_AMV_VAR__QM_MAP_IDX
];
1316 avcp
->avc_okey
= ok_v___qm
;
1317 ep
= &n_pstate_ex_no
;
1319 avmp
= &a_amv_var_map
[a_AMV_VAR__EM_MAP_IDX
];
1320 avcp
->avc_okey
= ok_v___em
;
1321 ep
= &n_pstate_err_no
;
1324 /* XXX Oh heaven, we are responsible to ensure that $?/! is up-to-date
1325 * TODO we could num=1 ok_v___[qe]m, but the thing is still a string
1326 * TODO and thus conversion takes places over and over again; also
1327 * TODO for now that would have to occur before we set _that_ value
1328 * TODO so let's special treat it until we store ints as such */
1330 case 0: rv
= n_0
; break;
1331 case 1: rv
= n_1
; break;
1333 snprintf(itoabuf
, sizeof itoabuf
, "%d", *ep
);
1337 n_PS_ROOT_BLOCK(n_var_okset(avcp
->avc_okey
, (uintptr_t)rv
));
1339 avcp
->avc_hash
= avmp
->avm_hash
;
1340 avcp
->avc_map
= avmp
;
1341 rv
= a_amv_var_lookup(avcp
, TRU1
) ? avcp
->avc_var
->av_value
: NULL
;
1347 a_amv_var_vsc_multiplex(struct a_amv_var_carrier
*avcp
){
1354 i
= strlen(rv
= &avcp
->avc_name
[1]);
1356 /* ERR, ERRDOC, ERRNAME, plus *-NAME variants */
1357 if(rv
[0] == 'E' && i
>= 3 && rv
[1] == 'R' && rv
[2] == 'R'){
1359 e
= n_pstate_err_no
;
1361 }else if(rv
[3] == '-'){
1362 e
= n_err_from_name(&rv
[4]);
1365 case 0: rv
= n_0
; break;
1366 case 1: rv
= n_1
; break;
1368 snprintf(itoabuf
, sizeof itoabuf
, "%d", e
);
1369 rv
= savestr(itoabuf
); /* XXX yet, cannot do numbers */
1374 if(!memcmp(&rv
[3], "DOC", 3)){
1377 case '\0': e
= n_pstate_err_no
; break;
1378 case '-': e
= n_err_from_name(&rv
[1]); break;
1381 rv
= n_err_to_doc(e
);
1383 }else if(i
>= 7 && !memcmp(&rv
[3], "NAME", 4)){
1386 case '\0': e
= n_pstate_err_no
; break;
1387 case '-': e
= n_err_from_name(&rv
[1]); break;
1390 rv
= n_err_to_name(e
);
1404 a_amv_var_vsc_pospar(struct a_amv_var_carrier
*avcp
){
1407 char const *rv
, **argv
;
1412 /* If in a macro/xy.. */
1413 if(a_amv_lopts
!= NULL
){
1415 struct a_amv_mac_call_args
*amcap
;
1417 amcap
= a_amv_lopts
->as_amcap
;
1418 argc
= amcap
->amca_pospar
.app_count
;
1419 argv
= amcap
->amca_pospar
.app_dat
;
1420 argv
+= amcap
->amca_pospar
.app_idx
;
1422 /* ..in a `call'ed macro only, to be exact. Or in a_AMV_MACKY_MACK */
1423 if(!(ismacky
= (amcap
->amca_amp
== a_AMV_MACKY_MACK
)) &&
1424 (amcap
->amca_ps_hook_mask
||
1425 (assert(amcap
->amca_amp
!= NULL
),
1426 (amcap
->amca_amp
->am_flags
& a_AMV_MF_TYPE_MASK
1427 ) == a_AMV_MF_ACCOUNT
)))
1430 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1431 if(avcp
->avc_special_prop
> 0){
1432 if(argc
>= avcp
->avc_special_prop
)
1433 rv
= argv
[avcp
->avc_special_prop
- 1];
1435 rv
= amcap
->amca_name
;
1437 rv
= (a_amv_lopts
->as_up
!= NULL
1438 ? a_amv_lopts
->as_up
->as_amcap
->amca_name
: n_empty
);
1441 /* MACKY_MACK doesn't know about [*@#] */
1442 /*else*/ if(ismacky
){
1443 if(n_poption
& n_PO_D_V
)
1444 n_err(_("Cannot use $*/$@/$# in this context: %s\n"),
1445 n_shexp_quote_cp(avcp
->avc_name
, FAL0
));
1449 argc
= a_amv_pospar
.app_count
;
1450 argv
= a_amv_pospar
.app_dat
;
1451 argv
+= a_amv_pospar
.app_idx
;
1453 if(avcp
->avc_special_cat
== a_AMV_VSC_POSPAR
){
1454 if(avcp
->avc_special_prop
> 0){
1455 if(argc
>= avcp
->avc_special_prop
)
1456 rv
= argv
[avcp
->avc_special_prop
- 1];
1463 switch(avcp
->avc_special_prop
){ /* XXX OPTIMIZE */
1464 case a_AMV_VST_STAR
:
1466 for(i
= j
= 0; i
< argc
; ++i
)
1467 j
+= strlen(argv
[i
]) + 1;
1473 rv
= cp
= n_autorec_alloc(j
);
1474 for(i
= j
= 0; i
< argc
; ++i
){
1475 j
= strlen(argv
[i
]);
1476 memcpy(cp
, argv
[i
], j
);
1483 case a_AMV_VST_NOSIGN
:{
1486 rv
= cp
= n_autorec_alloc(sizeof("65535"));
1487 snprintf(cp
, sizeof("65535"), "%hu", argc
);
1499 a_amv_var_set(struct a_amv_var_carrier
*avcp
, char const *value
,
1501 struct a_amv_var
*avp
;
1503 struct a_amv_var_map
const *avmp
;
1508 rv
= a_amv_var_clear(avcp
, force_env
);
1512 if((avmp
= avcp
->avc_map
) != NULL
){
1515 /* Validity checks */
1516 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_RDONLY
) != 0 &&
1517 !(n_pstate
& n_PS_ROOT
))){
1518 value
= N_("Variable is read-only: %s\n");
1521 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOTEMPTY
) && *value
== '\0')){
1522 value
= N_("Variable must not be empty: %s\n");
1525 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NOCNTRLS
) != 0 &&
1526 !a_amv_var_check_nocntrls(value
))){
1527 value
= N_("Variable forbids control characters: %s\n");
1530 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NUM
) &&
1531 !a_amv_var_check_num(value
, FAL0
))){
1532 value
= N_("Variable value not a number or out of range: %s\n");
1535 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_POSNUM
) &&
1536 !a_amv_var_check_num(value
, TRU1
))){
1537 value
= _("Variable value not a number, negative, "
1538 "or out of range: %s\n");
1541 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_IMPORT
) != 0 &&
1542 !(n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_ROOT
))){
1543 value
= N_("Variable cannot be set in a resource file: %s\n");
1547 /* Any more complicated inter-dependency? */
1548 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1549 !a_amv_var_check_vips(a_AMV_VIP_SET_PRE
, avcp
->avc_okey
, value
))){
1550 value
= N_("Assignment of variable aborted: %s\n");
1552 n_err(V_(value
), avcp
->avc_name
);
1556 /* Transformations */
1557 if(n_UNLIKELY(avmp
->avm_flags
& a_AMV_VF_LOWER
)){
1560 oval
= savestr(value
);
1562 for(; (c
= *oval
) != '\0'; ++oval
)
1563 *oval
= lowerconv(c
);
1568 a_amv_var_lookup(avcp
, TRU1
);
1570 /* Don't care what happens later on, store this in the unroll list */
1571 if(a_amv_lopts
!= NULL
&&
1572 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1573 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1575 if((avp
= avcp
->avc_var
) == NULL
){
1576 struct a_amv_var
**avpp
;
1579 l
= strlen(avcp
->avc_name
) +1;
1580 avcp
->avc_var
= avp
= smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var
, av_name
1582 avp
->av_link
= *(avpp
= &a_amv_vars
[avcp
->avc_prime
]);
1587 memcpy(avp
->av_name
, avcp
->avc_name
, l
);
1588 avp
->av_flags
= (avmp
!= NULL
) ? avmp
->avm_flags
: 0;
1589 oval
= n_UNCONST(n_empty
);
1591 oval
= avp
->av_value
;
1594 avp
->av_value
= a_amv_var_copy(value
);
1596 /* Via `set' etc. the user may give even boolean options non-boolean
1597 * values, ignore that and force boolean */
1598 if(avp
->av_flags
& a_AMV_VF_BOOL
){
1599 if(!(n_pstate
& n_PS_ROOT
) && (n_poption
& n_PO_D_VV
) &&
1601 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1602 avcp
->avc_name
, value
);
1603 avp
->av_value
= n_UNCONST(n_1
);
1605 avp
->av_value
= a_amv_var_copy(value
);
1608 if(force_env
&& !(avp
->av_flags
& a_AMV_VF_ENV
))
1609 avp
->av_flags
|= a_AMV_VF_LINKED
;
1610 if(avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
))
1611 rv
= a_amv_var__putenv(avcp
, avp
);
1612 if(avp
->av_flags
& a_AMV_VF_VIP
)
1613 a_amv_var_check_vips(a_AMV_VIP_SET_POST
, avcp
->avc_okey
, value
);
1614 a_amv_var_free(oval
);
1621 a_amv_var__putenv(struct a_amv_var_carrier
*avcp
, struct a_amv_var
*avp
){
1629 rv
= (setenv(avcp
->avc_name
, avp
->av_value
, 1) == 0);
1631 cp
= sstrdup(savecatsep(avcp
->avc_name
, '=', avp
->av_value
));
1633 if((rv
= (putenv(cp
) == 0))){
1636 if((ocp
= avp
->av_env
) != NULL
)
1647 a_amv_var_clear(struct a_amv_var_carrier
*avcp
, bool_t force_env
){
1648 struct a_amv_var
**avpp
, *avp
;
1649 struct a_amv_var_map
const *avmp
;
1655 if(n_LIKELY((avmp
= avcp
->avc_map
) != NULL
)){
1656 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_NODEL
) != 0 &&
1657 !(n_pstate
& n_PS_ROOT
))){
1658 n_err(_("Variable may not be unset: %s\n"), avcp
->avc_name
);
1661 if(n_UNLIKELY((avmp
->avm_flags
& a_AMV_VF_VIP
) != 0 &&
1662 !a_amv_var_check_vips(a_AMV_VIP_CLEAR
, avcp
->avc_okey
, NULL
))){
1663 n_err(_("Clearance of variable aborted: %s\n"), avcp
->avc_name
);
1670 if(n_UNLIKELY(!a_amv_var_lookup(avcp
, TRUM1
))){
1673 rv
= a_amv_var__clearenv(avcp
->avc_name
, NULL
);
1674 }else if(!(n_pstate
& (n_PS_ROOT
| n_PS_ROBOT
)) && (n_poption
& n_PO_D_V
))
1675 n_err(_("Can't unset undefined variable: %s\n"), avcp
->avc_name
);
1677 }else if(avcp
->avc_var
== (struct a_amv_var
*)-1){
1678 avcp
->avc_var
= NULL
;
1684 if(a_amv_lopts
!= NULL
&&
1685 (avmp
== NULL
|| !(avmp
->avm_flags
& a_AMV_VF_NOLOPTS
)))
1686 a_amv_lopts_add(a_amv_lopts
, avcp
->avc_name
, avcp
->avc_var
);
1688 avp
= avcp
->avc_var
;
1689 avcp
->avc_var
= NULL
;
1690 avpp
= &a_amv_vars
[avcp
->avc_prime
];
1691 assert(*avpp
== avp
); /* (always listhead after lookup()) */
1692 *avpp
= (*avpp
)->av_link
;
1700 envval
= avp
->av_env
;
1702 if((avp
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)) || envval
!= NULL
)
1703 rv
= a_amv_var__clearenv(avp
->av_name
, envval
);
1705 a_amv_var_free(avp
->av_value
);
1708 /* XXX Fun part, extremely simple-minded for now: if this variable has
1709 * XXX a default value, immediately reinstantiate it! TODO Heh? */
1710 if(n_UNLIKELY(avmp
!= NULL
&& (avmp
->avm_flags
& a_AMV_VF_DEFVAL
) != 0))
1711 a_amv_var_lookup(avcp
, TRU1
);
1718 a_amv_var__clearenv(char const *name
, char *value
){
1720 extern char **environ
;
1732 for(ecpp
= environ
; *ecpp
!= NULL
; ++ecpp
)
1737 while(*ecpp
++ != NULL
);
1747 a_amv_var_show_all(void){
1748 struct n_string msg
, *msgp
;
1751 struct a_amv_var
*avp
;
1752 char const **vacp
, **cap
;
1755 if((fp
= Ftmp(NULL
, "setlist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1756 n_perr(_("Can't create temporary file for `set' listing"), 0);
1760 /* We need to instantiate first-time-inits and default values here, so that
1761 * they will be regular members of our _vars[] table */
1762 for(i
= a_AMV_VAR_I3VALS_CNT
; i
-- > 0;)
1763 n_var_oklook(a_amv_var_i3vals
[i
].avdv_okey
);
1764 for(i
= a_AMV_VAR_DEFVALS_CNT
; i
-- > 0;)
1765 n_var_oklook(a_amv_var_defvals
[i
].avdv_okey
);
1767 for(no
= i
= 0; i
< a_AMV_PRIME
; ++i
)
1768 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1770 no
+= a_AMV_VAR_VIRTS_CNT
;
1772 vacp
= n_autorec_alloc(no
* sizeof(*vacp
));
1774 for(cap
= vacp
, i
= 0; i
< a_AMV_PRIME
; ++i
)
1775 for(avp
= a_amv_vars
[i
]; avp
!= NULL
; avp
= avp
->av_link
)
1776 *cap
++ = avp
->av_name
;
1777 for(i
= a_AMV_VAR_VIRTS_CNT
; i
-- > 0;)
1778 *cap
++ = a_amv_var_virts
[i
].avv_var
->av_name
;
1781 qsort(vacp
, no
, sizeof *vacp
, &a_amv_var__show_cmp
);
1784 msgp
= n_string_reserve(n_string_creat(msgp
), 80);
1785 for(i
= 0, cap
= vacp
; no
!= 0; ++cap
, --no
)
1786 i
+= a_amv_var_show(*cap
, fp
, msgp
);
1789 page_or_print(fp
, i
);
1796 a_amv_var__show_cmp(void const *s1
, void const *s2
){
1800 rv
= strcmp(*(char**)n_UNCONST(s1
), *(char**)n_UNCONST(s2
));
1806 a_amv_var_show(char const *name
, FILE *fp
, struct n_string
*msgp
){
1807 struct a_amv_var_carrier avc
;
1813 msgp
= n_string_trunc(msgp
, 0);
1816 a_amv_var_revlookup(&avc
, name
);
1817 isset
= a_amv_var_lookup(&avc
, FAL0
);
1819 if(n_poption
& n_PO_D_V
){
1820 if(avc
.avc_map
== NULL
){
1821 msgp
= n_string_push_cp(msgp
, "#assembled variable with value");
1828 {a_AMV_VF_VIRT
, "virtual"},
1829 {a_AMV_VF_RDONLY
, "read-only"},
1830 {a_AMV_VF_NODEL
, "nodelete"},
1831 {a_AMV_VF_NOTEMPTY
, "notempty"},
1832 {a_AMV_VF_NOCNTRLS
, "no-control-chars"},
1833 {a_AMV_VF_NUM
, "number"},
1834 {a_AMV_VF_POSNUM
, "positive-number"},
1835 {a_AMV_VF_IMPORT
, "import-environ-first\0"},
1836 {a_AMV_VF_ENV
, "sync-environ"},
1837 {a_AMV_VF_I3VAL
, "initial-value"},
1838 {a_AMV_VF_DEFVAL
, "default-value"},
1839 {a_AMV_VF_LINKED
, "`environ' link"}
1842 for(tp
= tbase
; PTRCMP(tp
, <, &tbase
[n_NELEM(tbase
)]); ++tp
)
1843 if(isset
? (avc
.avc_var
->av_flags
& tp
->flag
)
1844 : (avc
.avc_map
->avm_flags
& tp
->flag
)){
1845 msgp
= n_string_push_c(msgp
, (i
++ == 0 ? '#' : ','));
1846 msgp
= n_string_push_cp(msgp
, tp
->msg
);
1850 msgp
= n_string_push_cp(msgp
, "\n ");
1853 if(!isset
|| (avc
.avc_var
->av_flags
& a_AMV_VF_RDONLY
)){
1854 msgp
= n_string_push_c(msgp
, *n_ns
);
1856 if(avc
.avc_map
!= NULL
&& (avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
))
1857 msgp
= n_string_push_cp(msgp
, "boolean; ");
1858 msgp
= n_string_push_cp(msgp
, "variable not set: ");
1859 msgp
= n_string_push_cp(msgp
, n_shexp_quote_cp(name
, FAL0
));
1864 n_UNINIT(quote
, NULL
);
1865 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1866 quote
= n_shexp_quote_cp(avc
.avc_var
->av_value
, TRU1
);
1867 if(strcmp(quote
, avc
.avc_var
->av_value
))
1868 msgp
= n_string_push_cp(msgp
, "wysh ");
1869 }else if(n_poption
& n_PO_D_V
)
1870 msgp
= n_string_push_cp(msgp
, "wysh ");
1871 if(avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
)
1872 msgp
= n_string_push_cp(msgp
, "environ ");
1873 msgp
= n_string_push_cp(msgp
, "set ");
1874 msgp
= n_string_push_cp(msgp
, name
);
1875 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_BOOL
)){
1876 msgp
= n_string_push_c(msgp
, '=');
1877 msgp
= n_string_push_cp(msgp
, quote
);
1878 }else if(n_poption
& n_PO_D_V
)
1879 msgp
= n_string_push_cp(msgp
, " #boolean");
1882 msgp
= n_string_push_c(msgp
, '\n');
1883 fputs(n_string_cp(msgp
), fp
);
1885 return (i
> 0 ? 2 : 1);
1889 a_amv_var_c_set(char **ap
, bool_t issetenv
){
1890 char *cp
, *cp2
, *varbuf
, c
;
1896 while((cp
= *ap
++) != NULL
){
1898 cp2
= varbuf
= n_autorec_alloc(strlen(cp
) +1);
1900 for(; (c
= *cp
) != '=' && c
!= '\0'; ++cp
){
1901 if(cntrlchar(c
) || spacechar(c
)){
1902 n_err(_("Variable name with control character ignored: %s\n"),
1911 cp
= n_UNCONST(n_empty
);
1916 n_err(_("Empty variable name ignored\n"));
1919 struct a_amv_var_carrier avc
;
1922 if((isunset
= (varbuf
[0] == 'n' && varbuf
[1] == 'o')))
1923 varbuf
= &varbuf
[2];
1925 a_amv_var_revlookup(&avc
, varbuf
);
1928 errs
+= !a_amv_var_clear(&avc
, issetenv
);
1930 errs
+= !a_amv_var_set(&avc
, cp
, issetenv
);
1945 if((args
= v
)[0] == NULL
){
1946 rv
= (a_amv_mac_show(a_AMV_MF_NONE
) == FAL0
);
1950 if(args
[1] == NULL
|| args
[1][0] != '{' || args
[1][1] != '\0' ||
1952 n_err(_("Synopsis: define: <name> {\n"));
1956 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_NONE
) == FAL0
);
1963 c_undefine(void *v
){
1971 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_NONE
);
1972 while(*++args
!= NULL
);
1982 rv
= a_amv_mac_call(vp
, FAL0
);
1988 c_call_if(void *vp
){
1992 rv
= a_amv_mac_call(vp
, TRU1
);
1999 struct a_amv_mac_call_args
*amcap
;
2000 struct a_amv_mac
*amp
;
2002 int rv
, i
, oqf
, nqf
;
2007 if((args
= v
)[0] == NULL
){
2008 rv
= (a_amv_mac_show(a_AMV_MF_ACCOUNT
) == FAL0
);
2012 if(args
[1] && args
[1][0] == '{' && args
[1][1] == '\0'){
2013 if(args
[2] != NULL
){
2014 n_err(_("Synopsis: account: <name> {\n"));
2017 if(!asccasecmp(args
[0], ACCOUNT_NULL
)){
2018 n_err(_("`account': cannot use reserved name: %s\n"),
2022 rv
= (a_amv_mac_def(args
[0], a_AMV_MF_ACCOUNT
) == FAL0
);
2026 if(n_pstate
& n_PS_HOOK_MASK
){
2027 n_err(_("`account': can't change account from within a hook\n"));
2031 save_mbox_for_possible_quitstuff();
2034 if(asccasecmp(args
[0], ACCOUNT_NULL
) != 0 &&
2035 (amp
= a_amv_mac_lookup(args
[0], NULL
, a_AMV_MF_ACCOUNT
)) == NULL
){
2036 n_err(_("`account': account does not exist: %s\n"), args
[0]);
2040 oqf
= savequitflags();
2042 if(a_amv_acc_curr
!= NULL
){
2043 if(a_amv_acc_curr
->am_lopts
!= NULL
)
2044 a_amv_lopts_unroll(&a_amv_acc_curr
->am_lopts
);
2045 /* For accounts this lingers */
2046 --a_amv_acc_curr
->am_refcnt
;
2047 if(a_amv_acc_curr
->am_flags
& a_AMV_MF_DELETE
)
2048 a_amv_mac_free(a_amv_acc_curr
);
2051 a_amv_acc_curr
= amp
;
2054 assert(amp
->am_lopts
== NULL
);
2055 amcap
= n_lofi_alloc(sizeof *amcap
);
2056 memset(amcap
, 0, sizeof *amcap
);
2057 amcap
->amca_name
= amp
->am_name
;
2058 amcap
->amca_amp
= amp
;
2059 amcap
->amca_unroller
= &
->am_lopts
;
2060 amcap
->amca_lopts_on
= TRU1
;
2061 amcap
->amca_no_xcall
= TRU1
;
2062 ++amp
->am_refcnt
; /* We may not run 0 to avoid being deleted! */
2063 if(!a_amv_mac_exec(amcap
)){
2064 /* XXX account switch incomplete, unroll? */
2065 n_err(_("`account': failed to switch to account: %s\n"), amp
->am_name
);
2070 n_PS_ROOT_BLOCK((amp
!= NULL
? ok_vset(account
, amp
->am_name
)
2071 : ok_vclear(account
)));
2073 if((n_psonce
& n_PSO_STARTED
) && !(n_pstate
& n_PS_HOOK_MASK
)){
2074 nqf
= savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
2075 restorequitflags(oqf
);
2076 if((i
= setfile("%", 0)) < 0)
2078 temporary_folder_hook_check(FAL0
);
2079 if(i
> 0 && !ok_blook(emptystart
))
2081 n_folder_announce(n_ANNOUNCE_CHANGE
);
2082 restorequitflags(nqf
);
2091 c_unaccount(void *v
){
2099 rv
|= !a_amv_mac_undef(*args
, a_AMV_MF_ACCOUNT
);
2100 while(*++args
!= NULL
);
2106 c_localopts(void *v
){
2113 if(a_amv_lopts
== NULL
){
2114 n_err(_("Cannot use `localopts' in this context "
2115 "(not in `define' or `account', nor special hook)\n"));
2121 if(n_pstate
& (n_PS_HOOK
| n_PS_COMPOSE_MODE
)){
2122 if(n_poption
& n_PO_D_V
)
2123 n_err(_("Cannot turn off `localopts' for compose-mode hooks\n"));
2127 a_amv_lopts
->as_unroll
= (boolify(*(argv
= v
), UIZ_MAX
, FAL0
) > 0);
2135 struct a_amv_pospar
*appp
;
2142 if((v
= *(char**)v
) == NULL
)
2147 if((n_idec_si16_cp(&sib
, v
, 10, NULL
2148 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2149 ) != n_IDEC_STATE_CONSUMED
|| sib
< 0){
2150 n_err(_("`shift': invalid argument: %s\n"), v
);
2156 /* If in in a macro/xy */
2157 if(a_amv_lopts
!= NULL
){
2158 struct a_amv_mac
const *amp
;
2159 struct a_amv_mac_call_args
*amcap
;
2161 /* Explicitly do allow `vpospar' created things! */
2162 amp
= (amcap
= a_amv_lopts
->as_amcap
)->amca_amp
;
2163 if((amp
== NULL
|| amcap
->amca_ps_hook_mask
||
2164 (amp
->am_flags
& a_AMV_MF_TYPE_MASK
) == a_AMV_MF_ACCOUNT
) &&
2165 amcap
->amca_pospar
.app_not_heap
){
2166 n_err(_("Cannot use `shift' in `account's or hook macros etc.\n"));
2169 appp
= &amcap
->amca_pospar
;
2171 appp
= &a_amv_pospar
;
2173 if(i
> appp
->app_count
){
2174 n_err(_("`shift': cannot shift %hu of %hu parameters\n"),
2175 i
, appp
->app_count
);
2179 appp
->app_count
-= i
;
2188 c_return(void *v
){ /* TODO the exit status should be m_si64! */
2192 if(a_amv_lopts
!= NULL
){
2195 n_go_input_force_eof();
2196 n_pstate_err_no
= n_ERR_NONE
;
2199 if((argv
= v
)[0] != NULL
){
2202 if((n_idec_si32_cp(&i
, argv
[0], 10, NULL
2203 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2204 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2207 n_err(_("`return': return value argument is invalid: %s\n"),
2209 n_pstate_err_no
= n_ERR_INVAL
;
2213 if(argv
[1] != NULL
){
2214 if((n_idec_si32_cp(&i
, argv
[1], 10, NULL
2215 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2216 ) == n_IDEC_STATE_CONSUMED
&& i
>= 0)
2217 n_pstate_err_no
= i
;
2219 n_err(_("`return': error number argument is invalid: %s\n"),
2221 n_pstate_err_no
= n_ERR_INVAL
;
2227 n_err(_("Can only use `return' in a macro\n"));
2228 n_pstate_err_no
= n_ERR_OPNOTSUPP
;
2236 temporary_folder_hook_check(bool_t nmail
){ /* TODO temporary, v15: drop */
2237 struct a_amv_mac_call_args
*amcap
;
2238 struct a_amv_mac
*amp
;
2246 var
= n_autorec_alloc(len
= strlen(mailname
) + sizeof("folder-hook-") -1 +1);
2248 /* First try the fully resolved path */
2249 snprintf(var
, len
, "folder-hook-%s", mailname
);
2250 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2253 /* If we are under *folder*, try the usual +NAME syntax, too */
2254 if(displayname
[0] == '+'){
2257 for(x
= &mailname
[len
]; x
!= mailname
; --x
)
2259 snprintf(var
, len
, "folder-hook-+%s", x
);
2260 if((cp
= n_var_vlook(var
, FAL0
)) != NULL
)
2266 /* Plain *folder-hook* is our last try */
2267 if((cp
= ok_vlook(folder_hook
)) == NULL
)
2271 if((amp
= a_amv_mac_lookup(cp
, NULL
, a_AMV_MF_NONE
)) == NULL
){
2272 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
2273 n_shexp_quote_cp(displayname
, FAL0
), cp
);
2278 amcap
= n_lofi_alloc(sizeof *amcap
);
2279 memset(amcap
, 0, sizeof *amcap
);
2280 amcap
->amca_name
= cp
;
2281 amcap
->amca_amp
= amp
;
2282 n_pstate
&= ~n_PS_HOOK_MASK
;
2284 amcap
->amca_unroller
= NULL
;
2285 n_pstate
|= n_PS_HOOK_NEWMAIL
;
2287 amcap
->amca_unroller
= &a_amv_folder_hook_lopts
;
2288 n_pstate
|= n_PS_HOOK
;
2290 amcap
->amca_lopts_on
= TRU1
;
2291 amcap
->amca_ps_hook_mask
= TRU1
;
2292 amcap
->amca_no_xcall
= TRU1
;
2293 rv
= a_amv_mac_exec(amcap
);
2294 n_pstate
&= ~n_PS_HOOK_MASK
;
2302 temporary_folder_hook_unroll(void){ /* XXX intermediate hack */
2304 if(a_amv_folder_hook_lopts
!= NULL
){
2305 void *save
= a_amv_lopts
;
2308 a_amv_lopts_unroll(&a_amv_folder_hook_lopts
);
2309 assert(a_amv_folder_hook_lopts
== NULL
);
2316 temporary_compose_mode_hook_call(char const *macname
,
2317 void (*hook_pre
)(void *), void *hook_arg
){
2318 /* TODO compose_mode_hook_call() temporary, v15: generalize; see a_GO_SPLICE
2319 * TODO comment in go.c for the right way of doing things! */
2320 static struct a_amv_lostack
*cmh_losp
;
2321 struct a_amv_mac_call_args
*amcap
;
2322 struct a_amv_mac
*amp
;
2327 if(macname
== (char*)-1){
2328 a_amv_mac__finalize(cmh_losp
);
2330 }else if(macname
!= NULL
&&
2331 (amp
= a_amv_mac_lookup(macname
, NULL
, a_AMV_MF_NONE
)) == NULL
)
2332 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
2335 amcap
= n_lofi_alloc(sizeof *amcap
);
2336 memset(amcap
, 0, sizeof *amcap
);
2337 amcap
->amca_name
= (macname
!= NULL
) ? macname
2338 : "*on-compose-splice(-shell)?*";
2339 amcap
->amca_amp
= amp
;
2340 amcap
->amca_unroller
= &a_amv_compose_lopts
;
2341 amcap
->amca_hook_pre
= hook_pre
;
2342 amcap
->amca_hook_arg
= hook_arg
;
2343 amcap
->amca_lopts_on
= TRU1
;
2344 amcap
->amca_ps_hook_mask
= TRU1
;
2345 amcap
->amca_no_xcall
= TRU1
;
2346 n_pstate
&= ~n_PS_HOOK_MASK
;
2347 n_pstate
|= n_PS_HOOK
;
2349 a_amv_mac_exec(amcap
);
2351 cmh_losp
= n_lofi_alloc(sizeof *cmh_losp
);
2352 cmh_losp
->as_global_saved
= a_amv_lopts
;
2353 cmh_losp
->as_up
= NULL
;
2354 cmh_losp
->as_lopts
= *(cmh_losp
->as_amcap
= amcap
)->amca_unroller
;
2355 cmh_losp
->as_unroll
= TRU1
;
2356 a_amv_lopts
= cmh_losp
;
2363 temporary_compose_mode_hook_unroll(void){ /* XXX intermediate hack */
2365 if(a_amv_compose_lopts
!= NULL
){
2366 void *save
= a_amv_lopts
;
2369 a_amv_lopts_unroll(&a_amv_compose_lopts
);
2370 assert(a_amv_compose_lopts
== NULL
);
2377 n_var_is_user_writable(char const *name
){
2378 struct a_amv_var_carrier avc
;
2379 struct a_amv_var_map
const *avmp
;
2383 a_amv_var_revlookup(&avc
, name
);
2384 if((avmp
= avc
.avc_map
) == NULL
)
2387 rv
= ((avmp
->avm_flags
& (a_AMV_VF_BOOL
| a_AMV_VF_RDONLY
)) == 0);
2393 n_var_oklook(enum okeys okey
){
2394 struct a_amv_var_carrier avc
;
2396 struct a_amv_var_map
const *avmp
;
2399 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2400 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2401 avc
.avc_hash
= avmp
->avm_hash
;
2402 avc
.avc_okey
= okey
;
2404 if(a_amv_var_lookup(&avc
, FAL0
))
2405 rv
= avc
.avc_var
->av_value
;
2413 n_var_okset(enum okeys okey
, uintptr_t val
){
2414 struct a_amv_var_carrier avc
;
2416 struct a_amv_var_map
const *avmp
;
2419 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2420 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2421 avc
.avc_hash
= avmp
->avm_hash
;
2422 avc
.avc_okey
= okey
;
2424 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2430 n_var_okclear(enum okeys okey
){
2431 struct a_amv_var_carrier avc
;
2433 struct a_amv_var_map
const *avmp
;
2436 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2437 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2438 avc
.avc_hash
= avmp
->avm_hash
;
2439 avc
.avc_okey
= okey
;
2441 rv
= a_amv_var_clear(&avc
, FAL0
);
2447 n_var_vlook(char const *vokey
, bool_t try_getenv
){
2448 struct a_amv_var_carrier avc
;
2452 a_amv_var_revlookup(&avc
, vokey
);
2454 switch((enum a_amv_var_special_category
)avc
.avc_special_cat
){
2455 default: /* silence CC */
2456 case a_AMV_VSC_NONE
:
2457 if(a_amv_var_lookup(&avc
, FAL0
))
2458 rv
= avc
.avc_var
->av_value
;
2459 /* Only check the environment for something that is otherwise unknown */
2460 else if(try_getenv
&& avc
.avc_map
== NULL
)
2465 case a_AMV_VSC_GLOBAL
:
2466 rv
= a_amv_var_vsc_global(&avc
);
2468 case a_AMV_VSC_MULTIPLEX
:
2469 rv
= a_amv_var_vsc_multiplex(&avc
);
2471 case a_AMV_VSC_POSPAR
:
2472 case a_AMV_VSC_POSPAR_ENV
:
2473 rv
= a_amv_var_vsc_pospar(&avc
);
2481 n_var_vexplode(void const **cookie
){
2482 struct a_amv_pospar
*appp
;
2485 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
2487 *cookie
= (appp
->app_count
> 0) ? &appp
->app_dat
[appp
->app_idx
] : NULL
;
2489 return (*cookie
!= NULL
);
2493 n_var_vset(char const *vokey
, uintptr_t val
){
2494 struct a_amv_var_carrier avc
;
2498 a_amv_var_revlookup(&avc
, vokey
);
2500 ok
= a_amv_var_set(&avc
, (val
== 0x1 ? n_empty
: (char const*)val
), FAL0
);
2506 n_var_vclear(char const *vokey
){
2507 struct a_amv_var_carrier avc
;
2511 a_amv_var_revlookup(&avc
, vokey
);
2513 ok
= a_amv_var_clear(&avc
, FAL0
);
2520 n_var_xoklook(enum okeys okey
, struct url
const *urlp
,
2521 enum okey_xlook_mode oxm
){
2522 struct a_amv_var_carrier avc
;
2523 struct str
const *us
;
2526 struct a_amv_var_map
const *avmp
;
2529 assert(oxm
& (OXM_PLAIN
| OXM_H_P
| OXM_U_H_P
));
2531 /* For simplicity: allow this case too */
2532 if(!(oxm
& (OXM_H_P
| OXM_U_H_P
))){
2537 avc
.avc_map
= avmp
= &a_amv_var_map
[okey
];
2538 avc
.avc_name
= a_amv_var_names
+ avmp
->avm_keyoff
;
2539 avc
.avc_okey
= okey
;
2541 us
= (oxm
& OXM_U_H_P
) ? &urlp
->url_u_h_p
: &urlp
->url_h_p
;
2542 nlen
= strlen(avc
.avc_name
);
2543 nbuf
= n_lofi_alloc(nlen
+ 1 + us
->l
+1);
2544 memcpy(nbuf
, avc
.avc_name
, nlen
);
2547 /* One of .url_u_h_p and .url_h_p we test in here */
2548 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2549 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2550 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2551 if(a_amv_var_lookup(&avc
, FAL0
))
2556 us
= &urlp
->url_h_p
;
2557 memcpy(nbuf
+ nlen
, us
->s
, us
->l
+1);
2558 avc
.avc_name
= a_amv_var_canonify(nbuf
);
2559 avc
.avc_hash
= a_AMV_NAME2HASH(avc
.avc_name
);
2560 if(a_amv_var_lookup(&avc
, FAL0
)){
2562 rv
= avc
.avc_var
->av_value
;
2568 rv
= (oxm
& OXM_PLAIN
) ? n_var_oklook(okey
) : NULL
;
2575 #endif /* HAVE_SOCKETS */
2583 if(*(ap
= v
) == NULL
){
2584 a_amv_var_show_all();
2587 err
= !a_amv_var_c_set(ap
, FAL0
);
2598 for(err
= 0, ap
= v
; *ap
!= NULL
; ++ap
)
2599 err
|= !n_var_vclear(*ap
);
2609 if(*(ap
= v
) == NULL
)
2612 struct n_string msg
, *msgp
= &msg
;
2614 msgp
= n_string_creat(msgp
);
2615 for(; *ap
!= NULL
; ++ap
)
2616 a_amv_var_show(*ap
, n_stdout
, msgp
);
2620 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
2625 struct a_amv_var_carrier avc
;
2629 sighandler_type sigint
;
2632 sigint
= safe_signal(SIGINT
, SIG_IGN
);
2634 for(err
= 0, argv
= v
; *argv
!= NULL
; ++argv
){
2635 memset(&avc
, 0, sizeof avc
);
2637 a_amv_var_revlookup(&avc
, *argv
);
2639 if(avc
.avc_map
!= NULL
){
2640 if(avc
.avc_map
->avm_flags
& a_AMV_VF_BOOL
){
2641 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2645 if(avc
.avc_map
->avm_flags
& a_AMV_VF_RDONLY
){
2646 n_err(_("`varedit': cannot edit read-only variable: %s\n"),
2652 a_amv_var_lookup(&avc
, FAL0
);
2654 if((of
= Ftmp(NULL
, "varedit", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2656 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2659 }else if(avc
.avc_var
!= NULL
&& *(val
= avc
.avc_var
->av_value
) != '\0' &&
2660 sizeof *val
!= fwrite(val
, strlen(val
), sizeof *val
, of
)){
2661 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2668 nf
= run_editor(of
, (off_t
)-1, 'e', FAL0
, NULL
, NULL
, SEND_MBOX
, sigint
);
2677 if(UICMP(64, l
, >=, UIZ_MAX
-42)){
2678 n_err(_("`varedit': not enough memory to store variable: %s\n"),
2683 varres
= val
= n_autorec_alloc(l
+1);
2684 for(; l
> 0 && (c
= getc(nf
)) != EOF
; --l
)
2688 n_err(_("`varedit': I/O while reading new value of: %s\n"),
2694 if(!a_amv_var_set(&avc
, varres
, FAL0
))
2699 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2705 safe_signal(SIGINT
, sigint
);
2712 struct a_amv_var_carrier avc
;
2718 if((islnk
= is_asccaseprefix(*(ap
= v
), "link")) ||
2719 is_asccaseprefix(*ap
, "unlink")){
2720 for(err
= 0; *++ap
!= NULL
;){
2721 a_amv_var_revlookup(&avc
, *ap
);
2723 if(a_amv_var_lookup(&avc
, FAL0
) && (islnk
||
2724 (avc
.avc_var
->av_flags
& a_AMV_VF_LINKED
))){
2726 avc
.avc_var
->av_flags
&= ~a_AMV_VF_LINKED
;
2728 }else if(avc
.avc_var
->av_flags
& (a_AMV_VF_ENV
| a_AMV_VF_LINKED
)){
2729 if(n_poption
& n_PO_D_V
)
2730 n_err(_("`environ': link: already established: %s\n"), *ap
);
2733 avc
.avc_var
->av_flags
|= a_AMV_VF_LINKED
;
2734 if(!(avc
.avc_var
->av_flags
& a_AMV_VF_ENV
))
2735 a_amv_var__putenv(&avc
, avc
.avc_var
);
2737 n_err(_("`environ': unlink: no link established: %s\n"), *ap
);
2740 char const *evp
= getenv(*ap
);
2743 err
|= !a_amv_var_set(&avc
, evp
, TRU1
);
2745 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2751 }else if(is_asccaseprefix(*ap
, "set"))
2752 err
= !a_amv_var_c_set(++ap
, TRU1
);
2753 else if(is_asccaseprefix(*ap
, "unset")){
2754 for(err
= 0; *++ap
!= NULL
;){
2755 a_amv_var_revlookup(&avc
, *ap
);
2757 if(!a_amv_var_clear(&avc
, TRU1
))
2761 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2769 c_vexpr(void *v
){ /* TODO POSIX expr(1) comp. exit status; overly complicat. */
2771 enum n_idec_state ids
;
2773 char op
, varbuf
[64 + 64 / 8 +1];
2774 char const **argv
, *varname
, *varres
, *cp
;
2777 a_SOFTOVERFLOW
= 1u<<1,
2779 a_ISDECIMAL
= 1u<<3, /* Print only decimal result */
2780 a_SATURATED
= 1u<<4,
2782 a_UNSIGNED
= 1u<<6, /* Unsigned right shift (share bit ok) */
2789 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2790 n_UNINIT(varres
, n_empty
);
2792 if((cp
= argv
[0])[0] == '\0')
2802 if(argv
[1] == NULL
|| argv
[2] != NULL
)
2805 if(*(cp
= *++argv
) == '\0')
2807 else if(((ids
= n_idec_si64_cp(&lhv
, cp
, 0, NULL
)
2808 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2809 ) != n_IDEC_STATE_CONSUMED
){
2810 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2812 f
|= a_SOFTOVERFLOW
;
2829 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != 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
;
2845 if(*(cp
= *++argv
) == '\0')
2847 else if(((ids
= n_idec_si64_cp(&rhv
, cp
, 0, NULL
)
2848 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
2849 ) != n_IDEC_STATE_CONSUMED
){
2850 if(!(ids
& n_IDEC_STATE_EOVERFLOW
) || !(f
& a_SATURATED
))
2852 f
|= a_SOFTOVERFLOW
;
2862 if(rhv
!= SI64_MIN
){
2867 goto jenum_plusminus
;
2873 if(SI64_MAX
- rhv
< lhv
)
2874 goto jenum_plusminus
;
2879 if(rhv
!= SI64_MIN
){
2884 goto jenum_plusminus
;
2890 if(SI64_MIN
+ rhv
> lhv
){
2892 if(!(f
& a_SATURATED
))
2893 goto jenum_overflow
;
2894 f
|= a_SOFTOVERFLOW
;
2895 lhv
= (lhv
< 0 || xop
== '-') ? SI64_MIN
: SI64_MAX
;
2900 /* Will the result be positive? */
2901 if((lhv
< 0) == (rhv
< 0)){
2906 if(rhv
!= 0 && lhv
!= 0 && SI64_MAX
/ rhv
> lhv
){
2907 if(!(f
& a_SATURATED
))
2908 goto jenum_overflow
;
2909 f
|= a_SOFTOVERFLOW
;
2915 if(lhv
!= 0 && SI64_MIN
/ lhv
< rhv
){
2916 if(!(f
& a_SATURATED
))
2917 goto jenum_overflow
;
2918 f
|= a_SOFTOVERFLOW
;
2923 if(rhv
!= 0 && lhv
!= 0 && SI64_MIN
/ rhv
< lhv
){
2924 if(!(f
& a_SATURATED
))
2925 goto jenum_overflow
;
2926 f
|= a_SOFTOVERFLOW
;
2935 if(!(f
& a_SATURATED
))
2937 f
|= a_SOFTOVERFLOW
;
2944 if(!(f
& a_SATURATED
))
2946 f
|= a_SOFTOVERFLOW
;
2964 if(rhv
> 63){ /* xxx 63? */
2965 if(!(f
& a_SATURATED
))
2966 goto jenum_overflow
;
2971 else if(f
& a_UNSIGNED
)
2972 lhv
= (ui64_t
)lhv
>> (ui8_t
)rhv
;
2982 }else if(cp
[2] == '\0' && cp
[1] == '@'){
2986 }else if(cp
[0] == '<'){
2998 }else if(cp
[0] == '>'){
3014 }else if(is_asccaseprefix(cp
, "length")){
3015 f
|= a_ISNUM
| a_ISDECIMAL
;
3016 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3019 i
= strlen(*++argv
);
3020 if(UICMP(64, i
, >, SI64_MAX
))
3021 goto jestr_overflow
;
3023 }else if(is_asccaseprefix(cp
, "hash")){
3024 f
|= a_ISNUM
| a_ISDECIMAL
;
3025 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3028 i
= n_torek_hash(*++argv
);
3030 }else if(is_asccaseprefix(cp
, "find")){
3031 f
|= a_ISNUM
| a_ISDECIMAL
;
3032 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3035 if((cp
= strstr(argv
[1], argv
[2])) == NULL
)
3037 i
= PTR2SIZE(cp
- argv
[1]);
3038 if(UICMP(64, i
, >, SI64_MAX
))
3039 goto jestr_overflow
;
3041 }else if(is_asccaseprefix(cp
, "ifind")){
3042 f
|= a_ISNUM
| a_ISDECIMAL
;
3043 if(argv
[1] == NULL
|| argv
[2] == NULL
|| argv
[3] != NULL
)
3046 if((cp
= asccasestr(argv
[1], argv
[2])) == NULL
)
3048 i
= PTR2SIZE(cp
- argv
[1]);
3049 if(UICMP(64, i
, >, SI64_MAX
))
3050 goto jestr_overflow
;
3052 }else if(is_asccaseprefix(cp
, "substring")){
3053 if(argv
[1] == NULL
|| argv
[2] == NULL
)
3055 if(argv
[3] != NULL
&& argv
[4] != NULL
)
3058 i
= strlen(varres
= *++argv
);
3060 if(*(cp
= *++argv
) == '\0')
3062 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3063 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3064 ) != n_IDEC_STATE_CONSUMED
)
3065 goto jestr_numrange
;
3066 if(UICMP(64, i
, >=, lhv
)){
3070 if(n_poption
& n_PO_D_V
)
3071 n_err(_("`vexpr': substring: offset argument too large: %s\n"),
3072 n_shexp_quote_cp(argv
[-1], FAL0
));
3073 f
|= a_SOFTOVERFLOW
;
3076 if(argv
[1] != NULL
){
3077 if(*(cp
= *++argv
) == '\0')
3079 else if((n_idec_si64_cp(&lhv
, cp
, 0, NULL
3080 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3081 ) != n_IDEC_STATE_CONSUMED
)
3082 goto jestr_numrange
;
3083 if(UICMP(64, i
, >=, lhv
)){
3084 if(UICMP(64, i
, !=, lhv
))
3085 varres
= savestrbuf(varres
, (size_t)lhv
);
3087 if(n_poption
& n_PO_D_V
)
3088 n_err(_("`vexpr': substring: length argument too large: %s\n"),
3089 n_shexp_quote_cp(argv
[-2], FAL0
));
3090 f
|= a_SOFTOVERFLOW
;
3093 }else if(is_asccaseprefix(cp
, "random")){
3094 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3097 if((n_idec_si64_cp(&lhv
, argv
[1], 0, NULL
3098 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
3099 ) != n_IDEC_STATE_CONSUMED
|| lhv
< 0 || lhv
> PATH_MAX
)
3100 goto jestr_numrange
;
3103 varres
= n_random_create_cp((size_t)lhv
, NULL
);
3104 }else if(is_asccaseprefix(cp
, "file-expand")){
3105 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3108 if((varres
= fexpand(argv
[1], FEXP_NVAR
| FEXP_NOPROTO
)) == NULL
)
3110 }else if(is_asccaseprefix(cp
, "makeprint")){
3111 struct str sin
, sout
;
3113 if(argv
[1] == NULL
|| argv
[2] != NULL
)
3116 /* XXX using strlen for `vexpr makeprint' is wrong for UTF-16 */
3117 sin
.l
= strlen(sin
.s
= n_UNCONST(argv
[1]));
3118 makeprint(&sin
, &sout
);
3119 varres
= savestrbuf(sout
.s
, sout
.l
);
3121 /* TODO `vexpr': (wide) string length, find, etc!! */
3123 }else if(is_asccaseprefix(cp
, "regex")) Jregex
:{
3124 regmatch_t rema
[1 + n_VEXPR_REGEX_MAX
];
3128 f
|= a_ISNUM
| a_ISDECIMAL
;
3129 if(argv
[1] == NULL
|| argv
[2] == NULL
||
3130 (argv
[3] != NULL
&& argv
[4] != NULL
))
3133 reflrv
= REG_EXTENDED
;
3135 reflrv
|= REG_ICASE
;
3136 if((reflrv
= regcomp(&re
, argv
[2], reflrv
))){
3137 n_err(_("`vexpr': invalid regular expression: %s: %s\n"),
3138 n_shexp_quote_cp(argv
[2], FAL0
), n_regex_err_to_doc(&re
, reflrv
));
3140 n_pstate_err_no
= n_ERR_INVAL
;
3143 reflrv
= regexec(&re
, argv
[1], n_NELEM(rema
), rema
, 0);
3145 if(reflrv
== REG_NOMATCH
)
3148 /* Search only? Else replace, which is a bit */
3149 if(argv
[3] == NULL
){
3150 if(UICMP(64, rema
[0].rm_so
, >, SI64_MAX
))
3151 goto jestr_overflow
;
3152 lhv
= (si64_t
)rema
[0].rm_so
;
3154 /* We need to setup some kind of pseudo macro environment for this */
3155 struct a_amv_lostack los
;
3156 struct a_amv_mac_call_args amca
;
3157 char const **reargv
;
3159 memset(&amca
, 0, sizeof amca
);
3160 amca
.amca_name
= savestrbuf(&argv
[1][rema
[0].rm_so
],
3161 rema
[0].rm_eo
- rema
[0].rm_so
);
3162 amca
.amca_amp
= a_AMV_MACKY_MACK
;
3163 for(i
= 1; rema
[i
].rm_so
!= -1 && i
< n_NELEM(rema
); ++i
)
3165 amca
.amca_pospar
.app_count
= (ui32_t
)i
- 1;
3166 amca
.amca_pospar
.app_not_heap
= TRU1
;
3167 amca
.amca_pospar
.app_dat
=
3168 reargv
= n_autorec_alloc(sizeof(char*) * i
);
3169 for(i
= 1; rema
[i
].rm_so
!= -1 && i
< n_NELEM(rema
); ++i
)
3170 *reargv
++ = savestrbuf(&argv
[1][rema
[i
].rm_so
],
3171 rema
[i
].rm_eo
- rema
[i
].rm_so
);
3174 hold_all_sigs(); /* TODO DISLIKE! */
3175 los
.as_global_saved
= a_amv_lopts
;
3176 los
.as_amcap
= &amca
;
3177 los
.as_up
= los
.as_global_saved
;
3178 los
.as_lopts
= NULL
;
3179 los
.as_unroll
= FAL0
;
3184 struct n_string s_b
;
3185 enum n_shexp_state shs
;
3187 templ
.s
= n_UNCONST(argv
[3]);
3189 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
3190 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
3191 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
),
3192 n_string_creat_auto(&s_b
), &templ
, NULL
);
3193 if((shs
& (n_SHEXP_STATE_ERR_MASK
| n_SHEXP_STATE_STOP
)
3194 ) == n_SHEXP_STATE_STOP
){
3195 varres
= n_string_cp(&s_b
);
3196 n_string_drop_ownership(&s_b
);
3201 a_amv_lopts
= los
.as_global_saved
;
3202 rele_all_sigs(); /* TODO DISLIKE! */
3206 f
&= ~(a_ISNUM
| a_ISDECIMAL
);
3208 }else if(is_asccaseprefix(argv
[0], "iregex")){
3211 #endif /* HAVE_REGEX */
3215 n_pstate_err_no
= (f
& a_SOFTOVERFLOW
) ? n_ERR_OVERFLOW
: n_ERR_NONE
;
3218 /* Generate the variable value content for numerics.
3219 * Anticipate in our handling below! (Don't do needless work) */
3221 if((f
& a_ISNUM
) && ((f
& a_ISDECIMAL
) || varname
!= NULL
)){
3222 snprintf(varbuf
, sizeof varbuf
, "%" PRId64
, lhv
);
3226 if(varname
== NULL
){
3227 /* If there was no error and we are printing a numeric result, print some
3228 * more bases for the fun of it */
3229 if((f
& (a_ERR
| a_ISNUM
| a_ISDECIMAL
)) == a_ISNUM
){
3232 for(j
= 1, i
= 0; i
< 64; ++i
){
3233 varbuf
[63 + 64 / 8 -j
- i
] = (lhv
& ((ui64_t
)1 << i
)) ? '1' : '0';
3234 if((i
& 7) == 7 && i
!= 63){
3236 varbuf
[63 + 64 / 8 -j
- i
] = ' ';
3239 varbuf
[64 + 64 / 8 -1] = '\0';
3240 if(fprintf(n_stdout
,
3241 "%s\n0%" PRIo64
" | 0x%" PRIX64
" | %" PRId64
"\n",
3242 varbuf
, lhv
, lhv
, lhv
) < 0){
3243 n_pstate_err_no
= n_err_no
;
3246 }else if(varres
!= NULL
&& fprintf(n_stdout
, "%s\n", varres
) < 0){
3247 n_pstate_err_no
= n_err_no
;
3250 }else if(!n_var_vset(varname
, (uintptr_t)varres
)){
3251 n_pstate_err_no
= n_ERR_NOTSUP
;
3255 return (f
& a_ERR
) ? 1 : 0;
3258 f
= a_ERR
| a_ISNUM
;
3262 n_err(_("`vexpr': invalid subcommand: %s\n"),
3263 n_shexp_quote_cp(*argv
, FAL0
));
3264 n_pstate_err_no
= n_ERR_INVAL
;
3267 n_err(_("Synopsis: vexpr: <operator> <:argument:>\n"));
3268 n_pstate_err_no
= n_ERR_INVAL
;
3272 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3273 n_shexp_quote_cp(*argv
, FAL0
));
3274 n_pstate_err_no
= n_ERR_RANGE
;
3277 n_err(_("`vexpr': expression overflows datatype: %" PRId64
" %c %" PRId64
3278 "\n"), lhv
, op
, rhv
);
3279 n_pstate_err_no
= n_ERR_OVERFLOW
;
3283 n_err(_("`vexpr': numeric argument invalid or out of range: %s\n"),
3284 n_shexp_quote_cp(*argv
, FAL0
));
3285 n_pstate_err_no
= n_ERR_RANGE
;
3288 n_err(_("`vexpr': string length or offset overflows datatype\n"));
3289 n_pstate_err_no
= n_ERR_OVERFLOW
;
3292 n_pstate_err_no
= n_ERR_NODATA
;
3303 struct n_cmd_arg
*cap
;
3305 struct a_amv_pospar
*appp
;
3314 struct n_cmd_arg_ctx
*cacp
;
3317 n_pstate_err_no
= n_ERR_NONE
;
3318 n_UNINIT(varres
, n_empty
);
3320 cap
= cacp
->cac_arg
;
3322 if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "set"))
3324 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "clear"))
3326 else if(is_asccaseprefix(cap
->ca_arg
.ca_str
.s
, "quote"))
3329 n_err(_("`vpospar': invalid subcommand: %s\n"),
3330 n_shexp_quote_cp(cap
->ca_arg
.ca_str
.s
, FAL0
));
3331 n_pstate_err_no
= n_ERR_INVAL
;
3337 if((f
& (a_CLEAR
| a_QUOTE
)) && cap
->ca_next
!= NULL
){
3338 n_err(_("`vpospar': `%s': takes no argument\n"), cap
->ca_arg
.ca_str
.s
);
3339 n_pstate_err_no
= n_ERR_INVAL
;
3346 /* If in a macro, we need to overwrite the local instead of global argv */
3347 appp
= (a_amv_lopts
!= NULL
) ? &a_amv_lopts
->as_amcap
->amca_pospar
3350 if(f
& (a_SET
| a_CLEAR
)){
3351 if(cacp
->cac_vput
!= NULL
&& (n_poption
& n_PO_D_V
))
3352 n_err(_("`vpospar': `vput' only supported for `quote' subcommand\n"));
3353 if(!appp
->app_not_heap
&& appp
->app_maxcount
> 0){
3354 for(i
= appp
->app_maxcount
; i
-- != 0;)
3355 n_free(n_UNCONST(appp
->app_dat
[i
]));
3356 n_free(appp
->app_dat
);
3358 memset(appp
, 0, sizeof *appp
);
3361 if((i
= cacp
->cac_no
) > a_AMV_POSPAR_MAX
){
3362 n_err(_("`vpospar': overflow: %" PRIuZ
" arguments!\n"), i
);
3363 n_pstate_err_no
= n_ERR_OVERFLOW
;
3368 memset(appp
, 0, sizeof *appp
);
3370 appp
->app_maxcount
= appp
->app_count
= (ui16_t
)i
;
3371 /* XXX Optimize: store it all in one chunk! */
3373 i
*= sizeof *appp
->app_dat
;
3374 appp
->app_dat
= n_alloc(i
);
3376 for(i
= 0; cap
!= NULL
; ++i
, cap
= cap
->ca_next
){
3377 appp
->app_dat
[i
] = n_alloc(cap
->ca_arg
.ca_str
.l
+1);
3378 memcpy(n_UNCONST(appp
->app_dat
[i
]), cap
->ca_arg
.ca_str
.s
,
3379 cap
->ca_arg
.ca_str
.l
+1);
3382 appp
->app_dat
[i
] = NULL
;
3386 if(appp
->app_count
== 0)
3390 struct n_string s
, *sp
;
3393 sp
= n_string_creat_auto(&s
);
3395 sep1
= *ok_vlook(ifs
);
3396 sep2
= *ok_vlook(ifs_ws
);
3402 for(i
= 0; i
< appp
->app_count
; ++i
){
3404 if(!n_string_can_swallow(sp
, 2))
3406 sp
= n_string_push_c(sp
, sep1
);
3408 sp
= n_string_push_c(sp
, sep2
);
3410 in
.l
= strlen(in
.s
= n_UNCONST(appp
->app_dat
[i
+ appp
->app_idx
]));
3412 if(!n_string_can_swallow(sp
, in
.l
)){
3414 n_err(_("`vpospar': overflow: string too long!\n"));
3415 n_pstate_err_no
= n_ERR_OVERFLOW
;
3419 sp
= n_shexp_quote(sp
, &in
, TRU1
);
3422 varres
= n_string_cp(sp
);
3425 if(cacp
->cac_vput
== NULL
){
3426 if(fprintf(n_stdout
, "%s\n", varres
) < 0){
3427 n_pstate_err_no
= n_err_no
;
3430 }else if(!n_var_vset(cacp
->cac_vput
, (uintptr_t)varres
)){
3431 n_pstate_err_no
= n_ERR_NOTSUP
;
3437 return (f
& a_ERR
) ? 1 : 0;