(BWDIC!) Rewrite prompt handling via new n_tty_create_prompt()..
[s-mailx.git] / accmacvar.c
blob5232604560ddc444e719f8383421dc017cced631
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Account, macro and variable handling.
3 *@ HOWTO add a new non-dynamic boolean or value option:
4 *@ - add an entry to nail.h:enum okeys
5 *@ - run mk-okey-map.pl
6 *@ - update the manual!
7 *@ TODO . should be recursive environment based
8 *@ TODO . undefining and overwriting a macro should always be possible:
9 *@ TODO simply place the thing in a delete-later list and replace the
10 *@ TODO accessible entry! (instant delete if on top recursion level.)
11 *@ TODO . Likewise, overwriting an existing should be like delete+create
12 *@ TODO . once we can have non-fatal !0 returns for commands, we should
13 *@ TODO return error if "(environ)? unset" goes for non-existent.
15 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
16 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
19 * Copyright (c) 1980, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
46 #undef n_FILE
47 #define n_FILE accmacvar
49 #ifndef HAVE_AMALGAMATION
50 # include "nail.h"
51 #endif
53 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
54 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
55 #endif
57 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
58 #define a_AMV_PRIME HSHSIZE
59 #define a_AMV_NAME2HASH(N) torek_hash(N)
60 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
62 enum a_amv_mac_flags{
63 a_AMV_MF_NONE = 0,
64 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
65 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
66 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
67 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
68 a_AMV_MF__MAX = 0xFF
71 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
72 * _IMPORT implies _ENV; it doesn't verify anything... */
73 enum a_amv_var_flags{
74 a_AMV_VF_NONE = 0,
75 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
76 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
77 a_AMV_VF_NOLOPTS = 1<<2, /* May not be tracked by `localopts' */
78 a_AMV_VF_RDONLY = 1<<3, /* May not be set by user */
79 a_AMV_VF_NODEL = 1<<4, /* May not be deleted */
80 a_AMV_VF_NOTEMPTY = 1<<5, /* May not be assigned an empty value */
81 a_AMV_VF_NOCNTRLS = 1<<6, /* Value may not contain control characters */
82 a_AMV_VF_NUM = 1<<7, /* Value must be a 32-bit number */
83 a_AMV_VF_POSNUM = 1<<8, /* Value must be positive 32-bit number */
84 a_AMV_VF_VIP = 1<<9, /* Wants _var_check_vips() evaluation */
85 a_AMV_VF_IMPORT = 1<<10, /* Import ONLY from environ (before PS_STARTED) */
86 a_AMV_VF_ENV = 1<<11, /* Update environment on change */
87 a_AMV_VF_I3VAL = 1<<12, /* Has an initial value */
88 a_AMV_VF_DEFVAL = 1<<13, /* Has a default value */
89 a_AMV_VF_LINKED = 1<<14, /* `environ' linked */
90 a_AMV_VF__MASK = (1<<(14+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[n_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[n_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[n_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 ui8_t avv__dummy[4];
150 struct a_amv_var const *avv_var;
153 struct a_amv_var_defval{
154 ui32_t avdv_okey;
155 ui8_t avdv__pad[4];
156 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
159 struct a_amv_var_carrier{
160 char const *avc_name;
161 ui32_t avc_hash;
162 ui32_t avc_prime;
163 struct a_amv_var *avc_var;
164 struct a_amv_var_map const *avc_map;
165 enum okeys avc_okey;
166 ui8_t avc__pad[4];
169 /* Include the constant mk-okey-map.pl output */
170 #include "version.h"
171 #include "okeys.h"
173 /* True boolean visualization: this string will not be copied to heap memory
174 * in a_amv_var_copy(), but we must avoid confusion with identical user data.
175 * While here, add a special "0" one and speed up *.exit-status* assignments! */
176 static char const a_amv_var_1[] = "1";
177 static char const a_amv_var_0[] = "0";
179 /* The currently active account */
180 static struct a_amv_mac *a_amv_acc_curr;
182 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
184 /* Unroll list of currently running macro stack */
185 static struct a_amv_lostack *a_amv_lopts;
187 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
189 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
190 * TODO today via a static lostack, it should be a field in mailbox, once that
191 * TODO is a real multi-instance object */
192 static struct a_amv_var *a_amv_folder_hook_lopts;
194 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
195 static struct a_amv_var *a_amv_compose_lopts;
197 /* Does cp consist solely of WS and a } */
198 static bool_t a_amv_mac_is_closing_angle(char const *cp);
200 /* Lookup for macros/accounts */
201 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
202 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
204 /* Execute a macro */
205 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
207 /* User display helpers */
208 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
210 /* _def() returns error for faulty definitions and already existing * names,
211 * _undef() returns error if a named thing doesn't exist */
212 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
213 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
215 /* */
216 static void a_amv_mac_free(struct a_amv_mac *amp);
218 /* Update replay-log */
219 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
220 struct a_amv_var *oavp);
221 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
223 /* Special cased value string allocation */
224 static char *a_amv_var_copy(char const *str);
225 static void a_amv_var_free(char *cp);
227 /* Check for special housekeeping */
228 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
230 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
231 static bool_t a_amv_var_check_nocntrls(char const *val);
232 static bool_t a_amv_var_check_num(char const *val, bool_t pos);
234 /* If a variable name begins with a lowercase-character and contains at
235 * least one '@', it is converted to all-lowercase. This is necessary
236 * for lookups of names based on email addresses.
237 * Following the standard, only the part following the last '@' should
238 * be lower-cased, but practice has established otherwise here */
239 static char const *a_amv_var_canonify(char const *vn);
241 /* Try to reverse lookup an option name to an enum okeys mapping.
242 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
243 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
244 char const *name);
246 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
247 * Sets .avc_prime; .avc_var is NULL if not found.
248 * Here it is where we care for _I3VAL and _DEFVAL, too.
249 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
250 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
251 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
252 * return FAL0, then! */
253 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
254 bool_t i3val_nonew);
256 /* Set var from .avc_(map|name|hash), return success */
257 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
258 bool_t force_env);
260 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
261 struct a_amv_var *avp);
263 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
264 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
266 static bool_t a_amv_var__clearenv(char const *name, char *value);
268 /* List all variables */
269 static void a_amv_var_show_all(void);
271 static int a_amv_var__show_cmp(void const *s1, void const *s2);
273 /* Actually do print one, return number of lines written */
274 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
276 /* Shared c_set() and c_environ():set impl, return success */
277 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
279 static bool_t
280 a_amv_mac_is_closing_angle(char const *cp){
281 bool_t rv;
282 NYD2_ENTER;
284 while(spacechar(*cp))
285 ++cp;
287 if((rv = (*cp++ == '}'))){
288 while(spacechar(*cp))
289 ++cp;
290 rv = (*cp == '\0');
292 NYD2_LEAVE;
293 return rv;
296 static struct a_amv_mac *
297 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
298 enum a_amv_mac_flags amf){
299 struct a_amv_mac *amp, **ampp;
300 ui32_t h;
301 enum a_amv_mac_flags save_amf;
302 NYD2_ENTER;
304 save_amf = amf;
305 amf &= a_AMV_MF_TYPE_MASK;
306 h = a_AMV_NAME2HASH(name);
307 h = a_AMV_HASH2PRIME(h);
308 ampp = &a_amv_macs[h];
310 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
311 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
312 !strcmp(amp->am_name, name)){
313 if(n_LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
314 goto jleave;
316 *ampp = amp->am_next;
318 if((amf & a_AMV_MF_ACC) &&
319 account_name != NULL && !strcmp(account_name, name)){
320 amp->am_flags |= a_AMV_MF_DEL;
321 n_err(_("Delayed deletion of active account: %s\n"), name);
322 }else{
323 a_amv_mac_free(amp);
324 amp = (struct a_amv_mac*)-1;
326 goto jleave;
330 if(newamp != NULL){
331 ampp = &a_amv_macs[h];
332 newamp->am_next = *ampp;
333 *ampp = newamp;
334 amp = NULL;
336 jleave:
337 NYD2_LEAVE;
338 return amp;
341 static bool_t
342 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
343 struct a_amv_lostack los, *los_save;
344 struct a_amv_mac_line **amlp;
345 char **args_base, **args;
346 struct a_amv_mac const *amp;
347 bool_t rv;
348 NYD2_ENTER;
350 amp = amcap->amca_amp;
351 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
352 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
353 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
354 *args = NULL;
356 los.as_mac = n_UNCONST(amp); /* But not used.. */
357 los_save = a_amv_lopts;
358 if(amcap->amca_unroller == NULL){
359 los.as_up = los_save;
360 los.as_lopts = NULL;
361 }else{
362 los.as_up = NULL;
363 los.as_lopts = *amcap->amca_unroller;
365 los.as_unroll = amcap->amca_lopts_on;
367 a_amv_lopts = &los;
368 if(amcap->amca_hook_pre != NULL)
369 (*amcap->amca_hook_pre)(amcap->amca_hook_arg);
370 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base);
371 if(amcap->amca_hook_post != NULL)
372 (*amcap->amca_hook_post)(amcap->amca_hook_arg);
373 a_amv_lopts = los_save;
375 if(amcap->amca_unroller == NULL){
376 if(los.as_lopts != NULL)
377 a_amv_lopts_unroll(&los.as_lopts);
378 }else
379 *amcap->amca_unroller = los.as_lopts;
380 NYD2_LEAVE;
381 return rv;
384 static bool_t
385 a_amv_mac_show(enum a_amv_mac_flags amf){
386 size_t lc, mc, ti, i;
387 char const *typestr;
388 FILE *fp;
389 bool_t rv;
390 NYD2_ENTER;
392 rv = FAL0;
394 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
395 NULL){
396 n_perr(_("Can't create temporary file for `define' or `account' listing"),
398 goto jleave;
401 amf &= a_AMV_MF_TYPE_MASK;
402 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
404 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
405 struct a_amv_mac *amp;
407 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
408 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
409 struct a_amv_mac_line **amlpp;
411 if(++mc > 1){
412 putc('\n', fp);
413 ++lc;
415 ++lc;
416 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
417 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
418 for(i = (*amlpp)->aml_prespc; i > 0; --i)
419 putc(' ', fp);
420 fputs((*amlpp)->aml_dat, fp);
421 putc('\n', fp);
423 fputs("}\n", fp);
424 ++lc;
428 if(mc > 0)
429 page_or_print(fp, lc);
431 rv = (ferror(fp) == 0);
432 Fclose(fp);
433 jleave:
434 NYD2_LEAVE;
435 return rv;
438 static bool_t
439 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
440 struct str line;
441 ui32_t line_cnt, maxlen;
442 struct linelist{
443 struct linelist *ll_next;
444 struct a_amv_mac_line *ll_amlp;
445 } *llp, *ll_head, *ll_tail;
446 union {size_t s; int i; ui32_t ui; size_t l;} n;
447 struct a_amv_mac *amp;
448 bool_t rv;
449 NYD2_ENTER;
451 memset(&line, 0, sizeof line);
452 rv = FAL0;
453 amp = NULL;
455 /* Read in the lines which form the macro content */
456 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
457 ui32_t leaspc;
458 char *cp;
460 n.i = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, "",
461 &line.s, &line.l, NULL);
462 if(n.ui == 0)
463 continue;
464 if(n.i < 0){
465 n_err(_("Unterminated %s definition: %s\n"),
466 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
467 goto jerr;
469 if(a_amv_mac_is_closing_angle(line.s))
470 break;
472 /* Trim WS, remember amount of leading spaces for display purposes */
473 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
474 if(*cp == '\t')
475 leaspc = (leaspc + 8u) & ~7u;
476 else if(*cp == ' ')
477 ++leaspc;
478 else
479 break;
480 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
482 if(n.ui == 0)
483 continue;
485 maxlen = n_MAX(maxlen, n.ui);
486 cp[n.ui++] = '\0';
488 if(n_LIKELY(++line_cnt < UI32_MAX)){
489 struct a_amv_mac_line *amlp;
491 llp = salloc(sizeof *llp);
492 if(ll_head == NULL)
493 ll_head = llp;
494 else
495 ll_tail->ll_next = llp;
496 ll_tail = llp;
497 llp->ll_next = NULL;
498 llp->ll_amlp = amlp = smalloc(sizeof(*amlp) -
499 n_VFIELD_SIZEOF(struct a_amv_mac_line, aml_dat) + n.ui);
500 amlp->aml_len = n.ui -1;
501 amlp->aml_prespc = leaspc;
502 memcpy(amlp->aml_dat, cp, n.ui);
503 }else{
504 n_err(_("Too much content in %s definition: %s\n"),
505 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
506 goto jerr;
510 /* Create the new macro */
511 n.s = strlen(name) +1;
512 amp = smalloc(sizeof(*amp) - n_VFIELD_SIZEOF(struct a_amv_mac, am_name) +
513 n.s);
514 amp->am_next = NULL;
515 amp->am_maxlen = maxlen;
516 amp->am_line_cnt = line_cnt;
517 amp->am_flags = amf;
518 amp->am_lopts = NULL;
519 memcpy(amp->am_name, name, n.s);
520 /* C99 */{
521 struct a_amv_mac_line **amlpp;
523 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
524 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
525 *amlpp++ = llp->ll_amlp;
526 *amlpp = NULL;
529 /* Finally check whether such a macro already exists, in which case we throw
530 * it all away again. At least we know it would have worked */
531 if(a_amv_mac_lookup(name, amp, amf) != NULL){
532 n_err(_("There is already a %s of name: %s\n"),
533 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
534 goto jerr;
537 rv = TRU1;
538 jleave:
539 if(line.s != NULL)
540 free(line.s);
541 NYD2_LEAVE;
542 return rv;
544 jerr:
545 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
546 free(llp->ll_amlp);
547 if(amp != NULL){
548 free(amp->am_line_dat);
549 free(amp);
551 goto jleave;
554 static bool_t
555 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
556 struct a_amv_mac *amp;
557 bool_t rv;
558 NYD2_ENTER;
560 rv = TRU1;
562 if(n_LIKELY(name[0] != '*' || name[1] != '\0')){
563 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
564 n_err(_("%s not defined: %s\n"),
565 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
566 rv = FAL0;
568 }else{
569 struct a_amv_mac **ampp, *lamp;
571 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
572 ++ampp)
573 for(lamp = NULL, amp = *ampp; amp != NULL;){
574 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
575 /* xxx Expensive but rare: be simple */
576 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
577 amp = (lamp == NULL) ? *ampp : lamp->am_next;
578 }else{
579 lamp = amp;
580 amp = amp->am_next;
584 NYD2_LEAVE;
585 return rv;
588 static void
589 a_amv_mac_free(struct a_amv_mac *amp){
590 struct a_amv_mac_line **amlpp;
591 NYD2_ENTER;
593 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
594 free(*amlpp);
595 free(amp->am_line_dat);
596 free(amp);
597 NYD2_LEAVE;
600 static void
601 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
602 struct a_amv_var *oavp){
603 struct a_amv_var *avp;
604 size_t nl, vl;
605 NYD2_ENTER;
607 /* Propagate unrolling up the stack, as necessary */
608 assert(alp != NULL);
609 for(;;){
610 if(alp->as_unroll)
611 break;
612 if((alp = alp->as_up) == NULL)
613 goto jleave;
616 /* Check whether this variable is handled yet */
617 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
618 if(!strcmp(avp->av_name, name))
619 goto jleave;
621 nl = strlen(name) +1;
622 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
623 avp = smalloc(sizeof(*avp) - n_VFIELD_SIZEOF(struct a_amv_var, av_name) +
624 nl + vl);
625 avp->av_link = alp->as_lopts;
626 alp->as_lopts = avp;
627 memcpy(avp->av_name, name, nl);
628 if(vl == 0){
629 avp->av_value = NULL;
630 avp->av_flags = 0;
631 #ifdef HAVE_PUTENV
632 avp->av_env = NULL;
633 #endif
634 }else{
635 avp->av_value = &avp->av_name[nl];
636 avp->av_flags = oavp->av_flags;
637 memcpy(avp->av_value, oavp->av_value, vl);
638 #ifdef HAVE_PUTENV
639 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
640 #endif
642 jleave:
643 NYD2_LEAVE;
646 static void
647 a_amv_lopts_unroll(struct a_amv_var **avpp){
648 struct a_amv_lostack *save_alp;
649 bool_t reset;
650 struct a_amv_var *x, *avp;
651 NYD2_ENTER;
653 avp = *avpp;
654 *avpp = NULL;
655 reset = !(pstate & PS_ROOT);
657 save_alp = a_amv_lopts;
658 a_amv_lopts = NULL;
659 while(avp != NULL){
660 x = avp;
661 avp = avp->av_link;
662 pstate |= PS_ROOT;
663 vok_vset(x->av_name, x->av_value);
664 if(reset)
665 pstate &= ~PS_ROOT;
666 free(x);
668 a_amv_lopts = save_alp;
669 NYD2_LEAVE;
672 static char *
673 a_amv_var_copy(char const *str){
674 char *news;
675 size_t len;
676 NYD2_ENTER;
678 if(*str == '\0')
679 news = n_UNCONST("");
680 else if(str[1] == '\0'){
681 if(str[0] == '1')
682 news = n_UNCONST(a_amv_var_1);
683 else if(str[0] == '0')
684 news = n_UNCONST(a_amv_var_0);
685 else
686 goto jheap;
687 }else{
688 jheap:
689 len = strlen(str) +1;
690 news = smalloc(len);
691 memcpy(news, str, len);
693 NYD2_LEAVE;
694 return news;
697 static void
698 a_amv_var_free(char *cp){
699 NYD2_ENTER;
700 if(cp[0] != '\0' && cp != a_amv_var_1 && cp != a_amv_var_0)
701 free(cp);
702 NYD2_LEAVE;
705 static bool_t
706 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
707 int flag;
708 bool_t ok;
709 NYD2_ENTER;
711 ok = TRU1;
712 flag = 0;
714 switch(okey){
715 case ok_b_debug:
716 flag = OPT_DEBUG;
717 break;
718 case ok_v_HOME:
719 /* Invalidate any resolved folder then, too
720 * FALLTHRU */
721 case ok_v_folder:
722 ok = !(pstate & PS_ROOT);
723 pstate |= PS_ROOT;
724 ok_vclear(_folder_resolved);
725 if(ok)
726 pstate &= ~PS_ROOT;
727 ok = TRU1;
728 break;
729 case ok_b_header:
730 flag = OPT_N_FLAG;
731 enable = !enable;
732 break;
733 case ok_b_memdebug:
734 flag = OPT_MEMDEBUG;
735 break;
736 case ok_b_skipemptybody:
737 flag = OPT_E_FLAG;
738 break;
739 case ok_b_typescript_mode:
740 if(enable){
741 ok_bset(colour_disable);
742 ok_bset(line_editor_disable);
743 if(!(pstate & PS_STARTED))
744 ok_bset(termcap_disable);
746 case ok_v_umask:
747 assert(enable);
748 if(**val != '\0'){
749 ul_i ul;
751 if((ul = strtoul(*val, NULL, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
752 n_err(_("Invalid *umask* setting: %s\n"), *val);
753 ok = FAL0;
754 }else
755 umask((mode_t)ul);
757 break;
758 case ok_b_verbose:
759 flag = (enable && !(options & OPT_VERB))
760 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
761 break;
762 default:
763 DBG( n_err("Implementation error: never heard of %u\n", ok); )
764 break;
767 if(flag){
768 if(enable)
769 options |= flag;
770 else
771 options &= ~flag;
773 NYD2_LEAVE;
774 return ok;
777 static bool_t
778 a_amv_var_check_nocntrls(char const *val){
779 char c;
780 NYD2_ENTER;
782 while((c = *val++) != '\0')
783 if(cntrlchar(c))
784 break;
785 NYD2_LEAVE;
786 return (c == '\0');
789 static bool_t
790 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
791 /* TODO The internal/environment variables which are num= or posnum= should
792 * TODO gain special lookup functions, or the return should be void* and
793 * TODO castable to integer; i.e. no more strtoX() should be needed.
794 * TODO I.e., the result of this function should instead be stored.
795 * TODO Use intmax_t IF that is sizeof(void*) only? */
796 bool_t rv;
797 NYD2_ENTER;
799 rv = TRU1;
801 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
802 char *eptr;
803 union {long s; unsigned long u;} i;
805 if(!pos){
806 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
808 if(*eptr != '\0' ||
809 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
810 rv = FAL0;
811 #if INT_MIN != LONG_MIN
812 else if(i.s < INT_MIN)
813 rv = FAL0;
814 #endif
815 #if INT_MAX != LONG_MAX
816 else if(i.s > INT_MAX)
817 rv = FAL0;
818 #endif
819 }else{
820 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
822 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
823 rv = FAL0;
824 #if UINT_MAX != ULONG_MAX
825 else if(i.u > UINT_MAX)
826 rv = FAL0;
827 #endif
830 NYD2_LEAVE;
831 return rv;
834 static char const *
835 a_amv_var_canonify(char const *vn){
836 NYD2_ENTER;
837 if(!upperchar(*vn)){
838 char const *vp;
840 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
842 vn = (*vp == '@') ? i_strdup(vn) : vn;
844 NYD2_LEAVE;
845 return vn;
848 static bool_t
849 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
850 ui32_t hash, i, j;
851 struct a_amv_var_map const *avmp;
852 NYD2_ENTER;
854 avcp->avc_name = name = a_amv_var_canonify(name);
855 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
857 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
858 ui32_t x;
860 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
861 break;
863 avmp = &a_amv_var_map[x];
864 if(avmp->avm_hash == hash &&
865 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
866 avcp->avc_map = avmp;
867 avcp->avc_okey = (enum okeys)x;
868 goto jleave;
871 if(++i == a_AMV_VAR_REV_PRIME){
872 #ifdef a_AMV_VAR_REV_WRAPAROUND
873 i = 0;
874 #else
875 break;
876 #endif
879 avcp->avc_map = NULL;
880 avcp = NULL;
881 jleave:
882 NYD2_LEAVE;
883 return (avcp != NULL);
886 static bool_t
887 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
888 size_t i;
889 char const *cp;
890 struct a_amv_var_map const *avmp;
891 struct a_amv_var *avp;
892 NYD2_ENTER;
894 /* C99 */{
895 struct a_amv_var **avpp, *lavp;
897 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
899 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
900 if(!strcmp(avp->av_name, avcp->avc_name)){
901 /* Relink as head, hope it "sorts on usage" over time.
902 * _clear() relies on this behaviour */
903 if(lavp != NULL){
904 lavp->av_link = avp->av_link;
905 avp->av_link = *avpp;
906 *avpp = avp;
908 goto jleave;
912 /* If this is not an assembled variable we need to consider some special
913 * initialization cases and eventually create the variable anew */
914 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
915 /* Does it have an import-from-environment flag? */
916 if(n_UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
917 if(n_LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
918 /* May be better not to use that one, though? */
919 bool_t isempty, isbltin;
921 isempty = (*cp == '\0' &&
922 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
923 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
924 ) != 0);
926 if(n_UNLIKELY(isempty)){
927 if(!isbltin)
928 goto jerr;
929 }else if(n_LIKELY(*cp != '\0')){
930 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
931 !a_amv_var_check_nocntrls(cp))){
932 n_err(_("Ignoring environment, control characters "
933 "invalid in variable: %s\n"), avcp->avc_name);
934 goto jerr;
936 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
937 !a_amv_var_check_num(cp, FAL0))){
938 n_err(_("Environment variable value not a number "
939 "or out of range: %s\n"), avcp->avc_name);
940 goto jerr;
942 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
943 !a_amv_var_check_num(cp, TRU1))){
944 n_err(_("Environment variable value not a number, "
945 "negative or out of range: %s\n"), avcp->avc_name);
946 goto jerr;
949 goto jnewval;
953 /* A first-time init switch is to be handled now and here */
954 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
955 static struct a_amv_var_defval const **arr,
956 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
958 if(arr == NULL){
959 arr = &arr_base[0];
960 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
961 while(i-- > 0)
962 arr[i] = &a_amv_var_i3vals[i];
965 for(i = 0; arr[i] != NULL; ++i)
966 if(arr[i]->avdv_okey == avcp->avc_okey){
967 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
968 : arr[i]->avdv_value;
969 /* Remove this entry, hope entire block becomes no-op asap */
971 arr[i] = arr[i + 1];
972 while(arr[i++] != NULL);
974 if(!i3val_nonew)
975 goto jnewval;
976 if(i3val_nonew == TRUM1)
977 avp = (struct a_amv_var*)-1;
978 goto jleave;
982 /* The virtual variables */
983 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
984 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
985 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
986 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
987 goto jleave;
989 /* Not reached */
992 /* Place this last because once it is set first the variable will never
993 * be removed again and thus match in the first block above */
994 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
995 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
996 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
997 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? ""
998 : a_amv_var_defvals[i].avdv_value;
999 goto jnewval;
1004 jerr:
1005 avp = NULL;
1006 jleave:
1007 avcp->avc_var = avp;
1008 NYD2_LEAVE;
1009 return (avp != NULL);
1011 jnewval: /* C99 */{
1012 struct a_amv_var **avpp;
1013 size_t l;
1015 l = strlen(avcp->avc_name) +1;
1016 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1017 n_VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1018 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1019 *avpp = avp;
1020 memcpy(avp->av_name, avcp->avc_name, l);
1021 avp->av_value = a_amv_var_copy(cp);
1022 #ifdef HAVE_PUTENV
1023 avp->av_env = NULL;
1024 #endif
1025 avp->av_flags = avmp->avm_flags;
1027 if(avp->av_flags & a_AMV_VF_VIP)
1028 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1029 if(avp->av_flags & a_AMV_VF_ENV)
1030 a_amv_var__putenv(avcp, avp);
1031 goto jleave;
1035 static bool_t
1036 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1037 bool_t force_env){
1038 struct a_amv_var *avp;
1039 char *oval;
1040 struct a_amv_var_map const *avmp;
1041 bool_t rv;
1042 NYD2_ENTER;
1044 if(value == NULL){
1045 rv = a_amv_var_clear(avcp, force_env);
1046 goto jleave;
1049 if((avmp = avcp->avc_map) != NULL){
1050 rv = FAL0;
1052 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1053 !(pstate & PS_ROOT))){
1054 value = N_("Variable is readonly: %s\n");
1055 goto jeavmp;
1057 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1058 value = N_("Variable must not be empty: %s\n");
1059 goto jeavmp;
1061 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1062 !a_amv_var_check_nocntrls(value))){
1063 value = N_("Variable forbids control characters: %s\n");
1064 goto jeavmp;
1066 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1067 !a_amv_var_check_num(value, FAL0))){
1068 value = N_("Variable value not a number or out of range: %s\n");
1069 goto jeavmp;
1071 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1072 !a_amv_var_check_num(value, TRU1))){
1073 value = _("Variable value not a number, negative, "
1074 "or out of range: %s\n");
1075 goto jeavmp;
1077 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1078 !(pstate & (PS_ROOT | PS_STARTED)))){
1079 value = N_("Variable cannot be set in a resource file: %s\n");
1080 jeavmp:
1081 n_err(V_(value), avcp->avc_name);
1082 goto jleave;
1086 rv = TRU1;
1087 a_amv_var_lookup(avcp, TRU1);
1089 /* Don't care what happens later on, store this in the unroll list */
1090 if(a_amv_lopts != NULL &&
1091 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1092 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1094 if((avp = avcp->avc_var) == NULL){
1095 struct a_amv_var **avpp;
1096 size_t l;
1098 l = strlen(avcp->avc_name) +1;
1099 avcp->avc_var = avp = smalloc(sizeof(*avp) -
1100 n_VFIELD_SIZEOF(struct a_amv_var, av_name) + l);
1101 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1102 *avpp = avp;
1103 #ifdef HAVE_PUTENV
1104 avp->av_env = NULL;
1105 #endif
1106 memcpy(avp->av_name, avcp->avc_name, l);
1107 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1108 oval = n_UNCONST("");
1109 }else
1110 oval = avp->av_value;
1112 if(avmp == NULL)
1113 avp->av_value = a_amv_var_copy(value);
1114 else{
1115 /* Via `set' etc. the user may give even boolean options non-boolean
1116 * values, ignore that and force boolean */
1117 if(avp->av_flags & a_AMV_VF_BOOL){
1118 if((options & OPT_D_VV) && *value != '\0')
1119 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1120 avcp->avc_name, value);
1121 avp->av_value = n_UNCONST(a_amv_var_1);
1122 }else
1123 avp->av_value = a_amv_var_copy(value);
1125 /* Check if update allowed XXX wasteful on error! */
1126 if((avp->av_flags & a_AMV_VF_VIP) &&
1127 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1128 char *cp = avp->av_value;
1130 avp->av_value = oval;
1131 oval = cp;
1135 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1136 avp->av_flags |= a_AMV_VF_LINKED;
1137 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1138 rv = a_amv_var__putenv(avcp, avp);
1140 a_amv_var_free(oval);
1141 jleave:
1142 NYD2_LEAVE;
1143 return rv;
1146 static bool_t
1147 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1148 #ifndef HAVE_SETENV
1149 char *cp;
1150 #endif
1151 bool_t rv;
1152 NYD2_ENTER;
1154 #ifdef HAVE_SETENV
1155 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1156 #else
1157 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1159 if((rv = (putenv(cp) == 0))){
1160 char *ocp;
1162 if((ocp = avp->av_env) != NULL)
1163 free(ocp);
1164 avp->av_env = cp;
1165 }else
1166 free(cp);
1167 #endif
1168 NYD2_LEAVE;
1169 return rv;
1172 static bool_t
1173 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1174 struct a_amv_var **avpp, *avp;
1175 struct a_amv_var_map const *avmp;
1176 bool_t rv;
1177 NYD2_ENTER;
1179 rv = FAL0;
1181 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1182 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1183 !(pstate & PS_ROOT))){
1184 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1185 goto jleave;
1187 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1188 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1189 goto jleave;
1192 rv = TRU1;
1194 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1195 if(force_env){
1196 jforce_env:
1197 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1198 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1199 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1200 goto jleave;
1201 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1202 avcp->avc_var = NULL;
1203 if(force_env)
1204 goto jforce_env;
1205 goto jleave;
1208 if(a_amv_lopts != NULL &&
1209 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1210 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1212 avp = avcp->avc_var;
1213 avcp->avc_var = NULL;
1214 avpp = &a_amv_vars[avcp->avc_prime];
1215 assert(*avpp == avp); /* (always listhead after lookup()) */
1216 *avpp = (*avpp)->av_link;
1218 /* C99 */{
1219 #ifdef HAVE_SETENV
1220 char *envval = NULL;
1221 #else
1222 char *envval = avp->av_env;
1223 #endif
1224 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1225 rv = a_amv_var__clearenv(avp->av_name, envval);
1227 a_amv_var_free(avp->av_value);
1228 free(avp);
1230 /* XXX Fun part, extremely simple-minded for now: if this variable has
1231 * XXX a default value, immediately reinstantiate it! */
1232 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1233 a_amv_var_lookup(avcp, TRU1);
1234 jleave:
1235 NYD2_LEAVE;
1236 return rv;
1239 static bool_t
1240 a_amv_var__clearenv(char const *name, char *value){
1241 #ifndef HAVE_SETENV
1242 extern char **environ;
1243 char **ecpp;
1244 #endif
1245 bool_t rv;
1246 NYD2_ENTER;
1247 n_UNUSED(value);
1249 #ifdef HAVE_SETENV
1250 unsetenv(name);
1251 rv = TRU1;
1252 #else
1253 if(value != NULL)
1254 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1255 if(*ecpp == value){
1256 free(value);
1258 ecpp[0] = ecpp[1];
1259 while(*ecpp++ != NULL);
1260 break;
1262 rv = TRU1;
1263 #endif
1264 NYD2_LEAVE;
1265 return rv;
1268 static void
1269 a_amv_var_show_all(void){
1270 struct n_string msg, *msgp;
1271 FILE *fp;
1272 size_t no, i;
1273 struct a_amv_var *avp;
1274 char const **vacp, **cap;
1275 NYD2_ENTER;
1277 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1278 n_perr(_("Can't create temporary file for `set' listing"), 0);
1279 goto jleave;
1282 /* We need to instantiate first-time-inits and default values here, so that
1283 * they will be regular members of our _vars[] table */
1284 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1285 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1286 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1287 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1289 for(no = i = 0; i < a_AMV_PRIME; ++i)
1290 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1291 ++no;
1292 no += a_AMV_VAR_VIRTS_CNT;
1294 vacp = salloc(no * sizeof(*vacp));
1296 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1297 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1298 *cap++ = avp->av_name;
1299 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1300 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1302 if(no > 1)
1303 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1305 msgp = &msg;
1306 msgp = n_string_reserve(n_string_creat(msgp), 80);
1307 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1308 i += a_amv_var_show(*cap, fp, msgp);
1309 n_string_gut(&msg);
1311 page_or_print(fp, i);
1312 Fclose(fp);
1313 jleave:
1314 NYD2_LEAVE;
1317 static int
1318 a_amv_var__show_cmp(void const *s1, void const *s2){
1319 int rv;
1320 NYD2_ENTER;
1322 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1323 NYD2_LEAVE;
1324 return rv;
1327 static size_t
1328 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1329 struct a_amv_var_carrier avc;
1330 char const *quote;
1331 size_t i;
1332 NYD2_ENTER;
1334 msgp = n_string_trunc(msgp, 0);
1335 i = 0;
1337 a_amv_var_revlookup(&avc, name);
1338 if(!a_amv_var_lookup(&avc, FAL0)){
1339 struct str s;
1341 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1342 s.s = n_UNCONST(name);
1343 s.l = UIZ_MAX;
1344 msgp = n_shexp_quote(msgp, &s, FAL0);
1345 goto jleave;
1348 if(options & OPT_D_V){
1349 if(avc.avc_map == NULL){
1350 msgp = n_string_push_c(msgp, '#');
1351 msgp = n_string_push_cp(msgp, "assembled");
1352 i = 1;
1354 /* C99 */{
1355 struct{
1356 ui16_t flags;
1357 char msg[22];
1358 } const tbase[] = {
1359 {a_AMV_VF_VIRT, "virtual"},
1360 {a_AMV_VF_RDONLY, "readonly"},
1361 {a_AMV_VF_NODEL, "nodelete"},
1362 {a_AMV_VF_NOTEMPTY, "notempty"},
1363 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1364 {a_AMV_VF_NUM, "number"},
1365 {a_AMV_VF_POSNUM, "positive-number"},
1366 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1367 {a_AMV_VF_ENV, "sync-environ"},
1368 {a_AMV_VF_I3VAL, "initial-value"},
1369 {a_AMV_VF_DEFVAL, "default-value"},
1370 {a_AMV_VF_LINKED, "`environ' link"}
1371 }, *tp;
1373 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1374 if(avc.avc_var->av_flags & tp->flags){
1375 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1376 msgp = n_string_push_cp(msgp, tp->msg);
1380 if(i > 0)
1381 msgp = n_string_push_cp(msgp, "\n ");
1384 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1385 msgp = n_string_push_cp(msgp, "# ");
1386 n_UNINIT(quote, NULL);
1387 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1388 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1389 if(strcmp(quote, avc.avc_var->av_value))
1390 msgp = n_string_push_cp(msgp, "wysh ");
1392 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1393 msgp = n_string_push_cp(msgp, "environ ");
1394 msgp = n_string_push_cp(msgp, "set ");
1395 msgp = n_string_push_cp(msgp, name);
1396 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1397 msgp = n_string_push_c(msgp, '=');
1398 msgp = n_string_push_cp(msgp, quote);
1401 jleave:
1402 msgp = n_string_push_c(msgp, '\n');
1403 fputs(n_string_cp(msgp), fp);
1404 NYD2_ENTER;
1405 return (i > 0 ? 2 : 1);
1408 static bool_t
1409 a_amv_var_c_set(char **ap, bool_t issetenv){
1410 char *cp, *cp2, *varbuf, c;
1411 size_t errs;
1412 NYD2_ENTER;
1414 errs = 0;
1415 jouter:
1416 while((cp = *ap++) != NULL){
1417 /* Isolate key */
1418 cp2 = varbuf = salloc(strlen(cp) +1);
1420 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1421 if(cntrlchar(c) || whitechar(c)){
1422 n_err(_("Variable name with control character ignored: %s\n"),
1423 ap[-1]);
1424 ++errs;
1425 goto jouter;
1427 *cp2++ = c;
1429 *cp2 = '\0';
1430 if(c == '\0')
1431 cp = n_UNCONST("");
1432 else
1433 ++cp;
1435 if(varbuf == cp2){
1436 n_err(_("Empty variable name ignored\n"));
1437 ++errs;
1438 }else{
1439 struct a_amv_var_carrier avc;
1440 bool_t isunset;
1442 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1443 varbuf = &varbuf[2];
1445 a_amv_var_revlookup(&avc, varbuf);
1447 if(isunset)
1448 errs += !a_amv_var_clear(&avc, issetenv);
1449 else
1450 errs += !a_amv_var_set(&avc, cp, issetenv);
1453 NYD2_LEAVE;
1454 return (errs == 0);
1457 FL int
1458 c_define(void *v){
1459 int rv;
1460 char **args;
1461 NYD_ENTER;
1463 rv = 1;
1465 if((args = v)[0] == NULL){
1466 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1467 goto jleave;
1470 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1471 args[2] != NULL){
1472 n_err(_("Synopsis: define: <name> {\n"));
1473 goto jleave;
1476 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1477 jleave:
1478 NYD_LEAVE;
1479 return rv;
1482 FL int
1483 c_undefine(void *v){
1484 int rv;
1485 char **args;
1486 NYD_ENTER;
1488 rv = 0;
1489 args = v;
1491 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1492 while(*++args != NULL);
1493 NYD_LEAVE;
1494 return rv;
1497 FL int
1498 c_call(void *v){
1499 struct a_amv_mac_call_args amca;
1500 char const *errs, *name;
1501 struct a_amv_mac *amp;
1502 char **args;
1503 int rv;
1504 NYD_ENTER;
1506 rv = 1;
1508 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1509 errs = _("Synopsis: call: <%s>\n");
1510 name = "name";
1511 goto jerr;
1514 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1515 errs = _("Undefined macro `call'ed: %s\n");
1516 goto jerr;
1519 memset(&amca, 0, sizeof amca);
1520 amca.amca_name = name;
1521 amca.amca_amp = amp;
1522 rv = (a_amv_mac_exec(&amca) == FAL0);
1523 jleave:
1524 NYD_LEAVE;
1525 return rv;
1526 jerr:
1527 n_err(errs, name);
1528 goto jleave;
1531 FL bool_t
1532 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1533 struct a_amv_mac_call_args amca;
1534 struct a_amv_mac *amp;
1535 size_t len;
1536 char *var, *cp;
1537 bool_t rv;
1538 NYD_ENTER;
1540 rv = TRU1;
1541 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1543 /* First try the fully resolved path */
1544 snprintf(var, len, "folder-hook-%s", mailname);
1545 if((cp = vok_vlook(var)) != NULL)
1546 goto jmac;
1548 /* If we are under *folder*, try the usual +NAME syntax, too */
1549 if(displayname[0] == '+'){
1550 char *x;
1552 for(x = &mailname[len]; x != mailname; --x)
1553 if(x[-1] == '/'){
1554 snprintf(var, len, "folder-hook-+%s", x);
1555 if((cp = vok_vlook(var)) != NULL)
1556 goto jmac;
1557 break;
1561 /* Plain *folder-hook* is our last try */
1562 if((cp = ok_vlook(folder_hook)) == NULL)
1563 goto jleave;
1565 jmac:
1566 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1567 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1568 n_shexp_quote_cp(displayname, FAL0), cp);
1569 rv = FAL0;
1570 goto jleave;
1573 memset(&amca, 0, sizeof amca);
1574 amca.amca_name = cp;
1575 amca.amca_amp = amp;
1576 pstate &= ~PS_HOOK_MASK;
1577 if(nmail){
1578 amca.amca_unroller = NULL;
1579 pstate |= PS_HOOK_NEWMAIL;
1580 }else{
1581 amca.amca_unroller = &a_amv_folder_hook_lopts;
1582 pstate |= PS_HOOK;
1584 amca.amca_lopts_on = TRU1;
1585 rv = a_amv_mac_exec(&amca);
1586 pstate &= ~PS_HOOK_MASK;
1588 jleave:
1589 NYD_LEAVE;
1590 return rv;
1593 FL void
1594 call_compose_mode_hook(char const *macname, /* TODO temporary, v15: drop */
1595 void (*hook_pre)(void *), void *hook_arg){
1596 struct a_amv_mac_call_args amca;
1597 struct a_amv_mac *amp;
1598 NYD_ENTER;
1600 if((amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1601 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1602 macname);
1603 else{
1604 memset(&amca, 0, sizeof amca);
1605 amca.amca_name = macname;
1606 amca.amca_amp = amp;
1607 amca.amca_unroller = &a_amv_compose_lopts;
1608 amca.amca_hook_pre = hook_pre;
1609 amca.amca_hook_arg = hook_arg;
1610 amca.amca_lopts_on = TRU1;
1611 pstate &= ~PS_HOOK_MASK;
1612 pstate |= PS_HOOK;
1613 a_amv_mac_exec(&amca);
1614 pstate &= ~PS_HOOK_MASK;
1616 NYD_LEAVE;
1619 FL int
1620 c_account(void *v){
1621 struct a_amv_mac_call_args amca;
1622 struct a_amv_mac *amp;
1623 char **args;
1624 int rv, i, oqf, nqf;
1625 NYD_ENTER;
1627 rv = 1;
1629 if((args = v)[0] == NULL){
1630 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1631 goto jleave;
1634 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1635 if(args[2] != NULL){
1636 n_err(_("Synopsis: account: <name> {\n"));
1637 goto jleave;
1639 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1640 n_err(_("`account': cannot use reserved name: %s\n"),
1641 ACCOUNT_NULL);
1642 goto jleave;
1644 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1645 goto jleave;
1648 if(pstate & PS_HOOK_MASK){
1649 n_err(_("`account': can't change account from within a hook\n"));
1650 goto jleave;
1653 save_mbox_for_possible_quitstuff();
1655 amp = NULL;
1656 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1657 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1658 n_err(_("`account': account does not exist: %s\n"), args[0]);
1659 goto jleave;
1662 oqf = savequitflags();
1664 if(a_amv_acc_curr != NULL){
1665 if(a_amv_acc_curr->am_lopts != NULL)
1666 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1667 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1668 a_amv_mac_free(a_amv_acc_curr);
1671 account_name = (amp != NULL) ? amp->am_name : NULL;
1672 a_amv_acc_curr = amp;
1674 if(amp != NULL){
1675 assert(amp->am_lopts == NULL);
1676 memset(&amca, 0, sizeof amca);
1677 amca.amca_name = amp->am_name;
1678 amca.amca_amp = amp;
1679 amca.amca_unroller = &amp->am_lopts;
1680 amca.amca_lopts_on = TRU1;
1681 if(!a_amv_mac_exec(&amca)){
1682 /* XXX account switch incomplete, unroll? */
1683 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1684 goto jleave;
1688 /* C99 */{
1689 bool_t reset = !(pstate & PS_ROOT);
1691 pstate |= PS_ROOT;
1692 if(amp != NULL)
1693 ok_vset(_account_name, amp->am_name);
1694 else
1695 ok_vclear(_account_name);
1696 if(reset)
1697 pstate &= ~PS_ROOT;
1700 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1701 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1702 restorequitflags(oqf);
1703 if((i = setfile("%", 0)) < 0)
1704 goto jleave;
1705 check_folder_hook(FAL0);
1706 if(i > 0 && !ok_blook(emptystart))
1707 goto jleave;
1708 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1709 restorequitflags(nqf);
1711 rv = 0;
1712 jleave:
1713 NYD_LEAVE;
1714 return rv;
1717 FL int
1718 c_unaccount(void *v){
1719 int rv;
1720 char **args;
1721 NYD_ENTER;
1723 rv = 0;
1724 args = v;
1726 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1727 while(*++args != NULL);
1728 NYD_LEAVE;
1729 return rv;
1732 FL int
1733 c_localopts(void *v){
1734 char **argv;
1735 int rv;
1736 NYD_ENTER;
1738 rv = 1;
1740 if(a_amv_lopts == NULL){
1741 n_err(_("Cannot use `localopts' but from within a "
1742 "`define' or `account'\n"));
1743 goto jleave;
1746 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1747 rv = 0;
1748 jleave:
1749 NYD_LEAVE;
1750 return rv;
1753 FL void
1754 temporary_localopts_free(void){ /* XXX intermediate hack */
1755 NYD_ENTER;
1756 if(a_amv_compose_lopts != NULL){
1757 void *save = a_amv_lopts;
1759 a_amv_lopts = NULL;
1760 a_amv_lopts_unroll(&a_amv_compose_lopts);
1761 a_amv_compose_lopts = NULL;
1762 a_amv_lopts = save;
1764 NYD_LEAVE;
1767 FL void
1768 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1769 NYD_ENTER;
1770 if(a_amv_folder_hook_lopts != NULL){
1771 void *save = a_amv_lopts;
1773 a_amv_lopts = NULL;
1774 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1775 a_amv_folder_hook_lopts = NULL;
1776 a_amv_lopts = save;
1778 NYD_LEAVE;
1781 FL char *
1782 n_var_oklook(enum okeys okey){
1783 struct a_amv_var_carrier avc;
1784 char *rv;
1785 struct a_amv_var_map const *avmp;
1786 NYD_ENTER;
1788 avc.avc_map = avmp = &a_amv_var_map[okey];
1789 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1790 avc.avc_hash = avmp->avm_hash;
1791 avc.avc_okey = okey;
1793 if(a_amv_var_lookup(&avc, FAL0))
1794 rv = avc.avc_var->av_value;
1795 else
1796 rv = NULL;
1797 NYD_LEAVE;
1798 return rv;
1801 FL bool_t
1802 n_var_okset(enum okeys okey, uintptr_t val){
1803 struct a_amv_var_carrier avc;
1804 bool_t ok;
1805 struct a_amv_var_map const *avmp;
1806 NYD_ENTER;
1808 avc.avc_map = avmp = &a_amv_var_map[okey];
1809 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1810 avc.avc_hash = avmp->avm_hash;
1811 avc.avc_okey = okey;
1813 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1814 NYD_LEAVE;
1815 return ok;
1818 FL bool_t
1819 n_var_okclear(enum okeys okey){
1820 struct a_amv_var_carrier avc;
1821 bool_t rv;
1822 struct a_amv_var_map const *avmp;
1823 NYD_ENTER;
1825 avc.avc_map = avmp = &a_amv_var_map[okey];
1826 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1827 avc.avc_hash = avmp->avm_hash;
1828 avc.avc_okey = okey;
1830 rv = a_amv_var_clear(&avc, FAL0);
1831 NYD_LEAVE;
1832 return rv;
1835 FL char *
1836 n_var_voklook(char const *vokey){
1837 struct a_amv_var_carrier avc;
1838 char *rv;
1839 NYD_ENTER;
1841 a_amv_var_revlookup(&avc, vokey);
1843 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1844 NYD_LEAVE;
1845 return rv;
1848 FL bool_t
1849 n_var_vokset(char const *vokey, uintptr_t val){
1850 struct a_amv_var_carrier avc;
1851 bool_t ok;
1852 NYD_ENTER;
1854 a_amv_var_revlookup(&avc, vokey);
1856 ok = a_amv_var_set(&avc, (val == 0x1 ? "" : (char const*)val), FAL0);
1857 NYD_LEAVE;
1858 return ok;
1861 FL bool_t
1862 n_var_vokclear(char const *vokey){
1863 struct a_amv_var_carrier avc;
1864 bool_t ok;
1865 NYD_ENTER;
1867 a_amv_var_revlookup(&avc, vokey);
1869 ok = a_amv_var_clear(&avc, FAL0);
1870 NYD_LEAVE;
1871 return ok;
1874 #ifdef HAVE_SOCKETS
1875 FL char *
1876 n_var_xoklook(enum okeys okey, struct url const *urlp,
1877 enum okey_xlook_mode oxm){
1878 struct a_amv_var_carrier avc;
1879 struct str const *us;
1880 size_t nlen;
1881 char *nbuf, *rv;
1882 struct a_amv_var_map const *avmp;
1883 NYD_ENTER;
1885 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1887 /* For simplicity: allow this case too */
1888 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
1889 nbuf = NULL;
1890 goto jplain;
1893 avc.avc_map = avmp = &a_amv_var_map[okey];
1894 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1895 avc.avc_okey = okey;
1897 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1898 nlen = strlen(avc.avc_name);
1899 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
1900 memcpy(nbuf, avc.avc_name, nlen);
1901 nbuf[nlen++] = '-';
1903 /* One of .url_u_h_p and .url_h_p we test in here */
1904 memcpy(nbuf + nlen, us->s, us->l +1);
1905 avc.avc_name = a_amv_var_canonify(nbuf);
1906 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1907 if(a_amv_var_lookup(&avc, FAL0))
1908 goto jvar;
1910 /* The second */
1911 if(oxm & OXM_H_P){
1912 us = &urlp->url_h_p;
1913 memcpy(nbuf + nlen, us->s, us->l +1);
1914 avc.avc_name = a_amv_var_canonify(nbuf);
1915 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1916 if(a_amv_var_lookup(&avc, FAL0)){
1917 jvar:
1918 rv = avc.avc_var->av_value;
1919 goto jleave;
1923 jplain:
1924 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
1925 jleave:
1926 if(nbuf != NULL)
1927 n_lofi_free(nbuf);
1928 NYD_LEAVE;
1929 return rv;
1931 #endif /* HAVE_SOCKETS */
1933 FL int
1934 c_set(void *v){
1935 char **ap;
1936 int err;
1937 NYD_ENTER;
1939 if(*(ap = v) == NULL){
1940 a_amv_var_show_all();
1941 err = 0;
1942 }else
1943 err = !a_amv_var_c_set(ap, FAL0);
1944 NYD_LEAVE;
1945 return err;
1948 FL int
1949 c_unset(void *v){
1950 char **ap;
1951 int err;
1952 NYD_ENTER;
1954 for(err = 0, ap = v; *ap != NULL; ++ap)
1955 err |= !n_var_vokclear(*ap);
1956 NYD_LEAVE;
1957 return err;
1960 FL int
1961 c_varshow(void *v){
1962 char **ap;
1963 NYD_ENTER;
1965 if(*(ap = v) == NULL)
1966 v = NULL;
1967 else{
1968 struct n_string msg, *msgp = &msg;
1970 msgp = n_string_creat(msgp);
1971 for(; *ap != NULL; ++ap)
1972 a_amv_var_show(*ap, stdout, msgp);
1973 n_string_gut(msgp);
1975 NYD_LEAVE;
1976 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
1979 FL int
1980 c_varedit(void *v){
1981 struct a_amv_var_carrier avc;
1982 FILE *of, *nf;
1983 char *val, **argv;
1984 int err;
1985 sighandler_type sigint;
1986 NYD_ENTER;
1988 sigint = safe_signal(SIGINT, SIG_IGN);
1990 for(err = 0, argv = v; *argv != NULL; ++argv){
1991 memset(&avc, 0, sizeof avc);
1993 a_amv_var_revlookup(&avc, *argv);
1995 if(avc.avc_map != NULL){
1996 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
1997 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
1998 avc.avc_name);
1999 continue;
2001 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2002 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2003 avc.avc_name);
2004 continue;
2008 a_amv_var_lookup(&avc, FAL0);
2010 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2011 NULL){
2012 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2013 err = 1;
2014 break;
2015 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2016 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2017 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2018 Fclose(of);
2019 err = 1;
2020 continue;
2023 fflush_rewind(of);
2024 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2025 Fclose(of);
2027 if(nf != NULL){
2028 int c;
2029 char *base;
2030 off_t l;
2032 l = fsize(nf);
2033 assert(l >= 0);
2034 base = salloc((size_t)l +1);
2036 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2037 if(c == '\n' || c == '\r'){
2038 *val = ' ';
2039 ++l;
2040 }else{
2041 *val = (char)(uc_i)c;
2042 l = 0;
2044 val -= l;
2045 *val = '\0';
2047 if(!a_amv_var_set(&avc, base, FAL0))
2048 err = 1;
2050 Fclose(nf);
2051 }else{
2052 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2053 err = 1;
2054 break;
2058 safe_signal(SIGINT, sigint);
2059 NYD_LEAVE;
2060 return err;
2063 FL int
2064 c_environ(void *v){
2065 struct a_amv_var_carrier avc;
2066 int err;
2067 char **ap;
2068 bool_t islnk;
2069 NYD_ENTER;
2071 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2072 for(err = 0; *++ap != NULL;){
2073 a_amv_var_revlookup(&avc, *ap);
2075 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2076 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2077 if(!islnk){
2078 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2079 continue;
2080 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2081 if(options & OPT_D_V)
2082 n_err(_("`environ': link: already established: %s\n"), *ap);
2083 continue;
2085 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2086 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2087 a_amv_var__putenv(&avc, avc.avc_var);
2088 }else if(!islnk){
2089 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2090 err = 1;
2091 }else{
2092 char const *evp = getenv(*ap);
2094 if(evp != NULL)
2095 err |= !a_amv_var_set(&avc, evp, TRU1);
2096 else{
2097 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2098 *ap);
2099 err = 1;
2103 }else if(!strcmp(*ap, "set"))
2104 err = !a_amv_var_c_set(++ap, TRU1);
2105 else if(!strcmp(*ap, "unset")){
2106 for(err = 0; *++ap != NULL;){
2107 a_amv_var_revlookup(&avc, *ap);
2109 if(!a_amv_var_clear(&avc, TRU1))
2110 err = 1;
2112 }else{
2113 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2114 err = 1;
2116 NYD_LEAVE;
2117 return err;
2120 /* s-it-mode */