Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * Original BSD copyright notice is retained at the end of this file. |
| 9 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 10 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 14 | * was re-ported from NetBSD and debianized. |
| 15 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 16 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 18 | //config:config SHELL_ASH |
| 19 | //config: bool #hidden option |
| 20 | //config: depends on !NOMMU |
| 21 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 22 | //config:config ASH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 23 | //config: bool "ash (78 kb)" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 24 | //config: default y |
| 25 | //config: depends on !NOMMU |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 26 | //config: select SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: The most complete and most pedantically correct shell included with |
| 29 | //config: busybox. This shell is actually a derivative of the Debian 'dash' |
| 30 | //config: shell (by Herbert Xu), which was created by porting the 'ash' shell |
| 31 | //config: (written by Kenneth Almquist) from NetBSD. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 32 | //config: |
Kang-Che Sung | 6cd0294 | 2017-01-06 17:02:03 +0100 | [diff] [blame] | 33 | //config:# ash options |
| 34 | //config:# note: Don't remove !NOMMU part in the next line; it would break |
| 35 | //config:# menuconfig's indenting. |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 36 | //config:if !NOMMU && (SHELL_ASH || ASH || SH_IS_ASH || BASH_IS_ASH) |
Kang-Che Sung | 6cd0294 | 2017-01-06 17:02:03 +0100 | [diff] [blame] | 37 | //config: |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 38 | //config:config ASH_OPTIMIZE_FOR_SIZE |
| 39 | //config: bool "Optimize for size instead of speed" |
| 40 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 41 | //config: depends on SHELL_ASH |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 42 | //config: |
| 43 | //config:config ASH_INTERNAL_GLOB |
| 44 | //config: bool "Use internal glob() implementation" |
Denys Vlasenko | 326edc3 | 2016-12-22 14:36:49 +0100 | [diff] [blame] | 45 | //config: default y # Y is bigger, but because of uclibc glob() bug, let Y be default for now |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 46 | //config: depends on SHELL_ASH |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 47 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 48 | //config: Do not use glob() function from libc, use internal implementation. |
| 49 | //config: Use this if you are getting "glob.h: No such file or directory" |
| 50 | //config: or similar build errors. |
| 51 | //config: Note that as of now (2017-01), uclibc and musl glob() both have bugs |
| 52 | //config: which would break ash if you select N here. |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 53 | //config: |
| 54 | //config:config ASH_BASH_COMPAT |
| 55 | //config: bool "bash-compatible extensions" |
| 56 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 57 | //config: depends on SHELL_ASH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 58 | //config: |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 59 | //config:config ASH_BASH_SOURCE_CURDIR |
| 60 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 61 | //config: default n # do not encourage non-standard behavior |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 62 | //config: depends on ASH_BASH_COMPAT |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 63 | //config: help |
| 64 | //config: This is not compliant with standards. Avoid if possible. |
| 65 | //config: |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 66 | //config:config ASH_BASH_NOT_FOUND_HOOK |
| 67 | //config: bool "command_not_found_handle hook support" |
| 68 | //config: default y |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 69 | //config: depends on ASH_BASH_COMPAT |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 70 | //config: help |
| 71 | //config: Enable support for the 'command_not_found_handle' hook function, |
| 72 | //config: from GNU bash, which allows for alternative command not found |
| 73 | //config: handling. |
| 74 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 75 | //config:config ASH_JOB_CONTROL |
| 76 | //config: bool "Job control" |
| 77 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 78 | //config: depends on SHELL_ASH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 79 | //config: |
| 80 | //config:config ASH_ALIAS |
| 81 | //config: bool "Alias support" |
| 82 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 83 | //config: depends on SHELL_ASH |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 84 | //config: |
| 85 | //config:config ASH_RANDOM_SUPPORT |
| 86 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 87 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 88 | //config: depends on SHELL_ASH |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 89 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 90 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 91 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 92 | //config: You can reset the generator by using a specified start value. |
| 93 | //config: After "unset RANDOM" the generator will switch off and this |
| 94 | //config: variable will no longer have special treatment. |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 95 | //config: |
| 96 | //config:config ASH_EXPAND_PRMT |
| 97 | //config: bool "Expand prompt string" |
| 98 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 99 | //config: depends on SHELL_ASH |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 100 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 101 | //config: $PS# may contain volatile content, such as backquote commands. |
| 102 | //config: This option recreates the prompt string from the environment |
| 103 | //config: variable each time it is displayed. |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 104 | //config: |
Denys Vlasenko | 046341e | 2011-02-04 17:53:59 +0100 | [diff] [blame] | 105 | //config:config ASH_IDLE_TIMEOUT |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 106 | //config: bool "Idle timeout variable $TMOUT" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 107 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 108 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 109 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 110 | //config: Enable bash-like auto-logout after $TMOUT seconds of idle time. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 111 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 112 | //config:config ASH_MAIL |
| 113 | //config: bool "Check for new mail in interactive shell" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 114 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 115 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 116 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 117 | //config: Enable "check for new mail" function: |
| 118 | //config: if set, $MAIL file and $MAILPATH list of files |
| 119 | //config: are checked for mtime changes, and "you have mail" |
| 120 | //config: message is printed if change is detected. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 121 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 122 | //config:config ASH_ECHO |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 123 | //config: bool "echo builtin" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 124 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 125 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 126 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 127 | //config:config ASH_PRINTF |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 128 | //config: bool "printf builtin" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 129 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 130 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 131 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 132 | //config:config ASH_TEST |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 133 | //config: bool "test builtin" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 134 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 135 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 136 | //config: |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 137 | //config:config ASH_HELP |
| 138 | //config: bool "help builtin" |
| 139 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 140 | //config: depends on SHELL_ASH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 141 | //config: |
| 142 | //config:config ASH_GETOPTS |
| 143 | //config: bool "getopts builtin" |
| 144 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 145 | //config: depends on SHELL_ASH |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 146 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 147 | //config:config ASH_CMDCMD |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 148 | //config: bool "command builtin" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 149 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 150 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 151 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 152 | //config: Enable support for the 'command' builtin, which allows |
| 153 | //config: you to run the specified command or builtin, |
| 154 | //config: even when there is a function with the same name. |
Kang-Che Sung | 6cd0294 | 2017-01-06 17:02:03 +0100 | [diff] [blame] | 155 | //config: |
| 156 | //config:endif # ash options |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 157 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 158 | //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 159 | // APPLET_ODDNAME:name main location suid_type help |
| 160 | //applet:IF_SH_IS_ASH( APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, ash)) |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 161 | //applet:IF_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, ash)) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 162 | |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 163 | //kbuild:lib-$(CONFIG_SHELL_ASH) += ash.o ash_ptr_hack.o shell_common.o |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 164 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
| 165 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 166 | /* |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 167 | * DEBUG=1 to compile in debugging ('set -o debug' turns on) |
| 168 | * DEBUG=2 to compile in and turn on debugging. |
| 169 | * When debugging is on ("set -o debug" was executed, or DEBUG=2), |
| 170 | * debugging info is written to ./trace, quit signal generates core dump. |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 171 | */ |
| 172 | #define DEBUG 0 |
| 173 | /* Tweak debug output verbosity here */ |
| 174 | #define DEBUG_TIME 0 |
| 175 | #define DEBUG_PID 1 |
| 176 | #define DEBUG_SIG 1 |
| 177 | #define DEBUG_INTONOFF 0 |
| 178 | |
| 179 | #define PROFILE 0 |
| 180 | |
| 181 | #define JOBS ENABLE_ASH_JOB_CONTROL |
| 182 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 183 | #include <fnmatch.h> |
| 184 | #include <sys/times.h> |
| 185 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 186 | #include "busybox.h" /* for applet_names */ |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 187 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 188 | # include "embedded_scripts.h" |
| 189 | #else |
| 190 | # define NUM_SCRIPTS 0 |
| 191 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 192 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 193 | /* So far, all bash compat is controlled by one config option */ |
| 194 | /* Separate defines document which part of code implements what */ |
| 195 | /* function keyword */ |
| 196 | #define BASH_FUNCTION ENABLE_ASH_BASH_COMPAT |
| 197 | #define IF_BASH_FUNCTION IF_ASH_BASH_COMPAT |
| 198 | /* &>file */ |
| 199 | #define BASH_REDIR_OUTPUT ENABLE_ASH_BASH_COMPAT |
| 200 | #define IF_BASH_REDIR_OUTPUT IF_ASH_BASH_COMPAT |
| 201 | /* $'...' */ |
| 202 | #define BASH_DOLLAR_SQUOTE ENABLE_ASH_BASH_COMPAT |
| 203 | #define IF_BASH_DOLLAR_SQUOTE IF_ASH_BASH_COMPAT |
| 204 | #define BASH_PATTERN_SUBST ENABLE_ASH_BASH_COMPAT |
| 205 | #define IF_BASH_PATTERN_SUBST IF_ASH_BASH_COMPAT |
| 206 | #define BASH_SUBSTR ENABLE_ASH_BASH_COMPAT |
| 207 | #define IF_BASH_SUBSTR IF_ASH_BASH_COMPAT |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 208 | /* BASH_TEST2: [[ EXPR ]] |
| 209 | * Status of [[ support: |
| 210 | * We replace && and || with -a and -o |
| 211 | * TODO: |
| 212 | * singleword+noglob expansion: |
| 213 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 214 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 215 | * -a/-o are not AND/OR ops! (they are just strings) |
| 216 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
| 217 | * = is glob match operator, not equality operator: STR = GLOB |
| 218 | * (in GLOB, quoting is significant on char-by-char basis: a*cd"*") |
| 219 | * == same as = |
| 220 | * add =~ regex match operator: STR =~ REGEX |
| 221 | */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 222 | #define BASH_TEST2 (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST) |
| 223 | #define BASH_SOURCE ENABLE_ASH_BASH_COMPAT |
| 224 | #define BASH_PIPEFAIL ENABLE_ASH_BASH_COMPAT |
| 225 | #define BASH_HOSTNAME_VAR ENABLE_ASH_BASH_COMPAT |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 226 | #define BASH_EPOCH_VARS ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 227 | #define BASH_SHLVL_VAR ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | f8cdc7a | 2017-08-04 15:24:49 +0200 | [diff] [blame] | 228 | #define BASH_XTRACEFD ENABLE_ASH_BASH_COMPAT |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 229 | #define BASH_READ_D ENABLE_ASH_BASH_COMPAT |
| 230 | #define IF_BASH_READ_D IF_ASH_BASH_COMPAT |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 231 | #define BASH_WAIT_N ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 232 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 233 | #if defined(__ANDROID_API__) && __ANDROID_API__ <= 24 |
| 234 | /* Bionic at least up to version 24 has no glob() */ |
| 235 | # undef ENABLE_ASH_INTERNAL_GLOB |
| 236 | # define ENABLE_ASH_INTERNAL_GLOB 1 |
| 237 | #endif |
| 238 | |
| 239 | #if !ENABLE_ASH_INTERNAL_GLOB && defined(__UCLIBC__) |
| 240 | # error uClibc glob() is buggy, use ASH_INTERNAL_GLOB. |
| 241 | # error The bug is: for "$PWD"/<pattern> ash will escape e.g. dashes in "$PWD" |
| 242 | # error with backslash, even ones which do not need to be: "/a-b" -> "/a\-b" |
| 243 | # error glob() should unbackslash them and match. uClibc does not unbackslash, |
| 244 | # error fails to match dirname, subsequently not expanding <pattern> in it. |
| 245 | // Testcase: |
| 246 | // if (glob("/etc/polkit\\-1", 0, NULL, &pglob)) - this returns 0 on uclibc, no bug |
| 247 | // if (glob("/etc/polkit\\-1/*", 0, NULL, &pglob)) printf("uclibc bug!\n"); |
| 248 | #endif |
| 249 | |
| 250 | #if !ENABLE_ASH_INTERNAL_GLOB |
| 251 | # include <glob.h> |
| 252 | #endif |
| 253 | |
| 254 | #include "unicode.h" |
| 255 | #include "shell_common.h" |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 256 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 257 | # include "math.h" |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 258 | #else |
| 259 | typedef long arith_t; |
| 260 | # define ARITH_FMT "%ld" |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 261 | #endif |
| 262 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 263 | # include "random.h" |
| 264 | #else |
| 265 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 266 | #endif |
| 267 | |
| 268 | #include "NUM_APPLETS.h" |
| 269 | #if NUM_APPLETS == 1 |
| 270 | /* STANDALONE does not make sense, and won't compile */ |
| 271 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 272 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 273 | # undef IF_FEATURE_SH_STANDALONE |
| 274 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 275 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 276 | # define IF_FEATURE_SH_STANDALONE(...) |
| 277 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 278 | #endif |
| 279 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 280 | #ifndef F_DUPFD_CLOEXEC |
| 281 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 282 | #endif |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 283 | #ifndef O_CLOEXEC |
| 284 | # define O_CLOEXEC 0 |
| 285 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 286 | #ifndef PIPE_BUF |
| 287 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 288 | #endif |
| 289 | |
| 290 | #if !BB_MMU |
| 291 | # error "Do not even bother, ash will not run on NOMMU machine" |
| 292 | #endif |
| 293 | |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 294 | /* We use a trick to have more optimized code (fewer pointer reloads): |
| 295 | * ash.c: extern struct globals *const ash_ptr_to_globals; |
| 296 | * ash_ptr_hack.c: struct globals *ash_ptr_to_globals; |
| 297 | * This way, compiler in ash.c knows the pointer can not change. |
| 298 | * |
| 299 | * However, this may break on weird arches or toolchains. In this case, |
| 300 | * set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable |
| 301 | * this optimization. |
| 302 | */ |
| 303 | #ifndef BB_GLOBAL_CONST |
| 304 | # define BB_GLOBAL_CONST const |
| 305 | #endif |
| 306 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 307 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 308 | /* ============ Hash table sizes. Configurable. */ |
| 309 | |
| 310 | #define VTABSIZE 39 |
| 311 | #define ATABSIZE 39 |
| 312 | #define CMDTABLESIZE 31 /* should be prime */ |
| 313 | |
| 314 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 315 | /* ============ Shell options */ |
| 316 | |
| 317 | static const char *const optletters_optnames[] = { |
| 318 | "e" "errexit", |
| 319 | "f" "noglob", |
| 320 | "I" "ignoreeof", |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 321 | /* The below allowed this invocation: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 322 | * ash -c 'set -i; echo $-; sleep 5; echo $-' |
| 323 | * to be ^C-ed and get to interactive ash prompt. |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 324 | * bash does not support such "set -i". |
| 325 | * In our code, this is denoted by empty long name: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 326 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 327 | "i" "", |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 328 | "m" "monitor", |
| 329 | "n" "noexec", |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 330 | /* Ditto: bash has no "set -s" */ |
| 331 | "s" "", |
| 332 | "c" "", |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 333 | "x" "xtrace", |
| 334 | "v" "verbose", |
| 335 | "C" "noclobber", |
| 336 | "a" "allexport", |
| 337 | "b" "notify", |
| 338 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 339 | "\0" "vi" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 340 | #if BASH_PIPEFAIL |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 341 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 342 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 343 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 344 | ,"\0" "nolog" |
| 345 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 346 | #endif |
| 347 | }; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 348 | //bash 4.4.23 also has these opts (with these defaults): |
| 349 | //braceexpand on |
| 350 | //emacs on |
| 351 | //errtrace off |
| 352 | //functrace off |
| 353 | //hashall on |
| 354 | //histexpand off |
| 355 | //history on |
| 356 | //interactive-comments on |
| 357 | //keyword off |
| 358 | //onecmd off |
| 359 | //physical off |
| 360 | //posix off |
| 361 | //privileged off |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 362 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 363 | #define optletters(n) optletters_optnames[n][0] |
| 364 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 365 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 366 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 367 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 368 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 369 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 370 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 371 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 372 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 373 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 374 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 375 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 376 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 377 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 378 | * exception handlers, the user should save the value of handler on entry |
| 379 | * to an inner scope, set handler to point to a jmploc structure for the |
| 380 | * inner scope, and restore handler on exit from the scope. |
| 381 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 382 | struct jmploc { |
| 383 | jmp_buf loc; |
| 384 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 385 | |
| 386 | struct globals_misc { |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 387 | uint8_t exitstatus; /* exit status of last command */ |
| 388 | uint8_t back_exitstatus;/* exit status of backquoted command */ |
| 389 | smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 390 | int savestatus; /* exit status of last command outside traps */ |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 391 | int rootpid; /* pid of main shell */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 392 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 393 | int shlvl; |
| 394 | #define rootshell (!shlvl) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 395 | int errlinno; |
| 396 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 397 | char *minusc; /* argument to -c option */ |
| 398 | |
| 399 | char *curdir; // = nullstr; /* current working directory */ |
| 400 | char *physdir; // = nullstr; /* physical working directory */ |
| 401 | |
| 402 | char *arg0; /* value of $0 */ |
| 403 | |
| 404 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 405 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 406 | volatile int suppress_int; /* counter */ |
| 407 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 408 | volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */ |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 409 | volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 410 | smallint exception_type; /* kind of exception: */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 411 | #define EXINT 0 /* SIGINT received */ |
| 412 | #define EXERROR 1 /* a generic error */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 413 | #define EXEND 3 /* exit the shell */ |
| 414 | #define EXEXIT 4 /* exit the shell via exitcmd */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 415 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 416 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 417 | |
| 418 | char optlist[NOPTS]; |
| 419 | #define eflag optlist[0] |
| 420 | #define fflag optlist[1] |
| 421 | #define Iflag optlist[2] |
| 422 | #define iflag optlist[3] |
| 423 | #define mflag optlist[4] |
| 424 | #define nflag optlist[5] |
| 425 | #define sflag optlist[6] |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 426 | #define cflag optlist[7] |
| 427 | #define xflag optlist[8] |
| 428 | #define vflag optlist[9] |
| 429 | #define Cflag optlist[10] |
| 430 | #define aflag optlist[11] |
| 431 | #define bflag optlist[12] |
| 432 | #define uflag optlist[13] |
| 433 | #define viflag optlist[14] |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 434 | #if BASH_PIPEFAIL |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 435 | # define pipefail optlist[15] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 436 | #else |
| 437 | # define pipefail 0 |
| 438 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 439 | #if DEBUG |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 440 | # define nolog optlist[15 + BASH_PIPEFAIL] |
| 441 | # define debug optlist[16 + BASH_PIPEFAIL] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 442 | #endif |
| 443 | |
| 444 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 445 | /* |
| 446 | * Sigmode records the current value of the signal handlers for the various |
| 447 | * modes. A value of zero means that the current handler is not known. |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 448 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell. |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 449 | */ |
| 450 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 451 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 452 | #define S_CATCH 2 /* signal is caught */ |
| 453 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 454 | #define S_HARD_IGN 4 /* signal is ignored permanently (it was SIG_IGN on entry to shell) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 456 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 457 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 458 | uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 459 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 460 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 461 | |
| 462 | /* Rarely referenced stuff */ |
| 463 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 464 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 465 | #endif |
| 466 | pid_t backgndpid; /* pid of last background process */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 467 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 468 | extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 469 | #define G_misc (*ash_ptr_to_globals_misc) |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 470 | #define exitstatus (G_misc.exitstatus ) |
| 471 | #define back_exitstatus (G_misc.back_exitstatus ) |
| 472 | #define job_warning (G_misc.job_warning) |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 473 | #define savestatus (G_misc.savestatus ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 474 | #define rootpid (G_misc.rootpid ) |
| 475 | #define shlvl (G_misc.shlvl ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 476 | #define errlinno (G_misc.errlinno ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 477 | #define minusc (G_misc.minusc ) |
| 478 | #define curdir (G_misc.curdir ) |
| 479 | #define physdir (G_misc.physdir ) |
| 480 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 481 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 482 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 483 | #define suppress_int (G_misc.suppress_int ) |
| 484 | #define pending_int (G_misc.pending_int ) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 485 | #define got_sigchld (G_misc.got_sigchld ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 486 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 487 | #define nullstr (G_misc.nullstr ) |
| 488 | #define optlist (G_misc.optlist ) |
| 489 | #define sigmode (G_misc.sigmode ) |
| 490 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 491 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 492 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 493 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 494 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 495 | #define backgndpid (G_misc.backgndpid ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 496 | #define INIT_G_misc() do { \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 497 | (*(struct globals_misc**)not_const_pp(&ash_ptr_to_globals_misc)) = xzalloc(sizeof(G_misc)); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 498 | barrier(); \ |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 499 | savestatus = -1; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 500 | curdir = nullstr; \ |
| 501 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 502 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 503 | } while (0) |
| 504 | |
| 505 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 506 | /* ============ DEBUG */ |
| 507 | #if DEBUG |
| 508 | static void trace_printf(const char *fmt, ...); |
| 509 | static void trace_vprintf(const char *fmt, va_list va); |
| 510 | # define TRACE(param) trace_printf param |
| 511 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 512 | # define close(fd) do { \ |
| 513 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 514 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 515 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 516 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 517 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 518 | #else |
| 519 | # define TRACE(param) |
| 520 | # define TRACEV(param) |
| 521 | #endif |
| 522 | |
| 523 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 524 | /* ============ Utility functions */ |
Denys Vlasenko | 1961aea | 2013-02-26 00:36:53 +0100 | [diff] [blame] | 525 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 526 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 527 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 528 | static int |
| 529 | isdigit_str9(const char *str) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 530 | { |
| 531 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 532 | while (--maxlen && isdigit(*str)) |
| 533 | str++; |
| 534 | return (*str == '\0'); |
| 535 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 536 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 537 | static const char * |
| 538 | var_end(const char *var) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 539 | { |
| 540 | while (*var) |
| 541 | if (*var++ == '=') |
| 542 | break; |
| 543 | return var; |
| 544 | } |
| 545 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 546 | |
| 547 | /* ============ Interrupts / exceptions */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 548 | |
| 549 | static void exitshell(void) NORETURN; |
| 550 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 551 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 552 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 553 | * over a period of time. This is similar to SIGHOLD or to sigblock, but |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 554 | * much more efficient and portable. (But hacking the kernel is so much |
| 555 | * more fun than worrying about efficiency and portability. :-)) |
| 556 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 557 | #if DEBUG_INTONOFF |
| 558 | # define INT_OFF do { \ |
| 559 | TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 560 | suppress_int++; \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 561 | barrier(); \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 562 | } while (0) |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 563 | #else |
| 564 | # define INT_OFF do { \ |
| 565 | suppress_int++; \ |
| 566 | barrier(); \ |
| 567 | } while (0) |
| 568 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 572 | * just do a longjmp to the exception handler. The type of exception is |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 573 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 574 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 575 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 576 | static void |
| 577 | raise_exception(int e) |
| 578 | { |
| 579 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 580 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 581 | abort(); |
| 582 | #endif |
| 583 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 584 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 585 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 586 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 587 | #if DEBUG |
| 588 | #define raise_exception(e) do { \ |
| 589 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 590 | raise_exception(e); \ |
| 591 | } while (0) |
| 592 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 593 | |
| 594 | /* |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 595 | * Called when a SIGINT is received. (If the user specifies |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 596 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 597 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 598 | * are held using the INT_OFF macro. (The test for iflag is just |
| 599 | * defensive programming.) |
| 600 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 601 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 602 | static void |
| 603 | raise_interrupt(void) |
| 604 | { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 605 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 606 | /* Signal is not automatically unmasked after it is raised, |
| 607 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 608 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 609 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 610 | |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 611 | if (!(rootshell && iflag)) { |
| 612 | /* Kill ourself with SIGINT */ |
| 613 | signal(SIGINT, SIG_DFL); |
| 614 | raise(SIGINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 615 | } |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 616 | /* bash: ^C even on empty command line sets $? */ |
| 617 | exitstatus = SIGINT + 128; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 618 | raise_exception(EXINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 619 | /* NOTREACHED */ |
| 620 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 621 | #if DEBUG |
| 622 | #define raise_interrupt() do { \ |
| 623 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 624 | raise_interrupt(); \ |
| 625 | } while (0) |
| 626 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 627 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 628 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 629 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 630 | { |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 631 | barrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 632 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 633 | raise_interrupt(); |
| 634 | } |
| 635 | } |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 636 | #if DEBUG_INTONOFF |
| 637 | # define INT_ON do { \ |
| 638 | TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \ |
| 639 | int_on(); \ |
| 640 | } while (0) |
| 641 | #else |
| 642 | # define INT_ON int_on() |
| 643 | #endif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 644 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 645 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 646 | { |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 647 | barrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 648 | suppress_int = 0; |
| 649 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 650 | raise_interrupt(); |
| 651 | } |
| 652 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 653 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 654 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 655 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 656 | #define RESTORE_INT(v) do { \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 657 | barrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 658 | suppress_int = (v); \ |
| 659 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 660 | raise_interrupt(); \ |
| 661 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 662 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 663 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 664 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 665 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 666 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 667 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 668 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 669 | INT_OFF; |
| 670 | fputs(p, file); |
| 671 | INT_ON; |
| 672 | } |
| 673 | |
| 674 | static void |
| 675 | flush_stdout_stderr(void) |
| 676 | { |
| 677 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 678 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 679 | INT_ON; |
| 680 | } |
| 681 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 682 | /* Was called outcslow(c,FILE*), but c was always '\n' */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 683 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 684 | newline_and_flush(FILE *dest) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 685 | { |
| 686 | INT_OFF; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 687 | putc('\n', dest); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 688 | fflush(dest); |
| 689 | INT_ON; |
| 690 | } |
| 691 | |
| 692 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 693 | static int |
| 694 | out1fmt(const char *fmt, ...) |
| 695 | { |
| 696 | va_list ap; |
| 697 | int r; |
| 698 | |
| 699 | INT_OFF; |
| 700 | va_start(ap, fmt); |
| 701 | r = vprintf(fmt, ap); |
| 702 | va_end(ap); |
| 703 | INT_ON; |
| 704 | return r; |
| 705 | } |
| 706 | |
| 707 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 708 | static int |
| 709 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 710 | { |
| 711 | va_list ap; |
| 712 | int ret; |
| 713 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 714 | INT_OFF; |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 715 | va_start(ap, fmt); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 716 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 717 | va_end(ap); |
| 718 | INT_ON; |
Denys Vlasenko | 3f7fb2c | 2020-02-16 18:06:20 +0100 | [diff] [blame] | 719 | return ret > (int)length ? length : ret; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static void |
| 723 | out1str(const char *p) |
| 724 | { |
| 725 | outstr(p, stdout); |
| 726 | } |
| 727 | |
| 728 | static void |
| 729 | out2str(const char *p) |
| 730 | { |
| 731 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 732 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 736 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 737 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 738 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 739 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 740 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 741 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 742 | #define CTLENDVAR ((unsigned char)'\203') |
| 743 | #define CTLBACKQ ((unsigned char)'\204') |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 744 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 745 | #define CTLENDARI ((unsigned char)'\207') |
| 746 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 747 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 748 | |
| 749 | /* variable substitution byte (follows CTLVAR) */ |
| 750 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 751 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 752 | |
| 753 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 754 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 755 | #define VSMINUS 0x2 /* ${var-text} */ |
| 756 | #define VSPLUS 0x3 /* ${var+text} */ |
| 757 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 758 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 759 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 760 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 761 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 762 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 763 | #define VSLENGTH 0xa /* ${#var} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 764 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 765 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 766 | #endif |
| 767 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 768 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 769 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 770 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 771 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 772 | static const char dolatstr[] ALIGN1 = { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 773 | CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0' |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 774 | }; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 775 | #define DOLATSTRLEN 6 |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 776 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 777 | #define NCMD 0 |
| 778 | #define NPIPE 1 |
| 779 | #define NREDIR 2 |
| 780 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 781 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 782 | #define NAND 5 |
| 783 | #define NOR 6 |
| 784 | #define NSEMI 7 |
| 785 | #define NIF 8 |
| 786 | #define NWHILE 9 |
| 787 | #define NUNTIL 10 |
| 788 | #define NFOR 11 |
| 789 | #define NCASE 12 |
| 790 | #define NCLIST 13 |
| 791 | #define NDEFUN 14 |
| 792 | #define NARG 15 |
| 793 | #define NTO 16 |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 794 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 795 | #define NTO2 17 |
| 796 | #endif |
| 797 | #define NCLOBBER 18 |
| 798 | #define NFROM 19 |
| 799 | #define NFROMTO 20 |
| 800 | #define NAPPEND 21 |
| 801 | #define NTOFD 22 |
| 802 | #define NFROMFD 23 |
| 803 | #define NHERE 24 |
| 804 | #define NXHERE 25 |
| 805 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 806 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 807 | |
| 808 | union node; |
| 809 | |
| 810 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 811 | smallint type; /* Nxxxx */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 812 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 813 | union node *assign; |
| 814 | union node *args; |
| 815 | union node *redirect; |
| 816 | }; |
| 817 | |
| 818 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 819 | smallint type; |
| 820 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 821 | struct nodelist *cmdlist; |
| 822 | }; |
| 823 | |
| 824 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 825 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 826 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 827 | union node *n; |
| 828 | union node *redirect; |
| 829 | }; |
| 830 | |
| 831 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 832 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 833 | union node *ch1; |
| 834 | union node *ch2; |
| 835 | }; |
| 836 | |
| 837 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 838 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 839 | union node *test; |
| 840 | union node *ifpart; |
| 841 | union node *elsepart; |
| 842 | }; |
| 843 | |
| 844 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 845 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 846 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 847 | union node *args; |
| 848 | union node *body; |
| 849 | char *var; |
| 850 | }; |
| 851 | |
| 852 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 853 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 854 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 855 | union node *expr; |
| 856 | union node *cases; |
| 857 | }; |
| 858 | |
| 859 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 860 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 861 | union node *next; |
| 862 | union node *pattern; |
| 863 | union node *body; |
| 864 | }; |
| 865 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 866 | struct ndefun { |
| 867 | smallint type; |
| 868 | int linno; |
| 869 | char *text; |
| 870 | union node *body; |
| 871 | }; |
| 872 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 873 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 874 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 875 | union node *next; |
| 876 | char *text; |
| 877 | struct nodelist *backquote; |
| 878 | }; |
| 879 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 880 | /* nfile and ndup layout must match! |
| 881 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 882 | * that it is actually NTO2 (>&file), and change its type. |
| 883 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 884 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 885 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 886 | union node *next; |
| 887 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 888 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 889 | union node *fname; |
| 890 | char *expfname; |
| 891 | }; |
| 892 | |
| 893 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 894 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 895 | union node *next; |
| 896 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 897 | int dupfd; |
| 898 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 899 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 900 | }; |
| 901 | |
| 902 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 903 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 904 | union node *next; |
| 905 | int fd; |
| 906 | union node *doc; |
| 907 | }; |
| 908 | |
| 909 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 910 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 911 | union node *com; |
| 912 | }; |
| 913 | |
| 914 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 915 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 916 | struct ncmd ncmd; |
| 917 | struct npipe npipe; |
| 918 | struct nredir nredir; |
| 919 | struct nbinary nbinary; |
| 920 | struct nif nif; |
| 921 | struct nfor nfor; |
| 922 | struct ncase ncase; |
| 923 | struct nclist nclist; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 924 | struct ndefun ndefun; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 925 | struct narg narg; |
| 926 | struct nfile nfile; |
| 927 | struct ndup ndup; |
| 928 | struct nhere nhere; |
| 929 | struct nnot nnot; |
| 930 | }; |
| 931 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 932 | /* |
| 933 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 934 | * It must be distinct from NULL. |
| 935 | */ |
| 936 | #define NODE_EOF ((union node *) -1L) |
| 937 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 938 | struct nodelist { |
| 939 | struct nodelist *next; |
| 940 | union node *n; |
| 941 | }; |
| 942 | |
| 943 | struct funcnode { |
| 944 | int count; |
| 945 | union node n; |
| 946 | }; |
| 947 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 948 | /* |
| 949 | * Free a parse tree. |
| 950 | */ |
| 951 | static void |
| 952 | freefunc(struct funcnode *f) |
| 953 | { |
| 954 | if (f && --f->count < 0) |
| 955 | free(f); |
| 956 | } |
| 957 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 958 | |
| 959 | /* ============ Debugging output */ |
| 960 | |
| 961 | #if DEBUG |
| 962 | |
| 963 | static FILE *tracefile; |
| 964 | |
| 965 | static void |
| 966 | trace_printf(const char *fmt, ...) |
| 967 | { |
| 968 | va_list va; |
| 969 | |
| 970 | if (debug != 1) |
| 971 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 972 | if (DEBUG_TIME) |
| 973 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 974 | if (DEBUG_PID) |
| 975 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 976 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 977 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 978 | va_start(va, fmt); |
| 979 | vfprintf(tracefile, fmt, va); |
| 980 | va_end(va); |
| 981 | } |
| 982 | |
| 983 | static void |
| 984 | trace_vprintf(const char *fmt, va_list va) |
| 985 | { |
| 986 | if (debug != 1) |
| 987 | return; |
| 988 | vfprintf(tracefile, fmt, va); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 989 | fprintf(tracefile, "\n"); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static void |
| 993 | trace_puts(const char *s) |
| 994 | { |
| 995 | if (debug != 1) |
| 996 | return; |
| 997 | fputs(s, tracefile); |
| 998 | } |
| 999 | |
| 1000 | static void |
| 1001 | trace_puts_quoted(char *s) |
| 1002 | { |
| 1003 | char *p; |
| 1004 | char c; |
| 1005 | |
| 1006 | if (debug != 1) |
| 1007 | return; |
| 1008 | putc('"', tracefile); |
| 1009 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1010 | switch ((unsigned char)*p) { |
| 1011 | case '\n': c = 'n'; goto backslash; |
| 1012 | case '\t': c = 't'; goto backslash; |
| 1013 | case '\r': c = 'r'; goto backslash; |
| 1014 | case '\"': c = '\"'; goto backslash; |
| 1015 | case '\\': c = '\\'; goto backslash; |
| 1016 | case CTLESC: c = 'e'; goto backslash; |
| 1017 | case CTLVAR: c = 'v'; goto backslash; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1018 | case CTLBACKQ: c = 'q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1019 | backslash: |
| 1020 | putc('\\', tracefile); |
| 1021 | putc(c, tracefile); |
| 1022 | break; |
| 1023 | default: |
| 1024 | if (*p >= ' ' && *p <= '~') |
| 1025 | putc(*p, tracefile); |
| 1026 | else { |
| 1027 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1028 | putc((*p >> 6) & 03, tracefile); |
| 1029 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1030 | putc(*p & 07, tracefile); |
| 1031 | } |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | putc('"', tracefile); |
| 1036 | } |
| 1037 | |
| 1038 | static void |
| 1039 | trace_puts_args(char **ap) |
| 1040 | { |
| 1041 | if (debug != 1) |
| 1042 | return; |
| 1043 | if (!*ap) |
| 1044 | return; |
| 1045 | while (1) { |
| 1046 | trace_puts_quoted(*ap); |
| 1047 | if (!*++ap) { |
| 1048 | putc('\n', tracefile); |
| 1049 | break; |
| 1050 | } |
| 1051 | putc(' ', tracefile); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | opentrace(void) |
| 1057 | { |
| 1058 | char s[100]; |
| 1059 | #ifdef O_APPEND |
| 1060 | int flags; |
| 1061 | #endif |
| 1062 | |
| 1063 | if (debug != 1) { |
| 1064 | if (tracefile) |
| 1065 | fflush(tracefile); |
| 1066 | /* leave open because libedit might be using it */ |
| 1067 | return; |
| 1068 | } |
| 1069 | strcpy(s, "./trace"); |
| 1070 | if (tracefile) { |
| 1071 | if (!freopen(s, "a", tracefile)) { |
| 1072 | fprintf(stderr, "Can't re-open %s\n", s); |
| 1073 | debug = 0; |
| 1074 | return; |
| 1075 | } |
| 1076 | } else { |
| 1077 | tracefile = fopen(s, "a"); |
| 1078 | if (tracefile == NULL) { |
| 1079 | fprintf(stderr, "Can't open %s\n", s); |
| 1080 | debug = 0; |
| 1081 | return; |
| 1082 | } |
| 1083 | } |
| 1084 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 1085 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1086 | if (flags >= 0) |
| 1087 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 1088 | #endif |
| 1089 | setlinebuf(tracefile); |
| 1090 | fputs("\nTracing started.\n", tracefile); |
| 1091 | } |
| 1092 | |
| 1093 | static void |
| 1094 | indent(int amount, char *pfx, FILE *fp) |
| 1095 | { |
| 1096 | int i; |
| 1097 | |
| 1098 | for (i = 0; i < amount; i++) { |
| 1099 | if (pfx && i == amount - 1) |
| 1100 | fputs(pfx, fp); |
| 1101 | putc('\t', fp); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* little circular references here... */ |
| 1106 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 1107 | |
| 1108 | static void |
| 1109 | sharg(union node *arg, FILE *fp) |
| 1110 | { |
| 1111 | char *p; |
| 1112 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1113 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (arg->type != NARG) { |
| 1116 | out1fmt("<node type %d>\n", arg->type); |
| 1117 | abort(); |
| 1118 | } |
| 1119 | bqlist = arg->narg.backquote; |
| 1120 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1121 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1122 | case CTLESC: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1123 | p++; |
| 1124 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1125 | break; |
| 1126 | case CTLVAR: |
| 1127 | putc('$', fp); |
| 1128 | putc('{', fp); |
| 1129 | subtype = *++p; |
| 1130 | if (subtype == VSLENGTH) |
| 1131 | putc('#', fp); |
| 1132 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1133 | while (*p != '=') { |
| 1134 | putc(*p, fp); |
| 1135 | p++; |
| 1136 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (subtype & VSNUL) |
| 1139 | putc(':', fp); |
| 1140 | |
| 1141 | switch (subtype & VSTYPE) { |
| 1142 | case VSNORMAL: |
| 1143 | putc('}', fp); |
| 1144 | break; |
| 1145 | case VSMINUS: |
| 1146 | putc('-', fp); |
| 1147 | break; |
| 1148 | case VSPLUS: |
| 1149 | putc('+', fp); |
| 1150 | break; |
| 1151 | case VSQUESTION: |
| 1152 | putc('?', fp); |
| 1153 | break; |
| 1154 | case VSASSIGN: |
| 1155 | putc('=', fp); |
| 1156 | break; |
| 1157 | case VSTRIMLEFT: |
| 1158 | putc('#', fp); |
| 1159 | break; |
| 1160 | case VSTRIMLEFTMAX: |
| 1161 | putc('#', fp); |
| 1162 | putc('#', fp); |
| 1163 | break; |
| 1164 | case VSTRIMRIGHT: |
| 1165 | putc('%', fp); |
| 1166 | break; |
| 1167 | case VSTRIMRIGHTMAX: |
| 1168 | putc('%', fp); |
| 1169 | putc('%', fp); |
| 1170 | break; |
| 1171 | case VSLENGTH: |
| 1172 | break; |
| 1173 | default: |
| 1174 | out1fmt("<subtype %d>", subtype); |
| 1175 | } |
| 1176 | break; |
| 1177 | case CTLENDVAR: |
| 1178 | putc('}', fp); |
| 1179 | break; |
| 1180 | case CTLBACKQ: |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1181 | putc('$', fp); |
| 1182 | putc('(', fp); |
| 1183 | shtree(bqlist->n, -1, NULL, fp); |
| 1184 | putc(')', fp); |
| 1185 | break; |
| 1186 | default: |
| 1187 | putc(*p, fp); |
| 1188 | break; |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1193 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1194 | shcmd(union node *cmd, FILE *fp) |
| 1195 | { |
| 1196 | union node *np; |
| 1197 | int first; |
| 1198 | const char *s; |
| 1199 | int dftfd; |
| 1200 | |
| 1201 | first = 1; |
| 1202 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1203 | if (!first) |
| 1204 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1205 | sharg(np, fp); |
| 1206 | first = 0; |
| 1207 | } |
| 1208 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1209 | if (!first) |
| 1210 | putc(' ', fp); |
| 1211 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1212 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1213 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1214 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1215 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 1216 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1217 | case NTO2: |
| 1218 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1219 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1220 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1221 | case NFROMFD: s = "<&"; break; |
| 1222 | case NFROMTO: s = "<>"; break; |
| 1223 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1224 | } |
| 1225 | if (np->nfile.fd != dftfd) |
| 1226 | fprintf(fp, "%d", np->nfile.fd); |
| 1227 | fputs(s, fp); |
| 1228 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1229 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1230 | } else { |
| 1231 | sharg(np->nfile.fname, fp); |
| 1232 | } |
| 1233 | first = 0; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static void |
| 1238 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1239 | { |
| 1240 | struct nodelist *lp; |
| 1241 | const char *s; |
| 1242 | |
| 1243 | if (n == NULL) |
| 1244 | return; |
| 1245 | |
| 1246 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1247 | |
| 1248 | if (n == NODE_EOF) { |
| 1249 | fputs("<EOF>", fp); |
| 1250 | return; |
| 1251 | } |
| 1252 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1253 | switch (n->type) { |
| 1254 | case NSEMI: |
| 1255 | s = "; "; |
| 1256 | goto binop; |
| 1257 | case NAND: |
| 1258 | s = " && "; |
| 1259 | goto binop; |
| 1260 | case NOR: |
| 1261 | s = " || "; |
| 1262 | binop: |
| 1263 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1264 | /* if (ind < 0) */ |
| 1265 | fputs(s, fp); |
| 1266 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1267 | break; |
| 1268 | case NCMD: |
| 1269 | shcmd(n, fp); |
| 1270 | if (ind >= 0) |
| 1271 | putc('\n', fp); |
| 1272 | break; |
| 1273 | case NPIPE: |
| 1274 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1275 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1276 | if (lp->next) |
| 1277 | fputs(" | ", fp); |
| 1278 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1279 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1280 | fputs(" &", fp); |
| 1281 | if (ind >= 0) |
| 1282 | putc('\n', fp); |
| 1283 | break; |
| 1284 | default: |
| 1285 | fprintf(fp, "<node type %d>", n->type); |
| 1286 | if (ind >= 0) |
| 1287 | putc('\n', fp); |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | static void |
| 1293 | showtree(union node *n) |
| 1294 | { |
| 1295 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1296 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1299 | #endif /* DEBUG */ |
| 1300 | |
| 1301 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1302 | /* ============ Parser data */ |
| 1303 | |
| 1304 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1305 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1306 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1307 | struct strlist { |
| 1308 | struct strlist *next; |
| 1309 | char *text; |
| 1310 | }; |
| 1311 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1312 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1313 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1314 | struct strpush { |
| 1315 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1316 | char *prev_string; |
| 1317 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1318 | #if ENABLE_ASH_ALIAS |
| 1319 | struct alias *ap; /* if push was associated with an alias */ |
| 1320 | #endif |
| 1321 | char *string; /* remember the string since it may change */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 1322 | |
| 1323 | /* Remember last two characters for pungetc. */ |
| 1324 | int lastc[2]; |
| 1325 | |
| 1326 | /* Number of outstanding calls to pungetc. */ |
| 1327 | int unget; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1328 | }; |
| 1329 | |
Denys Vlasenko | 0485b67 | 2017-08-14 19:46:56 +0200 | [diff] [blame] | 1330 | /* |
| 1331 | * The parsefile structure pointed to by the global variable parsefile |
| 1332 | * contains information about the current file being read. |
| 1333 | */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1334 | struct parsefile { |
| 1335 | struct parsefile *prev; /* preceding file on stack */ |
| 1336 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1337 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1338 | int left_in_line; /* number of chars left in this line */ |
| 1339 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1340 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1341 | char *buf; /* input buffer */ |
| 1342 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1343 | struct strpush basestrpush; /* so pushing one is fast */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 1344 | |
| 1345 | /* Remember last two characters for pungetc. */ |
| 1346 | int lastc[2]; |
| 1347 | |
| 1348 | /* Number of outstanding calls to pungetc. */ |
| 1349 | int unget; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1350 | }; |
| 1351 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1352 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1353 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1354 | static char *commandname; /* currently executing command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1355 | |
| 1356 | |
| 1357 | /* ============ Message printing */ |
| 1358 | |
| 1359 | static void |
| 1360 | ash_vmsg(const char *msg, va_list ap) |
| 1361 | { |
| 1362 | fprintf(stderr, "%s: ", arg0); |
| 1363 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1364 | if (strcmp(arg0, commandname)) |
| 1365 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1366 | if (!iflag || g_parsefile->pf_fd > 0) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1367 | fprintf(stderr, "line %d: ", errlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1368 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1369 | vfprintf(stderr, msg, ap); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 1370 | newline_and_flush(stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1371 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* |
| 1374 | * Exverror is called to raise the error exception. If the second argument |
| 1375 | * is not NULL then error prints an error message using printf style |
| 1376 | * formatting. It then raises the error exception. |
| 1377 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1378 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1379 | static void |
| 1380 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1381 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1382 | #if DEBUG |
| 1383 | if (msg) { |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1384 | TRACE(("ash_vmsg_and_raise(%d):", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1385 | TRACEV((msg, ap)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1386 | } else |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1387 | TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1388 | if (msg) |
| 1389 | #endif |
| 1390 | ash_vmsg(msg, ap); |
| 1391 | |
| 1392 | flush_stdout_stderr(); |
| 1393 | raise_exception(cond); |
| 1394 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1395 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1396 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1397 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1398 | static void |
| 1399 | ash_msg_and_raise_error(const char *msg, ...) |
| 1400 | { |
| 1401 | va_list ap; |
| 1402 | |
Ron Yorston | ea7d2f6 | 2017-01-03 11:18:23 +0100 | [diff] [blame] | 1403 | exitstatus = 2; |
| 1404 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1405 | va_start(ap, msg); |
| 1406 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1407 | /* NOTREACHED */ |
| 1408 | va_end(ap); |
| 1409 | } |
| 1410 | |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1411 | /* |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1412 | * 'fmt' must be a string literal. |
| 1413 | */ |
Denys Vlasenko | 6f97b30 | 2017-09-29 18:17:25 +0200 | [diff] [blame] | 1414 | #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": "STRERROR_FMT, ##__VA_ARGS__ STRERROR_ERRNO) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1415 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1416 | static void raise_error_syntax(const char *) NORETURN; |
| 1417 | static void |
| 1418 | raise_error_syntax(const char *msg) |
| 1419 | { |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1420 | errlinno = g_parsefile->linno; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1421 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1422 | /* NOTREACHED */ |
| 1423 | } |
| 1424 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1425 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1426 | static void |
| 1427 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1428 | { |
| 1429 | va_list ap; |
| 1430 | |
| 1431 | va_start(ap, msg); |
| 1432 | ash_vmsg_and_raise(cond, msg, ap); |
| 1433 | /* NOTREACHED */ |
| 1434 | va_end(ap); |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * error/warning routines for external builtins |
| 1439 | */ |
| 1440 | static void |
| 1441 | ash_msg(const char *fmt, ...) |
| 1442 | { |
| 1443 | va_list ap; |
| 1444 | |
| 1445 | va_start(ap, fmt); |
| 1446 | ash_vmsg(fmt, ap); |
| 1447 | va_end(ap); |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Return a string describing an error. The returned string may be a |
| 1452 | * pointer to a static buffer that will be overwritten on the next call. |
| 1453 | * Action describes the operation that got the error. |
| 1454 | */ |
| 1455 | static const char * |
| 1456 | errmsg(int e, const char *em) |
| 1457 | { |
| 1458 | if (e == ENOENT || e == ENOTDIR) { |
| 1459 | return em; |
| 1460 | } |
| 1461 | return strerror(e); |
| 1462 | } |
| 1463 | |
| 1464 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1465 | /* ============ Memory allocation */ |
| 1466 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1467 | #if 0 |
| 1468 | /* I consider these wrappers nearly useless: |
| 1469 | * ok, they return you to nearest exception handler, but |
| 1470 | * how much memory do you leak in the process, making |
| 1471 | * memory starvation worse? |
| 1472 | */ |
| 1473 | static void * |
| 1474 | ckrealloc(void * p, size_t nbytes) |
| 1475 | { |
| 1476 | p = realloc(p, nbytes); |
| 1477 | if (!p) |
| 1478 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1479 | return p; |
| 1480 | } |
| 1481 | |
| 1482 | static void * |
| 1483 | ckmalloc(size_t nbytes) |
| 1484 | { |
| 1485 | return ckrealloc(NULL, nbytes); |
| 1486 | } |
| 1487 | |
| 1488 | static void * |
| 1489 | ckzalloc(size_t nbytes) |
| 1490 | { |
| 1491 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1492 | } |
| 1493 | |
| 1494 | static char * |
| 1495 | ckstrdup(const char *s) |
| 1496 | { |
| 1497 | char *p = strdup(s); |
| 1498 | if (!p) |
| 1499 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1500 | return p; |
| 1501 | } |
| 1502 | #else |
| 1503 | /* Using bbox equivalents. They exit if out of memory */ |
| 1504 | # define ckrealloc xrealloc |
| 1505 | # define ckmalloc xmalloc |
| 1506 | # define ckzalloc xzalloc |
| 1507 | # define ckstrdup xstrdup |
| 1508 | #endif |
| 1509 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1510 | /* |
| 1511 | * It appears that grabstackstr() will barf with such alignments |
| 1512 | * because stalloc() will return a string allocated in a new stackblock. |
| 1513 | */ |
| 1514 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1515 | enum { |
| 1516 | /* Most machines require the value returned from malloc to be aligned |
| 1517 | * in some way. The following macro will get this right |
| 1518 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1519 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1520 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1521 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1522 | }; |
| 1523 | |
| 1524 | struct stack_block { |
| 1525 | struct stack_block *prev; |
| 1526 | char space[MINSIZE]; |
| 1527 | }; |
| 1528 | |
| 1529 | struct stackmark { |
| 1530 | struct stack_block *stackp; |
| 1531 | char *stacknxt; |
| 1532 | size_t stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1533 | }; |
| 1534 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1535 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1536 | struct globals_memstack { |
| 1537 | struct stack_block *g_stackp; // = &stackbase; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1538 | char *g_stacknxt; // = stackbase.space; |
| 1539 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1540 | size_t g_stacknleft; // = MINSIZE; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1541 | struct stack_block stackbase; |
| 1542 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 1543 | extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1544 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1545 | #define g_stackp (G_memstack.g_stackp ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1546 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1547 | #define sstrend (G_memstack.sstrend ) |
| 1548 | #define g_stacknleft (G_memstack.g_stacknleft) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1549 | #define stackbase (G_memstack.stackbase ) |
| 1550 | #define INIT_G_memstack() do { \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 1551 | (*(struct globals_memstack**)not_const_pp(&ash_ptr_to_globals_memstack)) = xzalloc(sizeof(G_memstack)); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1552 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1553 | g_stackp = &stackbase; \ |
| 1554 | g_stacknxt = stackbase.space; \ |
| 1555 | g_stacknleft = MINSIZE; \ |
| 1556 | sstrend = stackbase.space + MINSIZE; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1557 | } while (0) |
| 1558 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1559 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1560 | #define stackblock() ((void *)g_stacknxt) |
| 1561 | #define stackblocksize() g_stacknleft |
| 1562 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1563 | /* |
| 1564 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1565 | * to make this more efficient, and also to avoid all sorts of exception |
| 1566 | * handling code to handle interrupts in the middle of a parse. |
| 1567 | * |
| 1568 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1569 | * well. |
| 1570 | */ |
| 1571 | static void * |
| 1572 | stalloc(size_t nbytes) |
| 1573 | { |
| 1574 | char *p; |
| 1575 | size_t aligned; |
| 1576 | |
| 1577 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1578 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1579 | size_t len; |
| 1580 | size_t blocksize; |
| 1581 | struct stack_block *sp; |
| 1582 | |
| 1583 | blocksize = aligned; |
| 1584 | if (blocksize < MINSIZE) |
| 1585 | blocksize = MINSIZE; |
| 1586 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1587 | if (len < blocksize) |
| 1588 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1589 | INT_OFF; |
| 1590 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1591 | sp->prev = g_stackp; |
| 1592 | g_stacknxt = sp->space; |
| 1593 | g_stacknleft = blocksize; |
| 1594 | sstrend = g_stacknxt + blocksize; |
| 1595 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1596 | INT_ON; |
| 1597 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1598 | p = g_stacknxt; |
| 1599 | g_stacknxt += aligned; |
| 1600 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1601 | return p; |
| 1602 | } |
| 1603 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1604 | static void * |
| 1605 | stzalloc(size_t nbytes) |
| 1606 | { |
| 1607 | return memset(stalloc(nbytes), 0, nbytes); |
| 1608 | } |
| 1609 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1610 | static void |
| 1611 | stunalloc(void *p) |
| 1612 | { |
| 1613 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1614 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1615 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1616 | abort(); |
| 1617 | } |
| 1618 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1619 | g_stacknleft += g_stacknxt - (char *)p; |
| 1620 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | * Like strdup but works with the ash stack. |
| 1625 | */ |
| 1626 | static char * |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 1627 | sstrdup(const char *p) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1628 | { |
| 1629 | size_t len = strlen(p) + 1; |
| 1630 | return memcpy(stalloc(len), p, len); |
| 1631 | } |
| 1632 | |
Denys Vlasenko | 03c36e0 | 2018-01-10 15:18:35 +0100 | [diff] [blame] | 1633 | static ALWAYS_INLINE void |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1634 | grabstackblock(size_t len) |
| 1635 | { |
Denys Vlasenko | a318bba | 2016-10-26 18:26:27 +0200 | [diff] [blame] | 1636 | stalloc(len); |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | static void |
| 1640 | pushstackmark(struct stackmark *mark, size_t len) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1641 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1642 | mark->stackp = g_stackp; |
| 1643 | mark->stacknxt = g_stacknxt; |
| 1644 | mark->stacknleft = g_stacknleft; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1645 | grabstackblock(len); |
| 1646 | } |
| 1647 | |
| 1648 | static void |
| 1649 | setstackmark(struct stackmark *mark) |
| 1650 | { |
| 1651 | pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static void |
| 1655 | popstackmark(struct stackmark *mark) |
| 1656 | { |
| 1657 | struct stack_block *sp; |
| 1658 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1659 | if (!mark->stackp) |
| 1660 | return; |
| 1661 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1662 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1663 | while (g_stackp != mark->stackp) { |
| 1664 | sp = g_stackp; |
| 1665 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1666 | free(sp); |
| 1667 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1668 | g_stacknxt = mark->stacknxt; |
| 1669 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1670 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1671 | INT_ON; |
| 1672 | } |
| 1673 | |
| 1674 | /* |
| 1675 | * When the parser reads in a string, it wants to stick the string on the |
| 1676 | * stack and only adjust the stack pointer when it knows how big the |
| 1677 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1678 | * of space on top of the stack and stackblocklen returns the length of |
| 1679 | * this block. Growstackblock will grow this space by at least one byte, |
| 1680 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1681 | * part of the block that has been used. |
| 1682 | */ |
| 1683 | static void |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1684 | growstackblock(size_t min) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1685 | { |
| 1686 | size_t newlen; |
| 1687 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1688 | newlen = g_stacknleft * 2; |
| 1689 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1690 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1691 | min = SHELL_ALIGN(min | 128); |
| 1692 | if (newlen < min) |
| 1693 | newlen += min; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1694 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1695 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1696 | struct stack_block *sp; |
| 1697 | struct stack_block *prevstackp; |
| 1698 | size_t grosslen; |
| 1699 | |
| 1700 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1701 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1702 | prevstackp = sp->prev; |
| 1703 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1704 | sp = ckrealloc(sp, grosslen); |
| 1705 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1706 | g_stackp = sp; |
| 1707 | g_stacknxt = sp->space; |
| 1708 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1709 | sstrend = sp->space + newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1710 | INT_ON; |
| 1711 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1712 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1713 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1714 | char *p = stalloc(newlen); |
| 1715 | |
| 1716 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1717 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1718 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1722 | /* |
| 1723 | * The following routines are somewhat easier to use than the above. |
| 1724 | * The user declares a variable of type STACKSTR, which may be declared |
| 1725 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1726 | * the user uses the macro STPUTC to add characters to the string. In |
| 1727 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1728 | * grown as necessary. When the user is done, she can just leave the |
| 1729 | * string there and refer to it using stackblock(). Or she can allocate |
| 1730 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1731 | * someone else to use the stack temporarily and then continue to grow |
| 1732 | * the string, the user should use grabstack to allocate the space, and |
| 1733 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1734 | * |
| 1735 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1736 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1737 | * is space for at least one character. |
| 1738 | */ |
| 1739 | static void * |
| 1740 | growstackstr(void) |
| 1741 | { |
| 1742 | size_t len = stackblocksize(); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1743 | growstackblock(0); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1744 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1747 | static char * |
| 1748 | growstackto(size_t len) |
| 1749 | { |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1750 | if (stackblocksize() < len) |
| 1751 | growstackblock(len); |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1752 | return stackblock(); |
| 1753 | } |
| 1754 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1755 | /* |
| 1756 | * Called from CHECKSTRSPACE. |
| 1757 | */ |
| 1758 | static char * |
| 1759 | makestrspace(size_t newlen, char *p) |
| 1760 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1761 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1762 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1763 | return growstackto(len + newlen) + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | static char * |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1767 | stnputs(const char *s, size_t n, char *p) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1768 | { |
| 1769 | p = makestrspace(n, p); |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1770 | p = (char *)mempcpy(p, s, n); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1771 | return p; |
| 1772 | } |
| 1773 | |
| 1774 | static char * |
| 1775 | stack_putstr(const char *s, char *p) |
| 1776 | { |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1777 | return stnputs(s, strlen(s), p); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1780 | static char * |
| 1781 | _STPUTC(int c, char *p) |
| 1782 | { |
| 1783 | if (p == sstrend) |
| 1784 | p = growstackstr(); |
| 1785 | *p++ = c; |
| 1786 | return p; |
| 1787 | } |
| 1788 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1789 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1790 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1791 | #define CHECKSTRSPACE(n, p) do { \ |
| 1792 | char *q = (p); \ |
| 1793 | size_t l = (n); \ |
| 1794 | size_t m = sstrend - q; \ |
| 1795 | if (l > m) \ |
| 1796 | (p) = makestrspace(l, q); \ |
| 1797 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1798 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1799 | #define STACKSTRNUL(p) do { \ |
| 1800 | if ((p) == sstrend) \ |
| 1801 | (p) = growstackstr(); \ |
| 1802 | *(p) = '\0'; \ |
| 1803 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1804 | #define STUNPUTC(p) (--(p)) |
| 1805 | #define STTOPC(p) ((p)[-1]) |
| 1806 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1807 | |
| 1808 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1809 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1810 | #define stackstrend() ((void *)sstrend) |
| 1811 | |
| 1812 | |
| 1813 | /* ============ String helpers */ |
| 1814 | |
| 1815 | /* |
| 1816 | * prefix -- see if pfx is a prefix of string. |
| 1817 | */ |
| 1818 | static char * |
| 1819 | prefix(const char *string, const char *pfx) |
| 1820 | { |
| 1821 | while (*pfx) { |
| 1822 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1823 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1824 | } |
| 1825 | return (char *) string; |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * Check for a valid number. This should be elsewhere. |
| 1830 | */ |
| 1831 | static int |
| 1832 | is_number(const char *p) |
| 1833 | { |
| 1834 | do { |
| 1835 | if (!isdigit(*p)) |
| 1836 | return 0; |
| 1837 | } while (*++p != '\0'); |
| 1838 | return 1; |
| 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * Convert a string of digits to an integer, printing an error message on |
| 1843 | * failure. |
| 1844 | */ |
| 1845 | static int |
| 1846 | number(const char *s) |
| 1847 | { |
| 1848 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1849 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1850 | return atoi(s); |
| 1851 | } |
| 1852 | |
| 1853 | /* |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1854 | * Produce a single quoted string suitable as input to the shell. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1855 | * The return string is allocated on the stack. |
| 1856 | */ |
| 1857 | static char * |
| 1858 | single_quote(const char *s) |
| 1859 | { |
| 1860 | char *p; |
| 1861 | |
| 1862 | STARTSTACKSTR(p); |
| 1863 | |
| 1864 | do { |
| 1865 | char *q; |
| 1866 | size_t len; |
| 1867 | |
| 1868 | len = strchrnul(s, '\'') - s; |
| 1869 | |
| 1870 | q = p = makestrspace(len + 3, p); |
| 1871 | |
| 1872 | *q++ = '\''; |
Denys Vlasenko | 94af83e | 2017-07-23 21:55:40 +0200 | [diff] [blame] | 1873 | q = (char *)mempcpy(q, s, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1874 | *q++ = '\''; |
| 1875 | s += len; |
| 1876 | |
| 1877 | STADJUST(q - p, p); |
| 1878 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1879 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1880 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1881 | len = 0; |
| 1882 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1883 | |
| 1884 | q = p = makestrspace(len + 3, p); |
| 1885 | |
| 1886 | *q++ = '"'; |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1887 | q = (char *)mempcpy(q, s - len, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1888 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1889 | |
| 1890 | STADJUST(q - p, p); |
| 1891 | } while (*s); |
| 1892 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1893 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1894 | |
| 1895 | return stackblock(); |
| 1896 | } |
| 1897 | |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1898 | /* |
| 1899 | * Produce a possibly single quoted string suitable as input to the shell. |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1900 | * If quoting was done, the return string is allocated on the stack, |
| 1901 | * otherwise a pointer to the original string is returned. |
| 1902 | */ |
| 1903 | static const char * |
| 1904 | maybe_single_quote(const char *s) |
| 1905 | { |
| 1906 | const char *p = s; |
| 1907 | |
| 1908 | while (*p) { |
| 1909 | /* Assuming ACSII */ |
| 1910 | /* quote ctrl_chars space !"#$%&'()* */ |
| 1911 | if (*p < '+') |
| 1912 | goto need_quoting; |
| 1913 | /* quote ;<=>? */ |
| 1914 | if (*p >= ';' && *p <= '?') |
| 1915 | goto need_quoting; |
| 1916 | /* quote `[\ */ |
| 1917 | if (*p == '`') |
| 1918 | goto need_quoting; |
| 1919 | if (*p == '[') |
| 1920 | goto need_quoting; |
| 1921 | if (*p == '\\') |
| 1922 | goto need_quoting; |
| 1923 | /* quote {|}~ DEL and high bytes */ |
| 1924 | if (*p > 'z') |
| 1925 | goto need_quoting; |
| 1926 | /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */ |
| 1927 | /* TODO: maybe avoid quoting % */ |
| 1928 | p++; |
| 1929 | } |
| 1930 | return s; |
| 1931 | |
| 1932 | need_quoting: |
| 1933 | return single_quote(s); |
| 1934 | } |
| 1935 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1936 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1937 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1938 | |
| 1939 | static char **argptr; /* argument list for builtin commands */ |
| 1940 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1941 | static char *optptr; /* used by nextopt */ |
| 1942 | |
| 1943 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1944 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1945 | * The library getopt must have the BSD extension static variable |
| 1946 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1947 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1948 | * Standard option processing (a la getopt) for builtin routines. |
| 1949 | * The only argument that is passed to nextopt is the option string; |
| 1950 | * the other arguments are unnecessary. It returns the character, |
| 1951 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1952 | */ |
| 1953 | static int |
| 1954 | nextopt(const char *optstring) |
| 1955 | { |
| 1956 | char *p; |
| 1957 | const char *q; |
| 1958 | char c; |
| 1959 | |
| 1960 | p = optptr; |
| 1961 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1962 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1963 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1964 | if (p == NULL) |
| 1965 | return '\0'; |
| 1966 | if (*p != '-') |
| 1967 | return '\0'; |
| 1968 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1969 | return '\0'; |
| 1970 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1971 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1972 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1973 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1974 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1975 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1976 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1977 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1978 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1979 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1980 | if (*++q == ':') |
| 1981 | q++; |
| 1982 | } |
| 1983 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1984 | if (*p == '\0') { |
| 1985 | p = *argptr++; |
| 1986 | if (p == NULL) |
| 1987 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1988 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1989 | optionarg = p; |
| 1990 | p = NULL; |
| 1991 | } |
| 1992 | optptr = p; |
| 1993 | return c; |
| 1994 | } |
| 1995 | |
| 1996 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1997 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1998 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1999 | struct shparam { |
| 2000 | int nparam; /* # of positional parameters (without $0) */ |
| 2001 | #if ENABLE_ASH_GETOPTS |
| 2002 | int optind; /* next parameter to be processed by getopts */ |
| 2003 | int optoff; /* used by getopts */ |
| 2004 | #endif |
| 2005 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 2006 | char **p; /* parameter list */ |
| 2007 | }; |
| 2008 | |
| 2009 | /* |
| 2010 | * Free the list of positional parameters. |
| 2011 | */ |
| 2012 | static void |
| 2013 | freeparam(volatile struct shparam *param) |
| 2014 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2015 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 2016 | char **ap, **ap1; |
| 2017 | ap = ap1 = param->p; |
| 2018 | while (*ap) |
| 2019 | free(*ap++); |
| 2020 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2025 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2026 | #endif |
| 2027 | |
| 2028 | struct var { |
| 2029 | struct var *next; /* next entry in hash list */ |
| 2030 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2031 | const char *var_text; /* name=value */ |
| 2032 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2033 | /* the variable gets set/unset */ |
| 2034 | }; |
| 2035 | |
| 2036 | struct localvar { |
| 2037 | struct localvar *next; /* next local variable in list */ |
| 2038 | struct var *vp; /* the variable that was made local */ |
| 2039 | int flags; /* saved flags */ |
| 2040 | const char *text; /* saved text */ |
| 2041 | }; |
| 2042 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2043 | /* flags */ |
| 2044 | #define VEXPORT 0x01 /* variable is exported */ |
| 2045 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 2046 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 2047 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 2048 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 2049 | #define VUNSET 0x20 /* the variable is not set */ |
| 2050 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 2051 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 2052 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2053 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2054 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 2055 | #else |
| 2056 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2057 | #endif |
| 2058 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2059 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2060 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2061 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2062 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2063 | change_lc_all(const char *value) |
| 2064 | { |
| 2065 | if (value && *value != '\0') |
| 2066 | setlocale(LC_ALL, value); |
| 2067 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2068 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2069 | change_lc_ctype(const char *value) |
| 2070 | { |
| 2071 | if (value && *value != '\0') |
| 2072 | setlocale(LC_CTYPE, value); |
| 2073 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2074 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2075 | #if ENABLE_ASH_MAIL |
| 2076 | static void chkmail(void); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 2077 | static void changemail(const char *var_value) FAST_FUNC; |
| 2078 | #else |
| 2079 | # define chkmail() ((void)0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2080 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2081 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2082 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2083 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2084 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2085 | #if BASH_EPOCH_VARS |
| 2086 | static void change_seconds(const char *) FAST_FUNC; |
| 2087 | static void change_realtime(const char *) FAST_FUNC; |
| 2088 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2089 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2090 | static const struct { |
| 2091 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2092 | const char *var_text; |
| 2093 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2094 | } varinit_data[] = { |
Denys Vlasenko | 566a313 | 2012-07-07 21:40:35 +0200 | [diff] [blame] | 2095 | /* |
| 2096 | * Note: VEXPORT would not work correctly here for NOFORK applets: |
| 2097 | * some environment strings may be constant. |
| 2098 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2099 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2100 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2101 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 2102 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2103 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2104 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 2105 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 2106 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 2107 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2108 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 2109 | { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2110 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2111 | { VSTRFIXED|VTEXTFIXED , NULL /* inited to linenovar */, NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2112 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2113 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2114 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2115 | #if BASH_EPOCH_VARS |
| 2116 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHSECONDS", change_seconds }, |
| 2117 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHREALTIME", change_realtime }, |
| 2118 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2119 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2120 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 2121 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2122 | #endif |
| 2123 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2124 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2125 | #endif |
| 2126 | }; |
| 2127 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2128 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2129 | |
| 2130 | struct globals_var { |
| 2131 | struct shparam shellparam; /* $@ current positional parameters */ |
| 2132 | struct redirtab *redirlist; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 2133 | int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2134 | struct var *vartab[VTABSIZE]; |
| 2135 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2136 | int lineno; |
| 2137 | char linenovar[sizeof("LINENO=") + sizeof(int)*3]; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2138 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 2139 | extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 2140 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2141 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2142 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2143 | #define preverrout_fd (G_var.preverrout_fd) |
| 2144 | #define vartab (G_var.vartab ) |
| 2145 | #define varinit (G_var.varinit ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2146 | #define lineno (G_var.lineno ) |
| 2147 | #define linenovar (G_var.linenovar ) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2148 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2149 | #if ENABLE_ASH_MAIL |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2150 | # define vmail varinit[1] |
| 2151 | # define vmpath varinit[2] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2152 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2153 | #define VAR_OFFSET1 (ENABLE_ASH_MAIL*2) |
| 2154 | #define vpath varinit[VAR_OFFSET1 + 1] |
| 2155 | #define vps1 varinit[VAR_OFFSET1 + 2] |
| 2156 | #define vps2 varinit[VAR_OFFSET1 + 3] |
| 2157 | #define vps4 varinit[VAR_OFFSET1 + 4] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2158 | #if ENABLE_ASH_GETOPTS |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2159 | # define voptind varinit[VAR_OFFSET1 + 5] |
| 2160 | #endif |
| 2161 | #define VAR_OFFSET2 (VAR_OFFSET1 + ENABLE_ASH_GETOPTS) |
| 2162 | #define vlineno varinit[VAR_OFFSET2 + 5] |
| 2163 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 2164 | # define vrandom varinit[VAR_OFFSET2 + 6] |
| 2165 | #endif |
| 2166 | #define VAR_OFFSET3 (VAR_OFFSET2 + ENABLE_ASH_RANDOM_SUPPORT) |
| 2167 | #if BASH_EPOCH_VARS |
| 2168 | # define vepochs varinit[VAR_OFFSET3 + 6] |
| 2169 | # define vepochr varinit[VAR_OFFSET3 + 7] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2170 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2171 | #define INIT_G_var() do { \ |
| 2172 | unsigned i; \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 2173 | (*(struct globals_var**)not_const_pp(&ash_ptr_to_globals_var)) = xzalloc(sizeof(G_var)); \ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2174 | barrier(); \ |
| 2175 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 2176 | varinit[i].flags = varinit_data[i].flags; \ |
| 2177 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 2178 | varinit[i].var_func = varinit_data[i].var_func; \ |
| 2179 | } \ |
| 2180 | strcpy(linenovar, "LINENO="); \ |
| 2181 | vlineno.var_text = linenovar; \ |
| 2182 | } while (0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2183 | |
| 2184 | /* |
| 2185 | * The following macros access the values of the above variables. |
| 2186 | * They have to skip over the name. They return the null string |
| 2187 | * for unset variables. |
| 2188 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2189 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2190 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2191 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2192 | # define mailval() (vmail.var_text + 5) |
| 2193 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2194 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 2195 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2196 | #define pathval() (vpath.var_text + 5) |
| 2197 | #define ps1val() (vps1.var_text + 4) |
| 2198 | #define ps2val() (vps2.var_text + 4) |
| 2199 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2200 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2201 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2202 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2203 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2204 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2205 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2206 | getoptsreset(const char *value) |
| 2207 | { |
Denys Vlasenko | 4628945 | 2017-08-11 00:59:36 +0200 | [diff] [blame] | 2208 | shellparam.optind = 1; |
| 2209 | if (is_number(value)) |
| 2210 | shellparam.optind = number(value) ?: 1; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2211 | shellparam.optoff = -1; |
| 2212 | } |
| 2213 | #endif |
| 2214 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2215 | /* |
| 2216 | * Compares two strings up to the first = or '\0'. The first |
| 2217 | * string must be terminated by '='; the second may be terminated by |
| 2218 | * either '=' or '\0'. |
| 2219 | */ |
| 2220 | static int |
| 2221 | varcmp(const char *p, const char *q) |
| 2222 | { |
| 2223 | int c, d; |
| 2224 | |
| 2225 | while ((c = *p) == (d = *q)) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2226 | if (c == '\0' || c == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2227 | goto out; |
| 2228 | p++; |
| 2229 | q++; |
| 2230 | } |
| 2231 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2232 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2233 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2234 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2235 | out: |
| 2236 | return c - d; |
| 2237 | } |
| 2238 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2239 | /* |
| 2240 | * Find the appropriate entry in the hash table from the name. |
| 2241 | */ |
| 2242 | static struct var ** |
| 2243 | hashvar(const char *p) |
| 2244 | { |
| 2245 | unsigned hashval; |
| 2246 | |
| 2247 | hashval = ((unsigned char) *p) << 4; |
| 2248 | while (*p && *p != '=') |
| 2249 | hashval += (unsigned char) *p++; |
| 2250 | return &vartab[hashval % VTABSIZE]; |
| 2251 | } |
| 2252 | |
| 2253 | static int |
| 2254 | vpcmp(const void *a, const void *b) |
| 2255 | { |
| 2256 | return varcmp(*(const char **)a, *(const char **)b); |
| 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * This routine initializes the builtin variables. |
| 2261 | */ |
| 2262 | static void |
| 2263 | initvar(void) |
| 2264 | { |
| 2265 | struct var *vp; |
| 2266 | struct var *end; |
| 2267 | struct var **vpp; |
| 2268 | |
| 2269 | /* |
| 2270 | * PS1 depends on uid |
| 2271 | */ |
| 2272 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2273 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2274 | #else |
| 2275 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2276 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2277 | #endif |
| 2278 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2279 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2280 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2281 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2282 | vp->next = *vpp; |
| 2283 | *vpp = vp; |
| 2284 | } while (++vp < end); |
| 2285 | } |
| 2286 | |
| 2287 | static struct var ** |
| 2288 | findvar(struct var **vpp, const char *name) |
| 2289 | { |
| 2290 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2291 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | } |
| 2294 | } |
| 2295 | return vpp; |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Find the value of a variable. Returns NULL if not set. |
| 2300 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2301 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2302 | lookupvar(const char *name) |
| 2303 | { |
| 2304 | struct var *v; |
| 2305 | |
| 2306 | v = *findvar(hashvar(name), name); |
| 2307 | if (v) { |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2308 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2309 | /* |
| 2310 | * Dynamic variables are implemented roughly the same way they are |
| 2311 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2312 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2313 | * lookup will no longer happen at that point. -- PFM. |
| 2314 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2315 | if (v->flags & VDYNAMIC) |
| 2316 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2317 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2318 | if (!(v->flags & VUNSET)) { |
| 2319 | if (v == &vlineno && v->var_text == linenovar) { |
| 2320 | fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno); |
| 2321 | } |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2322 | return var_end(v->var_text); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2323 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2324 | } |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2328 | #if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 2329 | static void |
| 2330 | reinit_unicode_for_ash(void) |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2331 | { |
| 2332 | /* Unicode support should be activated even if LANG is set |
| 2333 | * _during_ shell execution, not only if it was set when |
| 2334 | * shell was started. Therefore, re-check LANG every time: |
| 2335 | */ |
| 2336 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2337 | || ENABLE_UNICODE_USING_LOCALE |
| 2338 | ) { |
| 2339 | const char *s = lookupvar("LC_ALL"); |
| 2340 | if (!s) s = lookupvar("LC_CTYPE"); |
| 2341 | if (!s) s = lookupvar("LANG"); |
| 2342 | reinit_unicode(s); |
| 2343 | } |
| 2344 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2345 | #else |
| 2346 | # define reinit_unicode_for_ash() ((void)0) |
| 2347 | #endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2348 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2349 | /* |
| 2350 | * Search the environment of a builtin command. |
| 2351 | */ |
Denys Vlasenko | 488e609 | 2017-07-26 23:08:36 +0200 | [diff] [blame] | 2352 | static ALWAYS_INLINE const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2353 | bltinlookup(const char *name) |
| 2354 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2355 | return lookupvar(name); |
| 2356 | } |
| 2357 | |
| 2358 | /* |
| 2359 | * Same as setvar except that the variable and value are passed in |
| 2360 | * the first argument as name=value. Since the first argument will |
| 2361 | * be actually stored in the table, it should not be a string that |
| 2362 | * will go away. |
| 2363 | * Called with interrupts off. |
| 2364 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2365 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2366 | setvareq(char *s, int flags) |
| 2367 | { |
| 2368 | struct var *vp, **vpp; |
| 2369 | |
| 2370 | vpp = hashvar(s); |
| 2371 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2372 | vpp = findvar(vpp, s); |
| 2373 | vp = *vpp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2374 | if (vp) { |
| 2375 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2376 | const char *n; |
| 2377 | |
| 2378 | if (flags & VNOSAVE) |
| 2379 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2380 | n = vp->var_text; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 2381 | exitstatus = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2382 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2383 | } |
| 2384 | |
| 2385 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2386 | goto out; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2387 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2388 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2389 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2390 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2391 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2392 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2393 | |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2394 | if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) { |
| 2395 | *vpp = vp->next; |
| 2396 | free(vp); |
| 2397 | out_free: |
| 2398 | if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE) |
| 2399 | free(s); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2400 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2403 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2404 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Ron Yorston | d96c69d | 2019-04-15 10:49:35 +0100 | [diff] [blame] | 2405 | if (flags & VUNSET) |
| 2406 | flags &= ~VDYNAMIC; |
| 2407 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2408 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2409 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2410 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2411 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2412 | if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 2413 | goto out_free; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2414 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2415 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2416 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2417 | *vpp = vp; |
| 2418 | } |
| 2419 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2420 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2421 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2422 | vp->flags = flags; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2423 | |
| 2424 | out: |
| 2425 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * Set the value of a variable. The flags argument is ored with the |
| 2430 | * flags of the variable. If val is NULL, the variable is unset. |
| 2431 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2432 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2433 | setvar(const char *name, const char *val, int flags) |
| 2434 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2435 | const char *q; |
| 2436 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2437 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2438 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2439 | size_t vallen; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2440 | struct var *vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2441 | |
| 2442 | q = endofname(name); |
| 2443 | p = strchrnul(q, '='); |
| 2444 | namelen = p - name; |
| 2445 | if (!namelen || p != q) |
| 2446 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2447 | vallen = 0; |
| 2448 | if (val == NULL) { |
| 2449 | flags |= VUNSET; |
| 2450 | } else { |
| 2451 | vallen = strlen(val); |
| 2452 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2453 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2454 | INT_OFF; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2455 | nameeq = ckzalloc(namelen + vallen + 2); |
Denys Vlasenko | da2244f | 2017-07-21 18:51:29 +0200 | [diff] [blame] | 2456 | p = mempcpy(nameeq, name, namelen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2457 | if (val) { |
| 2458 | *p++ = '='; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2459 | memcpy(p, val, vallen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2460 | } |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2461 | vp = setvareq(nameeq, flags | VNOSAVE); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2462 | INT_ON; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2463 | |
| 2464 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2467 | static void FAST_FUNC |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2468 | setvar0(const char *name, const char *val) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2469 | { |
| 2470 | setvar(name, val, 0); |
| 2471 | } |
| 2472 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2473 | /* |
| 2474 | * Unset the specified variable. |
| 2475 | */ |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2476 | static void |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2477 | unsetvar(const char *s) |
| 2478 | { |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 2479 | setvar(s, NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2483 | * Generate a list of variables satisfying the given conditions. |
| 2484 | */ |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2485 | #if !ENABLE_FEATURE_SH_NOFORK |
| 2486 | # define listvars(on, off, lp, end) listvars(on, off, end) |
| 2487 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2488 | static char ** |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2489 | listvars(int on, int off, struct strlist *lp, char ***end) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2490 | { |
| 2491 | struct var **vpp; |
| 2492 | struct var *vp; |
| 2493 | char **ep; |
| 2494 | int mask; |
| 2495 | |
| 2496 | STARTSTACKSTR(ep); |
| 2497 | vpp = vartab; |
| 2498 | mask = on | off; |
| 2499 | do { |
| 2500 | for (vp = *vpp; vp; vp = vp->next) { |
| 2501 | if ((vp->flags & mask) == on) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2502 | #if ENABLE_FEATURE_SH_NOFORK |
| 2503 | /* If variable with the same name is both |
| 2504 | * exported and temporarily set for a command: |
| 2505 | * export ZVAR=5 |
| 2506 | * ZVAR=6 printenv |
| 2507 | * then "ZVAR=6" will be both in vartab and |
| 2508 | * lp lists. Do not pass it twice to printenv. |
| 2509 | */ |
| 2510 | struct strlist *lp1 = lp; |
| 2511 | while (lp1) { |
| 2512 | if (strcmp(lp1->text, vp->var_text) == 0) |
| 2513 | goto skip; |
| 2514 | lp1 = lp1->next; |
| 2515 | } |
| 2516 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2517 | if (ep == stackstrend()) |
| 2518 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2519 | *ep++ = (char*)vp->var_text; |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2520 | #if ENABLE_FEATURE_SH_NOFORK |
| 2521 | skip: ; |
| 2522 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | } while (++vpp < vartab + VTABSIZE); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2526 | |
| 2527 | #if ENABLE_FEATURE_SH_NOFORK |
| 2528 | while (lp) { |
| 2529 | if (ep == stackstrend()) |
| 2530 | ep = growstackstr(); |
| 2531 | *ep++ = lp->text; |
| 2532 | lp = lp->next; |
| 2533 | } |
| 2534 | #endif |
| 2535 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2536 | if (ep == stackstrend()) |
| 2537 | ep = growstackstr(); |
| 2538 | if (end) |
| 2539 | *end = ep; |
| 2540 | *ep++ = NULL; |
| 2541 | return grabstackstr(ep); |
| 2542 | } |
| 2543 | |
| 2544 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2545 | /* ============ Path search helper */ |
| 2546 | static const char * |
| 2547 | legal_pathopt(const char *opt, const char *term, int magic) |
| 2548 | { |
| 2549 | switch (magic) { |
| 2550 | case 0: |
| 2551 | opt = NULL; |
| 2552 | break; |
| 2553 | |
| 2554 | case 1: |
| 2555 | opt = prefix(opt, "builtin") ?: prefix(opt, "func"); |
| 2556 | break; |
| 2557 | |
| 2558 | default: |
| 2559 | opt += strcspn(opt, term); |
| 2560 | break; |
| 2561 | } |
| 2562 | |
| 2563 | if (opt && *opt == '%') |
| 2564 | opt++; |
| 2565 | |
| 2566 | return opt; |
| 2567 | } |
| 2568 | |
| 2569 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2570 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2571 | * of the path before the first call; padvance will update |
| 2572 | * this value as it proceeds. Successive calls to padvance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2573 | * the possible path expansions in sequence. If an option (indicated by |
| 2574 | * a percent sign) appears in the path entry then the global variable |
| 2575 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2576 | * NULL. |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2577 | * |
| 2578 | * If magic is 0 then pathopt recognition will be disabled. If magic is |
| 2579 | * 1 we shall recognise %builtin/%func. Otherwise we shall accept any |
| 2580 | * pathopt. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2581 | */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2582 | static const char *pathopt; /* set by padvance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2583 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2584 | static int |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2585 | padvance_magic(const char **path, const char *name, int magic) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2586 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2587 | const char *term = "%:"; |
| 2588 | const char *lpathopt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2589 | const char *p; |
| 2590 | char *q; |
| 2591 | const char *start; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2592 | size_t qlen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2593 | size_t len; |
| 2594 | |
| 2595 | if (*path == NULL) |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2596 | return -1; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2597 | |
| 2598 | lpathopt = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2599 | start = *path; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2600 | |
| 2601 | if (*start == '%' && (p = legal_pathopt(start + 1, term, magic))) { |
| 2602 | lpathopt = start + 1; |
| 2603 | start = p; |
| 2604 | term = ":"; |
| 2605 | } |
| 2606 | |
| 2607 | len = strcspn(start, term); |
| 2608 | p = start + len; |
| 2609 | |
| 2610 | if (*p == '%') { |
| 2611 | size_t extra = strchrnul(p, ':') - p; |
| 2612 | |
| 2613 | if (legal_pathopt(p + 1, term, magic)) |
| 2614 | lpathopt = p + 1; |
| 2615 | else |
| 2616 | len += extra; |
| 2617 | |
| 2618 | p += extra; |
| 2619 | } |
| 2620 | |
| 2621 | pathopt = lpathopt; |
| 2622 | *path = *p == ':' ? p + 1 : NULL; |
| 2623 | |
| 2624 | /* "2" is for '/' and '\0' */ |
| 2625 | qlen = len + strlen(name) + 2; |
| 2626 | q = growstackto(qlen); |
| 2627 | |
| 2628 | if (len) { |
| 2629 | q = mempcpy(q, start, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2630 | *q++ = '/'; |
| 2631 | } |
| 2632 | strcpy(q, name); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2633 | |
| 2634 | return qlen; |
| 2635 | } |
| 2636 | |
| 2637 | static int |
| 2638 | padvance(const char **path, const char *name) |
| 2639 | { |
| 2640 | return padvance_magic(path, name, 1); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | |
| 2644 | /* ============ Prompt */ |
| 2645 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2646 | static smallint doprompt; /* if set, prompt the user */ |
| 2647 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2648 | |
| 2649 | #if ENABLE_FEATURE_EDITING |
| 2650 | static line_input_t *line_input_state; |
| 2651 | static const char *cmdedit_prompt; |
| 2652 | static void |
| 2653 | putprompt(const char *s) |
| 2654 | { |
| 2655 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2656 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2657 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2658 | return; |
| 2659 | } |
| 2660 | cmdedit_prompt = s; |
| 2661 | } |
| 2662 | #else |
| 2663 | static void |
| 2664 | putprompt(const char *s) |
| 2665 | { |
| 2666 | out2str(s); |
| 2667 | } |
| 2668 | #endif |
| 2669 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2670 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2671 | static const char *expandstr(const char *ps, int syntax_type); |
| 2672 | /* Values for syntax param */ |
| 2673 | #define BASESYNTAX 0 /* not in quotes */ |
| 2674 | #define DQSYNTAX 1 /* in double quotes */ |
| 2675 | #define SQSYNTAX 2 /* in single quotes */ |
| 2676 | #define ARISYNTAX 3 /* in arithmetic */ |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 2677 | #if ENABLE_ASH_EXPAND_PRMT |
| 2678 | # define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
| 2679 | #endif |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2680 | /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2681 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2682 | /* |
| 2683 | * called by editline -- any expansions to the prompt should be added here. |
| 2684 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2685 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2686 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2687 | { |
| 2688 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2689 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2690 | |
| 2691 | if (!do_set) |
| 2692 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2693 | |
| 2694 | needprompt = 0; |
| 2695 | |
| 2696 | switch (whichprompt) { |
| 2697 | case 1: |
| 2698 | prompt = ps1val(); |
| 2699 | break; |
| 2700 | case 2: |
| 2701 | prompt = ps2val(); |
| 2702 | break; |
| 2703 | default: /* 0 */ |
| 2704 | prompt = nullstr; |
| 2705 | } |
| 2706 | #if ENABLE_ASH_EXPAND_PRMT |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 2707 | pushstackmark(&smark, stackblocksize()); |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2708 | putprompt(expandstr(prompt, PSSYNTAX)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2709 | popstackmark(&smark); |
Denys Vlasenko | 48c803a | 2017-07-01 23:24:48 +0200 | [diff] [blame] | 2710 | #else |
| 2711 | putprompt(prompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2712 | #endif |
| 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | /* ============ The cd and pwd commands */ |
| 2717 | |
| 2718 | #define CD_PHYSICAL 1 |
| 2719 | #define CD_PRINT 2 |
| 2720 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2721 | static int |
| 2722 | cdopt(void) |
| 2723 | { |
| 2724 | int flags = 0; |
| 2725 | int i, j; |
| 2726 | |
| 2727 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2728 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2729 | if (i != j) { |
| 2730 | flags ^= CD_PHYSICAL; |
| 2731 | j = i; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | return flags; |
| 2736 | } |
| 2737 | |
| 2738 | /* |
| 2739 | * Update curdir (the name of the current directory) in response to a |
| 2740 | * cd command. |
| 2741 | */ |
| 2742 | static const char * |
| 2743 | updatepwd(const char *dir) |
| 2744 | { |
| 2745 | char *new; |
| 2746 | char *p; |
| 2747 | char *cdcomppath; |
| 2748 | const char *lim; |
| 2749 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 2750 | cdcomppath = sstrdup(dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2751 | STARTSTACKSTR(new); |
| 2752 | if (*dir != '/') { |
| 2753 | if (curdir == nullstr) |
| 2754 | return 0; |
| 2755 | new = stack_putstr(curdir, new); |
| 2756 | } |
| 2757 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2758 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2759 | if (*dir != '/') { |
| 2760 | if (new[-1] != '/') |
| 2761 | USTPUTC('/', new); |
| 2762 | if (new > lim && *lim == '/') |
| 2763 | lim++; |
| 2764 | } else { |
| 2765 | USTPUTC('/', new); |
| 2766 | cdcomppath++; |
| 2767 | if (dir[1] == '/' && dir[2] != '/') { |
| 2768 | USTPUTC('/', new); |
| 2769 | cdcomppath++; |
| 2770 | lim++; |
| 2771 | } |
| 2772 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2773 | p = strtok_r(cdcomppath, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2774 | while (p) { |
| 2775 | switch (*p) { |
| 2776 | case '.': |
| 2777 | if (p[1] == '.' && p[2] == '\0') { |
| 2778 | while (new > lim) { |
| 2779 | STUNPUTC(new); |
| 2780 | if (new[-1] == '/') |
| 2781 | break; |
| 2782 | } |
| 2783 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2784 | } |
| 2785 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2786 | break; |
| 2787 | /* fall through */ |
| 2788 | default: |
| 2789 | new = stack_putstr(p, new); |
| 2790 | USTPUTC('/', new); |
| 2791 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2792 | p = strtok_r(NULL, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2793 | } |
| 2794 | if (new > lim) |
| 2795 | STUNPUTC(new); |
| 2796 | *new = 0; |
| 2797 | return stackblock(); |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * Find out what the current directory is. If we already know the current |
| 2802 | * directory, this routine returns immediately. |
| 2803 | */ |
| 2804 | static char * |
| 2805 | getpwd(void) |
| 2806 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2807 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2808 | return dir ? dir : nullstr; |
| 2809 | } |
| 2810 | |
| 2811 | static void |
| 2812 | setpwd(const char *val, int setold) |
| 2813 | { |
| 2814 | char *oldcur, *dir; |
| 2815 | |
| 2816 | oldcur = dir = curdir; |
| 2817 | |
| 2818 | if (setold) { |
| 2819 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2820 | } |
| 2821 | INT_OFF; |
| 2822 | if (physdir != nullstr) { |
| 2823 | if (physdir != oldcur) |
| 2824 | free(physdir); |
| 2825 | physdir = nullstr; |
| 2826 | } |
| 2827 | if (oldcur == val || !val) { |
| 2828 | char *s = getpwd(); |
| 2829 | physdir = s; |
| 2830 | if (!val) |
| 2831 | dir = s; |
| 2832 | } else |
| 2833 | dir = ckstrdup(val); |
| 2834 | if (oldcur != dir && oldcur != nullstr) { |
| 2835 | free(oldcur); |
| 2836 | } |
| 2837 | curdir = dir; |
| 2838 | INT_ON; |
| 2839 | setvar("PWD", dir, VEXPORT); |
| 2840 | } |
| 2841 | |
| 2842 | static void hashcd(void); |
| 2843 | |
| 2844 | /* |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 2845 | * Actually do the chdir. We also call hashcd to let other routines |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2846 | * know that the current directory has changed. |
| 2847 | */ |
| 2848 | static int |
| 2849 | docd(const char *dest, int flags) |
| 2850 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2851 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2852 | int err; |
| 2853 | |
| 2854 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2855 | |
| 2856 | INT_OFF; |
| 2857 | if (!(flags & CD_PHYSICAL)) { |
| 2858 | dir = updatepwd(dest); |
| 2859 | if (dir) |
| 2860 | dest = dir; |
| 2861 | } |
| 2862 | err = chdir(dest); |
| 2863 | if (err) |
| 2864 | goto out; |
| 2865 | setpwd(dir, 1); |
| 2866 | hashcd(); |
| 2867 | out: |
| 2868 | INT_ON; |
| 2869 | return err; |
| 2870 | } |
| 2871 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2872 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2873 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2874 | { |
| 2875 | const char *dest; |
| 2876 | const char *path; |
| 2877 | const char *p; |
| 2878 | char c; |
| 2879 | struct stat statb; |
| 2880 | int flags; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2881 | int len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2882 | |
| 2883 | flags = cdopt(); |
| 2884 | dest = *argptr; |
| 2885 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2886 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2887 | else if (LONE_DASH(dest)) { |
| 2888 | dest = bltinlookup("OLDPWD"); |
| 2889 | flags |= CD_PRINT; |
| 2890 | } |
| 2891 | if (!dest) |
| 2892 | dest = nullstr; |
| 2893 | if (*dest == '/') |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2894 | goto step6; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2895 | if (*dest == '.') { |
| 2896 | c = dest[1]; |
| 2897 | dotdot: |
| 2898 | switch (c) { |
| 2899 | case '\0': |
| 2900 | case '/': |
| 2901 | goto step6; |
| 2902 | case '.': |
| 2903 | c = dest[2]; |
| 2904 | if (c != '.') |
| 2905 | goto dotdot; |
| 2906 | } |
| 2907 | } |
| 2908 | if (!*dest) |
| 2909 | dest = "."; |
| 2910 | path = bltinlookup("CDPATH"); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2911 | while (p = path, (len = padvance(&path, dest)) >= 0) { |
| 2912 | c = *p; |
| 2913 | p = stalloc(len); |
| 2914 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2915 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2916 | if (c && c != ':') |
| 2917 | flags |= CD_PRINT; |
| 2918 | docd: |
| 2919 | if (!docd(p, flags)) |
| 2920 | goto out; |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2921 | goto err; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2922 | } |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | step6: |
| 2926 | p = dest; |
| 2927 | goto docd; |
| 2928 | |
| 2929 | err: |
Johannes Schindelin | 687aac0 | 2017-08-22 22:03:22 +0200 | [diff] [blame] | 2930 | ash_msg_and_raise_perror("can't cd to %s", dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2931 | /* NOTREACHED */ |
| 2932 | out: |
| 2933 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2934 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2935 | return 0; |
| 2936 | } |
| 2937 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2938 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2939 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2940 | { |
| 2941 | int flags; |
| 2942 | const char *dir = curdir; |
| 2943 | |
| 2944 | flags = cdopt(); |
| 2945 | if (flags) { |
| 2946 | if (physdir == nullstr) |
| 2947 | setpwd(dir, 0); |
| 2948 | dir = physdir; |
| 2949 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2950 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
| 2953 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2954 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2955 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2956 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2957 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2958 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2959 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2960 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2961 | #define CWORD 0 /* character is nothing special */ |
| 2962 | #define CNL 1 /* newline character */ |
| 2963 | #define CBACK 2 /* a backslash character */ |
| 2964 | #define CSQUOTE 3 /* single quote */ |
| 2965 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2966 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2967 | #define CBQUOTE 6 /* backwards single quote */ |
| 2968 | #define CVAR 7 /* a dollar sign */ |
| 2969 | #define CENDVAR 8 /* a '}' character */ |
| 2970 | #define CLP 9 /* a left paren in arithmetic */ |
| 2971 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2972 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2973 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2974 | #define CSPCL 13 /* these terminate a word */ |
| 2975 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2976 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2977 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2978 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2979 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2980 | #endif |
| 2981 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2982 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2983 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2984 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2985 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2986 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2987 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2988 | #endif |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 2989 | static const uint16_t S_I_T[] ALIGN2 = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2990 | #if ENABLE_ASH_ALIAS |
| 2991 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2992 | #endif |
| 2993 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2994 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2995 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2996 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2997 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2998 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2999 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 3000 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 3001 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 3002 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 3003 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3004 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3005 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 3006 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 3007 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 3008 | #endif |
| 3009 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3010 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3011 | /* Constants below must match table above */ |
| 3012 | enum { |
| 3013 | #if ENABLE_ASH_ALIAS |
| 3014 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 3015 | #endif |
| 3016 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 3017 | CNL_CNL_CNL_CNL , /* 2 */ |
| 3018 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 3019 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 3020 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 3021 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 3022 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 3023 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 3024 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 3025 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 3026 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 3027 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 3028 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 3029 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 3030 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3031 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3032 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 3033 | * caller must ensure proper cast on it if c is *char_ptr! |
| 3034 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3035 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3036 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3037 | static int |
| 3038 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3039 | { |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3040 | /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */ |
| 3041 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~"; |
| 3042 | /* |
| 3043 | * This causes '/' to be prepended with CTLESC in dquoted string, |
| 3044 | * making "./file"* treated incorrectly because we feed |
| 3045 | * ".\/file*" string to glob(), confusing it (see expandmeta func). |
| 3046 | * The "homegrown" glob implementation is okay with that, |
| 3047 | * but glibc one isn't. With '/' always treated as CWORD, |
| 3048 | * both work fine. |
| 3049 | */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3050 | # if ENABLE_ASH_ALIAS |
| 3051 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3052 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3053 | 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3054 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 3055 | 11, 3 /* "}~" */ |
| 3056 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3057 | # else |
| 3058 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3059 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3060 | 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3061 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 3062 | 10, 2 /* "}~" */ |
| 3063 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3064 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3065 | const char *s; |
| 3066 | int indx; |
| 3067 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3068 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3069 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3070 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3071 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3072 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3073 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3074 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3075 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3076 | /* Cast is purely for paranoia here, |
| 3077 | * just in case someone passed signed char to us */ |
| 3078 | if ((unsigned char)c >= CTL_FIRST |
| 3079 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3080 | ) { |
| 3081 | return CCTL; |
| 3082 | } |
| 3083 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3084 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3085 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3086 | indx = syntax_index_table[s - spec_symbls]; |
| 3087 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3088 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3091 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3092 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 3093 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3094 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3095 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 3096 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 3097 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 3098 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 3099 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 3100 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 3101 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 3102 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 3103 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 3104 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 3105 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 3106 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 3107 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 3108 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 3109 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 3110 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 3111 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 3112 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 3113 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 3114 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 3115 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 3116 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 3117 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 3118 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 3119 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 3120 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 3121 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 3122 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 3123 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 3124 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 3125 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 3126 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 3127 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 3128 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 3129 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 3130 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 3131 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 3132 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 3133 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 3134 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 3135 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 3136 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 3137 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 3138 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 3139 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 3140 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 3141 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3142 | /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */ |
| 3143 | /* 47 "/" */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3144 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 3145 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 3146 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 3147 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 3148 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 3149 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 3150 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 3151 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 3152 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 3153 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 3154 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 3155 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 3156 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 3157 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 3158 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 3159 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 3160 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 3161 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 3162 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 3163 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 3164 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 3165 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 3166 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 3167 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 3168 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 3169 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 3170 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 3171 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 3172 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 3173 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 3174 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 3175 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 3176 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 3177 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 3178 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 3179 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 3180 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 3181 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 3182 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 3183 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 3184 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 3185 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 3186 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 3187 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 3188 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 3189 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 3190 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 3191 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 3192 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 3193 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 3194 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 3195 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 3196 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 3197 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 3198 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 3199 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 3200 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 3201 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 3202 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 3203 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 3204 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 3205 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 3206 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 3207 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 3208 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 3209 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 3210 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 3211 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 3212 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 3213 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 3214 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 3215 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 3216 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 3217 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 3218 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 3219 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 3220 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 3221 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 3222 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 3223 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 3224 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 3225 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 3226 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3227 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3228 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 3229 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 3230 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 3231 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 3232 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 3233 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 3234 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 3235 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 3236 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 3237 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 3238 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 3239 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 3240 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 3241 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 3242 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 3243 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 3244 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 3245 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 3246 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 3247 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 3248 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 3249 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 3250 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 3251 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 3252 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 3253 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 3254 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 3255 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 3256 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 3257 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3258 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3259 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3260 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3261 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3262 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3263 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3264 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3265 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3266 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3267 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3268 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3269 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3270 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3271 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3272 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3273 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3274 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3275 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3276 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3277 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3278 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3279 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3280 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3281 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3282 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3283 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3284 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3285 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3286 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3287 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3288 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3289 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3290 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3291 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3292 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3293 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3294 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3295 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3296 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3297 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3298 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3299 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3300 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3301 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3302 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3303 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3304 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3305 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3306 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3307 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3308 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3309 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3310 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3311 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3312 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3313 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3314 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3315 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3316 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3317 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3318 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3319 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3320 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3321 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3322 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3323 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3324 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3325 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3326 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3327 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3328 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3329 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3330 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3331 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3332 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3333 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3334 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3335 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3336 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3337 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3338 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3339 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3340 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3341 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3342 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3343 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3344 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3345 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3346 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3347 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3348 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3349 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3350 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3351 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3352 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3353 | # if ENABLE_ASH_ALIAS |
| 3354 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 3355 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3356 | }; |
| 3357 | |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3358 | #if 1 |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3359 | # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3360 | #else /* debug version, caught one signed char bug */ |
| 3361 | # define SIT(c, syntax) \ |
| 3362 | ({ \ |
| 3363 | if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \ |
| 3364 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 3365 | if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \ |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3366 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
| 3367 | ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \ |
| 3368 | }) |
| 3369 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3370 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3371 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3372 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3373 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3374 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3375 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3376 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3377 | |
| 3378 | #define ALIASINUSE 1 |
| 3379 | #define ALIASDEAD 2 |
| 3380 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3381 | struct alias { |
| 3382 | struct alias *next; |
| 3383 | char *name; |
| 3384 | char *val; |
| 3385 | int flag; |
| 3386 | }; |
| 3387 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3388 | |
| 3389 | static struct alias **atab; // [ATABSIZE]; |
| 3390 | #define INIT_G_alias() do { \ |
| 3391 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3392 | } while (0) |
| 3393 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3394 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3395 | static struct alias ** |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 3396 | __lookupalias(const char *name) |
| 3397 | { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3398 | unsigned int hashval; |
| 3399 | struct alias **app; |
| 3400 | const char *p; |
| 3401 | unsigned int ch; |
| 3402 | |
| 3403 | p = name; |
| 3404 | |
| 3405 | ch = (unsigned char)*p; |
| 3406 | hashval = ch << 4; |
| 3407 | while (ch) { |
| 3408 | hashval += ch; |
| 3409 | ch = (unsigned char)*++p; |
| 3410 | } |
| 3411 | app = &atab[hashval % ATABSIZE]; |
| 3412 | |
| 3413 | for (; *app; app = &(*app)->next) { |
| 3414 | if (strcmp(name, (*app)->name) == 0) { |
| 3415 | break; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | return app; |
| 3420 | } |
| 3421 | |
| 3422 | static struct alias * |
| 3423 | lookupalias(const char *name, int check) |
| 3424 | { |
| 3425 | struct alias *ap = *__lookupalias(name); |
| 3426 | |
| 3427 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3428 | return NULL; |
| 3429 | return ap; |
| 3430 | } |
| 3431 | |
| 3432 | static struct alias * |
| 3433 | freealias(struct alias *ap) |
| 3434 | { |
| 3435 | struct alias *next; |
| 3436 | |
| 3437 | if (ap->flag & ALIASINUSE) { |
| 3438 | ap->flag |= ALIASDEAD; |
| 3439 | return ap; |
| 3440 | } |
| 3441 | |
| 3442 | next = ap->next; |
| 3443 | free(ap->name); |
| 3444 | free(ap->val); |
| 3445 | free(ap); |
| 3446 | return next; |
| 3447 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3448 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3449 | static void |
| 3450 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3451 | { |
| 3452 | struct alias *ap, **app; |
| 3453 | |
| 3454 | app = __lookupalias(name); |
| 3455 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3456 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3457 | if (ap) { |
| 3458 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3459 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3460 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3461 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3462 | ap->flag &= ~ALIASDEAD; |
| 3463 | } else { |
| 3464 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3465 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3466 | ap->name = ckstrdup(name); |
| 3467 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3468 | /*ap->flag = 0; - ckzalloc did it */ |
| 3469 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3470 | *app = ap; |
| 3471 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3472 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3475 | static int |
| 3476 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3477 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3478 | struct alias **app; |
| 3479 | |
| 3480 | app = __lookupalias(name); |
| 3481 | |
| 3482 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3483 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3484 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3485 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3486 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3489 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3492 | static void |
| 3493 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3494 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3495 | struct alias *ap, **app; |
| 3496 | int i; |
| 3497 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3498 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3499 | for (i = 0; i < ATABSIZE; i++) { |
| 3500 | app = &atab[i]; |
| 3501 | for (ap = *app; ap; ap = *app) { |
| 3502 | *app = freealias(*app); |
| 3503 | if (ap == *app) { |
| 3504 | app = &ap->next; |
| 3505 | } |
| 3506 | } |
| 3507 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3508 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3511 | static void |
| 3512 | printalias(const struct alias *ap) |
| 3513 | { |
| 3514 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3515 | } |
| 3516 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3517 | /* |
| 3518 | * TODO - sort output |
| 3519 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3520 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3521 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3522 | { |
| 3523 | char *n, *v; |
| 3524 | int ret = 0; |
| 3525 | struct alias *ap; |
| 3526 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3527 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3528 | int i; |
| 3529 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3530 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3531 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3532 | printalias(ap); |
| 3533 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3534 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3535 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3536 | } |
| 3537 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3538 | v = strchr(n+1, '='); |
| 3539 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3540 | ap = *__lookupalias(n); |
| 3541 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3542 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3543 | ret = 1; |
| 3544 | } else |
| 3545 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3546 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3547 | *v++ = '\0'; |
| 3548 | setalias(n, v); |
| 3549 | } |
| 3550 | } |
| 3551 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3552 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3555 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3556 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3557 | { |
| 3558 | int i; |
| 3559 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 3560 | while (nextopt("a") != '\0') { |
| 3561 | rmaliases(); |
| 3562 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3563 | } |
| 3564 | for (i = 0; *argptr; argptr++) { |
| 3565 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3566 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3567 | i = 1; |
| 3568 | } |
| 3569 | } |
| 3570 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3571 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3572 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3573 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3574 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3575 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3576 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3577 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3578 | #define FORK_FG 0 |
| 3579 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3580 | #define FORK_NOJOB 2 |
| 3581 | |
| 3582 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3583 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3584 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3585 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3586 | #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3587 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3588 | /* |
| 3589 | * A job structure contains information about a job. A job is either a |
| 3590 | * single process or a set of processes contained in a pipeline. In the |
| 3591 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3592 | * array of pids. |
| 3593 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3594 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3595 | pid_t ps_pid; /* process id */ |
| 3596 | int ps_status; /* last process status from wait() */ |
| 3597 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3598 | }; |
| 3599 | |
| 3600 | struct job { |
| 3601 | struct procstat ps0; /* status of process */ |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 3602 | struct procstat *ps; /* status of processes when more than one */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3603 | #if JOBS |
| 3604 | int stopstatus; /* status of a stopped job */ |
| 3605 | #endif |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3606 | unsigned nprocs; /* number of processes */ |
| 3607 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3608 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3609 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3610 | #define JOBDONE 2 /* all procs are completed */ |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3611 | unsigned |
| 3612 | state: 8, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3613 | #if JOBS |
| 3614 | sigint: 1, /* job was killed by SIGINT */ |
| 3615 | jobctl: 1, /* job running under job control */ |
| 3616 | #endif |
| 3617 | waited: 1, /* true if this entry has been waited for */ |
| 3618 | used: 1, /* true if this entry is in used */ |
| 3619 | changed: 1; /* true if status has changed */ |
| 3620 | struct job *prev_job; /* previous job */ |
| 3621 | }; |
| 3622 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3623 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3624 | static int forkshell(struct job *, union node *, int); |
| 3625 | static int waitforjob(struct job *); |
| 3626 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3627 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3628 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3629 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3630 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3631 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3632 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3633 | #endif |
| 3634 | |
| 3635 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3636 | * Ignore a signal. |
| 3637 | */ |
| 3638 | static void |
| 3639 | ignoresig(int signo) |
| 3640 | { |
| 3641 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3642 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3643 | /* No, need to do it */ |
| 3644 | signal(signo, SIG_IGN); |
| 3645 | } |
| 3646 | sigmode[signo - 1] = S_HARD_IGN; |
| 3647 | } |
| 3648 | |
| 3649 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3650 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3651 | */ |
| 3652 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3653 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3654 | { |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3655 | if (signo == SIGCHLD) { |
| 3656 | got_sigchld = 1; |
| 3657 | if (!trap[SIGCHLD]) |
| 3658 | return; |
| 3659 | } |
| 3660 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3661 | gotsig[signo - 1] = 1; |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3662 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3663 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3664 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3665 | if (!suppress_int) { |
| 3666 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3667 | raise_interrupt(); /* does not return */ |
| 3668 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3669 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3674 | * Set the signal handler for the specified signal. The routine figures |
| 3675 | * out what it should be set to. |
| 3676 | */ |
| 3677 | static void |
| 3678 | setsignal(int signo) |
| 3679 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3680 | char *t; |
| 3681 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3682 | struct sigaction act; |
| 3683 | |
| 3684 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3685 | new_act = S_DFL; |
| 3686 | if (t != NULL) { /* trap for this sig is set */ |
| 3687 | new_act = S_CATCH; |
| 3688 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3689 | new_act = S_IGN; |
| 3690 | } |
| 3691 | |
| 3692 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3693 | switch (signo) { |
| 3694 | case SIGINT: |
| 3695 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3696 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3697 | break; |
| 3698 | case SIGQUIT: |
| 3699 | #if DEBUG |
| 3700 | if (debug) |
| 3701 | break; |
| 3702 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3703 | /* man bash: |
| 3704 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3705 | * commands run by bash have signal handlers |
| 3706 | * set to the values inherited by the shell |
| 3707 | * from its parent". */ |
| 3708 | new_act = S_IGN; |
| 3709 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3710 | case SIGTERM: |
| 3711 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3712 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3713 | break; |
| 3714 | #if JOBS |
| 3715 | case SIGTSTP: |
| 3716 | case SIGTTOU: |
| 3717 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3718 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3719 | break; |
| 3720 | #endif |
| 3721 | } |
| 3722 | } |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3723 | /* if !rootshell, we reset SIGQUIT to DFL, |
| 3724 | * whereas we have to restore it to what shell got on entry. |
| 3725 | * This is handled by the fact that if signal was IGNored on entry, |
| 3726 | * then cur_act is S_HARD_IGN and we never change its sigaction |
| 3727 | * (see code below). |
| 3728 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3729 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3730 | if (signo == SIGCHLD) |
| 3731 | new_act = S_CATCH; |
| 3732 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3733 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3734 | cur_act = *t; |
| 3735 | if (cur_act == 0) { |
| 3736 | /* current setting is not yet known */ |
| 3737 | if (sigaction(signo, NULL, &act)) { |
| 3738 | /* pretend it worked; maybe we should give a warning, |
| 3739 | * but other shells don't. We don't alter sigmode, |
| 3740 | * so we retry every time. |
| 3741 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3742 | return; |
| 3743 | } |
| 3744 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3745 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3746 | if (mflag |
| 3747 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3748 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3749 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3750 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3751 | } |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 3752 | if (act.sa_handler == SIG_DFL && new_act == S_DFL) { |
| 3753 | /* installing SIG_DFL over SIG_DFL is a no-op */ |
| 3754 | /* saves one sigaction call in each "sh -c SCRIPT" invocation */ |
| 3755 | *t = S_DFL; |
| 3756 | return; |
| 3757 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3758 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3759 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3760 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3761 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3762 | *t = new_act; |
| 3763 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3764 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3765 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3766 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3767 | act.sa_handler = signal_handler; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3768 | break; |
| 3769 | case S_IGN: |
| 3770 | act.sa_handler = SIG_IGN; |
| 3771 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3772 | } |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3773 | /* flags and mask matter only if !DFL and !IGN, but we do it |
| 3774 | * for all cases for more deterministic behavior: |
| 3775 | */ |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3776 | act.sa_flags = 0; //TODO: why not SA_RESTART? |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3777 | sigfillset(&act.sa_mask); |
| 3778 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3779 | sigaction_set(signo, &act); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | /* mode flags for set_curjob */ |
| 3783 | #define CUR_DELETE 2 |
| 3784 | #define CUR_RUNNING 1 |
| 3785 | #define CUR_STOPPED 0 |
| 3786 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3787 | #if JOBS |
| 3788 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3789 | static int initialpgrp; //references:2 |
| 3790 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3791 | #endif |
| 3792 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3793 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3794 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3795 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3796 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3797 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3798 | |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 3799 | #if 0 |
| 3800 | /* Bash has a feature: it restores termios after a successful wait for |
| 3801 | * a foreground job which had at least one stopped or sigkilled member. |
| 3802 | * The probable rationale is that SIGSTOP and SIGKILL can preclude task from |
| 3803 | * properly restoring tty state. Should we do this too? |
| 3804 | * A reproducer: ^Z an interactive python: |
| 3805 | * |
| 3806 | * # python |
| 3807 | * Python 2.7.12 (...) |
| 3808 | * >>> ^Z |
| 3809 | * { python leaves tty in -icanon -echo state. We do survive that... } |
| 3810 | * [1]+ Stopped python |
| 3811 | * { ...however, next program (python #2) does not survive it well: } |
| 3812 | * # python |
| 3813 | * Python 2.7.12 (...) |
| 3814 | * >>> Traceback (most recent call last): |
| 3815 | * { above, I typed "qwerty<CR>", but -echo state is still in effect } |
| 3816 | * File "<stdin>", line 1, in <module> |
| 3817 | * NameError: name 'qwerty' is not defined |
| 3818 | * |
| 3819 | * The implementation below is modeled on bash code and seems to work. |
| 3820 | * However, I'm not sure we should do this. For one: what if I'd fg |
| 3821 | * the stopped python instead? It'll be confused by "restored" tty state. |
| 3822 | */ |
| 3823 | static struct termios shell_tty_info; |
| 3824 | static void |
| 3825 | get_tty_state(void) |
| 3826 | { |
| 3827 | if (rootshell && ttyfd >= 0) |
| 3828 | tcgetattr(ttyfd, &shell_tty_info); |
| 3829 | } |
| 3830 | static void |
| 3831 | set_tty_state(void) |
| 3832 | { |
| 3833 | /* if (rootshell) - caller ensures this */ |
| 3834 | if (ttyfd >= 0) |
| 3835 | tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info); |
| 3836 | } |
| 3837 | static int |
| 3838 | job_signal_status(struct job *jp) |
| 3839 | { |
| 3840 | int status; |
| 3841 | unsigned i; |
| 3842 | struct procstat *ps = jp->ps; |
| 3843 | for (i = 0; i < jp->nprocs; i++) { |
| 3844 | status = ps[i].ps_status; |
| 3845 | if (WIFSIGNALED(status) || WIFSTOPPED(status)) |
| 3846 | return status; |
| 3847 | } |
| 3848 | return 0; |
| 3849 | } |
| 3850 | static void |
| 3851 | restore_tty_if_stopped_or_signaled(struct job *jp) |
| 3852 | { |
| 3853 | //TODO: check what happens if we come from waitforjob() in expbackq() |
| 3854 | if (rootshell) { |
| 3855 | int s = job_signal_status(jp); |
| 3856 | if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */ |
| 3857 | set_tty_state(); |
| 3858 | } |
| 3859 | } |
| 3860 | #else |
| 3861 | # define get_tty_state() ((void)0) |
| 3862 | # define restore_tty_if_stopped_or_signaled(jp) ((void)0) |
| 3863 | #endif |
| 3864 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3865 | static void |
| 3866 | set_curjob(struct job *jp, unsigned mode) |
| 3867 | { |
| 3868 | struct job *jp1; |
| 3869 | struct job **jpp, **curp; |
| 3870 | |
| 3871 | /* first remove from list */ |
| 3872 | jpp = curp = &curjob; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3873 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3874 | jp1 = *jpp; |
| 3875 | if (jp1 == jp) |
| 3876 | break; |
| 3877 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3878 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3879 | *jpp = jp1->prev_job; |
| 3880 | |
| 3881 | /* Then re-insert in correct position */ |
| 3882 | jpp = curp; |
| 3883 | switch (mode) { |
| 3884 | default: |
| 3885 | #if DEBUG |
| 3886 | abort(); |
| 3887 | #endif |
| 3888 | case CUR_DELETE: |
| 3889 | /* job being deleted */ |
| 3890 | break; |
| 3891 | case CUR_RUNNING: |
| 3892 | /* newly created job or backgrounded job, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 3893 | * put after all stopped jobs. |
| 3894 | */ |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3895 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3896 | jp1 = *jpp; |
| 3897 | #if JOBS |
| 3898 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3899 | #endif |
| 3900 | break; |
| 3901 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3902 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3903 | /* FALLTHROUGH */ |
| 3904 | #if JOBS |
| 3905 | case CUR_STOPPED: |
| 3906 | #endif |
| 3907 | /* newly stopped job - becomes curjob */ |
| 3908 | jp->prev_job = *jpp; |
| 3909 | *jpp = jp; |
| 3910 | break; |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | #if JOBS || DEBUG |
| 3915 | static int |
| 3916 | jobno(const struct job *jp) |
| 3917 | { |
| 3918 | return jp - jobtab + 1; |
| 3919 | } |
| 3920 | #endif |
| 3921 | |
| 3922 | /* |
| 3923 | * Convert a job name to a job structure. |
| 3924 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3925 | #if !JOBS |
| 3926 | #define getjob(name, getctl) getjob(name) |
| 3927 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3928 | static struct job * |
| 3929 | getjob(const char *name, int getctl) |
| 3930 | { |
| 3931 | struct job *jp; |
| 3932 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3933 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3934 | unsigned num; |
| 3935 | int c; |
| 3936 | const char *p; |
| 3937 | char *(*match)(const char *, const char *); |
| 3938 | |
| 3939 | jp = curjob; |
| 3940 | p = name; |
| 3941 | if (!p) |
| 3942 | goto currentjob; |
| 3943 | |
| 3944 | if (*p != '%') |
| 3945 | goto err; |
| 3946 | |
| 3947 | c = *++p; |
| 3948 | if (!c) |
| 3949 | goto currentjob; |
| 3950 | |
| 3951 | if (!p[1]) { |
| 3952 | if (c == '+' || c == '%') { |
| 3953 | currentjob: |
| 3954 | err_msg = "No current job"; |
| 3955 | goto check; |
| 3956 | } |
| 3957 | if (c == '-') { |
| 3958 | if (jp) |
| 3959 | jp = jp->prev_job; |
| 3960 | err_msg = "No previous job"; |
| 3961 | check: |
| 3962 | if (!jp) |
| 3963 | goto err; |
| 3964 | goto gotit; |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | if (is_number(p)) { |
| 3969 | num = atoi(p); |
Denys Vlasenko | 46a45ce | 2016-09-29 01:10:08 +0200 | [diff] [blame] | 3970 | if (num > 0 && num <= njobs) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3971 | jp = jobtab + num - 1; |
| 3972 | if (jp->used) |
| 3973 | goto gotit; |
| 3974 | goto err; |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | match = prefix; |
| 3979 | if (*p == '?') { |
| 3980 | match = strstr; |
| 3981 | p++; |
| 3982 | } |
| 3983 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3984 | found = NULL; |
| 3985 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3986 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3987 | if (found) |
| 3988 | goto err; |
| 3989 | found = jp; |
| 3990 | err_msg = "%s: ambiguous"; |
| 3991 | } |
| 3992 | jp = jp->prev_job; |
| 3993 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3994 | if (!found) |
| 3995 | goto err; |
| 3996 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3997 | |
| 3998 | gotit: |
| 3999 | #if JOBS |
| 4000 | err_msg = "job %s not created under job control"; |
| 4001 | if (getctl && jp->jobctl == 0) |
| 4002 | goto err; |
| 4003 | #endif |
| 4004 | return jp; |
| 4005 | err: |
| 4006 | ash_msg_and_raise_error(err_msg, name); |
| 4007 | } |
| 4008 | |
| 4009 | /* |
| 4010 | * Mark a job structure as unused. |
| 4011 | */ |
| 4012 | static void |
| 4013 | freejob(struct job *jp) |
| 4014 | { |
| 4015 | struct procstat *ps; |
| 4016 | int i; |
| 4017 | |
| 4018 | INT_OFF; |
| 4019 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4020 | if (ps->ps_cmd != nullstr) |
| 4021 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4022 | } |
| 4023 | if (jp->ps != &jp->ps0) |
| 4024 | free(jp->ps); |
| 4025 | jp->used = 0; |
| 4026 | set_curjob(jp, CUR_DELETE); |
| 4027 | INT_ON; |
| 4028 | } |
| 4029 | |
| 4030 | #if JOBS |
| 4031 | static void |
| 4032 | xtcsetpgrp(int fd, pid_t pgrp) |
| 4033 | { |
| 4034 | if (tcsetpgrp(fd, pgrp)) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 4035 | ash_msg_and_raise_perror("can't set tty process group"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | /* |
| 4039 | * Turn job control on and off. |
| 4040 | * |
| 4041 | * Note: This code assumes that the third arg to ioctl is a character |
| 4042 | * pointer, which is true on Berkeley systems but not System V. Since |
| 4043 | * System V doesn't have job control yet, this isn't a problem now. |
| 4044 | * |
| 4045 | * Called with interrupts off. |
| 4046 | */ |
| 4047 | static void |
| 4048 | setjobctl(int on) |
| 4049 | { |
| 4050 | int fd; |
| 4051 | int pgrp; |
| 4052 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4053 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4054 | return; |
| 4055 | if (on) { |
| 4056 | int ofd; |
| 4057 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 4058 | if (fd < 0) { |
| 4059 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 4060 | * That sometimes helps to acquire controlling tty. |
| 4061 | * Obviously, a workaround for bugs when someone |
| 4062 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4063 | fd = 2; |
| 4064 | while (!isatty(fd)) |
| 4065 | if (--fd < 0) |
| 4066 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4067 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4068 | /* fd is a tty at this point */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4069 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 4070 | if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4071 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4072 | if (fd < 0) |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4073 | goto out; /* F_DUPFD failed */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4074 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 4075 | close_on_exec_on(fd); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4076 | while (1) { /* while we are in the background */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4077 | pgrp = tcgetpgrp(fd); |
| 4078 | if (pgrp < 0) { |
| 4079 | out: |
| 4080 | ash_msg("can't access tty; job control turned off"); |
| 4081 | mflag = on = 0; |
| 4082 | goto close; |
| 4083 | } |
| 4084 | if (pgrp == getpgrp()) |
| 4085 | break; |
| 4086 | killpg(0, SIGTTIN); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4087 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4088 | initialpgrp = pgrp; |
| 4089 | |
| 4090 | setsignal(SIGTSTP); |
| 4091 | setsignal(SIGTTOU); |
| 4092 | setsignal(SIGTTIN); |
| 4093 | pgrp = rootpid; |
| 4094 | setpgid(0, pgrp); |
| 4095 | xtcsetpgrp(fd, pgrp); |
| 4096 | } else { |
| 4097 | /* turning job control off */ |
| 4098 | fd = ttyfd; |
| 4099 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4100 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4101 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4102 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4103 | setpgid(0, pgrp); |
| 4104 | setsignal(SIGTSTP); |
| 4105 | setsignal(SIGTTOU); |
| 4106 | setsignal(SIGTTIN); |
| 4107 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4108 | if (fd >= 0) |
| 4109 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4110 | fd = -1; |
| 4111 | } |
| 4112 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4113 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4116 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4117 | killcmd(int argc, char **argv) |
| 4118 | { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4119 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4120 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4121 | do { |
| 4122 | if (argv[i][0] == '%') { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4123 | /* |
| 4124 | * "kill %N" - job kill |
| 4125 | * Converting to pgrp / pid kill |
| 4126 | */ |
| 4127 | struct job *jp; |
| 4128 | char *dst; |
| 4129 | int j, n; |
| 4130 | |
| 4131 | jp = getjob(argv[i], 0); |
| 4132 | /* |
| 4133 | * In jobs started under job control, we signal |
| 4134 | * entire process group by kill -PGRP_ID. |
| 4135 | * This happens, f.e., in interactive shell. |
| 4136 | * |
| 4137 | * Otherwise, we signal each child via |
| 4138 | * kill PID1 PID2 PID3. |
| 4139 | * Testcases: |
| 4140 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 4141 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 4142 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 4143 | */ |
| 4144 | n = jp->nprocs; /* can't be 0 (I hope) */ |
| 4145 | if (jp->jobctl) |
| 4146 | n = 1; |
| 4147 | dst = alloca(n * sizeof(int)*4); |
| 4148 | argv[i] = dst; |
| 4149 | for (j = 0; j < n; j++) { |
| 4150 | struct procstat *ps = &jp->ps[j]; |
| 4151 | /* Skip non-running and not-stopped members |
| 4152 | * (i.e. dead members) of the job |
| 4153 | */ |
| 4154 | if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status)) |
| 4155 | continue; |
| 4156 | /* |
| 4157 | * kill_main has matching code to expect |
| 4158 | * leading space. Needed to not confuse |
| 4159 | * negative pids with "kill -SIGNAL_NO" syntax |
| 4160 | */ |
| 4161 | dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid); |
| 4162 | } |
| 4163 | *dst = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4164 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4165 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4166 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4167 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4171 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4172 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4173 | struct procstat *ps; |
| 4174 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4175 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4176 | psend = jp->ps + jp->nprocs; |
| 4177 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 4178 | printf(" | %s", ps->ps_cmd); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4179 | newline_and_flush(stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4180 | flush_stdout_stderr(); |
| 4181 | } |
| 4182 | |
| 4183 | |
| 4184 | static int |
| 4185 | restartjob(struct job *jp, int mode) |
| 4186 | { |
| 4187 | struct procstat *ps; |
| 4188 | int i; |
| 4189 | int status; |
| 4190 | pid_t pgid; |
| 4191 | |
| 4192 | INT_OFF; |
| 4193 | if (jp->state == JOBDONE) |
| 4194 | goto out; |
| 4195 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4196 | pgid = jp->ps[0].ps_pid; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4197 | if (mode == FORK_FG) { |
| 4198 | get_tty_state(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4199 | xtcsetpgrp(ttyfd, pgid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4200 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4201 | killpg(pgid, SIGCONT); |
| 4202 | ps = jp->ps; |
| 4203 | i = jp->nprocs; |
| 4204 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4205 | if (WIFSTOPPED(ps->ps_status)) { |
| 4206 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4207 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4208 | ps++; |
| 4209 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4210 | out: |
| 4211 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 4212 | INT_ON; |
| 4213 | return status; |
| 4214 | } |
| 4215 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4216 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4217 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4218 | { |
| 4219 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4220 | int mode; |
| 4221 | int retval; |
| 4222 | |
| 4223 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 4224 | nextopt(nullstr); |
| 4225 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4226 | do { |
| 4227 | jp = getjob(*argv, 1); |
| 4228 | if (mode == FORK_BG) { |
| 4229 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4230 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4231 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4232 | out1str(jp->ps[0].ps_cmd); |
| 4233 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4234 | retval = restartjob(jp, mode); |
| 4235 | } while (*argv && *++argv); |
| 4236 | return retval; |
| 4237 | } |
| 4238 | #endif |
| 4239 | |
| 4240 | static int |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4241 | sprint_status48(char *os, int status, int sigonly) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4242 | { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4243 | char *s = os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4244 | int st; |
| 4245 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4246 | if (!WIFEXITED(status)) { |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4247 | #if JOBS |
| 4248 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4249 | st = WSTOPSIG(status); |
| 4250 | else |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4251 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4252 | st = WTERMSIG(status); |
| 4253 | if (sigonly) { |
| 4254 | if (st == SIGINT || st == SIGPIPE) |
| 4255 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4256 | #if JOBS |
| 4257 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4258 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4259 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4260 | } |
| 4261 | st &= 0x7f; |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 4262 | //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4263 | //s = stpncpy(s, strsignal(st), 32); //not all libc have stpncpy() |
| 4264 | s += fmtstr(s, 32, strsignal(st)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4265 | if (WCOREDUMP(status)) { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4266 | s = stpcpy(s, " (core dumped)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4267 | } |
| 4268 | } else if (!sigonly) { |
| 4269 | st = WEXITSTATUS(status); |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4270 | s += fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4271 | } |
| 4272 | out: |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4273 | return s - os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4276 | #define DOWAIT_NONBLOCK 0 |
| 4277 | #define DOWAIT_BLOCK 1 |
| 4278 | #define DOWAIT_BLOCK_OR_SIG 2 |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4279 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4280 | # define DOWAIT_JOBSTATUS 0x10 /* OR this to get job's exitstatus instead of pid */ |
| 4281 | #endif |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4282 | |
| 4283 | static int |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4284 | waitproc(int block, int *status) |
| 4285 | { |
| 4286 | sigset_t oldmask; |
| 4287 | int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG; |
| 4288 | int err; |
| 4289 | |
| 4290 | #if JOBS |
| 4291 | if (doing_jobctl) |
| 4292 | flags |= WUNTRACED; |
| 4293 | #endif |
| 4294 | |
| 4295 | do { |
| 4296 | got_sigchld = 0; |
| 4297 | do |
| 4298 | err = waitpid(-1, status, flags); |
| 4299 | while (err < 0 && errno == EINTR); |
| 4300 | |
| 4301 | if (err || (err = -!block)) |
| 4302 | break; |
| 4303 | |
| 4304 | sigfillset(&oldmask); |
| 4305 | sigprocmask2(SIG_SETMASK, &oldmask); /* mask is updated */ |
| 4306 | while (!got_sigchld && !pending_sig) |
| 4307 | sigsuspend(&oldmask); |
| 4308 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
| 4309 | //simpler, but unsafe: a signal can set pending_sig after check, but before pause(): |
| 4310 | //while (!got_sigchld && !pending_sig) |
| 4311 | // pause(); |
| 4312 | |
| 4313 | } while (got_sigchld); |
| 4314 | |
| 4315 | return err; |
| 4316 | } |
| 4317 | |
| 4318 | static int |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4319 | waitone(int block, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4320 | { |
| 4321 | int pid; |
| 4322 | int status; |
| 4323 | struct job *jp; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4324 | struct job *thisjob = NULL; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4325 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4326 | bool want_jobexitstatus = (block & DOWAIT_JOBSTATUS); |
| 4327 | block = (block & ~DOWAIT_JOBSTATUS); |
| 4328 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4329 | |
Denys Vlasenko | b543bda | 2016-10-27 20:08:28 +0200 | [diff] [blame] | 4330 | TRACE(("dowait(0x%x) called\n", block)); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 4331 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4332 | /* It's wrong to call waitpid() outside of INT_OFF region: |
| 4333 | * signal can arrive just after syscall return and handler can |
| 4334 | * longjmp away, losing stop/exit notification processing. |
| 4335 | * Thus, for "jobs" builtin, and for waiting for a fg job, |
| 4336 | * we call waitpid() (blocking or non-blocking) inside INT_OFF. |
| 4337 | * |
| 4338 | * However, for "wait" builtin it is wrong to simply call waitpid() |
| 4339 | * in INT_OFF region: "wait" needs to wait for any running job |
| 4340 | * to change state, but should exit on any trap too. |
| 4341 | * In INT_OFF region, a signal just before syscall entry can set |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4342 | * pending_sig variables, but we can't check them, and we would |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4343 | * either enter a sleeping waitpid() (BUG), or need to busy-loop. |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4344 | * |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4345 | * Because of this, we run inside INT_OFF, but use a special routine |
Denys Vlasenko | 1ab7c2f | 2016-11-03 20:17:23 +0100 | [diff] [blame] | 4346 | * which combines waitpid() and sigsuspend(). |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4347 | * This is the reason why we need to have a handler for SIGCHLD: |
Denys Vlasenko | 1ab7c2f | 2016-11-03 20:17:23 +0100 | [diff] [blame] | 4348 | * SIG_DFL handler does not wake sigsuspend(). |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4349 | */ |
| 4350 | INT_OFF; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4351 | pid = waitproc(block, &status); |
| 4352 | TRACE(("wait returns pid %d, status=%d\n", pid, status)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4353 | if (pid <= 0) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4354 | goto out; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4355 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4356 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4357 | int jobstate; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4358 | struct procstat *ps; |
| 4359 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4360 | if (jp->state == JOBDONE) |
| 4361 | continue; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4362 | jobstate = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4363 | ps = jp->ps; |
| 4364 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4365 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4366 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4367 | TRACE(("Job %d: changing status of proc %d " |
| 4368 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4369 | jobno(jp), pid, ps->ps_status, status)); |
| 4370 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4371 | thisjob = jp; |
| 4372 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4373 | if (ps->ps_status == -1) |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4374 | jobstate = JOBRUNNING; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4375 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4376 | if (jobstate == JOBRUNNING) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4377 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4378 | if (WIFSTOPPED(ps->ps_status)) { |
| 4379 | jp->stopstatus = ps->ps_status; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4380 | jobstate = JOBSTOPPED; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4381 | } |
| 4382 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4383 | } while (++ps < psend); |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4384 | if (!thisjob) |
| 4385 | continue; |
| 4386 | |
| 4387 | /* Found the job where one of its processes changed its state. |
| 4388 | * Is there at least one live and running process in this job? */ |
| 4389 | if (jobstate != JOBRUNNING) { |
| 4390 | /* No. All live processes in the job are stopped |
| 4391 | * (JOBSTOPPED) or there are no live processes (JOBDONE) |
| 4392 | */ |
| 4393 | thisjob->changed = 1; |
| 4394 | if (thisjob->state != jobstate) { |
| 4395 | TRACE(("Job %d: changing state from %d to %d\n", |
| 4396 | jobno(thisjob), thisjob->state, jobstate)); |
| 4397 | thisjob->state = jobstate; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4398 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4399 | if (jobstate == JOBSTOPPED) |
| 4400 | set_curjob(thisjob, CUR_STOPPED); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4401 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4402 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4403 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4404 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4405 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4406 | /* The process wasn't found in job list */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4407 | out: |
| 4408 | INT_ON; |
| 4409 | |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4410 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4411 | if (want_jobexitstatus) { |
| 4412 | pid = -1; |
| 4413 | if (thisjob && thisjob->state == JOBDONE) |
| 4414 | pid = thisjob->ps[thisjob->nprocs - 1].ps_status; |
| 4415 | } |
| 4416 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4417 | if (thisjob && thisjob == job) { |
| 4418 | char s[48 + 1]; |
| 4419 | int len; |
| 4420 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4421 | len = sprint_status48(s, status, 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4422 | if (len) { |
| 4423 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4424 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4425 | out2str(s); |
| 4426 | } |
| 4427 | } |
| 4428 | return pid; |
| 4429 | } |
| 4430 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4431 | static int |
| 4432 | dowait(int block, struct job *jp) |
| 4433 | { |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4434 | smallint gotchld = *(volatile smallint *)&got_sigchld; |
| 4435 | int rpid; |
| 4436 | int pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4437 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4438 | if (jp && jp->state != JOBRUNNING) |
| 4439 | block = DOWAIT_NONBLOCK; |
| 4440 | |
| 4441 | if (block == DOWAIT_NONBLOCK && !gotchld) |
| 4442 | return 1; |
| 4443 | |
| 4444 | rpid = 1; |
| 4445 | |
| 4446 | do { |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4447 | pid = waitone(block, jp); |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4448 | rpid &= !!pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4449 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4450 | if (!pid || (jp && jp->state != JOBRUNNING)) |
| 4451 | block = DOWAIT_NONBLOCK; |
| 4452 | } while (pid >= 0); |
| 4453 | |
| 4454 | return rpid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4455 | } |
| 4456 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4457 | #if JOBS |
| 4458 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4459 | showjob(struct job *jp, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4460 | { |
| 4461 | struct procstat *ps; |
| 4462 | struct procstat *psend; |
| 4463 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4464 | int indent_col; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4465 | char s[16 + 16 + 48]; |
| 4466 | FILE *out = (mode & SHOW_STDERR ? stderr : stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4467 | |
| 4468 | ps = jp->ps; |
| 4469 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4470 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4471 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4472 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4473 | return; |
| 4474 | } |
| 4475 | |
| 4476 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4477 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4478 | |
| 4479 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4480 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4481 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4482 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4483 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4484 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4485 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4486 | |
| 4487 | psend = ps + jp->nprocs; |
| 4488 | |
| 4489 | if (jp->state == JOBRUNNING) { |
| 4490 | strcpy(s + col, "Running"); |
| 4491 | col += sizeof("Running") - 1; |
| 4492 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4493 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4494 | if (jp->state == JOBSTOPPED) |
| 4495 | status = jp->stopstatus; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4496 | col += sprint_status48(s + col, status, 0); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4497 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4498 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4499 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4500 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4501 | * or prints several "PID | <cmdN>" lines, |
| 4502 | * depending on SHOW_PIDS bit. |
| 4503 | * We do not print status of individual processes |
| 4504 | * between PID and <cmdN>. bash does it, but not very well: |
| 4505 | * first line shows overall job status, not process status, |
| 4506 | * making it impossible to know 1st process status. |
| 4507 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4508 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4509 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4510 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4511 | s[0] = '\0'; |
| 4512 | col = 33; |
| 4513 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4514 | col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4515 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4516 | fprintf(out, "%s%*c%s%s", |
| 4517 | s, |
| 4518 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4519 | ps == jp->ps ? "" : "| ", |
| 4520 | ps->ps_cmd |
| 4521 | ); |
| 4522 | } while (++ps != psend); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4523 | newline_and_flush(out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4524 | |
| 4525 | jp->changed = 0; |
| 4526 | |
| 4527 | if (jp->state == JOBDONE) { |
| 4528 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4529 | freejob(jp); |
| 4530 | } |
| 4531 | } |
| 4532 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4533 | /* |
| 4534 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4535 | * statuses have changed since the last call to showjobs. |
| 4536 | */ |
| 4537 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4538 | showjobs(int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4539 | { |
| 4540 | struct job *jp; |
| 4541 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4542 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4543 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4544 | /* Handle all finished jobs */ |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4545 | dowait(DOWAIT_NONBLOCK, NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4546 | |
| 4547 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4548 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4549 | showjob(jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4550 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4551 | } |
| 4552 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4553 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4554 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4555 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4556 | { |
| 4557 | int mode, m; |
| 4558 | |
| 4559 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4560 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4561 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4562 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4563 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4564 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | argv = argptr; |
| 4568 | if (*argv) { |
| 4569 | do |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4570 | showjob(getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4571 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4572 | } else { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4573 | showjobs(mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4574 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4575 | |
| 4576 | return 0; |
| 4577 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4578 | #endif /* JOBS */ |
| 4579 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4580 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4581 | static int |
| 4582 | getstatus(struct job *job) |
| 4583 | { |
| 4584 | int status; |
| 4585 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4586 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4587 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4588 | /* Fetch last member's status */ |
| 4589 | ps = job->ps + job->nprocs - 1; |
| 4590 | status = ps->ps_status; |
| 4591 | if (pipefail) { |
| 4592 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4593 | while (status == 0 && --ps >= job->ps) |
| 4594 | status = ps->ps_status; |
| 4595 | } |
| 4596 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4597 | retval = WEXITSTATUS(status); |
| 4598 | if (!WIFEXITED(status)) { |
| 4599 | #if JOBS |
| 4600 | retval = WSTOPSIG(status); |
| 4601 | if (!WIFSTOPPED(status)) |
| 4602 | #endif |
| 4603 | { |
| 4604 | /* XXX: limits number of signals */ |
| 4605 | retval = WTERMSIG(status); |
| 4606 | #if JOBS |
| 4607 | if (retval == SIGINT) |
| 4608 | job->sigint = 1; |
| 4609 | #endif |
| 4610 | } |
| 4611 | retval += 128; |
| 4612 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4613 | TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n", |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4614 | jobno(job), job->nprocs, status, retval)); |
| 4615 | return retval; |
| 4616 | } |
| 4617 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4618 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4619 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4620 | { |
| 4621 | struct job *job; |
| 4622 | int retval; |
| 4623 | struct job *jp; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4624 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4625 | int status; |
| 4626 | char one = nextopt("n"); |
| 4627 | #else |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4628 | nextopt(nullstr); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4629 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4630 | retval = 0; |
| 4631 | |
| 4632 | argv = argptr; |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4633 | if (!argv[0]) { |
| 4634 | /* wait for all jobs / one job if -n */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4635 | for (;;) { |
| 4636 | jp = curjob; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4637 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4638 | if (one && !jp) |
| 4639 | /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */ |
| 4640 | retval = 127; |
| 4641 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4642 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4643 | if (!jp) /* no running procs */ |
| 4644 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4645 | if (jp->state == JOBRUNNING) |
| 4646 | break; |
| 4647 | jp->waited = 1; |
| 4648 | jp = jp->prev_job; |
| 4649 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4650 | /* man bash: |
| 4651 | * "When bash is waiting for an asynchronous command via |
| 4652 | * the wait builtin, the reception of a signal for which a trap |
| 4653 | * has been set will cause the wait builtin to return immediately |
| 4654 | * with an exit status greater than 128, immediately after which |
| 4655 | * the trap is executed." |
Denys Vlasenko | 6918811 | 2016-10-27 20:18:18 +0200 | [diff] [blame] | 4656 | */ |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4657 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4658 | status = dowait(DOWAIT_BLOCK_OR_SIG | DOWAIT_JOBSTATUS, NULL); |
| 4659 | #else |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4660 | dowait(DOWAIT_BLOCK_OR_SIG, NULL); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4661 | #endif |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4662 | /* if child sends us a signal *and immediately exits*, |
| 4663 | * dowait() returns pid > 0. Check this case, |
| 4664 | * not "if (dowait() < 0)"! |
| 4665 | */ |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4666 | if (pending_sig) |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4667 | goto sigout; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4668 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4669 | if (one) { |
| 4670 | /* wait -n waits for one _job_, not one _process_. |
| 4671 | * date; sleep 3 & sleep 2 | sleep 1 & wait -n; date |
| 4672 | * should wait for 2 seconds. Not 1 or 3. |
| 4673 | */ |
| 4674 | if (status != -1 && !WIFSTOPPED(status)) { |
| 4675 | retval = WEXITSTATUS(status); |
| 4676 | if (WIFSIGNALED(status)) |
| 4677 | retval = WTERMSIG(status) + 128; |
| 4678 | goto ret; |
| 4679 | } |
| 4680 | } |
| 4681 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | retval = 127; |
| 4686 | do { |
| 4687 | if (**argv != '%') { |
| 4688 | pid_t pid = number(*argv); |
| 4689 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4690 | while (1) { |
| 4691 | if (!job) |
| 4692 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4693 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4694 | break; |
| 4695 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4696 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4697 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4698 | job = getjob(*argv, 0); |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4699 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4700 | /* loop until process terminated or stopped */ |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4701 | dowait(DOWAIT_BLOCK_OR_SIG, job); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4702 | if (pending_sig) |
| 4703 | goto sigout; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4704 | job->waited = 1; |
| 4705 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4706 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4707 | } while (*++argv); |
| 4708 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4709 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4710 | return retval; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4711 | sigout: |
| 4712 | retval = 128 + pending_sig; |
| 4713 | return retval; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | static struct job * |
| 4717 | growjobtab(void) |
| 4718 | { |
| 4719 | size_t len; |
| 4720 | ptrdiff_t offset; |
| 4721 | struct job *jp, *jq; |
| 4722 | |
| 4723 | len = njobs * sizeof(*jp); |
| 4724 | jq = jobtab; |
| 4725 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4726 | |
| 4727 | offset = (char *)jp - (char *)jq; |
| 4728 | if (offset) { |
| 4729 | /* Relocate pointers */ |
| 4730 | size_t l = len; |
| 4731 | |
| 4732 | jq = (struct job *)((char *)jq + l); |
| 4733 | while (l) { |
| 4734 | l -= sizeof(*jp); |
| 4735 | jq--; |
| 4736 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4737 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4738 | if (joff(jp)->ps == &jq->ps0) |
| 4739 | jmove(joff(jp)->ps); |
| 4740 | if (joff(jp)->prev_job) |
| 4741 | jmove(joff(jp)->prev_job); |
| 4742 | } |
| 4743 | if (curjob) |
| 4744 | jmove(curjob); |
| 4745 | #undef joff |
| 4746 | #undef jmove |
| 4747 | } |
| 4748 | |
| 4749 | njobs += 4; |
| 4750 | jobtab = jp; |
| 4751 | jp = (struct job *)((char *)jp + len); |
| 4752 | jq = jp + 3; |
| 4753 | do { |
| 4754 | jq->used = 0; |
| 4755 | } while (--jq >= jp); |
| 4756 | return jp; |
| 4757 | } |
| 4758 | |
| 4759 | /* |
| 4760 | * Return a new job structure. |
| 4761 | * Called with interrupts off. |
| 4762 | */ |
| 4763 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4764 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4765 | { |
| 4766 | int i; |
| 4767 | struct job *jp; |
| 4768 | |
| 4769 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4770 | if (--i < 0) { |
| 4771 | jp = growjobtab(); |
| 4772 | break; |
| 4773 | } |
| 4774 | if (jp->used == 0) |
| 4775 | break; |
| 4776 | if (jp->state != JOBDONE || !jp->waited) |
| 4777 | continue; |
| 4778 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4779 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4780 | continue; |
| 4781 | #endif |
| 4782 | freejob(jp); |
| 4783 | break; |
| 4784 | } |
| 4785 | memset(jp, 0, sizeof(*jp)); |
| 4786 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4787 | /* jp->jobctl is a bitfield. |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4788 | * "jp->jobctl |= doing_jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4789 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4790 | jp->jobctl = 1; |
| 4791 | #endif |
| 4792 | jp->prev_job = curjob; |
| 4793 | curjob = jp; |
| 4794 | jp->used = 1; |
| 4795 | jp->ps = &jp->ps0; |
| 4796 | if (nprocs > 1) { |
| 4797 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4798 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4799 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4800 | jobno(jp))); |
| 4801 | return jp; |
| 4802 | } |
| 4803 | |
| 4804 | #if JOBS |
| 4805 | /* |
| 4806 | * Return a string identifying a command (to be printed by the |
| 4807 | * jobs command). |
| 4808 | */ |
| 4809 | static char *cmdnextc; |
| 4810 | |
| 4811 | static void |
| 4812 | cmdputs(const char *s) |
| 4813 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4814 | static const char vstype[VSTYPE + 1][3] = { |
| 4815 | "", "}", "-", "+", "?", "=", |
| 4816 | "%", "%%", "#", "##" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 4817 | IF_BASH_SUBSTR(, ":") |
| 4818 | IF_BASH_PATTERN_SUBST(, "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4819 | }; |
| 4820 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4821 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4822 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4823 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4824 | unsigned char c; |
| 4825 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4826 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4827 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4828 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4829 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4830 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4831 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4832 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4833 | switch (c) { |
| 4834 | case CTLESC: |
| 4835 | c = *p++; |
| 4836 | break; |
| 4837 | case CTLVAR: |
| 4838 | subtype = *p++; |
| 4839 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4840 | str = "${#"; |
| 4841 | else |
| 4842 | str = "${"; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 4843 | goto dostr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4844 | case CTLENDVAR: |
Denys Vlasenko | 3f4847b | 2020-02-16 19:06:42 +0100 | [diff] [blame] | 4845 | str = "\"}"; |
| 4846 | str += !(quoted & 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4847 | quoted >>= 1; |
| 4848 | subtype = 0; |
| 4849 | goto dostr; |
| 4850 | case CTLBACKQ: |
| 4851 | str = "$(...)"; |
| 4852 | goto dostr; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4853 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4854 | case CTLARI: |
| 4855 | str = "$(("; |
| 4856 | goto dostr; |
| 4857 | case CTLENDARI: |
| 4858 | str = "))"; |
| 4859 | goto dostr; |
| 4860 | #endif |
| 4861 | case CTLQUOTEMARK: |
| 4862 | quoted ^= 1; |
| 4863 | c = '"'; |
| 4864 | break; |
| 4865 | case '=': |
| 4866 | if (subtype == 0) |
| 4867 | break; |
| 4868 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4869 | quoted <<= 1; |
| 4870 | str = vstype[subtype & VSTYPE]; |
| 4871 | if (subtype & VSNUL) |
| 4872 | c = ':'; |
| 4873 | else |
| 4874 | goto checkstr; |
| 4875 | break; |
| 4876 | case '\'': |
| 4877 | case '\\': |
| 4878 | case '"': |
| 4879 | case '$': |
| 4880 | /* These can only happen inside quotes */ |
| 4881 | cc[0] = c; |
| 4882 | str = cc; |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 4883 | //FIXME: |
| 4884 | // $ true $$ & |
| 4885 | // $ <cr> |
| 4886 | // [1]+ Done true ${\$} <<=== BUG: ${\$} is not a valid way to write $$ (${$} would be ok) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4887 | c = '\\'; |
| 4888 | break; |
| 4889 | default: |
| 4890 | break; |
| 4891 | } |
| 4892 | USTPUTC(c, nextc); |
| 4893 | checkstr: |
| 4894 | if (!str) |
| 4895 | continue; |
| 4896 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4897 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4898 | USTPUTC(c, nextc); |
| 4899 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4900 | } /* while *p++ not NUL */ |
| 4901 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4902 | if (quoted & 1) { |
| 4903 | USTPUTC('"', nextc); |
| 4904 | } |
| 4905 | *nextc = 0; |
| 4906 | cmdnextc = nextc; |
| 4907 | } |
| 4908 | |
| 4909 | /* cmdtxt() and cmdlist() call each other */ |
| 4910 | static void cmdtxt(union node *n); |
| 4911 | |
| 4912 | static void |
| 4913 | cmdlist(union node *np, int sep) |
| 4914 | { |
| 4915 | for (; np; np = np->narg.next) { |
| 4916 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4917 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4918 | cmdtxt(np); |
| 4919 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4920 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | static void |
| 4925 | cmdtxt(union node *n) |
| 4926 | { |
| 4927 | union node *np; |
| 4928 | struct nodelist *lp; |
| 4929 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4930 | |
| 4931 | if (!n) |
| 4932 | return; |
| 4933 | switch (n->type) { |
| 4934 | default: |
| 4935 | #if DEBUG |
| 4936 | abort(); |
| 4937 | #endif |
| 4938 | case NPIPE: |
| 4939 | lp = n->npipe.cmdlist; |
| 4940 | for (;;) { |
| 4941 | cmdtxt(lp->n); |
| 4942 | lp = lp->next; |
| 4943 | if (!lp) |
| 4944 | break; |
| 4945 | cmdputs(" | "); |
| 4946 | } |
| 4947 | break; |
| 4948 | case NSEMI: |
| 4949 | p = "; "; |
| 4950 | goto binop; |
| 4951 | case NAND: |
| 4952 | p = " && "; |
| 4953 | goto binop; |
| 4954 | case NOR: |
| 4955 | p = " || "; |
| 4956 | binop: |
| 4957 | cmdtxt(n->nbinary.ch1); |
| 4958 | cmdputs(p); |
| 4959 | n = n->nbinary.ch2; |
| 4960 | goto donode; |
| 4961 | case NREDIR: |
| 4962 | case NBACKGND: |
| 4963 | n = n->nredir.n; |
| 4964 | goto donode; |
| 4965 | case NNOT: |
| 4966 | cmdputs("!"); |
| 4967 | n = n->nnot.com; |
| 4968 | donode: |
| 4969 | cmdtxt(n); |
| 4970 | break; |
| 4971 | case NIF: |
| 4972 | cmdputs("if "); |
| 4973 | cmdtxt(n->nif.test); |
| 4974 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4975 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4976 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4977 | cmdputs("; else "); |
| 4978 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4979 | } else { |
| 4980 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4981 | } |
| 4982 | p = "; fi"; |
| 4983 | goto dotail; |
| 4984 | case NSUBSHELL: |
| 4985 | cmdputs("("); |
| 4986 | n = n->nredir.n; |
| 4987 | p = ")"; |
| 4988 | goto dotail; |
| 4989 | case NWHILE: |
| 4990 | p = "while "; |
| 4991 | goto until; |
| 4992 | case NUNTIL: |
| 4993 | p = "until "; |
| 4994 | until: |
| 4995 | cmdputs(p); |
| 4996 | cmdtxt(n->nbinary.ch1); |
| 4997 | n = n->nbinary.ch2; |
| 4998 | p = "; done"; |
| 4999 | dodo: |
| 5000 | cmdputs("; do "); |
| 5001 | dotail: |
| 5002 | cmdtxt(n); |
| 5003 | goto dotail2; |
| 5004 | case NFOR: |
| 5005 | cmdputs("for "); |
| 5006 | cmdputs(n->nfor.var); |
| 5007 | cmdputs(" in "); |
| 5008 | cmdlist(n->nfor.args, 1); |
| 5009 | n = n->nfor.body; |
| 5010 | p = "; done"; |
| 5011 | goto dodo; |
| 5012 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 5013 | cmdputs(n->ndefun.text); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5014 | p = "() { ... }"; |
| 5015 | goto dotail2; |
| 5016 | case NCMD: |
| 5017 | cmdlist(n->ncmd.args, 1); |
| 5018 | cmdlist(n->ncmd.redirect, 0); |
| 5019 | break; |
| 5020 | case NARG: |
| 5021 | p = n->narg.text; |
| 5022 | dotail2: |
| 5023 | cmdputs(p); |
| 5024 | break; |
| 5025 | case NHERE: |
| 5026 | case NXHERE: |
| 5027 | p = "<<..."; |
| 5028 | goto dotail2; |
| 5029 | case NCASE: |
| 5030 | cmdputs("case "); |
| 5031 | cmdputs(n->ncase.expr->narg.text); |
| 5032 | cmdputs(" in "); |
| 5033 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 5034 | cmdtxt(np->nclist.pattern); |
| 5035 | cmdputs(") "); |
| 5036 | cmdtxt(np->nclist.body); |
| 5037 | cmdputs(";; "); |
| 5038 | } |
| 5039 | p = "esac"; |
| 5040 | goto dotail2; |
| 5041 | case NTO: |
| 5042 | p = ">"; |
| 5043 | goto redir; |
| 5044 | case NCLOBBER: |
| 5045 | p = ">|"; |
| 5046 | goto redir; |
| 5047 | case NAPPEND: |
| 5048 | p = ">>"; |
| 5049 | goto redir; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5050 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5051 | case NTO2: |
| 5052 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5053 | case NTOFD: |
| 5054 | p = ">&"; |
| 5055 | goto redir; |
| 5056 | case NFROM: |
| 5057 | p = "<"; |
| 5058 | goto redir; |
| 5059 | case NFROMFD: |
| 5060 | p = "<&"; |
| 5061 | goto redir; |
| 5062 | case NFROMTO: |
| 5063 | p = "<>"; |
| 5064 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5065 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5066 | cmdputs(p); |
| 5067 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 5068 | if (n->ndup.dupfd >= 0) |
| 5069 | cmdputs(utoa(n->ndup.dupfd)); |
| 5070 | else |
| 5071 | cmdputs("-"); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5072 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5073 | } |
| 5074 | n = n->nfile.fname; |
| 5075 | goto donode; |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | static char * |
| 5080 | commandtext(union node *n) |
| 5081 | { |
| 5082 | char *name; |
| 5083 | |
| 5084 | STARTSTACKSTR(cmdnextc); |
| 5085 | cmdtxt(n); |
| 5086 | name = stackblock(); |
Denys Vlasenko | 6a94cee | 2016-10-25 17:40:25 +0200 | [diff] [blame] | 5087 | TRACE(("commandtext: name %p, end %p\n", name, cmdnextc)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5088 | return ckstrdup(name); |
| 5089 | } |
| 5090 | #endif /* JOBS */ |
| 5091 | |
| 5092 | /* |
| 5093 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 5094 | * own process group. Jp is a job structure that the job is to be added to. |
| 5095 | * N is the command that will be evaluated by the child. Both jp and n may |
| 5096 | * be NULL. The mode parameter can be one of the following: |
| 5097 | * FORK_FG - Fork off a foreground process. |
| 5098 | * FORK_BG - Fork off a background process. |
| 5099 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 5100 | * process group even if job control is on. |
| 5101 | * |
| 5102 | * When job control is turned off, background processes have their standard |
| 5103 | * input redirected to /dev/null (except for the second and later processes |
| 5104 | * in a pipeline). |
| 5105 | * |
| 5106 | * Called with interrupts off. |
| 5107 | */ |
| 5108 | /* |
| 5109 | * Clear traps on a fork. |
| 5110 | */ |
| 5111 | static void |
| 5112 | clear_traps(void) |
| 5113 | { |
| 5114 | char **tp; |
| 5115 | |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5116 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5117 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5118 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5119 | if (trap_ptr == trap) |
| 5120 | free(*tp); |
| 5121 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5122 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 5123 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5124 | setsignal(tp - trap); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5125 | } |
| 5126 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 5127 | may_have_traps = 0; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5128 | INT_ON; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5129 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5130 | |
| 5131 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5132 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 5133 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5134 | /* Called after fork(), in child */ |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5135 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 5136 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5137 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5138 | { |
| 5139 | int oldlvl; |
| 5140 | |
| 5141 | TRACE(("Child shell %d\n", getpid())); |
| 5142 | oldlvl = shlvl; |
| 5143 | shlvl++; |
| 5144 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5145 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 5146 | * set to the values inherited by the shell from its parent". |
| 5147 | * Do we do it correctly? */ |
| 5148 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5149 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5150 | |
| 5151 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 5152 | && n && n->type == NCMD /* is it single cmd? */ |
| 5153 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5154 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5155 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 5156 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 5157 | ) { |
| 5158 | TRACE(("Trap hack\n")); |
| 5159 | /* Awful hack for `trap` or $(trap). |
| 5160 | * |
| 5161 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5162 | * contains an example where "trap" is executed in a subshell: |
| 5163 | * |
| 5164 | * save_traps=$(trap) |
| 5165 | * ... |
| 5166 | * eval "$save_traps" |
| 5167 | * |
| 5168 | * Standard does not say that "trap" in subshell shall print |
| 5169 | * parent shell's traps. It only says that its output |
| 5170 | * must have suitable form, but then, in the above example |
| 5171 | * (which is not supposed to be normative), it implies that. |
| 5172 | * |
| 5173 | * bash (and probably other shell) does implement it |
| 5174 | * (traps are reset to defaults, but "trap" still shows them), |
| 5175 | * but as a result, "trap" logic is hopelessly messed up: |
| 5176 | * |
| 5177 | * # trap |
| 5178 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5179 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5180 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5181 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5182 | * trap -- 'echo Ho' SIGWINCH |
| 5183 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5184 | * trap -- 'echo Ho' SIGWINCH |
| 5185 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5186 | * trap -- 'echo Ho' SIGWINCH |
| 5187 | * |
| 5188 | * The rules when to forget and when to not forget traps |
| 5189 | * get really complex and nonsensical. |
| 5190 | * |
| 5191 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5192 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5193 | /* Save trap handler strings for trap builtin to print */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 5194 | trap_ptr = xmemdup(trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5195 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5196 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5197 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5198 | #if JOBS |
| 5199 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5200 | doing_jobctl = 0; |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5201 | if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5202 | pid_t pgrp; |
| 5203 | |
| 5204 | if (jp->nprocs == 0) |
| 5205 | pgrp = getpid(); |
| 5206 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5207 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5208 | /* this can fail because we are doing it in the parent also */ |
| 5209 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5210 | if (mode == FORK_FG) |
| 5211 | xtcsetpgrp(ttyfd, pgrp); |
| 5212 | setsignal(SIGTSTP); |
| 5213 | setsignal(SIGTTOU); |
| 5214 | } else |
| 5215 | #endif |
| 5216 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5217 | /* man bash: "When job control is not in effect, |
| 5218 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5219 | ignoresig(SIGINT); |
| 5220 | ignoresig(SIGQUIT); |
| 5221 | if (jp->nprocs == 0) { |
| 5222 | close(0); |
| 5223 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5224 | ash_msg_and_raise_perror("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5227 | if (oldlvl == 0) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5228 | if (iflag) { /* why if iflag only? */ |
| 5229 | setsignal(SIGINT); |
| 5230 | setsignal(SIGTERM); |
| 5231 | } |
| 5232 | /* man bash: |
| 5233 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 5234 | * commands run by bash have signal handlers |
| 5235 | * set to the values inherited by the shell |
| 5236 | * from its parent". |
| 5237 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5238 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5239 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5240 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5241 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5242 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5243 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5244 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5245 | /* "jobs": we do not want to clear job list for it, |
| 5246 | * instead we remove only _its_ own_ job from job list. |
| 5247 | * This makes "jobs .... | cat" more useful. |
| 5248 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5249 | freejob(curjob); |
| 5250 | return; |
| 5251 | } |
| 5252 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5253 | for (jp = curjob; jp; jp = jp->prev_job) |
| 5254 | freejob(jp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5257 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 5258 | #if !JOBS |
| 5259 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 5260 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5261 | static void |
| 5262 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 5263 | { |
| 5264 | TRACE(("In parent shell: child = %d\n", pid)); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5265 | if (!jp) /* jp is NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5266 | return; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5267 | #if JOBS |
| 5268 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 5269 | int pgrp; |
| 5270 | |
| 5271 | if (jp->nprocs == 0) |
| 5272 | pgrp = pid; |
| 5273 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5274 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5275 | /* This can fail because we are doing it in the child also */ |
| 5276 | setpgid(pid, pgrp); |
| 5277 | } |
| 5278 | #endif |
| 5279 | if (mode == FORK_BG) { |
| 5280 | backgndpid = pid; /* set $! */ |
| 5281 | set_curjob(jp, CUR_RUNNING); |
| 5282 | } |
| 5283 | if (jp) { |
| 5284 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5285 | ps->ps_pid = pid; |
| 5286 | ps->ps_status = -1; |
| 5287 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5288 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5289 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5290 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5291 | #endif |
| 5292 | } |
| 5293 | } |
| 5294 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5295 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5296 | static int |
| 5297 | forkshell(struct job *jp, union node *n, int mode) |
| 5298 | { |
| 5299 | int pid; |
| 5300 | |
| 5301 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 5302 | pid = fork(); |
| 5303 | if (pid < 0) { |
| 5304 | TRACE(("Fork failed, errno=%d", errno)); |
| 5305 | if (jp) |
| 5306 | freejob(jp); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5307 | ash_msg_and_raise_perror("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5308 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5309 | if (pid == 0) { |
| 5310 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5311 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5312 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5313 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5314 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5315 | return pid; |
| 5316 | } |
| 5317 | |
| 5318 | /* |
| 5319 | * Wait for job to finish. |
| 5320 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5321 | * Under job control we have the problem that while a child process |
| 5322 | * is running interrupts generated by the user are sent to the child |
| 5323 | * but not to the shell. This means that an infinite loop started by |
| 5324 | * an interactive user may be hard to kill. With job control turned off, |
| 5325 | * an interactive user may place an interactive program inside a loop. |
| 5326 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5327 | * these interrupts to also abort the loop. The approach we take here |
| 5328 | * is to have the shell ignore interrupt signals while waiting for a |
| 5329 | * foreground process to terminate, and then send itself an interrupt |
| 5330 | * signal if the child process was terminated by an interrupt signal. |
| 5331 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 5332 | * exit on interrupt; unless these processes terminate themselves by |
| 5333 | * sending a signal to themselves (instead of calling exit) they will |
| 5334 | * confuse this approach. |
| 5335 | * |
| 5336 | * Called with interrupts off. |
| 5337 | */ |
| 5338 | static int |
| 5339 | waitforjob(struct job *jp) |
| 5340 | { |
| 5341 | int st; |
| 5342 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 5343 | TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5344 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5345 | /* In non-interactive shells, we _can_ get |
| 5346 | * a keyboard signal here and be EINTRed, but we just loop |
| 5347 | * inside dowait(), waiting for command to complete. |
| 5348 | * |
| 5349 | * man bash: |
| 5350 | * "If bash is waiting for a command to complete and receives |
| 5351 | * a signal for which a trap has been set, the trap |
| 5352 | * will not be executed until the command completes." |
| 5353 | * |
| 5354 | * Reality is that even if trap is not set, bash |
| 5355 | * will not act on the signal until command completes. |
| 5356 | * Try this. sleep5intoff.c: |
| 5357 | * #include <signal.h> |
| 5358 | * #include <unistd.h> |
| 5359 | * int main() { |
| 5360 | * sigset_t set; |
| 5361 | * sigemptyset(&set); |
| 5362 | * sigaddset(&set, SIGINT); |
| 5363 | * sigaddset(&set, SIGQUIT); |
| 5364 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 5365 | * sleep(5); |
| 5366 | * return 0; |
| 5367 | * } |
| 5368 | * $ bash -c './sleep5intoff; echo hi' |
| 5369 | * ^C^C^C^C <--- pressing ^C once a second |
| 5370 | * $ _ |
| 5371 | * $ bash -c './sleep5intoff; echo hi' |
| 5372 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 5373 | * $ _ |
| 5374 | */ |
| 5375 | dowait(jp ? DOWAIT_BLOCK : DOWAIT_NONBLOCK, jp); |
| 5376 | if (!jp) |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 5377 | return exitstatus; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5378 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5379 | st = getstatus(jp); |
| 5380 | #if JOBS |
| 5381 | if (jp->jobctl) { |
| 5382 | xtcsetpgrp(ttyfd, rootpid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 5383 | restore_tty_if_stopped_or_signaled(jp); |
| 5384 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5385 | /* |
| 5386 | * This is truly gross. |
| 5387 | * If we're doing job control, then we did a TIOCSPGRP which |
| 5388 | * caused us (the shell) to no longer be in the controlling |
| 5389 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 5390 | * intuit from the subprocess exit status whether a SIGINT |
| 5391 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 5392 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5393 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 5394 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5395 | } |
| 5396 | if (jp->state == JOBDONE) |
| 5397 | #endif |
| 5398 | freejob(jp); |
| 5399 | return st; |
| 5400 | } |
| 5401 | |
| 5402 | /* |
| 5403 | * return 1 if there are stopped jobs, otherwise 0 |
| 5404 | */ |
| 5405 | static int |
| 5406 | stoppedjobs(void) |
| 5407 | { |
| 5408 | struct job *jp; |
| 5409 | int retval; |
| 5410 | |
| 5411 | retval = 0; |
| 5412 | if (job_warning) |
| 5413 | goto out; |
| 5414 | jp = curjob; |
| 5415 | if (jp && jp->state == JOBSTOPPED) { |
| 5416 | out2str("You have stopped jobs.\n"); |
| 5417 | job_warning = 2; |
| 5418 | retval++; |
| 5419 | } |
| 5420 | out: |
| 5421 | return retval; |
| 5422 | } |
| 5423 | |
| 5424 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5425 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5426 | * Code for dealing with input/output redirection. |
| 5427 | */ |
| 5428 | |
Denys Vlasenko | 8d0e0cd | 2011-01-25 23:21:46 +0100 | [diff] [blame] | 5429 | #undef EMPTY |
| 5430 | #undef CLOSED |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5431 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5432 | #define CLOSED -1 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5433 | |
| 5434 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5435 | * Handle here documents. Normally we fork off a process to write the |
| 5436 | * data to a pipe. If the document is short, we can stuff the data in |
| 5437 | * the pipe without forking. |
| 5438 | */ |
| 5439 | /* openhere needs this forward reference */ |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5440 | static void expandhere(union node *arg); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5441 | static int |
| 5442 | openhere(union node *redir) |
| 5443 | { |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5444 | char *p; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5445 | int pip[2]; |
| 5446 | size_t len = 0; |
| 5447 | |
| 5448 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5449 | ash_msg_and_raise_perror("can't create pipe"); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5450 | |
| 5451 | p = redir->nhere.doc->narg.text; |
| 5452 | if (redir->type == NXHERE) { |
| 5453 | expandhere(redir->nhere.doc); |
| 5454 | p = stackblock(); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5455 | } |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5456 | |
| 5457 | len = strlen(p); |
| 5458 | if (len <= PIPE_BUF) { |
| 5459 | xwrite(pip[1], p, len); |
| 5460 | goto out; |
| 5461 | } |
| 5462 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5463 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5464 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5465 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5466 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5467 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5468 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5469 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5470 | signal(SIGPIPE, SIG_DFL); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5471 | xwrite(pip[1], p, len); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5472 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5473 | } |
| 5474 | out: |
| 5475 | close(pip[1]); |
| 5476 | return pip[0]; |
| 5477 | } |
| 5478 | |
| 5479 | static int |
| 5480 | openredirect(union node *redir) |
| 5481 | { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5482 | struct stat sb; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5483 | char *fname; |
| 5484 | int f; |
| 5485 | |
| 5486 | switch (redir->nfile.type) { |
Denys Vlasenko | 557482c | 2016-09-25 21:24:04 +0200 | [diff] [blame] | 5487 | /* Can't happen, our single caller does this itself */ |
| 5488 | // case NTOFD: |
| 5489 | // case NFROMFD: |
| 5490 | // return -1; |
| 5491 | case NHERE: |
| 5492 | case NXHERE: |
| 5493 | return openhere(redir); |
| 5494 | } |
| 5495 | |
| 5496 | /* For N[X]HERE, reading redir->nfile.expfname would touch beyond |
| 5497 | * allocated space. Do it only when we know it is safe. |
| 5498 | */ |
| 5499 | fname = redir->nfile.expfname; |
| 5500 | |
| 5501 | switch (redir->nfile.type) { |
| 5502 | default: |
| 5503 | #if DEBUG |
| 5504 | abort(); |
| 5505 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5506 | case NFROM: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5507 | f = open(fname, O_RDONLY); |
| 5508 | if (f < 0) |
| 5509 | goto eopen; |
| 5510 | break; |
| 5511 | case NFROMTO: |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5512 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5513 | if (f < 0) |
| 5514 | goto ecreate; |
| 5515 | break; |
| 5516 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5517 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5518 | case NTO2: |
| 5519 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5520 | /* Take care of noclobber mode. */ |
| 5521 | if (Cflag) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5522 | if (stat(fname, &sb) < 0) { |
| 5523 | f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 5524 | if (f < 0) |
| 5525 | goto ecreate; |
| 5526 | } else if (!S_ISREG(sb.st_mode)) { |
| 5527 | f = open(fname, O_WRONLY, 0666); |
| 5528 | if (f < 0) |
| 5529 | goto ecreate; |
Denys Vlasenko | 355ec35 | 2018-04-02 13:34:57 +0200 | [diff] [blame] | 5530 | if (!fstat(f, &sb) && S_ISREG(sb.st_mode)) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5531 | close(f); |
| 5532 | errno = EEXIST; |
| 5533 | goto ecreate; |
| 5534 | } |
| 5535 | } else { |
| 5536 | errno = EEXIST; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5537 | goto ecreate; |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5538 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5539 | break; |
| 5540 | } |
| 5541 | /* FALLTHROUGH */ |
| 5542 | case NCLOBBER: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5543 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5544 | if (f < 0) |
| 5545 | goto ecreate; |
| 5546 | break; |
| 5547 | case NAPPEND: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5548 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5549 | if (f < 0) |
| 5550 | goto ecreate; |
| 5551 | break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | return f; |
| 5555 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5556 | ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5557 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5558 | ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | /* |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5562 | * Copy a file descriptor to be >= 10. Throws exception on error. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5563 | */ |
| 5564 | static int |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5565 | savefd(int from) |
| 5566 | { |
| 5567 | int newfd; |
| 5568 | int err; |
| 5569 | |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5570 | newfd = fcntl(from, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5571 | err = newfd < 0 ? errno : 0; |
| 5572 | if (err != EBADF) { |
| 5573 | if (err) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5574 | ash_msg_and_raise_perror("%d", from); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5575 | close(from); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5576 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5577 | close_on_exec_on(newfd); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | return newfd; |
| 5581 | } |
| 5582 | static int |
| 5583 | dup2_or_raise(int from, int to) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5584 | { |
| 5585 | int newfd; |
| 5586 | |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5587 | newfd = (from != to) ? dup2(from, to) : to; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5588 | if (newfd < 0) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5589 | /* Happens when source fd is not open: try "echo >&99" */ |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5590 | ash_msg_and_raise_perror("%d", from); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5591 | } |
| 5592 | return newfd; |
| 5593 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5594 | static int |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5595 | dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5596 | { |
| 5597 | int newfd; |
| 5598 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5599 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 5600 | if (newfd >= 0) { |
| 5601 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5602 | close_on_exec_on(newfd); |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5603 | } else { /* newfd < 0 */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5604 | if (errno == EBUSY) |
| 5605 | goto repeat; |
| 5606 | if (errno == EINTR) |
| 5607 | goto repeat; |
| 5608 | } |
| 5609 | return newfd; |
| 5610 | } |
| 5611 | static int |
| 5612 | xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
| 5613 | { |
| 5614 | int newfd; |
| 5615 | repeat: |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5616 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5617 | if (newfd < 0) { |
| 5618 | if (errno == EBUSY) |
| 5619 | goto repeat; |
| 5620 | if (errno == EINTR) |
| 5621 | goto repeat; |
| 5622 | /* fd was not open? */ |
| 5623 | if (errno == EBADF) |
| 5624 | return fd; |
| 5625 | ash_msg_and_raise_perror("%d", newfd); |
| 5626 | } |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5627 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5628 | close_on_exec_on(newfd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5629 | close(fd); |
| 5630 | return newfd; |
| 5631 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5632 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5633 | /* Struct def and variable are moved down to the first usage site */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5634 | struct squirrel { |
| 5635 | int orig_fd; |
| 5636 | int moved_to; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5637 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5638 | struct redirtab { |
| 5639 | struct redirtab *next; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5640 | int pair_count; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5641 | struct squirrel two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5642 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5643 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5644 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5645 | static void |
| 5646 | add_squirrel_closed(struct redirtab *sq, int fd) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5647 | { |
| 5648 | int i; |
| 5649 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5650 | if (!sq) |
| 5651 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5652 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5653 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5654 | /* If we collide with an already moved fd... */ |
| 5655 | if (fd == sq->two_fd[i].orig_fd) { |
| 5656 | /* Examples: |
| 5657 | * "echo 3>FILE 3>&- 3>FILE" |
| 5658 | * "echo 3>&- 3>FILE" |
| 5659 | * No need for last redirect to insert |
| 5660 | * another "need to close 3" indicator. |
| 5661 | */ |
| 5662 | TRACE(("redirect_fd %d: already moved or closed\n", fd)); |
| 5663 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5664 | } |
| 5665 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5666 | TRACE(("redirect_fd %d: previous fd was closed\n", fd)); |
| 5667 | sq->two_fd[i].orig_fd = fd; |
| 5668 | sq->two_fd[i].moved_to = CLOSED; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 5671 | static int |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5672 | save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5673 | { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5674 | int i, new_fd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5675 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5676 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 5677 | avoid_fd = 9; |
| 5678 | |
| 5679 | #if JOBS |
| 5680 | if (fd == ttyfd) { |
| 5681 | /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */ |
| 5682 | ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd); |
| 5683 | TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd)); |
| 5684 | return 1; /* "we closed fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5685 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5686 | #endif |
| 5687 | /* Are we called from redirect(0)? E.g. redirect |
| 5688 | * in a forked child. No need to save fds, |
| 5689 | * we aren't going to use them anymore, ok to trash. |
| 5690 | */ |
| 5691 | if (!sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5692 | return 0; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5693 | |
| 5694 | /* If this one of script's fds? */ |
| 5695 | if (fd != 0) { |
| 5696 | struct parsefile *pf = g_parsefile; |
| 5697 | while (pf) { |
| 5698 | /* We skip fd == 0 case because of the following: |
| 5699 | * $ ash # running ash interactively |
| 5700 | * $ . ./script.sh |
| 5701 | * and in script.sh: "exec 9>&0". |
| 5702 | * Even though top-level pf_fd _is_ 0, |
| 5703 | * it's still ok to use it: "read" builtin uses it, |
| 5704 | * why should we cripple "exec" builtin? |
| 5705 | */ |
| 5706 | if (fd == pf->pf_fd) { |
| 5707 | pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 5708 | return 1; /* "we closed fd" */ |
| 5709 | } |
| 5710 | pf = pf->prev; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5713 | |
| 5714 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 5715 | |
| 5716 | /* First: do we collide with some already moved fds? */ |
| 5717 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5718 | /* If we collide with an already moved fd... */ |
| 5719 | if (fd == sq->two_fd[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5720 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5721 | sq->two_fd[i].moved_to = new_fd; |
| 5722 | TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd)); |
| 5723 | if (new_fd < 0) /* what? */ |
| 5724 | xfunc_die(); |
| 5725 | return 0; /* "we did not close fd" */ |
| 5726 | } |
| 5727 | if (fd == sq->two_fd[i].orig_fd) { |
| 5728 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 5729 | TRACE(("redirect_fd %d: already moved\n", fd)); |
| 5730 | return 0; /* "we did not close fd" */ |
| 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | /* If this fd is open, we move and remember it; if it's closed, new_fd = CLOSED (-1) */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5735 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5736 | TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd)); |
| 5737 | if (new_fd < 0) { |
| 5738 | if (errno != EBADF) |
| 5739 | xfunc_die(); |
| 5740 | /* new_fd = CLOSED; - already is -1 */ |
| 5741 | } |
| 5742 | sq->two_fd[i].moved_to = new_fd; |
| 5743 | sq->two_fd[i].orig_fd = fd; |
| 5744 | |
| 5745 | /* if we move stderr, let "set -x" code know */ |
| 5746 | if (fd == preverrout_fd) |
| 5747 | preverrout_fd = new_fd; |
| 5748 | |
| 5749 | return 0; /* "we did not close fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5752 | static int |
| 5753 | internally_opened_fd(int fd, struct redirtab *sq) |
| 5754 | { |
| 5755 | int i; |
| 5756 | #if JOBS |
| 5757 | if (fd == ttyfd) |
| 5758 | return 1; |
| 5759 | #endif |
| 5760 | /* If this one of script's fds? */ |
| 5761 | if (fd != 0) { |
| 5762 | struct parsefile *pf = g_parsefile; |
| 5763 | while (pf) { |
| 5764 | if (fd == pf->pf_fd) |
| 5765 | return 1; |
| 5766 | pf = pf->prev; |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5771 | if (fd == sq->two_fd[i].moved_to) |
| 5772 | return 1; |
| 5773 | } |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5777 | /* |
| 5778 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5779 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5780 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5781 | */ |
| 5782 | /* flags passed to redirect */ |
| 5783 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5784 | static void |
| 5785 | redirect(union node *redir, int flags) |
| 5786 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5787 | struct redirtab *sv; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5788 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5789 | if (!redir) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5790 | return; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5791 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5792 | sv = NULL; |
| 5793 | INT_OFF; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5794 | if (flags & REDIR_PUSH) |
| 5795 | sv = redirlist; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5796 | do { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5797 | int fd; |
| 5798 | int newfd; |
| 5799 | int close_fd; |
| 5800 | int closed; |
| 5801 | |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5802 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5803 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5804 | //bb_error_msg("doing %d > %d", fd, newfd); |
| 5805 | newfd = redir->ndup.dupfd; |
| 5806 | close_fd = -1; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5807 | } else { |
| 5808 | newfd = openredirect(redir); /* always >= 0 */ |
| 5809 | if (fd == newfd) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5810 | /* open() gave us precisely the fd we wanted. |
| 5811 | * This means that this fd was not busy |
| 5812 | * (not opened to anywhere). |
| 5813 | * Remember to close it on restore: |
| 5814 | */ |
| 5815 | add_squirrel_closed(sv, fd); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5816 | continue; |
| 5817 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5818 | close_fd = newfd; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5819 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5820 | |
| 5821 | if (fd == newfd) |
| 5822 | continue; |
| 5823 | |
| 5824 | /* if "N>FILE": move newfd to fd */ |
| 5825 | /* if "N>&M": dup newfd to fd */ |
| 5826 | /* if "N>&-": close fd (newfd is -1) */ |
| 5827 | |
| 5828 | IF_BASH_REDIR_OUTPUT(redirect_more:) |
| 5829 | |
| 5830 | closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv); |
| 5831 | if (newfd == -1) { |
| 5832 | /* "N>&-" means "close me" */ |
| 5833 | if (!closed) { |
| 5834 | /* ^^^ optimization: saving may already |
| 5835 | * have closed it. If not... */ |
| 5836 | close(fd); |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5837 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5838 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5839 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 5840 | if (internally_opened_fd(newfd, sv)) { |
| 5841 | errno = EBADF; |
| 5842 | ash_msg_and_raise_perror("%d", newfd); |
| 5843 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5844 | dup2_or_raise(newfd, fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5845 | if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */ |
| 5846 | close(close_fd); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5847 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5848 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5849 | /* ">&FILE". we already redirected to 1, now copy 1 to 2 */ |
| 5850 | fd = 2; |
| 5851 | newfd = 1; |
| 5852 | close_fd = -1; |
| 5853 | goto redirect_more; |
| 5854 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5855 | #endif |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5856 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5857 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5858 | INT_ON; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5859 | |
| 5860 | //dash:#define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5861 | #define REDIR_SAVEFD2 0 |
| 5862 | // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test |
| 5863 | // triggers for pure REDIR_PUSH too. Thus, this is done almost always, |
| 5864 | // not only for calls with flags containing REDIR_SAVEFD2. |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5865 | // We do this unconditionally (see save_fd_on_redirect()). |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5866 | //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5867 | // preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5870 | static int |
| 5871 | redirectsafe(union node *redir, int flags) |
| 5872 | { |
| 5873 | int err; |
| 5874 | volatile int saveint; |
| 5875 | struct jmploc *volatile savehandler = exception_handler; |
| 5876 | struct jmploc jmploc; |
| 5877 | |
| 5878 | SAVE_INT(saveint); |
| 5879 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5880 | err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */ |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5881 | if (!err) { |
| 5882 | exception_handler = &jmploc; |
| 5883 | redirect(redir, flags); |
| 5884 | } |
| 5885 | exception_handler = savehandler; |
| 5886 | if (err && exception_type != EXERROR) |
| 5887 | longjmp(exception_handler->loc, 1); |
| 5888 | RESTORE_INT(saveint); |
| 5889 | return err; |
| 5890 | } |
| 5891 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5892 | static struct redirtab* |
| 5893 | pushredir(union node *redir) |
| 5894 | { |
| 5895 | struct redirtab *sv; |
| 5896 | int i; |
| 5897 | |
| 5898 | if (!redir) |
| 5899 | return redirlist; |
| 5900 | |
| 5901 | i = 0; |
| 5902 | do { |
| 5903 | i++; |
| 5904 | #if BASH_REDIR_OUTPUT |
| 5905 | if (redir->nfile.type == NTO2) |
| 5906 | i++; |
| 5907 | #endif |
| 5908 | redir = redir->nfile.next; |
| 5909 | } while (redir); |
| 5910 | |
| 5911 | sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0])); |
| 5912 | sv->pair_count = i; |
| 5913 | while (--i >= 0) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5914 | sv->two_fd[i].orig_fd = sv->two_fd[i].moved_to = EMPTY; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5915 | sv->next = redirlist; |
| 5916 | redirlist = sv; |
| 5917 | return sv->next; |
| 5918 | } |
| 5919 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5920 | /* |
| 5921 | * Undo the effects of the last redirection. |
| 5922 | */ |
| 5923 | static void |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5924 | popredir(int drop) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5925 | { |
| 5926 | struct redirtab *rp; |
| 5927 | int i; |
| 5928 | |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 5929 | if (redirlist == NULL) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5930 | return; |
| 5931 | INT_OFF; |
| 5932 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5933 | for (i = 0; i < rp->pair_count; i++) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5934 | int fd = rp->two_fd[i].orig_fd; |
| 5935 | int copy = rp->two_fd[i].moved_to; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5936 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5937 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5938 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5939 | continue; |
| 5940 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5941 | if (copy != EMPTY) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5942 | if (!drop) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5943 | /*close(fd);*/ |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5944 | dup2_or_raise(copy, fd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5945 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5946 | close(copy); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5947 | } |
| 5948 | } |
| 5949 | redirlist = rp->next; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5950 | free(rp); |
| 5951 | INT_ON; |
| 5952 | } |
| 5953 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5954 | static void |
| 5955 | unwindredir(struct redirtab *stop) |
| 5956 | { |
| 5957 | while (redirlist != stop) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5958 | popredir(/*drop:*/ 0); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5959 | } |
| 5960 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5961 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5962 | /* ============ Routines to expand arguments to commands |
| 5963 | * |
| 5964 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5965 | */ |
| 5966 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5967 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5968 | static arith_t |
| 5969 | ash_arith(const char *s) |
| 5970 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5971 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5972 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5973 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5974 | math_state.lookupvar = lookupvar; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 5975 | math_state.setvar = setvar0; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5976 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5977 | |
| 5978 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5979 | result = arith(&math_state, s); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5980 | if (math_state.errmsg) |
| 5981 | ash_msg_and_raise_error(math_state.errmsg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5982 | INT_ON; |
| 5983 | |
| 5984 | return result; |
| 5985 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5986 | #endif |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 5987 | #if BASH_SUBSTR |
| 5988 | # if ENABLE_FEATURE_SH_MATH |
| 5989 | static int substr_atoi(const char *s) |
| 5990 | { |
| 5991 | arith_t t = ash_arith(s); |
| 5992 | if (sizeof(t) > sizeof(int)) { |
| 5993 | /* clamp very large or very large negative nums for ${v:N:M}: |
| 5994 | * else "${v:0:0x100000001}" would work as "${v:0:1}" |
| 5995 | */ |
| 5996 | if (t > INT_MAX) |
| 5997 | t = INT_MAX; |
| 5998 | if (t < INT_MIN) |
| 5999 | t = INT_MIN; |
| 6000 | } |
| 6001 | return t; |
| 6002 | } |
| 6003 | # else |
| 6004 | # define substr_atoi(s) number(s) |
| 6005 | # endif |
| 6006 | #endif |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 6007 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6008 | /* |
| 6009 | * expandarg flags |
| 6010 | */ |
| 6011 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 6012 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 6013 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 6014 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
Denys Vlasenko | db74c6c | 2016-10-24 21:12:33 +0200 | [diff] [blame] | 6015 | /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt" |
| 6016 | * POSIX says for this case: |
| 6017 | * Pathname expansion shall not be performed on the word by a |
| 6018 | * non-interactive shell; an interactive shell may perform it, but shall |
| 6019 | * do so only when the expansion would result in one word. |
| 6020 | * Currently, our code complies to the above rule by never globbing |
| 6021 | * redirection filenames. |
| 6022 | * Bash performs globbing, unless it is non-interactive and in POSIX mode. |
| 6023 | * (this means that on a typical Linux distro, bash almost always |
| 6024 | * performs globbing, and thus diverges from what we do). |
| 6025 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6026 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6027 | #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */ |
| 6028 | #define EXP_WORD 0x40 /* expand word in parameter expansion */ |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 6029 | #define EXP_QUOTED 0x100 /* expand word in double quotes */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6030 | #define EXP_KEEPNUL 0x200 /* do not skip NUL characters */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6031 | #define EXP_DISCARD 0x400 /* discard result of expansion */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6032 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6033 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6034 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6035 | */ |
| 6036 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 6037 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6038 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 6039 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 6040 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6041 | /* Add CTLESC when necessary. */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6042 | #define QUOTES_ESC (EXP_FULL | EXP_CASE) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6043 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6044 | /* |
| 6045 | * Structure specifying which parts of the string should be searched |
| 6046 | * for IFS characters. |
| 6047 | */ |
| 6048 | struct ifsregion { |
| 6049 | struct ifsregion *next; /* next region in list */ |
| 6050 | int begoff; /* offset of start of region */ |
| 6051 | int endoff; /* offset of end of region */ |
| 6052 | int nulonly; /* search for nul bytes only */ |
| 6053 | }; |
| 6054 | |
| 6055 | struct arglist { |
| 6056 | struct strlist *list; |
| 6057 | struct strlist **lastp; |
| 6058 | }; |
| 6059 | |
| 6060 | /* output of current string */ |
| 6061 | static char *expdest; |
| 6062 | /* list of back quote expressions */ |
| 6063 | static struct nodelist *argbackq; |
| 6064 | /* first struct in list of ifs regions */ |
| 6065 | static struct ifsregion ifsfirst; |
| 6066 | /* last struct in list */ |
| 6067 | static struct ifsregion *ifslastp; |
| 6068 | /* holds expanded arg list */ |
| 6069 | static struct arglist exparg; |
| 6070 | |
| 6071 | /* |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6072 | * Break the argument string into pieces based upon IFS and add the |
| 6073 | * strings to the argument list. The regions of the string to be |
| 6074 | * searched for IFS characters have been stored by recordregion. |
| 6075 | */ |
| 6076 | static void |
| 6077 | ifsbreakup(char *string, struct arglist *arglist) |
| 6078 | { |
| 6079 | struct ifsregion *ifsp; |
| 6080 | struct strlist *sp; |
| 6081 | char *start; |
| 6082 | char *p; |
| 6083 | char *q; |
| 6084 | const char *ifs, *realifs; |
| 6085 | int ifsspc; |
| 6086 | int nulonly; |
| 6087 | |
| 6088 | start = string; |
| 6089 | if (ifslastp != NULL) { |
| 6090 | ifsspc = 0; |
| 6091 | nulonly = 0; |
| 6092 | realifs = ifsset() ? ifsval() : defifs; |
| 6093 | ifsp = &ifsfirst; |
| 6094 | do { |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6095 | int afternul; |
| 6096 | |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6097 | p = string + ifsp->begoff; |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6098 | afternul = nulonly; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6099 | nulonly = ifsp->nulonly; |
| 6100 | ifs = nulonly ? nullstr : realifs; |
| 6101 | ifsspc = 0; |
| 6102 | while (p < string + ifsp->endoff) { |
| 6103 | q = p; |
| 6104 | if ((unsigned char)*p == CTLESC) |
| 6105 | p++; |
| 6106 | if (!strchr(ifs, *p)) { |
| 6107 | p++; |
| 6108 | continue; |
| 6109 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6110 | if (!(afternul || nulonly)) |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6111 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6112 | /* Ignore IFS whitespace at start */ |
| 6113 | if (q == start && ifsspc) { |
| 6114 | p++; |
| 6115 | start = p; |
| 6116 | continue; |
| 6117 | } |
| 6118 | *q = '\0'; |
| 6119 | sp = stzalloc(sizeof(*sp)); |
| 6120 | sp->text = start; |
| 6121 | *arglist->lastp = sp; |
| 6122 | arglist->lastp = &sp->next; |
| 6123 | p++; |
| 6124 | if (!nulonly) { |
| 6125 | for (;;) { |
| 6126 | if (p >= string + ifsp->endoff) { |
| 6127 | break; |
| 6128 | } |
| 6129 | q = p; |
| 6130 | if ((unsigned char)*p == CTLESC) |
| 6131 | p++; |
| 6132 | if (strchr(ifs, *p) == NULL) { |
| 6133 | p = q; |
| 6134 | break; |
| 6135 | } |
| 6136 | if (strchr(defifs, *p) == NULL) { |
| 6137 | if (ifsspc) { |
| 6138 | p++; |
| 6139 | ifsspc = 0; |
| 6140 | } else { |
| 6141 | p = q; |
| 6142 | break; |
| 6143 | } |
| 6144 | } else |
| 6145 | p++; |
| 6146 | } |
| 6147 | } |
| 6148 | start = p; |
| 6149 | } /* while */ |
| 6150 | ifsp = ifsp->next; |
| 6151 | } while (ifsp != NULL); |
| 6152 | if (nulonly) |
| 6153 | goto add; |
| 6154 | } |
| 6155 | |
| 6156 | if (!*start) |
| 6157 | return; |
| 6158 | |
| 6159 | add: |
| 6160 | sp = stzalloc(sizeof(*sp)); |
| 6161 | sp->text = start; |
| 6162 | *arglist->lastp = sp; |
| 6163 | arglist->lastp = &sp->next; |
| 6164 | } |
| 6165 | |
| 6166 | static void |
| 6167 | ifsfree(void) |
| 6168 | { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6169 | struct ifsregion *p = ifsfirst.next; |
| 6170 | |
| 6171 | if (!p) |
| 6172 | goto out; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6173 | |
| 6174 | INT_OFF; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6175 | do { |
| 6176 | struct ifsregion *ifsp; |
| 6177 | ifsp = p->next; |
| 6178 | free(p); |
| 6179 | p = ifsp; |
| 6180 | } while (p); |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6181 | ifsfirst.next = NULL; |
| 6182 | INT_ON; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6183 | out: |
| 6184 | ifslastp = NULL; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6185 | } |
| 6186 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6187 | static size_t |
| 6188 | esclen(const char *start, const char *p) |
| 6189 | { |
| 6190 | size_t esc = 0; |
| 6191 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6192 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6193 | esc++; |
| 6194 | } |
| 6195 | return esc; |
| 6196 | } |
| 6197 | |
| 6198 | /* |
| 6199 | * Remove any CTLESC characters from a string. |
| 6200 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6201 | #if !BASH_PATTERN_SUBST |
| 6202 | #define rmescapes(str, flag, slash_position) \ |
| 6203 | rmescapes(str, flag) |
| 6204 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6205 | static char * |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6206 | rmescapes(char *str, int flag, int *slash_position) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6207 | { |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6208 | static const char qchars[] ALIGN1 = { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6209 | IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 6210 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6211 | char *p, *q, *r; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6212 | unsigned protect_against_glob; |
| 6213 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6214 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6215 | p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash_position)); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6216 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6217 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6218 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6219 | q = p; |
| 6220 | r = str; |
| 6221 | if (flag & RMESCAPE_ALLOC) { |
| 6222 | size_t len = p - str; |
| 6223 | size_t fulllen = len + strlen(p) + 1; |
| 6224 | |
| 6225 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6226 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6227 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6228 | /* p and str may be invalidated by makestrspace */ |
| 6229 | str = (char *)stackblock() + strloc; |
| 6230 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6231 | } else if (flag & RMESCAPE_HEAP) { |
| 6232 | r = ckmalloc(fulllen); |
| 6233 | } else { |
| 6234 | r = stalloc(fulllen); |
| 6235 | } |
| 6236 | q = r; |
| 6237 | if (len > 0) { |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 6238 | q = (char *)mempcpy(q, str, len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6239 | } |
| 6240 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6241 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6242 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6243 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6244 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6245 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6246 | // Note: protect_against_glob only affect whether |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6247 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6248 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6249 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6250 | continue; |
| 6251 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6252 | if (*p == '\\') { |
| 6253 | /* naked back slash */ |
| 6254 | protect_against_glob = 0; |
| 6255 | goto copy; |
| 6256 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6257 | if ((unsigned char)*p == CTLESC) { |
| 6258 | p++; |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 6259 | #if DEBUG |
| 6260 | if (*p == '\0') |
| 6261 | ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)"); |
| 6262 | #endif |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6263 | if (protect_against_glob) { |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6264 | /* |
| 6265 | * We used to trust glob() and fnmatch() to eat |
| 6266 | * superfluous escapes (\z where z has no |
| 6267 | * special meaning anyway). But this causes |
| 6268 | * bugs such as string of one greek letter rho |
Denys Vlasenko | ed79a63 | 2017-07-05 19:20:43 +0200 | [diff] [blame] | 6269 | * (unicode-encoded as two bytes "cf,81") |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6270 | * getting encoded as "cf,CTLESC,81" |
| 6271 | * and here, converted to "cf,\,81" - |
| 6272 | * which does not go well with some flavors |
Denys Vlasenko | 92b8d9c | 2017-07-05 19:13:44 +0200 | [diff] [blame] | 6273 | * of fnmatch() in unicode locales |
| 6274 | * (for example, glibc <= 2.22). |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6275 | * |
| 6276 | * Lets add "\" only on the chars which need it. |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6277 | * Testcases for less obvious chars are shown. |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6278 | */ |
| 6279 | if (*p == '*' |
| 6280 | || *p == '?' |
| 6281 | || *p == '[' |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6282 | || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */ |
| 6283 | || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */ |
| 6284 | || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */ |
| 6285 | || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6286 | /* Some libc support [^negate], that's why "^" also needs love */ |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6287 | || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6288 | ) { |
| 6289 | *q++ = '\\'; |
| 6290 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6291 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6292 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6293 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6294 | else if (slash_position && p == str + *slash_position) { |
| 6295 | /* stop handling globbing */ |
| 6296 | globbing = 0; |
| 6297 | *slash_position = q - r; |
| 6298 | slash_position = NULL; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6299 | } |
| 6300 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6301 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6302 | copy: |
| 6303 | *q++ = *p++; |
| 6304 | } |
| 6305 | *q = '\0'; |
| 6306 | if (flag & RMESCAPE_GROW) { |
| 6307 | expdest = r; |
| 6308 | STADJUST(q - r + 1, expdest); |
| 6309 | } |
| 6310 | return r; |
| 6311 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6312 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 6313 | |
| 6314 | /* |
| 6315 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 6316 | * |
| 6317 | * Returns an stalloced string. |
| 6318 | */ |
| 6319 | static char * |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6320 | preglob(const char *pattern, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6321 | { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6322 | return rmescapes((char *)pattern, flag | RMESCAPE_GLOB, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
| 6325 | /* |
| 6326 | * Put a string on the stack. |
| 6327 | */ |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6328 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6329 | memtodest(const char *p, size_t len, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6330 | { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6331 | int syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6332 | char *q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6333 | char *s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6334 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6335 | if (!len) |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6336 | return 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6337 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6338 | q = makestrspace(len * 2, expdest); |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6339 | s = q; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6340 | |
| 6341 | do { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6342 | unsigned char c = *p++; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6343 | if (c) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6344 | if (flags & QUOTES_ESC) { |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6345 | int n = SIT(c, syntax); |
| 6346 | if (n == CCTL |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6347 | || ((flags & EXP_QUOTED) && n == CBACK) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6348 | ) { |
| 6349 | USTPUTC(CTLESC, q); |
| 6350 | } |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 6351 | } |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6352 | } else if (!(flags & EXP_KEEPNUL)) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6353 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6354 | USTPUTC(c, q); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6355 | } while (--len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6356 | |
| 6357 | expdest = q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6358 | return q - s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6361 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6362 | strtodest(const char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6363 | { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6364 | size_t len = strlen(p); |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6365 | memtodest(p, len, flags); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6366 | return len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | /* |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6370 | * Our own itoa(). |
| 6371 | * cvtnum() is used even if math support is off (to prepare $? values and such). |
| 6372 | */ |
| 6373 | static int |
| 6374 | cvtnum(arith_t num, int flags) |
| 6375 | { |
| 6376 | /* 32-bit and wider ints require buffer size of bytes*3 (or less) */ |
| 6377 | /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */ |
| 6378 | int len = (sizeof(arith_t) >= 4) ? sizeof(arith_t) * 3 : sizeof(arith_t) * 3 + 2; |
| 6379 | char buf[len]; |
| 6380 | |
| 6381 | len = fmtstr(buf, len, ARITH_FMT, num); |
| 6382 | return memtodest(buf, len, flags); |
| 6383 | } |
| 6384 | |
| 6385 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6386 | * Record the fact that we have to scan this region of the |
| 6387 | * string for IFS characters. |
| 6388 | */ |
| 6389 | static void |
| 6390 | recordregion(int start, int end, int nulonly) |
| 6391 | { |
| 6392 | struct ifsregion *ifsp; |
| 6393 | |
| 6394 | if (ifslastp == NULL) { |
| 6395 | ifsp = &ifsfirst; |
| 6396 | } else { |
| 6397 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6398 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 6399 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6400 | ifslastp->next = ifsp; |
| 6401 | INT_ON; |
| 6402 | } |
| 6403 | ifslastp = ifsp; |
| 6404 | ifslastp->begoff = start; |
| 6405 | ifslastp->endoff = end; |
| 6406 | ifslastp->nulonly = nulonly; |
| 6407 | } |
| 6408 | |
| 6409 | static void |
| 6410 | removerecordregions(int endoff) |
| 6411 | { |
| 6412 | if (ifslastp == NULL) |
| 6413 | return; |
| 6414 | |
| 6415 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6416 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6417 | struct ifsregion *ifsp; |
| 6418 | INT_OFF; |
| 6419 | ifsp = ifsfirst.next->next; |
| 6420 | free(ifsfirst.next); |
| 6421 | ifsfirst.next = ifsp; |
| 6422 | INT_ON; |
| 6423 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6424 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6425 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6426 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6427 | ifslastp = &ifsfirst; |
| 6428 | ifsfirst.endoff = endoff; |
| 6429 | } |
| 6430 | return; |
| 6431 | } |
| 6432 | |
| 6433 | ifslastp = &ifsfirst; |
| 6434 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6435 | ifslastp = ifslastp->next; |
| 6436 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6437 | struct ifsregion *ifsp; |
| 6438 | INT_OFF; |
| 6439 | ifsp = ifslastp->next->next; |
| 6440 | free(ifslastp->next); |
| 6441 | ifslastp->next = ifsp; |
| 6442 | INT_ON; |
| 6443 | } |
| 6444 | if (ifslastp->endoff > endoff) |
| 6445 | ifslastp->endoff = endoff; |
| 6446 | } |
| 6447 | |
| 6448 | static char * |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6449 | exptilde(char *startp, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6450 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6451 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6452 | char *name; |
| 6453 | struct passwd *pw; |
| 6454 | const char *home; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6455 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6456 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6457 | p = startp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6458 | name = p + 1; |
| 6459 | |
| 6460 | while ((c = *++p) != '\0') { |
| 6461 | switch (c) { |
| 6462 | case CTLESC: |
| 6463 | return startp; |
| 6464 | case CTLQUOTEMARK: |
| 6465 | return startp; |
| 6466 | case ':': |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6467 | if (flag & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6468 | goto done; |
| 6469 | break; |
| 6470 | case '/': |
| 6471 | case CTLENDVAR: |
| 6472 | goto done; |
| 6473 | } |
| 6474 | } |
| 6475 | done: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6476 | if (flag & EXP_DISCARD) |
| 6477 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6478 | *p = '\0'; |
| 6479 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 6480 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6481 | } else { |
| 6482 | pw = getpwnam(name); |
| 6483 | if (pw == NULL) |
| 6484 | goto lose; |
| 6485 | home = pw->pw_dir; |
| 6486 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6487 | *p = c; |
Denys Vlasenko | e880b1f | 2020-02-16 18:31:05 +0100 | [diff] [blame] | 6488 | if (!home) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6489 | goto lose; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6490 | strtodest(home, flag | EXP_QUOTED); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6491 | out: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6492 | return p; |
| 6493 | lose: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6494 | return startp; |
| 6495 | } |
| 6496 | |
| 6497 | /* |
| 6498 | * Execute a command inside back quotes. If it's a builtin command, we |
| 6499 | * want to save its output in a block obtained from malloc. Otherwise |
| 6500 | * we fork off a subprocess and get the output of the command via a pipe. |
| 6501 | * Should be called with interrupts off. |
| 6502 | */ |
| 6503 | struct backcmd { /* result of evalbackcmd */ |
| 6504 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6505 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 6506 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6507 | struct job *jp; /* job structure for command */ |
| 6508 | }; |
| 6509 | |
| 6510 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 6511 | /* flags in argument to evaltree */ |
| 6512 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 6513 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 6514 | static int evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6515 | |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6516 | /* An evaltree() which is known to never return. |
| 6517 | * Used to use an alias: |
| 6518 | * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__)); |
| 6519 | * but clang was reported to "transfer" noreturn-ness to evaltree() as well. |
| 6520 | */ |
| 6521 | static ALWAYS_INLINE NORETURN void |
| 6522 | evaltreenr(union node *n, int flags) |
| 6523 | { |
| 6524 | evaltree(n, flags); |
| 6525 | bb_unreachable(abort()); |
| 6526 | /* NOTREACHED */ |
| 6527 | } |
| 6528 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6529 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6530 | evalbackcmd(union node *n, struct backcmd *result) |
| 6531 | { |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6532 | int pip[2]; |
| 6533 | struct job *jp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6534 | |
| 6535 | result->fd = -1; |
| 6536 | result->buf = NULL; |
| 6537 | result->nleft = 0; |
| 6538 | result->jp = NULL; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6539 | if (n == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6540 | goto out; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6541 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6542 | |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6543 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 6544 | ash_msg_and_raise_perror("can't create pipe"); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6545 | jp = makejob(/*n,*/ 1); |
| 6546 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6547 | /* child */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6548 | FORCE_INT_ON; |
| 6549 | close(pip[0]); |
| 6550 | if (pip[1] != 1) { |
| 6551 | /*close(1);*/ |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 6552 | dup2_or_raise(pip[1], 1); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6553 | close(pip[1]); |
| 6554 | } |
Denys Vlasenko | 960ca38 | 2016-10-25 18:12:15 +0200 | [diff] [blame] | 6555 | /* TODO: eflag clearing makes the following not abort: |
| 6556 | * ash -c 'set -e; z=$(false;echo foo); echo $z' |
| 6557 | * which is what bash does (unless it is in POSIX mode). |
| 6558 | * dash deleted "eflag = 0" line in the commit |
| 6559 | * Date: Mon, 28 Jun 2010 17:11:58 +1000 |
| 6560 | * [EVAL] Don't clear eflag in evalbackcmd |
| 6561 | * For now, preserve bash-like behavior, it seems to be somewhat more useful: |
| 6562 | */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6563 | eflag = 0; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6564 | ifsfree(); |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6565 | evaltreenr(n, EV_EXIT); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6566 | /* NOTREACHED */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6567 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6568 | /* parent */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6569 | close(pip[1]); |
| 6570 | result->fd = pip[0]; |
| 6571 | result->jp = jp; |
| 6572 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6573 | out: |
| 6574 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 6575 | result->fd, result->buf, result->nleft, result->jp)); |
| 6576 | } |
| 6577 | |
| 6578 | /* |
| 6579 | * Expand stuff in backwards quotes. |
| 6580 | */ |
| 6581 | static void |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6582 | expbackq(union node *cmd, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6583 | { |
| 6584 | struct backcmd in; |
| 6585 | int i; |
| 6586 | char buf[128]; |
| 6587 | char *p; |
| 6588 | char *dest; |
| 6589 | int startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6590 | struct stackmark smark; |
| 6591 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6592 | if (flag & EXP_DISCARD) |
| 6593 | goto out; |
| 6594 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6595 | INT_OFF; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 6596 | startloc = expdest - (char *)stackblock(); |
| 6597 | pushstackmark(&smark, startloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6598 | evalbackcmd(cmd, &in); |
| 6599 | popstackmark(&smark); |
| 6600 | |
| 6601 | p = in.buf; |
| 6602 | i = in.nleft; |
| 6603 | if (i == 0) |
| 6604 | goto read; |
| 6605 | for (;;) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6606 | memtodest(p, i, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6607 | read: |
| 6608 | if (in.fd < 0) |
| 6609 | break; |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 6610 | i = nonblock_immune_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6611 | TRACE(("expbackq: read returns %d\n", i)); |
| 6612 | if (i <= 0) |
| 6613 | break; |
| 6614 | p = buf; |
| 6615 | } |
| 6616 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 6617 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6618 | if (in.fd >= 0) { |
| 6619 | close(in.fd); |
| 6620 | back_exitstatus = waitforjob(in.jp); |
| 6621 | } |
| 6622 | INT_ON; |
| 6623 | |
| 6624 | /* Eat all trailing newlines */ |
| 6625 | dest = expdest; |
Denys Vlasenko | 9ee5892 | 2020-02-17 10:24:32 +0100 | [diff] [blame] | 6626 | for (; dest > ((char *)stackblock() + startloc) && dest[-1] == '\n';) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6627 | STUNPUTC(dest); |
| 6628 | expdest = dest; |
| 6629 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6630 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6631 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6632 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 6633 | (int)((dest - (char *)stackblock()) - startloc), |
| 6634 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6635 | stackblock() + startloc)); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6636 | |
| 6637 | out: |
| 6638 | argbackq = argbackq->next; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6639 | } |
| 6640 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6641 | /* expari needs it */ |
| 6642 | static char *argstr(char *p, int flag); |
| 6643 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6644 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6645 | /* |
| 6646 | * Expand arithmetic expression. Backup to start of expression, |
| 6647 | * evaluate, place result in (backed up) result, adjust string position. |
| 6648 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6649 | static char * |
| 6650 | expari(char *start, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6651 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6652 | struct stackmark sm; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6653 | int begoff; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6654 | int endoff; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6655 | int len; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6656 | arith_t result; |
| 6657 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6658 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6659 | p = stackblock(); |
| 6660 | begoff = expdest - p; |
| 6661 | p = argstr(start, flag & EXP_DISCARD); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6662 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6663 | if (flag & EXP_DISCARD) |
| 6664 | goto out; |
| 6665 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6666 | start = stackblock(); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6667 | endoff = expdest - start; |
| 6668 | start += begoff; |
| 6669 | STADJUST(start - expdest, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6670 | |
| 6671 | removerecordregions(begoff); |
| 6672 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6673 | if (flag & QUOTES_ESC) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6674 | rmescapes(start, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6675 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6676 | pushstackmark(&sm, endoff); |
| 6677 | result = ash_arith(start); |
| 6678 | popstackmark(&sm); |
| 6679 | |
| 6680 | len = cvtnum(result, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6681 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6682 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6683 | recordregion(begoff, begoff + len, 0); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6684 | |
| 6685 | out: |
| 6686 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6687 | } |
| 6688 | #endif |
| 6689 | |
| 6690 | /* argstr needs it */ |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 6691 | static char *evalvar(char *p, int flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6692 | |
| 6693 | /* |
| 6694 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 6695 | * characters to allow for further processing. Otherwise treat |
| 6696 | * $@ like $* since no splitting will be performed. |
| 6697 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6698 | static char * |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6699 | argstr(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6700 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6701 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6702 | '=', |
| 6703 | ':', |
| 6704 | CTLQUOTEMARK, |
| 6705 | CTLENDVAR, |
| 6706 | CTLESC, |
| 6707 | CTLVAR, |
| 6708 | CTLBACKQ, |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6709 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6710 | CTLARI, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6711 | CTLENDARI, |
| 6712 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6713 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6714 | }; |
| 6715 | const char *reject = spclchars; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6716 | int breakall = (flag & (EXP_WORD | EXP_QUOTED)) == EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6717 | int inquotes; |
| 6718 | size_t length; |
| 6719 | int startloc; |
| 6720 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6721 | reject += !!(flag & EXP_VARTILDE2); |
| 6722 | reject += flag & EXP_VARTILDE ? 0 : 2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6723 | inquotes = 0; |
| 6724 | length = 0; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6725 | if (flag & EXP_TILDE) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6726 | flag &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6727 | tilde: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6728 | if (*p == '~') |
| 6729 | p = exptilde(p, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6730 | } |
| 6731 | start: |
| 6732 | startloc = expdest - (char *)stackblock(); |
| 6733 | for (;;) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6734 | int end; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6735 | unsigned char c; |
| 6736 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6737 | length += strcspn(p + length, reject); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6738 | end = 0; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6739 | c = p[length]; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6740 | if (!(c & 0x80) |
| 6741 | IF_FEATURE_SH_MATH(|| c == CTLENDARI) |
| 6742 | || c == CTLENDVAR |
| 6743 | ) { |
| 6744 | /* |
| 6745 | * c == '=' || c == ':' || c == '\0' || |
| 6746 | * c == CTLENDARI || c == CTLENDVAR |
| 6747 | */ |
| 6748 | length++; |
| 6749 | /* c == '\0' || c == CTLENDARI || c == CTLENDVAR */ |
| 6750 | end = !!((c - 1) & 0x80); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6751 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6752 | if (length > 0 && !(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6753 | int newloc; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6754 | char *q; |
| 6755 | |
| 6756 | q = stnputs(p, length, expdest); |
| 6757 | q[-1] &= end - 1; |
| 6758 | expdest = q - (flag & EXP_WORD ? end : 0); |
| 6759 | newloc = q - (char *)stackblock() - end; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6760 | if (breakall && !inquotes && newloc > startloc) { |
| 6761 | recordregion(startloc, newloc, 0); |
| 6762 | } |
| 6763 | startloc = newloc; |
| 6764 | } |
| 6765 | p += length + 1; |
| 6766 | length = 0; |
| 6767 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6768 | if (end) |
| 6769 | break; |
| 6770 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6771 | switch (c) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6772 | case '=': |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6773 | flag |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6774 | reject++; |
| 6775 | /* fall through */ |
| 6776 | case ':': |
| 6777 | /* |
| 6778 | * sort of a hack - expand tildes in variable |
| 6779 | * assignments (after the first '=' and after ':'s). |
| 6780 | */ |
| 6781 | if (*--p == '~') { |
| 6782 | goto tilde; |
| 6783 | } |
| 6784 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6785 | case CTLQUOTEMARK: |
| 6786 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6787 | if (!inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6788 | p = evalvar(p + 1, flag | EXP_QUOTED) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6789 | goto start; |
| 6790 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6791 | inquotes ^= EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6792 | addquote: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6793 | if (flag & QUOTES_ESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6794 | p--; |
| 6795 | length++; |
| 6796 | startloc++; |
| 6797 | } |
| 6798 | break; |
| 6799 | case CTLESC: |
| 6800 | startloc++; |
| 6801 | length++; |
| 6802 | goto addquote; |
| 6803 | case CTLVAR: |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6804 | TRACE(("argstr: evalvar('%s')\n", p)); |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6805 | p = evalvar(p, flag | inquotes); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6806 | TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock())); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6807 | goto start; |
| 6808 | case CTLBACKQ: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6809 | expbackq(argbackq->n, flag | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6810 | goto start; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6811 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6812 | case CTLARI: |
| 6813 | p = expari(p, flag | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6814 | goto start; |
| 6815 | #endif |
| 6816 | } |
| 6817 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6818 | return p - 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6822 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6823 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6824 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6825 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6826 | char c; |
| 6827 | |
| 6828 | loc = startp; |
| 6829 | loc2 = rmesc; |
| 6830 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6831 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6832 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6833 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6834 | c = *loc2; |
| 6835 | if (zero) { |
| 6836 | *loc2 = '\0'; |
| 6837 | s = rmesc; |
| 6838 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6839 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6840 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6841 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6842 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6843 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6844 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6845 | loc++; |
| 6846 | loc++; |
| 6847 | loc2++; |
| 6848 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6849 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6850 | } |
| 6851 | |
| 6852 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6853 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6854 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6855 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6856 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6857 | int try2optimize = match_at_start; |
| 6858 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6859 | int esc = 0; |
| 6860 | char *loc; |
| 6861 | char *loc2; |
| 6862 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6863 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6864 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6865 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6866 | * Logic: |
| 6867 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6868 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6869 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6870 | */ |
| 6871 | /* TODO: document in what other circumstances we are called. */ |
| 6872 | |
| 6873 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6874 | int match; |
| 6875 | char c = *loc2; |
| 6876 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6877 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6878 | *loc2 = '\0'; |
| 6879 | s = rmesc; |
| 6880 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6881 | match = pmatch(pattern, s); |
| 6882 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6883 | *loc2 = c; |
| 6884 | if (match) |
| 6885 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6886 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6887 | if (try2optimize) { |
| 6888 | /* Maybe we can optimize this: |
| 6889 | * if pattern ends with unescaped *, we can avoid checking |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6890 | * shorter strings: if "foo*" doesn't match "raw_value_of_v", |
| 6891 | * it won't match truncated "raw_value_of_" strings too. |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6892 | */ |
| 6893 | unsigned plen = strlen(pattern); |
| 6894 | /* Does it end with "*"? */ |
| 6895 | if (plen != 0 && pattern[--plen] == '*') { |
| 6896 | /* "xxxx*" is not escaped */ |
| 6897 | /* "xxx\*" is escaped */ |
| 6898 | /* "xx\\*" is not escaped */ |
| 6899 | /* "x\\\*" is escaped */ |
| 6900 | int slashes = 0; |
| 6901 | while (plen != 0 && pattern[--plen] == '\\') |
| 6902 | slashes++; |
| 6903 | if (!(slashes & 1)) |
| 6904 | break; /* ends with unescaped "*" */ |
| 6905 | } |
| 6906 | try2optimize = 0; |
| 6907 | } |
| 6908 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6909 | loc--; |
| 6910 | if (quotes) { |
| 6911 | if (--esc < 0) { |
| 6912 | esc = esclen(startp, loc); |
| 6913 | } |
| 6914 | if (esc % 2) { |
| 6915 | esc--; |
| 6916 | loc--; |
| 6917 | } |
| 6918 | } |
| 6919 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6920 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6923 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6924 | static void |
| 6925 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6926 | { |
| 6927 | const char *msg; |
| 6928 | const char *tail; |
| 6929 | |
| 6930 | tail = nullstr; |
| 6931 | msg = "parameter not set"; |
| 6932 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6933 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6934 | if (varflags & VSNUL) |
| 6935 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6936 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6937 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6938 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6939 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6940 | ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6943 | static char * |
| 6944 | subevalvar(char *start, char *str, int strloc, |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 6945 | int startloc, int varflags, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6946 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6947 | int subtype = varflags & VSTYPE; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6948 | int quotes = flag & QUOTES_ESC; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6949 | char *startp; |
| 6950 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6951 | char *rmesc, *rmescend; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6952 | long amount; |
| 6953 | int resetloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6954 | int argstr_flags; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6955 | IF_BASH_PATTERN_SUBST(int workloc;) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6956 | IF_BASH_PATTERN_SUBST(int slash_pos;) |
| 6957 | IF_BASH_PATTERN_SUBST(char *repl;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6958 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6959 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6960 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6961 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6962 | //bb_error_msg("subevalvar(start:'%s',str:'%s',strloc:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 6963 | // start, str, strloc, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6964 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6965 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 6966 | /* For "${v/pattern/repl}", we must find the delimiter _before_ |
| 6967 | * argstr() call expands possible variable references in pattern: |
| 6968 | * think about "v=a; a=a/; echo ${v/$a/r}" case. |
| 6969 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6970 | repl = NULL; |
| 6971 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 6972 | /* Find '/' and replace with NUL */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6973 | repl = start; |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 6974 | /* The pattern can't be empty. |
| 6975 | * IOW: if the first char after "${v//" is a slash, |
| 6976 | * it does not terminate the pattern - it's the first char of the pattern: |
| 6977 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 6978 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 6979 | */ |
| 6980 | if (*repl == '/') |
| 6981 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6982 | for (;;) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6983 | if (*repl == '\0') { |
| 6984 | repl = NULL; |
| 6985 | break; |
| 6986 | } |
| 6987 | if (*repl == '/') { |
| 6988 | *repl = '\0'; |
| 6989 | break; |
| 6990 | } |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 6991 | /* Handle escaped slashes, e.g. "${v/\//_}" (they are CTLESC'ed by this point) */ |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 6992 | if ((unsigned char)*repl == CTLESC && repl[1]) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6993 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6994 | repl++; |
| 6995 | } |
| 6996 | } |
| 6997 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6998 | argstr_flags = (flag & EXP_DISCARD) | EXP_TILDE; |
| 6999 | if (!str |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7000 | #if BASH_SUBSTR |
| 7001 | && subtype != VSSUBSTR |
| 7002 | #endif |
| 7003 | ) { |
| 7004 | /* EXP_CASE keeps CTLESC's */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7005 | argstr_flags |= EXP_CASE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7006 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7007 | p = argstr(start, argstr_flags); |
| 7008 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7009 | //bb_error_msg("str0:'%s'", (char *)stackblock() + strloc); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7010 | #if BASH_PATTERN_SUBST |
| 7011 | slash_pos = -1; |
| 7012 | if (repl) { |
| 7013 | slash_pos = expdest - ((char *)stackblock() + strloc); |
| 7014 | STPUTC('/', expdest); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7015 | //bb_error_msg("repl+1:'%s'", repl + 1); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7016 | p = argstr(repl + 1, (flag & EXP_DISCARD) | EXP_TILDE); /* EXP_TILDE: echo "${v/x/~}" expands ~ ! */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7017 | *repl = '/'; |
| 7018 | } |
| 7019 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7020 | if (flag & EXP_DISCARD) |
| 7021 | return p; |
| 7022 | |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7023 | startp = (char *)stackblock() + startloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7024 | //bb_error_msg("str1:'%s'", (char *)stackblock() + strloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7025 | |
| 7026 | switch (subtype) { |
| 7027 | case VSASSIGN: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7028 | setvar0(str, startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7029 | |
| 7030 | loc = startp; |
| 7031 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7032 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7033 | case VSQUESTION: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7034 | varunset(start, str, startp, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7035 | /* NOTREACHED */ |
| 7036 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7037 | #if BASH_SUBSTR |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7038 | case VSSUBSTR: { |
| 7039 | int pos, len, orig_len; |
| 7040 | char *colon; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7041 | char *vstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7042 | |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7043 | loc = vstr = stackblock() + strloc; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7044 | |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7045 | /* Read POS in ${var:POS:LEN} */ |
| 7046 | colon = strchr(loc, ':'); |
| 7047 | if (colon) *colon = '\0'; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7048 | pos = substr_atoi(loc); |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7049 | if (colon) *colon = ':'; |
| 7050 | |
| 7051 | /* Read LEN in ${var:POS:LEN} */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7052 | len = vstr - startp - 1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7053 | /* *loc != '\0', guaranteed by parser */ |
| 7054 | if (quotes) { |
| 7055 | char *ptr; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7056 | /* Adjust the length by the number of escapes */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7057 | for (ptr = startp; ptr < (vstr - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7058 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7059 | len--; |
| 7060 | ptr++; |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | orig_len = len; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7065 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7066 | /* ${var::LEN} */ |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7067 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7068 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7069 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7070 | len = orig_len; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7071 | while (*loc && *loc != ':') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7072 | loc++; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7073 | if (*loc++ == ':') |
| 7074 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7075 | } |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7076 | if (pos < 0) { |
| 7077 | /* ${VAR:$((-n)):l} starts n chars from the end */ |
| 7078 | pos = orig_len + pos; |
| 7079 | } |
| 7080 | if ((unsigned)pos >= orig_len) { |
| 7081 | /* apart from obvious ${VAR:999999:l}, |
| 7082 | * covers ${VAR:$((-9999999)):l} - result is "" |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7083 | * (bash compat) |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7084 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7085 | pos = 0; |
| 7086 | len = 0; |
| 7087 | } |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7088 | if (len < 0) { |
| 7089 | /* ${VAR:N:-M} sets LEN to strlen()-M */ |
| 7090 | len = (orig_len - pos) + len; |
| 7091 | } |
| 7092 | if ((unsigned)len > (orig_len - pos)) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7093 | len = orig_len - pos; |
| 7094 | |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7095 | for (vstr = startp; pos; vstr++, pos--) { |
| 7096 | if (quotes && (unsigned char)*vstr == CTLESC) |
| 7097 | vstr++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7098 | } |
| 7099 | for (loc = startp; len; len--) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7100 | if (quotes && (unsigned char)*vstr == CTLESC) |
| 7101 | *loc++ = *vstr++; |
| 7102 | *loc++ = *vstr++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7103 | } |
| 7104 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7105 | goto out; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7106 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7107 | #endif /* BASH_SUBSTR */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7108 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7109 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7110 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7111 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7112 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7113 | repl = NULL; |
| 7114 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7115 | /* We'll comeback here if we grow the stack while handling |
| 7116 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 7117 | * stack will need rebasing, and we'll need to remove our work |
| 7118 | * areas each time |
| 7119 | */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7120 | restart: |
| 7121 | #endif |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7122 | |
| 7123 | amount = expdest - ((char *)stackblock() + resetloc); |
| 7124 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7125 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7126 | |
| 7127 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7128 | rmescend = (char *)stackblock() + strloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7129 | //bb_error_msg("str7:'%s'", rmescend); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7130 | if (quotes) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7131 | //TODO: how to handle slash_pos here if string changes (shortens?) |
| 7132 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7133 | if (rmesc != startp) { |
| 7134 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7135 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7136 | } |
| 7137 | } |
| 7138 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7139 | str = (char *)stackblock() + strloc; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7140 | /* |
| 7141 | * Example: v='a\bc'; echo ${v/\\b/_\\_\z_} |
| 7142 | * The result is a_\_z_c (not a\_\_z_c)! |
| 7143 | * |
| 7144 | * The search pattern and replace string treat backslashes differently! |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7145 | * "&slash_pos" causes rmescapes() to work differently on the pattern |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7146 | * and string. It's only used on the first call. |
| 7147 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7148 | //bb_error_msg("str8:'%s' slash_pos:%d", str, slash_pos); |
| 7149 | rmescapes(str, RMESCAPE_GLOB, |
| 7150 | repl ? NULL : (slash_pos < 0 ? NULL : &slash_pos) |
| 7151 | ); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7152 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7153 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 7154 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7155 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7156 | int len; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7157 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7158 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 7159 | if (!repl) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7160 | //bb_error_msg("str9:'%s' slash_pos:%d", str, slash_pos); |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 7161 | repl = nullstr; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7162 | if (slash_pos >= 0) { |
| 7163 | repl = str + slash_pos; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7164 | *repl++ = '\0'; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7165 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7166 | } |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7167 | //bb_error_msg("str:'%s' repl:'%s'", str, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7168 | |
| 7169 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7170 | if (str[0] == '\0') |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7171 | goto out1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7172 | |
| 7173 | len = 0; |
| 7174 | idx = startp; |
| 7175 | end = str - 1; |
| 7176 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7177 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7178 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7179 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7180 | if (!loc) { |
| 7181 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7182 | char *restart_detect = stackblock(); |
| 7183 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7184 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7185 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7186 | idx++; |
| 7187 | len++; |
| 7188 | STPUTC(*idx, expdest); |
| 7189 | } |
| 7190 | if (stackblock() != restart_detect) |
| 7191 | goto restart; |
| 7192 | idx++; |
| 7193 | len++; |
| 7194 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7195 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 7196 | if (idx >= end) |
| 7197 | break; |
| 7198 | if (str[0] == '*') { |
| 7199 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 7200 | * it would never match "ong_string" etc, no point in trying. |
| 7201 | */ |
| 7202 | goto skip_matching; |
| 7203 | } |
| 7204 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | if (subtype == VSREPLACEALL) { |
| 7208 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7209 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7210 | idx++; |
| 7211 | idx++; |
| 7212 | rmesc++; |
| 7213 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7214 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7215 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7216 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7217 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7218 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7219 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7220 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7221 | if (quotes && *loc == '\\') { |
| 7222 | STPUTC(CTLESC, expdest); |
| 7223 | len++; |
| 7224 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7225 | STPUTC(*loc, expdest); |
| 7226 | if (stackblock() != restart_detect) |
| 7227 | goto restart; |
| 7228 | len++; |
| 7229 | } |
| 7230 | |
| 7231 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 7232 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7233 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7234 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7235 | STPUTC(*idx, expdest); |
| 7236 | if (stackblock() != restart_detect) |
| 7237 | goto restart; |
| 7238 | len++; |
| 7239 | idx++; |
| 7240 | } |
| 7241 | break; |
| 7242 | } |
| 7243 | } |
| 7244 | |
| 7245 | /* We've put the replaced text into a buffer at workloc, now |
| 7246 | * move it to the right place and adjust the stack. |
| 7247 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7248 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7249 | startp = (char *)stackblock() + startloc; |
| 7250 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7251 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7252 | loc = startp + len; |
| 7253 | goto out; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7254 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7255 | #endif /* BASH_PATTERN_SUBST */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7256 | |
| 7257 | subtype -= VSTRIMRIGHT; |
| 7258 | #if DEBUG |
| 7259 | if (subtype < 0 || subtype > 7) |
| 7260 | abort(); |
| 7261 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7262 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7263 | zero = subtype >> 1; |
| 7264 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 7265 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 7266 | |
| 7267 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 7268 | if (loc) { |
| 7269 | if (zero) { |
| 7270 | memmove(startp, loc, str - loc); |
| 7271 | loc = startp + (str - loc) - 1; |
| 7272 | } |
| 7273 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7274 | } else |
| 7275 | loc = str - 1; |
| 7276 | |
| 7277 | out: |
| 7278 | amount = loc - expdest; |
| 7279 | STADJUST(amount, expdest); |
| 7280 | out1: |
| 7281 | /* Remove any recorded regions beyond start of variable */ |
| 7282 | removerecordregions(startloc); |
| 7283 | |
| 7284 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
| 7287 | /* |
| 7288 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7289 | * name parameter (examples): |
| 7290 | * ash -c 'echo $1' name:'1=' |
| 7291 | * ash -c 'echo $qwe' name:'qwe=' |
| 7292 | * ash -c 'echo $$' name:'$=' |
| 7293 | * ash -c 'echo ${$}' name:'$=' |
| 7294 | * ash -c 'echo ${$##q}' name:'$=q' |
| 7295 | * ash -c 'echo ${#$}' name:'$=' |
| 7296 | * note: examples with bad shell syntax: |
| 7297 | * ash -c 'echo ${#$1}' name:'$=1' |
| 7298 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7299 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 7300 | static NOINLINE ssize_t |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7301 | varvalue(char *name, int varflags, int flags, int quoted) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7302 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7303 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7304 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7305 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7306 | ssize_t len = 0; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7307 | int sep; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7308 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7309 | int discard = (subtype == VSPLUS || subtype == VSLENGTH) | (flags & EXP_DISCARD); |
| 7310 | |
| 7311 | if (!subtype) { |
| 7312 | if (discard) |
| 7313 | return -1; |
| 7314 | |
| 7315 | raise_error_syntax("bad substitution"); |
| 7316 | } |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7317 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7318 | flags |= EXP_KEEPNUL; |
| 7319 | flags &= discard ? ~QUOTES_ESC : ~0; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7320 | sep = (flags & EXP_FULL) << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7321 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7322 | switch (*name) { |
| 7323 | case '$': |
| 7324 | num = rootpid; |
| 7325 | goto numvar; |
| 7326 | case '?': |
| 7327 | num = exitstatus; |
| 7328 | goto numvar; |
| 7329 | case '#': |
| 7330 | num = shellparam.nparam; |
| 7331 | goto numvar; |
| 7332 | case '!': |
| 7333 | num = backgndpid; |
| 7334 | if (num == 0) |
| 7335 | return -1; |
| 7336 | numvar: |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7337 | len = cvtnum(num, flags); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7338 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7339 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7340 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7341 | for (i = NOPTS - 1; i >= 0; i--) { |
Martijn Dekker | ad4e961 | 2018-03-31 18:15:59 +0200 | [diff] [blame] | 7342 | if (optlist[i] && optletters(i)) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7343 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7344 | len++; |
| 7345 | } |
| 7346 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7347 | check_1char_name: |
| 7348 | #if 0 |
| 7349 | /* handles cases similar to ${#$1} */ |
| 7350 | if (name[2] != '\0') |
| 7351 | raise_error_syntax("bad substitution"); |
| 7352 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7353 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7354 | case '@': |
| 7355 | if (quoted && sep) |
| 7356 | goto param; |
| 7357 | /* fall through */ |
| 7358 | case '*': { |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7359 | char **ap; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7360 | char sepc; |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7361 | char c; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7362 | |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7363 | /* We will set c to 0 or ~0 depending on whether |
| 7364 | * we're doing field splitting. We won't do field |
| 7365 | * splitting if either we're quoted or sep is zero. |
| 7366 | * |
| 7367 | * Instead of testing (quoted || !sep) the following |
| 7368 | * trick optimises away any branches by using the |
| 7369 | * fact that EXP_QUOTED (which is the only bit that |
| 7370 | * can be set in quoted) is the same as EXP_FULL << |
| 7371 | * CHAR_BIT (which is the only bit that can be set |
| 7372 | * in sep). |
| 7373 | */ |
| 7374 | #if EXP_QUOTED >> CHAR_BIT != EXP_FULL |
| 7375 | #error The following two lines expect EXP_QUOTED == EXP_FULL << CHAR_BIT |
| 7376 | #endif |
| 7377 | c = !((quoted | ~sep) & EXP_QUOTED) - 1; |
| 7378 | sep &= ~quoted; |
| 7379 | sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7380 | param: |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7381 | sepc = sep; |
Denys Vlasenko | 0dd8e45 | 2016-10-01 21:02:06 +0200 | [diff] [blame] | 7382 | ap = shellparam.p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7383 | if (!ap) |
| 7384 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7385 | while ((p = *ap++) != NULL) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7386 | len += strtodest(p, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7387 | |
| 7388 | if (*ap && sep) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7389 | len++; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7390 | memtodest(&sepc, 1, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7391 | } |
| 7392 | } |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7393 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7394 | } /* case '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7395 | case '0': |
| 7396 | case '1': |
| 7397 | case '2': |
| 7398 | case '3': |
| 7399 | case '4': |
| 7400 | case '5': |
| 7401 | case '6': |
| 7402 | case '7': |
| 7403 | case '8': |
| 7404 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 7405 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7406 | if (num < 0 || num > shellparam.nparam) |
| 7407 | return -1; |
| 7408 | p = num ? shellparam.p[num - 1] : arg0; |
| 7409 | goto value; |
| 7410 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7411 | /* NB: name has form "VAR=..." */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7412 | p = lookupvar(name); |
| 7413 | value: |
| 7414 | if (!p) |
| 7415 | return -1; |
| 7416 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7417 | len = strtodest(p, flags); |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7418 | #if ENABLE_UNICODE_SUPPORT |
| 7419 | if (subtype == VSLENGTH && len > 0) { |
| 7420 | reinit_unicode_for_ash(); |
| 7421 | if (unicode_status == UNICODE_ON) { |
Ron Yorston | 3e3bfb8 | 2016-03-18 11:29:19 +0000 | [diff] [blame] | 7422 | STADJUST(-len, expdest); |
| 7423 | discard = 0; |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7424 | len = unicode_strlen(p); |
| 7425 | } |
| 7426 | } |
| 7427 | #endif |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7428 | break; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7431 | if (discard) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7432 | STADJUST(-len, expdest); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7433 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7434 | return len; |
| 7435 | } |
| 7436 | |
| 7437 | /* |
| 7438 | * Expand a variable, and return a pointer to the next character in the |
| 7439 | * input string. |
| 7440 | */ |
| 7441 | static char * |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7442 | evalvar(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7443 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7444 | char varflags; |
| 7445 | char subtype; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7446 | char *var; |
| 7447 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7448 | int startloc; |
| 7449 | ssize_t varlen; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7450 | int discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7451 | int quoted; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7452 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 7453 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7454 | subtype = varflags & VSTYPE; |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 7455 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7456 | quoted = flag & EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7457 | var = p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7458 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 7459 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7460 | |
| 7461 | again: |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7462 | varlen = varvalue(var, varflags, flag, quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7463 | if (varflags & VSNUL) |
| 7464 | varlen--; |
| 7465 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7466 | discard = varlen < 0 ? EXP_DISCARD : 0; |
| 7467 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7468 | switch (subtype) { |
| 7469 | case VSPLUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7470 | discard ^= EXP_DISCARD; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7471 | /* fall through */ |
| 7472 | case 0: |
| 7473 | case VSMINUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7474 | p = argstr(p, flag | EXP_TILDE | EXP_WORD | (discard ^ EXP_DISCARD)); |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7475 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7476 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7477 | case VSASSIGN: |
| 7478 | case VSQUESTION: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7479 | p = subevalvar(p, var, 0, startloc, varflags, |
| 7480 | (flag & ~QUOTES_ESC) | (discard ^ EXP_DISCARD)); |
| 7481 | |
| 7482 | if ((flag | ~discard) & EXP_DISCARD) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7483 | goto record; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7484 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7485 | varflags &= ~VSNUL; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7486 | subtype = VSNORMAL; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7487 | goto again; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7488 | } |
| 7489 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7490 | if ((discard & ~flag) && uflag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7491 | varunset(p, var, 0, 0); |
| 7492 | |
| 7493 | if (subtype == VSLENGTH) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7494 | p++; |
| 7495 | if (flag & EXP_DISCARD) |
| 7496 | return p; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7497 | cvtnum(varlen > 0 ? varlen : 0, flag); |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7498 | goto really_record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7499 | } |
| 7500 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7501 | if (subtype == VSNORMAL) |
| 7502 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7503 | |
| 7504 | #if DEBUG |
| 7505 | switch (subtype) { |
| 7506 | case VSTRIMLEFT: |
| 7507 | case VSTRIMLEFTMAX: |
| 7508 | case VSTRIMRIGHT: |
| 7509 | case VSTRIMRIGHTMAX: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7510 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7511 | case VSSUBSTR: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7512 | #endif |
| 7513 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7514 | case VSREPLACE: |
| 7515 | case VSREPLACEALL: |
| 7516 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7517 | break; |
| 7518 | default: |
| 7519 | abort(); |
| 7520 | } |
| 7521 | #endif |
| 7522 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7523 | flag |= discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7524 | if (!(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7525 | /* |
| 7526 | * Terminate the string and start recording the pattern |
| 7527 | * right after it |
| 7528 | */ |
| 7529 | STPUTC('\0', expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7530 | } |
| 7531 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7532 | patloc = expdest - (char *)stackblock(); |
| 7533 | p = subevalvar(p, NULL, patloc, startloc, varflags, flag); |
Denys Vlasenko | 4ace385 | 2020-02-16 18:42:50 +0100 | [diff] [blame] | 7534 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7535 | record: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7536 | if ((flag | discard) & EXP_DISCARD) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7537 | return p; |
| 7538 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7539 | really_record: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7540 | if (quoted) { |
| 7541 | quoted = *var == '@' && shellparam.nparam; |
| 7542 | if (!quoted) |
| 7543 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7544 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7545 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7546 | return p; |
| 7547 | } |
| 7548 | |
| 7549 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7550 | * Add a file name to the list. |
| 7551 | */ |
| 7552 | static void |
| 7553 | addfname(const char *name) |
| 7554 | { |
| 7555 | struct strlist *sp; |
| 7556 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7557 | sp = stzalloc(sizeof(*sp)); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 7558 | sp->text = sstrdup(name); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7559 | *exparg.lastp = sp; |
| 7560 | exparg.lastp = &sp->next; |
| 7561 | } |
| 7562 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7563 | /* Avoid glob() (and thus, stat() et al) for words like "echo" */ |
| 7564 | static int |
| 7565 | hasmeta(const char *p) |
| 7566 | { |
| 7567 | static const char chars[] ALIGN1 = { |
| 7568 | '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0 |
| 7569 | }; |
| 7570 | |
| 7571 | for (;;) { |
| 7572 | p = strpbrk(p, chars); |
| 7573 | if (!p) |
| 7574 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7575 | switch ((unsigned char)*p) { |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7576 | case CTLQUOTEMARK: |
| 7577 | for (;;) { |
| 7578 | p++; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7579 | if ((unsigned char)*p == CTLQUOTEMARK) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7580 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7581 | if ((unsigned char)*p == CTLESC) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7582 | p++; |
| 7583 | if (*p == '\0') /* huh? */ |
| 7584 | return 0; |
| 7585 | } |
| 7586 | break; |
| 7587 | case '\\': |
| 7588 | case CTLESC: |
| 7589 | p++; |
| 7590 | if (*p == '\0') |
| 7591 | return 0; |
| 7592 | break; |
| 7593 | case '[': |
| 7594 | if (!strchr(p + 1, ']')) { |
| 7595 | /* It's not a properly closed [] pattern, |
| 7596 | * but other metas may follow. Continue checking. |
| 7597 | * my[file* _is_ globbed by bash |
| 7598 | * and matches filenames like "my[file1". |
| 7599 | */ |
| 7600 | break; |
| 7601 | } |
| 7602 | /* fallthrough */ |
| 7603 | default: |
| 7604 | /* case '*': */ |
| 7605 | /* case '?': */ |
| 7606 | return 1; |
| 7607 | } |
| 7608 | p++; |
| 7609 | } |
| 7610 | |
| 7611 | return 0; |
| 7612 | } |
| 7613 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7614 | /* If we want to use glob() from libc... */ |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7615 | #if !ENABLE_ASH_INTERNAL_GLOB |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7616 | |
| 7617 | /* Add the result of glob() to the list */ |
| 7618 | static void |
| 7619 | addglob(const glob_t *pglob) |
| 7620 | { |
| 7621 | char **p = pglob->gl_pathv; |
| 7622 | |
| 7623 | do { |
| 7624 | addfname(*p); |
| 7625 | } while (*++p); |
| 7626 | } |
| 7627 | static void |
| 7628 | expandmeta(struct strlist *str /*, int flag*/) |
| 7629 | { |
| 7630 | /* TODO - EXP_REDIR */ |
| 7631 | |
| 7632 | while (str) { |
| 7633 | char *p; |
| 7634 | glob_t pglob; |
| 7635 | int i; |
| 7636 | |
| 7637 | if (fflag) |
| 7638 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7639 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7640 | if (!hasmeta(str->text)) |
| 7641 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7642 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7643 | INT_OFF; |
| 7644 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7645 | // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match |
| 7646 | // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?) |
| 7647 | // |
| 7648 | // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping: |
| 7649 | // if you pass it "file\?", it returns "file\?", not "file?", if no match. |
| 7650 | // Which means you need to unescape the string, right? Not so fast: |
| 7651 | // if there _is_ a file named "file\?" (with backslash), it is returned |
| 7652 | // as "file\?" too (whichever pattern you used to find it, say, "file*"). |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7653 | // You DON'T KNOW by looking at the result whether you need to unescape it. |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7654 | // |
| 7655 | // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?", |
| 7656 | // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file. |
| 7657 | // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match). |
| 7658 | // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly. |
| 7659 | // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob); |
| 7660 | // i = glob(p, GLOB_NOMAGIC, NULL, &pglob); |
| 7661 | i = glob(p, 0, NULL, &pglob); |
| 7662 | //bb_error_msg("glob('%s'):%d '%s'...", p, i, pglob.gl_pathv ? pglob.gl_pathv[0] : "-"); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7663 | if (p != str->text) |
| 7664 | free(p); |
| 7665 | switch (i) { |
| 7666 | case 0: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7667 | #if 0 // glibc 2.24.90 bug? Patterns like "*/file", when match, don't set GLOB_MAGCHAR |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7668 | /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */ |
| 7669 | if (!(pglob.gl_flags & GLOB_MAGCHAR)) |
| 7670 | goto nometa2; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7671 | #endif |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7672 | addglob(&pglob); |
| 7673 | globfree(&pglob); |
| 7674 | INT_ON; |
| 7675 | break; |
| 7676 | case GLOB_NOMATCH: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7677 | //nometa2: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7678 | globfree(&pglob); |
| 7679 | INT_ON; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7680 | nometa: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7681 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7682 | rmescapes(str->text, 0, NULL); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7683 | exparg.lastp = &str->next; |
| 7684 | break; |
| 7685 | default: /* GLOB_NOSPACE */ |
| 7686 | globfree(&pglob); |
| 7687 | INT_ON; |
| 7688 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 7689 | } |
| 7690 | str = str->next; |
| 7691 | } |
| 7692 | } |
| 7693 | |
| 7694 | #else |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7695 | /* ENABLE_ASH_INTERNAL_GLOB: Homegrown globbing code. (dash also has both, uses homegrown one.) */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7696 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7697 | /* |
| 7698 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 7699 | */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7700 | typedef struct exp_t { |
| 7701 | char *dir; |
| 7702 | unsigned dir_max; |
| 7703 | } exp_t; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7704 | static void |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7705 | expmeta(exp_t *exp, char *name, unsigned name_len, unsigned expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7706 | { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7707 | #define expdir exp->dir |
| 7708 | #define expdir_max exp->dir_max |
| 7709 | char *enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7710 | char *p; |
| 7711 | const char *cp; |
| 7712 | char *start; |
| 7713 | char *endname; |
| 7714 | int metaflag; |
| 7715 | struct stat statb; |
| 7716 | DIR *dirp; |
| 7717 | struct dirent *dp; |
| 7718 | int atend; |
| 7719 | int matchdot; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7720 | int esc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7721 | |
| 7722 | metaflag = 0; |
| 7723 | start = name; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7724 | for (p = name; esc = 0, *p; p += esc + 1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7725 | if (*p == '*' || *p == '?') |
| 7726 | metaflag = 1; |
| 7727 | else if (*p == '[') { |
| 7728 | char *q = p + 1; |
| 7729 | if (*q == '!') |
| 7730 | q++; |
| 7731 | for (;;) { |
| 7732 | if (*q == '\\') |
| 7733 | q++; |
| 7734 | if (*q == '/' || *q == '\0') |
| 7735 | break; |
| 7736 | if (*++q == ']') { |
| 7737 | metaflag = 1; |
| 7738 | break; |
| 7739 | } |
| 7740 | } |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7741 | } else { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7742 | if (*p == '\\' && p[1]) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7743 | esc++; |
| 7744 | if (p[esc] == '/') { |
| 7745 | if (metaflag) |
| 7746 | break; |
| 7747 | start = p + esc + 1; |
| 7748 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7749 | } |
| 7750 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7751 | if (metaflag == 0) { /* we've reached the end of the file name */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7752 | if (!expdir_len) |
| 7753 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7754 | p = name; |
| 7755 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7756 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7757 | p++; |
| 7758 | *enddir++ = *p; |
| 7759 | } while (*p++); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7760 | if (lstat(expdir, &statb) == 0) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7761 | addfname(expdir); |
| 7762 | return; |
| 7763 | } |
| 7764 | endname = p; |
| 7765 | if (name < start) { |
| 7766 | p = name; |
| 7767 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7768 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7769 | p++; |
| 7770 | *enddir++ = *p++; |
| 7771 | } while (p < start); |
| 7772 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7773 | *enddir = '\0'; |
| 7774 | cp = expdir; |
| 7775 | expdir_len = enddir - cp; |
| 7776 | if (!expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7777 | cp = "."; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7778 | dirp = opendir(cp); |
| 7779 | if (dirp == NULL) |
| 7780 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7781 | if (*endname == 0) { |
| 7782 | atend = 1; |
| 7783 | } else { |
| 7784 | atend = 0; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7785 | *endname = '\0'; |
| 7786 | endname += esc + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7787 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7788 | name_len -= endname - name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7789 | matchdot = 0; |
| 7790 | p = start; |
| 7791 | if (*p == '\\') |
| 7792 | p++; |
| 7793 | if (*p == '.') |
| 7794 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7795 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7796 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7797 | continue; |
| 7798 | if (pmatch(start, dp->d_name)) { |
| 7799 | if (atend) { |
| 7800 | strcpy(enddir, dp->d_name); |
| 7801 | addfname(expdir); |
| 7802 | } else { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7803 | unsigned offset; |
| 7804 | unsigned len; |
| 7805 | |
| 7806 | p = stpcpy(enddir, dp->d_name); |
| 7807 | *p = '/'; |
| 7808 | |
| 7809 | offset = p - expdir + 1; |
| 7810 | len = offset + name_len + NAME_MAX; |
| 7811 | if (len > expdir_max) { |
| 7812 | len += PATH_MAX; |
| 7813 | expdir = ckrealloc(expdir, len); |
| 7814 | expdir_max = len; |
| 7815 | } |
| 7816 | |
| 7817 | expmeta(exp, endname, name_len, offset); |
| 7818 | enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7819 | } |
| 7820 | } |
| 7821 | } |
| 7822 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7823 | if (!atend) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7824 | endname[-esc - 1] = esc ? '\\' : '/'; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7825 | #undef expdir |
| 7826 | #undef expdir_max |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
| 7829 | static struct strlist * |
| 7830 | msort(struct strlist *list, int len) |
| 7831 | { |
| 7832 | struct strlist *p, *q = NULL; |
| 7833 | struct strlist **lpp; |
| 7834 | int half; |
| 7835 | int n; |
| 7836 | |
| 7837 | if (len <= 1) |
| 7838 | return list; |
| 7839 | half = len >> 1; |
| 7840 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7841 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7842 | q = p; |
| 7843 | p = p->next; |
| 7844 | } |
| 7845 | q->next = NULL; /* terminate first half of list */ |
| 7846 | q = msort(list, half); /* sort first half of list */ |
| 7847 | p = msort(p, len - half); /* sort second half */ |
| 7848 | lpp = &list; |
| 7849 | for (;;) { |
| 7850 | #if ENABLE_LOCALE_SUPPORT |
| 7851 | if (strcoll(p->text, q->text) < 0) |
| 7852 | #else |
| 7853 | if (strcmp(p->text, q->text) < 0) |
| 7854 | #endif |
| 7855 | { |
| 7856 | *lpp = p; |
| 7857 | lpp = &p->next; |
| 7858 | p = *lpp; |
| 7859 | if (p == NULL) { |
| 7860 | *lpp = q; |
| 7861 | break; |
| 7862 | } |
| 7863 | } else { |
| 7864 | *lpp = q; |
| 7865 | lpp = &q->next; |
| 7866 | q = *lpp; |
| 7867 | if (q == NULL) { |
| 7868 | *lpp = p; |
| 7869 | break; |
| 7870 | } |
| 7871 | } |
| 7872 | } |
| 7873 | return list; |
| 7874 | } |
| 7875 | |
| 7876 | /* |
| 7877 | * Sort the results of file name expansion. It calculates the number of |
| 7878 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7879 | * work. |
| 7880 | */ |
| 7881 | static struct strlist * |
| 7882 | expsort(struct strlist *str) |
| 7883 | { |
| 7884 | int len; |
| 7885 | struct strlist *sp; |
| 7886 | |
| 7887 | len = 0; |
| 7888 | for (sp = str; sp; sp = sp->next) |
| 7889 | len++; |
| 7890 | return msort(str, len); |
| 7891 | } |
| 7892 | |
| 7893 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7894 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7895 | { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7896 | /* TODO - EXP_REDIR */ |
| 7897 | |
| 7898 | while (str) { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7899 | exp_t exp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7900 | struct strlist **savelastp; |
| 7901 | struct strlist *sp; |
| 7902 | char *p; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7903 | unsigned len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7904 | |
| 7905 | if (fflag) |
| 7906 | goto nometa; |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7907 | if (!hasmeta(str->text)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7908 | goto nometa; |
| 7909 | savelastp = exparg.lastp; |
| 7910 | |
| 7911 | INT_OFF; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7912 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7913 | len = strlen(p); |
| 7914 | exp.dir_max = len + PATH_MAX; |
| 7915 | exp.dir = ckmalloc(exp.dir_max); |
| 7916 | |
| 7917 | expmeta(&exp, p, len, 0); |
| 7918 | free(exp.dir); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7919 | if (p != str->text) |
| 7920 | free(p); |
| 7921 | INT_ON; |
| 7922 | if (exparg.lastp == savelastp) { |
| 7923 | /* |
| 7924 | * no matches |
| 7925 | */ |
| 7926 | nometa: |
| 7927 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7928 | rmescapes(str->text, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7929 | exparg.lastp = &str->next; |
| 7930 | } else { |
| 7931 | *exparg.lastp = NULL; |
| 7932 | *savelastp = sp = expsort(*savelastp); |
| 7933 | while (sp->next != NULL) |
| 7934 | sp = sp->next; |
| 7935 | exparg.lastp = &sp->next; |
| 7936 | } |
| 7937 | str = str->next; |
| 7938 | } |
| 7939 | } |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7940 | #endif /* ENABLE_ASH_INTERNAL_GLOB */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7941 | |
| 7942 | /* |
| 7943 | * Perform variable substitution and command substitution on an argument, |
| 7944 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7945 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7946 | * here document expansion. |
| 7947 | */ |
| 7948 | static void |
| 7949 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7950 | { |
| 7951 | struct strlist *sp; |
| 7952 | char *p; |
| 7953 | |
| 7954 | argbackq = arg->narg.backquote; |
| 7955 | STARTSTACKSTR(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7956 | TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag)); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7957 | argstr(arg->narg.text, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7958 | if (arglist == NULL) { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 7959 | /* here document expanded */ |
| 7960 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7961 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7962 | p = grabstackstr(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7963 | TRACE(("expandarg: p:'%s'\n", p)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7964 | exparg.lastp = &exparg.list; |
| 7965 | /* |
| 7966 | * TODO - EXP_REDIR |
| 7967 | */ |
| 7968 | if (flag & EXP_FULL) { |
| 7969 | ifsbreakup(p, &exparg); |
| 7970 | *exparg.lastp = NULL; |
| 7971 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7972 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7973 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7974 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7975 | sp->text = p; |
| 7976 | *exparg.lastp = sp; |
| 7977 | exparg.lastp = &sp->next; |
| 7978 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7979 | *exparg.lastp = NULL; |
| 7980 | if (exparg.list) { |
| 7981 | *arglist->lastp = exparg.list; |
| 7982 | arglist->lastp = exparg.lastp; |
| 7983 | } |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 7984 | |
| 7985 | out: |
| 7986 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | /* |
| 7990 | * Expand shell variables and backquotes inside a here document. |
| 7991 | */ |
| 7992 | static void |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 7993 | expandhere(union node *arg) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7994 | { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7995 | expandarg(arg, (struct arglist *)NULL, EXP_QUOTED); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7996 | } |
| 7997 | |
| 7998 | /* |
| 7999 | * Returns true if the pattern matches the string. |
| 8000 | */ |
| 8001 | static int |
| 8002 | patmatch(char *pattern, const char *string) |
| 8003 | { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8004 | char *p = preglob(pattern, 0); |
Denys Vlasenko | 4476c70 | 2017-08-15 15:27:41 +0200 | [diff] [blame] | 8005 | int r = pmatch(p, string); |
| 8006 | //bb_error_msg("!fnmatch(pattern:'%s',str:'%s',0):%d", p, string, r); |
| 8007 | return r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8008 | } |
| 8009 | |
| 8010 | /* |
| 8011 | * See if a pattern matches in a case statement. |
| 8012 | */ |
| 8013 | static int |
| 8014 | casematch(union node *pattern, char *val) |
| 8015 | { |
| 8016 | struct stackmark smark; |
| 8017 | int result; |
| 8018 | |
| 8019 | setstackmark(&smark); |
| 8020 | argbackq = pattern->narg.backquote; |
| 8021 | STARTSTACKSTR(expdest); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 8022 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 8023 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8024 | result = patmatch(stackblock(), val); |
| 8025 | popstackmark(&smark); |
| 8026 | return result; |
| 8027 | } |
| 8028 | |
| 8029 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8030 | /* ============ find_command */ |
| 8031 | |
| 8032 | struct builtincmd { |
| 8033 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8034 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8035 | /* unsigned flags; */ |
| 8036 | }; |
| 8037 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 8038 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8039 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8040 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8041 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8042 | |
| 8043 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8044 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8045 | union param { |
| 8046 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8047 | /* index >= 0 for commands without path (slashes) */ |
| 8048 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 8049 | /* index == -1 for commands with slashes */ |
| 8050 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8051 | const struct builtincmd *cmd; |
| 8052 | struct funcnode *func; |
| 8053 | } u; |
| 8054 | }; |
| 8055 | /* values of cmdtype */ |
| 8056 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 8057 | #define CMDNORMAL 0 /* command is an executable program */ |
| 8058 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 8059 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 8060 | |
| 8061 | /* action to find_command() */ |
| 8062 | #define DO_ERR 0x01 /* prints errors */ |
| 8063 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 8064 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 8065 | #define DO_ALTPATH 0x08 /* using alternate path */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8066 | #define DO_REGBLTIN 0x10 /* regular built-ins and functions only */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8067 | |
| 8068 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 8069 | |
| 8070 | |
| 8071 | /* ============ Hashing commands */ |
| 8072 | |
| 8073 | /* |
| 8074 | * When commands are first encountered, they are entered in a hash table. |
| 8075 | * This ensures that a full path search will not have to be done for them |
| 8076 | * on each invocation. |
| 8077 | * |
| 8078 | * We should investigate converting to a linear search, even though that |
| 8079 | * would make the command name "hash" a misnomer. |
| 8080 | */ |
| 8081 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8082 | struct tblentry { |
| 8083 | struct tblentry *next; /* next entry in hash chain */ |
| 8084 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8085 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8086 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8087 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8088 | }; |
| 8089 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8090 | static struct tblentry **cmdtable; |
| 8091 | #define INIT_G_cmdtable() do { \ |
| 8092 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 8093 | } while (0) |
| 8094 | |
| 8095 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 8096 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8097 | |
| 8098 | static void |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8099 | tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, char **envp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8100 | { |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8101 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8102 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8103 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 8104 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8105 | while (*envp) |
| 8106 | putenv(*envp++); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 8107 | popredir(/*drop:*/ 1); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8108 | run_noexec_applet_and_exit(applet_no, cmd, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8109 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8110 | /* re-exec ourselves with the new arguments */ |
| 8111 | execve(bb_busybox_exec_path, argv, envp); |
| 8112 | /* If they called chroot or otherwise made the binary no longer |
| 8113 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8114 | } |
| 8115 | #endif |
| 8116 | |
| 8117 | repeat: |
| 8118 | #ifdef SYSV |
| 8119 | do { |
| 8120 | execve(cmd, argv, envp); |
| 8121 | } while (errno == EINTR); |
| 8122 | #else |
| 8123 | execve(cmd, argv, envp); |
| 8124 | #endif |
Ron Yorston | ca82b53 | 2018-11-01 11:45:03 +0100 | [diff] [blame] | 8125 | |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8126 | if (cmd != bb_busybox_exec_path && errno == ENOEXEC) { |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8127 | /* Run "cmd" as a shell script: |
| 8128 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 8129 | * "If the execve() function fails with ENOEXEC, the shell |
| 8130 | * shall execute a command equivalent to having a shell invoked |
| 8131 | * with the command name as its first operand, |
| 8132 | * with any remaining arguments passed to the new shell" |
| 8133 | * |
| 8134 | * That is, do not use $SHELL, user's shell, or /bin/sh; |
| 8135 | * just call ourselves. |
Denys Vlasenko | 2bef526 | 2011-12-16 00:25:17 +0100 | [diff] [blame] | 8136 | * |
| 8137 | * Note that bash reads ~80 chars of the file, and if it sees |
| 8138 | * a zero byte before it sees newline, it doesn't try to |
| 8139 | * interpret it, but fails with "cannot execute binary file" |
Denys Vlasenko | cda6ea9 | 2011-12-16 00:44:36 +0100 | [diff] [blame] | 8140 | * message and exit code 126. For one, this prevents attempts |
| 8141 | * to interpret foreign ELF binaries as shell scripts. |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8142 | */ |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8143 | argv[0] = (char*) cmd; |
| 8144 | cmd = bb_busybox_exec_path; |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8145 | /* NB: this is only possible because all callers of shellexec() |
| 8146 | * ensure that the argv[-1] slot exists! |
| 8147 | */ |
| 8148 | argv--; |
| 8149 | argv[0] = (char*) "ash"; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8150 | goto repeat; |
| 8151 | } |
| 8152 | } |
| 8153 | |
| 8154 | /* |
| 8155 | * Exec a program. Never returns. If you change this routine, you may |
| 8156 | * have to change the find_command routine as well. |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8157 | * argv[-1] must exist and be writable! See tryexec() for why. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8158 | */ |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8159 | static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN; |
| 8160 | static void shellexec(char *prog, char **argv, const char *path, int idx) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8161 | { |
| 8162 | char *cmdname; |
| 8163 | int e; |
| 8164 | char **envp; |
| 8165 | int exerrno; |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8166 | int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8167 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 8168 | envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL); |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8169 | if (strchr(prog, '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8170 | #if ENABLE_FEATURE_SH_STANDALONE |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8171 | || (applet_no = find_applet_by_name(prog)) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8172 | #endif |
| 8173 | ) { |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8174 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8175 | if (applet_no >= 0) { |
| 8176 | /* We tried execing ourself, but it didn't work. |
| 8177 | * Maybe /proc/self/exe doesn't exist? |
| 8178 | * Try $PATH search. |
| 8179 | */ |
| 8180 | goto try_PATH; |
| 8181 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8182 | e = errno; |
| 8183 | } else { |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8184 | try_PATH: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8185 | e = ENOENT; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8186 | while (padvance(&path, argv[0]) >= 0) { |
| 8187 | cmdname = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8188 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 8189 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8190 | if (errno != ENOENT && errno != ENOTDIR) |
| 8191 | e = errno; |
| 8192 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8193 | } |
| 8194 | } |
| 8195 | |
| 8196 | /* Map to POSIX errors */ |
| 8197 | switch (e) { |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8198 | default: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8199 | exerrno = 126; |
| 8200 | break; |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8201 | case ELOOP: |
| 8202 | case ENAMETOOLONG: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8203 | case ENOENT: |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8204 | case ENOTDIR: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8205 | exerrno = 127; |
| 8206 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8207 | } |
| 8208 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8209 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8210 | prog, e, suppress_int)); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 8211 | ash_msg_and_raise(EXEND, "%s: %s", prog, errmsg(e, "not found")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8212 | /* NOTREACHED */ |
| 8213 | } |
| 8214 | |
| 8215 | static void |
| 8216 | printentry(struct tblentry *cmdp) |
| 8217 | { |
| 8218 | int idx; |
| 8219 | const char *path; |
| 8220 | char *name; |
| 8221 | |
| 8222 | idx = cmdp->param.index; |
| 8223 | path = pathval(); |
| 8224 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8225 | padvance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8226 | } while (--idx >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8227 | name = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8228 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 8229 | } |
| 8230 | |
| 8231 | /* |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8232 | * Clear out command entries. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8233 | */ |
| 8234 | static void |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8235 | clearcmdentry(void) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8236 | { |
| 8237 | struct tblentry **tblp; |
| 8238 | struct tblentry **pp; |
| 8239 | struct tblentry *cmdp; |
| 8240 | |
| 8241 | INT_OFF; |
| 8242 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 8243 | pp = tblp; |
| 8244 | while ((cmdp = *pp) != NULL) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8245 | if (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8246 | || (cmdp->cmdtype == CMDBUILTIN |
| 8247 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8248 | && builtinloc > 0 |
| 8249 | ) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8250 | ) { |
| 8251 | *pp = cmdp->next; |
| 8252 | free(cmdp); |
| 8253 | } else { |
| 8254 | pp = &cmdp->next; |
| 8255 | } |
| 8256 | } |
| 8257 | } |
| 8258 | INT_ON; |
| 8259 | } |
| 8260 | |
| 8261 | /* |
| 8262 | * Locate a command in the command hash table. If "add" is nonzero, |
| 8263 | * add the command to the table if it is not already present. The |
| 8264 | * variable "lastcmdentry" is set to point to the address of the link |
| 8265 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 8266 | * entry. |
| 8267 | * |
| 8268 | * Interrupts must be off if called with add != 0. |
| 8269 | */ |
| 8270 | static struct tblentry **lastcmdentry; |
| 8271 | |
| 8272 | static struct tblentry * |
| 8273 | cmdlookup(const char *name, int add) |
| 8274 | { |
| 8275 | unsigned int hashval; |
| 8276 | const char *p; |
| 8277 | struct tblentry *cmdp; |
| 8278 | struct tblentry **pp; |
| 8279 | |
| 8280 | p = name; |
| 8281 | hashval = (unsigned char)*p << 4; |
| 8282 | while (*p) |
| 8283 | hashval += (unsigned char)*p++; |
| 8284 | hashval &= 0x7FFF; |
| 8285 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 8286 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8287 | if (strcmp(cmdp->cmdname, name) == 0) |
| 8288 | break; |
| 8289 | pp = &cmdp->next; |
| 8290 | } |
| 8291 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8292 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 8293 | + strlen(name) |
| 8294 | /* + 1 - already done because |
| 8295 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 8296 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8297 | cmdp->cmdtype = CMDUNKNOWN; |
| 8298 | strcpy(cmdp->cmdname, name); |
| 8299 | } |
| 8300 | lastcmdentry = pp; |
| 8301 | return cmdp; |
| 8302 | } |
| 8303 | |
| 8304 | /* |
| 8305 | * Delete the command entry returned on the last lookup. |
| 8306 | */ |
| 8307 | static void |
| 8308 | delete_cmd_entry(void) |
| 8309 | { |
| 8310 | struct tblentry *cmdp; |
| 8311 | |
| 8312 | INT_OFF; |
| 8313 | cmdp = *lastcmdentry; |
| 8314 | *lastcmdentry = cmdp->next; |
| 8315 | if (cmdp->cmdtype == CMDFUNCTION) |
| 8316 | freefunc(cmdp->param.func); |
| 8317 | free(cmdp); |
| 8318 | INT_ON; |
| 8319 | } |
| 8320 | |
| 8321 | /* |
| 8322 | * Add a new command entry, replacing any existing command entry for |
| 8323 | * the same name - except special builtins. |
| 8324 | */ |
| 8325 | static void |
| 8326 | addcmdentry(char *name, struct cmdentry *entry) |
| 8327 | { |
| 8328 | struct tblentry *cmdp; |
| 8329 | |
| 8330 | cmdp = cmdlookup(name, 1); |
| 8331 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 8332 | freefunc(cmdp->param.func); |
| 8333 | } |
| 8334 | cmdp->cmdtype = entry->cmdtype; |
| 8335 | cmdp->param = entry->u; |
| 8336 | cmdp->rehash = 0; |
| 8337 | } |
| 8338 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8339 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8340 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8341 | { |
| 8342 | struct tblentry **pp; |
| 8343 | struct tblentry *cmdp; |
| 8344 | int c; |
| 8345 | struct cmdentry entry; |
| 8346 | char *name; |
| 8347 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8348 | if (nextopt("r") != '\0') { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8349 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8350 | return 0; |
| 8351 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8352 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8353 | if (*argptr == NULL) { |
| 8354 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8355 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8356 | if (cmdp->cmdtype == CMDNORMAL) |
| 8357 | printentry(cmdp); |
| 8358 | } |
| 8359 | } |
| 8360 | return 0; |
| 8361 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8362 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8363 | c = 0; |
| 8364 | while ((name = *argptr) != NULL) { |
| 8365 | cmdp = cmdlookup(name, 0); |
| 8366 | if (cmdp != NULL |
| 8367 | && (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8368 | || (cmdp->cmdtype == CMDBUILTIN |
| 8369 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8370 | && builtinloc > 0 |
| 8371 | ) |
| 8372 | ) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8373 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8374 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8375 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8376 | find_command(name, &entry, DO_ERR, pathval()); |
| 8377 | if (entry.cmdtype == CMDUNKNOWN) |
| 8378 | c = 1; |
| 8379 | argptr++; |
| 8380 | } |
| 8381 | return c; |
| 8382 | } |
| 8383 | |
| 8384 | /* |
| 8385 | * Called when a cd is done. Marks all commands so the next time they |
| 8386 | * are executed they will be rehashed. |
| 8387 | */ |
| 8388 | static void |
| 8389 | hashcd(void) |
| 8390 | { |
| 8391 | struct tblentry **pp; |
| 8392 | struct tblentry *cmdp; |
| 8393 | |
| 8394 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8395 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8396 | if (cmdp->cmdtype == CMDNORMAL |
| 8397 | || (cmdp->cmdtype == CMDBUILTIN |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 8398 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8399 | && builtinloc > 0) |
| 8400 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8401 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8402 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8403 | } |
| 8404 | } |
| 8405 | } |
| 8406 | |
| 8407 | /* |
| 8408 | * Fix command hash table when PATH changed. |
| 8409 | * Called before PATH is changed. The argument is the new value of PATH; |
| 8410 | * pathval() still returns the old value at this point. |
| 8411 | * Called with interrupts off. |
| 8412 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8413 | static void FAST_FUNC |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8414 | changepath(const char *newval) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8415 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8416 | const char *new; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8417 | int idx; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8418 | int bltin; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8419 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8420 | new = newval; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8421 | idx = 0; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8422 | bltin = -1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8423 | for (;;) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8424 | if (*new == '%' && prefix(new + 1, "builtin")) { |
| 8425 | bltin = idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8426 | break; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8427 | } |
| 8428 | new = strchr(new, ':'); |
| 8429 | if (!new) |
| 8430 | break; |
| 8431 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 8432 | new++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8433 | } |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8434 | builtinloc = bltin; |
| 8435 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8436 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8437 | enum { |
| 8438 | TEOF, |
| 8439 | TNL, |
| 8440 | TREDIR, |
| 8441 | TWORD, |
| 8442 | TSEMI, |
| 8443 | TBACKGND, |
| 8444 | TAND, |
| 8445 | TOR, |
| 8446 | TPIPE, |
| 8447 | TLP, |
| 8448 | TRP, |
| 8449 | TENDCASE, |
| 8450 | TENDBQUOTE, |
| 8451 | TNOT, |
| 8452 | TCASE, |
| 8453 | TDO, |
| 8454 | TDONE, |
| 8455 | TELIF, |
| 8456 | TELSE, |
| 8457 | TESAC, |
| 8458 | TFI, |
| 8459 | TFOR, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8460 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8461 | TFUNCTION, |
| 8462 | #endif |
| 8463 | TIF, |
| 8464 | TIN, |
| 8465 | TTHEN, |
| 8466 | TUNTIL, |
| 8467 | TWHILE, |
| 8468 | TBEGIN, |
| 8469 | TEND |
| 8470 | }; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8471 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8472 | |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8473 | /* Nth bit indicates if token marks the end of a list */ |
| 8474 | enum { |
| 8475 | tokendlist = 0 |
| 8476 | /* 0 */ | (1u << TEOF) |
| 8477 | /* 1 */ | (0u << TNL) |
| 8478 | /* 2 */ | (0u << TREDIR) |
| 8479 | /* 3 */ | (0u << TWORD) |
| 8480 | /* 4 */ | (0u << TSEMI) |
| 8481 | /* 5 */ | (0u << TBACKGND) |
| 8482 | /* 6 */ | (0u << TAND) |
| 8483 | /* 7 */ | (0u << TOR) |
| 8484 | /* 8 */ | (0u << TPIPE) |
| 8485 | /* 9 */ | (0u << TLP) |
| 8486 | /* 10 */ | (1u << TRP) |
| 8487 | /* 11 */ | (1u << TENDCASE) |
| 8488 | /* 12 */ | (1u << TENDBQUOTE) |
| 8489 | /* 13 */ | (0u << TNOT) |
| 8490 | /* 14 */ | (0u << TCASE) |
| 8491 | /* 15 */ | (1u << TDO) |
| 8492 | /* 16 */ | (1u << TDONE) |
| 8493 | /* 17 */ | (1u << TELIF) |
| 8494 | /* 18 */ | (1u << TELSE) |
| 8495 | /* 19 */ | (1u << TESAC) |
| 8496 | /* 20 */ | (1u << TFI) |
| 8497 | /* 21 */ | (0u << TFOR) |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8498 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8499 | /* 22 */ | (0u << TFUNCTION) |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8500 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8501 | /* 23 */ | (0u << TIF) |
| 8502 | /* 24 */ | (0u << TIN) |
| 8503 | /* 25 */ | (1u << TTHEN) |
| 8504 | /* 26 */ | (0u << TUNTIL) |
| 8505 | /* 27 */ | (0u << TWHILE) |
| 8506 | /* 28 */ | (0u << TBEGIN) |
| 8507 | /* 29 */ | (1u << TEND) |
| 8508 | , /* thus far 29 bits used */ |
| 8509 | }; |
| 8510 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8511 | static const char *const tokname_array[] = { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8512 | "end of file", |
| 8513 | "newline", |
| 8514 | "redirection", |
| 8515 | "word", |
| 8516 | ";", |
| 8517 | "&", |
| 8518 | "&&", |
| 8519 | "||", |
| 8520 | "|", |
| 8521 | "(", |
| 8522 | ")", |
| 8523 | ";;", |
| 8524 | "`", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8525 | #define KWDOFFSET 13 |
| 8526 | /* the following are keywords */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8527 | "!", |
| 8528 | "case", |
| 8529 | "do", |
| 8530 | "done", |
| 8531 | "elif", |
| 8532 | "else", |
| 8533 | "esac", |
| 8534 | "fi", |
| 8535 | "for", |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8536 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8537 | "function", |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8538 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8539 | "if", |
| 8540 | "in", |
| 8541 | "then", |
| 8542 | "until", |
| 8543 | "while", |
| 8544 | "{", |
| 8545 | "}", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8546 | }; |
| 8547 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8548 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 8549 | static int |
| 8550 | pstrcmp(const void *a, const void *b) |
| 8551 | { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8552 | return strcmp((char*)a, *(char**)b); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
| 8555 | static const char *const * |
| 8556 | findkwd(const char *s) |
| 8557 | { |
| 8558 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8559 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 8560 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8561 | } |
| 8562 | |
| 8563 | /* |
| 8564 | * Locate and print what a word is... |
| 8565 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8566 | static int |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8567 | describe_command(char *command, const char *path, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8568 | { |
| 8569 | struct cmdentry entry; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8570 | #if ENABLE_ASH_ALIAS |
| 8571 | const struct alias *ap; |
| 8572 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8573 | |
| 8574 | path = path ? path : pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8575 | |
| 8576 | if (describe_command_verbose) { |
| 8577 | out1str(command); |
| 8578 | } |
| 8579 | |
| 8580 | /* First look at the keywords */ |
| 8581 | if (findkwd(command)) { |
| 8582 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 8583 | goto out; |
| 8584 | } |
| 8585 | |
| 8586 | #if ENABLE_ASH_ALIAS |
| 8587 | /* Then look at the aliases */ |
| 8588 | ap = lookupalias(command, 0); |
| 8589 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8590 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8591 | out1str("alias "); |
| 8592 | printalias(ap); |
| 8593 | return 0; |
| 8594 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8595 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8596 | goto out; |
| 8597 | } |
| 8598 | #endif |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8599 | /* Brute force */ |
| 8600 | find_command(command, &entry, DO_ABS, path); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8601 | |
| 8602 | switch (entry.cmdtype) { |
| 8603 | case CMDNORMAL: { |
| 8604 | int j = entry.u.index; |
| 8605 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8606 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8607 | p = command; |
| 8608 | } else { |
| 8609 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8610 | padvance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8611 | } while (--j >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8612 | p = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8613 | } |
| 8614 | if (describe_command_verbose) { |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8615 | out1fmt(" is %s", p); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8616 | } else { |
| 8617 | out1str(p); |
| 8618 | } |
| 8619 | break; |
| 8620 | } |
| 8621 | |
| 8622 | case CMDFUNCTION: |
| 8623 | if (describe_command_verbose) { |
Denys Vlasenko | 63c42af | 2018-07-24 17:08:04 +0200 | [diff] [blame] | 8624 | /*out1str(" is a shell function");*/ |
| 8625 | out1str(" is a function"); /* bash says this */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8626 | } else { |
| 8627 | out1str(command); |
| 8628 | } |
| 8629 | break; |
| 8630 | |
| 8631 | case CMDBUILTIN: |
| 8632 | if (describe_command_verbose) { |
| 8633 | out1fmt(" is a %sshell builtin", |
| 8634 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 8635 | "special " : nullstr |
| 8636 | ); |
| 8637 | } else { |
| 8638 | out1str(command); |
| 8639 | } |
| 8640 | break; |
| 8641 | |
| 8642 | default: |
| 8643 | if (describe_command_verbose) { |
| 8644 | out1str(": not found\n"); |
| 8645 | } |
| 8646 | return 127; |
| 8647 | } |
| 8648 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 8649 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8650 | return 0; |
| 8651 | } |
| 8652 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8653 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8654 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8655 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8656 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8657 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8658 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8659 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8660 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 8661 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8662 | i++; |
| 8663 | verbose = 0; |
| 8664 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8665 | while (argv[i]) { |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8666 | err |= describe_command(argv[i++], NULL, verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8667 | } |
| 8668 | return err; |
| 8669 | } |
| 8670 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8671 | static struct strlist * |
| 8672 | fill_arglist(struct arglist *arglist, union node **argpp) |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8673 | { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8674 | struct strlist **lastp = arglist->lastp; |
| 8675 | union node *argp; |
| 8676 | |
| 8677 | while ((argp = *argpp) != NULL) { |
| 8678 | expandarg(argp, arglist, EXP_FULL | EXP_TILDE); |
| 8679 | *argpp = argp->narg.next; |
| 8680 | if (*lastp) |
| 8681 | break; |
| 8682 | } |
| 8683 | |
| 8684 | return *lastp; |
| 8685 | } |
| 8686 | |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 8687 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8688 | /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */ |
| 8689 | static int |
| 8690 | parse_command_args(struct arglist *arglist, union node **argpp, const char **path) |
| 8691 | { |
| 8692 | struct strlist *sp = arglist->list; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8693 | char *cp, c; |
| 8694 | |
| 8695 | for (;;) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8696 | sp = sp->next ? sp->next : fill_arglist(arglist, argpp); |
| 8697 | if (!sp) |
| 8698 | return 0; |
| 8699 | cp = sp->text; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8700 | if (*cp++ != '-') |
| 8701 | break; |
| 8702 | c = *cp++; |
| 8703 | if (!c) |
| 8704 | break; |
| 8705 | if (c == '-' && !*cp) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8706 | if (!sp->next && !fill_arglist(arglist, argpp)) |
| 8707 | return 0; |
| 8708 | sp = sp->next; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8709 | break; |
| 8710 | } |
| 8711 | do { |
| 8712 | switch (c) { |
| 8713 | case 'p': |
| 8714 | *path = bb_default_path; |
| 8715 | break; |
| 8716 | default: |
| 8717 | /* run 'typecmd' for other options */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8718 | return 0; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8719 | } |
| 8720 | c = *cp++; |
| 8721 | } while (c); |
| 8722 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8723 | |
| 8724 | arglist->list = sp; |
| 8725 | return DO_NOFUNC; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8726 | } |
| 8727 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8728 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8729 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8730 | { |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8731 | char *cmd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8732 | int c; |
| 8733 | enum { |
| 8734 | VERIFY_BRIEF = 1, |
| 8735 | VERIFY_VERBOSE = 2, |
| 8736 | } verify = 0; |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8737 | const char *path = NULL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8738 | |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8739 | /* "command [-p] PROG ARGS" (that is, without -V or -v) |
| 8740 | * never reaches this function. |
| 8741 | */ |
| 8742 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8743 | while ((c = nextopt("pvV")) != '\0') |
| 8744 | if (c == 'V') |
| 8745 | verify |= VERIFY_VERBOSE; |
| 8746 | else if (c == 'v') |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8747 | /*verify |= VERIFY_BRIEF*/; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8748 | #if DEBUG |
| 8749 | else if (c != 'p') |
| 8750 | abort(); |
| 8751 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8752 | else |
| 8753 | path = bb_default_path; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8754 | |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 8755 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8756 | cmd = *argptr; |
| 8757 | if (/*verify && */ cmd) |
| 8758 | return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8759 | |
| 8760 | return 0; |
| 8761 | } |
| 8762 | #endif |
| 8763 | |
| 8764 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8765 | /*static int funcblocksize; // size of structures in function */ |
| 8766 | /*static int funcstringsize; // size of strings in node */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8767 | static void *funcblock; /* block to allocate function from */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8768 | static char *funcstring_end; /* end of block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8769 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 8770 | static const uint8_t nodesize[N_NUMBER] ALIGN1 = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8771 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 8772 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 8773 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8774 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8775 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8776 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8777 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8778 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8779 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 8780 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8781 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8782 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 8783 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 8784 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 8785 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8786 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8787 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8788 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8789 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8790 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8791 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8792 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8793 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8794 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8795 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8796 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8797 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8798 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8799 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8800 | }; |
| 8801 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8802 | static int calcsize(int funcblocksize, union node *n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8803 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8804 | static int |
| 8805 | sizenodelist(int funcblocksize, struct nodelist *lp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8806 | { |
| 8807 | while (lp) { |
| 8808 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8809 | funcblocksize = calcsize(funcblocksize, lp->n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8810 | lp = lp->next; |
| 8811 | } |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8812 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8813 | } |
| 8814 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8815 | static int |
| 8816 | calcsize(int funcblocksize, union node *n) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8817 | { |
| 8818 | if (n == NULL) |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8819 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8820 | funcblocksize += nodesize[n->type]; |
| 8821 | switch (n->type) { |
| 8822 | case NCMD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8823 | funcblocksize = calcsize(funcblocksize, n->ncmd.redirect); |
| 8824 | funcblocksize = calcsize(funcblocksize, n->ncmd.args); |
| 8825 | funcblocksize = calcsize(funcblocksize, n->ncmd.assign); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8826 | break; |
| 8827 | case NPIPE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8828 | funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8829 | break; |
| 8830 | case NREDIR: |
| 8831 | case NBACKGND: |
| 8832 | case NSUBSHELL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8833 | funcblocksize = calcsize(funcblocksize, n->nredir.redirect); |
| 8834 | funcblocksize = calcsize(funcblocksize, n->nredir.n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8835 | break; |
| 8836 | case NAND: |
| 8837 | case NOR: |
| 8838 | case NSEMI: |
| 8839 | case NWHILE: |
| 8840 | case NUNTIL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8841 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch2); |
| 8842 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8843 | break; |
| 8844 | case NIF: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8845 | funcblocksize = calcsize(funcblocksize, n->nif.elsepart); |
| 8846 | funcblocksize = calcsize(funcblocksize, n->nif.ifpart); |
| 8847 | funcblocksize = calcsize(funcblocksize, n->nif.test); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8848 | break; |
| 8849 | case NFOR: |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8850 | funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8851 | funcblocksize = calcsize(funcblocksize, n->nfor.body); |
| 8852 | funcblocksize = calcsize(funcblocksize, n->nfor.args); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8853 | break; |
| 8854 | case NCASE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8855 | funcblocksize = calcsize(funcblocksize, n->ncase.cases); |
| 8856 | funcblocksize = calcsize(funcblocksize, n->ncase.expr); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8857 | break; |
| 8858 | case NCLIST: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8859 | funcblocksize = calcsize(funcblocksize, n->nclist.body); |
| 8860 | funcblocksize = calcsize(funcblocksize, n->nclist.pattern); |
| 8861 | funcblocksize = calcsize(funcblocksize, n->nclist.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8862 | break; |
| 8863 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8864 | funcblocksize = calcsize(funcblocksize, n->ndefun.body); |
| 8865 | funcblocksize += SHELL_ALIGN(strlen(n->ndefun.text) + 1); |
| 8866 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8867 | case NARG: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8868 | funcblocksize = sizenodelist(funcblocksize, n->narg.backquote); |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8869 | funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8870 | funcblocksize = calcsize(funcblocksize, n->narg.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8871 | break; |
| 8872 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8873 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8874 | case NTO2: |
| 8875 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8876 | case NCLOBBER: |
| 8877 | case NFROM: |
| 8878 | case NFROMTO: |
| 8879 | case NAPPEND: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8880 | funcblocksize = calcsize(funcblocksize, n->nfile.fname); |
| 8881 | funcblocksize = calcsize(funcblocksize, n->nfile.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8882 | break; |
| 8883 | case NTOFD: |
| 8884 | case NFROMFD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8885 | funcblocksize = calcsize(funcblocksize, n->ndup.vname); |
| 8886 | funcblocksize = calcsize(funcblocksize, n->ndup.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8887 | break; |
| 8888 | case NHERE: |
| 8889 | case NXHERE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8890 | funcblocksize = calcsize(funcblocksize, n->nhere.doc); |
| 8891 | funcblocksize = calcsize(funcblocksize, n->nhere.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8892 | break; |
| 8893 | case NNOT: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8894 | funcblocksize = calcsize(funcblocksize, n->nnot.com); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8895 | break; |
| 8896 | }; |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8897 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
| 8900 | static char * |
| 8901 | nodeckstrdup(char *s) |
| 8902 | { |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8903 | funcstring_end -= SHELL_ALIGN(strlen(s) + 1); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8904 | return strcpy(funcstring_end, s); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | static union node *copynode(union node *); |
| 8908 | |
| 8909 | static struct nodelist * |
| 8910 | copynodelist(struct nodelist *lp) |
| 8911 | { |
| 8912 | struct nodelist *start; |
| 8913 | struct nodelist **lpp; |
| 8914 | |
| 8915 | lpp = &start; |
| 8916 | while (lp) { |
| 8917 | *lpp = funcblock; |
| 8918 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8919 | (*lpp)->n = copynode(lp->n); |
| 8920 | lp = lp->next; |
| 8921 | lpp = &(*lpp)->next; |
| 8922 | } |
| 8923 | *lpp = NULL; |
| 8924 | return start; |
| 8925 | } |
| 8926 | |
| 8927 | static union node * |
| 8928 | copynode(union node *n) |
| 8929 | { |
| 8930 | union node *new; |
| 8931 | |
| 8932 | if (n == NULL) |
| 8933 | return NULL; |
| 8934 | new = funcblock; |
| 8935 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8936 | |
| 8937 | switch (n->type) { |
| 8938 | case NCMD: |
| 8939 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8940 | new->ncmd.args = copynode(n->ncmd.args); |
| 8941 | new->ncmd.assign = copynode(n->ncmd.assign); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8942 | new->ncmd.linno = n->ncmd.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8943 | break; |
| 8944 | case NPIPE: |
| 8945 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8946 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8947 | break; |
| 8948 | case NREDIR: |
| 8949 | case NBACKGND: |
| 8950 | case NSUBSHELL: |
| 8951 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8952 | new->nredir.n = copynode(n->nredir.n); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8953 | new->nredir.linno = n->nredir.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8954 | break; |
| 8955 | case NAND: |
| 8956 | case NOR: |
| 8957 | case NSEMI: |
| 8958 | case NWHILE: |
| 8959 | case NUNTIL: |
| 8960 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8961 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8962 | break; |
| 8963 | case NIF: |
| 8964 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8965 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8966 | new->nif.test = copynode(n->nif.test); |
| 8967 | break; |
| 8968 | case NFOR: |
| 8969 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8970 | new->nfor.body = copynode(n->nfor.body); |
| 8971 | new->nfor.args = copynode(n->nfor.args); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8972 | new->nfor.linno = n->nfor.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8973 | break; |
| 8974 | case NCASE: |
| 8975 | new->ncase.cases = copynode(n->ncase.cases); |
| 8976 | new->ncase.expr = copynode(n->ncase.expr); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8977 | new->ncase.linno = n->ncase.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8978 | break; |
| 8979 | case NCLIST: |
| 8980 | new->nclist.body = copynode(n->nclist.body); |
| 8981 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8982 | new->nclist.next = copynode(n->nclist.next); |
| 8983 | break; |
| 8984 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8985 | new->ndefun.body = copynode(n->ndefun.body); |
| 8986 | new->ndefun.text = nodeckstrdup(n->ndefun.text); |
| 8987 | new->ndefun.linno = n->ndefun.linno; |
| 8988 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8989 | case NARG: |
| 8990 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8991 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8992 | new->narg.next = copynode(n->narg.next); |
| 8993 | break; |
| 8994 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8995 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8996 | case NTO2: |
| 8997 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8998 | case NCLOBBER: |
| 8999 | case NFROM: |
| 9000 | case NFROMTO: |
| 9001 | case NAPPEND: |
| 9002 | new->nfile.fname = copynode(n->nfile.fname); |
| 9003 | new->nfile.fd = n->nfile.fd; |
| 9004 | new->nfile.next = copynode(n->nfile.next); |
| 9005 | break; |
| 9006 | case NTOFD: |
| 9007 | case NFROMFD: |
| 9008 | new->ndup.vname = copynode(n->ndup.vname); |
| 9009 | new->ndup.dupfd = n->ndup.dupfd; |
| 9010 | new->ndup.fd = n->ndup.fd; |
| 9011 | new->ndup.next = copynode(n->ndup.next); |
| 9012 | break; |
| 9013 | case NHERE: |
| 9014 | case NXHERE: |
| 9015 | new->nhere.doc = copynode(n->nhere.doc); |
| 9016 | new->nhere.fd = n->nhere.fd; |
| 9017 | new->nhere.next = copynode(n->nhere.next); |
| 9018 | break; |
| 9019 | case NNOT: |
| 9020 | new->nnot.com = copynode(n->nnot.com); |
| 9021 | break; |
| 9022 | }; |
| 9023 | new->type = n->type; |
| 9024 | return new; |
| 9025 | } |
| 9026 | |
| 9027 | /* |
| 9028 | * Make a copy of a parse tree. |
| 9029 | */ |
| 9030 | static struct funcnode * |
| 9031 | copyfunc(union node *n) |
| 9032 | { |
| 9033 | struct funcnode *f; |
| 9034 | size_t blocksize; |
| 9035 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9036 | /*funcstringsize = 0;*/ |
| 9037 | blocksize = offsetof(struct funcnode, n) + calcsize(0, n); |
| 9038 | f = ckzalloc(blocksize /* + funcstringsize */); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9039 | funcblock = (char *) f + offsetof(struct funcnode, n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9040 | funcstring_end = (char *) f + blocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9041 | copynode(n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9042 | /* f->count = 0; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9043 | return f; |
| 9044 | } |
| 9045 | |
| 9046 | /* |
| 9047 | * Define a shell function. |
| 9048 | */ |
| 9049 | static void |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9050 | defun(union node *func) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9051 | { |
| 9052 | struct cmdentry entry; |
| 9053 | |
| 9054 | INT_OFF; |
| 9055 | entry.cmdtype = CMDFUNCTION; |
| 9056 | entry.u.func = copyfunc(func); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9057 | addcmdentry(func->ndefun.text, &entry); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9058 | INT_ON; |
| 9059 | } |
| 9060 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9061 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9062 | #define SKIPBREAK (1 << 0) |
| 9063 | #define SKIPCONT (1 << 1) |
| 9064 | #define SKIPFUNC (1 << 2) |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9065 | #define SKIPFUNCDEF (1 << 3) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9066 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9067 | static int skipcount; /* number of levels to skip */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 9068 | static int loopnest; /* current loop nesting level */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9069 | static int funcline; /* starting line number of current function, or 0 if not in a function */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9070 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9071 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9072 | static int evalstring(char *s, int flags); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9073 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9074 | /* Called to execute a trap. |
| 9075 | * Single callsite - at the end of evaltree(). |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9076 | * If we return non-zero, evaltree raises EXEXIT exception. |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9077 | * |
| 9078 | * Perhaps we should avoid entering new trap handlers |
| 9079 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9080 | */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9081 | static void |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9082 | dotrap(void) |
| 9083 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9084 | uint8_t *g; |
| 9085 | int sig; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9086 | int status, last_status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9087 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9088 | if (!pending_sig) |
| 9089 | return; |
| 9090 | |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9091 | status = savestatus; |
| 9092 | last_status = status; |
| 9093 | if (status < 0) { |
| 9094 | status = exitstatus; |
| 9095 | savestatus = status; |
| 9096 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9097 | pending_sig = 0; |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 9098 | barrier(); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9099 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9100 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9101 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9102 | char *p; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9103 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9104 | if (!*g) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9105 | continue; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9106 | |
| 9107 | if (evalskip) { |
| 9108 | pending_sig = sig; |
| 9109 | break; |
| 9110 | } |
| 9111 | |
| 9112 | p = trap[sig]; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9113 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 9114 | * don't upset it by resetting gotsig[SIGINT-1] */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9115 | if (sig == SIGINT && !p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9116 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9117 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9118 | TRACE(("sig %d is active, will run handler '%s'\n", sig, p)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9119 | *g = 0; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9120 | if (!p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9121 | continue; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9122 | evalstring(p, 0); |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9123 | if (evalskip != SKIPFUNC) |
| 9124 | exitstatus = status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9125 | } |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9126 | |
| 9127 | savestatus = last_status; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9128 | TRACE(("dotrap returns\n")); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9129 | } |
| 9130 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9131 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9132 | static int evalloop(union node *, int); |
| 9133 | static int evalfor(union node *, int); |
| 9134 | static int evalcase(union node *, int); |
| 9135 | static int evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9136 | static void expredir(union node *); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9137 | static int evalpipe(union node *, int); |
| 9138 | static int evalcommand(union node *, int); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9139 | static int evalbltin(const struct builtincmd *, int, char **, int); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9140 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9141 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9142 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9143 | * Evaluate a parse tree. The value is left in the global variable |
| 9144 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9145 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9146 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9147 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9148 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9149 | int checkexit = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9150 | int (*evalfn)(union node *, int); |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9151 | struct stackmark smark; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9152 | int status = 0; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9153 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9154 | setstackmark(&smark); |
| 9155 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9156 | if (n == NULL) { |
| 9157 | TRACE(("evaltree(NULL) called\n")); |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 9158 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9159 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9160 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9161 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9162 | dotrap(); |
| 9163 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9164 | switch (n->type) { |
| 9165 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9166 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9167 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9168 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9169 | break; |
| 9170 | #endif |
| 9171 | case NNOT: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9172 | status = !evaltree(n->nnot.com, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9173 | goto setstatus; |
| 9174 | case NREDIR: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9175 | errlinno = lineno = n->nredir.linno; |
| 9176 | if (funcline) |
| 9177 | lineno -= funcline - 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9178 | expredir(n->nredir.redirect); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 9179 | pushredir(n->nredir.redirect); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9180 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 9181 | if (!status) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9182 | status = evaltree(n->nredir.n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9183 | } |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 9184 | if (n->nredir.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 9185 | popredir(/*drop:*/ 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9186 | goto setstatus; |
| 9187 | case NCMD: |
| 9188 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9189 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9190 | if (eflag && !(flags & EV_TESTED)) |
| 9191 | checkexit = ~0; |
| 9192 | goto calleval; |
| 9193 | case NFOR: |
| 9194 | evalfn = evalfor; |
| 9195 | goto calleval; |
| 9196 | case NWHILE: |
| 9197 | case NUNTIL: |
| 9198 | evalfn = evalloop; |
| 9199 | goto calleval; |
| 9200 | case NSUBSHELL: |
| 9201 | case NBACKGND: |
| 9202 | evalfn = evalsubshell; |
Denys Vlasenko | cf98b0c | 2016-10-25 18:19:39 +0200 | [diff] [blame] | 9203 | goto checkexit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9204 | case NPIPE: |
| 9205 | evalfn = evalpipe; |
| 9206 | goto checkexit; |
| 9207 | case NCASE: |
| 9208 | evalfn = evalcase; |
| 9209 | goto calleval; |
| 9210 | case NAND: |
| 9211 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9212 | case NSEMI: { |
| 9213 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9214 | #if NAND + 1 != NOR |
| 9215 | #error NAND + 1 != NOR |
| 9216 | #endif |
| 9217 | #if NOR + 1 != NSEMI |
| 9218 | #error NOR + 1 != NSEMI |
| 9219 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 9220 | unsigned is_or = n->type - NAND; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9221 | status = evaltree( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9222 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9223 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9224 | ); |
Denys Vlasenko | bc1a008 | 2016-10-02 15:31:33 +0200 | [diff] [blame] | 9225 | if ((!status) == is_or || evalskip) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9226 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9227 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9228 | evaln: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9229 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9230 | calleval: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9231 | status = evalfn(n, flags); |
| 9232 | goto setstatus; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9233 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9234 | case NIF: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9235 | status = evaltree(n->nif.test, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9236 | if (evalskip) |
| 9237 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9238 | if (!status) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9239 | n = n->nif.ifpart; |
| 9240 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9241 | } |
| 9242 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9243 | n = n->nif.elsepart; |
| 9244 | goto evaln; |
| 9245 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9246 | status = 0; |
| 9247 | goto setstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9248 | case NDEFUN: |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9249 | defun(n); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9250 | /* Not necessary. To test it: |
| 9251 | * "false; f() { qwerty; }; echo $?" should print 0. |
| 9252 | */ |
| 9253 | /* status = 0; */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9254 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9255 | exitstatus = status; |
| 9256 | break; |
| 9257 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9258 | out: |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9259 | /* Order of checks below is important: |
| 9260 | * signal handlers trigger before exit caused by "set -e". |
| 9261 | */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9262 | dotrap(); |
| 9263 | |
| 9264 | if (checkexit & status) |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 9265 | raise_exception(EXEND); |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9266 | if (flags & EV_EXIT) |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 9267 | raise_exception(EXEND); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9268 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9269 | popstackmark(&smark); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9270 | TRACE(("leaving evaltree (no interrupts)\n")); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9271 | return exitstatus; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9272 | } |
| 9273 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 9274 | static int |
| 9275 | skiploop(void) |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9276 | { |
| 9277 | int skip = evalskip; |
| 9278 | |
| 9279 | switch (skip) { |
| 9280 | case 0: |
| 9281 | break; |
| 9282 | case SKIPBREAK: |
| 9283 | case SKIPCONT: |
| 9284 | if (--skipcount <= 0) { |
| 9285 | evalskip = 0; |
| 9286 | break; |
| 9287 | } |
| 9288 | skip = SKIPBREAK; |
| 9289 | break; |
| 9290 | } |
| 9291 | return skip; |
| 9292 | } |
| 9293 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9294 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9295 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9296 | { |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9297 | int skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9298 | int status; |
| 9299 | |
| 9300 | loopnest++; |
| 9301 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9302 | flags &= EV_TESTED; |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9303 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9304 | int i; |
| 9305 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9306 | i = evaltree(n->nbinary.ch1, EV_TESTED); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9307 | skip = skiploop(); |
| 9308 | if (skip == SKIPFUNC) |
| 9309 | status = i; |
| 9310 | if (skip) |
| 9311 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9312 | if (n->type != NWHILE) |
| 9313 | i = !i; |
| 9314 | if (i != 0) |
| 9315 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9316 | status = evaltree(n->nbinary.ch2, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9317 | skip = skiploop(); |
| 9318 | } while (!(skip & ~SKIPCONT)); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9319 | loopnest--; |
| 9320 | |
| 9321 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9322 | } |
| 9323 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9324 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9325 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9326 | { |
| 9327 | struct arglist arglist; |
| 9328 | union node *argp; |
| 9329 | struct strlist *sp; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9330 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9331 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9332 | errlinno = lineno = n->ncase.linno; |
| 9333 | if (funcline) |
| 9334 | lineno -= funcline - 1; |
| 9335 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9336 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9337 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9338 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 9339 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9340 | } |
| 9341 | *arglist.lastp = NULL; |
| 9342 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9343 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9344 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9345 | for (sp = arglist.list; sp; sp = sp->next) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9346 | setvar0(n->nfor.var, sp->text); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9347 | status = evaltree(n->nfor.body, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9348 | if (skiploop() & ~SKIPCONT) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9349 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9350 | } |
| 9351 | loopnest--; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9352 | |
| 9353 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9354 | } |
| 9355 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9356 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9357 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9358 | { |
| 9359 | union node *cp; |
| 9360 | union node *patp; |
| 9361 | struct arglist arglist; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9362 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9363 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9364 | errlinno = lineno = n->ncase.linno; |
| 9365 | if (funcline) |
| 9366 | lineno -= funcline - 1; |
| 9367 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9368 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9369 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9370 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9371 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 9372 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9373 | if (casematch(patp, arglist.list->text)) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9374 | /* Ensure body is non-empty as otherwise |
| 9375 | * EV_EXIT may prevent us from setting the |
| 9376 | * exit status. |
| 9377 | */ |
| 9378 | if (evalskip == 0 && cp->nclist.body) { |
| 9379 | status = evaltree(cp->nclist.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9380 | } |
| 9381 | goto out; |
| 9382 | } |
| 9383 | } |
| 9384 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9385 | out: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9386 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9389 | /* |
| 9390 | * Kick off a subshell to evaluate a tree. |
| 9391 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9392 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9393 | evalsubshell(union node *n, int flags) |
| 9394 | { |
| 9395 | struct job *jp; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9396 | int backgnd = (n->type == NBACKGND); /* FORK_BG(1) if yes, else FORK_FG(0) */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9397 | int status; |
| 9398 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9399 | errlinno = lineno = n->nredir.linno; |
| 9400 | if (funcline) |
| 9401 | lineno -= funcline - 1; |
| 9402 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9403 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9404 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9405 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9406 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9407 | if (backgnd == FORK_FG) |
| 9408 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9409 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9410 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9411 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9412 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9413 | flags |= EV_EXIT; |
| 9414 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9415 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9416 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9417 | redirect(n->nredir.redirect, 0); |
| 9418 | evaltreenr(n->nredir.n, flags); |
| 9419 | /* never returns */ |
| 9420 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9421 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9422 | status = 0; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9423 | if (backgnd == FORK_FG) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9424 | status = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9425 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9426 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9427 | } |
| 9428 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9429 | /* |
| 9430 | * Compute the names of the files in a redirection list. |
| 9431 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9432 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9433 | static void |
| 9434 | expredir(union node *n) |
| 9435 | { |
| 9436 | union node *redir; |
| 9437 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9438 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9439 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9440 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9441 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9442 | fn.lastp = &fn.list; |
| 9443 | switch (redir->type) { |
| 9444 | case NFROMTO: |
| 9445 | case NFROM: |
| 9446 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9447 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9448 | case NTO2: |
| 9449 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9450 | case NCLOBBER: |
| 9451 | case NAPPEND: |
| 9452 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 9453 | TRACE(("expredir expanded to '%s'\n", fn.list->text)); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9454 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9455 | store_expfname: |
| 9456 | #endif |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9457 | #if 0 |
| 9458 | // By the design of stack allocator, the loop of this kind: |
| 9459 | // while true; do while true; do break; done </dev/null; done |
| 9460 | // will look like a memory leak: ash plans to free expfname's |
| 9461 | // of "/dev/null" as soon as it finishes running the loop |
| 9462 | // (in this case, never). |
| 9463 | // This "fix" is wrong: |
Jon Tollefson | 4ba6c5d | 2012-11-13 19:26:53 +0100 | [diff] [blame] | 9464 | if (redir->nfile.expfname) |
| 9465 | stunalloc(redir->nfile.expfname); |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9466 | // It results in corrupted state of stacked allocations. |
| 9467 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9468 | redir->nfile.expfname = fn.list->text; |
| 9469 | break; |
| 9470 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9471 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9472 | if (redir->ndup.vname) { |
Denys Vlasenko | e368d85 | 2020-02-16 19:02:22 +0100 | [diff] [blame] | 9473 | expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9474 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9475 | ash_msg_and_raise_error("redir error"); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9476 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9477 | if (!isdigit_str9(fn.list->text)) { |
| 9478 | /* >&file, not >&fd */ |
| 9479 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 9480 | ash_msg_and_raise_error("redir error"); |
| 9481 | redir->type = NTO2; |
| 9482 | goto store_expfname; |
| 9483 | } |
| 9484 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9485 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9486 | } |
| 9487 | break; |
| 9488 | } |
| 9489 | } |
| 9490 | } |
| 9491 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9492 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9493 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 9494 | * of the process creating the pipeline. (This differs from some versions |
| 9495 | * of the shell, which make the last process in a pipeline the parent |
| 9496 | * of all the rest.) |
| 9497 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9498 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9499 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9500 | { |
| 9501 | struct job *jp; |
| 9502 | struct nodelist *lp; |
| 9503 | int pipelen; |
| 9504 | int prevfd; |
| 9505 | int pip[2]; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9506 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9507 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9508 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9509 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9510 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9511 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9512 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9513 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9514 | if (n->npipe.pipe_backgnd == 0) |
| 9515 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9516 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9517 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9518 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9519 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9520 | pip[1] = -1; |
| 9521 | if (lp->next) { |
| 9522 | if (pipe(pip) < 0) { |
| 9523 | close(prevfd); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 9524 | ash_msg_and_raise_perror("can't create pipe"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9525 | } |
| 9526 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9527 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9528 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9529 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9530 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9531 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9532 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9533 | if (prevfd > 0) { |
| 9534 | dup2(prevfd, 0); |
| 9535 | close(prevfd); |
| 9536 | } |
| 9537 | if (pip[1] > 1) { |
| 9538 | dup2(pip[1], 1); |
| 9539 | close(pip[1]); |
| 9540 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9541 | evaltreenr(lp->n, flags); |
| 9542 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9543 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9544 | /* parent */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9545 | if (prevfd >= 0) |
| 9546 | close(prevfd); |
| 9547 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 9548 | /* Don't want to trigger debugging */ |
| 9549 | if (pip[1] != -1) |
| 9550 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9551 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9552 | if (n->npipe.pipe_backgnd == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9553 | status = waitforjob(jp); |
| 9554 | TRACE(("evalpipe: job done exit status %d\n", status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9555 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9556 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9557 | |
| 9558 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9559 | } |
| 9560 | |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9561 | /* setinteractive needs this forward reference */ |
| 9562 | #if EDITING_HAS_get_exe_name |
| 9563 | static const char *get_builtin_name(int i) FAST_FUNC; |
| 9564 | #endif |
| 9565 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9566 | /* |
| 9567 | * Controls whether the shell is interactive or not. |
| 9568 | */ |
| 9569 | static void |
| 9570 | setinteractive(int on) |
| 9571 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9572 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9573 | |
| 9574 | if (++on == is_interactive) |
| 9575 | return; |
| 9576 | is_interactive = on; |
| 9577 | setsignal(SIGINT); |
| 9578 | setsignal(SIGQUIT); |
| 9579 | setsignal(SIGTERM); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9580 | if (is_interactive > 1) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9581 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9582 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9583 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9584 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9585 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9586 | /* note: ash and hush share this string */ |
| 9587 | out1fmt("\n\n%s %s\n" |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9588 | IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9589 | "\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9590 | bb_banner, |
| 9591 | "built-in shell (ash)" |
| 9592 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9593 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9594 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9595 | #endif |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9596 | #if ENABLE_FEATURE_EDITING |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9597 | if (!line_input_state) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9598 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9599 | # if EDITING_HAS_get_exe_name |
| 9600 | line_input_state->get_exe_name = get_builtin_name; |
| 9601 | # endif |
| 9602 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9603 | #endif |
| 9604 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9605 | } |
| 9606 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9607 | static void |
| 9608 | optschanged(void) |
| 9609 | { |
| 9610 | #if DEBUG |
| 9611 | opentrace(); |
| 9612 | #endif |
| 9613 | setinteractive(iflag); |
| 9614 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9615 | #if ENABLE_FEATURE_EDITING_VI |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9616 | if (line_input_state) { |
| 9617 | if (viflag) |
| 9618 | line_input_state->flags |= VI_MODE; |
| 9619 | else |
| 9620 | line_input_state->flags &= ~VI_MODE; |
| 9621 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9622 | #else |
| 9623 | viflag = 0; /* forcibly keep the option off */ |
| 9624 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9625 | } |
| 9626 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9627 | struct localvar_list { |
| 9628 | struct localvar_list *next; |
| 9629 | struct localvar *lv; |
| 9630 | }; |
| 9631 | |
| 9632 | static struct localvar_list *localvar_stack; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9633 | |
| 9634 | /* |
| 9635 | * Called after a function returns. |
| 9636 | * Interrupts must be off. |
| 9637 | */ |
| 9638 | static void |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9639 | poplocalvars(int keep) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9640 | { |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9641 | struct localvar_list *ll; |
| 9642 | struct localvar *lvp, *next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9643 | struct var *vp; |
| 9644 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9645 | INT_OFF; |
| 9646 | ll = localvar_stack; |
| 9647 | localvar_stack = ll->next; |
| 9648 | |
| 9649 | next = ll->lv; |
| 9650 | free(ll); |
| 9651 | |
| 9652 | while ((lvp = next) != NULL) { |
| 9653 | next = lvp->next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9654 | vp = lvp->vp; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9655 | TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-")); |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9656 | if (keep) { |
| 9657 | int bits = VSTRFIXED; |
| 9658 | |
| 9659 | if (lvp->flags != VUNSET) { |
| 9660 | if (vp->var_text == lvp->text) |
| 9661 | bits |= VTEXTFIXED; |
| 9662 | else if (!(lvp->flags & (VTEXTFIXED|VSTACK))) |
| 9663 | free((char*)lvp->text); |
| 9664 | } |
| 9665 | |
| 9666 | vp->flags &= ~bits; |
| 9667 | vp->flags |= (lvp->flags & bits); |
| 9668 | |
| 9669 | if ((vp->flags & |
| 9670 | (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 9671 | unsetvar(vp->var_text); |
| 9672 | } else if (vp == NULL) { /* $- saved */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9673 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 9674 | free((char*)lvp->text); |
| 9675 | optschanged(); |
Denys Vlasenko | d5b500c | 2017-07-26 19:25:40 +0200 | [diff] [blame] | 9676 | } else if (lvp->flags == VUNSET) { |
| 9677 | vp->flags &= ~(VSTRFIXED|VREADONLY); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9678 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9679 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9680 | if (vp->var_func) |
| 9681 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9682 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9683 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9684 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9685 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9686 | } |
| 9687 | free(lvp); |
| 9688 | } |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9689 | INT_ON; |
| 9690 | } |
| 9691 | |
| 9692 | /* |
| 9693 | * Create a new localvar environment. |
| 9694 | */ |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9695 | static struct localvar_list * |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9696 | pushlocalvars(int push) |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9697 | { |
| 9698 | struct localvar_list *ll; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9699 | struct localvar_list *top; |
| 9700 | |
| 9701 | top = localvar_stack; |
| 9702 | if (!push) |
| 9703 | goto out; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9704 | |
| 9705 | INT_OFF; |
| 9706 | ll = ckzalloc(sizeof(*ll)); |
| 9707 | /*ll->lv = NULL; - zalloc did it */ |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9708 | ll->next = top; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9709 | localvar_stack = ll; |
| 9710 | INT_ON; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9711 | out: |
| 9712 | return top; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9713 | } |
| 9714 | |
| 9715 | static void |
| 9716 | unwindlocalvars(struct localvar_list *stop) |
| 9717 | { |
| 9718 | while (localvar_stack != stop) |
| 9719 | poplocalvars(0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
| 9722 | static int |
| 9723 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 9724 | { |
| 9725 | volatile struct shparam saveparam; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9726 | struct jmploc *volatile savehandler; |
| 9727 | struct jmploc jmploc; |
| 9728 | int e; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9729 | int savefuncline; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9730 | |
| 9731 | saveparam = shellparam; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9732 | savefuncline = funcline; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 9733 | savehandler = exception_handler; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9734 | e = setjmp(jmploc.loc); |
| 9735 | if (e) { |
| 9736 | goto funcdone; |
| 9737 | } |
| 9738 | INT_OFF; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9739 | exception_handler = &jmploc; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9740 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9741 | func->count++; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9742 | funcline = func->n.ndefun.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9743 | INT_ON; |
| 9744 | shellparam.nparam = argc - 1; |
| 9745 | shellparam.p = argv + 1; |
| 9746 | #if ENABLE_ASH_GETOPTS |
| 9747 | shellparam.optind = 1; |
| 9748 | shellparam.optoff = -1; |
| 9749 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9750 | evaltree(func->n.ndefun.body, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9751 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9752 | INT_OFF; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9753 | funcline = savefuncline; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9754 | freefunc(func); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9755 | freeparam(&shellparam); |
| 9756 | shellparam = saveparam; |
| 9757 | exception_handler = savehandler; |
| 9758 | INT_ON; |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9759 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9760 | return e; |
| 9761 | } |
| 9762 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9763 | /* |
| 9764 | * Make a variable a local variable. When a variable is made local, it's |
| 9765 | * value and flags are saved in a localvar structure. The saved values |
| 9766 | * will be restored when the shell function returns. We handle the name |
Denys Vlasenko | e0a4e10 | 2015-05-13 02:20:14 +0200 | [diff] [blame] | 9767 | * "-" as a special case: it makes changes to "set +-options" local |
| 9768 | * (options will be restored on return from the function). |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9769 | */ |
| 9770 | static void |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9771 | mklocal(char *name, int flags) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9772 | { |
| 9773 | struct localvar *lvp; |
| 9774 | struct var **vpp; |
| 9775 | struct var *vp; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9776 | char *eq = strchr(name, '='); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9777 | |
| 9778 | INT_OFF; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9779 | /* Cater for duplicate "local". Examples: |
| 9780 | * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x |
| 9781 | * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x |
| 9782 | */ |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9783 | lvp = localvar_stack->lv; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9784 | while (lvp) { |
Eugene Rudoy | 1285aa6 | 2015-04-26 23:32:00 +0200 | [diff] [blame] | 9785 | if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9786 | if (eq) |
| 9787 | setvareq(name, 0); |
| 9788 | /* else: |
| 9789 | * it's a duplicate "local VAR" declaration, do nothing |
| 9790 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 9791 | goto ret; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9792 | } |
| 9793 | lvp = lvp->next; |
| 9794 | } |
| 9795 | |
| 9796 | lvp = ckzalloc(sizeof(*lvp)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9797 | if (LONE_DASH(name)) { |
| 9798 | char *p; |
| 9799 | p = ckmalloc(sizeof(optlist)); |
| 9800 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 9801 | vp = NULL; |
| 9802 | } else { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9803 | vpp = hashvar(name); |
| 9804 | vp = *findvar(vpp, name); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9805 | if (vp == NULL) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9806 | /* variable did not exist yet */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9807 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9808 | vp = setvareq(name, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9809 | else |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9810 | vp = setvar(name, NULL, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9811 | lvp->flags = VUNSET; |
| 9812 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9813 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9814 | lvp->flags = vp->flags; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9815 | /* make sure neither "struct var" nor string gets freed |
| 9816 | * during (un)setting: |
| 9817 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9818 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 9819 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9820 | setvareq(name, flags); |
Denys Vlasenko | 109ee5d | 2014-03-16 18:41:11 +0100 | [diff] [blame] | 9821 | else |
| 9822 | /* "local VAR" unsets VAR: */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9823 | setvar0(name, NULL); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9824 | } |
| 9825 | } |
| 9826 | lvp->vp = vp; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9827 | lvp->next = localvar_stack->lv; |
| 9828 | localvar_stack->lv = lvp; |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 9829 | ret: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9830 | INT_ON; |
| 9831 | } |
| 9832 | |
| 9833 | /* |
| 9834 | * The "local" command. |
| 9835 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9836 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9837 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9838 | { |
| 9839 | char *name; |
| 9840 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9841 | if (!localvar_stack) |
Ron Yorston | ef2386b | 2015-10-29 16:19:14 +0000 | [diff] [blame] | 9842 | ash_msg_and_raise_error("not in a function"); |
| 9843 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9844 | argv = argptr; |
| 9845 | while ((name = *argv++) != NULL) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9846 | mklocal(name, 0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9847 | } |
| 9848 | return 0; |
| 9849 | } |
| 9850 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9851 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9852 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9853 | { |
| 9854 | return 1; |
| 9855 | } |
| 9856 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9857 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9858 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9859 | { |
| 9860 | return 0; |
| 9861 | } |
| 9862 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9863 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9864 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9865 | { |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 9866 | optionarg = NULL; |
| 9867 | while (nextopt("a:") != '\0') |
| 9868 | /* nextopt() sets optionarg to "-a ARGV0" */; |
| 9869 | |
| 9870 | argv = argptr; |
| 9871 | if (argv[0]) { |
| 9872 | char *prog; |
| 9873 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9874 | iflag = 0; /* exit on error */ |
| 9875 | mflag = 0; |
| 9876 | optschanged(); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9877 | /* We should set up signals for "exec CMD" |
| 9878 | * the same way as for "CMD" without "exec". |
| 9879 | * But optschanged->setinteractive->setsignal |
| 9880 | * still thought we are a root shell. Therefore, for example, |
| 9881 | * SIGQUIT is still set to IGN. Fix it: |
| 9882 | */ |
| 9883 | shlvl++; |
| 9884 | setsignal(SIGQUIT); |
| 9885 | /*setsignal(SIGTERM); - unnecessary because of iflag=0 */ |
| 9886 | /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */ |
| 9887 | /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */ |
| 9888 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 9889 | prog = argv[0]; |
| 9890 | if (optionarg) |
| 9891 | argv[0] = optionarg; |
| 9892 | shellexec(prog, argv, pathval(), 0); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9893 | /* NOTREACHED */ |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9894 | } |
| 9895 | return 0; |
| 9896 | } |
| 9897 | |
| 9898 | /* |
| 9899 | * The return command. |
| 9900 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9901 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9902 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9903 | { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9904 | int skip; |
| 9905 | int status; |
| 9906 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9907 | /* |
| 9908 | * If called outside a function, do what ksh does; |
| 9909 | * skip the rest of the file. |
| 9910 | */ |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9911 | if (argv[1]) { |
| 9912 | skip = SKIPFUNC; |
| 9913 | status = number(argv[1]); |
| 9914 | } else { |
| 9915 | skip = SKIPFUNCDEF; |
| 9916 | status = exitstatus; |
| 9917 | } |
| 9918 | evalskip = skip; |
| 9919 | |
| 9920 | return status; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9921 | } |
| 9922 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9923 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9924 | static int breakcmd(int, char **) FAST_FUNC; |
| 9925 | static int dotcmd(int, char **) FAST_FUNC; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9926 | static int evalcmd(int, char **, int) FAST_FUNC; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9927 | static int exitcmd(int, char **) FAST_FUNC; |
| 9928 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9929 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9930 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9931 | #endif |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9932 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9933 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 9934 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9935 | #if MAX_HISTORY |
| 9936 | static int historycmd(int, char **) FAST_FUNC; |
| 9937 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 9938 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9939 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9940 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9941 | static int readcmd(int, char **) FAST_FUNC; |
| 9942 | static int setcmd(int, char **) FAST_FUNC; |
| 9943 | static int shiftcmd(int, char **) FAST_FUNC; |
| 9944 | static int timescmd(int, char **) FAST_FUNC; |
| 9945 | static int trapcmd(int, char **) FAST_FUNC; |
| 9946 | static int umaskcmd(int, char **) FAST_FUNC; |
| 9947 | static int unsetcmd(int, char **) FAST_FUNC; |
| 9948 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9949 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9950 | #define BUILTIN_NOSPEC "0" |
| 9951 | #define BUILTIN_SPECIAL "1" |
| 9952 | #define BUILTIN_REGULAR "2" |
| 9953 | #define BUILTIN_SPEC_REG "3" |
| 9954 | #define BUILTIN_ASSIGN "4" |
| 9955 | #define BUILTIN_SPEC_ASSG "5" |
| 9956 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9957 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 9958 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9959 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9960 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9961 | static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9962 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9963 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9964 | static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9965 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9966 | #if ENABLE_ASH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9967 | static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9968 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 9969 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9970 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9971 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9972 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 9973 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9974 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9975 | { BUILTIN_REGULAR "[" , testcmd }, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9976 | #endif |
| 9977 | #if BASH_TEST2 |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9978 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9979 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9980 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9981 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9982 | #endif |
| 9983 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9984 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9985 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9986 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 9987 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 9988 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9989 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9990 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9991 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9992 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9993 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9994 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9995 | #endif |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9996 | { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/ |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9997 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 9998 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 9999 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 10000 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10001 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10002 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10003 | #endif |
| 10004 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10005 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10006 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10007 | { BUILTIN_REGULAR "hash" , hashcmd }, |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 10008 | #if ENABLE_ASH_HELP |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10009 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10010 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 10011 | #if MAX_HISTORY |
| 10012 | { BUILTIN_NOSPEC "history" , historycmd }, |
| 10013 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10014 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10015 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 10016 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10017 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 10018 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10019 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10020 | #endif |
Denys Vlasenko | 85241c7 | 2017-07-26 20:00:08 +0200 | [diff] [blame] | 10021 | { BUILTIN_SPEC_REG_ASSG "local" , localcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10022 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10023 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 10024 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10025 | { BUILTIN_REGULAR "pwd" , pwdcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10026 | { BUILTIN_REGULAR "read" , readcmd }, |
| 10027 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 10028 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 10029 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 10030 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10031 | #if BASH_SOURCE |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10032 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 10033 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10034 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10035 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10036 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10037 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 10038 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 10039 | { BUILTIN_REGULAR "true" , truecmd }, |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10040 | { BUILTIN_REGULAR "type" , typecmd }, |
| 10041 | { BUILTIN_REGULAR "ulimit" , ulimitcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10042 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10043 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10044 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10045 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10046 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 10047 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10048 | }; |
| 10049 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10050 | /* Should match the above table! */ |
| 10051 | #define COMMANDCMD (builtintab + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10052 | /* . : */ 2 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10053 | /* [ */ 1 * ENABLE_ASH_TEST + \ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10054 | /* [[ */ 1 * BASH_TEST2 + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10055 | /* alias */ 1 * ENABLE_ASH_ALIAS + \ |
| 10056 | /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 10057 | /* break cd cddir */ 3) |
| 10058 | #define EVALCMD (COMMANDCMD + \ |
| 10059 | /* command */ 1 * ENABLE_ASH_CMDCMD + \ |
| 10060 | /* continue */ 1 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10061 | /* echo */ 1 * ENABLE_ASH_ECHO + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10062 | 0) |
| 10063 | #define EXECCMD (EVALCMD + \ |
| 10064 | /* eval */ 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10065 | |
| 10066 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10067 | * Search the table of builtin commands. |
| 10068 | */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10069 | static int |
| 10070 | pstrcmp1(const void *a, const void *b) |
| 10071 | { |
| 10072 | return strcmp((char*)a, *(char**)b + 1); |
| 10073 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10074 | static struct builtincmd * |
| 10075 | find_builtin(const char *name) |
| 10076 | { |
| 10077 | struct builtincmd *bp; |
| 10078 | |
| 10079 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 10080 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10081 | pstrcmp1 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10082 | ); |
| 10083 | return bp; |
| 10084 | } |
| 10085 | |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 10086 | #if EDITING_HAS_get_exe_name |
| 10087 | static const char * FAST_FUNC |
| 10088 | get_builtin_name(int i) |
| 10089 | { |
| 10090 | return /*i >= 0 &&*/ i < ARRAY_SIZE(builtintab) ? builtintab[i].name + 1 : NULL; |
| 10091 | } |
| 10092 | #endif |
| 10093 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10094 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10095 | * Execute a simple command. |
| 10096 | */ |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10097 | static void unwindfiles(struct parsefile *stop); |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10098 | static int |
| 10099 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10100 | { |
| 10101 | const char *q = endofname(p); |
| 10102 | if (p == q) |
| 10103 | return 0; |
| 10104 | return *q == '='; |
| 10105 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10106 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10107 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10108 | { |
| 10109 | /* Preserve exitstatus of a previous possible redirection |
| 10110 | * as POSIX mandates */ |
| 10111 | return back_exitstatus; |
| 10112 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10113 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10114 | evalcommand(union node *cmd, int flags) |
| 10115 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10116 | static const struct builtincmd null_bltin = { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10117 | BUILTIN_REGULAR "", bltincmd |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10118 | }; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 10119 | struct localvar_list *localvar_stop; |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10120 | struct parsefile *file_stop; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10121 | struct redirtab *redir_stop; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10122 | union node *argp; |
| 10123 | struct arglist arglist; |
| 10124 | struct arglist varlist; |
| 10125 | char **argv; |
| 10126 | int argc; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10127 | struct strlist *osp; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10128 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10129 | struct cmdentry cmdentry; |
| 10130 | struct job *jp; |
| 10131 | char *lastarg; |
| 10132 | const char *path; |
| 10133 | int spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10134 | int cmd_flag; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10135 | int status; |
| 10136 | char **nargv; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 10137 | smallint cmd_is_exec; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10138 | int vflags; |
| 10139 | int vlocal; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10140 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10141 | errlinno = lineno = cmd->ncmd.linno; |
| 10142 | if (funcline) |
| 10143 | lineno -= funcline - 1; |
| 10144 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10145 | /* First expand the arguments. */ |
| 10146 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10147 | file_stop = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10148 | back_exitstatus = 0; |
| 10149 | |
| 10150 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10151 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10152 | varlist.lastp = &varlist.list; |
| 10153 | *varlist.lastp = NULL; |
| 10154 | arglist.lastp = &arglist.list; |
| 10155 | *arglist.lastp = NULL; |
| 10156 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10157 | cmd_flag = 0; |
| 10158 | cmd_is_exec = 0; |
| 10159 | spclbltin = -1; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10160 | vflags = 0; |
| 10161 | vlocal = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10162 | path = NULL; |
| 10163 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10164 | argc = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10165 | argp = cmd->ncmd.args; |
| 10166 | osp = fill_arglist(&arglist, &argp); |
| 10167 | if (osp) { |
| 10168 | int pseudovarflag = 0; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10169 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10170 | for (;;) { |
| 10171 | find_command(arglist.list->text, &cmdentry, |
| 10172 | cmd_flag | DO_REGBLTIN, pathval()); |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10173 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10174 | vlocal++; |
| 10175 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10176 | /* implement bltin and command here */ |
| 10177 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 10178 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10179 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10180 | pseudovarflag = IS_BUILTIN_ASSIGN(cmdentry.u.cmd); |
| 10181 | if (spclbltin < 0) { |
| 10182 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10183 | vlocal = !spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10184 | } |
| 10185 | cmd_is_exec = cmdentry.u.cmd == EXECCMD; |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10186 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10187 | if (cmdentry.u.cmd != COMMANDCMD) |
| 10188 | break; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10189 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10190 | cmd_flag = parse_command_args(&arglist, &argp, &path); |
| 10191 | if (!cmd_flag) |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10192 | #endif |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10193 | break; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10194 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10195 | |
| 10196 | for (; argp; argp = argp->narg.next) |
| 10197 | expandarg(argp, &arglist, |
| 10198 | pseudovarflag && |
| 10199 | isassignment(argp->narg.text) ? |
| 10200 | EXP_VARTILDE : EXP_FULL | EXP_TILDE); |
| 10201 | |
| 10202 | for (sp = arglist.list; sp; sp = sp->next) |
| 10203 | argc++; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10204 | |
| 10205 | if (cmd_is_exec && argc > 1) |
| 10206 | vflags = VEXPORT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10209 | localvar_stop = pushlocalvars(vlocal); |
| 10210 | |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 10211 | /* Reserve one extra spot at the front for shellexec. */ |
| 10212 | nargv = stalloc(sizeof(char *) * (argc + 2)); |
| 10213 | argv = ++nargv; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10214 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10215 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 10216 | *nargv++ = sp->text; |
| 10217 | } |
| 10218 | *nargv = NULL; |
| 10219 | |
| 10220 | lastarg = NULL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10221 | if (iflag && funcline == 0 && argc > 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10222 | lastarg = nargv[-1]; |
| 10223 | |
| 10224 | expredir(cmd->ncmd.redirect); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10225 | redir_stop = pushredir(cmd->ncmd.redirect); |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10226 | preverrout_fd = 2; |
Denys Vlasenko | f8cdc7a | 2017-08-04 15:24:49 +0200 | [diff] [blame] | 10227 | if (BASH_XTRACEFD && xflag) { |
| 10228 | /* NB: bash closes fd == $BASH_XTRACEFD when it is changed. |
| 10229 | * we do not emulate this. We only use its value. |
| 10230 | */ |
| 10231 | const char *xtracefd = lookupvar("BASH_XTRACEFD"); |
| 10232 | if (xtracefd && is_number(xtracefd)) |
| 10233 | preverrout_fd = atoi(xtracefd); |
| 10234 | |
| 10235 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10236 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10237 | |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10238 | if (status) { |
| 10239 | bail: |
| 10240 | exitstatus = status; |
| 10241 | |
| 10242 | /* We have a redirection error. */ |
| 10243 | if (spclbltin > 0) |
| 10244 | raise_exception(EXERROR); |
| 10245 | |
| 10246 | goto out; |
| 10247 | } |
| 10248 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10249 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 10250 | struct strlist **spp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10251 | |
| 10252 | spp = varlist.lastp; |
| 10253 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 10254 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10255 | if (vlocal) |
| 10256 | mklocal((*spp)->text, VEXPORT); |
| 10257 | else |
| 10258 | setvareq((*spp)->text, vflags); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10259 | } |
| 10260 | |
| 10261 | /* Print the command if xflag is set. */ |
| 10262 | if (xflag) { |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10263 | const char *pfx = ""; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10264 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 10265 | fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX)); |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10266 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10267 | sp = varlist.list; |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10268 | while (sp) { |
| 10269 | char *varval = sp->text; |
| 10270 | char *eq = strchrnul(varval, '='); |
| 10271 | if (*eq) |
| 10272 | eq++; |
| 10273 | fdprintf(preverrout_fd, "%s%.*s%s", |
| 10274 | pfx, |
| 10275 | (int)(eq - varval), varval, |
| 10276 | maybe_single_quote(eq) |
| 10277 | ); |
| 10278 | sp = sp->next; |
| 10279 | pfx = " "; |
| 10280 | } |
| 10281 | |
| 10282 | sp = arglist.list; |
| 10283 | while (sp) { |
| 10284 | fdprintf(preverrout_fd, "%s%s", |
| 10285 | pfx, |
| 10286 | /* always quote if matches reserved word: */ |
| 10287 | findkwd(sp->text) |
| 10288 | ? single_quote(sp->text) |
| 10289 | : maybe_single_quote(sp->text) |
| 10290 | ); |
| 10291 | sp = sp->next; |
| 10292 | pfx = " "; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10293 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10294 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10295 | } |
| 10296 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10297 | /* Now locate the command. */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10298 | if (cmdentry.cmdtype != CMDBUILTIN |
| 10299 | || !(IS_BUILTIN_REGULAR(cmdentry.u.cmd)) |
| 10300 | ) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10301 | path = path ? path : pathval(); |
| 10302 | find_command(argv[0], &cmdentry, cmd_flag | DO_ERR, path); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10303 | } |
| 10304 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10305 | jp = NULL; |
| 10306 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10307 | /* Execute the command. */ |
| 10308 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10309 | case CMDUNKNOWN: |
| 10310 | status = 127; |
| 10311 | flush_stdout_stderr(); |
| 10312 | goto bail; |
| 10313 | |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10314 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 10315 | |
Denys Vlasenko | 1750d3a | 2018-01-15 00:41:04 +0100 | [diff] [blame] | 10316 | #if ENABLE_FEATURE_SH_STANDALONE \ |
| 10317 | && ENABLE_FEATURE_SH_NOFORK \ |
| 10318 | && NUM_APPLETS > 1 |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10319 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 10320 | * around run_nofork_applet() call. |
| 10321 | * (2) Should this check also be done in forkshell()? |
| 10322 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 10323 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 10324 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 10325 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10326 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10327 | char **sv_environ; |
| 10328 | |
| 10329 | INT_OFF; |
| 10330 | sv_environ = environ; |
| 10331 | environ = listvars(VEXPORT, VUNSET, varlist.list, /*end:*/ NULL); |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10332 | /* |
| 10333 | * Run <applet>_main(). |
| 10334 | * Signals (^C) can't interrupt here. |
| 10335 | * Otherwise we can mangle stdio or malloc internal state. |
| 10336 | * This makes applets which can run for a long time |
| 10337 | * and/or wait for user input ineligible for NOFORK: |
| 10338 | * for example, "yes" or "rm" (rm -i waits for input). |
| 10339 | */ |
Ron Yorston | d5bfe26 | 2020-02-20 08:23:03 +0000 | [diff] [blame] | 10340 | exitstatus = run_nofork_applet(applet_no, argv); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10341 | environ = sv_environ; |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10342 | /* |
| 10343 | * Try enabling NOFORK for "yes" applet. |
| 10344 | * ^C _will_ stop it (write returns EINTR), |
| 10345 | * but this causes stdout FILE to be stuck |
| 10346 | * and needing clearerr(). What if other applets |
| 10347 | * also can get EINTRs? Do we need to switch |
| 10348 | * our signals to SA_RESTART? |
| 10349 | */ |
| 10350 | /*clearerr(stdout);*/ |
| 10351 | INT_ON; |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10352 | break; |
| 10353 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10354 | #endif |
Denys Vlasenko | cfd392b | 2017-08-03 19:56:29 +0200 | [diff] [blame] | 10355 | /* Can we avoid forking? For example, very last command |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10356 | * in a script or a subshell does not need forking, |
| 10357 | * we can just exec it. |
| 10358 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10359 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10360 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10361 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 10362 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10363 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10364 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10365 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10366 | break; |
| 10367 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10368 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10369 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 10370 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10371 | } |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 10372 | shellexec(argv[0], argv, path, cmdentry.u.index); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10373 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10374 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10375 | case CMDBUILTIN: |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10376 | if (evalbltin(cmdentry.u.cmd, argc, argv, flags) |
| 10377 | && !(exception_type == EXERROR && spclbltin <= 0) |
| 10378 | ) { |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 10379 | raise: |
| 10380 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10381 | } |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10382 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10383 | |
| 10384 | case CMDFUNCTION: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10385 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 10386 | goto raise; |
| 10387 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10388 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10389 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10390 | status = waitforjob(jp); |
Ron Yorston | 6cda0b0 | 2020-02-21 16:16:56 +0000 | [diff] [blame] | 10391 | if (jp) |
| 10392 | TRACE(("forked child exited with %d\n", status)); |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10393 | FORCE_INT_ON; |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10394 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10395 | out: |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 10396 | if (cmd->ncmd.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 10397 | popredir(/*drop:*/ cmd_is_exec); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10398 | unwindredir(redir_stop); |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10399 | unwindfiles(file_stop); |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10400 | unwindlocalvars(localvar_stop); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10401 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10402 | /* dsl: I think this is intended to be used to support |
| 10403 | * '_' in 'vi' command mode during line editing... |
| 10404 | * However I implemented that within libedit itself. |
| 10405 | */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10406 | setvar0("_", lastarg); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10407 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10408 | |
| 10409 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
| 10412 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10413 | evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10414 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10415 | char *volatile savecmdname; |
| 10416 | struct jmploc *volatile savehandler; |
| 10417 | struct jmploc jmploc; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10418 | int status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10419 | int i; |
| 10420 | |
| 10421 | savecmdname = commandname; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 10422 | savehandler = exception_handler; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10423 | i = setjmp(jmploc.loc); |
| 10424 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10425 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10426 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10427 | commandname = argv[0]; |
| 10428 | argptr = argv + 1; |
| 10429 | optptr = NULL; /* initialize nextopt */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10430 | if (cmd == EVALCMD) |
| 10431 | status = evalcmd(argc, argv, flags); |
| 10432 | else |
| 10433 | status = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10434 | flush_stdout_stderr(); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10435 | status |= ferror(stdout); |
| 10436 | exitstatus = status; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10437 | cmddone: |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 10438 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10439 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10440 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10441 | |
| 10442 | return i; |
| 10443 | } |
| 10444 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10445 | static int |
| 10446 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10447 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 10448 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10449 | } |
| 10450 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10451 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10452 | /* |
| 10453 | * Search for a command. This is called before we fork so that the |
| 10454 | * location of the command will be available in the parent as well as |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10455 | * the child. The check for "goodname" is an overly conservative |
| 10456 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10457 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10458 | static void |
| 10459 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10460 | { |
| 10461 | struct cmdentry entry; |
| 10462 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10463 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 10464 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10465 | } |
| 10466 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10467 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10468 | /* ============ Builtin commands |
| 10469 | * |
| 10470 | * Builtin commands whose functions are closely tied to evaluation |
| 10471 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10472 | */ |
| 10473 | |
| 10474 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10475 | * Handle break and continue commands. Break, continue, and return are |
| 10476 | * all handled by setting the evalskip flag. The evaluation routines |
| 10477 | * above all check this flag, and if it is set they start skipping |
| 10478 | * commands rather than executing them. The variable skipcount is |
| 10479 | * the number of loops to break/continue, or the number of function |
| 10480 | * levels to return. (The latter is always 1.) It should probably |
| 10481 | * be an error to break out of more loops than exist, but it isn't |
| 10482 | * in the standard shell so we don't make it one here. |
| 10483 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10484 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10485 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10486 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10487 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10488 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 10489 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 10490 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10491 | if (n > loopnest) |
| 10492 | n = loopnest; |
| 10493 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10494 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10495 | skipcount = n; |
| 10496 | } |
| 10497 | return 0; |
| 10498 | } |
| 10499 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10500 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 10501 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10502 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10503 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10504 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10505 | enum { |
| 10506 | INPUT_PUSH_FILE = 1, |
| 10507 | INPUT_NOFILE_OK = 2, |
| 10508 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10509 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10510 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10511 | /* values of checkkwd variable */ |
| 10512 | #define CHKALIAS 0x1 |
| 10513 | #define CHKKWD 0x2 |
| 10514 | #define CHKNL 0x4 |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 10515 | #define CHKEOFMARK 0x8 |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10516 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10517 | /* |
| 10518 | * Push a string back onto the input at this current parsefile level. |
| 10519 | * We handle aliases this way. |
| 10520 | */ |
| 10521 | #if !ENABLE_ASH_ALIAS |
| 10522 | #define pushstring(s, ap) pushstring(s) |
| 10523 | #endif |
| 10524 | static void |
| 10525 | pushstring(char *s, struct alias *ap) |
| 10526 | { |
| 10527 | struct strpush *sp; |
| 10528 | int len; |
| 10529 | |
| 10530 | len = strlen(s); |
| 10531 | INT_OFF; |
| 10532 | if (g_parsefile->strpush) { |
| 10533 | sp = ckzalloc(sizeof(*sp)); |
| 10534 | sp->prev = g_parsefile->strpush; |
| 10535 | } else { |
| 10536 | sp = &(g_parsefile->basestrpush); |
| 10537 | } |
| 10538 | g_parsefile->strpush = sp; |
| 10539 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 10540 | sp->prev_left_in_line = g_parsefile->left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10541 | sp->unget = g_parsefile->unget; |
| 10542 | memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10543 | #if ENABLE_ASH_ALIAS |
| 10544 | sp->ap = ap; |
| 10545 | if (ap) { |
| 10546 | ap->flag |= ALIASINUSE; |
| 10547 | sp->string = s; |
| 10548 | } |
| 10549 | #endif |
| 10550 | g_parsefile->next_to_pgetc = s; |
| 10551 | g_parsefile->left_in_line = len; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10552 | g_parsefile->unget = 0; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10553 | INT_ON; |
| 10554 | } |
| 10555 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10556 | static void |
| 10557 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10558 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10559 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10560 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10561 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10562 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10563 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10564 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 10565 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 10566 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10567 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10568 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10569 | if (sp->string != sp->ap->val) { |
| 10570 | free(sp->string); |
| 10571 | } |
| 10572 | sp->ap->flag &= ~ALIASINUSE; |
| 10573 | if (sp->ap->flag & ALIASDEAD) { |
| 10574 | unalias(sp->ap->name); |
| 10575 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10576 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10577 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10578 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 10579 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10580 | g_parsefile->unget = sp->unget; |
| 10581 | memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10582 | g_parsefile->strpush = sp->prev; |
| 10583 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10584 | free(sp); |
| 10585 | INT_ON; |
| 10586 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10587 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10588 | static int |
| 10589 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10590 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10591 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 10592 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10593 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10594 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 10595 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 10596 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10597 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10598 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10599 | else { |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10600 | # if ENABLE_ASH_IDLE_TIMEOUT |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10601 | int timeout = -1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 10602 | const char *tmout_var = lookupvar("TMOUT"); |
| 10603 | if (tmout_var) { |
| 10604 | timeout = atoi(tmout_var) * 1000; |
| 10605 | if (timeout <= 0) |
| 10606 | timeout = -1; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10607 | } |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10608 | line_input_state->timeout = timeout; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10609 | # endif |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10610 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10611 | line_input_state->path_lookup = pathval(); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10612 | # endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 10613 | reinit_unicode_for_ash(); |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10614 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10615 | if (nr == 0) { |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 10616 | /* ^C pressed, "convert" to SIGINT */ |
| 10617 | write(STDOUT_FILENO, "^C", 2); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10618 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10619 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10620 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10621 | raise(SIGINT); |
| 10622 | return 1; |
| 10623 | } |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 10624 | exitstatus = 128 + SIGINT; |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 10625 | bb_putchar('\n'); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10626 | goto retry; |
| 10627 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10628 | if (nr < 0) { |
| 10629 | if (errno == 0) { |
| 10630 | /* Ctrl+D pressed */ |
| 10631 | nr = 0; |
| 10632 | } |
| 10633 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 10634 | else if (errno == EAGAIN && timeout > 0) { |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 10635 | puts("\007timed out waiting for input: auto-logout"); |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10636 | exitshell(); |
| 10637 | } |
| 10638 | # endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10639 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10640 | } |
| 10641 | #else |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10642 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10643 | #endif |
| 10644 | |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 10645 | #if 0 /* disabled: nonblock_immune_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10646 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10647 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 10648 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 10649 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 10650 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10651 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 10652 | out2str("sh: turning off NDELAY mode\n"); |
| 10653 | goto retry; |
| 10654 | } |
| 10655 | } |
| 10656 | } |
| 10657 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 10658 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10659 | return nr; |
| 10660 | } |
| 10661 | |
| 10662 | /* |
| 10663 | * Refill the input buffer and return the next input character: |
| 10664 | * |
| 10665 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10666 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 10667 | * or we are reading from a string so we can't refill the buffer, |
| 10668 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 10669 | * 3) If there is more stuff in this buffer, use it else call read to fill it. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10670 | * 4) Process input up to the next newline, deleting nul characters. |
| 10671 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10672 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 10673 | #define pgetc_debug(...) ((void)0) |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10674 | static int pgetc(void); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10675 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10676 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10677 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10678 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10679 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10680 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10681 | if (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10682 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10683 | if (g_parsefile->left_in_line == -1 |
| 10684 | && g_parsefile->strpush->ap |
| 10685 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 10686 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 10687 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10688 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10689 | return PEOA; |
| 10690 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10691 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10692 | popstring(); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10693 | return pgetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10694 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10695 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10696 | * "pgetc" needs refilling. |
| 10697 | */ |
| 10698 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 10699 | /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10700 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 10701 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10702 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10703 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10704 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10705 | /* even in failure keep left_in_line and next_to_pgetc |
| 10706 | * in lock step, for correct multi-layer pungetc. |
| 10707 | * left_in_line was decremented before preadbuffer(), |
| 10708 | * must inc next_to_pgetc: */ |
| 10709 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10710 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10711 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10712 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10713 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10714 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10715 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10716 | again: |
| 10717 | more = preadfd(); |
| 10718 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10719 | /* don't try reading again */ |
| 10720 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10721 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10722 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10723 | return PEOF; |
| 10724 | } |
| 10725 | } |
| 10726 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10727 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10728 | * Set g_parsefile->left_in_line |
| 10729 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10730 | * NUL chars are deleted. |
| 10731 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10732 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10733 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10734 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10735 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10736 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10737 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10738 | c = *q; |
| 10739 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10740 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10741 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10742 | q++; |
| 10743 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10744 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10745 | break; |
| 10746 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10747 | } |
| 10748 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10749 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10750 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 10751 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10752 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10753 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10754 | } |
| 10755 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10756 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10757 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10759 | char save = *q; |
| 10760 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10761 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10762 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10763 | } |
| 10764 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10765 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 10766 | g_parsefile->left_in_line, |
| 10767 | g_parsefile->next_to_pgetc, |
| 10768 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10769 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10770 | } |
| 10771 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10772 | static void |
| 10773 | nlprompt(void) |
| 10774 | { |
| 10775 | g_parsefile->linno++; |
| 10776 | setprompt_if(doprompt, 2); |
| 10777 | } |
| 10778 | static void |
| 10779 | nlnoprompt(void) |
| 10780 | { |
| 10781 | g_parsefile->linno++; |
| 10782 | needprompt = doprompt; |
| 10783 | } |
| 10784 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10785 | static int |
| 10786 | pgetc(void) |
| 10787 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10788 | int c; |
| 10789 | |
| 10790 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10791 | g_parsefile->left_in_line, |
| 10792 | g_parsefile->next_to_pgetc, |
| 10793 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10794 | if (g_parsefile->unget) |
| 10795 | return g_parsefile->lastc[--g_parsefile->unget]; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10796 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10797 | if (--g_parsefile->left_in_line >= 0) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 10798 | c = (unsigned char)*g_parsefile->next_to_pgetc++; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10799 | else |
| 10800 | c = preadbuffer(); |
| 10801 | |
| 10802 | g_parsefile->lastc[1] = g_parsefile->lastc[0]; |
| 10803 | g_parsefile->lastc[0] = c; |
| 10804 | |
| 10805 | return c; |
| 10806 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10807 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10808 | #if ENABLE_ASH_ALIAS |
| 10809 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10810 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10811 | { |
| 10812 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10813 | do { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10814 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10815 | g_parsefile->left_in_line, |
| 10816 | g_parsefile->next_to_pgetc, |
| 10817 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10818 | c = pgetc(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10819 | } while (c == PEOA); |
| 10820 | return c; |
| 10821 | } |
| 10822 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10823 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10824 | #endif |
| 10825 | |
| 10826 | /* |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10827 | * Undo a call to pgetc. Only two characters may be pushed back. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10828 | * PEOF may be pushed back. |
| 10829 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10830 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10831 | pungetc(void) |
| 10832 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10833 | g_parsefile->unget++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 10836 | /* This one eats backslash+newline */ |
| 10837 | static int |
| 10838 | pgetc_eatbnl(void) |
| 10839 | { |
| 10840 | int c; |
| 10841 | |
| 10842 | while ((c = pgetc()) == '\\') { |
| 10843 | if (pgetc() != '\n') { |
| 10844 | pungetc(); |
| 10845 | break; |
| 10846 | } |
| 10847 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10848 | nlprompt(); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 10849 | } |
| 10850 | |
| 10851 | return c; |
| 10852 | } |
| 10853 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 10854 | struct synstack { |
| 10855 | smalluint syntax; |
| 10856 | uint8_t innerdq :1; |
| 10857 | uint8_t varpushed :1; |
| 10858 | uint8_t dblquote :1; |
| 10859 | int varnest; /* levels of variables expansion */ |
| 10860 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10861 | int parenlevel; /* levels of parens in arithmetic */ |
| 10862 | struct synstack *prev; |
| 10863 | struct synstack *next; |
| 10864 | }; |
| 10865 | |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 10866 | static int |
| 10867 | pgetc_top(struct synstack *stack) |
| 10868 | { |
| 10869 | return stack->syntax == SQSYNTAX ? pgetc() : pgetc_eatbnl(); |
| 10870 | } |
| 10871 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 10872 | static void |
| 10873 | synstack_push(struct synstack **stack, struct synstack *next, int syntax) |
| 10874 | { |
| 10875 | memset(next, 0, sizeof(*next)); |
| 10876 | next->syntax = syntax; |
| 10877 | next->next = *stack; |
| 10878 | (*stack)->prev = next; |
| 10879 | *stack = next; |
| 10880 | } |
| 10881 | |
| 10882 | static ALWAYS_INLINE void |
| 10883 | synstack_pop(struct synstack **stack) |
| 10884 | { |
| 10885 | *stack = (*stack)->next; |
| 10886 | } |
| 10887 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10888 | /* |
| 10889 | * To handle the "." command, a stack of input files is used. Pushfile |
| 10890 | * adds a new entry to the stack and popfile restores the previous level. |
| 10891 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10892 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10893 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10894 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10895 | struct parsefile *pf; |
| 10896 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10897 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10898 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10899 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10900 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 10901 | /*pf->basestrpush.prev = NULL;*/ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10902 | /*pf->unget = 0;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10903 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10904 | } |
| 10905 | |
| 10906 | static void |
| 10907 | popfile(void) |
| 10908 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10909 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10910 | |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 10911 | if (pf == &basepf) |
| 10912 | return; |
| 10913 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10914 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10915 | if (pf->pf_fd >= 0) |
| 10916 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 10917 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10918 | while (pf->strpush) |
| 10919 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10920 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10921 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10922 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10923 | } |
| 10924 | |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10925 | static void |
| 10926 | unwindfiles(struct parsefile *stop) |
| 10927 | { |
| 10928 | while (g_parsefile != stop) |
| 10929 | popfile(); |
| 10930 | } |
| 10931 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10932 | /* |
| 10933 | * Return to top level. |
| 10934 | */ |
| 10935 | static void |
| 10936 | popallfiles(void) |
| 10937 | { |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10938 | unwindfiles(&basepf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10939 | } |
| 10940 | |
| 10941 | /* |
| 10942 | * Close the file(s) that the shell is reading commands from. Called |
| 10943 | * after a fork is done. |
| 10944 | */ |
| 10945 | static void |
| 10946 | closescript(void) |
| 10947 | { |
| 10948 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10949 | if (g_parsefile->pf_fd > 0) { |
| 10950 | close(g_parsefile->pf_fd); |
| 10951 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10952 | } |
| 10953 | } |
| 10954 | |
| 10955 | /* |
| 10956 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 10957 | * interrupts off. |
| 10958 | */ |
| 10959 | static void |
| 10960 | setinputfd(int fd, int push) |
| 10961 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10962 | if (push) { |
| 10963 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10964 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10965 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10966 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10967 | if (g_parsefile->buf == NULL) |
| 10968 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10969 | g_parsefile->left_in_buffer = 0; |
| 10970 | g_parsefile->left_in_line = 0; |
| 10971 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10972 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10973 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10974 | /* |
| 10975 | * Set the input to take input from a file. If push is set, push the |
| 10976 | * old input onto the stack first. |
| 10977 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10978 | static int |
| 10979 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10980 | { |
| 10981 | int fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10982 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10983 | INT_OFF; |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 10984 | fd = open(fname, O_RDONLY | O_CLOEXEC); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10985 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10986 | if (flags & INPUT_NOFILE_OK) |
| 10987 | goto out; |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10988 | exitstatus = 127; |
Johannes Schindelin | 20a63b2 | 2017-08-22 22:03:17 +0200 | [diff] [blame] | 10989 | ash_msg_and_raise_perror("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10990 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 10991 | if (fd < 10) |
| 10992 | fd = savefd(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 10993 | else if (O_CLOEXEC == 0) /* old libc */ |
Denys Vlasenko | e19923f | 2016-10-26 15:38:44 +0200 | [diff] [blame] | 10994 | close_on_exec_on(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 10995 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10996 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10997 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10998 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10999 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11000 | } |
| 11001 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11002 | /* |
| 11003 | * Like setinputfile, but takes input from a string. |
| 11004 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11005 | static void |
| 11006 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11007 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11008 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11009 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11010 | g_parsefile->next_to_pgetc = string; |
| 11011 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11012 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11013 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11014 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11015 | } |
| 11016 | |
| 11017 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 11018 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11019 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11020 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11021 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11022 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11023 | |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11024 | /* Hash of mtimes of mailboxes */ |
| 11025 | static unsigned mailtime_hash; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11026 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11027 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11028 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11029 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11030 | * Print appropriate message(s) if mail has arrived. |
| 11031 | * If mail_var_path_changed is set, |
| 11032 | * then the value of MAIL has mail_var_path_changed, |
| 11033 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11034 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11035 | static void |
| 11036 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11037 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11038 | const char *mpath; |
| 11039 | char *p; |
| 11040 | char *q; |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11041 | unsigned new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11042 | struct stackmark smark; |
| 11043 | struct stat statb; |
| 11044 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11045 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11046 | mpath = mpathset() ? mpathval() : mailval(); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11047 | new_hash = 0; |
| 11048 | for (;;) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11049 | int len; |
| 11050 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 11051 | len = padvance_magic(&mpath, nullstr, 2); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11052 | if (!len) |
| 11053 | break; |
| 11054 | p = stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11055 | break; |
| 11056 | if (*p == '\0') |
| 11057 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11058 | for (q = p; *q; q++) |
| 11059 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 11060 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11061 | if (q[-1] != '/') |
| 11062 | abort(); |
| 11063 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11064 | q[-1] = '\0'; /* delete trailing '/' */ |
| 11065 | if (stat(p, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11066 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11067 | } |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11068 | /* Very simplistic "hash": just a sum of all mtimes */ |
| 11069 | new_hash += (unsigned)statb.st_mtime; |
| 11070 | } |
| 11071 | if (!mail_var_path_changed && mailtime_hash != new_hash) { |
Denys Vlasenko | 4cd99e7 | 2015-10-09 16:02:53 +0200 | [diff] [blame] | 11072 | if (mailtime_hash != 0) |
| 11073 | out2str("you have mail\n"); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11074 | mailtime_hash = new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11075 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11076 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11077 | popstackmark(&smark); |
| 11078 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11079 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11080 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11081 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11082 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11083 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11084 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11085 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11086 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11087 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11088 | |
| 11089 | /* ============ ??? */ |
| 11090 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11091 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11092 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11093 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11094 | static void |
| 11095 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11096 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11097 | char **newparam; |
| 11098 | char **ap; |
| 11099 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11100 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11101 | for (nparam = 0; argv[nparam]; nparam++) |
| 11102 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11103 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 11104 | while (*argv) { |
| 11105 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11106 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11107 | *ap = NULL; |
| 11108 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11109 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11110 | shellparam.nparam = nparam; |
| 11111 | shellparam.p = newparam; |
| 11112 | #if ENABLE_ASH_GETOPTS |
| 11113 | shellparam.optind = 1; |
| 11114 | shellparam.optoff = -1; |
| 11115 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11116 | } |
| 11117 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11118 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11119 | * Process shell options. The global variable argptr contains a pointer |
| 11120 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11121 | * |
| 11122 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 11123 | * For a non-interactive shell, an error condition encountered |
| 11124 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 11125 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 11126 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11127 | * ... |
| 11128 | * Utility syntax error (option or operand error) Shall exit |
| 11129 | * ... |
| 11130 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 11131 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 11132 | * and shell continues), and people rely on this behavior! |
| 11133 | * Testcase: |
| 11134 | * set -o barfoo 2>/dev/null |
| 11135 | * echo $? |
| 11136 | * |
| 11137 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11138 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11139 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11140 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11141 | { |
| 11142 | int i; |
| 11143 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11144 | if (name) { |
| 11145 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11146 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11147 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11148 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11149 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11150 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11151 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11152 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11153 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11154 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | 2f9c124 | 2019-08-02 16:43:36 +0200 | [diff] [blame] | 11155 | if (optnames(i)[0] == '\0') |
| 11156 | continue; |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11157 | if (val) { |
| 11158 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 11159 | } else { |
| 11160 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 11161 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11162 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11163 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11164 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11165 | static void |
| 11166 | setoption(int flag, int val) |
| 11167 | { |
| 11168 | int i; |
| 11169 | |
| 11170 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11171 | if (optletters(i) == flag && optnames(i)[0] != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11172 | optlist[i] = val; |
| 11173 | return; |
| 11174 | } |
| 11175 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11176 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11177 | /* NOTREACHED */ |
| 11178 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11179 | /* If login_sh is not NULL, we are called to parse command line opts, |
| 11180 | * not "set -opts" |
| 11181 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11182 | static int |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11183 | options(int *login_sh) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11184 | { |
| 11185 | char *p; |
| 11186 | int val; |
| 11187 | int c; |
| 11188 | |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11189 | if (login_sh) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11190 | minusc = NULL; |
| 11191 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11192 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11193 | if (c != '-' && c != '+') |
| 11194 | break; |
| 11195 | argptr++; |
| 11196 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11197 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11198 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11199 | if (p[0] == '\0' || LONE_DASH(p)) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11200 | if (!login_sh) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11201 | /* "-" means turn off -x and -v */ |
| 11202 | if (p[0] == '\0') |
| 11203 | xflag = vflag = 0; |
| 11204 | /* "--" means reset params */ |
| 11205 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11206 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11207 | } |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11208 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11209 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11210 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11211 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11212 | while ((c = *p++) != '\0') { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11213 | if (login_sh) { |
| 11214 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
| 11215 | if (c == 'c') { |
| 11216 | minusc = p; /* command is after shell args */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11217 | cflag = 1; |
| 11218 | continue; |
| 11219 | } |
| 11220 | if (c == 's') { /* -s, +s */ |
| 11221 | sflag = 1; |
| 11222 | continue; |
| 11223 | } |
| 11224 | if (c == 'i') { /* -i, +i */ |
| 11225 | iflag = 1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11226 | continue; |
| 11227 | } |
| 11228 | if (c == 'l') { |
| 11229 | *login_sh = 1; /* -l or +l == --login */ |
| 11230 | continue; |
| 11231 | } |
| 11232 | /* bash does not accept +-login, we also won't */ |
| 11233 | if (val && (c == '-')) { /* long options */ |
| 11234 | if (strcmp(p, "login") == 0) { |
| 11235 | *login_sh = 1; |
| 11236 | } |
| 11237 | break; |
| 11238 | } |
| 11239 | } |
| 11240 | if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11241 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11242 | /* it already printed err message */ |
| 11243 | return 1; /* error */ |
| 11244 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11245 | if (*argptr) |
| 11246 | argptr++; |
| 11247 | } else { |
| 11248 | setoption(c, val); |
| 11249 | } |
| 11250 | } |
| 11251 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11252 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11253 | } |
| 11254 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11255 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11256 | * The shift builtin command. |
| 11257 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11258 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11259 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11260 | { |
| 11261 | int n; |
| 11262 | char **ap1, **ap2; |
| 11263 | |
| 11264 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11265 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11266 | n = number(argv[1]); |
| 11267 | if (n > shellparam.nparam) |
Ingo van Lil | 9c8e94b | 2018-01-05 15:04:23 +0100 | [diff] [blame] | 11268 | return 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11269 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11270 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11271 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11272 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11273 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11274 | } |
| 11275 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11276 | while ((*ap2++ = *ap1++) != NULL) |
| 11277 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11278 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11279 | shellparam.optind = 1; |
| 11280 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11281 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11282 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11283 | return 0; |
| 11284 | } |
| 11285 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11286 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11287 | * POSIX requires that 'set' (but not export or readonly) output the |
| 11288 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 11289 | * Maybe we could keep them in an ordered balanced binary tree |
| 11290 | * instead of hashed lists. |
| 11291 | * For now just roll 'em through qsort for printing... |
| 11292 | */ |
| 11293 | static int |
| 11294 | showvars(const char *sep_prefix, int on, int off) |
| 11295 | { |
| 11296 | const char *sep; |
| 11297 | char **ep, **epend; |
| 11298 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 11299 | ep = listvars(on, off, /*strlist:*/ NULL, &epend); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11300 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 11301 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11302 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11303 | |
| 11304 | for (; ep < epend; ep++) { |
| 11305 | const char *p; |
| 11306 | const char *q; |
| 11307 | |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11308 | p = endofname(*ep); |
| 11309 | /* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this |
| 11310 | * makes "export -p" to have output not suitable for "eval": |
| 11311 | * import os |
| 11312 | * os.environ["test-test"]="test" |
| 11313 | * if os.fork() == 0: |
| 11314 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 11315 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) |
| 11316 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11317 | q = nullstr; |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11318 | if (*p == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11319 | q = single_quote(++p); |
| 11320 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 11321 | } |
| 11322 | return 0; |
| 11323 | } |
| 11324 | |
| 11325 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11326 | * The set command builtin. |
| 11327 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11328 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11329 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11330 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11331 | int retval; |
| 11332 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11333 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11334 | return showvars(nullstr, 0, VUNSET); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11335 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11336 | INT_OFF; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11337 | retval = options(/*login_sh:*/ NULL); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11338 | if (retval == 0) { /* if no parse error... */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11339 | optschanged(); |
| 11340 | if (*argptr != NULL) { |
| 11341 | setparam(argptr); |
| 11342 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11343 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11344 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11345 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11346 | } |
| 11347 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11348 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11349 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11350 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11351 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11352 | uint32_t t; |
| 11353 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11354 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11355 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11356 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11357 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 11358 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11359 | vrandom.flags &= ~VNOFUNC; |
| 11360 | } else { |
| 11361 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11362 | t = strtoul(value, NULL, 10); |
| 11363 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11364 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11365 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11368 | #if BASH_EPOCH_VARS |
| 11369 | static void FAST_FUNC |
| 11370 | change_epoch(struct var *vepoch, const char *fmt) |
| 11371 | { |
| 11372 | struct timeval tv; |
| 11373 | char buffer[sizeof("%lu.nnnnnn") + sizeof(long)*3]; |
| 11374 | |
| 11375 | gettimeofday(&tv, NULL); |
| 11376 | sprintf(buffer, fmt, (unsigned long)tv.tv_sec, (unsigned)tv.tv_usec); |
| 11377 | setvar(vepoch->var_text, buffer, VNOFUNC); |
| 11378 | vepoch->flags &= ~VNOFUNC; |
| 11379 | } |
| 11380 | |
| 11381 | static void FAST_FUNC |
| 11382 | change_seconds(const char *value UNUSED_PARAM) |
| 11383 | { |
| 11384 | change_epoch(&vepochs, "%lu"); |
| 11385 | } |
| 11386 | |
| 11387 | static void FAST_FUNC |
| 11388 | change_realtime(const char *value UNUSED_PARAM) |
| 11389 | { |
| 11390 | change_epoch(&vepochr, "%lu.%06u"); |
| 11391 | } |
| 11392 | #endif |
| 11393 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11394 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11395 | static int |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11396 | getopts(char *optstr, char *optvar, char **optfirst) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11397 | { |
| 11398 | char *p, *q; |
| 11399 | char c = '?'; |
| 11400 | int done = 0; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11401 | char sbuf[2]; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11402 | char **optnext; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11403 | int ind = shellparam.optind; |
| 11404 | int off = shellparam.optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11405 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11406 | sbuf[1] = '\0'; |
| 11407 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11408 | shellparam.optind = -1; |
| 11409 | optnext = optfirst + ind - 1; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11410 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11411 | if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11412 | p = NULL; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11413 | else |
| 11414 | p = optnext[-1] + off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11415 | if (p == NULL || *p == '\0') { |
| 11416 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11417 | p = *optnext; |
| 11418 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11419 | atend: |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11420 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11421 | p = NULL; |
| 11422 | done = 1; |
| 11423 | goto out; |
| 11424 | } |
| 11425 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11426 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11427 | goto atend; |
| 11428 | } |
| 11429 | |
| 11430 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 11431 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11432 | if (*q == '\0') { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11433 | /* OPTERR is a bashism */ |
| 11434 | const char *cp = lookupvar("OPTERR"); |
| 11435 | if ((cp && LONE_CHAR(cp, '0')) |
| 11436 | || (optstr[0] == ':') |
| 11437 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11438 | sbuf[0] = c; |
| 11439 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11440 | setvar0("OPTARG", sbuf); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11441 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11442 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11443 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11444 | } |
| 11445 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11446 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11447 | } |
| 11448 | if (*++q == ':') |
| 11449 | q++; |
| 11450 | } |
| 11451 | |
| 11452 | if (*++q == ':') { |
| 11453 | if (*p == '\0' && (p = *optnext) == NULL) { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11454 | /* OPTERR is a bashism */ |
| 11455 | const char *cp = lookupvar("OPTERR"); |
| 11456 | if ((cp && LONE_CHAR(cp, '0')) |
| 11457 | || (optstr[0] == ':') |
| 11458 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11459 | sbuf[0] = c; |
| 11460 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11461 | setvar0("OPTARG", sbuf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11462 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11463 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11464 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11465 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11466 | c = '?'; |
| 11467 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11468 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11469 | } |
| 11470 | |
| 11471 | if (p == *optnext) |
| 11472 | optnext++; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11473 | setvar0("OPTARG", p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11474 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11475 | } else |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11476 | setvar0("OPTARG", nullstr); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11477 | out: |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11478 | ind = optnext - optfirst + 1; |
| 11479 | setvar("OPTIND", itoa(ind), VNOFUNC); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11480 | sbuf[0] = c; |
| 11481 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11482 | setvar0(optvar, sbuf); |
| 11483 | |
| 11484 | shellparam.optoff = p ? p - *(optnext - 1) : -1; |
| 11485 | shellparam.optind = ind; |
| 11486 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11487 | return done; |
| 11488 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11489 | |
| 11490 | /* |
| 11491 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 11492 | * to be processed. Shellparam.optptr points to the next character to |
| 11493 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 11494 | * then it's the first time getopts has been called. |
| 11495 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11496 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11497 | getoptscmd(int argc, char **argv) |
| 11498 | { |
| 11499 | char **optbase; |
| 11500 | |
| 11501 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 11502 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11503 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11504 | optbase = shellparam.p; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11505 | if ((unsigned)shellparam.optind > shellparam.nparam + 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11506 | shellparam.optind = 1; |
| 11507 | shellparam.optoff = -1; |
| 11508 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11509 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11510 | optbase = &argv[3]; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11511 | if ((unsigned)shellparam.optind > argc - 2) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11512 | shellparam.optind = 1; |
| 11513 | shellparam.optoff = -1; |
| 11514 | } |
| 11515 | } |
| 11516 | |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11517 | return getopts(argv[1], argv[2], optbase); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11518 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11519 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11520 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11521 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11522 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11523 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11524 | struct heredoc { |
| 11525 | struct heredoc *next; /* next here document in list */ |
| 11526 | union node *here; /* redirection node */ |
| 11527 | char *eofmark; /* string indicating end of input */ |
| 11528 | smallint striptabs; /* if set, strip leading tabs */ |
| 11529 | }; |
| 11530 | |
| 11531 | static smallint tokpushback; /* last token pushed back */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11532 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 11533 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 11534 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 11535 | static char *wordtext; /* text of last word returned by readtoken */ |
| 11536 | static struct nodelist *backquotelist; |
| 11537 | static union node *redirnode; |
| 11538 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11539 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11540 | static const char * |
| 11541 | tokname(char *buf, int tok) |
| 11542 | { |
| 11543 | if (tok < TSEMI) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11544 | return tokname_array[tok]; |
| 11545 | sprintf(buf, "\"%s\"", tokname_array[tok]); |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11546 | return buf; |
| 11547 | } |
| 11548 | |
| 11549 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11550 | * Called when an unexpected token is read during the parse. The argument |
| 11551 | * is the token that is expected, or -1 if more than one type of token can |
| 11552 | * occur at this point. |
| 11553 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11554 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11555 | static void |
| 11556 | raise_error_unexpected_syntax(int token) |
| 11557 | { |
| 11558 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11559 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11560 | int l; |
| 11561 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11562 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11563 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11564 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11565 | raise_error_syntax(msg); |
| 11566 | /* NOTREACHED */ |
| 11567 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11568 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11569 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 11570 | static union node *andor(void); |
| 11571 | static union node *pipeline(void); |
| 11572 | static union node *parse_command(void); |
| 11573 | static void parseheredoc(void); |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11574 | static int peektoken(void); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11575 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11576 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11577 | static union node * |
| 11578 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11579 | { |
| 11580 | union node *n1, *n2, *n3; |
| 11581 | int tok; |
| 11582 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11583 | n1 = NULL; |
| 11584 | for (;;) { |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11585 | switch (readtoken()) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11586 | case TNL: |
| 11587 | if (!(nlflag & 1)) |
| 11588 | break; |
| 11589 | parseheredoc(); |
| 11590 | return n1; |
| 11591 | |
| 11592 | case TEOF: |
| 11593 | if (!n1 && (nlflag & 1)) |
| 11594 | n1 = NODE_EOF; |
| 11595 | parseheredoc(); |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11596 | tokpushback++; |
| 11597 | lasttoken = TEOF; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11598 | return n1; |
| 11599 | } |
| 11600 | |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11601 | tokpushback++; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11602 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11603 | if (nlflag == 2 && ((1 << peektoken()) & tokendlist)) |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11604 | return n1; |
| 11605 | nlflag |= 2; |
| 11606 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11607 | n2 = andor(); |
| 11608 | tok = readtoken(); |
| 11609 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11610 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11611 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11612 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11613 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11614 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11615 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11616 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11617 | n2 = n3; |
| 11618 | } |
| 11619 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11620 | } |
| 11621 | } |
| 11622 | if (n1 == NULL) { |
| 11623 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11624 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11625 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11626 | n3->type = NSEMI; |
| 11627 | n3->nbinary.ch1 = n1; |
| 11628 | n3->nbinary.ch2 = n2; |
| 11629 | n1 = n3; |
| 11630 | } |
| 11631 | switch (tok) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11632 | case TNL: |
| 11633 | case TEOF: |
| 11634 | tokpushback = 1; |
| 11635 | /* fall through */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11636 | case TBACKGND: |
| 11637 | case TSEMI: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11638 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11639 | default: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11640 | if ((nlflag & 1)) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11641 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11642 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11643 | return n1; |
| 11644 | } |
| 11645 | } |
| 11646 | } |
| 11647 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11648 | static union node * |
| 11649 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11650 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11651 | union node *n1, *n2, *n3; |
| 11652 | int t; |
| 11653 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11654 | n1 = pipeline(); |
| 11655 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11656 | t = readtoken(); |
| 11657 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11658 | t = NAND; |
| 11659 | } else if (t == TOR) { |
| 11660 | t = NOR; |
| 11661 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11662 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11663 | return n1; |
| 11664 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11665 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11666 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11667 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11668 | n3->type = t; |
| 11669 | n3->nbinary.ch1 = n1; |
| 11670 | n3->nbinary.ch2 = n2; |
| 11671 | n1 = n3; |
| 11672 | } |
| 11673 | } |
| 11674 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11675 | static union node * |
| 11676 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11677 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11678 | union node *n1, *n2, *pipenode; |
| 11679 | struct nodelist *lp, *prev; |
| 11680 | int negate; |
| 11681 | |
| 11682 | negate = 0; |
| 11683 | TRACE(("pipeline: entered\n")); |
| 11684 | if (readtoken() == TNOT) { |
| 11685 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11686 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11687 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11688 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11689 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11690 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11691 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11692 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11693 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11694 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11695 | pipenode->npipe.cmdlist = lp; |
| 11696 | lp->n = n1; |
| 11697 | do { |
| 11698 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11699 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11700 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11701 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11702 | prev->next = lp; |
| 11703 | } while (readtoken() == TPIPE); |
| 11704 | lp->next = NULL; |
| 11705 | n1 = pipenode; |
| 11706 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11707 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11708 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11709 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11710 | n2->type = NNOT; |
| 11711 | n2->nnot.com = n1; |
| 11712 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11713 | } |
| 11714 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11715 | } |
| 11716 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11717 | static union node * |
| 11718 | makename(void) |
| 11719 | { |
| 11720 | union node *n; |
| 11721 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11722 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11723 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11724 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11725 | n->narg.text = wordtext; |
| 11726 | n->narg.backquote = backquotelist; |
| 11727 | return n; |
| 11728 | } |
| 11729 | |
| 11730 | static void |
| 11731 | fixredir(union node *n, const char *text, int err) |
| 11732 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11733 | int fd; |
| 11734 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11735 | TRACE(("Fix redir %s %d\n", text, err)); |
| 11736 | if (!err) |
| 11737 | n->ndup.vname = NULL; |
| 11738 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11739 | fd = bb_strtou(text, NULL, 10); |
| 11740 | if (!errno && fd >= 0) |
| 11741 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11742 | else if (LONE_DASH(text)) |
| 11743 | n->ndup.dupfd = -1; |
| 11744 | else { |
| 11745 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11746 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11747 | n->ndup.vname = makename(); |
| 11748 | } |
| 11749 | } |
| 11750 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11751 | static void |
| 11752 | parsefname(void) |
| 11753 | { |
| 11754 | union node *n = redirnode; |
| 11755 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 11756 | if (n->type == NHERE) |
| 11757 | checkkwd = CHKEOFMARK; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11758 | if (readtoken() != TWORD) |
| 11759 | raise_error_unexpected_syntax(-1); |
| 11760 | if (n->type == NHERE) { |
| 11761 | struct heredoc *here = heredoc; |
| 11762 | struct heredoc *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11763 | |
| 11764 | if (quoteflag == 0) |
| 11765 | n->type = NXHERE; |
| 11766 | TRACE(("Here document %d\n", n->type)); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 11767 | rmescapes(wordtext, 0, NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11768 | here->eofmark = wordtext; |
| 11769 | here->next = NULL; |
| 11770 | if (heredoclist == NULL) |
| 11771 | heredoclist = here; |
| 11772 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11773 | for (p = heredoclist; p->next; p = p->next) |
| 11774 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11775 | p->next = here; |
| 11776 | } |
| 11777 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 11778 | fixredir(n, wordtext, 0); |
| 11779 | } else { |
| 11780 | n->nfile.fname = makename(); |
| 11781 | } |
| 11782 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11783 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11784 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11785 | simplecmd(void) |
| 11786 | { |
| 11787 | union node *args, **app; |
| 11788 | union node *n = NULL; |
| 11789 | union node *vars, **vpp; |
| 11790 | union node **rpp, *redir; |
| 11791 | int savecheckkwd; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11792 | int savelinno; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11793 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11794 | smallint double_brackets_flag = 0; |
| 11795 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11796 | IF_BASH_FUNCTION(smallint function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11797 | |
| 11798 | args = NULL; |
| 11799 | app = &args; |
| 11800 | vars = NULL; |
| 11801 | vpp = &vars; |
| 11802 | redir = NULL; |
| 11803 | rpp = &redir; |
| 11804 | |
| 11805 | savecheckkwd = CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11806 | savelinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11807 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11808 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11809 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11810 | t = readtoken(); |
| 11811 | switch (t) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11812 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11813 | case TFUNCTION: |
| 11814 | if (peektoken() != TWORD) |
| 11815 | raise_error_unexpected_syntax(TWORD); |
| 11816 | function_flag = 1; |
| 11817 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11818 | #endif |
| 11819 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11820 | case TAND: /* "&&" */ |
| 11821 | case TOR: /* "||" */ |
| 11822 | if (!double_brackets_flag) { |
| 11823 | tokpushback = 1; |
| 11824 | goto out; |
| 11825 | } |
| 11826 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 11827 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11828 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11829 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11830 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11831 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11832 | n->narg.text = wordtext; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11833 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11834 | if (strcmp("[[", wordtext) == 0) |
| 11835 | double_brackets_flag = 1; |
| 11836 | else if (strcmp("]]", wordtext) == 0) |
| 11837 | double_brackets_flag = 0; |
| 11838 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11839 | n->narg.backquote = backquotelist; |
| 11840 | if (savecheckkwd && isassignment(wordtext)) { |
| 11841 | *vpp = n; |
| 11842 | vpp = &n->narg.next; |
| 11843 | } else { |
| 11844 | *app = n; |
| 11845 | app = &n->narg.next; |
| 11846 | savecheckkwd = 0; |
| 11847 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11848 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11849 | if (function_flag) { |
| 11850 | checkkwd = CHKNL | CHKKWD; |
| 11851 | switch (peektoken()) { |
| 11852 | case TBEGIN: |
| 11853 | case TIF: |
| 11854 | case TCASE: |
| 11855 | case TUNTIL: |
| 11856 | case TWHILE: |
| 11857 | case TFOR: |
| 11858 | goto do_func; |
| 11859 | case TLP: |
| 11860 | function_flag = 0; |
| 11861 | break; |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 11862 | # if BASH_TEST2 |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11863 | case TWORD: |
| 11864 | if (strcmp("[[", wordtext) == 0) |
| 11865 | goto do_func; |
| 11866 | /* fall through */ |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 11867 | # endif |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11868 | default: |
| 11869 | raise_error_unexpected_syntax(-1); |
| 11870 | } |
| 11871 | } |
| 11872 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11873 | break; |
| 11874 | case TREDIR: |
| 11875 | *rpp = n = redirnode; |
| 11876 | rpp = &n->nfile.next; |
| 11877 | parsefname(); /* read name of redirection file */ |
| 11878 | break; |
| 11879 | case TLP: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11880 | IF_BASH_FUNCTION(do_func:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11881 | if (args && app == &args->narg.next |
| 11882 | && !vars && !redir |
| 11883 | ) { |
| 11884 | struct builtincmd *bcmd; |
| 11885 | const char *name; |
| 11886 | |
| 11887 | /* We have a function */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11888 | if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11889 | raise_error_unexpected_syntax(TRP); |
| 11890 | name = n->narg.text; |
| 11891 | if (!goodname(name) |
| 11892 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 11893 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11894 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11895 | } |
| 11896 | n->type = NDEFUN; |
| 11897 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11898 | n->ndefun.text = n->narg.text; |
| 11899 | n->ndefun.linno = g_parsefile->linno; |
| 11900 | n->ndefun.body = parse_command(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11901 | return n; |
| 11902 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11903 | IF_BASH_FUNCTION(function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11904 | /* fall through */ |
| 11905 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11906 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11907 | goto out; |
| 11908 | } |
| 11909 | } |
| 11910 | out: |
| 11911 | *app = NULL; |
| 11912 | *vpp = NULL; |
| 11913 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11914 | n = stzalloc(sizeof(struct ncmd)); |
Denys Vlasenko | 57b7efb | 2018-04-10 01:20:26 +0200 | [diff] [blame] | 11915 | if (NCMD != 0) |
| 11916 | n->type = NCMD; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11917 | n->ncmd.linno = savelinno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11918 | n->ncmd.args = args; |
| 11919 | n->ncmd.assign = vars; |
| 11920 | n->ncmd.redirect = redir; |
| 11921 | return n; |
| 11922 | } |
| 11923 | |
| 11924 | static union node * |
| 11925 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11926 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11927 | union node *n1, *n2; |
| 11928 | union node *ap, **app; |
| 11929 | union node *cp, **cpp; |
| 11930 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11931 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11932 | int t; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11933 | int savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11934 | |
| 11935 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11936 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 11937 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11938 | savelinno = g_parsefile->linno; |
| 11939 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11940 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11941 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11942 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11943 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11944 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11945 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11946 | n1->type = NIF; |
| 11947 | n1->nif.test = list(0); |
| 11948 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11949 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11950 | n1->nif.ifpart = list(0); |
| 11951 | n2 = n1; |
| 11952 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11953 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11954 | n2 = n2->nif.elsepart; |
| 11955 | n2->type = NIF; |
| 11956 | n2->nif.test = list(0); |
| 11957 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11958 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11959 | n2->nif.ifpart = list(0); |
| 11960 | } |
| 11961 | if (lasttoken == TELSE) |
| 11962 | n2->nif.elsepart = list(0); |
| 11963 | else { |
| 11964 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11965 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11966 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11967 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11968 | break; |
| 11969 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11970 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11971 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11972 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11973 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11974 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11975 | got = readtoken(); |
| 11976 | if (got != TDO) { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11977 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got], |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11978 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11979 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11980 | } |
| 11981 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11982 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11983 | break; |
| 11984 | } |
| 11985 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11986 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11987 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11988 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11989 | n1->type = NFOR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11990 | n1->nfor.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11991 | n1->nfor.var = wordtext; |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 11992 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11993 | if (readtoken() == TIN) { |
| 11994 | app = ≈ |
| 11995 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11996 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11997 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11998 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11999 | n2->narg.text = wordtext; |
| 12000 | n2->narg.backquote = backquotelist; |
| 12001 | *app = n2; |
| 12002 | app = &n2->narg.next; |
| 12003 | } |
| 12004 | *app = NULL; |
| 12005 | n1->nfor.args = ap; |
| 12006 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12007 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12008 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12009 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12010 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12011 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12012 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12013 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12014 | n1->nfor.args = n2; |
| 12015 | /* |
| 12016 | * Newline or semicolon here is optional (but note |
| 12017 | * that the original Bourne shell only allowed NL). |
| 12018 | */ |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 12019 | if (lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12020 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12021 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12022 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12023 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12024 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12025 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12026 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12027 | break; |
| 12028 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12029 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12030 | n1->type = NCASE; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12031 | n1->ncase.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12032 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12033 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12034 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12035 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12036 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12037 | n2->narg.text = wordtext; |
| 12038 | n2->narg.backquote = backquotelist; |
Ron Yorston | 383b885 | 2015-08-03 13:46:25 +0100 | [diff] [blame] | 12039 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 12040 | if (readtoken() != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12041 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12042 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12043 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12044 | checkkwd = CHKNL | CHKKWD; |
| 12045 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12046 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12047 | if (lasttoken == TLP) |
| 12048 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12049 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12050 | cp->type = NCLIST; |
| 12051 | app = &cp->nclist.pattern; |
| 12052 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12053 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12054 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12055 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12056 | ap->narg.text = wordtext; |
| 12057 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12058 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12059 | break; |
| 12060 | app = &ap->narg.next; |
| 12061 | readtoken(); |
| 12062 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12063 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12064 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12065 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12066 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12067 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12068 | cpp = &cp->nclist.next; |
| 12069 | |
| 12070 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12071 | t = readtoken(); |
| 12072 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12073 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12074 | raise_error_unexpected_syntax(TENDCASE); |
| 12075 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12076 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12077 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12078 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12079 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12080 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12081 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12082 | n1->type = NSUBSHELL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12083 | n1->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12084 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12085 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12086 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12087 | break; |
| 12088 | case TBEGIN: |
| 12089 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12090 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12091 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12092 | IF_BASH_FUNCTION(case TFUNCTION:) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12093 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12094 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12095 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12096 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12097 | } |
| 12098 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12099 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12100 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12101 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12102 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12103 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12104 | checkkwd = CHKKWD | CHKALIAS; |
| 12105 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12106 | while (readtoken() == TREDIR) { |
| 12107 | *rpp = n2 = redirnode; |
| 12108 | rpp = &n2->nfile.next; |
| 12109 | parsefname(); |
| 12110 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12111 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12112 | *rpp = NULL; |
| 12113 | if (redir) { |
| 12114 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12115 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12116 | n2->type = NREDIR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12117 | n2->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12118 | n2->nredir.n = n1; |
| 12119 | n1 = n2; |
| 12120 | } |
| 12121 | n1->nredir.redirect = redir; |
| 12122 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12123 | return n1; |
| 12124 | } |
| 12125 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12126 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 12127 | static int |
| 12128 | decode_dollar_squote(void) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12129 | { |
| 12130 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 12131 | int c, cnt; |
| 12132 | char *p; |
| 12133 | char buf[4]; |
| 12134 | |
| 12135 | c = pgetc(); |
| 12136 | p = strchr(C_escapes, c); |
| 12137 | if (p) { |
| 12138 | buf[0] = c; |
| 12139 | p = buf; |
| 12140 | cnt = 3; |
| 12141 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 12142 | do { |
| 12143 | c = pgetc(); |
| 12144 | *++p = c; |
| 12145 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 12146 | pungetc(); |
| 12147 | } else if (c == 'x') { /* \xHH */ |
| 12148 | do { |
| 12149 | c = pgetc(); |
| 12150 | *++p = c; |
| 12151 | } while (isxdigit(c) && --cnt); |
| 12152 | pungetc(); |
| 12153 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 12154 | c = 'x'; |
| 12155 | goto unrecognized; |
| 12156 | } |
| 12157 | } else { /* simple seq like \\ or \t */ |
| 12158 | p++; |
| 12159 | } |
| 12160 | *p = '\0'; |
| 12161 | p = buf; |
| 12162 | c = bb_process_escape_sequence((void*)&p); |
| 12163 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 12164 | if (c != '\'' && c != '"') { |
| 12165 | unrecognized: |
| 12166 | c |= 0x100; /* "please encode \, then me" */ |
| 12167 | } |
| 12168 | } |
| 12169 | return c; |
| 12170 | } |
| 12171 | #endif |
| 12172 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12173 | /* Used by expandstr to get here-doc like behaviour. */ |
| 12174 | #define FAKEEOFMARK ((char*)(uintptr_t)1) |
| 12175 | |
| 12176 | static ALWAYS_INLINE int |
| 12177 | realeofmark(const char *eofmark) |
| 12178 | { |
| 12179 | return eofmark && eofmark != FAKEEOFMARK; |
| 12180 | } |
| 12181 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12182 | /* |
| 12183 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 12184 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 12185 | * word which marks the end of the document and striptabs is true if |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12186 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12187 | * is the first character of the input token or document. |
| 12188 | * |
| 12189 | * Because C does not have internal subroutines, I have simulated them |
| 12190 | * using goto's to implement the subroutine linkage. The following macros |
| 12191 | * will run code that appears at the end of readtoken1. |
| 12192 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12193 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 12194 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 12195 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 12196 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 12197 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 12198 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12199 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12200 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 12201 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12202 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12203 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12204 | char *out; |
Denys Vlasenko | 50e6d42 | 2016-09-30 11:35:54 +0200 | [diff] [blame] | 12205 | size_t len; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12206 | struct nodelist *bqlist; |
| 12207 | smallint quotef; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12208 | smallint oldstyle; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12209 | smallint pssyntax; /* we are expanding a prompt string */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12210 | IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12211 | /* syntax stack */ |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12212 | struct synstack synbase = { }; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12213 | struct synstack *synstack = &synbase; |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12214 | |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12215 | #if ENABLE_ASH_EXPAND_PRMT |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12216 | pssyntax = (syntax == PSSYNTAX); |
| 12217 | if (pssyntax) |
| 12218 | syntax = DQSYNTAX; |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12219 | #else |
| 12220 | pssyntax = 0; /* constant */ |
| 12221 | #endif |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12222 | synstack->syntax = syntax; |
| 12223 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12224 | if (syntax == DQSYNTAX) |
| 12225 | synstack->dblquote = 1; |
| 12226 | quotef = 0; |
| 12227 | bqlist = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12228 | |
| 12229 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12230 | loop: |
| 12231 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12232 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 12233 | for (;;) { /* until end of line or end of word */ |
| 12234 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12235 | switch (SIT(c, synstack->syntax)) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12236 | case CNL: /* '\n' */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12237 | if (synstack->syntax == BASESYNTAX |
| 12238 | && !synstack->varnest |
| 12239 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12240 | goto endword; /* exit outer loop */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12241 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12242 | USTPUTC(c, out); |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12243 | nlprompt(); |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12244 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12245 | goto loop; /* continue outer loop */ |
| 12246 | case CWORD: |
| 12247 | USTPUTC(c, out); |
| 12248 | break; |
| 12249 | case CCTL: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12250 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12251 | if (c == '\\' && bash_dollar_squote) { |
| 12252 | c = decode_dollar_squote(); |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12253 | if (c == '\0') { |
| 12254 | /* skip $'\000', $'\x00' (like bash) */ |
| 12255 | break; |
| 12256 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12257 | if (c & 0x100) { |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12258 | /* Unknown escape. Encode as '\z' */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12259 | c = (unsigned char)c; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12260 | if (eofmark == NULL || synstack->dblquote) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12261 | USTPUTC(CTLESC, out); |
| 12262 | USTPUTC('\\', out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12263 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12264 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12265 | #endif |
Denys Vlasenko | c4c2012 | 2018-04-02 13:29:20 +0200 | [diff] [blame] | 12266 | if (!eofmark || synstack->dblquote || synstack->varnest) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12267 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12268 | USTPUTC(c, out); |
| 12269 | break; |
| 12270 | case CBACK: /* backslash */ |
| 12271 | c = pgetc_without_PEOA(); |
| 12272 | if (c == PEOF) { |
| 12273 | USTPUTC(CTLESC, out); |
| 12274 | USTPUTC('\\', out); |
| 12275 | pungetc(); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12276 | } else { |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12277 | if (pssyntax && c == '$') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12278 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12279 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12280 | } |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12281 | /* Backslash is retained if we are in "str" |
| 12282 | * and next char isn't dquote-special. |
| 12283 | */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12284 | if (synstack->dblquote |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12285 | && c != '\\' |
| 12286 | && c != '`' |
| 12287 | && c != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12288 | && (c != '"' || (eofmark != NULL && !synstack->varnest)) |
| 12289 | && (c != '}' || !synstack->varnest) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12290 | ) { |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12291 | USTPUTC(CTLESC, out); /* protect '\' from glob */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12292 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12293 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12294 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 12295 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12296 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12297 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12298 | break; |
| 12299 | case CSQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12300 | synstack->syntax = SQSYNTAX; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12301 | quotemark: |
| 12302 | if (eofmark == NULL) { |
| 12303 | USTPUTC(CTLQUOTEMARK, out); |
| 12304 | } |
| 12305 | break; |
| 12306 | case CDQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12307 | synstack->syntax = DQSYNTAX; |
| 12308 | synstack->dblquote = 1; |
| 12309 | toggledq: |
| 12310 | if (synstack->varnest) |
| 12311 | synstack->innerdq ^= 1; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12312 | goto quotemark; |
| 12313 | case CENDQUOTE: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12314 | IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12315 | if (eofmark != NULL && synstack->varnest == 0) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12316 | USTPUTC(c, out); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12317 | break; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12318 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12319 | |
| 12320 | if (synstack->dqvarnest == 0) { |
| 12321 | synstack->syntax = BASESYNTAX; |
| 12322 | synstack->dblquote = 0; |
| 12323 | } |
| 12324 | |
| 12325 | quotef = 1; |
| 12326 | |
| 12327 | if (c == '"') |
| 12328 | goto toggledq; |
| 12329 | |
| 12330 | goto quotemark; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12331 | case CVAR: /* '$' */ |
| 12332 | PARSESUB(); /* parse substitution */ |
| 12333 | break; |
| 12334 | case CENDVAR: /* '}' */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12335 | if (!synstack->innerdq && synstack->varnest > 0) { |
| 12336 | if (!--synstack->varnest && synstack->varpushed) |
| 12337 | synstack_pop(&synstack); |
| 12338 | else if (synstack->dqvarnest > 0) |
| 12339 | synstack->dqvarnest--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12340 | c = CTLENDVAR; |
| 12341 | } |
| 12342 | USTPUTC(c, out); |
| 12343 | break; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12344 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12345 | case CLP: /* '(' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12346 | synstack->parenlevel++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12347 | USTPUTC(c, out); |
| 12348 | break; |
| 12349 | case CRP: /* ')' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12350 | if (synstack->parenlevel > 0) { |
| 12351 | synstack->parenlevel--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12352 | } else { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12353 | if (pgetc_eatbnl() == ')') { |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 12354 | c = CTLENDARI; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12355 | synstack_pop(&synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12356 | } else { |
| 12357 | /* |
| 12358 | * unbalanced parens |
| 12359 | * (don't 2nd guess - no error) |
| 12360 | */ |
| 12361 | pungetc(); |
| 12362 | } |
| 12363 | } |
| 12364 | USTPUTC(c, out); |
| 12365 | break; |
| 12366 | #endif |
| 12367 | case CBQUOTE: /* '`' */ |
Denys Vlasenko | 41fddb4 | 2018-04-01 16:38:32 +0200 | [diff] [blame] | 12368 | if (checkkwd & CHKEOFMARK) { |
| 12369 | quotef = 1; |
| 12370 | USTPUTC('`', out); |
| 12371 | break; |
| 12372 | } |
| 12373 | |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12374 | PARSEBACKQOLD(); |
| 12375 | break; |
| 12376 | case CENDFILE: |
| 12377 | goto endword; /* exit outer loop */ |
| 12378 | case CIGN: |
| 12379 | break; |
| 12380 | default: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12381 | if (synstack->varnest == 0) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12382 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12383 | if (c == '&') { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12384 | //Can't call pgetc_eatbnl() here, this requires three-deep pungetc() |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12385 | if (pgetc() == '>') |
| 12386 | c = 0x100 + '>'; /* flag &> */ |
| 12387 | pungetc(); |
| 12388 | } |
| 12389 | #endif |
| 12390 | goto endword; /* exit outer loop */ |
| 12391 | } |
| 12392 | IF_ASH_ALIAS(if (c != PEOA)) |
| 12393 | USTPUTC(c, out); |
| 12394 | } |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12395 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12396 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12397 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12398 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12399 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12400 | if (synstack->syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12401 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12402 | #endif |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12403 | if (synstack->syntax != BASESYNTAX && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12404 | raise_error_syntax("unterminated quoted string"); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12405 | if (synstack->varnest != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12406 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12407 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12408 | } |
| 12409 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12410 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12411 | out = stackblock(); |
| 12412 | if (eofmark == NULL) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12413 | if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12414 | && quotef == 0 |
| 12415 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12416 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12417 | PARSEREDIR(); /* passed as params: out, c */ |
| 12418 | lasttoken = TREDIR; |
| 12419 | return lasttoken; |
| 12420 | } |
| 12421 | /* else: non-number X seen, interpret it |
| 12422 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12423 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12424 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12425 | } |
| 12426 | quoteflag = quotef; |
| 12427 | backquotelist = bqlist; |
| 12428 | grabstackblock(len); |
| 12429 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12430 | lasttoken = TWORD; |
| 12431 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12432 | /* end of readtoken routine */ |
| 12433 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12434 | /* |
| 12435 | * Check to see whether we are at the end of the here document. When this |
| 12436 | * is called, c is set to the first character of the next input line. If |
| 12437 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 12438 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12439 | checkend: { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12440 | if (realeofmark(eofmark)) { |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12441 | int markloc; |
| 12442 | char *p; |
| 12443 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12444 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12445 | if (c == PEOA) |
| 12446 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12447 | #endif |
| 12448 | if (striptabs) { |
| 12449 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12450 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12451 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12452 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12453 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12454 | markloc = out - (char *)stackblock(); |
| 12455 | for (p = eofmark; STPUTC(c, out), *p; p++) { |
| 12456 | if (c != *p) |
| 12457 | goto more_heredoc; |
Denys Vlasenko | 35e349d | 2019-09-05 14:31:49 +0200 | [diff] [blame] | 12458 | /* FIXME: fails for backslash-newlined terminator: |
| 12459 | * cat <<EOF |
| 12460 | * ... |
| 12461 | * EO\ |
| 12462 | * F |
| 12463 | * (see heredoc_bkslash_newline2.tests) |
| 12464 | */ |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12465 | c = pgetc_without_PEOA(); |
| 12466 | } |
| 12467 | |
| 12468 | if (c == '\n' || c == PEOF) { |
| 12469 | c = PEOF; |
| 12470 | g_parsefile->linno++; |
| 12471 | needprompt = doprompt; |
| 12472 | } else { |
| 12473 | int len_here; |
| 12474 | |
| 12475 | more_heredoc: |
| 12476 | p = (char *)stackblock() + markloc + 1; |
| 12477 | len_here = out - p; |
| 12478 | |
| 12479 | if (len_here) { |
| 12480 | len_here -= (c >= PEOF); |
| 12481 | c = p[-1]; |
| 12482 | |
| 12483 | if (len_here) { |
| 12484 | char *str; |
| 12485 | |
| 12486 | str = alloca(len_here + 1); |
| 12487 | *(char *)mempcpy(str, p, len_here) = '\0'; |
| 12488 | |
| 12489 | pushstring(str, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12490 | } |
| 12491 | } |
| 12492 | } |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12493 | |
| 12494 | STADJUST((char *)stackblock() + markloc - out, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12495 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12496 | goto checkend_return; |
| 12497 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12498 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12499 | /* |
| 12500 | * Parse a redirection operator. The variable "out" points to a string |
| 12501 | * specifying the fd to be redirected. The variable "c" contains the |
| 12502 | * first character of the redirection operator. |
| 12503 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12504 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12505 | /* out is already checked to be a valid number or "" */ |
| 12506 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12507 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12508 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12509 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12510 | if (c == '>') { |
| 12511 | np->nfile.fd = 1; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12512 | c = pgetc_eatbnl(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12513 | if (c == '>') |
| 12514 | np->type = NAPPEND; |
| 12515 | else if (c == '|') |
| 12516 | np->type = NCLOBBER; |
| 12517 | else if (c == '&') |
| 12518 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12519 | /* it also can be NTO2 (>&file), but we can't figure it out yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12520 | else { |
| 12521 | np->type = NTO; |
| 12522 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12523 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12524 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12525 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12526 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 12527 | np->nfile.fd = 1; |
| 12528 | pgetc(); /* this is '>', no need to check */ |
| 12529 | np->type = NTO2; |
| 12530 | } |
| 12531 | #endif |
| 12532 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12533 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12534 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12535 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12536 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12537 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12538 | np = stzalloc(sizeof(struct nhere)); |
| 12539 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12540 | } |
| 12541 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12542 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12543 | heredoc->here = np; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12544 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12545 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12546 | heredoc->striptabs = 1; |
| 12547 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12548 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12549 | pungetc(); |
| 12550 | } |
| 12551 | break; |
| 12552 | |
| 12553 | case '&': |
| 12554 | np->type = NFROMFD; |
| 12555 | break; |
| 12556 | |
| 12557 | case '>': |
| 12558 | np->type = NFROMTO; |
| 12559 | break; |
| 12560 | |
| 12561 | default: |
| 12562 | np->type = NFROM; |
| 12563 | pungetc(); |
| 12564 | break; |
| 12565 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12566 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12567 | if (fd >= 0) |
| 12568 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12569 | redirnode = np; |
| 12570 | goto parseredir_return; |
| 12571 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12572 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12573 | /* |
| 12574 | * Parse a substitution. At this point, we have read the dollar sign |
| 12575 | * and nothing else. |
| 12576 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12577 | |
| 12578 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 12579 | * (assuming ascii char codes, as the original implementation did) */ |
| 12580 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12581 | (((unsigned)(c) - 33 < 32) \ |
| 12582 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12583 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12584 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12585 | int typeloc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12586 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12587 | c = pgetc_eatbnl(); |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12588 | if ((checkkwd & CHKEOFMARK) |
| 12589 | || c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12590 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12591 | ) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12592 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12593 | if (synstack->syntax != DQSYNTAX && c == '\'') |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12594 | bash_dollar_squote = 1; |
| 12595 | else |
| 12596 | #endif |
| 12597 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12598 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12599 | } else if (c == '(') { |
| 12600 | /* $(command) or $((arith)) */ |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12601 | if (pgetc_eatbnl() == '(') { |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12602 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12603 | PARSEARITH(); |
| 12604 | #else |
Denys Vlasenko | 4f8079d | 2017-07-17 17:11:48 +0200 | [diff] [blame] | 12605 | raise_error_syntax("support for $((arith)) is disabled"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12606 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12607 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12608 | pungetc(); |
| 12609 | PARSEBACKQNEW(); |
| 12610 | } |
| 12611 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12612 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12613 | smalluint newsyn = synstack->syntax; |
| 12614 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12615 | USTPUTC(CTLVAR, out); |
| 12616 | typeloc = out - (char *)stackblock(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12617 | STADJUST(1, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12618 | subtype = VSNORMAL; |
| 12619 | if (c == '{') { |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12620 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12621 | subtype = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12622 | } |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12623 | varname: |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12624 | if (is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12625 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12626 | do { |
| 12627 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12628 | c = pgetc_eatbnl(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12629 | } while (is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12630 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12631 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12632 | do { |
| 12633 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12634 | c = pgetc_eatbnl(); |
Denys Vlasenko | c2ce888 | 2020-02-17 10:15:35 +0100 | [diff] [blame] | 12635 | } while (!subtype && isdigit(c)); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12636 | } else if (c != '}') { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12637 | /* $[{[#]]<specialchar>[}] */ |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12638 | int cc = c; |
| 12639 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12640 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12641 | if (!subtype && cc == '#') { |
| 12642 | subtype = VSLENGTH; |
| 12643 | if (c == '_' || isalnum(c)) |
| 12644 | goto varname; |
| 12645 | cc = c; |
| 12646 | c = pgetc_eatbnl(); |
| 12647 | if (cc == '}' || c != '}') { |
| 12648 | pungetc(); |
| 12649 | subtype = 0; |
| 12650 | c = cc; |
| 12651 | cc = '#'; |
| 12652 | } |
| 12653 | } |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12654 | |
| 12655 | if (!is_special(cc)) { |
| 12656 | if (subtype == VSLENGTH) |
| 12657 | subtype = 0; |
| 12658 | goto badsub; |
| 12659 | } |
| 12660 | |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12661 | USTPUTC(cc, out); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12662 | } else |
| 12663 | goto badsub; |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12664 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12665 | if (c != '}' && subtype == VSLENGTH) { |
| 12666 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 12667 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12668 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12669 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12670 | if (subtype == 0) { |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12671 | static const char types[] ALIGN1 = "}-+?="; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12672 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 12673 | /* c == first char after VAR */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12674 | int cc = c; |
| 12675 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12676 | switch (c) { |
| 12677 | case ':': |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12678 | c = pgetc_eatbnl(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12679 | #if BASH_SUBSTR |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12680 | /* This check is only needed to not misinterpret |
| 12681 | * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD} |
| 12682 | * constructs. |
| 12683 | */ |
| 12684 | if (!strchr(types, c)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12685 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12686 | pungetc(); |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12687 | break; /* "goto badsub" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12688 | } |
| 12689 | #endif |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12690 | subtype = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12691 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12692 | default: { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12693 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12694 | if (p == NULL) |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12695 | break; |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12696 | subtype |= p - types + VSNORMAL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12697 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12698 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12699 | case '%': |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12700 | case '#': |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12701 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12702 | c = pgetc_eatbnl(); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12703 | if (c == cc) |
| 12704 | subtype++; |
| 12705 | else |
| 12706 | pungetc(); |
| 12707 | |
| 12708 | newsyn = BASESYNTAX; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12709 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12710 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12711 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 12712 | /* ${v/[/]pattern/repl} */ |
| 12713 | //TODO: encode pattern and repl separately. |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12714 | // Currently cases like: v=1;echo ${v/$((1/1))/ONE} |
| 12715 | // are broken (should print "ONE") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12716 | subtype = VSREPLACE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12717 | newsyn = BASESYNTAX; |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12718 | c = pgetc_eatbnl(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12719 | if (c != '/') |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12720 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12721 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12722 | break; |
| 12723 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12724 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12725 | } else { |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12726 | badsub: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12727 | pungetc(); |
| 12728 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12729 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12730 | if (newsyn == ARISYNTAX) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12731 | newsyn = DQSYNTAX; |
| 12732 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12733 | if ((newsyn != synstack->syntax || synstack->innerdq) |
| 12734 | && subtype != VSNORMAL |
| 12735 | ) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12736 | synstack_push(&synstack, |
| 12737 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 12738 | newsyn); |
| 12739 | |
| 12740 | synstack->varpushed = 1; |
| 12741 | synstack->dblquote = newsyn != BASESYNTAX; |
| 12742 | } |
| 12743 | |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12744 | ((unsigned char *)stackblock())[typeloc] = subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12745 | if (subtype != VSNORMAL) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12746 | synstack->varnest++; |
| 12747 | if (synstack->dblquote) |
| 12748 | synstack->dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12749 | } |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12750 | STPUTC('=', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12751 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12752 | goto parsesub_return; |
| 12753 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12754 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12755 | /* |
| 12756 | * Called to parse command substitutions. Newstyle is set if the command |
| 12757 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 12758 | * list of commands (passed by reference), and savelen is the number of |
| 12759 | * characters on the top of the stack which must be preserved. |
| 12760 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12761 | parsebackq: { |
| 12762 | struct nodelist **nlpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12763 | union node *n; |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 12764 | char *str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12765 | size_t savelen; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12766 | struct heredoc *saveheredoclist; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12767 | smallint saveprompt = 0; |
| 12768 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12769 | str = NULL; |
| 12770 | savelen = out - (char *)stackblock(); |
| 12771 | if (savelen > 0) { |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12772 | /* |
| 12773 | * FIXME: this can allocate very large block on stack and SEGV. |
| 12774 | * Example: |
| 12775 | * echo "..<100kbytes>..`true` $(true) `true` ..." |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 12776 | * allocates 100kb for every command subst. With about |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12777 | * a hundred command substitutions stack overflows. |
| 12778 | * With larger prepended string, SEGV happens sooner. |
| 12779 | */ |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 12780 | str = alloca(savelen); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12781 | memcpy(str, stackblock(), savelen); |
| 12782 | } |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12783 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12784 | if (oldstyle) { |
| 12785 | /* We must read until the closing backquote, giving special |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 12786 | * treatment to some slashes, and then push the string and |
| 12787 | * reread it as input, interpreting it normally. |
| 12788 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12789 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12790 | size_t psavelen; |
| 12791 | char *pstr; |
| 12792 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12793 | STARTSTACKSTR(pout); |
| 12794 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12795 | int pc; |
| 12796 | |
| 12797 | setprompt_if(needprompt, 2); |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12798 | pc = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12799 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12800 | case '`': |
| 12801 | goto done; |
| 12802 | |
| 12803 | case '\\': |
Denys Vlasenko | 777a635 | 2020-09-29 16:25:32 +0200 | [diff] [blame] | 12804 | pc = pgetc(); /* not pgetc_eatbnl! */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12805 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12806 | && (!synstack->dblquote || pc != '"') |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 12807 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12808 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 12809 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12810 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12811 | break; |
| 12812 | } |
| 12813 | /* fall through */ |
| 12814 | |
| 12815 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12816 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12817 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12818 | |
| 12819 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12820 | nlnoprompt(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12821 | break; |
| 12822 | |
| 12823 | default: |
| 12824 | break; |
| 12825 | } |
| 12826 | STPUTC(pc, pout); |
| 12827 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12828 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12829 | STPUTC('\0', pout); |
| 12830 | psavelen = pout - (char *)stackblock(); |
| 12831 | if (psavelen > 0) { |
| 12832 | pstr = grabstackstr(pout); |
| 12833 | setinputstring(pstr); |
| 12834 | } |
| 12835 | } |
| 12836 | nlpp = &bqlist; |
| 12837 | while (*nlpp) |
| 12838 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12839 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 12840 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12841 | |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12842 | saveheredoclist = heredoclist; |
| 12843 | heredoclist = NULL; |
| 12844 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12845 | if (oldstyle) { |
| 12846 | saveprompt = doprompt; |
| 12847 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12848 | } |
| 12849 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12850 | n = list(2); |
| 12851 | |
| 12852 | if (oldstyle) |
| 12853 | doprompt = saveprompt; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12854 | else { |
| 12855 | if (readtoken() != TRP) |
| 12856 | raise_error_unexpected_syntax(TRP); |
| 12857 | setinputstring(nullstr); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12858 | } |
| 12859 | |
Denys Vlasenko | 9a1a659 | 2020-02-22 16:39:27 +0100 | [diff] [blame] | 12860 | parseheredoc(); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12861 | heredoclist = saveheredoclist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12862 | |
| 12863 | (*nlpp)->n = n; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12864 | /* Start reading from old file again. */ |
| 12865 | popfile(); |
| 12866 | /* Ignore any pushed back tokens left from the backquote parsing. */ |
| 12867 | if (oldstyle) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12868 | tokpushback = 0; |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 12869 | out = growstackto(savelen + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12870 | if (str) { |
| 12871 | memcpy(out, str, savelen); |
| 12872 | STADJUST(savelen, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12873 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12874 | USTPUTC(CTLBACKQ, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12875 | if (oldstyle) |
| 12876 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12877 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12878 | } |
| 12879 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12880 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12881 | /* |
| 12882 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 12883 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12884 | parsearith: { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12885 | |
| 12886 | synstack_push(&synstack, |
| 12887 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 12888 | ARISYNTAX); |
| 12889 | synstack->dblquote = 1; |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 12890 | USTPUTC(CTLARI, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12891 | goto parsearith_return; |
| 12892 | } |
| 12893 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12894 | } /* end of readtoken */ |
| 12895 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12896 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12897 | * Read the next input token. |
| 12898 | * If the token is a word, we set backquotelist to the list of cmds in |
| 12899 | * backquotes. We set quoteflag to true if any part of the word was |
| 12900 | * quoted. |
| 12901 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 12902 | * the redirection. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12903 | * |
| 12904 | * [Change comment: here documents and internal procedures] |
| 12905 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 12906 | * word parsing code into a separate routine. In this case, readtoken |
| 12907 | * doesn't need to have any internal procedures, but parseword does. |
| 12908 | * We could also make parseoperator in essence the main routine, and |
| 12909 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12910 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12911 | #define NEW_xxreadtoken |
| 12912 | #ifdef NEW_xxreadtoken |
| 12913 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12914 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12915 | '\n', '(', ')', /* singles */ |
| 12916 | '&', '|', ';', /* doubles */ |
| 12917 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12918 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12919 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12920 | #define xxreadtoken_singles 3 |
| 12921 | #define xxreadtoken_doubles 3 |
| 12922 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12923 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12924 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 12925 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 12926 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12927 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12928 | }; |
| 12929 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12930 | static int |
| 12931 | xxreadtoken(void) |
| 12932 | { |
| 12933 | int c; |
| 12934 | |
| 12935 | if (tokpushback) { |
| 12936 | tokpushback = 0; |
| 12937 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12938 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12939 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12940 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12941 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 12942 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12943 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12944 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12945 | if (c == '#') { |
| 12946 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 12947 | continue; |
| 12948 | pungetc(); |
| 12949 | } else if (c == '\\') { |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12950 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12951 | } else { |
| 12952 | const char *p; |
| 12953 | |
| 12954 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 12955 | if (c != PEOF) { |
| 12956 | if (c == '\n') { |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12957 | nlnoprompt(); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12958 | } |
| 12959 | |
| 12960 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12961 | if (p == NULL) |
| 12962 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12963 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12964 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 12965 | int cc = pgetc_eatbnl(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12966 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12967 | p += xxreadtoken_doubles + 1; |
| 12968 | } else { |
| 12969 | pungetc(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12970 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12971 | if (c == '&' && cc == '>') /* &> */ |
| 12972 | break; /* return readtoken1(...) */ |
| 12973 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12974 | } |
| 12975 | } |
| 12976 | } |
| 12977 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 12978 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12979 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12980 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12981 | |
| 12982 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12983 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12984 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12985 | #define RETURN(token) return lasttoken = token |
| 12986 | static int |
| 12987 | xxreadtoken(void) |
| 12988 | { |
| 12989 | int c; |
| 12990 | |
| 12991 | if (tokpushback) { |
| 12992 | tokpushback = 0; |
| 12993 | return lasttoken; |
| 12994 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12995 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12996 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12997 | c = pgetc_eatbnl(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12998 | switch (c) { |
| 12999 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 13000 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13001 | continue; |
| 13002 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 13003 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 13004 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13005 | pungetc(); |
| 13006 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13007 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 13008 | nlnoprompt(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13009 | RETURN(TNL); |
| 13010 | case PEOF: |
| 13011 | RETURN(TEOF); |
| 13012 | case '&': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13013 | if (pgetc_eatbnl() == '&') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13014 | RETURN(TAND); |
| 13015 | pungetc(); |
| 13016 | RETURN(TBACKGND); |
| 13017 | case '|': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13018 | if (pgetc_eatbnl() == '|') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13019 | RETURN(TOR); |
| 13020 | pungetc(); |
| 13021 | RETURN(TPIPE); |
| 13022 | case ';': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13023 | if (pgetc_eatbnl() == ';') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13024 | RETURN(TENDCASE); |
| 13025 | pungetc(); |
| 13026 | RETURN(TSEMI); |
| 13027 | case '(': |
| 13028 | RETURN(TLP); |
| 13029 | case ')': |
| 13030 | RETURN(TRP); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13031 | } |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13032 | break; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13033 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13034 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 13035 | #undef RETURN |
| 13036 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13037 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13038 | |
| 13039 | static int |
| 13040 | readtoken(void) |
| 13041 | { |
| 13042 | int t; |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13043 | int kwd = checkkwd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13044 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13045 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13046 | #endif |
| 13047 | |
| 13048 | #if ENABLE_ASH_ALIAS |
| 13049 | top: |
| 13050 | #endif |
| 13051 | |
| 13052 | t = xxreadtoken(); |
| 13053 | |
| 13054 | /* |
| 13055 | * eat newlines |
| 13056 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13057 | if (kwd & CHKNL) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13058 | while (t == TNL) { |
| 13059 | parseheredoc(); |
| 13060 | t = xxreadtoken(); |
| 13061 | } |
| 13062 | } |
| 13063 | |
| 13064 | if (t != TWORD || quoteflag) { |
| 13065 | goto out; |
| 13066 | } |
| 13067 | |
| 13068 | /* |
| 13069 | * check for keywords |
| 13070 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13071 | if (kwd & CHKKWD) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13072 | const char *const *pp; |
| 13073 | |
| 13074 | pp = findkwd(wordtext); |
| 13075 | if (pp) { |
| 13076 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13077 | TRACE(("keyword '%s' recognized\n", tokname_array[t])); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13078 | goto out; |
| 13079 | } |
| 13080 | } |
| 13081 | |
| 13082 | if (checkkwd & CHKALIAS) { |
| 13083 | #if ENABLE_ASH_ALIAS |
| 13084 | struct alias *ap; |
| 13085 | ap = lookupalias(wordtext, 1); |
| 13086 | if (ap != NULL) { |
| 13087 | if (*ap->val) { |
| 13088 | pushstring(ap->val, ap); |
| 13089 | } |
| 13090 | goto top; |
| 13091 | } |
| 13092 | #endif |
| 13093 | } |
| 13094 | out: |
| 13095 | checkkwd = 0; |
| 13096 | #if DEBUG |
| 13097 | if (!alreadyseen) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13098 | TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13099 | else |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13100 | TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13101 | #endif |
| 13102 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13103 | } |
| 13104 | |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13105 | static int |
Ron Yorston | 6bd2fab | 2015-10-29 11:30:22 +0000 | [diff] [blame] | 13106 | peektoken(void) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13107 | { |
| 13108 | int t; |
| 13109 | |
| 13110 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13111 | tokpushback = 1; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13112 | return t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13113 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13114 | |
| 13115 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13116 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 13117 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13118 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13119 | static union node * |
| 13120 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13121 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13122 | tokpushback = 0; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13123 | checkkwd = 0; |
| 13124 | heredoclist = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13125 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13126 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13127 | needprompt = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13128 | return list(1); |
| 13129 | } |
| 13130 | |
| 13131 | /* |
| 13132 | * Input any here documents. |
| 13133 | */ |
| 13134 | static void |
| 13135 | parseheredoc(void) |
| 13136 | { |
| 13137 | struct heredoc *here; |
| 13138 | union node *n; |
| 13139 | |
| 13140 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 13141 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13142 | |
| 13143 | while (here) { |
Christoph Schulz | 03ad7ae | 2018-11-20 17:45:52 +0100 | [diff] [blame] | 13144 | tokpushback = 0; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13145 | setprompt_if(needprompt, 2); |
Denys Vlasenko | acf79f9 | 2020-02-14 16:12:06 +0100 | [diff] [blame] | 13146 | if (here->here->type == NHERE) |
| 13147 | readtoken1(pgetc(), SQSYNTAX, here->eofmark, here->striptabs); |
| 13148 | else |
| 13149 | readtoken1(pgetc_eatbnl(), DQSYNTAX, here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13150 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13151 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13152 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13153 | n->narg.text = wordtext; |
| 13154 | n->narg.backquote = backquotelist; |
| 13155 | here->here->nhere.doc = n; |
| 13156 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13157 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13158 | } |
| 13159 | |
| 13160 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13161 | static const char * |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13162 | expandstr(const char *ps, int syntax_type) |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13163 | { |
| 13164 | union node n; |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13165 | int saveprompt; |
Ron Yorston | 48645b8 | 2019-04-18 09:48:13 +0100 | [diff] [blame] | 13166 | struct parsefile *file_stop = g_parsefile; |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13167 | volatile int saveint; |
| 13168 | struct jmploc *volatile savehandler = exception_handler; |
| 13169 | struct jmploc jmploc; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13170 | const char *volatile result; |
| 13171 | int err; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13172 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13173 | /* XXX Fix (char *) cast. */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13174 | setinputstring((char *)ps); |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13175 | |
| 13176 | saveprompt = doprompt; |
| 13177 | doprompt = 0; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13178 | result = ps; |
| 13179 | |
| 13180 | SAVE_INT(saveint); |
| 13181 | err = setjmp(jmploc.loc); |
| 13182 | if (err) |
| 13183 | goto out; |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13184 | |
| 13185 | /* readtoken1() might die horribly. |
Denys Vlasenko | 3c183a8 | 2017-10-12 19:35:42 +0200 | [diff] [blame] | 13186 | * Try a prompt with syntactically wrong command: |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13187 | * PS1='$(date "+%H:%M:%S) > ' |
| 13188 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13189 | exception_handler = &jmploc; |
| 13190 | readtoken1(pgetc(), syntax_type, FAKEEOFMARK, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13191 | |
| 13192 | n.narg.type = NARG; |
| 13193 | n.narg.next = NULL; |
| 13194 | n.narg.text = wordtext; |
| 13195 | n.narg.backquote = backquotelist; |
| 13196 | |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13197 | /* expandarg() might fail too: |
| 13198 | * PS1='$((123+))' |
| 13199 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13200 | expandarg(&n, NULL, EXP_QUOTED); |
| 13201 | result = stackblock(); |
| 13202 | |
| 13203 | out: |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13204 | exception_handler = savehandler; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13205 | if (err && exception_type != EXERROR) |
| 13206 | longjmp(exception_handler->loc, 1); |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13207 | RESTORE_INT(saveint); |
| 13208 | |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13209 | doprompt = saveprompt; |
| 13210 | /* Try: PS1='`xxx(`' */ |
| 13211 | unwindfiles(file_stop); |
| 13212 | |
| 13213 | return result; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13214 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13215 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13216 | static inline int |
| 13217 | parser_eof(void) |
| 13218 | { |
| 13219 | return tokpushback && lasttoken == TEOF; |
| 13220 | } |
| 13221 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13222 | /* |
| 13223 | * Execute a command or commands contained in a string. |
| 13224 | */ |
| 13225 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13226 | evalstring(char *s, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13227 | { |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13228 | struct jmploc *volatile savehandler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13229 | struct jmploc jmploc; |
| 13230 | int ex; |
| 13231 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13232 | union node *n; |
| 13233 | struct stackmark smark; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13234 | int status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13235 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13236 | s = sstrdup(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13237 | setinputstring(s); |
| 13238 | setstackmark(&smark); |
| 13239 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13240 | status = 0; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13241 | /* On exception inside execution loop, we must popfile(). |
| 13242 | * Try interactively: |
| 13243 | * readonly a=a |
| 13244 | * command eval "a=b" # throws "is read only" error |
| 13245 | * "command BLTIN" is not supposed to abort (even in non-interactive use). |
| 13246 | * But if we skip popfile(), we hit EOF in eval's string, and exit. |
| 13247 | */ |
| 13248 | savehandler = exception_handler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13249 | ex = setjmp(jmploc.loc); |
| 13250 | if (ex) |
| 13251 | goto out; |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13252 | exception_handler = &jmploc; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13253 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13254 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13255 | int i; |
| 13256 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13257 | i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT)); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13258 | if (n) |
| 13259 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13260 | popstackmark(&smark); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13261 | if (evalskip) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13262 | break; |
| 13263 | } |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13264 | out: |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13265 | popstackmark(&smark); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13266 | popfile(); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13267 | stunalloc(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13268 | |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13269 | exception_handler = savehandler; |
| 13270 | if (ex) |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 13271 | longjmp(exception_handler->loc, ex); |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13272 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13273 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13274 | } |
| 13275 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13276 | /* |
| 13277 | * The eval command. |
| 13278 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13279 | static int FAST_FUNC |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13280 | evalcmd(int argc UNUSED_PARAM, char **argv, int flags) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13281 | { |
| 13282 | char *p; |
| 13283 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13284 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13285 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13286 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13287 | argv += 2; |
| 13288 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13289 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13290 | for (;;) { |
| 13291 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13292 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13293 | if (p == NULL) |
| 13294 | break; |
| 13295 | STPUTC(' ', concat); |
| 13296 | } |
| 13297 | STPUTC('\0', concat); |
| 13298 | p = grabstackstr(concat); |
| 13299 | } |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13300 | return evalstring(p, flags & EV_TESTED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13301 | } |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13302 | return 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13303 | } |
| 13304 | |
| 13305 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 13306 | * Read and execute commands. |
| 13307 | * "Top" is nonzero for the top level command loop; |
| 13308 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13309 | */ |
| 13310 | static int |
| 13311 | cmdloop(int top) |
| 13312 | { |
| 13313 | union node *n; |
| 13314 | struct stackmark smark; |
| 13315 | int inter; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13316 | int status = 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13317 | int numeof = 0; |
| 13318 | |
| 13319 | TRACE(("cmdloop(%d) called\n", top)); |
| 13320 | for (;;) { |
| 13321 | int skip; |
| 13322 | |
| 13323 | setstackmark(&smark); |
| 13324 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 13325 | if (doing_jobctl) |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 13326 | showjobs(SHOW_CHANGED|SHOW_STDERR); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13327 | #endif |
| 13328 | inter = 0; |
| 13329 | if (iflag && top) { |
| 13330 | inter++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13331 | chkmail(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13332 | } |
| 13333 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 13334 | #if DEBUG |
| 13335 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 13336 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 13337 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13338 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13339 | if (!top || numeof >= 50) |
| 13340 | break; |
| 13341 | if (!stoppedjobs()) { |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13342 | if (!Iflag) { |
| 13343 | if (iflag) { |
| 13344 | newline_and_flush(stderr); |
| 13345 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13346 | break; |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13347 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13348 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 13349 | } |
| 13350 | numeof++; |
| 13351 | } else if (nflag == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13352 | int i; |
| 13353 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 13354 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 13355 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13356 | numeof = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13357 | i = evaltree(n, 0); |
| 13358 | if (n) |
| 13359 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13360 | } |
| 13361 | popstackmark(&smark); |
| 13362 | skip = evalskip; |
| 13363 | |
| 13364 | if (skip) { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 13365 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 13366 | break; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13367 | } |
| 13368 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13369 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13370 | } |
| 13371 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13372 | /* |
| 13373 | * Take commands from a file. To be compatible we should do a path |
| 13374 | * search for the file, which is necessary to find sub-commands. |
| 13375 | */ |
| 13376 | static char * |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13377 | find_dot_file(char *basename) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13378 | { |
| 13379 | char *fullname; |
| 13380 | const char *path = pathval(); |
| 13381 | struct stat statb; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13382 | int len; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13383 | |
| 13384 | /* don't try this for absolute or relative paths */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13385 | if (strchr(basename, '/')) |
| 13386 | return basename; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13387 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13388 | while ((len = padvance(&path, basename)) >= 0) { |
| 13389 | fullname = stackblock(); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13390 | if ((!pathopt || *pathopt == 'f') |
| 13391 | && !stat(fullname, &statb) && S_ISREG(statb.st_mode) |
| 13392 | ) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13393 | /* This will be freed by the caller. */ |
| 13394 | return stalloc(len); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13395 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13396 | } |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13397 | /* not found in PATH */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13398 | |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13399 | #if ENABLE_ASH_BASH_SOURCE_CURDIR |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13400 | return basename; |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13401 | #else |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13402 | ash_msg_and_raise_error("%s: not found", basename); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13403 | /* NOTREACHED */ |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13404 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13405 | } |
| 13406 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13407 | static int FAST_FUNC |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13408 | dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13409 | { |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13410 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 13411 | int status = 0; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13412 | char *fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13413 | char **argv; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13414 | char *args_need_save; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13415 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13416 | |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 13417 | //??? |
| 13418 | // struct strlist *sp; |
| 13419 | // for (sp = cmdenviron; sp; sp = sp->next) |
| 13420 | // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13421 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13422 | nextopt(nullstr); /* handle possible "--" */ |
| 13423 | argv = argptr; |
| 13424 | |
| 13425 | if (!argv[0]) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13426 | /* bash says: "bash: .: filename argument required" */ |
| 13427 | return 2; /* bash compat */ |
| 13428 | } |
| 13429 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13430 | /* This aborts if file isn't found, which is POSIXly correct. |
| 13431 | * bash returns exitcode 1 instead. |
| 13432 | */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13433 | fullname = find_dot_file(argv[0]); |
| 13434 | argv++; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13435 | args_need_save = argv[0]; |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 13436 | if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13437 | int argc; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13438 | saveparam = shellparam; |
| 13439 | shellparam.malloced = 0; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13440 | argc = 1; |
| 13441 | while (argv[argc]) |
| 13442 | argc++; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13443 | shellparam.nparam = argc; |
| 13444 | shellparam.p = argv; |
| 13445 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13446 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13447 | /* This aborts if file can't be opened, which is POSIXly correct. |
| 13448 | * bash returns exitcode 1 instead. |
| 13449 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13450 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 13451 | commandname = fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13452 | status = cmdloop(0); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13453 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13454 | |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13455 | if (args_need_save) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13456 | freeparam(&shellparam); |
| 13457 | shellparam = saveparam; |
| 13458 | }; |
| 13459 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13460 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13461 | } |
| 13462 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13463 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13464 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13465 | { |
| 13466 | if (stoppedjobs()) |
| 13467 | return 0; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13468 | |
Denys Vlasenko | 970470e | 2020-02-14 17:32:22 +0100 | [diff] [blame] | 13469 | if (argv[1]) |
| 13470 | savestatus = number(argv[1]); |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13471 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13472 | raise_exception(EXEXIT); |
| 13473 | /* NOTREACHED */ |
| 13474 | } |
| 13475 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13476 | /* |
| 13477 | * Read a file containing shell functions. |
| 13478 | */ |
| 13479 | static void |
| 13480 | readcmdfile(char *name) |
| 13481 | { |
| 13482 | setinputfile(name, INPUT_PUSH_FILE); |
| 13483 | cmdloop(0); |
| 13484 | popfile(); |
| 13485 | } |
| 13486 | |
| 13487 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13488 | /* ============ find_command inplementation */ |
| 13489 | |
| 13490 | /* |
| 13491 | * Resolve a command name. If you change this routine, you may have to |
| 13492 | * change the shellexec routine as well. |
| 13493 | */ |
| 13494 | static void |
| 13495 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 13496 | { |
| 13497 | struct tblentry *cmdp; |
| 13498 | int idx; |
| 13499 | int prev; |
| 13500 | char *fullname; |
| 13501 | struct stat statb; |
| 13502 | int e; |
| 13503 | int updatetbl; |
| 13504 | struct builtincmd *bcmd; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13505 | int len; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13506 | |
| 13507 | /* If name contains a slash, don't use PATH or hash table */ |
| 13508 | if (strchr(name, '/') != NULL) { |
| 13509 | entry->u.index = -1; |
| 13510 | if (act & DO_ABS) { |
| 13511 | while (stat(name, &statb) < 0) { |
| 13512 | #ifdef SYSV |
| 13513 | if (errno == EINTR) |
| 13514 | continue; |
| 13515 | #endif |
| 13516 | entry->cmdtype = CMDUNKNOWN; |
| 13517 | return; |
| 13518 | } |
| 13519 | } |
| 13520 | entry->cmdtype = CMDNORMAL; |
| 13521 | return; |
| 13522 | } |
| 13523 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13524 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13525 | |
| 13526 | updatetbl = (path == pathval()); |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13527 | if (!updatetbl) |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13528 | act |= DO_ALTPATH; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13529 | |
| 13530 | /* If name is in the table, check answer will be ok */ |
| 13531 | cmdp = cmdlookup(name, 0); |
| 13532 | if (cmdp != NULL) { |
| 13533 | int bit; |
| 13534 | |
| 13535 | switch (cmdp->cmdtype) { |
| 13536 | default: |
| 13537 | #if DEBUG |
| 13538 | abort(); |
| 13539 | #endif |
| 13540 | case CMDNORMAL: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13541 | bit = DO_ALTPATH | DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13542 | break; |
| 13543 | case CMDFUNCTION: |
| 13544 | bit = DO_NOFUNC; |
| 13545 | break; |
| 13546 | case CMDBUILTIN: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13547 | bit = IS_BUILTIN_REGULAR(cmdp->param.cmd) ? 0 : DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13548 | break; |
| 13549 | } |
| 13550 | if (act & bit) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13551 | if (act & bit & DO_REGBLTIN) |
| 13552 | goto fail; |
| 13553 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13554 | updatetbl = 0; |
| 13555 | cmdp = NULL; |
| 13556 | } else if (cmdp->rehash == 0) |
| 13557 | /* if not invalidated by cd, we're done */ |
| 13558 | goto success; |
| 13559 | } |
| 13560 | |
| 13561 | /* If %builtin not in path, check for builtin next */ |
| 13562 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13563 | if (bcmd) { |
| 13564 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 13565 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13566 | if (act & DO_ALTPATH) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13567 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13568 | if (builtinloc <= 0) |
| 13569 | goto builtin_success; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13570 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13571 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13572 | if (act & DO_REGBLTIN) |
| 13573 | goto fail; |
| 13574 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13575 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 13576 | { |
| 13577 | int applet_no = find_applet_by_name(name); |
| 13578 | if (applet_no >= 0) { |
| 13579 | entry->cmdtype = CMDNORMAL; |
| 13580 | entry->u.index = -2 - applet_no; |
| 13581 | return; |
| 13582 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13583 | } |
| 13584 | #endif |
| 13585 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13586 | /* We have to search path. */ |
| 13587 | prev = -1; /* where to start */ |
| 13588 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 13589 | if (cmdp->cmdtype == CMDBUILTIN) |
| 13590 | prev = builtinloc; |
| 13591 | else |
| 13592 | prev = cmdp->param.index; |
| 13593 | } |
| 13594 | |
| 13595 | e = ENOENT; |
| 13596 | idx = -1; |
| 13597 | loop: |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13598 | while ((len = padvance(&path, name)) >= 0) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13599 | const char *lpathopt = pathopt; |
| 13600 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13601 | fullname = stackblock(); |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13602 | idx++; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13603 | if (lpathopt) { |
| 13604 | if (*lpathopt == 'b') { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13605 | if (bcmd) |
| 13606 | goto builtin_success; |
| 13607 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13608 | } else if (!(act & DO_NOFUNC)) { |
| 13609 | /* handled below */ |
| 13610 | } else { |
| 13611 | /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13612 | continue; |
| 13613 | } |
| 13614 | } |
| 13615 | /* if rehash, don't redo absolute path names */ |
| 13616 | if (fullname[0] == '/' && idx <= prev) { |
| 13617 | if (idx < prev) |
| 13618 | continue; |
| 13619 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 13620 | goto success; |
| 13621 | } |
| 13622 | while (stat(fullname, &statb) < 0) { |
| 13623 | #ifdef SYSV |
| 13624 | if (errno == EINTR) |
| 13625 | continue; |
| 13626 | #endif |
| 13627 | if (errno != ENOENT && errno != ENOTDIR) |
| 13628 | e = errno; |
| 13629 | goto loop; |
| 13630 | } |
| 13631 | e = EACCES; /* if we fail, this will be the error */ |
| 13632 | if (!S_ISREG(statb.st_mode)) |
| 13633 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13634 | if (lpathopt) { /* this is a %func directory */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13635 | stalloc(len); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 13636 | /* NB: stalloc will return space pointed by fullname |
| 13637 | * (because we don't have any intervening allocations |
| 13638 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13639 | readcmdfile(fullname); |
| 13640 | cmdp = cmdlookup(name, 0); |
| 13641 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 13642 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 13643 | stunalloc(fullname); |
| 13644 | goto success; |
| 13645 | } |
| 13646 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 13647 | if (!updatetbl) { |
| 13648 | entry->cmdtype = CMDNORMAL; |
| 13649 | entry->u.index = idx; |
| 13650 | return; |
| 13651 | } |
| 13652 | INT_OFF; |
| 13653 | cmdp = cmdlookup(name, 1); |
| 13654 | cmdp->cmdtype = CMDNORMAL; |
| 13655 | cmdp->param.index = idx; |
| 13656 | INT_ON; |
| 13657 | goto success; |
| 13658 | } |
| 13659 | |
| 13660 | /* We failed. If there was an entry for this command, delete it */ |
| 13661 | if (cmdp && updatetbl) |
| 13662 | delete_cmd_entry(); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13663 | if (act & DO_ERR) { |
| 13664 | #if ENABLE_ASH_BASH_NOT_FOUND_HOOK |
| 13665 | struct tblentry *hookp = cmdlookup("command_not_found_handle", 0); |
| 13666 | if (hookp && hookp->cmdtype == CMDFUNCTION) { |
| 13667 | char *argv[3]; |
| 13668 | argv[0] = (char*) "command_not_found_handle"; |
| 13669 | argv[1] = name; |
| 13670 | argv[2] = NULL; |
| 13671 | evalfun(hookp->param.func, 2, argv, 0); |
| 13672 | entry->cmdtype = CMDUNKNOWN; |
| 13673 | return; |
| 13674 | } |
| 13675 | #endif |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13676 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13677 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13678 | fail: |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13679 | entry->cmdtype = CMDUNKNOWN; |
| 13680 | return; |
| 13681 | |
| 13682 | builtin_success: |
| 13683 | if (!updatetbl) { |
| 13684 | entry->cmdtype = CMDBUILTIN; |
| 13685 | entry->u.cmd = bcmd; |
| 13686 | return; |
| 13687 | } |
| 13688 | INT_OFF; |
| 13689 | cmdp = cmdlookup(name, 1); |
| 13690 | cmdp->cmdtype = CMDBUILTIN; |
| 13691 | cmdp->param.cmd = bcmd; |
| 13692 | INT_ON; |
| 13693 | success: |
| 13694 | cmdp->rehash = 0; |
| 13695 | entry->cmdtype = cmdp->cmdtype; |
| 13696 | entry->u = cmdp->param; |
| 13697 | } |
| 13698 | |
| 13699 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13700 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13701 | * The trap builtin. |
| 13702 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13703 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13704 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13705 | { |
| 13706 | char *action; |
| 13707 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13708 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13709 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13710 | nextopt(nullstr); |
| 13711 | ap = argptr; |
| 13712 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13713 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13714 | char *tr = trap_ptr[signo]; |
| 13715 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13716 | /* note: bash adds "SIG", but only if invoked |
| 13717 | * as "bash". If called as "sh", or if set -o posix, |
| 13718 | * then it prints short signal names. |
| 13719 | * We are printing short names: */ |
| 13720 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13721 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 13722 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13723 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 13724 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13725 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13726 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13727 | } |
| 13728 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13729 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13730 | if (trap_ptr != trap) { |
| 13731 | free(trap_ptr); |
| 13732 | trap_ptr = trap; |
| 13733 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13734 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13735 | return 0; |
| 13736 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13737 | |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13738 | /* Why the second check? |
| 13739 | * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..." |
| 13740 | * In this case, NUM is signal no, not an action. |
| 13741 | */ |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 13742 | action = NULL; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13743 | if (ap[1] && !is_number(ap[0])) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13744 | action = *ap++; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13745 | |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13746 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13747 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13748 | signo = get_signum(*ap); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13749 | if (signo < 0) { |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13750 | /* Mimic bash message exactly */ |
| 13751 | ash_msg("%s: invalid signal specification", *ap); |
| 13752 | exitcode = 1; |
| 13753 | goto next; |
| 13754 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13755 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13756 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 13757 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13758 | action = NULL; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 13759 | else { |
| 13760 | if (action[0]) /* not NULL and not "" and not "-" */ |
| 13761 | may_have_traps = 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13762 | action = ckstrdup(action); |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 13763 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13764 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 13765 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13766 | trap[signo] = action; |
| 13767 | if (signo != 0) |
| 13768 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13769 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13770 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13771 | ap++; |
| 13772 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13773 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13774 | } |
| 13775 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13776 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13777 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13778 | |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13779 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13780 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13781 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13782 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 13783 | unsigned col; |
| 13784 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13785 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 13786 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 13787 | "Built-in commands:\n" |
| 13788 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 13789 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13790 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 13791 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13792 | if (col > 60) { |
| 13793 | out1fmt("\n"); |
| 13794 | col = 0; |
| 13795 | } |
| 13796 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13797 | # if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 13798 | { |
| 13799 | const char *a = applet_names; |
| 13800 | while (*a) { |
| 13801 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 13802 | if (col > 60) { |
| 13803 | out1fmt("\n"); |
| 13804 | col = 0; |
| 13805 | } |
Ron Yorston | 2b91958 | 2016-04-08 11:57:20 +0100 | [diff] [blame] | 13806 | while (*a++ != '\0') |
| 13807 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13808 | } |
| 13809 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13810 | # endif |
Denys Vlasenko | ebedb94 | 2016-10-02 18:45:09 +0200 | [diff] [blame] | 13811 | newline_and_flush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13812 | return EXIT_SUCCESS; |
| 13813 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13814 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13815 | |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 13816 | #if MAX_HISTORY |
| 13817 | static int FAST_FUNC |
| 13818 | historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 13819 | { |
Ron Yorston | 9f3b410 | 2019-12-16 09:31:10 +0000 | [diff] [blame] | 13820 | show_history(line_input_state); |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 13821 | return EXIT_SUCCESS; |
| 13822 | } |
| 13823 | #endif |
| 13824 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13825 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13826 | * The export and readonly commands. |
| 13827 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13828 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13829 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13830 | { |
| 13831 | struct var *vp; |
| 13832 | char *name; |
| 13833 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13834 | char **aptr; |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13835 | char opt; |
| 13836 | int flag; |
| 13837 | int flag_off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13838 | |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13839 | /* "readonly" in bash accepts, but ignores -n. |
| 13840 | * We do the same: it saves a conditional in nextopt's param. |
| 13841 | */ |
| 13842 | flag_off = 0; |
| 13843 | while ((opt = nextopt("np")) != '\0') { |
| 13844 | if (opt == 'n') |
| 13845 | flag_off = VEXPORT; |
| 13846 | } |
| 13847 | flag = VEXPORT; |
| 13848 | if (argv[0][0] == 'r') { |
| 13849 | flag = VREADONLY; |
| 13850 | flag_off = 0; /* readonly ignores -n */ |
| 13851 | } |
| 13852 | flag_off = ~flag_off; |
| 13853 | |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 13854 | /*if (opt_p_not_specified) - bash doesn't check this. Try "export -p NAME" */ |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13855 | { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13856 | aptr = argptr; |
| 13857 | name = *aptr; |
| 13858 | if (name) { |
| 13859 | do { |
| 13860 | p = strchr(name, '='); |
| 13861 | if (p != NULL) { |
| 13862 | p++; |
| 13863 | } else { |
| 13864 | vp = *findvar(hashvar(name), name); |
| 13865 | if (vp) { |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13866 | vp->flags = ((vp->flags | flag) & flag_off); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13867 | continue; |
| 13868 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13869 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13870 | setvar(name, p, (flag & flag_off)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13871 | } while ((name = *++aptr) != NULL); |
| 13872 | return 0; |
| 13873 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13874 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13875 | |
| 13876 | /* No arguments. Show the list of exported or readonly vars. |
| 13877 | * -n is ignored. |
| 13878 | */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13879 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13880 | return 0; |
| 13881 | } |
| 13882 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13883 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13884 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13885 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13886 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13887 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 13888 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13889 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13890 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13891 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13892 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13893 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13894 | } |
| 13895 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13896 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13897 | * The unset builtin command. We unset the function before we unset the |
| 13898 | * variable to allow a function to be unset when there is a readonly variable |
| 13899 | * with the same name. |
| 13900 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13901 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13902 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13903 | { |
| 13904 | char **ap; |
| 13905 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13906 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13907 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13908 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13909 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13910 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13911 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13912 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13913 | if (flag != 'f') { |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 13914 | unsetvar(*ap); |
| 13915 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13916 | } |
| 13917 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13918 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13919 | } |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 13920 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13921 | } |
| 13922 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13923 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13924 | ' ', offsetof(struct tms, tms_utime), |
| 13925 | '\n', offsetof(struct tms, tms_stime), |
| 13926 | ' ', offsetof(struct tms, tms_cutime), |
| 13927 | '\n', offsetof(struct tms, tms_cstime), |
| 13928 | 0 |
| 13929 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13930 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13931 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13932 | { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13933 | unsigned clk_tck; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13934 | const unsigned char *p; |
| 13935 | struct tms buf; |
| 13936 | |
Bartosz Golaszewski | 5d2e409 | 2014-06-22 14:01:13 +0200 | [diff] [blame] | 13937 | clk_tck = bb_clk_tck(); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13938 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13939 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13940 | p = timescmd_str; |
| 13941 | do { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13942 | unsigned sec, frac; |
| 13943 | unsigned long t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13944 | t = *(clock_t *)(((char *) &buf) + p[1]); |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13945 | sec = t / clk_tck; |
| 13946 | frac = t % clk_tck; |
| 13947 | out1fmt("%um%u.%03us%c", |
| 13948 | sec / 60, sec % 60, |
| 13949 | (frac * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13950 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13951 | p += 2; |
| 13952 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13953 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13954 | return 0; |
| 13955 | } |
| 13956 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 13957 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13958 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13959 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13960 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13961 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13962 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13963 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13964 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13965 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13966 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13967 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13968 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13969 | argv++; |
| 13970 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13971 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13972 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13973 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13974 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13975 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 13976 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13977 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13978 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 13979 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13980 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13981 | * The read builtin. Options: |
| 13982 | * -r Do not interpret '\' specially |
| 13983 | * -s Turn off echo (tty only) |
| 13984 | * -n NCHARS Read NCHARS max |
| 13985 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 13986 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 13987 | * -u FD Read from given FD instead of fd 0 |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 13988 | * -d DELIM End on DELIM char, not newline |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13989 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13990 | * TODO: bash also has: |
| 13991 | * -a ARRAY Read into array[0],[1],etc |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13992 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13993 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13994 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13995 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13996 | { |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 13997 | struct builtin_read_params params; |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 13998 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13999 | int i; |
| 14000 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14001 | memset(¶ms, 0, sizeof(params)); |
| 14002 | |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14003 | while ((i = nextopt("p:u:rt:n:sd:")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 14004 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14005 | case 'p': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14006 | params.opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14007 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14008 | case 'n': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14009 | params.opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14010 | break; |
| 14011 | case 's': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14012 | params.read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14013 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14014 | case 't': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14015 | params.opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14016 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14017 | case 'r': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14018 | params.read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14019 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14020 | case 'u': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14021 | params.opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14022 | break; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14023 | #if BASH_READ_D |
| 14024 | case 'd': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14025 | params.opt_d = optionarg; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14026 | break; |
| 14027 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14028 | default: |
| 14029 | break; |
| 14030 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14031 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14032 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14033 | params.argv = argptr; |
| 14034 | params.setvar = setvar0; |
| 14035 | params.ifs = bltinlookup("IFS"); /* can be NULL */ |
| 14036 | |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14037 | /* "read -s" needs to save/restore termios, can't allow ^C |
| 14038 | * to jump out of it. |
| 14039 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14040 | again: |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14041 | INT_OFF; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14042 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14043 | INT_ON; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 14044 | |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14045 | if ((uintptr_t)r == 1 && errno == EINTR) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14046 | /* To get SIGCHLD: sleep 1 & read x; echo $x |
| 14047 | * Correct behavior is to not exit "read" |
| 14048 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14049 | if (pending_sig == 0) |
| 14050 | goto again; |
| 14051 | } |
| 14052 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14053 | if ((uintptr_t)r > 1) |
| 14054 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 14055 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14056 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14057 | } |
| 14058 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14059 | static int FAST_FUNC |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14060 | umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14061 | { |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14062 | static const char permuser[3] ALIGN1 = "ogu"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14063 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14064 | mode_t mask; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14065 | int symbolic_mode = 0; |
| 14066 | |
| 14067 | while (nextopt("S") != '\0') { |
| 14068 | symbolic_mode = 1; |
| 14069 | } |
| 14070 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14071 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14072 | mask = umask(0); |
| 14073 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14074 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14075 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14076 | if (*argptr == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14077 | if (symbolic_mode) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14078 | char buf[sizeof(",u=rwx,g=rwx,o=rwx")]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14079 | char *p = buf; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14080 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14081 | |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14082 | i = 2; |
| 14083 | for (;;) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14084 | *p++ = ','; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14085 | *p++ = permuser[i]; |
| 14086 | *p++ = '='; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14087 | /* mask is 0..0uuugggooo. i=2 selects uuu bits */ |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14088 | if (!(mask & 0400)) *p++ = 'r'; |
| 14089 | if (!(mask & 0200)) *p++ = 'w'; |
| 14090 | if (!(mask & 0100)) *p++ = 'x'; |
| 14091 | mask <<= 3; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14092 | if (--i < 0) |
| 14093 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14094 | } |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14095 | *p = '\0'; |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14096 | puts(buf + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14097 | } else { |
Denys Vlasenko | ec046f7 | 2015-10-07 17:57:53 +0200 | [diff] [blame] | 14098 | out1fmt("%04o\n", mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14099 | } |
| 14100 | } else { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14101 | char *modestr = *argptr; |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 14102 | /* numeric umasks are taken as-is */ |
| 14103 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14104 | if (!isdigit(modestr[0])) |
| 14105 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 14106 | mask = bb_parse_mode(modestr, mask); |
| 14107 | if ((unsigned)mask > 0777) { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14108 | ash_msg_and_raise_error("illegal mode: %s", modestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14109 | } |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14110 | if (!isdigit(modestr[0])) |
| 14111 | mask ^= 0777; |
| 14112 | umask(mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14113 | } |
| 14114 | return 0; |
| 14115 | } |
| 14116 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14117 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14118 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14119 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14120 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14121 | } |
| 14122 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14123 | /* ============ main() and helpers */ |
| 14124 | |
| 14125 | /* |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14126 | * This routine is called when an error or an interrupt occurs in an |
| 14127 | * interactive shell and control is returned to the main command loop |
| 14128 | * but prior to exitshell. |
| 14129 | */ |
| 14130 | static void |
| 14131 | exitreset(void) |
| 14132 | { |
| 14133 | /* from eval.c: */ |
| 14134 | if (savestatus >= 0) { |
| 14135 | if (exception_type == EXEXIT || evalskip == SKIPFUNCDEF) |
| 14136 | exitstatus = savestatus; |
| 14137 | savestatus = -1; |
| 14138 | } |
| 14139 | evalskip = 0; |
| 14140 | loopnest = 0; |
| 14141 | |
| 14142 | /* from expand.c: */ |
| 14143 | ifsfree(); |
| 14144 | |
| 14145 | /* from redir.c: */ |
| 14146 | unwindredir(NULL); |
| 14147 | } |
| 14148 | |
| 14149 | /* |
| 14150 | * This routine is called when an error or an interrupt occurs in an |
| 14151 | * interactive shell and control is returned to the main command loop. |
| 14152 | * (In dash, this function is auto-generated by build machinery). |
| 14153 | */ |
| 14154 | static void |
| 14155 | reset(void) |
| 14156 | { |
| 14157 | /* from input.c: */ |
| 14158 | g_parsefile->left_in_buffer = 0; |
| 14159 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
| 14160 | popallfiles(); |
| 14161 | |
| 14162 | /* from var.c: */ |
| 14163 | unwindlocalvars(NULL); |
| 14164 | } |
| 14165 | |
| 14166 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14167 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14168 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14169 | static void |
| 14170 | exitshell(void) |
| 14171 | { |
| 14172 | struct jmploc loc; |
| 14173 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14174 | |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14175 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 14176 | if (line_input_state) |
| 14177 | save_history(line_input_state); |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14178 | #endif |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 14179 | savestatus = exitstatus; |
| 14180 | TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus)); |
| 14181 | if (setjmp(loc.loc)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14182 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14183 | exception_handler = &loc; |
| 14184 | p = trap[0]; |
| 14185 | if (p) { |
| 14186 | trap[0] = NULL; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 14187 | evalskip = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14188 | evalstring(p, 0); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14189 | evalskip = SKIPFUNCDEF; |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14190 | /*free(p); - we'll exit soon */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14191 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14192 | out: |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14193 | exitreset(); |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14194 | /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}". |
| 14195 | * our setjobctl(0) does not panic if tcsetpgrp fails inside it. |
| 14196 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14197 | setjobctl(0); |
Denys Vlasenko | caee80c | 2016-10-25 20:49:53 +0200 | [diff] [blame] | 14198 | flush_stdout_stderr(); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14199 | _exit(exitstatus); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14200 | /* NOTREACHED */ |
| 14201 | } |
| 14202 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14203 | /* Don't inline: conserve stack of caller from having our locals too */ |
| 14204 | static NOINLINE void |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 14205 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14206 | { |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 14207 | /* we will never free this */ |
| 14208 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denys Vlasenko | 0485b67 | 2017-08-14 19:46:56 +0200 | [diff] [blame] | 14209 | basepf.linno = 1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14210 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14211 | sigmode[SIGCHLD - 1] = S_DFL; /* ensure we install handler even if it is SIG_IGNed */ |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 14212 | setsignal(SIGCHLD); |
| 14213 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14214 | { |
| 14215 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14216 | const char *p; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14217 | |
| 14218 | initvar(); |
| 14219 | for (envp = environ; envp && *envp; envp++) { |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 14220 | /* Used to have |
| 14221 | * p = endofname(*envp); |
| 14222 | * if (p != *envp && *p == '=') { |
| 14223 | * here to weed out badly-named variables, but this breaks |
| 14224 | * scenarios where people do want them passed to children: |
| 14225 | * import os |
| 14226 | * os.environ["test-test"]="test" |
| 14227 | * if os.fork() == 0: |
| 14228 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 14229 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) # breaks this |
| 14230 | */ |
| 14231 | if (strchr(*envp, '=')) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14232 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 14233 | } |
| 14234 | } |
| 14235 | |
Denys Vlasenko | 67dae15 | 2018-08-05 13:59:35 +0200 | [diff] [blame] | 14236 | setvareq((char*)defifsvar, VTEXTFIXED); |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 14237 | setvareq((char*)defoptindvar, VTEXTFIXED); |
| 14238 | |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14239 | setvar0("PPID", utoa(getppid())); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14240 | #if BASH_SHLVL_VAR |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14241 | p = lookupvar("SHLVL"); |
Denys Vlasenko | 5680e98 | 2014-01-07 16:12:48 +0100 | [diff] [blame] | 14242 | setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14243 | #endif |
| 14244 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14245 | if (!lookupvar("HOSTNAME")) { |
| 14246 | struct utsname uts; |
| 14247 | uname(&uts); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14248 | setvar0("HOSTNAME", uts.nodename); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14249 | } |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14250 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14251 | p = lookupvar("PWD"); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14252 | if (p) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14253 | struct stat st1, st2; |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14254 | if (p[0] != '/' || stat(p, &st1) || stat(".", &st2) |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14255 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino |
| 14256 | ) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14257 | p = NULL; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14258 | } |
| 14259 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14260 | setpwd(p, 0); |
| 14261 | } |
| 14262 | } |
| 14263 | |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14264 | |
| 14265 | //usage:#define ash_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 14266 | //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14267 | //usage:#define ash_full_usage "\n\n" |
| 14268 | //usage: "Unix shell interpreter" |
| 14269 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14270 | /* |
| 14271 | * Process the shell command line arguments. |
| 14272 | */ |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14273 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14274 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14275 | { |
| 14276 | int i; |
| 14277 | const char *xminusc; |
| 14278 | char **xargv; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14279 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14280 | |
| 14281 | xargv = argv; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14282 | login_sh = xargv[0] && xargv[0][0] == '-'; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14283 | #if NUM_SCRIPTS > 0 |
| 14284 | if (minusc) |
| 14285 | goto setarg0; |
| 14286 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14287 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14288 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14289 | xargv++; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14290 | argptr = xargv; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14291 | for (i = 0; i < NOPTS; i++) |
| 14292 | optlist[i] = 2; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 14293 | if (options(&login_sh)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 14294 | /* it already printed err message */ |
| 14295 | raise_exception(EXERROR); |
| 14296 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14297 | xargv = argptr; |
| 14298 | xminusc = minusc; |
| 14299 | if (*xargv == NULL) { |
| 14300 | if (xminusc) |
| 14301 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 14302 | sflag = 1; |
| 14303 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14304 | if (iflag == 2 /* no explicit -i given */ |
| 14305 | && sflag == 1 /* -s given (or implied) */ |
| 14306 | && !minusc /* bash compat: ash -sc 'echo $-' is not interactive (dash is) */ |
| 14307 | && isatty(0) && isatty(1) /* we are on tty */ |
| 14308 | ) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14309 | iflag = 1; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14310 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14311 | if (mflag == 2) |
| 14312 | mflag = iflag; |
| 14313 | for (i = 0; i < NOPTS; i++) |
| 14314 | if (optlist[i] == 2) |
| 14315 | optlist[i] = 0; |
| 14316 | #if DEBUG == 2 |
| 14317 | debug = 1; |
| 14318 | #endif |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14319 | /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14320 | if (xminusc) { |
| 14321 | minusc = *xargv++; |
| 14322 | if (*xargv) |
| 14323 | goto setarg0; |
| 14324 | } else if (!sflag) { |
| 14325 | setinputfile(*xargv, 0); |
| 14326 | setarg0: |
| 14327 | arg0 = *xargv++; |
| 14328 | commandname = arg0; |
| 14329 | } |
| 14330 | |
| 14331 | shellparam.p = xargv; |
| 14332 | #if ENABLE_ASH_GETOPTS |
| 14333 | shellparam.optind = 1; |
| 14334 | shellparam.optoff = -1; |
| 14335 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14336 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14337 | while (*xargv) { |
| 14338 | shellparam.nparam++; |
| 14339 | xargv++; |
| 14340 | } |
| 14341 | optschanged(); |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14342 | |
| 14343 | return login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14344 | } |
| 14345 | |
| 14346 | /* |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14347 | * Read /etc/profile, ~/.profile, $ENV. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14348 | */ |
| 14349 | static void |
| 14350 | read_profile(const char *name) |
| 14351 | { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 14352 | name = expandstr(name, DQSYNTAX); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14353 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 14354 | return; |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 14355 | cmdloop(0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14356 | popfile(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14357 | } |
| 14358 | |
| 14359 | #if PROFILE |
| 14360 | static short profile_buf[16384]; |
| 14361 | extern int etext(); |
| 14362 | #endif |
| 14363 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14364 | /* |
| 14365 | * Main routine. We initialize things, parse the arguments, execute |
| 14366 | * profiles if we're a login shell, and then call cmdloop to execute |
| 14367 | * commands. The setjmp call sets up the location to jump to when an |
| 14368 | * exception occurs. When an exception occurs the variable "state" |
| 14369 | * is used to figure out how far we had gotten. |
| 14370 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14371 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14372 | #if NUM_SCRIPTS > 0 |
| 14373 | int ash_main(int argc, char **argv) |
| 14374 | #else |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14375 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14376 | #endif |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14377 | /* note: 'argc' is used only if embedded scripts are enabled */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14378 | { |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14379 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14380 | struct jmploc jmploc; |
| 14381 | struct stackmark smark; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14382 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14383 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14384 | /* Initialize global data */ |
| 14385 | INIT_G_misc(); |
| 14386 | INIT_G_memstack(); |
| 14387 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14388 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14389 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14390 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14391 | INIT_G_cmdtable(); |
| 14392 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14393 | #if PROFILE |
| 14394 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 14395 | #endif |
| 14396 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14397 | state = 0; |
| 14398 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14399 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14400 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14401 | |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14402 | exitreset(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14403 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14404 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14405 | s = state; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14406 | if (e == EXEND || e == EXEXIT || s == 0 || iflag == 0 || shlvl) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14407 | exitshell(); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14408 | } |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14409 | |
| 14410 | reset(); |
| 14411 | |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14412 | if (e == EXINT) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 14413 | newline_and_flush(stderr); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14414 | } |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14415 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14416 | popstackmark(&smark); |
| 14417 | FORCE_INT_ON; /* enable interrupts */ |
| 14418 | if (s == 1) |
| 14419 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14420 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14421 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14422 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14423 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14424 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14425 | } |
| 14426 | exception_handler = &jmploc; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14427 | rootpid = getpid(); |
| 14428 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14429 | init(); |
| 14430 | setstackmark(&smark); |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14431 | |
| 14432 | #if NUM_SCRIPTS > 0 |
| 14433 | if (argc < 0) |
| 14434 | /* Non-NULL minusc tells procargs that an embedded script is being run */ |
| 14435 | minusc = get_script_content(-argc - 1); |
| 14436 | #endif |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14437 | login_sh = procargs(argv); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 14438 | #if DEBUG |
| 14439 | TRACE(("Shell args: ")); |
| 14440 | trace_puts_args(argv); |
| 14441 | #endif |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14442 | |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14443 | if (login_sh) { |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14444 | const char *hp; |
| 14445 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14446 | state = 1; |
| 14447 | read_profile("/etc/profile"); |
| 14448 | state1: |
| 14449 | state = 2; |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14450 | hp = lookupvar("HOME"); |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14451 | if (hp) |
| 14452 | read_profile("$HOME/.profile"); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14453 | } |
| 14454 | state2: |
| 14455 | state = 3; |
| 14456 | if ( |
| 14457 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 14458 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14459 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 14460 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14461 | ) { |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14462 | const char *shinit = lookupvar("ENV"); |
| 14463 | if (shinit != NULL && *shinit != '\0') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14464 | read_profile(shinit); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14465 | } |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14466 | popstackmark(&smark); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14467 | state3: |
| 14468 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14469 | if (minusc) { |
| 14470 | /* evalstring pushes parsefile stack. |
| 14471 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 14472 | * is one of stacked source fds. |
| 14473 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14474 | |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 14475 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 14476 | // ^^ not necessary since now we special-case fd 0 |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 14477 | // in save_fd_on_redirect() |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14478 | |
| 14479 | // dash: evalstring(minusc, sflag ? 0 : EV_EXIT); |
| 14480 | // The above makes |
| 14481 | // ash -sc 'echo $-' |
| 14482 | // continue reading input from stdin after running 'echo'. |
| 14483 | // bash does not do this: it prints "hBcs" and exits. |
| 14484 | evalstring(minusc, EV_EXIT); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14485 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14486 | |
| 14487 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 14488 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 14489 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14490 | const char *hp = lookupvar("HISTFILE"); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14491 | if (!hp) { |
| 14492 | hp = lookupvar("HOME"); |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14493 | if (hp) { |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14494 | INT_OFF; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14495 | hp = concat_path_file(hp, ".ash_history"); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14496 | setvar0("HISTFILE", hp); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14497 | free((char*)hp); |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14498 | INT_ON; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14499 | hp = lookupvar("HISTFILE"); |
| 14500 | } |
| 14501 | } |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14502 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14503 | line_input_state->hist_file = hp; |
Denys Vlasenko | 2c4de5b | 2011-03-31 13:16:52 +0200 | [diff] [blame] | 14504 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 14505 | hp = lookupvar("HISTFILESIZE"); |
| 14506 | line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 14507 | # endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14508 | } |
| 14509 | #endif |
| 14510 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 14511 | |
| 14512 | /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 14513 | * Try: |
| 14514 | * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect |
| 14515 | * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed |
| 14516 | */ |
| 14517 | signal(SIGHUP, SIG_DFL); |
| 14518 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14519 | cmdloop(1); |
| 14520 | } |
| 14521 | #if PROFILE |
| 14522 | monitor(0); |
| 14523 | #endif |
| 14524 | #ifdef GPROF |
| 14525 | { |
| 14526 | extern void _mcleanup(void); |
| 14527 | _mcleanup(); |
| 14528 | } |
| 14529 | #endif |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14530 | TRACE(("End of main reached\n")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14531 | exitshell(); |
| 14532 | /* NOTREACHED */ |
| 14533 | } |
| 14534 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14535 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14536 | /*- |
| 14537 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 14538 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14539 | * |
| 14540 | * This code is derived from software contributed to Berkeley by |
| 14541 | * Kenneth Almquist. |
| 14542 | * |
| 14543 | * Redistribution and use in source and binary forms, with or without |
| 14544 | * modification, are permitted provided that the following conditions |
| 14545 | * are met: |
| 14546 | * 1. Redistributions of source code must retain the above copyright |
| 14547 | * notice, this list of conditions and the following disclaimer. |
| 14548 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14549 | * notice, this list of conditions and the following disclaimer in the |
| 14550 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 14551 | * 3. Neither the name of the University nor the names of its contributors |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14552 | * may be used to endorse or promote products derived from this software |
| 14553 | * without specific prior written permission. |
| 14554 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 14555 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14556 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 14557 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 14558 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 14559 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 14560 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 14561 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 14562 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 14563 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 14564 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 14565 | * SUCH DAMAGE. |
| 14566 | */ |