Tweak [36578d1] (Add PS_SETFILE_OPENED..) by saving errno
[s-mailx.git] / accmacvar.c
blob5c9bca55d842df9d7b49295c3e73c71367a3ccc1
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_b_typescript_mode:
724 if(enable){
725 ok_bset(colour_disable);
726 ok_bset(line_editor_disable);
727 if(!(pstate & PS_STARTED))
728 ok_bset(termcap_disable);
730 case ok_v_umask:
731 assert(enable);
732 if(**val != '\0'){
733 ul_i ul;
735 if((ul = strtoul(*val, NULL, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
736 n_err(_("Invalid *umask* setting: %s\n"), *val);
737 ok = FAL0;
738 }else
739 umask((mode_t)ul);
741 break;
742 case ok_b_verbose:
743 flag = (enable && !(options & OPT_VERB))
744 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
745 break;
746 default:
747 DBG( n_err("Implementation error: never heard of %u\n", ok); )
748 break;
751 if(flag){
752 if(enable)
753 options |= flag;
754 else
755 options &= ~flag;
757 NYD2_LEAVE;
758 return ok;
761 static bool_t
762 a_amv_var_check_nocntrls(char const *val){
763 char c;
764 NYD2_ENTER;
766 while((c = *val++) != '\0')
767 if(cntrlchar(c))
768 break;
769 NYD2_LEAVE;
770 return (c == '\0');
773 static bool_t
774 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
775 /* TODO The internal/environment variables which are num= or posnum= should
776 * TODO gain special lookup functions, or the return should be void* and
777 * TODO castable to integer; i.e. no more strtoX() should be needed.
778 * TODO I.e., the result of this function should instead be stored.
779 * TODO Use intmax_t IF that is sizeof(void*) only? */
780 bool_t rv;
781 NYD2_ENTER;
783 rv = TRU1;
785 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
786 char *eptr;
787 union {long s; unsigned long u;} i;
789 if(!pos){
790 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
792 if(*eptr != '\0' ||
793 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
794 rv = FAL0;
795 #if INT_MIN != LONG_MIN
796 else if(i.s < INT_MIN)
797 rv = FAL0;
798 #endif
799 #if INT_MAX != LONG_MAX
800 else if(i.s > INT_MAX)
801 rv = FAL0;
802 #endif
803 }else{
804 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
806 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
807 rv = FAL0;
808 #if UINT_MAX != ULONG_MAX
809 else if(i.u > UINT_MAX)
810 rv = FAL0;
811 #endif
814 NYD2_LEAVE;
815 return rv;
818 static char const *
819 a_amv_var_canonify(char const *vn){
820 NYD2_ENTER;
821 if(!upperchar(*vn)){
822 char const *vp;
824 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
826 vn = (*vp == '@') ? i_strdup(vn) : vn;
828 NYD2_LEAVE;
829 return vn;
832 static bool_t
833 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
834 ui32_t hash, i, j;
835 struct a_amv_var_map const *avmp;
836 NYD2_ENTER;
838 avcp->avc_name = name = a_amv_var_canonify(name);
839 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
841 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
842 ui32_t x = a_amv_var_revmap[i];
844 if(x == a_AMV_VAR_REV_ILL)
845 break;
847 avmp = &a_amv_var_map[x];
848 if(avmp->avm_hash == hash &&
849 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
850 avcp->avc_map = avmp;
851 avcp->avc_okey = (enum okeys)x;
852 goto jleave;
855 if(++i == a_AMV_VAR_REV_PRIME){
856 #ifdef a_AMV_VAR_REV_WRAPAROUND
857 i = 0;
858 #else
859 break;
860 #endif
863 avcp->avc_map = NULL;
864 avcp = NULL;
865 jleave:
866 NYD2_LEAVE;
867 return (avcp != NULL);
870 static bool_t
871 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
872 size_t i;
873 char const *cp;
874 struct a_amv_var_map const *avmp;
875 struct a_amv_var *avp;
876 NYD2_ENTER;
878 /* C99 */{
879 struct a_amv_var **avpp, *lavp;
881 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
883 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
884 if(!strcmp(avp->av_name, avcp->avc_name)){
885 /* Relink as head, hope it "sorts on usage" over time.
886 * _clear() relies on this behaviour */
887 if(lavp != NULL){
888 lavp->av_link = avp->av_link;
889 avp->av_link = *avpp;
890 *avpp = avp;
892 goto jleave;
896 /* If this is not an assembled variable we need to consider some special
897 * initialization cases and eventually create the variable anew */
898 if(LIKELY((avmp = avcp->avc_map) != NULL)){
899 /* Does it have an import-from-environment flag? */
900 if(UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
901 if(LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
902 /* May be better not to use that one, though? */
903 bool_t isempty, isbltin;
905 isempty = (*cp == '\0' &&
906 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
907 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
908 ) != 0);
910 if(UNLIKELY(isempty)){
911 if(!isbltin)
912 goto jerr;
913 }else if(LIKELY(*cp != '\0')){
914 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
915 !a_amv_var_check_nocntrls(cp))){
916 n_err(_("Ignoring environment, control characters "
917 "invalid in variable: %s\n"), avcp->avc_name);
918 goto jerr;
920 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
921 !a_amv_var_check_num(cp, FAL0))){
922 n_err(_("Environment variable value not a number "
923 "or out of range: %s\n"), avcp->avc_name);
924 goto jerr;
926 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
927 !a_amv_var_check_num(cp, TRU1))){
928 n_err(_("Environment variable value not a number, "
929 "negative or out of range: %s\n"), avcp->avc_name);
930 goto jerr;
933 goto jnewval;
937 /* A first-time init switch is to be handled now and here */
938 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
939 static struct a_amv_var_defval const **arr,
940 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
942 if(arr == NULL){
943 arr = &arr_base[0];
944 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
945 while(i-- > 0)
946 arr[i] = &a_amv_var_i3vals[i];
949 for(i = 0; arr[i] != NULL; ++i)
950 if(arr[i]->avdv_okey == avcp->avc_okey){
951 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
952 : arr[i]->avdv_value;
953 /* Remove this entry, hope entire block becomes no-op asap */
955 arr[i] = arr[i + 1];
956 while(arr[i++] != NULL);
958 if(!i3val_nonew)
959 goto jnewval;
960 if(i3val_nonew == TRUM1)
961 avp = (struct a_amv_var*)-1;
962 goto jleave;
966 /* The virtual variables */
967 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
968 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
969 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
970 avp = UNCONST(a_amv_var_virts[i].avv_var);
971 goto jleave;
973 /* Not reached */
976 /* Place this last because once it is set first the variable will never
977 * be removed again and thus match in the first block above */
978 if(UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
979 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
980 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
981 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
982 : a_amv_var_defvals[i].avdv_value;
983 goto jnewval;
988 jerr:
989 avp = NULL;
990 jleave:
991 avcp->avc_var = avp;
992 NYD2_LEAVE;
993 return (avp != NULL);
995 jnewval: /* C99 */{
996 struct a_amv_var **avpp;
997 size_t l;
999 l = strlen(avcp->avc_name) +1;
1000 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1001 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1002 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1003 *avpp = avp;
1004 memcpy(avp->av_name, avcp->avc_name, l);
1005 avp->av_value = a_amv_var_copy(cp);
1006 #ifdef HAVE_PUTENV
1007 avp->av_env = NULL;
1008 #endif
1009 avp->av_flags = avmp->avm_flags;
1011 if(avp->av_flags & a_AMV_VF_VIP)
1012 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1013 if(avp->av_flags & a_AMV_VF_ENV)
1014 a_amv_var__putenv(avcp, avp);
1015 goto jleave;
1019 static bool_t
1020 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1021 bool_t force_env){
1022 struct a_amv_var *avp;
1023 char *oval;
1024 struct a_amv_var_map const *avmp;
1025 bool_t rv;
1026 NYD2_ENTER;
1028 if(value == NULL){
1029 rv = a_amv_var_clear(avcp, force_env);
1030 goto jleave;
1033 if((avmp = avcp->avc_map) != NULL){
1034 rv = FAL0;
1036 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1037 !(pstate & PS_ROOT))){
1038 n_err(_("Variable is readonly: %s\n"), avcp->avc_name);
1039 goto jleave;
1041 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1042 n_err(_("Variable must not be empty: %s\n"), avcp->avc_name);
1043 goto jleave;
1045 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1046 !a_amv_var_check_nocntrls(value))){
1047 n_err(_("Variable forbids control characters: %s\n"),
1048 avcp->avc_name);
1049 goto jleave;
1051 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1052 !a_amv_var_check_num(value, FAL0))){
1053 n_err(_("Variable value not a number or out of range: %s\n"),
1054 avcp->avc_name);
1055 goto jleave;
1057 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1058 !a_amv_var_check_num(value, TRU1))){
1059 n_err(_("Variable value not a number, negative or out of range: %s\n"),
1060 avcp->avc_name);
1061 goto jleave;
1063 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1064 !(pstate & (PS_ROOT | PS_STARTED)))){
1065 n_err(_("Variable cannot be set in a resource file: %s\n"),
1066 avcp->avc_name);
1067 goto jleave;
1071 rv = TRU1;
1072 a_amv_var_lookup(avcp, TRU1);
1074 /* Don't care what happens later on, store this in the unroll list */
1075 if(a_amv_lopts != NULL)
1076 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1078 if((avp = avcp->avc_var) == NULL){
1079 struct a_amv_var **avpp;
1080 size_t l = strlen(avcp->avc_name) +1;
1082 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1083 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1084 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1085 *avpp = avp;
1086 #ifdef HAVE_PUTENV
1087 avp->av_env = NULL;
1088 #endif
1089 memcpy(avp->av_name, avcp->avc_name, l);
1090 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1091 oval = UNCONST("");
1092 }else
1093 oval = avp->av_value;
1095 if(avmp == NULL)
1096 avp->av_value = a_amv_var_copy(value);
1097 else{
1098 /* Via `set' etc. the user may give even boolean options non-boolean
1099 * values, ignore that and force boolean */
1100 if(avp->av_flags & a_AMV_VF_BOOL){
1101 if((options & OPT_D_VV) && *value != '\0')
1102 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1103 avcp->avc_name, value);
1104 avp->av_value = UNCONST(value = "");
1105 }else
1106 avp->av_value = a_amv_var_copy(value);
1108 /* Check if update allowed XXX wasteful on error! */
1109 if((avp->av_flags & a_AMV_VF_VIP) &&
1110 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1111 char *cp = avp->av_value;
1113 avp->av_value = oval;
1114 oval = cp;
1118 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1119 avp->av_flags |= a_AMV_VF_LINKED;
1120 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1121 rv = a_amv_var__putenv(avcp, avp);
1123 a_amv_var_free(oval);
1124 jleave:
1125 NYD2_LEAVE;
1126 return rv;
1129 static bool_t
1130 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1131 #ifndef HAVE_SETENV
1132 char *cp;
1133 #endif
1134 bool_t rv;
1135 NYD2_ENTER;
1137 #ifdef HAVE_SETENV
1138 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1139 #else
1140 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1142 if((rv = (putenv(cp) == 0))){
1143 char *ocp;
1145 if((ocp = avp->av_env) != NULL)
1146 free(ocp);
1147 avp->av_env = cp;
1148 }else
1149 free(cp);
1150 #endif
1151 NYD2_LEAVE;
1152 return rv;
1155 static bool_t
1156 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1157 struct a_amv_var **avpp, *avp;
1158 struct a_amv_var_map const *avmp;
1159 bool_t rv;
1160 NYD2_ENTER;
1162 rv = FAL0;
1164 if(LIKELY((avmp = avcp->avc_map) != NULL)){
1165 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1166 !(pstate & PS_ROOT))){
1167 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1168 goto jleave;
1170 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1171 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1172 goto jleave;
1175 rv = TRU1;
1177 if(UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1178 if(force_env){
1179 jforce_env:
1180 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1181 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1182 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1183 goto jleave;
1184 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1185 avcp->avc_var = NULL;
1186 if(force_env)
1187 goto jforce_env;
1188 goto jleave;
1191 if(a_amv_lopts != NULL)
1192 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1194 avp = avcp->avc_var;
1195 avcp->avc_var = NULL;
1196 avpp = &a_amv_vars[avcp->avc_prime];
1197 assert(*avpp == avp); /* (always listhead after lookup()) */
1198 *avpp = (*avpp)->av_link;
1200 /* C99 */{
1201 #ifdef HAVE_SETENV
1202 char *envval = NULL;
1203 #else
1204 char *envval = avp->av_env;
1205 #endif
1206 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1207 rv = a_amv_var__clearenv(avp->av_name, envval);
1209 a_amv_var_free(avp->av_value);
1210 free(avp);
1212 /* XXX Fun part, extremely simple-minded for now: if this variable has
1213 * XXX a default value, immediately reinstantiate it! */
1214 if(UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1215 a_amv_var_lookup(avcp, TRU1);
1216 jleave:
1217 NYD2_LEAVE;
1218 return rv;
1221 static bool_t
1222 a_amv_var__clearenv(char const *name, char *value){
1223 #ifndef HAVE_SETENV
1224 extern char **environ;
1225 char **ecpp;
1226 #endif
1227 bool_t rv;
1228 NYD2_ENTER;
1229 UNUSED(value);
1231 #ifdef HAVE_SETENV
1232 unsetenv(name);
1233 rv = TRU1;
1234 #else
1235 if(value != NULL)
1236 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1237 if(*ecpp == value){
1238 free(value);
1240 ecpp[0] = ecpp[1];
1241 while(*ecpp++ != NULL);
1242 break;
1244 rv = TRU1;
1245 #endif
1246 NYD2_LEAVE;
1247 return rv;
1250 static void
1251 a_amv_var_show_all(void){
1252 struct n_string msg, *msgp;
1253 FILE *fp;
1254 size_t no, i;
1255 struct a_amv_var *avp;
1256 char const **vacp, **cap;
1257 NYD2_ENTER;
1259 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1260 n_perr(_("Can't create temporary file for `set' listing"), 0);
1261 goto jleave;
1264 /* We need to instantiate first-time-inits and default values here, so that
1265 * they will be regular members of our _vars[] table */
1266 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1267 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1268 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1269 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1271 for(no = i = 0; i < a_AMV_PRIME; ++i)
1272 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1273 ++no;
1274 no += a_AMV_VAR_VIRTS_CNT;
1276 vacp = salloc(no * sizeof(*vacp));
1278 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1279 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1280 *cap++ = avp->av_name;
1281 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1282 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1284 if(no > 1)
1285 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1287 msgp = &msg;
1288 msgp = n_string_reserve(n_string_creat(msgp), 80);
1289 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1290 i += a_amv_var_show(*cap, fp, msgp);
1291 n_string_gut(&msg);
1293 page_or_print(fp, i);
1294 Fclose(fp);
1295 jleave:
1296 NYD2_LEAVE;
1299 static int
1300 a_amv_var__show_cmp(void const *s1, void const *s2){
1301 int rv;
1302 NYD2_ENTER;
1304 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
1305 NYD2_LEAVE;
1306 return rv;
1309 static size_t
1310 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1311 struct a_amv_var_carrier avc;
1312 char const *quote;
1313 size_t i;
1314 NYD2_ENTER;
1316 msgp = n_string_trunc(msgp, 0);
1317 i = 0;
1319 a_amv_var_revlookup(&avc, name);
1320 if(!a_amv_var_lookup(&avc, FAL0)){
1321 struct str s;
1323 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1324 s.s = UNCONST(name);
1325 s.l = UIZ_MAX;
1326 msgp = n_shexp_quote(msgp, &s, FAL0);
1327 goto jleave;
1330 if(options & OPT_D_V){
1331 if(avc.avc_map == NULL){
1332 msgp = n_string_push_c(msgp, '#');
1333 msgp = n_string_push_cp(msgp, "assembled");
1334 i = 1;
1336 /* C99 */{
1337 struct{
1338 ui16_t flags;
1339 char msg[22];
1340 } const tbase[] = {
1341 {a_AMV_VF_VIRT, "virtual"},
1342 {a_AMV_VF_RDONLY, "readonly"},
1343 {a_AMV_VF_NODEL, "nodelete"},
1344 {a_AMV_VF_NOTEMPTY, "notempty"},
1345 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1346 {a_AMV_VF_NUM, "number"},
1347 {a_AMV_VF_POSNUM, "positive-number"},
1348 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1349 {a_AMV_VF_ENV, "sync-environ"},
1350 {a_AMV_VF_I3VAL, "initial-value"},
1351 {a_AMV_VF_DEFVAL, "default-value"},
1352 {a_AMV_VF_LINKED, "`environ' link"}
1353 }, *tp;
1355 for(tp = tbase; PTRCMP(tp, <, &tbase[NELEM(tbase)]); ++tp)
1356 if(avc.avc_var->av_flags & tp->flags){
1357 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1358 msgp = n_string_push_cp(msgp, tp->msg);
1362 if(i > 0)
1363 msgp = n_string_push_cp(msgp, "\n ");
1366 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1367 msgp = n_string_push_cp(msgp, "# ");
1368 UNINIT(quote, NULL);
1369 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1370 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1371 if(strcmp(quote, avc.avc_var->av_value))
1372 msgp = n_string_push_cp(msgp, "wysh ");
1374 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1375 msgp = n_string_push_cp(msgp, "environ ");
1376 msgp = n_string_push_cp(msgp, "set ");
1377 msgp = n_string_push_cp(msgp, name);
1378 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1379 msgp = n_string_push_c(msgp, '=');
1380 msgp = n_string_push_cp(msgp, quote);
1383 jleave:
1384 msgp = n_string_push_c(msgp, '\n');
1385 fputs(n_string_cp(msgp), fp);
1386 NYD2_ENTER;
1387 return (i > 0 ? 2 : 1);
1390 static bool_t
1391 a_amv_var_c_set(char **ap, bool_t issetenv){
1392 char *cp, *cp2, *varbuf, c;
1393 size_t errs;
1394 NYD2_ENTER;
1396 errs = 0;
1397 jouter:
1398 while((cp = *ap++) != NULL){
1399 /* Isolate key */
1400 cp2 = varbuf = salloc(strlen(cp) +1);
1402 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1403 if(cntrlchar(c) || whitechar(c)){
1404 n_err(_("Variable name with control character ignored: %s\n"),
1405 ap[-1]);
1406 ++errs;
1407 goto jouter;
1409 *cp2++ = c;
1411 *cp2 = '\0';
1412 if(c == '\0')
1413 cp = UNCONST("");
1414 else
1415 ++cp;
1417 if(varbuf == cp2){
1418 n_err(_("Empty variable name ignored\n"));
1419 ++errs;
1420 }else{
1421 struct a_amv_var_carrier avc;
1422 bool_t isunset;
1424 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1425 varbuf = &varbuf[2];
1427 a_amv_var_revlookup(&avc, varbuf);
1429 if(isunset)
1430 errs += !a_amv_var_clear(&avc, issetenv);
1431 else
1432 errs += !a_amv_var_set(&avc, cp, issetenv);
1435 NYD2_LEAVE;
1436 return (errs == 0);
1439 FL int
1440 c_define(void *v){
1441 int rv;
1442 char **args;
1443 NYD_ENTER;
1445 rv = 1;
1447 if((args = v)[0] == NULL){
1448 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1449 goto jleave;
1452 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1453 args[2] != NULL){
1454 n_err(_("Synopsis: define: <name> {\n"));
1455 goto jleave;
1458 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1459 jleave:
1460 NYD_LEAVE;
1461 return rv;
1464 FL int
1465 c_undefine(void *v){
1466 int rv;
1467 char **args;
1468 NYD_ENTER;
1470 rv = 0;
1471 args = v;
1473 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1474 while(*++args != NULL);
1475 NYD_LEAVE;
1476 return rv;
1479 FL int
1480 c_call(void *v){
1481 struct a_amv_mac_call_args amca;
1482 char const *errs, *name;
1483 struct a_amv_mac *amp;
1484 char **args;
1485 int rv;
1486 NYD_ENTER;
1488 rv = 1;
1490 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1491 errs = _("Synopsis: call: <%s>\n");
1492 name = "name";
1493 goto jerr;
1496 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1497 errs = _("Undefined macro `call'ed: %s\n");
1498 goto jerr;
1501 memset(&amca, 0, sizeof amca);
1502 amca.amca_name = name;
1503 amca.amca_amp = amp;
1504 rv = (a_amv_mac_exec(&amca) == FAL0);
1505 jleave:
1506 NYD_LEAVE;
1507 return rv;
1508 jerr:
1509 n_err(errs, name);
1510 goto jleave;
1513 FL bool_t
1514 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1515 struct a_amv_mac_call_args amca;
1516 struct a_amv_mac *amp;
1517 size_t len;
1518 char *var, *cp;
1519 bool_t rv;
1520 NYD_ENTER;
1522 rv = TRU1;
1523 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1525 /* First try the fully resolved path */
1526 snprintf(var, len, "folder-hook-%s", mailname);
1527 if((cp = vok_vlook(var)) != NULL)
1528 goto jmac;
1530 /* If we are under *folder*, try the usual +NAME syntax, too */
1531 if(displayname[0] == '+'){
1532 char *x = mailname + len;
1534 for(; x > mailname; --x)
1535 if(x[-1] == '/'){
1536 snprintf(var, len, "folder-hook-+%s", x);
1537 if((cp = vok_vlook(var)) != NULL)
1538 goto jmac;
1539 break;
1543 /* Plain *folder-hook* is our last try */
1544 if((cp = ok_vlook(folder_hook)) == NULL)
1545 goto jleave;
1547 jmac:
1548 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1549 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1550 n_shexp_quote_cp(displayname, FAL0), cp);
1551 rv = FAL0;
1552 goto jleave;
1555 memset(&amca, 0, sizeof amca);
1556 amca.amca_name = cp;
1557 amca.amca_amp = amp;
1558 pstate &= ~PS_HOOK_MASK;
1559 if(nmail){
1560 amca.amca_unroller = NULL;
1561 pstate |= PS_HOOK_NEWMAIL;
1562 }else{
1563 amca.amca_unroller = &a_amv_folder_hook_lopts;
1564 pstate |= PS_HOOK;
1566 amca.amca_lopts_on = TRU1;
1567 rv = a_amv_mac_exec(&amca);
1568 pstate &= ~PS_HOOK_MASK;
1570 jleave:
1571 NYD_LEAVE;
1572 return rv;
1575 FL void
1576 call_compose_mode_hook(char const *macname, /* TODO temporary, v15: drop */
1577 void (*hook_pre)(void *), void *hook_arg){
1578 struct a_amv_mac_call_args amca;
1579 struct a_amv_mac *amp;
1580 NYD_ENTER;
1582 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1583 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1584 macname);
1585 else{
1586 memset(&amca, 0, sizeof amca);
1587 amca.amca_name = macname;
1588 amca.amca_amp = amp;
1589 amca.amca_unroller = &a_amv_compose_lopts;
1590 amca.amca_hook_pre = hook_pre;
1591 amca.amca_hook_arg = hook_arg;
1592 amca.amca_lopts_on = TRU1;
1593 pstate &= ~PS_HOOK_MASK;
1594 pstate |= PS_HOOK;
1595 a_amv_mac_exec(&amca);
1596 pstate &= ~PS_HOOK_MASK;
1598 NYD_LEAVE;
1601 FL int
1602 c_account(void *v){
1603 struct a_amv_mac_call_args amca;
1604 struct a_amv_mac *amp;
1605 char **args;
1606 int rv, i, oqf, nqf;
1607 NYD_ENTER;
1609 rv = 1;
1611 if((args = v)[0] == NULL){
1612 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1613 goto jleave;
1616 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1617 if(args[2] != NULL){
1618 n_err(_("Synopsis: account: <name> {\n"));
1619 goto jleave;
1621 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1622 n_err(_("`account': cannot use reserved name: %s\n"),
1623 ACCOUNT_NULL);
1624 goto jleave;
1626 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1627 goto jleave;
1630 if(pstate & PS_HOOK_MASK){
1631 n_err(_("`account': can't change account from within a hook\n"));
1632 goto jleave;
1635 save_mbox_for_possible_quitstuff();
1637 amp = NULL;
1638 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1639 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1640 n_err(_("`account': account does not exist: %s\n"), args[0]);
1641 goto jleave;
1644 oqf = savequitflags();
1646 if(a_amv_acc_curr != NULL){
1647 if(a_amv_acc_curr->am_lopts != NULL)
1648 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1649 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1650 a_amv_mac_free(a_amv_acc_curr);
1653 account_name = (amp != NULL) ? amp->am_name : NULL;
1654 a_amv_acc_curr = amp;
1656 if(amp != NULL){
1657 assert(amp->am_lopts == NULL);
1658 memset(&amca, 0, sizeof amca);
1659 amca.amca_name = amp->am_name;
1660 amca.amca_amp = amp;
1661 amca.amca_unroller = &amp->am_lopts;
1662 amca.amca_lopts_on = TRU1;
1663 if(!a_amv_mac_exec(&amca)){
1664 /* XXX account switch incomplete, unroll? */
1665 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1666 goto jleave;
1670 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1671 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1672 restorequitflags(oqf);
1673 if((i = setfile("%", 0)) < 0)
1674 goto jleave;
1675 check_folder_hook(FAL0);
1676 if(i > 0 && !ok_blook(emptystart))
1677 goto jleave;
1678 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1679 restorequitflags(nqf);
1681 rv = 0;
1682 jleave:
1683 NYD_LEAVE;
1684 return rv;
1687 FL int
1688 c_unaccount(void *v){
1689 int rv;
1690 char **args;
1691 NYD_ENTER;
1693 rv = 0;
1694 args = v;
1696 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1697 while(*++args != NULL);
1698 NYD_LEAVE;
1699 return rv;
1702 FL int
1703 c_localopts(void *v){
1704 char **argv;
1705 int rv;
1706 NYD_ENTER;
1708 rv = 1;
1710 if(a_amv_lopts == NULL){
1711 n_err(_("Cannot use `localopts' but from within a "
1712 "`define' or `account'\n"));
1713 goto jleave;
1716 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1717 rv = 0;
1718 jleave:
1719 NYD_LEAVE;
1720 return rv;
1723 FL void
1724 temporary_localopts_free(void){ /* XXX intermediate hack */
1725 NYD_ENTER;
1726 if(a_amv_compose_lopts != NULL){
1727 void *save = a_amv_lopts;
1729 a_amv_lopts = NULL;
1730 a_amv_lopts_unroll(&a_amv_compose_lopts);
1731 a_amv_compose_lopts = NULL;
1732 a_amv_lopts = save;
1734 NYD_LEAVE;
1737 FL void
1738 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1739 NYD_ENTER;
1740 if(a_amv_folder_hook_lopts != NULL){
1741 void *save = a_amv_lopts;
1743 a_amv_lopts = NULL;
1744 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1745 a_amv_folder_hook_lopts = NULL;
1746 a_amv_lopts = save;
1748 NYD_LEAVE;
1751 FL char *
1752 n_var_oklook(enum okeys okey){
1753 struct a_amv_var_carrier avc;
1754 char *rv;
1755 struct a_amv_var_map const *avmp;
1756 NYD_ENTER;
1758 avc.avc_map = avmp = &a_amv_var_map[okey];
1759 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1760 avc.avc_hash = avmp->avm_hash;
1761 avc.avc_okey = okey;
1763 if(a_amv_var_lookup(&avc, FAL0))
1764 rv = avc.avc_var->av_value;
1765 else
1766 rv = NULL;
1767 NYD_LEAVE;
1768 return rv;
1771 FL bool_t
1772 n_var_okset(enum okeys okey, uintptr_t val){
1773 struct a_amv_var_carrier avc;
1774 bool_t ok;
1775 struct a_amv_var_map const *avmp;
1776 NYD_ENTER;
1778 avc.avc_map = avmp = &a_amv_var_map[okey];
1779 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1780 avc.avc_hash = avmp->avm_hash;
1781 avc.avc_okey = okey;
1783 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1784 NYD_LEAVE;
1785 return ok;
1788 FL bool_t
1789 n_var_okclear(enum okeys okey){
1790 struct a_amv_var_carrier avc;
1791 bool_t rv;
1792 struct a_amv_var_map const *avmp;
1793 NYD_ENTER;
1795 avc.avc_map = avmp = &a_amv_var_map[okey];
1796 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1797 avc.avc_hash = avmp->avm_hash;
1798 avc.avc_okey = okey;
1800 rv = a_amv_var_clear(&avc, FAL0);
1801 NYD_LEAVE;
1802 return rv;
1805 FL char *
1806 n_var_voklook(char const *vokey){
1807 struct a_amv_var_carrier avc;
1808 char *rv;
1809 NYD_ENTER;
1811 a_amv_var_revlookup(&avc, vokey);
1813 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1814 NYD_LEAVE;
1815 return rv;
1818 FL bool_t
1819 n_var_vokset(char const *vokey, uintptr_t val){
1820 struct a_amv_var_carrier avc;
1821 bool_t ok;
1822 NYD_ENTER;
1824 a_amv_var_revlookup(&avc, vokey);
1826 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1827 NYD_LEAVE;
1828 return ok;
1831 FL bool_t
1832 n_var_vokclear(char const *vokey){
1833 struct a_amv_var_carrier avc;
1834 bool_t ok;
1835 NYD_ENTER;
1837 a_amv_var_revlookup(&avc, vokey);
1839 ok = a_amv_var_clear(&avc, FAL0);
1840 NYD_LEAVE;
1841 return ok;
1844 #ifdef HAVE_SOCKETS
1845 FL char *
1846 n_var_xoklook(enum okeys okey, struct url const *urlp,
1847 enum okey_xlook_mode oxm){
1848 struct a_amv_var_carrier avc;
1849 struct str const *us;
1850 size_t nlen;
1851 char *nbuf, *rv;
1852 struct a_amv_var_map const *avmp;
1853 NYD_ENTER;
1855 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1857 /* For simplicity: allow this case too */
1858 if(!(oxm & (OXM_H_P | OXM_U_H_P)))
1859 goto jplain;
1861 avc.avc_map = avmp = &a_amv_var_map[okey];
1862 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1863 avc.avc_okey = okey;
1865 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1866 nlen = strlen(avc.avc_name);
1867 nbuf = salloc(nlen + 1 + us->l +1);
1868 memcpy(nbuf, avc.avc_name, nlen);
1869 nbuf[nlen++] = '-';
1871 /* One of .url_u_h_p and .url_h_p we test in here */
1872 memcpy(nbuf + nlen, us->s, us->l +1);
1873 avc.avc_name = a_amv_var_canonify(nbuf);
1874 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1875 if(a_amv_var_lookup(&avc, FAL0))
1876 goto jvar;
1878 /* The second */
1879 if(oxm & OXM_H_P){
1880 us = &urlp->url_h_p;
1881 memcpy(nbuf + nlen, us->s, us->l +1);
1882 avc.avc_name = a_amv_var_canonify(nbuf);
1883 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1884 if(a_amv_var_lookup(&avc, FAL0)){
1885 jvar:
1886 rv = avc.avc_var->av_value;
1887 goto jleave;
1891 jplain:
1892 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1893 jleave:
1894 NYD_LEAVE;
1895 return rv;
1897 #endif /* HAVE_SOCKETS */
1899 FL int
1900 c_set(void *v){
1901 char **ap;
1902 int err;
1903 NYD_ENTER;
1905 if(*(ap = v) == NULL){
1906 a_amv_var_show_all();
1907 err = 0;
1908 }else
1909 err = !a_amv_var_c_set(ap, FAL0);
1910 NYD_LEAVE;
1911 return err;
1914 FL int
1915 c_unset(void *v){
1916 char **ap;
1917 int err;
1918 NYD_ENTER;
1920 for(err = 0, ap = v; *ap != NULL; ++ap)
1921 err |= !n_var_vokclear(*ap);
1922 NYD_LEAVE;
1923 return err;
1926 FL int
1927 c_varshow(void *v){
1928 char **ap;
1929 NYD_ENTER;
1931 if(*(ap = v) == NULL)
1932 v = NULL;
1933 else{
1934 struct n_string msg, *msgp = &msg;
1936 msgp = n_string_creat(msgp);
1937 for(; *ap != NULL; ++ap)
1938 a_amv_var_show(*ap, stdout, msgp);
1939 n_string_gut(msgp);
1941 NYD_LEAVE;
1942 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1945 FL int
1946 c_varedit(void *v){
1947 struct a_amv_var_carrier avc;
1948 FILE *of, *nf;
1949 char *val, **argv;
1950 int err;
1951 sighandler_type sigint;
1952 NYD_ENTER;
1954 sigint = safe_signal(SIGINT, SIG_IGN);
1956 for(err = 0, argv = v; *argv != NULL; ++argv){
1957 memset(&avc, 0, sizeof avc);
1959 a_amv_var_revlookup(&avc, *argv);
1961 if(avc.avc_map != NULL){
1962 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1963 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
1964 avc.avc_name);
1965 continue;
1967 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
1968 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
1969 avc.avc_name);
1970 continue;
1974 a_amv_var_lookup(&avc, FAL0);
1976 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1977 NULL){
1978 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1979 err = 1;
1980 break;
1981 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
1982 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
1983 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1984 Fclose(of);
1985 err = 1;
1986 continue;
1989 fflush_rewind(of);
1990 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1991 Fclose(of);
1993 if(nf != NULL){
1994 int c;
1995 char *base;
1996 off_t l = fsize(nf);
1998 assert(l >= 0);
1999 base = salloc((size_t)l +1);
2001 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2002 if(c == '\n' || c == '\r'){
2003 *val = ' ';
2004 ++l;
2005 }else{
2006 *val = (char)(uc_i)c;
2007 l = 0;
2009 val -= l;
2010 *val = '\0';
2012 if(!a_amv_var_set(&avc, base, FAL0))
2013 err = 1;
2015 Fclose(nf);
2016 }else{
2017 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2018 err = 1;
2019 break;
2023 safe_signal(SIGINT, sigint);
2024 NYD_LEAVE;
2025 return err;
2028 FL int
2029 c_environ(void *v){
2030 struct a_amv_var_carrier avc;
2031 int err;
2032 char **ap;
2033 bool_t islnk;
2034 NYD_ENTER;
2036 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2037 for(err = 0; *++ap != NULL;){
2038 a_amv_var_revlookup(&avc, *ap);
2040 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2041 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2042 if(!islnk){
2043 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2044 continue;
2045 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2046 if(options & OPT_D_V)
2047 n_err(_("`environ': link: already established: %s\n"), *ap);
2048 continue;
2050 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2051 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2052 a_amv_var__putenv(&avc, avc.avc_var);
2053 }else if(!islnk){
2054 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2055 err = 1;
2056 }else{
2057 char const *evp = getenv(*ap);
2059 if(evp != NULL)
2060 err |= !a_amv_var_set(&avc, evp, TRU1);
2061 else{
2062 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2063 *ap);
2064 err = 1;
2068 }else if(!strcmp(*ap, "set"))
2069 err = !a_amv_var_c_set(++ap, TRU1);
2070 else if(!strcmp(*ap, "unset")){
2071 for(err = 0; *++ap != NULL;){
2072 a_amv_var_revlookup(&avc, *ap);
2074 if(!a_amv_var_clear(&avc, TRU1))
2075 err = 1;
2077 }else{
2078 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2079 err = 1;
2081 NYD_LEAVE;
2082 return err;
2085 /* s-it-mode */