Integrate S/MIME passwords into usual credential lookups (a bit)..
[s-mailx.git] / accmacvar.c
blob5ebc3fcbfc6d72b93962511b249eb8bce31e0ce4
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 Otherwise, the `localopts' should be an attribute of the lex_input.c
9 *@ TODO command context, so that it belongs to the execution context
10 *@ TODO we are running in, instead of being global data. See, e.g.,
11 *@ TODO the a_LEX_SLICE comment in lex_input.c.
12 *@ TODO . undefining and overwriting a macro should always be possible:
13 *@ TODO simply place the thing in a delete-later list and replace the
14 *@ TODO accessible entry! (instant delete if on top recursion level.)
15 *@ TODO . Likewise, overwriting an existing should be like delete+create
16 *@ TODO . once we can have non-fatal !0 returns for commands, we should
17 *@ TODO return error if "(environ)? unset" goes for non-existent.
19 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
20 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
23 * Copyright (c) 1980, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
50 #undef n_FILE
51 #define n_FILE accmacvar
53 #ifndef HAVE_AMALGAMATION
54 # include "nail.h"
55 #endif
57 #if !defined HAVE_SETENV && !defined HAVE_PUTENV
58 # error Exactly one of HAVE_SETENV and HAVE_PUTENV
59 #endif
61 /* Note: changing the hash function must be reflected in mk-okey-map.pl */
62 #define a_AMV_PRIME HSHSIZE
63 #define a_AMV_NAME2HASH(N) torek_hash(N)
64 #define a_AMV_HASH2PRIME(H) ((H) % a_AMV_PRIME)
66 enum a_amv_mac_flags{
67 a_AMV_MF_NONE = 0,
68 a_AMV_MF_ACC = 1<<0, /* This macro is an `account' */
69 a_AMV_MF_TYPE_MASK = a_AMV_MF_ACC,
70 a_AMV_MF_UNDEF = 1<<1, /* Unlink after lookup */
71 a_AMV_MF_DEL = 1<<7, /* Current `account': deleted while active */
72 a_AMV_MF__MAX = 0xFF
75 /* mk-okey-map.pl ensures that _VIRT implies _RDONLY and _NODEL, and that
76 * _IMPORT implies _ENV; it doesn't verify anything... */
77 enum a_amv_var_flags{
78 a_AMV_VF_NONE = 0,
79 a_AMV_VF_BOOL = 1<<0, /* ok_b_* */
80 a_AMV_VF_VIRT = 1<<1, /* "Stateless" automatic variable */
81 a_AMV_VF_NOLOPTS = 1<<2, /* May not be tracked by `localopts' */
82 a_AMV_VF_RDONLY = 1<<3, /* May not be set by user */
83 a_AMV_VF_NODEL = 1<<4, /* May not be deleted */
84 a_AMV_VF_NOTEMPTY = 1<<5, /* May not be assigned an empty value */
85 a_AMV_VF_NOCNTRLS = 1<<6, /* Value may not contain control characters */
86 a_AMV_VF_NUM = 1<<7, /* Value must be a 32-bit number */
87 a_AMV_VF_POSNUM = 1<<8, /* Value must be positive 32-bit number */
88 a_AMV_VF_LOWER = 1<<9, /* Values will be stored in a lowercase version */
89 a_AMV_VF_VIP = 1<<10, /* Wants _var_check_vips() evaluation */
90 a_AMV_VF_IMPORT = 1<<11, /* Import ONLY from environ (before PS_STARTED) */
91 a_AMV_VF_ENV = 1<<12, /* Update environment on change */
92 a_AMV_VF_I3VAL = 1<<13, /* Has an initial value */
93 a_AMV_VF_DEFVAL = 1<<14, /* Has a default value */
94 a_AMV_VF_LINKED = 1<<15, /* `environ' linked */
95 a_AMV_VF__MASK = (1<<(15+1)) - 1
98 struct a_amv_mac{
99 struct a_amv_mac *am_next;
100 ui32_t am_maxlen; /* of any line in .am_line_dat */
101 ui32_t am_line_cnt; /* of *.am_line_dat (but NULL terminated) */
102 struct a_amv_mac_line **am_line_dat; /* TODO use deque? */
103 struct a_amv_var *am_lopts; /* `localopts' unroll list */
104 ui8_t am_flags; /* enum a_amv_mac_flags */
105 char am_name[n_VFIELD_SIZE(7)]; /* of this macro */
107 n_CTA(a_AMV_MF__MAX <= UI8_MAX, "Enumeration excesses storage datatype");
109 struct a_amv_mac_line{
110 ui32_t aml_len;
111 ui32_t aml_prespc; /* Number of leading SPC, for display purposes */
112 char aml_dat[n_VFIELD_SIZE(0)];
115 struct a_amv_mac_call_args{
116 char const *amca_name;
117 struct a_amv_mac const *amca_amp;
118 struct a_amv_var **amca_unroller;
119 void (*amca_hook_pre)(void *);
120 void *amca_hook_arg;
121 bool_t amca_lopts_on;
122 bool_t amca_ps_hook_mask;
123 ui8_t amca__pad[6];
126 struct a_amv_lostack{
127 struct a_amv_lostack *as_global_saved; /* Saved global XXX due to jump */
128 struct a_amv_mac_call_args *as_amcap;
129 struct a_amv_lostack *as_up; /* Outer context */
130 struct a_amv_var *as_lopts;
131 bool_t as_unroll; /* Unrolling enabled? */
132 ui8_t avs__pad[7];
135 struct a_amv_var{
136 struct a_amv_var *av_link;
137 char *av_value;
138 #ifdef HAVE_PUTENV
139 char *av_env; /* Actively managed putenv(3) memory */
140 #endif
141 ui16_t av_flags; /* enum a_amv_var_flags */
142 char av_name[n_VFIELD_SIZE(6)];
144 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
146 struct a_amv_var_map{
147 ui32_t avm_hash;
148 ui16_t avm_keyoff;
149 ui16_t avm_flags; /* enum a_amv_var_flags */
151 n_CTA(a_AMV_VF__MASK <= UI16_MAX, "Enumeration excesses storage datatype");
153 struct a_amv_var_virt{
154 ui32_t avv_okey;
155 ui8_t avv__dummy[4];
156 struct a_amv_var const *avv_var;
159 struct a_amv_var_defval{
160 ui32_t avdv_okey;
161 ui8_t avdv__pad[4];
162 char const *avdv_value; /* Only for !BOOL (otherwise plain existence) */
165 struct a_amv_var_carrier{
166 char const *avc_name;
167 ui32_t avc_hash;
168 ui32_t avc_prime;
169 struct a_amv_var *avc_var;
170 struct a_amv_var_map const *avc_map;
171 enum okeys avc_okey;
172 ui8_t avc__pad[4];
175 /* Include the constant mk-okey-map.pl output */
176 #include "version.h"
177 #include "okeys.h"
179 /* True boolean visualization: this string will not be copied to heap memory
180 * in a_amv_var_copy(), but we must avoid confusion with identical user data.
181 * While here, add a special "0" one and speed up *.exit-status* assignments! */
182 static char const a_amv_var_1[] = "1";
183 static char const a_amv_var_0[] = "0";
185 /* The currently active account */
186 static struct a_amv_mac *a_amv_acc_curr;
188 static struct a_amv_mac *a_amv_macs[a_AMV_PRIME]; /* TODO dynamically spaced */
190 /* Unroll list of currently running macro stack */
191 static struct a_amv_lostack *a_amv_lopts;
193 static struct a_amv_var *a_amv_vars[a_AMV_PRIME]; /* TODO dynamically spaced */
195 /* TODO We really deserve localopts support for *folder-hook*s, so hack it in
196 * TODO today via a static lostack, it should be a field in mailbox, once that
197 * TODO is a real multi-instance object */
198 static struct a_amv_var *a_amv_folder_hook_lopts;
200 /* TODO Rather ditto (except for storage -> cmd_ctx), compose hooks */
201 static struct a_amv_var *a_amv_compose_lopts;
203 /* Does cp consist solely of WS and a } */
204 static bool_t a_amv_mac_is_closing_angle(char const *cp);
206 /* Lookup for macros/accounts */
207 static struct a_amv_mac *a_amv_mac_lookup(char const *name,
208 struct a_amv_mac *newamp, enum a_amv_mac_flags amf);
210 /* Execute a macro; amcap must reside in LOFI memory */
211 static bool_t a_amv_mac_exec(struct a_amv_mac_call_args *amcap);
213 static void a_amv_mac__finalize(void *vp);
215 /* User display helpers */
216 static bool_t a_amv_mac_show(enum a_amv_mac_flags amf);
218 /* _def() returns error for faulty definitions and already existing * names,
219 * _undef() returns error if a named thing doesn't exist */
220 static bool_t a_amv_mac_def(char const *name, enum a_amv_mac_flags amf);
221 static bool_t a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf);
223 /* */
224 static void a_amv_mac_free(struct a_amv_mac *amp);
226 /* Update replay-log */
227 static void a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
228 struct a_amv_var *oavp);
229 static void a_amv_lopts_unroll(struct a_amv_var **avpp);
231 /* Special cased value string allocation */
232 static char *a_amv_var_copy(char const *str);
233 static void a_amv_var_free(char *cp);
235 /* Check for special housekeeping */
236 static bool_t a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val);
238 /* _VF_NOCNTRLS, _VF_NUM / _VF_POSNUM */
239 static bool_t a_amv_var_check_nocntrls(char const *val);
240 static bool_t a_amv_var_check_num(char const *val, bool_t pos);
242 /* If a variable name begins with a lowercase-character and contains at
243 * least one '@', it is converted to all-lowercase. This is necessary
244 * for lookups of names based on email addresses.
245 * Following the standard, only the part following the last '@' should
246 * be lower-cased, but practice has established otherwise here */
247 static char const *a_amv_var_canonify(char const *vn);
249 /* Try to reverse lookup an option name to an enum okeys mapping.
250 * Updates .avc_name and .avc_hash; .avc_map is NULL if none found */
251 static bool_t a_amv_var_revlookup(struct a_amv_var_carrier *avcp,
252 char const *name);
254 /* Lookup a variable from .avc_(map|name|hash), return whether it was found.
255 * Sets .avc_prime; .avc_var is NULL if not found.
256 * Here it is where we care for _I3VAL and _DEFVAL, too.
257 * An _I3VAL will be "consumed" as necessary anyway, but it won't be used to
258 * create a new variable if i3val_nonew is true; if i3val_nonew is TRUM1 then
259 * we set .avc_var to -1 and return true if that was the case, otherwise we'll
260 * return FAL0, then! */
261 static bool_t a_amv_var_lookup(struct a_amv_var_carrier *avcp,
262 bool_t i3val_nonew);
264 /* Set var from .avc_(map|name|hash), return success */
265 static bool_t a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
266 bool_t force_env);
268 static bool_t a_amv_var__putenv(struct a_amv_var_carrier *avcp,
269 struct a_amv_var *avp);
271 /* Clear var from .avc_(map|name|hash); sets .avc_var=NULL, return success */
272 static bool_t a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env);
274 static bool_t a_amv_var__clearenv(char const *name, char *value);
276 /* List all variables */
277 static void a_amv_var_show_all(void);
279 static int a_amv_var__show_cmp(void const *s1, void const *s2);
281 /* Actually do print one, return number of lines written */
282 static size_t a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp);
284 /* Shared c_set() and c_environ():set impl, return success */
285 static bool_t a_amv_var_c_set(char **ap, bool_t issetenv);
287 static bool_t
288 a_amv_mac_is_closing_angle(char const *cp){
289 bool_t rv;
290 NYD2_ENTER;
292 while(spacechar(*cp))
293 ++cp;
295 if((rv = (*cp++ == '}'))){
296 while(spacechar(*cp))
297 ++cp;
298 rv = (*cp == '\0');
300 NYD2_LEAVE;
301 return rv;
304 static struct a_amv_mac *
305 a_amv_mac_lookup(char const *name, struct a_amv_mac *newamp,
306 enum a_amv_mac_flags amf){
307 struct a_amv_mac *amp, **ampp;
308 ui32_t h;
309 enum a_amv_mac_flags save_amf;
310 NYD2_ENTER;
312 save_amf = amf;
313 amf &= a_AMV_MF_TYPE_MASK;
314 h = a_AMV_NAME2HASH(name);
315 h = a_AMV_HASH2PRIME(h);
316 ampp = &a_amv_macs[h];
318 for(amp = *ampp; amp != NULL; ampp = &(*ampp)->am_next, amp = amp->am_next){
319 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf &&
320 !strcmp(amp->am_name, name)){
321 if(n_LIKELY((save_amf & a_AMV_MF_UNDEF) == 0))
322 goto jleave;
324 *ampp = amp->am_next;
326 if((amf & a_AMV_MF_ACC) &&
327 account_name != NULL && !strcmp(account_name, name)){
328 amp->am_flags |= a_AMV_MF_DEL;
329 n_err(_("Delayed deletion of active account: %s\n"), name);
330 }else{
331 a_amv_mac_free(amp);
332 amp = (struct a_amv_mac*)-1;
334 goto jleave;
338 if(newamp != NULL){
339 ampp = &a_amv_macs[h];
340 newamp->am_next = *ampp;
341 *ampp = newamp;
342 amp = NULL;
344 jleave:
345 NYD2_LEAVE;
346 return amp;
349 static bool_t
350 a_amv_mac_exec(struct a_amv_mac_call_args *amcap){
351 struct a_amv_lostack *losp;
352 struct a_amv_mac_line **amlp;
353 char **args_base, **args;
354 struct a_amv_mac const *amp;
355 bool_t rv;
356 NYD2_ENTER;
358 amp = amcap->amca_amp;
359 /* XXX Unfortunately we yet need to dup the macro lines! :( */
360 args_base = args = smalloc(sizeof(*args) * (amp->am_line_cnt +1));
361 for(amlp = amp->am_line_dat; *amlp != NULL; ++amlp)
362 *(args++) = sbufdup((*amlp)->aml_dat, (*amlp)->aml_len);
363 *args = NULL;
365 losp = n_lofi_alloc(sizeof *losp);
366 losp->as_global_saved = a_amv_lopts;
367 if((losp->as_amcap = amcap)->amca_unroller == NULL){
368 losp->as_up = losp->as_global_saved;
369 losp->as_lopts = NULL;
370 }else{
371 losp->as_up = NULL;
372 losp->as_lopts = *amcap->amca_unroller;
374 losp->as_unroll = amcap->amca_lopts_on;
376 a_amv_lopts = losp;
377 if(amcap->amca_hook_pre != NULL)
378 (*amcap->amca_hook_pre)(amcap->amca_hook_arg);
379 rv = n_source_macro(n_LEXINPUT_NONE, amp->am_name, args_base,
380 &a_amv_mac__finalize, losp);
381 NYD2_LEAVE;
382 return rv;
385 static void
386 a_amv_mac__finalize(void *vp){
387 struct a_amv_mac_call_args *amcap;
388 struct a_amv_lostack *losp;
389 NYD2_ENTER;
391 losp = vp;
392 a_amv_lopts = losp->as_global_saved;
394 if((amcap = losp->as_amcap)->amca_unroller == NULL){
395 if(losp->as_lopts != NULL)
396 a_amv_lopts_unroll(&losp->as_lopts);
397 }else
398 *amcap->amca_unroller = losp->as_lopts;
400 if(amcap->amca_ps_hook_mask)
401 pstate &= ~PS_HOOK_MASK;
403 n_lofi_free(losp);
404 n_lofi_free(amcap);
405 NYD2_LEAVE;
408 static bool_t
409 a_amv_mac_show(enum a_amv_mac_flags amf){
410 size_t lc, mc, ti, i;
411 char const *typestr;
412 FILE *fp;
413 bool_t rv;
414 NYD2_ENTER;
416 rv = FAL0;
418 if((fp = Ftmp(NULL, "deflist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
419 NULL){
420 n_perr(_("Can't create temporary file for `define' or `account' listing"),
422 goto jleave;
425 amf &= a_AMV_MF_TYPE_MASK;
426 typestr = (amf & a_AMV_MF_ACC) ? "account" : "define";
428 for(lc = mc = ti = 0; ti < a_AMV_PRIME; ++ti){
429 struct a_amv_mac *amp;
431 for(amp = a_amv_macs[ti]; amp != NULL; amp = amp->am_next){
432 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
433 struct a_amv_mac_line **amlpp;
435 if(++mc > 1){
436 putc('\n', fp);
437 ++lc;
439 ++lc;
440 fprintf(fp, "%s %s {\n", typestr, amp->am_name);
441 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++lc, ++amlpp){
442 for(i = (*amlpp)->aml_prespc; i > 0; --i)
443 putc(' ', fp);
444 fputs((*amlpp)->aml_dat, fp);
445 putc('\n', fp);
447 fputs("}\n", fp);
448 ++lc;
452 if(mc > 0)
453 page_or_print(fp, lc);
455 rv = (ferror(fp) == 0);
456 Fclose(fp);
457 jleave:
458 NYD2_LEAVE;
459 return rv;
462 static bool_t
463 a_amv_mac_def(char const *name, enum a_amv_mac_flags amf){
464 struct str line;
465 ui32_t line_cnt, maxlen;
466 struct linelist{
467 struct linelist *ll_next;
468 struct a_amv_mac_line *ll_amlp;
469 } *llp, *ll_head, *ll_tail;
470 union {size_t s; int i; ui32_t ui; size_t l;} n;
471 struct a_amv_mac *amp;
472 bool_t rv;
473 NYD2_ENTER;
475 memset(&line, 0, sizeof line);
476 rv = FAL0;
477 amp = NULL;
479 /* Read in the lines which form the macro content */
480 for(ll_tail = ll_head = NULL, line_cnt = maxlen = 0;;){
481 ui32_t leaspc;
482 char *cp;
484 n.i = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, n_empty,
485 &line.s, &line.l, NULL);
486 if(n.ui == 0)
487 continue;
488 if(n.i < 0){
489 n_err(_("Unterminated %s definition: %s\n"),
490 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
491 goto jerr;
493 if(a_amv_mac_is_closing_angle(line.s))
494 break;
496 /* Trim WS, remember amount of leading spaces for display purposes */
497 for(cp = line.s, leaspc = 0; n.ui > 0; ++cp, --n.ui)
498 if(*cp == '\t')
499 leaspc = (leaspc + 8u) & ~7u;
500 else if(*cp == ' ')
501 ++leaspc;
502 else
503 break;
504 for(; n.ui > 0 && whitechar(cp[n.ui - 1]); --n.ui)
506 if(n.ui == 0)
507 continue;
509 maxlen = n_MAX(maxlen, n.ui);
510 cp[n.ui++] = '\0';
512 if(n_LIKELY(++line_cnt < UI32_MAX)){
513 struct a_amv_mac_line *amlp;
515 llp = salloc(sizeof *llp);
516 if(ll_head == NULL)
517 ll_head = llp;
518 else
519 ll_tail->ll_next = llp;
520 ll_tail = llp;
521 llp->ll_next = NULL;
522 llp->ll_amlp = amlp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac_line,
523 aml_dat) + n.ui);
524 amlp->aml_len = n.ui -1;
525 amlp->aml_prespc = leaspc;
526 memcpy(amlp->aml_dat, cp, n.ui);
527 }else{
528 n_err(_("Too much content in %s definition: %s\n"),
529 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
530 goto jerr;
534 /* Create the new macro */
535 n.s = strlen(name) +1;
536 amp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_mac, am_name) + n.s);
537 amp->am_next = NULL;
538 amp->am_maxlen = maxlen;
539 amp->am_line_cnt = line_cnt;
540 amp->am_flags = amf;
541 amp->am_lopts = NULL;
542 memcpy(amp->am_name, name, n.s);
543 /* C99 */{
544 struct a_amv_mac_line **amlpp;
546 amp->am_line_dat = amlpp = smalloc(sizeof(*amlpp) * ++line_cnt);
547 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
548 *amlpp++ = llp->ll_amlp;
549 *amlpp = NULL;
552 /* Finally check whether such a macro already exists, in which case we throw
553 * it all away again. At least we know it would have worked */
554 if(a_amv_mac_lookup(name, amp, amf) != NULL){
555 n_err(_("There is already a %s of name: %s\n"),
556 (amf & a_AMV_MF_ACC ? "account" : "macro"), name);
557 goto jerr;
560 rv = TRU1;
561 jleave:
562 if(line.s != NULL)
563 free(line.s);
564 NYD2_LEAVE;
565 return rv;
567 jerr:
568 for(llp = ll_head; llp != NULL; llp = llp->ll_next)
569 free(llp->ll_amlp);
570 if(amp != NULL){
571 free(amp->am_line_dat);
572 free(amp);
574 goto jleave;
577 static bool_t
578 a_amv_mac_undef(char const *name, enum a_amv_mac_flags amf){
579 struct a_amv_mac *amp;
580 bool_t rv;
581 NYD2_ENTER;
583 rv = TRU1;
585 if(n_LIKELY(name[0] != '*' || name[1] != '\0')){
586 if((amp = a_amv_mac_lookup(name, NULL, amf | a_AMV_MF_UNDEF)) == NULL){
587 n_err(_("%s not defined: %s\n"),
588 (amf & a_AMV_MF_ACC ? "Account" : "Macro"), name);
589 rv = FAL0;
591 }else{
592 struct a_amv_mac **ampp, *lamp;
594 for(ampp = a_amv_macs; PTRCMP(ampp, <, &a_amv_macs[n_NELEM(a_amv_macs)]);
595 ++ampp)
596 for(lamp = NULL, amp = *ampp; amp != NULL;){
597 if((amp->am_flags & a_AMV_MF_TYPE_MASK) == amf){
598 /* xxx Expensive but rare: be simple */
599 a_amv_mac_lookup(amp->am_name, NULL, amf | a_AMV_MF_UNDEF);
600 amp = (lamp == NULL) ? *ampp : lamp->am_next;
601 }else{
602 lamp = amp;
603 amp = amp->am_next;
607 NYD2_LEAVE;
608 return rv;
611 static void
612 a_amv_mac_free(struct a_amv_mac *amp){
613 struct a_amv_mac_line **amlpp;
614 NYD2_ENTER;
616 for(amlpp = amp->am_line_dat; *amlpp != NULL; ++amlpp)
617 free(*amlpp);
618 free(amp->am_line_dat);
619 free(amp);
620 NYD2_LEAVE;
623 static void
624 a_amv_lopts_add(struct a_amv_lostack *alp, char const *name,
625 struct a_amv_var *oavp){
626 struct a_amv_var *avp;
627 size_t nl, vl;
628 NYD2_ENTER;
630 /* Propagate unrolling up the stack, as necessary */
631 assert(alp != NULL);
632 for(;;){
633 if(alp->as_unroll)
634 break;
635 if((alp = alp->as_up) == NULL)
636 goto jleave;
639 /* Check whether this variable is handled yet */
640 for(avp = alp->as_lopts; avp != NULL; avp = avp->av_link)
641 if(!strcmp(avp->av_name, name))
642 goto jleave;
644 nl = strlen(name) +1;
645 vl = (oavp != NULL) ? strlen(oavp->av_value) +1 : 0;
646 avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name) + nl + vl);
647 avp->av_link = alp->as_lopts;
648 alp->as_lopts = avp;
649 memcpy(avp->av_name, name, nl);
650 if(vl == 0){
651 avp->av_value = NULL;
652 avp->av_flags = 0;
653 #ifdef HAVE_PUTENV
654 avp->av_env = NULL;
655 #endif
656 }else{
657 avp->av_value = &avp->av_name[nl];
658 avp->av_flags = oavp->av_flags;
659 memcpy(avp->av_value, oavp->av_value, vl);
660 #ifdef HAVE_PUTENV
661 avp->av_env = (oavp->av_env == NULL) ? NULL : sstrdup(oavp->av_env);
662 #endif
664 jleave:
665 NYD2_LEAVE;
668 static void
669 a_amv_lopts_unroll(struct a_amv_var **avpp){
670 struct a_amv_lostack *save_alp;
671 bool_t reset;
672 struct a_amv_var *x, *avp;
673 NYD2_ENTER;
675 avp = *avpp;
676 *avpp = NULL;
677 reset = !(pstate & PS_ROOT);
679 save_alp = a_amv_lopts;
680 a_amv_lopts = NULL;
681 while(avp != NULL){
682 x = avp;
683 avp = avp->av_link;
684 pstate |= PS_ROOT;
685 vok_vset(x->av_name, x->av_value);
686 if(reset)
687 pstate &= ~PS_ROOT;
688 free(x);
690 a_amv_lopts = save_alp;
691 NYD2_LEAVE;
694 static char *
695 a_amv_var_copy(char const *str){
696 char *news;
697 size_t len;
698 NYD2_ENTER;
700 if(*str == '\0')
701 news = n_UNCONST(n_empty);
702 else if(str[1] == '\0'){
703 if(str[0] == '1')
704 news = n_UNCONST(a_amv_var_1);
705 else if(str[0] == '0')
706 news = n_UNCONST(a_amv_var_0);
707 else
708 goto jheap;
709 }else{
710 jheap:
711 len = strlen(str) +1;
712 news = smalloc(len);
713 memcpy(news, str, len);
715 NYD2_LEAVE;
716 return news;
719 static void
720 a_amv_var_free(char *cp){
721 NYD2_ENTER;
722 if(cp[0] != '\0' && cp != a_amv_var_1 && cp != a_amv_var_0)
723 free(cp);
724 NYD2_LEAVE;
727 static bool_t
728 a_amv_var_check_vips(enum okeys okey, bool_t enable, char **val){
729 int flag;
730 bool_t ok;
731 NYD2_ENTER;
733 ok = TRU1;
734 flag = 0;
736 switch(okey){
737 case ok_b_debug:
738 flag = OPT_DEBUG;
739 break;
740 case ok_v_HOME:
741 /* Invalidate any resolved folder then, too
742 * FALLTHRU */
743 case ok_v_folder:
744 ok = !(pstate & PS_ROOT);
745 pstate |= PS_ROOT;
746 ok_vclear(_folder_resolved);
747 if(ok)
748 pstate &= ~PS_ROOT;
749 ok = TRU1;
750 break;
751 case ok_b_header:
752 flag = OPT_N_FLAG;
753 enable = !enable;
754 break;
755 case ok_b_memdebug:
756 flag = OPT_MEMDEBUG;
757 break;
758 case ok_b_POSIXLY_CORRECT:
759 if(!(pstate & PS_ROOT)){
760 bool_t reset = !(pstate & PS_ROOT);
762 pstate |= PS_ROOT;
763 if(enable)
764 ok_bset(posix);
765 else
766 ok_bclear(posix);
767 if(reset)
768 pstate &= ~PS_ROOT;
770 break;
771 case ok_b_posix:
772 if(!(pstate & PS_ROOT)){
773 bool_t reset = !(pstate & PS_ROOT);
775 pstate |= PS_ROOT;
776 if(enable)
777 ok_bset(POSIXLY_CORRECT);
778 else
779 ok_bclear(POSIXLY_CORRECT);
780 if(reset)
781 pstate &= ~PS_ROOT;
783 break;
784 case ok_b_skipemptybody:
785 flag = OPT_E_FLAG;
786 break;
787 case ok_b_typescript_mode:
788 if(enable){
789 ok_bset(colour_disable);
790 ok_bset(line_editor_disable);
791 if(!(pstate & PS_STARTED))
792 ok_bset(termcap_disable);
794 case ok_v_umask:
795 assert(enable);
796 if(**val != '\0'){
797 ul_i ul;
799 if((ul = strtoul(*val, NULL, 0)) & ~0777u){ /* (is valid _VF_POSNUM) */
800 n_err(_("Invalid *umask* setting: %s\n"), *val);
801 ok = FAL0;
802 }else
803 umask((mode_t)ul);
805 break;
806 case ok_b_verbose:
807 flag = (enable && !(options & OPT_VERB))
808 ? OPT_VERB : OPT_VERB | OPT_VERBVERB;
809 break;
810 default:
811 DBG( n_err("Implementation error: never heard of %u\n", ok); )
812 break;
815 if(flag){
816 if(enable)
817 options |= flag;
818 else
819 options &= ~flag;
821 NYD2_LEAVE;
822 return ok;
825 static bool_t
826 a_amv_var_check_nocntrls(char const *val){
827 char c;
828 NYD2_ENTER;
830 while((c = *val++) != '\0')
831 if(cntrlchar(c))
832 break;
833 NYD2_LEAVE;
834 return (c == '\0');
837 static bool_t
838 a_amv_var_check_num(char const *val, bool_t pos){ /* TODO intmax_t anywhere! */
839 /* TODO The internal/environment variables which are num= or posnum= should
840 * TODO gain special lookup functions, or the return should be void* and
841 * TODO castable to integer; i.e. no more strtoX() should be needed.
842 * TODO I.e., the result of this function should instead be stored.
843 * TODO Use intmax_t IF that is sizeof(void*) only? */
844 bool_t rv;
845 NYD2_ENTER;
847 rv = TRU1;
849 if(*val != '\0'){ /* Would be _VF_NOTEMPTY if not allowed */
850 char *eptr;
851 union {long s; unsigned long u;} i;
853 if(!pos){
854 i.s = strtol(val, &eptr, 0); /* TODO strtoimax() */
856 if(*eptr != '\0' ||
857 ((i.s == LONG_MIN || i.s == LONG_MAX) && errno == ERANGE))
858 rv = FAL0;
859 #if INT_MIN != LONG_MIN
860 else if(i.s < INT_MIN)
861 rv = FAL0;
862 #endif
863 #if INT_MAX != LONG_MAX
864 else if(i.s > INT_MAX)
865 rv = FAL0;
866 #endif
867 }else{
868 i.u = strtoul(val, &eptr, 0); /* TODO strtoumax() */
870 if(*eptr != '\0' || (i.u == ULONG_MAX && errno == ERANGE))
871 rv = FAL0;
872 #if UINT_MAX != ULONG_MAX
873 else if(i.u > UINT_MAX)
874 rv = FAL0;
875 #endif
878 NYD2_LEAVE;
879 return rv;
882 static char const *
883 a_amv_var_canonify(char const *vn){
884 NYD2_ENTER;
885 if(!upperchar(*vn)){
886 char const *vp;
888 for(vp = vn; *vp != '\0' && *vp != '@'; ++vp)
890 vn = (*vp == '@') ? i_strdup(vn) : vn;
892 NYD2_LEAVE;
893 return vn;
896 static bool_t
897 a_amv_var_revlookup(struct a_amv_var_carrier *avcp, char const *name){
898 ui32_t hash, i, j;
899 struct a_amv_var_map const *avmp;
900 NYD2_ENTER;
902 avcp->avc_name = name = a_amv_var_canonify(name);
903 avcp->avc_hash = hash = a_AMV_NAME2HASH(name);
905 for(i = hash % a_AMV_VAR_REV_PRIME, j = 0; j <= a_AMV_VAR_REV_LONGEST; ++j){
906 ui32_t x;
908 if((x = a_amv_var_revmap[i]) == a_AMV_VAR_REV_ILL)
909 break;
911 avmp = &a_amv_var_map[x];
912 if(avmp->avm_hash == hash &&
913 !strcmp(&a_amv_var_names[avmp->avm_keyoff], name)){
914 avcp->avc_map = avmp;
915 avcp->avc_okey = (enum okeys)x;
916 goto jleave;
919 if(++i == a_AMV_VAR_REV_PRIME){
920 #ifdef a_AMV_VAR_REV_WRAPAROUND
921 i = 0;
922 #else
923 break;
924 #endif
927 avcp->avc_map = NULL;
928 avcp = NULL;
929 jleave:
930 NYD2_LEAVE;
931 return (avcp != NULL);
934 static bool_t
935 a_amv_var_lookup(struct a_amv_var_carrier *avcp, bool_t i3val_nonew){
936 size_t i;
937 char const *cp;
938 struct a_amv_var_map const *avmp;
939 struct a_amv_var *avp;
940 NYD2_ENTER;
942 /* C99 */{
943 struct a_amv_var **avpp, *lavp;
945 avpp = &a_amv_vars[avcp->avc_prime = a_AMV_HASH2PRIME(avcp->avc_hash)];
947 for(lavp = NULL, avp = *avpp; avp != NULL; lavp = avp, avp = avp->av_link)
948 if(!strcmp(avp->av_name, avcp->avc_name)){
949 /* Relink as head, hope it "sorts on usage" over time.
950 * _clear() relies on this behaviour */
951 if(lavp != NULL){
952 lavp->av_link = avp->av_link;
953 avp->av_link = *avpp;
954 *avpp = avp;
956 goto jleave;
960 /* If this is not an assembled variable we need to consider some special
961 * initialization cases and eventually create the variable anew */
962 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
963 /* Does it have an import-from-environment flag? */
964 if(n_UNLIKELY((avmp->avm_flags & (a_AMV_VF_IMPORT | a_AMV_VF_ENV)) != 0)){
965 if(n_LIKELY((cp = getenv(avcp->avc_name)) != NULL)){
966 /* May be better not to use that one, though? */
967 bool_t isempty, isbltin;
969 isempty = (*cp == '\0' &&
970 (avmp->avm_flags & a_AMV_VF_NOTEMPTY) != 0);
971 isbltin = ((avmp->avm_flags & (a_AMV_VF_I3VAL | a_AMV_VF_DEFVAL)
972 ) != 0);
974 if(n_UNLIKELY(isempty)){
975 if(!isbltin)
976 goto jerr;
977 }else if(n_LIKELY(*cp != '\0')){
978 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) &&
979 !a_amv_var_check_nocntrls(cp))){
980 n_err(_("Ignoring environment, control characters "
981 "invalid in variable: %s\n"), avcp->avc_name);
982 goto jerr;
984 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
985 !a_amv_var_check_num(cp, FAL0))){
986 n_err(_("Environment variable value not a number "
987 "or out of range: %s\n"), avcp->avc_name);
988 goto jerr;
990 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
991 !a_amv_var_check_num(cp, TRU1))){
992 n_err(_("Environment variable value not a number, "
993 "negative or out of range: %s\n"), avcp->avc_name);
994 goto jerr;
997 goto jnewval;
1001 /* A first-time init switch is to be handled now and here */
1002 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
1003 static struct a_amv_var_defval const **arr,
1004 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
1006 if(arr == NULL){
1007 arr = &arr_base[0];
1008 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1009 while(i-- > 0)
1010 arr[i] = &a_amv_var_i3vals[i];
1013 for(i = 0; arr[i] != NULL; ++i)
1014 if(arr[i]->avdv_okey == avcp->avc_okey){
1015 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1016 : arr[i]->avdv_value;
1017 /* Remove this entry, hope entire block becomes no-op asap */
1019 arr[i] = arr[i + 1];
1020 while(arr[i++] != NULL);
1022 if(!i3val_nonew)
1023 goto jnewval;
1024 if(i3val_nonew == TRUM1)
1025 avp = (struct a_amv_var*)-1;
1026 goto jleave;
1030 /* The virtual variables */
1031 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
1032 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
1033 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
1034 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
1035 goto jleave;
1037 /* Not reached */
1040 /* Place this last because once it is set first the variable will never
1041 * be removed again and thus match in the first block above */
1042 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
1043 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
1044 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
1045 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1046 : a_amv_var_defvals[i].avdv_value;
1047 goto jnewval;
1052 jerr:
1053 avp = NULL;
1054 jleave:
1055 avcp->avc_var = avp;
1056 NYD2_LEAVE;
1057 return (avp != NULL);
1059 jnewval: /* C99 */{
1060 struct a_amv_var **avpp;
1061 size_t l;
1063 l = strlen(avcp->avc_name) +1;
1064 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1065 ) + l);
1066 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1067 *avpp = avp;
1068 memcpy(avp->av_name, avcp->avc_name, l);
1069 avp->av_value = a_amv_var_copy(cp);
1070 #ifdef HAVE_PUTENV
1071 avp->av_env = NULL;
1072 #endif
1073 avp->av_flags = avmp->avm_flags;
1075 if(avp->av_flags & a_AMV_VF_VIP)
1076 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1077 if(avp->av_flags & a_AMV_VF_ENV)
1078 a_amv_var__putenv(avcp, avp);
1079 goto jleave;
1083 static bool_t
1084 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1085 bool_t force_env){
1086 struct a_amv_var *avp;
1087 char *oval;
1088 struct a_amv_var_map const *avmp;
1089 bool_t rv;
1090 NYD2_ENTER;
1092 if(value == NULL){
1093 rv = a_amv_var_clear(avcp, force_env);
1094 goto jleave;
1097 if((avmp = avcp->avc_map) != NULL){
1098 rv = FAL0;
1100 /* Validity checks */
1101 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1102 !(pstate & PS_ROOT))){
1103 value = N_("Variable is readonly: %s\n");
1104 goto jeavmp;
1106 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1107 value = N_("Variable must not be empty: %s\n");
1108 goto jeavmp;
1110 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1111 !a_amv_var_check_nocntrls(value))){
1112 value = N_("Variable forbids control characters: %s\n");
1113 goto jeavmp;
1115 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1116 !a_amv_var_check_num(value, FAL0))){
1117 value = N_("Variable value not a number or out of range: %s\n");
1118 goto jeavmp;
1120 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1121 !a_amv_var_check_num(value, TRU1))){
1122 value = _("Variable value not a number, negative, "
1123 "or out of range: %s\n");
1124 goto jeavmp;
1126 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1127 !(pstate & (PS_ROOT | PS_STARTED)))){
1128 value = N_("Variable cannot be set in a resource file: %s\n");
1129 jeavmp:
1130 n_err(V_(value), avcp->avc_name);
1131 goto jleave;
1134 /* Transformations */
1135 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_LOWER)){
1136 char c;
1138 oval = savestr(value);
1139 value = oval;
1140 for(; (c = *oval) != '\0'; ++oval)
1141 *oval = lowerconv(c);
1145 rv = TRU1;
1146 a_amv_var_lookup(avcp, TRU1);
1148 /* Don't care what happens later on, store this in the unroll list */
1149 if(a_amv_lopts != NULL &&
1150 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1151 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1153 if((avp = avcp->avc_var) == NULL){
1154 struct a_amv_var **avpp;
1155 size_t l;
1157 l = strlen(avcp->avc_name) +1;
1158 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1159 ) + l);
1160 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1161 *avpp = avp;
1162 #ifdef HAVE_PUTENV
1163 avp->av_env = NULL;
1164 #endif
1165 memcpy(avp->av_name, avcp->avc_name, l);
1166 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1167 oval = n_UNCONST(n_empty);
1168 }else
1169 oval = avp->av_value;
1171 if(avmp == NULL)
1172 avp->av_value = a_amv_var_copy(value);
1173 else{
1174 /* Via `set' etc. the user may give even boolean options non-boolean
1175 * values, ignore that and force boolean */
1176 if(avp->av_flags & a_AMV_VF_BOOL){
1177 if((options & OPT_D_VV) && *value != '\0')
1178 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1179 avcp->avc_name, value);
1180 avp->av_value = n_UNCONST(a_amv_var_1);
1181 }else
1182 avp->av_value = a_amv_var_copy(value);
1184 /* Check if update allowed XXX wasteful on error! */
1185 if((avp->av_flags & a_AMV_VF_VIP) &&
1186 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1187 char *cp = avp->av_value;
1189 avp->av_value = oval;
1190 oval = cp;
1194 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1195 avp->av_flags |= a_AMV_VF_LINKED;
1196 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1197 rv = a_amv_var__putenv(avcp, avp);
1199 a_amv_var_free(oval);
1200 jleave:
1201 NYD2_LEAVE;
1202 return rv;
1205 static bool_t
1206 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1207 #ifndef HAVE_SETENV
1208 char *cp;
1209 #endif
1210 bool_t rv;
1211 NYD2_ENTER;
1213 #ifdef HAVE_SETENV
1214 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1215 #else
1216 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1218 if((rv = (putenv(cp) == 0))){
1219 char *ocp;
1221 if((ocp = avp->av_env) != NULL)
1222 free(ocp);
1223 avp->av_env = cp;
1224 }else
1225 free(cp);
1226 #endif
1227 NYD2_LEAVE;
1228 return rv;
1231 static bool_t
1232 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1233 struct a_amv_var **avpp, *avp;
1234 struct a_amv_var_map const *avmp;
1235 bool_t rv;
1236 NYD2_ENTER;
1238 rv = FAL0;
1240 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1241 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1242 !(pstate & PS_ROOT))){
1243 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1244 goto jleave;
1246 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1247 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1248 goto jleave;
1251 rv = TRU1;
1253 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1254 if(force_env){
1255 jforce_env:
1256 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1257 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1258 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1259 goto jleave;
1260 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1261 avcp->avc_var = NULL;
1262 if(force_env)
1263 goto jforce_env;
1264 goto jleave;
1267 if(a_amv_lopts != NULL &&
1268 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1269 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1271 avp = avcp->avc_var;
1272 avcp->avc_var = NULL;
1273 avpp = &a_amv_vars[avcp->avc_prime];
1274 assert(*avpp == avp); /* (always listhead after lookup()) */
1275 *avpp = (*avpp)->av_link;
1277 /* C99 */{
1278 #ifdef HAVE_SETENV
1279 char *envval = NULL;
1280 #else
1281 char *envval = avp->av_env;
1282 #endif
1283 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1284 rv = a_amv_var__clearenv(avp->av_name, envval);
1286 a_amv_var_free(avp->av_value);
1287 free(avp);
1289 /* XXX Fun part, extremely simple-minded for now: if this variable has
1290 * XXX a default value, immediately reinstantiate it! */
1291 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1292 a_amv_var_lookup(avcp, TRU1);
1293 jleave:
1294 NYD2_LEAVE;
1295 return rv;
1298 static bool_t
1299 a_amv_var__clearenv(char const *name, char *value){
1300 #ifndef HAVE_SETENV
1301 extern char **environ;
1302 char **ecpp;
1303 #endif
1304 bool_t rv;
1305 NYD2_ENTER;
1306 n_UNUSED(value);
1308 #ifdef HAVE_SETENV
1309 unsetenv(name);
1310 rv = TRU1;
1311 #else
1312 if(value != NULL)
1313 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1314 if(*ecpp == value){
1315 free(value);
1317 ecpp[0] = ecpp[1];
1318 while(*ecpp++ != NULL);
1319 break;
1321 rv = TRU1;
1322 #endif
1323 NYD2_LEAVE;
1324 return rv;
1327 static void
1328 a_amv_var_show_all(void){
1329 struct n_string msg, *msgp;
1330 FILE *fp;
1331 size_t no, i;
1332 struct a_amv_var *avp;
1333 char const **vacp, **cap;
1334 NYD2_ENTER;
1336 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1337 n_perr(_("Can't create temporary file for `set' listing"), 0);
1338 goto jleave;
1341 /* We need to instantiate first-time-inits and default values here, so that
1342 * they will be regular members of our _vars[] table */
1343 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1344 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1345 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1346 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1348 for(no = i = 0; i < a_AMV_PRIME; ++i)
1349 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1350 ++no;
1351 no += a_AMV_VAR_VIRTS_CNT;
1353 vacp = salloc(no * sizeof(*vacp));
1355 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1356 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1357 *cap++ = avp->av_name;
1358 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1359 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1361 if(no > 1)
1362 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1364 msgp = &msg;
1365 msgp = n_string_reserve(n_string_creat(msgp), 80);
1366 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1367 i += a_amv_var_show(*cap, fp, msgp);
1368 n_string_gut(&msg);
1370 page_or_print(fp, i);
1371 Fclose(fp);
1372 jleave:
1373 NYD2_LEAVE;
1376 static int
1377 a_amv_var__show_cmp(void const *s1, void const *s2){
1378 int rv;
1379 NYD2_ENTER;
1381 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1382 NYD2_LEAVE;
1383 return rv;
1386 static size_t
1387 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1388 struct a_amv_var_carrier avc;
1389 char const *quote;
1390 size_t i;
1391 NYD2_ENTER;
1393 msgp = n_string_trunc(msgp, 0);
1394 i = 0;
1396 a_amv_var_revlookup(&avc, name);
1397 if(!a_amv_var_lookup(&avc, FAL0)){
1398 struct str s;
1400 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1401 s.s = n_UNCONST(name);
1402 s.l = UIZ_MAX;
1403 msgp = n_shexp_quote(msgp, &s, FAL0);
1404 goto jleave;
1407 if(options & OPT_D_V){
1408 if(avc.avc_map == NULL){
1409 msgp = n_string_push_c(msgp, '#');
1410 msgp = n_string_push_cp(msgp, "assembled");
1411 i = 1;
1413 /* C99 */{
1414 struct{
1415 ui16_t flags;
1416 char msg[22];
1417 } const tbase[] = {
1418 {a_AMV_VF_VIRT, "virtual"},
1419 {a_AMV_VF_RDONLY, "readonly"},
1420 {a_AMV_VF_NODEL, "nodelete"},
1421 {a_AMV_VF_NOTEMPTY, "notempty"},
1422 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1423 {a_AMV_VF_NUM, "number"},
1424 {a_AMV_VF_POSNUM, "positive-number"},
1425 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1426 {a_AMV_VF_ENV, "sync-environ"},
1427 {a_AMV_VF_I3VAL, "initial-value"},
1428 {a_AMV_VF_DEFVAL, "default-value"},
1429 {a_AMV_VF_LINKED, "`environ' link"}
1430 }, *tp;
1432 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1433 if(avc.avc_var->av_flags & tp->flags){
1434 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1435 msgp = n_string_push_cp(msgp, tp->msg);
1439 if(i > 0)
1440 msgp = n_string_push_cp(msgp, "\n ");
1443 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1444 msgp = n_string_push_cp(msgp, "# ");
1445 n_UNINIT(quote, NULL);
1446 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1447 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1448 if(strcmp(quote, avc.avc_var->av_value))
1449 msgp = n_string_push_cp(msgp, "wysh ");
1451 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1452 msgp = n_string_push_cp(msgp, "environ ");
1453 msgp = n_string_push_cp(msgp, "set ");
1454 msgp = n_string_push_cp(msgp, name);
1455 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1456 msgp = n_string_push_c(msgp, '=');
1457 msgp = n_string_push_cp(msgp, quote);
1460 jleave:
1461 msgp = n_string_push_c(msgp, '\n');
1462 fputs(n_string_cp(msgp), fp);
1463 NYD2_ENTER;
1464 return (i > 0 ? 2 : 1);
1467 static bool_t
1468 a_amv_var_c_set(char **ap, bool_t issetenv){
1469 char *cp, *cp2, *varbuf, c;
1470 size_t errs;
1471 NYD2_ENTER;
1473 errs = 0;
1474 jouter:
1475 while((cp = *ap++) != NULL){
1476 /* Isolate key */
1477 cp2 = varbuf = salloc(strlen(cp) +1);
1479 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1480 if(cntrlchar(c) || whitechar(c)){
1481 n_err(_("Variable name with control character ignored: %s\n"),
1482 ap[-1]);
1483 ++errs;
1484 goto jouter;
1486 *cp2++ = c;
1488 *cp2 = '\0';
1489 if(c == '\0')
1490 cp = n_UNCONST(n_empty);
1491 else
1492 ++cp;
1494 if(varbuf == cp2){
1495 n_err(_("Empty variable name ignored\n"));
1496 ++errs;
1497 }else{
1498 struct a_amv_var_carrier avc;
1499 bool_t isunset;
1501 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1502 varbuf = &varbuf[2];
1504 a_amv_var_revlookup(&avc, varbuf);
1506 if(isunset)
1507 errs += !a_amv_var_clear(&avc, issetenv);
1508 else
1509 errs += !a_amv_var_set(&avc, cp, issetenv);
1512 NYD2_LEAVE;
1513 return (errs == 0);
1516 FL int
1517 c_define(void *v){
1518 int rv;
1519 char **args;
1520 NYD_ENTER;
1522 rv = 1;
1524 if((args = v)[0] == NULL){
1525 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1526 goto jleave;
1529 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1530 args[2] != NULL){
1531 n_err(_("Synopsis: define: <name> {\n"));
1532 goto jleave;
1535 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1536 jleave:
1537 NYD_LEAVE;
1538 return rv;
1541 FL int
1542 c_undefine(void *v){
1543 int rv;
1544 char **args;
1545 NYD_ENTER;
1547 rv = 0;
1548 args = v;
1550 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1551 while(*++args != NULL);
1552 NYD_LEAVE;
1553 return rv;
1556 FL int
1557 c_call(void *v){
1558 struct a_amv_mac_call_args *amcap;
1559 char const *errs, *name;
1560 struct a_amv_mac *amp;
1561 char **args;
1562 int rv;
1563 NYD_ENTER;
1565 rv = 1;
1567 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1568 errs = _("Synopsis: call: <%s>\n");
1569 name = "name";
1570 goto jerr;
1573 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1574 errs = _("Undefined macro `call'ed: %s\n");
1575 goto jerr;
1578 amcap = n_lofi_alloc(sizeof *amcap);
1579 memset(amcap, 0, sizeof *amcap);
1580 amcap->amca_name = name;
1581 amcap->amca_amp = amp;
1582 rv = (a_amv_mac_exec(amcap) == FAL0);
1583 jleave:
1584 NYD_LEAVE;
1585 return rv;
1586 jerr:
1587 n_err(errs, name);
1588 goto jleave;
1591 FL bool_t
1592 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1593 struct a_amv_mac_call_args *amcap;
1594 struct a_amv_mac *amp;
1595 size_t len;
1596 char *var, *cp;
1597 bool_t rv;
1598 NYD_ENTER;
1600 rv = TRU1;
1601 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1603 /* First try the fully resolved path */
1604 snprintf(var, len, "folder-hook-%s", mailname);
1605 if((cp = vok_vlook(var)) != NULL)
1606 goto jmac;
1608 /* If we are under *folder*, try the usual +NAME syntax, too */
1609 if(displayname[0] == '+'){
1610 char *x;
1612 for(x = &mailname[len]; x != mailname; --x)
1613 if(x[-1] == '/'){
1614 snprintf(var, len, "folder-hook-+%s", x);
1615 if((cp = vok_vlook(var)) != NULL)
1616 goto jmac;
1617 break;
1621 /* Plain *folder-hook* is our last try */
1622 if((cp = ok_vlook(folder_hook)) == NULL)
1623 goto jleave;
1625 jmac:
1626 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1627 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1628 n_shexp_quote_cp(displayname, FAL0), cp);
1629 rv = FAL0;
1630 goto jleave;
1633 amcap = n_lofi_alloc(sizeof *amcap);
1634 memset(amcap, 0, sizeof *amcap);
1635 amcap->amca_name = cp;
1636 amcap->amca_amp = amp;
1637 pstate &= ~PS_HOOK_MASK;
1638 if(nmail){
1639 amcap->amca_unroller = NULL;
1640 pstate |= PS_HOOK_NEWMAIL;
1641 }else{
1642 amcap->amca_unroller = &a_amv_folder_hook_lopts;
1643 pstate |= PS_HOOK;
1645 amcap->amca_lopts_on = TRU1;
1646 amcap->amca_ps_hook_mask = TRU1;
1647 rv = a_amv_mac_exec(amcap);
1648 pstate &= ~PS_HOOK_MASK;
1650 jleave:
1651 NYD_LEAVE;
1652 return rv;
1655 FL int
1656 c_account(void *v){
1657 struct a_amv_mac_call_args *amcap;
1658 struct a_amv_mac *amp;
1659 char **args;
1660 int rv, i, oqf, nqf;
1661 NYD_ENTER;
1663 rv = 1;
1665 if((args = v)[0] == NULL){
1666 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1667 goto jleave;
1670 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1671 if(args[2] != NULL){
1672 n_err(_("Synopsis: account: <name> {\n"));
1673 goto jleave;
1675 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1676 n_err(_("`account': cannot use reserved name: %s\n"),
1677 ACCOUNT_NULL);
1678 goto jleave;
1680 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1681 goto jleave;
1684 if(pstate & PS_HOOK_MASK){
1685 n_err(_("`account': can't change account from within a hook\n"));
1686 goto jleave;
1689 save_mbox_for_possible_quitstuff();
1691 amp = NULL;
1692 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1693 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1694 n_err(_("`account': account does not exist: %s\n"), args[0]);
1695 goto jleave;
1698 oqf = savequitflags();
1700 if(a_amv_acc_curr != NULL){
1701 if(a_amv_acc_curr->am_lopts != NULL)
1702 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1703 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1704 a_amv_mac_free(a_amv_acc_curr);
1707 account_name = (amp != NULL) ? amp->am_name : NULL;
1708 a_amv_acc_curr = amp;
1710 if(amp != NULL){
1711 bool_t ok;
1712 assert(amp->am_lopts == NULL);
1713 amcap = n_lofi_alloc(sizeof *amcap);
1714 memset(amcap, 0, sizeof *amcap);
1715 amcap->amca_name = amp->am_name;
1716 amcap->amca_amp = amp;
1717 amcap->amca_unroller = &amp->am_lopts;
1718 amcap->amca_lopts_on = TRU1;
1719 ok = a_amv_mac_exec(amcap);
1720 if(!ok){
1721 /* XXX account switch incomplete, unroll? */
1722 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1723 goto jleave;
1727 /* C99 */{
1728 bool_t reset = !(pstate & PS_ROOT);
1730 pstate |= PS_ROOT;
1731 if(amp != NULL)
1732 ok_vset(_account_name, amp->am_name);
1733 else
1734 ok_vclear(_account_name);
1735 if(reset)
1736 pstate &= ~PS_ROOT;
1739 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1740 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1741 restorequitflags(oqf);
1742 if((i = setfile("%", 0)) < 0)
1743 goto jleave;
1744 check_folder_hook(FAL0);
1745 if(i > 0 && !ok_blook(emptystart))
1746 goto jleave;
1747 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1748 restorequitflags(nqf);
1750 rv = 0;
1751 jleave:
1752 NYD_LEAVE;
1753 return rv;
1756 FL int
1757 c_unaccount(void *v){
1758 int rv;
1759 char **args;
1760 NYD_ENTER;
1762 rv = 0;
1763 args = v;
1765 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1766 while(*++args != NULL);
1767 NYD_LEAVE;
1768 return rv;
1771 FL int
1772 c_localopts(void *v){
1773 char **argv;
1774 int rv;
1775 NYD_ENTER;
1777 rv = 1;
1779 if(a_amv_lopts == NULL){
1780 n_err(_("Cannot use `localopts' in this context "
1781 "(not in `define' or `account', nor special hook)\n"));
1782 goto jleave;
1785 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1786 rv = 0;
1787 jleave:
1788 NYD_LEAVE;
1789 return rv;
1792 FL void
1793 temporary_call_compose_mode_hook(char const *macname,
1794 void (*hook_pre)(void *), void *hook_arg){
1795 /* TODO call_compose_mode_hook() temporary, v15: generalize; see a_LEX_SLICE
1796 * TODO comment in lex_input.c for the right way of doing things! */
1797 static struct a_amv_lostack *cmh_losp;
1798 struct a_amv_mac_call_args *amcap;
1799 struct a_amv_mac *amp;
1800 NYD_ENTER;
1802 amp = NULL;
1804 if(macname == (char*)-1){
1805 a_amv_mac__finalize(cmh_losp);
1806 cmh_losp = NULL;
1807 }else if(macname != NULL &&
1808 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1809 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1810 macname);
1811 else{
1812 amcap = n_lofi_alloc(sizeof *amcap);
1813 memset(amcap, 0, sizeof *amcap);
1814 amcap->amca_name = (macname != NULL) ? macname : "on-compose-done-shell";
1815 amcap->amca_amp = amp;
1816 amcap->amca_unroller = &a_amv_compose_lopts;
1817 amcap->amca_hook_pre = hook_pre;
1818 amcap->amca_hook_arg = hook_arg;
1819 amcap->amca_lopts_on = TRU1;
1820 amcap->amca_ps_hook_mask = TRU1;
1821 pstate &= ~PS_HOOK_MASK;
1822 pstate |= PS_HOOK;
1823 if(macname != NULL)
1824 a_amv_mac_exec(amcap);
1825 else{
1826 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
1827 cmh_losp->as_global_saved = a_amv_lopts;
1828 cmh_losp->as_up = NULL;
1829 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
1830 cmh_losp->as_unroll = TRU1;
1831 a_amv_lopts = cmh_losp;
1834 NYD_LEAVE;
1837 FL void
1838 temporary_unroll_compose_mode(void){ /* XXX intermediate hack */
1839 NYD_ENTER;
1840 if(a_amv_compose_lopts != NULL){
1841 void *save = a_amv_lopts;
1843 a_amv_lopts = NULL;
1844 a_amv_lopts_unroll(&a_amv_compose_lopts);
1845 a_amv_compose_lopts = NULL;
1846 a_amv_lopts = save;
1848 NYD_LEAVE;
1851 FL void
1852 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1853 NYD_ENTER;
1854 if(a_amv_folder_hook_lopts != NULL){
1855 void *save = a_amv_lopts;
1857 a_amv_lopts = NULL;
1858 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1859 a_amv_folder_hook_lopts = NULL;
1860 a_amv_lopts = save;
1862 NYD_LEAVE;
1865 FL char *
1866 n_var_oklook(enum okeys okey){
1867 struct a_amv_var_carrier avc;
1868 char *rv;
1869 struct a_amv_var_map const *avmp;
1870 NYD_ENTER;
1872 avc.avc_map = avmp = &a_amv_var_map[okey];
1873 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1874 avc.avc_hash = avmp->avm_hash;
1875 avc.avc_okey = okey;
1877 if(a_amv_var_lookup(&avc, FAL0))
1878 rv = avc.avc_var->av_value;
1879 else
1880 rv = NULL;
1881 NYD_LEAVE;
1882 return rv;
1885 FL bool_t
1886 n_var_okset(enum okeys okey, uintptr_t val){
1887 struct a_amv_var_carrier avc;
1888 bool_t ok;
1889 struct a_amv_var_map const *avmp;
1890 NYD_ENTER;
1892 avc.avc_map = avmp = &a_amv_var_map[okey];
1893 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1894 avc.avc_hash = avmp->avm_hash;
1895 avc.avc_okey = okey;
1897 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1898 NYD_LEAVE;
1899 return ok;
1902 FL bool_t
1903 n_var_okclear(enum okeys okey){
1904 struct a_amv_var_carrier avc;
1905 bool_t rv;
1906 struct a_amv_var_map const *avmp;
1907 NYD_ENTER;
1909 avc.avc_map = avmp = &a_amv_var_map[okey];
1910 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1911 avc.avc_hash = avmp->avm_hash;
1912 avc.avc_okey = okey;
1914 rv = a_amv_var_clear(&avc, FAL0);
1915 NYD_LEAVE;
1916 return rv;
1919 FL char *
1920 n_var_voklook(char const *vokey){
1921 struct a_amv_var_carrier avc;
1922 char *rv;
1923 NYD_ENTER;
1925 a_amv_var_revlookup(&avc, vokey);
1927 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1928 NYD_LEAVE;
1929 return rv;
1932 FL bool_t
1933 n_var_vokset(char const *vokey, uintptr_t val){
1934 struct a_amv_var_carrier avc;
1935 bool_t ok;
1936 NYD_ENTER;
1938 a_amv_var_revlookup(&avc, vokey);
1940 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1941 NYD_LEAVE;
1942 return ok;
1945 FL bool_t
1946 n_var_vokclear(char const *vokey){
1947 struct a_amv_var_carrier avc;
1948 bool_t ok;
1949 NYD_ENTER;
1951 a_amv_var_revlookup(&avc, vokey);
1953 ok = a_amv_var_clear(&avc, FAL0);
1954 NYD_LEAVE;
1955 return ok;
1958 #ifdef HAVE_SOCKETS
1959 FL char *
1960 n_var_xoklook(enum okeys okey, struct url const *urlp,
1961 enum okey_xlook_mode oxm){
1962 struct a_amv_var_carrier avc;
1963 struct str const *us;
1964 size_t nlen;
1965 char *nbuf, *rv;
1966 struct a_amv_var_map const *avmp;
1967 NYD_ENTER;
1969 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1971 /* For simplicity: allow this case too */
1972 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
1973 nbuf = NULL;
1974 goto jplain;
1977 avc.avc_map = avmp = &a_amv_var_map[okey];
1978 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1979 avc.avc_okey = okey;
1981 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1982 nlen = strlen(avc.avc_name);
1983 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
1984 memcpy(nbuf, avc.avc_name, nlen);
1985 nbuf[nlen++] = '-';
1987 /* One of .url_u_h_p and .url_h_p we test in here */
1988 memcpy(nbuf + nlen, us->s, us->l +1);
1989 avc.avc_name = a_amv_var_canonify(nbuf);
1990 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1991 if(a_amv_var_lookup(&avc, FAL0))
1992 goto jvar;
1994 /* The second */
1995 if(oxm & OXM_H_P){
1996 us = &urlp->url_h_p;
1997 memcpy(nbuf + nlen, us->s, us->l +1);
1998 avc.avc_name = a_amv_var_canonify(nbuf);
1999 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2000 if(a_amv_var_lookup(&avc, FAL0)){
2001 jvar:
2002 rv = avc.avc_var->av_value;
2003 goto jleave;
2007 jplain:
2008 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2009 jleave:
2010 if(nbuf != NULL)
2011 n_lofi_free(nbuf);
2012 NYD_LEAVE;
2013 return rv;
2015 #endif /* HAVE_SOCKETS */
2017 FL int
2018 c_set(void *v){
2019 char **ap;
2020 int err;
2021 NYD_ENTER;
2023 if(*(ap = v) == NULL){
2024 a_amv_var_show_all();
2025 err = 0;
2026 }else
2027 err = !a_amv_var_c_set(ap, FAL0);
2028 NYD_LEAVE;
2029 return err;
2032 FL int
2033 c_unset(void *v){
2034 char **ap;
2035 int err;
2036 NYD_ENTER;
2038 for(err = 0, ap = v; *ap != NULL; ++ap)
2039 err |= !n_var_vokclear(*ap);
2040 NYD_LEAVE;
2041 return err;
2044 FL int
2045 c_varshow(void *v){
2046 char **ap;
2047 NYD_ENTER;
2049 if(*(ap = v) == NULL)
2050 v = NULL;
2051 else{
2052 struct n_string msg, *msgp = &msg;
2054 msgp = n_string_creat(msgp);
2055 for(; *ap != NULL; ++ap)
2056 a_amv_var_show(*ap, stdout, msgp);
2057 n_string_gut(msgp);
2059 NYD_LEAVE;
2060 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2063 FL int
2064 c_varedit(void *v){
2065 struct a_amv_var_carrier avc;
2066 FILE *of, *nf;
2067 char *val, **argv;
2068 int err;
2069 sighandler_type sigint;
2070 NYD_ENTER;
2072 sigint = safe_signal(SIGINT, SIG_IGN);
2074 for(err = 0, argv = v; *argv != NULL; ++argv){
2075 memset(&avc, 0, sizeof avc);
2077 a_amv_var_revlookup(&avc, *argv);
2079 if(avc.avc_map != NULL){
2080 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2081 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2082 avc.avc_name);
2083 continue;
2085 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2086 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2087 avc.avc_name);
2088 continue;
2092 a_amv_var_lookup(&avc, FAL0);
2094 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2095 NULL){
2096 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2097 err = 1;
2098 break;
2099 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2100 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2101 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2102 Fclose(of);
2103 err = 1;
2104 continue;
2107 fflush_rewind(of);
2108 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2109 Fclose(of);
2111 if(nf != NULL){
2112 int c;
2113 char *base;
2114 off_t l;
2116 l = fsize(nf);
2117 assert(l >= 0);
2118 base = salloc((size_t)l +1);
2120 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2121 if(c == '\n' || c == '\r'){
2122 *val = ' ';
2123 ++l;
2124 }else{
2125 *val = (char)(uc_i)c;
2126 l = 0;
2128 val -= l;
2129 *val = '\0';
2131 if(!a_amv_var_set(&avc, base, FAL0))
2132 err = 1;
2134 Fclose(nf);
2135 }else{
2136 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2137 err = 1;
2138 break;
2142 safe_signal(SIGINT, sigint);
2143 NYD_LEAVE;
2144 return err;
2147 FL int
2148 c_environ(void *v){
2149 struct a_amv_var_carrier avc;
2150 int err;
2151 char **ap;
2152 bool_t islnk;
2153 NYD_ENTER;
2155 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2156 for(err = 0; *++ap != NULL;){
2157 a_amv_var_revlookup(&avc, *ap);
2159 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2160 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2161 if(!islnk){
2162 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2163 continue;
2164 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2165 if(options & OPT_D_V)
2166 n_err(_("`environ': link: already established: %s\n"), *ap);
2167 continue;
2169 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2170 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2171 a_amv_var__putenv(&avc, avc.avc_var);
2172 }else if(!islnk){
2173 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2174 err = 1;
2175 }else{
2176 char const *evp = getenv(*ap);
2178 if(evp != NULL)
2179 err |= !a_amv_var_set(&avc, evp, TRU1);
2180 else{
2181 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2182 *ap);
2183 err = 1;
2187 }else if(!strcmp(*ap, "set"))
2188 err = !a_amv_var_c_set(++ap, TRU1);
2189 else if(!strcmp(*ap, "unset")){
2190 for(err = 0; *++ap != NULL;){
2191 a_amv_var_revlookup(&avc, *ap);
2193 if(!a_amv_var_clear(&avc, TRU1))
2194 err = 1;
2196 }else{
2197 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2198 err = 1;
2200 NYD_LEAVE;
2201 return err;
2204 /* s-it-mode */