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: |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 210 | * && and || work as they should |
| 211 | * = is glob match operator, not equality operator: STR = GLOB |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 212 | * == same as = |
Denys Vlasenko | a7c0653 | 2020-10-31 04:32:34 +0100 | [diff] [blame] | 213 | * =~ is regex match operator: STR =~ REGEX |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 214 | * TODO: |
| 215 | * singleword+noglob expansion: |
| 216 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 217 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 218 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
Denys Vlasenko | a7c0653 | 2020-10-31 04:32:34 +0100 | [diff] [blame] | 219 | * ( ) < > should not have special meaning (IOW: should not require quoting) |
| 220 | * in word = GLOB, quoting should be significant on char-by-char basis: a*cd"*" |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 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 | |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 317 | /* If you add/change options hare, update --help text too */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 318 | static const char *const optletters_optnames[] = { |
| 319 | "e" "errexit", |
| 320 | "f" "noglob", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 321 | /* bash has '-o ignoreeof', but no short synonym -I for it */ |
| 322 | /* (in bash, set -I disables invisible variables (what's that?)) */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 323 | "I" "ignoreeof", |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 324 | /* The below allowed this invocation: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 325 | * ash -c 'set -i; echo $-; sleep 5; echo $-' |
| 326 | * to be ^C-ed and get to interactive ash prompt. |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 327 | * bash does not support such "set -i". |
| 328 | * In our code, this is denoted by empty long name: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 329 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 330 | "i" "", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 331 | /* (removing "i" altogether would remove it from "$-", not good) */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 332 | "m" "monitor", |
| 333 | "n" "noexec", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 334 | /* Ditto: bash has no "set -s", "set -c" */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 335 | "s" "", |
| 336 | "c" "", |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 337 | "x" "xtrace", |
| 338 | "v" "verbose", |
| 339 | "C" "noclobber", |
| 340 | "a" "allexport", |
| 341 | "b" "notify", |
| 342 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 343 | "\0" "vi" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 344 | #if BASH_PIPEFAIL |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 345 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 346 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 347 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 348 | ,"\0" "nolog" |
| 349 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 350 | #endif |
| 351 | }; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 352 | //bash 4.4.23 also has these opts (with these defaults): |
| 353 | //braceexpand on |
| 354 | //emacs on |
| 355 | //errtrace off |
| 356 | //functrace off |
| 357 | //hashall on |
| 358 | //histexpand off |
| 359 | //history on |
| 360 | //interactive-comments on |
| 361 | //keyword off |
| 362 | //onecmd off |
| 363 | //physical off |
| 364 | //posix off |
| 365 | //privileged off |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 366 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 367 | #define optletters(n) optletters_optnames[n][0] |
| 368 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 369 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 370 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 371 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 372 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 373 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 374 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 375 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 376 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 377 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 378 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 379 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 380 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 381 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 382 | * exception handlers, the user should save the value of handler on entry |
| 383 | * to an inner scope, set handler to point to a jmploc structure for the |
| 384 | * inner scope, and restore handler on exit from the scope. |
| 385 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 386 | struct jmploc { |
| 387 | jmp_buf loc; |
| 388 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 389 | |
| 390 | struct globals_misc { |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 391 | uint8_t exitstatus; /* exit status of last command */ |
| 392 | uint8_t back_exitstatus;/* exit status of backquoted command */ |
| 393 | 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] | 394 | int savestatus; /* exit status of last command outside traps */ |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 395 | int rootpid; /* pid of main shell */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 396 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 397 | int shlvl; |
| 398 | #define rootshell (!shlvl) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 399 | int errlinno; |
| 400 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 401 | char *minusc; /* argument to -c option */ |
| 402 | |
| 403 | char *curdir; // = nullstr; /* current working directory */ |
| 404 | char *physdir; // = nullstr; /* physical working directory */ |
| 405 | |
| 406 | char *arg0; /* value of $0 */ |
| 407 | |
| 408 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 409 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 410 | volatile int suppress_int; /* counter */ |
| 411 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 412 | volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */ |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 413 | volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 414 | smallint exception_type; /* kind of exception: */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 415 | #define EXINT 0 /* SIGINT received */ |
| 416 | #define EXERROR 1 /* a generic error */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 417 | #define EXEND 3 /* exit the shell */ |
| 418 | #define EXEXIT 4 /* exit the shell via exitcmd */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 419 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 420 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 421 | |
| 422 | char optlist[NOPTS]; |
| 423 | #define eflag optlist[0] |
| 424 | #define fflag optlist[1] |
| 425 | #define Iflag optlist[2] |
| 426 | #define iflag optlist[3] |
| 427 | #define mflag optlist[4] |
| 428 | #define nflag optlist[5] |
| 429 | #define sflag optlist[6] |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 430 | #define cflag optlist[7] |
| 431 | #define xflag optlist[8] |
| 432 | #define vflag optlist[9] |
| 433 | #define Cflag optlist[10] |
| 434 | #define aflag optlist[11] |
| 435 | #define bflag optlist[12] |
| 436 | #define uflag optlist[13] |
| 437 | #define viflag optlist[14] |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 438 | #if BASH_PIPEFAIL |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 439 | # define pipefail optlist[15] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 440 | #else |
| 441 | # define pipefail 0 |
| 442 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 443 | #if DEBUG |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 444 | # define nolog optlist[15 + BASH_PIPEFAIL] |
| 445 | # define debug optlist[16 + BASH_PIPEFAIL] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 446 | #endif |
| 447 | |
| 448 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 449 | /* |
| 450 | * Sigmode records the current value of the signal handlers for the various |
| 451 | * 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] | 452 | * 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] | 453 | */ |
| 454 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 455 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 456 | #define S_CATCH 2 /* signal is caught */ |
| 457 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 458 | #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] | 459 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 460 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 461 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 462 | 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] | 463 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 464 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 465 | |
| 466 | /* Rarely referenced stuff */ |
| 467 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 468 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 469 | #endif |
| 470 | pid_t backgndpid; /* pid of last background process */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 471 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 472 | extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 473 | #define G_misc (*ash_ptr_to_globals_misc) |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 474 | #define exitstatus (G_misc.exitstatus ) |
| 475 | #define back_exitstatus (G_misc.back_exitstatus ) |
| 476 | #define job_warning (G_misc.job_warning) |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 477 | #define savestatus (G_misc.savestatus ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 478 | #define rootpid (G_misc.rootpid ) |
| 479 | #define shlvl (G_misc.shlvl ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 480 | #define errlinno (G_misc.errlinno ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 481 | #define minusc (G_misc.minusc ) |
| 482 | #define curdir (G_misc.curdir ) |
| 483 | #define physdir (G_misc.physdir ) |
| 484 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 485 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 486 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 487 | #define suppress_int (G_misc.suppress_int ) |
| 488 | #define pending_int (G_misc.pending_int ) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 489 | #define got_sigchld (G_misc.got_sigchld ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 490 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 491 | #define nullstr (G_misc.nullstr ) |
| 492 | #define optlist (G_misc.optlist ) |
| 493 | #define sigmode (G_misc.sigmode ) |
| 494 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 495 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 496 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 497 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 498 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 499 | #define backgndpid (G_misc.backgndpid ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 500 | #define INIT_G_misc() do { \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 501 | (*(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] | 502 | barrier(); \ |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 503 | savestatus = -1; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 504 | curdir = nullstr; \ |
| 505 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 506 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 507 | } while (0) |
| 508 | |
| 509 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 510 | /* ============ DEBUG */ |
| 511 | #if DEBUG |
| 512 | static void trace_printf(const char *fmt, ...); |
| 513 | static void trace_vprintf(const char *fmt, va_list va); |
| 514 | # define TRACE(param) trace_printf param |
| 515 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 516 | # define close(fd) do { \ |
| 517 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 518 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 519 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 520 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 521 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 522 | #else |
| 523 | # define TRACE(param) |
| 524 | # define TRACEV(param) |
| 525 | #endif |
| 526 | |
| 527 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 528 | /* ============ Utility functions */ |
Denys Vlasenko | 1961aea | 2013-02-26 00:36:53 +0100 | [diff] [blame] | 529 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 530 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 531 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 532 | static int |
| 533 | isdigit_str9(const char *str) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 534 | { |
| 535 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 536 | while (--maxlen && isdigit(*str)) |
| 537 | str++; |
| 538 | return (*str == '\0'); |
| 539 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 540 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 541 | static const char * |
| 542 | var_end(const char *var) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 543 | { |
| 544 | while (*var) |
| 545 | if (*var++ == '=') |
| 546 | break; |
| 547 | return var; |
| 548 | } |
| 549 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 550 | |
Denys Vlasenko | 2124c0e | 2020-12-19 14:33:02 +0100 | [diff] [blame] | 551 | /* ============ Parser data */ |
| 552 | |
| 553 | /* |
| 554 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 555 | */ |
| 556 | struct strlist { |
| 557 | struct strlist *next; |
| 558 | char *text; |
| 559 | }; |
| 560 | |
| 561 | struct alias; |
| 562 | |
| 563 | struct strpush { |
| 564 | struct strpush *prev; /* preceding string on stack */ |
| 565 | char *prev_string; |
| 566 | int prev_left_in_line; |
| 567 | #if ENABLE_ASH_ALIAS |
| 568 | struct alias *ap; /* if push was associated with an alias */ |
| 569 | #endif |
| 570 | char *string; /* remember the string since it may change */ |
| 571 | |
| 572 | /* Remember last two characters for pungetc. */ |
| 573 | int lastc[2]; |
| 574 | |
| 575 | /* Number of outstanding calls to pungetc. */ |
| 576 | int unget; |
| 577 | }; |
| 578 | |
| 579 | /* |
| 580 | * The parsefile structure pointed to by the global variable parsefile |
| 581 | * contains information about the current file being read. |
| 582 | */ |
| 583 | struct parsefile { |
| 584 | struct parsefile *prev; /* preceding file on stack */ |
| 585 | int linno; /* current line */ |
| 586 | int pf_fd; /* file descriptor (or -1 if string) */ |
| 587 | int left_in_line; /* number of chars left in this line */ |
| 588 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 589 | char *next_to_pgetc; /* next char in buffer */ |
| 590 | char *buf; /* input buffer */ |
| 591 | struct strpush *strpush; /* for pushing strings at this level */ |
| 592 | struct strpush basestrpush; /* so pushing one is fast */ |
| 593 | |
| 594 | /* Remember last two characters for pungetc. */ |
| 595 | int lastc[2]; |
| 596 | |
| 597 | /* Number of outstanding calls to pungetc. */ |
| 598 | int unget; |
| 599 | }; |
| 600 | |
| 601 | static struct parsefile basepf; /* top level input file */ |
| 602 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
| 603 | static char *commandname; /* currently executing command */ |
| 604 | |
| 605 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 606 | /* ============ Interrupts / exceptions */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 607 | |
| 608 | static void exitshell(void) NORETURN; |
| 609 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 610 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 611 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 612 | * 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] | 613 | * much more efficient and portable. (But hacking the kernel is so much |
| 614 | * more fun than worrying about efficiency and portability. :-)) |
| 615 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 616 | #if DEBUG_INTONOFF |
| 617 | # define INT_OFF do { \ |
| 618 | TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 619 | suppress_int++; \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 620 | barrier(); \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 621 | } while (0) |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 622 | #else |
| 623 | # define INT_OFF do { \ |
| 624 | suppress_int++; \ |
| 625 | barrier(); \ |
| 626 | } while (0) |
| 627 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 631 | * 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] | 632 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 633 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 634 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 635 | static void |
| 636 | raise_exception(int e) |
| 637 | { |
| 638 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 639 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 640 | abort(); |
| 641 | #endif |
| 642 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 643 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 644 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 645 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 646 | #if DEBUG |
| 647 | #define raise_exception(e) do { \ |
| 648 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 649 | raise_exception(e); \ |
| 650 | } while (0) |
| 651 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 652 | |
| 653 | /* |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 654 | * Called when a SIGINT is received. (If the user specifies |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 655 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 656 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 657 | * are held using the INT_OFF macro. (The test for iflag is just |
| 658 | * defensive programming.) |
| 659 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 660 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 661 | static void |
| 662 | raise_interrupt(void) |
| 663 | { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 664 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 665 | /* Signal is not automatically unmasked after it is raised, |
| 666 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 667 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 668 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 669 | |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 670 | if (!(rootshell && iflag)) { |
| 671 | /* Kill ourself with SIGINT */ |
| 672 | signal(SIGINT, SIG_DFL); |
| 673 | raise(SIGINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 674 | } |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 675 | /* bash: ^C even on empty command line sets $? */ |
| 676 | exitstatus = SIGINT + 128; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 677 | raise_exception(EXINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 678 | /* NOTREACHED */ |
| 679 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 680 | #if DEBUG |
| 681 | #define raise_interrupt() do { \ |
| 682 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 683 | raise_interrupt(); \ |
| 684 | } while (0) |
| 685 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 686 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 687 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 688 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 689 | { |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 690 | barrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 691 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 692 | raise_interrupt(); |
| 693 | } |
| 694 | } |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 695 | #if DEBUG_INTONOFF |
| 696 | # define INT_ON do { \ |
| 697 | TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \ |
| 698 | int_on(); \ |
| 699 | } while (0) |
| 700 | #else |
| 701 | # define INT_ON int_on() |
| 702 | #endif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 703 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 704 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 705 | { |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 706 | barrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 707 | suppress_int = 0; |
| 708 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 709 | raise_interrupt(); |
| 710 | } |
| 711 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 712 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 713 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 714 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 715 | #define RESTORE_INT(v) do { \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 716 | barrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 717 | suppress_int = (v); \ |
| 718 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 719 | raise_interrupt(); \ |
| 720 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 721 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 722 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 723 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 724 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 725 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 726 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 727 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 728 | INT_OFF; |
| 729 | fputs(p, file); |
| 730 | INT_ON; |
| 731 | } |
| 732 | |
| 733 | static void |
| 734 | flush_stdout_stderr(void) |
| 735 | { |
| 736 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 737 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 738 | INT_ON; |
| 739 | } |
| 740 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 741 | /* Was called outcslow(c,FILE*), but c was always '\n' */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 742 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 743 | newline_and_flush(FILE *dest) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 744 | { |
| 745 | INT_OFF; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 746 | putc('\n', dest); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 747 | fflush(dest); |
| 748 | INT_ON; |
| 749 | } |
| 750 | |
| 751 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 752 | static int |
| 753 | out1fmt(const char *fmt, ...) |
| 754 | { |
| 755 | va_list ap; |
| 756 | int r; |
| 757 | |
| 758 | INT_OFF; |
| 759 | va_start(ap, fmt); |
| 760 | r = vprintf(fmt, ap); |
| 761 | va_end(ap); |
| 762 | INT_ON; |
| 763 | return r; |
| 764 | } |
| 765 | |
| 766 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 767 | static int |
| 768 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 769 | { |
| 770 | va_list ap; |
| 771 | int ret; |
| 772 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 773 | INT_OFF; |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 774 | va_start(ap, fmt); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 775 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 776 | va_end(ap); |
| 777 | INT_ON; |
Denys Vlasenko | 3f7fb2c | 2020-02-16 18:06:20 +0100 | [diff] [blame] | 778 | return ret > (int)length ? length : ret; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static void |
| 782 | out1str(const char *p) |
| 783 | { |
| 784 | outstr(p, stdout); |
| 785 | } |
| 786 | |
| 787 | static void |
| 788 | out2str(const char *p) |
| 789 | { |
| 790 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 791 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 795 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 796 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 797 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 798 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 799 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 800 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 801 | #define CTLENDVAR ((unsigned char)'\203') |
| 802 | #define CTLBACKQ ((unsigned char)'\204') |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 803 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 804 | #define CTLENDARI ((unsigned char)'\207') |
| 805 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 806 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 807 | |
| 808 | /* variable substitution byte (follows CTLVAR) */ |
| 809 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 810 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 811 | |
| 812 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 813 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 814 | #define VSMINUS 0x2 /* ${var-text} */ |
| 815 | #define VSPLUS 0x3 /* ${var+text} */ |
| 816 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 817 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 818 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 819 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 820 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 821 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 822 | #define VSLENGTH 0xa /* ${#var} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 823 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 824 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 825 | #endif |
| 826 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 827 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 828 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 829 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 830 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 831 | static const char dolatstr[] ALIGN1 = { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 832 | CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0' |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 833 | }; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 834 | #define DOLATSTRLEN 6 |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 835 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 836 | #define NCMD 0 |
| 837 | #define NPIPE 1 |
| 838 | #define NREDIR 2 |
| 839 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 840 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 841 | #define NAND 5 |
| 842 | #define NOR 6 |
| 843 | #define NSEMI 7 |
| 844 | #define NIF 8 |
| 845 | #define NWHILE 9 |
| 846 | #define NUNTIL 10 |
| 847 | #define NFOR 11 |
| 848 | #define NCASE 12 |
| 849 | #define NCLIST 13 |
| 850 | #define NDEFUN 14 |
| 851 | #define NARG 15 |
| 852 | #define NTO 16 |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 853 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 854 | #define NTO2 17 |
| 855 | #endif |
| 856 | #define NCLOBBER 18 |
| 857 | #define NFROM 19 |
| 858 | #define NFROMTO 20 |
| 859 | #define NAPPEND 21 |
| 860 | #define NTOFD 22 |
| 861 | #define NFROMFD 23 |
| 862 | #define NHERE 24 |
| 863 | #define NXHERE 25 |
| 864 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 865 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 866 | |
| 867 | union node; |
| 868 | |
| 869 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 870 | smallint type; /* Nxxxx */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 871 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 872 | union node *assign; |
| 873 | union node *args; |
| 874 | union node *redirect; |
| 875 | }; |
| 876 | |
| 877 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 878 | smallint type; |
| 879 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 880 | struct nodelist *cmdlist; |
| 881 | }; |
| 882 | |
| 883 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 884 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 885 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 886 | union node *n; |
| 887 | union node *redirect; |
| 888 | }; |
| 889 | |
| 890 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 891 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 892 | union node *ch1; |
| 893 | union node *ch2; |
| 894 | }; |
| 895 | |
| 896 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 897 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 898 | union node *test; |
| 899 | union node *ifpart; |
| 900 | union node *elsepart; |
| 901 | }; |
| 902 | |
| 903 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 904 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 905 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 906 | union node *args; |
| 907 | union node *body; |
| 908 | char *var; |
| 909 | }; |
| 910 | |
| 911 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 912 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 913 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 914 | union node *expr; |
| 915 | union node *cases; |
| 916 | }; |
| 917 | |
| 918 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 919 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 920 | union node *next; |
| 921 | union node *pattern; |
| 922 | union node *body; |
| 923 | }; |
| 924 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 925 | struct ndefun { |
| 926 | smallint type; |
| 927 | int linno; |
| 928 | char *text; |
| 929 | union node *body; |
| 930 | }; |
| 931 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 932 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 933 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 934 | union node *next; |
| 935 | char *text; |
| 936 | struct nodelist *backquote; |
| 937 | }; |
| 938 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 939 | /* nfile and ndup layout must match! |
| 940 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 941 | * that it is actually NTO2 (>&file), and change its type. |
| 942 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 943 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 944 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 945 | union node *next; |
| 946 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 947 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 948 | union node *fname; |
| 949 | char *expfname; |
| 950 | }; |
| 951 | |
| 952 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 953 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 954 | union node *next; |
| 955 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 956 | int dupfd; |
| 957 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 958 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 962 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 963 | union node *next; |
| 964 | int fd; |
| 965 | union node *doc; |
| 966 | }; |
| 967 | |
| 968 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 969 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 970 | union node *com; |
| 971 | }; |
| 972 | |
| 973 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 974 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 975 | struct ncmd ncmd; |
| 976 | struct npipe npipe; |
| 977 | struct nredir nredir; |
| 978 | struct nbinary nbinary; |
| 979 | struct nif nif; |
| 980 | struct nfor nfor; |
| 981 | struct ncase ncase; |
| 982 | struct nclist nclist; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 983 | struct ndefun ndefun; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 984 | struct narg narg; |
| 985 | struct nfile nfile; |
| 986 | struct ndup ndup; |
| 987 | struct nhere nhere; |
| 988 | struct nnot nnot; |
| 989 | }; |
| 990 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 991 | /* |
| 992 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 993 | * It must be distinct from NULL. |
| 994 | */ |
| 995 | #define NODE_EOF ((union node *) -1L) |
| 996 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 997 | struct nodelist { |
| 998 | struct nodelist *next; |
| 999 | union node *n; |
| 1000 | }; |
| 1001 | |
| 1002 | struct funcnode { |
| 1003 | int count; |
| 1004 | union node n; |
| 1005 | }; |
| 1006 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1007 | /* |
| 1008 | * Free a parse tree. |
| 1009 | */ |
| 1010 | static void |
| 1011 | freefunc(struct funcnode *f) |
| 1012 | { |
| 1013 | if (f && --f->count < 0) |
| 1014 | free(f); |
| 1015 | } |
| 1016 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* ============ Debugging output */ |
| 1019 | |
| 1020 | #if DEBUG |
| 1021 | |
| 1022 | static FILE *tracefile; |
| 1023 | |
| 1024 | static void |
| 1025 | trace_printf(const char *fmt, ...) |
| 1026 | { |
| 1027 | va_list va; |
| 1028 | |
| 1029 | if (debug != 1) |
| 1030 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 1031 | if (DEBUG_TIME) |
| 1032 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 1033 | if (DEBUG_PID) |
| 1034 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 1035 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1036 | 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] | 1037 | va_start(va, fmt); |
| 1038 | vfprintf(tracefile, fmt, va); |
| 1039 | va_end(va); |
| 1040 | } |
| 1041 | |
| 1042 | static void |
| 1043 | trace_vprintf(const char *fmt, va_list va) |
| 1044 | { |
| 1045 | if (debug != 1) |
| 1046 | return; |
| 1047 | vfprintf(tracefile, fmt, va); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1048 | fprintf(tracefile, "\n"); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static void |
| 1052 | trace_puts(const char *s) |
| 1053 | { |
| 1054 | if (debug != 1) |
| 1055 | return; |
| 1056 | fputs(s, tracefile); |
| 1057 | } |
| 1058 | |
| 1059 | static void |
| 1060 | trace_puts_quoted(char *s) |
| 1061 | { |
| 1062 | char *p; |
| 1063 | char c; |
| 1064 | |
| 1065 | if (debug != 1) |
| 1066 | return; |
| 1067 | putc('"', tracefile); |
| 1068 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1069 | switch ((unsigned char)*p) { |
| 1070 | case '\n': c = 'n'; goto backslash; |
| 1071 | case '\t': c = 't'; goto backslash; |
| 1072 | case '\r': c = 'r'; goto backslash; |
| 1073 | case '\"': c = '\"'; goto backslash; |
| 1074 | case '\\': c = '\\'; goto backslash; |
| 1075 | case CTLESC: c = 'e'; goto backslash; |
| 1076 | case CTLVAR: c = 'v'; goto backslash; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1077 | case CTLBACKQ: c = 'q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1078 | backslash: |
| 1079 | putc('\\', tracefile); |
| 1080 | putc(c, tracefile); |
| 1081 | break; |
| 1082 | default: |
| 1083 | if (*p >= ' ' && *p <= '~') |
| 1084 | putc(*p, tracefile); |
| 1085 | else { |
| 1086 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1087 | putc((*p >> 6) & 03, tracefile); |
| 1088 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1089 | putc(*p & 07, tracefile); |
| 1090 | } |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | putc('"', tracefile); |
| 1095 | } |
| 1096 | |
| 1097 | static void |
| 1098 | trace_puts_args(char **ap) |
| 1099 | { |
| 1100 | if (debug != 1) |
| 1101 | return; |
| 1102 | if (!*ap) |
| 1103 | return; |
| 1104 | while (1) { |
| 1105 | trace_puts_quoted(*ap); |
| 1106 | if (!*++ap) { |
| 1107 | putc('\n', tracefile); |
| 1108 | break; |
| 1109 | } |
| 1110 | putc(' ', tracefile); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | static void |
| 1115 | opentrace(void) |
| 1116 | { |
| 1117 | char s[100]; |
| 1118 | #ifdef O_APPEND |
| 1119 | int flags; |
| 1120 | #endif |
| 1121 | |
| 1122 | if (debug != 1) { |
| 1123 | if (tracefile) |
| 1124 | fflush(tracefile); |
| 1125 | /* leave open because libedit might be using it */ |
| 1126 | return; |
| 1127 | } |
| 1128 | strcpy(s, "./trace"); |
| 1129 | if (tracefile) { |
| 1130 | if (!freopen(s, "a", tracefile)) { |
| 1131 | fprintf(stderr, "Can't re-open %s\n", s); |
| 1132 | debug = 0; |
| 1133 | return; |
| 1134 | } |
| 1135 | } else { |
| 1136 | tracefile = fopen(s, "a"); |
| 1137 | if (tracefile == NULL) { |
| 1138 | fprintf(stderr, "Can't open %s\n", s); |
| 1139 | debug = 0; |
| 1140 | return; |
| 1141 | } |
| 1142 | } |
| 1143 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 1144 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1145 | if (flags >= 0) |
| 1146 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 1147 | #endif |
| 1148 | setlinebuf(tracefile); |
| 1149 | fputs("\nTracing started.\n", tracefile); |
| 1150 | } |
| 1151 | |
| 1152 | static void |
| 1153 | indent(int amount, char *pfx, FILE *fp) |
| 1154 | { |
| 1155 | int i; |
| 1156 | |
| 1157 | for (i = 0; i < amount; i++) { |
| 1158 | if (pfx && i == amount - 1) |
| 1159 | fputs(pfx, fp); |
| 1160 | putc('\t', fp); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* little circular references here... */ |
| 1165 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 1166 | |
| 1167 | static void |
| 1168 | sharg(union node *arg, FILE *fp) |
| 1169 | { |
| 1170 | char *p; |
| 1171 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1172 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (arg->type != NARG) { |
| 1175 | out1fmt("<node type %d>\n", arg->type); |
| 1176 | abort(); |
| 1177 | } |
| 1178 | bqlist = arg->narg.backquote; |
| 1179 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1180 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1181 | case CTLESC: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1182 | p++; |
| 1183 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | case CTLVAR: |
| 1186 | putc('$', fp); |
| 1187 | putc('{', fp); |
| 1188 | subtype = *++p; |
| 1189 | if (subtype == VSLENGTH) |
| 1190 | putc('#', fp); |
| 1191 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1192 | while (*p != '=') { |
| 1193 | putc(*p, fp); |
| 1194 | p++; |
| 1195 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (subtype & VSNUL) |
| 1198 | putc(':', fp); |
| 1199 | |
| 1200 | switch (subtype & VSTYPE) { |
| 1201 | case VSNORMAL: |
| 1202 | putc('}', fp); |
| 1203 | break; |
| 1204 | case VSMINUS: |
| 1205 | putc('-', fp); |
| 1206 | break; |
| 1207 | case VSPLUS: |
| 1208 | putc('+', fp); |
| 1209 | break; |
| 1210 | case VSQUESTION: |
| 1211 | putc('?', fp); |
| 1212 | break; |
| 1213 | case VSASSIGN: |
| 1214 | putc('=', fp); |
| 1215 | break; |
| 1216 | case VSTRIMLEFT: |
| 1217 | putc('#', fp); |
| 1218 | break; |
| 1219 | case VSTRIMLEFTMAX: |
| 1220 | putc('#', fp); |
| 1221 | putc('#', fp); |
| 1222 | break; |
| 1223 | case VSTRIMRIGHT: |
| 1224 | putc('%', fp); |
| 1225 | break; |
| 1226 | case VSTRIMRIGHTMAX: |
| 1227 | putc('%', fp); |
| 1228 | putc('%', fp); |
| 1229 | break; |
| 1230 | case VSLENGTH: |
| 1231 | break; |
| 1232 | default: |
| 1233 | out1fmt("<subtype %d>", subtype); |
| 1234 | } |
| 1235 | break; |
| 1236 | case CTLENDVAR: |
| 1237 | putc('}', fp); |
| 1238 | break; |
| 1239 | case CTLBACKQ: |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1240 | putc('$', fp); |
| 1241 | putc('(', fp); |
| 1242 | shtree(bqlist->n, -1, NULL, fp); |
| 1243 | putc(')', fp); |
| 1244 | break; |
| 1245 | default: |
| 1246 | putc(*p, fp); |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1252 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1253 | shcmd(union node *cmd, FILE *fp) |
| 1254 | { |
| 1255 | union node *np; |
| 1256 | int first; |
| 1257 | const char *s; |
| 1258 | int dftfd; |
| 1259 | |
| 1260 | first = 1; |
| 1261 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1262 | if (!first) |
| 1263 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1264 | sharg(np, fp); |
| 1265 | first = 0; |
| 1266 | } |
| 1267 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1268 | if (!first) |
| 1269 | putc(' ', fp); |
| 1270 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1271 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1272 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1273 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1274 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 1275 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1276 | case NTO2: |
| 1277 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1278 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1279 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1280 | case NFROMFD: s = "<&"; break; |
| 1281 | case NFROMTO: s = "<>"; break; |
| 1282 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1283 | } |
| 1284 | if (np->nfile.fd != dftfd) |
| 1285 | fprintf(fp, "%d", np->nfile.fd); |
| 1286 | fputs(s, fp); |
| 1287 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1288 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1289 | } else { |
| 1290 | sharg(np->nfile.fname, fp); |
| 1291 | } |
| 1292 | first = 0; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | static void |
| 1297 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1298 | { |
| 1299 | struct nodelist *lp; |
| 1300 | const char *s; |
| 1301 | |
| 1302 | if (n == NULL) |
| 1303 | return; |
| 1304 | |
| 1305 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1306 | |
| 1307 | if (n == NODE_EOF) { |
| 1308 | fputs("<EOF>", fp); |
| 1309 | return; |
| 1310 | } |
| 1311 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1312 | switch (n->type) { |
| 1313 | case NSEMI: |
| 1314 | s = "; "; |
| 1315 | goto binop; |
| 1316 | case NAND: |
| 1317 | s = " && "; |
| 1318 | goto binop; |
| 1319 | case NOR: |
| 1320 | s = " || "; |
| 1321 | binop: |
| 1322 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1323 | /* if (ind < 0) */ |
| 1324 | fputs(s, fp); |
| 1325 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1326 | break; |
| 1327 | case NCMD: |
| 1328 | shcmd(n, fp); |
| 1329 | if (ind >= 0) |
| 1330 | putc('\n', fp); |
| 1331 | break; |
| 1332 | case NPIPE: |
| 1333 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1334 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1335 | if (lp->next) |
| 1336 | fputs(" | ", fp); |
| 1337 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1338 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1339 | fputs(" &", fp); |
| 1340 | if (ind >= 0) |
| 1341 | putc('\n', fp); |
| 1342 | break; |
| 1343 | default: |
| 1344 | fprintf(fp, "<node type %d>", n->type); |
| 1345 | if (ind >= 0) |
| 1346 | putc('\n', fp); |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | static void |
| 1352 | showtree(union node *n) |
| 1353 | { |
| 1354 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1355 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1358 | #endif /* DEBUG */ |
| 1359 | |
| 1360 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1361 | /* ============ Message printing */ |
| 1362 | |
| 1363 | static void |
| 1364 | ash_vmsg(const char *msg, va_list ap) |
| 1365 | { |
| 1366 | fprintf(stderr, "%s: ", arg0); |
| 1367 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1368 | if (strcmp(arg0, commandname)) |
| 1369 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1370 | if (!iflag || g_parsefile->pf_fd > 0) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1371 | fprintf(stderr, "line %d: ", errlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1372 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1373 | vfprintf(stderr, msg, ap); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 1374 | newline_and_flush(stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1375 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* |
| 1378 | * Exverror is called to raise the error exception. If the second argument |
| 1379 | * is not NULL then error prints an error message using printf style |
| 1380 | * formatting. It then raises the error exception. |
| 1381 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1382 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1383 | static void |
| 1384 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1385 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1386 | #if DEBUG |
| 1387 | if (msg) { |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1388 | TRACE(("ash_vmsg_and_raise(%d):", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1389 | TRACEV((msg, ap)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1390 | } else |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1391 | TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1392 | if (msg) |
| 1393 | #endif |
| 1394 | ash_vmsg(msg, ap); |
| 1395 | |
| 1396 | flush_stdout_stderr(); |
| 1397 | raise_exception(cond); |
| 1398 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1399 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1400 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1401 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1402 | static void |
| 1403 | ash_msg_and_raise_error(const char *msg, ...) |
| 1404 | { |
| 1405 | va_list ap; |
| 1406 | |
Ron Yorston | ea7d2f6 | 2017-01-03 11:18:23 +0100 | [diff] [blame] | 1407 | exitstatus = 2; |
| 1408 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1409 | va_start(ap, msg); |
| 1410 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1411 | /* NOTREACHED */ |
| 1412 | va_end(ap); |
| 1413 | } |
| 1414 | |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1415 | /* |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1416 | * 'fmt' must be a string literal. |
| 1417 | */ |
Denys Vlasenko | 6f97b30 | 2017-09-29 18:17:25 +0200 | [diff] [blame] | 1418 | #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] | 1419 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1420 | static void raise_error_syntax(const char *) NORETURN; |
| 1421 | static void |
| 1422 | raise_error_syntax(const char *msg) |
| 1423 | { |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1424 | errlinno = g_parsefile->linno; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1425 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1426 | /* NOTREACHED */ |
| 1427 | } |
| 1428 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1429 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1430 | static void |
| 1431 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1432 | { |
| 1433 | va_list ap; |
| 1434 | |
| 1435 | va_start(ap, msg); |
| 1436 | ash_vmsg_and_raise(cond, msg, ap); |
| 1437 | /* NOTREACHED */ |
| 1438 | va_end(ap); |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * error/warning routines for external builtins |
| 1443 | */ |
| 1444 | static void |
| 1445 | ash_msg(const char *fmt, ...) |
| 1446 | { |
| 1447 | va_list ap; |
| 1448 | |
| 1449 | va_start(ap, fmt); |
| 1450 | ash_vmsg(fmt, ap); |
| 1451 | va_end(ap); |
| 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * Return a string describing an error. The returned string may be a |
| 1456 | * pointer to a static buffer that will be overwritten on the next call. |
| 1457 | * Action describes the operation that got the error. |
| 1458 | */ |
| 1459 | static const char * |
| 1460 | errmsg(int e, const char *em) |
| 1461 | { |
| 1462 | if (e == ENOENT || e == ENOTDIR) { |
| 1463 | return em; |
| 1464 | } |
| 1465 | return strerror(e); |
| 1466 | } |
| 1467 | |
| 1468 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1469 | /* ============ Memory allocation */ |
| 1470 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1471 | #if 0 |
| 1472 | /* I consider these wrappers nearly useless: |
| 1473 | * ok, they return you to nearest exception handler, but |
| 1474 | * how much memory do you leak in the process, making |
| 1475 | * memory starvation worse? |
| 1476 | */ |
| 1477 | static void * |
| 1478 | ckrealloc(void * p, size_t nbytes) |
| 1479 | { |
| 1480 | p = realloc(p, nbytes); |
| 1481 | if (!p) |
| 1482 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1483 | return p; |
| 1484 | } |
| 1485 | |
| 1486 | static void * |
| 1487 | ckmalloc(size_t nbytes) |
| 1488 | { |
| 1489 | return ckrealloc(NULL, nbytes); |
| 1490 | } |
| 1491 | |
| 1492 | static void * |
| 1493 | ckzalloc(size_t nbytes) |
| 1494 | { |
| 1495 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1496 | } |
| 1497 | |
| 1498 | static char * |
| 1499 | ckstrdup(const char *s) |
| 1500 | { |
| 1501 | char *p = strdup(s); |
| 1502 | if (!p) |
| 1503 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1504 | return p; |
| 1505 | } |
| 1506 | #else |
| 1507 | /* Using bbox equivalents. They exit if out of memory */ |
| 1508 | # define ckrealloc xrealloc |
| 1509 | # define ckmalloc xmalloc |
| 1510 | # define ckzalloc xzalloc |
| 1511 | # define ckstrdup xstrdup |
| 1512 | #endif |
| 1513 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1514 | /* |
| 1515 | * It appears that grabstackstr() will barf with such alignments |
| 1516 | * because stalloc() will return a string allocated in a new stackblock. |
| 1517 | */ |
| 1518 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1519 | enum { |
| 1520 | /* Most machines require the value returned from malloc to be aligned |
| 1521 | * in some way. The following macro will get this right |
| 1522 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1523 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1524 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1525 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1526 | }; |
| 1527 | |
| 1528 | struct stack_block { |
| 1529 | struct stack_block *prev; |
| 1530 | char space[MINSIZE]; |
| 1531 | }; |
| 1532 | |
| 1533 | struct stackmark { |
| 1534 | struct stack_block *stackp; |
| 1535 | char *stacknxt; |
| 1536 | size_t stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1537 | }; |
| 1538 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1539 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1540 | struct globals_memstack { |
| 1541 | struct stack_block *g_stackp; // = &stackbase; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1542 | char *g_stacknxt; // = stackbase.space; |
| 1543 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1544 | size_t g_stacknleft; // = MINSIZE; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1545 | struct stack_block stackbase; |
| 1546 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 1547 | extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1548 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1549 | #define g_stackp (G_memstack.g_stackp ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1550 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1551 | #define sstrend (G_memstack.sstrend ) |
| 1552 | #define g_stacknleft (G_memstack.g_stacknleft) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1553 | #define stackbase (G_memstack.stackbase ) |
| 1554 | #define INIT_G_memstack() do { \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 1555 | (*(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] | 1556 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1557 | g_stackp = &stackbase; \ |
| 1558 | g_stacknxt = stackbase.space; \ |
| 1559 | g_stacknleft = MINSIZE; \ |
| 1560 | sstrend = stackbase.space + MINSIZE; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1561 | } while (0) |
| 1562 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1563 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1564 | #define stackblock() ((void *)g_stacknxt) |
| 1565 | #define stackblocksize() g_stacknleft |
| 1566 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1567 | /* |
| 1568 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1569 | * to make this more efficient, and also to avoid all sorts of exception |
| 1570 | * handling code to handle interrupts in the middle of a parse. |
| 1571 | * |
| 1572 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1573 | * well. |
| 1574 | */ |
| 1575 | static void * |
| 1576 | stalloc(size_t nbytes) |
| 1577 | { |
| 1578 | char *p; |
| 1579 | size_t aligned; |
| 1580 | |
| 1581 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1582 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1583 | size_t len; |
| 1584 | size_t blocksize; |
| 1585 | struct stack_block *sp; |
| 1586 | |
| 1587 | blocksize = aligned; |
| 1588 | if (blocksize < MINSIZE) |
| 1589 | blocksize = MINSIZE; |
| 1590 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1591 | if (len < blocksize) |
| 1592 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1593 | INT_OFF; |
| 1594 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1595 | sp->prev = g_stackp; |
| 1596 | g_stacknxt = sp->space; |
| 1597 | g_stacknleft = blocksize; |
| 1598 | sstrend = g_stacknxt + blocksize; |
| 1599 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1600 | INT_ON; |
| 1601 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1602 | p = g_stacknxt; |
| 1603 | g_stacknxt += aligned; |
| 1604 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1605 | return p; |
| 1606 | } |
| 1607 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1608 | static void * |
| 1609 | stzalloc(size_t nbytes) |
| 1610 | { |
| 1611 | return memset(stalloc(nbytes), 0, nbytes); |
| 1612 | } |
| 1613 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1614 | static void |
| 1615 | stunalloc(void *p) |
| 1616 | { |
| 1617 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1618 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1619 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1620 | abort(); |
| 1621 | } |
| 1622 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1623 | g_stacknleft += g_stacknxt - (char *)p; |
| 1624 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1627 | /* |
| 1628 | * Like strdup but works with the ash stack. |
| 1629 | */ |
| 1630 | static char * |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 1631 | sstrdup(const char *p) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1632 | { |
| 1633 | size_t len = strlen(p) + 1; |
| 1634 | return memcpy(stalloc(len), p, len); |
| 1635 | } |
| 1636 | |
Denys Vlasenko | 03c36e0 | 2018-01-10 15:18:35 +0100 | [diff] [blame] | 1637 | static ALWAYS_INLINE void |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1638 | grabstackblock(size_t len) |
| 1639 | { |
Denys Vlasenko | a318bba | 2016-10-26 18:26:27 +0200 | [diff] [blame] | 1640 | stalloc(len); |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | static void |
| 1644 | pushstackmark(struct stackmark *mark, size_t len) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1645 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1646 | mark->stackp = g_stackp; |
| 1647 | mark->stacknxt = g_stacknxt; |
| 1648 | mark->stacknleft = g_stacknleft; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1649 | grabstackblock(len); |
| 1650 | } |
| 1651 | |
| 1652 | static void |
| 1653 | setstackmark(struct stackmark *mark) |
| 1654 | { |
| 1655 | pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | static void |
| 1659 | popstackmark(struct stackmark *mark) |
| 1660 | { |
| 1661 | struct stack_block *sp; |
| 1662 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1663 | if (!mark->stackp) |
| 1664 | return; |
| 1665 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1666 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1667 | while (g_stackp != mark->stackp) { |
| 1668 | sp = g_stackp; |
| 1669 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1670 | free(sp); |
| 1671 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1672 | g_stacknxt = mark->stacknxt; |
| 1673 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1674 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1675 | INT_ON; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * When the parser reads in a string, it wants to stick the string on the |
| 1680 | * stack and only adjust the stack pointer when it knows how big the |
| 1681 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1682 | * of space on top of the stack and stackblocklen returns the length of |
| 1683 | * this block. Growstackblock will grow this space by at least one byte, |
| 1684 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1685 | * part of the block that has been used. |
| 1686 | */ |
| 1687 | static void |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1688 | growstackblock(size_t min) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1689 | { |
| 1690 | size_t newlen; |
| 1691 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1692 | newlen = g_stacknleft * 2; |
| 1693 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1694 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1695 | min = SHELL_ALIGN(min | 128); |
| 1696 | if (newlen < min) |
| 1697 | newlen += min; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1698 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1699 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1700 | struct stack_block *sp; |
| 1701 | struct stack_block *prevstackp; |
| 1702 | size_t grosslen; |
| 1703 | |
| 1704 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1705 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1706 | prevstackp = sp->prev; |
| 1707 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1708 | sp = ckrealloc(sp, grosslen); |
| 1709 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1710 | g_stackp = sp; |
| 1711 | g_stacknxt = sp->space; |
| 1712 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1713 | sstrend = sp->space + newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1714 | INT_ON; |
| 1715 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1716 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1717 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1718 | char *p = stalloc(newlen); |
| 1719 | |
| 1720 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1721 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1722 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1726 | /* |
| 1727 | * The following routines are somewhat easier to use than the above. |
| 1728 | * The user declares a variable of type STACKSTR, which may be declared |
| 1729 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1730 | * the user uses the macro STPUTC to add characters to the string. In |
| 1731 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1732 | * grown as necessary. When the user is done, she can just leave the |
| 1733 | * string there and refer to it using stackblock(). Or she can allocate |
| 1734 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1735 | * someone else to use the stack temporarily and then continue to grow |
| 1736 | * the string, the user should use grabstack to allocate the space, and |
| 1737 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1738 | * |
| 1739 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1740 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1741 | * is space for at least one character. |
| 1742 | */ |
| 1743 | static void * |
| 1744 | growstackstr(void) |
| 1745 | { |
| 1746 | size_t len = stackblocksize(); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1747 | growstackblock(0); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1748 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1751 | static char * |
| 1752 | growstackto(size_t len) |
| 1753 | { |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1754 | if (stackblocksize() < len) |
| 1755 | growstackblock(len); |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1756 | return stackblock(); |
| 1757 | } |
| 1758 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1759 | /* |
| 1760 | * Called from CHECKSTRSPACE. |
| 1761 | */ |
| 1762 | static char * |
| 1763 | makestrspace(size_t newlen, char *p) |
| 1764 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1765 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1766 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1767 | return growstackto(len + newlen) + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | static char * |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1771 | stnputs(const char *s, size_t n, char *p) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1772 | { |
| 1773 | p = makestrspace(n, p); |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1774 | p = (char *)mempcpy(p, s, n); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1775 | return p; |
| 1776 | } |
| 1777 | |
| 1778 | static char * |
| 1779 | stack_putstr(const char *s, char *p) |
| 1780 | { |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1781 | return stnputs(s, strlen(s), p); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1784 | static char * |
| 1785 | _STPUTC(int c, char *p) |
| 1786 | { |
| 1787 | if (p == sstrend) |
| 1788 | p = growstackstr(); |
| 1789 | *p++ = c; |
| 1790 | return p; |
| 1791 | } |
| 1792 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1793 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1794 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1795 | #define CHECKSTRSPACE(n, p) do { \ |
| 1796 | char *q = (p); \ |
| 1797 | size_t l = (n); \ |
| 1798 | size_t m = sstrend - q; \ |
| 1799 | if (l > m) \ |
| 1800 | (p) = makestrspace(l, q); \ |
| 1801 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1802 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1803 | #define STACKSTRNUL(p) do { \ |
| 1804 | if ((p) == sstrend) \ |
| 1805 | (p) = growstackstr(); \ |
| 1806 | *(p) = '\0'; \ |
| 1807 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1808 | #define STUNPUTC(p) (--(p)) |
| 1809 | #define STTOPC(p) ((p)[-1]) |
| 1810 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1811 | |
| 1812 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1813 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1814 | #define stackstrend() ((void *)sstrend) |
| 1815 | |
| 1816 | |
| 1817 | /* ============ String helpers */ |
| 1818 | |
| 1819 | /* |
| 1820 | * prefix -- see if pfx is a prefix of string. |
| 1821 | */ |
| 1822 | static char * |
| 1823 | prefix(const char *string, const char *pfx) |
| 1824 | { |
| 1825 | while (*pfx) { |
| 1826 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1827 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1828 | } |
| 1829 | return (char *) string; |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Check for a valid number. This should be elsewhere. |
| 1834 | */ |
| 1835 | static int |
| 1836 | is_number(const char *p) |
| 1837 | { |
| 1838 | do { |
| 1839 | if (!isdigit(*p)) |
| 1840 | return 0; |
| 1841 | } while (*++p != '\0'); |
| 1842 | return 1; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * Convert a string of digits to an integer, printing an error message on |
| 1847 | * failure. |
| 1848 | */ |
| 1849 | static int |
| 1850 | number(const char *s) |
| 1851 | { |
| 1852 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1853 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1854 | return atoi(s); |
| 1855 | } |
| 1856 | |
| 1857 | /* |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1858 | * Produce a single quoted string suitable as input to the shell. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1859 | * The return string is allocated on the stack. |
| 1860 | */ |
| 1861 | static char * |
| 1862 | single_quote(const char *s) |
| 1863 | { |
| 1864 | char *p; |
| 1865 | |
| 1866 | STARTSTACKSTR(p); |
| 1867 | |
| 1868 | do { |
| 1869 | char *q; |
| 1870 | size_t len; |
| 1871 | |
| 1872 | len = strchrnul(s, '\'') - s; |
| 1873 | |
| 1874 | q = p = makestrspace(len + 3, p); |
| 1875 | |
| 1876 | *q++ = '\''; |
Denys Vlasenko | 94af83e | 2017-07-23 21:55:40 +0200 | [diff] [blame] | 1877 | q = (char *)mempcpy(q, s, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1878 | *q++ = '\''; |
| 1879 | s += len; |
| 1880 | |
| 1881 | STADJUST(q - p, p); |
| 1882 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1883 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1884 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1885 | len = 0; |
| 1886 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1887 | |
| 1888 | q = p = makestrspace(len + 3, p); |
| 1889 | |
| 1890 | *q++ = '"'; |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1891 | q = (char *)mempcpy(q, s - len, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1892 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1893 | |
| 1894 | STADJUST(q - p, p); |
| 1895 | } while (*s); |
| 1896 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1897 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1898 | |
| 1899 | return stackblock(); |
| 1900 | } |
| 1901 | |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1902 | /* |
| 1903 | * Produce a possibly single quoted string suitable as input to the shell. |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1904 | * If quoting was done, the return string is allocated on the stack, |
| 1905 | * otherwise a pointer to the original string is returned. |
| 1906 | */ |
| 1907 | static const char * |
| 1908 | maybe_single_quote(const char *s) |
| 1909 | { |
| 1910 | const char *p = s; |
| 1911 | |
| 1912 | while (*p) { |
| 1913 | /* Assuming ACSII */ |
| 1914 | /* quote ctrl_chars space !"#$%&'()* */ |
| 1915 | if (*p < '+') |
| 1916 | goto need_quoting; |
| 1917 | /* quote ;<=>? */ |
| 1918 | if (*p >= ';' && *p <= '?') |
| 1919 | goto need_quoting; |
| 1920 | /* quote `[\ */ |
| 1921 | if (*p == '`') |
| 1922 | goto need_quoting; |
| 1923 | if (*p == '[') |
| 1924 | goto need_quoting; |
| 1925 | if (*p == '\\') |
| 1926 | goto need_quoting; |
| 1927 | /* quote {|}~ DEL and high bytes */ |
| 1928 | if (*p > 'z') |
| 1929 | goto need_quoting; |
| 1930 | /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */ |
| 1931 | /* TODO: maybe avoid quoting % */ |
| 1932 | p++; |
| 1933 | } |
| 1934 | return s; |
| 1935 | |
| 1936 | need_quoting: |
| 1937 | return single_quote(s); |
| 1938 | } |
| 1939 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1940 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1941 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1942 | |
| 1943 | static char **argptr; /* argument list for builtin commands */ |
| 1944 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1945 | static char *optptr; /* used by nextopt */ |
| 1946 | |
| 1947 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1948 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1949 | * The library getopt must have the BSD extension static variable |
| 1950 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1951 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1952 | * Standard option processing (a la getopt) for builtin routines. |
| 1953 | * The only argument that is passed to nextopt is the option string; |
| 1954 | * the other arguments are unnecessary. It returns the character, |
| 1955 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1956 | */ |
| 1957 | static int |
| 1958 | nextopt(const char *optstring) |
| 1959 | { |
| 1960 | char *p; |
| 1961 | const char *q; |
| 1962 | char c; |
| 1963 | |
| 1964 | p = optptr; |
| 1965 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1966 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1967 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1968 | if (p == NULL) |
| 1969 | return '\0'; |
| 1970 | if (*p != '-') |
| 1971 | return '\0'; |
| 1972 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1973 | return '\0'; |
| 1974 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1975 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1976 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1977 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1978 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1979 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1980 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1981 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1982 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1983 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1984 | if (*++q == ':') |
| 1985 | q++; |
| 1986 | } |
| 1987 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1988 | if (*p == '\0') { |
| 1989 | p = *argptr++; |
| 1990 | if (p == NULL) |
| 1991 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1992 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1993 | optionarg = p; |
| 1994 | p = NULL; |
| 1995 | } |
| 1996 | optptr = p; |
| 1997 | return c; |
| 1998 | } |
| 1999 | |
| 2000 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2001 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2002 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2003 | struct shparam { |
| 2004 | int nparam; /* # of positional parameters (without $0) */ |
| 2005 | #if ENABLE_ASH_GETOPTS |
| 2006 | int optind; /* next parameter to be processed by getopts */ |
| 2007 | int optoff; /* used by getopts */ |
| 2008 | #endif |
| 2009 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 2010 | char **p; /* parameter list */ |
| 2011 | }; |
| 2012 | |
| 2013 | /* |
| 2014 | * Free the list of positional parameters. |
| 2015 | */ |
| 2016 | static void |
| 2017 | freeparam(volatile struct shparam *param) |
| 2018 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2019 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 2020 | char **ap, **ap1; |
| 2021 | ap = ap1 = param->p; |
| 2022 | while (*ap) |
| 2023 | free(*ap++); |
| 2024 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2029 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2030 | #endif |
| 2031 | |
| 2032 | struct var { |
| 2033 | struct var *next; /* next entry in hash list */ |
| 2034 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2035 | const char *var_text; /* name=value */ |
| 2036 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2037 | /* the variable gets set/unset */ |
| 2038 | }; |
| 2039 | |
| 2040 | struct localvar { |
| 2041 | struct localvar *next; /* next local variable in list */ |
| 2042 | struct var *vp; /* the variable that was made local */ |
| 2043 | int flags; /* saved flags */ |
| 2044 | const char *text; /* saved text */ |
| 2045 | }; |
| 2046 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2047 | /* flags */ |
| 2048 | #define VEXPORT 0x01 /* variable is exported */ |
| 2049 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 2050 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 2051 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 2052 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 2053 | #define VUNSET 0x20 /* the variable is not set */ |
| 2054 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 2055 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 2056 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2057 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2058 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 2059 | #else |
| 2060 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2061 | #endif |
| 2062 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2063 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2064 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2065 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2066 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2067 | change_lc_all(const char *value) |
| 2068 | { |
| 2069 | if (value && *value != '\0') |
| 2070 | setlocale(LC_ALL, value); |
| 2071 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2072 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2073 | change_lc_ctype(const char *value) |
| 2074 | { |
| 2075 | if (value && *value != '\0') |
| 2076 | setlocale(LC_CTYPE, value); |
| 2077 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2078 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2079 | #if ENABLE_ASH_MAIL |
| 2080 | static void chkmail(void); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 2081 | static void changemail(const char *var_value) FAST_FUNC; |
| 2082 | #else |
| 2083 | # define chkmail() ((void)0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2084 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2085 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2086 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2087 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2088 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2089 | #if BASH_EPOCH_VARS |
| 2090 | static void change_seconds(const char *) FAST_FUNC; |
| 2091 | static void change_realtime(const char *) FAST_FUNC; |
| 2092 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2093 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2094 | static const struct { |
| 2095 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2096 | const char *var_text; |
| 2097 | void (*var_func)(const char *) FAST_FUNC; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 2098 | } varinit_data[] ALIGN_PTR = { |
Denys Vlasenko | 566a313 | 2012-07-07 21:40:35 +0200 | [diff] [blame] | 2099 | /* |
| 2100 | * Note: VEXPORT would not work correctly here for NOFORK applets: |
| 2101 | * some environment strings may be constant. |
| 2102 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2103 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2104 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2105 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 2106 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2107 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2108 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 2109 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 2110 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 2111 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2112 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 2113 | { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2114 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2115 | { VSTRFIXED|VTEXTFIXED , NULL /* inited to linenovar */, NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2116 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2117 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2118 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2119 | #if BASH_EPOCH_VARS |
| 2120 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHSECONDS", change_seconds }, |
| 2121 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHREALTIME", change_realtime }, |
| 2122 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2123 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2124 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 2125 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2126 | #endif |
| 2127 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2128 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2129 | #endif |
| 2130 | }; |
| 2131 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2132 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2133 | |
| 2134 | struct globals_var { |
| 2135 | struct shparam shellparam; /* $@ current positional parameters */ |
| 2136 | struct redirtab *redirlist; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 2137 | int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2138 | struct var *vartab[VTABSIZE]; |
| 2139 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2140 | int lineno; |
| 2141 | char linenovar[sizeof("LINENO=") + sizeof(int)*3]; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2142 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 2143 | extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 2144 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2145 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2146 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2147 | #define preverrout_fd (G_var.preverrout_fd) |
| 2148 | #define vartab (G_var.vartab ) |
| 2149 | #define varinit (G_var.varinit ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2150 | #define lineno (G_var.lineno ) |
| 2151 | #define linenovar (G_var.linenovar ) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2152 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2153 | #if ENABLE_ASH_MAIL |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2154 | # define vmail varinit[1] |
| 2155 | # define vmpath varinit[2] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2156 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2157 | #define VAR_OFFSET1 (ENABLE_ASH_MAIL*2) |
| 2158 | #define vpath varinit[VAR_OFFSET1 + 1] |
| 2159 | #define vps1 varinit[VAR_OFFSET1 + 2] |
| 2160 | #define vps2 varinit[VAR_OFFSET1 + 3] |
| 2161 | #define vps4 varinit[VAR_OFFSET1 + 4] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2162 | #if ENABLE_ASH_GETOPTS |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2163 | # define voptind varinit[VAR_OFFSET1 + 5] |
| 2164 | #endif |
| 2165 | #define VAR_OFFSET2 (VAR_OFFSET1 + ENABLE_ASH_GETOPTS) |
| 2166 | #define vlineno varinit[VAR_OFFSET2 + 5] |
| 2167 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 2168 | # define vrandom varinit[VAR_OFFSET2 + 6] |
| 2169 | #endif |
| 2170 | #define VAR_OFFSET3 (VAR_OFFSET2 + ENABLE_ASH_RANDOM_SUPPORT) |
| 2171 | #if BASH_EPOCH_VARS |
| 2172 | # define vepochs varinit[VAR_OFFSET3 + 6] |
| 2173 | # define vepochr varinit[VAR_OFFSET3 + 7] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2174 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2175 | #define INIT_G_var() do { \ |
| 2176 | unsigned i; \ |
Denys Vlasenko | af7169b | 2019-10-25 12:12:22 +0200 | [diff] [blame] | 2177 | (*(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] | 2178 | barrier(); \ |
| 2179 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 2180 | varinit[i].flags = varinit_data[i].flags; \ |
| 2181 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 2182 | varinit[i].var_func = varinit_data[i].var_func; \ |
| 2183 | } \ |
| 2184 | strcpy(linenovar, "LINENO="); \ |
| 2185 | vlineno.var_text = linenovar; \ |
| 2186 | } while (0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2187 | |
| 2188 | /* |
| 2189 | * The following macros access the values of the above variables. |
| 2190 | * They have to skip over the name. They return the null string |
| 2191 | * for unset variables. |
| 2192 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2193 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2194 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2195 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2196 | # define mailval() (vmail.var_text + 5) |
| 2197 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2198 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 2199 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2200 | #define pathval() (vpath.var_text + 5) |
| 2201 | #define ps1val() (vps1.var_text + 4) |
| 2202 | #define ps2val() (vps2.var_text + 4) |
| 2203 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2204 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2205 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2206 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2207 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2208 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2209 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2210 | getoptsreset(const char *value) |
| 2211 | { |
Denys Vlasenko | 4628945 | 2017-08-11 00:59:36 +0200 | [diff] [blame] | 2212 | shellparam.optind = 1; |
| 2213 | if (is_number(value)) |
| 2214 | shellparam.optind = number(value) ?: 1; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2215 | shellparam.optoff = -1; |
| 2216 | } |
| 2217 | #endif |
| 2218 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2219 | /* |
| 2220 | * Compares two strings up to the first = or '\0'. The first |
| 2221 | * string must be terminated by '='; the second may be terminated by |
| 2222 | * either '=' or '\0'. |
| 2223 | */ |
| 2224 | static int |
| 2225 | varcmp(const char *p, const char *q) |
| 2226 | { |
| 2227 | int c, d; |
| 2228 | |
| 2229 | while ((c = *p) == (d = *q)) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2230 | if (c == '\0' || c == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2231 | goto out; |
| 2232 | p++; |
| 2233 | q++; |
| 2234 | } |
| 2235 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2236 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2237 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2238 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2239 | out: |
| 2240 | return c - d; |
| 2241 | } |
| 2242 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2243 | /* |
| 2244 | * Find the appropriate entry in the hash table from the name. |
| 2245 | */ |
| 2246 | static struct var ** |
| 2247 | hashvar(const char *p) |
| 2248 | { |
| 2249 | unsigned hashval; |
| 2250 | |
| 2251 | hashval = ((unsigned char) *p) << 4; |
| 2252 | while (*p && *p != '=') |
| 2253 | hashval += (unsigned char) *p++; |
| 2254 | return &vartab[hashval % VTABSIZE]; |
| 2255 | } |
| 2256 | |
| 2257 | static int |
| 2258 | vpcmp(const void *a, const void *b) |
| 2259 | { |
| 2260 | return varcmp(*(const char **)a, *(const char **)b); |
| 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * This routine initializes the builtin variables. |
| 2265 | */ |
| 2266 | static void |
| 2267 | initvar(void) |
| 2268 | { |
| 2269 | struct var *vp; |
| 2270 | struct var *end; |
| 2271 | struct var **vpp; |
| 2272 | |
| 2273 | /* |
| 2274 | * PS1 depends on uid |
| 2275 | */ |
| 2276 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2277 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2278 | #else |
| 2279 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2280 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2281 | #endif |
| 2282 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2283 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2284 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2285 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2286 | vp->next = *vpp; |
| 2287 | *vpp = vp; |
| 2288 | } while (++vp < end); |
| 2289 | } |
| 2290 | |
| 2291 | static struct var ** |
| 2292 | findvar(struct var **vpp, const char *name) |
| 2293 | { |
| 2294 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2295 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2296 | break; |
| 2297 | } |
| 2298 | } |
| 2299 | return vpp; |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Find the value of a variable. Returns NULL if not set. |
| 2304 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2305 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2306 | lookupvar(const char *name) |
| 2307 | { |
| 2308 | struct var *v; |
| 2309 | |
| 2310 | v = *findvar(hashvar(name), name); |
| 2311 | if (v) { |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2312 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2313 | /* |
| 2314 | * Dynamic variables are implemented roughly the same way they are |
| 2315 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2316 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2317 | * lookup will no longer happen at that point. -- PFM. |
| 2318 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2319 | if (v->flags & VDYNAMIC) |
| 2320 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2321 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2322 | if (!(v->flags & VUNSET)) { |
| 2323 | if (v == &vlineno && v->var_text == linenovar) { |
| 2324 | fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno); |
| 2325 | } |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2326 | return var_end(v->var_text); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2327 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2328 | } |
| 2329 | return NULL; |
| 2330 | } |
| 2331 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2332 | #if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 2333 | static void |
| 2334 | reinit_unicode_for_ash(void) |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2335 | { |
| 2336 | /* Unicode support should be activated even if LANG is set |
| 2337 | * _during_ shell execution, not only if it was set when |
| 2338 | * shell was started. Therefore, re-check LANG every time: |
| 2339 | */ |
| 2340 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2341 | || ENABLE_UNICODE_USING_LOCALE |
| 2342 | ) { |
| 2343 | const char *s = lookupvar("LC_ALL"); |
| 2344 | if (!s) s = lookupvar("LC_CTYPE"); |
| 2345 | if (!s) s = lookupvar("LANG"); |
| 2346 | reinit_unicode(s); |
| 2347 | } |
| 2348 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2349 | #else |
| 2350 | # define reinit_unicode_for_ash() ((void)0) |
| 2351 | #endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2352 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2353 | /* |
| 2354 | * Search the environment of a builtin command. |
| 2355 | */ |
Denys Vlasenko | 488e609 | 2017-07-26 23:08:36 +0200 | [diff] [blame] | 2356 | static ALWAYS_INLINE const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2357 | bltinlookup(const char *name) |
| 2358 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2359 | return lookupvar(name); |
| 2360 | } |
| 2361 | |
| 2362 | /* |
| 2363 | * Same as setvar except that the variable and value are passed in |
| 2364 | * the first argument as name=value. Since the first argument will |
| 2365 | * be actually stored in the table, it should not be a string that |
| 2366 | * will go away. |
| 2367 | * Called with interrupts off. |
| 2368 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2369 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2370 | setvareq(char *s, int flags) |
| 2371 | { |
| 2372 | struct var *vp, **vpp; |
| 2373 | |
| 2374 | vpp = hashvar(s); |
| 2375 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2376 | vpp = findvar(vpp, s); |
| 2377 | vp = *vpp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2378 | if (vp) { |
| 2379 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2380 | const char *n; |
| 2381 | |
| 2382 | if (flags & VNOSAVE) |
| 2383 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2384 | n = vp->var_text; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 2385 | exitstatus = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2386 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2387 | } |
| 2388 | |
| 2389 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2390 | goto out; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2391 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2392 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2393 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2394 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2395 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2396 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2397 | |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2398 | if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) { |
| 2399 | *vpp = vp->next; |
| 2400 | free(vp); |
| 2401 | out_free: |
| 2402 | if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE) |
| 2403 | free(s); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2404 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2407 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2408 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Ron Yorston | d96c69d | 2019-04-15 10:49:35 +0100 | [diff] [blame] | 2409 | if (flags & VUNSET) |
| 2410 | flags &= ~VDYNAMIC; |
| 2411 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2412 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2413 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2414 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2415 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2416 | if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 2417 | goto out_free; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2418 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2419 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2420 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2421 | *vpp = vp; |
| 2422 | } |
| 2423 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2424 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2425 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2426 | vp->flags = flags; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2427 | |
| 2428 | out: |
| 2429 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * Set the value of a variable. The flags argument is ored with the |
| 2434 | * flags of the variable. If val is NULL, the variable is unset. |
| 2435 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2436 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2437 | setvar(const char *name, const char *val, int flags) |
| 2438 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2439 | const char *q; |
| 2440 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2441 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2442 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2443 | size_t vallen; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2444 | struct var *vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2445 | |
| 2446 | q = endofname(name); |
| 2447 | p = strchrnul(q, '='); |
| 2448 | namelen = p - name; |
| 2449 | if (!namelen || p != q) |
| 2450 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2451 | vallen = 0; |
| 2452 | if (val == NULL) { |
| 2453 | flags |= VUNSET; |
| 2454 | } else { |
| 2455 | vallen = strlen(val); |
| 2456 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2457 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2458 | INT_OFF; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2459 | nameeq = ckzalloc(namelen + vallen + 2); |
Denys Vlasenko | da2244f | 2017-07-21 18:51:29 +0200 | [diff] [blame] | 2460 | p = mempcpy(nameeq, name, namelen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2461 | if (val) { |
| 2462 | *p++ = '='; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2463 | memcpy(p, val, vallen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2464 | } |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2465 | vp = setvareq(nameeq, flags | VNOSAVE); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2466 | INT_ON; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2467 | |
| 2468 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2471 | static void FAST_FUNC |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2472 | setvar0(const char *name, const char *val) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2473 | { |
| 2474 | setvar(name, val, 0); |
| 2475 | } |
| 2476 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2477 | /* |
| 2478 | * Unset the specified variable. |
| 2479 | */ |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2480 | static void |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2481 | unsetvar(const char *s) |
| 2482 | { |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 2483 | setvar(s, NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2487 | * Generate a list of variables satisfying the given conditions. |
| 2488 | */ |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2489 | #if !ENABLE_FEATURE_SH_NOFORK |
| 2490 | # define listvars(on, off, lp, end) listvars(on, off, end) |
| 2491 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2492 | static char ** |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2493 | listvars(int on, int off, struct strlist *lp, char ***end) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2494 | { |
| 2495 | struct var **vpp; |
| 2496 | struct var *vp; |
| 2497 | char **ep; |
| 2498 | int mask; |
| 2499 | |
| 2500 | STARTSTACKSTR(ep); |
| 2501 | vpp = vartab; |
| 2502 | mask = on | off; |
| 2503 | do { |
| 2504 | for (vp = *vpp; vp; vp = vp->next) { |
| 2505 | if ((vp->flags & mask) == on) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2506 | #if ENABLE_FEATURE_SH_NOFORK |
| 2507 | /* If variable with the same name is both |
| 2508 | * exported and temporarily set for a command: |
| 2509 | * export ZVAR=5 |
| 2510 | * ZVAR=6 printenv |
| 2511 | * then "ZVAR=6" will be both in vartab and |
| 2512 | * lp lists. Do not pass it twice to printenv. |
| 2513 | */ |
| 2514 | struct strlist *lp1 = lp; |
| 2515 | while (lp1) { |
| 2516 | if (strcmp(lp1->text, vp->var_text) == 0) |
| 2517 | goto skip; |
| 2518 | lp1 = lp1->next; |
| 2519 | } |
| 2520 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2521 | if (ep == stackstrend()) |
| 2522 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2523 | *ep++ = (char*)vp->var_text; |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2524 | #if ENABLE_FEATURE_SH_NOFORK |
| 2525 | skip: ; |
| 2526 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | } while (++vpp < vartab + VTABSIZE); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2530 | |
| 2531 | #if ENABLE_FEATURE_SH_NOFORK |
| 2532 | while (lp) { |
| 2533 | if (ep == stackstrend()) |
| 2534 | ep = growstackstr(); |
| 2535 | *ep++ = lp->text; |
| 2536 | lp = lp->next; |
| 2537 | } |
| 2538 | #endif |
| 2539 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2540 | if (ep == stackstrend()) |
| 2541 | ep = growstackstr(); |
| 2542 | if (end) |
| 2543 | *end = ep; |
| 2544 | *ep++ = NULL; |
| 2545 | return grabstackstr(ep); |
| 2546 | } |
| 2547 | |
| 2548 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2549 | /* ============ Path search helper */ |
| 2550 | static const char * |
| 2551 | legal_pathopt(const char *opt, const char *term, int magic) |
| 2552 | { |
| 2553 | switch (magic) { |
| 2554 | case 0: |
| 2555 | opt = NULL; |
| 2556 | break; |
| 2557 | |
| 2558 | case 1: |
| 2559 | opt = prefix(opt, "builtin") ?: prefix(opt, "func"); |
| 2560 | break; |
| 2561 | |
| 2562 | default: |
| 2563 | opt += strcspn(opt, term); |
| 2564 | break; |
| 2565 | } |
| 2566 | |
| 2567 | if (opt && *opt == '%') |
| 2568 | opt++; |
| 2569 | |
| 2570 | return opt; |
| 2571 | } |
| 2572 | |
| 2573 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2574 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2575 | * of the path before the first call; padvance will update |
| 2576 | * this value as it proceeds. Successive calls to padvance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2577 | * the possible path expansions in sequence. If an option (indicated by |
| 2578 | * a percent sign) appears in the path entry then the global variable |
| 2579 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2580 | * NULL. |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2581 | * |
| 2582 | * If magic is 0 then pathopt recognition will be disabled. If magic is |
| 2583 | * 1 we shall recognise %builtin/%func. Otherwise we shall accept any |
| 2584 | * pathopt. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2585 | */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2586 | static const char *pathopt; /* set by padvance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2587 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2588 | static int |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2589 | padvance_magic(const char **path, const char *name, int magic) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2590 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2591 | const char *term = "%:"; |
| 2592 | const char *lpathopt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2593 | const char *p; |
| 2594 | char *q; |
| 2595 | const char *start; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2596 | size_t qlen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2597 | size_t len; |
| 2598 | |
| 2599 | if (*path == NULL) |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2600 | return -1; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2601 | |
| 2602 | lpathopt = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2603 | start = *path; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2604 | |
| 2605 | if (*start == '%' && (p = legal_pathopt(start + 1, term, magic))) { |
| 2606 | lpathopt = start + 1; |
| 2607 | start = p; |
| 2608 | term = ":"; |
| 2609 | } |
| 2610 | |
| 2611 | len = strcspn(start, term); |
| 2612 | p = start + len; |
| 2613 | |
| 2614 | if (*p == '%') { |
| 2615 | size_t extra = strchrnul(p, ':') - p; |
| 2616 | |
| 2617 | if (legal_pathopt(p + 1, term, magic)) |
| 2618 | lpathopt = p + 1; |
| 2619 | else |
| 2620 | len += extra; |
| 2621 | |
| 2622 | p += extra; |
| 2623 | } |
| 2624 | |
| 2625 | pathopt = lpathopt; |
| 2626 | *path = *p == ':' ? p + 1 : NULL; |
| 2627 | |
| 2628 | /* "2" is for '/' and '\0' */ |
| 2629 | qlen = len + strlen(name) + 2; |
| 2630 | q = growstackto(qlen); |
| 2631 | |
| 2632 | if (len) { |
| 2633 | q = mempcpy(q, start, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2634 | *q++ = '/'; |
| 2635 | } |
| 2636 | strcpy(q, name); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2637 | |
| 2638 | return qlen; |
| 2639 | } |
| 2640 | |
| 2641 | static int |
| 2642 | padvance(const char **path, const char *name) |
| 2643 | { |
| 2644 | return padvance_magic(path, name, 1); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | |
| 2648 | /* ============ Prompt */ |
| 2649 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2650 | static smallint doprompt; /* if set, prompt the user */ |
| 2651 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2652 | |
| 2653 | #if ENABLE_FEATURE_EDITING |
| 2654 | static line_input_t *line_input_state; |
| 2655 | static const char *cmdedit_prompt; |
| 2656 | static void |
| 2657 | putprompt(const char *s) |
| 2658 | { |
| 2659 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2660 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2661 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
| 2664 | cmdedit_prompt = s; |
| 2665 | } |
| 2666 | #else |
| 2667 | static void |
| 2668 | putprompt(const char *s) |
| 2669 | { |
| 2670 | out2str(s); |
| 2671 | } |
| 2672 | #endif |
| 2673 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2674 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2675 | static const char *expandstr(const char *ps, int syntax_type); |
| 2676 | /* Values for syntax param */ |
| 2677 | #define BASESYNTAX 0 /* not in quotes */ |
| 2678 | #define DQSYNTAX 1 /* in double quotes */ |
| 2679 | #define SQSYNTAX 2 /* in single quotes */ |
| 2680 | #define ARISYNTAX 3 /* in arithmetic */ |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 2681 | #if ENABLE_ASH_EXPAND_PRMT |
| 2682 | # define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
| 2683 | #endif |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2684 | /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2685 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2686 | /* |
| 2687 | * called by editline -- any expansions to the prompt should be added here. |
| 2688 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2689 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2690 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2691 | { |
| 2692 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2693 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2694 | |
| 2695 | if (!do_set) |
| 2696 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2697 | |
| 2698 | needprompt = 0; |
| 2699 | |
| 2700 | switch (whichprompt) { |
| 2701 | case 1: |
| 2702 | prompt = ps1val(); |
| 2703 | break; |
| 2704 | case 2: |
| 2705 | prompt = ps2val(); |
| 2706 | break; |
| 2707 | default: /* 0 */ |
| 2708 | prompt = nullstr; |
| 2709 | } |
| 2710 | #if ENABLE_ASH_EXPAND_PRMT |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 2711 | pushstackmark(&smark, stackblocksize()); |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2712 | putprompt(expandstr(prompt, PSSYNTAX)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2713 | popstackmark(&smark); |
Denys Vlasenko | 48c803a | 2017-07-01 23:24:48 +0200 | [diff] [blame] | 2714 | #else |
| 2715 | putprompt(prompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2716 | #endif |
| 2717 | } |
| 2718 | |
| 2719 | |
| 2720 | /* ============ The cd and pwd commands */ |
| 2721 | |
| 2722 | #define CD_PHYSICAL 1 |
| 2723 | #define CD_PRINT 2 |
| 2724 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2725 | static int |
| 2726 | cdopt(void) |
| 2727 | { |
| 2728 | int flags = 0; |
| 2729 | int i, j; |
| 2730 | |
| 2731 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2732 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2733 | if (i != j) { |
| 2734 | flags ^= CD_PHYSICAL; |
| 2735 | j = i; |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | return flags; |
| 2740 | } |
| 2741 | |
| 2742 | /* |
| 2743 | * Update curdir (the name of the current directory) in response to a |
| 2744 | * cd command. |
| 2745 | */ |
| 2746 | static const char * |
| 2747 | updatepwd(const char *dir) |
| 2748 | { |
| 2749 | char *new; |
| 2750 | char *p; |
| 2751 | char *cdcomppath; |
| 2752 | const char *lim; |
| 2753 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 2754 | cdcomppath = sstrdup(dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2755 | STARTSTACKSTR(new); |
| 2756 | if (*dir != '/') { |
| 2757 | if (curdir == nullstr) |
| 2758 | return 0; |
| 2759 | new = stack_putstr(curdir, new); |
| 2760 | } |
| 2761 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2762 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2763 | if (*dir != '/') { |
| 2764 | if (new[-1] != '/') |
| 2765 | USTPUTC('/', new); |
| 2766 | if (new > lim && *lim == '/') |
| 2767 | lim++; |
| 2768 | } else { |
| 2769 | USTPUTC('/', new); |
| 2770 | cdcomppath++; |
| 2771 | if (dir[1] == '/' && dir[2] != '/') { |
| 2772 | USTPUTC('/', new); |
| 2773 | cdcomppath++; |
| 2774 | lim++; |
| 2775 | } |
| 2776 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2777 | p = strtok_r(cdcomppath, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2778 | while (p) { |
| 2779 | switch (*p) { |
| 2780 | case '.': |
| 2781 | if (p[1] == '.' && p[2] == '\0') { |
| 2782 | while (new > lim) { |
| 2783 | STUNPUTC(new); |
| 2784 | if (new[-1] == '/') |
| 2785 | break; |
| 2786 | } |
| 2787 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2788 | } |
| 2789 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | /* fall through */ |
| 2792 | default: |
| 2793 | new = stack_putstr(p, new); |
| 2794 | USTPUTC('/', new); |
| 2795 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2796 | p = strtok_r(NULL, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2797 | } |
| 2798 | if (new > lim) |
| 2799 | STUNPUTC(new); |
| 2800 | *new = 0; |
| 2801 | return stackblock(); |
| 2802 | } |
| 2803 | |
| 2804 | /* |
| 2805 | * Find out what the current directory is. If we already know the current |
| 2806 | * directory, this routine returns immediately. |
| 2807 | */ |
| 2808 | static char * |
| 2809 | getpwd(void) |
| 2810 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2811 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2812 | return dir ? dir : nullstr; |
| 2813 | } |
| 2814 | |
| 2815 | static void |
| 2816 | setpwd(const char *val, int setold) |
| 2817 | { |
| 2818 | char *oldcur, *dir; |
| 2819 | |
| 2820 | oldcur = dir = curdir; |
| 2821 | |
| 2822 | if (setold) { |
| 2823 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2824 | } |
| 2825 | INT_OFF; |
| 2826 | if (physdir != nullstr) { |
| 2827 | if (physdir != oldcur) |
| 2828 | free(physdir); |
| 2829 | physdir = nullstr; |
| 2830 | } |
| 2831 | if (oldcur == val || !val) { |
| 2832 | char *s = getpwd(); |
| 2833 | physdir = s; |
| 2834 | if (!val) |
| 2835 | dir = s; |
| 2836 | } else |
| 2837 | dir = ckstrdup(val); |
| 2838 | if (oldcur != dir && oldcur != nullstr) { |
| 2839 | free(oldcur); |
| 2840 | } |
| 2841 | curdir = dir; |
| 2842 | INT_ON; |
| 2843 | setvar("PWD", dir, VEXPORT); |
| 2844 | } |
| 2845 | |
| 2846 | static void hashcd(void); |
| 2847 | |
| 2848 | /* |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 2849 | * Actually do the chdir. We also call hashcd to let other routines |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2850 | * know that the current directory has changed. |
| 2851 | */ |
| 2852 | static int |
| 2853 | docd(const char *dest, int flags) |
| 2854 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2855 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2856 | int err; |
| 2857 | |
| 2858 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2859 | |
| 2860 | INT_OFF; |
| 2861 | if (!(flags & CD_PHYSICAL)) { |
| 2862 | dir = updatepwd(dest); |
| 2863 | if (dir) |
| 2864 | dest = dir; |
| 2865 | } |
| 2866 | err = chdir(dest); |
| 2867 | if (err) |
| 2868 | goto out; |
| 2869 | setpwd(dir, 1); |
| 2870 | hashcd(); |
| 2871 | out: |
| 2872 | INT_ON; |
| 2873 | return err; |
| 2874 | } |
| 2875 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2876 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2877 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2878 | { |
| 2879 | const char *dest; |
| 2880 | const char *path; |
| 2881 | const char *p; |
| 2882 | char c; |
| 2883 | struct stat statb; |
| 2884 | int flags; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2885 | int len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2886 | |
| 2887 | flags = cdopt(); |
| 2888 | dest = *argptr; |
| 2889 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2890 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2891 | else if (LONE_DASH(dest)) { |
| 2892 | dest = bltinlookup("OLDPWD"); |
| 2893 | flags |= CD_PRINT; |
| 2894 | } |
| 2895 | if (!dest) |
| 2896 | dest = nullstr; |
| 2897 | if (*dest == '/') |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2898 | goto step6; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2899 | if (*dest == '.') { |
| 2900 | c = dest[1]; |
| 2901 | dotdot: |
| 2902 | switch (c) { |
| 2903 | case '\0': |
| 2904 | case '/': |
| 2905 | goto step6; |
| 2906 | case '.': |
| 2907 | c = dest[2]; |
| 2908 | if (c != '.') |
| 2909 | goto dotdot; |
| 2910 | } |
| 2911 | } |
| 2912 | if (!*dest) |
| 2913 | dest = "."; |
| 2914 | path = bltinlookup("CDPATH"); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2915 | while (p = path, (len = padvance(&path, dest)) >= 0) { |
| 2916 | c = *p; |
| 2917 | p = stalloc(len); |
| 2918 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2919 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2920 | if (c && c != ':') |
| 2921 | flags |= CD_PRINT; |
| 2922 | docd: |
| 2923 | if (!docd(p, flags)) |
| 2924 | goto out; |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2925 | goto err; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2926 | } |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | step6: |
| 2930 | p = dest; |
| 2931 | goto docd; |
| 2932 | |
| 2933 | err: |
Johannes Schindelin | 687aac0 | 2017-08-22 22:03:22 +0200 | [diff] [blame] | 2934 | ash_msg_and_raise_perror("can't cd to %s", dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2935 | /* NOTREACHED */ |
| 2936 | out: |
| 2937 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2938 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2939 | return 0; |
| 2940 | } |
| 2941 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2942 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2943 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2944 | { |
| 2945 | int flags; |
| 2946 | const char *dir = curdir; |
| 2947 | |
| 2948 | flags = cdopt(); |
| 2949 | if (flags) { |
| 2950 | if (physdir == nullstr) |
| 2951 | setpwd(dir, 0); |
| 2952 | dir = physdir; |
| 2953 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2954 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2955 | return 0; |
| 2956 | } |
| 2957 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2958 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2959 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2960 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2961 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2962 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2963 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2964 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2965 | #define CWORD 0 /* character is nothing special */ |
| 2966 | #define CNL 1 /* newline character */ |
| 2967 | #define CBACK 2 /* a backslash character */ |
| 2968 | #define CSQUOTE 3 /* single quote */ |
| 2969 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2970 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2971 | #define CBQUOTE 6 /* backwards single quote */ |
| 2972 | #define CVAR 7 /* a dollar sign */ |
| 2973 | #define CENDVAR 8 /* a '}' character */ |
| 2974 | #define CLP 9 /* a left paren in arithmetic */ |
| 2975 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2976 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2977 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2978 | #define CSPCL 13 /* these terminate a word */ |
| 2979 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2980 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2981 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2982 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2983 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2984 | #endif |
| 2985 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2986 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2987 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2988 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2989 | # 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] | 2990 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2991 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2992 | #endif |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 2993 | static const uint16_t S_I_T[] ALIGN2 = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2994 | #if ENABLE_ASH_ALIAS |
| 2995 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2996 | #endif |
| 2997 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2998 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2999 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 3000 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 3001 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 3002 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 3003 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 3004 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 3005 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 3006 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 3007 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3008 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3009 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 3010 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 3011 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 3012 | #endif |
| 3013 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3014 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3015 | /* Constants below must match table above */ |
| 3016 | enum { |
| 3017 | #if ENABLE_ASH_ALIAS |
| 3018 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 3019 | #endif |
| 3020 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 3021 | CNL_CNL_CNL_CNL , /* 2 */ |
| 3022 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 3023 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 3024 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 3025 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 3026 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 3027 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 3028 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 3029 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 3030 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 3031 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 3032 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 3033 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 3034 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3035 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3036 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 3037 | * caller must ensure proper cast on it if c is *char_ptr! |
| 3038 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3039 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3040 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3041 | static int |
| 3042 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3043 | { |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3044 | /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */ |
| 3045 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~"; |
| 3046 | /* |
| 3047 | * This causes '/' to be prepended with CTLESC in dquoted string, |
| 3048 | * making "./file"* treated incorrectly because we feed |
| 3049 | * ".\/file*" string to glob(), confusing it (see expandmeta func). |
| 3050 | * The "homegrown" glob implementation is okay with that, |
| 3051 | * but glibc one isn't. With '/' always treated as CWORD, |
| 3052 | * both work fine. |
| 3053 | */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3054 | # if ENABLE_ASH_ALIAS |
| 3055 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3056 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3057 | 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3058 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 3059 | 11, 3 /* "}~" */ |
| 3060 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3061 | # else |
| 3062 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3063 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3064 | 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3065 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 3066 | 10, 2 /* "}~" */ |
| 3067 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3068 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3069 | const char *s; |
| 3070 | int indx; |
| 3071 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3072 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3073 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3074 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3075 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3076 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3077 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3078 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3079 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3080 | /* Cast is purely for paranoia here, |
| 3081 | * just in case someone passed signed char to us */ |
| 3082 | if ((unsigned char)c >= CTL_FIRST |
| 3083 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3084 | ) { |
| 3085 | return CCTL; |
| 3086 | } |
| 3087 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3088 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3089 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3090 | indx = syntax_index_table[s - spec_symbls]; |
| 3091 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3092 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3095 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3096 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 3097 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3098 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3099 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 3100 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 3101 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 3102 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 3103 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 3104 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 3105 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 3106 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 3107 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 3108 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 3109 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 3110 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 3111 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 3112 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 3113 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 3114 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 3115 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 3116 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 3117 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 3118 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 3119 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 3120 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 3121 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 3122 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 3123 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 3124 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 3125 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 3126 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 3127 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 3128 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 3129 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 3130 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 3131 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 3132 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 3133 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 3134 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 3135 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 3136 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 3137 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 3138 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 3139 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 3140 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 3141 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 3142 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 3143 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 3144 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 3145 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3146 | /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */ |
| 3147 | /* 47 "/" */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3148 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 3149 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 3150 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 3151 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 3152 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 3153 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 3154 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 3155 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 3156 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 3157 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 3158 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 3159 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 3160 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 3161 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 3162 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 3163 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 3164 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 3165 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 3166 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 3167 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 3168 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 3169 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 3170 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 3171 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 3172 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 3173 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 3174 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 3175 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 3176 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 3177 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 3178 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 3179 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 3180 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 3181 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 3182 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 3183 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 3184 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 3185 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 3186 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 3187 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 3188 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 3189 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 3190 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 3191 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 3192 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 3193 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 3194 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 3195 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 3196 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 3197 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 3198 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 3199 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 3200 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 3201 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 3202 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 3203 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 3204 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 3205 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 3206 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 3207 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 3208 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 3209 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 3210 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 3211 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 3212 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 3213 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 3214 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 3215 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 3216 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 3217 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 3218 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 3219 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 3220 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 3221 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 3222 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 3223 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 3224 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 3225 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 3226 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 3227 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 3228 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 3229 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 3230 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3231 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3232 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 3233 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 3234 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 3235 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 3236 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 3237 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 3238 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 3239 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 3240 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 3241 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 3242 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 3243 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 3244 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 3245 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 3246 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 3247 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 3248 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 3249 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 3250 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 3251 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 3252 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 3253 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 3254 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 3255 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 3256 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 3257 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 3258 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 3259 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 3260 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 3261 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3262 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3263 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3264 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3265 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3266 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3267 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3268 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3269 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3270 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3271 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3272 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3273 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3274 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3275 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3276 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3277 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3278 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3279 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3280 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3281 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3282 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3283 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3284 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3285 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3286 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3287 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3288 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3289 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3290 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3291 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3292 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3293 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3294 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3295 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3296 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3297 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3298 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3299 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3300 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3301 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3302 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3303 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3304 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3305 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3306 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3307 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3308 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3309 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3310 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3311 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3312 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3313 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3314 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3315 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3316 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3317 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3318 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3319 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3320 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3321 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3322 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3323 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3324 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3325 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3326 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3327 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3328 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3329 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3330 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3331 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3332 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3333 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3334 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3335 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3336 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3337 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3338 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3339 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3340 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3341 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3342 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3343 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3344 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3345 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3346 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3347 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3348 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3349 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3350 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3351 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3352 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3353 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3354 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3355 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3356 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3357 | # if ENABLE_ASH_ALIAS |
| 3358 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 3359 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3360 | }; |
| 3361 | |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3362 | #if 1 |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3363 | # 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] | 3364 | #else /* debug version, caught one signed char bug */ |
| 3365 | # define SIT(c, syntax) \ |
| 3366 | ({ \ |
| 3367 | if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \ |
| 3368 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 3369 | if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \ |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3370 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
| 3371 | ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \ |
| 3372 | }) |
| 3373 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3374 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3375 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3376 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3377 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3378 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3379 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3380 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3381 | |
| 3382 | #define ALIASINUSE 1 |
| 3383 | #define ALIASDEAD 2 |
| 3384 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3385 | struct alias { |
| 3386 | struct alias *next; |
| 3387 | char *name; |
| 3388 | char *val; |
| 3389 | int flag; |
| 3390 | }; |
| 3391 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3392 | |
| 3393 | static struct alias **atab; // [ATABSIZE]; |
| 3394 | #define INIT_G_alias() do { \ |
| 3395 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3396 | } while (0) |
| 3397 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3398 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3399 | static struct alias ** |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 3400 | __lookupalias(const char *name) |
| 3401 | { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3402 | unsigned int hashval; |
| 3403 | struct alias **app; |
| 3404 | const char *p; |
| 3405 | unsigned int ch; |
| 3406 | |
| 3407 | p = name; |
| 3408 | |
| 3409 | ch = (unsigned char)*p; |
| 3410 | hashval = ch << 4; |
| 3411 | while (ch) { |
| 3412 | hashval += ch; |
| 3413 | ch = (unsigned char)*++p; |
| 3414 | } |
| 3415 | app = &atab[hashval % ATABSIZE]; |
| 3416 | |
| 3417 | for (; *app; app = &(*app)->next) { |
| 3418 | if (strcmp(name, (*app)->name) == 0) { |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | return app; |
| 3424 | } |
| 3425 | |
| 3426 | static struct alias * |
| 3427 | lookupalias(const char *name, int check) |
| 3428 | { |
| 3429 | struct alias *ap = *__lookupalias(name); |
| 3430 | |
| 3431 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3432 | return NULL; |
| 3433 | return ap; |
| 3434 | } |
| 3435 | |
| 3436 | static struct alias * |
| 3437 | freealias(struct alias *ap) |
| 3438 | { |
| 3439 | struct alias *next; |
| 3440 | |
| 3441 | if (ap->flag & ALIASINUSE) { |
| 3442 | ap->flag |= ALIASDEAD; |
| 3443 | return ap; |
| 3444 | } |
| 3445 | |
| 3446 | next = ap->next; |
| 3447 | free(ap->name); |
| 3448 | free(ap->val); |
| 3449 | free(ap); |
| 3450 | return next; |
| 3451 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3452 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3453 | static void |
| 3454 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3455 | { |
| 3456 | struct alias *ap, **app; |
| 3457 | |
| 3458 | app = __lookupalias(name); |
| 3459 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3460 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3461 | if (ap) { |
| 3462 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3463 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3464 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3465 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3466 | ap->flag &= ~ALIASDEAD; |
| 3467 | } else { |
| 3468 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3469 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3470 | ap->name = ckstrdup(name); |
| 3471 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3472 | /*ap->flag = 0; - ckzalloc did it */ |
| 3473 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3474 | *app = ap; |
| 3475 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3476 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3479 | static int |
| 3480 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3481 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3482 | struct alias **app; |
| 3483 | |
| 3484 | app = __lookupalias(name); |
| 3485 | |
| 3486 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3487 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3488 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3489 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3490 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3493 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3496 | static void |
| 3497 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3498 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3499 | struct alias *ap, **app; |
| 3500 | int i; |
| 3501 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3502 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3503 | for (i = 0; i < ATABSIZE; i++) { |
| 3504 | app = &atab[i]; |
| 3505 | for (ap = *app; ap; ap = *app) { |
| 3506 | *app = freealias(*app); |
| 3507 | if (ap == *app) { |
| 3508 | app = &ap->next; |
| 3509 | } |
| 3510 | } |
| 3511 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3512 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3515 | static void |
| 3516 | printalias(const struct alias *ap) |
| 3517 | { |
| 3518 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3519 | } |
| 3520 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3521 | /* |
| 3522 | * TODO - sort output |
| 3523 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3524 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3525 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3526 | { |
| 3527 | char *n, *v; |
| 3528 | int ret = 0; |
| 3529 | struct alias *ap; |
| 3530 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3531 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3532 | int i; |
| 3533 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3534 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3535 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3536 | printalias(ap); |
| 3537 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3538 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3539 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3540 | } |
| 3541 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3542 | v = strchr(n+1, '='); |
| 3543 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3544 | ap = *__lookupalias(n); |
| 3545 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3546 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3547 | ret = 1; |
| 3548 | } else |
| 3549 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3550 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3551 | *v++ = '\0'; |
| 3552 | setalias(n, v); |
| 3553 | } |
| 3554 | } |
| 3555 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3556 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3559 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3560 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3561 | { |
| 3562 | int i; |
| 3563 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 3564 | while (nextopt("a") != '\0') { |
| 3565 | rmaliases(); |
| 3566 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3567 | } |
| 3568 | for (i = 0; *argptr; argptr++) { |
| 3569 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3570 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3571 | i = 1; |
| 3572 | } |
| 3573 | } |
| 3574 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3575 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3576 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3577 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3578 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3579 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3580 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3581 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3582 | #define FORK_FG 0 |
| 3583 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3584 | #define FORK_NOJOB 2 |
| 3585 | |
| 3586 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3587 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3588 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3589 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3590 | #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3591 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3592 | /* |
| 3593 | * A job structure contains information about a job. A job is either a |
| 3594 | * single process or a set of processes contained in a pipeline. In the |
| 3595 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3596 | * array of pids. |
| 3597 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3598 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3599 | pid_t ps_pid; /* process id */ |
| 3600 | int ps_status; /* last process status from wait() */ |
| 3601 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3602 | }; |
| 3603 | |
| 3604 | struct job { |
| 3605 | struct procstat ps0; /* status of process */ |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 3606 | struct procstat *ps; /* status of processes when more than one */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3607 | #if JOBS |
| 3608 | int stopstatus; /* status of a stopped job */ |
| 3609 | #endif |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3610 | unsigned nprocs; /* number of processes */ |
| 3611 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3612 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3613 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3614 | #define JOBDONE 2 /* all procs are completed */ |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3615 | unsigned |
| 3616 | state: 8, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3617 | #if JOBS |
| 3618 | sigint: 1, /* job was killed by SIGINT */ |
| 3619 | jobctl: 1, /* job running under job control */ |
| 3620 | #endif |
| 3621 | waited: 1, /* true if this entry has been waited for */ |
| 3622 | used: 1, /* true if this entry is in used */ |
| 3623 | changed: 1; /* true if status has changed */ |
| 3624 | struct job *prev_job; /* previous job */ |
| 3625 | }; |
| 3626 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3627 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3628 | static int forkshell(struct job *, union node *, int); |
| 3629 | static int waitforjob(struct job *); |
| 3630 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3631 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3632 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3633 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3634 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3635 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3636 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3637 | #endif |
| 3638 | |
| 3639 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3640 | * Ignore a signal. |
| 3641 | */ |
| 3642 | static void |
| 3643 | ignoresig(int signo) |
| 3644 | { |
| 3645 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3646 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3647 | /* No, need to do it */ |
| 3648 | signal(signo, SIG_IGN); |
| 3649 | } |
| 3650 | sigmode[signo - 1] = S_HARD_IGN; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3654 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3655 | */ |
| 3656 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3657 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3658 | { |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3659 | if (signo == SIGCHLD) { |
| 3660 | got_sigchld = 1; |
| 3661 | if (!trap[SIGCHLD]) |
| 3662 | return; |
| 3663 | } |
| 3664 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3665 | gotsig[signo - 1] = 1; |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3666 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3667 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3668 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3669 | if (!suppress_int) { |
| 3670 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3671 | raise_interrupt(); /* does not return */ |
| 3672 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3673 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3678 | * Set the signal handler for the specified signal. The routine figures |
| 3679 | * out what it should be set to. |
| 3680 | */ |
| 3681 | static void |
| 3682 | setsignal(int signo) |
| 3683 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3684 | char *t; |
| 3685 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3686 | struct sigaction act; |
| 3687 | |
| 3688 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3689 | new_act = S_DFL; |
| 3690 | if (t != NULL) { /* trap for this sig is set */ |
| 3691 | new_act = S_CATCH; |
| 3692 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3693 | new_act = S_IGN; |
| 3694 | } |
| 3695 | |
| 3696 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3697 | switch (signo) { |
| 3698 | case SIGINT: |
| 3699 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3700 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | case SIGQUIT: |
| 3703 | #if DEBUG |
| 3704 | if (debug) |
| 3705 | break; |
| 3706 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3707 | /* man bash: |
| 3708 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3709 | * commands run by bash have signal handlers |
| 3710 | * set to the values inherited by the shell |
| 3711 | * from its parent". */ |
| 3712 | new_act = S_IGN; |
| 3713 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3714 | case SIGTERM: |
| 3715 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3716 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3717 | break; |
| 3718 | #if JOBS |
| 3719 | case SIGTSTP: |
| 3720 | case SIGTTOU: |
| 3721 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3722 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3723 | break; |
| 3724 | #endif |
| 3725 | } |
| 3726 | } |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3727 | /* if !rootshell, we reset SIGQUIT to DFL, |
| 3728 | * whereas we have to restore it to what shell got on entry. |
| 3729 | * This is handled by the fact that if signal was IGNored on entry, |
| 3730 | * then cur_act is S_HARD_IGN and we never change its sigaction |
| 3731 | * (see code below). |
| 3732 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3733 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3734 | if (signo == SIGCHLD) |
| 3735 | new_act = S_CATCH; |
| 3736 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3737 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3738 | cur_act = *t; |
| 3739 | if (cur_act == 0) { |
| 3740 | /* current setting is not yet known */ |
| 3741 | if (sigaction(signo, NULL, &act)) { |
| 3742 | /* pretend it worked; maybe we should give a warning, |
| 3743 | * but other shells don't. We don't alter sigmode, |
| 3744 | * so we retry every time. |
| 3745 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3746 | return; |
| 3747 | } |
| 3748 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3749 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3750 | if (mflag |
| 3751 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3752 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3753 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3754 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3755 | } |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 3756 | if (act.sa_handler == SIG_DFL && new_act == S_DFL) { |
| 3757 | /* installing SIG_DFL over SIG_DFL is a no-op */ |
| 3758 | /* saves one sigaction call in each "sh -c SCRIPT" invocation */ |
| 3759 | *t = S_DFL; |
| 3760 | return; |
| 3761 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3762 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3763 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3764 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3765 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3766 | *t = new_act; |
| 3767 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3768 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3769 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3770 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3771 | act.sa_handler = signal_handler; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3772 | break; |
| 3773 | case S_IGN: |
| 3774 | act.sa_handler = SIG_IGN; |
| 3775 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3776 | } |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3777 | /* flags and mask matter only if !DFL and !IGN, but we do it |
| 3778 | * for all cases for more deterministic behavior: |
| 3779 | */ |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3780 | act.sa_flags = 0; //TODO: why not SA_RESTART? |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3781 | sigfillset(&act.sa_mask); |
| 3782 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3783 | sigaction_set(signo, &act); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | /* mode flags for set_curjob */ |
| 3787 | #define CUR_DELETE 2 |
| 3788 | #define CUR_RUNNING 1 |
| 3789 | #define CUR_STOPPED 0 |
| 3790 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3791 | #if JOBS |
| 3792 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3793 | static int initialpgrp; //references:2 |
| 3794 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3795 | #endif |
| 3796 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3797 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3798 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3799 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3800 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3801 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3802 | |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 3803 | #if 0 |
| 3804 | /* Bash has a feature: it restores termios after a successful wait for |
| 3805 | * a foreground job which had at least one stopped or sigkilled member. |
| 3806 | * The probable rationale is that SIGSTOP and SIGKILL can preclude task from |
| 3807 | * properly restoring tty state. Should we do this too? |
| 3808 | * A reproducer: ^Z an interactive python: |
| 3809 | * |
| 3810 | * # python |
| 3811 | * Python 2.7.12 (...) |
| 3812 | * >>> ^Z |
| 3813 | * { python leaves tty in -icanon -echo state. We do survive that... } |
| 3814 | * [1]+ Stopped python |
| 3815 | * { ...however, next program (python #2) does not survive it well: } |
| 3816 | * # python |
| 3817 | * Python 2.7.12 (...) |
| 3818 | * >>> Traceback (most recent call last): |
| 3819 | * { above, I typed "qwerty<CR>", but -echo state is still in effect } |
| 3820 | * File "<stdin>", line 1, in <module> |
| 3821 | * NameError: name 'qwerty' is not defined |
| 3822 | * |
| 3823 | * The implementation below is modeled on bash code and seems to work. |
| 3824 | * However, I'm not sure we should do this. For one: what if I'd fg |
| 3825 | * the stopped python instead? It'll be confused by "restored" tty state. |
| 3826 | */ |
| 3827 | static struct termios shell_tty_info; |
| 3828 | static void |
| 3829 | get_tty_state(void) |
| 3830 | { |
| 3831 | if (rootshell && ttyfd >= 0) |
| 3832 | tcgetattr(ttyfd, &shell_tty_info); |
| 3833 | } |
| 3834 | static void |
| 3835 | set_tty_state(void) |
| 3836 | { |
| 3837 | /* if (rootshell) - caller ensures this */ |
| 3838 | if (ttyfd >= 0) |
| 3839 | tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info); |
| 3840 | } |
| 3841 | static int |
| 3842 | job_signal_status(struct job *jp) |
| 3843 | { |
| 3844 | int status; |
| 3845 | unsigned i; |
| 3846 | struct procstat *ps = jp->ps; |
| 3847 | for (i = 0; i < jp->nprocs; i++) { |
| 3848 | status = ps[i].ps_status; |
| 3849 | if (WIFSIGNALED(status) || WIFSTOPPED(status)) |
| 3850 | return status; |
| 3851 | } |
| 3852 | return 0; |
| 3853 | } |
| 3854 | static void |
| 3855 | restore_tty_if_stopped_or_signaled(struct job *jp) |
| 3856 | { |
| 3857 | //TODO: check what happens if we come from waitforjob() in expbackq() |
| 3858 | if (rootshell) { |
| 3859 | int s = job_signal_status(jp); |
| 3860 | if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */ |
| 3861 | set_tty_state(); |
| 3862 | } |
| 3863 | } |
| 3864 | #else |
| 3865 | # define get_tty_state() ((void)0) |
| 3866 | # define restore_tty_if_stopped_or_signaled(jp) ((void)0) |
| 3867 | #endif |
| 3868 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3869 | static void |
| 3870 | set_curjob(struct job *jp, unsigned mode) |
| 3871 | { |
| 3872 | struct job *jp1; |
| 3873 | struct job **jpp, **curp; |
| 3874 | |
| 3875 | /* first remove from list */ |
| 3876 | jpp = curp = &curjob; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3877 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3878 | jp1 = *jpp; |
| 3879 | if (jp1 == jp) |
| 3880 | break; |
| 3881 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3882 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3883 | *jpp = jp1->prev_job; |
| 3884 | |
| 3885 | /* Then re-insert in correct position */ |
| 3886 | jpp = curp; |
| 3887 | switch (mode) { |
| 3888 | default: |
| 3889 | #if DEBUG |
| 3890 | abort(); |
| 3891 | #endif |
| 3892 | case CUR_DELETE: |
| 3893 | /* job being deleted */ |
| 3894 | break; |
| 3895 | case CUR_RUNNING: |
| 3896 | /* newly created job or backgrounded job, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 3897 | * put after all stopped jobs. |
| 3898 | */ |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3899 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3900 | jp1 = *jpp; |
| 3901 | #if JOBS |
| 3902 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3903 | #endif |
| 3904 | break; |
| 3905 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3906 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3907 | /* FALLTHROUGH */ |
| 3908 | #if JOBS |
| 3909 | case CUR_STOPPED: |
| 3910 | #endif |
| 3911 | /* newly stopped job - becomes curjob */ |
| 3912 | jp->prev_job = *jpp; |
| 3913 | *jpp = jp; |
| 3914 | break; |
| 3915 | } |
| 3916 | } |
| 3917 | |
| 3918 | #if JOBS || DEBUG |
| 3919 | static int |
| 3920 | jobno(const struct job *jp) |
| 3921 | { |
| 3922 | return jp - jobtab + 1; |
| 3923 | } |
| 3924 | #endif |
| 3925 | |
| 3926 | /* |
| 3927 | * Convert a job name to a job structure. |
| 3928 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3929 | #if !JOBS |
| 3930 | #define getjob(name, getctl) getjob(name) |
| 3931 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3932 | static struct job * |
| 3933 | getjob(const char *name, int getctl) |
| 3934 | { |
| 3935 | struct job *jp; |
| 3936 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3937 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3938 | unsigned num; |
| 3939 | int c; |
| 3940 | const char *p; |
| 3941 | char *(*match)(const char *, const char *); |
| 3942 | |
| 3943 | jp = curjob; |
| 3944 | p = name; |
| 3945 | if (!p) |
| 3946 | goto currentjob; |
| 3947 | |
| 3948 | if (*p != '%') |
| 3949 | goto err; |
| 3950 | |
| 3951 | c = *++p; |
| 3952 | if (!c) |
| 3953 | goto currentjob; |
| 3954 | |
| 3955 | if (!p[1]) { |
| 3956 | if (c == '+' || c == '%') { |
| 3957 | currentjob: |
| 3958 | err_msg = "No current job"; |
| 3959 | goto check; |
| 3960 | } |
| 3961 | if (c == '-') { |
| 3962 | if (jp) |
| 3963 | jp = jp->prev_job; |
| 3964 | err_msg = "No previous job"; |
| 3965 | check: |
| 3966 | if (!jp) |
| 3967 | goto err; |
| 3968 | goto gotit; |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | if (is_number(p)) { |
| 3973 | num = atoi(p); |
Denys Vlasenko | 46a45ce | 2016-09-29 01:10:08 +0200 | [diff] [blame] | 3974 | if (num > 0 && num <= njobs) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3975 | jp = jobtab + num - 1; |
| 3976 | if (jp->used) |
| 3977 | goto gotit; |
| 3978 | goto err; |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | match = prefix; |
| 3983 | if (*p == '?') { |
| 3984 | match = strstr; |
| 3985 | p++; |
| 3986 | } |
| 3987 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3988 | found = NULL; |
| 3989 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3990 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3991 | if (found) |
| 3992 | goto err; |
| 3993 | found = jp; |
| 3994 | err_msg = "%s: ambiguous"; |
| 3995 | } |
| 3996 | jp = jp->prev_job; |
| 3997 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3998 | if (!found) |
| 3999 | goto err; |
| 4000 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4001 | |
| 4002 | gotit: |
| 4003 | #if JOBS |
| 4004 | err_msg = "job %s not created under job control"; |
| 4005 | if (getctl && jp->jobctl == 0) |
| 4006 | goto err; |
| 4007 | #endif |
| 4008 | return jp; |
| 4009 | err: |
| 4010 | ash_msg_and_raise_error(err_msg, name); |
| 4011 | } |
| 4012 | |
| 4013 | /* |
| 4014 | * Mark a job structure as unused. |
| 4015 | */ |
| 4016 | static void |
| 4017 | freejob(struct job *jp) |
| 4018 | { |
| 4019 | struct procstat *ps; |
| 4020 | int i; |
| 4021 | |
| 4022 | INT_OFF; |
| 4023 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4024 | if (ps->ps_cmd != nullstr) |
| 4025 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4026 | } |
| 4027 | if (jp->ps != &jp->ps0) |
| 4028 | free(jp->ps); |
| 4029 | jp->used = 0; |
| 4030 | set_curjob(jp, CUR_DELETE); |
| 4031 | INT_ON; |
| 4032 | } |
| 4033 | |
| 4034 | #if JOBS |
| 4035 | static void |
| 4036 | xtcsetpgrp(int fd, pid_t pgrp) |
| 4037 | { |
| 4038 | if (tcsetpgrp(fd, pgrp)) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 4039 | ash_msg_and_raise_perror("can't set tty process group"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | /* |
| 4043 | * Turn job control on and off. |
| 4044 | * |
| 4045 | * Note: This code assumes that the third arg to ioctl is a character |
| 4046 | * pointer, which is true on Berkeley systems but not System V. Since |
| 4047 | * System V doesn't have job control yet, this isn't a problem now. |
| 4048 | * |
| 4049 | * Called with interrupts off. |
| 4050 | */ |
| 4051 | static void |
| 4052 | setjobctl(int on) |
| 4053 | { |
| 4054 | int fd; |
| 4055 | int pgrp; |
| 4056 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4057 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4058 | return; |
| 4059 | if (on) { |
| 4060 | int ofd; |
| 4061 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 4062 | if (fd < 0) { |
| 4063 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 4064 | * That sometimes helps to acquire controlling tty. |
| 4065 | * Obviously, a workaround for bugs when someone |
| 4066 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4067 | fd = 2; |
| 4068 | while (!isatty(fd)) |
| 4069 | if (--fd < 0) |
| 4070 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4071 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4072 | /* fd is a tty at this point */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4073 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 4074 | 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] | 4075 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4076 | if (fd < 0) |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4077 | goto out; /* F_DUPFD failed */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4078 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 4079 | close_on_exec_on(fd); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4080 | while (1) { /* while we are in the background */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4081 | pgrp = tcgetpgrp(fd); |
| 4082 | if (pgrp < 0) { |
| 4083 | out: |
| 4084 | ash_msg("can't access tty; job control turned off"); |
| 4085 | mflag = on = 0; |
| 4086 | goto close; |
| 4087 | } |
| 4088 | if (pgrp == getpgrp()) |
| 4089 | break; |
| 4090 | killpg(0, SIGTTIN); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4091 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4092 | initialpgrp = pgrp; |
| 4093 | |
| 4094 | setsignal(SIGTSTP); |
| 4095 | setsignal(SIGTTOU); |
| 4096 | setsignal(SIGTTIN); |
| 4097 | pgrp = rootpid; |
| 4098 | setpgid(0, pgrp); |
| 4099 | xtcsetpgrp(fd, pgrp); |
| 4100 | } else { |
| 4101 | /* turning job control off */ |
| 4102 | fd = ttyfd; |
| 4103 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4104 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4105 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4106 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4107 | setpgid(0, pgrp); |
| 4108 | setsignal(SIGTSTP); |
| 4109 | setsignal(SIGTTOU); |
| 4110 | setsignal(SIGTTIN); |
| 4111 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4112 | if (fd >= 0) |
| 4113 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4114 | fd = -1; |
| 4115 | } |
| 4116 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4117 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4120 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4121 | killcmd(int argc, char **argv) |
| 4122 | { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4123 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4124 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4125 | do { |
| 4126 | if (argv[i][0] == '%') { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4127 | /* |
| 4128 | * "kill %N" - job kill |
| 4129 | * Converting to pgrp / pid kill |
| 4130 | */ |
| 4131 | struct job *jp; |
| 4132 | char *dst; |
| 4133 | int j, n; |
| 4134 | |
| 4135 | jp = getjob(argv[i], 0); |
| 4136 | /* |
| 4137 | * In jobs started under job control, we signal |
| 4138 | * entire process group by kill -PGRP_ID. |
| 4139 | * This happens, f.e., in interactive shell. |
| 4140 | * |
| 4141 | * Otherwise, we signal each child via |
| 4142 | * kill PID1 PID2 PID3. |
| 4143 | * Testcases: |
| 4144 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 4145 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 4146 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 4147 | */ |
| 4148 | n = jp->nprocs; /* can't be 0 (I hope) */ |
| 4149 | if (jp->jobctl) |
| 4150 | n = 1; |
| 4151 | dst = alloca(n * sizeof(int)*4); |
| 4152 | argv[i] = dst; |
| 4153 | for (j = 0; j < n; j++) { |
| 4154 | struct procstat *ps = &jp->ps[j]; |
| 4155 | /* Skip non-running and not-stopped members |
| 4156 | * (i.e. dead members) of the job |
| 4157 | */ |
| 4158 | if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status)) |
| 4159 | continue; |
| 4160 | /* |
| 4161 | * kill_main has matching code to expect |
| 4162 | * leading space. Needed to not confuse |
| 4163 | * negative pids with "kill -SIGNAL_NO" syntax |
| 4164 | */ |
| 4165 | dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid); |
| 4166 | } |
| 4167 | *dst = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4168 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4169 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4170 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4171 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4175 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4176 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4177 | struct procstat *ps; |
| 4178 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4179 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4180 | psend = jp->ps + jp->nprocs; |
| 4181 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 4182 | printf(" | %s", ps->ps_cmd); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4183 | newline_and_flush(stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4184 | flush_stdout_stderr(); |
| 4185 | } |
| 4186 | |
| 4187 | |
| 4188 | static int |
| 4189 | restartjob(struct job *jp, int mode) |
| 4190 | { |
| 4191 | struct procstat *ps; |
| 4192 | int i; |
| 4193 | int status; |
| 4194 | pid_t pgid; |
| 4195 | |
| 4196 | INT_OFF; |
| 4197 | if (jp->state == JOBDONE) |
| 4198 | goto out; |
| 4199 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4200 | pgid = jp->ps[0].ps_pid; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4201 | if (mode == FORK_FG) { |
| 4202 | get_tty_state(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4203 | xtcsetpgrp(ttyfd, pgid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4204 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4205 | killpg(pgid, SIGCONT); |
| 4206 | ps = jp->ps; |
| 4207 | i = jp->nprocs; |
| 4208 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4209 | if (WIFSTOPPED(ps->ps_status)) { |
| 4210 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4211 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4212 | ps++; |
| 4213 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4214 | out: |
| 4215 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 4216 | INT_ON; |
| 4217 | return status; |
| 4218 | } |
| 4219 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4220 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4221 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4222 | { |
| 4223 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4224 | int mode; |
| 4225 | int retval; |
| 4226 | |
| 4227 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 4228 | nextopt(nullstr); |
| 4229 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4230 | do { |
| 4231 | jp = getjob(*argv, 1); |
| 4232 | if (mode == FORK_BG) { |
| 4233 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4234 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4235 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4236 | out1str(jp->ps[0].ps_cmd); |
| 4237 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4238 | retval = restartjob(jp, mode); |
| 4239 | } while (*argv && *++argv); |
| 4240 | return retval; |
| 4241 | } |
| 4242 | #endif |
| 4243 | |
| 4244 | static int |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4245 | sprint_status48(char *os, int status, int sigonly) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4246 | { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4247 | char *s = os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4248 | int st; |
| 4249 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4250 | if (!WIFEXITED(status)) { |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4251 | #if JOBS |
| 4252 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4253 | st = WSTOPSIG(status); |
| 4254 | else |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4255 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4256 | st = WTERMSIG(status); |
| 4257 | if (sigonly) { |
| 4258 | if (st == SIGINT || st == SIGPIPE) |
| 4259 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4260 | #if JOBS |
| 4261 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4262 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4263 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4264 | } |
| 4265 | st &= 0x7f; |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 4266 | //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] | 4267 | //s = stpncpy(s, strsignal(st), 32); //not all libc have stpncpy() |
| 4268 | s += fmtstr(s, 32, strsignal(st)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4269 | if (WCOREDUMP(status)) { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4270 | s = stpcpy(s, " (core dumped)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4271 | } |
| 4272 | } else if (!sigonly) { |
| 4273 | st = WEXITSTATUS(status); |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4274 | s += fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4275 | } |
| 4276 | out: |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4277 | return s - os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4280 | #define DOWAIT_NONBLOCK 0 |
| 4281 | #define DOWAIT_BLOCK 1 |
| 4282 | #define DOWAIT_BLOCK_OR_SIG 2 |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4283 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4284 | # define DOWAIT_JOBSTATUS 0x10 /* OR this to get job's exitstatus instead of pid */ |
| 4285 | #endif |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4286 | |
| 4287 | static int |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4288 | waitproc(int block, int *status) |
| 4289 | { |
| 4290 | sigset_t oldmask; |
| 4291 | int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG; |
| 4292 | int err; |
| 4293 | |
| 4294 | #if JOBS |
| 4295 | if (doing_jobctl) |
| 4296 | flags |= WUNTRACED; |
| 4297 | #endif |
| 4298 | |
| 4299 | do { |
| 4300 | got_sigchld = 0; |
| 4301 | do |
| 4302 | err = waitpid(-1, status, flags); |
| 4303 | while (err < 0 && errno == EINTR); |
| 4304 | |
| 4305 | if (err || (err = -!block)) |
| 4306 | break; |
| 4307 | |
| 4308 | sigfillset(&oldmask); |
| 4309 | sigprocmask2(SIG_SETMASK, &oldmask); /* mask is updated */ |
| 4310 | while (!got_sigchld && !pending_sig) |
| 4311 | sigsuspend(&oldmask); |
| 4312 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
| 4313 | //simpler, but unsafe: a signal can set pending_sig after check, but before pause(): |
| 4314 | //while (!got_sigchld && !pending_sig) |
| 4315 | // pause(); |
| 4316 | |
| 4317 | } while (got_sigchld); |
| 4318 | |
| 4319 | return err; |
| 4320 | } |
| 4321 | |
| 4322 | static int |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4323 | waitone(int block, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4324 | { |
| 4325 | int pid; |
| 4326 | int status; |
| 4327 | struct job *jp; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4328 | struct job *thisjob = NULL; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4329 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4330 | bool want_jobexitstatus = (block & DOWAIT_JOBSTATUS); |
| 4331 | block = (block & ~DOWAIT_JOBSTATUS); |
| 4332 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4333 | |
Denys Vlasenko | b543bda | 2016-10-27 20:08:28 +0200 | [diff] [blame] | 4334 | TRACE(("dowait(0x%x) called\n", block)); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 4335 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4336 | /* It's wrong to call waitpid() outside of INT_OFF region: |
| 4337 | * signal can arrive just after syscall return and handler can |
| 4338 | * longjmp away, losing stop/exit notification processing. |
| 4339 | * Thus, for "jobs" builtin, and for waiting for a fg job, |
| 4340 | * we call waitpid() (blocking or non-blocking) inside INT_OFF. |
| 4341 | * |
| 4342 | * However, for "wait" builtin it is wrong to simply call waitpid() |
| 4343 | * in INT_OFF region: "wait" needs to wait for any running job |
| 4344 | * to change state, but should exit on any trap too. |
| 4345 | * In INT_OFF region, a signal just before syscall entry can set |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4346 | * pending_sig variables, but we can't check them, and we would |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4347 | * either enter a sleeping waitpid() (BUG), or need to busy-loop. |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4348 | * |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4349 | * 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] | 4350 | * which combines waitpid() and sigsuspend(). |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4351 | * 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] | 4352 | * SIG_DFL handler does not wake sigsuspend(). |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4353 | */ |
| 4354 | INT_OFF; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4355 | pid = waitproc(block, &status); |
| 4356 | TRACE(("wait returns pid %d, status=%d\n", pid, status)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4357 | if (pid <= 0) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4358 | goto out; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4359 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4360 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4361 | int jobstate; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4362 | struct procstat *ps; |
| 4363 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4364 | if (jp->state == JOBDONE) |
| 4365 | continue; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4366 | jobstate = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4367 | ps = jp->ps; |
| 4368 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4369 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4370 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4371 | TRACE(("Job %d: changing status of proc %d " |
| 4372 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4373 | jobno(jp), pid, ps->ps_status, status)); |
| 4374 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4375 | thisjob = jp; |
| 4376 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4377 | if (ps->ps_status == -1) |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4378 | jobstate = JOBRUNNING; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4379 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4380 | if (jobstate == JOBRUNNING) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4381 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4382 | if (WIFSTOPPED(ps->ps_status)) { |
| 4383 | jp->stopstatus = ps->ps_status; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4384 | jobstate = JOBSTOPPED; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4385 | } |
| 4386 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4387 | } while (++ps < psend); |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4388 | if (!thisjob) |
| 4389 | continue; |
| 4390 | |
| 4391 | /* Found the job where one of its processes changed its state. |
| 4392 | * Is there at least one live and running process in this job? */ |
| 4393 | if (jobstate != JOBRUNNING) { |
| 4394 | /* No. All live processes in the job are stopped |
| 4395 | * (JOBSTOPPED) or there are no live processes (JOBDONE) |
| 4396 | */ |
| 4397 | thisjob->changed = 1; |
| 4398 | if (thisjob->state != jobstate) { |
| 4399 | TRACE(("Job %d: changing state from %d to %d\n", |
| 4400 | jobno(thisjob), thisjob->state, jobstate)); |
| 4401 | thisjob->state = jobstate; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4402 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4403 | if (jobstate == JOBSTOPPED) |
| 4404 | set_curjob(thisjob, CUR_STOPPED); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4405 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4406 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4407 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4408 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4409 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4410 | /* The process wasn't found in job list */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4411 | out: |
| 4412 | INT_ON; |
| 4413 | |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4414 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4415 | if (want_jobexitstatus) { |
| 4416 | pid = -1; |
| 4417 | if (thisjob && thisjob->state == JOBDONE) |
| 4418 | pid = thisjob->ps[thisjob->nprocs - 1].ps_status; |
| 4419 | } |
| 4420 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4421 | if (thisjob && thisjob == job) { |
| 4422 | char s[48 + 1]; |
| 4423 | int len; |
| 4424 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4425 | len = sprint_status48(s, status, 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4426 | if (len) { |
| 4427 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4428 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4429 | out2str(s); |
| 4430 | } |
| 4431 | } |
| 4432 | return pid; |
| 4433 | } |
| 4434 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4435 | static int |
| 4436 | dowait(int block, struct job *jp) |
| 4437 | { |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4438 | smallint gotchld = *(volatile smallint *)&got_sigchld; |
| 4439 | int rpid; |
| 4440 | int pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4441 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4442 | if (jp && jp->state != JOBRUNNING) |
| 4443 | block = DOWAIT_NONBLOCK; |
| 4444 | |
| 4445 | if (block == DOWAIT_NONBLOCK && !gotchld) |
| 4446 | return 1; |
| 4447 | |
| 4448 | rpid = 1; |
| 4449 | |
| 4450 | do { |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4451 | pid = waitone(block, jp); |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4452 | rpid &= !!pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4453 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4454 | if (!pid || (jp && jp->state != JOBRUNNING)) |
| 4455 | block = DOWAIT_NONBLOCK; |
| 4456 | } while (pid >= 0); |
| 4457 | |
| 4458 | return rpid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4459 | } |
| 4460 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4461 | #if JOBS |
| 4462 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4463 | showjob(struct job *jp, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4464 | { |
| 4465 | struct procstat *ps; |
| 4466 | struct procstat *psend; |
| 4467 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4468 | int indent_col; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4469 | char s[16 + 16 + 48]; |
| 4470 | FILE *out = (mode & SHOW_STDERR ? stderr : stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4471 | |
| 4472 | ps = jp->ps; |
| 4473 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4474 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4475 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4476 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4477 | return; |
| 4478 | } |
| 4479 | |
| 4480 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4481 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4482 | |
| 4483 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4484 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4485 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4486 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4487 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4488 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4489 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4490 | |
| 4491 | psend = ps + jp->nprocs; |
| 4492 | |
| 4493 | if (jp->state == JOBRUNNING) { |
| 4494 | strcpy(s + col, "Running"); |
| 4495 | col += sizeof("Running") - 1; |
| 4496 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4497 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4498 | if (jp->state == JOBSTOPPED) |
| 4499 | status = jp->stopstatus; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4500 | col += sprint_status48(s + col, status, 0); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4501 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4502 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4503 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4504 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4505 | * or prints several "PID | <cmdN>" lines, |
| 4506 | * depending on SHOW_PIDS bit. |
| 4507 | * We do not print status of individual processes |
| 4508 | * between PID and <cmdN>. bash does it, but not very well: |
| 4509 | * first line shows overall job status, not process status, |
| 4510 | * making it impossible to know 1st process status. |
| 4511 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4512 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4513 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4514 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4515 | s[0] = '\0'; |
| 4516 | col = 33; |
| 4517 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4518 | 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] | 4519 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4520 | fprintf(out, "%s%*c%s%s", |
| 4521 | s, |
| 4522 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4523 | ps == jp->ps ? "" : "| ", |
| 4524 | ps->ps_cmd |
| 4525 | ); |
| 4526 | } while (++ps != psend); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4527 | newline_and_flush(out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4528 | |
| 4529 | jp->changed = 0; |
| 4530 | |
| 4531 | if (jp->state == JOBDONE) { |
| 4532 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4533 | freejob(jp); |
| 4534 | } |
| 4535 | } |
| 4536 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4537 | /* |
| 4538 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4539 | * statuses have changed since the last call to showjobs. |
| 4540 | */ |
| 4541 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4542 | showjobs(int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4543 | { |
| 4544 | struct job *jp; |
| 4545 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4546 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4547 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4548 | /* Handle all finished jobs */ |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4549 | dowait(DOWAIT_NONBLOCK, NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4550 | |
| 4551 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4552 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4553 | showjob(jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4554 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4557 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4558 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4559 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4560 | { |
| 4561 | int mode, m; |
| 4562 | |
| 4563 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4564 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4565 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4566 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4567 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4568 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | argv = argptr; |
| 4572 | if (*argv) { |
| 4573 | do |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4574 | showjob(getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4575 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4576 | } else { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4577 | showjobs(mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4578 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4579 | |
| 4580 | return 0; |
| 4581 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4582 | #endif /* JOBS */ |
| 4583 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4584 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4585 | static int |
| 4586 | getstatus(struct job *job) |
| 4587 | { |
| 4588 | int status; |
| 4589 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4590 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4591 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4592 | /* Fetch last member's status */ |
| 4593 | ps = job->ps + job->nprocs - 1; |
| 4594 | status = ps->ps_status; |
| 4595 | if (pipefail) { |
| 4596 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4597 | while (status == 0 && --ps >= job->ps) |
| 4598 | status = ps->ps_status; |
| 4599 | } |
| 4600 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4601 | retval = WEXITSTATUS(status); |
| 4602 | if (!WIFEXITED(status)) { |
| 4603 | #if JOBS |
| 4604 | retval = WSTOPSIG(status); |
| 4605 | if (!WIFSTOPPED(status)) |
| 4606 | #endif |
| 4607 | { |
| 4608 | /* XXX: limits number of signals */ |
| 4609 | retval = WTERMSIG(status); |
| 4610 | #if JOBS |
| 4611 | if (retval == SIGINT) |
| 4612 | job->sigint = 1; |
| 4613 | #endif |
| 4614 | } |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4615 | retval |= 128; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4616 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4617 | 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] | 4618 | jobno(job), job->nprocs, status, retval)); |
| 4619 | return retval; |
| 4620 | } |
| 4621 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4622 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4623 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4624 | { |
| 4625 | struct job *job; |
| 4626 | int retval; |
| 4627 | struct job *jp; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4628 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4629 | int status; |
| 4630 | char one = nextopt("n"); |
| 4631 | #else |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4632 | nextopt(nullstr); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4633 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4634 | retval = 0; |
| 4635 | |
| 4636 | argv = argptr; |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4637 | if (!argv[0]) { |
| 4638 | /* wait for all jobs / one job if -n */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4639 | for (;;) { |
| 4640 | jp = curjob; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4641 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4642 | if (one && !jp) |
| 4643 | /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */ |
| 4644 | retval = 127; |
| 4645 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4646 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4647 | if (!jp) /* no running procs */ |
| 4648 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4649 | if (jp->state == JOBRUNNING) |
| 4650 | break; |
| 4651 | jp->waited = 1; |
| 4652 | jp = jp->prev_job; |
| 4653 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4654 | /* man bash: |
| 4655 | * "When bash is waiting for an asynchronous command via |
| 4656 | * the wait builtin, the reception of a signal for which a trap |
| 4657 | * has been set will cause the wait builtin to return immediately |
| 4658 | * with an exit status greater than 128, immediately after which |
| 4659 | * the trap is executed." |
Denys Vlasenko | 6918811 | 2016-10-27 20:18:18 +0200 | [diff] [blame] | 4660 | */ |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4661 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4662 | status = dowait(DOWAIT_BLOCK_OR_SIG | DOWAIT_JOBSTATUS, NULL); |
| 4663 | #else |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4664 | dowait(DOWAIT_BLOCK_OR_SIG, NULL); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4665 | #endif |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4666 | /* if child sends us a signal *and immediately exits*, |
| 4667 | * dowait() returns pid > 0. Check this case, |
| 4668 | * not "if (dowait() < 0)"! |
| 4669 | */ |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4670 | if (pending_sig) |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4671 | goto sigout; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4672 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4673 | if (one) { |
| 4674 | /* wait -n waits for one _job_, not one _process_. |
| 4675 | * date; sleep 3 & sleep 2 | sleep 1 & wait -n; date |
| 4676 | * should wait for 2 seconds. Not 1 or 3. |
| 4677 | */ |
| 4678 | if (status != -1 && !WIFSTOPPED(status)) { |
| 4679 | retval = WEXITSTATUS(status); |
| 4680 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4681 | retval = 128 | WTERMSIG(status); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4682 | goto ret; |
| 4683 | } |
| 4684 | } |
| 4685 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | retval = 127; |
| 4690 | do { |
| 4691 | if (**argv != '%') { |
| 4692 | pid_t pid = number(*argv); |
| 4693 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4694 | while (1) { |
| 4695 | if (!job) |
| 4696 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4697 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4698 | break; |
| 4699 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4700 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4701 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4702 | job = getjob(*argv, 0); |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4703 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4704 | /* loop until process terminated or stopped */ |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4705 | dowait(DOWAIT_BLOCK_OR_SIG, job); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4706 | if (pending_sig) |
| 4707 | goto sigout; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4708 | job->waited = 1; |
| 4709 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4710 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4711 | } while (*++argv); |
| 4712 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4713 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4714 | return retval; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4715 | sigout: |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4716 | retval = 128 | pending_sig; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4717 | return retval; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | static struct job * |
| 4721 | growjobtab(void) |
| 4722 | { |
| 4723 | size_t len; |
| 4724 | ptrdiff_t offset; |
| 4725 | struct job *jp, *jq; |
| 4726 | |
| 4727 | len = njobs * sizeof(*jp); |
| 4728 | jq = jobtab; |
| 4729 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4730 | |
| 4731 | offset = (char *)jp - (char *)jq; |
| 4732 | if (offset) { |
| 4733 | /* Relocate pointers */ |
| 4734 | size_t l = len; |
| 4735 | |
| 4736 | jq = (struct job *)((char *)jq + l); |
| 4737 | while (l) { |
| 4738 | l -= sizeof(*jp); |
| 4739 | jq--; |
| 4740 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4741 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4742 | if (joff(jp)->ps == &jq->ps0) |
| 4743 | jmove(joff(jp)->ps); |
| 4744 | if (joff(jp)->prev_job) |
| 4745 | jmove(joff(jp)->prev_job); |
| 4746 | } |
| 4747 | if (curjob) |
| 4748 | jmove(curjob); |
| 4749 | #undef joff |
| 4750 | #undef jmove |
| 4751 | } |
| 4752 | |
| 4753 | njobs += 4; |
| 4754 | jobtab = jp; |
| 4755 | jp = (struct job *)((char *)jp + len); |
| 4756 | jq = jp + 3; |
| 4757 | do { |
| 4758 | jq->used = 0; |
| 4759 | } while (--jq >= jp); |
| 4760 | return jp; |
| 4761 | } |
| 4762 | |
| 4763 | /* |
| 4764 | * Return a new job structure. |
| 4765 | * Called with interrupts off. |
| 4766 | */ |
| 4767 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4768 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4769 | { |
| 4770 | int i; |
| 4771 | struct job *jp; |
| 4772 | |
| 4773 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4774 | if (--i < 0) { |
| 4775 | jp = growjobtab(); |
| 4776 | break; |
| 4777 | } |
| 4778 | if (jp->used == 0) |
| 4779 | break; |
| 4780 | if (jp->state != JOBDONE || !jp->waited) |
| 4781 | continue; |
| 4782 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4783 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4784 | continue; |
| 4785 | #endif |
| 4786 | freejob(jp); |
| 4787 | break; |
| 4788 | } |
| 4789 | memset(jp, 0, sizeof(*jp)); |
| 4790 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4791 | /* jp->jobctl is a bitfield. |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4792 | * "jp->jobctl |= doing_jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4793 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4794 | jp->jobctl = 1; |
| 4795 | #endif |
| 4796 | jp->prev_job = curjob; |
| 4797 | curjob = jp; |
| 4798 | jp->used = 1; |
| 4799 | jp->ps = &jp->ps0; |
| 4800 | if (nprocs > 1) { |
| 4801 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4802 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4803 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4804 | jobno(jp))); |
| 4805 | return jp; |
| 4806 | } |
| 4807 | |
| 4808 | #if JOBS |
| 4809 | /* |
| 4810 | * Return a string identifying a command (to be printed by the |
| 4811 | * jobs command). |
| 4812 | */ |
| 4813 | static char *cmdnextc; |
| 4814 | |
| 4815 | static void |
| 4816 | cmdputs(const char *s) |
| 4817 | { |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 4818 | static const char vstype[VSTYPE + 1][3] ALIGN1 = { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4819 | "", "}", "-", "+", "?", "=", |
| 4820 | "%", "%%", "#", "##" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 4821 | IF_BASH_SUBSTR(, ":") |
| 4822 | IF_BASH_PATTERN_SUBST(, "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4823 | }; |
| 4824 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4825 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4826 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4827 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4828 | unsigned char c; |
| 4829 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4830 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4831 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4832 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4833 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4834 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4835 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4836 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4837 | switch (c) { |
| 4838 | case CTLESC: |
| 4839 | c = *p++; |
| 4840 | break; |
| 4841 | case CTLVAR: |
| 4842 | subtype = *p++; |
| 4843 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4844 | str = "${#"; |
| 4845 | else |
| 4846 | str = "${"; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 4847 | goto dostr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4848 | case CTLENDVAR: |
Denys Vlasenko | 3f4847b | 2020-02-16 19:06:42 +0100 | [diff] [blame] | 4849 | str = "\"}"; |
| 4850 | str += !(quoted & 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4851 | quoted >>= 1; |
| 4852 | subtype = 0; |
| 4853 | goto dostr; |
| 4854 | case CTLBACKQ: |
| 4855 | str = "$(...)"; |
| 4856 | goto dostr; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4857 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4858 | case CTLARI: |
| 4859 | str = "$(("; |
| 4860 | goto dostr; |
| 4861 | case CTLENDARI: |
| 4862 | str = "))"; |
| 4863 | goto dostr; |
| 4864 | #endif |
| 4865 | case CTLQUOTEMARK: |
| 4866 | quoted ^= 1; |
| 4867 | c = '"'; |
| 4868 | break; |
| 4869 | case '=': |
| 4870 | if (subtype == 0) |
| 4871 | break; |
| 4872 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4873 | quoted <<= 1; |
| 4874 | str = vstype[subtype & VSTYPE]; |
| 4875 | if (subtype & VSNUL) |
| 4876 | c = ':'; |
| 4877 | else |
| 4878 | goto checkstr; |
| 4879 | break; |
| 4880 | case '\'': |
| 4881 | case '\\': |
| 4882 | case '"': |
| 4883 | case '$': |
| 4884 | /* These can only happen inside quotes */ |
| 4885 | cc[0] = c; |
| 4886 | str = cc; |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 4887 | //FIXME: |
| 4888 | // $ true $$ & |
| 4889 | // $ <cr> |
| 4890 | // [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] | 4891 | c = '\\'; |
| 4892 | break; |
| 4893 | default: |
| 4894 | break; |
| 4895 | } |
| 4896 | USTPUTC(c, nextc); |
| 4897 | checkstr: |
| 4898 | if (!str) |
| 4899 | continue; |
| 4900 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4901 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4902 | USTPUTC(c, nextc); |
| 4903 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4904 | } /* while *p++ not NUL */ |
| 4905 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4906 | if (quoted & 1) { |
| 4907 | USTPUTC('"', nextc); |
| 4908 | } |
| 4909 | *nextc = 0; |
| 4910 | cmdnextc = nextc; |
| 4911 | } |
| 4912 | |
| 4913 | /* cmdtxt() and cmdlist() call each other */ |
| 4914 | static void cmdtxt(union node *n); |
| 4915 | |
| 4916 | static void |
| 4917 | cmdlist(union node *np, int sep) |
| 4918 | { |
| 4919 | for (; np; np = np->narg.next) { |
| 4920 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4921 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4922 | cmdtxt(np); |
| 4923 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4924 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4925 | } |
| 4926 | } |
| 4927 | |
| 4928 | static void |
| 4929 | cmdtxt(union node *n) |
| 4930 | { |
| 4931 | union node *np; |
| 4932 | struct nodelist *lp; |
| 4933 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4934 | |
| 4935 | if (!n) |
| 4936 | return; |
| 4937 | switch (n->type) { |
| 4938 | default: |
| 4939 | #if DEBUG |
| 4940 | abort(); |
| 4941 | #endif |
| 4942 | case NPIPE: |
| 4943 | lp = n->npipe.cmdlist; |
| 4944 | for (;;) { |
| 4945 | cmdtxt(lp->n); |
| 4946 | lp = lp->next; |
| 4947 | if (!lp) |
| 4948 | break; |
| 4949 | cmdputs(" | "); |
| 4950 | } |
| 4951 | break; |
| 4952 | case NSEMI: |
| 4953 | p = "; "; |
| 4954 | goto binop; |
| 4955 | case NAND: |
| 4956 | p = " && "; |
| 4957 | goto binop; |
| 4958 | case NOR: |
| 4959 | p = " || "; |
| 4960 | binop: |
| 4961 | cmdtxt(n->nbinary.ch1); |
| 4962 | cmdputs(p); |
| 4963 | n = n->nbinary.ch2; |
| 4964 | goto donode; |
| 4965 | case NREDIR: |
| 4966 | case NBACKGND: |
| 4967 | n = n->nredir.n; |
| 4968 | goto donode; |
| 4969 | case NNOT: |
| 4970 | cmdputs("!"); |
| 4971 | n = n->nnot.com; |
| 4972 | donode: |
| 4973 | cmdtxt(n); |
| 4974 | break; |
| 4975 | case NIF: |
| 4976 | cmdputs("if "); |
| 4977 | cmdtxt(n->nif.test); |
| 4978 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4979 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4980 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4981 | cmdputs("; else "); |
| 4982 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4983 | } else { |
| 4984 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4985 | } |
| 4986 | p = "; fi"; |
| 4987 | goto dotail; |
| 4988 | case NSUBSHELL: |
| 4989 | cmdputs("("); |
| 4990 | n = n->nredir.n; |
| 4991 | p = ")"; |
| 4992 | goto dotail; |
| 4993 | case NWHILE: |
| 4994 | p = "while "; |
| 4995 | goto until; |
| 4996 | case NUNTIL: |
| 4997 | p = "until "; |
| 4998 | until: |
| 4999 | cmdputs(p); |
| 5000 | cmdtxt(n->nbinary.ch1); |
| 5001 | n = n->nbinary.ch2; |
| 5002 | p = "; done"; |
| 5003 | dodo: |
| 5004 | cmdputs("; do "); |
| 5005 | dotail: |
| 5006 | cmdtxt(n); |
| 5007 | goto dotail2; |
| 5008 | case NFOR: |
| 5009 | cmdputs("for "); |
| 5010 | cmdputs(n->nfor.var); |
| 5011 | cmdputs(" in "); |
| 5012 | cmdlist(n->nfor.args, 1); |
| 5013 | n = n->nfor.body; |
| 5014 | p = "; done"; |
| 5015 | goto dodo; |
| 5016 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 5017 | cmdputs(n->ndefun.text); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5018 | p = "() { ... }"; |
| 5019 | goto dotail2; |
| 5020 | case NCMD: |
| 5021 | cmdlist(n->ncmd.args, 1); |
| 5022 | cmdlist(n->ncmd.redirect, 0); |
| 5023 | break; |
| 5024 | case NARG: |
| 5025 | p = n->narg.text; |
| 5026 | dotail2: |
| 5027 | cmdputs(p); |
| 5028 | break; |
| 5029 | case NHERE: |
| 5030 | case NXHERE: |
| 5031 | p = "<<..."; |
| 5032 | goto dotail2; |
| 5033 | case NCASE: |
| 5034 | cmdputs("case "); |
| 5035 | cmdputs(n->ncase.expr->narg.text); |
| 5036 | cmdputs(" in "); |
| 5037 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 5038 | cmdtxt(np->nclist.pattern); |
| 5039 | cmdputs(") "); |
| 5040 | cmdtxt(np->nclist.body); |
| 5041 | cmdputs(";; "); |
| 5042 | } |
| 5043 | p = "esac"; |
| 5044 | goto dotail2; |
| 5045 | case NTO: |
| 5046 | p = ">"; |
| 5047 | goto redir; |
| 5048 | case NCLOBBER: |
| 5049 | p = ">|"; |
| 5050 | goto redir; |
| 5051 | case NAPPEND: |
| 5052 | p = ">>"; |
| 5053 | goto redir; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5054 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5055 | case NTO2: |
| 5056 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5057 | case NTOFD: |
| 5058 | p = ">&"; |
| 5059 | goto redir; |
| 5060 | case NFROM: |
| 5061 | p = "<"; |
| 5062 | goto redir; |
| 5063 | case NFROMFD: |
| 5064 | p = "<&"; |
| 5065 | goto redir; |
| 5066 | case NFROMTO: |
| 5067 | p = "<>"; |
| 5068 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5069 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5070 | cmdputs(p); |
| 5071 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 5072 | if (n->ndup.dupfd >= 0) |
| 5073 | cmdputs(utoa(n->ndup.dupfd)); |
| 5074 | else |
| 5075 | cmdputs("-"); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5076 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5077 | } |
| 5078 | n = n->nfile.fname; |
| 5079 | goto donode; |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | static char * |
| 5084 | commandtext(union node *n) |
| 5085 | { |
| 5086 | char *name; |
| 5087 | |
| 5088 | STARTSTACKSTR(cmdnextc); |
| 5089 | cmdtxt(n); |
| 5090 | name = stackblock(); |
Denys Vlasenko | 6a94cee | 2016-10-25 17:40:25 +0200 | [diff] [blame] | 5091 | TRACE(("commandtext: name %p, end %p\n", name, cmdnextc)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5092 | return ckstrdup(name); |
| 5093 | } |
| 5094 | #endif /* JOBS */ |
| 5095 | |
| 5096 | /* |
| 5097 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 5098 | * own process group. Jp is a job structure that the job is to be added to. |
| 5099 | * N is the command that will be evaluated by the child. Both jp and n may |
| 5100 | * be NULL. The mode parameter can be one of the following: |
| 5101 | * FORK_FG - Fork off a foreground process. |
| 5102 | * FORK_BG - Fork off a background process. |
| 5103 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 5104 | * process group even if job control is on. |
| 5105 | * |
| 5106 | * When job control is turned off, background processes have their standard |
| 5107 | * input redirected to /dev/null (except for the second and later processes |
| 5108 | * in a pipeline). |
| 5109 | * |
| 5110 | * Called with interrupts off. |
| 5111 | */ |
| 5112 | /* |
| 5113 | * Clear traps on a fork. |
| 5114 | */ |
| 5115 | static void |
| 5116 | clear_traps(void) |
| 5117 | { |
| 5118 | char **tp; |
| 5119 | |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5120 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5121 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5122 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5123 | if (trap_ptr == trap) |
| 5124 | free(*tp); |
| 5125 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5126 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 5127 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5128 | setsignal(tp - trap); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5129 | } |
| 5130 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 5131 | may_have_traps = 0; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5132 | INT_ON; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5133 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5134 | |
| 5135 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5136 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 5137 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5138 | /* Called after fork(), in child */ |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5139 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 5140 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5141 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5142 | { |
| 5143 | int oldlvl; |
| 5144 | |
| 5145 | TRACE(("Child shell %d\n", getpid())); |
| 5146 | oldlvl = shlvl; |
| 5147 | shlvl++; |
| 5148 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5149 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 5150 | * set to the values inherited by the shell from its parent". |
| 5151 | * Do we do it correctly? */ |
| 5152 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5153 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5154 | |
| 5155 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 5156 | && n && n->type == NCMD /* is it single cmd? */ |
| 5157 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5158 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5159 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 5160 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 5161 | ) { |
| 5162 | TRACE(("Trap hack\n")); |
| 5163 | /* Awful hack for `trap` or $(trap). |
| 5164 | * |
| 5165 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5166 | * contains an example where "trap" is executed in a subshell: |
| 5167 | * |
| 5168 | * save_traps=$(trap) |
| 5169 | * ... |
| 5170 | * eval "$save_traps" |
| 5171 | * |
| 5172 | * Standard does not say that "trap" in subshell shall print |
| 5173 | * parent shell's traps. It only says that its output |
| 5174 | * must have suitable form, but then, in the above example |
| 5175 | * (which is not supposed to be normative), it implies that. |
| 5176 | * |
| 5177 | * bash (and probably other shell) does implement it |
| 5178 | * (traps are reset to defaults, but "trap" still shows them), |
| 5179 | * but as a result, "trap" logic is hopelessly messed up: |
| 5180 | * |
| 5181 | * # trap |
| 5182 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5183 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5184 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5185 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5186 | * trap -- 'echo Ho' SIGWINCH |
| 5187 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5188 | * trap -- 'echo Ho' SIGWINCH |
| 5189 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5190 | * trap -- 'echo Ho' SIGWINCH |
| 5191 | * |
| 5192 | * The rules when to forget and when to not forget traps |
| 5193 | * get really complex and nonsensical. |
| 5194 | * |
| 5195 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5196 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5197 | /* Save trap handler strings for trap builtin to print */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 5198 | trap_ptr = xmemdup(trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5199 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5200 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5201 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5202 | #if JOBS |
| 5203 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5204 | doing_jobctl = 0; |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5205 | if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5206 | pid_t pgrp; |
| 5207 | |
| 5208 | if (jp->nprocs == 0) |
| 5209 | pgrp = getpid(); |
| 5210 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5211 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5212 | /* this can fail because we are doing it in the parent also */ |
| 5213 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5214 | if (mode == FORK_FG) |
| 5215 | xtcsetpgrp(ttyfd, pgrp); |
| 5216 | setsignal(SIGTSTP); |
| 5217 | setsignal(SIGTTOU); |
| 5218 | } else |
| 5219 | #endif |
| 5220 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5221 | /* man bash: "When job control is not in effect, |
| 5222 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5223 | ignoresig(SIGINT); |
| 5224 | ignoresig(SIGQUIT); |
| 5225 | if (jp->nprocs == 0) { |
| 5226 | close(0); |
| 5227 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5228 | ash_msg_and_raise_perror("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5229 | } |
| 5230 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5231 | if (oldlvl == 0) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5232 | if (iflag) { /* why if iflag only? */ |
| 5233 | setsignal(SIGINT); |
| 5234 | setsignal(SIGTERM); |
| 5235 | } |
| 5236 | /* man bash: |
| 5237 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 5238 | * commands run by bash have signal handlers |
| 5239 | * set to the values inherited by the shell |
| 5240 | * from its parent". |
| 5241 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5242 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5243 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5244 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5245 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5246 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5247 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5248 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5249 | /* "jobs": we do not want to clear job list for it, |
| 5250 | * instead we remove only _its_ own_ job from job list. |
| 5251 | * This makes "jobs .... | cat" more useful. |
| 5252 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5253 | freejob(curjob); |
| 5254 | return; |
| 5255 | } |
| 5256 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5257 | for (jp = curjob; jp; jp = jp->prev_job) |
| 5258 | freejob(jp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5261 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 5262 | #if !JOBS |
| 5263 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 5264 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5265 | static void |
| 5266 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 5267 | { |
| 5268 | TRACE(("In parent shell: child = %d\n", pid)); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5269 | if (!jp) /* jp is NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5270 | return; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5271 | #if JOBS |
| 5272 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 5273 | int pgrp; |
| 5274 | |
| 5275 | if (jp->nprocs == 0) |
| 5276 | pgrp = pid; |
| 5277 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5278 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5279 | /* This can fail because we are doing it in the child also */ |
| 5280 | setpgid(pid, pgrp); |
| 5281 | } |
| 5282 | #endif |
| 5283 | if (mode == FORK_BG) { |
| 5284 | backgndpid = pid; /* set $! */ |
| 5285 | set_curjob(jp, CUR_RUNNING); |
| 5286 | } |
| 5287 | if (jp) { |
| 5288 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5289 | ps->ps_pid = pid; |
| 5290 | ps->ps_status = -1; |
| 5291 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5292 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5293 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5294 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5295 | #endif |
| 5296 | } |
| 5297 | } |
| 5298 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5299 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5300 | static int |
| 5301 | forkshell(struct job *jp, union node *n, int mode) |
| 5302 | { |
| 5303 | int pid; |
| 5304 | |
| 5305 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 5306 | pid = fork(); |
| 5307 | if (pid < 0) { |
| 5308 | TRACE(("Fork failed, errno=%d", errno)); |
| 5309 | if (jp) |
| 5310 | freejob(jp); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5311 | ash_msg_and_raise_perror("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5312 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5313 | if (pid == 0) { |
| 5314 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5315 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5316 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5317 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5318 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5319 | return pid; |
| 5320 | } |
| 5321 | |
| 5322 | /* |
| 5323 | * Wait for job to finish. |
| 5324 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5325 | * Under job control we have the problem that while a child process |
| 5326 | * is running interrupts generated by the user are sent to the child |
| 5327 | * but not to the shell. This means that an infinite loop started by |
| 5328 | * an interactive user may be hard to kill. With job control turned off, |
| 5329 | * an interactive user may place an interactive program inside a loop. |
| 5330 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5331 | * these interrupts to also abort the loop. The approach we take here |
| 5332 | * is to have the shell ignore interrupt signals while waiting for a |
| 5333 | * foreground process to terminate, and then send itself an interrupt |
| 5334 | * signal if the child process was terminated by an interrupt signal. |
| 5335 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 5336 | * exit on interrupt; unless these processes terminate themselves by |
| 5337 | * sending a signal to themselves (instead of calling exit) they will |
| 5338 | * confuse this approach. |
| 5339 | * |
| 5340 | * Called with interrupts off. |
| 5341 | */ |
| 5342 | static int |
| 5343 | waitforjob(struct job *jp) |
| 5344 | { |
| 5345 | int st; |
| 5346 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 5347 | TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5348 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5349 | /* In non-interactive shells, we _can_ get |
| 5350 | * a keyboard signal here and be EINTRed, but we just loop |
| 5351 | * inside dowait(), waiting for command to complete. |
| 5352 | * |
| 5353 | * man bash: |
| 5354 | * "If bash is waiting for a command to complete and receives |
| 5355 | * a signal for which a trap has been set, the trap |
| 5356 | * will not be executed until the command completes." |
| 5357 | * |
| 5358 | * Reality is that even if trap is not set, bash |
| 5359 | * will not act on the signal until command completes. |
| 5360 | * Try this. sleep5intoff.c: |
| 5361 | * #include <signal.h> |
| 5362 | * #include <unistd.h> |
| 5363 | * int main() { |
| 5364 | * sigset_t set; |
| 5365 | * sigemptyset(&set); |
| 5366 | * sigaddset(&set, SIGINT); |
| 5367 | * sigaddset(&set, SIGQUIT); |
| 5368 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 5369 | * sleep(5); |
| 5370 | * return 0; |
| 5371 | * } |
| 5372 | * $ bash -c './sleep5intoff; echo hi' |
| 5373 | * ^C^C^C^C <--- pressing ^C once a second |
| 5374 | * $ _ |
| 5375 | * $ bash -c './sleep5intoff; echo hi' |
| 5376 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 5377 | * $ _ |
| 5378 | */ |
| 5379 | dowait(jp ? DOWAIT_BLOCK : DOWAIT_NONBLOCK, jp); |
| 5380 | if (!jp) |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 5381 | return exitstatus; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5382 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5383 | st = getstatus(jp); |
| 5384 | #if JOBS |
| 5385 | if (jp->jobctl) { |
| 5386 | xtcsetpgrp(ttyfd, rootpid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 5387 | restore_tty_if_stopped_or_signaled(jp); |
| 5388 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5389 | /* |
| 5390 | * This is truly gross. |
| 5391 | * If we're doing job control, then we did a TIOCSPGRP which |
| 5392 | * caused us (the shell) to no longer be in the controlling |
| 5393 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 5394 | * intuit from the subprocess exit status whether a SIGINT |
| 5395 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 5396 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5397 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 5398 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5399 | } |
| 5400 | if (jp->state == JOBDONE) |
| 5401 | #endif |
| 5402 | freejob(jp); |
| 5403 | return st; |
| 5404 | } |
| 5405 | |
| 5406 | /* |
| 5407 | * return 1 if there are stopped jobs, otherwise 0 |
| 5408 | */ |
| 5409 | static int |
| 5410 | stoppedjobs(void) |
| 5411 | { |
| 5412 | struct job *jp; |
| 5413 | int retval; |
| 5414 | |
| 5415 | retval = 0; |
| 5416 | if (job_warning) |
| 5417 | goto out; |
| 5418 | jp = curjob; |
| 5419 | if (jp && jp->state == JOBSTOPPED) { |
| 5420 | out2str("You have stopped jobs.\n"); |
| 5421 | job_warning = 2; |
| 5422 | retval++; |
| 5423 | } |
| 5424 | out: |
| 5425 | return retval; |
| 5426 | } |
| 5427 | |
| 5428 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5429 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5430 | * Code for dealing with input/output redirection. |
| 5431 | */ |
| 5432 | |
Denys Vlasenko | 8d0e0cd | 2011-01-25 23:21:46 +0100 | [diff] [blame] | 5433 | #undef EMPTY |
| 5434 | #undef CLOSED |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5435 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5436 | #define CLOSED -1 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5437 | |
| 5438 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5439 | * Handle here documents. Normally we fork off a process to write the |
| 5440 | * data to a pipe. If the document is short, we can stuff the data in |
| 5441 | * the pipe without forking. |
| 5442 | */ |
| 5443 | /* openhere needs this forward reference */ |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5444 | static void expandhere(union node *arg); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5445 | static int |
| 5446 | openhere(union node *redir) |
| 5447 | { |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5448 | char *p; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5449 | int pip[2]; |
| 5450 | size_t len = 0; |
| 5451 | |
| 5452 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5453 | ash_msg_and_raise_perror("can't create pipe"); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5454 | |
| 5455 | p = redir->nhere.doc->narg.text; |
| 5456 | if (redir->type == NXHERE) { |
| 5457 | expandhere(redir->nhere.doc); |
| 5458 | p = stackblock(); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5459 | } |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5460 | |
| 5461 | len = strlen(p); |
| 5462 | if (len <= PIPE_BUF) { |
| 5463 | xwrite(pip[1], p, len); |
| 5464 | goto out; |
| 5465 | } |
| 5466 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5467 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5468 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5469 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5470 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5471 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5472 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5473 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5474 | signal(SIGPIPE, SIG_DFL); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5475 | xwrite(pip[1], p, len); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5476 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5477 | } |
| 5478 | out: |
| 5479 | close(pip[1]); |
| 5480 | return pip[0]; |
| 5481 | } |
| 5482 | |
| 5483 | static int |
| 5484 | openredirect(union node *redir) |
| 5485 | { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5486 | struct stat sb; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5487 | char *fname; |
| 5488 | int f; |
| 5489 | |
| 5490 | switch (redir->nfile.type) { |
Denys Vlasenko | 557482c | 2016-09-25 21:24:04 +0200 | [diff] [blame] | 5491 | /* Can't happen, our single caller does this itself */ |
| 5492 | // case NTOFD: |
| 5493 | // case NFROMFD: |
| 5494 | // return -1; |
| 5495 | case NHERE: |
| 5496 | case NXHERE: |
| 5497 | return openhere(redir); |
| 5498 | } |
| 5499 | |
| 5500 | /* For N[X]HERE, reading redir->nfile.expfname would touch beyond |
| 5501 | * allocated space. Do it only when we know it is safe. |
| 5502 | */ |
| 5503 | fname = redir->nfile.expfname; |
| 5504 | |
| 5505 | switch (redir->nfile.type) { |
| 5506 | default: |
| 5507 | #if DEBUG |
| 5508 | abort(); |
| 5509 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5510 | case NFROM: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5511 | f = open(fname, O_RDONLY); |
| 5512 | if (f < 0) |
| 5513 | goto eopen; |
| 5514 | break; |
| 5515 | case NFROMTO: |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5516 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5517 | if (f < 0) |
| 5518 | goto ecreate; |
| 5519 | break; |
| 5520 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5521 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5522 | case NTO2: |
| 5523 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5524 | /* Take care of noclobber mode. */ |
| 5525 | if (Cflag) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5526 | if (stat(fname, &sb) < 0) { |
| 5527 | f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 5528 | if (f < 0) |
| 5529 | goto ecreate; |
| 5530 | } else if (!S_ISREG(sb.st_mode)) { |
| 5531 | f = open(fname, O_WRONLY, 0666); |
| 5532 | if (f < 0) |
| 5533 | goto ecreate; |
Denys Vlasenko | 355ec35 | 2018-04-02 13:34:57 +0200 | [diff] [blame] | 5534 | if (!fstat(f, &sb) && S_ISREG(sb.st_mode)) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5535 | close(f); |
| 5536 | errno = EEXIST; |
| 5537 | goto ecreate; |
| 5538 | } |
| 5539 | } else { |
| 5540 | errno = EEXIST; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5541 | goto ecreate; |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5542 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5543 | break; |
| 5544 | } |
| 5545 | /* FALLTHROUGH */ |
| 5546 | case NCLOBBER: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5547 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5548 | if (f < 0) |
| 5549 | goto ecreate; |
| 5550 | break; |
| 5551 | case NAPPEND: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5552 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5553 | if (f < 0) |
| 5554 | goto ecreate; |
| 5555 | break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | return f; |
| 5559 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5560 | 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] | 5561 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5562 | 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] | 5563 | } |
| 5564 | |
| 5565 | /* |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5566 | * Copy a file descriptor to be >= 10. Throws exception on error. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5567 | */ |
| 5568 | static int |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5569 | savefd(int from) |
| 5570 | { |
| 5571 | int newfd; |
| 5572 | int err; |
| 5573 | |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5574 | newfd = fcntl(from, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5575 | err = newfd < 0 ? errno : 0; |
| 5576 | if (err != EBADF) { |
| 5577 | if (err) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5578 | ash_msg_and_raise_perror("%d", from); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5579 | close(from); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5580 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5581 | close_on_exec_on(newfd); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | return newfd; |
| 5585 | } |
| 5586 | static int |
| 5587 | dup2_or_raise(int from, int to) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5588 | { |
| 5589 | int newfd; |
| 5590 | |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5591 | newfd = (from != to) ? dup2(from, to) : to; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5592 | if (newfd < 0) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5593 | /* Happens when source fd is not open: try "echo >&99" */ |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5594 | ash_msg_and_raise_perror("%d", from); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5595 | } |
| 5596 | return newfd; |
| 5597 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5598 | static int |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5599 | dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5600 | { |
| 5601 | int newfd; |
| 5602 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5603 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 5604 | if (newfd >= 0) { |
| 5605 | 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] | 5606 | close_on_exec_on(newfd); |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5607 | } else { /* newfd < 0 */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5608 | if (errno == EBUSY) |
| 5609 | goto repeat; |
| 5610 | if (errno == EINTR) |
| 5611 | goto repeat; |
| 5612 | } |
| 5613 | return newfd; |
| 5614 | } |
| 5615 | static int |
| 5616 | xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
| 5617 | { |
| 5618 | int newfd; |
| 5619 | repeat: |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5620 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5621 | if (newfd < 0) { |
| 5622 | if (errno == EBUSY) |
| 5623 | goto repeat; |
| 5624 | if (errno == EINTR) |
| 5625 | goto repeat; |
| 5626 | /* fd was not open? */ |
| 5627 | if (errno == EBADF) |
| 5628 | return fd; |
| 5629 | ash_msg_and_raise_perror("%d", newfd); |
| 5630 | } |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5631 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5632 | close_on_exec_on(newfd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5633 | close(fd); |
| 5634 | return newfd; |
| 5635 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5636 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5637 | /* Struct def and variable are moved down to the first usage site */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5638 | struct squirrel { |
| 5639 | int orig_fd; |
| 5640 | int moved_to; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5641 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5642 | struct redirtab { |
| 5643 | struct redirtab *next; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5644 | int pair_count; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5645 | struct squirrel two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5646 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5647 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5648 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5649 | static void |
| 5650 | add_squirrel_closed(struct redirtab *sq, int fd) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5651 | { |
| 5652 | int i; |
| 5653 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5654 | if (!sq) |
| 5655 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5656 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5657 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5658 | /* If we collide with an already moved fd... */ |
| 5659 | if (fd == sq->two_fd[i].orig_fd) { |
| 5660 | /* Examples: |
| 5661 | * "echo 3>FILE 3>&- 3>FILE" |
| 5662 | * "echo 3>&- 3>FILE" |
| 5663 | * No need for last redirect to insert |
| 5664 | * another "need to close 3" indicator. |
| 5665 | */ |
| 5666 | TRACE(("redirect_fd %d: already moved or closed\n", fd)); |
| 5667 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5668 | } |
| 5669 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5670 | TRACE(("redirect_fd %d: previous fd was closed\n", fd)); |
| 5671 | sq->two_fd[i].orig_fd = fd; |
| 5672 | sq->two_fd[i].moved_to = CLOSED; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 5675 | static int |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5676 | save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5677 | { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5678 | int i, new_fd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5679 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5680 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 5681 | avoid_fd = 9; |
| 5682 | |
| 5683 | #if JOBS |
| 5684 | if (fd == ttyfd) { |
| 5685 | /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */ |
| 5686 | ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd); |
| 5687 | TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd)); |
| 5688 | return 1; /* "we closed fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5689 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5690 | #endif |
| 5691 | /* Are we called from redirect(0)? E.g. redirect |
| 5692 | * in a forked child. No need to save fds, |
| 5693 | * we aren't going to use them anymore, ok to trash. |
| 5694 | */ |
| 5695 | if (!sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5696 | return 0; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5697 | |
| 5698 | /* If this one of script's fds? */ |
| 5699 | if (fd != 0) { |
| 5700 | struct parsefile *pf = g_parsefile; |
| 5701 | while (pf) { |
| 5702 | /* We skip fd == 0 case because of the following: |
| 5703 | * $ ash # running ash interactively |
| 5704 | * $ . ./script.sh |
| 5705 | * and in script.sh: "exec 9>&0". |
| 5706 | * Even though top-level pf_fd _is_ 0, |
| 5707 | * it's still ok to use it: "read" builtin uses it, |
| 5708 | * why should we cripple "exec" builtin? |
| 5709 | */ |
| 5710 | if (fd == pf->pf_fd) { |
| 5711 | pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 5712 | return 1; /* "we closed fd" */ |
| 5713 | } |
| 5714 | pf = pf->prev; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5717 | |
| 5718 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 5719 | |
| 5720 | /* First: do we collide with some already moved fds? */ |
| 5721 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5722 | /* If we collide with an already moved fd... */ |
| 5723 | if (fd == sq->two_fd[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5724 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5725 | sq->two_fd[i].moved_to = new_fd; |
| 5726 | TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd)); |
| 5727 | if (new_fd < 0) /* what? */ |
| 5728 | xfunc_die(); |
| 5729 | return 0; /* "we did not close fd" */ |
| 5730 | } |
| 5731 | if (fd == sq->two_fd[i].orig_fd) { |
| 5732 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 5733 | TRACE(("redirect_fd %d: already moved\n", fd)); |
| 5734 | return 0; /* "we did not close fd" */ |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | /* 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] | 5739 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5740 | TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd)); |
| 5741 | if (new_fd < 0) { |
| 5742 | if (errno != EBADF) |
| 5743 | xfunc_die(); |
| 5744 | /* new_fd = CLOSED; - already is -1 */ |
| 5745 | } |
| 5746 | sq->two_fd[i].moved_to = new_fd; |
| 5747 | sq->two_fd[i].orig_fd = fd; |
| 5748 | |
| 5749 | /* if we move stderr, let "set -x" code know */ |
| 5750 | if (fd == preverrout_fd) |
| 5751 | preverrout_fd = new_fd; |
| 5752 | |
| 5753 | return 0; /* "we did not close fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5756 | static int |
| 5757 | internally_opened_fd(int fd, struct redirtab *sq) |
| 5758 | { |
| 5759 | int i; |
| 5760 | #if JOBS |
| 5761 | if (fd == ttyfd) |
| 5762 | return 1; |
| 5763 | #endif |
| 5764 | /* If this one of script's fds? */ |
| 5765 | if (fd != 0) { |
| 5766 | struct parsefile *pf = g_parsefile; |
| 5767 | while (pf) { |
| 5768 | if (fd == pf->pf_fd) |
| 5769 | return 1; |
| 5770 | pf = pf->prev; |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5775 | if (fd == sq->two_fd[i].moved_to) |
| 5776 | return 1; |
| 5777 | } |
| 5778 | return 0; |
| 5779 | } |
| 5780 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5781 | /* |
| 5782 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5783 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5784 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5785 | */ |
| 5786 | /* flags passed to redirect */ |
| 5787 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5788 | static void |
| 5789 | redirect(union node *redir, int flags) |
| 5790 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5791 | struct redirtab *sv; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5792 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5793 | if (!redir) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5794 | return; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5795 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5796 | sv = NULL; |
| 5797 | INT_OFF; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5798 | if (flags & REDIR_PUSH) |
| 5799 | sv = redirlist; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5800 | do { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5801 | int fd; |
| 5802 | int newfd; |
| 5803 | int close_fd; |
| 5804 | int closed; |
| 5805 | |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5806 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5807 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5808 | //bb_error_msg("doing %d > %d", fd, newfd); |
| 5809 | newfd = redir->ndup.dupfd; |
| 5810 | close_fd = -1; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5811 | } else { |
| 5812 | newfd = openredirect(redir); /* always >= 0 */ |
| 5813 | if (fd == newfd) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5814 | /* open() gave us precisely the fd we wanted. |
| 5815 | * This means that this fd was not busy |
| 5816 | * (not opened to anywhere). |
| 5817 | * Remember to close it on restore: |
| 5818 | */ |
| 5819 | add_squirrel_closed(sv, fd); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5820 | continue; |
| 5821 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5822 | close_fd = newfd; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5823 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5824 | |
| 5825 | if (fd == newfd) |
| 5826 | continue; |
| 5827 | |
| 5828 | /* if "N>FILE": move newfd to fd */ |
| 5829 | /* if "N>&M": dup newfd to fd */ |
| 5830 | /* if "N>&-": close fd (newfd is -1) */ |
| 5831 | |
| 5832 | IF_BASH_REDIR_OUTPUT(redirect_more:) |
| 5833 | |
| 5834 | closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv); |
| 5835 | if (newfd == -1) { |
| 5836 | /* "N>&-" means "close me" */ |
| 5837 | if (!closed) { |
| 5838 | /* ^^^ optimization: saving may already |
| 5839 | * have closed it. If not... */ |
| 5840 | close(fd); |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5841 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5842 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5843 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 5844 | if (internally_opened_fd(newfd, sv)) { |
| 5845 | errno = EBADF; |
| 5846 | ash_msg_and_raise_perror("%d", newfd); |
| 5847 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5848 | dup2_or_raise(newfd, fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5849 | if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */ |
| 5850 | close(close_fd); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5851 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5852 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5853 | /* ">&FILE". we already redirected to 1, now copy 1 to 2 */ |
| 5854 | fd = 2; |
| 5855 | newfd = 1; |
| 5856 | close_fd = -1; |
| 5857 | goto redirect_more; |
| 5858 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5859 | #endif |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5860 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5861 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5862 | INT_ON; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5863 | |
| 5864 | //dash:#define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5865 | #define REDIR_SAVEFD2 0 |
| 5866 | // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test |
| 5867 | // triggers for pure REDIR_PUSH too. Thus, this is done almost always, |
| 5868 | // not only for calls with flags containing REDIR_SAVEFD2. |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5869 | // We do this unconditionally (see save_fd_on_redirect()). |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5870 | //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5871 | // preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5874 | static int |
| 5875 | redirectsafe(union node *redir, int flags) |
| 5876 | { |
| 5877 | int err; |
| 5878 | volatile int saveint; |
| 5879 | struct jmploc *volatile savehandler = exception_handler; |
| 5880 | struct jmploc jmploc; |
| 5881 | |
| 5882 | SAVE_INT(saveint); |
| 5883 | /* "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] | 5884 | err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */ |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5885 | if (!err) { |
| 5886 | exception_handler = &jmploc; |
| 5887 | redirect(redir, flags); |
| 5888 | } |
| 5889 | exception_handler = savehandler; |
| 5890 | if (err && exception_type != EXERROR) |
| 5891 | longjmp(exception_handler->loc, 1); |
| 5892 | RESTORE_INT(saveint); |
| 5893 | return err; |
| 5894 | } |
| 5895 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5896 | static struct redirtab* |
| 5897 | pushredir(union node *redir) |
| 5898 | { |
| 5899 | struct redirtab *sv; |
| 5900 | int i; |
| 5901 | |
| 5902 | if (!redir) |
| 5903 | return redirlist; |
| 5904 | |
| 5905 | i = 0; |
| 5906 | do { |
| 5907 | i++; |
| 5908 | #if BASH_REDIR_OUTPUT |
| 5909 | if (redir->nfile.type == NTO2) |
| 5910 | i++; |
| 5911 | #endif |
| 5912 | redir = redir->nfile.next; |
| 5913 | } while (redir); |
| 5914 | |
| 5915 | sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0])); |
| 5916 | sv->pair_count = i; |
| 5917 | while (--i >= 0) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5918 | 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] | 5919 | sv->next = redirlist; |
| 5920 | redirlist = sv; |
| 5921 | return sv->next; |
| 5922 | } |
| 5923 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5924 | /* |
| 5925 | * Undo the effects of the last redirection. |
| 5926 | */ |
| 5927 | static void |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5928 | popredir(int drop) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5929 | { |
| 5930 | struct redirtab *rp; |
| 5931 | int i; |
| 5932 | |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 5933 | if (redirlist == NULL) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5934 | return; |
| 5935 | INT_OFF; |
| 5936 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5937 | for (i = 0; i < rp->pair_count; i++) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5938 | int fd = rp->two_fd[i].orig_fd; |
| 5939 | int copy = rp->two_fd[i].moved_to; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5940 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5941 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5942 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5943 | continue; |
| 5944 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5945 | if (copy != EMPTY) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5946 | if (!drop) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5947 | /*close(fd);*/ |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5948 | dup2_or_raise(copy, fd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5949 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5950 | close(copy); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5951 | } |
| 5952 | } |
| 5953 | redirlist = rp->next; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5954 | free(rp); |
| 5955 | INT_ON; |
| 5956 | } |
| 5957 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5958 | static void |
| 5959 | unwindredir(struct redirtab *stop) |
| 5960 | { |
| 5961 | while (redirlist != stop) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5962 | popredir(/*drop:*/ 0); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5963 | } |
| 5964 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5965 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5966 | /* ============ Routines to expand arguments to commands |
| 5967 | * |
| 5968 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5969 | */ |
| 5970 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5971 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5972 | static arith_t |
| 5973 | ash_arith(const char *s) |
| 5974 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5975 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5976 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5977 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5978 | math_state.lookupvar = lookupvar; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 5979 | math_state.setvar = setvar0; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5980 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5981 | |
| 5982 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5983 | result = arith(&math_state, s); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5984 | if (math_state.errmsg) |
| 5985 | ash_msg_and_raise_error(math_state.errmsg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5986 | INT_ON; |
| 5987 | |
| 5988 | return result; |
| 5989 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5990 | #endif |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 5991 | #if BASH_SUBSTR |
| 5992 | # if ENABLE_FEATURE_SH_MATH |
| 5993 | static int substr_atoi(const char *s) |
| 5994 | { |
| 5995 | arith_t t = ash_arith(s); |
| 5996 | if (sizeof(t) > sizeof(int)) { |
| 5997 | /* clamp very large or very large negative nums for ${v:N:M}: |
| 5998 | * else "${v:0:0x100000001}" would work as "${v:0:1}" |
| 5999 | */ |
| 6000 | if (t > INT_MAX) |
| 6001 | t = INT_MAX; |
| 6002 | if (t < INT_MIN) |
| 6003 | t = INT_MIN; |
| 6004 | } |
| 6005 | return t; |
| 6006 | } |
| 6007 | # else |
| 6008 | # define substr_atoi(s) number(s) |
| 6009 | # endif |
| 6010 | #endif |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 6011 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6012 | /* |
| 6013 | * expandarg flags |
| 6014 | */ |
| 6015 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 6016 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 6017 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 6018 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
Denys Vlasenko | db74c6c | 2016-10-24 21:12:33 +0200 | [diff] [blame] | 6019 | /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt" |
| 6020 | * POSIX says for this case: |
| 6021 | * Pathname expansion shall not be performed on the word by a |
| 6022 | * non-interactive shell; an interactive shell may perform it, but shall |
| 6023 | * do so only when the expansion would result in one word. |
| 6024 | * Currently, our code complies to the above rule by never globbing |
| 6025 | * redirection filenames. |
| 6026 | * Bash performs globbing, unless it is non-interactive and in POSIX mode. |
| 6027 | * (this means that on a typical Linux distro, bash almost always |
| 6028 | * performs globbing, and thus diverges from what we do). |
| 6029 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6030 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6031 | #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */ |
| 6032 | #define EXP_WORD 0x40 /* expand word in parameter expansion */ |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 6033 | #define EXP_QUOTED 0x100 /* expand word in double quotes */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6034 | #define EXP_KEEPNUL 0x200 /* do not skip NUL characters */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6035 | #define EXP_DISCARD 0x400 /* discard result of expansion */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6036 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6037 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6038 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6039 | */ |
| 6040 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 6041 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6042 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 6043 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 6044 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6045 | /* Add CTLESC when necessary. */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6046 | #define QUOTES_ESC (EXP_FULL | EXP_CASE) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6047 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6048 | /* |
| 6049 | * Structure specifying which parts of the string should be searched |
| 6050 | * for IFS characters. |
| 6051 | */ |
| 6052 | struct ifsregion { |
| 6053 | struct ifsregion *next; /* next region in list */ |
| 6054 | int begoff; /* offset of start of region */ |
| 6055 | int endoff; /* offset of end of region */ |
| 6056 | int nulonly; /* search for nul bytes only */ |
| 6057 | }; |
| 6058 | |
| 6059 | struct arglist { |
| 6060 | struct strlist *list; |
| 6061 | struct strlist **lastp; |
| 6062 | }; |
| 6063 | |
| 6064 | /* output of current string */ |
| 6065 | static char *expdest; |
| 6066 | /* list of back quote expressions */ |
| 6067 | static struct nodelist *argbackq; |
| 6068 | /* first struct in list of ifs regions */ |
| 6069 | static struct ifsregion ifsfirst; |
| 6070 | /* last struct in list */ |
| 6071 | static struct ifsregion *ifslastp; |
| 6072 | /* holds expanded arg list */ |
| 6073 | static struct arglist exparg; |
| 6074 | |
| 6075 | /* |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6076 | * Break the argument string into pieces based upon IFS and add the |
| 6077 | * strings to the argument list. The regions of the string to be |
| 6078 | * searched for IFS characters have been stored by recordregion. |
| 6079 | */ |
| 6080 | static void |
| 6081 | ifsbreakup(char *string, struct arglist *arglist) |
| 6082 | { |
| 6083 | struct ifsregion *ifsp; |
| 6084 | struct strlist *sp; |
| 6085 | char *start; |
| 6086 | char *p; |
| 6087 | char *q; |
| 6088 | const char *ifs, *realifs; |
| 6089 | int ifsspc; |
| 6090 | int nulonly; |
| 6091 | |
| 6092 | start = string; |
| 6093 | if (ifslastp != NULL) { |
| 6094 | ifsspc = 0; |
| 6095 | nulonly = 0; |
| 6096 | realifs = ifsset() ? ifsval() : defifs; |
| 6097 | ifsp = &ifsfirst; |
| 6098 | do { |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6099 | int afternul; |
| 6100 | |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6101 | p = string + ifsp->begoff; |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6102 | afternul = nulonly; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6103 | nulonly = ifsp->nulonly; |
| 6104 | ifs = nulonly ? nullstr : realifs; |
| 6105 | ifsspc = 0; |
| 6106 | while (p < string + ifsp->endoff) { |
| 6107 | q = p; |
| 6108 | if ((unsigned char)*p == CTLESC) |
| 6109 | p++; |
| 6110 | if (!strchr(ifs, *p)) { |
| 6111 | p++; |
| 6112 | continue; |
| 6113 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6114 | if (!(afternul || nulonly)) |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6115 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6116 | /* Ignore IFS whitespace at start */ |
| 6117 | if (q == start && ifsspc) { |
| 6118 | p++; |
| 6119 | start = p; |
| 6120 | continue; |
| 6121 | } |
| 6122 | *q = '\0'; |
| 6123 | sp = stzalloc(sizeof(*sp)); |
| 6124 | sp->text = start; |
| 6125 | *arglist->lastp = sp; |
| 6126 | arglist->lastp = &sp->next; |
| 6127 | p++; |
| 6128 | if (!nulonly) { |
| 6129 | for (;;) { |
| 6130 | if (p >= string + ifsp->endoff) { |
| 6131 | break; |
| 6132 | } |
| 6133 | q = p; |
| 6134 | if ((unsigned char)*p == CTLESC) |
| 6135 | p++; |
| 6136 | if (strchr(ifs, *p) == NULL) { |
| 6137 | p = q; |
| 6138 | break; |
| 6139 | } |
| 6140 | if (strchr(defifs, *p) == NULL) { |
| 6141 | if (ifsspc) { |
| 6142 | p++; |
| 6143 | ifsspc = 0; |
| 6144 | } else { |
| 6145 | p = q; |
| 6146 | break; |
| 6147 | } |
| 6148 | } else |
| 6149 | p++; |
| 6150 | } |
| 6151 | } |
| 6152 | start = p; |
| 6153 | } /* while */ |
| 6154 | ifsp = ifsp->next; |
| 6155 | } while (ifsp != NULL); |
| 6156 | if (nulonly) |
| 6157 | goto add; |
| 6158 | } |
| 6159 | |
| 6160 | if (!*start) |
| 6161 | return; |
| 6162 | |
| 6163 | add: |
| 6164 | sp = stzalloc(sizeof(*sp)); |
| 6165 | sp->text = start; |
| 6166 | *arglist->lastp = sp; |
| 6167 | arglist->lastp = &sp->next; |
| 6168 | } |
| 6169 | |
| 6170 | static void |
| 6171 | ifsfree(void) |
| 6172 | { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6173 | struct ifsregion *p = ifsfirst.next; |
| 6174 | |
| 6175 | if (!p) |
| 6176 | goto out; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6177 | |
| 6178 | INT_OFF; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6179 | do { |
| 6180 | struct ifsregion *ifsp; |
| 6181 | ifsp = p->next; |
| 6182 | free(p); |
| 6183 | p = ifsp; |
| 6184 | } while (p); |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6185 | ifsfirst.next = NULL; |
| 6186 | INT_ON; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6187 | out: |
| 6188 | ifslastp = NULL; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6189 | } |
| 6190 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6191 | static size_t |
| 6192 | esclen(const char *start, const char *p) |
| 6193 | { |
| 6194 | size_t esc = 0; |
| 6195 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6196 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6197 | esc++; |
| 6198 | } |
| 6199 | return esc; |
| 6200 | } |
| 6201 | |
| 6202 | /* |
| 6203 | * Remove any CTLESC characters from a string. |
| 6204 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6205 | #if !BASH_PATTERN_SUBST |
| 6206 | #define rmescapes(str, flag, slash_position) \ |
| 6207 | rmescapes(str, flag) |
| 6208 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6209 | static char * |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6210 | rmescapes(char *str, int flag, int *slash_position) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6211 | { |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6212 | static const char qchars[] ALIGN1 = { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6213 | IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 6214 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6215 | char *p, *q, *r; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6216 | unsigned protect_against_glob; |
| 6217 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6218 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6219 | p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash_position)); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6220 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6221 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6222 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6223 | q = p; |
| 6224 | r = str; |
| 6225 | if (flag & RMESCAPE_ALLOC) { |
| 6226 | size_t len = p - str; |
| 6227 | size_t fulllen = len + strlen(p) + 1; |
| 6228 | |
| 6229 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6230 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6231 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6232 | /* p and str may be invalidated by makestrspace */ |
| 6233 | str = (char *)stackblock() + strloc; |
| 6234 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6235 | } else if (flag & RMESCAPE_HEAP) { |
| 6236 | r = ckmalloc(fulllen); |
| 6237 | } else { |
| 6238 | r = stalloc(fulllen); |
| 6239 | } |
| 6240 | q = r; |
| 6241 | if (len > 0) { |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 6242 | q = (char *)mempcpy(q, str, len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6243 | } |
| 6244 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6245 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6246 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6247 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6248 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6249 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6250 | // Note: protect_against_glob only affect whether |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6251 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6252 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6253 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6254 | continue; |
| 6255 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6256 | if (*p == '\\') { |
| 6257 | /* naked back slash */ |
| 6258 | protect_against_glob = 0; |
| 6259 | goto copy; |
| 6260 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6261 | if ((unsigned char)*p == CTLESC) { |
| 6262 | p++; |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 6263 | #if DEBUG |
| 6264 | if (*p == '\0') |
| 6265 | ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)"); |
| 6266 | #endif |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6267 | if (protect_against_glob) { |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6268 | /* |
| 6269 | * We used to trust glob() and fnmatch() to eat |
| 6270 | * superfluous escapes (\z where z has no |
| 6271 | * special meaning anyway). But this causes |
| 6272 | * bugs such as string of one greek letter rho |
Denys Vlasenko | ed79a63 | 2017-07-05 19:20:43 +0200 | [diff] [blame] | 6273 | * (unicode-encoded as two bytes "cf,81") |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6274 | * getting encoded as "cf,CTLESC,81" |
| 6275 | * and here, converted to "cf,\,81" - |
| 6276 | * which does not go well with some flavors |
Denys Vlasenko | 92b8d9c | 2017-07-05 19:13:44 +0200 | [diff] [blame] | 6277 | * of fnmatch() in unicode locales |
| 6278 | * (for example, glibc <= 2.22). |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6279 | * |
| 6280 | * Lets add "\" only on the chars which need it. |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6281 | * Testcases for less obvious chars are shown. |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6282 | */ |
| 6283 | if (*p == '*' |
| 6284 | || *p == '?' |
| 6285 | || *p == '[' |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6286 | || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */ |
| 6287 | || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */ |
| 6288 | || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */ |
| 6289 | || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6290 | /* Some libc support [^negate], that's why "^" also needs love */ |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6291 | || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6292 | ) { |
| 6293 | *q++ = '\\'; |
| 6294 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6295 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6296 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6297 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6298 | else if (slash_position && p == str + *slash_position) { |
| 6299 | /* stop handling globbing */ |
| 6300 | globbing = 0; |
| 6301 | *slash_position = q - r; |
| 6302 | slash_position = NULL; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6303 | } |
| 6304 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6305 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6306 | copy: |
| 6307 | *q++ = *p++; |
| 6308 | } |
| 6309 | *q = '\0'; |
| 6310 | if (flag & RMESCAPE_GROW) { |
| 6311 | expdest = r; |
| 6312 | STADJUST(q - r + 1, expdest); |
| 6313 | } |
| 6314 | return r; |
| 6315 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6316 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 6317 | |
| 6318 | /* |
| 6319 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 6320 | * |
| 6321 | * Returns an stalloced string. |
| 6322 | */ |
| 6323 | static char * |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6324 | preglob(const char *pattern, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6325 | { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6326 | return rmescapes((char *)pattern, flag | RMESCAPE_GLOB, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | /* |
| 6330 | * Put a string on the stack. |
| 6331 | */ |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6332 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6333 | memtodest(const char *p, size_t len, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6334 | { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6335 | int syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6336 | char *q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6337 | char *s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6338 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6339 | if (!len) |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6340 | return 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6341 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6342 | q = makestrspace(len * 2, expdest); |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6343 | s = q; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6344 | |
| 6345 | do { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6346 | unsigned char c = *p++; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6347 | if (c) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6348 | if (flags & QUOTES_ESC) { |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6349 | int n = SIT(c, syntax); |
| 6350 | if (n == CCTL |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6351 | || ((flags & EXP_QUOTED) && n == CBACK) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6352 | ) { |
| 6353 | USTPUTC(CTLESC, q); |
| 6354 | } |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 6355 | } |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6356 | } else if (!(flags & EXP_KEEPNUL)) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6357 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6358 | USTPUTC(c, q); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6359 | } while (--len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6360 | |
| 6361 | expdest = q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6362 | return q - s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6363 | } |
| 6364 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6365 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6366 | strtodest(const char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6367 | { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6368 | size_t len = strlen(p); |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6369 | memtodest(p, len, flags); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6370 | return len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | /* |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6374 | * Our own itoa(). |
| 6375 | * cvtnum() is used even if math support is off (to prepare $? values and such). |
| 6376 | */ |
| 6377 | static int |
| 6378 | cvtnum(arith_t num, int flags) |
| 6379 | { |
| 6380 | /* 32-bit and wider ints require buffer size of bytes*3 (or less) */ |
| 6381 | /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */ |
| 6382 | int len = (sizeof(arith_t) >= 4) ? sizeof(arith_t) * 3 : sizeof(arith_t) * 3 + 2; |
| 6383 | char buf[len]; |
| 6384 | |
| 6385 | len = fmtstr(buf, len, ARITH_FMT, num); |
| 6386 | return memtodest(buf, len, flags); |
| 6387 | } |
| 6388 | |
| 6389 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6390 | * Record the fact that we have to scan this region of the |
| 6391 | * string for IFS characters. |
| 6392 | */ |
| 6393 | static void |
| 6394 | recordregion(int start, int end, int nulonly) |
| 6395 | { |
| 6396 | struct ifsregion *ifsp; |
| 6397 | |
| 6398 | if (ifslastp == NULL) { |
| 6399 | ifsp = &ifsfirst; |
| 6400 | } else { |
| 6401 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6402 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 6403 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6404 | ifslastp->next = ifsp; |
| 6405 | INT_ON; |
| 6406 | } |
| 6407 | ifslastp = ifsp; |
| 6408 | ifslastp->begoff = start; |
| 6409 | ifslastp->endoff = end; |
| 6410 | ifslastp->nulonly = nulonly; |
| 6411 | } |
| 6412 | |
| 6413 | static void |
| 6414 | removerecordregions(int endoff) |
| 6415 | { |
| 6416 | if (ifslastp == NULL) |
| 6417 | return; |
| 6418 | |
| 6419 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6420 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6421 | struct ifsregion *ifsp; |
| 6422 | INT_OFF; |
| 6423 | ifsp = ifsfirst.next->next; |
| 6424 | free(ifsfirst.next); |
| 6425 | ifsfirst.next = ifsp; |
| 6426 | INT_ON; |
| 6427 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6428 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6429 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6430 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6431 | ifslastp = &ifsfirst; |
| 6432 | ifsfirst.endoff = endoff; |
| 6433 | } |
| 6434 | return; |
| 6435 | } |
| 6436 | |
| 6437 | ifslastp = &ifsfirst; |
| 6438 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6439 | ifslastp = ifslastp->next; |
| 6440 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6441 | struct ifsregion *ifsp; |
| 6442 | INT_OFF; |
| 6443 | ifsp = ifslastp->next->next; |
| 6444 | free(ifslastp->next); |
| 6445 | ifslastp->next = ifsp; |
| 6446 | INT_ON; |
| 6447 | } |
| 6448 | if (ifslastp->endoff > endoff) |
| 6449 | ifslastp->endoff = endoff; |
| 6450 | } |
| 6451 | |
| 6452 | static char * |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6453 | exptilde(char *startp, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6454 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6455 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6456 | char *name; |
| 6457 | struct passwd *pw; |
| 6458 | const char *home; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6459 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6460 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6461 | p = startp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6462 | name = p + 1; |
| 6463 | |
| 6464 | while ((c = *++p) != '\0') { |
| 6465 | switch (c) { |
| 6466 | case CTLESC: |
| 6467 | return startp; |
| 6468 | case CTLQUOTEMARK: |
| 6469 | return startp; |
| 6470 | case ':': |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6471 | if (flag & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6472 | goto done; |
| 6473 | break; |
| 6474 | case '/': |
| 6475 | case CTLENDVAR: |
| 6476 | goto done; |
| 6477 | } |
| 6478 | } |
| 6479 | done: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6480 | if (flag & EXP_DISCARD) |
| 6481 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6482 | *p = '\0'; |
| 6483 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 6484 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6485 | } else { |
| 6486 | pw = getpwnam(name); |
| 6487 | if (pw == NULL) |
| 6488 | goto lose; |
| 6489 | home = pw->pw_dir; |
| 6490 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6491 | *p = c; |
Denys Vlasenko | e880b1f | 2020-02-16 18:31:05 +0100 | [diff] [blame] | 6492 | if (!home) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6493 | goto lose; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6494 | strtodest(home, flag | EXP_QUOTED); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6495 | out: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6496 | return p; |
| 6497 | lose: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6498 | return startp; |
| 6499 | } |
| 6500 | |
| 6501 | /* |
| 6502 | * Execute a command inside back quotes. If it's a builtin command, we |
| 6503 | * want to save its output in a block obtained from malloc. Otherwise |
| 6504 | * we fork off a subprocess and get the output of the command via a pipe. |
| 6505 | * Should be called with interrupts off. |
| 6506 | */ |
| 6507 | struct backcmd { /* result of evalbackcmd */ |
| 6508 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6509 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 6510 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6511 | struct job *jp; /* job structure for command */ |
| 6512 | }; |
| 6513 | |
| 6514 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 6515 | /* flags in argument to evaltree */ |
| 6516 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 6517 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 6518 | static int evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6519 | |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6520 | /* An evaltree() which is known to never return. |
| 6521 | * Used to use an alias: |
| 6522 | * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__)); |
| 6523 | * but clang was reported to "transfer" noreturn-ness to evaltree() as well. |
| 6524 | */ |
| 6525 | static ALWAYS_INLINE NORETURN void |
| 6526 | evaltreenr(union node *n, int flags) |
| 6527 | { |
| 6528 | evaltree(n, flags); |
| 6529 | bb_unreachable(abort()); |
| 6530 | /* NOTREACHED */ |
| 6531 | } |
| 6532 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6533 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6534 | evalbackcmd(union node *n, struct backcmd *result) |
| 6535 | { |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6536 | int pip[2]; |
| 6537 | struct job *jp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6538 | |
| 6539 | result->fd = -1; |
| 6540 | result->buf = NULL; |
| 6541 | result->nleft = 0; |
| 6542 | result->jp = NULL; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6543 | if (n == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6544 | goto out; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6545 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6546 | |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6547 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 6548 | ash_msg_and_raise_perror("can't create pipe"); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6549 | jp = makejob(/*n,*/ 1); |
| 6550 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6551 | /* child */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6552 | FORCE_INT_ON; |
| 6553 | close(pip[0]); |
| 6554 | if (pip[1] != 1) { |
| 6555 | /*close(1);*/ |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 6556 | dup2_or_raise(pip[1], 1); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6557 | close(pip[1]); |
| 6558 | } |
Denys Vlasenko | 960ca38 | 2016-10-25 18:12:15 +0200 | [diff] [blame] | 6559 | /* TODO: eflag clearing makes the following not abort: |
| 6560 | * ash -c 'set -e; z=$(false;echo foo); echo $z' |
| 6561 | * which is what bash does (unless it is in POSIX mode). |
| 6562 | * dash deleted "eflag = 0" line in the commit |
| 6563 | * Date: Mon, 28 Jun 2010 17:11:58 +1000 |
| 6564 | * [EVAL] Don't clear eflag in evalbackcmd |
| 6565 | * For now, preserve bash-like behavior, it seems to be somewhat more useful: |
| 6566 | */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6567 | eflag = 0; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6568 | ifsfree(); |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6569 | evaltreenr(n, EV_EXIT); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6570 | /* NOTREACHED */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6571 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6572 | /* parent */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6573 | close(pip[1]); |
| 6574 | result->fd = pip[0]; |
| 6575 | result->jp = jp; |
| 6576 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6577 | out: |
| 6578 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 6579 | result->fd, result->buf, result->nleft, result->jp)); |
| 6580 | } |
| 6581 | |
| 6582 | /* |
| 6583 | * Expand stuff in backwards quotes. |
| 6584 | */ |
| 6585 | static void |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6586 | expbackq(union node *cmd, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6587 | { |
| 6588 | struct backcmd in; |
| 6589 | int i; |
| 6590 | char buf[128]; |
| 6591 | char *p; |
| 6592 | char *dest; |
| 6593 | int startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6594 | struct stackmark smark; |
| 6595 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6596 | if (flag & EXP_DISCARD) |
| 6597 | goto out; |
| 6598 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6599 | INT_OFF; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 6600 | startloc = expdest - (char *)stackblock(); |
| 6601 | pushstackmark(&smark, startloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6602 | evalbackcmd(cmd, &in); |
| 6603 | popstackmark(&smark); |
| 6604 | |
| 6605 | p = in.buf; |
| 6606 | i = in.nleft; |
| 6607 | if (i == 0) |
| 6608 | goto read; |
| 6609 | for (;;) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6610 | memtodest(p, i, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6611 | read: |
| 6612 | if (in.fd < 0) |
| 6613 | break; |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 6614 | i = nonblock_immune_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6615 | TRACE(("expbackq: read returns %d\n", i)); |
| 6616 | if (i <= 0) |
| 6617 | break; |
| 6618 | p = buf; |
| 6619 | } |
| 6620 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 6621 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6622 | if (in.fd >= 0) { |
| 6623 | close(in.fd); |
| 6624 | back_exitstatus = waitforjob(in.jp); |
| 6625 | } |
| 6626 | INT_ON; |
| 6627 | |
| 6628 | /* Eat all trailing newlines */ |
| 6629 | dest = expdest; |
Denys Vlasenko | 9ee5892 | 2020-02-17 10:24:32 +0100 | [diff] [blame] | 6630 | for (; dest > ((char *)stackblock() + startloc) && dest[-1] == '\n';) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6631 | STUNPUTC(dest); |
| 6632 | expdest = dest; |
| 6633 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6634 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6635 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6636 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 6637 | (int)((dest - (char *)stackblock()) - startloc), |
| 6638 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6639 | stackblock() + startloc)); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6640 | |
| 6641 | out: |
| 6642 | argbackq = argbackq->next; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6645 | /* expari needs it */ |
| 6646 | static char *argstr(char *p, int flag); |
| 6647 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6648 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6649 | /* |
| 6650 | * Expand arithmetic expression. Backup to start of expression, |
| 6651 | * evaluate, place result in (backed up) result, adjust string position. |
| 6652 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6653 | static char * |
| 6654 | expari(char *start, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6655 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6656 | struct stackmark sm; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6657 | int begoff; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6658 | int endoff; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6659 | int len; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6660 | arith_t result; |
| 6661 | char *p; |
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 | p = stackblock(); |
| 6664 | begoff = expdest - p; |
| 6665 | p = argstr(start, flag & EXP_DISCARD); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6666 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6667 | if (flag & EXP_DISCARD) |
| 6668 | goto out; |
| 6669 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6670 | start = stackblock(); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6671 | endoff = expdest - start; |
| 6672 | start += begoff; |
| 6673 | STADJUST(start - expdest, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6674 | |
| 6675 | removerecordregions(begoff); |
| 6676 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6677 | if (flag & QUOTES_ESC) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6678 | rmescapes(start, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6679 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6680 | pushstackmark(&sm, endoff); |
| 6681 | result = ash_arith(start); |
| 6682 | popstackmark(&sm); |
| 6683 | |
| 6684 | len = cvtnum(result, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6685 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6686 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6687 | recordregion(begoff, begoff + len, 0); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6688 | |
| 6689 | out: |
| 6690 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6691 | } |
| 6692 | #endif |
| 6693 | |
| 6694 | /* argstr needs it */ |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 6695 | static char *evalvar(char *p, int flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6696 | |
| 6697 | /* |
| 6698 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 6699 | * characters to allow for further processing. Otherwise treat |
| 6700 | * $@ like $* since no splitting will be performed. |
| 6701 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6702 | static char * |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6703 | argstr(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6704 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6705 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6706 | '=', |
| 6707 | ':', |
| 6708 | CTLQUOTEMARK, |
| 6709 | CTLENDVAR, |
| 6710 | CTLESC, |
| 6711 | CTLVAR, |
| 6712 | CTLBACKQ, |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6713 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6714 | CTLARI, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6715 | CTLENDARI, |
| 6716 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6717 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6718 | }; |
| 6719 | const char *reject = spclchars; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6720 | int breakall = (flag & (EXP_WORD | EXP_QUOTED)) == EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6721 | int inquotes; |
| 6722 | size_t length; |
| 6723 | int startloc; |
| 6724 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6725 | reject += !!(flag & EXP_VARTILDE2); |
| 6726 | reject += flag & EXP_VARTILDE ? 0 : 2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6727 | inquotes = 0; |
| 6728 | length = 0; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6729 | if (flag & EXP_TILDE) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6730 | flag &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6731 | tilde: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6732 | if (*p == '~') |
| 6733 | p = exptilde(p, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6734 | } |
| 6735 | start: |
| 6736 | startloc = expdest - (char *)stackblock(); |
| 6737 | for (;;) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6738 | int end; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6739 | unsigned char c; |
| 6740 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6741 | length += strcspn(p + length, reject); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6742 | end = 0; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6743 | c = p[length]; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6744 | if (!(c & 0x80) |
| 6745 | IF_FEATURE_SH_MATH(|| c == CTLENDARI) |
| 6746 | || c == CTLENDVAR |
| 6747 | ) { |
| 6748 | /* |
| 6749 | * c == '=' || c == ':' || c == '\0' || |
| 6750 | * c == CTLENDARI || c == CTLENDVAR |
| 6751 | */ |
| 6752 | length++; |
| 6753 | /* c == '\0' || c == CTLENDARI || c == CTLENDVAR */ |
| 6754 | end = !!((c - 1) & 0x80); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6755 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6756 | if (length > 0 && !(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6757 | int newloc; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6758 | char *q; |
| 6759 | |
| 6760 | q = stnputs(p, length, expdest); |
| 6761 | q[-1] &= end - 1; |
| 6762 | expdest = q - (flag & EXP_WORD ? end : 0); |
| 6763 | newloc = q - (char *)stackblock() - end; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6764 | if (breakall && !inquotes && newloc > startloc) { |
| 6765 | recordregion(startloc, newloc, 0); |
| 6766 | } |
| 6767 | startloc = newloc; |
| 6768 | } |
| 6769 | p += length + 1; |
| 6770 | length = 0; |
| 6771 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6772 | if (end) |
| 6773 | break; |
| 6774 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6775 | switch (c) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6776 | case '=': |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6777 | flag |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6778 | reject++; |
| 6779 | /* fall through */ |
| 6780 | case ':': |
| 6781 | /* |
| 6782 | * sort of a hack - expand tildes in variable |
| 6783 | * assignments (after the first '=' and after ':'s). |
| 6784 | */ |
| 6785 | if (*--p == '~') { |
| 6786 | goto tilde; |
| 6787 | } |
| 6788 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6789 | case CTLQUOTEMARK: |
| 6790 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6791 | if (!inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6792 | p = evalvar(p + 1, flag | EXP_QUOTED) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6793 | goto start; |
| 6794 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6795 | inquotes ^= EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6796 | addquote: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6797 | if (flag & QUOTES_ESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6798 | p--; |
| 6799 | length++; |
| 6800 | startloc++; |
| 6801 | } |
| 6802 | break; |
| 6803 | case CTLESC: |
| 6804 | startloc++; |
| 6805 | length++; |
| 6806 | goto addquote; |
| 6807 | case CTLVAR: |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6808 | TRACE(("argstr: evalvar('%s')\n", p)); |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6809 | p = evalvar(p, flag | inquotes); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6810 | TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock())); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6811 | goto start; |
| 6812 | case CTLBACKQ: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6813 | expbackq(argbackq->n, flag | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6814 | goto start; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6815 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6816 | case CTLARI: |
| 6817 | p = expari(p, flag | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6818 | goto start; |
| 6819 | #endif |
| 6820 | } |
| 6821 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6822 | return p - 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6826 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6827 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6828 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6829 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6830 | char c; |
| 6831 | |
| 6832 | loc = startp; |
| 6833 | loc2 = rmesc; |
| 6834 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6835 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6836 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6837 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6838 | c = *loc2; |
| 6839 | if (zero) { |
| 6840 | *loc2 = '\0'; |
| 6841 | s = rmesc; |
| 6842 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6843 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6844 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6845 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6846 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6847 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6848 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6849 | loc++; |
| 6850 | loc++; |
| 6851 | loc2++; |
| 6852 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6853 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
| 6856 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6857 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6858 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6859 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6860 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6861 | int try2optimize = match_at_start; |
| 6862 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6863 | int esc = 0; |
| 6864 | char *loc; |
| 6865 | char *loc2; |
| 6866 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6867 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6868 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6869 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6870 | * Logic: |
| 6871 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6872 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6873 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6874 | */ |
| 6875 | /* TODO: document in what other circumstances we are called. */ |
| 6876 | |
| 6877 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6878 | int match; |
| 6879 | char c = *loc2; |
| 6880 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6881 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6882 | *loc2 = '\0'; |
| 6883 | s = rmesc; |
| 6884 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6885 | match = pmatch(pattern, s); |
| 6886 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6887 | *loc2 = c; |
| 6888 | if (match) |
| 6889 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6890 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6891 | if (try2optimize) { |
| 6892 | /* Maybe we can optimize this: |
| 6893 | * if pattern ends with unescaped *, we can avoid checking |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6894 | * shorter strings: if "foo*" doesn't match "raw_value_of_v", |
| 6895 | * it won't match truncated "raw_value_of_" strings too. |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6896 | */ |
| 6897 | unsigned plen = strlen(pattern); |
| 6898 | /* Does it end with "*"? */ |
| 6899 | if (plen != 0 && pattern[--plen] == '*') { |
| 6900 | /* "xxxx*" is not escaped */ |
| 6901 | /* "xxx\*" is escaped */ |
| 6902 | /* "xx\\*" is not escaped */ |
| 6903 | /* "x\\\*" is escaped */ |
| 6904 | int slashes = 0; |
| 6905 | while (plen != 0 && pattern[--plen] == '\\') |
| 6906 | slashes++; |
| 6907 | if (!(slashes & 1)) |
| 6908 | break; /* ends with unescaped "*" */ |
| 6909 | } |
| 6910 | try2optimize = 0; |
| 6911 | } |
| 6912 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6913 | loc--; |
| 6914 | if (quotes) { |
| 6915 | if (--esc < 0) { |
| 6916 | esc = esclen(startp, loc); |
| 6917 | } |
| 6918 | if (esc % 2) { |
| 6919 | esc--; |
| 6920 | loc--; |
| 6921 | } |
| 6922 | } |
| 6923 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6924 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6927 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6928 | static void |
| 6929 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6930 | { |
| 6931 | const char *msg; |
| 6932 | const char *tail; |
| 6933 | |
| 6934 | tail = nullstr; |
| 6935 | msg = "parameter not set"; |
| 6936 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6937 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6938 | if (varflags & VSNUL) |
| 6939 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6940 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6941 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6942 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6943 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6944 | 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] | 6945 | } |
| 6946 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6947 | static char * |
| 6948 | subevalvar(char *start, char *str, int strloc, |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 6949 | int startloc, int varflags, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6950 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6951 | int subtype = varflags & VSTYPE; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6952 | int quotes = flag & QUOTES_ESC; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6953 | char *startp; |
| 6954 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6955 | char *rmesc, *rmescend; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6956 | long amount; |
| 6957 | int resetloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6958 | int argstr_flags; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6959 | IF_BASH_PATTERN_SUBST(int workloc;) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6960 | IF_BASH_PATTERN_SUBST(int slash_pos;) |
| 6961 | IF_BASH_PATTERN_SUBST(char *repl;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6962 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6963 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6964 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6965 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6966 | //bb_error_msg("subevalvar(start:'%s',str:'%s',strloc:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 6967 | // start, str, strloc, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6968 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6969 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 6970 | /* For "${v/pattern/repl}", we must find the delimiter _before_ |
| 6971 | * argstr() call expands possible variable references in pattern: |
| 6972 | * think about "v=a; a=a/; echo ${v/$a/r}" case. |
| 6973 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6974 | repl = NULL; |
| 6975 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 6976 | /* Find '/' and replace with NUL */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6977 | repl = start; |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 6978 | /* The pattern can't be empty. |
| 6979 | * IOW: if the first char after "${v//" is a slash, |
| 6980 | * it does not terminate the pattern - it's the first char of the pattern: |
| 6981 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 6982 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 6983 | */ |
| 6984 | if (*repl == '/') |
| 6985 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6986 | for (;;) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6987 | if (*repl == '\0') { |
| 6988 | repl = NULL; |
| 6989 | break; |
| 6990 | } |
| 6991 | if (*repl == '/') { |
| 6992 | *repl = '\0'; |
| 6993 | break; |
| 6994 | } |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 6995 | /* 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] | 6996 | if ((unsigned char)*repl == CTLESC && repl[1]) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6997 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6998 | repl++; |
| 6999 | } |
| 7000 | } |
| 7001 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7002 | argstr_flags = (flag & EXP_DISCARD) | EXP_TILDE; |
| 7003 | if (!str |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7004 | #if BASH_SUBSTR |
| 7005 | && subtype != VSSUBSTR |
| 7006 | #endif |
| 7007 | ) { |
| 7008 | /* EXP_CASE keeps CTLESC's */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7009 | argstr_flags |= EXP_CASE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7010 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7011 | p = argstr(start, argstr_flags); |
| 7012 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7013 | //bb_error_msg("str0:'%s'", (char *)stackblock() + strloc); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7014 | #if BASH_PATTERN_SUBST |
| 7015 | slash_pos = -1; |
| 7016 | if (repl) { |
| 7017 | slash_pos = expdest - ((char *)stackblock() + strloc); |
Denys Vlasenko | 883cdb7 | 2021-01-09 08:27:37 +0100 | [diff] [blame^] | 7018 | if (!(flag & EXP_DISCARD)) |
| 7019 | STPUTC('/', expdest); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7020 | //bb_error_msg("repl+1:'%s'", repl + 1); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7021 | 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] | 7022 | *repl = '/'; |
| 7023 | } |
| 7024 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7025 | if (flag & EXP_DISCARD) |
| 7026 | return p; |
| 7027 | |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7028 | startp = (char *)stackblock() + startloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7029 | //bb_error_msg("str1:'%s'", (char *)stackblock() + strloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7030 | |
| 7031 | switch (subtype) { |
| 7032 | case VSASSIGN: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7033 | setvar0(str, startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7034 | |
| 7035 | loc = startp; |
| 7036 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7037 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7038 | case VSQUESTION: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7039 | varunset(start, str, startp, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7040 | /* NOTREACHED */ |
| 7041 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7042 | #if BASH_SUBSTR |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7043 | case VSSUBSTR: { |
| 7044 | int pos, len, orig_len; |
| 7045 | char *colon; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7046 | char *vstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7047 | |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7048 | loc = vstr = stackblock() + strloc; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7049 | |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7050 | /* Read POS in ${var:POS:LEN} */ |
| 7051 | colon = strchr(loc, ':'); |
| 7052 | if (colon) *colon = '\0'; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7053 | pos = substr_atoi(loc); |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7054 | if (colon) *colon = ':'; |
| 7055 | |
| 7056 | /* Read LEN in ${var:POS:LEN} */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7057 | len = vstr - startp - 1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7058 | /* *loc != '\0', guaranteed by parser */ |
| 7059 | if (quotes) { |
| 7060 | char *ptr; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7061 | /* Adjust the length by the number of escapes */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7062 | for (ptr = startp; ptr < (vstr - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7063 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7064 | len--; |
| 7065 | ptr++; |
| 7066 | } |
| 7067 | } |
| 7068 | } |
| 7069 | orig_len = len; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7070 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7071 | /* ${var::LEN} */ |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7072 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7073 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7074 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7075 | len = orig_len; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7076 | while (*loc && *loc != ':') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7077 | loc++; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7078 | if (*loc++ == ':') |
| 7079 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7080 | } |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7081 | if (pos < 0) { |
| 7082 | /* ${VAR:$((-n)):l} starts n chars from the end */ |
| 7083 | pos = orig_len + pos; |
| 7084 | } |
| 7085 | if ((unsigned)pos >= orig_len) { |
| 7086 | /* apart from obvious ${VAR:999999:l}, |
| 7087 | * covers ${VAR:$((-9999999)):l} - result is "" |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7088 | * (bash compat) |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7089 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7090 | pos = 0; |
| 7091 | len = 0; |
| 7092 | } |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7093 | if (len < 0) { |
| 7094 | /* ${VAR:N:-M} sets LEN to strlen()-M */ |
| 7095 | len = (orig_len - pos) + len; |
| 7096 | } |
| 7097 | if ((unsigned)len > (orig_len - pos)) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7098 | len = orig_len - pos; |
| 7099 | |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7100 | for (vstr = startp; pos; vstr++, pos--) { |
| 7101 | if (quotes && (unsigned char)*vstr == CTLESC) |
| 7102 | vstr++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7103 | } |
| 7104 | for (loc = startp; len; len--) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7105 | if (quotes && (unsigned char)*vstr == CTLESC) |
| 7106 | *loc++ = *vstr++; |
| 7107 | *loc++ = *vstr++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7108 | } |
| 7109 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7110 | goto out; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7111 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7112 | #endif /* BASH_SUBSTR */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7113 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7114 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7115 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7116 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7117 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7118 | repl = NULL; |
| 7119 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7120 | /* We'll comeback here if we grow the stack while handling |
| 7121 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 7122 | * stack will need rebasing, and we'll need to remove our work |
| 7123 | * areas each time |
| 7124 | */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7125 | restart: |
| 7126 | #endif |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7127 | |
| 7128 | amount = expdest - ((char *)stackblock() + resetloc); |
| 7129 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7130 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7131 | |
| 7132 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7133 | rmescend = (char *)stackblock() + strloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7134 | //bb_error_msg("str7:'%s'", rmescend); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7135 | if (quotes) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7136 | //TODO: how to handle slash_pos here if string changes (shortens?) |
| 7137 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7138 | if (rmesc != startp) { |
| 7139 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7140 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7141 | } |
| 7142 | } |
| 7143 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7144 | str = (char *)stackblock() + strloc; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7145 | /* |
| 7146 | * Example: v='a\bc'; echo ${v/\\b/_\\_\z_} |
| 7147 | * The result is a_\_z_c (not a\_\_z_c)! |
| 7148 | * |
| 7149 | * The search pattern and replace string treat backslashes differently! |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7150 | * "&slash_pos" causes rmescapes() to work differently on the pattern |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7151 | * and string. It's only used on the first call. |
| 7152 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7153 | //bb_error_msg("str8:'%s' slash_pos:%d", str, slash_pos); |
| 7154 | rmescapes(str, RMESCAPE_GLOB, |
| 7155 | repl ? NULL : (slash_pos < 0 ? NULL : &slash_pos) |
| 7156 | ); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7157 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7158 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 7159 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7160 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7161 | int len; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7162 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7163 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 7164 | if (!repl) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7165 | //bb_error_msg("str9:'%s' slash_pos:%d", str, slash_pos); |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 7166 | repl = nullstr; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7167 | if (slash_pos >= 0) { |
| 7168 | repl = str + slash_pos; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7169 | *repl++ = '\0'; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7170 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7171 | } |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7172 | //bb_error_msg("str:'%s' repl:'%s'", str, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7173 | |
| 7174 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7175 | if (str[0] == '\0') |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7176 | goto out1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7177 | |
| 7178 | len = 0; |
| 7179 | idx = startp; |
| 7180 | end = str - 1; |
| 7181 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7182 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7183 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7184 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7185 | if (!loc) { |
| 7186 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7187 | char *restart_detect = stackblock(); |
| 7188 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7189 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7190 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7191 | idx++; |
| 7192 | len++; |
| 7193 | STPUTC(*idx, expdest); |
| 7194 | } |
| 7195 | if (stackblock() != restart_detect) |
| 7196 | goto restart; |
| 7197 | idx++; |
| 7198 | len++; |
| 7199 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7200 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 7201 | if (idx >= end) |
| 7202 | break; |
| 7203 | if (str[0] == '*') { |
| 7204 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 7205 | * it would never match "ong_string" etc, no point in trying. |
| 7206 | */ |
| 7207 | goto skip_matching; |
| 7208 | } |
| 7209 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | if (subtype == VSREPLACEALL) { |
| 7213 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7214 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7215 | idx++; |
| 7216 | idx++; |
| 7217 | rmesc++; |
| 7218 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7219 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7220 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7221 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7222 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7223 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7224 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7225 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7226 | if (quotes && *loc == '\\') { |
| 7227 | STPUTC(CTLESC, expdest); |
| 7228 | len++; |
| 7229 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7230 | STPUTC(*loc, expdest); |
| 7231 | if (stackblock() != restart_detect) |
| 7232 | goto restart; |
| 7233 | len++; |
| 7234 | } |
| 7235 | |
| 7236 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 7237 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7238 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7239 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7240 | STPUTC(*idx, expdest); |
| 7241 | if (stackblock() != restart_detect) |
| 7242 | goto restart; |
| 7243 | len++; |
| 7244 | idx++; |
| 7245 | } |
| 7246 | break; |
| 7247 | } |
| 7248 | } |
| 7249 | |
| 7250 | /* We've put the replaced text into a buffer at workloc, now |
| 7251 | * move it to the right place and adjust the stack. |
| 7252 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7253 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7254 | startp = (char *)stackblock() + startloc; |
| 7255 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7256 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7257 | loc = startp + len; |
| 7258 | goto out; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7259 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7260 | #endif /* BASH_PATTERN_SUBST */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7261 | |
| 7262 | subtype -= VSTRIMRIGHT; |
| 7263 | #if DEBUG |
| 7264 | if (subtype < 0 || subtype > 7) |
| 7265 | abort(); |
| 7266 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7267 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7268 | zero = subtype >> 1; |
| 7269 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 7270 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 7271 | |
| 7272 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 7273 | if (loc) { |
| 7274 | if (zero) { |
| 7275 | memmove(startp, loc, str - loc); |
| 7276 | loc = startp + (str - loc) - 1; |
| 7277 | } |
| 7278 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7279 | } else |
| 7280 | loc = str - 1; |
| 7281 | |
| 7282 | out: |
| 7283 | amount = loc - expdest; |
| 7284 | STADJUST(amount, expdest); |
Cristian Ionescu-Idbohrn | ddfdf68 | 2020-11-18 10:41:14 +0100 | [diff] [blame] | 7285 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7286 | out1: |
Cristian Ionescu-Idbohrn | ddfdf68 | 2020-11-18 10:41:14 +0100 | [diff] [blame] | 7287 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7288 | /* Remove any recorded regions beyond start of variable */ |
| 7289 | removerecordregions(startloc); |
| 7290 | |
| 7291 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
| 7294 | /* |
| 7295 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7296 | * name parameter (examples): |
| 7297 | * ash -c 'echo $1' name:'1=' |
| 7298 | * ash -c 'echo $qwe' name:'qwe=' |
| 7299 | * ash -c 'echo $$' name:'$=' |
| 7300 | * ash -c 'echo ${$}' name:'$=' |
| 7301 | * ash -c 'echo ${$##q}' name:'$=q' |
| 7302 | * ash -c 'echo ${#$}' name:'$=' |
| 7303 | * note: examples with bad shell syntax: |
| 7304 | * ash -c 'echo ${#$1}' name:'$=1' |
| 7305 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7306 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 7307 | static NOINLINE ssize_t |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7308 | varvalue(char *name, int varflags, int flags, int quoted) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7309 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7310 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7311 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7312 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7313 | ssize_t len = 0; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7314 | int sep; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7315 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7316 | int discard = (subtype == VSPLUS || subtype == VSLENGTH) | (flags & EXP_DISCARD); |
| 7317 | |
| 7318 | if (!subtype) { |
| 7319 | if (discard) |
| 7320 | return -1; |
| 7321 | |
| 7322 | raise_error_syntax("bad substitution"); |
| 7323 | } |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7324 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7325 | flags |= EXP_KEEPNUL; |
| 7326 | flags &= discard ? ~QUOTES_ESC : ~0; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7327 | sep = (flags & EXP_FULL) << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7328 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7329 | switch (*name) { |
| 7330 | case '$': |
| 7331 | num = rootpid; |
| 7332 | goto numvar; |
| 7333 | case '?': |
| 7334 | num = exitstatus; |
| 7335 | goto numvar; |
| 7336 | case '#': |
| 7337 | num = shellparam.nparam; |
| 7338 | goto numvar; |
| 7339 | case '!': |
| 7340 | num = backgndpid; |
| 7341 | if (num == 0) |
| 7342 | return -1; |
| 7343 | numvar: |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7344 | len = cvtnum(num, flags); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7345 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7346 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7347 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7348 | for (i = NOPTS - 1; i >= 0; i--) { |
Martijn Dekker | ad4e961 | 2018-03-31 18:15:59 +0200 | [diff] [blame] | 7349 | if (optlist[i] && optletters(i)) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7350 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7351 | len++; |
| 7352 | } |
| 7353 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7354 | check_1char_name: |
| 7355 | #if 0 |
| 7356 | /* handles cases similar to ${#$1} */ |
| 7357 | if (name[2] != '\0') |
| 7358 | raise_error_syntax("bad substitution"); |
| 7359 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7360 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7361 | case '@': |
| 7362 | if (quoted && sep) |
| 7363 | goto param; |
| 7364 | /* fall through */ |
| 7365 | case '*': { |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7366 | char **ap; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7367 | char sepc; |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7368 | char c; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7369 | |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7370 | /* We will set c to 0 or ~0 depending on whether |
| 7371 | * we're doing field splitting. We won't do field |
| 7372 | * splitting if either we're quoted or sep is zero. |
| 7373 | * |
| 7374 | * Instead of testing (quoted || !sep) the following |
| 7375 | * trick optimises away any branches by using the |
| 7376 | * fact that EXP_QUOTED (which is the only bit that |
| 7377 | * can be set in quoted) is the same as EXP_FULL << |
| 7378 | * CHAR_BIT (which is the only bit that can be set |
| 7379 | * in sep). |
| 7380 | */ |
| 7381 | #if EXP_QUOTED >> CHAR_BIT != EXP_FULL |
| 7382 | #error The following two lines expect EXP_QUOTED == EXP_FULL << CHAR_BIT |
| 7383 | #endif |
| 7384 | c = !((quoted | ~sep) & EXP_QUOTED) - 1; |
| 7385 | sep &= ~quoted; |
| 7386 | sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7387 | param: |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7388 | sepc = sep; |
Denys Vlasenko | 0dd8e45 | 2016-10-01 21:02:06 +0200 | [diff] [blame] | 7389 | ap = shellparam.p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7390 | if (!ap) |
| 7391 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7392 | while ((p = *ap++) != NULL) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7393 | len += strtodest(p, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7394 | |
| 7395 | if (*ap && sep) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7396 | len++; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7397 | memtodest(&sepc, 1, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7398 | } |
| 7399 | } |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7400 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7401 | } /* case '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7402 | case '0': |
| 7403 | case '1': |
| 7404 | case '2': |
| 7405 | case '3': |
| 7406 | case '4': |
| 7407 | case '5': |
| 7408 | case '6': |
| 7409 | case '7': |
| 7410 | case '8': |
| 7411 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 7412 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7413 | if (num < 0 || num > shellparam.nparam) |
| 7414 | return -1; |
| 7415 | p = num ? shellparam.p[num - 1] : arg0; |
| 7416 | goto value; |
| 7417 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7418 | /* NB: name has form "VAR=..." */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7419 | p = lookupvar(name); |
| 7420 | value: |
| 7421 | if (!p) |
| 7422 | return -1; |
| 7423 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7424 | len = strtodest(p, flags); |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7425 | #if ENABLE_UNICODE_SUPPORT |
| 7426 | if (subtype == VSLENGTH && len > 0) { |
| 7427 | reinit_unicode_for_ash(); |
| 7428 | if (unicode_status == UNICODE_ON) { |
Ron Yorston | 3e3bfb8 | 2016-03-18 11:29:19 +0000 | [diff] [blame] | 7429 | STADJUST(-len, expdest); |
| 7430 | discard = 0; |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7431 | len = unicode_strlen(p); |
| 7432 | } |
| 7433 | } |
| 7434 | #endif |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7435 | break; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7438 | if (discard) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7439 | STADJUST(-len, expdest); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7440 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7441 | return len; |
| 7442 | } |
| 7443 | |
| 7444 | /* |
| 7445 | * Expand a variable, and return a pointer to the next character in the |
| 7446 | * input string. |
| 7447 | */ |
| 7448 | static char * |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7449 | evalvar(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7450 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7451 | char varflags; |
| 7452 | char subtype; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7453 | char *var; |
| 7454 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7455 | int startloc; |
| 7456 | ssize_t varlen; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7457 | int discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7458 | int quoted; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7459 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 7460 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7461 | subtype = varflags & VSTYPE; |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 7462 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7463 | quoted = flag & EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7464 | var = p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7465 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 7466 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7467 | |
| 7468 | again: |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7469 | varlen = varvalue(var, varflags, flag, quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7470 | if (varflags & VSNUL) |
| 7471 | varlen--; |
| 7472 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7473 | discard = varlen < 0 ? EXP_DISCARD : 0; |
| 7474 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7475 | switch (subtype) { |
| 7476 | case VSPLUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7477 | discard ^= EXP_DISCARD; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7478 | /* fall through */ |
| 7479 | case 0: |
| 7480 | case VSMINUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7481 | p = argstr(p, flag | EXP_TILDE | EXP_WORD | (discard ^ EXP_DISCARD)); |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7482 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7483 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7484 | case VSASSIGN: |
| 7485 | case VSQUESTION: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7486 | p = subevalvar(p, var, 0, startloc, varflags, |
| 7487 | (flag & ~QUOTES_ESC) | (discard ^ EXP_DISCARD)); |
| 7488 | |
| 7489 | if ((flag | ~discard) & EXP_DISCARD) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7490 | goto record; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7491 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7492 | varflags &= ~VSNUL; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7493 | subtype = VSNORMAL; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7494 | goto again; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7497 | if ((discard & ~flag) && uflag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7498 | varunset(p, var, 0, 0); |
| 7499 | |
| 7500 | if (subtype == VSLENGTH) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7501 | p++; |
| 7502 | if (flag & EXP_DISCARD) |
| 7503 | return p; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7504 | cvtnum(varlen > 0 ? varlen : 0, flag); |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7505 | goto really_record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7508 | if (subtype == VSNORMAL) |
| 7509 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7510 | |
| 7511 | #if DEBUG |
| 7512 | switch (subtype) { |
| 7513 | case VSTRIMLEFT: |
| 7514 | case VSTRIMLEFTMAX: |
| 7515 | case VSTRIMRIGHT: |
| 7516 | case VSTRIMRIGHTMAX: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7517 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7518 | case VSSUBSTR: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7519 | #endif |
| 7520 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7521 | case VSREPLACE: |
| 7522 | case VSREPLACEALL: |
| 7523 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7524 | break; |
| 7525 | default: |
| 7526 | abort(); |
| 7527 | } |
| 7528 | #endif |
| 7529 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7530 | flag |= discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7531 | if (!(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7532 | /* |
| 7533 | * Terminate the string and start recording the pattern |
| 7534 | * right after it |
| 7535 | */ |
| 7536 | STPUTC('\0', expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7539 | patloc = expdest - (char *)stackblock(); |
| 7540 | p = subevalvar(p, NULL, patloc, startloc, varflags, flag); |
Denys Vlasenko | 4ace385 | 2020-02-16 18:42:50 +0100 | [diff] [blame] | 7541 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7542 | record: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7543 | if ((flag | discard) & EXP_DISCARD) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7544 | return p; |
| 7545 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7546 | really_record: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7547 | if (quoted) { |
| 7548 | quoted = *var == '@' && shellparam.nparam; |
| 7549 | if (!quoted) |
| 7550 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7551 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7552 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7553 | return p; |
| 7554 | } |
| 7555 | |
| 7556 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7557 | * Add a file name to the list. |
| 7558 | */ |
| 7559 | static void |
| 7560 | addfname(const char *name) |
| 7561 | { |
| 7562 | struct strlist *sp; |
| 7563 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7564 | sp = stzalloc(sizeof(*sp)); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 7565 | sp->text = sstrdup(name); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7566 | *exparg.lastp = sp; |
| 7567 | exparg.lastp = &sp->next; |
| 7568 | } |
| 7569 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7570 | /* Avoid glob() (and thus, stat() et al) for words like "echo" */ |
| 7571 | static int |
| 7572 | hasmeta(const char *p) |
| 7573 | { |
| 7574 | static const char chars[] ALIGN1 = { |
| 7575 | '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0 |
| 7576 | }; |
| 7577 | |
| 7578 | for (;;) { |
| 7579 | p = strpbrk(p, chars); |
| 7580 | if (!p) |
| 7581 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7582 | switch ((unsigned char)*p) { |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7583 | case CTLQUOTEMARK: |
| 7584 | for (;;) { |
| 7585 | p++; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7586 | if ((unsigned char)*p == CTLQUOTEMARK) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7587 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7588 | if ((unsigned char)*p == CTLESC) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7589 | p++; |
| 7590 | if (*p == '\0') /* huh? */ |
| 7591 | return 0; |
| 7592 | } |
| 7593 | break; |
| 7594 | case '\\': |
| 7595 | case CTLESC: |
| 7596 | p++; |
| 7597 | if (*p == '\0') |
| 7598 | return 0; |
| 7599 | break; |
| 7600 | case '[': |
| 7601 | if (!strchr(p + 1, ']')) { |
| 7602 | /* It's not a properly closed [] pattern, |
| 7603 | * but other metas may follow. Continue checking. |
| 7604 | * my[file* _is_ globbed by bash |
| 7605 | * and matches filenames like "my[file1". |
| 7606 | */ |
| 7607 | break; |
| 7608 | } |
| 7609 | /* fallthrough */ |
| 7610 | default: |
| 7611 | /* case '*': */ |
| 7612 | /* case '?': */ |
| 7613 | return 1; |
| 7614 | } |
| 7615 | p++; |
| 7616 | } |
| 7617 | |
| 7618 | return 0; |
| 7619 | } |
| 7620 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7621 | /* If we want to use glob() from libc... */ |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7622 | #if !ENABLE_ASH_INTERNAL_GLOB |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7623 | |
| 7624 | /* Add the result of glob() to the list */ |
| 7625 | static void |
| 7626 | addglob(const glob_t *pglob) |
| 7627 | { |
| 7628 | char **p = pglob->gl_pathv; |
| 7629 | |
| 7630 | do { |
| 7631 | addfname(*p); |
| 7632 | } while (*++p); |
| 7633 | } |
| 7634 | static void |
| 7635 | expandmeta(struct strlist *str /*, int flag*/) |
| 7636 | { |
| 7637 | /* TODO - EXP_REDIR */ |
| 7638 | |
| 7639 | while (str) { |
| 7640 | char *p; |
| 7641 | glob_t pglob; |
| 7642 | int i; |
| 7643 | |
| 7644 | if (fflag) |
| 7645 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7646 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7647 | if (!hasmeta(str->text)) |
| 7648 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7649 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7650 | INT_OFF; |
| 7651 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7652 | // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match |
| 7653 | // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?) |
| 7654 | // |
| 7655 | // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping: |
| 7656 | // if you pass it "file\?", it returns "file\?", not "file?", if no match. |
| 7657 | // Which means you need to unescape the string, right? Not so fast: |
| 7658 | // if there _is_ a file named "file\?" (with backslash), it is returned |
| 7659 | // as "file\?" too (whichever pattern you used to find it, say, "file*"). |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7660 | // 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] | 7661 | // |
| 7662 | // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?", |
| 7663 | // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file. |
| 7664 | // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match). |
| 7665 | // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly. |
| 7666 | // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob); |
| 7667 | // i = glob(p, GLOB_NOMAGIC, NULL, &pglob); |
| 7668 | i = glob(p, 0, NULL, &pglob); |
| 7669 | //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] | 7670 | if (p != str->text) |
| 7671 | free(p); |
| 7672 | switch (i) { |
| 7673 | case 0: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7674 | #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] | 7675 | /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */ |
| 7676 | if (!(pglob.gl_flags & GLOB_MAGCHAR)) |
| 7677 | goto nometa2; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7678 | #endif |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7679 | addglob(&pglob); |
| 7680 | globfree(&pglob); |
| 7681 | INT_ON; |
| 7682 | break; |
| 7683 | case GLOB_NOMATCH: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7684 | //nometa2: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7685 | globfree(&pglob); |
| 7686 | INT_ON; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7687 | nometa: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7688 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7689 | rmescapes(str->text, 0, NULL); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7690 | exparg.lastp = &str->next; |
| 7691 | break; |
| 7692 | default: /* GLOB_NOSPACE */ |
| 7693 | globfree(&pglob); |
| 7694 | INT_ON; |
| 7695 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 7696 | } |
| 7697 | str = str->next; |
| 7698 | } |
| 7699 | } |
| 7700 | |
| 7701 | #else |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7702 | /* 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] | 7703 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7704 | /* |
| 7705 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 7706 | */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7707 | typedef struct exp_t { |
| 7708 | char *dir; |
| 7709 | unsigned dir_max; |
| 7710 | } exp_t; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7711 | static void |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7712 | expmeta(exp_t *exp, char *name, unsigned name_len, unsigned expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7713 | { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7714 | #define expdir exp->dir |
| 7715 | #define expdir_max exp->dir_max |
| 7716 | char *enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7717 | char *p; |
| 7718 | const char *cp; |
| 7719 | char *start; |
| 7720 | char *endname; |
| 7721 | int metaflag; |
| 7722 | struct stat statb; |
| 7723 | DIR *dirp; |
| 7724 | struct dirent *dp; |
| 7725 | int atend; |
| 7726 | int matchdot; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7727 | int esc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7728 | |
| 7729 | metaflag = 0; |
| 7730 | start = name; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7731 | for (p = name; esc = 0, *p; p += esc + 1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7732 | if (*p == '*' || *p == '?') |
| 7733 | metaflag = 1; |
| 7734 | else if (*p == '[') { |
| 7735 | char *q = p + 1; |
| 7736 | if (*q == '!') |
| 7737 | q++; |
| 7738 | for (;;) { |
| 7739 | if (*q == '\\') |
| 7740 | q++; |
| 7741 | if (*q == '/' || *q == '\0') |
| 7742 | break; |
| 7743 | if (*++q == ']') { |
| 7744 | metaflag = 1; |
| 7745 | break; |
| 7746 | } |
| 7747 | } |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7748 | } else { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7749 | if (*p == '\\' && p[1]) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7750 | esc++; |
| 7751 | if (p[esc] == '/') { |
| 7752 | if (metaflag) |
| 7753 | break; |
| 7754 | start = p + esc + 1; |
| 7755 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7756 | } |
| 7757 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7758 | if (metaflag == 0) { /* we've reached the end of the file name */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7759 | if (!expdir_len) |
| 7760 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7761 | p = name; |
| 7762 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7763 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7764 | p++; |
| 7765 | *enddir++ = *p; |
| 7766 | } while (*p++); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7767 | if (lstat(expdir, &statb) == 0) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7768 | addfname(expdir); |
| 7769 | return; |
| 7770 | } |
| 7771 | endname = p; |
| 7772 | if (name < start) { |
| 7773 | p = name; |
| 7774 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7775 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7776 | p++; |
| 7777 | *enddir++ = *p++; |
| 7778 | } while (p < start); |
| 7779 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7780 | *enddir = '\0'; |
| 7781 | cp = expdir; |
| 7782 | expdir_len = enddir - cp; |
| 7783 | if (!expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7784 | cp = "."; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7785 | dirp = opendir(cp); |
| 7786 | if (dirp == NULL) |
| 7787 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7788 | if (*endname == 0) { |
| 7789 | atend = 1; |
| 7790 | } else { |
| 7791 | atend = 0; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7792 | *endname = '\0'; |
| 7793 | endname += esc + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7794 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7795 | name_len -= endname - name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7796 | matchdot = 0; |
| 7797 | p = start; |
| 7798 | if (*p == '\\') |
| 7799 | p++; |
| 7800 | if (*p == '.') |
| 7801 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7802 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7803 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7804 | continue; |
| 7805 | if (pmatch(start, dp->d_name)) { |
| 7806 | if (atend) { |
| 7807 | strcpy(enddir, dp->d_name); |
| 7808 | addfname(expdir); |
| 7809 | } else { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7810 | unsigned offset; |
| 7811 | unsigned len; |
| 7812 | |
| 7813 | p = stpcpy(enddir, dp->d_name); |
| 7814 | *p = '/'; |
| 7815 | |
| 7816 | offset = p - expdir + 1; |
| 7817 | len = offset + name_len + NAME_MAX; |
| 7818 | if (len > expdir_max) { |
| 7819 | len += PATH_MAX; |
| 7820 | expdir = ckrealloc(expdir, len); |
| 7821 | expdir_max = len; |
| 7822 | } |
| 7823 | |
| 7824 | expmeta(exp, endname, name_len, offset); |
| 7825 | enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7826 | } |
| 7827 | } |
| 7828 | } |
| 7829 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7830 | if (!atend) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7831 | endname[-esc - 1] = esc ? '\\' : '/'; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7832 | #undef expdir |
| 7833 | #undef expdir_max |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7834 | } |
| 7835 | |
| 7836 | static struct strlist * |
| 7837 | msort(struct strlist *list, int len) |
| 7838 | { |
| 7839 | struct strlist *p, *q = NULL; |
| 7840 | struct strlist **lpp; |
| 7841 | int half; |
| 7842 | int n; |
| 7843 | |
| 7844 | if (len <= 1) |
| 7845 | return list; |
| 7846 | half = len >> 1; |
| 7847 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7848 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7849 | q = p; |
| 7850 | p = p->next; |
| 7851 | } |
| 7852 | q->next = NULL; /* terminate first half of list */ |
| 7853 | q = msort(list, half); /* sort first half of list */ |
| 7854 | p = msort(p, len - half); /* sort second half */ |
| 7855 | lpp = &list; |
| 7856 | for (;;) { |
| 7857 | #if ENABLE_LOCALE_SUPPORT |
| 7858 | if (strcoll(p->text, q->text) < 0) |
| 7859 | #else |
| 7860 | if (strcmp(p->text, q->text) < 0) |
| 7861 | #endif |
| 7862 | { |
| 7863 | *lpp = p; |
| 7864 | lpp = &p->next; |
| 7865 | p = *lpp; |
| 7866 | if (p == NULL) { |
| 7867 | *lpp = q; |
| 7868 | break; |
| 7869 | } |
| 7870 | } else { |
| 7871 | *lpp = q; |
| 7872 | lpp = &q->next; |
| 7873 | q = *lpp; |
| 7874 | if (q == NULL) { |
| 7875 | *lpp = p; |
| 7876 | break; |
| 7877 | } |
| 7878 | } |
| 7879 | } |
| 7880 | return list; |
| 7881 | } |
| 7882 | |
| 7883 | /* |
| 7884 | * Sort the results of file name expansion. It calculates the number of |
| 7885 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7886 | * work. |
| 7887 | */ |
| 7888 | static struct strlist * |
| 7889 | expsort(struct strlist *str) |
| 7890 | { |
| 7891 | int len; |
| 7892 | struct strlist *sp; |
| 7893 | |
| 7894 | len = 0; |
| 7895 | for (sp = str; sp; sp = sp->next) |
| 7896 | len++; |
| 7897 | return msort(str, len); |
| 7898 | } |
| 7899 | |
| 7900 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7901 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7902 | { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7903 | /* TODO - EXP_REDIR */ |
| 7904 | |
| 7905 | while (str) { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7906 | exp_t exp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7907 | struct strlist **savelastp; |
| 7908 | struct strlist *sp; |
| 7909 | char *p; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7910 | unsigned len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7911 | |
| 7912 | if (fflag) |
| 7913 | goto nometa; |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7914 | if (!hasmeta(str->text)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7915 | goto nometa; |
| 7916 | savelastp = exparg.lastp; |
| 7917 | |
| 7918 | INT_OFF; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7919 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7920 | len = strlen(p); |
| 7921 | exp.dir_max = len + PATH_MAX; |
| 7922 | exp.dir = ckmalloc(exp.dir_max); |
| 7923 | |
| 7924 | expmeta(&exp, p, len, 0); |
| 7925 | free(exp.dir); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7926 | if (p != str->text) |
| 7927 | free(p); |
| 7928 | INT_ON; |
| 7929 | if (exparg.lastp == savelastp) { |
| 7930 | /* |
| 7931 | * no matches |
| 7932 | */ |
| 7933 | nometa: |
| 7934 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7935 | rmescapes(str->text, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7936 | exparg.lastp = &str->next; |
| 7937 | } else { |
| 7938 | *exparg.lastp = NULL; |
| 7939 | *savelastp = sp = expsort(*savelastp); |
| 7940 | while (sp->next != NULL) |
| 7941 | sp = sp->next; |
| 7942 | exparg.lastp = &sp->next; |
| 7943 | } |
| 7944 | str = str->next; |
| 7945 | } |
| 7946 | } |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7947 | #endif /* ENABLE_ASH_INTERNAL_GLOB */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7948 | |
| 7949 | /* |
| 7950 | * Perform variable substitution and command substitution on an argument, |
| 7951 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7952 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7953 | * here document expansion. |
| 7954 | */ |
| 7955 | static void |
| 7956 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7957 | { |
| 7958 | struct strlist *sp; |
| 7959 | char *p; |
| 7960 | |
| 7961 | argbackq = arg->narg.backquote; |
| 7962 | STARTSTACKSTR(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7963 | TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag)); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7964 | argstr(arg->narg.text, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7965 | if (arglist == NULL) { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 7966 | /* here document expanded */ |
| 7967 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7968 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7969 | p = grabstackstr(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7970 | TRACE(("expandarg: p:'%s'\n", p)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7971 | exparg.lastp = &exparg.list; |
| 7972 | /* |
| 7973 | * TODO - EXP_REDIR |
| 7974 | */ |
| 7975 | if (flag & EXP_FULL) { |
| 7976 | ifsbreakup(p, &exparg); |
| 7977 | *exparg.lastp = NULL; |
| 7978 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7979 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7980 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7981 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7982 | sp->text = p; |
| 7983 | *exparg.lastp = sp; |
| 7984 | exparg.lastp = &sp->next; |
| 7985 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7986 | *exparg.lastp = NULL; |
| 7987 | if (exparg.list) { |
| 7988 | *arglist->lastp = exparg.list; |
| 7989 | arglist->lastp = exparg.lastp; |
| 7990 | } |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 7991 | |
| 7992 | out: |
| 7993 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | /* |
| 7997 | * Expand shell variables and backquotes inside a here document. |
| 7998 | */ |
| 7999 | static void |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 8000 | expandhere(union node *arg) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8001 | { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 8002 | expandarg(arg, (struct arglist *)NULL, EXP_QUOTED); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8003 | } |
| 8004 | |
| 8005 | /* |
| 8006 | * Returns true if the pattern matches the string. |
| 8007 | */ |
| 8008 | static int |
| 8009 | patmatch(char *pattern, const char *string) |
| 8010 | { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8011 | char *p = preglob(pattern, 0); |
Denys Vlasenko | 4476c70 | 2017-08-15 15:27:41 +0200 | [diff] [blame] | 8012 | int r = pmatch(p, string); |
| 8013 | //bb_error_msg("!fnmatch(pattern:'%s',str:'%s',0):%d", p, string, r); |
| 8014 | return r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8015 | } |
| 8016 | |
| 8017 | /* |
| 8018 | * See if a pattern matches in a case statement. |
| 8019 | */ |
| 8020 | static int |
| 8021 | casematch(union node *pattern, char *val) |
| 8022 | { |
| 8023 | struct stackmark smark; |
| 8024 | int result; |
| 8025 | |
| 8026 | setstackmark(&smark); |
| 8027 | argbackq = pattern->narg.backquote; |
| 8028 | STARTSTACKSTR(expdest); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 8029 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 8030 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8031 | result = patmatch(stackblock(), val); |
| 8032 | popstackmark(&smark); |
| 8033 | return result; |
| 8034 | } |
| 8035 | |
| 8036 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8037 | /* ============ find_command */ |
| 8038 | |
| 8039 | struct builtincmd { |
| 8040 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8041 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8042 | /* unsigned flags; */ |
| 8043 | }; |
| 8044 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 8045 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8046 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8047 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8048 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8049 | |
| 8050 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8051 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8052 | union param { |
| 8053 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8054 | /* index >= 0 for commands without path (slashes) */ |
| 8055 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 8056 | /* index == -1 for commands with slashes */ |
| 8057 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8058 | const struct builtincmd *cmd; |
| 8059 | struct funcnode *func; |
| 8060 | } u; |
| 8061 | }; |
| 8062 | /* values of cmdtype */ |
| 8063 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 8064 | #define CMDNORMAL 0 /* command is an executable program */ |
| 8065 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 8066 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 8067 | |
| 8068 | /* action to find_command() */ |
| 8069 | #define DO_ERR 0x01 /* prints errors */ |
| 8070 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 8071 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 8072 | #define DO_ALTPATH 0x08 /* using alternate path */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8073 | #define DO_REGBLTIN 0x10 /* regular built-ins and functions only */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8074 | |
| 8075 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 8076 | |
| 8077 | |
| 8078 | /* ============ Hashing commands */ |
| 8079 | |
| 8080 | /* |
| 8081 | * When commands are first encountered, they are entered in a hash table. |
| 8082 | * This ensures that a full path search will not have to be done for them |
| 8083 | * on each invocation. |
| 8084 | * |
| 8085 | * We should investigate converting to a linear search, even though that |
| 8086 | * would make the command name "hash" a misnomer. |
| 8087 | */ |
| 8088 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8089 | struct tblentry { |
| 8090 | struct tblentry *next; /* next entry in hash chain */ |
| 8091 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8092 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8093 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8094 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8095 | }; |
| 8096 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8097 | static struct tblentry **cmdtable; |
| 8098 | #define INIT_G_cmdtable() do { \ |
| 8099 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 8100 | } while (0) |
| 8101 | |
| 8102 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 8103 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8104 | |
| 8105 | static void |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8106 | 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] | 8107 | { |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8108 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8109 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8110 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 8111 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8112 | while (*envp) |
| 8113 | putenv(*envp++); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 8114 | popredir(/*drop:*/ 1); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8115 | run_noexec_applet_and_exit(applet_no, cmd, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8116 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8117 | /* re-exec ourselves with the new arguments */ |
| 8118 | execve(bb_busybox_exec_path, argv, envp); |
| 8119 | /* If they called chroot or otherwise made the binary no longer |
| 8120 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8121 | } |
| 8122 | #endif |
| 8123 | |
| 8124 | repeat: |
| 8125 | #ifdef SYSV |
| 8126 | do { |
| 8127 | execve(cmd, argv, envp); |
| 8128 | } while (errno == EINTR); |
| 8129 | #else |
| 8130 | execve(cmd, argv, envp); |
| 8131 | #endif |
Ron Yorston | ca82b53 | 2018-11-01 11:45:03 +0100 | [diff] [blame] | 8132 | |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8133 | if (cmd != bb_busybox_exec_path && errno == ENOEXEC) { |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8134 | /* Run "cmd" as a shell script: |
| 8135 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 8136 | * "If the execve() function fails with ENOEXEC, the shell |
| 8137 | * shall execute a command equivalent to having a shell invoked |
| 8138 | * with the command name as its first operand, |
| 8139 | * with any remaining arguments passed to the new shell" |
| 8140 | * |
| 8141 | * That is, do not use $SHELL, user's shell, or /bin/sh; |
| 8142 | * just call ourselves. |
Denys Vlasenko | 2bef526 | 2011-12-16 00:25:17 +0100 | [diff] [blame] | 8143 | * |
| 8144 | * Note that bash reads ~80 chars of the file, and if it sees |
| 8145 | * a zero byte before it sees newline, it doesn't try to |
| 8146 | * interpret it, but fails with "cannot execute binary file" |
Denys Vlasenko | cda6ea9 | 2011-12-16 00:44:36 +0100 | [diff] [blame] | 8147 | * message and exit code 126. For one, this prevents attempts |
| 8148 | * to interpret foreign ELF binaries as shell scripts. |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8149 | */ |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8150 | argv[0] = (char*) cmd; |
| 8151 | cmd = bb_busybox_exec_path; |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8152 | /* NB: this is only possible because all callers of shellexec() |
| 8153 | * ensure that the argv[-1] slot exists! |
| 8154 | */ |
| 8155 | argv--; |
| 8156 | argv[0] = (char*) "ash"; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8157 | goto repeat; |
| 8158 | } |
| 8159 | } |
| 8160 | |
| 8161 | /* |
| 8162 | * Exec a program. Never returns. If you change this routine, you may |
| 8163 | * have to change the find_command routine as well. |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8164 | * argv[-1] must exist and be writable! See tryexec() for why. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8165 | */ |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8166 | static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN; |
| 8167 | static void shellexec(char *prog, char **argv, const char *path, int idx) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8168 | { |
| 8169 | char *cmdname; |
| 8170 | int e; |
| 8171 | char **envp; |
| 8172 | int exerrno; |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8173 | int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8174 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 8175 | envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL); |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8176 | if (strchr(prog, '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8177 | #if ENABLE_FEATURE_SH_STANDALONE |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8178 | || (applet_no = find_applet_by_name(prog)) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8179 | #endif |
| 8180 | ) { |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8181 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8182 | if (applet_no >= 0) { |
| 8183 | /* We tried execing ourself, but it didn't work. |
| 8184 | * Maybe /proc/self/exe doesn't exist? |
| 8185 | * Try $PATH search. |
| 8186 | */ |
| 8187 | goto try_PATH; |
| 8188 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8189 | e = errno; |
| 8190 | } else { |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8191 | try_PATH: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8192 | e = ENOENT; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8193 | while (padvance(&path, argv[0]) >= 0) { |
| 8194 | cmdname = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8195 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 8196 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8197 | if (errno != ENOENT && errno != ENOTDIR) |
| 8198 | e = errno; |
| 8199 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8200 | } |
| 8201 | } |
| 8202 | |
| 8203 | /* Map to POSIX errors */ |
| 8204 | switch (e) { |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8205 | default: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8206 | exerrno = 126; |
| 8207 | break; |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8208 | case ELOOP: |
| 8209 | case ENAMETOOLONG: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8210 | case ENOENT: |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8211 | case ENOTDIR: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8212 | exerrno = 127; |
| 8213 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8214 | } |
| 8215 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8216 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8217 | prog, e, suppress_int)); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 8218 | ash_msg_and_raise(EXEND, "%s: %s", prog, errmsg(e, "not found")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8219 | /* NOTREACHED */ |
| 8220 | } |
| 8221 | |
| 8222 | static void |
| 8223 | printentry(struct tblentry *cmdp) |
| 8224 | { |
| 8225 | int idx; |
| 8226 | const char *path; |
| 8227 | char *name; |
| 8228 | |
| 8229 | idx = cmdp->param.index; |
| 8230 | path = pathval(); |
| 8231 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8232 | padvance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8233 | } while (--idx >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8234 | name = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8235 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 8236 | } |
| 8237 | |
| 8238 | /* |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8239 | * Clear out command entries. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8240 | */ |
| 8241 | static void |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8242 | clearcmdentry(void) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8243 | { |
| 8244 | struct tblentry **tblp; |
| 8245 | struct tblentry **pp; |
| 8246 | struct tblentry *cmdp; |
| 8247 | |
| 8248 | INT_OFF; |
| 8249 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 8250 | pp = tblp; |
| 8251 | while ((cmdp = *pp) != NULL) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8252 | if (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8253 | || (cmdp->cmdtype == CMDBUILTIN |
| 8254 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8255 | && builtinloc > 0 |
| 8256 | ) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8257 | ) { |
| 8258 | *pp = cmdp->next; |
| 8259 | free(cmdp); |
| 8260 | } else { |
| 8261 | pp = &cmdp->next; |
| 8262 | } |
| 8263 | } |
| 8264 | } |
| 8265 | INT_ON; |
| 8266 | } |
| 8267 | |
| 8268 | /* |
| 8269 | * Locate a command in the command hash table. If "add" is nonzero, |
| 8270 | * add the command to the table if it is not already present. The |
| 8271 | * variable "lastcmdentry" is set to point to the address of the link |
| 8272 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 8273 | * entry. |
| 8274 | * |
| 8275 | * Interrupts must be off if called with add != 0. |
| 8276 | */ |
| 8277 | static struct tblentry **lastcmdentry; |
| 8278 | |
| 8279 | static struct tblentry * |
| 8280 | cmdlookup(const char *name, int add) |
| 8281 | { |
| 8282 | unsigned int hashval; |
| 8283 | const char *p; |
| 8284 | struct tblentry *cmdp; |
| 8285 | struct tblentry **pp; |
| 8286 | |
| 8287 | p = name; |
| 8288 | hashval = (unsigned char)*p << 4; |
| 8289 | while (*p) |
| 8290 | hashval += (unsigned char)*p++; |
| 8291 | hashval &= 0x7FFF; |
| 8292 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 8293 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8294 | if (strcmp(cmdp->cmdname, name) == 0) |
| 8295 | break; |
| 8296 | pp = &cmdp->next; |
| 8297 | } |
| 8298 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8299 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 8300 | + strlen(name) |
| 8301 | /* + 1 - already done because |
| 8302 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 8303 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8304 | cmdp->cmdtype = CMDUNKNOWN; |
| 8305 | strcpy(cmdp->cmdname, name); |
| 8306 | } |
| 8307 | lastcmdentry = pp; |
| 8308 | return cmdp; |
| 8309 | } |
| 8310 | |
| 8311 | /* |
| 8312 | * Delete the command entry returned on the last lookup. |
| 8313 | */ |
| 8314 | static void |
| 8315 | delete_cmd_entry(void) |
| 8316 | { |
| 8317 | struct tblentry *cmdp; |
| 8318 | |
| 8319 | INT_OFF; |
| 8320 | cmdp = *lastcmdentry; |
| 8321 | *lastcmdentry = cmdp->next; |
| 8322 | if (cmdp->cmdtype == CMDFUNCTION) |
| 8323 | freefunc(cmdp->param.func); |
| 8324 | free(cmdp); |
| 8325 | INT_ON; |
| 8326 | } |
| 8327 | |
| 8328 | /* |
| 8329 | * Add a new command entry, replacing any existing command entry for |
| 8330 | * the same name - except special builtins. |
| 8331 | */ |
| 8332 | static void |
| 8333 | addcmdentry(char *name, struct cmdentry *entry) |
| 8334 | { |
| 8335 | struct tblentry *cmdp; |
| 8336 | |
| 8337 | cmdp = cmdlookup(name, 1); |
| 8338 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 8339 | freefunc(cmdp->param.func); |
| 8340 | } |
| 8341 | cmdp->cmdtype = entry->cmdtype; |
| 8342 | cmdp->param = entry->u; |
| 8343 | cmdp->rehash = 0; |
| 8344 | } |
| 8345 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8346 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8347 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8348 | { |
| 8349 | struct tblentry **pp; |
| 8350 | struct tblentry *cmdp; |
| 8351 | int c; |
| 8352 | struct cmdentry entry; |
| 8353 | char *name; |
| 8354 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8355 | if (nextopt("r") != '\0') { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8356 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8357 | return 0; |
| 8358 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8359 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8360 | if (*argptr == NULL) { |
| 8361 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8362 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8363 | if (cmdp->cmdtype == CMDNORMAL) |
| 8364 | printentry(cmdp); |
| 8365 | } |
| 8366 | } |
| 8367 | return 0; |
| 8368 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8369 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8370 | c = 0; |
| 8371 | while ((name = *argptr) != NULL) { |
| 8372 | cmdp = cmdlookup(name, 0); |
| 8373 | if (cmdp != NULL |
| 8374 | && (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8375 | || (cmdp->cmdtype == CMDBUILTIN |
| 8376 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8377 | && builtinloc > 0 |
| 8378 | ) |
| 8379 | ) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8380 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8381 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8382 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8383 | find_command(name, &entry, DO_ERR, pathval()); |
| 8384 | if (entry.cmdtype == CMDUNKNOWN) |
| 8385 | c = 1; |
| 8386 | argptr++; |
| 8387 | } |
| 8388 | return c; |
| 8389 | } |
| 8390 | |
| 8391 | /* |
| 8392 | * Called when a cd is done. Marks all commands so the next time they |
| 8393 | * are executed they will be rehashed. |
| 8394 | */ |
| 8395 | static void |
| 8396 | hashcd(void) |
| 8397 | { |
| 8398 | struct tblentry **pp; |
| 8399 | struct tblentry *cmdp; |
| 8400 | |
| 8401 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8402 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8403 | if (cmdp->cmdtype == CMDNORMAL |
| 8404 | || (cmdp->cmdtype == CMDBUILTIN |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 8405 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8406 | && builtinloc > 0) |
| 8407 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8408 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8409 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8410 | } |
| 8411 | } |
| 8412 | } |
| 8413 | |
| 8414 | /* |
| 8415 | * Fix command hash table when PATH changed. |
| 8416 | * Called before PATH is changed. The argument is the new value of PATH; |
| 8417 | * pathval() still returns the old value at this point. |
| 8418 | * Called with interrupts off. |
| 8419 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8420 | static void FAST_FUNC |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8421 | changepath(const char *newval) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8422 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8423 | const char *new; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8424 | int idx; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8425 | int bltin; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8426 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8427 | new = newval; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8428 | idx = 0; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8429 | bltin = -1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8430 | for (;;) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8431 | if (*new == '%' && prefix(new + 1, "builtin")) { |
| 8432 | bltin = idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8433 | break; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8434 | } |
| 8435 | new = strchr(new, ':'); |
| 8436 | if (!new) |
| 8437 | break; |
| 8438 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 8439 | new++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8440 | } |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8441 | builtinloc = bltin; |
| 8442 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8443 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8444 | enum { |
| 8445 | TEOF, |
| 8446 | TNL, |
| 8447 | TREDIR, |
| 8448 | TWORD, |
| 8449 | TSEMI, |
| 8450 | TBACKGND, |
| 8451 | TAND, |
| 8452 | TOR, |
| 8453 | TPIPE, |
| 8454 | TLP, |
| 8455 | TRP, |
| 8456 | TENDCASE, |
| 8457 | TENDBQUOTE, |
| 8458 | TNOT, |
| 8459 | TCASE, |
| 8460 | TDO, |
| 8461 | TDONE, |
| 8462 | TELIF, |
| 8463 | TELSE, |
| 8464 | TESAC, |
| 8465 | TFI, |
| 8466 | TFOR, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8467 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8468 | TFUNCTION, |
| 8469 | #endif |
| 8470 | TIF, |
| 8471 | TIN, |
| 8472 | TTHEN, |
| 8473 | TUNTIL, |
| 8474 | TWHILE, |
| 8475 | TBEGIN, |
| 8476 | TEND |
| 8477 | }; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8478 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8479 | |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8480 | /* Nth bit indicates if token marks the end of a list */ |
| 8481 | enum { |
| 8482 | tokendlist = 0 |
| 8483 | /* 0 */ | (1u << TEOF) |
| 8484 | /* 1 */ | (0u << TNL) |
| 8485 | /* 2 */ | (0u << TREDIR) |
| 8486 | /* 3 */ | (0u << TWORD) |
| 8487 | /* 4 */ | (0u << TSEMI) |
| 8488 | /* 5 */ | (0u << TBACKGND) |
| 8489 | /* 6 */ | (0u << TAND) |
| 8490 | /* 7 */ | (0u << TOR) |
| 8491 | /* 8 */ | (0u << TPIPE) |
| 8492 | /* 9 */ | (0u << TLP) |
| 8493 | /* 10 */ | (1u << TRP) |
| 8494 | /* 11 */ | (1u << TENDCASE) |
| 8495 | /* 12 */ | (1u << TENDBQUOTE) |
| 8496 | /* 13 */ | (0u << TNOT) |
| 8497 | /* 14 */ | (0u << TCASE) |
| 8498 | /* 15 */ | (1u << TDO) |
| 8499 | /* 16 */ | (1u << TDONE) |
| 8500 | /* 17 */ | (1u << TELIF) |
| 8501 | /* 18 */ | (1u << TELSE) |
| 8502 | /* 19 */ | (1u << TESAC) |
| 8503 | /* 20 */ | (1u << TFI) |
| 8504 | /* 21 */ | (0u << TFOR) |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8505 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8506 | /* 22 */ | (0u << TFUNCTION) |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8507 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8508 | /* 23 */ | (0u << TIF) |
| 8509 | /* 24 */ | (0u << TIN) |
| 8510 | /* 25 */ | (1u << TTHEN) |
| 8511 | /* 26 */ | (0u << TUNTIL) |
| 8512 | /* 27 */ | (0u << TWHILE) |
| 8513 | /* 28 */ | (0u << TBEGIN) |
| 8514 | /* 29 */ | (1u << TEND) |
| 8515 | , /* thus far 29 bits used */ |
| 8516 | }; |
| 8517 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 8518 | static const char *const tokname_array[] ALIGN_PTR = { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8519 | "end of file", |
| 8520 | "newline", |
| 8521 | "redirection", |
| 8522 | "word", |
| 8523 | ";", |
| 8524 | "&", |
| 8525 | "&&", |
| 8526 | "||", |
| 8527 | "|", |
| 8528 | "(", |
| 8529 | ")", |
| 8530 | ";;", |
| 8531 | "`", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8532 | #define KWDOFFSET 13 |
| 8533 | /* the following are keywords */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8534 | "!", |
| 8535 | "case", |
| 8536 | "do", |
| 8537 | "done", |
| 8538 | "elif", |
| 8539 | "else", |
| 8540 | "esac", |
| 8541 | "fi", |
| 8542 | "for", |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8543 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8544 | "function", |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8545 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8546 | "if", |
| 8547 | "in", |
| 8548 | "then", |
| 8549 | "until", |
| 8550 | "while", |
| 8551 | "{", |
| 8552 | "}", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8553 | }; |
| 8554 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8555 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 8556 | static int |
| 8557 | pstrcmp(const void *a, const void *b) |
| 8558 | { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8559 | return strcmp((char*)a, *(char**)b); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | static const char *const * |
| 8563 | findkwd(const char *s) |
| 8564 | { |
| 8565 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8566 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 8567 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
| 8570 | /* |
| 8571 | * Locate and print what a word is... |
| 8572 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8573 | static int |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8574 | describe_command(char *command, const char *path, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8575 | { |
| 8576 | struct cmdentry entry; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8577 | #if ENABLE_ASH_ALIAS |
| 8578 | const struct alias *ap; |
| 8579 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8580 | |
| 8581 | path = path ? path : pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8582 | |
| 8583 | if (describe_command_verbose) { |
| 8584 | out1str(command); |
| 8585 | } |
| 8586 | |
| 8587 | /* First look at the keywords */ |
| 8588 | if (findkwd(command)) { |
| 8589 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 8590 | goto out; |
| 8591 | } |
| 8592 | |
| 8593 | #if ENABLE_ASH_ALIAS |
| 8594 | /* Then look at the aliases */ |
| 8595 | ap = lookupalias(command, 0); |
| 8596 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8597 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8598 | out1str("alias "); |
| 8599 | printalias(ap); |
| 8600 | return 0; |
| 8601 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8602 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8603 | goto out; |
| 8604 | } |
| 8605 | #endif |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8606 | /* Brute force */ |
| 8607 | find_command(command, &entry, DO_ABS, path); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8608 | |
| 8609 | switch (entry.cmdtype) { |
| 8610 | case CMDNORMAL: { |
| 8611 | int j = entry.u.index; |
| 8612 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8613 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8614 | p = command; |
| 8615 | } else { |
| 8616 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8617 | padvance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8618 | } while (--j >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8619 | p = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8620 | } |
| 8621 | if (describe_command_verbose) { |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8622 | out1fmt(" is %s", p); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8623 | } else { |
| 8624 | out1str(p); |
| 8625 | } |
| 8626 | break; |
| 8627 | } |
| 8628 | |
| 8629 | case CMDFUNCTION: |
| 8630 | if (describe_command_verbose) { |
Denys Vlasenko | 63c42af | 2018-07-24 17:08:04 +0200 | [diff] [blame] | 8631 | /*out1str(" is a shell function");*/ |
| 8632 | out1str(" is a function"); /* bash says this */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8633 | } else { |
| 8634 | out1str(command); |
| 8635 | } |
| 8636 | break; |
| 8637 | |
| 8638 | case CMDBUILTIN: |
| 8639 | if (describe_command_verbose) { |
| 8640 | out1fmt(" is a %sshell builtin", |
| 8641 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 8642 | "special " : nullstr |
| 8643 | ); |
| 8644 | } else { |
| 8645 | out1str(command); |
| 8646 | } |
| 8647 | break; |
| 8648 | |
| 8649 | default: |
| 8650 | if (describe_command_verbose) { |
| 8651 | out1str(": not found\n"); |
| 8652 | } |
| 8653 | return 127; |
| 8654 | } |
| 8655 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 8656 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8657 | return 0; |
| 8658 | } |
| 8659 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8660 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8661 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8662 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8663 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8664 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8665 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8666 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8667 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 8668 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8669 | i++; |
| 8670 | verbose = 0; |
| 8671 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8672 | while (argv[i]) { |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8673 | err |= describe_command(argv[i++], NULL, verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8674 | } |
| 8675 | return err; |
| 8676 | } |
| 8677 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8678 | static struct strlist * |
| 8679 | fill_arglist(struct arglist *arglist, union node **argpp) |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8680 | { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8681 | struct strlist **lastp = arglist->lastp; |
| 8682 | union node *argp; |
| 8683 | |
| 8684 | while ((argp = *argpp) != NULL) { |
| 8685 | expandarg(argp, arglist, EXP_FULL | EXP_TILDE); |
| 8686 | *argpp = argp->narg.next; |
| 8687 | if (*lastp) |
| 8688 | break; |
| 8689 | } |
| 8690 | |
| 8691 | return *lastp; |
| 8692 | } |
| 8693 | |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 8694 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8695 | /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */ |
| 8696 | static int |
| 8697 | parse_command_args(struct arglist *arglist, union node **argpp, const char **path) |
| 8698 | { |
| 8699 | struct strlist *sp = arglist->list; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8700 | char *cp, c; |
| 8701 | |
| 8702 | for (;;) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8703 | sp = sp->next ? sp->next : fill_arglist(arglist, argpp); |
| 8704 | if (!sp) |
| 8705 | return 0; |
| 8706 | cp = sp->text; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8707 | if (*cp++ != '-') |
| 8708 | break; |
| 8709 | c = *cp++; |
| 8710 | if (!c) |
| 8711 | break; |
| 8712 | if (c == '-' && !*cp) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8713 | if (!sp->next && !fill_arglist(arglist, argpp)) |
| 8714 | return 0; |
| 8715 | sp = sp->next; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8716 | break; |
| 8717 | } |
| 8718 | do { |
| 8719 | switch (c) { |
| 8720 | case 'p': |
| 8721 | *path = bb_default_path; |
| 8722 | break; |
| 8723 | default: |
| 8724 | /* run 'typecmd' for other options */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8725 | return 0; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8726 | } |
| 8727 | c = *cp++; |
| 8728 | } while (c); |
| 8729 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8730 | |
| 8731 | arglist->list = sp; |
| 8732 | return DO_NOFUNC; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8733 | } |
| 8734 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8735 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8736 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8737 | { |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8738 | char *cmd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8739 | int c; |
| 8740 | enum { |
| 8741 | VERIFY_BRIEF = 1, |
| 8742 | VERIFY_VERBOSE = 2, |
| 8743 | } verify = 0; |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8744 | const char *path = NULL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8745 | |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8746 | /* "command [-p] PROG ARGS" (that is, without -V or -v) |
| 8747 | * never reaches this function. |
| 8748 | */ |
| 8749 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8750 | while ((c = nextopt("pvV")) != '\0') |
| 8751 | if (c == 'V') |
| 8752 | verify |= VERIFY_VERBOSE; |
| 8753 | else if (c == 'v') |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8754 | /*verify |= VERIFY_BRIEF*/; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8755 | #if DEBUG |
| 8756 | else if (c != 'p') |
| 8757 | abort(); |
| 8758 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8759 | else |
| 8760 | path = bb_default_path; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8761 | |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 8762 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8763 | cmd = *argptr; |
| 8764 | if (/*verify && */ cmd) |
| 8765 | return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8766 | |
| 8767 | return 0; |
| 8768 | } |
| 8769 | #endif |
| 8770 | |
| 8771 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8772 | /*static int funcblocksize; // size of structures in function */ |
| 8773 | /*static int funcstringsize; // size of strings in node */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8774 | static void *funcblock; /* block to allocate function from */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8775 | static char *funcstring_end; /* end of block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8776 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 8777 | static const uint8_t nodesize[N_NUMBER] ALIGN1 = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8778 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 8779 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 8780 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8781 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8782 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8783 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8784 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8785 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8786 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 8787 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8788 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8789 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 8790 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 8791 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 8792 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8793 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8794 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8795 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8796 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8797 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8798 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8799 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8800 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8801 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8802 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8803 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8804 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8805 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8806 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8807 | }; |
| 8808 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8809 | static int calcsize(int funcblocksize, union node *n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8810 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8811 | static int |
| 8812 | sizenodelist(int funcblocksize, struct nodelist *lp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8813 | { |
| 8814 | while (lp) { |
| 8815 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8816 | funcblocksize = calcsize(funcblocksize, lp->n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8817 | lp = lp->next; |
| 8818 | } |
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 | } |
| 8821 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8822 | static int |
| 8823 | calcsize(int funcblocksize, union node *n) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8824 | { |
| 8825 | if (n == NULL) |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8826 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8827 | funcblocksize += nodesize[n->type]; |
| 8828 | switch (n->type) { |
| 8829 | case NCMD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8830 | funcblocksize = calcsize(funcblocksize, n->ncmd.redirect); |
| 8831 | funcblocksize = calcsize(funcblocksize, n->ncmd.args); |
| 8832 | funcblocksize = calcsize(funcblocksize, n->ncmd.assign); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8833 | break; |
| 8834 | case NPIPE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8835 | funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8836 | break; |
| 8837 | case NREDIR: |
| 8838 | case NBACKGND: |
| 8839 | case NSUBSHELL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8840 | funcblocksize = calcsize(funcblocksize, n->nredir.redirect); |
| 8841 | funcblocksize = calcsize(funcblocksize, n->nredir.n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8842 | break; |
| 8843 | case NAND: |
| 8844 | case NOR: |
| 8845 | case NSEMI: |
| 8846 | case NWHILE: |
| 8847 | case NUNTIL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8848 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch2); |
| 8849 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8850 | break; |
| 8851 | case NIF: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8852 | funcblocksize = calcsize(funcblocksize, n->nif.elsepart); |
| 8853 | funcblocksize = calcsize(funcblocksize, n->nif.ifpart); |
| 8854 | funcblocksize = calcsize(funcblocksize, n->nif.test); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8855 | break; |
| 8856 | case NFOR: |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8857 | funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8858 | funcblocksize = calcsize(funcblocksize, n->nfor.body); |
| 8859 | funcblocksize = calcsize(funcblocksize, n->nfor.args); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8860 | break; |
| 8861 | case NCASE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8862 | funcblocksize = calcsize(funcblocksize, n->ncase.cases); |
| 8863 | funcblocksize = calcsize(funcblocksize, n->ncase.expr); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8864 | break; |
| 8865 | case NCLIST: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8866 | funcblocksize = calcsize(funcblocksize, n->nclist.body); |
| 8867 | funcblocksize = calcsize(funcblocksize, n->nclist.pattern); |
| 8868 | funcblocksize = calcsize(funcblocksize, n->nclist.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8869 | break; |
| 8870 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8871 | funcblocksize = calcsize(funcblocksize, n->ndefun.body); |
| 8872 | funcblocksize += SHELL_ALIGN(strlen(n->ndefun.text) + 1); |
| 8873 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8874 | case NARG: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8875 | funcblocksize = sizenodelist(funcblocksize, n->narg.backquote); |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8876 | funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8877 | funcblocksize = calcsize(funcblocksize, n->narg.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8878 | break; |
| 8879 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8880 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8881 | case NTO2: |
| 8882 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8883 | case NCLOBBER: |
| 8884 | case NFROM: |
| 8885 | case NFROMTO: |
| 8886 | case NAPPEND: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8887 | funcblocksize = calcsize(funcblocksize, n->nfile.fname); |
| 8888 | funcblocksize = calcsize(funcblocksize, n->nfile.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8889 | break; |
| 8890 | case NTOFD: |
| 8891 | case NFROMFD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8892 | funcblocksize = calcsize(funcblocksize, n->ndup.vname); |
| 8893 | funcblocksize = calcsize(funcblocksize, n->ndup.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8894 | break; |
| 8895 | case NHERE: |
| 8896 | case NXHERE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8897 | funcblocksize = calcsize(funcblocksize, n->nhere.doc); |
| 8898 | funcblocksize = calcsize(funcblocksize, n->nhere.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8899 | break; |
| 8900 | case NNOT: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8901 | funcblocksize = calcsize(funcblocksize, n->nnot.com); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8902 | break; |
| 8903 | }; |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8904 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | static char * |
| 8908 | nodeckstrdup(char *s) |
| 8909 | { |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 8910 | funcstring_end -= SHELL_ALIGN(strlen(s) + 1); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8911 | return strcpy(funcstring_end, s); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
| 8914 | static union node *copynode(union node *); |
| 8915 | |
| 8916 | static struct nodelist * |
| 8917 | copynodelist(struct nodelist *lp) |
| 8918 | { |
| 8919 | struct nodelist *start; |
| 8920 | struct nodelist **lpp; |
| 8921 | |
| 8922 | lpp = &start; |
| 8923 | while (lp) { |
| 8924 | *lpp = funcblock; |
| 8925 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8926 | (*lpp)->n = copynode(lp->n); |
| 8927 | lp = lp->next; |
| 8928 | lpp = &(*lpp)->next; |
| 8929 | } |
| 8930 | *lpp = NULL; |
| 8931 | return start; |
| 8932 | } |
| 8933 | |
| 8934 | static union node * |
| 8935 | copynode(union node *n) |
| 8936 | { |
| 8937 | union node *new; |
| 8938 | |
| 8939 | if (n == NULL) |
| 8940 | return NULL; |
| 8941 | new = funcblock; |
| 8942 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8943 | |
| 8944 | switch (n->type) { |
| 8945 | case NCMD: |
| 8946 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8947 | new->ncmd.args = copynode(n->ncmd.args); |
| 8948 | new->ncmd.assign = copynode(n->ncmd.assign); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8949 | new->ncmd.linno = n->ncmd.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8950 | break; |
| 8951 | case NPIPE: |
| 8952 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8953 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8954 | break; |
| 8955 | case NREDIR: |
| 8956 | case NBACKGND: |
| 8957 | case NSUBSHELL: |
| 8958 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8959 | new->nredir.n = copynode(n->nredir.n); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8960 | new->nredir.linno = n->nredir.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8961 | break; |
| 8962 | case NAND: |
| 8963 | case NOR: |
| 8964 | case NSEMI: |
| 8965 | case NWHILE: |
| 8966 | case NUNTIL: |
| 8967 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8968 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8969 | break; |
| 8970 | case NIF: |
| 8971 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8972 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8973 | new->nif.test = copynode(n->nif.test); |
| 8974 | break; |
| 8975 | case NFOR: |
| 8976 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8977 | new->nfor.body = copynode(n->nfor.body); |
| 8978 | new->nfor.args = copynode(n->nfor.args); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8979 | new->nfor.linno = n->nfor.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8980 | break; |
| 8981 | case NCASE: |
| 8982 | new->ncase.cases = copynode(n->ncase.cases); |
| 8983 | new->ncase.expr = copynode(n->ncase.expr); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8984 | new->ncase.linno = n->ncase.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8985 | break; |
| 8986 | case NCLIST: |
| 8987 | new->nclist.body = copynode(n->nclist.body); |
| 8988 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8989 | new->nclist.next = copynode(n->nclist.next); |
| 8990 | break; |
| 8991 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 8992 | new->ndefun.body = copynode(n->ndefun.body); |
| 8993 | new->ndefun.text = nodeckstrdup(n->ndefun.text); |
| 8994 | new->ndefun.linno = n->ndefun.linno; |
| 8995 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8996 | case NARG: |
| 8997 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8998 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8999 | new->narg.next = copynode(n->narg.next); |
| 9000 | break; |
| 9001 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9002 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9003 | case NTO2: |
| 9004 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9005 | case NCLOBBER: |
| 9006 | case NFROM: |
| 9007 | case NFROMTO: |
| 9008 | case NAPPEND: |
| 9009 | new->nfile.fname = copynode(n->nfile.fname); |
| 9010 | new->nfile.fd = n->nfile.fd; |
| 9011 | new->nfile.next = copynode(n->nfile.next); |
| 9012 | break; |
| 9013 | case NTOFD: |
| 9014 | case NFROMFD: |
| 9015 | new->ndup.vname = copynode(n->ndup.vname); |
| 9016 | new->ndup.dupfd = n->ndup.dupfd; |
| 9017 | new->ndup.fd = n->ndup.fd; |
| 9018 | new->ndup.next = copynode(n->ndup.next); |
| 9019 | break; |
| 9020 | case NHERE: |
| 9021 | case NXHERE: |
| 9022 | new->nhere.doc = copynode(n->nhere.doc); |
| 9023 | new->nhere.fd = n->nhere.fd; |
| 9024 | new->nhere.next = copynode(n->nhere.next); |
| 9025 | break; |
| 9026 | case NNOT: |
| 9027 | new->nnot.com = copynode(n->nnot.com); |
| 9028 | break; |
| 9029 | }; |
| 9030 | new->type = n->type; |
| 9031 | return new; |
| 9032 | } |
| 9033 | |
| 9034 | /* |
| 9035 | * Make a copy of a parse tree. |
| 9036 | */ |
| 9037 | static struct funcnode * |
| 9038 | copyfunc(union node *n) |
| 9039 | { |
| 9040 | struct funcnode *f; |
| 9041 | size_t blocksize; |
| 9042 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9043 | /*funcstringsize = 0;*/ |
| 9044 | blocksize = offsetof(struct funcnode, n) + calcsize(0, n); |
| 9045 | f = ckzalloc(blocksize /* + funcstringsize */); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9046 | funcblock = (char *) f + offsetof(struct funcnode, n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9047 | funcstring_end = (char *) f + blocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9048 | copynode(n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9049 | /* f->count = 0; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9050 | return f; |
| 9051 | } |
| 9052 | |
| 9053 | /* |
| 9054 | * Define a shell function. |
| 9055 | */ |
| 9056 | static void |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9057 | defun(union node *func) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9058 | { |
| 9059 | struct cmdentry entry; |
| 9060 | |
| 9061 | INT_OFF; |
| 9062 | entry.cmdtype = CMDFUNCTION; |
| 9063 | entry.u.func = copyfunc(func); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9064 | addcmdentry(func->ndefun.text, &entry); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9065 | INT_ON; |
| 9066 | } |
| 9067 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9068 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9069 | #define SKIPBREAK (1 << 0) |
| 9070 | #define SKIPCONT (1 << 1) |
| 9071 | #define SKIPFUNC (1 << 2) |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9072 | #define SKIPFUNCDEF (1 << 3) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9073 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9074 | static int skipcount; /* number of levels to skip */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 9075 | static int loopnest; /* current loop nesting level */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9076 | 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] | 9077 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9078 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9079 | static int evalstring(char *s, int flags); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9080 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9081 | /* Called to execute a trap. |
| 9082 | * Single callsite - at the end of evaltree(). |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9083 | * If we return non-zero, evaltree raises EXEXIT exception. |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9084 | * |
| 9085 | * Perhaps we should avoid entering new trap handlers |
| 9086 | * while we are executing a trap handler. [is it a TODO?] |
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 | static void |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9089 | dotrap(void) |
| 9090 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9091 | uint8_t *g; |
| 9092 | int sig; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9093 | int status, last_status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9094 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9095 | if (!pending_sig) |
| 9096 | return; |
| 9097 | |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9098 | status = savestatus; |
| 9099 | last_status = status; |
| 9100 | if (status < 0) { |
| 9101 | status = exitstatus; |
| 9102 | savestatus = status; |
| 9103 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9104 | pending_sig = 0; |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 9105 | barrier(); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9106 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9107 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9108 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9109 | char *p; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9110 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9111 | if (!*g) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9112 | continue; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9113 | |
| 9114 | if (evalskip) { |
| 9115 | pending_sig = sig; |
| 9116 | break; |
| 9117 | } |
| 9118 | |
| 9119 | p = trap[sig]; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9120 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 9121 | * don't upset it by resetting gotsig[SIGINT-1] */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9122 | if (sig == SIGINT && !p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9123 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9124 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9125 | TRACE(("sig %d is active, will run handler '%s'\n", sig, p)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9126 | *g = 0; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9127 | if (!p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9128 | continue; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9129 | evalstring(p, 0); |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9130 | if (evalskip != SKIPFUNC) |
| 9131 | exitstatus = status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9132 | } |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9133 | |
| 9134 | savestatus = last_status; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9135 | TRACE(("dotrap returns\n")); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9136 | } |
| 9137 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9138 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9139 | static int evalloop(union node *, int); |
| 9140 | static int evalfor(union node *, int); |
| 9141 | static int evalcase(union node *, int); |
| 9142 | static int evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9143 | static void expredir(union node *); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9144 | static int evalpipe(union node *, int); |
| 9145 | static int evalcommand(union node *, int); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9146 | static int evalbltin(const struct builtincmd *, int, char **, int); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9147 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9148 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9149 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9150 | * Evaluate a parse tree. The value is left in the global variable |
| 9151 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9152 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9153 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9154 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9155 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9156 | int checkexit = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9157 | int (*evalfn)(union node *, int); |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9158 | struct stackmark smark; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9159 | int status = 0; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9160 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9161 | setstackmark(&smark); |
| 9162 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9163 | if (n == NULL) { |
| 9164 | TRACE(("evaltree(NULL) called\n")); |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 9165 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9166 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9167 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9168 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9169 | dotrap(); |
| 9170 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9171 | switch (n->type) { |
| 9172 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9173 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9174 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9175 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9176 | break; |
| 9177 | #endif |
| 9178 | case NNOT: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9179 | status = !evaltree(n->nnot.com, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9180 | goto setstatus; |
| 9181 | case NREDIR: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9182 | errlinno = lineno = n->nredir.linno; |
| 9183 | if (funcline) |
| 9184 | lineno -= funcline - 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9185 | expredir(n->nredir.redirect); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 9186 | pushredir(n->nredir.redirect); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9187 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 9188 | if (!status) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9189 | status = evaltree(n->nredir.n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9190 | } |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 9191 | if (n->nredir.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 9192 | popredir(/*drop:*/ 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9193 | goto setstatus; |
| 9194 | case NCMD: |
| 9195 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9196 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9197 | if (eflag && !(flags & EV_TESTED)) |
| 9198 | checkexit = ~0; |
| 9199 | goto calleval; |
| 9200 | case NFOR: |
| 9201 | evalfn = evalfor; |
| 9202 | goto calleval; |
| 9203 | case NWHILE: |
| 9204 | case NUNTIL: |
| 9205 | evalfn = evalloop; |
| 9206 | goto calleval; |
| 9207 | case NSUBSHELL: |
| 9208 | case NBACKGND: |
| 9209 | evalfn = evalsubshell; |
Denys Vlasenko | cf98b0c | 2016-10-25 18:19:39 +0200 | [diff] [blame] | 9210 | goto checkexit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9211 | case NPIPE: |
| 9212 | evalfn = evalpipe; |
| 9213 | goto checkexit; |
| 9214 | case NCASE: |
| 9215 | evalfn = evalcase; |
| 9216 | goto calleval; |
| 9217 | case NAND: |
| 9218 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9219 | case NSEMI: { |
| 9220 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9221 | #if NAND + 1 != NOR |
| 9222 | #error NAND + 1 != NOR |
| 9223 | #endif |
| 9224 | #if NOR + 1 != NSEMI |
| 9225 | #error NOR + 1 != NSEMI |
| 9226 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 9227 | unsigned is_or = n->type - NAND; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9228 | status = evaltree( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9229 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9230 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9231 | ); |
Denys Vlasenko | bc1a008 | 2016-10-02 15:31:33 +0200 | [diff] [blame] | 9232 | if ((!status) == is_or || evalskip) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9233 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9234 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9235 | evaln: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9236 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9237 | calleval: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9238 | status = evalfn(n, flags); |
| 9239 | goto setstatus; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9240 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9241 | case NIF: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9242 | status = evaltree(n->nif.test, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9243 | if (evalskip) |
| 9244 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9245 | if (!status) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9246 | n = n->nif.ifpart; |
| 9247 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9248 | } |
| 9249 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9250 | n = n->nif.elsepart; |
| 9251 | goto evaln; |
| 9252 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9253 | status = 0; |
| 9254 | goto setstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9255 | case NDEFUN: |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9256 | defun(n); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9257 | /* Not necessary. To test it: |
| 9258 | * "false; f() { qwerty; }; echo $?" should print 0. |
| 9259 | */ |
| 9260 | /* status = 0; */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9261 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9262 | exitstatus = status; |
| 9263 | break; |
| 9264 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9265 | out: |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9266 | /* Order of checks below is important: |
| 9267 | * signal handlers trigger before exit caused by "set -e". |
| 9268 | */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9269 | dotrap(); |
| 9270 | |
| 9271 | if (checkexit & status) |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 9272 | raise_exception(EXEND); |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9273 | if (flags & EV_EXIT) |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 9274 | raise_exception(EXEND); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9275 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9276 | popstackmark(&smark); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9277 | TRACE(("leaving evaltree (no interrupts)\n")); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9278 | return exitstatus; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9279 | } |
| 9280 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 9281 | static int |
| 9282 | skiploop(void) |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9283 | { |
| 9284 | int skip = evalskip; |
| 9285 | |
| 9286 | switch (skip) { |
| 9287 | case 0: |
| 9288 | break; |
| 9289 | case SKIPBREAK: |
| 9290 | case SKIPCONT: |
| 9291 | if (--skipcount <= 0) { |
| 9292 | evalskip = 0; |
| 9293 | break; |
| 9294 | } |
| 9295 | skip = SKIPBREAK; |
| 9296 | break; |
| 9297 | } |
| 9298 | return skip; |
| 9299 | } |
| 9300 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9301 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9302 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9303 | { |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9304 | int skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9305 | int status; |
| 9306 | |
| 9307 | loopnest++; |
| 9308 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9309 | flags &= EV_TESTED; |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9310 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9311 | int i; |
| 9312 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9313 | i = evaltree(n->nbinary.ch1, EV_TESTED); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9314 | skip = skiploop(); |
| 9315 | if (skip == SKIPFUNC) |
| 9316 | status = i; |
| 9317 | if (skip) |
| 9318 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9319 | if (n->type != NWHILE) |
| 9320 | i = !i; |
| 9321 | if (i != 0) |
| 9322 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9323 | status = evaltree(n->nbinary.ch2, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9324 | skip = skiploop(); |
| 9325 | } while (!(skip & ~SKIPCONT)); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9326 | loopnest--; |
| 9327 | |
| 9328 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9329 | } |
| 9330 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9331 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9332 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9333 | { |
| 9334 | struct arglist arglist; |
| 9335 | union node *argp; |
| 9336 | struct strlist *sp; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9337 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9338 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9339 | errlinno = lineno = n->ncase.linno; |
| 9340 | if (funcline) |
| 9341 | lineno -= funcline - 1; |
| 9342 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9343 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9344 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9345 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 9346 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9347 | } |
| 9348 | *arglist.lastp = NULL; |
| 9349 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9350 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9351 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9352 | for (sp = arglist.list; sp; sp = sp->next) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9353 | setvar0(n->nfor.var, sp->text); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9354 | status = evaltree(n->nfor.body, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9355 | if (skiploop() & ~SKIPCONT) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9356 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9357 | } |
| 9358 | loopnest--; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9359 | |
| 9360 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9363 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9364 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9365 | { |
| 9366 | union node *cp; |
| 9367 | union node *patp; |
| 9368 | struct arglist arglist; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9369 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9370 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9371 | errlinno = lineno = n->ncase.linno; |
| 9372 | if (funcline) |
| 9373 | lineno -= funcline - 1; |
| 9374 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9375 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9376 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9377 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9378 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 9379 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9380 | if (casematch(patp, arglist.list->text)) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9381 | /* Ensure body is non-empty as otherwise |
| 9382 | * EV_EXIT may prevent us from setting the |
| 9383 | * exit status. |
| 9384 | */ |
| 9385 | if (evalskip == 0 && cp->nclist.body) { |
| 9386 | status = evaltree(cp->nclist.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9387 | } |
| 9388 | goto out; |
| 9389 | } |
| 9390 | } |
| 9391 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9392 | out: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9393 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9394 | } |
| 9395 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9396 | /* |
| 9397 | * Kick off a subshell to evaluate a tree. |
| 9398 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9399 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9400 | evalsubshell(union node *n, int flags) |
| 9401 | { |
| 9402 | struct job *jp; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9403 | 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] | 9404 | int status; |
| 9405 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9406 | errlinno = lineno = n->nredir.linno; |
| 9407 | if (funcline) |
| 9408 | lineno -= funcline - 1; |
| 9409 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9410 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9411 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9412 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9413 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9414 | if (backgnd == FORK_FG) |
| 9415 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9416 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9417 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9418 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9419 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9420 | flags |= EV_EXIT; |
| 9421 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9422 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9423 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9424 | redirect(n->nredir.redirect, 0); |
| 9425 | evaltreenr(n->nredir.n, flags); |
| 9426 | /* never returns */ |
| 9427 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9428 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9429 | status = 0; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9430 | if (backgnd == FORK_FG) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9431 | status = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9432 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9433 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9436 | /* |
| 9437 | * Compute the names of the files in a redirection list. |
| 9438 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9439 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9440 | static void |
| 9441 | expredir(union node *n) |
| 9442 | { |
| 9443 | union node *redir; |
| 9444 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9445 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9446 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9447 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9448 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9449 | fn.lastp = &fn.list; |
| 9450 | switch (redir->type) { |
| 9451 | case NFROMTO: |
| 9452 | case NFROM: |
| 9453 | case NTO: |
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 | case NTO2: |
| 9456 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9457 | case NCLOBBER: |
| 9458 | case NAPPEND: |
| 9459 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 9460 | TRACE(("expredir expanded to '%s'\n", fn.list->text)); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9461 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9462 | store_expfname: |
| 9463 | #endif |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9464 | #if 0 |
| 9465 | // By the design of stack allocator, the loop of this kind: |
| 9466 | // while true; do while true; do break; done </dev/null; done |
| 9467 | // will look like a memory leak: ash plans to free expfname's |
| 9468 | // of "/dev/null" as soon as it finishes running the loop |
| 9469 | // (in this case, never). |
| 9470 | // This "fix" is wrong: |
Jon Tollefson | 4ba6c5d | 2012-11-13 19:26:53 +0100 | [diff] [blame] | 9471 | if (redir->nfile.expfname) |
| 9472 | stunalloc(redir->nfile.expfname); |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9473 | // It results in corrupted state of stacked allocations. |
| 9474 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9475 | redir->nfile.expfname = fn.list->text; |
| 9476 | break; |
| 9477 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9478 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9479 | if (redir->ndup.vname) { |
Denys Vlasenko | e368d85 | 2020-02-16 19:02:22 +0100 | [diff] [blame] | 9480 | expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9481 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9482 | ash_msg_and_raise_error("redir error"); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9483 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9484 | if (!isdigit_str9(fn.list->text)) { |
| 9485 | /* >&file, not >&fd */ |
| 9486 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 9487 | ash_msg_and_raise_error("redir error"); |
| 9488 | redir->type = NTO2; |
| 9489 | goto store_expfname; |
| 9490 | } |
| 9491 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9492 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9493 | } |
| 9494 | break; |
| 9495 | } |
| 9496 | } |
| 9497 | } |
| 9498 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9499 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9500 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 9501 | * of the process creating the pipeline. (This differs from some versions |
| 9502 | * of the shell, which make the last process in a pipeline the parent |
| 9503 | * of all the rest.) |
| 9504 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9505 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9506 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9507 | { |
| 9508 | struct job *jp; |
| 9509 | struct nodelist *lp; |
| 9510 | int pipelen; |
| 9511 | int prevfd; |
| 9512 | int pip[2]; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9513 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9514 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9515 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9516 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9517 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9518 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9519 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9520 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9521 | if (n->npipe.pipe_backgnd == 0) |
| 9522 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9523 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9524 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9525 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9526 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9527 | pip[1] = -1; |
| 9528 | if (lp->next) { |
| 9529 | if (pipe(pip) < 0) { |
| 9530 | close(prevfd); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 9531 | ash_msg_and_raise_perror("can't create pipe"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9532 | } |
| 9533 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9534 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9535 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9536 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9537 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9538 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9539 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9540 | if (prevfd > 0) { |
| 9541 | dup2(prevfd, 0); |
| 9542 | close(prevfd); |
| 9543 | } |
| 9544 | if (pip[1] > 1) { |
| 9545 | dup2(pip[1], 1); |
| 9546 | close(pip[1]); |
| 9547 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9548 | evaltreenr(lp->n, flags); |
| 9549 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9550 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9551 | /* parent */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9552 | if (prevfd >= 0) |
| 9553 | close(prevfd); |
| 9554 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 9555 | /* Don't want to trigger debugging */ |
| 9556 | if (pip[1] != -1) |
| 9557 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9558 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9559 | if (n->npipe.pipe_backgnd == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9560 | status = waitforjob(jp); |
| 9561 | TRACE(("evalpipe: job done exit status %d\n", status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9562 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9563 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9564 | |
| 9565 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9568 | /* setinteractive needs this forward reference */ |
| 9569 | #if EDITING_HAS_get_exe_name |
| 9570 | static const char *get_builtin_name(int i) FAST_FUNC; |
| 9571 | #endif |
| 9572 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9573 | /* |
| 9574 | * Controls whether the shell is interactive or not. |
| 9575 | */ |
| 9576 | static void |
| 9577 | setinteractive(int on) |
| 9578 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9579 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9580 | |
| 9581 | if (++on == is_interactive) |
| 9582 | return; |
| 9583 | is_interactive = on; |
| 9584 | setsignal(SIGINT); |
| 9585 | setsignal(SIGQUIT); |
| 9586 | setsignal(SIGTERM); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9587 | if (is_interactive > 1) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9588 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9589 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9590 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9591 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9592 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9593 | /* note: ash and hush share this string */ |
| 9594 | out1fmt("\n\n%s %s\n" |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9595 | IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9596 | "\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9597 | bb_banner, |
| 9598 | "built-in shell (ash)" |
| 9599 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9600 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9601 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9602 | #endif |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9603 | #if ENABLE_FEATURE_EDITING |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9604 | if (!line_input_state) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9605 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9606 | # if EDITING_HAS_get_exe_name |
| 9607 | line_input_state->get_exe_name = get_builtin_name; |
| 9608 | # endif |
| 9609 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9610 | #endif |
| 9611 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9612 | } |
| 9613 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9614 | static void |
| 9615 | optschanged(void) |
| 9616 | { |
| 9617 | #if DEBUG |
| 9618 | opentrace(); |
| 9619 | #endif |
| 9620 | setinteractive(iflag); |
| 9621 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9622 | #if ENABLE_FEATURE_EDITING_VI |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9623 | if (line_input_state) { |
| 9624 | if (viflag) |
| 9625 | line_input_state->flags |= VI_MODE; |
| 9626 | else |
| 9627 | line_input_state->flags &= ~VI_MODE; |
| 9628 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9629 | #else |
| 9630 | viflag = 0; /* forcibly keep the option off */ |
| 9631 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9632 | } |
| 9633 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9634 | struct localvar_list { |
| 9635 | struct localvar_list *next; |
| 9636 | struct localvar *lv; |
| 9637 | }; |
| 9638 | |
| 9639 | static struct localvar_list *localvar_stack; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9640 | |
| 9641 | /* |
| 9642 | * Called after a function returns. |
| 9643 | * Interrupts must be off. |
| 9644 | */ |
| 9645 | static void |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9646 | poplocalvars(int keep) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9647 | { |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9648 | struct localvar_list *ll; |
| 9649 | struct localvar *lvp, *next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9650 | struct var *vp; |
| 9651 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9652 | INT_OFF; |
| 9653 | ll = localvar_stack; |
| 9654 | localvar_stack = ll->next; |
| 9655 | |
| 9656 | next = ll->lv; |
| 9657 | free(ll); |
| 9658 | |
| 9659 | while ((lvp = next) != NULL) { |
| 9660 | next = lvp->next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9661 | vp = lvp->vp; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9662 | TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-")); |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9663 | if (keep) { |
| 9664 | int bits = VSTRFIXED; |
| 9665 | |
| 9666 | if (lvp->flags != VUNSET) { |
| 9667 | if (vp->var_text == lvp->text) |
| 9668 | bits |= VTEXTFIXED; |
| 9669 | else if (!(lvp->flags & (VTEXTFIXED|VSTACK))) |
| 9670 | free((char*)lvp->text); |
| 9671 | } |
| 9672 | |
| 9673 | vp->flags &= ~bits; |
| 9674 | vp->flags |= (lvp->flags & bits); |
| 9675 | |
| 9676 | if ((vp->flags & |
| 9677 | (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 9678 | unsetvar(vp->var_text); |
| 9679 | } else if (vp == NULL) { /* $- saved */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9680 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 9681 | free((char*)lvp->text); |
| 9682 | optschanged(); |
Denys Vlasenko | d5b500c | 2017-07-26 19:25:40 +0200 | [diff] [blame] | 9683 | } else if (lvp->flags == VUNSET) { |
| 9684 | vp->flags &= ~(VSTRFIXED|VREADONLY); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9685 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9686 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9687 | if (vp->var_func) |
| 9688 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9689 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9690 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9691 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9692 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9693 | } |
| 9694 | free(lvp); |
| 9695 | } |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9696 | INT_ON; |
| 9697 | } |
| 9698 | |
| 9699 | /* |
| 9700 | * Create a new localvar environment. |
| 9701 | */ |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9702 | static struct localvar_list * |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9703 | pushlocalvars(int push) |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9704 | { |
| 9705 | struct localvar_list *ll; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9706 | struct localvar_list *top; |
| 9707 | |
| 9708 | top = localvar_stack; |
| 9709 | if (!push) |
| 9710 | goto out; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9711 | |
| 9712 | INT_OFF; |
| 9713 | ll = ckzalloc(sizeof(*ll)); |
| 9714 | /*ll->lv = NULL; - zalloc did it */ |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9715 | ll->next = top; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9716 | localvar_stack = ll; |
| 9717 | INT_ON; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9718 | out: |
| 9719 | return top; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9720 | } |
| 9721 | |
| 9722 | static void |
| 9723 | unwindlocalvars(struct localvar_list *stop) |
| 9724 | { |
| 9725 | while (localvar_stack != stop) |
| 9726 | poplocalvars(0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9727 | } |
| 9728 | |
| 9729 | static int |
| 9730 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 9731 | { |
| 9732 | volatile struct shparam saveparam; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9733 | struct jmploc *volatile savehandler; |
| 9734 | struct jmploc jmploc; |
| 9735 | int e; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9736 | int savefuncline; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9737 | |
| 9738 | saveparam = shellparam; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9739 | savefuncline = funcline; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 9740 | savehandler = exception_handler; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9741 | e = setjmp(jmploc.loc); |
| 9742 | if (e) { |
| 9743 | goto funcdone; |
| 9744 | } |
| 9745 | INT_OFF; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9746 | exception_handler = &jmploc; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9747 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9748 | func->count++; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9749 | funcline = func->n.ndefun.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9750 | INT_ON; |
| 9751 | shellparam.nparam = argc - 1; |
| 9752 | shellparam.p = argv + 1; |
| 9753 | #if ENABLE_ASH_GETOPTS |
| 9754 | shellparam.optind = 1; |
| 9755 | shellparam.optoff = -1; |
| 9756 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9757 | evaltree(func->n.ndefun.body, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9758 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9759 | INT_OFF; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9760 | funcline = savefuncline; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9761 | freefunc(func); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9762 | freeparam(&shellparam); |
| 9763 | shellparam = saveparam; |
| 9764 | exception_handler = savehandler; |
| 9765 | INT_ON; |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9766 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9767 | return e; |
| 9768 | } |
| 9769 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9770 | /* |
| 9771 | * Make a variable a local variable. When a variable is made local, it's |
| 9772 | * value and flags are saved in a localvar structure. The saved values |
| 9773 | * will be restored when the shell function returns. We handle the name |
Denys Vlasenko | e0a4e10 | 2015-05-13 02:20:14 +0200 | [diff] [blame] | 9774 | * "-" as a special case: it makes changes to "set +-options" local |
| 9775 | * (options will be restored on return from the function). |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9776 | */ |
| 9777 | static void |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9778 | mklocal(char *name, int flags) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9779 | { |
| 9780 | struct localvar *lvp; |
| 9781 | struct var **vpp; |
| 9782 | struct var *vp; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9783 | char *eq = strchr(name, '='); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9784 | |
| 9785 | INT_OFF; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9786 | /* Cater for duplicate "local". Examples: |
| 9787 | * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x |
| 9788 | * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x |
| 9789 | */ |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9790 | lvp = localvar_stack->lv; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9791 | while (lvp) { |
Eugene Rudoy | 1285aa6 | 2015-04-26 23:32:00 +0200 | [diff] [blame] | 9792 | if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9793 | if (eq) |
| 9794 | setvareq(name, 0); |
| 9795 | /* else: |
| 9796 | * it's a duplicate "local VAR" declaration, do nothing |
| 9797 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 9798 | goto ret; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9799 | } |
| 9800 | lvp = lvp->next; |
| 9801 | } |
| 9802 | |
| 9803 | lvp = ckzalloc(sizeof(*lvp)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9804 | if (LONE_DASH(name)) { |
| 9805 | char *p; |
| 9806 | p = ckmalloc(sizeof(optlist)); |
| 9807 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 9808 | vp = NULL; |
| 9809 | } else { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9810 | vpp = hashvar(name); |
| 9811 | vp = *findvar(vpp, name); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9812 | if (vp == NULL) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9813 | /* variable did not exist yet */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9814 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9815 | vp = setvareq(name, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9816 | else |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9817 | vp = setvar(name, NULL, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9818 | lvp->flags = VUNSET; |
| 9819 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9820 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9821 | lvp->flags = vp->flags; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9822 | /* make sure neither "struct var" nor string gets freed |
| 9823 | * during (un)setting: |
| 9824 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9825 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 9826 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9827 | setvareq(name, flags); |
Denys Vlasenko | 109ee5d | 2014-03-16 18:41:11 +0100 | [diff] [blame] | 9828 | else |
| 9829 | /* "local VAR" unsets VAR: */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9830 | setvar0(name, NULL); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9831 | } |
| 9832 | } |
| 9833 | lvp->vp = vp; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9834 | lvp->next = localvar_stack->lv; |
| 9835 | localvar_stack->lv = lvp; |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 9836 | ret: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9837 | INT_ON; |
| 9838 | } |
| 9839 | |
| 9840 | /* |
| 9841 | * The "local" command. |
| 9842 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9843 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9844 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9845 | { |
| 9846 | char *name; |
| 9847 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9848 | if (!localvar_stack) |
Ron Yorston | ef2386b | 2015-10-29 16:19:14 +0000 | [diff] [blame] | 9849 | ash_msg_and_raise_error("not in a function"); |
| 9850 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9851 | argv = argptr; |
| 9852 | while ((name = *argv++) != NULL) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9853 | mklocal(name, 0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9854 | } |
| 9855 | return 0; |
| 9856 | } |
| 9857 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9858 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9859 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9860 | { |
| 9861 | return 1; |
| 9862 | } |
| 9863 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9864 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9865 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9866 | { |
| 9867 | return 0; |
| 9868 | } |
| 9869 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9870 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9871 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9872 | { |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 9873 | optionarg = NULL; |
| 9874 | while (nextopt("a:") != '\0') |
| 9875 | /* nextopt() sets optionarg to "-a ARGV0" */; |
| 9876 | |
| 9877 | argv = argptr; |
| 9878 | if (argv[0]) { |
| 9879 | char *prog; |
| 9880 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9881 | iflag = 0; /* exit on error */ |
| 9882 | mflag = 0; |
| 9883 | optschanged(); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9884 | /* We should set up signals for "exec CMD" |
| 9885 | * the same way as for "CMD" without "exec". |
| 9886 | * But optschanged->setinteractive->setsignal |
| 9887 | * still thought we are a root shell. Therefore, for example, |
| 9888 | * SIGQUIT is still set to IGN. Fix it: |
| 9889 | */ |
| 9890 | shlvl++; |
| 9891 | setsignal(SIGQUIT); |
| 9892 | /*setsignal(SIGTERM); - unnecessary because of iflag=0 */ |
| 9893 | /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */ |
| 9894 | /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */ |
| 9895 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 9896 | prog = argv[0]; |
| 9897 | if (optionarg) |
| 9898 | argv[0] = optionarg; |
| 9899 | shellexec(prog, argv, pathval(), 0); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9900 | /* NOTREACHED */ |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9901 | } |
| 9902 | return 0; |
| 9903 | } |
| 9904 | |
| 9905 | /* |
| 9906 | * The return command. |
| 9907 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9908 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9909 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9910 | { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9911 | int skip; |
| 9912 | int status; |
| 9913 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9914 | /* |
| 9915 | * If called outside a function, do what ksh does; |
| 9916 | * skip the rest of the file. |
| 9917 | */ |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9918 | if (argv[1]) { |
| 9919 | skip = SKIPFUNC; |
| 9920 | status = number(argv[1]); |
| 9921 | } else { |
| 9922 | skip = SKIPFUNCDEF; |
| 9923 | status = exitstatus; |
| 9924 | } |
| 9925 | evalskip = skip; |
| 9926 | |
| 9927 | return status; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9928 | } |
| 9929 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9930 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9931 | static int breakcmd(int, char **) FAST_FUNC; |
| 9932 | static int dotcmd(int, char **) FAST_FUNC; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9933 | static int evalcmd(int, char **, int) FAST_FUNC; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9934 | static int exitcmd(int, char **) FAST_FUNC; |
| 9935 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9936 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9937 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9938 | #endif |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9939 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9940 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 9941 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9942 | #if MAX_HISTORY |
| 9943 | static int historycmd(int, char **) FAST_FUNC; |
| 9944 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 9945 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9946 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9947 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9948 | static int readcmd(int, char **) FAST_FUNC; |
| 9949 | static int setcmd(int, char **) FAST_FUNC; |
| 9950 | static int shiftcmd(int, char **) FAST_FUNC; |
| 9951 | static int timescmd(int, char **) FAST_FUNC; |
| 9952 | static int trapcmd(int, char **) FAST_FUNC; |
| 9953 | static int umaskcmd(int, char **) FAST_FUNC; |
| 9954 | static int unsetcmd(int, char **) FAST_FUNC; |
| 9955 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9956 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9957 | #define BUILTIN_NOSPEC "0" |
| 9958 | #define BUILTIN_SPECIAL "1" |
| 9959 | #define BUILTIN_REGULAR "2" |
| 9960 | #define BUILTIN_SPEC_REG "3" |
| 9961 | #define BUILTIN_ASSIGN "4" |
| 9962 | #define BUILTIN_SPEC_ASSG "5" |
| 9963 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9964 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 9965 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9966 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9967 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9968 | 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] | 9969 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9970 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9971 | 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] | 9972 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9973 | #if ENABLE_ASH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9974 | 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] | 9975 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 9976 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9977 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9978 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9979 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 9980 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9981 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9982 | { BUILTIN_REGULAR "[" , testcmd }, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9983 | #endif |
| 9984 | #if BASH_TEST2 |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9985 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9986 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9987 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9988 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9989 | #endif |
| 9990 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9991 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9992 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9993 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 9994 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 9995 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9996 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9997 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9998 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9999 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10000 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10001 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10002 | #endif |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10003 | { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/ |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10004 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 10005 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 10006 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 10007 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10008 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10009 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10010 | #endif |
| 10011 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10012 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10013 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10014 | { BUILTIN_REGULAR "hash" , hashcmd }, |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 10015 | #if ENABLE_ASH_HELP |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10016 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10017 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 10018 | #if MAX_HISTORY |
| 10019 | { BUILTIN_NOSPEC "history" , historycmd }, |
| 10020 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10021 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10022 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 10023 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10024 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 10025 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10026 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10027 | #endif |
Denys Vlasenko | 85241c7 | 2017-07-26 20:00:08 +0200 | [diff] [blame] | 10028 | { BUILTIN_SPEC_REG_ASSG "local" , localcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10029 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10030 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 10031 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10032 | { BUILTIN_REGULAR "pwd" , pwdcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10033 | { BUILTIN_REGULAR "read" , readcmd }, |
| 10034 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 10035 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 10036 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 10037 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10038 | #if BASH_SOURCE |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10039 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 10040 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10041 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10042 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10043 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10044 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 10045 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 10046 | { BUILTIN_REGULAR "true" , truecmd }, |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10047 | { BUILTIN_REGULAR "type" , typecmd }, |
| 10048 | { BUILTIN_REGULAR "ulimit" , ulimitcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10049 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10050 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10051 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10052 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10053 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 10054 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10055 | }; |
| 10056 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10057 | /* Should match the above table! */ |
| 10058 | #define COMMANDCMD (builtintab + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10059 | /* . : */ 2 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10060 | /* [ */ 1 * ENABLE_ASH_TEST + \ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10061 | /* [[ */ 1 * BASH_TEST2 + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10062 | /* alias */ 1 * ENABLE_ASH_ALIAS + \ |
| 10063 | /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 10064 | /* break cd cddir */ 3) |
| 10065 | #define EVALCMD (COMMANDCMD + \ |
| 10066 | /* command */ 1 * ENABLE_ASH_CMDCMD + \ |
| 10067 | /* continue */ 1 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10068 | /* echo */ 1 * ENABLE_ASH_ECHO + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10069 | 0) |
| 10070 | #define EXECCMD (EVALCMD + \ |
| 10071 | /* eval */ 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10072 | |
| 10073 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10074 | * Search the table of builtin commands. |
| 10075 | */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10076 | static int |
| 10077 | pstrcmp1(const void *a, const void *b) |
| 10078 | { |
| 10079 | return strcmp((char*)a, *(char**)b + 1); |
| 10080 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10081 | static struct builtincmd * |
| 10082 | find_builtin(const char *name) |
| 10083 | { |
| 10084 | struct builtincmd *bp; |
| 10085 | |
| 10086 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 10087 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10088 | pstrcmp1 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10089 | ); |
| 10090 | return bp; |
| 10091 | } |
| 10092 | |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 10093 | #if EDITING_HAS_get_exe_name |
| 10094 | static const char * FAST_FUNC |
| 10095 | get_builtin_name(int i) |
| 10096 | { |
| 10097 | return /*i >= 0 &&*/ i < ARRAY_SIZE(builtintab) ? builtintab[i].name + 1 : NULL; |
| 10098 | } |
| 10099 | #endif |
| 10100 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10101 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10102 | * Execute a simple command. |
| 10103 | */ |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10104 | static void unwindfiles(struct parsefile *stop); |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10105 | static int |
| 10106 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10107 | { |
| 10108 | const char *q = endofname(p); |
| 10109 | if (p == q) |
| 10110 | return 0; |
| 10111 | return *q == '='; |
| 10112 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10113 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10114 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10115 | { |
| 10116 | /* Preserve exitstatus of a previous possible redirection |
| 10117 | * as POSIX mandates */ |
| 10118 | return back_exitstatus; |
| 10119 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10120 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10121 | evalcommand(union node *cmd, int flags) |
| 10122 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10123 | static const struct builtincmd null_bltin = { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10124 | BUILTIN_REGULAR "", bltincmd |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10125 | }; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 10126 | struct localvar_list *localvar_stop; |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10127 | struct parsefile *file_stop; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10128 | struct redirtab *redir_stop; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10129 | union node *argp; |
| 10130 | struct arglist arglist; |
| 10131 | struct arglist varlist; |
| 10132 | char **argv; |
| 10133 | int argc; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10134 | struct strlist *osp; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10135 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10136 | struct cmdentry cmdentry; |
| 10137 | struct job *jp; |
| 10138 | char *lastarg; |
| 10139 | const char *path; |
| 10140 | int spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10141 | int cmd_flag; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10142 | int status; |
| 10143 | char **nargv; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 10144 | smallint cmd_is_exec; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10145 | int vflags; |
| 10146 | int vlocal; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10147 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10148 | errlinno = lineno = cmd->ncmd.linno; |
| 10149 | if (funcline) |
| 10150 | lineno -= funcline - 1; |
| 10151 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10152 | /* First expand the arguments. */ |
| 10153 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10154 | file_stop = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10155 | back_exitstatus = 0; |
| 10156 | |
| 10157 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10158 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10159 | varlist.lastp = &varlist.list; |
| 10160 | *varlist.lastp = NULL; |
| 10161 | arglist.lastp = &arglist.list; |
| 10162 | *arglist.lastp = NULL; |
| 10163 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10164 | cmd_flag = 0; |
| 10165 | cmd_is_exec = 0; |
| 10166 | spclbltin = -1; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10167 | vflags = 0; |
| 10168 | vlocal = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10169 | path = NULL; |
| 10170 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10171 | argc = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10172 | argp = cmd->ncmd.args; |
| 10173 | osp = fill_arglist(&arglist, &argp); |
| 10174 | if (osp) { |
| 10175 | int pseudovarflag = 0; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10176 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10177 | for (;;) { |
| 10178 | find_command(arglist.list->text, &cmdentry, |
| 10179 | cmd_flag | DO_REGBLTIN, pathval()); |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10180 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10181 | vlocal++; |
| 10182 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10183 | /* implement bltin and command here */ |
| 10184 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 10185 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10186 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10187 | pseudovarflag = IS_BUILTIN_ASSIGN(cmdentry.u.cmd); |
| 10188 | if (spclbltin < 0) { |
| 10189 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10190 | vlocal = !spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10191 | } |
| 10192 | cmd_is_exec = cmdentry.u.cmd == EXECCMD; |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10193 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10194 | if (cmdentry.u.cmd != COMMANDCMD) |
| 10195 | break; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10196 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10197 | cmd_flag = parse_command_args(&arglist, &argp, &path); |
| 10198 | if (!cmd_flag) |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10199 | #endif |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10200 | break; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10201 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10202 | |
| 10203 | for (; argp; argp = argp->narg.next) |
| 10204 | expandarg(argp, &arglist, |
| 10205 | pseudovarflag && |
| 10206 | isassignment(argp->narg.text) ? |
| 10207 | EXP_VARTILDE : EXP_FULL | EXP_TILDE); |
| 10208 | |
| 10209 | for (sp = arglist.list; sp; sp = sp->next) |
| 10210 | argc++; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10211 | |
| 10212 | if (cmd_is_exec && argc > 1) |
| 10213 | vflags = VEXPORT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10214 | } |
| 10215 | |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10216 | localvar_stop = pushlocalvars(vlocal); |
| 10217 | |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 10218 | /* Reserve one extra spot at the front for shellexec. */ |
| 10219 | nargv = stalloc(sizeof(char *) * (argc + 2)); |
| 10220 | argv = ++nargv; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10221 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10222 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 10223 | *nargv++ = sp->text; |
| 10224 | } |
| 10225 | *nargv = NULL; |
| 10226 | |
| 10227 | lastarg = NULL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10228 | if (iflag && funcline == 0 && argc > 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10229 | lastarg = nargv[-1]; |
| 10230 | |
| 10231 | expredir(cmd->ncmd.redirect); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10232 | redir_stop = pushredir(cmd->ncmd.redirect); |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10233 | preverrout_fd = 2; |
Denys Vlasenko | f8cdc7a | 2017-08-04 15:24:49 +0200 | [diff] [blame] | 10234 | if (BASH_XTRACEFD && xflag) { |
| 10235 | /* NB: bash closes fd == $BASH_XTRACEFD when it is changed. |
| 10236 | * we do not emulate this. We only use its value. |
| 10237 | */ |
| 10238 | const char *xtracefd = lookupvar("BASH_XTRACEFD"); |
| 10239 | if (xtracefd && is_number(xtracefd)) |
| 10240 | preverrout_fd = atoi(xtracefd); |
| 10241 | |
| 10242 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10243 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10244 | |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10245 | if (status) { |
| 10246 | bail: |
| 10247 | exitstatus = status; |
| 10248 | |
| 10249 | /* We have a redirection error. */ |
| 10250 | if (spclbltin > 0) |
| 10251 | raise_exception(EXERROR); |
| 10252 | |
| 10253 | goto out; |
| 10254 | } |
| 10255 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10256 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 10257 | struct strlist **spp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10258 | |
| 10259 | spp = varlist.lastp; |
| 10260 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 10261 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10262 | if (vlocal) |
| 10263 | mklocal((*spp)->text, VEXPORT); |
| 10264 | else |
| 10265 | setvareq((*spp)->text, vflags); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10266 | } |
| 10267 | |
| 10268 | /* Print the command if xflag is set. */ |
| 10269 | if (xflag) { |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10270 | const char *pfx = ""; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10271 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 10272 | fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX)); |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10273 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10274 | sp = varlist.list; |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10275 | while (sp) { |
| 10276 | char *varval = sp->text; |
| 10277 | char *eq = strchrnul(varval, '='); |
| 10278 | if (*eq) |
| 10279 | eq++; |
| 10280 | fdprintf(preverrout_fd, "%s%.*s%s", |
| 10281 | pfx, |
| 10282 | (int)(eq - varval), varval, |
| 10283 | maybe_single_quote(eq) |
| 10284 | ); |
| 10285 | sp = sp->next; |
| 10286 | pfx = " "; |
| 10287 | } |
| 10288 | |
| 10289 | sp = arglist.list; |
| 10290 | while (sp) { |
| 10291 | fdprintf(preverrout_fd, "%s%s", |
| 10292 | pfx, |
| 10293 | /* always quote if matches reserved word: */ |
| 10294 | findkwd(sp->text) |
| 10295 | ? single_quote(sp->text) |
| 10296 | : maybe_single_quote(sp->text) |
| 10297 | ); |
| 10298 | sp = sp->next; |
| 10299 | pfx = " "; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10300 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10301 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10302 | } |
| 10303 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10304 | /* Now locate the command. */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10305 | if (cmdentry.cmdtype != CMDBUILTIN |
| 10306 | || !(IS_BUILTIN_REGULAR(cmdentry.u.cmd)) |
| 10307 | ) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10308 | path = path ? path : pathval(); |
| 10309 | find_command(argv[0], &cmdentry, cmd_flag | DO_ERR, path); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10312 | jp = NULL; |
| 10313 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10314 | /* Execute the command. */ |
| 10315 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10316 | case CMDUNKNOWN: |
| 10317 | status = 127; |
| 10318 | flush_stdout_stderr(); |
| 10319 | goto bail; |
| 10320 | |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10321 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 10322 | |
Denys Vlasenko | 1750d3a | 2018-01-15 00:41:04 +0100 | [diff] [blame] | 10323 | #if ENABLE_FEATURE_SH_STANDALONE \ |
| 10324 | && ENABLE_FEATURE_SH_NOFORK \ |
| 10325 | && NUM_APPLETS > 1 |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10326 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 10327 | * around run_nofork_applet() call. |
| 10328 | * (2) Should this check also be done in forkshell()? |
| 10329 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 10330 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 10331 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 10332 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10333 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10334 | char **sv_environ; |
| 10335 | |
| 10336 | INT_OFF; |
| 10337 | sv_environ = environ; |
| 10338 | environ = listvars(VEXPORT, VUNSET, varlist.list, /*end:*/ NULL); |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10339 | /* |
| 10340 | * Run <applet>_main(). |
| 10341 | * Signals (^C) can't interrupt here. |
| 10342 | * Otherwise we can mangle stdio or malloc internal state. |
| 10343 | * This makes applets which can run for a long time |
| 10344 | * and/or wait for user input ineligible for NOFORK: |
| 10345 | * for example, "yes" or "rm" (rm -i waits for input). |
| 10346 | */ |
Ron Yorston | d5bfe26 | 2020-02-20 08:23:03 +0000 | [diff] [blame] | 10347 | exitstatus = run_nofork_applet(applet_no, argv); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10348 | environ = sv_environ; |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10349 | /* |
| 10350 | * Try enabling NOFORK for "yes" applet. |
| 10351 | * ^C _will_ stop it (write returns EINTR), |
| 10352 | * but this causes stdout FILE to be stuck |
| 10353 | * and needing clearerr(). What if other applets |
| 10354 | * also can get EINTRs? Do we need to switch |
| 10355 | * our signals to SA_RESTART? |
| 10356 | */ |
| 10357 | /*clearerr(stdout);*/ |
| 10358 | INT_ON; |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10359 | break; |
| 10360 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10361 | #endif |
Denys Vlasenko | cfd392b | 2017-08-03 19:56:29 +0200 | [diff] [blame] | 10362 | /* Can we avoid forking? For example, very last command |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10363 | * in a script or a subshell does not need forking, |
| 10364 | * we can just exec it. |
| 10365 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10366 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10367 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10368 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 10369 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10370 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10371 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10372 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10373 | break; |
| 10374 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10375 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10376 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 10377 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10378 | } |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 10379 | shellexec(argv[0], argv, path, cmdentry.u.index); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10380 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10381 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10382 | case CMDBUILTIN: |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10383 | if (evalbltin(cmdentry.u.cmd, argc, argv, flags) |
| 10384 | && !(exception_type == EXERROR && spclbltin <= 0) |
| 10385 | ) { |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 10386 | raise: |
| 10387 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10388 | } |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10389 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10390 | |
| 10391 | case CMDFUNCTION: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10392 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 10393 | goto raise; |
| 10394 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10395 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10396 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10397 | status = waitforjob(jp); |
Ron Yorston | 6cda0b0 | 2020-02-21 16:16:56 +0000 | [diff] [blame] | 10398 | if (jp) |
| 10399 | TRACE(("forked child exited with %d\n", status)); |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10400 | FORCE_INT_ON; |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10401 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10402 | out: |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 10403 | if (cmd->ncmd.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 10404 | popredir(/*drop:*/ cmd_is_exec); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10405 | unwindredir(redir_stop); |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10406 | unwindfiles(file_stop); |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10407 | unwindlocalvars(localvar_stop); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10408 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10409 | /* dsl: I think this is intended to be used to support |
| 10410 | * '_' in 'vi' command mode during line editing... |
| 10411 | * However I implemented that within libedit itself. |
| 10412 | */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10413 | setvar0("_", lastarg); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10414 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10415 | |
| 10416 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10417 | } |
| 10418 | |
| 10419 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10420 | evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10421 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10422 | char *volatile savecmdname; |
| 10423 | struct jmploc *volatile savehandler; |
| 10424 | struct jmploc jmploc; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10425 | int status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10426 | int i; |
| 10427 | |
| 10428 | savecmdname = commandname; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 10429 | savehandler = exception_handler; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10430 | i = setjmp(jmploc.loc); |
| 10431 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10432 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10433 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10434 | commandname = argv[0]; |
| 10435 | argptr = argv + 1; |
| 10436 | optptr = NULL; /* initialize nextopt */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10437 | if (cmd == EVALCMD) |
| 10438 | status = evalcmd(argc, argv, flags); |
| 10439 | else |
| 10440 | status = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10441 | flush_stdout_stderr(); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10442 | status |= ferror(stdout); |
| 10443 | exitstatus = status; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10444 | cmddone: |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 10445 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10446 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10447 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10448 | |
| 10449 | return i; |
| 10450 | } |
| 10451 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10452 | static int |
| 10453 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10454 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 10455 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10456 | } |
| 10457 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10458 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10459 | /* |
| 10460 | * Search for a command. This is called before we fork so that the |
| 10461 | * 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] | 10462 | * the child. The check for "goodname" is an overly conservative |
| 10463 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10464 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10465 | static void |
| 10466 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10467 | { |
| 10468 | struct cmdentry entry; |
| 10469 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10470 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 10471 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10472 | } |
| 10473 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10474 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10475 | /* ============ Builtin commands |
| 10476 | * |
| 10477 | * Builtin commands whose functions are closely tied to evaluation |
| 10478 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10479 | */ |
| 10480 | |
| 10481 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10482 | * Handle break and continue commands. Break, continue, and return are |
| 10483 | * all handled by setting the evalskip flag. The evaluation routines |
| 10484 | * above all check this flag, and if it is set they start skipping |
| 10485 | * commands rather than executing them. The variable skipcount is |
| 10486 | * the number of loops to break/continue, or the number of function |
| 10487 | * levels to return. (The latter is always 1.) It should probably |
| 10488 | * be an error to break out of more loops than exist, but it isn't |
| 10489 | * in the standard shell so we don't make it one here. |
| 10490 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10491 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10492 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10493 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10494 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10495 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 10496 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 10497 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10498 | if (n > loopnest) |
| 10499 | n = loopnest; |
| 10500 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10501 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10502 | skipcount = n; |
| 10503 | } |
| 10504 | return 0; |
| 10505 | } |
| 10506 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10507 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 10508 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10509 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10510 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10511 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10512 | enum { |
| 10513 | INPUT_PUSH_FILE = 1, |
| 10514 | INPUT_NOFILE_OK = 2, |
| 10515 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10516 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10517 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10518 | /* values of checkkwd variable */ |
| 10519 | #define CHKALIAS 0x1 |
| 10520 | #define CHKKWD 0x2 |
| 10521 | #define CHKNL 0x4 |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 10522 | #define CHKEOFMARK 0x8 |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10523 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10524 | /* |
| 10525 | * Push a string back onto the input at this current parsefile level. |
| 10526 | * We handle aliases this way. |
| 10527 | */ |
| 10528 | #if !ENABLE_ASH_ALIAS |
| 10529 | #define pushstring(s, ap) pushstring(s) |
| 10530 | #endif |
| 10531 | static void |
| 10532 | pushstring(char *s, struct alias *ap) |
| 10533 | { |
| 10534 | struct strpush *sp; |
| 10535 | int len; |
| 10536 | |
| 10537 | len = strlen(s); |
| 10538 | INT_OFF; |
| 10539 | if (g_parsefile->strpush) { |
| 10540 | sp = ckzalloc(sizeof(*sp)); |
| 10541 | sp->prev = g_parsefile->strpush; |
| 10542 | } else { |
| 10543 | sp = &(g_parsefile->basestrpush); |
| 10544 | } |
| 10545 | g_parsefile->strpush = sp; |
| 10546 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 10547 | sp->prev_left_in_line = g_parsefile->left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10548 | sp->unget = g_parsefile->unget; |
| 10549 | memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10550 | #if ENABLE_ASH_ALIAS |
| 10551 | sp->ap = ap; |
| 10552 | if (ap) { |
| 10553 | ap->flag |= ALIASINUSE; |
| 10554 | sp->string = s; |
| 10555 | } |
| 10556 | #endif |
| 10557 | g_parsefile->next_to_pgetc = s; |
| 10558 | g_parsefile->left_in_line = len; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10559 | g_parsefile->unget = 0; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10560 | INT_ON; |
| 10561 | } |
| 10562 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10563 | static void |
| 10564 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10565 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10566 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10567 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10568 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10569 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10570 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10571 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 10572 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 10573 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10574 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10575 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10576 | if (sp->string != sp->ap->val) { |
| 10577 | free(sp->string); |
| 10578 | } |
| 10579 | sp->ap->flag &= ~ALIASINUSE; |
| 10580 | if (sp->ap->flag & ALIASDEAD) { |
| 10581 | unalias(sp->ap->name); |
| 10582 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10583 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10584 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10585 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 10586 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10587 | g_parsefile->unget = sp->unget; |
| 10588 | memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10589 | g_parsefile->strpush = sp->prev; |
| 10590 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10591 | free(sp); |
| 10592 | INT_ON; |
| 10593 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10594 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10595 | static int |
| 10596 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10597 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10598 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 10599 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10600 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10601 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 10602 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 4ac35a3 | 2020-11-16 13:09:37 +0100 | [diff] [blame] | 10603 | /* retry: */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10604 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10605 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10606 | else { |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10607 | # if ENABLE_ASH_IDLE_TIMEOUT |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10608 | int timeout = -1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 10609 | const char *tmout_var = lookupvar("TMOUT"); |
| 10610 | if (tmout_var) { |
| 10611 | timeout = atoi(tmout_var) * 1000; |
| 10612 | if (timeout <= 0) |
| 10613 | timeout = -1; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10614 | } |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10615 | line_input_state->timeout = timeout; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10616 | # endif |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10617 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10618 | line_input_state->path_lookup = pathval(); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10619 | # endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 10620 | reinit_unicode_for_ash(); |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10621 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10622 | if (nr == 0) { |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 10623 | /* ^C pressed, "convert" to SIGINT */ |
| 10624 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 4ac35a3 | 2020-11-16 13:09:37 +0100 | [diff] [blame] | 10625 | raise(SIGINT); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10626 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10627 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10628 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10629 | return 1; |
| 10630 | } |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 10631 | exitstatus = 128 + SIGINT; |
Denys Vlasenko | 4ac35a3 | 2020-11-16 13:09:37 +0100 | [diff] [blame] | 10632 | return -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10633 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10634 | if (nr < 0) { |
| 10635 | if (errno == 0) { |
| 10636 | /* Ctrl+D pressed */ |
| 10637 | nr = 0; |
| 10638 | } |
| 10639 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 10640 | else if (errno == EAGAIN && timeout > 0) { |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 10641 | puts("\007timed out waiting for input: auto-logout"); |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10642 | exitshell(); |
| 10643 | } |
| 10644 | # endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10645 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10646 | } |
| 10647 | #else |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10648 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10649 | #endif |
| 10650 | |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 10651 | #if 0 /* disabled: nonblock_immune_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10652 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10653 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 10654 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 10655 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 10656 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10657 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 10658 | out2str("sh: turning off NDELAY mode\n"); |
| 10659 | goto retry; |
| 10660 | } |
| 10661 | } |
| 10662 | } |
| 10663 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 10664 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10665 | return nr; |
| 10666 | } |
| 10667 | |
| 10668 | /* |
| 10669 | * Refill the input buffer and return the next input character: |
| 10670 | * |
| 10671 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10672 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 10673 | * or we are reading from a string so we can't refill the buffer, |
| 10674 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 10675 | * 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] | 10676 | * 4) Process input up to the next newline, deleting nul characters. |
| 10677 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10678 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 10679 | #define pgetc_debug(...) ((void)0) |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10680 | static int pgetc(void); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10681 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10682 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10683 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10684 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10685 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10686 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10687 | if (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10688 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10689 | if (g_parsefile->left_in_line == -1 |
| 10690 | && g_parsefile->strpush->ap |
| 10691 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 10692 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 10693 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10694 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10695 | return PEOA; |
| 10696 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10697 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10698 | popstring(); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10699 | return pgetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10700 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10701 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10702 | * "pgetc" needs refilling. |
| 10703 | */ |
| 10704 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 10705 | /* -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] | 10706 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 10707 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10708 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10709 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10710 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10711 | /* even in failure keep left_in_line and next_to_pgetc |
| 10712 | * in lock step, for correct multi-layer pungetc. |
| 10713 | * left_in_line was decremented before preadbuffer(), |
| 10714 | * must inc next_to_pgetc: */ |
| 10715 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10716 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10717 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10718 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10719 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10720 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10721 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10722 | again: |
| 10723 | more = preadfd(); |
| 10724 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10725 | /* don't try reading again */ |
| 10726 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10727 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10728 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10729 | return PEOF; |
| 10730 | } |
| 10731 | } |
| 10732 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10733 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10734 | * Set g_parsefile->left_in_line |
| 10735 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10736 | * NUL chars are deleted. |
| 10737 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10738 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10739 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10740 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10741 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10742 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10743 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10744 | c = *q; |
| 10745 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10746 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10747 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10748 | q++; |
| 10749 | if (c == '\n') { |
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; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10751 | break; |
| 10752 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10753 | } |
| 10754 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10755 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10756 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 10757 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10759 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10760 | } |
| 10761 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10762 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10763 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10764 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10765 | char save = *q; |
| 10766 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10767 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10768 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10769 | } |
| 10770 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10771 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 10772 | g_parsefile->left_in_line, |
| 10773 | g_parsefile->next_to_pgetc, |
| 10774 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10775 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10776 | } |
| 10777 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10778 | static void |
| 10779 | nlprompt(void) |
| 10780 | { |
| 10781 | g_parsefile->linno++; |
| 10782 | setprompt_if(doprompt, 2); |
| 10783 | } |
| 10784 | static void |
| 10785 | nlnoprompt(void) |
| 10786 | { |
| 10787 | g_parsefile->linno++; |
| 10788 | needprompt = doprompt; |
| 10789 | } |
| 10790 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10791 | static int |
| 10792 | pgetc(void) |
| 10793 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10794 | int c; |
| 10795 | |
| 10796 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10797 | g_parsefile->left_in_line, |
| 10798 | g_parsefile->next_to_pgetc, |
| 10799 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10800 | if (g_parsefile->unget) |
| 10801 | return g_parsefile->lastc[--g_parsefile->unget]; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10802 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10803 | if (--g_parsefile->left_in_line >= 0) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 10804 | c = (unsigned char)*g_parsefile->next_to_pgetc++; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10805 | else |
| 10806 | c = preadbuffer(); |
| 10807 | |
| 10808 | g_parsefile->lastc[1] = g_parsefile->lastc[0]; |
| 10809 | g_parsefile->lastc[0] = c; |
| 10810 | |
| 10811 | return c; |
| 10812 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10813 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10814 | #if ENABLE_ASH_ALIAS |
| 10815 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10816 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10817 | { |
| 10818 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10819 | do { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10820 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10821 | g_parsefile->left_in_line, |
| 10822 | g_parsefile->next_to_pgetc, |
| 10823 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10824 | c = pgetc(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10825 | } while (c == PEOA); |
| 10826 | return c; |
| 10827 | } |
| 10828 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 10829 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10830 | #endif |
| 10831 | |
| 10832 | /* |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10833 | * Undo a call to pgetc. Only two characters may be pushed back. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10834 | * PEOF may be pushed back. |
| 10835 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10836 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10837 | pungetc(void) |
| 10838 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10839 | g_parsefile->unget++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10840 | } |
| 10841 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 10842 | /* This one eats backslash+newline */ |
| 10843 | static int |
| 10844 | pgetc_eatbnl(void) |
| 10845 | { |
| 10846 | int c; |
| 10847 | |
| 10848 | while ((c = pgetc()) == '\\') { |
| 10849 | if (pgetc() != '\n') { |
| 10850 | pungetc(); |
| 10851 | break; |
| 10852 | } |
| 10853 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10854 | nlprompt(); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 10855 | } |
| 10856 | |
| 10857 | return c; |
| 10858 | } |
| 10859 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 10860 | struct synstack { |
| 10861 | smalluint syntax; |
| 10862 | uint8_t innerdq :1; |
| 10863 | uint8_t varpushed :1; |
| 10864 | uint8_t dblquote :1; |
| 10865 | int varnest; /* levels of variables expansion */ |
| 10866 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 10867 | int parenlevel; /* levels of parens in arithmetic */ |
| 10868 | struct synstack *prev; |
| 10869 | struct synstack *next; |
| 10870 | }; |
| 10871 | |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 10872 | static int |
| 10873 | pgetc_top(struct synstack *stack) |
| 10874 | { |
| 10875 | return stack->syntax == SQSYNTAX ? pgetc() : pgetc_eatbnl(); |
| 10876 | } |
| 10877 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 10878 | static void |
| 10879 | synstack_push(struct synstack **stack, struct synstack *next, int syntax) |
| 10880 | { |
| 10881 | memset(next, 0, sizeof(*next)); |
| 10882 | next->syntax = syntax; |
| 10883 | next->next = *stack; |
| 10884 | (*stack)->prev = next; |
| 10885 | *stack = next; |
| 10886 | } |
| 10887 | |
| 10888 | static ALWAYS_INLINE void |
| 10889 | synstack_pop(struct synstack **stack) |
| 10890 | { |
| 10891 | *stack = (*stack)->next; |
| 10892 | } |
| 10893 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10894 | /* |
| 10895 | * To handle the "." command, a stack of input files is used. Pushfile |
| 10896 | * adds a new entry to the stack and popfile restores the previous level. |
| 10897 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10898 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10899 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10900 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10901 | struct parsefile *pf; |
| 10902 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10903 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10904 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10905 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10906 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 10907 | /*pf->basestrpush.prev = NULL;*/ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10908 | /*pf->unget = 0;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10909 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10910 | } |
| 10911 | |
| 10912 | static void |
| 10913 | popfile(void) |
| 10914 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10915 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10916 | |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 10917 | if (pf == &basepf) |
| 10918 | return; |
| 10919 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10920 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10921 | if (pf->pf_fd >= 0) |
| 10922 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 10923 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10924 | while (pf->strpush) |
| 10925 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10926 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10927 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10928 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10929 | } |
| 10930 | |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10931 | static void |
| 10932 | unwindfiles(struct parsefile *stop) |
| 10933 | { |
| 10934 | while (g_parsefile != stop) |
| 10935 | popfile(); |
| 10936 | } |
| 10937 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10938 | /* |
| 10939 | * Return to top level. |
| 10940 | */ |
| 10941 | static void |
| 10942 | popallfiles(void) |
| 10943 | { |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10944 | unwindfiles(&basepf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10945 | } |
| 10946 | |
| 10947 | /* |
| 10948 | * Close the file(s) that the shell is reading commands from. Called |
| 10949 | * after a fork is done. |
| 10950 | */ |
| 10951 | static void |
| 10952 | closescript(void) |
| 10953 | { |
| 10954 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10955 | if (g_parsefile->pf_fd > 0) { |
| 10956 | close(g_parsefile->pf_fd); |
| 10957 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10958 | } |
| 10959 | } |
| 10960 | |
| 10961 | /* |
| 10962 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 10963 | * interrupts off. |
| 10964 | */ |
| 10965 | static void |
| 10966 | setinputfd(int fd, int push) |
| 10967 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10968 | if (push) { |
| 10969 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10970 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10971 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10972 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10973 | if (g_parsefile->buf == NULL) |
| 10974 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10975 | g_parsefile->left_in_buffer = 0; |
| 10976 | g_parsefile->left_in_line = 0; |
| 10977 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10978 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10979 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10980 | /* |
| 10981 | * Set the input to take input from a file. If push is set, push the |
| 10982 | * old input onto the stack first. |
| 10983 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10984 | static int |
| 10985 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10986 | { |
| 10987 | int fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10988 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10989 | INT_OFF; |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 10990 | fd = open(fname, O_RDONLY | O_CLOEXEC); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10991 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10992 | if (flags & INPUT_NOFILE_OK) |
| 10993 | goto out; |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10994 | exitstatus = 127; |
Johannes Schindelin | 20a63b2 | 2017-08-22 22:03:17 +0200 | [diff] [blame] | 10995 | ash_msg_and_raise_perror("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10996 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 10997 | if (fd < 10) |
| 10998 | fd = savefd(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 10999 | else if (O_CLOEXEC == 0) /* old libc */ |
Denys Vlasenko | e19923f | 2016-10-26 15:38:44 +0200 | [diff] [blame] | 11000 | close_on_exec_on(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 11001 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11002 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11003 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11004 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11005 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11006 | } |
| 11007 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11008 | /* |
| 11009 | * Like setinputfile, but takes input from a string. |
| 11010 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11011 | static void |
| 11012 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11013 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11014 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11015 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11016 | g_parsefile->next_to_pgetc = string; |
| 11017 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11018 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11019 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11020 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11021 | } |
| 11022 | |
| 11023 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 11024 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11025 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11026 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11027 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11028 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11029 | |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11030 | /* Hash of mtimes of mailboxes */ |
| 11031 | static unsigned mailtime_hash; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11032 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11033 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11034 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11035 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11036 | * Print appropriate message(s) if mail has arrived. |
| 11037 | * If mail_var_path_changed is set, |
| 11038 | * then the value of MAIL has mail_var_path_changed, |
| 11039 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11040 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11041 | static void |
| 11042 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11043 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11044 | const char *mpath; |
| 11045 | char *p; |
| 11046 | char *q; |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11047 | unsigned new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11048 | struct stackmark smark; |
| 11049 | struct stat statb; |
| 11050 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11051 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11052 | mpath = mpathset() ? mpathval() : mailval(); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11053 | new_hash = 0; |
| 11054 | for (;;) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11055 | int len; |
| 11056 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 11057 | len = padvance_magic(&mpath, nullstr, 2); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11058 | if (!len) |
| 11059 | break; |
| 11060 | p = stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11061 | break; |
| 11062 | if (*p == '\0') |
| 11063 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11064 | for (q = p; *q; q++) |
| 11065 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 11066 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11067 | if (q[-1] != '/') |
| 11068 | abort(); |
| 11069 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11070 | q[-1] = '\0'; /* delete trailing '/' */ |
| 11071 | if (stat(p, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11072 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11073 | } |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11074 | /* Very simplistic "hash": just a sum of all mtimes */ |
| 11075 | new_hash += (unsigned)statb.st_mtime; |
| 11076 | } |
| 11077 | if (!mail_var_path_changed && mailtime_hash != new_hash) { |
Denys Vlasenko | 4cd99e7 | 2015-10-09 16:02:53 +0200 | [diff] [blame] | 11078 | if (mailtime_hash != 0) |
| 11079 | out2str("you have mail\n"); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11080 | mailtime_hash = new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11081 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11082 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11083 | popstackmark(&smark); |
| 11084 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11085 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11086 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11087 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11088 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11089 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11090 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11091 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11092 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11093 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11094 | |
| 11095 | /* ============ ??? */ |
| 11096 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11097 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11098 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11099 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11100 | static void |
| 11101 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11102 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11103 | char **newparam; |
| 11104 | char **ap; |
| 11105 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11106 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11107 | for (nparam = 0; argv[nparam]; nparam++) |
| 11108 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11109 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 11110 | while (*argv) { |
| 11111 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11112 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11113 | *ap = NULL; |
| 11114 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11115 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11116 | shellparam.nparam = nparam; |
| 11117 | shellparam.p = newparam; |
| 11118 | #if ENABLE_ASH_GETOPTS |
| 11119 | shellparam.optind = 1; |
| 11120 | shellparam.optoff = -1; |
| 11121 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11122 | } |
| 11123 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11124 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11125 | * Process shell options. The global variable argptr contains a pointer |
| 11126 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11127 | * |
| 11128 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 11129 | * For a non-interactive shell, an error condition encountered |
| 11130 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 11131 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 11132 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11133 | * ... |
| 11134 | * Utility syntax error (option or operand error) Shall exit |
| 11135 | * ... |
| 11136 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 11137 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 11138 | * and shell continues), and people rely on this behavior! |
| 11139 | * Testcase: |
| 11140 | * set -o barfoo 2>/dev/null |
| 11141 | * echo $? |
| 11142 | * |
| 11143 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11144 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11145 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11146 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11147 | { |
| 11148 | int i; |
| 11149 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11150 | if (name) { |
| 11151 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11152 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11153 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11154 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11155 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11156 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11157 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11158 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11159 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11160 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | 2f9c124 | 2019-08-02 16:43:36 +0200 | [diff] [blame] | 11161 | if (optnames(i)[0] == '\0') |
| 11162 | continue; |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11163 | if (val) { |
| 11164 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 11165 | } else { |
| 11166 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 11167 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11168 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11169 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11170 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11171 | static void |
| 11172 | setoption(int flag, int val) |
| 11173 | { |
| 11174 | int i; |
| 11175 | |
| 11176 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11177 | if (optletters(i) == flag && optnames(i)[0] != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11178 | optlist[i] = val; |
| 11179 | return; |
| 11180 | } |
| 11181 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11182 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11183 | /* NOTREACHED */ |
| 11184 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11185 | /* If login_sh is not NULL, we are called to parse command line opts, |
| 11186 | * not "set -opts" |
| 11187 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11188 | static int |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11189 | options(int *login_sh) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11190 | { |
| 11191 | char *p; |
| 11192 | int val; |
| 11193 | int c; |
| 11194 | |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11195 | if (login_sh) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11196 | minusc = NULL; |
| 11197 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11198 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11199 | if (c != '-' && c != '+') |
| 11200 | break; |
| 11201 | argptr++; |
| 11202 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11203 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11204 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11205 | if (p[0] == '\0' || LONE_DASH(p)) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11206 | if (!login_sh) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11207 | /* "-" means turn off -x and -v */ |
| 11208 | if (p[0] == '\0') |
| 11209 | xflag = vflag = 0; |
| 11210 | /* "--" means reset params */ |
| 11211 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11212 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11213 | } |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11214 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11215 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11216 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11217 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11218 | while ((c = *p++) != '\0') { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11219 | if (login_sh) { |
| 11220 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
| 11221 | if (c == 'c') { |
| 11222 | minusc = p; /* command is after shell args */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11223 | cflag = 1; |
| 11224 | continue; |
| 11225 | } |
| 11226 | if (c == 's') { /* -s, +s */ |
| 11227 | sflag = 1; |
| 11228 | continue; |
| 11229 | } |
| 11230 | if (c == 'i') { /* -i, +i */ |
| 11231 | iflag = 1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11232 | continue; |
| 11233 | } |
| 11234 | if (c == 'l') { |
| 11235 | *login_sh = 1; /* -l or +l == --login */ |
| 11236 | continue; |
| 11237 | } |
| 11238 | /* bash does not accept +-login, we also won't */ |
| 11239 | if (val && (c == '-')) { /* long options */ |
| 11240 | if (strcmp(p, "login") == 0) { |
| 11241 | *login_sh = 1; |
| 11242 | } |
| 11243 | break; |
| 11244 | } |
| 11245 | } |
| 11246 | if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11247 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11248 | /* it already printed err message */ |
| 11249 | return 1; /* error */ |
| 11250 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11251 | if (*argptr) |
| 11252 | argptr++; |
| 11253 | } else { |
| 11254 | setoption(c, val); |
| 11255 | } |
| 11256 | } |
| 11257 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11258 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11259 | } |
| 11260 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11261 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11262 | * The shift builtin command. |
| 11263 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11264 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11265 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11266 | { |
| 11267 | int n; |
| 11268 | char **ap1, **ap2; |
| 11269 | |
| 11270 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11271 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11272 | n = number(argv[1]); |
| 11273 | if (n > shellparam.nparam) |
Ingo van Lil | 9c8e94b | 2018-01-05 15:04:23 +0100 | [diff] [blame] | 11274 | return 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11275 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11276 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11277 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11278 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11279 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11280 | } |
| 11281 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11282 | while ((*ap2++ = *ap1++) != NULL) |
| 11283 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11284 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11285 | shellparam.optind = 1; |
| 11286 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11287 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11288 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11289 | return 0; |
| 11290 | } |
| 11291 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11292 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11293 | * POSIX requires that 'set' (but not export or readonly) output the |
| 11294 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 11295 | * Maybe we could keep them in an ordered balanced binary tree |
| 11296 | * instead of hashed lists. |
| 11297 | * For now just roll 'em through qsort for printing... |
| 11298 | */ |
| 11299 | static int |
| 11300 | showvars(const char *sep_prefix, int on, int off) |
| 11301 | { |
| 11302 | const char *sep; |
| 11303 | char **ep, **epend; |
| 11304 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 11305 | ep = listvars(on, off, /*strlist:*/ NULL, &epend); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11306 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 11307 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11308 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11309 | |
| 11310 | for (; ep < epend; ep++) { |
| 11311 | const char *p; |
| 11312 | const char *q; |
| 11313 | |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11314 | p = endofname(*ep); |
| 11315 | /* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this |
| 11316 | * makes "export -p" to have output not suitable for "eval": |
| 11317 | * import os |
| 11318 | * os.environ["test-test"]="test" |
| 11319 | * if os.fork() == 0: |
| 11320 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 11321 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) |
| 11322 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11323 | q = nullstr; |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11324 | if (*p == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11325 | q = single_quote(++p); |
| 11326 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 11327 | } |
| 11328 | return 0; |
| 11329 | } |
| 11330 | |
| 11331 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11332 | * The set command builtin. |
| 11333 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11334 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11335 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11336 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11337 | int retval; |
| 11338 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11339 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11340 | return showvars(nullstr, 0, VUNSET); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11341 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11342 | INT_OFF; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11343 | retval = options(/*login_sh:*/ NULL); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11344 | if (retval == 0) { /* if no parse error... */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11345 | optschanged(); |
| 11346 | if (*argptr != NULL) { |
| 11347 | setparam(argptr); |
| 11348 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11349 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11350 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11351 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11352 | } |
| 11353 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11354 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11355 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11356 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11357 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11358 | uint32_t t; |
| 11359 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11360 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11361 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11362 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11363 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 11364 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11365 | vrandom.flags &= ~VNOFUNC; |
| 11366 | } else { |
| 11367 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11368 | t = strtoul(value, NULL, 10); |
| 11369 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11370 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11371 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11372 | #endif |
| 11373 | |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11374 | #if BASH_EPOCH_VARS |
| 11375 | static void FAST_FUNC |
| 11376 | change_epoch(struct var *vepoch, const char *fmt) |
| 11377 | { |
| 11378 | struct timeval tv; |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11379 | char buffer[sizeof("%llu.nnnnnn") + sizeof(long long)*3]; |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11380 | |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11381 | xgettimeofday(&tv); |
| 11382 | sprintf(buffer, fmt, (unsigned long long)tv.tv_sec, (unsigned)tv.tv_usec); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11383 | setvar(vepoch->var_text, buffer, VNOFUNC); |
| 11384 | vepoch->flags &= ~VNOFUNC; |
| 11385 | } |
| 11386 | |
| 11387 | static void FAST_FUNC |
| 11388 | change_seconds(const char *value UNUSED_PARAM) |
| 11389 | { |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11390 | change_epoch(&vepochs, "%llu"); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11391 | } |
| 11392 | |
| 11393 | static void FAST_FUNC |
| 11394 | change_realtime(const char *value UNUSED_PARAM) |
| 11395 | { |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11396 | change_epoch(&vepochr, "%llu.%06u"); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11397 | } |
| 11398 | #endif |
| 11399 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11400 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11401 | static int |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11402 | getopts(char *optstr, char *optvar, char **optfirst) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11403 | { |
| 11404 | char *p, *q; |
| 11405 | char c = '?'; |
| 11406 | int done = 0; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11407 | char sbuf[2]; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11408 | char **optnext; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11409 | int ind = shellparam.optind; |
| 11410 | int off = shellparam.optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11411 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11412 | sbuf[1] = '\0'; |
| 11413 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11414 | shellparam.optind = -1; |
| 11415 | optnext = optfirst + ind - 1; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11416 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11417 | if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11418 | p = NULL; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11419 | else |
| 11420 | p = optnext[-1] + off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11421 | if (p == NULL || *p == '\0') { |
| 11422 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11423 | p = *optnext; |
| 11424 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11425 | atend: |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11426 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11427 | p = NULL; |
| 11428 | done = 1; |
| 11429 | goto out; |
| 11430 | } |
| 11431 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11432 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11433 | goto atend; |
| 11434 | } |
| 11435 | |
| 11436 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 11437 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11438 | if (*q == '\0') { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11439 | /* OPTERR is a bashism */ |
| 11440 | const char *cp = lookupvar("OPTERR"); |
| 11441 | if ((cp && LONE_CHAR(cp, '0')) |
| 11442 | || (optstr[0] == ':') |
| 11443 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11444 | sbuf[0] = c; |
| 11445 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11446 | setvar0("OPTARG", sbuf); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11447 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11448 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11449 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11450 | } |
| 11451 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11452 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11453 | } |
| 11454 | if (*++q == ':') |
| 11455 | q++; |
| 11456 | } |
| 11457 | |
| 11458 | if (*++q == ':') { |
| 11459 | if (*p == '\0' && (p = *optnext) == NULL) { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11460 | /* OPTERR is a bashism */ |
| 11461 | const char *cp = lookupvar("OPTERR"); |
| 11462 | if ((cp && LONE_CHAR(cp, '0')) |
| 11463 | || (optstr[0] == ':') |
| 11464 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11465 | sbuf[0] = c; |
| 11466 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11467 | setvar0("OPTARG", sbuf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11468 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11469 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11470 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11471 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11472 | c = '?'; |
| 11473 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11474 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11475 | } |
| 11476 | |
| 11477 | if (p == *optnext) |
| 11478 | optnext++; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11479 | setvar0("OPTARG", p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11480 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11481 | } else |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11482 | setvar0("OPTARG", nullstr); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11483 | out: |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11484 | ind = optnext - optfirst + 1; |
| 11485 | setvar("OPTIND", itoa(ind), VNOFUNC); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11486 | sbuf[0] = c; |
| 11487 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11488 | setvar0(optvar, sbuf); |
| 11489 | |
| 11490 | shellparam.optoff = p ? p - *(optnext - 1) : -1; |
| 11491 | shellparam.optind = ind; |
| 11492 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11493 | return done; |
| 11494 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11495 | |
| 11496 | /* |
| 11497 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 11498 | * to be processed. Shellparam.optptr points to the next character to |
| 11499 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 11500 | * then it's the first time getopts has been called. |
| 11501 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11502 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11503 | getoptscmd(int argc, char **argv) |
| 11504 | { |
| 11505 | char **optbase; |
| 11506 | |
| 11507 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 11508 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11509 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11510 | optbase = shellparam.p; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11511 | if ((unsigned)shellparam.optind > shellparam.nparam + 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11512 | shellparam.optind = 1; |
| 11513 | shellparam.optoff = -1; |
| 11514 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11515 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11516 | optbase = &argv[3]; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11517 | if ((unsigned)shellparam.optind > argc - 2) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11518 | shellparam.optind = 1; |
| 11519 | shellparam.optoff = -1; |
| 11520 | } |
| 11521 | } |
| 11522 | |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11523 | return getopts(argv[1], argv[2], optbase); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11524 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11525 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11526 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11527 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11528 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11529 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11530 | struct heredoc { |
| 11531 | struct heredoc *next; /* next here document in list */ |
| 11532 | union node *here; /* redirection node */ |
| 11533 | char *eofmark; /* string indicating end of input */ |
| 11534 | smallint striptabs; /* if set, strip leading tabs */ |
| 11535 | }; |
| 11536 | |
| 11537 | static smallint tokpushback; /* last token pushed back */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11538 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 11539 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 11540 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 11541 | static char *wordtext; /* text of last word returned by readtoken */ |
| 11542 | static struct nodelist *backquotelist; |
| 11543 | static union node *redirnode; |
| 11544 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11545 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11546 | static const char * |
| 11547 | tokname(char *buf, int tok) |
| 11548 | { |
| 11549 | if (tok < TSEMI) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11550 | return tokname_array[tok]; |
| 11551 | sprintf(buf, "\"%s\"", tokname_array[tok]); |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11552 | return buf; |
| 11553 | } |
| 11554 | |
| 11555 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11556 | * Called when an unexpected token is read during the parse. The argument |
| 11557 | * is the token that is expected, or -1 if more than one type of token can |
| 11558 | * occur at this point. |
| 11559 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11560 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11561 | static void |
| 11562 | raise_error_unexpected_syntax(int token) |
| 11563 | { |
| 11564 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11565 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11566 | int l; |
| 11567 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11568 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11569 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11570 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11571 | raise_error_syntax(msg); |
| 11572 | /* NOTREACHED */ |
| 11573 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11574 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11575 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 11576 | static union node *andor(void); |
| 11577 | static union node *pipeline(void); |
| 11578 | static union node *parse_command(void); |
| 11579 | static void parseheredoc(void); |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11580 | static int peektoken(void); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11581 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11582 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11583 | static union node * |
| 11584 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11585 | { |
| 11586 | union node *n1, *n2, *n3; |
| 11587 | int tok; |
| 11588 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11589 | n1 = NULL; |
| 11590 | for (;;) { |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11591 | switch (readtoken()) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11592 | case TNL: |
| 11593 | if (!(nlflag & 1)) |
| 11594 | break; |
| 11595 | parseheredoc(); |
| 11596 | return n1; |
| 11597 | |
| 11598 | case TEOF: |
| 11599 | if (!n1 && (nlflag & 1)) |
| 11600 | n1 = NODE_EOF; |
| 11601 | parseheredoc(); |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11602 | tokpushback++; |
| 11603 | lasttoken = TEOF; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11604 | return n1; |
| 11605 | } |
| 11606 | |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11607 | tokpushback++; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11608 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11609 | if (nlflag == 2 && ((1 << peektoken()) & tokendlist)) |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11610 | return n1; |
| 11611 | nlflag |= 2; |
| 11612 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11613 | n2 = andor(); |
| 11614 | tok = readtoken(); |
| 11615 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11616 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11617 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11618 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11619 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11620 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11621 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11622 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11623 | n2 = n3; |
| 11624 | } |
| 11625 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11626 | } |
| 11627 | } |
| 11628 | if (n1 == NULL) { |
| 11629 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11630 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11631 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11632 | n3->type = NSEMI; |
| 11633 | n3->nbinary.ch1 = n1; |
| 11634 | n3->nbinary.ch2 = n2; |
| 11635 | n1 = n3; |
| 11636 | } |
| 11637 | switch (tok) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11638 | case TNL: |
| 11639 | case TEOF: |
| 11640 | tokpushback = 1; |
| 11641 | /* fall through */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11642 | case TBACKGND: |
| 11643 | case TSEMI: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11644 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11645 | default: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11646 | if ((nlflag & 1)) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11647 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11648 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11649 | return n1; |
| 11650 | } |
| 11651 | } |
| 11652 | } |
| 11653 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11654 | static union node * |
| 11655 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11656 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11657 | union node *n1, *n2, *n3; |
| 11658 | int t; |
| 11659 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11660 | n1 = pipeline(); |
| 11661 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11662 | t = readtoken(); |
| 11663 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11664 | t = NAND; |
| 11665 | } else if (t == TOR) { |
| 11666 | t = NOR; |
| 11667 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11668 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11669 | return n1; |
| 11670 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11671 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11672 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11673 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11674 | n3->type = t; |
| 11675 | n3->nbinary.ch1 = n1; |
| 11676 | n3->nbinary.ch2 = n2; |
| 11677 | n1 = n3; |
| 11678 | } |
| 11679 | } |
| 11680 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11681 | static union node * |
| 11682 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11683 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11684 | union node *n1, *n2, *pipenode; |
| 11685 | struct nodelist *lp, *prev; |
| 11686 | int negate; |
| 11687 | |
| 11688 | negate = 0; |
| 11689 | TRACE(("pipeline: entered\n")); |
| 11690 | if (readtoken() == TNOT) { |
| 11691 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11692 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11693 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11694 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11695 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11696 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11697 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11698 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11699 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11700 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11701 | pipenode->npipe.cmdlist = lp; |
| 11702 | lp->n = n1; |
| 11703 | do { |
| 11704 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11705 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11706 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11707 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11708 | prev->next = lp; |
| 11709 | } while (readtoken() == TPIPE); |
| 11710 | lp->next = NULL; |
| 11711 | n1 = pipenode; |
| 11712 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11713 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11714 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11715 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11716 | n2->type = NNOT; |
| 11717 | n2->nnot.com = n1; |
| 11718 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11719 | } |
| 11720 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11721 | } |
| 11722 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11723 | static union node * |
| 11724 | makename(void) |
| 11725 | { |
| 11726 | union node *n; |
| 11727 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11728 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11729 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11730 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11731 | n->narg.text = wordtext; |
| 11732 | n->narg.backquote = backquotelist; |
| 11733 | return n; |
| 11734 | } |
| 11735 | |
| 11736 | static void |
| 11737 | fixredir(union node *n, const char *text, int err) |
| 11738 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11739 | int fd; |
| 11740 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11741 | TRACE(("Fix redir %s %d\n", text, err)); |
| 11742 | if (!err) |
| 11743 | n->ndup.vname = NULL; |
| 11744 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11745 | fd = bb_strtou(text, NULL, 10); |
| 11746 | if (!errno && fd >= 0) |
| 11747 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11748 | else if (LONE_DASH(text)) |
| 11749 | n->ndup.dupfd = -1; |
| 11750 | else { |
| 11751 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11752 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11753 | n->ndup.vname = makename(); |
| 11754 | } |
| 11755 | } |
| 11756 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11757 | static void |
| 11758 | parsefname(void) |
| 11759 | { |
| 11760 | union node *n = redirnode; |
| 11761 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 11762 | if (n->type == NHERE) |
| 11763 | checkkwd = CHKEOFMARK; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11764 | if (readtoken() != TWORD) |
| 11765 | raise_error_unexpected_syntax(-1); |
| 11766 | if (n->type == NHERE) { |
| 11767 | struct heredoc *here = heredoc; |
| 11768 | struct heredoc *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11769 | |
| 11770 | if (quoteflag == 0) |
| 11771 | n->type = NXHERE; |
| 11772 | TRACE(("Here document %d\n", n->type)); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 11773 | rmescapes(wordtext, 0, NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11774 | here->eofmark = wordtext; |
| 11775 | here->next = NULL; |
| 11776 | if (heredoclist == NULL) |
| 11777 | heredoclist = here; |
| 11778 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11779 | for (p = heredoclist; p->next; p = p->next) |
| 11780 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11781 | p->next = here; |
| 11782 | } |
| 11783 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 11784 | fixredir(n, wordtext, 0); |
| 11785 | } else { |
| 11786 | n->nfile.fname = makename(); |
| 11787 | } |
| 11788 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11789 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11790 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11791 | simplecmd(void) |
| 11792 | { |
| 11793 | union node *args, **app; |
| 11794 | union node *n = NULL; |
| 11795 | union node *vars, **vpp; |
| 11796 | union node **rpp, *redir; |
| 11797 | int savecheckkwd; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11798 | int savelinno; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11799 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11800 | smallint double_brackets_flag = 0; |
| 11801 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11802 | IF_BASH_FUNCTION(smallint function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11803 | |
| 11804 | args = NULL; |
| 11805 | app = &args; |
| 11806 | vars = NULL; |
| 11807 | vpp = &vars; |
| 11808 | redir = NULL; |
| 11809 | rpp = &redir; |
| 11810 | |
| 11811 | savecheckkwd = CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11812 | savelinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11813 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11814 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11815 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11816 | t = readtoken(); |
| 11817 | switch (t) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11818 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11819 | case TFUNCTION: |
| 11820 | if (peektoken() != TWORD) |
| 11821 | raise_error_unexpected_syntax(TWORD); |
| 11822 | function_flag = 1; |
| 11823 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11824 | #endif |
| 11825 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11826 | case TAND: /* "&&" */ |
| 11827 | case TOR: /* "||" */ |
| 11828 | if (!double_brackets_flag) { |
| 11829 | tokpushback = 1; |
| 11830 | goto out; |
| 11831 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 11832 | /* pass "&&" or "||" to [[ ]] as literal args */ |
| 11833 | wordtext = (char *) (t == TAND ? "&&" : "||"); |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11834 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11835 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11836 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11837 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11838 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11839 | n->narg.text = wordtext; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11840 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 11841 | if (strcmp("[[", wordtext) == 0) |
| 11842 | double_brackets_flag = 1; |
| 11843 | else if (strcmp("]]", wordtext) == 0) |
| 11844 | double_brackets_flag = 0; |
| 11845 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11846 | n->narg.backquote = backquotelist; |
| 11847 | if (savecheckkwd && isassignment(wordtext)) { |
| 11848 | *vpp = n; |
| 11849 | vpp = &n->narg.next; |
| 11850 | } else { |
| 11851 | *app = n; |
| 11852 | app = &n->narg.next; |
| 11853 | savecheckkwd = 0; |
| 11854 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11855 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11856 | if (function_flag) { |
| 11857 | checkkwd = CHKNL | CHKKWD; |
| 11858 | switch (peektoken()) { |
| 11859 | case TBEGIN: |
| 11860 | case TIF: |
| 11861 | case TCASE: |
| 11862 | case TUNTIL: |
| 11863 | case TWHILE: |
| 11864 | case TFOR: |
| 11865 | goto do_func; |
| 11866 | case TLP: |
| 11867 | function_flag = 0; |
| 11868 | break; |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 11869 | # if BASH_TEST2 |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11870 | case TWORD: |
| 11871 | if (strcmp("[[", wordtext) == 0) |
| 11872 | goto do_func; |
| 11873 | /* fall through */ |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 11874 | # endif |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11875 | default: |
| 11876 | raise_error_unexpected_syntax(-1); |
| 11877 | } |
| 11878 | } |
| 11879 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11880 | break; |
| 11881 | case TREDIR: |
| 11882 | *rpp = n = redirnode; |
| 11883 | rpp = &n->nfile.next; |
| 11884 | parsefname(); /* read name of redirection file */ |
| 11885 | break; |
| 11886 | case TLP: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11887 | IF_BASH_FUNCTION(do_func:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11888 | if (args && app == &args->narg.next |
| 11889 | && !vars && !redir |
| 11890 | ) { |
| 11891 | struct builtincmd *bcmd; |
| 11892 | const char *name; |
| 11893 | |
| 11894 | /* We have a function */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11895 | if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11896 | raise_error_unexpected_syntax(TRP); |
| 11897 | name = n->narg.text; |
| 11898 | if (!goodname(name) |
| 11899 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 11900 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11901 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11902 | } |
| 11903 | n->type = NDEFUN; |
| 11904 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11905 | n->ndefun.text = n->narg.text; |
| 11906 | n->ndefun.linno = g_parsefile->linno; |
| 11907 | n->ndefun.body = parse_command(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11908 | return n; |
| 11909 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 11910 | IF_BASH_FUNCTION(function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11911 | /* fall through */ |
| 11912 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11913 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11914 | goto out; |
| 11915 | } |
| 11916 | } |
| 11917 | out: |
| 11918 | *app = NULL; |
| 11919 | *vpp = NULL; |
| 11920 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11921 | n = stzalloc(sizeof(struct ncmd)); |
Denys Vlasenko | 57b7efb | 2018-04-10 01:20:26 +0200 | [diff] [blame] | 11922 | if (NCMD != 0) |
| 11923 | n->type = NCMD; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11924 | n->ncmd.linno = savelinno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11925 | n->ncmd.args = args; |
| 11926 | n->ncmd.assign = vars; |
| 11927 | n->ncmd.redirect = redir; |
| 11928 | return n; |
| 11929 | } |
| 11930 | |
| 11931 | static union node * |
| 11932 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11933 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11934 | union node *n1, *n2; |
| 11935 | union node *ap, **app; |
| 11936 | union node *cp, **cpp; |
| 11937 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11938 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11939 | int t; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11940 | int savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11941 | |
| 11942 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11943 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 11944 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11945 | savelinno = g_parsefile->linno; |
| 11946 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11947 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11948 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11949 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11950 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11951 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11952 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11953 | n1->type = NIF; |
| 11954 | n1->nif.test = list(0); |
| 11955 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11956 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11957 | n1->nif.ifpart = list(0); |
| 11958 | n2 = n1; |
| 11959 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11960 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11961 | n2 = n2->nif.elsepart; |
| 11962 | n2->type = NIF; |
| 11963 | n2->nif.test = list(0); |
| 11964 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11965 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11966 | n2->nif.ifpart = list(0); |
| 11967 | } |
| 11968 | if (lasttoken == TELSE) |
| 11969 | n2->nif.elsepart = list(0); |
| 11970 | else { |
| 11971 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11972 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11973 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11974 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11975 | break; |
| 11976 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11977 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11978 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11979 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11980 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11981 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11982 | got = readtoken(); |
| 11983 | if (got != TDO) { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11984 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got], |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11985 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11986 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11987 | } |
| 11988 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11989 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11990 | break; |
| 11991 | } |
| 11992 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11993 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11994 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11995 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11996 | n1->type = NFOR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 11997 | n1->nfor.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11998 | n1->nfor.var = wordtext; |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 11999 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12000 | if (readtoken() == TIN) { |
| 12001 | app = ≈ |
| 12002 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12003 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12004 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12005 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12006 | n2->narg.text = wordtext; |
| 12007 | n2->narg.backquote = backquotelist; |
| 12008 | *app = n2; |
| 12009 | app = &n2->narg.next; |
| 12010 | } |
| 12011 | *app = NULL; |
| 12012 | n1->nfor.args = ap; |
| 12013 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12014 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12015 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12016 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12017 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12018 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12019 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12020 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12021 | n1->nfor.args = n2; |
| 12022 | /* |
| 12023 | * Newline or semicolon here is optional (but note |
| 12024 | * that the original Bourne shell only allowed NL). |
| 12025 | */ |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 12026 | if (lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12027 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12028 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12029 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12030 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12031 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12032 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12033 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12034 | break; |
| 12035 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12036 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12037 | n1->type = NCASE; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12038 | n1->ncase.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12039 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12040 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12041 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12042 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12043 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12044 | n2->narg.text = wordtext; |
| 12045 | n2->narg.backquote = backquotelist; |
Ron Yorston | 383b885 | 2015-08-03 13:46:25 +0100 | [diff] [blame] | 12046 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 12047 | if (readtoken() != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12048 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12049 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12050 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12051 | checkkwd = CHKNL | CHKKWD; |
| 12052 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12053 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12054 | if (lasttoken == TLP) |
| 12055 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12056 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12057 | cp->type = NCLIST; |
| 12058 | app = &cp->nclist.pattern; |
| 12059 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12060 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12061 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12062 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12063 | ap->narg.text = wordtext; |
| 12064 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12065 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12066 | break; |
| 12067 | app = &ap->narg.next; |
| 12068 | readtoken(); |
| 12069 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12070 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12071 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12072 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12073 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12074 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12075 | cpp = &cp->nclist.next; |
| 12076 | |
| 12077 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12078 | t = readtoken(); |
| 12079 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12080 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12081 | raise_error_unexpected_syntax(TENDCASE); |
| 12082 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12083 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12084 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12085 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12086 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12087 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12088 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12089 | n1->type = NSUBSHELL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12090 | n1->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12091 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12092 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12093 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12094 | break; |
| 12095 | case TBEGIN: |
| 12096 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12097 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12098 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12099 | IF_BASH_FUNCTION(case TFUNCTION:) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12100 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12101 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12102 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12103 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12104 | } |
| 12105 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12106 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12107 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12108 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12109 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12110 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12111 | checkkwd = CHKKWD | CHKALIAS; |
| 12112 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12113 | while (readtoken() == TREDIR) { |
| 12114 | *rpp = n2 = redirnode; |
| 12115 | rpp = &n2->nfile.next; |
| 12116 | parsefname(); |
| 12117 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12118 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12119 | *rpp = NULL; |
| 12120 | if (redir) { |
| 12121 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12122 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12123 | n2->type = NREDIR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12124 | n2->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12125 | n2->nredir.n = n1; |
| 12126 | n1 = n2; |
| 12127 | } |
| 12128 | n1->nredir.redirect = redir; |
| 12129 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12130 | return n1; |
| 12131 | } |
| 12132 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12133 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 12134 | static int |
| 12135 | decode_dollar_squote(void) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12136 | { |
| 12137 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 12138 | int c, cnt; |
| 12139 | char *p; |
| 12140 | char buf[4]; |
| 12141 | |
| 12142 | c = pgetc(); |
| 12143 | p = strchr(C_escapes, c); |
| 12144 | if (p) { |
| 12145 | buf[0] = c; |
| 12146 | p = buf; |
| 12147 | cnt = 3; |
| 12148 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 12149 | do { |
| 12150 | c = pgetc(); |
| 12151 | *++p = c; |
| 12152 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 12153 | pungetc(); |
| 12154 | } else if (c == 'x') { /* \xHH */ |
| 12155 | do { |
| 12156 | c = pgetc(); |
| 12157 | *++p = c; |
| 12158 | } while (isxdigit(c) && --cnt); |
| 12159 | pungetc(); |
| 12160 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 12161 | c = 'x'; |
| 12162 | goto unrecognized; |
| 12163 | } |
| 12164 | } else { /* simple seq like \\ or \t */ |
| 12165 | p++; |
| 12166 | } |
| 12167 | *p = '\0'; |
| 12168 | p = buf; |
| 12169 | c = bb_process_escape_sequence((void*)&p); |
| 12170 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 12171 | if (c != '\'' && c != '"') { |
| 12172 | unrecognized: |
| 12173 | c |= 0x100; /* "please encode \, then me" */ |
| 12174 | } |
| 12175 | } |
| 12176 | return c; |
| 12177 | } |
| 12178 | #endif |
| 12179 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12180 | /* Used by expandstr to get here-doc like behaviour. */ |
| 12181 | #define FAKEEOFMARK ((char*)(uintptr_t)1) |
| 12182 | |
| 12183 | static ALWAYS_INLINE int |
| 12184 | realeofmark(const char *eofmark) |
| 12185 | { |
| 12186 | return eofmark && eofmark != FAKEEOFMARK; |
| 12187 | } |
| 12188 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12189 | /* |
| 12190 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 12191 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 12192 | * 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] | 12193 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12194 | * is the first character of the input token or document. |
| 12195 | * |
| 12196 | * Because C does not have internal subroutines, I have simulated them |
| 12197 | * using goto's to implement the subroutine linkage. The following macros |
| 12198 | * will run code that appears at the end of readtoken1. |
| 12199 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12200 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 12201 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 12202 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 12203 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 12204 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 12205 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12206 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12207 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 12208 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12209 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12210 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12211 | char *out; |
Denys Vlasenko | 50e6d42 | 2016-09-30 11:35:54 +0200 | [diff] [blame] | 12212 | size_t len; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12213 | struct nodelist *bqlist; |
| 12214 | smallint quotef; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12215 | smallint oldstyle; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12216 | smallint pssyntax; /* we are expanding a prompt string */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12217 | IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12218 | /* syntax stack */ |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12219 | struct synstack synbase = { }; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12220 | struct synstack *synstack = &synbase; |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12221 | |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12222 | #if ENABLE_ASH_EXPAND_PRMT |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12223 | pssyntax = (syntax == PSSYNTAX); |
| 12224 | if (pssyntax) |
| 12225 | syntax = DQSYNTAX; |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12226 | #else |
| 12227 | pssyntax = 0; /* constant */ |
| 12228 | #endif |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12229 | synstack->syntax = syntax; |
| 12230 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12231 | if (syntax == DQSYNTAX) |
| 12232 | synstack->dblquote = 1; |
| 12233 | quotef = 0; |
| 12234 | bqlist = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12235 | |
| 12236 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12237 | loop: |
| 12238 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12239 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 12240 | for (;;) { /* until end of line or end of word */ |
| 12241 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12242 | switch (SIT(c, synstack->syntax)) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12243 | case CNL: /* '\n' */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12244 | if (synstack->syntax == BASESYNTAX |
| 12245 | && !synstack->varnest |
| 12246 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12247 | goto endword; /* exit outer loop */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12248 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12249 | USTPUTC(c, out); |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12250 | nlprompt(); |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12251 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12252 | goto loop; /* continue outer loop */ |
| 12253 | case CWORD: |
| 12254 | USTPUTC(c, out); |
| 12255 | break; |
| 12256 | case CCTL: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12257 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12258 | if (c == '\\' && bash_dollar_squote) { |
| 12259 | c = decode_dollar_squote(); |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12260 | if (c == '\0') { |
| 12261 | /* skip $'\000', $'\x00' (like bash) */ |
| 12262 | break; |
| 12263 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12264 | if (c & 0x100) { |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12265 | /* Unknown escape. Encode as '\z' */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12266 | c = (unsigned char)c; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12267 | if (eofmark == NULL || synstack->dblquote) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12268 | USTPUTC(CTLESC, out); |
| 12269 | USTPUTC('\\', out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12270 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12271 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12272 | #endif |
Denys Vlasenko | c4c2012 | 2018-04-02 13:29:20 +0200 | [diff] [blame] | 12273 | if (!eofmark || synstack->dblquote || synstack->varnest) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12274 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12275 | USTPUTC(c, out); |
| 12276 | break; |
| 12277 | case CBACK: /* backslash */ |
| 12278 | c = pgetc_without_PEOA(); |
| 12279 | if (c == PEOF) { |
| 12280 | USTPUTC(CTLESC, out); |
| 12281 | USTPUTC('\\', out); |
| 12282 | pungetc(); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12283 | } else { |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12284 | if (pssyntax && c == '$') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12285 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12286 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12287 | } |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12288 | /* Backslash is retained if we are in "str" |
| 12289 | * and next char isn't dquote-special. |
| 12290 | */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12291 | if (synstack->dblquote |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12292 | && c != '\\' |
| 12293 | && c != '`' |
| 12294 | && c != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12295 | && (c != '"' || (eofmark != NULL && !synstack->varnest)) |
| 12296 | && (c != '}' || !synstack->varnest) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12297 | ) { |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12298 | USTPUTC(CTLESC, out); /* protect '\' from glob */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12299 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12300 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12301 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 12302 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12303 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12304 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12305 | break; |
| 12306 | case CSQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12307 | synstack->syntax = SQSYNTAX; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12308 | quotemark: |
| 12309 | if (eofmark == NULL) { |
| 12310 | USTPUTC(CTLQUOTEMARK, out); |
| 12311 | } |
| 12312 | break; |
| 12313 | case CDQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12314 | synstack->syntax = DQSYNTAX; |
| 12315 | synstack->dblquote = 1; |
| 12316 | toggledq: |
| 12317 | if (synstack->varnest) |
| 12318 | synstack->innerdq ^= 1; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12319 | goto quotemark; |
| 12320 | case CENDQUOTE: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12321 | IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12322 | if (eofmark != NULL && synstack->varnest == 0) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12323 | USTPUTC(c, out); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12324 | break; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12325 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12326 | |
| 12327 | if (synstack->dqvarnest == 0) { |
| 12328 | synstack->syntax = BASESYNTAX; |
| 12329 | synstack->dblquote = 0; |
| 12330 | } |
| 12331 | |
| 12332 | quotef = 1; |
| 12333 | |
| 12334 | if (c == '"') |
| 12335 | goto toggledq; |
| 12336 | |
| 12337 | goto quotemark; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12338 | case CVAR: /* '$' */ |
| 12339 | PARSESUB(); /* parse substitution */ |
| 12340 | break; |
| 12341 | case CENDVAR: /* '}' */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12342 | if (!synstack->innerdq && synstack->varnest > 0) { |
| 12343 | if (!--synstack->varnest && synstack->varpushed) |
| 12344 | synstack_pop(&synstack); |
| 12345 | else if (synstack->dqvarnest > 0) |
| 12346 | synstack->dqvarnest--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12347 | c = CTLENDVAR; |
| 12348 | } |
| 12349 | USTPUTC(c, out); |
| 12350 | break; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12351 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12352 | case CLP: /* '(' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12353 | synstack->parenlevel++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12354 | USTPUTC(c, out); |
| 12355 | break; |
| 12356 | case CRP: /* ')' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12357 | if (synstack->parenlevel > 0) { |
| 12358 | synstack->parenlevel--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12359 | } else { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12360 | if (pgetc_eatbnl() == ')') { |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 12361 | c = CTLENDARI; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12362 | synstack_pop(&synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12363 | } else { |
| 12364 | /* |
| 12365 | * unbalanced parens |
| 12366 | * (don't 2nd guess - no error) |
| 12367 | */ |
| 12368 | pungetc(); |
| 12369 | } |
| 12370 | } |
| 12371 | USTPUTC(c, out); |
| 12372 | break; |
| 12373 | #endif |
| 12374 | case CBQUOTE: /* '`' */ |
Denys Vlasenko | 41fddb4 | 2018-04-01 16:38:32 +0200 | [diff] [blame] | 12375 | if (checkkwd & CHKEOFMARK) { |
| 12376 | quotef = 1; |
| 12377 | USTPUTC('`', out); |
| 12378 | break; |
| 12379 | } |
| 12380 | |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12381 | PARSEBACKQOLD(); |
| 12382 | break; |
| 12383 | case CENDFILE: |
| 12384 | goto endword; /* exit outer loop */ |
| 12385 | case CIGN: |
| 12386 | break; |
| 12387 | default: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12388 | if (synstack->varnest == 0) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12389 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12390 | if (c == '&') { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12391 | //Can't call pgetc_eatbnl() here, this requires three-deep pungetc() |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12392 | if (pgetc() == '>') |
| 12393 | c = 0x100 + '>'; /* flag &> */ |
| 12394 | pungetc(); |
| 12395 | } |
| 12396 | #endif |
| 12397 | goto endword; /* exit outer loop */ |
| 12398 | } |
| 12399 | IF_ASH_ALIAS(if (c != PEOA)) |
| 12400 | USTPUTC(c, out); |
| 12401 | } |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12402 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12403 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12404 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12405 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12406 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12407 | if (synstack->syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12408 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12409 | #endif |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12410 | if (synstack->syntax != BASESYNTAX && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12411 | raise_error_syntax("unterminated quoted string"); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12412 | if (synstack->varnest != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12413 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12414 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12415 | } |
| 12416 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12417 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12418 | out = stackblock(); |
| 12419 | if (eofmark == NULL) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12420 | if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12421 | && quotef == 0 |
| 12422 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12423 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12424 | PARSEREDIR(); /* passed as params: out, c */ |
| 12425 | lasttoken = TREDIR; |
| 12426 | return lasttoken; |
| 12427 | } |
| 12428 | /* else: non-number X seen, interpret it |
| 12429 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12430 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12431 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12432 | } |
| 12433 | quoteflag = quotef; |
| 12434 | backquotelist = bqlist; |
| 12435 | grabstackblock(len); |
| 12436 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12437 | lasttoken = TWORD; |
| 12438 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12439 | /* end of readtoken routine */ |
| 12440 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12441 | /* |
| 12442 | * Check to see whether we are at the end of the here document. When this |
| 12443 | * is called, c is set to the first character of the next input line. If |
| 12444 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 12445 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12446 | checkend: { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12447 | if (realeofmark(eofmark)) { |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12448 | int markloc; |
| 12449 | char *p; |
| 12450 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12451 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12452 | if (c == PEOA) |
| 12453 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12454 | #endif |
| 12455 | if (striptabs) { |
| 12456 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12457 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12458 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12459 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12460 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12461 | markloc = out - (char *)stackblock(); |
| 12462 | for (p = eofmark; STPUTC(c, out), *p; p++) { |
| 12463 | if (c != *p) |
| 12464 | goto more_heredoc; |
Denys Vlasenko | 35e349d | 2019-09-05 14:31:49 +0200 | [diff] [blame] | 12465 | /* FIXME: fails for backslash-newlined terminator: |
| 12466 | * cat <<EOF |
| 12467 | * ... |
| 12468 | * EO\ |
| 12469 | * F |
| 12470 | * (see heredoc_bkslash_newline2.tests) |
| 12471 | */ |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12472 | c = pgetc_without_PEOA(); |
| 12473 | } |
| 12474 | |
| 12475 | if (c == '\n' || c == PEOF) { |
| 12476 | c = PEOF; |
| 12477 | g_parsefile->linno++; |
| 12478 | needprompt = doprompt; |
| 12479 | } else { |
| 12480 | int len_here; |
| 12481 | |
| 12482 | more_heredoc: |
| 12483 | p = (char *)stackblock() + markloc + 1; |
| 12484 | len_here = out - p; |
| 12485 | |
| 12486 | if (len_here) { |
| 12487 | len_here -= (c >= PEOF); |
| 12488 | c = p[-1]; |
| 12489 | |
| 12490 | if (len_here) { |
| 12491 | char *str; |
| 12492 | |
| 12493 | str = alloca(len_here + 1); |
| 12494 | *(char *)mempcpy(str, p, len_here) = '\0'; |
| 12495 | |
| 12496 | pushstring(str, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12497 | } |
| 12498 | } |
| 12499 | } |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12500 | |
| 12501 | STADJUST((char *)stackblock() + markloc - out, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12502 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12503 | goto checkend_return; |
| 12504 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12505 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12506 | /* |
| 12507 | * Parse a redirection operator. The variable "out" points to a string |
| 12508 | * specifying the fd to be redirected. The variable "c" contains the |
| 12509 | * first character of the redirection operator. |
| 12510 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12511 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12512 | /* out is already checked to be a valid number or "" */ |
| 12513 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12514 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12515 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12516 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12517 | if (c == '>') { |
| 12518 | np->nfile.fd = 1; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12519 | c = pgetc_eatbnl(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12520 | if (c == '>') |
| 12521 | np->type = NAPPEND; |
| 12522 | else if (c == '|') |
| 12523 | np->type = NCLOBBER; |
| 12524 | else if (c == '&') |
| 12525 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12526 | /* 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] | 12527 | else { |
| 12528 | np->type = NTO; |
| 12529 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12530 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12531 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12532 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12533 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 12534 | np->nfile.fd = 1; |
| 12535 | pgetc(); /* this is '>', no need to check */ |
| 12536 | np->type = NTO2; |
| 12537 | } |
| 12538 | #endif |
| 12539 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12540 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12541 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12542 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12543 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12544 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12545 | np = stzalloc(sizeof(struct nhere)); |
| 12546 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12547 | } |
| 12548 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12549 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12550 | heredoc->here = np; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12551 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12552 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12553 | heredoc->striptabs = 1; |
| 12554 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12555 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12556 | pungetc(); |
| 12557 | } |
| 12558 | break; |
| 12559 | |
| 12560 | case '&': |
| 12561 | np->type = NFROMFD; |
| 12562 | break; |
| 12563 | |
| 12564 | case '>': |
| 12565 | np->type = NFROMTO; |
| 12566 | break; |
| 12567 | |
| 12568 | default: |
| 12569 | np->type = NFROM; |
| 12570 | pungetc(); |
| 12571 | break; |
| 12572 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12573 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12574 | if (fd >= 0) |
| 12575 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12576 | redirnode = np; |
| 12577 | goto parseredir_return; |
| 12578 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12579 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12580 | /* |
| 12581 | * Parse a substitution. At this point, we have read the dollar sign |
| 12582 | * and nothing else. |
| 12583 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12584 | |
| 12585 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 12586 | * (assuming ascii char codes, as the original implementation did) */ |
| 12587 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12588 | (((unsigned)(c) - 33 < 32) \ |
| 12589 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12590 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12591 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12592 | int typeloc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12593 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12594 | c = pgetc_eatbnl(); |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12595 | if ((checkkwd & CHKEOFMARK) |
| 12596 | || c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12597 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12598 | ) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12599 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12600 | if (synstack->syntax != DQSYNTAX && c == '\'') |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12601 | bash_dollar_squote = 1; |
| 12602 | else |
| 12603 | #endif |
| 12604 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12605 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12606 | } else if (c == '(') { |
| 12607 | /* $(command) or $((arith)) */ |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12608 | if (pgetc_eatbnl() == '(') { |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12609 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12610 | PARSEARITH(); |
| 12611 | #else |
Denys Vlasenko | 4f8079d | 2017-07-17 17:11:48 +0200 | [diff] [blame] | 12612 | raise_error_syntax("support for $((arith)) is disabled"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12613 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12614 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12615 | pungetc(); |
| 12616 | PARSEBACKQNEW(); |
| 12617 | } |
| 12618 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12619 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12620 | smalluint newsyn = synstack->syntax; |
| 12621 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12622 | USTPUTC(CTLVAR, out); |
| 12623 | typeloc = out - (char *)stackblock(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12624 | STADJUST(1, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12625 | subtype = VSNORMAL; |
| 12626 | if (c == '{') { |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12627 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12628 | subtype = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12629 | } |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12630 | varname: |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12631 | if (is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12632 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12633 | do { |
| 12634 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12635 | c = pgetc_eatbnl(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12636 | } while (is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12637 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12638 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12639 | do { |
| 12640 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12641 | c = pgetc_eatbnl(); |
Denys Vlasenko | c2ce888 | 2020-02-17 10:15:35 +0100 | [diff] [blame] | 12642 | } while (!subtype && isdigit(c)); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12643 | } else if (c != '}') { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12644 | /* $[{[#]]<specialchar>[}] */ |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12645 | int cc = c; |
| 12646 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12647 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12648 | if (!subtype && cc == '#') { |
| 12649 | subtype = VSLENGTH; |
| 12650 | if (c == '_' || isalnum(c)) |
| 12651 | goto varname; |
| 12652 | cc = c; |
| 12653 | c = pgetc_eatbnl(); |
| 12654 | if (cc == '}' || c != '}') { |
| 12655 | pungetc(); |
| 12656 | subtype = 0; |
| 12657 | c = cc; |
| 12658 | cc = '#'; |
| 12659 | } |
| 12660 | } |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12661 | |
| 12662 | if (!is_special(cc)) { |
| 12663 | if (subtype == VSLENGTH) |
| 12664 | subtype = 0; |
| 12665 | goto badsub; |
| 12666 | } |
| 12667 | |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12668 | USTPUTC(cc, out); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12669 | } else |
| 12670 | goto badsub; |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12671 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12672 | if (c != '}' && subtype == VSLENGTH) { |
| 12673 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 12674 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12675 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12676 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12677 | if (subtype == 0) { |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12678 | static const char types[] ALIGN1 = "}-+?="; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12679 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 12680 | /* c == first char after VAR */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12681 | int cc = c; |
| 12682 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12683 | switch (c) { |
| 12684 | case ':': |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12685 | c = pgetc_eatbnl(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12686 | #if BASH_SUBSTR |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12687 | /* This check is only needed to not misinterpret |
| 12688 | * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD} |
| 12689 | * constructs. |
| 12690 | */ |
| 12691 | if (!strchr(types, c)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12692 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12693 | pungetc(); |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12694 | break; /* "goto badsub" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12695 | } |
| 12696 | #endif |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12697 | subtype = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12698 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12699 | default: { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12700 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12701 | if (p == NULL) |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12702 | break; |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12703 | subtype |= p - types + VSNORMAL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12704 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12705 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12706 | case '%': |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12707 | case '#': |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12708 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12709 | c = pgetc_eatbnl(); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12710 | if (c == cc) |
| 12711 | subtype++; |
| 12712 | else |
| 12713 | pungetc(); |
| 12714 | |
| 12715 | newsyn = BASESYNTAX; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12716 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12717 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12718 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 12719 | /* ${v/[/]pattern/repl} */ |
| 12720 | //TODO: encode pattern and repl separately. |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12721 | // Currently cases like: v=1;echo ${v/$((1/1))/ONE} |
| 12722 | // are broken (should print "ONE") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12723 | subtype = VSREPLACE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12724 | newsyn = BASESYNTAX; |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12725 | c = pgetc_eatbnl(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12726 | if (c != '/') |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12727 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12728 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12729 | break; |
| 12730 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12731 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12732 | } else { |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12733 | badsub: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12734 | pungetc(); |
| 12735 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12736 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12737 | if (newsyn == ARISYNTAX) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12738 | newsyn = DQSYNTAX; |
| 12739 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12740 | if ((newsyn != synstack->syntax || synstack->innerdq) |
| 12741 | && subtype != VSNORMAL |
| 12742 | ) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12743 | synstack_push(&synstack, |
| 12744 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 12745 | newsyn); |
| 12746 | |
| 12747 | synstack->varpushed = 1; |
| 12748 | synstack->dblquote = newsyn != BASESYNTAX; |
| 12749 | } |
| 12750 | |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12751 | ((unsigned char *)stackblock())[typeloc] = subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12752 | if (subtype != VSNORMAL) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12753 | synstack->varnest++; |
| 12754 | if (synstack->dblquote) |
| 12755 | synstack->dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12756 | } |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12757 | STPUTC('=', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12758 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12759 | goto parsesub_return; |
| 12760 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12761 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12762 | /* |
| 12763 | * Called to parse command substitutions. Newstyle is set if the command |
| 12764 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 12765 | * list of commands (passed by reference), and savelen is the number of |
| 12766 | * characters on the top of the stack which must be preserved. |
| 12767 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12768 | parsebackq: { |
| 12769 | struct nodelist **nlpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12770 | union node *n; |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 12771 | char *str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12772 | size_t savelen; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12773 | struct heredoc *saveheredoclist; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12774 | smallint saveprompt = 0; |
| 12775 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12776 | str = NULL; |
| 12777 | savelen = out - (char *)stackblock(); |
| 12778 | if (savelen > 0) { |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12779 | /* |
| 12780 | * FIXME: this can allocate very large block on stack and SEGV. |
| 12781 | * Example: |
| 12782 | * echo "..<100kbytes>..`true` $(true) `true` ..." |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 12783 | * allocates 100kb for every command subst. With about |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12784 | * a hundred command substitutions stack overflows. |
| 12785 | * With larger prepended string, SEGV happens sooner. |
| 12786 | */ |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 12787 | str = alloca(savelen); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12788 | memcpy(str, stackblock(), savelen); |
| 12789 | } |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 12790 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12791 | if (oldstyle) { |
| 12792 | /* We must read until the closing backquote, giving special |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 12793 | * treatment to some slashes, and then push the string and |
| 12794 | * reread it as input, interpreting it normally. |
| 12795 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12796 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12797 | size_t psavelen; |
| 12798 | char *pstr; |
| 12799 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12800 | STARTSTACKSTR(pout); |
| 12801 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12802 | int pc; |
| 12803 | |
| 12804 | setprompt_if(needprompt, 2); |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12805 | pc = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12806 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12807 | case '`': |
| 12808 | goto done; |
| 12809 | |
| 12810 | case '\\': |
Denys Vlasenko | 777a635 | 2020-09-29 16:25:32 +0200 | [diff] [blame] | 12811 | pc = pgetc(); /* not pgetc_eatbnl! */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12812 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12813 | && (!synstack->dblquote || pc != '"') |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 12814 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12815 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 12816 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12817 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12818 | break; |
| 12819 | } |
| 12820 | /* fall through */ |
| 12821 | |
| 12822 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12823 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12824 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12825 | |
| 12826 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12827 | nlnoprompt(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12828 | break; |
| 12829 | |
| 12830 | default: |
| 12831 | break; |
| 12832 | } |
| 12833 | STPUTC(pc, pout); |
| 12834 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12835 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12836 | STPUTC('\0', pout); |
| 12837 | psavelen = pout - (char *)stackblock(); |
| 12838 | if (psavelen > 0) { |
| 12839 | pstr = grabstackstr(pout); |
| 12840 | setinputstring(pstr); |
| 12841 | } |
| 12842 | } |
| 12843 | nlpp = &bqlist; |
| 12844 | while (*nlpp) |
| 12845 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12846 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 12847 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12848 | |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12849 | saveheredoclist = heredoclist; |
| 12850 | heredoclist = NULL; |
| 12851 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12852 | if (oldstyle) { |
| 12853 | saveprompt = doprompt; |
| 12854 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12857 | n = list(2); |
| 12858 | |
| 12859 | if (oldstyle) |
| 12860 | doprompt = saveprompt; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12861 | else { |
| 12862 | if (readtoken() != TRP) |
| 12863 | raise_error_unexpected_syntax(TRP); |
| 12864 | setinputstring(nullstr); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12865 | } |
| 12866 | |
Denys Vlasenko | 9a1a659 | 2020-02-22 16:39:27 +0100 | [diff] [blame] | 12867 | parseheredoc(); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12868 | heredoclist = saveheredoclist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12869 | |
| 12870 | (*nlpp)->n = n; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 12871 | /* Start reading from old file again. */ |
| 12872 | popfile(); |
| 12873 | /* Ignore any pushed back tokens left from the backquote parsing. */ |
| 12874 | if (oldstyle) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12875 | tokpushback = 0; |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 12876 | out = growstackto(savelen + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12877 | if (str) { |
| 12878 | memcpy(out, str, savelen); |
| 12879 | STADJUST(savelen, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12880 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12881 | USTPUTC(CTLBACKQ, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12882 | if (oldstyle) |
| 12883 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12884 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12885 | } |
| 12886 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12887 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12888 | /* |
| 12889 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 12890 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12891 | parsearith: { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12892 | |
| 12893 | synstack_push(&synstack, |
| 12894 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 12895 | ARISYNTAX); |
| 12896 | synstack->dblquote = 1; |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 12897 | USTPUTC(CTLARI, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12898 | goto parsearith_return; |
| 12899 | } |
| 12900 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12901 | } /* end of readtoken */ |
| 12902 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12903 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12904 | * Read the next input token. |
| 12905 | * If the token is a word, we set backquotelist to the list of cmds in |
| 12906 | * backquotes. We set quoteflag to true if any part of the word was |
| 12907 | * quoted. |
| 12908 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 12909 | * the redirection. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12910 | * |
| 12911 | * [Change comment: here documents and internal procedures] |
| 12912 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 12913 | * word parsing code into a separate routine. In this case, readtoken |
| 12914 | * doesn't need to have any internal procedures, but parseword does. |
| 12915 | * We could also make parseoperator in essence the main routine, and |
| 12916 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12917 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12918 | #define NEW_xxreadtoken |
| 12919 | #ifdef NEW_xxreadtoken |
| 12920 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12921 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12922 | '\n', '(', ')', /* singles */ |
| 12923 | '&', '|', ';', /* doubles */ |
| 12924 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12925 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12926 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12927 | #define xxreadtoken_singles 3 |
| 12928 | #define xxreadtoken_doubles 3 |
| 12929 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12930 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12931 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 12932 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 12933 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12934 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12935 | }; |
| 12936 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12937 | static int |
| 12938 | xxreadtoken(void) |
| 12939 | { |
| 12940 | int c; |
| 12941 | |
| 12942 | if (tokpushback) { |
| 12943 | tokpushback = 0; |
| 12944 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12945 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12946 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12947 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12948 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 12949 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12950 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12951 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12952 | if (c == '#') { |
| 12953 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 12954 | continue; |
| 12955 | pungetc(); |
| 12956 | } else if (c == '\\') { |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12957 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12958 | } else { |
| 12959 | const char *p; |
| 12960 | |
| 12961 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 12962 | if (c != PEOF) { |
| 12963 | if (c == '\n') { |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12964 | nlnoprompt(); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12965 | } |
| 12966 | |
| 12967 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12968 | if (p == NULL) |
| 12969 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12970 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12971 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 12972 | int cc = pgetc_eatbnl(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12973 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12974 | p += xxreadtoken_doubles + 1; |
| 12975 | } else { |
| 12976 | pungetc(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12977 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12978 | if (c == '&' && cc == '>') /* &> */ |
| 12979 | break; /* return readtoken1(...) */ |
| 12980 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12981 | } |
| 12982 | } |
| 12983 | } |
| 12984 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 12985 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12986 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12987 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12988 | |
| 12989 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12990 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12991 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12992 | #define RETURN(token) return lasttoken = token |
| 12993 | static int |
| 12994 | xxreadtoken(void) |
| 12995 | { |
| 12996 | int c; |
| 12997 | |
| 12998 | if (tokpushback) { |
| 12999 | tokpushback = 0; |
| 13000 | return lasttoken; |
| 13001 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13002 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13003 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13004 | c = pgetc_eatbnl(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13005 | switch (c) { |
| 13006 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 13007 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13008 | continue; |
| 13009 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 13010 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 13011 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13012 | pungetc(); |
| 13013 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13014 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 13015 | nlnoprompt(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13016 | RETURN(TNL); |
| 13017 | case PEOF: |
| 13018 | RETURN(TEOF); |
| 13019 | case '&': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13020 | if (pgetc_eatbnl() == '&') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13021 | RETURN(TAND); |
| 13022 | pungetc(); |
| 13023 | RETURN(TBACKGND); |
| 13024 | case '|': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13025 | if (pgetc_eatbnl() == '|') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13026 | RETURN(TOR); |
| 13027 | pungetc(); |
| 13028 | RETURN(TPIPE); |
| 13029 | case ';': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13030 | if (pgetc_eatbnl() == ';') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13031 | RETURN(TENDCASE); |
| 13032 | pungetc(); |
| 13033 | RETURN(TSEMI); |
| 13034 | case '(': |
| 13035 | RETURN(TLP); |
| 13036 | case ')': |
| 13037 | RETURN(TRP); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13038 | } |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13039 | break; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13040 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13041 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 13042 | #undef RETURN |
| 13043 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13044 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13045 | |
| 13046 | static int |
| 13047 | readtoken(void) |
| 13048 | { |
| 13049 | int t; |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13050 | int kwd = checkkwd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13051 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13052 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13053 | #endif |
| 13054 | |
| 13055 | #if ENABLE_ASH_ALIAS |
| 13056 | top: |
| 13057 | #endif |
| 13058 | |
| 13059 | t = xxreadtoken(); |
| 13060 | |
| 13061 | /* |
| 13062 | * eat newlines |
| 13063 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13064 | if (kwd & CHKNL) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13065 | while (t == TNL) { |
| 13066 | parseheredoc(); |
| 13067 | t = xxreadtoken(); |
| 13068 | } |
| 13069 | } |
| 13070 | |
| 13071 | if (t != TWORD || quoteflag) { |
| 13072 | goto out; |
| 13073 | } |
| 13074 | |
| 13075 | /* |
| 13076 | * check for keywords |
| 13077 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13078 | if (kwd & CHKKWD) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13079 | const char *const *pp; |
| 13080 | |
| 13081 | pp = findkwd(wordtext); |
| 13082 | if (pp) { |
| 13083 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13084 | TRACE(("keyword '%s' recognized\n", tokname_array[t])); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13085 | goto out; |
| 13086 | } |
| 13087 | } |
| 13088 | |
| 13089 | if (checkkwd & CHKALIAS) { |
| 13090 | #if ENABLE_ASH_ALIAS |
| 13091 | struct alias *ap; |
| 13092 | ap = lookupalias(wordtext, 1); |
| 13093 | if (ap != NULL) { |
| 13094 | if (*ap->val) { |
| 13095 | pushstring(ap->val, ap); |
| 13096 | } |
| 13097 | goto top; |
| 13098 | } |
| 13099 | #endif |
| 13100 | } |
| 13101 | out: |
| 13102 | checkkwd = 0; |
| 13103 | #if DEBUG |
| 13104 | if (!alreadyseen) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13105 | TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13106 | else |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13107 | TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13108 | #endif |
| 13109 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13110 | } |
| 13111 | |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13112 | static int |
Ron Yorston | 6bd2fab | 2015-10-29 11:30:22 +0000 | [diff] [blame] | 13113 | peektoken(void) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13114 | { |
| 13115 | int t; |
| 13116 | |
| 13117 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13118 | tokpushback = 1; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13119 | return t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13120 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13121 | |
| 13122 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13123 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 13124 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13125 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13126 | static union node * |
| 13127 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13128 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13129 | tokpushback = 0; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13130 | checkkwd = 0; |
| 13131 | heredoclist = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13132 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13133 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13134 | needprompt = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13135 | return list(1); |
| 13136 | } |
| 13137 | |
| 13138 | /* |
| 13139 | * Input any here documents. |
| 13140 | */ |
| 13141 | static void |
| 13142 | parseheredoc(void) |
| 13143 | { |
| 13144 | struct heredoc *here; |
| 13145 | union node *n; |
| 13146 | |
| 13147 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 13148 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13149 | |
| 13150 | while (here) { |
Christoph Schulz | 03ad7ae | 2018-11-20 17:45:52 +0100 | [diff] [blame] | 13151 | tokpushback = 0; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13152 | setprompt_if(needprompt, 2); |
Denys Vlasenko | acf79f9 | 2020-02-14 16:12:06 +0100 | [diff] [blame] | 13153 | if (here->here->type == NHERE) |
| 13154 | readtoken1(pgetc(), SQSYNTAX, here->eofmark, here->striptabs); |
| 13155 | else |
| 13156 | readtoken1(pgetc_eatbnl(), DQSYNTAX, here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13157 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13158 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13159 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13160 | n->narg.text = wordtext; |
| 13161 | n->narg.backquote = backquotelist; |
| 13162 | here->here->nhere.doc = n; |
| 13163 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13164 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13165 | } |
| 13166 | |
| 13167 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13168 | static const char * |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13169 | expandstr(const char *ps, int syntax_type) |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13170 | { |
| 13171 | union node n; |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13172 | int saveprompt; |
Ron Yorston | 48645b8 | 2019-04-18 09:48:13 +0100 | [diff] [blame] | 13173 | struct parsefile *file_stop = g_parsefile; |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13174 | volatile int saveint; |
| 13175 | struct jmploc *volatile savehandler = exception_handler; |
| 13176 | struct jmploc jmploc; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13177 | const char *volatile result; |
| 13178 | int err; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13179 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13180 | /* XXX Fix (char *) cast. */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13181 | setinputstring((char *)ps); |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13182 | |
| 13183 | saveprompt = doprompt; |
| 13184 | doprompt = 0; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13185 | result = ps; |
| 13186 | |
| 13187 | SAVE_INT(saveint); |
| 13188 | err = setjmp(jmploc.loc); |
| 13189 | if (err) |
| 13190 | goto out; |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13191 | |
| 13192 | /* readtoken1() might die horribly. |
Denys Vlasenko | 3c183a8 | 2017-10-12 19:35:42 +0200 | [diff] [blame] | 13193 | * Try a prompt with syntactically wrong command: |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13194 | * PS1='$(date "+%H:%M:%S) > ' |
| 13195 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13196 | exception_handler = &jmploc; |
| 13197 | readtoken1(pgetc(), syntax_type, FAKEEOFMARK, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13198 | |
| 13199 | n.narg.type = NARG; |
| 13200 | n.narg.next = NULL; |
| 13201 | n.narg.text = wordtext; |
| 13202 | n.narg.backquote = backquotelist; |
| 13203 | |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13204 | /* expandarg() might fail too: |
| 13205 | * PS1='$((123+))' |
| 13206 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13207 | expandarg(&n, NULL, EXP_QUOTED); |
| 13208 | result = stackblock(); |
| 13209 | |
| 13210 | out: |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13211 | exception_handler = savehandler; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13212 | if (err && exception_type != EXERROR) |
| 13213 | longjmp(exception_handler->loc, 1); |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13214 | RESTORE_INT(saveint); |
| 13215 | |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13216 | doprompt = saveprompt; |
| 13217 | /* Try: PS1='`xxx(`' */ |
| 13218 | unwindfiles(file_stop); |
| 13219 | |
| 13220 | return result; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13221 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13222 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13223 | static inline int |
| 13224 | parser_eof(void) |
| 13225 | { |
| 13226 | return tokpushback && lasttoken == TEOF; |
| 13227 | } |
| 13228 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13229 | /* |
| 13230 | * Execute a command or commands contained in a string. |
| 13231 | */ |
| 13232 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13233 | evalstring(char *s, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13234 | { |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13235 | struct jmploc *volatile savehandler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13236 | struct jmploc jmploc; |
| 13237 | int ex; |
| 13238 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13239 | union node *n; |
| 13240 | struct stackmark smark; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13241 | int status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13242 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13243 | s = sstrdup(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13244 | setinputstring(s); |
| 13245 | setstackmark(&smark); |
| 13246 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13247 | status = 0; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13248 | /* On exception inside execution loop, we must popfile(). |
| 13249 | * Try interactively: |
| 13250 | * readonly a=a |
| 13251 | * command eval "a=b" # throws "is read only" error |
| 13252 | * "command BLTIN" is not supposed to abort (even in non-interactive use). |
| 13253 | * But if we skip popfile(), we hit EOF in eval's string, and exit. |
| 13254 | */ |
| 13255 | savehandler = exception_handler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13256 | ex = setjmp(jmploc.loc); |
| 13257 | if (ex) |
| 13258 | goto out; |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13259 | exception_handler = &jmploc; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13260 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13261 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13262 | int i; |
| 13263 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13264 | i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT)); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13265 | if (n) |
| 13266 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13267 | popstackmark(&smark); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13268 | if (evalskip) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13269 | break; |
| 13270 | } |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13271 | out: |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13272 | popstackmark(&smark); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13273 | popfile(); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13274 | stunalloc(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13275 | |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13276 | exception_handler = savehandler; |
| 13277 | if (ex) |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 13278 | longjmp(exception_handler->loc, ex); |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13279 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13280 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13281 | } |
| 13282 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13283 | /* |
| 13284 | * The eval command. |
| 13285 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13286 | static int FAST_FUNC |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13287 | evalcmd(int argc UNUSED_PARAM, char **argv, int flags) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13288 | { |
| 13289 | char *p; |
| 13290 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13291 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13292 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13293 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13294 | argv += 2; |
| 13295 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13296 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13297 | for (;;) { |
| 13298 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13299 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13300 | if (p == NULL) |
| 13301 | break; |
| 13302 | STPUTC(' ', concat); |
| 13303 | } |
| 13304 | STPUTC('\0', concat); |
| 13305 | p = grabstackstr(concat); |
| 13306 | } |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13307 | return evalstring(p, flags & EV_TESTED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13308 | } |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13309 | return 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13310 | } |
| 13311 | |
| 13312 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 13313 | * Read and execute commands. |
| 13314 | * "Top" is nonzero for the top level command loop; |
| 13315 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13316 | */ |
| 13317 | static int |
| 13318 | cmdloop(int top) |
| 13319 | { |
| 13320 | union node *n; |
| 13321 | struct stackmark smark; |
| 13322 | int inter; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13323 | int status = 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13324 | int numeof = 0; |
| 13325 | |
| 13326 | TRACE(("cmdloop(%d) called\n", top)); |
| 13327 | for (;;) { |
| 13328 | int skip; |
| 13329 | |
| 13330 | setstackmark(&smark); |
| 13331 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 13332 | if (doing_jobctl) |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 13333 | showjobs(SHOW_CHANGED|SHOW_STDERR); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13334 | #endif |
| 13335 | inter = 0; |
| 13336 | if (iflag && top) { |
| 13337 | inter++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13338 | chkmail(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13339 | } |
| 13340 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 13341 | #if DEBUG |
| 13342 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 13343 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 13344 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13345 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13346 | if (!top || numeof >= 50) |
| 13347 | break; |
| 13348 | if (!stoppedjobs()) { |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13349 | if (!Iflag) { |
| 13350 | if (iflag) { |
| 13351 | newline_and_flush(stderr); |
| 13352 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13353 | break; |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13354 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13355 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 13356 | } |
| 13357 | numeof++; |
| 13358 | } else if (nflag == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13359 | int i; |
| 13360 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 13361 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 13362 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13363 | numeof = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13364 | i = evaltree(n, 0); |
| 13365 | if (n) |
| 13366 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13367 | } |
| 13368 | popstackmark(&smark); |
| 13369 | skip = evalskip; |
| 13370 | |
| 13371 | if (skip) { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 13372 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 13373 | break; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13374 | } |
| 13375 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13376 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13377 | } |
| 13378 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13379 | /* |
| 13380 | * Take commands from a file. To be compatible we should do a path |
| 13381 | * search for the file, which is necessary to find sub-commands. |
| 13382 | */ |
| 13383 | static char * |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13384 | find_dot_file(char *basename) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13385 | { |
| 13386 | char *fullname; |
| 13387 | const char *path = pathval(); |
| 13388 | struct stat statb; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13389 | int len; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13390 | |
| 13391 | /* don't try this for absolute or relative paths */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13392 | if (strchr(basename, '/')) |
| 13393 | return basename; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13394 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13395 | while ((len = padvance(&path, basename)) >= 0) { |
| 13396 | fullname = stackblock(); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13397 | if ((!pathopt || *pathopt == 'f') |
| 13398 | && !stat(fullname, &statb) && S_ISREG(statb.st_mode) |
| 13399 | ) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13400 | /* This will be freed by the caller. */ |
| 13401 | return stalloc(len); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13402 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13403 | } |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13404 | /* not found in PATH */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13405 | |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13406 | #if ENABLE_ASH_BASH_SOURCE_CURDIR |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13407 | return basename; |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13408 | #else |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13409 | ash_msg_and_raise_error("%s: not found", basename); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13410 | /* NOTREACHED */ |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13411 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13412 | } |
| 13413 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13414 | static int FAST_FUNC |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13415 | dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13416 | { |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13417 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 13418 | int status = 0; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13419 | char *fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13420 | char **argv; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13421 | char *args_need_save; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13422 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13423 | |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 13424 | //??? |
| 13425 | // struct strlist *sp; |
| 13426 | // for (sp = cmdenviron; sp; sp = sp->next) |
| 13427 | // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13428 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13429 | nextopt(nullstr); /* handle possible "--" */ |
| 13430 | argv = argptr; |
| 13431 | |
| 13432 | if (!argv[0]) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13433 | /* bash says: "bash: .: filename argument required" */ |
| 13434 | return 2; /* bash compat */ |
| 13435 | } |
| 13436 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13437 | /* This aborts if file isn't found, which is POSIXly correct. |
| 13438 | * bash returns exitcode 1 instead. |
| 13439 | */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13440 | fullname = find_dot_file(argv[0]); |
| 13441 | argv++; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13442 | args_need_save = argv[0]; |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 13443 | if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13444 | int argc; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13445 | saveparam = shellparam; |
| 13446 | shellparam.malloced = 0; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13447 | argc = 1; |
| 13448 | while (argv[argc]) |
| 13449 | argc++; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13450 | shellparam.nparam = argc; |
| 13451 | shellparam.p = argv; |
| 13452 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13453 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13454 | /* This aborts if file can't be opened, which is POSIXly correct. |
| 13455 | * bash returns exitcode 1 instead. |
| 13456 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13457 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 13458 | commandname = fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13459 | status = cmdloop(0); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13460 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13461 | |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13462 | if (args_need_save) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13463 | freeparam(&shellparam); |
| 13464 | shellparam = saveparam; |
| 13465 | }; |
| 13466 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13467 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13468 | } |
| 13469 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13470 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13471 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13472 | { |
| 13473 | if (stoppedjobs()) |
| 13474 | return 0; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13475 | |
Denys Vlasenko | 970470e | 2020-02-14 17:32:22 +0100 | [diff] [blame] | 13476 | if (argv[1]) |
| 13477 | savestatus = number(argv[1]); |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13478 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13479 | raise_exception(EXEXIT); |
| 13480 | /* NOTREACHED */ |
| 13481 | } |
| 13482 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13483 | /* |
| 13484 | * Read a file containing shell functions. |
| 13485 | */ |
| 13486 | static void |
| 13487 | readcmdfile(char *name) |
| 13488 | { |
| 13489 | setinputfile(name, INPUT_PUSH_FILE); |
| 13490 | cmdloop(0); |
| 13491 | popfile(); |
| 13492 | } |
| 13493 | |
| 13494 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13495 | /* ============ find_command inplementation */ |
| 13496 | |
| 13497 | /* |
| 13498 | * Resolve a command name. If you change this routine, you may have to |
| 13499 | * change the shellexec routine as well. |
| 13500 | */ |
| 13501 | static void |
| 13502 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 13503 | { |
| 13504 | struct tblentry *cmdp; |
| 13505 | int idx; |
| 13506 | int prev; |
| 13507 | char *fullname; |
| 13508 | struct stat statb; |
| 13509 | int e; |
| 13510 | int updatetbl; |
| 13511 | struct builtincmd *bcmd; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13512 | int len; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13513 | |
| 13514 | /* If name contains a slash, don't use PATH or hash table */ |
| 13515 | if (strchr(name, '/') != NULL) { |
| 13516 | entry->u.index = -1; |
| 13517 | if (act & DO_ABS) { |
| 13518 | while (stat(name, &statb) < 0) { |
| 13519 | #ifdef SYSV |
| 13520 | if (errno == EINTR) |
| 13521 | continue; |
| 13522 | #endif |
| 13523 | entry->cmdtype = CMDUNKNOWN; |
| 13524 | return; |
| 13525 | } |
| 13526 | } |
| 13527 | entry->cmdtype = CMDNORMAL; |
| 13528 | return; |
| 13529 | } |
| 13530 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13531 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13532 | |
| 13533 | updatetbl = (path == pathval()); |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13534 | if (!updatetbl) |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13535 | act |= DO_ALTPATH; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13536 | |
| 13537 | /* If name is in the table, check answer will be ok */ |
| 13538 | cmdp = cmdlookup(name, 0); |
| 13539 | if (cmdp != NULL) { |
| 13540 | int bit; |
| 13541 | |
| 13542 | switch (cmdp->cmdtype) { |
| 13543 | default: |
| 13544 | #if DEBUG |
| 13545 | abort(); |
| 13546 | #endif |
| 13547 | case CMDNORMAL: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13548 | bit = DO_ALTPATH | DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13549 | break; |
| 13550 | case CMDFUNCTION: |
| 13551 | bit = DO_NOFUNC; |
| 13552 | break; |
| 13553 | case CMDBUILTIN: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13554 | bit = IS_BUILTIN_REGULAR(cmdp->param.cmd) ? 0 : DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13555 | break; |
| 13556 | } |
| 13557 | if (act & bit) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13558 | if (act & bit & DO_REGBLTIN) |
| 13559 | goto fail; |
| 13560 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13561 | updatetbl = 0; |
| 13562 | cmdp = NULL; |
| 13563 | } else if (cmdp->rehash == 0) |
| 13564 | /* if not invalidated by cd, we're done */ |
| 13565 | goto success; |
| 13566 | } |
| 13567 | |
| 13568 | /* If %builtin not in path, check for builtin next */ |
| 13569 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13570 | if (bcmd) { |
| 13571 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 13572 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13573 | if (act & DO_ALTPATH) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13574 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13575 | if (builtinloc <= 0) |
| 13576 | goto builtin_success; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13577 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13578 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13579 | if (act & DO_REGBLTIN) |
| 13580 | goto fail; |
| 13581 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13582 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 13583 | { |
| 13584 | int applet_no = find_applet_by_name(name); |
| 13585 | if (applet_no >= 0) { |
| 13586 | entry->cmdtype = CMDNORMAL; |
| 13587 | entry->u.index = -2 - applet_no; |
| 13588 | return; |
| 13589 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13590 | } |
| 13591 | #endif |
| 13592 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13593 | /* We have to search path. */ |
| 13594 | prev = -1; /* where to start */ |
| 13595 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 13596 | if (cmdp->cmdtype == CMDBUILTIN) |
| 13597 | prev = builtinloc; |
| 13598 | else |
| 13599 | prev = cmdp->param.index; |
| 13600 | } |
| 13601 | |
| 13602 | e = ENOENT; |
| 13603 | idx = -1; |
| 13604 | loop: |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13605 | while ((len = padvance(&path, name)) >= 0) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13606 | const char *lpathopt = pathopt; |
| 13607 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13608 | fullname = stackblock(); |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13609 | idx++; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13610 | if (lpathopt) { |
| 13611 | if (*lpathopt == 'b') { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13612 | if (bcmd) |
| 13613 | goto builtin_success; |
| 13614 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13615 | } else if (!(act & DO_NOFUNC)) { |
| 13616 | /* handled below */ |
| 13617 | } else { |
| 13618 | /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13619 | continue; |
| 13620 | } |
| 13621 | } |
| 13622 | /* if rehash, don't redo absolute path names */ |
| 13623 | if (fullname[0] == '/' && idx <= prev) { |
| 13624 | if (idx < prev) |
| 13625 | continue; |
| 13626 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 13627 | goto success; |
| 13628 | } |
| 13629 | while (stat(fullname, &statb) < 0) { |
| 13630 | #ifdef SYSV |
| 13631 | if (errno == EINTR) |
| 13632 | continue; |
| 13633 | #endif |
| 13634 | if (errno != ENOENT && errno != ENOTDIR) |
| 13635 | e = errno; |
| 13636 | goto loop; |
| 13637 | } |
| 13638 | e = EACCES; /* if we fail, this will be the error */ |
| 13639 | if (!S_ISREG(statb.st_mode)) |
| 13640 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13641 | if (lpathopt) { /* this is a %func directory */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13642 | stalloc(len); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 13643 | /* NB: stalloc will return space pointed by fullname |
| 13644 | * (because we don't have any intervening allocations |
| 13645 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13646 | readcmdfile(fullname); |
| 13647 | cmdp = cmdlookup(name, 0); |
| 13648 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 13649 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 13650 | stunalloc(fullname); |
| 13651 | goto success; |
| 13652 | } |
| 13653 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 13654 | if (!updatetbl) { |
| 13655 | entry->cmdtype = CMDNORMAL; |
| 13656 | entry->u.index = idx; |
| 13657 | return; |
| 13658 | } |
| 13659 | INT_OFF; |
| 13660 | cmdp = cmdlookup(name, 1); |
| 13661 | cmdp->cmdtype = CMDNORMAL; |
| 13662 | cmdp->param.index = idx; |
| 13663 | INT_ON; |
| 13664 | goto success; |
| 13665 | } |
| 13666 | |
| 13667 | /* We failed. If there was an entry for this command, delete it */ |
| 13668 | if (cmdp && updatetbl) |
| 13669 | delete_cmd_entry(); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13670 | if (act & DO_ERR) { |
| 13671 | #if ENABLE_ASH_BASH_NOT_FOUND_HOOK |
| 13672 | struct tblentry *hookp = cmdlookup("command_not_found_handle", 0); |
| 13673 | if (hookp && hookp->cmdtype == CMDFUNCTION) { |
| 13674 | char *argv[3]; |
| 13675 | argv[0] = (char*) "command_not_found_handle"; |
| 13676 | argv[1] = name; |
| 13677 | argv[2] = NULL; |
| 13678 | evalfun(hookp->param.func, 2, argv, 0); |
| 13679 | entry->cmdtype = CMDUNKNOWN; |
| 13680 | return; |
| 13681 | } |
| 13682 | #endif |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13683 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13684 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13685 | fail: |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13686 | entry->cmdtype = CMDUNKNOWN; |
| 13687 | return; |
| 13688 | |
| 13689 | builtin_success: |
| 13690 | if (!updatetbl) { |
| 13691 | entry->cmdtype = CMDBUILTIN; |
| 13692 | entry->u.cmd = bcmd; |
| 13693 | return; |
| 13694 | } |
| 13695 | INT_OFF; |
| 13696 | cmdp = cmdlookup(name, 1); |
| 13697 | cmdp->cmdtype = CMDBUILTIN; |
| 13698 | cmdp->param.cmd = bcmd; |
| 13699 | INT_ON; |
| 13700 | success: |
| 13701 | cmdp->rehash = 0; |
| 13702 | entry->cmdtype = cmdp->cmdtype; |
| 13703 | entry->u = cmdp->param; |
| 13704 | } |
| 13705 | |
| 13706 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13707 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13708 | * The trap builtin. |
| 13709 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13710 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13711 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13712 | { |
| 13713 | char *action; |
| 13714 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13715 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13716 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13717 | nextopt(nullstr); |
| 13718 | ap = argptr; |
| 13719 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13720 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13721 | char *tr = trap_ptr[signo]; |
| 13722 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13723 | /* note: bash adds "SIG", but only if invoked |
| 13724 | * as "bash". If called as "sh", or if set -o posix, |
| 13725 | * then it prints short signal names. |
| 13726 | * We are printing short names: */ |
| 13727 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13728 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 13729 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13730 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 13731 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13732 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13733 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13734 | } |
| 13735 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13736 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13737 | if (trap_ptr != trap) { |
| 13738 | free(trap_ptr); |
| 13739 | trap_ptr = trap; |
| 13740 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13741 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13742 | return 0; |
| 13743 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13744 | |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13745 | /* Why the second check? |
| 13746 | * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..." |
| 13747 | * In this case, NUM is signal no, not an action. |
| 13748 | */ |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 13749 | action = NULL; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13750 | if (ap[1] && !is_number(ap[0])) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13751 | action = *ap++; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13752 | |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13753 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13754 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13755 | signo = get_signum(*ap); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13756 | if (signo < 0) { |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13757 | /* Mimic bash message exactly */ |
| 13758 | ash_msg("%s: invalid signal specification", *ap); |
| 13759 | exitcode = 1; |
| 13760 | goto next; |
| 13761 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13762 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13763 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 13764 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13765 | action = NULL; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 13766 | else { |
| 13767 | if (action[0]) /* not NULL and not "" and not "-" */ |
| 13768 | may_have_traps = 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13769 | action = ckstrdup(action); |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 13770 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13771 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 13772 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13773 | trap[signo] = action; |
| 13774 | if (signo != 0) |
| 13775 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13776 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13777 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13778 | ap++; |
| 13779 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13780 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13781 | } |
| 13782 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13783 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13784 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13785 | |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13786 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13787 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13788 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13789 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 13790 | unsigned col; |
| 13791 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13792 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 13793 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 13794 | "Built-in commands:\n" |
| 13795 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 13796 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13797 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 13798 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13799 | if (col > 60) { |
| 13800 | out1fmt("\n"); |
| 13801 | col = 0; |
| 13802 | } |
| 13803 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13804 | # if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 13805 | { |
| 13806 | const char *a = applet_names; |
| 13807 | while (*a) { |
| 13808 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 13809 | if (col > 60) { |
| 13810 | out1fmt("\n"); |
| 13811 | col = 0; |
| 13812 | } |
Ron Yorston | 2b91958 | 2016-04-08 11:57:20 +0100 | [diff] [blame] | 13813 | while (*a++ != '\0') |
| 13814 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13815 | } |
| 13816 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13817 | # endif |
Denys Vlasenko | ebedb94 | 2016-10-02 18:45:09 +0200 | [diff] [blame] | 13818 | newline_and_flush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13819 | return EXIT_SUCCESS; |
| 13820 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 13821 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13822 | |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 13823 | #if MAX_HISTORY |
| 13824 | static int FAST_FUNC |
| 13825 | historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 13826 | { |
Ron Yorston | 9f3b410 | 2019-12-16 09:31:10 +0000 | [diff] [blame] | 13827 | show_history(line_input_state); |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 13828 | return EXIT_SUCCESS; |
| 13829 | } |
| 13830 | #endif |
| 13831 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13832 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13833 | * The export and readonly commands. |
| 13834 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13835 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13836 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13837 | { |
| 13838 | struct var *vp; |
| 13839 | char *name; |
| 13840 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13841 | char **aptr; |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13842 | char opt; |
| 13843 | int flag; |
| 13844 | int flag_off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13845 | |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13846 | /* "readonly" in bash accepts, but ignores -n. |
| 13847 | * We do the same: it saves a conditional in nextopt's param. |
| 13848 | */ |
| 13849 | flag_off = 0; |
| 13850 | while ((opt = nextopt("np")) != '\0') { |
| 13851 | if (opt == 'n') |
| 13852 | flag_off = VEXPORT; |
| 13853 | } |
| 13854 | flag = VEXPORT; |
| 13855 | if (argv[0][0] == 'r') { |
| 13856 | flag = VREADONLY; |
| 13857 | flag_off = 0; /* readonly ignores -n */ |
| 13858 | } |
| 13859 | flag_off = ~flag_off; |
| 13860 | |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 13861 | /*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] | 13862 | { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13863 | aptr = argptr; |
| 13864 | name = *aptr; |
| 13865 | if (name) { |
| 13866 | do { |
| 13867 | p = strchr(name, '='); |
| 13868 | if (p != NULL) { |
| 13869 | p++; |
| 13870 | } else { |
| 13871 | vp = *findvar(hashvar(name), name); |
| 13872 | if (vp) { |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13873 | vp->flags = ((vp->flags | flag) & flag_off); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13874 | continue; |
| 13875 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13876 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13877 | setvar(name, p, (flag & flag_off)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13878 | } while ((name = *++aptr) != NULL); |
| 13879 | return 0; |
| 13880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13881 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 13882 | |
| 13883 | /* No arguments. Show the list of exported or readonly vars. |
| 13884 | * -n is ignored. |
| 13885 | */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13886 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13887 | return 0; |
| 13888 | } |
| 13889 | |
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 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13892 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13893 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13894 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 13895 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13896 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13897 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13898 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13899 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 13900 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13901 | } |
| 13902 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13903 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13904 | * The unset builtin command. We unset the function before we unset the |
| 13905 | * variable to allow a function to be unset when there is a readonly variable |
| 13906 | * with the same name. |
| 13907 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13908 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13909 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13910 | { |
| 13911 | char **ap; |
| 13912 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13913 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13914 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13915 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13916 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13917 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13918 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 13919 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13920 | if (flag != 'f') { |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 13921 | unsetvar(*ap); |
| 13922 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13923 | } |
| 13924 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13925 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13926 | } |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 13927 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13928 | } |
| 13929 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13930 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13931 | ' ', offsetof(struct tms, tms_utime), |
| 13932 | '\n', offsetof(struct tms, tms_stime), |
| 13933 | ' ', offsetof(struct tms, tms_cutime), |
| 13934 | '\n', offsetof(struct tms, tms_cstime), |
| 13935 | 0 |
| 13936 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13937 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13938 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13939 | { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13940 | unsigned clk_tck; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13941 | const unsigned char *p; |
| 13942 | struct tms buf; |
| 13943 | |
Bartosz Golaszewski | 5d2e409 | 2014-06-22 14:01:13 +0200 | [diff] [blame] | 13944 | clk_tck = bb_clk_tck(); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13945 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13946 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13947 | p = timescmd_str; |
| 13948 | do { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13949 | unsigned sec, frac; |
| 13950 | unsigned long t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13951 | t = *(clock_t *)(((char *) &buf) + p[1]); |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 13952 | sec = t / clk_tck; |
| 13953 | frac = t % clk_tck; |
| 13954 | out1fmt("%um%u.%03us%c", |
| 13955 | sec / 60, sec % 60, |
| 13956 | (frac * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13957 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13958 | p += 2; |
| 13959 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 13960 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13961 | return 0; |
| 13962 | } |
| 13963 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 13964 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13965 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 13966 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13967 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13968 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13969 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13970 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13971 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13972 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13973 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13974 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13975 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13976 | argv++; |
| 13977 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13978 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13979 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 13980 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13981 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13982 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 13983 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13984 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13985 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 13986 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13987 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13988 | * The read builtin. Options: |
| 13989 | * -r Do not interpret '\' specially |
| 13990 | * -s Turn off echo (tty only) |
| 13991 | * -n NCHARS Read NCHARS max |
| 13992 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 13993 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 13994 | * -u FD Read from given FD instead of fd 0 |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 13995 | * -d DELIM End on DELIM char, not newline |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13996 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13997 | * TODO: bash also has: |
| 13998 | * -a ARRAY Read into array[0],[1],etc |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 13999 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14000 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14001 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14002 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14003 | { |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14004 | struct builtin_read_params params; |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14005 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14006 | int i; |
| 14007 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14008 | memset(¶ms, 0, sizeof(params)); |
| 14009 | |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14010 | while ((i = nextopt("p:u:rt:n:sd:")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 14011 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14012 | case 'p': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14013 | params.opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14014 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14015 | case 'n': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14016 | params.opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14017 | break; |
| 14018 | case 's': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14019 | params.read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14020 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14021 | case 't': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14022 | params.opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14023 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14024 | case 'r': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14025 | params.read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14026 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14027 | case 'u': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14028 | params.opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14029 | break; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14030 | #if BASH_READ_D |
| 14031 | case 'd': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14032 | params.opt_d = optionarg; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14033 | break; |
| 14034 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14035 | default: |
| 14036 | break; |
| 14037 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14038 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14039 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14040 | params.argv = argptr; |
| 14041 | params.setvar = setvar0; |
| 14042 | params.ifs = bltinlookup("IFS"); /* can be NULL */ |
| 14043 | |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14044 | /* "read -s" needs to save/restore termios, can't allow ^C |
| 14045 | * to jump out of it. |
| 14046 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14047 | again: |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14048 | INT_OFF; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14049 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14050 | INT_ON; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 14051 | |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14052 | if ((uintptr_t)r == 1 && errno == EINTR) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14053 | /* To get SIGCHLD: sleep 1 & read x; echo $x |
| 14054 | * Correct behavior is to not exit "read" |
| 14055 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14056 | if (pending_sig == 0) |
| 14057 | goto again; |
| 14058 | } |
| 14059 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14060 | if ((uintptr_t)r > 1) |
| 14061 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 14062 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14063 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14064 | } |
| 14065 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14066 | static int FAST_FUNC |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14067 | umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14068 | { |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14069 | static const char permuser[3] ALIGN1 = "ogu"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14070 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14071 | mode_t mask; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14072 | int symbolic_mode = 0; |
| 14073 | |
| 14074 | while (nextopt("S") != '\0') { |
| 14075 | symbolic_mode = 1; |
| 14076 | } |
| 14077 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14078 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14079 | mask = umask(0); |
| 14080 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14081 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14082 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14083 | if (*argptr == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14084 | if (symbolic_mode) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14085 | char buf[sizeof(",u=rwx,g=rwx,o=rwx")]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14086 | char *p = buf; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14087 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14088 | |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14089 | i = 2; |
| 14090 | for (;;) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14091 | *p++ = ','; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14092 | *p++ = permuser[i]; |
| 14093 | *p++ = '='; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14094 | /* mask is 0..0uuugggooo. i=2 selects uuu bits */ |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14095 | if (!(mask & 0400)) *p++ = 'r'; |
| 14096 | if (!(mask & 0200)) *p++ = 'w'; |
| 14097 | if (!(mask & 0100)) *p++ = 'x'; |
| 14098 | mask <<= 3; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14099 | if (--i < 0) |
| 14100 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14101 | } |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14102 | *p = '\0'; |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14103 | puts(buf + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14104 | } else { |
Denys Vlasenko | ec046f7 | 2015-10-07 17:57:53 +0200 | [diff] [blame] | 14105 | out1fmt("%04o\n", mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14106 | } |
| 14107 | } else { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14108 | char *modestr = *argptr; |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 14109 | /* numeric umasks are taken as-is */ |
| 14110 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14111 | if (!isdigit(modestr[0])) |
| 14112 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 14113 | mask = bb_parse_mode(modestr, mask); |
| 14114 | if ((unsigned)mask > 0777) { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14115 | ash_msg_and_raise_error("illegal mode: %s", modestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14116 | } |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14117 | if (!isdigit(modestr[0])) |
| 14118 | mask ^= 0777; |
| 14119 | umask(mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14120 | } |
| 14121 | return 0; |
| 14122 | } |
| 14123 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14124 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14125 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14126 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14127 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14128 | } |
| 14129 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14130 | /* ============ main() and helpers */ |
| 14131 | |
| 14132 | /* |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14133 | * This routine is called when an error or an interrupt occurs in an |
| 14134 | * interactive shell and control is returned to the main command loop |
| 14135 | * but prior to exitshell. |
| 14136 | */ |
| 14137 | static void |
| 14138 | exitreset(void) |
| 14139 | { |
| 14140 | /* from eval.c: */ |
| 14141 | if (savestatus >= 0) { |
| 14142 | if (exception_type == EXEXIT || evalskip == SKIPFUNCDEF) |
| 14143 | exitstatus = savestatus; |
| 14144 | savestatus = -1; |
| 14145 | } |
| 14146 | evalskip = 0; |
| 14147 | loopnest = 0; |
| 14148 | |
| 14149 | /* from expand.c: */ |
| 14150 | ifsfree(); |
| 14151 | |
| 14152 | /* from redir.c: */ |
| 14153 | unwindredir(NULL); |
| 14154 | } |
| 14155 | |
| 14156 | /* |
| 14157 | * This routine is called when an error or an interrupt occurs in an |
| 14158 | * interactive shell and control is returned to the main command loop. |
| 14159 | * (In dash, this function is auto-generated by build machinery). |
| 14160 | */ |
| 14161 | static void |
| 14162 | reset(void) |
| 14163 | { |
| 14164 | /* from input.c: */ |
| 14165 | g_parsefile->left_in_buffer = 0; |
| 14166 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denys Vlasenko | 51a471d | 2020-12-24 00:22:24 +0100 | [diff] [blame] | 14167 | g_parsefile->unget = 0; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14168 | popallfiles(); |
| 14169 | |
| 14170 | /* from var.c: */ |
| 14171 | unwindlocalvars(NULL); |
| 14172 | } |
| 14173 | |
| 14174 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14175 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14176 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14177 | static void |
| 14178 | exitshell(void) |
| 14179 | { |
| 14180 | struct jmploc loc; |
| 14181 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14182 | |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14183 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 14184 | save_history(line_input_state); /* may be NULL */ |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14185 | #endif |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 14186 | savestatus = exitstatus; |
| 14187 | TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus)); |
| 14188 | if (setjmp(loc.loc)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14189 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14190 | exception_handler = &loc; |
| 14191 | p = trap[0]; |
| 14192 | if (p) { |
| 14193 | trap[0] = NULL; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 14194 | evalskip = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14195 | evalstring(p, 0); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14196 | evalskip = SKIPFUNCDEF; |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14197 | /*free(p); - we'll exit soon */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14198 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14199 | out: |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14200 | exitreset(); |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14201 | /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}". |
| 14202 | * our setjobctl(0) does not panic if tcsetpgrp fails inside it. |
| 14203 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14204 | setjobctl(0); |
Denys Vlasenko | caee80c | 2016-10-25 20:49:53 +0200 | [diff] [blame] | 14205 | flush_stdout_stderr(); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14206 | _exit(exitstatus); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14207 | /* NOTREACHED */ |
| 14208 | } |
| 14209 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14210 | /* Don't inline: conserve stack of caller from having our locals too */ |
| 14211 | static NOINLINE void |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 14212 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14213 | { |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 14214 | /* we will never free this */ |
| 14215 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denys Vlasenko | 0485b67 | 2017-08-14 19:46:56 +0200 | [diff] [blame] | 14216 | basepf.linno = 1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14217 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14218 | 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] | 14219 | setsignal(SIGCHLD); |
| 14220 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14221 | { |
| 14222 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14223 | const char *p; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14224 | |
| 14225 | initvar(); |
| 14226 | for (envp = environ; envp && *envp; envp++) { |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 14227 | /* Used to have |
| 14228 | * p = endofname(*envp); |
| 14229 | * if (p != *envp && *p == '=') { |
| 14230 | * here to weed out badly-named variables, but this breaks |
| 14231 | * scenarios where people do want them passed to children: |
| 14232 | * import os |
| 14233 | * os.environ["test-test"]="test" |
| 14234 | * if os.fork() == 0: |
| 14235 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 14236 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) # breaks this |
| 14237 | */ |
| 14238 | if (strchr(*envp, '=')) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14239 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 14240 | } |
| 14241 | } |
| 14242 | |
Denys Vlasenko | 67dae15 | 2018-08-05 13:59:35 +0200 | [diff] [blame] | 14243 | setvareq((char*)defifsvar, VTEXTFIXED); |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 14244 | setvareq((char*)defoptindvar, VTEXTFIXED); |
| 14245 | |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14246 | setvar0("PPID", utoa(getppid())); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14247 | #if BASH_SHLVL_VAR |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14248 | p = lookupvar("SHLVL"); |
Denys Vlasenko | 5680e98 | 2014-01-07 16:12:48 +0100 | [diff] [blame] | 14249 | setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14250 | #endif |
| 14251 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14252 | if (!lookupvar("HOSTNAME")) { |
| 14253 | struct utsname uts; |
| 14254 | uname(&uts); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14255 | setvar0("HOSTNAME", uts.nodename); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14256 | } |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14257 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14258 | p = lookupvar("PWD"); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14259 | if (p) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14260 | struct stat st1, st2; |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14261 | if (p[0] != '/' || stat(p, &st1) || stat(".", &st2) |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14262 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino |
| 14263 | ) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14264 | p = NULL; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14265 | } |
| 14266 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14267 | setpwd(p, 0); |
| 14268 | } |
| 14269 | } |
| 14270 | |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14271 | |
| 14272 | //usage:#define ash_trivial_usage |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 14273 | //usage: "[-il] [-/+Cabefmnuvx] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
| 14274 | //////// comes from ^^^^^^^^^^optletters |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14275 | //usage:#define ash_full_usage "\n\n" |
| 14276 | //usage: "Unix shell interpreter" |
| 14277 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14278 | /* |
| 14279 | * Process the shell command line arguments. |
| 14280 | */ |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14281 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14282 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14283 | { |
| 14284 | int i; |
| 14285 | const char *xminusc; |
| 14286 | char **xargv; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14287 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14288 | |
| 14289 | xargv = argv; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14290 | login_sh = xargv[0] && xargv[0][0] == '-'; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14291 | #if NUM_SCRIPTS > 0 |
| 14292 | if (minusc) |
| 14293 | goto setarg0; |
| 14294 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14295 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14296 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14297 | xargv++; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14298 | argptr = xargv; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14299 | for (i = 0; i < NOPTS; i++) |
| 14300 | optlist[i] = 2; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 14301 | if (options(&login_sh)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 14302 | /* it already printed err message */ |
| 14303 | raise_exception(EXERROR); |
| 14304 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14305 | xargv = argptr; |
| 14306 | xminusc = minusc; |
| 14307 | if (*xargv == NULL) { |
| 14308 | if (xminusc) |
| 14309 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 14310 | sflag = 1; |
| 14311 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14312 | if (iflag == 2 /* no explicit -i given */ |
| 14313 | && sflag == 1 /* -s given (or implied) */ |
| 14314 | && !minusc /* bash compat: ash -sc 'echo $-' is not interactive (dash is) */ |
| 14315 | && isatty(0) && isatty(1) /* we are on tty */ |
| 14316 | ) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14317 | iflag = 1; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14318 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14319 | if (mflag == 2) |
| 14320 | mflag = iflag; |
Denys Vlasenko | 85158b6 | 2021-01-03 12:14:58 +0100 | [diff] [blame] | 14321 | /* Unset options which weren't explicitly set or unset */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14322 | for (i = 0; i < NOPTS; i++) |
Denys Vlasenko | 85158b6 | 2021-01-03 12:14:58 +0100 | [diff] [blame] | 14323 | optlist[i] &= 1; /* same effect as "if (optlist[i] == 2) optlist[i] = 0;" */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14324 | #if DEBUG == 2 |
| 14325 | debug = 1; |
| 14326 | #endif |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14327 | /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14328 | if (xminusc) { |
| 14329 | minusc = *xargv++; |
| 14330 | if (*xargv) |
| 14331 | goto setarg0; |
| 14332 | } else if (!sflag) { |
| 14333 | setinputfile(*xargv, 0); |
| 14334 | setarg0: |
| 14335 | arg0 = *xargv++; |
| 14336 | commandname = arg0; |
| 14337 | } |
| 14338 | |
| 14339 | shellparam.p = xargv; |
| 14340 | #if ENABLE_ASH_GETOPTS |
| 14341 | shellparam.optind = 1; |
| 14342 | shellparam.optoff = -1; |
| 14343 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14344 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14345 | while (*xargv) { |
| 14346 | shellparam.nparam++; |
| 14347 | xargv++; |
| 14348 | } |
Denys Vlasenko | 31df5a3 | 2020-12-13 16:36:28 +0100 | [diff] [blame] | 14349 | |
| 14350 | /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 14351 | * Try: |
| 14352 | * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect |
| 14353 | * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed |
| 14354 | * NB: must do it before setting up signals (in optschanged()) |
| 14355 | * and reading .profile etc (after we return from here): |
| 14356 | */ |
| 14357 | if (iflag) |
| 14358 | signal(SIGHUP, SIG_DFL); |
| 14359 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14360 | optschanged(); |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14361 | |
| 14362 | return login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14363 | } |
| 14364 | |
| 14365 | /* |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14366 | * Read /etc/profile, ~/.profile, $ENV. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14367 | */ |
| 14368 | static void |
| 14369 | read_profile(const char *name) |
| 14370 | { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 14371 | name = expandstr(name, DQSYNTAX); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14372 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 14373 | return; |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 14374 | cmdloop(0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14375 | popfile(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14376 | } |
| 14377 | |
| 14378 | #if PROFILE |
| 14379 | static short profile_buf[16384]; |
| 14380 | extern int etext(); |
| 14381 | #endif |
| 14382 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14383 | /* |
| 14384 | * Main routine. We initialize things, parse the arguments, execute |
| 14385 | * profiles if we're a login shell, and then call cmdloop to execute |
| 14386 | * commands. The setjmp call sets up the location to jump to when an |
| 14387 | * exception occurs. When an exception occurs the variable "state" |
| 14388 | * is used to figure out how far we had gotten. |
| 14389 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14390 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14391 | #if NUM_SCRIPTS > 0 |
| 14392 | int ash_main(int argc, char **argv) |
| 14393 | #else |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14394 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14395 | #endif |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14396 | /* note: 'argc' is used only if embedded scripts are enabled */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14397 | { |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14398 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14399 | struct jmploc jmploc; |
| 14400 | struct stackmark smark; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14401 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14402 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14403 | /* Initialize global data */ |
| 14404 | INIT_G_misc(); |
| 14405 | INIT_G_memstack(); |
| 14406 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14407 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14408 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14409 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14410 | INIT_G_cmdtable(); |
| 14411 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14412 | #if PROFILE |
| 14413 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 14414 | #endif |
| 14415 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14416 | state = 0; |
| 14417 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14418 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14419 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14420 | |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14421 | exitreset(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14422 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14423 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14424 | s = state; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14425 | if (e == EXEND || e == EXEXIT || s == 0 || iflag == 0 || shlvl) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14426 | exitshell(); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14427 | } |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14428 | |
| 14429 | reset(); |
| 14430 | |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14431 | if (e == EXINT) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 14432 | newline_and_flush(stderr); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14433 | } |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14434 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14435 | popstackmark(&smark); |
| 14436 | FORCE_INT_ON; /* enable interrupts */ |
| 14437 | if (s == 1) |
| 14438 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14439 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14440 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14441 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14442 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14443 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14444 | } |
| 14445 | exception_handler = &jmploc; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14446 | rootpid = getpid(); |
| 14447 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14448 | init(); |
| 14449 | setstackmark(&smark); |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14450 | |
| 14451 | #if NUM_SCRIPTS > 0 |
| 14452 | if (argc < 0) |
| 14453 | /* Non-NULL minusc tells procargs that an embedded script is being run */ |
| 14454 | minusc = get_script_content(-argc - 1); |
| 14455 | #endif |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14456 | login_sh = procargs(argv); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 14457 | #if DEBUG |
| 14458 | TRACE(("Shell args: ")); |
| 14459 | trace_puts_args(argv); |
| 14460 | #endif |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14461 | |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14462 | if (login_sh) { |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14463 | const char *hp; |
| 14464 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14465 | state = 1; |
| 14466 | read_profile("/etc/profile"); |
| 14467 | state1: |
| 14468 | state = 2; |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14469 | hp = lookupvar("HOME"); |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14470 | if (hp) |
| 14471 | read_profile("$HOME/.profile"); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14472 | } |
| 14473 | state2: |
| 14474 | state = 3; |
| 14475 | if ( |
| 14476 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 14477 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14478 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 14479 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14480 | ) { |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14481 | const char *shinit = lookupvar("ENV"); |
| 14482 | if (shinit != NULL && *shinit != '\0') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14483 | read_profile(shinit); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14484 | } |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14485 | popstackmark(&smark); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14486 | state3: |
| 14487 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14488 | if (minusc) { |
| 14489 | /* evalstring pushes parsefile stack. |
| 14490 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 14491 | * is one of stacked source fds. |
| 14492 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14493 | |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 14494 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 14495 | // ^^ not necessary since now we special-case fd 0 |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 14496 | // in save_fd_on_redirect() |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14497 | |
| 14498 | // dash: evalstring(minusc, sflag ? 0 : EV_EXIT); |
| 14499 | // The above makes |
| 14500 | // ash -sc 'echo $-' |
| 14501 | // continue reading input from stdin after running 'echo'. |
| 14502 | // bash does not do this: it prints "hBcs" and exits. |
| 14503 | evalstring(minusc, EV_EXIT); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14504 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14505 | |
| 14506 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 14507 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 3f8ec00 | 2021-01-03 10:55:39 +0100 | [diff] [blame] | 14508 | if (line_input_state) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14509 | const char *hp = lookupvar("HISTFILE"); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14510 | if (!hp) { |
| 14511 | hp = lookupvar("HOME"); |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14512 | if (hp) { |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14513 | INT_OFF; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14514 | hp = concat_path_file(hp, ".ash_history"); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14515 | setvar0("HISTFILE", hp); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14516 | free((char*)hp); |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14517 | INT_ON; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14518 | hp = lookupvar("HISTFILE"); |
| 14519 | } |
| 14520 | } |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14521 | if (hp) |
Denys Vlasenko | 3f8ec00 | 2021-01-03 10:55:39 +0100 | [diff] [blame] | 14522 | line_input_state->hist_file = xstrdup(hp); |
Denys Vlasenko | 2c4de5b | 2011-03-31 13:16:52 +0200 | [diff] [blame] | 14523 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 14524 | hp = lookupvar("HISTFILESIZE"); |
| 14525 | line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 14526 | # endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14527 | } |
| 14528 | #endif |
| 14529 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 14530 | cmdloop(1); |
| 14531 | } |
| 14532 | #if PROFILE |
| 14533 | monitor(0); |
| 14534 | #endif |
| 14535 | #ifdef GPROF |
| 14536 | { |
| 14537 | extern void _mcleanup(void); |
| 14538 | _mcleanup(); |
| 14539 | } |
| 14540 | #endif |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14541 | TRACE(("End of main reached\n")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14542 | exitshell(); |
| 14543 | /* NOTREACHED */ |
| 14544 | } |
| 14545 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14546 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14547 | /*- |
| 14548 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 14549 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14550 | * |
| 14551 | * This code is derived from software contributed to Berkeley by |
| 14552 | * Kenneth Almquist. |
| 14553 | * |
| 14554 | * Redistribution and use in source and binary forms, with or without |
| 14555 | * modification, are permitted provided that the following conditions |
| 14556 | * are met: |
| 14557 | * 1. Redistributions of source code must retain the above copyright |
| 14558 | * notice, this list of conditions and the following disclaimer. |
| 14559 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14560 | * notice, this list of conditions and the following disclaimer in the |
| 14561 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 14562 | * 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] | 14563 | * may be used to endorse or promote products derived from this software |
| 14564 | * without specific prior written permission. |
| 14565 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 14566 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14567 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 14568 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 14569 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 14570 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 14571 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 14572 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 14573 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 14574 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 14575 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 14576 | * SUCH DAMAGE. |
| 14577 | */ |