Add n_shexp_is_valid_varname()
[s-mailx.git] / accmacvar.c
blob03cf039613e470e97f49a78a6fb4ceba5a66b2d0
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run mk-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . should be recursive environment based.
8 *@ TODO Otherwise, the `localopts' should be an attribute of the lex_input.c
9 *@ TODO command context, so that it belongs to the execution context
10 *@ TODO we are running in, instead of being global data. See, e.g.,
11 *@ TODO the a_LEX_SLICE comment in lex_input.c.
12 *@ TODO . undefining and overwriting a macro should always be possible:
13 *@ TODO simply place the thing in a delete-later list and replace the
14 *@ TODO accessible entry! (instant delete if on top recursion level.)
15 *@ TODO . Likewise, overwriting an existing should be like delete+create
16 *@ TODO . once we can have non-fatal !0 returns for commands, we should
17 *@ TODO return error if "(environ)? unset" goes for non-existent.
19 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
20 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
23 * Copyright (c) 1980, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
50 #undef n_FILE
51 #define n_FILE accmacvar
53 #ifndef HAVE_AMALGAMATION
54 # include "nail.h"
55 #endif
57 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
58 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
59 #endif
61 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
62 #define a_AMV_PRIME HSHSIZE
63 #define a_AMV_NAME2HASH(N) torek_hash(N)
64 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
66 enum a_amv_mac_flags{
67 a_AMV_MF_NONE = 0,
68 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
69 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
70 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
71 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
72 a_AMV_MF__MAX = 0xFF
75 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
76 * _IMPORT implies _ENV; it doesn't verify anything... */
77 enum a_amv_var_flags{
78 a_AMV_VF_NONE = 0,
79 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
80 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
81 a_AMV_VF_NOLOPTS = 1<<2, /* May not be tracked by `localopts' */
82 a_AMV_VF_RDONLY = 1<<3, /* May not be set by user */
83 a_AMV_VF_NODEL = 1<<4, /* May not be deleted */
84 a_AMV_VF_NOTEMPTY = 1<<5, /* May not be assigned an empty value */
85 a_AMV_VF_NOCNTRLS = 1<<6, /* Value may not contain control characters */
86 a_AMV_VF_NUM = 1<<7, /* Value must be a 32-bit number */
87 a_AMV_VF_POSNUM = 1<<8, /* Value must be positive 32-bit number */
88 a_AMV_VF_VIP = 1<<9, /* Wants _var_check_vips() evaluation */
89 a_AMV_VF_IMPORT = 1<<10, /* Import ONLY from environ (before PS_STARTED) */
90 a_AMV_VF_ENV = 1<<11, /* Update environment on change */
91 a_AMV_VF_I3VAL = 1<<12, /* Has an initial value */
92 a_AMV_VF_DEFVAL = 1<<13, /* Has a default value */
93 a_AMV_VF_LINKED = 1<<14, /* `environ' linked */
94 a_AMV_VF__MASK = (1<<(14+1)) - 1
97 struct a_amv_mac{
98 struct a_amv_mac *am_next;
99 ui32_t am_maxlen; /* of any line in .am_line_dat */
100 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
101 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
102 struct a_amv_var *am_lopts; /* `localopts' unroll list */
103 ui8_t am_flags; /* enum a_amv_mac_flags */
104 char am_name[n_VFIELD_SIZE(7)]; /* of this macro */
106 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
108 struct a_amv_mac_line{
109 ui32_t aml_len;
110 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
111 char aml_dat[n_VFIELD_SIZE(0)];
114 struct a_amv_mac_call_args{
115 char const *amca_name;
116 struct a_amv_mac const *amca_amp;
117 struct a_amv_var **amca_unroller;
118 void (*amca_hook_pre)(void *);
119 void *amca_hook_arg;
120 bool_t amca_lopts_on;
121 bool_t amca_ps_hook_mask;
122 ui8_t amca__pad[6];
125 struct a_amv_lostack{
126 struct a_amv_lostack *as_global_saved; /* Saved global XXX due to jump */
127 struct a_amv_mac_call_args *as_amcap;
128 struct a_amv_lostack *as_up; /* Outer context */
129 struct a_amv_var *as_lopts;
130 bool_t as_unroll; /* Unrolling enabled? */
131 ui8_t avs__pad[7];
134 struct a_amv_var{
135 struct a_amv_var *av_link;
136 char *av_value;
137 #ifdef HAVE_PUTENV
138 char *av_env; /* Actively managed putenv(3) memory */
139 #endif
140 ui16_t av_flags; /* enum a_amv_var_flags */
141 char av_name[n_VFIELD_SIZE(6)];
143 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
145 struct a_amv_var_map{
146 ui32_t avm_hash;
147 ui16_t avm_keyoff;
148 ui16_t avm_flags; /* enum a_amv_var_flags */
150 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
152 struct a_amv_var_virt{
153 ui32_t avv_okey;
154 ui8_t avv__dummy[4];
155 struct a_amv_var const *avv_var;
158 struct a_amv_var_defval{
159 ui32_t avdv_okey;
160 ui8_t avdv__pad[4];
161 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
164 struct a_amv_var_carrier{
165 char const *avc_name;
166 ui32_t avc_hash;
167 ui32_t avc_prime;
168 struct a_amv_var *avc_var;
169 struct a_amv_var_map const *avc_map;
170 enum okeys avc_okey;
171 ui8_t avc__pad[4];
174 /* Include the constant mk-okey-map.pl output */
175 #include "version.h"
176 #include "okeys.h"
178 /* True boolean visualization: this string will not be copied to heap memory
179 * in a_amv_var_copy(), but we must avoid confusion with identical user data.
180 * While here, add a special "0" one and speed up *.exit-status* assignments! */
181 static char const a_amv_var_1[] = "1";
182 static char const a_amv_var_0[] = "0";
184 /* The currently active account */
185 static struct a_amv_mac *a_amv_acc_curr;
187 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
189 /* Unroll list of currently running macro stack */
190 static struct a_amv_lostack *a_amv_lopts;
192 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
194 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
195 * TODO today via a static lostack, it should be a field in mailbox, once that
196 * TODO is a real multi-instance object */
197 static struct a_amv_var *a_amv_folder_hook_lopts;
199 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
200 static struct a_amv_var *a_amv_compose_lopts;
202 /* Does cp consist solely of WS and a } */
203 static bool_t a_amv_mac_is_closing_angle(char const *cp);
205 /* Lookup for macros/accounts */
206 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
207 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
209 /* Execute a macro; amcap must reside in LOFI memory */
210 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
212 static void a_amv_mac__finalize(void *vp);
214 /* User display helpers */
215 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
217 /* _def() returns error for faulty definitions and already existing * names,
218 * _undef() returns error if a named thing doesn't exist */
219 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
220 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
222 /* */
223 static void a_amv_mac_free(struct a_amv_mac *amp);
225 /* Update replay-log */
226 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
227 struct a_amv_var *oavp);
228 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
230 /* Special cased value string allocation */
231 static char *a_amv_var_copy(char const *str);
232 static void a_amv_var_free(char *cp);
234 /* Check for special housekeeping */
235 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
237 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
238 static bool_t a_amv_var_check_nocntrls(char const *val);
239 static bool_t a_amv_var_check_num(char const *val, bool_t pos);
241 /* If a variable name begins with a lowercase-character and contains at
242 * least one '@', it is converted to all-lowercase. This is necessary
243 * for lookups of names based on email addresses.
244 * Following the standard, only the part following the last '@' should
245 * be lower-cased, but practice has established otherwise here */
246 static char const *a_amv_var_canonify(char const *vn);
248 /* Try to reverse lookup an option name to an enum okeys mapping.
249 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
250 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
251 char const *name);
253 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
254 * Sets .avc_prime; .avc_var is NULL if not found.
255 * Here it is where we care for _I3VAL and _DEFVAL, too.
256 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
257 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
258 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
259 * return FAL0, then! */
260 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
261 bool_t i3val_nonew);
263 /* Set var from .avc_(map|name|hash), return success */
264 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
265 bool_t force_env);
267 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
268 struct a_amv_var *avp);
270 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
271 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
273 static bool_t a_amv_var__clearenv(char const *name, char *value);
275 /* List all variables */
276 static void a_amv_var_show_all(void);
278 static int a_amv_var__show_cmp(void const *s1, void const *s2);
280 /* Actually do print one, return number of lines written */
281 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
283 /* Shared c_set() and c_environ():set impl, return success */
284 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
286 static bool_t
287 a_amv_mac_is_closing_angle(char const *cp){
288 bool_t rv;
289 NYD2_ENTER;
291 while(spacechar(*cp))
292 ++cp;
294 if((rv = (*cp++ == '}'))){
295 while(spacechar(*cp))
296 ++cp;
297 rv = (*cp == '\0');
299 NYD2_LEAVE;
300 return rv;
303 static struct a_amv_mac *
304 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
305 enum a_amv_mac_flags amf){
306 struct a_amv_mac *amp, **ampp;
307 ui32_t h;
308 enum a_amv_mac_flags save_amf;
309 NYD2_ENTER;
311 save_amf = amf;
312 amf &= a_AMV_MF_TYPE_MASK;
313 h = a_AMV_NAME2HASH(name);
314 h = a_AMV_HASH2PRIME(h);
315 ampp = &a_amv_macs[h];
317 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
318 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
319 !strcmp(amp->am_name, name)){
320 if(n_LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
321 goto jleave;
323 *ampp = amp->am_next;
325 if((amf & a_AMV_MF_ACC) &&
326 account_name != NULL && !strcmp(account_name, name)){
327 amp->am_flags |= a_AMV_MF_DEL;
328 n_err(_("Delayed deletion of active account: %s\n"), name);
329 }else{
330 a_amv_mac_free(amp);
331 amp = (struct a_amv_mac*)-1;
333 goto jleave;
337 if(newamp != NULL){
338 ampp = &a_amv_macs[h];
339 newamp->am_next = *ampp;
340 *ampp = newamp;
341 amp = NULL;
343 jleave:
344 NYD2_LEAVE;
345 return amp;
348 static bool_t
349 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
350 struct a_amv_lostack *losp;
351 struct a_amv_mac_line **amlp;
352 char **args_base, **args;
353 struct a_amv_mac const *amp;
354 bool_t rv;
355 NYD2_ENTER;
357 amp = amcap->amca_amp;
358 /* XXX Unfortunately we yet need to dup the macro lines! :( */
359 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
360 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
361 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
362 *args = NULL;
364 losp = n_lofi_alloc(sizeof *losp);
365 losp->as_global_saved = a_amv_lopts;
366 if((losp->as_amcap = amcap)->amca_unroller == NULL){
367 losp->as_up = losp->as_global_saved;
368 losp->as_lopts = NULL;
369 }else{
370 losp->as_up = NULL;
371 losp->as_lopts = *amcap->amca_unroller;
373 losp->as_unroll = amcap->amca_lopts_on;
375 a_amv_lopts = losp;
376 if(amcap->amca_hook_pre != NULL)
377 (*amcap->amca_hook_pre)(amcap->amca_hook_arg);
378 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base,
379 &a_amv_mac__finalize, losp);
380 NYD2_LEAVE;
381 return rv;
384 static void
385 a_amv_mac__finalize(void *vp){
386 struct a_amv_mac_call_args *amcap;
387 struct a_amv_lostack *losp;
388 NYD2_ENTER;
390 losp = vp;
391 a_amv_lopts = losp->as_global_saved;
393 if((amcap = losp->as_amcap)->amca_unroller == NULL){
394 if(losp->as_lopts != NULL)
395 a_amv_lopts_unroll(&losp->as_lopts);
396 }else
397 *amcap->amca_unroller = losp->as_lopts;
399 if(amcap->amca_ps_hook_mask)
400 pstate &= ~PS_HOOK_MASK;
402 n_lofi_free(losp);
403 n_lofi_free(amcap);
404 NYD2_LEAVE;
407 static bool_t
408 a_amv_mac_show(enum a_amv_mac_flags amf){
409 size_t lc, mc, ti, i;
410 char const *typestr;
411 FILE *fp;
412 bool_t rv;
413 NYD2_ENTER;
415 rv = FAL0;
417 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
418 NULL){
419 n_perr(_("Can't create temporary file for `define' or `account' listing"),
421 goto jleave;
424 amf &= a_AMV_MF_TYPE_MASK;
425 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
427 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
428 struct a_amv_mac *amp;
430 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
431 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
432 struct a_amv_mac_line **amlpp;
434 if(++mc > 1){
435 putc('\n', fp);
436 ++lc;
438 ++lc;
439 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
440 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
441 for(i = (*amlpp)->aml_prespc; i > 0; --i)
442 putc(' ', fp);
443 fputs((*amlpp)->aml_dat, fp);
444 putc('\n', fp);
446 fputs("}\n", fp);
447 ++lc;
451 if(mc > 0)
452 page_or_print(fp, lc);
454 rv = (ferror(fp) == 0);
455 Fclose(fp);
456 jleave:
457 NYD2_LEAVE;
458 return rv;
461 static bool_t
462 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
463 struct str line;
464 ui32_t line_cnt, maxlen;
465 struct linelist{
466 struct linelist *ll_next;
467 struct a_amv_mac_line *ll_amlp;
468 } *llp, *ll_head, *ll_tail;
469 union {size_t s; int i; ui32_t ui; size_t l;} n;
470 struct a_amv_mac *amp;
471 bool_t rv;
472 NYD2_ENTER;
474 memset(&line, 0, sizeof line);
475 rv = FAL0;
476 amp = NULL;
478 /* Read in the lines which form the macro content */
479 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
480 ui32_t leaspc;
481 char *cp;
483 n.i = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, n_empty,
484 &line.s, &line.l, NULL);
485 if(n.ui == 0)
486 continue;
487 if(n.i < 0){
488 n_err(_("Unterminated %s definition: %s\n"),
489 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
490 goto jerr;
492 if(a_amv_mac_is_closing_angle(line.s))
493 break;
495 /* Trim WS, remember amount of leading spaces for display purposes */
496 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
497 if(*cp == '\t')
498 leaspc = (leaspc + 8u) & ~7u;
499 else if(*cp == ' ')
500 ++leaspc;
501 else
502 break;
503 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
505 if(n.ui == 0)
506 continue;
508 maxlen = n_MAX(maxlen, n.ui);
509 cp[n.ui++] = '\0';
511 if(n_LIKELY(++line_cnt < UI32_MAX)){
512 struct a_amv_mac_line *amlp;
514 llp = salloc(sizeof *llp);
515 if(ll_head == NULL)
516 ll_head = llp;
517 else
518 ll_tail->ll_next = llp;
519 ll_tail = llp;
520 llp->ll_next = NULL;
521 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
522 n_VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
523 amlp->aml_len = n.ui -1;
524 amlp->aml_prespc = leaspc;
525 memcpy(amlp->aml_dat, cp, n.ui);
526 }else{
527 n_err(_("Too much content in %s definition: %s\n"),
528 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
529 goto jerr;
533 /* Create the new macro */
534 n.s = strlen(name) +1;
535 amp = smalloc(sizeof(*amp) - n_VFIELD_SIZEOF(struct a_amv_mac, am_name) +
536 n.s);
537 amp->am_next = NULL;
538 amp->am_maxlen = maxlen;
539 amp->am_line_cnt = line_cnt;
540 amp->am_flags = amf;
541 amp->am_lopts = NULL;
542 memcpy(amp->am_name, name, n.s);
543 /* C99 */{
544 struct a_amv_mac_line **amlpp;
546 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
547 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
548 *amlpp++ = llp->ll_amlp;
549 *amlpp = NULL;
552 /* Finally check whether such a macro already exists, in which case we throw
553 * it all away again. At least we know it would have worked */
554 if(a_amv_mac_lookup(name, amp, amf) != NULL){
555 n_err(_("There is already a %s of name: %s\n"),
556 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
557 goto jerr;
560 rv = TRU1;
561 jleave:
562 if(line.s != NULL)
563 free(line.s);
564 NYD2_LEAVE;
565 return rv;
567 jerr:
568 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
569 free(llp->ll_amlp);
570 if(amp != NULL){
571 free(amp->am_line_dat);
572 free(amp);
574 goto jleave;
577 static bool_t
578 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
579 struct a_amv_mac *amp;
580 bool_t rv;
581 NYD2_ENTER;
583 rv = TRU1;
585 if(n_LIKELY(name[0] != '*' || name[1] != '\0')){
586 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
587 n_err(_("%s not defined: %s\n"),
588 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
589 rv = FAL0;
591 }else{
592 struct a_amv_mac **ampp, *lamp;
594 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
595 ++ampp)
596 for(lamp = NULL, amp = *ampp; amp != NULL;){
597 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
598 /* xxx Expensive but rare: be simple */
599 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
600 amp = (lamp == NULL) ? *ampp : lamp->am_next;
601 }else{
602 lamp = amp;
603 amp = amp->am_next;
607 NYD2_LEAVE;
608 return rv;
611 static void
612 a_amv_mac_free(struct a_amv_mac *amp){
613 struct a_amv_mac_line **amlpp;
614 NYD2_ENTER;
616 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
617 free(*amlpp);
618 free(amp->am_line_dat);
619 free(amp);
620 NYD2_LEAVE;
623 static void
624 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
625 struct a_amv_var *oavp){
626 struct a_amv_var *avp;
627 size_t nl, vl;
628 NYD2_ENTER;
630 /* Propagate unrolling up the stack, as necessary */
631 assert(alp != NULL);
632 for(;;){
633 if(alp->as_unroll)
634 break;
635 if((alp = alp->as_up) == NULL)
636 goto jleave;
639 /* Check whether this variable is handled yet */
640 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
641 if(!strcmp(avp->av_name, name))
642 goto jleave;
644 nl = strlen(name) +1;
645 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
646 avp = smalloc(sizeof(*avp) - n_VFIELD_SIZEOF(struct a_amv_var, av_name) +
647 nl + vl);
648 avp->av_link = alp->as_lopts;
649 alp->as_lopts = avp;
650 memcpy(avp->av_name, name, nl);
651 if(vl == 0){
652 avp->av_value = NULL;
653 avp->av_flags = 0;
654 #ifdef HAVE_PUTENV
655 avp->av_env = NULL;
656 #endif
657 }else{
658 avp->av_value = &avp->av_name[nl];
659 avp->av_flags = oavp->av_flags;
660 memcpy(avp->av_value, oavp->av_value, vl);
661 #ifdef HAVE_PUTENV
662 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
663 #endif
665 jleave:
666 NYD2_LEAVE;
669 static void
670 a_amv_lopts_unroll(struct a_amv_var **avpp){
671 struct a_amv_lostack *save_alp;
672 bool_t reset;
673 struct a_amv_var *x, *avp;
674 NYD2_ENTER;
676 avp = *avpp;
677 *avpp = NULL;
678 reset = !(pstate & PS_ROOT);
680 save_alp = a_amv_lopts;
681 a_amv_lopts = NULL;
682 while(avp != NULL){
683 x = avp;
684 avp = avp->av_link;
685 pstate |= PS_ROOT;
686 vok_vset(x->av_name, x->av_value);
687 if(reset)
688 pstate &= ~PS_ROOT;
689 free(x);
691 a_amv_lopts = save_alp;
692 NYD2_LEAVE;
695 static char *
696 a_amv_var_copy(char const *str){
697 char *news;
698 size_t len;
699 NYD2_ENTER;
701 if(*str == '\0')
702 news = n_UNCONST(n_empty);
703 else if(str[1] == '\0'){
704 if(str[0] == '1')
705 news = n_UNCONST(a_amv_var_1);
706 else if(str[0] == '0')
707 news = n_UNCONST(a_amv_var_0);
708 else
709 goto jheap;
710 }else{
711 jheap:
712 len = strlen(str) +1;
713 news = smalloc(len);
714 memcpy(news, str, len);
716 NYD2_LEAVE;
717 return news;
720 static void
721 a_amv_var_free(char *cp){
722 NYD2_ENTER;
723 if(cp[0] != '\0' && cp != a_amv_var_1 && cp != a_amv_var_0)
724 free(cp);
725 NYD2_LEAVE;
728 static bool_t
729 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
730 int flag;
731 bool_t ok;
732 NYD2_ENTER;
734 ok = TRU1;
735 flag = 0;
737 switch(okey){
738 case ok_b_debug:
739 flag = OPT_DEBUG;
740 break;
741 case ok_v_HOME:
742 /* Invalidate any resolved folder then, too
743 * FALLTHRU */
744 case ok_v_folder:
745 ok = !(pstate & PS_ROOT);
746 pstate |= PS_ROOT;
747 ok_vclear(_folder_resolved);
748 if(ok)
749 pstate &= ~PS_ROOT;
750 ok = TRU1;
751 break;
752 case ok_b_header:
753 flag = OPT_N_FLAG;
754 enable = !enable;
755 break;
756 case ok_b_memdebug:
757 flag = OPT_MEMDEBUG;
758 break;
759 case ok_b_POSIXLY_CORRECT:
760 if(!(pstate & PS_ROOT)){
761 bool_t reset = !(pstate & PS_ROOT);
763 pstate |= PS_ROOT;
764 if(enable)
765 ok_bset(posix);
766 else
767 ok_bclear(posix);
768 if(reset)
769 pstate &= ~PS_ROOT;
771 break;
772 case ok_b_posix:
773 if(!(pstate & PS_ROOT)){
774 bool_t reset = !(pstate & PS_ROOT);
776 pstate |= PS_ROOT;
777 if(enable)
778 ok_bset(POSIXLY_CORRECT);
779 else
780 ok_bclear(POSIXLY_CORRECT);
781 if(reset)
782 pstate &= ~PS_ROOT;
784 break;
785 case ok_b_skipemptybody:
786 flag = OPT_E_FLAG;
787 break;
788 case ok_b_typescript_mode:
789 if(enable){
790 ok_bset(colour_disable);
791 ok_bset(line_editor_disable);
792 if(!(pstate & PS_STARTED))
793 ok_bset(termcap_disable);
795 case ok_v_umask:
796 assert(enable);
797 if(**val != '\0'){
798 ul_i ul;
800 if((ul = strtoul(*val, NULL, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
801 n_err(_("Invalid *umask* setting: %s\n"), *val);
802 ok = FAL0;
803 }else
804 umask((mode_t)ul);
806 break;
807 case ok_b_verbose:
808 flag = (enable && !(options & OPT_VERB))
809 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
810 break;
811 default:
812 DBG( n_err("Implementation error: never heard of %u\n", ok); )
813 break;
816 if(flag){
817 if(enable)
818 options |= flag;
819 else
820 options &= ~flag;
822 NYD2_LEAVE;
823 return ok;
826 static bool_t
827 a_amv_var_check_nocntrls(char const *val){
828 char c;
829 NYD2_ENTER;
831 while((c = *val++) != '\0')
832 if(cntrlchar(c))
833 break;
834 NYD2_LEAVE;
835 return (c == '\0');
838 static bool_t
839 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
840 /* TODO The internal/environment variables which are num= or posnum= should
841 * TODO gain special lookup functions, or the return should be void* and
842 * TODO castable to integer; i.e. no more strtoX() should be needed.
843 * TODO I.e., the result of this function should instead be stored.
844 * TODO Use intmax_t IF that is sizeof(void*) only? */
845 bool_t rv;
846 NYD2_ENTER;
848 rv = TRU1;
850 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
851 char *eptr;
852 union {long s; unsigned long u;} i;
854 if(!pos){
855 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
857 if(*eptr != '\0' ||
858 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
859 rv = FAL0;
860 #if INT_MIN != LONG_MIN
861 else if(i.s < INT_MIN)
862 rv = FAL0;
863 #endif
864 #if INT_MAX != LONG_MAX
865 else if(i.s > INT_MAX)
866 rv = FAL0;
867 #endif
868 }else{
869 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
871 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
872 rv = FAL0;
873 #if UINT_MAX != ULONG_MAX
874 else if(i.u > UINT_MAX)
875 rv = FAL0;
876 #endif
879 NYD2_LEAVE;
880 return rv;
883 static char const *
884 a_amv_var_canonify(char const *vn){
885 NYD2_ENTER;
886 if(!upperchar(*vn)){
887 char const *vp;
889 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
891 vn = (*vp == '@') ? i_strdup(vn) : vn;
893 NYD2_LEAVE;
894 return vn;
897 static bool_t
898 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
899 ui32_t hash, i, j;
900 struct a_amv_var_map const *avmp;
901 NYD2_ENTER;
903 avcp->avc_name = name = a_amv_var_canonify(name);
904 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
906 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
907 ui32_t x;
909 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
910 break;
912 avmp = &a_amv_var_map[x];
913 if(avmp->avm_hash == hash &&
914 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
915 avcp->avc_map = avmp;
916 avcp->avc_okey = (enum okeys)x;
917 goto jleave;
920 if(++i == a_AMV_VAR_REV_PRIME){
921 #ifdef a_AMV_VAR_REV_WRAPAROUND
922 i = 0;
923 #else
924 break;
925 #endif
928 avcp->avc_map = NULL;
929 avcp = NULL;
930 jleave:
931 NYD2_LEAVE;
932 return (avcp != NULL);
935 static bool_t
936 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
937 size_t i;
938 char const *cp;
939 struct a_amv_var_map const *avmp;
940 struct a_amv_var *avp;
941 NYD2_ENTER;
943 /* C99 */{
944 struct a_amv_var **avpp, *lavp;
946 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
948 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
949 if(!strcmp(avp->av_name, avcp->avc_name)){
950 /* Relink as head, hope it "sorts on usage" over time.
951 * _clear() relies on this behaviour */
952 if(lavp != NULL){
953 lavp->av_link = avp->av_link;
954 avp->av_link = *avpp;
955 *avpp = avp;
957 goto jleave;
961 /* If this is not an assembled variable we need to consider some special
962 * initialization cases and eventually create the variable anew */
963 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
964 /* Does it have an import-from-environment flag? */
965 if(n_UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
966 if(n_LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
967 /* May be better not to use that one, though? */
968 bool_t isempty, isbltin;
970 isempty = (*cp == '\0' &&
971 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
972 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
973 ) != 0);
975 if(n_UNLIKELY(isempty)){
976 if(!isbltin)
977 goto jerr;
978 }else if(n_LIKELY(*cp != '\0')){
979 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
980 !a_amv_var_check_nocntrls(cp))){
981 n_err(_("Ignoring environment, control characters "
982 "invalid in variable: %s\n"), avcp->avc_name);
983 goto jerr;
985 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
986 !a_amv_var_check_num(cp, FAL0))){
987 n_err(_("Environment variable value not a number "
988 "or out of range: %s\n"), avcp->avc_name);
989 goto jerr;
991 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
992 !a_amv_var_check_num(cp, TRU1))){
993 n_err(_("Environment variable value not a number, "
994 "negative or out of range: %s\n"), avcp->avc_name);
995 goto jerr;
998 goto jnewval;
1002 /* A first-time init switch is to be handled now and here */
1003 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
1004 static struct a_amv_var_defval const **arr,
1005 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
1007 if(arr == NULL){
1008 arr = &arr_base[0];
1009 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1010 while(i-- > 0)
1011 arr[i] = &a_amv_var_i3vals[i];
1014 for(i = 0; arr[i] != NULL; ++i)
1015 if(arr[i]->avdv_okey == avcp->avc_okey){
1016 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1017 : arr[i]->avdv_value;
1018 /* Remove this entry, hope entire block becomes no-op asap */
1020 arr[i] = arr[i + 1];
1021 while(arr[i++] != NULL);
1023 if(!i3val_nonew)
1024 goto jnewval;
1025 if(i3val_nonew == TRUM1)
1026 avp = (struct a_amv_var*)-1;
1027 goto jleave;
1031 /* The virtual variables */
1032 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
1033 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
1034 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
1035 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
1036 goto jleave;
1038 /* Not reached */
1041 /* Place this last because once it is set first the variable will never
1042 * be removed again and thus match in the first block above */
1043 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
1044 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
1045 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
1046 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1047 : a_amv_var_defvals[i].avdv_value;
1048 goto jnewval;
1053 jerr:
1054 avp = NULL;
1055 jleave:
1056 avcp->avc_var = avp;
1057 NYD2_LEAVE;
1058 return (avp != NULL);
1060 jnewval: /* C99 */{
1061 struct a_amv_var **avpp;
1062 size_t l;
1064 l = strlen(avcp->avc_name) +1;
1065 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1066 n_VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1067 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1068 *avpp = avp;
1069 memcpy(avp->av_name, avcp->avc_name, l);
1070 avp->av_value = a_amv_var_copy(cp);
1071 #ifdef HAVE_PUTENV
1072 avp->av_env = NULL;
1073 #endif
1074 avp->av_flags = avmp->avm_flags;
1076 if(avp->av_flags & a_AMV_VF_VIP)
1077 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1078 if(avp->av_flags & a_AMV_VF_ENV)
1079 a_amv_var__putenv(avcp, avp);
1080 goto jleave;
1084 static bool_t
1085 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1086 bool_t force_env){
1087 struct a_amv_var *avp;
1088 char *oval;
1089 struct a_amv_var_map const *avmp;
1090 bool_t rv;
1091 NYD2_ENTER;
1093 if(value == NULL){
1094 rv = a_amv_var_clear(avcp, force_env);
1095 goto jleave;
1098 if((avmp = avcp->avc_map) != NULL){
1099 rv = FAL0;
1101 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1102 !(pstate & PS_ROOT))){
1103 value = N_("Variable is readonly: %s\n");
1104 goto jeavmp;
1106 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1107 value = N_("Variable must not be empty: %s\n");
1108 goto jeavmp;
1110 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1111 !a_amv_var_check_nocntrls(value))){
1112 value = N_("Variable forbids control characters: %s\n");
1113 goto jeavmp;
1115 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1116 !a_amv_var_check_num(value, FAL0))){
1117 value = N_("Variable value not a number or out of range: %s\n");
1118 goto jeavmp;
1120 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1121 !a_amv_var_check_num(value, TRU1))){
1122 value = _("Variable value not a number, negative, "
1123 "or out of range: %s\n");
1124 goto jeavmp;
1126 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1127 !(pstate & (PS_ROOT | PS_STARTED)))){
1128 value = N_("Variable cannot be set in a resource file: %s\n");
1129 jeavmp:
1130 n_err(V_(value), avcp->avc_name);
1131 goto jleave;
1135 rv = TRU1;
1136 a_amv_var_lookup(avcp, TRU1);
1138 /* Don't care what happens later on, store this in the unroll list */
1139 if(a_amv_lopts != NULL &&
1140 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1141 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1143 if((avp = avcp->avc_var) == NULL){
1144 struct a_amv_var **avpp;
1145 size_t l;
1147 l = strlen(avcp->avc_name) +1;
1148 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1149 n_VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1150 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1151 *avpp = avp;
1152 #ifdef HAVE_PUTENV
1153 avp->av_env = NULL;
1154 #endif
1155 memcpy(avp->av_name, avcp->avc_name, l);
1156 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1157 oval = n_UNCONST(n_empty);
1158 }else
1159 oval = avp->av_value;
1161 if(avmp == NULL)
1162 avp->av_value = a_amv_var_copy(value);
1163 else{
1164 /* Via `set' etc. the user may give even boolean options non-boolean
1165 * values, ignore that and force boolean */
1166 if(avp->av_flags & a_AMV_VF_BOOL){
1167 if((options & OPT_D_VV) && *value != '\0')
1168 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1169 avcp->avc_name, value);
1170 avp->av_value = n_UNCONST(a_amv_var_1);
1171 }else
1172 avp->av_value = a_amv_var_copy(value);
1174 /* Check if update allowed XXX wasteful on error! */
1175 if((avp->av_flags & a_AMV_VF_VIP) &&
1176 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1177 char *cp = avp->av_value;
1179 avp->av_value = oval;
1180 oval = cp;
1184 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1185 avp->av_flags |= a_AMV_VF_LINKED;
1186 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1187 rv = a_amv_var__putenv(avcp, avp);
1189 a_amv_var_free(oval);
1190 jleave:
1191 NYD2_LEAVE;
1192 return rv;
1195 static bool_t
1196 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1197 #ifndef HAVE_SETENV
1198 char *cp;
1199 #endif
1200 bool_t rv;
1201 NYD2_ENTER;
1203 #ifdef HAVE_SETENV
1204 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1205 #else
1206 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1208 if((rv = (putenv(cp) == 0))){
1209 char *ocp;
1211 if((ocp = avp->av_env) != NULL)
1212 free(ocp);
1213 avp->av_env = cp;
1214 }else
1215 free(cp);
1216 #endif
1217 NYD2_LEAVE;
1218 return rv;
1221 static bool_t
1222 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1223 struct a_amv_var **avpp, *avp;
1224 struct a_amv_var_map const *avmp;
1225 bool_t rv;
1226 NYD2_ENTER;
1228 rv = FAL0;
1230 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1231 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1232 !(pstate & PS_ROOT))){
1233 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1234 goto jleave;
1236 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1237 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1238 goto jleave;
1241 rv = TRU1;
1243 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1244 if(force_env){
1245 jforce_env:
1246 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1247 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1248 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1249 goto jleave;
1250 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1251 avcp->avc_var = NULL;
1252 if(force_env)
1253 goto jforce_env;
1254 goto jleave;
1257 if(a_amv_lopts != NULL &&
1258 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1259 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1261 avp = avcp->avc_var;
1262 avcp->avc_var = NULL;
1263 avpp = &a_amv_vars[avcp->avc_prime];
1264 assert(*avpp == avp); /* (always listhead after lookup()) */
1265 *avpp = (*avpp)->av_link;
1267 /* C99 */{
1268 #ifdef HAVE_SETENV
1269 char *envval = NULL;
1270 #else
1271 char *envval = avp->av_env;
1272 #endif
1273 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1274 rv = a_amv_var__clearenv(avp->av_name, envval);
1276 a_amv_var_free(avp->av_value);
1277 free(avp);
1279 /* XXX Fun part, extremely simple-minded for now: if this variable has
1280 * XXX a default value, immediately reinstantiate it! */
1281 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1282 a_amv_var_lookup(avcp, TRU1);
1283 jleave:
1284 NYD2_LEAVE;
1285 return rv;
1288 static bool_t
1289 a_amv_var__clearenv(char const *name, char *value){
1290 #ifndef HAVE_SETENV
1291 extern char **environ;
1292 char **ecpp;
1293 #endif
1294 bool_t rv;
1295 NYD2_ENTER;
1296 n_UNUSED(value);
1298 #ifdef HAVE_SETENV
1299 unsetenv(name);
1300 rv = TRU1;
1301 #else
1302 if(value != NULL)
1303 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1304 if(*ecpp == value){
1305 free(value);
1307 ecpp[0] = ecpp[1];
1308 while(*ecpp++ != NULL);
1309 break;
1311 rv = TRU1;
1312 #endif
1313 NYD2_LEAVE;
1314 return rv;
1317 static void
1318 a_amv_var_show_all(void){
1319 struct n_string msg, *msgp;
1320 FILE *fp;
1321 size_t no, i;
1322 struct a_amv_var *avp;
1323 char const **vacp, **cap;
1324 NYD2_ENTER;
1326 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1327 n_perr(_("Can't create temporary file for `set' listing"), 0);
1328 goto jleave;
1331 /* We need to instantiate first-time-inits and default values here, so that
1332 * they will be regular members of our _vars[] table */
1333 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1334 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1335 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1336 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1338 for(no = i = 0; i < a_AMV_PRIME; ++i)
1339 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1340 ++no;
1341 no += a_AMV_VAR_VIRTS_CNT;
1343 vacp = salloc(no * sizeof(*vacp));
1345 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1346 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1347 *cap++ = avp->av_name;
1348 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1349 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1351 if(no > 1)
1352 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1354 msgp = &msg;
1355 msgp = n_string_reserve(n_string_creat(msgp), 80);
1356 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1357 i += a_amv_var_show(*cap, fp, msgp);
1358 n_string_gut(&msg);
1360 page_or_print(fp, i);
1361 Fclose(fp);
1362 jleave:
1363 NYD2_LEAVE;
1366 static int
1367 a_amv_var__show_cmp(void const *s1, void const *s2){
1368 int rv;
1369 NYD2_ENTER;
1371 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1372 NYD2_LEAVE;
1373 return rv;
1376 static size_t
1377 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1378 struct a_amv_var_carrier avc;
1379 char const *quote;
1380 size_t i;
1381 NYD2_ENTER;
1383 msgp = n_string_trunc(msgp, 0);
1384 i = 0;
1386 a_amv_var_revlookup(&avc, name);
1387 if(!a_amv_var_lookup(&avc, FAL0)){
1388 struct str s;
1390 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1391 s.s = n_UNCONST(name);
1392 s.l = UIZ_MAX;
1393 msgp = n_shexp_quote(msgp, &s, FAL0);
1394 goto jleave;
1397 if(options & OPT_D_V){
1398 if(avc.avc_map == NULL){
1399 msgp = n_string_push_c(msgp, '#');
1400 msgp = n_string_push_cp(msgp, "assembled");
1401 i = 1;
1403 /* C99 */{
1404 struct{
1405 ui16_t flags;
1406 char msg[22];
1407 } const tbase[] = {
1408 {a_AMV_VF_VIRT, "virtual"},
1409 {a_AMV_VF_RDONLY, "readonly"},
1410 {a_AMV_VF_NODEL, "nodelete"},
1411 {a_AMV_VF_NOTEMPTY, "notempty"},
1412 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1413 {a_AMV_VF_NUM, "number"},
1414 {a_AMV_VF_POSNUM, "positive-number"},
1415 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1416 {a_AMV_VF_ENV, "sync-environ"},
1417 {a_AMV_VF_I3VAL, "initial-value"},
1418 {a_AMV_VF_DEFVAL, "default-value"},
1419 {a_AMV_VF_LINKED, "`environ' link"}
1420 }, *tp;
1422 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1423 if(avc.avc_var->av_flags & tp->flags){
1424 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1425 msgp = n_string_push_cp(msgp, tp->msg);
1429 if(i > 0)
1430 msgp = n_string_push_cp(msgp, "\n ");
1433 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1434 msgp = n_string_push_cp(msgp, "# ");
1435 n_UNINIT(quote, NULL);
1436 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1437 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1438 if(strcmp(quote, avc.avc_var->av_value))
1439 msgp = n_string_push_cp(msgp, "wysh ");
1441 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1442 msgp = n_string_push_cp(msgp, "environ ");
1443 msgp = n_string_push_cp(msgp, "set ");
1444 msgp = n_string_push_cp(msgp, name);
1445 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1446 msgp = n_string_push_c(msgp, '=');
1447 msgp = n_string_push_cp(msgp, quote);
1450 jleave:
1451 msgp = n_string_push_c(msgp, '\n');
1452 fputs(n_string_cp(msgp), fp);
1453 NYD2_ENTER;
1454 return (i > 0 ? 2 : 1);
1457 static bool_t
1458 a_amv_var_c_set(char **ap, bool_t issetenv){
1459 char *cp, *cp2, *varbuf, c;
1460 size_t errs;
1461 NYD2_ENTER;
1463 errs = 0;
1464 jouter:
1465 while((cp = *ap++) != NULL){
1466 /* Isolate key */
1467 cp2 = varbuf = salloc(strlen(cp) +1);
1469 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1470 if(cntrlchar(c) || whitechar(c)){
1471 n_err(_("Variable name with control character ignored: %s\n"),
1472 ap[-1]);
1473 ++errs;
1474 goto jouter;
1476 *cp2++ = c;
1478 *cp2 = '\0';
1479 if(c == '\0')
1480 cp = n_UNCONST(n_empty);
1481 else
1482 ++cp;
1484 if(varbuf == cp2){
1485 n_err(_("Empty variable name ignored\n"));
1486 ++errs;
1487 }else{
1488 struct a_amv_var_carrier avc;
1489 bool_t isunset;
1491 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1492 varbuf = &varbuf[2];
1494 a_amv_var_revlookup(&avc, varbuf);
1496 if(isunset)
1497 errs += !a_amv_var_clear(&avc, issetenv);
1498 else
1499 errs += !a_amv_var_set(&avc, cp, issetenv);
1502 NYD2_LEAVE;
1503 return (errs == 0);
1506 FL int
1507 c_define(void *v){
1508 int rv;
1509 char **args;
1510 NYD_ENTER;
1512 rv = 1;
1514 if((args = v)[0] == NULL){
1515 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1516 goto jleave;
1519 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1520 args[2] != NULL){
1521 n_err(_("Synopsis: define: <name> {\n"));
1522 goto jleave;
1525 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1526 jleave:
1527 NYD_LEAVE;
1528 return rv;
1531 FL int
1532 c_undefine(void *v){
1533 int rv;
1534 char **args;
1535 NYD_ENTER;
1537 rv = 0;
1538 args = v;
1540 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1541 while(*++args != NULL);
1542 NYD_LEAVE;
1543 return rv;
1546 FL int
1547 c_call(void *v){
1548 struct a_amv_mac_call_args *amcap;
1549 char const *errs, *name;
1550 struct a_amv_mac *amp;
1551 char **args;
1552 int rv;
1553 NYD_ENTER;
1555 rv = 1;
1557 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1558 errs = _("Synopsis: call: <%s>\n");
1559 name = "name";
1560 goto jerr;
1563 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1564 errs = _("Undefined macro `call'ed: %s\n");
1565 goto jerr;
1568 amcap = n_lofi_alloc(sizeof *amcap);
1569 memset(amcap, 0, sizeof *amcap);
1570 amcap->amca_name = name;
1571 amcap->amca_amp = amp;
1572 rv = (a_amv_mac_exec(amcap) == FAL0);
1573 jleave:
1574 NYD_LEAVE;
1575 return rv;
1576 jerr:
1577 n_err(errs, name);
1578 goto jleave;
1581 FL bool_t
1582 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1583 struct a_amv_mac_call_args *amcap;
1584 struct a_amv_mac *amp;
1585 size_t len;
1586 char *var, *cp;
1587 bool_t rv;
1588 NYD_ENTER;
1590 rv = TRU1;
1591 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1593 /* First try the fully resolved path */
1594 snprintf(var, len, "folder-hook-%s", mailname);
1595 if((cp = vok_vlook(var)) != NULL)
1596 goto jmac;
1598 /* If we are under *folder*, try the usual +NAME syntax, too */
1599 if(displayname[0] == '+'){
1600 char *x;
1602 for(x = &mailname[len]; x != mailname; --x)
1603 if(x[-1] == '/'){
1604 snprintf(var, len, "folder-hook-+%s", x);
1605 if((cp = vok_vlook(var)) != NULL)
1606 goto jmac;
1607 break;
1611 /* Plain *folder-hook* is our last try */
1612 if((cp = ok_vlook(folder_hook)) == NULL)
1613 goto jleave;
1615 jmac:
1616 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1617 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1618 n_shexp_quote_cp(displayname, FAL0), cp);
1619 rv = FAL0;
1620 goto jleave;
1623 amcap = n_lofi_alloc(sizeof *amcap);
1624 memset(amcap, 0, sizeof *amcap);
1625 amcap->amca_name = cp;
1626 amcap->amca_amp = amp;
1627 pstate &= ~PS_HOOK_MASK;
1628 if(nmail){
1629 amcap->amca_unroller = NULL;
1630 pstate |= PS_HOOK_NEWMAIL;
1631 }else{
1632 amcap->amca_unroller = &a_amv_folder_hook_lopts;
1633 pstate |= PS_HOOK;
1635 amcap->amca_lopts_on = TRU1;
1636 amcap->amca_ps_hook_mask = TRU1;
1637 rv = a_amv_mac_exec(amcap);
1638 pstate &= ~PS_HOOK_MASK;
1640 jleave:
1641 NYD_LEAVE;
1642 return rv;
1645 FL int
1646 c_account(void *v){
1647 struct a_amv_mac_call_args *amcap;
1648 struct a_amv_mac *amp;
1649 char **args;
1650 int rv, i, oqf, nqf;
1651 NYD_ENTER;
1653 rv = 1;
1655 if((args = v)[0] == NULL){
1656 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1657 goto jleave;
1660 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1661 if(args[2] != NULL){
1662 n_err(_("Synopsis: account: <name> {\n"));
1663 goto jleave;
1665 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1666 n_err(_("`account': cannot use reserved name: %s\n"),
1667 ACCOUNT_NULL);
1668 goto jleave;
1670 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1671 goto jleave;
1674 if(pstate & PS_HOOK_MASK){
1675 n_err(_("`account': can't change account from within a hook\n"));
1676 goto jleave;
1679 save_mbox_for_possible_quitstuff();
1681 amp = NULL;
1682 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1683 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1684 n_err(_("`account': account does not exist: %s\n"), args[0]);
1685 goto jleave;
1688 oqf = savequitflags();
1690 if(a_amv_acc_curr != NULL){
1691 if(a_amv_acc_curr->am_lopts != NULL)
1692 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1693 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1694 a_amv_mac_free(a_amv_acc_curr);
1697 account_name = (amp != NULL) ? amp->am_name : NULL;
1698 a_amv_acc_curr = amp;
1700 if(amp != NULL){
1701 bool_t ok;
1702 assert(amp->am_lopts == NULL);
1703 amcap = n_lofi_alloc(sizeof *amcap);
1704 memset(amcap, 0, sizeof *amcap);
1705 amcap->amca_name = amp->am_name;
1706 amcap->amca_amp = amp;
1707 amcap->amca_unroller = &amp->am_lopts;
1708 amcap->amca_lopts_on = TRU1;
1709 ok = a_amv_mac_exec(amcap);
1710 if(!ok){
1711 /* XXX account switch incomplete, unroll? */
1712 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1713 goto jleave;
1717 /* C99 */{
1718 bool_t reset = !(pstate & PS_ROOT);
1720 pstate |= PS_ROOT;
1721 if(amp != NULL)
1722 ok_vset(_account_name, amp->am_name);
1723 else
1724 ok_vclear(_account_name);
1725 if(reset)
1726 pstate &= ~PS_ROOT;
1729 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1730 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1731 restorequitflags(oqf);
1732 if((i = setfile("%", 0)) < 0)
1733 goto jleave;
1734 check_folder_hook(FAL0);
1735 if(i > 0 && !ok_blook(emptystart))
1736 goto jleave;
1737 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1738 restorequitflags(nqf);
1740 rv = 0;
1741 jleave:
1742 NYD_LEAVE;
1743 return rv;
1746 FL int
1747 c_unaccount(void *v){
1748 int rv;
1749 char **args;
1750 NYD_ENTER;
1752 rv = 0;
1753 args = v;
1755 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1756 while(*++args != NULL);
1757 NYD_LEAVE;
1758 return rv;
1761 FL int
1762 c_localopts(void *v){
1763 char **argv;
1764 int rv;
1765 NYD_ENTER;
1767 rv = 1;
1769 if(a_amv_lopts == NULL){
1770 n_err(_("Cannot use `localopts' in this context "
1771 "(not in `define' or `account', nor special hook)\n"));
1772 goto jleave;
1775 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1776 rv = 0;
1777 jleave:
1778 NYD_LEAVE;
1779 return rv;
1782 FL void
1783 temporary_call_compose_mode_hook(char const *macname,
1784 void (*hook_pre)(void *), void *hook_arg){
1785 /* TODO call_compose_mode_hook() temporary, v15: generalize; see a_LEX_SLICE
1786 * TODO comment in lex_input.c for the right way of doing things! */
1787 static struct a_amv_lostack *cmh_losp;
1788 struct a_amv_mac_call_args *amcap;
1789 struct a_amv_mac *amp;
1790 NYD_ENTER;
1792 amp = NULL;
1794 if(macname == (char*)-1){
1795 a_amv_mac__finalize(cmh_losp);
1796 cmh_losp = NULL;
1797 }else if(macname != NULL &&
1798 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1799 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1800 macname);
1801 else{
1802 amcap = n_lofi_alloc(sizeof *amcap);
1803 memset(amcap, 0, sizeof *amcap);
1804 amcap->amca_name = (macname != NULL) ? macname : "on-compose-done-shell";
1805 amcap->amca_amp = amp;
1806 amcap->amca_unroller = &a_amv_compose_lopts;
1807 amcap->amca_hook_pre = hook_pre;
1808 amcap->amca_hook_arg = hook_arg;
1809 amcap->amca_lopts_on = TRU1;
1810 amcap->amca_ps_hook_mask = TRU1;
1811 pstate &= ~PS_HOOK_MASK;
1812 pstate |= PS_HOOK;
1813 if(macname != NULL)
1814 a_amv_mac_exec(amcap);
1815 else{
1816 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
1817 cmh_losp->as_global_saved = a_amv_lopts;
1818 cmh_losp->as_up = NULL;
1819 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
1820 cmh_losp->as_unroll = TRU1;
1821 a_amv_lopts = cmh_losp;
1824 NYD_LEAVE;
1827 FL void
1828 temporary_unroll_compose_mode(void){ /* XXX intermediate hack */
1829 NYD_ENTER;
1830 if(a_amv_compose_lopts != NULL){
1831 void *save = a_amv_lopts;
1833 a_amv_lopts = NULL;
1834 a_amv_lopts_unroll(&a_amv_compose_lopts);
1835 a_amv_compose_lopts = NULL;
1836 a_amv_lopts = save;
1838 NYD_LEAVE;
1841 FL void
1842 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1843 NYD_ENTER;
1844 if(a_amv_folder_hook_lopts != NULL){
1845 void *save = a_amv_lopts;
1847 a_amv_lopts = NULL;
1848 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1849 a_amv_folder_hook_lopts = NULL;
1850 a_amv_lopts = save;
1852 NYD_LEAVE;
1855 FL char *
1856 n_var_oklook(enum okeys okey){
1857 struct a_amv_var_carrier avc;
1858 char *rv;
1859 struct a_amv_var_map const *avmp;
1860 NYD_ENTER;
1862 avc.avc_map = avmp = &a_amv_var_map[okey];
1863 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1864 avc.avc_hash = avmp->avm_hash;
1865 avc.avc_okey = okey;
1867 if(a_amv_var_lookup(&avc, FAL0))
1868 rv = avc.avc_var->av_value;
1869 else
1870 rv = NULL;
1871 NYD_LEAVE;
1872 return rv;
1875 FL bool_t
1876 n_var_okset(enum okeys okey, uintptr_t val){
1877 struct a_amv_var_carrier avc;
1878 bool_t ok;
1879 struct a_amv_var_map const *avmp;
1880 NYD_ENTER;
1882 avc.avc_map = avmp = &a_amv_var_map[okey];
1883 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1884 avc.avc_hash = avmp->avm_hash;
1885 avc.avc_okey = okey;
1887 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1888 NYD_LEAVE;
1889 return ok;
1892 FL bool_t
1893 n_var_okclear(enum okeys okey){
1894 struct a_amv_var_carrier avc;
1895 bool_t rv;
1896 struct a_amv_var_map const *avmp;
1897 NYD_ENTER;
1899 avc.avc_map = avmp = &a_amv_var_map[okey];
1900 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1901 avc.avc_hash = avmp->avm_hash;
1902 avc.avc_okey = okey;
1904 rv = a_amv_var_clear(&avc, FAL0);
1905 NYD_LEAVE;
1906 return rv;
1909 FL char *
1910 n_var_voklook(char const *vokey){
1911 struct a_amv_var_carrier avc;
1912 char *rv;
1913 NYD_ENTER;
1915 a_amv_var_revlookup(&avc, vokey);
1917 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1918 NYD_LEAVE;
1919 return rv;
1922 FL bool_t
1923 n_var_vokset(char const *vokey, uintptr_t val){
1924 struct a_amv_var_carrier avc;
1925 bool_t ok;
1926 NYD_ENTER;
1928 a_amv_var_revlookup(&avc, vokey);
1930 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1931 NYD_LEAVE;
1932 return ok;
1935 FL bool_t
1936 n_var_vokclear(char const *vokey){
1937 struct a_amv_var_carrier avc;
1938 bool_t ok;
1939 NYD_ENTER;
1941 a_amv_var_revlookup(&avc, vokey);
1943 ok = a_amv_var_clear(&avc, FAL0);
1944 NYD_LEAVE;
1945 return ok;
1948 #ifdef HAVE_SOCKETS
1949 FL char *
1950 n_var_xoklook(enum okeys okey, struct url const *urlp,
1951 enum okey_xlook_mode oxm){
1952 struct a_amv_var_carrier avc;
1953 struct str const *us;
1954 size_t nlen;
1955 char *nbuf, *rv;
1956 struct a_amv_var_map const *avmp;
1957 NYD_ENTER;
1959 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1961 /* For simplicity: allow this case too */
1962 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
1963 nbuf = NULL;
1964 goto jplain;
1967 avc.avc_map = avmp = &a_amv_var_map[okey];
1968 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1969 avc.avc_okey = okey;
1971 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1972 nlen = strlen(avc.avc_name);
1973 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
1974 memcpy(nbuf, avc.avc_name, nlen);
1975 nbuf[nlen++] = '-';
1977 /* One of .url_u_h_p and .url_h_p we test in here */
1978 memcpy(nbuf + nlen, us->s, us->l +1);
1979 avc.avc_name = a_amv_var_canonify(nbuf);
1980 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1981 if(a_amv_var_lookup(&avc, FAL0))
1982 goto jvar;
1984 /* The second */
1985 if(oxm & OXM_H_P){
1986 us = &urlp->url_h_p;
1987 memcpy(nbuf + nlen, us->s, us->l +1);
1988 avc.avc_name = a_amv_var_canonify(nbuf);
1989 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1990 if(a_amv_var_lookup(&avc, FAL0)){
1991 jvar:
1992 rv = avc.avc_var->av_value;
1993 goto jleave;
1997 jplain:
1998 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1999 jleave:
2000 if(nbuf != NULL)
2001 n_lofi_free(nbuf);
2002 NYD_LEAVE;
2003 return rv;
2005 #endif /* HAVE_SOCKETS */
2007 FL int
2008 c_set(void *v){
2009 char **ap;
2010 int err;
2011 NYD_ENTER;
2013 if(*(ap = v) == NULL){
2014 a_amv_var_show_all();
2015 err = 0;
2016 }else
2017 err = !a_amv_var_c_set(ap, FAL0);
2018 NYD_LEAVE;
2019 return err;
2022 FL int
2023 c_unset(void *v){
2024 char **ap;
2025 int err;
2026 NYD_ENTER;
2028 for(err = 0, ap = v; *ap != NULL; ++ap)
2029 err |= !n_var_vokclear(*ap);
2030 NYD_LEAVE;
2031 return err;
2034 FL int
2035 c_varshow(void *v){
2036 char **ap;
2037 NYD_ENTER;
2039 if(*(ap = v) == NULL)
2040 v = NULL;
2041 else{
2042 struct n_string msg, *msgp = &msg;
2044 msgp = n_string_creat(msgp);
2045 for(; *ap != NULL; ++ap)
2046 a_amv_var_show(*ap, stdout, msgp);
2047 n_string_gut(msgp);
2049 NYD_LEAVE;
2050 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2053 FL int
2054 c_varedit(void *v){
2055 struct a_amv_var_carrier avc;
2056 FILE *of, *nf;
2057 char *val, **argv;
2058 int err;
2059 sighandler_type sigint;
2060 NYD_ENTER;
2062 sigint = safe_signal(SIGINT, SIG_IGN);
2064 for(err = 0, argv = v; *argv != NULL; ++argv){
2065 memset(&avc, 0, sizeof avc);
2067 a_amv_var_revlookup(&avc, *argv);
2069 if(avc.avc_map != NULL){
2070 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2071 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2072 avc.avc_name);
2073 continue;
2075 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2076 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2077 avc.avc_name);
2078 continue;
2082 a_amv_var_lookup(&avc, FAL0);
2084 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2085 NULL){
2086 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2087 err = 1;
2088 break;
2089 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2090 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2091 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2092 Fclose(of);
2093 err = 1;
2094 continue;
2097 fflush_rewind(of);
2098 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2099 Fclose(of);
2101 if(nf != NULL){
2102 int c;
2103 char *base;
2104 off_t l;
2106 l = fsize(nf);
2107 assert(l >= 0);
2108 base = salloc((size_t)l +1);
2110 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2111 if(c == '\n' || c == '\r'){
2112 *val = ' ';
2113 ++l;
2114 }else{
2115 *val = (char)(uc_i)c;
2116 l = 0;
2118 val -= l;
2119 *val = '\0';
2121 if(!a_amv_var_set(&avc, base, FAL0))
2122 err = 1;
2124 Fclose(nf);
2125 }else{
2126 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2127 err = 1;
2128 break;
2132 safe_signal(SIGINT, sigint);
2133 NYD_LEAVE;
2134 return err;
2137 FL int
2138 c_environ(void *v){
2139 struct a_amv_var_carrier avc;
2140 int err;
2141 char **ap;
2142 bool_t islnk;
2143 NYD_ENTER;
2145 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2146 for(err = 0; *++ap != NULL;){
2147 a_amv_var_revlookup(&avc, *ap);
2149 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2150 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2151 if(!islnk){
2152 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2153 continue;
2154 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2155 if(options & OPT_D_V)
2156 n_err(_("`environ': link: already established: %s\n"), *ap);
2157 continue;
2159 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2160 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2161 a_amv_var__putenv(&avc, avc.avc_var);
2162 }else if(!islnk){
2163 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2164 err = 1;
2165 }else{
2166 char const *evp = getenv(*ap);
2168 if(evp != NULL)
2169 err |= !a_amv_var_set(&avc, evp, TRU1);
2170 else{
2171 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2172 *ap);
2173 err = 1;
2177 }else if(!strcmp(*ap, "set"))
2178 err = !a_amv_var_c_set(++ap, TRU1);
2179 else if(!strcmp(*ap, "unset")){
2180 for(err = 0; *++ap != NULL;){
2181 a_amv_var_revlookup(&avc, *ap);
2183 if(!a_amv_var_clear(&avc, TRU1))
2184 err = 1;
2186 }else{
2187 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2188 err = 1;
2190 NYD_LEAVE;
2191 return err;
2194 /* s-it-mode */