privsep.c: and just verify the box is also in CWD (wapiflapi)
[s-mailx.git] / accmacvar.c
blob70b5a491f7919140de1a204233387ef8ee5c1650
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 - 2017 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;
996 goto jnewval;
997 }else
998 goto jnewval;
1002 /* A first-time init switch is to be handled now and here */
1003 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_I3VAL) != 0)){
1004 static struct a_amv_var_defval const **arr,
1005 *arr_base[a_AMV_VAR_I3VALS_CNT +1];
1007 if(arr == NULL){
1008 arr = &arr_base[0];
1009 arr[i = a_AMV_VAR_I3VALS_CNT] = NULL;
1010 while(i-- > 0)
1011 arr[i] = &a_amv_var_i3vals[i];
1014 for(i = 0; arr[i] != NULL; ++i)
1015 if(arr[i]->avdv_okey == avcp->avc_okey){
1016 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1017 : arr[i]->avdv_value;
1018 /* Remove this entry, hope entire block becomes no-op asap */
1020 arr[i] = arr[i + 1];
1021 while(arr[i++] != NULL);
1023 if(!i3val_nonew)
1024 goto jnewval;
1025 if(i3val_nonew == TRUM1)
1026 avp = (struct a_amv_var*)-1;
1027 goto jleave;
1031 /* The virtual variables */
1032 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_VIRT) != 0)){
1033 for(i = 0; i < a_AMV_VAR_VIRTS_CNT; ++i)
1034 if(a_amv_var_virts[i].avv_okey == avcp->avc_okey){
1035 avp = n_UNCONST(a_amv_var_virts[i].avv_var);
1036 goto jleave;
1038 /* Not reached */
1041 /* Place this last because once it is set first the variable will never
1042 * be removed again and thus match in the first block above */
1043 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_DEFVAL) != 0){
1044 for(i = 0; i < a_AMV_VAR_DEFVALS_CNT; ++i)
1045 if(a_amv_var_defvals[i].avdv_okey == avcp->avc_okey){
1046 cp = (avmp->avm_flags & a_AMV_VF_BOOL) ? n_empty
1047 : a_amv_var_defvals[i].avdv_value;
1048 goto jnewval;
1053 jerr:
1054 avp = NULL;
1055 jleave:
1056 avcp->avc_var = avp;
1057 NYD2_LEAVE;
1058 return (avp != NULL);
1060 jnewval: /* C99 */{
1061 struct a_amv_var **avpp;
1062 size_t l;
1064 l = strlen(avcp->avc_name) +1;
1065 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1066 ) + l);
1067 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1068 *avpp = avp;
1069 memcpy(avp->av_name, avcp->avc_name, l);
1070 avp->av_value = a_amv_var_copy(cp);
1071 #ifdef HAVE_PUTENV
1072 avp->av_env = NULL;
1073 #endif
1074 avp->av_flags = avmp->avm_flags;
1076 if(avp->av_flags & a_AMV_VF_VIP)
1077 a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value);
1078 if(avp->av_flags & a_AMV_VF_ENV)
1079 a_amv_var__putenv(avcp, avp);
1080 goto jleave;
1084 static bool_t
1085 a_amv_var_set(struct a_amv_var_carrier *avcp, char const *value,
1086 bool_t force_env){
1087 struct a_amv_var *avp;
1088 char *oval;
1089 struct a_amv_var_map const *avmp;
1090 bool_t rv;
1091 NYD2_ENTER;
1093 if(value == NULL){
1094 rv = a_amv_var_clear(avcp, force_env);
1095 goto jleave;
1098 if((avmp = avcp->avc_map) != NULL){
1099 rv = FAL0;
1101 /* Validity checks */
1102 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_RDONLY) != 0 &&
1103 !(pstate & PS_ROOT))){
1104 value = N_("Variable is readonly: %s\n");
1105 goto jeavmp;
1107 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOTEMPTY) && *value == '\0')){
1108 value = N_("Variable must not be empty: %s\n");
1109 goto jeavmp;
1111 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NOCNTRLS) != 0 &&
1112 !a_amv_var_check_nocntrls(value))){
1113 value = N_("Variable forbids control characters: %s\n");
1114 goto jeavmp;
1116 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NUM) &&
1117 !a_amv_var_check_num(value, FAL0))){
1118 value = N_("Variable value not a number or out of range: %s\n");
1119 goto jeavmp;
1121 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_POSNUM) &&
1122 !a_amv_var_check_num(value, TRU1))){
1123 value = _("Variable value not a number, negative, "
1124 "or out of range: %s\n");
1125 goto jeavmp;
1127 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_IMPORT) != 0 &&
1128 !(pstate & (PS_ROOT | PS_STARTED)))){
1129 value = N_("Variable cannot be set in a resource file: %s\n");
1130 jeavmp:
1131 n_err(V_(value), avcp->avc_name);
1132 goto jleave;
1135 /* Transformations */
1136 if(n_UNLIKELY(avmp->avm_flags & a_AMV_VF_LOWER)){
1137 char c;
1139 oval = savestr(value);
1140 value = oval;
1141 for(; (c = *oval) != '\0'; ++oval)
1142 *oval = lowerconv(c);
1146 rv = TRU1;
1147 a_amv_var_lookup(avcp, TRU1);
1149 /* Don't care what happens later on, store this in the unroll list */
1150 if(a_amv_lopts != NULL &&
1151 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1152 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1154 if((avp = avcp->avc_var) == NULL){
1155 struct a_amv_var **avpp;
1156 size_t l;
1158 l = strlen(avcp->avc_name) +1;
1159 avcp->avc_var = avp = smalloc(n_VSTRUCT_SIZEOF(struct a_amv_var, av_name
1160 ) + l);
1161 avp->av_link = *(avpp = &a_amv_vars[avcp->avc_prime]);
1162 *avpp = avp;
1163 #ifdef HAVE_PUTENV
1164 avp->av_env = NULL;
1165 #endif
1166 memcpy(avp->av_name, avcp->avc_name, l);
1167 avp->av_flags = (avmp != NULL) ? avmp->avm_flags : 0;
1168 oval = n_UNCONST(n_empty);
1169 }else
1170 oval = avp->av_value;
1172 if(avmp == NULL)
1173 avp->av_value = a_amv_var_copy(value);
1174 else{
1175 /* Via `set' etc. the user may give even boolean options non-boolean
1176 * values, ignore that and force boolean */
1177 if(avp->av_flags & a_AMV_VF_BOOL){
1178 if(!(pstate & PS_ROOT) && (options & OPT_D_VV) && *value != '\0')
1179 n_err(_("Ignoring value of boolean variable: %s: %s\n"),
1180 avcp->avc_name, value);
1181 avp->av_value = n_UNCONST(a_amv_var_1);
1182 }else
1183 avp->av_value = a_amv_var_copy(value);
1185 /* Check if update allowed XXX wasteful on error! */
1186 if((avp->av_flags & a_AMV_VF_VIP) &&
1187 !(rv = a_amv_var_check_vips(avcp->avc_okey, TRU1, &avp->av_value))){
1188 char *cp = avp->av_value;
1190 avp->av_value = oval;
1191 oval = cp;
1195 if(force_env && !(avp->av_flags & a_AMV_VF_ENV))
1196 avp->av_flags |= a_AMV_VF_LINKED;
1197 if(avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED))
1198 rv = a_amv_var__putenv(avcp, avp);
1200 a_amv_var_free(oval);
1201 jleave:
1202 NYD2_LEAVE;
1203 return rv;
1206 static bool_t
1207 a_amv_var__putenv(struct a_amv_var_carrier *avcp, struct a_amv_var *avp){
1208 #ifndef HAVE_SETENV
1209 char *cp;
1210 #endif
1211 bool_t rv;
1212 NYD2_ENTER;
1214 #ifdef HAVE_SETENV
1215 rv = (setenv(avcp->avc_name, avp->av_value, 1) == 0);
1216 #else
1217 cp = sstrdup(savecatsep(avcp->avc_name, '=', avp->av_value));
1219 if((rv = (putenv(cp) == 0))){
1220 char *ocp;
1222 if((ocp = avp->av_env) != NULL)
1223 free(ocp);
1224 avp->av_env = cp;
1225 }else
1226 free(cp);
1227 #endif
1228 NYD2_LEAVE;
1229 return rv;
1232 static bool_t
1233 a_amv_var_clear(struct a_amv_var_carrier *avcp, bool_t force_env){
1234 struct a_amv_var **avpp, *avp;
1235 struct a_amv_var_map const *avmp;
1236 bool_t rv;
1237 NYD2_ENTER;
1239 rv = FAL0;
1241 if(n_LIKELY((avmp = avcp->avc_map) != NULL)){
1242 if(n_UNLIKELY((avmp->avm_flags & a_AMV_VF_NODEL) != 0 &&
1243 !(pstate & PS_ROOT))){
1244 n_err(_("Variable may not be unset: %s\n"), avcp->avc_name);
1245 goto jleave;
1247 if((avmp->avm_flags & a_AMV_VF_VIP) &&
1248 !a_amv_var_check_vips(avcp->avc_okey, FAL0, NULL))
1249 goto jleave;
1252 rv = TRU1;
1254 if(n_UNLIKELY(!a_amv_var_lookup(avcp, TRUM1))){
1255 if(force_env){
1256 jforce_env:
1257 rv = a_amv_var__clearenv(avcp->avc_name, NULL);
1258 }else if(!(pstate & (PS_ROOT | PS_ROBOT)) && (options & OPT_D_V))
1259 n_err(_("Can't unset undefined variable: %s\n"), avcp->avc_name);
1260 goto jleave;
1261 }else if(avcp->avc_var == (struct a_amv_var*)-1){
1262 avcp->avc_var = NULL;
1263 if(force_env)
1264 goto jforce_env;
1265 goto jleave;
1268 if(a_amv_lopts != NULL &&
1269 (avmp == NULL || !(avmp->avm_flags & a_AMV_VF_NOLOPTS)))
1270 a_amv_lopts_add(a_amv_lopts, avcp->avc_name, avcp->avc_var);
1272 avp = avcp->avc_var;
1273 avcp->avc_var = NULL;
1274 avpp = &a_amv_vars[avcp->avc_prime];
1275 assert(*avpp == avp); /* (always listhead after lookup()) */
1276 *avpp = (*avpp)->av_link;
1278 /* C99 */{
1279 #ifdef HAVE_SETENV
1280 char *envval = NULL;
1281 #else
1282 char *envval = avp->av_env;
1283 #endif
1284 if((avp->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)) || envval != NULL)
1285 rv = a_amv_var__clearenv(avp->av_name, envval);
1287 a_amv_var_free(avp->av_value);
1288 free(avp);
1290 /* XXX Fun part, extremely simple-minded for now: if this variable has
1291 * XXX a default value, immediately reinstantiate it! */
1292 if(n_UNLIKELY(avmp != NULL && (avmp->avm_flags & a_AMV_VF_DEFVAL) != 0))
1293 a_amv_var_lookup(avcp, TRU1);
1294 jleave:
1295 NYD2_LEAVE;
1296 return rv;
1299 static bool_t
1300 a_amv_var__clearenv(char const *name, char *value){
1301 #ifndef HAVE_SETENV
1302 extern char **environ;
1303 char **ecpp;
1304 #endif
1305 bool_t rv;
1306 NYD2_ENTER;
1307 n_UNUSED(value);
1309 #ifdef HAVE_SETENV
1310 unsetenv(name);
1311 rv = TRU1;
1312 #else
1313 if(value != NULL)
1314 for(ecpp = environ; *ecpp != NULL; ++ecpp)
1315 if(*ecpp == value){
1316 free(value);
1318 ecpp[0] = ecpp[1];
1319 while(*ecpp++ != NULL);
1320 break;
1322 rv = TRU1;
1323 #endif
1324 NYD2_LEAVE;
1325 return rv;
1328 static void
1329 a_amv_var_show_all(void){
1330 struct n_string msg, *msgp;
1331 FILE *fp;
1332 size_t no, i;
1333 struct a_amv_var *avp;
1334 char const **vacp, **cap;
1335 NYD2_ENTER;
1337 if((fp = Ftmp(NULL, "setlist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1338 n_perr(_("Can't create temporary file for `set' listing"), 0);
1339 goto jleave;
1342 /* We need to instantiate first-time-inits and default values here, so that
1343 * they will be regular members of our _vars[] table */
1344 for(i = a_AMV_VAR_I3VALS_CNT; i-- > 0;)
1345 n_var_oklook(a_amv_var_i3vals[i].avdv_okey);
1346 for(i = a_AMV_VAR_DEFVALS_CNT; i-- > 0;)
1347 n_var_oklook(a_amv_var_defvals[i].avdv_okey);
1349 for(no = i = 0; i < a_AMV_PRIME; ++i)
1350 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1351 ++no;
1352 no += a_AMV_VAR_VIRTS_CNT;
1354 vacp = salloc(no * sizeof(*vacp));
1356 for(cap = vacp, i = 0; i < a_AMV_PRIME; ++i)
1357 for(avp = a_amv_vars[i]; avp != NULL; avp = avp->av_link)
1358 *cap++ = avp->av_name;
1359 for(i = a_AMV_VAR_VIRTS_CNT; i-- > 0;)
1360 *cap++ = a_amv_var_virts[i].avv_var->av_name;
1362 if(no > 1)
1363 qsort(vacp, no, sizeof *vacp, &a_amv_var__show_cmp);
1365 msgp = &msg;
1366 msgp = n_string_reserve(n_string_creat(msgp), 80);
1367 for(i = 0, cap = vacp; no != 0; ++cap, --no)
1368 i += a_amv_var_show(*cap, fp, msgp);
1369 n_string_gut(&msg);
1371 page_or_print(fp, i);
1372 Fclose(fp);
1373 jleave:
1374 NYD2_LEAVE;
1377 static int
1378 a_amv_var__show_cmp(void const *s1, void const *s2){
1379 int rv;
1380 NYD2_ENTER;
1382 rv = strcmp(*(char**)n_UNCONST(s1), *(char**)n_UNCONST(s2));
1383 NYD2_LEAVE;
1384 return rv;
1387 static size_t
1388 a_amv_var_show(char const *name, FILE *fp, struct n_string *msgp){
1389 struct a_amv_var_carrier avc;
1390 char const *quote;
1391 size_t i;
1392 NYD2_ENTER;
1394 msgp = n_string_trunc(msgp, 0);
1395 i = 0;
1397 a_amv_var_revlookup(&avc, name);
1398 if(!a_amv_var_lookup(&avc, FAL0)){
1399 struct str s;
1401 msgp = n_string_assign_cp(msgp, _("No such variable: "));
1402 s.s = n_UNCONST(name);
1403 s.l = UIZ_MAX;
1404 msgp = n_shexp_quote(msgp, &s, FAL0);
1405 goto jleave;
1408 if(options & OPT_D_V){
1409 if(avc.avc_map == NULL){
1410 msgp = n_string_push_c(msgp, '#');
1411 msgp = n_string_push_cp(msgp, "assembled");
1412 i = 1;
1414 /* C99 */{
1415 struct{
1416 ui16_t flags;
1417 char msg[22];
1418 } const tbase[] = {
1419 {a_AMV_VF_VIRT, "virtual"},
1420 {a_AMV_VF_RDONLY, "readonly"},
1421 {a_AMV_VF_NODEL, "nodelete"},
1422 {a_AMV_VF_NOTEMPTY, "notempty"},
1423 {a_AMV_VF_NOCNTRLS, "no-control-chars"},
1424 {a_AMV_VF_NUM, "number"},
1425 {a_AMV_VF_POSNUM, "positive-number"},
1426 {a_AMV_VF_IMPORT, "import-environ-first\0"},
1427 {a_AMV_VF_ENV, "sync-environ"},
1428 {a_AMV_VF_I3VAL, "initial-value"},
1429 {a_AMV_VF_DEFVAL, "default-value"},
1430 {a_AMV_VF_LINKED, "`environ' link"}
1431 }, *tp;
1433 for(tp = tbase; PTRCMP(tp, <, &tbase[n_NELEM(tbase)]); ++tp)
1434 if(avc.avc_var->av_flags & tp->flags){
1435 msgp = n_string_push_c(msgp, (i++ == 0 ? '#' : ','));
1436 msgp = n_string_push_cp(msgp, tp->msg);
1440 if(i > 0)
1441 msgp = n_string_push_cp(msgp, "\n ");
1444 if(avc.avc_var->av_flags & a_AMV_VF_RDONLY)
1445 msgp = n_string_push_cp(msgp, "# ");
1446 n_UNINIT(quote, NULL);
1447 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1448 quote = n_shexp_quote_cp(avc.avc_var->av_value, TRU1);
1449 if(strcmp(quote, avc.avc_var->av_value))
1450 msgp = n_string_push_cp(msgp, "wysh ");
1452 if(avc.avc_var->av_flags & a_AMV_VF_LINKED)
1453 msgp = n_string_push_cp(msgp, "environ ");
1454 msgp = n_string_push_cp(msgp, "set ");
1455 msgp = n_string_push_cp(msgp, name);
1456 if(!(avc.avc_var->av_flags & a_AMV_VF_BOOL)){
1457 msgp = n_string_push_c(msgp, '=');
1458 msgp = n_string_push_cp(msgp, quote);
1461 jleave:
1462 msgp = n_string_push_c(msgp, '\n');
1463 fputs(n_string_cp(msgp), fp);
1464 NYD2_ENTER;
1465 return (i > 0 ? 2 : 1);
1468 static bool_t
1469 a_amv_var_c_set(char **ap, bool_t issetenv){
1470 char *cp, *cp2, *varbuf, c;
1471 size_t errs;
1472 NYD2_ENTER;
1474 errs = 0;
1475 jouter:
1476 while((cp = *ap++) != NULL){
1477 /* Isolate key */
1478 cp2 = varbuf = salloc(strlen(cp) +1);
1480 for(; (c = *cp) != '=' && c != '\0'; ++cp){
1481 if(cntrlchar(c) || whitechar(c)){
1482 n_err(_("Variable name with control character ignored: %s\n"),
1483 ap[-1]);
1484 ++errs;
1485 goto jouter;
1487 *cp2++ = c;
1489 *cp2 = '\0';
1490 if(c == '\0')
1491 cp = n_UNCONST(n_empty);
1492 else
1493 ++cp;
1495 if(varbuf == cp2){
1496 n_err(_("Empty variable name ignored\n"));
1497 ++errs;
1498 }else{
1499 struct a_amv_var_carrier avc;
1500 bool_t isunset;
1502 if((isunset = (varbuf[0] == 'n' && varbuf[1] == 'o')))
1503 varbuf = &varbuf[2];
1505 a_amv_var_revlookup(&avc, varbuf);
1507 if(isunset)
1508 errs += !a_amv_var_clear(&avc, issetenv);
1509 else
1510 errs += !a_amv_var_set(&avc, cp, issetenv);
1513 NYD2_LEAVE;
1514 return (errs == 0);
1517 FL int
1518 c_define(void *v){
1519 int rv;
1520 char **args;
1521 NYD_ENTER;
1523 rv = 1;
1525 if((args = v)[0] == NULL){
1526 rv = (a_amv_mac_show(a_AMV_MF_NONE) == FAL0);
1527 goto jleave;
1530 if(args[1] == NULL || args[1][0] != '{' || args[1][1] != '\0' ||
1531 args[2] != NULL){
1532 n_err(_("Synopsis: define: <name> {\n"));
1533 goto jleave;
1536 rv = (a_amv_mac_def(args[0], a_AMV_MF_NONE) == FAL0);
1537 jleave:
1538 NYD_LEAVE;
1539 return rv;
1542 FL int
1543 c_undefine(void *v){
1544 int rv;
1545 char **args;
1546 NYD_ENTER;
1548 rv = 0;
1549 args = v;
1551 rv |= !a_amv_mac_undef(*args, a_AMV_MF_NONE);
1552 while(*++args != NULL);
1553 NYD_LEAVE;
1554 return rv;
1557 FL int
1558 c_call(void *v){
1559 struct a_amv_mac_call_args *amcap;
1560 char const *errs, *name;
1561 struct a_amv_mac *amp;
1562 char **args;
1563 int rv;
1564 NYD_ENTER;
1566 rv = 1;
1568 if((args = v)[0] == NULL || (args[1] != NULL && args[2] != NULL)){
1569 errs = _("Synopsis: call: <%s>\n");
1570 name = "name";
1571 goto jerr;
1574 if((amp = a_amv_mac_lookup(name = *args, NULL, a_AMV_MF_NONE)) == NULL){
1575 errs = _("Undefined macro `call'ed: %s\n");
1576 goto jerr;
1579 amcap = n_lofi_alloc(sizeof *amcap);
1580 memset(amcap, 0, sizeof *amcap);
1581 amcap->amca_name = name;
1582 amcap->amca_amp = amp;
1583 rv = (a_amv_mac_exec(amcap) == FAL0);
1584 jleave:
1585 NYD_LEAVE;
1586 return rv;
1587 jerr:
1588 n_err(errs, name);
1589 goto jleave;
1592 FL bool_t
1593 check_folder_hook(bool_t nmail){ /* TODO temporary, v15: drop */
1594 struct a_amv_mac_call_args *amcap;
1595 struct a_amv_mac *amp;
1596 size_t len;
1597 char *var, *cp;
1598 bool_t rv;
1599 NYD_ENTER;
1601 rv = TRU1;
1602 var = salloc(len = strlen(mailname) + sizeof("folder-hook-") -1 +1);
1604 /* First try the fully resolved path */
1605 snprintf(var, len, "folder-hook-%s", mailname);
1606 if((cp = vok_vlook(var)) != NULL)
1607 goto jmac;
1609 /* If we are under *folder*, try the usual +NAME syntax, too */
1610 if(displayname[0] == '+'){
1611 char *x;
1613 for(x = &mailname[len]; x != mailname; --x)
1614 if(x[-1] == '/'){
1615 snprintf(var, len, "folder-hook-+%s", x);
1616 if((cp = vok_vlook(var)) != NULL)
1617 goto jmac;
1618 break;
1622 /* Plain *folder-hook* is our last try */
1623 if((cp = ok_vlook(folder_hook)) == NULL)
1624 goto jleave;
1626 jmac:
1627 if((amp = a_amv_mac_lookup(cp, NULL, a_AMV_MF_NONE)) == NULL){
1628 n_err(_("Cannot call *folder-hook* for %s: macro does not exist: %s\n"),
1629 n_shexp_quote_cp(displayname, FAL0), cp);
1630 rv = FAL0;
1631 goto jleave;
1634 amcap = n_lofi_alloc(sizeof *amcap);
1635 memset(amcap, 0, sizeof *amcap);
1636 amcap->amca_name = cp;
1637 amcap->amca_amp = amp;
1638 pstate &= ~PS_HOOK_MASK;
1639 if(nmail){
1640 amcap->amca_unroller = NULL;
1641 pstate |= PS_HOOK_NEWMAIL;
1642 }else{
1643 amcap->amca_unroller = &a_amv_folder_hook_lopts;
1644 pstate |= PS_HOOK;
1646 amcap->amca_lopts_on = TRU1;
1647 amcap->amca_ps_hook_mask = TRU1;
1648 rv = a_amv_mac_exec(amcap);
1649 pstate &= ~PS_HOOK_MASK;
1651 jleave:
1652 NYD_LEAVE;
1653 return rv;
1656 FL int
1657 c_account(void *v){
1658 struct a_amv_mac_call_args *amcap;
1659 struct a_amv_mac *amp;
1660 char **args;
1661 int rv, i, oqf, nqf;
1662 NYD_ENTER;
1664 rv = 1;
1666 if((args = v)[0] == NULL){
1667 rv = (a_amv_mac_show(a_AMV_MF_ACC) == FAL0);
1668 goto jleave;
1671 if(args[1] && args[1][0] == '{' && args[1][1] == '\0'){
1672 if(args[2] != NULL){
1673 n_err(_("Synopsis: account: <name> {\n"));
1674 goto jleave;
1676 if(!asccasecmp(args[0], ACCOUNT_NULL)){
1677 n_err(_("`account': cannot use reserved name: %s\n"),
1678 ACCOUNT_NULL);
1679 goto jleave;
1681 rv = (a_amv_mac_def(args[0], a_AMV_MF_ACC) == FAL0);
1682 goto jleave;
1685 if(pstate & PS_HOOK_MASK){
1686 n_err(_("`account': can't change account from within a hook\n"));
1687 goto jleave;
1690 save_mbox_for_possible_quitstuff();
1692 amp = NULL;
1693 if(asccasecmp(args[0], ACCOUNT_NULL) != 0 &&
1694 (amp = a_amv_mac_lookup(args[0], NULL, a_AMV_MF_ACC)) == NULL) {
1695 n_err(_("`account': account does not exist: %s\n"), args[0]);
1696 goto jleave;
1699 oqf = savequitflags();
1701 if(a_amv_acc_curr != NULL){
1702 if(a_amv_acc_curr->am_lopts != NULL)
1703 a_amv_lopts_unroll(&a_amv_acc_curr->am_lopts);
1704 if(a_amv_acc_curr->am_flags & a_AMV_MF_DEL)
1705 a_amv_mac_free(a_amv_acc_curr);
1708 account_name = (amp != NULL) ? amp->am_name : NULL;
1709 a_amv_acc_curr = amp;
1711 if(amp != NULL){
1712 bool_t ok;
1713 assert(amp->am_lopts == NULL);
1714 amcap = n_lofi_alloc(sizeof *amcap);
1715 memset(amcap, 0, sizeof *amcap);
1716 amcap->amca_name = amp->am_name;
1717 amcap->amca_amp = amp;
1718 amcap->amca_unroller = &amp->am_lopts;
1719 amcap->amca_lopts_on = TRU1;
1720 ok = a_amv_mac_exec(amcap);
1721 if(!ok){
1722 /* XXX account switch incomplete, unroll? */
1723 n_err(_("`account': failed to switch to account: %s\n"), amp->am_name);
1724 goto jleave;
1728 /* C99 */{
1729 bool_t reset = !(pstate & PS_ROOT);
1731 pstate |= PS_ROOT;
1732 if(amp != NULL)
1733 ok_vset(_account_name, amp->am_name);
1734 else
1735 ok_vclear(_account_name);
1736 if(reset)
1737 pstate &= ~PS_ROOT;
1740 if((pstate & (PS_STARTED | PS_HOOK_MASK)) == PS_STARTED){
1741 nqf = savequitflags(); /* TODO obsolete (leave -> void -> new box!) */
1742 restorequitflags(oqf);
1743 if((i = setfile("%", 0)) < 0)
1744 goto jleave;
1745 check_folder_hook(FAL0);
1746 if(i > 0 && !ok_blook(emptystart))
1747 goto jleave;
1748 announce(ok_blook(bsdcompat) || ok_blook(bsdannounce));
1749 restorequitflags(nqf);
1751 rv = 0;
1752 jleave:
1753 NYD_LEAVE;
1754 return rv;
1757 FL int
1758 c_unaccount(void *v){
1759 int rv;
1760 char **args;
1761 NYD_ENTER;
1763 rv = 0;
1764 args = v;
1766 rv |= !a_amv_mac_undef(*args, a_AMV_MF_ACC);
1767 while(*++args != NULL);
1768 NYD_LEAVE;
1769 return rv;
1772 FL int
1773 c_localopts(void *v){
1774 char **argv;
1775 int rv;
1776 NYD_ENTER;
1778 rv = 1;
1780 if(a_amv_lopts == NULL){
1781 n_err(_("Cannot use `localopts' in this context "
1782 "(not in `define' or `account', nor special hook)\n"));
1783 goto jleave;
1786 a_amv_lopts->as_unroll = (boolify(*(argv = v), UIZ_MAX, FAL0) > 0);
1787 rv = 0;
1788 jleave:
1789 NYD_LEAVE;
1790 return rv;
1793 FL void
1794 temporary_call_compose_mode_hook(char const *macname,
1795 void (*hook_pre)(void *), void *hook_arg){
1796 /* TODO call_compose_mode_hook() temporary, v15: generalize; see a_LEX_SLICE
1797 * TODO comment in lex_input.c for the right way of doing things! */
1798 static struct a_amv_lostack *cmh_losp;
1799 struct a_amv_mac_call_args *amcap;
1800 struct a_amv_mac *amp;
1801 NYD_ENTER;
1803 amp = NULL;
1805 if(macname == (char*)-1){
1806 a_amv_mac__finalize(cmh_losp);
1807 cmh_losp = NULL;
1808 }else if(macname != NULL &&
1809 (amp = a_amv_mac_lookup(macname, NULL, a_AMV_MF_NONE)) == NULL)
1810 n_err(_("Cannot call *on-compose-**: macro does not exist: %s\n"),
1811 macname);
1812 else{
1813 amcap = n_lofi_alloc(sizeof *amcap);
1814 memset(amcap, 0, sizeof *amcap);
1815 amcap->amca_name = (macname != NULL) ? macname : "on-compose-done-shell";
1816 amcap->amca_amp = amp;
1817 amcap->amca_unroller = &a_amv_compose_lopts;
1818 amcap->amca_hook_pre = hook_pre;
1819 amcap->amca_hook_arg = hook_arg;
1820 amcap->amca_lopts_on = TRU1;
1821 amcap->amca_ps_hook_mask = TRU1;
1822 pstate &= ~PS_HOOK_MASK;
1823 pstate |= PS_HOOK;
1824 if(macname != NULL)
1825 a_amv_mac_exec(amcap);
1826 else{
1827 cmh_losp = n_lofi_alloc(sizeof *cmh_losp);
1828 cmh_losp->as_global_saved = a_amv_lopts;
1829 cmh_losp->as_up = NULL;
1830 cmh_losp->as_lopts = *(cmh_losp->as_amcap = amcap)->amca_unroller;
1831 cmh_losp->as_unroll = TRU1;
1832 a_amv_lopts = cmh_losp;
1835 NYD_LEAVE;
1838 FL void
1839 temporary_unroll_compose_mode(void){ /* XXX intermediate hack */
1840 NYD_ENTER;
1841 if(a_amv_compose_lopts != NULL){
1842 void *save = a_amv_lopts;
1844 a_amv_lopts = NULL;
1845 a_amv_lopts_unroll(&a_amv_compose_lopts);
1846 a_amv_compose_lopts = NULL;
1847 a_amv_lopts = save;
1849 NYD_LEAVE;
1852 FL void
1853 temporary_localopts_folder_hook_unroll(void){ /* XXX intermediate hack */
1854 NYD_ENTER;
1855 if(a_amv_folder_hook_lopts != NULL){
1856 void *save = a_amv_lopts;
1858 a_amv_lopts = NULL;
1859 a_amv_lopts_unroll(&a_amv_folder_hook_lopts);
1860 a_amv_folder_hook_lopts = NULL;
1861 a_amv_lopts = save;
1863 NYD_LEAVE;
1866 FL char *
1867 n_var_oklook(enum okeys okey){
1868 struct a_amv_var_carrier avc;
1869 char *rv;
1870 struct a_amv_var_map const *avmp;
1871 NYD_ENTER;
1873 avc.avc_map = avmp = &a_amv_var_map[okey];
1874 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1875 avc.avc_hash = avmp->avm_hash;
1876 avc.avc_okey = okey;
1878 if(a_amv_var_lookup(&avc, FAL0))
1879 rv = avc.avc_var->av_value;
1880 else
1881 rv = NULL;
1882 NYD_LEAVE;
1883 return rv;
1886 FL bool_t
1887 n_var_okset(enum okeys okey, uintptr_t val){
1888 struct a_amv_var_carrier avc;
1889 bool_t ok;
1890 struct a_amv_var_map const *avmp;
1891 NYD_ENTER;
1893 avc.avc_map = avmp = &a_amv_var_map[okey];
1894 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1895 avc.avc_hash = avmp->avm_hash;
1896 avc.avc_okey = okey;
1898 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1899 NYD_LEAVE;
1900 return ok;
1903 FL bool_t
1904 n_var_okclear(enum okeys okey){
1905 struct a_amv_var_carrier avc;
1906 bool_t rv;
1907 struct a_amv_var_map const *avmp;
1908 NYD_ENTER;
1910 avc.avc_map = avmp = &a_amv_var_map[okey];
1911 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1912 avc.avc_hash = avmp->avm_hash;
1913 avc.avc_okey = okey;
1915 rv = a_amv_var_clear(&avc, FAL0);
1916 NYD_LEAVE;
1917 return rv;
1920 FL char *
1921 n_var_voklook(char const *vokey){
1922 struct a_amv_var_carrier avc;
1923 char *rv;
1924 NYD_ENTER;
1926 a_amv_var_revlookup(&avc, vokey);
1928 rv = a_amv_var_lookup(&avc, FAL0) ? avc.avc_var->av_value : NULL;
1929 NYD_LEAVE;
1930 return rv;
1933 FL bool_t
1934 n_var_vokset(char const *vokey, uintptr_t val){
1935 struct a_amv_var_carrier avc;
1936 bool_t ok;
1937 NYD_ENTER;
1939 a_amv_var_revlookup(&avc, vokey);
1941 ok = a_amv_var_set(&avc, (val == 0x1 ? n_empty : (char const*)val), FAL0);
1942 NYD_LEAVE;
1943 return ok;
1946 FL bool_t
1947 n_var_vokclear(char const *vokey){
1948 struct a_amv_var_carrier avc;
1949 bool_t ok;
1950 NYD_ENTER;
1952 a_amv_var_revlookup(&avc, vokey);
1954 ok = a_amv_var_clear(&avc, FAL0);
1955 NYD_LEAVE;
1956 return ok;
1959 #ifdef HAVE_SOCKETS
1960 FL char *
1961 n_var_xoklook(enum okeys okey, struct url const *urlp,
1962 enum okey_xlook_mode oxm){
1963 struct a_amv_var_carrier avc;
1964 struct str const *us;
1965 size_t nlen;
1966 char *nbuf, *rv;
1967 struct a_amv_var_map const *avmp;
1968 NYD_ENTER;
1970 assert(oxm & (OXM_PLAIN | OXM_H_P | OXM_U_H_P));
1972 /* For simplicity: allow this case too */
1973 if(!(oxm & (OXM_H_P | OXM_U_H_P))){
1974 nbuf = NULL;
1975 goto jplain;
1978 avc.avc_map = avmp = &a_amv_var_map[okey];
1979 avc.avc_name = a_amv_var_names + avmp->avm_keyoff;
1980 avc.avc_okey = okey;
1982 us = (oxm & OXM_U_H_P) ? &urlp->url_u_h_p : &urlp->url_h_p;
1983 nlen = strlen(avc.avc_name);
1984 nbuf = n_lofi_alloc(nlen + 1 + us->l +1);
1985 memcpy(nbuf, avc.avc_name, nlen);
1986 nbuf[nlen++] = '-';
1988 /* One of .url_u_h_p and .url_h_p we test in here */
1989 memcpy(nbuf + nlen, us->s, us->l +1);
1990 avc.avc_name = a_amv_var_canonify(nbuf);
1991 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
1992 if(a_amv_var_lookup(&avc, FAL0))
1993 goto jvar;
1995 /* The second */
1996 if(oxm & OXM_H_P){
1997 us = &urlp->url_h_p;
1998 memcpy(nbuf + nlen, us->s, us->l +1);
1999 avc.avc_name = a_amv_var_canonify(nbuf);
2000 avc.avc_hash = a_AMV_NAME2HASH(avc.avc_name);
2001 if(a_amv_var_lookup(&avc, FAL0)){
2002 jvar:
2003 rv = avc.avc_var->av_value;
2004 goto jleave;
2008 jplain:
2009 rv = (oxm & OXM_PLAIN) ? n_var_oklook(okey) : NULL;
2010 jleave:
2011 if(nbuf != NULL)
2012 n_lofi_free(nbuf);
2013 NYD_LEAVE;
2014 return rv;
2016 #endif /* HAVE_SOCKETS */
2018 FL int
2019 c_set(void *v){
2020 char **ap;
2021 int err;
2022 NYD_ENTER;
2024 if(*(ap = v) == NULL){
2025 a_amv_var_show_all();
2026 err = 0;
2027 }else
2028 err = !a_amv_var_c_set(ap, FAL0);
2029 NYD_LEAVE;
2030 return err;
2033 FL int
2034 c_unset(void *v){
2035 char **ap;
2036 int err;
2037 NYD_ENTER;
2039 for(err = 0, ap = v; *ap != NULL; ++ap)
2040 err |= !n_var_vokclear(*ap);
2041 NYD_LEAVE;
2042 return err;
2045 FL int
2046 c_varshow(void *v){
2047 char **ap;
2048 NYD_ENTER;
2050 if(*(ap = v) == NULL)
2051 v = NULL;
2052 else{
2053 struct n_string msg, *msgp = &msg;
2055 msgp = n_string_creat(msgp);
2056 for(; *ap != NULL; ++ap)
2057 a_amv_var_show(*ap, stdout, msgp);
2058 n_string_gut(msgp);
2060 NYD_LEAVE;
2061 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
2064 FL int
2065 c_varedit(void *v){
2066 struct a_amv_var_carrier avc;
2067 FILE *of, *nf;
2068 char *val, **argv;
2069 int err;
2070 sighandler_type sigint;
2071 NYD_ENTER;
2073 sigint = safe_signal(SIGINT, SIG_IGN);
2075 for(err = 0, argv = v; *argv != NULL; ++argv){
2076 memset(&avc, 0, sizeof avc);
2078 a_amv_var_revlookup(&avc, *argv);
2080 if(avc.avc_map != NULL){
2081 if(avc.avc_map->avm_flags & a_AMV_VF_BOOL){
2082 n_err(_("`varedit': cannot edit boolean variable: %s\n"),
2083 avc.avc_name);
2084 continue;
2086 if(avc.avc_map->avm_flags & a_AMV_VF_RDONLY){
2087 n_err(_("`varedit': cannot edit readonly variable: %s\n"),
2088 avc.avc_name);
2089 continue;
2093 a_amv_var_lookup(&avc, FAL0);
2095 if((of = Ftmp(NULL, "varedit", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
2096 NULL){
2097 n_perr(_("`varedit': can't create temporary file, bailing out"), 0);
2098 err = 1;
2099 break;
2100 }else if(avc.avc_var != NULL && *(val = avc.avc_var->av_value) != '\0' &&
2101 sizeof *val != fwrite(val, strlen(val), sizeof *val, of)){
2102 n_perr(_("`varedit' failed to write old value to temporary file"), 0);
2103 Fclose(of);
2104 err = 1;
2105 continue;
2108 fflush_rewind(of);
2109 nf = run_editor(of, (off_t)-1, 'e', FAL0, NULL, NULL, SEND_MBOX, sigint);
2110 Fclose(of);
2112 if(nf != NULL){
2113 int c;
2114 char *base;
2115 off_t l;
2117 l = fsize(nf);
2118 assert(l >= 0);
2119 base = salloc((size_t)l +1);
2121 for(l = 0, val = base; (c = getc(nf)) != EOF; ++val)
2122 if(c == '\n' || c == '\r'){
2123 *val = ' ';
2124 ++l;
2125 }else{
2126 *val = (char)(uc_i)c;
2127 l = 0;
2129 val -= l;
2130 *val = '\0';
2132 if(!a_amv_var_set(&avc, base, FAL0))
2133 err = 1;
2135 Fclose(nf);
2136 }else{
2137 n_err(_("`varedit': can't start $EDITOR, bailing out\n"));
2138 err = 1;
2139 break;
2143 safe_signal(SIGINT, sigint);
2144 NYD_LEAVE;
2145 return err;
2148 FL int
2149 c_environ(void *v){
2150 struct a_amv_var_carrier avc;
2151 int err;
2152 char **ap;
2153 bool_t islnk;
2154 NYD_ENTER;
2156 if((islnk = !strcmp(*(ap = v), "link")) || !strcmp(*ap, "unlink")){
2157 for(err = 0; *++ap != NULL;){
2158 a_amv_var_revlookup(&avc, *ap);
2160 if(a_amv_var_lookup(&avc, FAL0) && (islnk ||
2161 (avc.avc_var->av_flags & a_AMV_VF_LINKED))){
2162 if(!islnk){
2163 avc.avc_var->av_flags &= ~a_AMV_VF_LINKED;
2164 continue;
2165 }else if(avc.avc_var->av_flags & (a_AMV_VF_ENV | a_AMV_VF_LINKED)){
2166 if(options & OPT_D_V)
2167 n_err(_("`environ': link: already established: %s\n"), *ap);
2168 continue;
2170 avc.avc_var->av_flags |= a_AMV_VF_LINKED;
2171 if(!(avc.avc_var->av_flags & a_AMV_VF_ENV))
2172 a_amv_var__putenv(&avc, avc.avc_var);
2173 }else if(!islnk){
2174 n_err(_("`environ': unlink: no link established: %s\n"), *ap);
2175 err = 1;
2176 }else{
2177 char const *evp = getenv(*ap);
2179 if(evp != NULL)
2180 err |= !a_amv_var_set(&avc, evp, TRU1);
2181 else{
2182 n_err(_("`environ': link: cannot link to non-existent: %s\n"),
2183 *ap);
2184 err = 1;
2188 }else if(!strcmp(*ap, "set"))
2189 err = !a_amv_var_c_set(++ap, TRU1);
2190 else if(!strcmp(*ap, "unset")){
2191 for(err = 0; *++ap != NULL;){
2192 a_amv_var_revlookup(&avc, *ap);
2194 if(!a_amv_var_clear(&avc, TRU1))
2195 err = 1;
2197 }else{
2198 n_err(_("Synopsis: environ: <link|set|unset> <variable>...\n"));
2199 err = 1;
2201 NYD_LEAVE;
2202 return err;
2205 /* s-it-mode */