]>
git.saurik.com Git - apple/shell_cmds.git/blob - env/envopts.c
2 * Copyright (c) 2005 - Garance Alistair Drosehn <gad@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * The views and conclusions contained in the software and documentation
27 * are those of the authors and should not be interpreted as representing
28 * official policies, either expressed or implied, of the FreeBSD Project.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
47 expand_vars(int in_thisarg
, char **thisarg_p
, char **dest_p
,
49 static int is_there(char *candidate
);
52 * The is*() routines take a parameter of 'int', but expect values in the range
53 * of unsigned char. Define some wrappers which take a value of type 'char',
54 * whether signed or unsigned, and ensure the value ends up in the right range.
56 #define isalnumch(Anychar) isalnum((u_char)(Anychar))
57 #define isalphach(Anychar) isalpha((u_char)(Anychar))
58 #define isspacech(Anychar) isspace((u_char)(Anychar))
61 * Routine to determine if a given fully-qualified filename is executable.
62 * This is copied almost verbatim from FreeBSD's usr.bin/which/which.c.
65 is_there(char *candidate
)
69 /* XXX work around access(2) false positives for superuser */
70 if (access(candidate
, X_OK
) == 0 &&
71 stat(candidate
, &fin
) == 0 &&
72 S_ISREG(fin
.st_mode
) &&
74 (fin
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) != 0)) {
75 if (env_verbosity
> 1)
76 fprintf(stderr
, "#env matched:\t'%s'\n", candidate
);
83 * Routine to search through an alternate path-list, looking for a given
84 * filename to execute. If the file is found, replace the original
85 * unqualified name with a fully-qualified path. This allows `env' to
86 * execute programs from a specific strict list of possible paths, without
87 * changing the value of PATH seen by the program which will be executed.
89 * #!/usr/bin/env -S-P/usr/local/bin:/usr/bin perl
90 * will execute /usr/local/bin/perl or /usr/bin/perl (whichever is found
91 * first), no matter what the current value of PATH is, and without
92 * changing the value of PATH that the script will see when it runs.
94 * This is similar to the print_matches() routine in usr.bin/which/which.c.
97 search_paths(char *path
, char **argv
)
99 char candidate
[PATH_MAX
];
101 char *filename
, *fqname
;
103 /* If the file has a `/' in it, then no search is done */
105 if (strchr(filename
, '/') != NULL
)
108 if (env_verbosity
> 1) {
109 fprintf(stderr
, "#env Searching:\t'%s'\n", path
);
110 fprintf(stderr
, "#env for file:\t'%s'\n", filename
);
114 while ((d
= strsep(&path
, ":")) != NULL
) {
117 if (snprintf(candidate
, sizeof(candidate
), "%s/%s", d
,
118 filename
) >= (int)sizeof(candidate
))
120 if (is_there(candidate
)) {
126 if (fqname
== NULL
) {
128 err(127, "%s", filename
);
130 *argv
= strdup(candidate
);
134 * Routine to split a string into multiple parameters, while recognizing a
135 * few special characters. It recognizes both single and double-quoted
136 * strings. This processing is designed entirely for the benefit of the
137 * parsing of "#!"-lines (aka "shebang" lines == the first line of an
138 * executable script). Different operating systems parse that line in very
139 * different ways, and this split-on-spaces processing is meant to provide
140 * ways to specify arbitrary arguments on that line, no matter how the OS
143 * Within a single-quoted string, the two characters "\'" are treated as
144 * a literal "'" character to add to the string, and "\\" are treated as
145 * a literal "\" character to add. Other than that, all characters are
146 * copied until the processing gets to a terminating "'".
148 * Within a double-quoted string, many more "\"-style escape sequences
149 * are recognized, mostly copied from what is recognized in the `printf'
150 * command. Some OS's will not allow a literal blank character to be
151 * included in the one argument that they recognize on a shebang-line,
152 * so a few additional escape-sequences are defined to provide ways to
155 * Within a double-quoted string "\_" is turned into a literal blank.
156 * (Inside of a single-quoted string, the two characters are just copied)
157 * Outside of a quoted string, "\_" is treated as both a blank, and the
158 * end of the current argument. So with a shelbang-line of:
159 * #!/usr/bin/env -SA=avalue\_perl
160 * the -S value would be broken up into arguments "A=avalue" and "perl".
163 split_spaces(const char *str
, int *origind
, int *origc
, char ***origv
)
165 static const char *nullarg
= "";
166 const char *bq_src
, *copystr
, *src
;
167 char *dest
, **newargv
, *newstr
, **nextarg
, **oldarg
;
168 int addcount
, bq_destlen
, copychar
, found_sep
, in_arg
, in_dq
, in_sq
;
171 * Ignore leading space on the string, and then malloc enough room
172 * to build a copy of it. The copy might end up shorter than the
173 * original, due to quoted strings and '\'-processing.
175 while (isspacech(*str
))
179 newstr
= malloc(strlen(str
) + 1);
182 * Allocate plenty of space for the new array of arg-pointers,
183 * and start that array off with the first element of the old
186 newargv
= malloc((*origc
+ (strlen(str
) / 2) + 2) * sizeof(char *));
188 *nextarg
++ = **origv
;
190 /* Come up with the new args by splitting up the given string. */
192 bq_destlen
= in_arg
= in_dq
= in_sq
= 0;
194 for (src
= str
, dest
= newstr
; *src
!= '\0'; src
++) {
196 * This switch will look at a character in *src, and decide
197 * what should be copied to *dest. It only decides what
198 * character(s) to copy, it should not modify *dest. In some
199 * cases, it will look at multiple characters from *src.
201 copychar
= found_sep
= 0;
211 * Referencing nullarg ensures that a new
212 * argument is created, even if this quoted
213 * string ends up with zero characters.
217 bq_destlen
= dest
- *(nextarg
- 1);
225 copystr
= expand_vars(in_arg
, (nextarg
- 1),
236 * Referencing nullarg ensures that a new
237 * argument is created, even if this quoted
238 * string ends up with zero characters.
242 bq_destlen
= dest
- *(nextarg
- 1);
249 * Inside single-quoted strings, only the
250 * "\'" and "\\" are recognized as special
253 copychar
= *(src
+ 1);
254 if (copychar
== '\'' || copychar
== '\\')
271 * Alternate way to get a blank, which allows
272 * that blank be used to separate arguments
273 * when it is not inside a quoted string.
284 * Ignore remaining characters in the -S string.
285 * This would not make sense if found in the
286 * middle of a quoted string.
289 errx(1, "Sequence '\\%c' is not allowed"
290 " in quoted strings", *src
);
311 errx(1, "Invalid sequence '\\%c' in -S",
316 if ((in_dq
|| in_sq
) && in_arg
)
318 else if (isspacech(*src
))
322 * If the first character of a new argument
323 * is `#', then ignore the remaining chars.
325 if (!in_arg
&& *src
== '#')
331 * Now that the switch has determined what (if anything)
332 * needs to be copied, copy whatever that is to *dest.
334 if (copychar
|| copystr
!= NULL
) {
336 /* This is the first byte of a new argument */
342 *dest
++ = (char)copychar
;
343 else if (copystr
!= NULL
)
344 while (*copystr
!= '\0')
345 *dest
++ = *copystr
++;
346 } else if (found_sep
) {
348 while (isspacech(*src
))
357 if (in_dq
|| in_sq
) {
358 errx(1, "No terminating quote for string: %.*s%s",
359 bq_destlen
, *(nextarg
- 1), bq_src
);
361 if (env_verbosity
> 1) {
362 fprintf(stderr
, "#env split -S:\t'%s'\n", str
);
363 oldarg
= newargv
+ 1;
364 fprintf(stderr
, "#env into:\t'%s'\n", *oldarg
);
365 for (oldarg
++; *oldarg
; oldarg
++)
366 fprintf(stderr
, "#env &\t'%s'\n", *oldarg
);
369 /* Copy the unprocessed arg-pointers from the original array */
370 for (oldarg
= *origv
+ *origind
; *oldarg
; oldarg
++)
371 *nextarg
++ = *oldarg
;
374 /* Update optind/argc/argv in the calling routine */
381 * Routine to split expand any environment variables referenced in the string
382 * that -S is processing. For now it only supports the form ${VARNAME}. It
383 * explicitly does not support $VARNAME, and obviously can not handle special
384 * shell-variables such as $?, $*, $1, etc. It is called with *src_p pointing
385 * at the initial '$', and if successful it will update *src_p, *dest_p, and
386 * possibly *thisarg_p in the calling routine.
389 expand_vars(int in_thisarg
, char **thisarg_p
, char **dest_p
, const char **src_p
)
391 const char *vbegin
, *vend
, *vvalue
;
392 char *newstr
, *vname
;
394 size_t namelen
, newlen
;
397 vbegin
= vend
= (*src_p
) + 1;
398 if (*vbegin
++ == '{')
399 if (*vbegin
== '_' || isalphach(*vbegin
)) {
401 while (*vend
== '_' || isalnumch(*vend
))
407 errx(1, "Only ${VARNAME} expansion is supported, error at: %s",
411 * We now know we have a valid environment variable name, so update
412 * the caller's source-pointer to the last character in that reference,
413 * and then pick up the matching value. If the variable is not found,
414 * or if it has a null value, then our work here is done.
417 namelen
= vend
- vbegin
+ 1;
418 vname
= malloc(namelen
);
419 strlcpy(vname
, vbegin
, namelen
);
420 vvalue
= getenv(vname
);
421 if (vvalue
== NULL
|| *vvalue
== '\0') {
422 if (env_verbosity
> 2)
424 "#env replacing ${%s} with null string\n",
430 if (env_verbosity
> 2)
431 fprintf(stderr
, "#env expanding ${%s} into '%s'\n", vname
,
435 * There is some value to copy to the destination. If the value is
436 * shorter than the ${VARNAME} reference that it replaces, then our
437 * caller can just copy the value to the existing destination.
439 if (strlen(vname
) + 3 >= strlen(vvalue
)) {
445 * The value is longer than the string it replaces, which means the
446 * present destination area is too small to hold it. Create a new
447 * destination area, and update the caller's 'dest' variable to match.
448 * If the caller has already started copying some info for 'thisarg'
449 * into the present destination, then the new destination area must
450 * include a copy of that data, and the pointer to 'thisarg' must also
451 * be updated. Note that it is still the caller which copies this
452 * vvalue to the new *dest.
454 newlen
= strlen(vvalue
) + strlen(*src_p
) + 1;
456 **dest_p
= '\0'; /* Provide terminator for 'thisarg' */
457 newlen
+= strlen(*thisarg_p
);
458 newstr
= malloc(newlen
);
459 strcpy(newstr
, *thisarg_p
);
462 newstr
= malloc(newlen
);
465 *dest_p
= strchr(newstr
, '\0');