`list': include argument-type etc. if *verbose*
[s-mailx.git] / accmacvar.c
blobc5f9a12d869088a5db5783f26625cef17dbad409
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 wether 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(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("", TRU1, &line.s, &line.l, NULL);
453 if(n.ui == 0)
454 continue;
455 if(n.i < 0){
456 n_err(_("Unterminated %s definition: \"%s\"\n"),
457 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
458 goto jerr;
460 if(a_amv_mac_is_closing_angle(line.s))
461 break;
463 /* Trim WS, remember amount of leading spaces for display purposes */
464 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
465 if(*cp == '\t')
466 leaspc = (leaspc + 8) & ~7;
467 else if(*cp == ' ')
468 ++leaspc;
469 else
470 break;
471 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
473 if(n.ui == 0)
474 continue;
476 maxlen = MAX(maxlen, n.ui);
477 cp[n.ui++] = '\0';
479 if(LIKELY(++line_cnt < UI32_MAX)){
480 struct a_amv_mac_line *amlp;
482 llp = salloc(sizeof *llp);
483 if(ll_head == NULL)
484 ll_head = llp;
485 else
486 ll_tail->ll_next = llp;
487 ll_tail = llp;
488 llp->ll_next = NULL;
489 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
490 VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
491 amlp->aml_len = n.ui -1;
492 amlp->aml_prespc = leaspc;
493 memcpy(amlp->aml_dat, cp, n.ui);
494 }else{
495 n_err(_("Too much content in %s definition: \"%s\"\n"),
496 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
497 goto jerr;
501 /* Create the new macro */
502 n.s = strlen(name) +1;
503 amp = smalloc(sizeof(*amp) - VFIELD_SIZEOF(struct a_amv_mac, am_name) + n.s);
504 amp->am_next = NULL;
505 amp->am_maxlen = maxlen;
506 amp->am_line_cnt = line_cnt;
507 amp->am_flags = amf;
508 amp->am_lopts = NULL;
509 memcpy(amp->am_name, name, n.s);
510 /* C99 */{
511 struct a_amv_mac_line **amlpp;
513 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
514 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
515 *amlpp++ = llp->ll_amlp;
516 *amlpp = NULL;
519 /* Finally check wether such a macro already exists, in which case we throw
520 * it all away again. At least we know it would have worked */
521 if(a_amv_mac_lookup(name, amp, amf) != NULL){
522 n_err(_("A %s named \"%s\" already exists\n"),
523 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
524 goto jerr;
527 rv = TRU1;
528 jleave:
529 if(line.s != NULL)
530 free(line.s);
531 NYD2_LEAVE;
532 return rv;
534 jerr:
535 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
536 free(llp->ll_amlp);
537 if(amp != NULL){
538 free(amp->am_line_dat);
539 free(amp);
541 goto jleave;
544 static bool_t
545 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
546 struct a_amv_mac *amp;
547 bool_t rv;
548 NYD2_ENTER;
550 rv = TRU1;
552 if(LIKELY(name[0] != '*' || name[1] != '\0')){
553 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
554 n_err(_("%s \"%s\" is not defined\n"),
555 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
556 rv = FAL0;
558 }else{
559 struct a_amv_mac **ampp, *lamp;
561 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[NELEM(a_amv_macs)]);
562 ++ampp)
563 for(lamp = NULL, amp = *ampp; amp != NULL;){
564 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
565 /* xxx Expensive but rare: be simple */
566 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
567 amp = (lamp == NULL) ? *ampp : lamp->am_next;
568 }else{
569 lamp = amp;
570 amp = amp->am_next;
574 NYD2_LEAVE;
575 return rv;
578 static void
579 a_amv_mac_free(struct a_amv_mac *amp){
580 struct a_amv_mac_line **amlpp;
581 NYD2_ENTER;
583 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
584 free(*amlpp);
585 free(amp->am_line_dat);
586 free(amp);
587 NYD2_LEAVE;
590 static void
591 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
592 struct a_amv_var *oavp){
593 struct a_amv_var *avp;
594 size_t nl, vl;
595 NYD2_ENTER;
597 /* Propagate unrolling up the stack, as necessary */
598 assert(alp != NULL);
599 for(;;){
600 if(alp->as_unroll)
601 break;
602 if((alp = alp->as_up) == NULL)
603 goto jleave;
606 /* Check wether this variable is handled yet */
607 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
608 if(!strcmp(avp->av_name, name))
609 goto jleave;
611 nl = strlen(name) +1;
612 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
613 avp = smalloc(sizeof(*avp) - VFIELD_SIZEOF(struct a_amv_var, av_name) +
614 nl + vl);
615 avp->av_link = alp->as_lopts;
616 alp->as_lopts = avp;
617 memcpy(avp->av_name, name, nl);
618 if(vl == 0){
619 avp->av_value = NULL;
620 avp->av_flags = 0;
621 #ifdef HAVE_PUTENV
622 avp->av_env = NULL;
623 #endif
624 }else{
625 avp->av_value = avp->av_name + nl;
626 avp->av_flags = oavp->av_flags;
627 memcpy(avp->av_value, oavp->av_value, vl);
628 #ifdef HAVE_PUTENV
629 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
630 #endif
632 jleave:
633 NYD2_LEAVE;
636 static void
637 a_amv_lopts_unroll(struct a_amv_var **avpp){
638 struct a_amv_lostack *save_alp;
639 bool_t reset;
640 struct a_amv_var *x, *avp;
641 NYD2_ENTER;
643 avp = *avpp;
644 *avpp = NULL;
645 reset = !(pstate & PS_ROOT);
647 save_alp = a_amv_lopts;
648 a_amv_lopts = NULL;
649 while(avp != NULL){
650 x = avp;
651 avp = avp->av_link;
652 pstate |= PS_ROOT;
653 vok_vset(x->av_name, x->av_value);
654 if(reset)
655 pstate &= ~PS_ROOT;
656 free(x);
658 a_amv_lopts = save_alp;
659 NYD2_LEAVE;
662 static char *
663 a_amv_var_copy(char const *str){
664 char *news;
665 size_t len;
666 NYD2_ENTER;
668 if(*str == '\0')
669 news = UNCONST("");
670 else{
671 len = strlen(str) +1;
672 news = smalloc(len);
673 memcpy(news, str, len);
675 NYD2_LEAVE;
676 return news;
679 static void
680 a_amv_var_free(char *cp){
681 NYD2_ENTER;
682 if(*cp != '\0')
683 free(cp);
684 NYD2_LEAVE;
687 static bool_t
688 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
689 int flag;
690 bool_t ok;
691 NYD2_ENTER;
693 ok = TRU1;
694 flag = 0;
696 switch(okey){
697 case ok_b_debug:
698 flag = OPT_DEBUG;
699 break;
700 case ok_v_HOME:
701 /* Invalidate any resolved folder then, too
702 * FALLTHRU */
703 case ok_v_folder:
704 ok = !(pstate & PS_ROOT);
705 pstate |= PS_ROOT;
706 ok_vclear(_folder_resolved);
707 if(ok)
708 pstate &= ~PS_ROOT;
709 ok = TRU1;
710 break;
711 case ok_b_header:
712 flag = OPT_N_FLAG;
713 enable = !enable;
714 break;
715 case ok_b_memdebug:
716 flag = OPT_MEMDEBUG;
717 break;
718 case ok_b_skipemptybody:
719 flag = OPT_E_FLAG;
720 break;
721 case ok_v_TMPDIR:
722 if(enable) /* DEFVAL will soon ensure a value otherwise! */
723 tempdir = *val; /* XXX replace users with ok_vlook(TMPDIR) */
724 break;
725 case ok_b_verbose:
726 flag = (enable && !(options & OPT_VERB))
727 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
728 break;
729 default:
730 DBG( n_err("Implementation error: never heard of %u\n", ok); )
731 break;
734 if(flag){
735 if(enable)
736 options |= flag;
737 else
738 options &= ~flag;
740 NYD2_LEAVE;
741 return ok;
744 static bool_t
745 a_amv_var_check_nocntrls(char const *val){
746 char c;
747 NYD2_ENTER;
749 while((c = *val++) != '\0')
750 if(cntrlchar(c))
751 break;
752 NYD2_LEAVE;
753 return (c == '\0');
756 static bool_t
757 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
758 /* TODO The internal/environment variables which are num= or posnum= should
759 * TODO gain special lookup functions, or the return should be void* and
760 * TODO castable to integer; i.e. no more strtoX() should be needed.
761 * TODO I.e., the result of this function should instead be stored.
762 * TODO Use intmax_t IF that is sizeof(void*) only? */
763 bool_t rv;
764 NYD2_ENTER;
766 rv = TRU1;
768 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
769 char *eptr;
770 union {long s; unsigned long u;} i;
772 if(!pos){
773 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
775 if(*eptr != '\0' ||
776 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
777 rv = FAL0;
778 #if INT_MIN != LONG_MIN
779 else if(i.s < INT_MIN)
780 rv = FAL0;
781 #endif
782 #if INT_MAX != LONG_MAX
783 else if(i.s > INT_MAX)
784 rv = FAL0;
785 #endif
786 }else{
787 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
789 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
790 rv = FAL0;
791 #if UINT_MAX != ULONG_MAX
792 else if(i.u > UINT_MAX)
793 rv = FAL0;
794 #endif
797 NYD2_LEAVE;
798 return rv;
801 static char const *
802 a_amv_var_canonify(char const *vn){
803 NYD2_ENTER;
804 if(!upperchar(*vn)){
805 char const *vp;
807 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
809 vn = (*vp == '@') ? i_strdup(vn) : vn;
811 NYD2_LEAVE;
812 return vn;
815 static bool_t
816 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
817 ui32_t hash, i, j;
818 struct a_amv_var_map const *avmp;
819 NYD2_ENTER;
821 avcp->avc_name = name = a_amv_var_canonify(name);
822 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
824 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
825 ui32_t x = a_amv_var_revmap[i];
827 if(x == a_AMV_VAR_REV_ILL)
828 break;
830 avmp = &a_amv_var_map[x];
831 if(avmp->avm_hash == hash &&
832 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
833 avcp->avc_map = avmp;
834 avcp->avc_okey = (enum okeys)x;
835 goto jleave;
838 if(++i == a_AMV_VAR_REV_PRIME){
839 #ifdef a_AMV_VAR_REV_WRAPAROUND
840 i = 0;
841 #else
842 break;
843 #endif
846 avcp->avc_map = NULL;
847 avcp = NULL;
848 jleave:
849 NYD2_LEAVE;
850 return (avcp != NULL);
853 static bool_t
854 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
855 size_t i;
856 char const *cp;
857 struct a_amv_var_map const *avmp;
858 struct a_amv_var *avp;
859 NYD2_ENTER;
861 /* C99 */{
862 struct a_amv_var **avpp, *lavp;
864 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
866 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
867 if(!strcmp(avp->av_name, avcp->avc_name)){
868 /* Relink as head, hope it "sorts on usage" over time.
869 * _clear() relies on this behaviour */
870 if(lavp != NULL){
871 lavp->av_link = avp->av_link;
872 avp->av_link = *avpp;
873 *avpp = avp;
875 goto jleave;
879 /* If this is not an assembled variable we need to consider some special
880 * initialization cases and eventually create the variable anew */
881 if(LIKELY((avmp = avcp->avc_map) != NULL)){
882 /* Does it have an import-from-environment flag? */
883 if(UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
884 if(LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
885 /* May be better not to use that one, though? */
886 bool_t isempty, isbltin;
888 isempty = (*cp == '\0' &&
889 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
890 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
891 ) != 0);
893 if(UNLIKELY(isempty)){
894 if(!isbltin)
895 goto jerr;
896 }else if(LIKELY(*cp != '\0')){
897 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
898 !a_amv_var_check_nocntrls(cp))){
899 n_err(_("Ignoring environment, control characters "
900 "invalid in variable: %s\n"), avcp->avc_name);
901 goto jerr;
903 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
904 !a_amv_var_check_num(cp, FAL0))){
905 n_err(_("Environment variable value not a number "
906 "or out of range: %s\n"), avcp->avc_name);
907 goto jerr;
909 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
910 !a_amv_var_check_num(cp, TRU1))){
911 n_err(_("Environment variable value not a number, "
912 "negative or out of range: %s\n"), avcp->avc_name);
913 goto jerr;
916 goto jnewval;
920 /* A first-time init switch is to be handled now and here */
921 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
922 static struct a_amv_var_defval const **arr,
923 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
925 if(arr == NULL){
926 arr = &arr_base[0];
927 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
928 while(i-- > 0)
929 arr[i] = &a_amv_var_i3vals[i];
932 for(i = 0; arr[i] != NULL; ++i)
933 if(arr[i]->avdv_okey == avcp->avc_okey){
934 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
935 : arr[i]->avdv_value;
936 /* Remove this entry, hope entire block becomes no-op asap */
938 arr[i] = arr[i + 1];
939 while(arr[i++] != NULL);
941 if(!i3val_nonew)
942 goto jnewval;
943 if(i3val_nonew == TRUM1)
944 avp = (struct a_amv_var*)-1;
945 goto jleave;
949 /* The virtual variables */
950 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
951 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
952 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
953 avp = UNCONST(a_amv_var_virts[i].avv_var);
954 goto jleave;
956 /* Not reached */
959 /* Place this last because once it is set first the variable will never
960 * be removed again and thus match in the first block above */
961 if(UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
962 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
963 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
964 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
965 : a_amv_var_defvals[i].avdv_value;
966 goto jnewval;
971 jerr:
972 avp = NULL;
973 jleave:
974 avcp->avc_var = avp;
975 NYD2_LEAVE;
976 return (avp != NULL);
978 jnewval: /* C99 */{
979 struct a_amv_var **avpp;
980 size_t l;
982 l = strlen(avcp->avc_name) +1;
983 avcp->avc_var = avp = smalloc(sizeof(*avp) -
984 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
985 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
986 *avpp = avp;
987 memcpy(avp->av_name, avcp->avc_name, l);
988 avp->av_value = a_amv_var_copy(cp);
989 #ifdef HAVE_PUTENV
990 avp->av_env = NULL;
991 #endif
992 avp->av_flags = avmp->avm_flags;
994 if(avp->av_flags & a_AMV_VF_VIP)
995 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
996 if(avp->av_flags & a_AMV_VF_ENV)
997 a_amv_var__putenv(avcp, avp);
998 goto jleave;
1002 static bool_t
1003 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1004 bool_t force_env){
1005 struct a_amv_var *avp;
1006 char *oval;
1007 struct a_amv_var_map const *avmp;
1008 bool_t rv;
1009 NYD2_ENTER;
1011 if(value == NULL){
1012 rv = a_amv_var_clear(avcp, force_env);
1013 goto jleave;
1016 if((avmp = avcp->avc_map) != NULL){
1017 rv = FAL0;
1019 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1020 !(pstate & PS_ROOT))){
1021 n_err(_("Variable is readonly: \"%s\"\n"), avcp->avc_name);
1022 goto jleave;
1024 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1025 n_err(_("Variable must not be empty: \"%s\"\n"), avcp->avc_name);
1026 goto jleave;
1028 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1029 !a_amv_var_check_nocntrls(value))){
1030 n_err(_("Variable forbids control characters: %s\n"),
1031 avcp->avc_name);
1032 goto jleave;
1034 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1035 !a_amv_var_check_num(value, FAL0))){
1036 n_err(_("Variable value not a number or out of range: %s\n"),
1037 avcp->avc_name);
1038 goto jleave;
1040 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1041 !a_amv_var_check_num(value, TRU1))){
1042 n_err(_("Variable value not a number, negative or out of range: %s\n"),
1043 avcp->avc_name);
1044 goto jleave;
1046 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1047 !(pstate & (PS_ROOT | PS_STARTED)))){
1048 n_err(_("Variable cannot be set in a resource file: \"%s\"\n"),
1049 avcp->avc_name);
1050 goto jleave;
1054 rv = TRU1;
1055 a_amv_var_lookup(avcp, TRU1);
1057 /* Don't care what happens later on, store this in the unroll list */
1058 if(a_amv_lopts != NULL)
1059 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1061 if((avp = avcp->avc_var) == NULL){
1062 struct a_amv_var **avpp;
1063 size_t l = strlen(avcp->avc_name) +1;
1065 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1066 VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1067 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1068 *avpp = avp;
1069 #ifdef HAVE_PUTENV
1070 avp->av_env = NULL;
1071 #endif
1072 memcpy(avp->av_name, avcp->avc_name, l);
1073 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1074 oval = UNCONST("");
1075 }else
1076 oval = avp->av_value;
1078 if(avmp == NULL)
1079 avp->av_value = a_amv_var_copy(value);
1080 else{
1081 /* Via `set' etc. the user may give even boolean options non-boolean
1082 * values, ignore that and force boolean */
1083 if(avp->av_flags & a_AMV_VF_BOOL){
1084 if((options & OPT_D_VV) && *value != '\0')
1085 n_err(_("\"%s\" is a boolean variable, ignoring value: %s\n"),
1086 avcp->avc_name, value);
1087 avp->av_value = UNCONST(value = "");
1088 }else
1089 avp->av_value = a_amv_var_copy(value);
1091 /* Check if update allowed XXX wasteful on error! */
1092 if((avp->av_flags & a_AMV_VF_VIP) &&
1093 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1094 char *cp = avp->av_value;
1096 avp->av_value = oval;
1097 oval = cp;
1101 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1102 avp->av_flags |= a_AMV_VF_LINKED;
1103 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1104 rv = a_amv_var__putenv(avcp, avp);
1106 a_amv_var_free(oval);
1107 jleave:
1108 NYD2_LEAVE;
1109 return rv;
1112 static bool_t
1113 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1114 #ifndef HAVE_SETENV
1115 char *cp;
1116 #endif
1117 bool_t rv;
1118 NYD2_ENTER;
1120 #ifdef HAVE_SETENV
1121 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1122 #else
1123 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1125 if((rv = (putenv(cp) == 0))){
1126 char *ocp;
1128 if((ocp = avp->av_env) != NULL)
1129 free(ocp);
1130 avp->av_env = cp;
1131 }else
1132 free(cp);
1133 #endif
1134 NYD2_LEAVE;
1135 return rv;
1138 static bool_t
1139 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1140 struct a_amv_var **avpp, *avp;
1141 struct a_amv_var_map const *avmp;
1142 bool_t rv;
1143 NYD2_ENTER;
1145 rv = FAL0;
1147 if(LIKELY((avmp = avcp->avc_map) != NULL)){
1148 if(UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1149 !(pstate & PS_ROOT))){
1150 n_err(_("Variable may not be unset: \"%s\"\n"), avcp->avc_name);
1151 goto jleave;
1153 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1154 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1155 goto jleave;
1158 rv = TRU1;
1160 if(UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1161 if(force_env){
1162 jforce_env:
1163 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1164 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1165 n_err(_("Can't unset undefined variable: \"%s\"\n"), avcp->avc_name);
1166 goto jleave;
1167 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1168 avcp->avc_var = NULL;
1169 if(force_env)
1170 goto jforce_env;
1171 goto jleave;
1174 if(a_amv_lopts != NULL)
1175 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1177 avp = avcp->avc_var;
1178 avcp->avc_var = NULL;
1179 avpp = &a_amv_vars[avcp->avc_prime];
1180 assert(*avpp == avp); /* (always listhead after lookup()) */
1181 *avpp = (*avpp)->av_link;
1183 /* C99 */{
1184 #ifdef HAVE_SETENV
1185 char *envval = NULL;
1186 #else
1187 char *envval = avp->av_env;
1188 #endif
1189 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1190 rv = a_amv_var__clearenv(avp->av_name, envval);
1192 a_amv_var_free(avp->av_value);
1193 free(avp);
1195 /* XXX Fun part, extremely simple-minded for now: if this variable has
1196 * XXX a default value, immediately reinstantiate it! */
1197 if(UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1198 a_amv_var_lookup(avcp, TRU1);
1199 jleave:
1200 NYD2_LEAVE;
1201 return rv;
1204 static bool_t
1205 a_amv_var__clearenv(char const *name, char *value){
1206 #ifndef HAVE_SETENV
1207 extern char **environ;
1208 char **ecpp;
1209 #endif
1210 bool_t rv;
1211 NYD2_ENTER;
1212 UNUSED(value);
1214 #ifdef HAVE_SETENV
1215 unsetenv(name);
1216 rv = TRU1;
1217 #else
1218 if(value != NULL)
1219 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1220 if(*ecpp == value){
1221 free(value);
1223 ecpp[0] = ecpp[1];
1224 while(*ecpp++ != NULL);
1225 break;
1227 rv = TRU1;
1228 #endif
1229 NYD2_LEAVE;
1230 return rv;
1233 static void
1234 a_amv_var_show_all(void){
1235 struct n_string msg, *msgp;
1236 FILE *fp;
1237 size_t no, i;
1238 struct a_amv_var *avp;
1239 char const **vacp, **cap;
1240 NYD2_ENTER;
1242 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1243 n_perr(_("Can't create temporary file for `set' listing"), 0);
1244 goto jleave;
1247 /* We need to instantiate first-time-inits and default values here, so that
1248 * they will be regular members of our _vars[] table */
1249 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1250 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1251 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1252 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1254 for(no = i = 0; i < a_AMV_PRIME; ++i)
1255 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1256 ++no;
1257 no += a_AMV_VAR_VIRTS_CNT;
1259 vacp = salloc(no * sizeof(*vacp));
1261 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1262 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1263 *cap++ = avp->av_name;
1264 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1265 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1267 if(no > 1)
1268 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1270 msgp = &msg;
1271 msgp = n_string_reserve(n_string_creat(msgp), 80);
1272 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1273 i += a_amv_var_show(*cap, fp, msgp);
1274 n_string_gut(&msg);
1276 page_or_print(fp, i);
1277 Fclose(fp);
1278 jleave:
1279 NYD2_LEAVE;
1282 static int
1283 a_amv_var__show_cmp(void const *s1, void const *s2){
1284 int rv;
1285 NYD2_ENTER;
1287 rv = strcmp(*(char**)UNCONST(s1), *(char**)UNCONST(s2));
1288 NYD2_LEAVE;
1289 return rv;
1292 static size_t
1293 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1294 struct a_amv_var_carrier avc;
1295 char const *quote;
1296 size_t i;
1297 NYD2_ENTER;
1299 msgp = n_string_trunc(msgp, 0);
1300 i = 0;
1302 a_amv_var_revlookup(&avc, name);
1303 if(!a_amv_var_lookup(&avc, FAL0)){
1304 struct str s;
1306 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1307 s.s = UNCONST(name);
1308 s.l = UIZ_MAX;
1309 msgp = n_shell_quote(msgp, &s, FAL0);
1310 goto jleave;
1313 if(options & OPT_D_V){
1314 if(avc.avc_map == NULL){
1315 msgp = n_string_push_c(msgp, '#');
1316 msgp = n_string_push_cp(msgp, "assembled");
1317 i = 1;
1319 /* C99 */{
1320 struct{
1321 ui16_t flags;
1322 char msg[22];
1323 } const tbase[] = {
1324 {a_AMV_VF_VIRT, "virtual"},
1325 {a_AMV_VF_RDONLY, "readonly"},
1326 {a_AMV_VF_NODEL, "nodelete"},
1327 {a_AMV_VF_NOTEMPTY, "notempty"},
1328 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1329 {a_AMV_VF_NUM, "number"},
1330 {a_AMV_VF_POSNUM, "positive-number"},
1331 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1332 {a_AMV_VF_ENV, "sync-environ"},
1333 {a_AMV_VF_I3VAL, "initial-value"},
1334 {a_AMV_VF_DEFVAL, "default-value"},
1335 {a_AMV_VF_LINKED, "`environ' link"}
1336 }, *tp;
1338 for(tp = tbase; PTRCMP(tp, <, &tbase[NELEM(tbase)]); ++tp)
1339 if(avc.avc_var->av_flags & tp->flags){
1340 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1341 msgp = n_string_push_cp(msgp, tp->msg);
1345 if(i > 0)
1346 msgp = n_string_push_cp(msgp, "\n ");
1349 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1350 msgp = n_string_push_cp(msgp, "# ");
1351 UNINIT(quote, NULL);
1352 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1353 quote = n_shell_quote_cp(avc.avc_var->av_value, TRU1);
1354 if(strcmp(quote, avc.avc_var->av_value))
1355 msgp = n_string_push_cp(msgp, "wysh ");
1357 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1358 msgp = n_string_push_cp(msgp, "environ ");
1359 msgp = n_string_push_cp(msgp, "set ");
1360 msgp = n_string_push_cp(msgp, name);
1361 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1362 msgp = n_string_push_c(msgp, '=');
1363 msgp = n_string_push_cp(msgp, quote);
1366 jleave:
1367 msgp = n_string_push_c(msgp, '\n');
1368 fputs(n_string_cp(msgp), fp);
1369 NYD2_ENTER;
1370 return (i > 0 ? 2 : 1);
1373 static bool_t
1374 a_amv_var_c_set(char **ap, bool_t issetenv){
1375 char *cp, *cp2, *varbuf, c;
1376 size_t errs;
1377 NYD2_ENTER;
1379 errs = 0;
1380 jouter:
1381 while((cp = *ap++) != NULL){
1382 /* Isolate key */
1383 cp2 = varbuf = salloc(strlen(cp) +1);
1385 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1386 if(cntrlchar(c) || whitechar(c)){
1387 n_err(_("Variable name with control or newline character ignored: "
1388 "\"%s\"\n"), ap[-1]);
1389 ++errs;
1390 goto jouter;
1392 *cp2++ = c;
1394 *cp2 = '\0';
1395 if(c == '\0')
1396 cp = UNCONST("");
1397 else
1398 ++cp;
1400 if(varbuf == cp2){
1401 n_err(_("Empty variable name ignored\n"));
1402 ++errs;
1403 }else{
1404 struct a_amv_var_carrier avc;
1405 bool_t isunset;
1407 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1408 varbuf = &varbuf[2];
1410 a_amv_var_revlookup(&avc, varbuf);
1412 if(isunset)
1413 errs += !a_amv_var_clear(&avc, issetenv);
1414 else
1415 errs += !a_amv_var_set(&avc, cp, issetenv);
1418 NYD2_LEAVE;
1419 return (errs == 0);
1422 FL int
1423 c_define(void *v){
1424 int rv;
1425 char **args;
1426 NYD_ENTER;
1428 rv = 1;
1430 if((args = v)[0] == NULL){
1431 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1432 goto jleave;
1435 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1436 args[2] != NULL){
1437 n_err(_("Synopsis: define: <name> {\n"));
1438 goto jleave;
1441 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1442 jleave:
1443 NYD_LEAVE;
1444 return rv;
1447 FL int
1448 c_undefine(void *v){
1449 int rv;
1450 char **args;
1451 NYD_ENTER;
1453 rv = 0;
1454 args = v;
1456 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1457 while(*++args != NULL);
1458 NYD_LEAVE;
1459 return rv;
1462 FL int
1463 c_call(void *v){
1464 struct a_amv_mac_call_args amca;
1465 char const *errs, *name;
1466 struct a_amv_mac *amp;
1467 char **args;
1468 int rv;
1469 NYD_ENTER;
1471 rv = 1;
1473 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1474 errs = _("Synopsis: call: <%s>\n");
1475 name = "name";
1476 goto jerr;
1479 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1480 errs = _("Undefined macro `call'ed: \"%s\"\n");
1481 goto jerr;
1484 memset(&amca, 0, sizeof amca);
1485 amca.amca_name = name;
1486 amca.amca_amp = amp;
1487 rv = (a_amv_mac_exec(&amca) == FAL0);
1488 jleave:
1489 NYD_LEAVE;
1490 return rv;
1491 jerr:
1492 n_err(errs, name);
1493 goto jleave;
1496 FL bool_t
1497 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1498 struct a_amv_mac_call_args amca;
1499 struct a_amv_mac *amp;
1500 size_t len;
1501 char *var, *cp;
1502 bool_t rv;
1503 NYD_ENTER;
1505 rv = TRU1;
1506 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1508 /* First try the fully resolved path */
1509 snprintf(var, len, "folder-hook-%s", mailname);
1510 if((cp = vok_vlook(var)) != NULL)
1511 goto jmac;
1513 /* If we are under *folder*, try the usual +NAME syntax, too */
1514 if(displayname[0] == '+'){
1515 char *x = mailname + len;
1517 for(; x > mailname; --x)
1518 if(x[-1] == '/'){
1519 snprintf(var, len, "folder-hook-+%s", x);
1520 if((cp = vok_vlook(var)) != NULL)
1521 goto jmac;
1522 break;
1526 /* Plain *folder-hook* is our last try */
1527 if((cp = ok_vlook(folder_hook)) == NULL)
1528 goto jleave;
1530 jmac:
1531 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1532 n_err(_("Cannot call *folder-hook* for \"%s\": "
1533 "macro \"%s\" does not exist\n"), displayname, cp);
1534 rv = FAL0;
1535 goto jleave;
1538 memset(&amca, 0, sizeof amca);
1539 amca.amca_name = cp;
1540 amca.amca_amp = amp;
1541 pstate &= ~PS_HOOK_MASK;
1542 if(nmail){
1543 amca.amca_unroller = NULL;
1544 pstate |= PS_HOOK_NEWMAIL;
1545 }else{
1546 amca.amca_unroller = &a_amv_folder_hook_lopts;
1547 pstate |= PS_HOOK;
1549 amca.amca_lopts_on = TRU1;
1550 rv = a_amv_mac_exec(&amca);
1551 pstate &= ~PS_HOOK_MASK;
1553 jleave:
1554 NYD_LEAVE;
1555 return rv;
1558 FL void
1559 call_compose_mode_hook(char const *macname, /* TODO temporary, v15: drop */
1560 void (*hook_pre)(void *), void *hook_arg){
1561 struct a_amv_mac_call_args amca;
1562 struct a_amv_mac *amp;
1563 NYD_ENTER;
1565 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1566 n_err(_("Cannot call *on-compose-**: macro \"%s\" does not exist\n"),
1567 macname);
1568 else{
1569 memset(&amca, 0, sizeof amca);
1570 amca.amca_name = macname;
1571 amca.amca_amp = amp;
1572 amca.amca_unroller = &a_amv_compose_lopts;
1573 amca.amca_hook_pre = hook_pre;
1574 amca.amca_hook_arg = hook_arg;
1575 amca.amca_lopts_on = TRU1;
1576 pstate &= ~PS_HOOK_MASK;
1577 pstate |= PS_HOOK;
1578 a_amv_mac_exec(&amca);
1579 pstate &= ~PS_HOOK_MASK;
1581 NYD_LEAVE;
1584 FL int
1585 c_account(void *v){
1586 struct a_amv_mac_call_args amca;
1587 struct a_amv_mac *amp;
1588 char **args;
1589 int rv, i, oqf, nqf;
1590 NYD_ENTER;
1592 rv = 1;
1594 if((args = v)[0] == NULL){
1595 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1596 goto jleave;
1599 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1600 if(args[2] != NULL){
1601 n_err(_("Synopsis: account: <name> {\n"));
1602 goto jleave;
1604 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1605 n_err(_("`account': error: \"%s\" is a reserved name\n"),
1606 ACCOUNT_NULL);
1607 goto jleave;
1609 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1610 goto jleave;
1613 if(pstate & PS_HOOK_MASK){
1614 n_err(_("`account': can't change account from within a hook\n"));
1615 goto jleave;
1618 save_mbox_for_possible_quitstuff();
1620 amp = NULL;
1621 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1622 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1623 n_err(_("`account': account \"%s\" does not exist\n"), args[0]);
1624 goto jleave;
1627 oqf = savequitflags();
1629 if(a_amv_acc_curr != NULL){
1630 if(a_amv_acc_curr->am_lopts != NULL)
1631 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1632 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1633 a_amv_mac_free(a_amv_acc_curr);
1636 account_name = (amp != NULL) ? amp->am_name : NULL;
1637 a_amv_acc_curr = amp;
1639 if(amp != NULL){
1640 assert(amp->am_lopts == NULL);
1641 memset(&amca, 0, sizeof amca);
1642 amca.amca_name = amp->am_name;
1643 amca.amca_amp = amp;
1644 amca.amca_unroller = &amp->am_lopts;
1645 amca.amca_lopts_on = TRU1;
1646 if(!a_amv_mac_exec(&amca)){
1647 /* XXX account switch incomplete, unroll? */
1648 n_err(_("`account': switching to account \"%s\" failed\n"),
1649 amp->am_name);
1650 goto jleave;
1654 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1655 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1656 restorequitflags(oqf);
1657 if((i = setfile("%", 0)) < 0)
1658 goto jleave;
1659 check_folder_hook(FAL0);
1660 if(i > 0 && !ok_blook(emptystart))
1661 goto jleave;
1662 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1663 restorequitflags(nqf);
1665 rv = 0;
1666 jleave:
1667 NYD_LEAVE;
1668 return rv;
1671 FL int
1672 c_unaccount(void *v){
1673 int rv;
1674 char **args;
1675 NYD_ENTER;
1677 rv = 0;
1678 args = v;
1680 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1681 while(*++args != NULL);
1682 NYD_LEAVE;
1683 return rv;
1686 FL int
1687 c_localopts(void *v){
1688 char **argv;
1689 int rv;
1690 NYD_ENTER;
1692 rv = 1;
1694 if(a_amv_lopts == NULL){
1695 n_err(_("Cannot use `localopts' but from within a "
1696 "`define' or `account'\n"));
1697 goto jleave;
1700 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1701 rv = 0;
1702 jleave:
1703 NYD_LEAVE;
1704 return rv;
1707 FL void
1708 temporary_localopts_free(void){ /* XXX intermediate hack */
1709 NYD_ENTER;
1710 if(a_amv_compose_lopts != NULL){
1711 void *save = a_amv_lopts;
1713 a_amv_lopts = NULL;
1714 a_amv_lopts_unroll(&a_amv_compose_lopts);
1715 a_amv_compose_lopts = NULL;
1716 a_amv_lopts = save;
1718 NYD_LEAVE;
1721 FL void
1722 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1723 NYD_ENTER;
1724 if(a_amv_folder_hook_lopts != NULL){
1725 void *save = a_amv_lopts;
1727 a_amv_lopts = NULL;
1728 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1729 a_amv_folder_hook_lopts = NULL;
1730 a_amv_lopts = save;
1732 NYD_LEAVE;
1735 FL char *
1736 n_var_oklook(enum okeys okey){
1737 struct a_amv_var_carrier avc;
1738 char *rv;
1739 struct a_amv_var_map const *avmp;
1740 NYD_ENTER;
1742 avc.avc_map = avmp = &a_amv_var_map[okey];
1743 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1744 avc.avc_hash = avmp->avm_hash;
1745 avc.avc_okey = okey;
1747 if(a_amv_var_lookup(&avc, FAL0))
1748 rv = avc.avc_var->av_value;
1749 else
1750 rv = NULL;
1751 NYD_LEAVE;
1752 return rv;
1755 FL bool_t
1756 n_var_okset(enum okeys okey, uintptr_t val){
1757 struct a_amv_var_carrier avc;
1758 bool_t ok;
1759 struct a_amv_var_map const *avmp;
1760 NYD_ENTER;
1762 avc.avc_map = avmp = &a_amv_var_map[okey];
1763 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1764 avc.avc_hash = avmp->avm_hash;
1765 avc.avc_okey = okey;
1767 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1768 NYD_LEAVE;
1769 return ok;
1772 FL bool_t
1773 n_var_okclear(enum okeys okey){
1774 struct a_amv_var_carrier avc;
1775 bool_t rv;
1776 struct a_amv_var_map const *avmp;
1777 NYD_ENTER;
1779 avc.avc_map = avmp = &a_amv_var_map[okey];
1780 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1781 avc.avc_hash = avmp->avm_hash;
1782 avc.avc_okey = okey;
1784 rv = a_amv_var_clear(&avc, FAL0);
1785 NYD_LEAVE;
1786 return rv;
1789 FL char *
1790 n_var_voklook(char const *vokey){
1791 struct a_amv_var_carrier avc;
1792 char *rv;
1793 NYD_ENTER;
1795 a_amv_var_revlookup(&avc, vokey);
1797 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1798 NYD_LEAVE;
1799 return rv;
1802 FL bool_t
1803 n_var_vokset(char const *vokey, uintptr_t val){
1804 struct a_amv_var_carrier avc;
1805 bool_t ok;
1806 NYD_ENTER;
1808 a_amv_var_revlookup(&avc, vokey);
1810 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1811 NYD_LEAVE;
1812 return ok;
1815 FL bool_t
1816 n_var_vokclear(char const *vokey){
1817 struct a_amv_var_carrier avc;
1818 bool_t ok;
1819 NYD_ENTER;
1821 a_amv_var_revlookup(&avc, vokey);
1823 ok = a_amv_var_clear(&avc, FAL0);
1824 NYD_LEAVE;
1825 return ok;
1828 #ifdef HAVE_SOCKETS
1829 FL char *
1830 n_var_xoklook(enum okeys okey, struct url const *urlp,
1831 enum okey_xlook_mode oxm){
1832 struct a_amv_var_carrier avc;
1833 struct str const *us;
1834 size_t nlen;
1835 char *nbuf, *rv;
1836 struct a_amv_var_map const *avmp;
1837 NYD_ENTER;
1839 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1841 /* For simplicity: allow this case too */
1842 if(!(oxm & (OXM_H_P | OXM_U_H_P)))
1843 goto jplain;
1845 avc.avc_map = avmp = &a_amv_var_map[okey];
1846 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1847 avc.avc_okey = okey;
1849 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1850 nlen = strlen(avc.avc_name);
1851 nbuf = salloc(nlen + 1 + us->l +1);
1852 memcpy(nbuf, avc.avc_name, nlen);
1853 nbuf[nlen++] = '-';
1855 /* One of .url_u_h_p and .url_h_p we test in here */
1856 memcpy(nbuf + nlen, us->s, us->l +1);
1857 avc.avc_name = a_amv_var_canonify(nbuf);
1858 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1859 if(a_amv_var_lookup(&avc, FAL0))
1860 goto jvar;
1862 /* The second */
1863 if(oxm & OXM_H_P){
1864 us = &urlp->url_h_p;
1865 memcpy(nbuf + nlen, us->s, us->l +1);
1866 avc.avc_name = a_amv_var_canonify(nbuf);
1867 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1868 if(a_amv_var_lookup(&avc, FAL0)){
1869 jvar:
1870 rv = avc.avc_var->av_value;
1871 goto jleave;
1875 jplain:
1876 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1877 jleave:
1878 NYD_LEAVE;
1879 return rv;
1881 #endif /* HAVE_SOCKETS */
1883 FL int
1884 c_set(void *v){
1885 char **ap;
1886 int err;
1887 NYD_ENTER;
1889 if(*(ap = v) == NULL){
1890 a_amv_var_show_all();
1891 err = 0;
1892 }else
1893 err = !a_amv_var_c_set(ap, FAL0);
1894 NYD_LEAVE;
1895 return err;
1898 FL int
1899 c_unset(void *v){
1900 char **ap;
1901 int err;
1902 NYD_ENTER;
1904 for(err = 0, ap = v; *ap != NULL; ++ap)
1905 err |= !n_var_vokclear(*ap);
1906 NYD_LEAVE;
1907 return err;
1910 FL int
1911 c_varshow(void *v){
1912 char **ap;
1913 NYD_ENTER;
1915 if(*(ap = v) == NULL)
1916 v = NULL;
1917 else{
1918 struct n_string msg, *msgp = &msg;
1920 msgp = n_string_creat(msgp);
1921 for(; *ap != NULL; ++ap)
1922 a_amv_var_show(*ap, stdout, msgp);
1923 n_string_gut(msgp);
1925 NYD_LEAVE;
1926 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1929 FL int
1930 c_varedit(void *v){
1931 struct a_amv_var_carrier avc;
1932 FILE *of, *nf;
1933 char *val, **argv;
1934 int err;
1935 sighandler_type sigint;
1936 NYD_ENTER;
1938 sigint = safe_signal(SIGINT, SIG_IGN);
1940 for(err = 0, argv = v; *argv != NULL; ++argv){
1941 memset(&avc, 0, sizeof avc);
1943 a_amv_var_revlookup(&avc, *argv);
1945 if(avc.avc_map != NULL){
1946 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1947 n_err(_("`varedit': can't edit boolean variable \"%s\"\n"),
1948 avc.avc_name);
1949 continue;
1951 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
1952 n_err(_("`varedit': can't edit readonly variable \"%s\"\n"),
1953 avc.avc_name);
1954 continue;
1958 a_amv_var_lookup(&avc, FAL0);
1960 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
1961 NULL){
1962 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
1963 err = 1;
1964 break;
1965 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
1966 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
1967 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
1968 Fclose(of);
1969 err = 1;
1970 continue;
1973 fflush_rewind(of);
1974 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
1975 Fclose(of);
1977 if(nf != NULL){
1978 int c;
1979 char *base;
1980 off_t l = fsize(nf);
1982 assert(l >= 0);
1983 base = salloc((size_t)l +1);
1985 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
1986 if(c == '\n' || c == '\r'){
1987 *val = ' ';
1988 ++l;
1989 }else{
1990 *val = (char)(uc_i)c;
1991 l = 0;
1993 val -= l;
1994 *val = '\0';
1996 if(!a_amv_var_set(&avc, base, FAL0))
1997 err = 1;
1999 Fclose(nf);
2000 }else{
2001 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2002 err = 1;
2003 break;
2007 safe_signal(SIGINT, sigint);
2008 NYD_LEAVE;
2009 return err;
2012 FL int
2013 c_environ(void *v){
2014 struct a_amv_var_carrier avc;
2015 int err;
2016 char **ap;
2017 bool_t islnk;
2018 NYD_ENTER;
2020 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2021 for(err = 0; *++ap != NULL;){
2022 a_amv_var_revlookup(&avc, *ap);
2024 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2025 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2026 if(!islnk){
2027 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2028 continue;
2029 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2030 if(options & OPT_D_V)
2031 n_err(_("`environ': link: already established for \"%s\"\n"),
2032 *ap);
2033 continue;
2035 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2036 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2037 a_amv_var__putenv(&avc, avc.avc_var);
2038 }else if(!islnk){
2039 n_err(_("`environ': unlink: no link established: \"%s\"\n"), *ap);
2040 err = 1;
2041 }else{
2042 char const *evp = getenv(*ap);
2044 if(evp != NULL)
2045 err |= !a_amv_var_set(&avc, evp, TRU1);
2046 else{
2047 n_err(_("`environ': link: cannot link non-existent variable "
2048 "\"%s\"\n"), *ap);
2049 err = 1;
2053 }else if(!strcmp(*ap, "set"))
2054 err = !a_amv_var_c_set(++ap, TRU1);
2055 else if(!strcmp(*ap, "unset")){
2056 for(err = 0; *++ap != NULL;){
2057 a_amv_var_revlookup(&avc, *ap);
2059 if(!a_amv_var_clear(&avc, TRU1))
2060 err = 1;
2062 }else{
2063 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2064 err = 1;
2066 NYD_LEAVE;
2067 return err;
2070 /* s-it-mode */