diff options
author | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
---|---|---|
committer | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
commit | 5fd83771641d15c418f747bd343ba6738d3875f7 (patch) | |
tree | 5abf0f78f680d9837dbd93d4d4c3933bb7509599 /shell_cmds/env | |
download | apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.gz apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.zst apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.zip |
Import macOS userland
adv_cmds-176
basic_cmds-55
bootstrap_cmds-116.100.1
developer_cmds-66
diskdev_cmds-667.40.1
doc_cmds-53.60.1
file_cmds-321.40.3
mail_cmds-35
misc_cmds-34
network_cmds-606.40.1
patch_cmds-17
remote_cmds-63
shell_cmds-216.60.1
system_cmds-880.60.2
text_cmds-106
Diffstat (limited to 'shell_cmds/env')
-rw-r--r-- | shell_cmds/env/env.1 | 482 | ||||
-rw-r--r-- | shell_cmds/env/env.c | 144 | ||||
-rw-r--r-- | shell_cmds/env/envopts.c | 468 | ||||
-rw-r--r-- | shell_cmds/env/envopts.h | 37 |
4 files changed, 1131 insertions, 0 deletions
diff --git a/shell_cmds/env/env.1 b/shell_cmds/env/env.1 new file mode 100644 index 0000000..101f4eb --- /dev/null +++ b/shell_cmds/env/env.1 @@ -0,0 +1,482 @@ +.\" Copyright (c) 1980, 1990, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" 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. +.\" 4. Neither the name of the University nor the names of its contributors +.\" 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. +.\" +.\" From @(#)printenv.1 8.1 (Berkeley) 6/6/93 +.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp +.\" $FreeBSD$ +.\" +.Dd April 17, 2008 +.Dt ENV 1 +.Os +.Sh NAME +.Nm env +.Nd set environment and execute command, or print environment +.Sh SYNOPSIS +.Nm +.Op Fl iv +.Op Fl P Ar altpath +.Op Fl S Ar string +.Op Fl u Ar name +.Op Ar name Ns = Ns Ar value ... +.Op Ar utility Op Ar argument ... +.Sh DESCRIPTION +The +.Nm +utility executes another +.Ar utility +after modifying the environment as +specified on the command line. +Each +.Ar name Ns = Ns Ar value +option specifies the setting of an environment variable, +.Ar name , +with a value of +.Ar value . +All such environment variables are set before the +.Ar utility +is executed. +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl i +Execute the +.Ar utility +with only those environment variables specified by +.Ar name Ns = Ns Ar value +options. +The environment inherited +by +.Nm +is ignored completely. +.\" -P +.It Fl P Ar altpath +Search the set of directories as specified by +.Ar altpath +to locate the specified +.Ar utility +program, instead of using the value of the +.Ev PATH +environment variable. +.\" -S +.It Fl S Ar string +Split apart the given +.Ar string +into multiple strings, and process each of the resulting strings +as separate arguments to the +.Nm +utility. +The +.Fl S +option recognizes some special character escape sequences and +also supports environment-variable substitution, as described +below. +.\" -u +.It Fl u Ar name +If the environment variable +.Ar name +is in the environment, then remove it before processing the +remaining options. +This is similar to the +.Ic unset +command in +.Xr sh 1 . +The value for +.Ar name +must not include the +.Ql = +character. +.\" -v +.It Fl v +Print verbose information for each step of processing done by the +.Nm +utility. +Additional information will be printed if +.Fl v +is specified multiple times. +.El +.Pp +The above options are only recognized when they are specified +before any +.Ar name Ns = Ns Ar value +options. +.Pp +If no +.Ar utility +is specified, +.Nm +prints out the names and values +of the variables in the environment, with one name/value pair per line. +.\" +.Ss Details of Fl S Ss (split-string) processing +The processing of the +.Fl S +option will split the given +.Ar string +into separate arguments based on any space or <tab> characters found in the +.Ar string . +Each of those new arguments will then be treated as if it had been +specified as a separate argument on the original +.Nm +command. +.Pp +Spaces and tabs may be embedded in one of those new arguments by using +single +.Pq Dq Li ' +or double +.Pq Ql \&" +quotes, or backslashes +.Pq Ql \e . +Single quotes will escape all non-single quote characters, up to +the matching single quote. +Double quotes will escape all non-double quote characters, up to +the matching double quote. +It is an error if the end of the +.Ar string +is reached before the matching quote character. +.Pp +If +.Fl S +would create a new argument that starts with the +.Ql # +character, then that argument and the remainder of the +.Ar string +will be ignored. +The +.Ql \e# +sequence can be used when you want a new argument to start +with a +.Ql # +character, without causing the remainder of the +.Ar string +to be skipped. +.Pp +While processing the +.Ar string +value, +.Fl S +processing will treat certain character combinations as escape +sequences which represent some action to take. +The character escape sequences are in backslash notation. +The characters and their meanings are as follows: +.Pp +.Bl -tag -width indent -offset indent -compact +.It Cm \ec +Ignore the remaining characters in the +.Ar string . +This must not appear inside a double-quoted string. +.It Cm \ef +Replace with a <form-feed> character. +.It Cm \en +Replace with a <new-line> character. +.It Cm \er +Replace with a <carriage return> character. +.It Cm \et +Replace with a <tab> character. +.It Cm \ev +Replace with a <vertical tab> character. +.It Cm \e# +Replace with a +.Ql # +character. +This would be useful when you need a +.Ql # +as the first character in one of the arguments created +by splitting apart the given +.Ar string . +.It Cm \e$ +Replace with a +.Ql $ +character. +.It Cm \e_ +If this is found inside of a double-quoted string, then replace it +with a single blank. +If this is found outside of a quoted string, then treat this as the +separator character between new arguments in the original +.Ar string . +.It Cm \e" +Replace with a <double quote> character. +.It Cm \e\' +Replace with a <single quote> character. +.It Cm \e\e +Replace with a backslash character. +.El +.Pp +The sequences for <single-quote> and backslash are the only sequences +which are recognized inside of a single-quoted string. +The other sequences have no special meaning inside a single-quoted +string. +All escape sequences are recognized inside of a double-quoted string. +It is an error if a single +.Ql \e +character is followed by a character other than the ones listed above. +.Pp +The processing of +.Fl S +also supports substitution of values from environment variables. +To do this, the name of the environment variable must be inside of +.Ql ${} , +such as: +.Li ${SOMEVAR} . +The common shell syntax of +.Li $SOMEVAR +is not supported. +All values substituted will be the values of the environment variables +as they were when the +.Nm +utility was originally invoked. +Those values will not be checked for any of the escape sequences as +described above. +And any settings of +.Ar name Ns = Ns Ar value +will not effect the values used for substitution in +.Fl S +processing. +.Pp +Also, +.Fl S +processing can not reference the value of the special parameters +which are defined by most shells. +For instance, +.Fl S +can not recognize special parameters such as: +.Ql $* , +.Ql $@ , +.Ql $# , +.Ql $? +or +.Ql $$ +if they appear inside the given +.Ar string . +.\" +.Ss Use in shell-scripts +The +.Nm +utility is often used as the +.Ar interpreter +on the first line of interpreted scripts, as +described in +.Xr execve 2 . +.Pp +Note that the way the kernel parses the +.Ql #! +(first line) of an interpreted script has changed as of +.Fx 6.0 . +Prior to that, the +.Fx +kernel would split that first line into separate arguments based +on any whitespace (space or <tab> characters) found in the line. +So, if a script named +.Pa /usr/local/bin/someport +had a first line of: +.Pp +.Dl "#!/usr/local/bin/php -n -q -dsafe_mode=0" +.Pp +then the +.Pa /usr/local/bin/php +program would have been started with the arguments of: +.Bd -literal -offset indent +arg[0] = '/usr/local/bin/php' +arg[1] = '-n' +arg[2] = '-q' +arg[3] = '-dsafe_mode=0' +arg[4] = '/usr/local/bin/someport' +.Ed +.Pp +plus any arguments the user specified when executing +.Pa someport . +However, this processing of multiple options on the +.Ql #! +line is not the way any other operating system parses the +first line of an interpreted script. +So after a change which was made for +.Fx 6.0 +release, that script will result in +.Pa /usr/local/bin/php +being started with the arguments of: +.Bd -literal -offset indent +arg[0] = '/usr/local/bin/php' +arg[1] = '-n -q -dsafe_mode=0' +arg[2] = '/usr/local/bin/someport' +.Ed +.Pp +plus any arguments the user specified. +This caused a significant change in the behavior of a few scripts. +In the case of above script, to have it behave the same way under +.Fx 6.0 +as it did under earlier releases, the first line should be +changed to: +.Pp +.Dl "#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0" +.Pp +The +.Nm +utility will be started with the entire line as a single +argument: +.Pp +.Dl "arg[1] = '-S /usr/local/bin/php -n -q -dsafe_mode=0'" +.Pp +and then +.Fl S +processing will split that line into separate arguments before +executing +.Pa /usr/local/bin/php . +.\" +.Sh ENVIRONMENT +The +.Nm +utility uses the +.Ev PATH +environment variable to locate the requested +.Ar utility +if the name contains no +.Ql / +characters, unless the +.Fl P +option has been specified. +.Sh EXIT STATUS +.Ex -std +An exit status of 126 indicates that +.Ar utility +was found, but could not be executed. +An exit status of 127 indicates that +.Ar utility +could not be found. +.Sh EXAMPLES +Since the +.Nm +utility is often used as part of the first line of an interpreted script, +the following examples show a number of ways that the +.Nm +utility can be useful in scripts. +.Pp +The kernel processing of an interpreted script does not allow a script +to directly reference some other script as its own interpreter. +As a way around this, the main difference between +.Pp +.Dl #!/usr/local/bin/foo +and +.Dl "#!/usr/bin/env /usr/local/bin/foo" +.Pp +is that the latter works even if +.Pa /usr/local/bin/foo +is itself an interpreted script. +.Pp +Probably the most common use of +.Nm +is to find the correct interpreter for a script, when the interpreter +may be in different directories on different systems. +The following example will find the +.Ql perl +interpreter by searching through the directories specified by +.Ev PATH . +.Pp +.Dl "#!/usr/bin/env perl" +.Pp +One limitation of that example is that it assumes the user's value +for +.Ev PATH +is set to a value which will find the interpreter you want +to execute. +The +.Fl P +option can be used to make sure a specific list of directories is +used in the search for +.Ar utility . +Note that the +.Fl S +option is also required for this example to work correctly. +.Pp +.Dl "#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl" +.Pp +The above finds +.Ql perl +only if it is in +.Pa /usr/local/bin +or +.Pa /usr/bin . +That could be combined with the present value of +.Ev PATH , +to provide more flexibility. +Note that spaces are not required between the +.Fl S +and +.Fl P +options: +.Pp +.Dl "#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl" +.Sh COMPATIBILITY +The +.Nm +utility accepts the +.Fl +option as a synonym for +.Fl i . +.Sh SEE ALSO +.Xr printenv 1 , +.Xr sh 1 , +.Xr execvp 3 , +.Xr environ 7 +.Sh STANDARDS +The +.Nm +utility conforms to +.St -p1003.1-2001 . +The +.Fl P , S , u +and +.Fl v +options are non-standard extensions supported by +.Fx , +but which may not be available on other operating systems. +.Sh HISTORY +The +.Nm +command appeared in +.Bx 4.4 . +The +.Fl P , S +and +.Fl v +options were added in +.Fx 6.0 . +.Sh BUGS +The +.Nm +utility does not handle values of +.Ar utility +which have an equals sign +.Pq Ql = +in their name, for obvious reasons. +.Pp +The +.Nm +utility does not take multibyte characters into account when +processing the +.Fl S +option, which may lead to incorrect results in some locales. diff --git a/shell_cmds/env/env.c b/shell_cmds/env/env.c new file mode 100644 index 0000000..3dc152a --- /dev/null +++ b/shell_cmds/env/env.c @@ -0,0 +1,144 @@ +/* + * Copyright (c) 1988, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 4. Neither the name of the University nor the names of its contributors + * 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. + */ + +#ifndef lint +static const char copyright[] = +"@(#) Copyright (c) 1988, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#if 0 +#ifndef lint +static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ +#endif + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +#include "envopts.h" + +extern char **environ; + +int env_verbosity; + +static void usage(void); + +int +main(int argc, char **argv) +{ + char *altpath, **ep, *p, **parg; + char *cleanenv[1]; + int ch, want_clear; + int rtrn; + + altpath = NULL; + want_clear = 0; + while ((ch = getopt(argc, argv, "-iP:S:u:v")) != -1) + switch(ch) { + case '-': + case 'i': + want_clear = 1; + break; + case 'P': + altpath = strdup(optarg); + break; + case 'S': + /* + * The -S option, for "split string on spaces, with + * support for some simple substitutions"... + */ + split_spaces(optarg, &optind, &argc, &argv); + break; + case 'u': + if (env_verbosity) + fprintf(stderr, "#env unset:\t%s\n", optarg); + rtrn = unsetenv(optarg); + if (rtrn == -1) + err(EXIT_FAILURE, "unsetenv %s", optarg); + break; + case 'v': + env_verbosity++; + if (env_verbosity > 1) + fprintf(stderr, "#env verbosity now at %d\n", + env_verbosity); + break; + case '?': + default: + usage(); + } + if (want_clear) { + environ = cleanenv; + cleanenv[0] = NULL; + if (env_verbosity) + fprintf(stderr, "#env clearing environ\n"); + } + for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { + if (env_verbosity) + fprintf(stderr, "#env setenv:\t%s\n", *argv); + *p = '\0'; + rtrn = setenv(*argv, p + 1, 1); + *p = '='; + if (rtrn == -1) + err(EXIT_FAILURE, "setenv %s", *argv); + } + if (*argv) { + if (altpath) + search_paths(altpath, argv); + if (env_verbosity) { + fprintf(stderr, "#env executing:\t%s\n", *argv); + for (parg = argv, argc = 0; *parg; parg++, argc++) + fprintf(stderr, "#env arg[%d]=\t'%s'\n", + argc, *parg); + if (env_verbosity > 1) + sleep(1); + } + execvp(*argv, argv); + err(errno == ENOENT ? 127 : 126, "%s", *argv); + } + for (ep = environ; *ep; ep++) + (void)printf("%s\n", *ep); + exit(0); +} + +static void +usage(void) +{ + (void)fprintf(stderr, + "usage: env [-iv] [-P utilpath] [-S string] [-u name]\n" + " [name=value ...] [utility [argument ...]]\n"); + exit(1); +} diff --git a/shell_cmds/env/envopts.c b/shell_cmds/env/envopts.c new file mode 100644 index 0000000..f821430 --- /dev/null +++ b/shell_cmds/env/envopts.c @@ -0,0 +1,468 @@ +/*- + * Copyright (c) 2005 - Garance Alistair Drosehn <gad@FreeBSD.org>. + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + * + * The views and conclusions contained in the software and documentation + * are those of the authors and should not be interpreted as representing + * official policies, either expressed or implied, of the FreeBSD Project. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/stat.h> +#include <sys/param.h> +#include <err.h> +#include <errno.h> +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "envopts.h" + +static const char * + expand_vars(int in_thisarg, char **thisarg_p, char **dest_p, + const char **src_p); +static int is_there(char *candidate); + +/* + * The is*() routines take a parameter of 'int', but expect values in the range + * of unsigned char. Define some wrappers which take a value of type 'char', + * whether signed or unsigned, and ensure the value ends up in the right range. + */ +#define isalnumch(Anychar) isalnum((u_char)(Anychar)) +#define isalphach(Anychar) isalpha((u_char)(Anychar)) +#define isspacech(Anychar) isspace((u_char)(Anychar)) + +/* + * Routine to determine if a given fully-qualified filename is executable. + * This is copied almost verbatim from FreeBSD's usr.bin/which/which.c. + */ +static int +is_there(char *candidate) +{ + struct stat fin; + + /* XXX work around access(2) false positives for superuser */ + if (access(candidate, X_OK) == 0 && + stat(candidate, &fin) == 0 && + S_ISREG(fin.st_mode) && + (getuid() != 0 || + (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) { + if (env_verbosity > 1) + fprintf(stderr, "#env matched:\t'%s'\n", candidate); + return (1); + } + return (0); +} + +/** + * Routine to search through an alternate path-list, looking for a given + * filename to execute. If the file is found, replace the original + * unqualified name with a fully-qualified path. This allows `env' to + * execute programs from a specific strict list of possible paths, without + * changing the value of PATH seen by the program which will be executed. + * E.G.: + * #!/usr/bin/env -S-P/usr/local/bin:/usr/bin perl + * will execute /usr/local/bin/perl or /usr/bin/perl (whichever is found + * first), no matter what the current value of PATH is, and without + * changing the value of PATH that the script will see when it runs. + * + * This is similar to the print_matches() routine in usr.bin/which/which.c. + */ +void +search_paths(char *path, char **argv) +{ + char candidate[PATH_MAX]; + const char *d; + char *filename, *fqname; + + /* If the file has a `/' in it, then no search is done */ + filename = *argv; + if (strchr(filename, '/') != NULL) + return; + + if (env_verbosity > 1) { + fprintf(stderr, "#env Searching:\t'%s'\n", path); + fprintf(stderr, "#env for file:\t'%s'\n", filename); + } + + fqname = NULL; + while ((d = strsep(&path, ":")) != NULL) { + if (*d == '\0') + d = "."; + if (snprintf(candidate, sizeof(candidate), "%s/%s", d, + filename) >= (int)sizeof(candidate)) + continue; + if (is_there(candidate)) { + fqname = candidate; + break; + } + } + + if (fqname == NULL) { + errno = ENOENT; + err(127, "%s", filename); + } + *argv = strdup(candidate); +} + +/** + * Routine to split a string into multiple parameters, while recognizing a + * few special characters. It recognizes both single and double-quoted + * strings. This processing is designed entirely for the benefit of the + * parsing of "#!"-lines (aka "shebang" lines == the first line of an + * executable script). Different operating systems parse that line in very + * different ways, and this split-on-spaces processing is meant to provide + * ways to specify arbitrary arguments on that line, no matter how the OS + * parses it. + * + * Within a single-quoted string, the two characters "\'" are treated as + * a literal "'" character to add to the string, and "\\" are treated as + * a literal "\" character to add. Other than that, all characters are + * copied until the processing gets to a terminating "'". + * + * Within a double-quoted string, many more "\"-style escape sequences + * are recognized, mostly copied from what is recognized in the `printf' + * command. Some OS's will not allow a literal blank character to be + * included in the one argument that they recognize on a shebang-line, + * so a few additional escape-sequences are defined to provide ways to + * specify blanks. + * + * Within a double-quoted string "\_" is turned into a literal blank. + * (Inside of a single-quoted string, the two characters are just copied) + * Outside of a quoted string, "\_" is treated as both a blank, and the + * end of the current argument. So with a shelbang-line of: + * #!/usr/bin/env -SA=avalue\_perl + * the -S value would be broken up into arguments "A=avalue" and "perl". + */ +void +split_spaces(const char *str, int *origind, int *origc, char ***origv) +{ + static const char *nullarg = ""; + const char *bq_src, *copystr, *src; + char *dest, **newargv, *newstr, **nextarg, **oldarg; + int addcount, bq_destlen, copychar, found_sep, in_arg, in_dq, in_sq; + + /* + * Ignore leading space on the string, and then malloc enough room + * to build a copy of it. The copy might end up shorter than the + * original, due to quoted strings and '\'-processing. + */ + while (isspacech(*str)) + str++; + if (*str == '\0') + return; + newstr = malloc(strlen(str) + 1); + + /* + * Allocate plenty of space for the new array of arg-pointers, + * and start that array off with the first element of the old + * array. + */ + newargv = malloc((*origc + (strlen(str) / 2) + 2) * sizeof(char *)); + nextarg = newargv; + *nextarg++ = **origv; + + /* Come up with the new args by splitting up the given string. */ + addcount = 0; + bq_destlen = in_arg = in_dq = in_sq = 0; + bq_src = NULL; + for (src = str, dest = newstr; *src != '\0'; src++) { + /* + * This switch will look at a character in *src, and decide + * what should be copied to *dest. It only decides what + * character(s) to copy, it should not modify *dest. In some + * cases, it will look at multiple characters from *src. + */ + copychar = found_sep = 0; + copystr = NULL; + switch (*src) { + case '"': + if (in_sq) + copychar = *src; + else if (in_dq) + in_dq = 0; + else { + /* + * Referencing nullarg ensures that a new + * argument is created, even if this quoted + * string ends up with zero characters. + */ + copystr = nullarg; + in_dq = 1; + bq_destlen = dest - *(nextarg - 1); + bq_src = src; + } + break; + case '$': + if (in_sq) + copychar = *src; + else { + copystr = expand_vars(in_arg, (nextarg - 1), + &dest, &src); + } + break; + case '\'': + if (in_dq) + copychar = *src; + else if (in_sq) + in_sq = 0; + else { + /* + * Referencing nullarg ensures that a new + * argument is created, even if this quoted + * string ends up with zero characters. + */ + copystr = nullarg; + in_sq = 1; + bq_destlen = dest - *(nextarg - 1); + bq_src = src; + } + break; + case '\\': + if (in_sq) { + /* + * Inside single-quoted strings, only the + * "\'" and "\\" are recognized as special + * strings. + */ + copychar = *(src + 1); + if (copychar == '\'' || copychar == '\\') + src++; + else + copychar = *src; + break; + } + src++; + switch (*src) { + case '"': + case '#': + case '$': + case '\'': + case '\\': + copychar = *src; + break; + case '_': + /* + * Alternate way to get a blank, which allows + * that blank be used to separate arguments + * when it is not inside a quoted string. + */ + if (in_dq) + copychar = ' '; + else { + found_sep = 1; + src++; + } + break; + case 'c': + /* + * Ignore remaining characters in the -S string. + * This would not make sense if found in the + * middle of a quoted string. + */ + if (in_dq) + errx(1, "Sequence '\\%c' is not allowed" + " in quoted strings", *src); + goto str_done; + case 'f': + copychar = '\f'; + break; + case 'n': + copychar = '\n'; + break; + case 'r': + copychar = '\r'; + break; + case 't': + copychar = '\t'; + break; + case 'v': + copychar = '\v'; + break; + default: + if (isspacech(*src)) + copychar = *src; + else + errx(1, "Invalid sequence '\\%c' in -S", + *src); + } + break; + default: + if ((in_dq || in_sq) && in_arg) + copychar = *src; + else if (isspacech(*src)) + found_sep = 1; + else { + /* + * If the first character of a new argument + * is `#', then ignore the remaining chars. + */ + if (!in_arg && *src == '#') + goto str_done; + copychar = *src; + } + } + /* + * Now that the switch has determined what (if anything) + * needs to be copied, copy whatever that is to *dest. + */ + if (copychar || copystr != NULL) { + if (!in_arg) { + /* This is the first byte of a new argument */ + *nextarg++ = dest; + addcount++; + in_arg = 1; + } + if (copychar) + *dest++ = (char)copychar; + else if (copystr != NULL) + while (*copystr != '\0') + *dest++ = *copystr++; + } else if (found_sep) { + *dest++ = '\0'; + while (isspacech(*src)) + src++; + --src; + in_arg = 0; + } + } +str_done: + *dest = '\0'; + *nextarg = NULL; + if (in_dq || in_sq) { + errx(1, "No terminating quote for string: %.*s%s", + bq_destlen, *(nextarg - 1), bq_src); + } + if (env_verbosity > 1) { + fprintf(stderr, "#env split -S:\t'%s'\n", str); + oldarg = newargv + 1; + fprintf(stderr, "#env into:\t'%s'\n", *oldarg); + for (oldarg++; *oldarg; oldarg++) + fprintf(stderr, "#env &\t'%s'\n", *oldarg); + } + + /* Copy the unprocessed arg-pointers from the original array */ + for (oldarg = *origv + *origind; *oldarg; oldarg++) + *nextarg++ = *oldarg; + *nextarg = NULL; + + /* Update optind/argc/argv in the calling routine */ + *origind = 1; + *origc += addcount; + *origv = newargv; +} + +/** + * Routine to split expand any environment variables referenced in the string + * that -S is processing. For now it only supports the form ${VARNAME}. It + * explicitly does not support $VARNAME, and obviously can not handle special + * shell-variables such as $?, $*, $1, etc. It is called with *src_p pointing + * at the initial '$', and if successful it will update *src_p, *dest_p, and + * possibly *thisarg_p in the calling routine. + */ +static const char * +expand_vars(int in_thisarg, char **thisarg_p, char **dest_p, const char **src_p) +{ + const char *vbegin, *vend, *vvalue; + char *newstr, *vname; + int bad_reference; + size_t namelen, newlen; + + bad_reference = 1; + vbegin = vend = (*src_p) + 1; + if (*vbegin++ == '{') + if (*vbegin == '_' || isalphach(*vbegin)) { + vend = vbegin + 1; + while (*vend == '_' || isalnumch(*vend)) + vend++; + if (*vend == '}') + bad_reference = 0; + } + if (bad_reference) + errx(1, "Only ${VARNAME} expansion is supported, error at: %s", + *src_p); + + /* + * We now know we have a valid environment variable name, so update + * the caller's source-pointer to the last character in that reference, + * and then pick up the matching value. If the variable is not found, + * or if it has a null value, then our work here is done. + */ + *src_p = vend; + namelen = vend - vbegin + 1; + vname = malloc(namelen); + strlcpy(vname, vbegin, namelen); + vvalue = getenv(vname); + if (vvalue == NULL || *vvalue == '\0') { + if (env_verbosity > 2) + fprintf(stderr, + "#env replacing ${%s} with null string\n", + vname); + free(vname); + return (NULL); + } + + if (env_verbosity > 2) + fprintf(stderr, "#env expanding ${%s} into '%s'\n", vname, + vvalue); + + /* + * There is some value to copy to the destination. If the value is + * shorter than the ${VARNAME} reference that it replaces, then our + * caller can just copy the value to the existing destination. + */ + if (strlen(vname) + 3 >= strlen(vvalue)) { + free(vname); + return (vvalue); + } + + /* + * The value is longer than the string it replaces, which means the + * present destination area is too small to hold it. Create a new + * destination area, and update the caller's 'dest' variable to match. + * If the caller has already started copying some info for 'thisarg' + * into the present destination, then the new destination area must + * include a copy of that data, and the pointer to 'thisarg' must also + * be updated. Note that it is still the caller which copies this + * vvalue to the new *dest. + */ + newlen = strlen(vvalue) + strlen(*src_p) + 1; + if (in_thisarg) { + **dest_p = '\0'; /* Provide terminator for 'thisarg' */ + newlen += strlen(*thisarg_p); + newstr = malloc(newlen); + strcpy(newstr, *thisarg_p); + *thisarg_p = newstr; + } else { + newstr = malloc(newlen); + *newstr = '\0'; + } + *dest_p = strchr(newstr, '\0'); + free(vname); + return (vvalue); +} diff --git a/shell_cmds/env/envopts.h b/shell_cmds/env/envopts.h new file mode 100644 index 0000000..1f15c69 --- /dev/null +++ b/shell_cmds/env/envopts.h @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2005 - Garance Alistair Drosehn <gad@FreeBSD.org>. + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + * + * The views and conclusions contained in the software and documentation + * are those of the authors and should not be interpreted as representing + * official policies, either expressed or implied, of the FreeBSD Project. + * + * $FreeBSD$ + */ + +void search_paths(char *path, char **argv); +void split_spaces(const char *str, int *origind, int *origc, + char ***origv); + +extern int env_verbosity; |