NetBSD/bin/sh/var.h

152 lines
5.1 KiB
C
Raw Normal View History

Alter a design botch when magic (self modifying) variables were added to sh ... in other shells, setting such a variable (for most of them) causes it to lose its special properties, and act the same as any other variable. I had assumed that was just implementor laziness... I was wrong. From now on the NetBSD shell will act like the others, and if vars like HOSTNAME (and SECONDS, etc) are used as variables in a script or whatever, they will act just like normal variables (and unless this happens when they have been made local, or as a variable-assignment as a prefix to a command, the special properties they would have had otherwise are lost for the remainder of the life of the (sub-)shell in which the variables were set). Importing a value from the environment counts as setting the value for this purpose (so if HOSTNAME is set in the environment, the value there will be the value $HOSTNAME expands to). The two exceptions to this are LINENO and RANDOM. RANDOM needs to be able to be set to (re-)set its seed. LINENO needs to be able to be set (at least in the "local" command) to achieve the desired functionality. It is unlikely that any (sane) script is going to want to use those two as normal vars however. While here, fix a minor bug in popping local vars (fn return) that need to notify the shell of changes in value (like PATH). Change sh(1) to reflect this alteration. Also add doc of the (forgotten) magic var EUSER (which has been there since the others were added), and add a few more vars (which are documented in other places in sh(1) - like ENV) into the defined or used variable list (as well as wherever else they appear). XXX pullup -8
2018-12-04 17:03:30 +03:00
/* $NetBSD: var.h,v 1.38 2018/12/04 14:03:30 kre Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*-
1994-05-11 21:09:42 +04:00
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Kenneth Almquist.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)var.h 8.2 (Berkeley) 5/4/95
1993-03-21 12:45:37 +03:00
*/
Make arg parsing in kill POSIX compatible with POSIX (XBD 2.12) by parsing the way getopt(3) would, if only it could handle the (required) -signumber and -signame options. This adds two "features" to kill, -ssigname and -lstatus now work (ie: one word with all of the '-', the option letter, and its value) and "--" also now works (kill -- -pid1 pid2 will not attempt to send the pid1 signal to pid2, but rather SIGTERM to the pid1 process group and pid2). It is still the case that (apart from --) at most 1 option is permitted (-l, -s, -signame, or -signumber.) Note that we now have an ambiguity, -sname might mean "-s name" or send the signal "sname" - if one of those turns out to be valid, that will be accepted, otherwise the error message will indicate that "sname" is not a valid signal name, not that "name" is not. Keeping the "-s" and signal name as separate words avoids this issue. Also caution: should someone be weird enough to define a new signal name (as in the part after SIG) which is almost the same name as an existing name that starts with 'S' by adding an extra 'S' prepended (eg: adding a SIGSSYS) then the ambiguity problem becomes much worse. In that case "kill -ssys" will be resolved in favour of the "-s" flag being used (the more modern syntax) and would send a SIGSYS, rather that a SIGSSYS. So don't do that. While here, switch to using signalname(3) (bye bye NSIG, et. al.), add some constipation, and show a little pride in formatting the signal names for "kill -l" (and in the usage when appropriate -- same routine.) Respect COLUMNS (POSIX XBD 8.3) as primary specification of the width (terminal width, not number of columns to print) for kill -l, a very small value for COLUMNS will cause kill -l output to list signals one per line, a very large value will cause them all to be listed on one line.) (eg: "COLUMNS=1 kill -l") TODO: the signal printing for "trap -l" and that for "kill -l" should be switched to use a common routine (for the sh builtin versions.) All changes of relevance here are to bin/kill - the (minor) changes to bin/sh are only to properly expose the builtin version of getenv(3) so the builtin version of kill can use it (ie: make its prototype available.)
2017-06-27 01:09:16 +03:00
#ifndef VUNSET /* double include protection */
1993-03-21 12:45:37 +03:00
/*
* Shell variables.
*/
/* flags */
#define VUNSET 0x0001 /* the variable is not set */
#define VEXPORT 0x0002 /* variable is exported */
#define VREADONLY 0x0004 /* variable cannot be modified */
#define VNOEXPORT 0x0008 /* variable may not be exported */
#define VSTRFIXED 0x0010 /* variable struct is statically allocated */
#define VTEXTFIXED 0x0020 /* text is statically allocated */
#define VSTACK 0x0040 /* text is allocated on the stack */
#define VNOFUNC 0x0100 /* don't call the callback function */
A better LINENO implementation. This version deletes (well, #if 0's out) the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)). (Code to invert the LINENO hack when required, like when de-compiling the execution tree to provide the "jobs" command strings, is still included, that can be deleted when the LINENO hack is completely removed - look for refs to VSLINENO throughout the code. The var funclinno in parser.c can also be removed, it is used only for the LINENO hack.) This version produces accurate results: $((LINENO)) was made as accurate as the LINENO hack made ${LINENO} which is very good. That's why the LINENO hack is not yet completely removed, so it can be easily re-enabled. If you can tell the difference when it is in use, or not in use, then something has broken (or I managed to miss a case somewhere.) The way that LINENO works is documented in its own (new) section in the man page, so nothing more about that, or the new options, etc, here. This version introduces the possibility of having a "reference" function associated with a variable, which gets called whenever the value of the variable is required (that's what implements LINENO). There is just one function pointer however, so any particular variable gets at most one of the set function (as used for PATH, etc) or the reference function. The VFUNCREF bit in the var flags indicates which func the variable in question uses (if any - the func ptr, as before, can be NULL). I would not call the results of this perfect yet, but it is close.
2017-06-07 08:08:32 +03:00
#define VFUNCREF 0x0200 /* the function is called on ref, not set */
1993-03-21 12:45:37 +03:00
Alter a design botch when magic (self modifying) variables were added to sh ... in other shells, setting such a variable (for most of them) causes it to lose its special properties, and act the same as any other variable. I had assumed that was just implementor laziness... I was wrong. From now on the NetBSD shell will act like the others, and if vars like HOSTNAME (and SECONDS, etc) are used as variables in a script or whatever, they will act just like normal variables (and unless this happens when they have been made local, or as a variable-assignment as a prefix to a command, the special properties they would have had otherwise are lost for the remainder of the life of the (sub-)shell in which the variables were set). Importing a value from the environment counts as setting the value for this purpose (so if HOSTNAME is set in the environment, the value there will be the value $HOSTNAME expands to). The two exceptions to this are LINENO and RANDOM. RANDOM needs to be able to be set to (re-)set its seed. LINENO needs to be able to be set (at least in the "local" command) to achieve the desired functionality. It is unlikely that any (sane) script is going to want to use those two as normal vars however. While here, fix a minor bug in popping local vars (fn return) that need to notify the shell of changes in value (like PATH). Change sh(1) to reflect this alteration. Also add doc of the (forgotten) magic var EUSER (which has been there since the others were added), and add a few more vars (which are documented in other places in sh(1) - like ENV) into the defined or used variable list (as well as wherever else they appear). XXX pullup -8
2018-12-04 17:03:30 +03:00
#define VSPECIAL 0x1000 /* magic properties not lost when set */
#define VDOEXPORT 0x2000 /* obey VEXPORT even if VNOEXPORT */
#define VNOSET 0x4000 /* do not set variable - just readonly test */
#define VNOERROR 0x8000 /* be quiet if set fails (no error msg) */
A better LINENO implementation. This version deletes (well, #if 0's out) the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)). (Code to invert the LINENO hack when required, like when de-compiling the execution tree to provide the "jobs" command strings, is still included, that can be deleted when the LINENO hack is completely removed - look for refs to VSLINENO throughout the code. The var funclinno in parser.c can also be removed, it is used only for the LINENO hack.) This version produces accurate results: $((LINENO)) was made as accurate as the LINENO hack made ${LINENO} which is very good. That's why the LINENO hack is not yet completely removed, so it can be easily re-enabled. If you can tell the difference when it is in use, or not in use, then something has broken (or I managed to miss a case somewhere.) The way that LINENO works is documented in its own (new) section in the man page, so nothing more about that, or the new options, etc, here. This version introduces the possibility of having a "reference" function associated with a variable, which gets called whenever the value of the variable is required (that's what implements LINENO). There is just one function pointer however, so any particular variable gets at most one of the set function (as used for PATH, etc) or the reference function. The VFUNCREF bit in the var flags indicates which func the variable in question uses (if any - the func ptr, as before, can be NULL). I would not call the results of this perfect yet, but it is close.
2017-06-07 08:08:32 +03:00
struct var;
union var_func_union { /* function to be called when: */
void (*set_func)(const char *); /* variable gets set/unset */
char*(*ref_func)(struct var *); /* variable is referenced */
};
1993-03-21 12:45:37 +03:00
struct var {
struct var *next; /* next entry in hash list */
int flags; /* flags are defined above */
char *text; /* name=value */
int name_len; /* length of name */
A better LINENO implementation. This version deletes (well, #if 0's out) the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)). (Code to invert the LINENO hack when required, like when de-compiling the execution tree to provide the "jobs" command strings, is still included, that can be deleted when the LINENO hack is completely removed - look for refs to VSLINENO throughout the code. The var funclinno in parser.c can also be removed, it is used only for the LINENO hack.) This version produces accurate results: $((LINENO)) was made as accurate as the LINENO hack made ${LINENO} which is very good. That's why the LINENO hack is not yet completely removed, so it can be easily re-enabled. If you can tell the difference when it is in use, or not in use, then something has broken (or I managed to miss a case somewhere.) The way that LINENO works is documented in its own (new) section in the man page, so nothing more about that, or the new options, etc, here. This version introduces the possibility of having a "reference" function associated with a variable, which gets called whenever the value of the variable is required (that's what implements LINENO). There is just one function pointer however, so any particular variable gets at most one of the set function (as used for PATH, etc) or the reference function. The VFUNCREF bit in the var flags indicates which func the variable in question uses (if any - the func ptr, as before, can be NULL). I would not call the results of this perfect yet, but it is close.
2017-06-07 08:08:32 +03:00
union var_func_union v_u; /* function to apply (sometimes) */
1993-03-21 12:45:37 +03:00
};
struct localvar {
struct localvar *next; /* next local variable in list */
struct var *vp; /* the variable that was made local */
int flags; /* saved flags */
char *text; /* saved text */
Alter a design botch when magic (self modifying) variables were added to sh ... in other shells, setting such a variable (for most of them) causes it to lose its special properties, and act the same as any other variable. I had assumed that was just implementor laziness... I was wrong. From now on the NetBSD shell will act like the others, and if vars like HOSTNAME (and SECONDS, etc) are used as variables in a script or whatever, they will act just like normal variables (and unless this happens when they have been made local, or as a variable-assignment as a prefix to a command, the special properties they would have had otherwise are lost for the remainder of the life of the (sub-)shell in which the variables were set). Importing a value from the environment counts as setting the value for this purpose (so if HOSTNAME is set in the environment, the value there will be the value $HOSTNAME expands to). The two exceptions to this are LINENO and RANDOM. RANDOM needs to be able to be set to (re-)set its seed. LINENO needs to be able to be set (at least in the "local" command) to achieve the desired functionality. It is unlikely that any (sane) script is going to want to use those two as normal vars however. While here, fix a minor bug in popping local vars (fn return) that need to notify the shell of changes in value (like PATH). Change sh(1) to reflect this alteration. Also add doc of the (forgotten) magic var EUSER (which has been there since the others were added), and add a few more vars (which are documented in other places in sh(1) - like ENV) into the defined or used variable list (as well as wherever else they appear). XXX pullup -8
2018-12-04 17:03:30 +03:00
union var_func_union v_u; /* saved function */
1993-03-21 12:45:37 +03:00
};
extern struct localvar *localvars;
1993-03-21 12:45:37 +03:00
extern struct var vifs;
extern char ifs_default[];
1993-03-21 12:45:37 +03:00
extern struct var vmail;
extern struct var vmpath;
extern struct var vpath;
extern struct var vps1;
extern struct var vps2;
extern struct var vps4;
An initial attempt at implementing LINENO to meet the specs. Aside from one problem (not too hard to fix if it was ever needed) this version does about as well as most other shell implementations when expanding $((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the latter, and that is very accurate. Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce the same value when used on the same line (a defect that other shells do not share - aside from the FreeBSD sh as it is today, where only the LINENO hack exists and so (like for us before this commit) $((LINENO)) is always either 0, or at least whatever value was last set, perhaps by LINENO=${LINENO} which does actually work ... for that one line...) This could be corrected by simply removing the LINENO hack (look for the string LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the same (not perfectly accurate) values, as do most other shells. POSIX requires that LINENO be set before each command, and this implementation does that fairly literally - except that we only bother before the commands which actually expand words (for, case and simple commands). Unfortunately this forgot that expansions also occur in redirects, and the other compound commands can also have redirects, so if a redirect on one of the other compound commands wants to use the value of $((LINENO)) as a part of a generated file name, then it will get an incorrect value. This is the "one problem" above. (Because the LINENO hack is still enabled, using ${LINENO} works.) This could be fixed, but as this version of the LINENO implementation is just for reference purposes (it will be superseded within minutes by a better one) I won't bother. However should anyone else decide that this is a better choice (it is probably a smaller implementation, in terms of code & data space then the replacement, but also I would expect, slower, and definitely less accurate) this defect is something to bear in mind, and fix. This version retains the *BSD historical practice that line numbers in functions (all functions) count from 1 from the start of the function, and elsewhere, start from 1 from where the shell started reading the input file/stream in question. In an "eval" expression the line number starts at the line of the "eval" (and then increases if the input is a multi-line string). Note: this version is not documented (beyond as much as LINENO was before) hence this slightly longer than usual commit message.
2017-06-07 07:44:17 +03:00
extern struct var line_num;
1997-03-14 04:42:18 +03:00
#ifndef SMALL
extern struct var editrc;
extern struct var vterm;
extern struct var vtermcap;
extern struct var vhistsize;
extern struct var ps_lit;
extern struct var euname;
extern struct var random_num;
extern intmax_t sh_start_time;
#endif
1993-03-21 12:45:37 +03:00
A better LINENO implementation. This version deletes (well, #if 0's out) the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)). (Code to invert the LINENO hack when required, like when de-compiling the execution tree to provide the "jobs" command strings, is still included, that can be deleted when the LINENO hack is completely removed - look for refs to VSLINENO throughout the code. The var funclinno in parser.c can also be removed, it is used only for the LINENO hack.) This version produces accurate results: $((LINENO)) was made as accurate as the LINENO hack made ${LINENO} which is very good. That's why the LINENO hack is not yet completely removed, so it can be easily re-enabled. If you can tell the difference when it is in use, or not in use, then something has broken (or I managed to miss a case somewhere.) The way that LINENO works is documented in its own (new) section in the man page, so nothing more about that, or the new options, etc, here. This version introduces the possibility of having a "reference" function associated with a variable, which gets called whenever the value of the variable is required (that's what implements LINENO). There is just one function pointer however, so any particular variable gets at most one of the set function (as used for PATH, etc) or the reference function. The VFUNCREF bit in the var flags indicates which func the variable in question uses (if any - the func ptr, as before, can be NULL). I would not call the results of this perfect yet, but it is close.
2017-06-07 08:08:32 +03:00
extern int line_number;
extern int funclinebase;
extern int funclineabs;
1993-03-21 12:45:37 +03:00
/*
* The following macros access the values of the above variables.
* They have to skip over the name. They return the null string
* for unset variables.
*/
#define ifsset() ((vifs.flags & VUNSET) == 0)
#define ifsval() (ifsset() ? (vifs.text + 4) : ifs_default)
1993-03-21 12:45:37 +03:00
#define mailval() (vmail.text + 5)
#define mpathval() (vmpath.text + 9)
#define pathval() (vpath.text + 5)
#define ps1val() (vps1.text + 4)
#define ps2val() (vps2.text + 4)
#define ps4val() (vps4.text + 4)
#define optindval() (voptind.text + 7)
1997-03-14 04:42:18 +03:00
#ifndef SMALL
#define histsizeval() (vhistsize.text + 9)
#define termval() (vterm.text + 5)
#endif
1993-03-21 12:45:37 +03:00
#define mpathset() ((vmpath.flags & VUNSET) == 0)
void initvar(void);
void setvar(const char *, const char *, int);
void setvareq(char *, int);
1993-03-21 12:45:37 +03:00
struct strlist;
void listsetvar(struct strlist *, int);
char *lookupvar(const char *);
char *bltinlookup(const char *, int);
char **environment(void);
void shprocvar(void);
int showvars(const char *, int, int, const char *);
void mklocal(const char *, int);
void listmklocal(struct strlist *, int);
void poplocalvars(void);
int unsetvar(const char *, int);
void choose_ps1(void);
int setvarsafe(const char *, const char *, int);
void print_quoted(const char *);
int validname(const char *, int, int *);
Make arg parsing in kill POSIX compatible with POSIX (XBD 2.12) by parsing the way getopt(3) would, if only it could handle the (required) -signumber and -signame options. This adds two "features" to kill, -ssigname and -lstatus now work (ie: one word with all of the '-', the option letter, and its value) and "--" also now works (kill -- -pid1 pid2 will not attempt to send the pid1 signal to pid2, but rather SIGTERM to the pid1 process group and pid2). It is still the case that (apart from --) at most 1 option is permitted (-l, -s, -signame, or -signumber.) Note that we now have an ambiguity, -sname might mean "-s name" or send the signal "sname" - if one of those turns out to be valid, that will be accepted, otherwise the error message will indicate that "sname" is not a valid signal name, not that "name" is not. Keeping the "-s" and signal name as separate words avoids this issue. Also caution: should someone be weird enough to define a new signal name (as in the part after SIG) which is almost the same name as an existing name that starts with 'S' by adding an extra 'S' prepended (eg: adding a SIGSSYS) then the ambiguity problem becomes much worse. In that case "kill -ssys" will be resolved in favour of the "-s" flag being used (the more modern syntax) and would send a SIGSYS, rather that a SIGSSYS. So don't do that. While here, switch to using signalname(3) (bye bye NSIG, et. al.), add some constipation, and show a little pride in formatting the signal names for "kill -l" (and in the usage when appropriate -- same routine.) Respect COLUMNS (POSIX XBD 8.3) as primary specification of the width (terminal width, not number of columns to print) for kill -l, a very small value for COLUMNS will cause kill -l output to list signals one per line, a very large value will cause them all to be listed on one line.) (eg: "COLUMNS=1 kill -l") TODO: the signal printing for "trap -l" and that for "kill -l" should be switched to use a common routine (for the sh builtin versions.) All changes of relevance here are to bin/kill - the (minor) changes to bin/sh are only to properly expose the builtin version of getenv(3) so the builtin version of kill can use it (ie: make its prototype available.)
2017-06-27 01:09:16 +03:00
#endif