Support LEXINPUT, thus `bind', contexts..
[s-mailx.git] / accmacvar.c
blobf53655e4bd540b239fb914d11deb034fac13e9a2
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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 # define _ACCMACVAR_SOURCE /* For _features[] */
51 # include "nail.h"
52 #endif
54 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
55 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
56 #endif
58 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
59 #define a_AMV_PRIME HSHSIZE
60 #define a_AMV_NAME2HASH(N) torek_hash(N)
61 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
63 enum a_amv_mac_flags{
64 a_AMV_MF_NONE = 0,
65 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
66 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
67 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
68 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
69 a_AMV_MF__MAX = 0xFF
72 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
73 * _IMPORT implies _ENV; it doesn't verify anything... */
74 enum a_amv_var_flags{
75 a_AMV_VF_NONE = 0,
76 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
77 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
78 a_AMV_VF_RDONLY = 1<<2, /* May not be set by user */
79 a_AMV_VF_NODEL = 1<<3, /* May not be deleted */
80 a_AMV_VF_NOTEMPTY = 1<<4, /* May not be assigned an empty value */
81 a_AMV_VF_NOCNTRLS = 1<<5, /* Value may not contain control characters */
82 a_AMV_VF_NUM = 1<<6, /* Value must be a 32-bit number */
83 a_AMV_VF_POSNUM = 1<<7, /* Value must be positive 32-bit number */
84 a_AMV_VF_VIP = 1<<8, /* Wants _var_check_vips() evaluation */
85 a_AMV_VF_IMPORT = 1<<9, /* Import ONLY from environ (before PS_STARTED) */
86 a_AMV_VF_ENV = 1<<10, /* Update environment on change */
87 a_AMV_VF_I3VAL = 1<<11, /* Has an initial value */
88 a_AMV_VF_DEFVAL = 1<<12, /* Has a default value */
89 a_AMV_VF_LINKED = 1<<13, /* `environ' linked */
90 a_AMV_VF__MASK = (1<<(13+1)) - 1
93 struct a_amv_mac{
94 struct a_amv_mac *am_next;
95 ui32_t am_maxlen; /* of any line in .am_line_dat */
96 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
97 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
98 struct a_amv_var *am_lopts; /* `localopts' unroll list */
99 ui8_t am_flags; /* enum a_amv_mac_flags */
100 char am_name[VFIELD_SIZE(7)]; /* of this macro */
102 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
104 struct a_amv_mac_line{
105 ui32_t aml_len;
106 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
107 char aml_dat[VFIELD_SIZE(0)];
110 struct a_amv_mac_call_args{
111 char const *amca_name;
112 struct a_amv_mac const *amca_amp;
113 struct a_amv_var **amca_unroller;
114 void (*amca_hook_pre)(void *);
115 void (*amca_hook_post)(void *);
116 void *amca_hook_arg;
117 bool_t amca_lopts_on;
118 ui8_t amca__pad[7];
121 struct a_amv_lostack{
122 struct a_amv_lostack *as_up; /* Outer context */
123 struct a_amv_mac *as_mac; /* Context (`account' or `define') */
124 struct a_amv_var *as_lopts;
125 bool_t as_unroll; /* Unrolling enabled? */
126 ui8_t avs__pad[7];
129 struct a_amv_var{
130 struct a_amv_var *av_link;
131 char *av_value;
132 #ifdef HAVE_PUTENV
133 char *av_env; /* Actively managed putenv(3) memory */
134 #endif
135 ui16_t av_flags; /* enum a_amv_var_flags */
136 char av_name[VFIELD_SIZE(6)];
138 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
140 struct a_amv_var_map{
141 ui32_t avm_hash;
142 ui16_t avm_keyoff;
143 ui16_t avm_flags; /* enum a_amv_var_flags */
145 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
147 struct a_amv_var_virt{
148 ui32_t avv_okey;
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;
167 /* Include the constant mk-okey-map.pl output */
168 #include "version.h"
169 #include "okeys.h"
171 /* The currently active account */
172 static struct a_amv_mac *a_amv_acc_curr;
174 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
176 /* Unroll list of currently running macro stack */
177 static struct a_amv_lostack *a_amv_lopts;
179 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
181 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
182 * TODO today via a static lostack, it should be a field in mailbox, once that
183 * TODO is a real multi-instance object */
184 static struct a_amv_var *a_amv_folder_hook_lopts;
186 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
187 static struct a_amv_var *a_amv_compose_lopts;
189 /* Does cp consist solely of WS and a } */
190 static bool_t a_amv_mac_is_closing_angle(char const *cp);
192 /* Lookup for macros/accounts */
193 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
194 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
196 /* Execute a macro */
197 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
199 /* User display helpers */
200 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
202 /* _def() returns error for faulty definitions and already existing * names,
203 * _undef() returns error if a named thing doesn't exist */
204 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
205 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
207 /* */
208 static void a_amv_mac_free(struct a_amv_mac *amp);
210 /* Update replay-log */
211 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
212 struct a_amv_var *oavp);
213 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
215 /* Special cased value string allocation */
216 static char *a_amv_var_copy(char const *str);
217 static void a_amv_var_free(char *cp);
219 /* Check for special housekeeping */
220 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
222 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
223 static bool_t a_amv_var_check_nocntrls(char const *val);
224 static bool_t a_amv_var_check_num(char const *val, bool_t pos);
226 /* If a variable name begins with a lowercase-character and contains at
227 * least one '@', it is converted to all-lowercase. This is necessary
228 * for lookups of names based on email addresses.
229 * Following the standard, only the part following the last '@' should
230 * be lower-cased, but practice has established otherwise here */
231 static char const *a_amv_var_canonify(char const *vn);
233 /* Try to reverse lookup an option name to an enum okeys mapping.
234 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
235 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
236 char const *name);
238 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
239 * Sets .avc_prime; .avc_var is NULL if not found.
240 * Here it is where we care for _I3VAL and _DEFVAL, too.
241 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
242 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
243 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
244 * return FAL0, then! */
245 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
246 bool_t i3val_nonew);
248 /* Set var from .avc_(map|name|hash), return success */
249 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
250 bool_t force_env);
252 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
253 struct a_amv_var *avp);
255 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
256 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
258 static bool_t a_amv_var__clearenv(char const *name, char *value);
260 /* List all variables */
261 static void a_amv_var_show_all(void);
263 static int a_amv_var__show_cmp(void const *s1, void const *s2);
265 /* Actually do print one, return number of lines written */
266 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
268 /* Shared c_set() and c_environ():set impl, return success */
269 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
271 static bool_t
272 a_amv_mac_is_closing_angle(char const *cp){
273 bool_t rv;
274 NYD2_ENTER;
276 while(spacechar(*cp))
277 ++cp;
279 if((rv = (*cp++ == '}'))){
280 while(spacechar(*cp))
281 ++cp;
282 rv = (*cp == '\0');
284 NYD2_LEAVE;
285 return rv;
288 static struct a_amv_mac *
289 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
290 enum a_amv_mac_flags amf){
291 struct a_amv_mac *amp, **ampp;
292 ui32_t h;
293 enum a_amv_mac_flags save_amf;
294 NYD2_ENTER;
296 save_amf = amf;
297 amf &= a_AMV_MF_TYPE_MASK;
298 h = a_AMV_NAME2HASH(name);
299 h = a_AMV_HASH2PRIME(h);
300 ampp = &a_amv_macs[h];
302 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
303 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
304 !strcmp(amp->am_name, name)){
305 if(LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
306 goto jleave;
308 *ampp = amp->am_next;
310 if((amf & a_AMV_MF_ACC) &&
311 account_name != NULL && !strcmp(account_name, name)){
312 amp->am_flags |= a_AMV_MF_DEL;
313 n_err(_("Delayed deletion of active account: %s\n"), name);
314 }else{
315 a_amv_mac_free(amp);
316 amp = (struct a_amv_mac*)-1;
318 goto jleave;
322 if(newamp != NULL){
323 ampp = &a_amv_macs[h];
324 newamp->am_next = *ampp;
325 *ampp = newamp;
326 amp = NULL;
328 jleave:
329 NYD2_LEAVE;
330 return amp;
333 static bool_t
334 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
335 struct a_amv_lostack los, *los_save;
336 struct a_amv_mac_line **amlp;
337 char **args_base, **args;
338 struct a_amv_mac const *amp;
339 bool_t rv;
340 NYD2_ENTER;
342 amp = amcap->amca_amp;
343 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
344 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
345 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
346 *args = NULL;
348 los.as_mac = UNCONST(amp); /* But not used.. */
349 los_save = a_amv_lopts;
350 if(amcap->amca_unroller == NULL){
351 los.as_up = los_save;
352 los.as_lopts = NULL;
353 }else{
354 los.as_up = NULL;
355 los.as_lopts = *amcap->amca_unroller;
357 los.as_unroll = amcap->amca_lopts_on;
359 a_amv_lopts = &los;
360 if(amcap->amca_hook_pre != NULL)
361 (*amcap->amca_hook_pre)(amcap->amca_hook_arg);
362 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base);
363 if(amcap->amca_hook_post != NULL)
364 (*amcap->amca_hook_post)(amcap->amca_hook_arg);
365 a_amv_lopts = los_save;
367 if(amcap->amca_unroller == NULL){
368 if(los.as_lopts != NULL)
369 a_amv_lopts_unroll(&los.as_lopts);
370 }else
371 *amcap->amca_unroller = los.as_lopts;
372 NYD2_LEAVE;
373 return rv;
376 static bool_t
377 a_amv_mac_show(enum a_amv_mac_flags amf){
378 size_t lc, mc, ti, i;
379 char const *typestr;
380 FILE *fp;
381 bool_t rv;
382 NYD2_ENTER;
384 rv = FAL0;
386 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
387 NULL){
388 n_perr(_("Can't create temporary file for `define' or `account' listing"),
390 goto jleave;
393 amf &= a_AMV_MF_TYPE_MASK;
394 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
396 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
397 struct a_amv_mac *amp;
399 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
400 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
401 struct a_amv_mac_line **amlpp;
403 if(++mc > 1){
404 putc('\n', fp);
405 ++lc;
407 ++lc;
408 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
409 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
410 for(i = (*amlpp)->aml_prespc; i > 0; --i)
411 putc(' ', fp);
412 fputs((*amlpp)->aml_dat, fp);
413 putc('\n', fp);
415 fputs("}\n", fp);
416 ++lc;
420 if(mc > 0)
421 page_or_print(fp, lc);
423 rv = (ferror(fp) == 0);
424 Fclose(fp);
425 jleave:
426 NYD2_LEAVE;
427 return rv;
430 static bool_t
431 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
432 struct str line;
433 ui32_t line_cnt, maxlen;
434 struct linelist{
435 struct linelist *ll_next;
436 struct a_amv_mac_line *ll_amlp;
437 } *llp, *ll_head, *ll_tail;
438 union {size_t s; int i; ui32_t ui; size_t l;} n;
439 struct a_amv_mac *amp;
440 bool_t rv;
441 NYD2_ENTER;
443 memset(&line, 0, sizeof line);
444 rv = FAL0;
445 amp = NULL;
447 /* Read in the lines which form the macro content */
448 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
449 ui32_t leaspc;
450 char *cp;
452 n.i = n_lex_input(n_LEXINPUT_CTX_BASE | n_LEXINPUT_NL_ESC, "",
453 &line.s, &line.l, NULL);
454 if(n.ui == 0)
455 continue;
456 if(n.i < 0){
457 n_err(_("Unterminated %s definition: %s\n"),
458 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
459 goto jerr;
461 if(a_amv_mac_is_closing_angle(line.s))
462 break;
464 /* Trim WS, remember amount of leading spaces for display purposes */
465 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
466 if(*cp == '\t')
467 leaspc = (leaspc + 8) & ~7;
468 else if(*cp == ' ')
469 ++leaspc;
470 else
471 break;
472 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
474 if(n.ui == 0)
475 continue;
477 maxlen = MAX(maxlen, n.ui);
478 cp[n.ui++] = '\0';
480 if(LIKELY(++line_cnt < UI32_MAX)){
481 struct a_amv_mac_line *amlp;
483 llp = salloc(sizeof *llp);
484 if(ll_head == NULL)
485 ll_head = llp;
486 else
487 ll_tail->ll_next = llp;
488 ll_tail = llp;
489 llp->ll_next = NULL;
490 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
491 VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
492 amlp->aml_len = n.ui -1;
493 amlp->aml_prespc = leaspc;
494 memcpy(amlp->aml_dat, cp, n.ui);
495 }else{
496 n_err(_("Too much content in %s definition: %s\n"),
497 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
498 goto jerr;
502 /* Create the new macro */
503 n.s = strlen(name) +1;
504 amp = smalloc(sizeof(*amp) - VFIELD_SIZEOF(struct a_amv_mac, am_name) + n.s);
505 amp->am_next = NULL;
506 amp->am_maxlen = maxlen;
507 amp->am_line_cnt = line_cnt;
508 amp->am_flags = amf;
509 amp->am_lopts = NULL;
510 memcpy(amp->am_name, name, n.s);
511 /* C99 */{
512 struct a_amv_mac_line **amlpp;
514 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
515 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
516 *amlpp++ = llp->ll_amlp;
517 *amlpp = NULL;
520 /* Finally check whether such a macro already exists, in which case we throw
521 * it all away again. At least we know it would have worked */
522 if(a_amv_mac_lookup(name, amp, amf) != NULL){
523 n_err(_("There is already a %s of name: %s\n"),
524 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
525 goto jerr;
528 rv = TRU1;
529 jleave:
530 if(line.s != NULL)
531 free(line.s);
532 NYD2_LEAVE;
533 return rv;
535 jerr:
536 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
537 free(llp->ll_amlp);
538 if(amp != NULL){
539 free(amp->am_line_dat);
540 free(amp);
542 goto jleave;
545 static bool_t
546 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
547 struct a_amv_mac *amp;
548 bool_t rv;
549 NYD2_ENTER;
551 rv = TRU1;
553 if(LIKELY(name[0] != '*' || name[1] != '\0')){
554 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
555 n_err(_("%s not defined: %s\n"),
556 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
557 rv = FAL0;
559 }else{
560 struct a_amv_mac **ampp, *lamp;
562 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[NELEM(a_amv_macs)]);
563 ++ampp)
564 for(lamp = NULL, amp = *ampp; amp != NULL;){
565 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
566 /* xxx Expensive but rare: be simple */
567 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
568 amp = (lamp == NULL) ? *ampp : lamp->am_next;
569 }else{
570 lamp = amp;
571 amp = amp->am_next;
575 NYD2_LEAVE;
576 return rv;
579 static void
580 a_amv_mac_free(struct a_amv_mac *amp){
581 struct a_amv_mac_line **amlpp;
582 NYD2_ENTER;
584 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
585 free(*amlpp);
586 free(amp->am_line_dat);
587 free(amp);
588 NYD2_LEAVE;
591 static void
592 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
593 struct a_amv_var *oavp){
594 struct a_amv_var *avp;
595 size_t nl, vl;
596 NYD2_ENTER;
598 /* Propagate unrolling up the stack, as necessary */
599 assert(alp != NULL);
600 for(;;){
601 if(alp->as_unroll)
602 break;
603 if((alp = alp->as_up) == NULL)
604 goto jleave;
607 /* Check whether this variable is handled yet */
608 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
609 if(!strcmp(avp->av_name, name))
610 goto jleave;
612 nl = strlen(name) +1;
613 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
614 avp = smalloc(sizeof(*avp) - VFIELD_SIZEOF(struct a_amv_var, av_name) +
615 nl + vl);
616 avp->av_link = alp->as_lopts;
617 alp->as_lopts = avp;
618 memcpy(avp->av_name, name, nl);
619 if(vl == 0){
620 avp->av_value = NULL;
621 avp->av_flags = 0;
622 #ifdef HAVE_PUTENV
623 avp->av_env = NULL;
624 #endif
625 }else{
626 avp->av_value = avp->av_name + nl;
627 avp->av_flags = oavp->av_flags;
628 memcpy(avp->av_value, oavp->av_value, vl);
629 #ifdef HAVE_PUTENV
630 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
631 #endif
633 jleave:
634 NYD2_LEAVE;
637 static void
638 a_amv_lopts_unroll(struct a_amv_var **avpp){
639 struct a_amv_lostack *save_alp;
640 bool_t reset;
641 struct a_amv_var *x, *avp;
642 NYD2_ENTER;
644 avp = *avpp;
645 *avpp = NULL;
646 reset = !(pstate & PS_ROOT);
648 save_alp = a_amv_lopts;
649 a_amv_lopts = NULL;
650 while(avp != NULL){
651 x = avp;
652 avp = avp->av_link;
653 pstate |= PS_ROOT;
654 vok_vset(x->av_name, x->av_value);
655 if(reset)
656 pstate &= ~PS_ROOT;
657 free(x);
659 a_amv_lopts = save_alp;
660 NYD2_LEAVE;
663 static char *
664 a_amv_var_copy(char const *str){
665 char *news;
666 size_t len;
667 NYD2_ENTER;
669 if(*str == '\0')
670 news = UNCONST("");
671 else{
672 len = strlen(str) +1;
673 news = smalloc(len);
674 memcpy(news, str, len);
676 NYD2_LEAVE;
677 return news;
680 static void
681 a_amv_var_free(char *cp){
682 NYD2_ENTER;
683 if(*cp != '\0')
684 free(cp);
685 NYD2_LEAVE;
688 static bool_t
689 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
690 int flag;
691 bool_t ok;
692 NYD2_ENTER;
694 ok = TRU1;
695 flag = 0;
697 switch(okey){
698 case ok_b_debug:
699 flag = OPT_DEBUG;
700 break;
701 case ok_v_HOME:
702 /* Invalidate any resolved folder then, too
703 * FALLTHRU */
704 case ok_v_folder:
705 ok = !(pstate & PS_ROOT);
706 pstate |= PS_ROOT;
707 ok_vclear(_folder_resolved);
708 if(ok)
709 pstate &= ~PS_ROOT;
710 ok = TRU1;
711 break;
712 case ok_b_header:
713 flag = OPT_N_FLAG;
714 enable = !enable;
715 break;
716 case ok_b_memdebug:
717 flag = OPT_MEMDEBUG;
718 break;
719 case ok_b_skipemptybody:
720 flag = OPT_E_FLAG;
721 break;
722 case ok_v_TMPDIR:
723 if(enable) /* DEFVAL will soon ensure a value otherwise! */
724 tempdir = *val; /* XXX replace users with ok_vlook(TMPDIR) */
725 break;
726 case ok_v_umask:
727 assert(enable);
728 if(**val != '\0'){
729 ul_i ul;
731 if((ul = strtoul(*val, NULL, 0)) & ~0777){ /* (is valid _VF_POSNUM) */
732 n_err(_("Invalid *umask* setting: %s\n"), *val);
733 ok = FAL0;
734 }else
735 umask((mode_t)ul);
737 break;
738 case ok_b_verbose:
739 flag = (enable && !(options & OPT_VERB))
740 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
741 break;
742 default:
743 DBG( n_err("Implementation error: never heard of %u\n", ok); )
744 break;
747 if(flag){
748 if(enable)
749 options |= flag;
750 else
751 options &= ~flag;
753 NYD2_LEAVE;
754 return ok;
757 static bool_t
758 a_amv_var_check_nocntrls(char const *val){
759 char c;
760 NYD2_ENTER;
762 while((c = *val++) != '\0')
763 if(cntrlchar(c))
764 break;
765 NYD2_LEAVE;
766 return (c == '\0');
769 static bool_t
770 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
771 /* TODO The internal/environment variables which are num= or posnum= should
772 * TODO gain special lookup functions, or the return should be void* and
773 * TODO castable to integer; i.e. no more strtoX() should be needed.
774 * TODO I.e., the result of this function should instead be stored.
775 * TODO Use intmax_t IF that is sizeof(void*) only? */
776 bool_t rv;
777 NYD2_ENTER;
779 rv = TRU1;
781 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
782 char *eptr;
783 union {long s; unsigned long u;} i;
785 if(!pos){
786 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
788 if(*eptr != '\0' ||
789 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
790 rv = FAL0;
791 #if INT_MIN != LONG_MIN
792 else if(i.s < INT_MIN)
793 rv = FAL0;
794 #endif
795 #if INT_MAX != LONG_MAX
796 else if(i.s > INT_MAX)
797 rv = FAL0;
798 #endif
799 }else{
800 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
802 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
803 rv = FAL0;
804 #if UINT_MAX != ULONG_MAX
805 else if(i.u > UINT_MAX)
806 rv = FAL0;
807 #endif
810 NYD2_LEAVE;
811 return rv;
814 static char const *
815 a_amv_var_canonify(char const *vn){
816 NYD2_ENTER;
817 if(!upperchar(*vn)){
818 char const *vp;
820 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
822 vn = (*vp == '@') ? i_strdup(vn) : vn;
824 NYD2_LEAVE;
825 return vn;
828 static bool_t
829 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
830 ui32_t hash, i, j;
831 struct a_amv_var_map const *avmp;
832 NYD2_ENTER;
834 avcp->avc_name = name = a_amv_var_canonify(name);
835 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
837 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
838 ui32_t x = a_amv_var_revmap[i];
840 if(x == a_AMV_VAR_REV_ILL)
841 break;
843 avmp = &a_amv_var_map[x];
844 if(avmp->avm_hash == hash &&
845 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
846 avcp->avc_map = avmp;
847 avcp->avc_okey = (enum okeys)x;
848 goto jleave;
851 if(++i == a_AMV_VAR_REV_PRIME){
852 #ifdef a_AMV_VAR_REV_WRAPAROUND
853 i = 0;
854 #else
855 break;
856 #endif
859 avcp->avc_map = NULL;
860 avcp = NULL;
861 jleave:
862 NYD2_LEAVE;
863 return (avcp != NULL);
866 static bool_t
867 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
868 size_t i;
869 char const *cp;
870 struct a_amv_var_map const *avmp;
871 struct a_amv_var *avp;
872 NYD2_ENTER;
874 /* C99 */{
875 struct a_amv_var **avpp, *lavp;
877 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
879 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
880 if(!strcmp(avp->av_name, avcp->avc_name)){
881 /* Relink as head, hope it "sorts on usage" over time.
882 * _clear() relies on this behaviour */
883 if(lavp != NULL){
884 lavp->av_link = avp->av_link;
885 avp->av_link = *avpp;
886 *avpp = avp;
888 goto jleave;
892 /* If this is not an assembled variable we need to consider some special
893 * initialization cases and eventually create the variable anew */
894 if(LIKELY((avmp = avcp->avc_map) != NULL)){
895 /* Does it have an import-from-environment flag? */
896 if(UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
897 if(LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
898 /* May be better not to use that one, though? */
899 bool_t isempty, isbltin;
901 isempty = (*cp == '\0' &&
902 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
903 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
904 ) != 0);
906 if(UNLIKELY(isempty)){
907 if(!isbltin)
908 goto jerr;
909 }else if(LIKELY(*cp != '\0')){
910 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
911 !a_amv_var_check_nocntrls(cp))){
912 n_err(_("Ignoring environment, control characters "
913 "invalid in variable: %s\n"), avcp->avc_name);
914 goto jerr;
916 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
917 !a_amv_var_check_num(cp, FAL0))){
918 n_err(_("Environment variable value not a number "
919 "or out of range: %s\n"), avcp->avc_name);
920 goto jerr;
922 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
923 !a_amv_var_check_num(cp, TRU1))){
924 n_err(_("Environment variable value not a number, "
925 "negative or out of range: %s\n"), avcp->avc_name);
926 goto jerr;
929 goto jnewval;
933 /* A first-time init switch is to be handled now and here */
934 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
935 static struct a_amv_var_defval const **arr,
936 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
938 if(arr == NULL){
939 arr = &arr_base[0];
940 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
941 while(i-- > 0)
942 arr[i] = &a_amv_var_i3vals[i];
945 for(i = 0; arr[i] != NULL; ++i)
946 if(arr[i]->avdv_okey == avcp->avc_okey){
947 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
948 : arr[i]->avdv_value;
949 /* Remove this entry, hope entire block becomes no-op asap */
951 arr[i] = arr[i + 1];
952 while(arr[i++] != NULL);
954 if(!i3val_nonew)
955 goto jnewval;
956 if(i3val_nonew == TRUM1)
957 avp = (struct a_amv_var*)-1;
958 goto jleave;
962 /* The virtual variables */
963 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
964 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
965 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
966 avp = UNCONST(a_amv_var_virts[i].avv_var);
967 goto jleave;
969 /* Not reached */
972 /* Place this last because once it is set first the variable will never
973 * be removed again and thus match in the first block above */
974 if(UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
975 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
976 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
977 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
978 : a_amv_var_defvals[i].avdv_value;
979 goto jnewval;
984 jerr:
985 avp = NULL;
986 jleave:
987 avcp->avc_var = avp;
988 NYD2_LEAVE;
989 return (avp != NULL);
991 jnewval: /* C99 */{
992 struct a_amv_var **avpp;
993 size_t l;
995 l = strlen(avcp->avc_name) +1;
996 avcp->avc_var = avp = smalloc(sizeof(*avp) -
997 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
998 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
999 *avpp = avp;
1000 memcpy(avp->av_name, avcp->avc_name, l);
1001 avp->av_value = a_amv_var_copy(cp);
1002 #ifdef HAVE_PUTENV
1003 avp->av_env = NULL;
1004 #endif
1005 avp->av_flags = avmp->avm_flags;
1007 if(avp->av_flags & a_AMV_VF_VIP)
1008 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1009 if(avp->av_flags & a_AMV_VF_ENV)
1010 a_amv_var__putenv(avcp, avp);
1011 goto jleave;
1015 static bool_t
1016 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1017 bool_t force_env){
1018 struct a_amv_var *avp;
1019 char *oval;
1020 struct a_amv_var_map const *avmp;
1021 bool_t rv;
1022 NYD2_ENTER;
1024 if(value == NULL){
1025 rv = a_amv_var_clear(avcp, force_env);
1026 goto jleave;
1029 if((avmp = avcp->avc_map) != NULL){
1030 rv = FAL0;
1032 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1033 !(pstate & PS_ROOT))){
1034 n_err(_("Variable is readonly: %s\n"), avcp->avc_name);
1035 goto jleave;
1037 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1038 n_err(_("Variable must not be empty: %s\n"), avcp->avc_name);
1039 goto jleave;
1041 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1042 !a_amv_var_check_nocntrls(value))){
1043 n_err(_("Variable forbids control characters: %s\n"),
1044 avcp->avc_name);
1045 goto jleave;
1047 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1048 !a_amv_var_check_num(value, FAL0))){
1049 n_err(_("Variable value not a number or out of range: %s\n"),
1050 avcp->avc_name);
1051 goto jleave;
1053 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1054 !a_amv_var_check_num(value, TRU1))){
1055 n_err(_("Variable value not a number, negative or out of range: %s\n"),
1056 avcp->avc_name);
1057 goto jleave;
1059 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1060 !(pstate & (PS_ROOT | PS_STARTED)))){
1061 n_err(_("Variable cannot be set in a resource file: %s\n"),
1062 avcp->avc_name);
1063 goto jleave;
1067 rv = TRU1;
1068 a_amv_var_lookup(avcp, TRU1);
1070 /* Don't care what happens later on, store this in the unroll list */
1071 if(a_amv_lopts != NULL)
1072 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1074 if((avp = avcp->avc_var) == NULL){
1075 struct a_amv_var **avpp;
1076 size_t l = strlen(avcp->avc_name) +1;
1078 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1079 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1080 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1081 *avpp = avp;
1082 #ifdef HAVE_PUTENV
1083 avp->av_env = NULL;
1084 #endif
1085 memcpy(avp->av_name, avcp->avc_name, l);
1086 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1087 oval = UNCONST("");
1088 }else
1089 oval = avp->av_value;
1091 if(avmp == NULL)
1092 avp->av_value = a_amv_var_copy(value);
1093 else{
1094 /* Via `set' etc. the user may give even boolean options non-boolean
1095 * values, ignore that and force boolean */
1096 if(avp->av_flags & a_AMV_VF_BOOL){
1097 if((options & OPT_D_VV) && *value != '\0')
1098 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1099 avcp->avc_name, value);
1100 avp->av_value = UNCONST(value = "");
1101 }else
1102 avp->av_value = a_amv_var_copy(value);
1104 /* Check if update allowed XXX wasteful on error! */
1105 if((avp->av_flags & a_AMV_VF_VIP) &&
1106 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1107 char *cp = avp->av_value;
1109 avp->av_value = oval;
1110 oval = cp;
1114 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1115 avp->av_flags |= a_AMV_VF_LINKED;
1116 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1117 rv = a_amv_var__putenv(avcp, avp);
1119 a_amv_var_free(oval);
1120 jleave:
1121 NYD2_LEAVE;
1122 return rv;
1125 static bool_t
1126 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1127 #ifndef HAVE_SETENV
1128 char *cp;
1129 #endif
1130 bool_t rv;
1131 NYD2_ENTER;
1133 #ifdef HAVE_SETENV
1134 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1135 #else
1136 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1138 if((rv = (putenv(cp) == 0))){
1139 char *ocp;
1141 if((ocp = avp->av_env) != NULL)
1142 free(ocp);
1143 avp->av_env = cp;
1144 }else
1145 free(cp);
1146 #endif
1147 NYD2_LEAVE;
1148 return rv;
1151 static bool_t
1152 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1153 struct a_amv_var **avpp, *avp;
1154 struct a_amv_var_map const *avmp;
1155 bool_t rv;
1156 NYD2_ENTER;
1158 rv = FAL0;
1160 if(LIKELY((avmp = avcp->avc_map) != NULL)){
1161 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1162 !(pstate & PS_ROOT))){
1163 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1164 goto jleave;
1166 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1167 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1168 goto jleave;
1171 rv = TRU1;
1173 if(UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1174 if(force_env){
1175 jforce_env:
1176 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1177 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1178 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1179 goto jleave;
1180 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1181 avcp->avc_var = NULL;
1182 if(force_env)
1183 goto jforce_env;
1184 goto jleave;
1187 if(a_amv_lopts != NULL)
1188 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1190 avp = avcp->avc_var;
1191 avcp->avc_var = NULL;
1192 avpp = &a_amv_vars[avcp->avc_prime];
1193 assert(*avpp == avp); /* (always listhead after lookup()) */
1194 *avpp = (*avpp)->av_link;
1196 /* C99 */{
1197 #ifdef HAVE_SETENV
1198 char *envval = NULL;
1199 #else
1200 char *envval = avp->av_env;
1201 #endif
1202 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1203 rv = a_amv_var__clearenv(avp->av_name, envval);
1205 a_amv_var_free(avp->av_value);
1206 free(avp);
1208 /* XXX Fun part, extremely simple-minded for now: if this variable has
1209 * XXX a default value, immediately reinstantiate it! */
1210 if(UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1211 a_amv_var_lookup(avcp, TRU1);
1212 jleave:
1213 NYD2_LEAVE;
1214 return rv;
1217 static bool_t
1218 a_amv_var__clearenv(char const *name, char *value){
1219 #ifndef HAVE_SETENV
1220 extern char **environ;
1221 char **ecpp;
1222 #endif
1223 bool_t rv;
1224 NYD2_ENTER;
1225 UNUSED(value);
1227 #ifdef HAVE_SETENV
1228 unsetenv(name);
1229 rv = TRU1;
1230 #else
1231 if(value != NULL)
1232 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1233 if(*ecpp == value){
1234 free(value);
1236 ecpp[0] = ecpp[1];
1237 while(*ecpp++ != NULL);
1238 break;
1240 rv = TRU1;
1241 #endif
1242 NYD2_LEAVE;
1243 return rv;
1246 static void
1247 a_amv_var_show_all(void){
1248 struct n_string msg, *msgp;
1249 FILE *fp;
1250 size_t no, i;
1251 struct a_amv_var *avp;
1252 char const **vacp, **cap;
1253 NYD2_ENTER;
1255 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1256 n_perr(_("Can't create temporary file for `set' listing"), 0);
1257 goto jleave;
1260 /* We need to instantiate first-time-inits and default values here, so that
1261 * they will be regular members of our _vars[] table */
1262 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1263 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1264 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1265 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1267 for(no = i = 0; i < a_AMV_PRIME; ++i)
1268 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1269 ++no;
1270 no += a_AMV_VAR_VIRTS_CNT;
1272 vacp = salloc(no * sizeof(*vacp));
1274 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1275 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1276 *cap++ = avp->av_name;
1277 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1278 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1280 if(no > 1)
1281 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1283 msgp = &msg;
1284 msgp = n_string_reserve(n_string_creat(msgp), 80);
1285 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1286 i += a_amv_var_show(*cap, fp, msgp);
1287 n_string_gut(&msg);
1289 page_or_print(fp, i);
1290 Fclose(fp);
1291 jleave:
1292 NYD2_LEAVE;
1295 static int
1296 a_amv_var__show_cmp(void const *s1, void const *s2){
1297 int rv;
1298 NYD2_ENTER;
1300 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
1301 NYD2_LEAVE;
1302 return rv;
1305 static size_t
1306 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1307 struct a_amv_var_carrier avc;
1308 char const *quote;
1309 size_t i;
1310 NYD2_ENTER;
1312 msgp = n_string_trunc(msgp, 0);
1313 i = 0;
1315 a_amv_var_revlookup(&avc, name);
1316 if(!a_amv_var_lookup(&avc, FAL0)){
1317 struct str s;
1319 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1320 s.s = UNCONST(name);
1321 s.l = UIZ_MAX;
1322 msgp = n_shell_quote(msgp, &s, FAL0);
1323 goto jleave;
1326 if(options & OPT_D_V){
1327 if(avc.avc_map == NULL){
1328 msgp = n_string_push_c(msgp, '#');
1329 msgp = n_string_push_cp(msgp, "assembled");
1330 i = 1;
1332 /* C99 */{
1333 struct{
1334 ui16_t flags;
1335 char msg[22];
1336 } const tbase[] = {
1337 {a_AMV_VF_VIRT, "virtual"},
1338 {a_AMV_VF_RDONLY, "readonly"},
1339 {a_AMV_VF_NODEL, "nodelete"},
1340 {a_AMV_VF_NOTEMPTY, "notempty"},
1341 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1342 {a_AMV_VF_NUM, "number"},
1343 {a_AMV_VF_POSNUM, "positive-number"},
1344 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1345 {a_AMV_VF_ENV, "sync-environ"},
1346 {a_AMV_VF_I3VAL, "initial-value"},
1347 {a_AMV_VF_DEFVAL, "default-value"},
1348 {a_AMV_VF_LINKED, "`environ' link"}
1349 }, *tp;
1351 for(tp = tbase; PTRCMP(tp, <, &tbase[NELEM(tbase)]); ++tp)
1352 if(avc.avc_var->av_flags & tp->flags){
1353 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1354 msgp = n_string_push_cp(msgp, tp->msg);
1358 if(i > 0)
1359 msgp = n_string_push_cp(msgp, "\n ");
1362 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1363 msgp = n_string_push_cp(msgp, "# ");
1364 UNINIT(quote, NULL);
1365 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1366 quote = n_shell_quote_cp(avc.avc_var->av_value, TRU1);
1367 if(strcmp(quote, avc.avc_var->av_value))
1368 msgp = n_string_push_cp(msgp, "wysh ");
1370 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1371 msgp = n_string_push_cp(msgp, "environ ");
1372 msgp = n_string_push_cp(msgp, "set ");
1373 msgp = n_string_push_cp(msgp, name);
1374 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1375 msgp = n_string_push_c(msgp, '=');
1376 msgp = n_string_push_cp(msgp, quote);
1379 jleave:
1380 msgp = n_string_push_c(msgp, '\n');
1381 fputs(n_string_cp(msgp), fp);
1382 NYD2_ENTER;
1383 return (i > 0 ? 2 : 1);
1386 static bool_t
1387 a_amv_var_c_set(char **ap, bool_t issetenv){
1388 char *cp, *cp2, *varbuf, c;
1389 size_t errs;
1390 NYD2_ENTER;
1392 errs = 0;
1393 jouter:
1394 while((cp = *ap++) != NULL){
1395 /* Isolate key */
1396 cp2 = varbuf = salloc(strlen(cp) +1);
1398 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1399 if(cntrlchar(c) || whitechar(c)){
1400 n_err(_("Variable name with control character ignored: %s\n"),
1401 ap[-1]);
1402 ++errs;
1403 goto jouter;
1405 *cp2++ = c;
1407 *cp2 = '\0';
1408 if(c == '\0')
1409 cp = UNCONST("");
1410 else
1411 ++cp;
1413 if(varbuf == cp2){
1414 n_err(_("Empty variable name ignored\n"));
1415 ++errs;
1416 }else{
1417 struct a_amv_var_carrier avc;
1418 bool_t isunset;
1420 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1421 varbuf = &varbuf[2];
1423 a_amv_var_revlookup(&avc, varbuf);
1425 if(isunset)
1426 errs += !a_amv_var_clear(&avc, issetenv);
1427 else
1428 errs += !a_amv_var_set(&avc, cp, issetenv);
1431 NYD2_LEAVE;
1432 return (errs == 0);
1435 FL int
1436 c_define(void *v){
1437 int rv;
1438 char **args;
1439 NYD_ENTER;
1441 rv = 1;
1443 if((args = v)[0] == NULL){
1444 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1445 goto jleave;
1448 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1449 args[2] != NULL){
1450 n_err(_("Synopsis: define: <name> {\n"));
1451 goto jleave;
1454 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1455 jleave:
1456 NYD_LEAVE;
1457 return rv;
1460 FL int
1461 c_undefine(void *v){
1462 int rv;
1463 char **args;
1464 NYD_ENTER;
1466 rv = 0;
1467 args = v;
1469 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1470 while(*++args != NULL);
1471 NYD_LEAVE;
1472 return rv;
1475 FL int
1476 c_call(void *v){
1477 struct a_amv_mac_call_args amca;
1478 char const *errs, *name;
1479 struct a_amv_mac *amp;
1480 char **args;
1481 int rv;
1482 NYD_ENTER;
1484 rv = 1;
1486 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1487 errs = _("Synopsis: call: <%s>\n");
1488 name = "name";
1489 goto jerr;
1492 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1493 errs = _("Undefined macro `call'ed: %s\n");
1494 goto jerr;
1497 memset(&amca, 0, sizeof amca);
1498 amca.amca_name = name;
1499 amca.amca_amp = amp;
1500 rv = (a_amv_mac_exec(&amca) == FAL0);
1501 jleave:
1502 NYD_LEAVE;
1503 return rv;
1504 jerr:
1505 n_err(errs, name);
1506 goto jleave;
1509 FL bool_t
1510 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1511 struct a_amv_mac_call_args amca;
1512 struct a_amv_mac *amp;
1513 size_t len;
1514 char *var, *cp;
1515 bool_t rv;
1516 NYD_ENTER;
1518 rv = TRU1;
1519 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1521 /* First try the fully resolved path */
1522 snprintf(var, len, "folder-hook-%s", mailname);
1523 if((cp = vok_vlook(var)) != NULL)
1524 goto jmac;
1526 /* If we are under *folder*, try the usual +NAME syntax, too */
1527 if(displayname[0] == '+'){
1528 char *x = mailname + len;
1530 for(; x > mailname; --x)
1531 if(x[-1] == '/'){
1532 snprintf(var, len, "folder-hook-+%s", x);
1533 if((cp = vok_vlook(var)) != NULL)
1534 goto jmac;
1535 break;
1539 /* Plain *folder-hook* is our last try */
1540 if((cp = ok_vlook(folder_hook)) == NULL)
1541 goto jleave;
1543 jmac:
1544 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1545 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1546 n_shell_quote_cp(displayname, FAL0), cp);
1547 rv = FAL0;
1548 goto jleave;
1551 memset(&amca, 0, sizeof amca);
1552 amca.amca_name = cp;
1553 amca.amca_amp = amp;
1554 pstate &= ~PS_HOOK_MASK;
1555 if(nmail){
1556 amca.amca_unroller = NULL;
1557 pstate |= PS_HOOK_NEWMAIL;
1558 }else{
1559 amca.amca_unroller = &a_amv_folder_hook_lopts;
1560 pstate |= PS_HOOK;
1562 amca.amca_lopts_on = TRU1;
1563 rv = a_amv_mac_exec(&amca);
1564 pstate &= ~PS_HOOK_MASK;
1566 jleave:
1567 NYD_LEAVE;
1568 return rv;
1571 FL void
1572 call_compose_mode_hook(char const *macname, /* TODO temporary, v15: drop */
1573 void (*hook_pre)(void *), void *hook_arg){
1574 struct a_amv_mac_call_args amca;
1575 struct a_amv_mac *amp;
1576 NYD_ENTER;
1578 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1579 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1580 macname);
1581 else{
1582 memset(&amca, 0, sizeof amca);
1583 amca.amca_name = macname;
1584 amca.amca_amp = amp;
1585 amca.amca_unroller = &a_amv_compose_lopts;
1586 amca.amca_hook_pre = hook_pre;
1587 amca.amca_hook_arg = hook_arg;
1588 amca.amca_lopts_on = TRU1;
1589 pstate &= ~PS_HOOK_MASK;
1590 pstate |= PS_HOOK;
1591 a_amv_mac_exec(&amca);
1592 pstate &= ~PS_HOOK_MASK;
1594 NYD_LEAVE;
1597 FL int
1598 c_account(void *v){
1599 struct a_amv_mac_call_args amca;
1600 struct a_amv_mac *amp;
1601 char **args;
1602 int rv, i, oqf, nqf;
1603 NYD_ENTER;
1605 rv = 1;
1607 if((args = v)[0] == NULL){
1608 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1609 goto jleave;
1612 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1613 if(args[2] != NULL){
1614 n_err(_("Synopsis: account: <name> {\n"));
1615 goto jleave;
1617 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1618 n_err(_("`account': cannot use reserved name: %s\n"),
1619 ACCOUNT_NULL);
1620 goto jleave;
1622 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1623 goto jleave;
1626 if(pstate & PS_HOOK_MASK){
1627 n_err(_("`account': can't change account from within a hook\n"));
1628 goto jleave;
1631 save_mbox_for_possible_quitstuff();
1633 amp = NULL;
1634 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1635 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1636 n_err(_("`account': account does not exist: %s\n"), args[0]);
1637 goto jleave;
1640 oqf = savequitflags();
1642 if(a_amv_acc_curr != NULL){
1643 if(a_amv_acc_curr->am_lopts != NULL)
1644 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1645 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1646 a_amv_mac_free(a_amv_acc_curr);
1649 account_name = (amp != NULL) ? amp->am_name : NULL;
1650 a_amv_acc_curr = amp;
1652 if(amp != NULL){
1653 assert(amp->am_lopts == NULL);
1654 memset(&amca, 0, sizeof amca);
1655 amca.amca_name = amp->am_name;
1656 amca.amca_amp = amp;
1657 amca.amca_unroller = &amp->am_lopts;
1658 amca.amca_lopts_on = TRU1;
1659 if(!a_amv_mac_exec(&amca)){
1660 /* XXX account switch incomplete, unroll? */
1661 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1662 goto jleave;
1666 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1667 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1668 restorequitflags(oqf);
1669 if((i = setfile("%", 0)) < 0)
1670 goto jleave;
1671 check_folder_hook(FAL0);
1672 if(i > 0 && !ok_blook(emptystart))
1673 goto jleave;
1674 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1675 restorequitflags(nqf);
1677 rv = 0;
1678 jleave:
1679 NYD_LEAVE;
1680 return rv;
1683 FL int
1684 c_unaccount(void *v){
1685 int rv;
1686 char **args;
1687 NYD_ENTER;
1689 rv = 0;
1690 args = v;
1692 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1693 while(*++args != NULL);
1694 NYD_LEAVE;
1695 return rv;
1698 FL int
1699 c_localopts(void *v){
1700 char **argv;
1701 int rv;
1702 NYD_ENTER;
1704 rv = 1;
1706 if(a_amv_lopts == NULL){
1707 n_err(_("Cannot use `localopts' but from within a "
1708 "`define' or `account'\n"));
1709 goto jleave;
1712 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1713 rv = 0;
1714 jleave:
1715 NYD_LEAVE;
1716 return rv;
1719 FL void
1720 temporary_localopts_free(void){ /* XXX intermediate hack */
1721 NYD_ENTER;
1722 if(a_amv_compose_lopts != NULL){
1723 void *save = a_amv_lopts;
1725 a_amv_lopts = NULL;
1726 a_amv_lopts_unroll(&a_amv_compose_lopts);
1727 a_amv_compose_lopts = NULL;
1728 a_amv_lopts = save;
1730 NYD_LEAVE;
1733 FL void
1734 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1735 NYD_ENTER;
1736 if(a_amv_folder_hook_lopts != NULL){
1737 void *save = a_amv_lopts;
1739 a_amv_lopts = NULL;
1740 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1741 a_amv_folder_hook_lopts = NULL;
1742 a_amv_lopts = save;
1744 NYD_LEAVE;
1747 FL char *
1748 n_var_oklook(enum okeys okey){
1749 struct a_amv_var_carrier avc;
1750 char *rv;
1751 struct a_amv_var_map const *avmp;
1752 NYD_ENTER;
1754 avc.avc_map = avmp = &a_amv_var_map[okey];
1755 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1756 avc.avc_hash = avmp->avm_hash;
1757 avc.avc_okey = okey;
1759 if(a_amv_var_lookup(&avc, FAL0))
1760 rv = avc.avc_var->av_value;
1761 else
1762 rv = NULL;
1763 NYD_LEAVE;
1764 return rv;
1767 FL bool_t
1768 n_var_okset(enum okeys okey, uintptr_t val){
1769 struct a_amv_var_carrier avc;
1770 bool_t ok;
1771 struct a_amv_var_map const *avmp;
1772 NYD_ENTER;
1774 avc.avc_map = avmp = &a_amv_var_map[okey];
1775 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1776 avc.avc_hash = avmp->avm_hash;
1777 avc.avc_okey = okey;
1779 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1780 NYD_LEAVE;
1781 return ok;
1784 FL bool_t
1785 n_var_okclear(enum okeys okey){
1786 struct a_amv_var_carrier avc;
1787 bool_t rv;
1788 struct a_amv_var_map const *avmp;
1789 NYD_ENTER;
1791 avc.avc_map = avmp = &a_amv_var_map[okey];
1792 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1793 avc.avc_hash = avmp->avm_hash;
1794 avc.avc_okey = okey;
1796 rv = a_amv_var_clear(&avc, FAL0);
1797 NYD_LEAVE;
1798 return rv;
1801 FL char *
1802 n_var_voklook(char const *vokey){
1803 struct a_amv_var_carrier avc;
1804 char *rv;
1805 NYD_ENTER;
1807 a_amv_var_revlookup(&avc, vokey);
1809 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1810 NYD_LEAVE;
1811 return rv;
1814 FL bool_t
1815 n_var_vokset(char const *vokey, uintptr_t val){
1816 struct a_amv_var_carrier avc;
1817 bool_t ok;
1818 NYD_ENTER;
1820 a_amv_var_revlookup(&avc, vokey);
1822 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1823 NYD_LEAVE;
1824 return ok;
1827 FL bool_t
1828 n_var_vokclear(char const *vokey){
1829 struct a_amv_var_carrier avc;
1830 bool_t ok;
1831 NYD_ENTER;
1833 a_amv_var_revlookup(&avc, vokey);
1835 ok = a_amv_var_clear(&avc, FAL0);
1836 NYD_LEAVE;
1837 return ok;
1840 #ifdef HAVE_SOCKETS
1841 FL char *
1842 n_var_xoklook(enum okeys okey, struct url const *urlp,
1843 enum okey_xlook_mode oxm){
1844 struct a_amv_var_carrier avc;
1845 struct str const *us;
1846 size_t nlen;
1847 char *nbuf, *rv;
1848 struct a_amv_var_map const *avmp;
1849 NYD_ENTER;
1851 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1853 /* For simplicity: allow this case too */
1854 if(!(oxm & (OXM_H_P | OXM_U_H_P)))
1855 goto jplain;
1857 avc.avc_map = avmp = &a_amv_var_map[okey];
1858 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1859 avc.avc_okey = okey;
1861 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1862 nlen = strlen(avc.avc_name);
1863 nbuf = salloc(nlen + 1 + us->l +1);
1864 memcpy(nbuf, avc.avc_name, nlen);
1865 nbuf[nlen++] = '-';
1867 /* One of .url_u_h_p and .url_h_p we test in here */
1868 memcpy(nbuf + nlen, us->s, us->l +1);
1869 avc.avc_name = a_amv_var_canonify(nbuf);
1870 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1871 if(a_amv_var_lookup(&avc, FAL0))
1872 goto jvar;
1874 /* The second */
1875 if(oxm & OXM_H_P){
1876 us = &urlp->url_h_p;
1877 memcpy(nbuf + nlen, us->s, us->l +1);
1878 avc.avc_name = a_amv_var_canonify(nbuf);
1879 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1880 if(a_amv_var_lookup(&avc, FAL0)){
1881 jvar:
1882 rv = avc.avc_var->av_value;
1883 goto jleave;
1887 jplain:
1888 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1889 jleave:
1890 NYD_LEAVE;
1891 return rv;
1893 #endif /* HAVE_SOCKETS */
1895 FL int
1896 c_set(void *v){
1897 char **ap;
1898 int err;
1899 NYD_ENTER;
1901 if(*(ap = v) == NULL){
1902 a_amv_var_show_all();
1903 err = 0;
1904 }else
1905 err = !a_amv_var_c_set(ap, FAL0);
1906 NYD_LEAVE;
1907 return err;
1910 FL int
1911 c_unset(void *v){
1912 char **ap;
1913 int err;
1914 NYD_ENTER;
1916 for(err = 0, ap = v; *ap != NULL; ++ap)
1917 err |= !n_var_vokclear(*ap);
1918 NYD_LEAVE;
1919 return err;
1922 FL int
1923 c_varshow(void *v){
1924 char **ap;
1925 NYD_ENTER;
1927 if(*(ap = v) == NULL)
1928 v = NULL;
1929 else{
1930 struct n_string msg, *msgp = &msg;
1932 msgp = n_string_creat(msgp);
1933 for(; *ap != NULL; ++ap)
1934 a_amv_var_show(*ap, stdout, msgp);
1935 n_string_gut(msgp);
1937 NYD_LEAVE;
1938 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1941 FL int
1942 c_varedit(void *v){
1943 struct a_amv_var_carrier avc;
1944 FILE *of, *nf;
1945 char *val, **argv;
1946 int err;
1947 sighandler_type sigint;
1948 NYD_ENTER;
1950 sigint = safe_signal(SIGINT, SIG_IGN);
1952 for(err = 0, argv = v; *argv != NULL; ++argv){
1953 memset(&avc, 0, sizeof avc);
1955 a_amv_var_revlookup(&avc, *argv);
1957 if(avc.avc_map != NULL){
1958 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1959 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
1960 avc.avc_name);
1961 continue;
1963 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
1964 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
1965 avc.avc_name);
1966 continue;
1970 a_amv_var_lookup(&avc, FAL0);
1972 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1973 NULL){
1974 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1975 err = 1;
1976 break;
1977 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
1978 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
1979 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1980 Fclose(of);
1981 err = 1;
1982 continue;
1985 fflush_rewind(of);
1986 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1987 Fclose(of);
1989 if(nf != NULL){
1990 int c;
1991 char *base;
1992 off_t l = fsize(nf);
1994 assert(l >= 0);
1995 base = salloc((size_t)l +1);
1997 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
1998 if(c == '\n' || c == '\r'){
1999 *val = ' ';
2000 ++l;
2001 }else{
2002 *val = (char)(uc_i)c;
2003 l = 0;
2005 val -= l;
2006 *val = '\0';
2008 if(!a_amv_var_set(&avc, base, FAL0))
2009 err = 1;
2011 Fclose(nf);
2012 }else{
2013 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2014 err = 1;
2015 break;
2019 safe_signal(SIGINT, sigint);
2020 NYD_LEAVE;
2021 return err;
2024 FL int
2025 c_environ(void *v){
2026 struct a_amv_var_carrier avc;
2027 int err;
2028 char **ap;
2029 bool_t islnk;
2030 NYD_ENTER;
2032 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2033 for(err = 0; *++ap != NULL;){
2034 a_amv_var_revlookup(&avc, *ap);
2036 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2037 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2038 if(!islnk){
2039 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2040 continue;
2041 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2042 if(options & OPT_D_V)
2043 n_err(_("`environ': link: already established: %s\n"), *ap);
2044 continue;
2046 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2047 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2048 a_amv_var__putenv(&avc, avc.avc_var);
2049 }else if(!islnk){
2050 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2051 err = 1;
2052 }else{
2053 char const *evp = getenv(*ap);
2055 if(evp != NULL)
2056 err |= !a_amv_var_set(&avc, evp, TRU1);
2057 else{
2058 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2059 *ap);
2060 err = 1;
2064 }else if(!strcmp(*ap, "set"))
2065 err = !a_amv_var_c_set(++ap, TRU1);
2066 else if(!strcmp(*ap, "unset")){
2067 for(err = 0; *++ap != NULL;){
2068 a_amv_var_revlookup(&avc, *ap);
2070 if(!a_amv_var_clear(&avc, TRU1))
2071 err = 1;
2073 }else{
2074 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2075 err = 1;
2077 NYD_LEAVE;
2078 return err;
2081 /* s-it-mode */