dotlock.c: fix false indentation that sneaked into [ea12691]
[s-mailx.git] / accmacvar.c
blob58f57bfa5aed5759380e9f25a0d14527a78b8839
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run mk-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . should be recursive environment based
8 *@ TODO . undefining and overwriting a macro should always be possible:
9 *@ TODO simply place the thing in a delete-later list and replace the
10 *@ TODO accessible entry! (instant delete if on top recursion level.)
11 *@ TODO . Likewise, overwriting an existing should be like delete+create
12 *@ TODO . once we can have non-fatal !0 returns for commands, we should
13 *@ TODO return error if "(environ)? unset" goes for non-existent.
15 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
16 * Copyright (c) 2012 - 2016 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
24 * are met:
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
44 * SUCH DAMAGE.
46 #undef n_FILE
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
50 # include "nail.h"
51 #endif
53 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
54 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
55 #endif
57 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
58 #define a_AMV_PRIME HSHSIZE
59 #define a_AMV_NAME2HASH(N) torek_hash(N)
60 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
62 enum a_amv_mac_flags{
63 a_AMV_MF_NONE = 0,
64 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
65 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
66 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
67 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
68 a_AMV_MF__MAX = 0xFF
71 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
72 * _IMPORT implies _ENV; it doesn't verify anything... */
73 enum a_amv_var_flags{
74 a_AMV_VF_NONE = 0,
75 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
76 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
77 a_AMV_VF_RDONLY = 1<<2, /* May not be set by user */
78 a_AMV_VF_NODEL = 1<<3, /* May not be deleted */
79 a_AMV_VF_NOTEMPTY = 1<<4, /* May not be assigned an empty value */
80 a_AMV_VF_NOCNTRLS = 1<<5, /* Value may not contain control characters */
81 a_AMV_VF_NUM = 1<<6, /* Value must be a 32-bit number */
82 a_AMV_VF_POSNUM = 1<<7, /* Value must be positive 32-bit number */
83 a_AMV_VF_VIP = 1<<8, /* Wants _var_check_vips() evaluation */
84 a_AMV_VF_IMPORT = 1<<9, /* Import ONLY from environ (before PS_STARTED) */
85 a_AMV_VF_ENV = 1<<10, /* Update environment on change */
86 a_AMV_VF_I3VAL = 1<<11, /* Has an initial value */
87 a_AMV_VF_DEFVAL = 1<<12, /* Has a default value */
88 a_AMV_VF_LINKED = 1<<13, /* `environ' linked */
89 a_AMV_VF__MASK = (1<<(13+1)) - 1
92 struct a_amv_mac{
93 struct a_amv_mac *am_next;
94 ui32_t am_maxlen; /* of any line in .am_line_dat */
95 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
96 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
97 struct a_amv_var *am_lopts; /* `localopts' unroll list */
98 ui8_t am_flags; /* enum a_amv_mac_flags */
99 char am_name[VFIELD_SIZE(7)]; /* of this macro */
101 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
103 struct a_amv_mac_line{
104 ui32_t aml_len;
105 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
106 char aml_dat[VFIELD_SIZE(0)];
109 struct a_amv_mac_call_args{
110 char const *amca_name;
111 struct a_amv_mac const *amca_amp;
112 struct a_amv_var **amca_unroller;
113 void (*amca_hook_pre)(void *);
114 void (*amca_hook_post)(void *);
115 void *amca_hook_arg;
116 bool_t amca_lopts_on;
117 ui8_t amca__pad[7];
120 struct a_amv_lostack{
121 struct a_amv_lostack *as_up; /* Outer context */
122 struct a_amv_mac *as_mac; /* Context (`account' or `define') */
123 struct a_amv_var *as_lopts;
124 bool_t as_unroll; /* Unrolling enabled? */
125 ui8_t avs__pad[7];
128 struct a_amv_var{
129 struct a_amv_var *av_link;
130 char *av_value;
131 #ifdef HAVE_PUTENV
132 char *av_env; /* Actively managed putenv(3) memory */
133 #endif
134 ui16_t av_flags; /* enum a_amv_var_flags */
135 char av_name[VFIELD_SIZE(6)];
137 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
139 struct a_amv_var_map{
140 ui32_t avm_hash;
141 ui16_t avm_keyoff;
142 ui16_t avm_flags; /* enum a_amv_var_flags */
144 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
146 struct a_amv_var_virt{
147 ui32_t avv_okey;
148 ui8_t avv__dummy[4];
149 struct a_amv_var const *avv_var;
152 struct a_amv_var_defval{
153 ui32_t avdv_okey;
154 ui8_t avdv__pad[4];
155 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
158 struct a_amv_var_carrier{
159 char const *avc_name;
160 ui32_t avc_hash;
161 ui32_t avc_prime;
162 struct a_amv_var *avc_var;
163 struct a_amv_var_map const *avc_map;
164 enum okeys avc_okey;
165 ui8_t avc__pad[4];
168 /* Include the constant mk-okey-map.pl output */
169 #include "version.h"
170 #include "okeys.h"
172 /* The currently active account */
173 static struct a_amv_mac *a_amv_acc_curr;
175 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
177 /* Unroll list of currently running macro stack */
178 static struct a_amv_lostack *a_amv_lopts;
180 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
182 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
183 * TODO today via a static lostack, it should be a field in mailbox, once that
184 * TODO is a real multi-instance object */
185 static struct a_amv_var *a_amv_folder_hook_lopts;
187 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
188 static struct a_amv_var *a_amv_compose_lopts;
190 /* Does cp consist solely of WS and a } */
191 static bool_t a_amv_mac_is_closing_angle(char const *cp);
193 /* Lookup for macros/accounts */
194 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
195 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
197 /* Execute a macro */
198 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
200 /* User display helpers */
201 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
203 /* _def() returns error for faulty definitions and already existing * names,
204 * _undef() returns error if a named thing doesn't exist */
205 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
206 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
208 /* */
209 static void a_amv_mac_free(struct a_amv_mac *amp);
211 /* Update replay-log */
212 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
213 struct a_amv_var *oavp);
214 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
216 /* Special cased value string allocation */
217 static char *a_amv_var_copy(char const *str);
218 static void a_amv_var_free(char *cp);
220 /* Check for special housekeeping */
221 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
223 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
224 static bool_t a_amv_var_check_nocntrls(char const *val);
225 static bool_t a_amv_var_check_num(char const *val, bool_t pos);
227 /* If a variable name begins with a lowercase-character and contains at
228 * least one '@', it is converted to all-lowercase. This is necessary
229 * for lookups of names based on email addresses.
230 * Following the standard, only the part following the last '@' should
231 * be lower-cased, but practice has established otherwise here */
232 static char const *a_amv_var_canonify(char const *vn);
234 /* Try to reverse lookup an option name to an enum okeys mapping.
235 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
236 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
237 char const *name);
239 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
240 * Sets .avc_prime; .avc_var is NULL if not found.
241 * Here it is where we care for _I3VAL and _DEFVAL, too.
242 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
243 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
244 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
245 * return FAL0, then! */
246 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
247 bool_t i3val_nonew);
249 /* Set var from .avc_(map|name|hash), return success */
250 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
251 bool_t force_env);
253 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
254 struct a_amv_var *avp);
256 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
257 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
259 static bool_t a_amv_var__clearenv(char const *name, char *value);
261 /* List all variables */
262 static void a_amv_var_show_all(void);
264 static int a_amv_var__show_cmp(void const *s1, void const *s2);
266 /* Actually do print one, return number of lines written */
267 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
269 /* Shared c_set() and c_environ():set impl, return success */
270 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
272 static bool_t
273 a_amv_mac_is_closing_angle(char const *cp){
274 bool_t rv;
275 NYD2_ENTER;
277 while(spacechar(*cp))
278 ++cp;
280 if((rv = (*cp++ == '}'))){
281 while(spacechar(*cp))
282 ++cp;
283 rv = (*cp == '\0');
285 NYD2_LEAVE;
286 return rv;
289 static struct a_amv_mac *
290 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
291 enum a_amv_mac_flags amf){
292 struct a_amv_mac *amp, **ampp;
293 ui32_t h;
294 enum a_amv_mac_flags save_amf;
295 NYD2_ENTER;
297 save_amf = amf;
298 amf &= a_AMV_MF_TYPE_MASK;
299 h = a_AMV_NAME2HASH(name);
300 h = a_AMV_HASH2PRIME(h);
301 ampp = &a_amv_macs[h];
303 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
304 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
305 !strcmp(amp->am_name, name)){
306 if(LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
307 goto jleave;
309 *ampp = amp->am_next;
311 if((amf & a_AMV_MF_ACC) &&
312 account_name != NULL && !strcmp(account_name, name)){
313 amp->am_flags |= a_AMV_MF_DEL;
314 n_err(_("Delayed deletion of active account: %s\n"), name);
315 }else{
316 a_amv_mac_free(amp);
317 amp = (struct a_amv_mac*)-1;
319 goto jleave;
323 if(newamp != NULL){
324 ampp = &a_amv_macs[h];
325 newamp->am_next = *ampp;
326 *ampp = newamp;
327 amp = NULL;
329 jleave:
330 NYD2_LEAVE;
331 return amp;
334 static bool_t
335 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
336 struct a_amv_lostack los, *los_save;
337 struct a_amv_mac_line **amlp;
338 char **args_base, **args;
339 struct a_amv_mac const *amp;
340 bool_t rv;
341 NYD2_ENTER;
343 amp = amcap->amca_amp;
344 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
345 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
346 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
347 *args = NULL;
349 los.as_mac = UNCONST(amp); /* But not used.. */
350 los_save = a_amv_lopts;
351 if(amcap->amca_unroller == NULL){
352 los.as_up = los_save;
353 los.as_lopts = NULL;
354 }else{
355 los.as_up = NULL;
356 los.as_lopts = *amcap->amca_unroller;
358 los.as_unroll = amcap->amca_lopts_on;
360 a_amv_lopts = &los;
361 if(amcap->amca_hook_pre != NULL)
362 (*amcap->amca_hook_pre)(amcap->amca_hook_arg);
363 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base);
364 if(amcap->amca_hook_post != NULL)
365 (*amcap->amca_hook_post)(amcap->amca_hook_arg);
366 a_amv_lopts = los_save;
368 if(amcap->amca_unroller == NULL){
369 if(los.as_lopts != NULL)
370 a_amv_lopts_unroll(&los.as_lopts);
371 }else
372 *amcap->amca_unroller = los.as_lopts;
373 NYD2_LEAVE;
374 return rv;
377 static bool_t
378 a_amv_mac_show(enum a_amv_mac_flags amf){
379 size_t lc, mc, ti, i;
380 char const *typestr;
381 FILE *fp;
382 bool_t rv;
383 NYD2_ENTER;
385 rv = FAL0;
387 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
388 NULL){
389 n_perr(_("Can't create temporary file for `define' or `account' listing"),
391 goto jleave;
394 amf &= a_AMV_MF_TYPE_MASK;
395 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
397 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
398 struct a_amv_mac *amp;
400 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
401 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
402 struct a_amv_mac_line **amlpp;
404 if(++mc > 1){
405 putc('\n', fp);
406 ++lc;
408 ++lc;
409 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
410 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
411 for(i = (*amlpp)->aml_prespc; i > 0; --i)
412 putc(' ', fp);
413 fputs((*amlpp)->aml_dat, fp);
414 putc('\n', fp);
416 fputs("}\n", fp);
417 ++lc;
421 if(mc > 0)
422 page_or_print(fp, lc);
424 rv = (ferror(fp) == 0);
425 Fclose(fp);
426 jleave:
427 NYD2_LEAVE;
428 return rv;
431 static bool_t
432 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
433 struct str line;
434 ui32_t line_cnt, maxlen;
435 struct linelist{
436 struct linelist *ll_next;
437 struct a_amv_mac_line *ll_amlp;
438 } *llp, *ll_head, *ll_tail;
439 union {size_t s; int i; ui32_t ui; size_t l;} n;
440 struct a_amv_mac *amp;
441 bool_t rv;
442 NYD2_ENTER;
444 memset(&line, 0, sizeof line);
445 rv = FAL0;
446 amp = NULL;
448 /* Read in the lines which form the macro content */
449 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
450 ui32_t leaspc;
451 char *cp;
453 n.i = n_lex_input(n_LEXINPUT_CTX_BASE | n_LEXINPUT_NL_ESC, "",
454 &line.s, &line.l, NULL);
455 if(n.ui == 0)
456 continue;
457 if(n.i < 0){
458 n_err(_("Unterminated %s definition: %s\n"),
459 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
460 goto jerr;
462 if(a_amv_mac_is_closing_angle(line.s))
463 break;
465 /* Trim WS, remember amount of leading spaces for display purposes */
466 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
467 if(*cp == '\t')
468 leaspc = (leaspc + 8u) & ~7u;
469 else if(*cp == ' ')
470 ++leaspc;
471 else
472 break;
473 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
475 if(n.ui == 0)
476 continue;
478 maxlen = MAX(maxlen, n.ui);
479 cp[n.ui++] = '\0';
481 if(LIKELY(++line_cnt < UI32_MAX)){
482 struct a_amv_mac_line *amlp;
484 llp = salloc(sizeof *llp);
485 if(ll_head == NULL)
486 ll_head = llp;
487 else
488 ll_tail->ll_next = llp;
489 ll_tail = llp;
490 llp->ll_next = NULL;
491 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
492 VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
493 amlp->aml_len = n.ui -1;
494 amlp->aml_prespc = leaspc;
495 memcpy(amlp->aml_dat, cp, n.ui);
496 }else{
497 n_err(_("Too much content in %s definition: %s\n"),
498 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
499 goto jerr;
503 /* Create the new macro */
504 n.s = strlen(name) +1;
505 amp = smalloc(sizeof(*amp) - VFIELD_SIZEOF(struct a_amv_mac, am_name) + n.s);
506 amp->am_next = NULL;
507 amp->am_maxlen = maxlen;
508 amp->am_line_cnt = line_cnt;
509 amp->am_flags = amf;
510 amp->am_lopts = NULL;
511 memcpy(amp->am_name, name, n.s);
512 /* C99 */{
513 struct a_amv_mac_line **amlpp;
515 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
516 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
517 *amlpp++ = llp->ll_amlp;
518 *amlpp = NULL;
521 /* Finally check whether such a macro already exists, in which case we throw
522 * it all away again. At least we know it would have worked */
523 if(a_amv_mac_lookup(name, amp, amf) != NULL){
524 n_err(_("There is already a %s of name: %s\n"),
525 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
526 goto jerr;
529 rv = TRU1;
530 jleave:
531 if(line.s != NULL)
532 free(line.s);
533 NYD2_LEAVE;
534 return rv;
536 jerr:
537 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
538 free(llp->ll_amlp);
539 if(amp != NULL){
540 free(amp->am_line_dat);
541 free(amp);
543 goto jleave;
546 static bool_t
547 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
548 struct a_amv_mac *amp;
549 bool_t rv;
550 NYD2_ENTER;
552 rv = TRU1;
554 if(LIKELY(name[0] != '*' || name[1] != '\0')){
555 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
556 n_err(_("%s not defined: %s\n"),
557 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
558 rv = FAL0;
560 }else{
561 struct a_amv_mac **ampp, *lamp;
563 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[NELEM(a_amv_macs)]);
564 ++ampp)
565 for(lamp = NULL, amp = *ampp; amp != NULL;){
566 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
567 /* xxx Expensive but rare: be simple */
568 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
569 amp = (lamp == NULL) ? *ampp : lamp->am_next;
570 }else{
571 lamp = amp;
572 amp = amp->am_next;
576 NYD2_LEAVE;
577 return rv;
580 static void
581 a_amv_mac_free(struct a_amv_mac *amp){
582 struct a_amv_mac_line **amlpp;
583 NYD2_ENTER;
585 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
586 free(*amlpp);
587 free(amp->am_line_dat);
588 free(amp);
589 NYD2_LEAVE;
592 static void
593 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
594 struct a_amv_var *oavp){
595 struct a_amv_var *avp;
596 size_t nl, vl;
597 NYD2_ENTER;
599 /* Propagate unrolling up the stack, as necessary */
600 assert(alp != NULL);
601 for(;;){
602 if(alp->as_unroll)
603 break;
604 if((alp = alp->as_up) == NULL)
605 goto jleave;
608 /* Check whether this variable is handled yet */
609 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
610 if(!strcmp(avp->av_name, name))
611 goto jleave;
613 nl = strlen(name) +1;
614 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
615 avp = smalloc(sizeof(*avp) - VFIELD_SIZEOF(struct a_amv_var, av_name) +
616 nl + vl);
617 avp->av_link = alp->as_lopts;
618 alp->as_lopts = avp;
619 memcpy(avp->av_name, name, nl);
620 if(vl == 0){
621 avp->av_value = NULL;
622 avp->av_flags = 0;
623 #ifdef HAVE_PUTENV
624 avp->av_env = NULL;
625 #endif
626 }else{
627 avp->av_value = avp->av_name + nl;
628 avp->av_flags = oavp->av_flags;
629 memcpy(avp->av_value, oavp->av_value, vl);
630 #ifdef HAVE_PUTENV
631 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
632 #endif
634 jleave:
635 NYD2_LEAVE;
638 static void
639 a_amv_lopts_unroll(struct a_amv_var **avpp){
640 struct a_amv_lostack *save_alp;
641 bool_t reset;
642 struct a_amv_var *x, *avp;
643 NYD2_ENTER;
645 avp = *avpp;
646 *avpp = NULL;
647 reset = !(pstate & PS_ROOT);
649 save_alp = a_amv_lopts;
650 a_amv_lopts = NULL;
651 while(avp != NULL){
652 x = avp;
653 avp = avp->av_link;
654 pstate |= PS_ROOT;
655 vok_vset(x->av_name, x->av_value);
656 if(reset)
657 pstate &= ~PS_ROOT;
658 free(x);
660 a_amv_lopts = save_alp;
661 NYD2_LEAVE;
664 static char *
665 a_amv_var_copy(char const *str){
666 char *news;
667 size_t len;
668 NYD2_ENTER;
670 if(*str == '\0')
671 news = UNCONST("");
672 else{
673 len = strlen(str) +1;
674 news = smalloc(len);
675 memcpy(news, str, len);
677 NYD2_LEAVE;
678 return news;
681 static void
682 a_amv_var_free(char *cp){
683 NYD2_ENTER;
684 if(*cp != '\0')
685 free(cp);
686 NYD2_LEAVE;
689 static bool_t
690 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
691 int flag;
692 bool_t ok;
693 NYD2_ENTER;
695 ok = TRU1;
696 flag = 0;
698 switch(okey){
699 case ok_b_debug:
700 flag = OPT_DEBUG;
701 break;
702 case ok_v_HOME:
703 /* Invalidate any resolved folder then, too
704 * FALLTHRU */
705 case ok_v_folder:
706 ok = !(pstate & PS_ROOT);
707 pstate |= PS_ROOT;
708 ok_vclear(_folder_resolved);
709 if(ok)
710 pstate &= ~PS_ROOT;
711 ok = TRU1;
712 break;
713 case ok_b_header:
714 flag = OPT_N_FLAG;
715 enable = !enable;
716 break;
717 case ok_b_memdebug:
718 flag = OPT_MEMDEBUG;
719 break;
720 case ok_b_skipemptybody:
721 flag = OPT_E_FLAG;
722 break;
723 case ok_v_TMPDIR:
724 if(enable) /* DEFVAL will soon ensure a value otherwise! */
725 tempdir = *val; /* XXX replace users with ok_vlook(TMPDIR) */
726 break;
727 case ok_v_umask:
728 assert(enable);
729 if(**val != '\0'){
730 ul_i ul;
732 if((ul = strtoul(*val, NULL, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
733 n_err(_("Invalid *umask* setting: %s\n"), *val);
734 ok = FAL0;
735 }else
736 umask((mode_t)ul);
738 break;
739 case ok_b_verbose:
740 flag = (enable && !(options & OPT_VERB))
741 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
742 break;
743 default:
744 DBG( n_err("Implementation error: never heard of %u\n", ok); )
745 break;
748 if(flag){
749 if(enable)
750 options |= flag;
751 else
752 options &= ~flag;
754 NYD2_LEAVE;
755 return ok;
758 static bool_t
759 a_amv_var_check_nocntrls(char const *val){
760 char c;
761 NYD2_ENTER;
763 while((c = *val++) != '\0')
764 if(cntrlchar(c))
765 break;
766 NYD2_LEAVE;
767 return (c == '\0');
770 static bool_t
771 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
772 /* TODO The internal/environment variables which are num= or posnum= should
773 * TODO gain special lookup functions, or the return should be void* and
774 * TODO castable to integer; i.e. no more strtoX() should be needed.
775 * TODO I.e., the result of this function should instead be stored.
776 * TODO Use intmax_t IF that is sizeof(void*) only? */
777 bool_t rv;
778 NYD2_ENTER;
780 rv = TRU1;
782 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
783 char *eptr;
784 union {long s; unsigned long u;} i;
786 if(!pos){
787 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
789 if(*eptr != '\0' ||
790 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
791 rv = FAL0;
792 #if INT_MIN != LONG_MIN
793 else if(i.s < INT_MIN)
794 rv = FAL0;
795 #endif
796 #if INT_MAX != LONG_MAX
797 else if(i.s > INT_MAX)
798 rv = FAL0;
799 #endif
800 }else{
801 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
803 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
804 rv = FAL0;
805 #if UINT_MAX != ULONG_MAX
806 else if(i.u > UINT_MAX)
807 rv = FAL0;
808 #endif
811 NYD2_LEAVE;
812 return rv;
815 static char const *
816 a_amv_var_canonify(char const *vn){
817 NYD2_ENTER;
818 if(!upperchar(*vn)){
819 char const *vp;
821 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
823 vn = (*vp == '@') ? i_strdup(vn) : vn;
825 NYD2_LEAVE;
826 return vn;
829 static bool_t
830 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
831 ui32_t hash, i, j;
832 struct a_amv_var_map const *avmp;
833 NYD2_ENTER;
835 avcp->avc_name = name = a_amv_var_canonify(name);
836 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
838 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
839 ui32_t x = a_amv_var_revmap[i];
841 if(x == a_AMV_VAR_REV_ILL)
842 break;
844 avmp = &a_amv_var_map[x];
845 if(avmp->avm_hash == hash &&
846 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
847 avcp->avc_map = avmp;
848 avcp->avc_okey = (enum okeys)x;
849 goto jleave;
852 if(++i == a_AMV_VAR_REV_PRIME){
853 #ifdef a_AMV_VAR_REV_WRAPAROUND
854 i = 0;
855 #else
856 break;
857 #endif
860 avcp->avc_map = NULL;
861 avcp = NULL;
862 jleave:
863 NYD2_LEAVE;
864 return (avcp != NULL);
867 static bool_t
868 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
869 size_t i;
870 char const *cp;
871 struct a_amv_var_map const *avmp;
872 struct a_amv_var *avp;
873 NYD2_ENTER;
875 /* C99 */{
876 struct a_amv_var **avpp, *lavp;
878 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
880 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
881 if(!strcmp(avp->av_name, avcp->avc_name)){
882 /* Relink as head, hope it "sorts on usage" over time.
883 * _clear() relies on this behaviour */
884 if(lavp != NULL){
885 lavp->av_link = avp->av_link;
886 avp->av_link = *avpp;
887 *avpp = avp;
889 goto jleave;
893 /* If this is not an assembled variable we need to consider some special
894 * initialization cases and eventually create the variable anew */
895 if(LIKELY((avmp = avcp->avc_map) != NULL)){
896 /* Does it have an import-from-environment flag? */
897 if(UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
898 if(LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
899 /* May be better not to use that one, though? */
900 bool_t isempty, isbltin;
902 isempty = (*cp == '\0' &&
903 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
904 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
905 ) != 0);
907 if(UNLIKELY(isempty)){
908 if(!isbltin)
909 goto jerr;
910 }else if(LIKELY(*cp != '\0')){
911 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
912 !a_amv_var_check_nocntrls(cp))){
913 n_err(_("Ignoring environment, control characters "
914 "invalid in variable: %s\n"), avcp->avc_name);
915 goto jerr;
917 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
918 !a_amv_var_check_num(cp, FAL0))){
919 n_err(_("Environment variable value not a number "
920 "or out of range: %s\n"), avcp->avc_name);
921 goto jerr;
923 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
924 !a_amv_var_check_num(cp, TRU1))){
925 n_err(_("Environment variable value not a number, "
926 "negative or out of range: %s\n"), avcp->avc_name);
927 goto jerr;
930 goto jnewval;
934 /* A first-time init switch is to be handled now and here */
935 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
936 static struct a_amv_var_defval const **arr,
937 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
939 if(arr == NULL){
940 arr = &arr_base[0];
941 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
942 while(i-- > 0)
943 arr[i] = &a_amv_var_i3vals[i];
946 for(i = 0; arr[i] != NULL; ++i)
947 if(arr[i]->avdv_okey == avcp->avc_okey){
948 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
949 : arr[i]->avdv_value;
950 /* Remove this entry, hope entire block becomes no-op asap */
952 arr[i] = arr[i + 1];
953 while(arr[i++] != NULL);
955 if(!i3val_nonew)
956 goto jnewval;
957 if(i3val_nonew == TRUM1)
958 avp = (struct a_amv_var*)-1;
959 goto jleave;
963 /* The virtual variables */
964 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
965 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
966 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
967 avp = UNCONST(a_amv_var_virts[i].avv_var);
968 goto jleave;
970 /* Not reached */
973 /* Place this last because once it is set first the variable will never
974 * be removed again and thus match in the first block above */
975 if(UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
976 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
977 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
978 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
979 : a_amv_var_defvals[i].avdv_value;
980 goto jnewval;
985 jerr:
986 avp = NULL;
987 jleave:
988 avcp->avc_var = avp;
989 NYD2_LEAVE;
990 return (avp != NULL);
992 jnewval: /* C99 */{
993 struct a_amv_var **avpp;
994 size_t l;
996 l = strlen(avcp->avc_name) +1;
997 avcp->avc_var = avp = smalloc(sizeof(*avp) -
998 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
999 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1000 *avpp = avp;
1001 memcpy(avp->av_name, avcp->avc_name, l);
1002 avp->av_value = a_amv_var_copy(cp);
1003 #ifdef HAVE_PUTENV
1004 avp->av_env = NULL;
1005 #endif
1006 avp->av_flags = avmp->avm_flags;
1008 if(avp->av_flags & a_AMV_VF_VIP)
1009 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1010 if(avp->av_flags & a_AMV_VF_ENV)
1011 a_amv_var__putenv(avcp, avp);
1012 goto jleave;
1016 static bool_t
1017 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1018 bool_t force_env){
1019 struct a_amv_var *avp;
1020 char *oval;
1021 struct a_amv_var_map const *avmp;
1022 bool_t rv;
1023 NYD2_ENTER;
1025 if(value == NULL){
1026 rv = a_amv_var_clear(avcp, force_env);
1027 goto jleave;
1030 if((avmp = avcp->avc_map) != NULL){
1031 rv = FAL0;
1033 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1034 !(pstate & PS_ROOT))){
1035 n_err(_("Variable is readonly: %s\n"), avcp->avc_name);
1036 goto jleave;
1038 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1039 n_err(_("Variable must not be empty: %s\n"), avcp->avc_name);
1040 goto jleave;
1042 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1043 !a_amv_var_check_nocntrls(value))){
1044 n_err(_("Variable forbids control characters: %s\n"),
1045 avcp->avc_name);
1046 goto jleave;
1048 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1049 !a_amv_var_check_num(value, FAL0))){
1050 n_err(_("Variable value not a number or out of range: %s\n"),
1051 avcp->avc_name);
1052 goto jleave;
1054 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1055 !a_amv_var_check_num(value, TRU1))){
1056 n_err(_("Variable value not a number, negative or out of range: %s\n"),
1057 avcp->avc_name);
1058 goto jleave;
1060 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1061 !(pstate & (PS_ROOT | PS_STARTED)))){
1062 n_err(_("Variable cannot be set in a resource file: %s\n"),
1063 avcp->avc_name);
1064 goto jleave;
1068 rv = TRU1;
1069 a_amv_var_lookup(avcp, TRU1);
1071 /* Don't care what happens later on, store this in the unroll list */
1072 if(a_amv_lopts != NULL)
1073 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1075 if((avp = avcp->avc_var) == NULL){
1076 struct a_amv_var **avpp;
1077 size_t l = strlen(avcp->avc_name) +1;
1079 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1080 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1081 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1082 *avpp = avp;
1083 #ifdef HAVE_PUTENV
1084 avp->av_env = NULL;
1085 #endif
1086 memcpy(avp->av_name, avcp->avc_name, l);
1087 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1088 oval = UNCONST("");
1089 }else
1090 oval = avp->av_value;
1092 if(avmp == NULL)
1093 avp->av_value = a_amv_var_copy(value);
1094 else{
1095 /* Via `set' etc. the user may give even boolean options non-boolean
1096 * values, ignore that and force boolean */
1097 if(avp->av_flags & a_AMV_VF_BOOL){
1098 if((options & OPT_D_VV) && *value != '\0')
1099 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1100 avcp->avc_name, value);
1101 avp->av_value = UNCONST(value = "");
1102 }else
1103 avp->av_value = a_amv_var_copy(value);
1105 /* Check if update allowed XXX wasteful on error! */
1106 if((avp->av_flags & a_AMV_VF_VIP) &&
1107 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1108 char *cp = avp->av_value;
1110 avp->av_value = oval;
1111 oval = cp;
1115 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1116 avp->av_flags |= a_AMV_VF_LINKED;
1117 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1118 rv = a_amv_var__putenv(avcp, avp);
1120 a_amv_var_free(oval);
1121 jleave:
1122 NYD2_LEAVE;
1123 return rv;
1126 static bool_t
1127 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1128 #ifndef HAVE_SETENV
1129 char *cp;
1130 #endif
1131 bool_t rv;
1132 NYD2_ENTER;
1134 #ifdef HAVE_SETENV
1135 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1136 #else
1137 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1139 if((rv = (putenv(cp) == 0))){
1140 char *ocp;
1142 if((ocp = avp->av_env) != NULL)
1143 free(ocp);
1144 avp->av_env = cp;
1145 }else
1146 free(cp);
1147 #endif
1148 NYD2_LEAVE;
1149 return rv;
1152 static bool_t
1153 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1154 struct a_amv_var **avpp, *avp;
1155 struct a_amv_var_map const *avmp;
1156 bool_t rv;
1157 NYD2_ENTER;
1159 rv = FAL0;
1161 if(LIKELY((avmp = avcp->avc_map) != NULL)){
1162 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1163 !(pstate & PS_ROOT))){
1164 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1165 goto jleave;
1167 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1168 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1169 goto jleave;
1172 rv = TRU1;
1174 if(UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1175 if(force_env){
1176 jforce_env:
1177 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1178 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1179 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1180 goto jleave;
1181 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1182 avcp->avc_var = NULL;
1183 if(force_env)
1184 goto jforce_env;
1185 goto jleave;
1188 if(a_amv_lopts != NULL)
1189 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1191 avp = avcp->avc_var;
1192 avcp->avc_var = NULL;
1193 avpp = &a_amv_vars[avcp->avc_prime];
1194 assert(*avpp == avp); /* (always listhead after lookup()) */
1195 *avpp = (*avpp)->av_link;
1197 /* C99 */{
1198 #ifdef HAVE_SETENV
1199 char *envval = NULL;
1200 #else
1201 char *envval = avp->av_env;
1202 #endif
1203 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1204 rv = a_amv_var__clearenv(avp->av_name, envval);
1206 a_amv_var_free(avp->av_value);
1207 free(avp);
1209 /* XXX Fun part, extremely simple-minded for now: if this variable has
1210 * XXX a default value, immediately reinstantiate it! */
1211 if(UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1212 a_amv_var_lookup(avcp, TRU1);
1213 jleave:
1214 NYD2_LEAVE;
1215 return rv;
1218 static bool_t
1219 a_amv_var__clearenv(char const *name, char *value){
1220 #ifndef HAVE_SETENV
1221 extern char **environ;
1222 char **ecpp;
1223 #endif
1224 bool_t rv;
1225 NYD2_ENTER;
1226 UNUSED(value);
1228 #ifdef HAVE_SETENV
1229 unsetenv(name);
1230 rv = TRU1;
1231 #else
1232 if(value != NULL)
1233 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1234 if(*ecpp == value){
1235 free(value);
1237 ecpp[0] = ecpp[1];
1238 while(*ecpp++ != NULL);
1239 break;
1241 rv = TRU1;
1242 #endif
1243 NYD2_LEAVE;
1244 return rv;
1247 static void
1248 a_amv_var_show_all(void){
1249 struct n_string msg, *msgp;
1250 FILE *fp;
1251 size_t no, i;
1252 struct a_amv_var *avp;
1253 char const **vacp, **cap;
1254 NYD2_ENTER;
1256 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1257 n_perr(_("Can't create temporary file for `set' listing"), 0);
1258 goto jleave;
1261 /* We need to instantiate first-time-inits and default values here, so that
1262 * they will be regular members of our _vars[] table */
1263 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1264 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1265 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1266 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1268 for(no = i = 0; i < a_AMV_PRIME; ++i)
1269 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1270 ++no;
1271 no += a_AMV_VAR_VIRTS_CNT;
1273 vacp = salloc(no * sizeof(*vacp));
1275 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1276 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1277 *cap++ = avp->av_name;
1278 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1279 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1281 if(no > 1)
1282 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1284 msgp = &msg;
1285 msgp = n_string_reserve(n_string_creat(msgp), 80);
1286 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1287 i += a_amv_var_show(*cap, fp, msgp);
1288 n_string_gut(&msg);
1290 page_or_print(fp, i);
1291 Fclose(fp);
1292 jleave:
1293 NYD2_LEAVE;
1296 static int
1297 a_amv_var__show_cmp(void const *s1, void const *s2){
1298 int rv;
1299 NYD2_ENTER;
1301 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
1302 NYD2_LEAVE;
1303 return rv;
1306 static size_t
1307 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1308 struct a_amv_var_carrier avc;
1309 char const *quote;
1310 size_t i;
1311 NYD2_ENTER;
1313 msgp = n_string_trunc(msgp, 0);
1314 i = 0;
1316 a_amv_var_revlookup(&avc, name);
1317 if(!a_amv_var_lookup(&avc, FAL0)){
1318 struct str s;
1320 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1321 s.s = UNCONST(name);
1322 s.l = UIZ_MAX;
1323 msgp = n_shell_quote(msgp, &s, FAL0);
1324 goto jleave;
1327 if(options & OPT_D_V){
1328 if(avc.avc_map == NULL){
1329 msgp = n_string_push_c(msgp, '#');
1330 msgp = n_string_push_cp(msgp, "assembled");
1331 i = 1;
1333 /* C99 */{
1334 struct{
1335 ui16_t flags;
1336 char msg[22];
1337 } const tbase[] = {
1338 {a_AMV_VF_VIRT, "virtual"},
1339 {a_AMV_VF_RDONLY, "readonly"},
1340 {a_AMV_VF_NODEL, "nodelete"},
1341 {a_AMV_VF_NOTEMPTY, "notempty"},
1342 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1343 {a_AMV_VF_NUM, "number"},
1344 {a_AMV_VF_POSNUM, "positive-number"},
1345 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1346 {a_AMV_VF_ENV, "sync-environ"},
1347 {a_AMV_VF_I3VAL, "initial-value"},
1348 {a_AMV_VF_DEFVAL, "default-value"},
1349 {a_AMV_VF_LINKED, "`environ' link"}
1350 }, *tp;
1352 for(tp = tbase; PTRCMP(tp, <, &tbase[NELEM(tbase)]); ++tp)
1353 if(avc.avc_var->av_flags & tp->flags){
1354 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1355 msgp = n_string_push_cp(msgp, tp->msg);
1359 if(i > 0)
1360 msgp = n_string_push_cp(msgp, "\n ");
1363 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1364 msgp = n_string_push_cp(msgp, "# ");
1365 UNINIT(quote, NULL);
1366 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1367 quote = n_shell_quote_cp(avc.avc_var->av_value, TRU1);
1368 if(strcmp(quote, avc.avc_var->av_value))
1369 msgp = n_string_push_cp(msgp, "wysh ");
1371 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1372 msgp = n_string_push_cp(msgp, "environ ");
1373 msgp = n_string_push_cp(msgp, "set ");
1374 msgp = n_string_push_cp(msgp, name);
1375 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1376 msgp = n_string_push_c(msgp, '=');
1377 msgp = n_string_push_cp(msgp, quote);
1380 jleave:
1381 msgp = n_string_push_c(msgp, '\n');
1382 fputs(n_string_cp(msgp), fp);
1383 NYD2_ENTER;
1384 return (i > 0 ? 2 : 1);
1387 static bool_t
1388 a_amv_var_c_set(char **ap, bool_t issetenv){
1389 char *cp, *cp2, *varbuf, c;
1390 size_t errs;
1391 NYD2_ENTER;
1393 errs = 0;
1394 jouter:
1395 while((cp = *ap++) != NULL){
1396 /* Isolate key */
1397 cp2 = varbuf = salloc(strlen(cp) +1);
1399 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1400 if(cntrlchar(c) || whitechar(c)){
1401 n_err(_("Variable name with control character ignored: %s\n"),
1402 ap[-1]);
1403 ++errs;
1404 goto jouter;
1406 *cp2++ = c;
1408 *cp2 = '\0';
1409 if(c == '\0')
1410 cp = UNCONST("");
1411 else
1412 ++cp;
1414 if(varbuf == cp2){
1415 n_err(_("Empty variable name ignored\n"));
1416 ++errs;
1417 }else{
1418 struct a_amv_var_carrier avc;
1419 bool_t isunset;
1421 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1422 varbuf = &varbuf[2];
1424 a_amv_var_revlookup(&avc, varbuf);
1426 if(isunset)
1427 errs += !a_amv_var_clear(&avc, issetenv);
1428 else
1429 errs += !a_amv_var_set(&avc, cp, issetenv);
1432 NYD2_LEAVE;
1433 return (errs == 0);
1436 FL int
1437 c_define(void *v){
1438 int rv;
1439 char **args;
1440 NYD_ENTER;
1442 rv = 1;
1444 if((args = v)[0] == NULL){
1445 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1446 goto jleave;
1449 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1450 args[2] != NULL){
1451 n_err(_("Synopsis: define: <name> {\n"));
1452 goto jleave;
1455 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1456 jleave:
1457 NYD_LEAVE;
1458 return rv;
1461 FL int
1462 c_undefine(void *v){
1463 int rv;
1464 char **args;
1465 NYD_ENTER;
1467 rv = 0;
1468 args = v;
1470 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1471 while(*++args != NULL);
1472 NYD_LEAVE;
1473 return rv;
1476 FL int
1477 c_call(void *v){
1478 struct a_amv_mac_call_args amca;
1479 char const *errs, *name;
1480 struct a_amv_mac *amp;
1481 char **args;
1482 int rv;
1483 NYD_ENTER;
1485 rv = 1;
1487 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1488 errs = _("Synopsis: call: <%s>\n");
1489 name = "name";
1490 goto jerr;
1493 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1494 errs = _("Undefined macro `call'ed: %s\n");
1495 goto jerr;
1498 memset(&amca, 0, sizeof amca);
1499 amca.amca_name = name;
1500 amca.amca_amp = amp;
1501 rv = (a_amv_mac_exec(&amca) == FAL0);
1502 jleave:
1503 NYD_LEAVE;
1504 return rv;
1505 jerr:
1506 n_err(errs, name);
1507 goto jleave;
1510 FL bool_t
1511 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1512 struct a_amv_mac_call_args amca;
1513 struct a_amv_mac *amp;
1514 size_t len;
1515 char *var, *cp;
1516 bool_t rv;
1517 NYD_ENTER;
1519 rv = TRU1;
1520 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1522 /* First try the fully resolved path */
1523 snprintf(var, len, "folder-hook-%s", mailname);
1524 if((cp = vok_vlook(var)) != NULL)
1525 goto jmac;
1527 /* If we are under *folder*, try the usual +NAME syntax, too */
1528 if(displayname[0] == '+'){
1529 char *x = mailname + len;
1531 for(; x > mailname; --x)
1532 if(x[-1] == '/'){
1533 snprintf(var, len, "folder-hook-+%s", x);
1534 if((cp = vok_vlook(var)) != NULL)
1535 goto jmac;
1536 break;
1540 /* Plain *folder-hook* is our last try */
1541 if((cp = ok_vlook(folder_hook)) == NULL)
1542 goto jleave;
1544 jmac:
1545 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1546 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1547 n_shell_quote_cp(displayname, FAL0), cp);
1548 rv = FAL0;
1549 goto jleave;
1552 memset(&amca, 0, sizeof amca);
1553 amca.amca_name = cp;
1554 amca.amca_amp = amp;
1555 pstate &= ~PS_HOOK_MASK;
1556 if(nmail){
1557 amca.amca_unroller = NULL;
1558 pstate |= PS_HOOK_NEWMAIL;
1559 }else{
1560 amca.amca_unroller = &a_amv_folder_hook_lopts;
1561 pstate |= PS_HOOK;
1563 amca.amca_lopts_on = TRU1;
1564 rv = a_amv_mac_exec(&amca);
1565 pstate &= ~PS_HOOK_MASK;
1567 jleave:
1568 NYD_LEAVE;
1569 return rv;
1572 FL void
1573 call_compose_mode_hook(char const *macname, /* TODO temporary, v15: drop */
1574 void (*hook_pre)(void *), void *hook_arg){
1575 struct a_amv_mac_call_args amca;
1576 struct a_amv_mac *amp;
1577 NYD_ENTER;
1579 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1580 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1581 macname);
1582 else{
1583 memset(&amca, 0, sizeof amca);
1584 amca.amca_name = macname;
1585 amca.amca_amp = amp;
1586 amca.amca_unroller = &a_amv_compose_lopts;
1587 amca.amca_hook_pre = hook_pre;
1588 amca.amca_hook_arg = hook_arg;
1589 amca.amca_lopts_on = TRU1;
1590 pstate &= ~PS_HOOK_MASK;
1591 pstate |= PS_HOOK;
1592 a_amv_mac_exec(&amca);
1593 pstate &= ~PS_HOOK_MASK;
1595 NYD_LEAVE;
1598 FL int
1599 c_account(void *v){
1600 struct a_amv_mac_call_args amca;
1601 struct a_amv_mac *amp;
1602 char **args;
1603 int rv, i, oqf, nqf;
1604 NYD_ENTER;
1606 rv = 1;
1608 if((args = v)[0] == NULL){
1609 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1610 goto jleave;
1613 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1614 if(args[2] != NULL){
1615 n_err(_("Synopsis: account: <name> {\n"));
1616 goto jleave;
1618 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1619 n_err(_("`account': cannot use reserved name: %s\n"),
1620 ACCOUNT_NULL);
1621 goto jleave;
1623 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1624 goto jleave;
1627 if(pstate & PS_HOOK_MASK){
1628 n_err(_("`account': can't change account from within a hook\n"));
1629 goto jleave;
1632 save_mbox_for_possible_quitstuff();
1634 amp = NULL;
1635 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1636 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1637 n_err(_("`account': account does not exist: %s\n"), args[0]);
1638 goto jleave;
1641 oqf = savequitflags();
1643 if(a_amv_acc_curr != NULL){
1644 if(a_amv_acc_curr->am_lopts != NULL)
1645 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1646 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1647 a_amv_mac_free(a_amv_acc_curr);
1650 account_name = (amp != NULL) ? amp->am_name : NULL;
1651 a_amv_acc_curr = amp;
1653 if(amp != NULL){
1654 assert(amp->am_lopts == NULL);
1655 memset(&amca, 0, sizeof amca);
1656 amca.amca_name = amp->am_name;
1657 amca.amca_amp = amp;
1658 amca.amca_unroller = &amp->am_lopts;
1659 amca.amca_lopts_on = TRU1;
1660 if(!a_amv_mac_exec(&amca)){
1661 /* XXX account switch incomplete, unroll? */
1662 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1663 goto jleave;
1667 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1668 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1669 restorequitflags(oqf);
1670 if((i = setfile("%", 0)) < 0)
1671 goto jleave;
1672 check_folder_hook(FAL0);
1673 if(i > 0 && !ok_blook(emptystart))
1674 goto jleave;
1675 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1676 restorequitflags(nqf);
1678 rv = 0;
1679 jleave:
1680 NYD_LEAVE;
1681 return rv;
1684 FL int
1685 c_unaccount(void *v){
1686 int rv;
1687 char **args;
1688 NYD_ENTER;
1690 rv = 0;
1691 args = v;
1693 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1694 while(*++args != NULL);
1695 NYD_LEAVE;
1696 return rv;
1699 FL int
1700 c_localopts(void *v){
1701 char **argv;
1702 int rv;
1703 NYD_ENTER;
1705 rv = 1;
1707 if(a_amv_lopts == NULL){
1708 n_err(_("Cannot use `localopts' but from within a "
1709 "`define' or `account'\n"));
1710 goto jleave;
1713 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1714 rv = 0;
1715 jleave:
1716 NYD_LEAVE;
1717 return rv;
1720 FL void
1721 temporary_localopts_free(void){ /* XXX intermediate hack */
1722 NYD_ENTER;
1723 if(a_amv_compose_lopts != NULL){
1724 void *save = a_amv_lopts;
1726 a_amv_lopts = NULL;
1727 a_amv_lopts_unroll(&a_amv_compose_lopts);
1728 a_amv_compose_lopts = NULL;
1729 a_amv_lopts = save;
1731 NYD_LEAVE;
1734 FL void
1735 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1736 NYD_ENTER;
1737 if(a_amv_folder_hook_lopts != NULL){
1738 void *save = a_amv_lopts;
1740 a_amv_lopts = NULL;
1741 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1742 a_amv_folder_hook_lopts = NULL;
1743 a_amv_lopts = save;
1745 NYD_LEAVE;
1748 FL char *
1749 n_var_oklook(enum okeys okey){
1750 struct a_amv_var_carrier avc;
1751 char *rv;
1752 struct a_amv_var_map const *avmp;
1753 NYD_ENTER;
1755 avc.avc_map = avmp = &a_amv_var_map[okey];
1756 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1757 avc.avc_hash = avmp->avm_hash;
1758 avc.avc_okey = okey;
1760 if(a_amv_var_lookup(&avc, FAL0))
1761 rv = avc.avc_var->av_value;
1762 else
1763 rv = NULL;
1764 NYD_LEAVE;
1765 return rv;
1768 FL bool_t
1769 n_var_okset(enum okeys okey, uintptr_t val){
1770 struct a_amv_var_carrier avc;
1771 bool_t ok;
1772 struct a_amv_var_map const *avmp;
1773 NYD_ENTER;
1775 avc.avc_map = avmp = &a_amv_var_map[okey];
1776 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1777 avc.avc_hash = avmp->avm_hash;
1778 avc.avc_okey = okey;
1780 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1781 NYD_LEAVE;
1782 return ok;
1785 FL bool_t
1786 n_var_okclear(enum okeys okey){
1787 struct a_amv_var_carrier avc;
1788 bool_t rv;
1789 struct a_amv_var_map const *avmp;
1790 NYD_ENTER;
1792 avc.avc_map = avmp = &a_amv_var_map[okey];
1793 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1794 avc.avc_hash = avmp->avm_hash;
1795 avc.avc_okey = okey;
1797 rv = a_amv_var_clear(&avc, FAL0);
1798 NYD_LEAVE;
1799 return rv;
1802 FL char *
1803 n_var_voklook(char const *vokey){
1804 struct a_amv_var_carrier avc;
1805 char *rv;
1806 NYD_ENTER;
1808 a_amv_var_revlookup(&avc, vokey);
1810 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1811 NYD_LEAVE;
1812 return rv;
1815 FL bool_t
1816 n_var_vokset(char const *vokey, uintptr_t val){
1817 struct a_amv_var_carrier avc;
1818 bool_t ok;
1819 NYD_ENTER;
1821 a_amv_var_revlookup(&avc, vokey);
1823 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1824 NYD_LEAVE;
1825 return ok;
1828 FL bool_t
1829 n_var_vokclear(char const *vokey){
1830 struct a_amv_var_carrier avc;
1831 bool_t ok;
1832 NYD_ENTER;
1834 a_amv_var_revlookup(&avc, vokey);
1836 ok = a_amv_var_clear(&avc, FAL0);
1837 NYD_LEAVE;
1838 return ok;
1841 #ifdef HAVE_SOCKETS
1842 FL char *
1843 n_var_xoklook(enum okeys okey, struct url const *urlp,
1844 enum okey_xlook_mode oxm){
1845 struct a_amv_var_carrier avc;
1846 struct str const *us;
1847 size_t nlen;
1848 char *nbuf, *rv;
1849 struct a_amv_var_map const *avmp;
1850 NYD_ENTER;
1852 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1854 /* For simplicity: allow this case too */
1855 if(!(oxm & (OXM_H_P | OXM_U_H_P)))
1856 goto jplain;
1858 avc.avc_map = avmp = &a_amv_var_map[okey];
1859 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1860 avc.avc_okey = okey;
1862 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1863 nlen = strlen(avc.avc_name);
1864 nbuf = salloc(nlen + 1 + us->l +1);
1865 memcpy(nbuf, avc.avc_name, nlen);
1866 nbuf[nlen++] = '-';
1868 /* One of .url_u_h_p and .url_h_p we test in here */
1869 memcpy(nbuf + nlen, us->s, us->l +1);
1870 avc.avc_name = a_amv_var_canonify(nbuf);
1871 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1872 if(a_amv_var_lookup(&avc, FAL0))
1873 goto jvar;
1875 /* The second */
1876 if(oxm & OXM_H_P){
1877 us = &urlp->url_h_p;
1878 memcpy(nbuf + nlen, us->s, us->l +1);
1879 avc.avc_name = a_amv_var_canonify(nbuf);
1880 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1881 if(a_amv_var_lookup(&avc, FAL0)){
1882 jvar:
1883 rv = avc.avc_var->av_value;
1884 goto jleave;
1888 jplain:
1889 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1890 jleave:
1891 NYD_LEAVE;
1892 return rv;
1894 #endif /* HAVE_SOCKETS */
1896 FL int
1897 c_set(void *v){
1898 char **ap;
1899 int err;
1900 NYD_ENTER;
1902 if(*(ap = v) == NULL){
1903 a_amv_var_show_all();
1904 err = 0;
1905 }else
1906 err = !a_amv_var_c_set(ap, FAL0);
1907 NYD_LEAVE;
1908 return err;
1911 FL int
1912 c_unset(void *v){
1913 char **ap;
1914 int err;
1915 NYD_ENTER;
1917 for(err = 0, ap = v; *ap != NULL; ++ap)
1918 err |= !n_var_vokclear(*ap);
1919 NYD_LEAVE;
1920 return err;
1923 FL int
1924 c_varshow(void *v){
1925 char **ap;
1926 NYD_ENTER;
1928 if(*(ap = v) == NULL)
1929 v = NULL;
1930 else{
1931 struct n_string msg, *msgp = &msg;
1933 msgp = n_string_creat(msgp);
1934 for(; *ap != NULL; ++ap)
1935 a_amv_var_show(*ap, stdout, msgp);
1936 n_string_gut(msgp);
1938 NYD_LEAVE;
1939 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1942 FL int
1943 c_varedit(void *v){
1944 struct a_amv_var_carrier avc;
1945 FILE *of, *nf;
1946 char *val, **argv;
1947 int err;
1948 sighandler_type sigint;
1949 NYD_ENTER;
1951 sigint = safe_signal(SIGINT, SIG_IGN);
1953 for(err = 0, argv = v; *argv != NULL; ++argv){
1954 memset(&avc, 0, sizeof avc);
1956 a_amv_var_revlookup(&avc, *argv);
1958 if(avc.avc_map != NULL){
1959 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1960 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
1961 avc.avc_name);
1962 continue;
1964 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
1965 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
1966 avc.avc_name);
1967 continue;
1971 a_amv_var_lookup(&avc, FAL0);
1973 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1974 NULL){
1975 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1976 err = 1;
1977 break;
1978 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
1979 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
1980 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1981 Fclose(of);
1982 err = 1;
1983 continue;
1986 fflush_rewind(of);
1987 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1988 Fclose(of);
1990 if(nf != NULL){
1991 int c;
1992 char *base;
1993 off_t l = fsize(nf);
1995 assert(l >= 0);
1996 base = salloc((size_t)l +1);
1998 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
1999 if(c == '\n' || c == '\r'){
2000 *val = ' ';
2001 ++l;
2002 }else{
2003 *val = (char)(uc_i)c;
2004 l = 0;
2006 val -= l;
2007 *val = '\0';
2009 if(!a_amv_var_set(&avc, base, FAL0))
2010 err = 1;
2012 Fclose(nf);
2013 }else{
2014 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2015 err = 1;
2016 break;
2020 safe_signal(SIGINT, sigint);
2021 NYD_LEAVE;
2022 return err;
2025 FL int
2026 c_environ(void *v){
2027 struct a_amv_var_carrier avc;
2028 int err;
2029 char **ap;
2030 bool_t islnk;
2031 NYD_ENTER;
2033 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2034 for(err = 0; *++ap != NULL;){
2035 a_amv_var_revlookup(&avc, *ap);
2037 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2038 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2039 if(!islnk){
2040 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2041 continue;
2042 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2043 if(options & OPT_D_V)
2044 n_err(_("`environ': link: already established: %s\n"), *ap);
2045 continue;
2047 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2048 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2049 a_amv_var__putenv(&avc, avc.avc_var);
2050 }else if(!islnk){
2051 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2052 err = 1;
2053 }else{
2054 char const *evp = getenv(*ap);
2056 if(evp != NULL)
2057 err |= !a_amv_var_set(&avc, evp, TRU1);
2058 else{
2059 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2060 *ap);
2061 err = 1;
2065 }else if(!strcmp(*ap, "set"))
2066 err = !a_amv_var_c_set(++ap, TRU1);
2067 else if(!strcmp(*ap, "unset")){
2068 for(err = 0; *++ap != NULL;){
2069 a_amv_var_revlookup(&avc, *ap);
2071 if(!a_amv_var_clear(&avc, TRU1))
2072 err = 1;
2074 }else{
2075 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2076 err = 1;
2078 NYD_LEAVE;
2079 return err;
2082 /* s-it-mode */