1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1997-2005 5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)var.h 8.2 (Berkeley) 5/4/95 35 */ 36 37#include <inttypes.h> 38 39/* 40 * Shell variables. 41 */ 42 43/* flags */ 44#define VEXPORT 0x01 /* variable is exported */ 45#define VREADONLY 0x02 /* variable cannot be modified */ 46#define VSTRFIXED 0x04 /* variable struct is statically allocated */ 47#define VTEXTFIXED 0x08 /* text is statically allocated */ 48#define VSTACK 0x10 /* text is allocated on the stack */ 49#define VUNSET 0x20 /* the variable is not set */ 50#define VNOFUNC 0x40 /* don't call the callback function */ 51#define VNOSET 0x80 /* do not set variable - just readonly test */ 52#define VNOSAVE 0x100 /* when text is on the heap before setvareq */ 53 54 55struct var { 56 struct var *next; /* next entry in hash list */ 57 int flags; /* flags are defined above */ 58 const char *text; /* name=value */ 59 void (*func)(const char *); 60 /* function to be called when */ 61 /* the variable gets set/unset */ 62}; 63 64 65struct localvar { 66 struct localvar *next; /* next local variable in list */ 67 struct var *vp; /* the variable that was made local */ 68 int flags; /* saved flags */ 69 const char *text; /* saved text */ 70}; 71 72struct localvar_list; 73 74 75extern struct localvar *localvars; 76extern struct var varinit[]; 77 78#if ATTY 79#define vatty varinit[0] 80#define vifs varinit[1] 81#else 82#define vifs varinit[0] 83#endif 84#define vpath (&vifs)[1] 85#define vps1 (&vpath)[1] 86#define vps2 (&vps1)[1] 87#define vps4 (&vps2)[1] 88#define voptind (&vps4)[1] 89#ifdef WITH_LINENO 90#define vlineno (&voptind)[1] 91#endif 92 93#ifdef IFS_BROKEN 94extern const char defifsvar[]; 95#define defifs (defifsvar + 4) 96#else 97extern const char defifs[]; 98#endif 99extern const char defpathvar[]; 100#define defpath (&defpathvar[sizeof("PATH=") - 1]) 101 102extern int lineno; 103extern char linenovar[]; 104 105/* 106 * The following macros access the values of the above variables. 107 * They have to skip over the name. They return the null string 108 * for unset variables. 109 */ 110 111#define ifsval() (vifs.text + 4) 112#define ifsset() ((vifs.flags & VUNSET) == 0) 113#define mailval() (vmail.text + 5) 114#define mpathval() (vmpath.text + 9) 115#define pathval() (vpath.text + 5) 116#define ps1val() (vps1.text + 4) 117#define ps2val() (vps2.text + 4) 118#define ps4val() (vps4.text + 4) 119#define optindval() (voptind.text + 7) 120#define linenoval() (vlineno.text + 7) 121 122#if ATTY 123#define attyset() ((vatty.flags & VUNSET) == 0) 124#endif 125#define mpathset() ((vmpath.flags & VUNSET) == 0) 126 127void initvar(void); 128struct var *setvar(const char *name, const char *val, int flags); 129intmax_t setvarint(const char *, intmax_t, int); 130struct var *setvareq(char *s, int flags); 131struct strlist; 132void listsetvar(struct strlist *, int); 133char *lookupvar(const char *); 134intmax_t lookupvarint(const char *); 135char **listvars(int, int, char ***); 136#define environment() listvars(VEXPORT, VUNSET, 0) 137int showvars(const char *, int, int); 138int exportcmd(int, char **); 139int localcmd(int, char **); 140void mklocal(char *); 141struct localvar_list *pushlocalvars(void); 142void poplocalvars(int); 143void unwindlocalvars(struct localvar_list *stop); 144int unsetcmd(int, char **); 145void unsetvar(const char *); 146int varcmp(const char *, const char *); 147 148static inline int varequal(const char *a, const char *b) { 149 return !varcmp(a, b); 150} 151 152/* 153 * Search the environment of a builtin command. 154 */ 155 156static inline char *bltinlookup(const char *name) 157{ 158 return lookupvar(name); 159} 160 161 162