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: |
Shawn Landden | 58598eb | 2022-08-27 19:56:21 +0200 | [diff] [blame] | 137 | //config:config ASH_SLEEP |
| 138 | //config: bool "sleep builtin" |
| 139 | //config: default y |
| 140 | //config: depends on SHELL_ASH |
| 141 | //config: |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 142 | //config:config ASH_HELP |
| 143 | //config: bool "help 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 | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 146 | //config: |
| 147 | //config:config ASH_GETOPTS |
| 148 | //config: bool "getopts builtin" |
| 149 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 150 | //config: depends on SHELL_ASH |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 151 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 152 | //config:config ASH_CMDCMD |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 153 | //config: bool "command builtin" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 154 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 155 | //config: depends on SHELL_ASH |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 156 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 157 | //config: Enable support for the 'command' builtin, which allows |
| 158 | //config: you to run the specified command or builtin, |
| 159 | //config: even when there is a function with the same name. |
Kang-Che Sung | 6cd0294 | 2017-01-06 17:02:03 +0100 | [diff] [blame] | 160 | //config: |
| 161 | //config:endif # ash options |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 162 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 163 | //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 164 | // APPLET_ODDNAME:name main location suid_type help |
| 165 | //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] | 166 | //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] | 167 | |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 168 | //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] | 169 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
| 170 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 171 | /* |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 172 | * DEBUG=1 to compile in debugging ('set -o debug' turns on) |
| 173 | * DEBUG=2 to compile in and turn on debugging. |
| 174 | * When debugging is on ("set -o debug" was executed, or DEBUG=2), |
| 175 | * debugging info is written to ./trace, quit signal generates core dump. |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 176 | */ |
| 177 | #define DEBUG 0 |
| 178 | /* Tweak debug output verbosity here */ |
| 179 | #define DEBUG_TIME 0 |
| 180 | #define DEBUG_PID 1 |
| 181 | #define DEBUG_SIG 1 |
| 182 | #define DEBUG_INTONOFF 0 |
| 183 | |
| 184 | #define PROFILE 0 |
| 185 | |
| 186 | #define JOBS ENABLE_ASH_JOB_CONTROL |
| 187 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 188 | #include <fnmatch.h> |
| 189 | #include <sys/times.h> |
| 190 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 191 | #include "busybox.h" /* for applet_names */ |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 192 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 193 | # include "embedded_scripts.h" |
| 194 | #else |
| 195 | # define NUM_SCRIPTS 0 |
| 196 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 197 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 198 | /* So far, all bash compat is controlled by one config option */ |
| 199 | /* Separate defines document which part of code implements what */ |
| 200 | /* function keyword */ |
| 201 | #define BASH_FUNCTION ENABLE_ASH_BASH_COMPAT |
| 202 | #define IF_BASH_FUNCTION IF_ASH_BASH_COMPAT |
| 203 | /* &>file */ |
| 204 | #define BASH_REDIR_OUTPUT ENABLE_ASH_BASH_COMPAT |
| 205 | #define IF_BASH_REDIR_OUTPUT IF_ASH_BASH_COMPAT |
| 206 | /* $'...' */ |
| 207 | #define BASH_DOLLAR_SQUOTE ENABLE_ASH_BASH_COMPAT |
| 208 | #define IF_BASH_DOLLAR_SQUOTE IF_ASH_BASH_COMPAT |
| 209 | #define BASH_PATTERN_SUBST ENABLE_ASH_BASH_COMPAT |
| 210 | #define IF_BASH_PATTERN_SUBST IF_ASH_BASH_COMPAT |
| 211 | #define BASH_SUBSTR ENABLE_ASH_BASH_COMPAT |
| 212 | #define IF_BASH_SUBSTR IF_ASH_BASH_COMPAT |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 213 | /* BASH_TEST2: [[ EXPR ]] |
| 214 | * Status of [[ support: |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 215 | * && and || work as they should |
| 216 | * = is glob match operator, not equality operator: STR = GLOB |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 217 | * == same as = |
Denys Vlasenko | a7c0653 | 2020-10-31 04:32:34 +0100 | [diff] [blame] | 218 | * =~ is regex match operator: STR =~ REGEX |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 219 | * TODO: |
| 220 | * singleword+noglob expansion: |
| 221 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 222 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 223 | * 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] | 224 | * ( ) < > should not have special meaning (IOW: should not require quoting) |
| 225 | * 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] | 226 | */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 227 | #define BASH_TEST2 (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST) |
| 228 | #define BASH_SOURCE ENABLE_ASH_BASH_COMPAT |
| 229 | #define BASH_PIPEFAIL ENABLE_ASH_BASH_COMPAT |
| 230 | #define BASH_HOSTNAME_VAR ENABLE_ASH_BASH_COMPAT |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 231 | #define BASH_EPOCH_VARS ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 232 | #define BASH_SHLVL_VAR ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | f8cdc7a | 2017-08-04 15:24:49 +0200 | [diff] [blame] | 233 | #define BASH_XTRACEFD ENABLE_ASH_BASH_COMPAT |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 234 | #define BASH_READ_D ENABLE_ASH_BASH_COMPAT |
| 235 | #define IF_BASH_READ_D IF_ASH_BASH_COMPAT |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 236 | #define BASH_WAIT_N ENABLE_ASH_BASH_COMPAT |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 237 | /* <(...) and >(...) */ |
| 238 | #if HAVE_DEV_FD |
| 239 | # define BASH_PROCESS_SUBST ENABLE_ASH_BASH_COMPAT |
| 240 | # define IF_BASH_PROCESS_SUBST IF_ASH_BASH_COMPAT |
| 241 | #else |
| 242 | # define BASH_PROCESS_SUBST 0 |
| 243 | # define IF_BASH_PROCESS_SUBST(...) |
| 244 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 245 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 246 | #if defined(__ANDROID_API__) && __ANDROID_API__ <= 24 |
| 247 | /* Bionic at least up to version 24 has no glob() */ |
| 248 | # undef ENABLE_ASH_INTERNAL_GLOB |
| 249 | # define ENABLE_ASH_INTERNAL_GLOB 1 |
| 250 | #endif |
| 251 | |
| 252 | #if !ENABLE_ASH_INTERNAL_GLOB && defined(__UCLIBC__) |
| 253 | # error uClibc glob() is buggy, use ASH_INTERNAL_GLOB. |
| 254 | # error The bug is: for "$PWD"/<pattern> ash will escape e.g. dashes in "$PWD" |
| 255 | # error with backslash, even ones which do not need to be: "/a-b" -> "/a\-b" |
| 256 | # error glob() should unbackslash them and match. uClibc does not unbackslash, |
| 257 | # error fails to match dirname, subsequently not expanding <pattern> in it. |
| 258 | // Testcase: |
| 259 | // if (glob("/etc/polkit\\-1", 0, NULL, &pglob)) - this returns 0 on uclibc, no bug |
| 260 | // if (glob("/etc/polkit\\-1/*", 0, NULL, &pglob)) printf("uclibc bug!\n"); |
| 261 | #endif |
| 262 | |
| 263 | #if !ENABLE_ASH_INTERNAL_GLOB |
| 264 | # include <glob.h> |
| 265 | #endif |
| 266 | |
| 267 | #include "unicode.h" |
| 268 | #include "shell_common.h" |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 269 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 270 | # include "math.h" |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 271 | #else |
| 272 | typedef long arith_t; |
| 273 | # define ARITH_FMT "%ld" |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 274 | #endif |
| 275 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 276 | # include "random.h" |
| 277 | #else |
| 278 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 279 | #endif |
| 280 | |
| 281 | #include "NUM_APPLETS.h" |
| 282 | #if NUM_APPLETS == 1 |
| 283 | /* STANDALONE does not make sense, and won't compile */ |
| 284 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 285 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 286 | # undef IF_FEATURE_SH_STANDALONE |
| 287 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 288 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 289 | # define IF_FEATURE_SH_STANDALONE(...) |
| 290 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 291 | #endif |
| 292 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 293 | #ifndef F_DUPFD_CLOEXEC |
| 294 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 295 | #endif |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 296 | #ifndef O_CLOEXEC |
| 297 | # define O_CLOEXEC 0 |
| 298 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 299 | #ifndef PIPE_BUF |
| 300 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 301 | #endif |
| 302 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 303 | #ifndef unlikely |
| 304 | # define unlikely(cond) (cond) |
| 305 | #endif |
| 306 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 307 | #if !BB_MMU |
| 308 | # error "Do not even bother, ash will not run on NOMMU machine" |
| 309 | #endif |
| 310 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 311 | /* ============ Hash table sizes. Configurable. */ |
| 312 | |
| 313 | #define VTABSIZE 39 |
| 314 | #define ATABSIZE 39 |
| 315 | #define CMDTABLESIZE 31 /* should be prime */ |
| 316 | |
| 317 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 318 | /* ============ Shell options */ |
| 319 | |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 320 | /* If you add/change options hare, update --help text too */ |
Denys Vlasenko | ca466f3 | 2022-02-06 19:53:10 +0100 | [diff] [blame] | 321 | static const char *const optletters_optnames[] ALIGN_PTR = { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 322 | "e" "errexit", |
| 323 | "f" "noglob", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 324 | /* bash has '-o ignoreeof', but no short synonym -I for it */ |
| 325 | /* (in bash, set -I disables invisible variables (what's that?)) */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 326 | "I" "ignoreeof", |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 327 | /* The below allowed this invocation: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 328 | * ash -c 'set -i; echo $-; sleep 5; echo $-' |
| 329 | * to be ^C-ed and get to interactive ash prompt. |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 330 | * bash does not support such "set -i". |
| 331 | * In our code, this is denoted by empty long name: |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 332 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 333 | "i" "", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 334 | /* (removing "i" altogether would remove it from "$-", not good) */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 335 | "m" "monitor", |
| 336 | "n" "noexec", |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 337 | /* Ditto: bash has no "set -s", "set -c" */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 338 | "s" "", |
| 339 | "c" "", |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 340 | "x" "xtrace", |
| 341 | "v" "verbose", |
| 342 | "C" "noclobber", |
| 343 | "a" "allexport", |
| 344 | "b" "notify", |
| 345 | "u" "nounset", |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 346 | "E" "errtrace", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 347 | "\0" "vi" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 348 | #if BASH_PIPEFAIL |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 349 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 350 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 351 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 352 | ,"\0" "nolog" |
| 353 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 354 | #endif |
| 355 | }; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 356 | //bash 4.4.23 also has these opts (with these defaults): |
| 357 | //braceexpand on |
| 358 | //emacs on |
| 359 | //errtrace off |
| 360 | //functrace off |
| 361 | //hashall on |
| 362 | //histexpand off |
| 363 | //history on |
| 364 | //interactive-comments on |
| 365 | //keyword off |
| 366 | //onecmd off |
| 367 | //physical off |
| 368 | //posix off |
| 369 | //privileged off |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 370 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 371 | #define optletters(n) optletters_optnames[n][0] |
| 372 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 373 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 374 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 375 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 376 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 377 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 378 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 379 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 381 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 382 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 383 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 384 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 385 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 386 | * exception handlers, the user should save the value of handler on entry |
| 387 | * to an inner scope, set handler to point to a jmploc structure for the |
| 388 | * inner scope, and restore handler on exit from the scope. |
| 389 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 390 | struct jmploc { |
| 391 | jmp_buf loc; |
| 392 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 393 | |
| 394 | struct globals_misc { |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 395 | uint8_t exitstatus; /* exit status of last command */ |
| 396 | uint8_t back_exitstatus;/* exit status of backquoted command */ |
| 397 | smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 398 | smallint inps4; /* Prevent PS4 nesting. */ |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 399 | int savestatus; /* exit status of last command outside traps */ |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 400 | int rootpid; /* pid of main shell */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 401 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 402 | int shlvl; |
| 403 | #define rootshell (!shlvl) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 404 | int errlinno; |
| 405 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 406 | char *minusc; /* argument to -c option */ |
| 407 | |
| 408 | char *curdir; // = nullstr; /* current working directory */ |
| 409 | char *physdir; // = nullstr; /* physical working directory */ |
| 410 | |
| 411 | char *arg0; /* value of $0 */ |
| 412 | |
| 413 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 414 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 415 | volatile int suppress_int; /* counter */ |
| 416 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 417 | volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */ |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 418 | volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 419 | smallint exception_type; /* kind of exception: */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 420 | #define EXINT 0 /* SIGINT received */ |
| 421 | #define EXERROR 1 /* a generic error */ |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 422 | #define EXEND 3 /* exit the shell */ |
| 423 | #define EXEXIT 4 /* exit the shell via exitcmd */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 424 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 425 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 426 | |
| 427 | char optlist[NOPTS]; |
| 428 | #define eflag optlist[0] |
| 429 | #define fflag optlist[1] |
| 430 | #define Iflag optlist[2] |
| 431 | #define iflag optlist[3] |
| 432 | #define mflag optlist[4] |
| 433 | #define nflag optlist[5] |
| 434 | #define sflag optlist[6] |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 435 | #define cflag optlist[7] |
| 436 | #define xflag optlist[8] |
| 437 | #define vflag optlist[9] |
| 438 | #define Cflag optlist[10] |
| 439 | #define aflag optlist[11] |
| 440 | #define bflag optlist[12] |
| 441 | #define uflag optlist[13] |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 442 | #define Eflag optlist[14] |
| 443 | #define viflag optlist[15] |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 444 | #if BASH_PIPEFAIL |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 445 | # define pipefail optlist[16] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 446 | #else |
| 447 | # define pipefail 0 |
| 448 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 449 | #if DEBUG |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 450 | # define nolog optlist[16 + BASH_PIPEFAIL] |
| 451 | # define debug optlist[17 + BASH_PIPEFAIL] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 452 | #endif |
| 453 | |
| 454 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Sigmode records the current value of the signal handlers for the various |
| 457 | * 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] | 458 | * 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] | 459 | */ |
| 460 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 461 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 462 | #define S_CATCH 2 /* signal is caught */ |
| 463 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 464 | #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] | 465 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 466 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 467 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 468 | uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */ |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 469 | char *trap[NSIG + 1]; |
| 470 | /* trap[0] is EXIT trap, trap[NTRAP_ERR] is ERR trap, other trap[i] are signal traps */ |
| 471 | #define NTRAP_ERR NSIG |
| 472 | #define NTRAP_LAST NSIG |
| 473 | |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 474 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 475 | |
| 476 | /* Rarely referenced stuff */ |
| 477 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 478 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 479 | #endif |
| 480 | pid_t backgndpid; /* pid of last background process */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 481 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 482 | extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 483 | #define G_misc (*ash_ptr_to_globals_misc) |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 484 | #define exitstatus (G_misc.exitstatus ) |
| 485 | #define back_exitstatus (G_misc.back_exitstatus ) |
| 486 | #define job_warning (G_misc.job_warning) |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 487 | #define inps4 (G_misc.inps4 ) |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 488 | #define savestatus (G_misc.savestatus ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 489 | #define rootpid (G_misc.rootpid ) |
| 490 | #define shlvl (G_misc.shlvl ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 491 | #define errlinno (G_misc.errlinno ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 492 | #define minusc (G_misc.minusc ) |
| 493 | #define curdir (G_misc.curdir ) |
| 494 | #define physdir (G_misc.physdir ) |
| 495 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 496 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 497 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 498 | #define suppress_int (G_misc.suppress_int ) |
| 499 | #define pending_int (G_misc.pending_int ) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 500 | #define got_sigchld (G_misc.got_sigchld ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 501 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 502 | #define nullstr (G_misc.nullstr ) |
| 503 | #define optlist (G_misc.optlist ) |
| 504 | #define sigmode (G_misc.sigmode ) |
| 505 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 506 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 507 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 508 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 509 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 510 | #define backgndpid (G_misc.backgndpid ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 511 | #define INIT_G_misc() do { \ |
YU Jincheng | 5156b24 | 2021-10-10 02:19:51 +0800 | [diff] [blame] | 512 | XZALLOC_CONST_PTR(&ash_ptr_to_globals_misc, sizeof(G_misc)); \ |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 513 | savestatus = -1; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 514 | curdir = nullstr; \ |
| 515 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 516 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 517 | } while (0) |
| 518 | |
| 519 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 520 | /* ============ DEBUG */ |
| 521 | #if DEBUG |
| 522 | static void trace_printf(const char *fmt, ...); |
| 523 | static void trace_vprintf(const char *fmt, va_list va); |
| 524 | # define TRACE(param) trace_printf param |
| 525 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 526 | # define close(fd) do { \ |
| 527 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 528 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 529 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 530 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 531 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 532 | #else |
| 533 | # define TRACE(param) |
| 534 | # define TRACEV(param) |
| 535 | #endif |
| 536 | |
| 537 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 538 | /* ============ Utility functions */ |
Denys Vlasenko | 1961aea | 2013-02-26 00:36:53 +0100 | [diff] [blame] | 539 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 540 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 541 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 542 | static int |
| 543 | isdigit_str9(const char *str) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 544 | { |
| 545 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 546 | while (--maxlen && isdigit(*str)) |
| 547 | str++; |
| 548 | return (*str == '\0'); |
| 549 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 550 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 551 | static const char * |
| 552 | var_end(const char *var) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 553 | { |
| 554 | while (*var) |
| 555 | if (*var++ == '=') |
| 556 | break; |
| 557 | return var; |
| 558 | } |
| 559 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 560 | |
Denys Vlasenko | 2124c0e | 2020-12-19 14:33:02 +0100 | [diff] [blame] | 561 | /* ============ Parser data */ |
| 562 | |
| 563 | /* |
| 564 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 565 | */ |
| 566 | struct strlist { |
| 567 | struct strlist *next; |
| 568 | char *text; |
| 569 | }; |
| 570 | |
| 571 | struct alias; |
| 572 | |
| 573 | struct strpush { |
| 574 | struct strpush *prev; /* preceding string on stack */ |
| 575 | char *prev_string; |
| 576 | int prev_left_in_line; |
| 577 | #if ENABLE_ASH_ALIAS |
| 578 | struct alias *ap; /* if push was associated with an alias */ |
| 579 | #endif |
| 580 | char *string; /* remember the string since it may change */ |
| 581 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 582 | /* Delay freeing so we can stop nested aliases. */ |
| 583 | struct strpush *spfree; |
| 584 | |
Denys Vlasenko | 2124c0e | 2020-12-19 14:33:02 +0100 | [diff] [blame] | 585 | /* Remember last two characters for pungetc. */ |
| 586 | int lastc[2]; |
| 587 | |
| 588 | /* Number of outstanding calls to pungetc. */ |
| 589 | int unget; |
| 590 | }; |
| 591 | |
| 592 | /* |
| 593 | * The parsefile structure pointed to by the global variable parsefile |
| 594 | * contains information about the current file being read. |
| 595 | */ |
| 596 | struct parsefile { |
| 597 | struct parsefile *prev; /* preceding file on stack */ |
| 598 | int linno; /* current line */ |
| 599 | int pf_fd; /* file descriptor (or -1 if string) */ |
| 600 | int left_in_line; /* number of chars left in this line */ |
| 601 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 602 | char *next_to_pgetc; /* next char in buffer */ |
| 603 | char *buf; /* input buffer */ |
| 604 | struct strpush *strpush; /* for pushing strings at this level */ |
| 605 | struct strpush basestrpush; /* so pushing one is fast */ |
| 606 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 607 | /* Delay freeing so we can stop nested aliases. */ |
| 608 | struct strpush *spfree; |
| 609 | |
Denys Vlasenko | 2124c0e | 2020-12-19 14:33:02 +0100 | [diff] [blame] | 610 | /* Remember last two characters for pungetc. */ |
| 611 | int lastc[2]; |
| 612 | |
| 613 | /* Number of outstanding calls to pungetc. */ |
| 614 | int unget; |
| 615 | }; |
| 616 | |
| 617 | static struct parsefile basepf; /* top level input file */ |
| 618 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
| 619 | static char *commandname; /* currently executing command */ |
| 620 | |
| 621 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 622 | /* ============ Interrupts / exceptions */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 623 | |
| 624 | static void exitshell(void) NORETURN; |
| 625 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 626 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 627 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 628 | * 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] | 629 | * much more efficient and portable. (But hacking the kernel is so much |
| 630 | * more fun than worrying about efficiency and portability. :-)) |
| 631 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 632 | #if DEBUG_INTONOFF |
| 633 | # define INT_OFF do { \ |
| 634 | TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 635 | suppress_int++; \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 636 | barrier(); \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 637 | } while (0) |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 638 | #else |
| 639 | # define INT_OFF do { \ |
| 640 | suppress_int++; \ |
| 641 | barrier(); \ |
| 642 | } while (0) |
| 643 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 647 | * 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] | 648 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 649 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 650 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 651 | static void |
| 652 | raise_exception(int e) |
| 653 | { |
| 654 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 655 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 656 | abort(); |
| 657 | #endif |
| 658 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 659 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 660 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 661 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 662 | #if DEBUG |
| 663 | #define raise_exception(e) do { \ |
| 664 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 665 | raise_exception(e); \ |
| 666 | } while (0) |
| 667 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 668 | |
| 669 | /* |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 670 | * Called when a SIGINT is received. (If the user specifies |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 671 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
Denys Vlasenko | a277506 | 2022-01-16 23:54:46 +0100 | [diff] [blame] | 672 | * this routine is not called.) suppress_int is nonzero when interrupts |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 673 | * are held using the INT_OFF macro. (The test for iflag is just |
| 674 | * defensive programming.) |
| 675 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 676 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 677 | static void |
| 678 | raise_interrupt(void) |
| 679 | { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 680 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 681 | /* Signal is not automatically unmasked after it is raised, |
| 682 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 683 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 684 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 685 | |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 686 | if (!(rootshell && iflag)) { |
| 687 | /* Kill ourself with SIGINT */ |
| 688 | signal(SIGINT, SIG_DFL); |
| 689 | raise(SIGINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 690 | } |
Denys Vlasenko | 4d12e94 | 2016-10-01 16:03:11 +0200 | [diff] [blame] | 691 | /* bash: ^C even on empty command line sets $? */ |
| 692 | exitstatus = SIGINT + 128; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 693 | raise_exception(EXINT); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 694 | /* NOTREACHED */ |
| 695 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 696 | #if DEBUG |
| 697 | #define raise_interrupt() do { \ |
| 698 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 699 | raise_interrupt(); \ |
| 700 | } while (0) |
| 701 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 702 | |
Denys Vlasenko | a277506 | 2022-01-16 23:54:46 +0100 | [diff] [blame] | 703 | static IF_NOT_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 704 | 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 | a277506 | 2022-01-16 23:54:46 +0100 | [diff] [blame] | 707 | if (--suppress_int == 0 && pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 708 | raise_interrupt(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 709 | } |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 710 | #if DEBUG_INTONOFF |
| 711 | # define INT_ON do { \ |
| 712 | TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \ |
| 713 | int_on(); \ |
| 714 | } while (0) |
| 715 | #else |
| 716 | # define INT_ON int_on() |
| 717 | #endif |
Denys Vlasenko | a277506 | 2022-01-16 23:54:46 +0100 | [diff] [blame] | 718 | static IF_NOT_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 719 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 720 | { |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 721 | barrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 722 | suppress_int = 0; |
| 723 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 724 | raise_interrupt(); |
| 725 | } |
| 726 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 727 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 728 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 729 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 730 | #define RESTORE_INT(v) do { \ |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 731 | barrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 732 | suppress_int = (v); \ |
| 733 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 734 | raise_interrupt(); \ |
| 735 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 736 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 737 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 738 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 739 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 740 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 741 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 742 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 743 | INT_OFF; |
| 744 | fputs(p, file); |
| 745 | INT_ON; |
| 746 | } |
| 747 | |
| 748 | static void |
| 749 | flush_stdout_stderr(void) |
| 750 | { |
| 751 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 752 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 753 | INT_ON; |
| 754 | } |
| 755 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 756 | /* Was called outcslow(c,FILE*), but c was always '\n' */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 757 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 758 | newline_and_flush(FILE *dest) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 759 | { |
| 760 | INT_OFF; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 761 | putc('\n', dest); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 762 | fflush(dest); |
| 763 | INT_ON; |
| 764 | } |
| 765 | |
| 766 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 767 | static int |
| 768 | out1fmt(const char *fmt, ...) |
| 769 | { |
| 770 | va_list ap; |
| 771 | int r; |
| 772 | |
| 773 | INT_OFF; |
| 774 | va_start(ap, fmt); |
| 775 | r = vprintf(fmt, ap); |
| 776 | va_end(ap); |
| 777 | INT_ON; |
| 778 | return r; |
| 779 | } |
| 780 | |
| 781 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 782 | static int |
| 783 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 784 | { |
| 785 | va_list ap; |
| 786 | int ret; |
| 787 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 788 | INT_OFF; |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 789 | va_start(ap, fmt); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 790 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 791 | va_end(ap); |
| 792 | INT_ON; |
Denys Vlasenko | 3f7fb2c | 2020-02-16 18:06:20 +0100 | [diff] [blame] | 793 | return ret > (int)length ? length : ret; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static void |
| 797 | out1str(const char *p) |
| 798 | { |
| 799 | outstr(p, stdout); |
| 800 | } |
| 801 | |
| 802 | static void |
| 803 | out2str(const char *p) |
| 804 | { |
| 805 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 806 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 810 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 811 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 812 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 813 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 814 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 815 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 816 | #define CTLENDVAR ((unsigned char)'\203') |
| 817 | #define CTLBACKQ ((unsigned char)'\204') |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 818 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 819 | #define CTLENDARI ((unsigned char)'\207') |
| 820 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 821 | #define CTL_LAST CTLQUOTEMARK |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 822 | #if BASH_PROCESS_SUBST |
| 823 | # define CTLTOPROC ((unsigned char)'\211') |
| 824 | # define CTLFROMPROC ((unsigned char)'\212') |
| 825 | # undef CTL_LAST |
| 826 | # define CTL_LAST CTLFROMPROC |
| 827 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 828 | |
| 829 | /* variable substitution byte (follows CTLVAR) */ |
| 830 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 831 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 832 | |
| 833 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 834 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 835 | #define VSMINUS 0x2 /* ${var-text} */ |
| 836 | #define VSPLUS 0x3 /* ${var+text} */ |
| 837 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 838 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 839 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 840 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 841 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 842 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 843 | #define VSLENGTH 0xa /* ${#var} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 844 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 845 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 846 | #endif |
| 847 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 848 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 849 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 850 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 851 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 852 | static const char dolatstr[] ALIGN1 = { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 853 | CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0' |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 854 | }; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 855 | #define DOLATSTRLEN 6 |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 856 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 857 | #define NCMD 0 |
| 858 | #define NPIPE 1 |
| 859 | #define NREDIR 2 |
| 860 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 861 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 862 | #define NAND 5 |
| 863 | #define NOR 6 |
| 864 | #define NSEMI 7 |
| 865 | #define NIF 8 |
| 866 | #define NWHILE 9 |
| 867 | #define NUNTIL 10 |
| 868 | #define NFOR 11 |
| 869 | #define NCASE 12 |
| 870 | #define NCLIST 13 |
| 871 | #define NDEFUN 14 |
| 872 | #define NARG 15 |
| 873 | #define NTO 16 |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 874 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 875 | #define NTO2 17 |
| 876 | #endif |
| 877 | #define NCLOBBER 18 |
| 878 | #define NFROM 19 |
| 879 | #define NFROMTO 20 |
| 880 | #define NAPPEND 21 |
| 881 | #define NTOFD 22 |
| 882 | #define NFROMFD 23 |
| 883 | #define NHERE 24 |
| 884 | #define NXHERE 25 |
| 885 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 886 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 887 | |
| 888 | union node; |
| 889 | |
| 890 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 891 | smallint type; /* Nxxxx */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 892 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 893 | union node *assign; |
| 894 | union node *args; |
| 895 | union node *redirect; |
| 896 | }; |
| 897 | |
| 898 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 899 | smallint type; |
| 900 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 901 | struct nodelist *cmdlist; |
| 902 | }; |
| 903 | |
| 904 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 905 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 906 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 907 | union node *n; |
| 908 | union node *redirect; |
| 909 | }; |
| 910 | |
| 911 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 912 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 913 | union node *ch1; |
| 914 | union node *ch2; |
| 915 | }; |
| 916 | |
| 917 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 918 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 919 | union node *test; |
| 920 | union node *ifpart; |
| 921 | union node *elsepart; |
| 922 | }; |
| 923 | |
| 924 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 925 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 926 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 927 | union node *args; |
| 928 | union node *body; |
| 929 | char *var; |
| 930 | }; |
| 931 | |
| 932 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 933 | smallint type; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 934 | int linno; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 935 | union node *expr; |
| 936 | union node *cases; |
| 937 | }; |
| 938 | |
| 939 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 940 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 941 | union node *next; |
| 942 | union node *pattern; |
| 943 | union node *body; |
| 944 | }; |
| 945 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 946 | struct ndefun { |
| 947 | smallint type; |
| 948 | int linno; |
| 949 | char *text; |
| 950 | union node *body; |
| 951 | }; |
| 952 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 953 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 954 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 955 | union node *next; |
| 956 | char *text; |
| 957 | struct nodelist *backquote; |
| 958 | }; |
| 959 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 960 | /* nfile and ndup layout must match! |
| 961 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 962 | * that it is actually NTO2 (>&file), and change its type. |
| 963 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 964 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 965 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 966 | union node *next; |
| 967 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 968 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 969 | union node *fname; |
| 970 | char *expfname; |
| 971 | }; |
| 972 | |
| 973 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 974 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 975 | union node *next; |
| 976 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 977 | int dupfd; |
| 978 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 979 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 980 | }; |
| 981 | |
| 982 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 983 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 984 | union node *next; |
| 985 | int fd; |
| 986 | union node *doc; |
| 987 | }; |
| 988 | |
| 989 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 990 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 991 | union node *com; |
| 992 | }; |
| 993 | |
| 994 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 995 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 996 | struct ncmd ncmd; |
| 997 | struct npipe npipe; |
| 998 | struct nredir nredir; |
| 999 | struct nbinary nbinary; |
| 1000 | struct nif nif; |
| 1001 | struct nfor nfor; |
| 1002 | struct ncase ncase; |
| 1003 | struct nclist nclist; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1004 | struct ndefun ndefun; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1005 | struct narg narg; |
| 1006 | struct nfile nfile; |
| 1007 | struct ndup ndup; |
| 1008 | struct nhere nhere; |
| 1009 | struct nnot nnot; |
| 1010 | }; |
| 1011 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1012 | /* |
| 1013 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 1014 | * It must be distinct from NULL. |
| 1015 | */ |
| 1016 | #define NODE_EOF ((union node *) -1L) |
| 1017 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1018 | struct nodelist { |
| 1019 | struct nodelist *next; |
| 1020 | union node *n; |
| 1021 | }; |
| 1022 | |
| 1023 | struct funcnode { |
| 1024 | int count; |
| 1025 | union node n; |
| 1026 | }; |
| 1027 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1028 | /* |
| 1029 | * Free a parse tree. |
| 1030 | */ |
| 1031 | static void |
| 1032 | freefunc(struct funcnode *f) |
| 1033 | { |
| 1034 | if (f && --f->count < 0) |
| 1035 | free(f); |
| 1036 | } |
| 1037 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* ============ Debugging output */ |
| 1040 | |
| 1041 | #if DEBUG |
| 1042 | |
| 1043 | static FILE *tracefile; |
| 1044 | |
| 1045 | static void |
| 1046 | trace_printf(const char *fmt, ...) |
| 1047 | { |
| 1048 | va_list va; |
| 1049 | |
| 1050 | if (debug != 1) |
| 1051 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 1052 | if (DEBUG_TIME) |
| 1053 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 1054 | if (DEBUG_PID) |
| 1055 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 1056 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1057 | 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] | 1058 | va_start(va, fmt); |
| 1059 | vfprintf(tracefile, fmt, va); |
| 1060 | va_end(va); |
| 1061 | } |
| 1062 | |
| 1063 | static void |
| 1064 | trace_vprintf(const char *fmt, va_list va) |
| 1065 | { |
| 1066 | if (debug != 1) |
| 1067 | return; |
| 1068 | vfprintf(tracefile, fmt, va); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1069 | fprintf(tracefile, "\n"); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | static void |
| 1073 | trace_puts(const char *s) |
| 1074 | { |
| 1075 | if (debug != 1) |
| 1076 | return; |
| 1077 | fputs(s, tracefile); |
| 1078 | } |
| 1079 | |
| 1080 | static void |
| 1081 | trace_puts_quoted(char *s) |
| 1082 | { |
| 1083 | char *p; |
| 1084 | char c; |
| 1085 | |
| 1086 | if (debug != 1) |
| 1087 | return; |
| 1088 | putc('"', tracefile); |
| 1089 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1090 | switch ((unsigned char)*p) { |
| 1091 | case '\n': c = 'n'; goto backslash; |
| 1092 | case '\t': c = 't'; goto backslash; |
| 1093 | case '\r': c = 'r'; goto backslash; |
| 1094 | case '\"': c = '\"'; goto backslash; |
| 1095 | case '\\': c = '\\'; goto backslash; |
| 1096 | case CTLESC: c = 'e'; goto backslash; |
| 1097 | case CTLVAR: c = 'v'; goto backslash; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1098 | case CTLBACKQ: c = 'q'; goto backslash; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 1099 | #if BASH_PROCESS_SUBST |
| 1100 | case CTLTOPROC: c = 'p'; goto backslash; |
| 1101 | case CTLFROMPROC: c = 'P'; goto backslash; |
| 1102 | #endif |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1103 | backslash: |
| 1104 | putc('\\', tracefile); |
| 1105 | putc(c, tracefile); |
| 1106 | break; |
| 1107 | default: |
| 1108 | if (*p >= ' ' && *p <= '~') |
| 1109 | putc(*p, tracefile); |
| 1110 | else { |
| 1111 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1112 | putc((*p >> 6) & 03, tracefile); |
| 1113 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1114 | putc(*p & 07, tracefile); |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | putc('"', tracefile); |
| 1120 | } |
| 1121 | |
| 1122 | static void |
| 1123 | trace_puts_args(char **ap) |
| 1124 | { |
| 1125 | if (debug != 1) |
| 1126 | return; |
| 1127 | if (!*ap) |
| 1128 | return; |
| 1129 | while (1) { |
| 1130 | trace_puts_quoted(*ap); |
| 1131 | if (!*++ap) { |
| 1132 | putc('\n', tracefile); |
| 1133 | break; |
| 1134 | } |
| 1135 | putc(' ', tracefile); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | static void |
| 1140 | opentrace(void) |
| 1141 | { |
| 1142 | char s[100]; |
| 1143 | #ifdef O_APPEND |
| 1144 | int flags; |
| 1145 | #endif |
| 1146 | |
| 1147 | if (debug != 1) { |
| 1148 | if (tracefile) |
| 1149 | fflush(tracefile); |
| 1150 | /* leave open because libedit might be using it */ |
| 1151 | return; |
| 1152 | } |
| 1153 | strcpy(s, "./trace"); |
| 1154 | if (tracefile) { |
| 1155 | if (!freopen(s, "a", tracefile)) { |
| 1156 | fprintf(stderr, "Can't re-open %s\n", s); |
| 1157 | debug = 0; |
| 1158 | return; |
| 1159 | } |
| 1160 | } else { |
| 1161 | tracefile = fopen(s, "a"); |
| 1162 | if (tracefile == NULL) { |
| 1163 | fprintf(stderr, "Can't open %s\n", s); |
| 1164 | debug = 0; |
| 1165 | return; |
| 1166 | } |
| 1167 | } |
| 1168 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 1169 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1170 | if (flags >= 0) |
| 1171 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 1172 | #endif |
| 1173 | setlinebuf(tracefile); |
| 1174 | fputs("\nTracing started.\n", tracefile); |
| 1175 | } |
| 1176 | |
| 1177 | static void |
| 1178 | indent(int amount, char *pfx, FILE *fp) |
| 1179 | { |
| 1180 | int i; |
| 1181 | |
| 1182 | for (i = 0; i < amount; i++) { |
| 1183 | if (pfx && i == amount - 1) |
| 1184 | fputs(pfx, fp); |
| 1185 | putc('\t', fp); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /* little circular references here... */ |
| 1190 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 1191 | |
| 1192 | static void |
| 1193 | sharg(union node *arg, FILE *fp) |
| 1194 | { |
| 1195 | char *p; |
| 1196 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1197 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (arg->type != NARG) { |
| 1200 | out1fmt("<node type %d>\n", arg->type); |
| 1201 | abort(); |
| 1202 | } |
| 1203 | bqlist = arg->narg.backquote; |
| 1204 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1205 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1206 | case CTLESC: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1207 | p++; |
| 1208 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | case CTLVAR: |
| 1211 | putc('$', fp); |
| 1212 | putc('{', fp); |
| 1213 | subtype = *++p; |
| 1214 | if (subtype == VSLENGTH) |
| 1215 | putc('#', fp); |
| 1216 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 1217 | while (*p != '=') { |
| 1218 | putc(*p, fp); |
| 1219 | p++; |
| 1220 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (subtype & VSNUL) |
| 1223 | putc(':', fp); |
| 1224 | |
| 1225 | switch (subtype & VSTYPE) { |
| 1226 | case VSNORMAL: |
| 1227 | putc('}', fp); |
| 1228 | break; |
| 1229 | case VSMINUS: |
| 1230 | putc('-', fp); |
| 1231 | break; |
| 1232 | case VSPLUS: |
| 1233 | putc('+', fp); |
| 1234 | break; |
| 1235 | case VSQUESTION: |
| 1236 | putc('?', fp); |
| 1237 | break; |
| 1238 | case VSASSIGN: |
| 1239 | putc('=', fp); |
| 1240 | break; |
| 1241 | case VSTRIMLEFT: |
| 1242 | putc('#', fp); |
| 1243 | break; |
| 1244 | case VSTRIMLEFTMAX: |
| 1245 | putc('#', fp); |
| 1246 | putc('#', fp); |
| 1247 | break; |
| 1248 | case VSTRIMRIGHT: |
| 1249 | putc('%', fp); |
| 1250 | break; |
| 1251 | case VSTRIMRIGHTMAX: |
| 1252 | putc('%', fp); |
| 1253 | putc('%', fp); |
| 1254 | break; |
| 1255 | case VSLENGTH: |
| 1256 | break; |
| 1257 | default: |
| 1258 | out1fmt("<subtype %d>", subtype); |
| 1259 | } |
| 1260 | break; |
| 1261 | case CTLENDVAR: |
| 1262 | putc('}', fp); |
| 1263 | break; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 1264 | #if BASH_PROCESS_SUBST |
| 1265 | case CTLTOPROC: |
| 1266 | putc('>', fp); |
| 1267 | goto backq; |
| 1268 | case CTLFROMPROC: |
| 1269 | putc('<', fp); |
| 1270 | goto backq; |
| 1271 | #endif |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1272 | case CTLBACKQ: |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1273 | putc('$', fp); |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 1274 | IF_BASH_PROCESS_SUBST(backq:) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1275 | putc('(', fp); |
| 1276 | shtree(bqlist->n, -1, NULL, fp); |
| 1277 | putc(')', fp); |
| 1278 | break; |
| 1279 | default: |
| 1280 | putc(*p, fp); |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1286 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1287 | shcmd(union node *cmd, FILE *fp) |
| 1288 | { |
| 1289 | union node *np; |
| 1290 | int first; |
| 1291 | const char *s; |
| 1292 | int dftfd; |
| 1293 | |
| 1294 | first = 1; |
| 1295 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1296 | if (!first) |
| 1297 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1298 | sharg(np, fp); |
| 1299 | first = 0; |
| 1300 | } |
| 1301 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1302 | if (!first) |
| 1303 | putc(' ', fp); |
| 1304 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1305 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1306 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1307 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1308 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 1309 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1310 | case NTO2: |
| 1311 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1312 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1313 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1314 | case NFROMFD: s = "<&"; break; |
| 1315 | case NFROMTO: s = "<>"; break; |
| 1316 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1317 | } |
| 1318 | if (np->nfile.fd != dftfd) |
| 1319 | fprintf(fp, "%d", np->nfile.fd); |
| 1320 | fputs(s, fp); |
| 1321 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1322 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1323 | } else { |
| 1324 | sharg(np->nfile.fname, fp); |
| 1325 | } |
| 1326 | first = 0; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | static void |
| 1331 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1332 | { |
| 1333 | struct nodelist *lp; |
| 1334 | const char *s; |
| 1335 | |
| 1336 | if (n == NULL) |
| 1337 | return; |
| 1338 | |
| 1339 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1340 | |
| 1341 | if (n == NODE_EOF) { |
| 1342 | fputs("<EOF>", fp); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1346 | switch (n->type) { |
| 1347 | case NSEMI: |
| 1348 | s = "; "; |
| 1349 | goto binop; |
| 1350 | case NAND: |
| 1351 | s = " && "; |
| 1352 | goto binop; |
| 1353 | case NOR: |
| 1354 | s = " || "; |
| 1355 | binop: |
| 1356 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1357 | /* if (ind < 0) */ |
| 1358 | fputs(s, fp); |
| 1359 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1360 | break; |
| 1361 | case NCMD: |
| 1362 | shcmd(n, fp); |
| 1363 | if (ind >= 0) |
| 1364 | putc('\n', fp); |
| 1365 | break; |
| 1366 | case NPIPE: |
| 1367 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1368 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1369 | if (lp->next) |
| 1370 | fputs(" | ", fp); |
| 1371 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1372 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1373 | fputs(" &", fp); |
| 1374 | if (ind >= 0) |
| 1375 | putc('\n', fp); |
| 1376 | break; |
| 1377 | default: |
| 1378 | fprintf(fp, "<node type %d>", n->type); |
| 1379 | if (ind >= 0) |
| 1380 | putc('\n', fp); |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | static void |
| 1386 | showtree(union node *n) |
| 1387 | { |
| 1388 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1389 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1392 | #endif /* DEBUG */ |
| 1393 | |
| 1394 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1395 | /* ============ Message printing */ |
| 1396 | |
| 1397 | static void |
| 1398 | ash_vmsg(const char *msg, va_list ap) |
| 1399 | { |
| 1400 | fprintf(stderr, "%s: ", arg0); |
| 1401 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1402 | if (strcmp(arg0, commandname)) |
| 1403 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1404 | if (!iflag || g_parsefile->pf_fd > 0) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1405 | fprintf(stderr, "line %d: ", errlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1406 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1407 | vfprintf(stderr, msg, ap); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 1408 | newline_and_flush(stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1409 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1410 | |
| 1411 | /* |
| 1412 | * Exverror is called to raise the error exception. If the second argument |
| 1413 | * is not NULL then error prints an error message using printf style |
| 1414 | * formatting. It then raises the error exception. |
| 1415 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1416 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1417 | static void |
| 1418 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1419 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1420 | #if DEBUG |
| 1421 | if (msg) { |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1422 | TRACE(("ash_vmsg_and_raise(%d):", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1423 | TRACEV((msg, ap)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1424 | } else |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 1425 | TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond)); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1426 | if (msg) |
| 1427 | #endif |
| 1428 | ash_vmsg(msg, ap); |
| 1429 | |
| 1430 | flush_stdout_stderr(); |
| 1431 | raise_exception(cond); |
| 1432 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1433 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1434 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1435 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1436 | static void |
| 1437 | ash_msg_and_raise_error(const char *msg, ...) |
| 1438 | { |
| 1439 | va_list ap; |
| 1440 | |
Ron Yorston | ea7d2f6 | 2017-01-03 11:18:23 +0100 | [diff] [blame] | 1441 | exitstatus = 2; |
| 1442 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1443 | va_start(ap, msg); |
| 1444 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1445 | /* NOTREACHED */ |
| 1446 | va_end(ap); |
| 1447 | } |
| 1448 | |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1449 | /* |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 1450 | * 'fmt' must be a string literal. |
| 1451 | */ |
Denys Vlasenko | 6f97b30 | 2017-09-29 18:17:25 +0200 | [diff] [blame] | 1452 | #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] | 1453 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1454 | static void raise_error_syntax(const char *) NORETURN; |
| 1455 | static void |
| 1456 | raise_error_syntax(const char *msg) |
| 1457 | { |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 1458 | errlinno = g_parsefile->linno; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1459 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1460 | /* NOTREACHED */ |
| 1461 | } |
| 1462 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1463 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1464 | static void |
| 1465 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1466 | { |
| 1467 | va_list ap; |
| 1468 | |
| 1469 | va_start(ap, msg); |
| 1470 | ash_vmsg_and_raise(cond, msg, ap); |
| 1471 | /* NOTREACHED */ |
| 1472 | va_end(ap); |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * error/warning routines for external builtins |
| 1477 | */ |
| 1478 | static void |
| 1479 | ash_msg(const char *fmt, ...) |
| 1480 | { |
| 1481 | va_list ap; |
| 1482 | |
| 1483 | va_start(ap, fmt); |
| 1484 | ash_vmsg(fmt, ap); |
| 1485 | va_end(ap); |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Return a string describing an error. The returned string may be a |
| 1490 | * pointer to a static buffer that will be overwritten on the next call. |
| 1491 | * Action describes the operation that got the error. |
| 1492 | */ |
| 1493 | static const char * |
| 1494 | errmsg(int e, const char *em) |
| 1495 | { |
| 1496 | if (e == ENOENT || e == ENOTDIR) { |
| 1497 | return em; |
| 1498 | } |
| 1499 | return strerror(e); |
| 1500 | } |
| 1501 | |
| 1502 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1503 | /* ============ Memory allocation */ |
| 1504 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1505 | #if 0 |
| 1506 | /* I consider these wrappers nearly useless: |
| 1507 | * ok, they return you to nearest exception handler, but |
| 1508 | * how much memory do you leak in the process, making |
| 1509 | * memory starvation worse? |
| 1510 | */ |
| 1511 | static void * |
| 1512 | ckrealloc(void * p, size_t nbytes) |
| 1513 | { |
| 1514 | p = realloc(p, nbytes); |
| 1515 | if (!p) |
| 1516 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1517 | return p; |
| 1518 | } |
| 1519 | |
| 1520 | static void * |
| 1521 | ckmalloc(size_t nbytes) |
| 1522 | { |
| 1523 | return ckrealloc(NULL, nbytes); |
| 1524 | } |
| 1525 | |
| 1526 | static void * |
| 1527 | ckzalloc(size_t nbytes) |
| 1528 | { |
| 1529 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1530 | } |
| 1531 | |
| 1532 | static char * |
| 1533 | ckstrdup(const char *s) |
| 1534 | { |
| 1535 | char *p = strdup(s); |
| 1536 | if (!p) |
| 1537 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1538 | return p; |
| 1539 | } |
| 1540 | #else |
| 1541 | /* Using bbox equivalents. They exit if out of memory */ |
| 1542 | # define ckrealloc xrealloc |
| 1543 | # define ckmalloc xmalloc |
| 1544 | # define ckzalloc xzalloc |
| 1545 | # define ckstrdup xstrdup |
| 1546 | #endif |
| 1547 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1548 | /* |
| 1549 | * It appears that grabstackstr() will barf with such alignments |
| 1550 | * because stalloc() will return a string allocated in a new stackblock. |
| 1551 | */ |
| 1552 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1553 | enum { |
| 1554 | /* Most machines require the value returned from malloc to be aligned |
| 1555 | * in some way. The following macro will get this right |
| 1556 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1557 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1558 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1559 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1560 | }; |
| 1561 | |
| 1562 | struct stack_block { |
| 1563 | struct stack_block *prev; |
| 1564 | char space[MINSIZE]; |
| 1565 | }; |
| 1566 | |
| 1567 | struct stackmark { |
| 1568 | struct stack_block *stackp; |
| 1569 | char *stacknxt; |
| 1570 | size_t stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1571 | }; |
| 1572 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1573 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1574 | struct globals_memstack { |
| 1575 | struct stack_block *g_stackp; // = &stackbase; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1576 | char *g_stacknxt; // = stackbase.space; |
| 1577 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1578 | size_t g_stacknleft; // = MINSIZE; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1579 | struct stack_block stackbase; |
| 1580 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 1581 | extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1582 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1583 | #define g_stackp (G_memstack.g_stackp ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1584 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1585 | #define sstrend (G_memstack.sstrend ) |
| 1586 | #define g_stacknleft (G_memstack.g_stacknleft) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1587 | #define stackbase (G_memstack.stackbase ) |
| 1588 | #define INIT_G_memstack() do { \ |
YU Jincheng | 5156b24 | 2021-10-10 02:19:51 +0800 | [diff] [blame] | 1589 | XZALLOC_CONST_PTR(&ash_ptr_to_globals_memstack, sizeof(G_memstack)); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1590 | g_stackp = &stackbase; \ |
| 1591 | g_stacknxt = stackbase.space; \ |
| 1592 | g_stacknleft = MINSIZE; \ |
| 1593 | sstrend = stackbase.space + MINSIZE; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1594 | } while (0) |
| 1595 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1596 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1597 | #define stackblock() ((void *)g_stacknxt) |
| 1598 | #define stackblocksize() g_stacknleft |
| 1599 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1600 | /* |
| 1601 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1602 | * to make this more efficient, and also to avoid all sorts of exception |
| 1603 | * handling code to handle interrupts in the middle of a parse. |
| 1604 | * |
| 1605 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1606 | * well. |
| 1607 | */ |
| 1608 | static void * |
| 1609 | stalloc(size_t nbytes) |
| 1610 | { |
| 1611 | char *p; |
| 1612 | size_t aligned; |
| 1613 | |
| 1614 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1615 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1616 | size_t len; |
| 1617 | size_t blocksize; |
| 1618 | struct stack_block *sp; |
| 1619 | |
| 1620 | blocksize = aligned; |
| 1621 | if (blocksize < MINSIZE) |
| 1622 | blocksize = MINSIZE; |
| 1623 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1624 | if (len < blocksize) |
| 1625 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1626 | INT_OFF; |
| 1627 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1628 | sp->prev = g_stackp; |
| 1629 | g_stacknxt = sp->space; |
| 1630 | g_stacknleft = blocksize; |
| 1631 | sstrend = g_stacknxt + blocksize; |
| 1632 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1633 | INT_ON; |
| 1634 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1635 | p = g_stacknxt; |
| 1636 | g_stacknxt += aligned; |
| 1637 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1638 | return p; |
| 1639 | } |
| 1640 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1641 | static void * |
| 1642 | stzalloc(size_t nbytes) |
| 1643 | { |
| 1644 | return memset(stalloc(nbytes), 0, nbytes); |
| 1645 | } |
| 1646 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1647 | static void |
| 1648 | stunalloc(void *p) |
| 1649 | { |
| 1650 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1651 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1652 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1653 | abort(); |
| 1654 | } |
| 1655 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1656 | g_stacknleft += g_stacknxt - (char *)p; |
| 1657 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1660 | /* |
| 1661 | * Like strdup but works with the ash stack. |
| 1662 | */ |
| 1663 | static char * |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 1664 | sstrdup(const char *p) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1665 | { |
| 1666 | size_t len = strlen(p) + 1; |
| 1667 | return memcpy(stalloc(len), p, len); |
| 1668 | } |
| 1669 | |
Denys Vlasenko | 03c36e0 | 2018-01-10 15:18:35 +0100 | [diff] [blame] | 1670 | static ALWAYS_INLINE void |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1671 | grabstackblock(size_t len) |
| 1672 | { |
Denys Vlasenko | a318bba | 2016-10-26 18:26:27 +0200 | [diff] [blame] | 1673 | stalloc(len); |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | static void |
| 1677 | pushstackmark(struct stackmark *mark, size_t len) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1678 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1679 | mark->stackp = g_stackp; |
| 1680 | mark->stacknxt = g_stacknxt; |
| 1681 | mark->stacknleft = g_stacknleft; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1682 | grabstackblock(len); |
| 1683 | } |
| 1684 | |
| 1685 | static void |
| 1686 | setstackmark(struct stackmark *mark) |
| 1687 | { |
| 1688 | pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | static void |
| 1692 | popstackmark(struct stackmark *mark) |
| 1693 | { |
| 1694 | struct stack_block *sp; |
| 1695 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1696 | if (!mark->stackp) |
| 1697 | return; |
| 1698 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1699 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1700 | while (g_stackp != mark->stackp) { |
| 1701 | sp = g_stackp; |
| 1702 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1703 | free(sp); |
| 1704 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1705 | g_stacknxt = mark->stacknxt; |
| 1706 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1707 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1708 | INT_ON; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * When the parser reads in a string, it wants to stick the string on the |
| 1713 | * stack and only adjust the stack pointer when it knows how big the |
| 1714 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1715 | * of space on top of the stack and stackblocklen returns the length of |
| 1716 | * this block. Growstackblock will grow this space by at least one byte, |
| 1717 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1718 | * part of the block that has been used. |
| 1719 | */ |
| 1720 | static void |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1721 | growstackblock(size_t min) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1722 | { |
| 1723 | size_t newlen; |
| 1724 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1725 | newlen = g_stacknleft * 2; |
| 1726 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1727 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1728 | min = SHELL_ALIGN(min | 128); |
| 1729 | if (newlen < min) |
| 1730 | newlen += min; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1731 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1732 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1733 | struct stack_block *sp; |
| 1734 | struct stack_block *prevstackp; |
| 1735 | size_t grosslen; |
| 1736 | |
| 1737 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1738 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1739 | prevstackp = sp->prev; |
| 1740 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1741 | sp = ckrealloc(sp, grosslen); |
| 1742 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1743 | g_stackp = sp; |
| 1744 | g_stacknxt = sp->space; |
| 1745 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1746 | sstrend = sp->space + newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1747 | INT_ON; |
| 1748 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1749 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1750 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1751 | char *p = stalloc(newlen); |
| 1752 | |
| 1753 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1754 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1755 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1759 | /* |
| 1760 | * The following routines are somewhat easier to use than the above. |
| 1761 | * The user declares a variable of type STACKSTR, which may be declared |
| 1762 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1763 | * the user uses the macro STPUTC to add characters to the string. In |
| 1764 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1765 | * grown as necessary. When the user is done, she can just leave the |
| 1766 | * string there and refer to it using stackblock(). Or she can allocate |
| 1767 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1768 | * someone else to use the stack temporarily and then continue to grow |
| 1769 | * the string, the user should use grabstack to allocate the space, and |
| 1770 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1771 | * |
| 1772 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1773 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1774 | * is space for at least one character. |
| 1775 | */ |
| 1776 | static void * |
| 1777 | growstackstr(void) |
| 1778 | { |
| 1779 | size_t len = stackblocksize(); |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1780 | growstackblock(0); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1781 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1784 | static char * |
| 1785 | growstackto(size_t len) |
| 1786 | { |
Denys Vlasenko | da2e46d | 2020-02-21 15:25:37 +0100 | [diff] [blame] | 1787 | if (stackblocksize() < len) |
| 1788 | growstackblock(len); |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1789 | return stackblock(); |
| 1790 | } |
| 1791 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1792 | /* |
| 1793 | * Called from CHECKSTRSPACE. |
| 1794 | */ |
| 1795 | static char * |
| 1796 | makestrspace(size_t newlen, char *p) |
| 1797 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1798 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1799 | |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 1800 | return growstackto(len + newlen) + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | static char * |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1804 | stnputs(const char *s, size_t n, char *p) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1805 | { |
| 1806 | p = makestrspace(n, p); |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1807 | p = (char *)mempcpy(p, s, n); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1808 | return p; |
| 1809 | } |
| 1810 | |
| 1811 | static char * |
| 1812 | stack_putstr(const char *s, char *p) |
| 1813 | { |
Denys Vlasenko | 538ee41 | 2020-02-22 19:11:41 +0100 | [diff] [blame] | 1814 | return stnputs(s, strlen(s), p); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1817 | static char * |
| 1818 | _STPUTC(int c, char *p) |
| 1819 | { |
| 1820 | if (p == sstrend) |
| 1821 | p = growstackstr(); |
| 1822 | *p++ = c; |
| 1823 | return p; |
| 1824 | } |
| 1825 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1826 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1827 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1828 | #define CHECKSTRSPACE(n, p) do { \ |
| 1829 | char *q = (p); \ |
| 1830 | size_t l = (n); \ |
| 1831 | size_t m = sstrend - q; \ |
| 1832 | if (l > m) \ |
| 1833 | (p) = makestrspace(l, q); \ |
| 1834 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1835 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1836 | #define STACKSTRNUL(p) do { \ |
| 1837 | if ((p) == sstrend) \ |
| 1838 | (p) = growstackstr(); \ |
| 1839 | *(p) = '\0'; \ |
| 1840 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1841 | #define STUNPUTC(p) (--(p)) |
| 1842 | #define STTOPC(p) ((p)[-1]) |
| 1843 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1844 | |
| 1845 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1846 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1847 | #define stackstrend() ((void *)sstrend) |
| 1848 | |
| 1849 | |
| 1850 | /* ============ String helpers */ |
| 1851 | |
| 1852 | /* |
| 1853 | * prefix -- see if pfx is a prefix of string. |
| 1854 | */ |
| 1855 | static char * |
| 1856 | prefix(const char *string, const char *pfx) |
| 1857 | { |
| 1858 | while (*pfx) { |
| 1859 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1860 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1861 | } |
| 1862 | return (char *) string; |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Check for a valid number. This should be elsewhere. |
| 1867 | */ |
| 1868 | static int |
| 1869 | is_number(const char *p) |
| 1870 | { |
| 1871 | do { |
| 1872 | if (!isdigit(*p)) |
| 1873 | return 0; |
| 1874 | } while (*++p != '\0'); |
| 1875 | return 1; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Convert a string of digits to an integer, printing an error message on |
| 1880 | * failure. |
| 1881 | */ |
| 1882 | static int |
| 1883 | number(const char *s) |
| 1884 | { |
| 1885 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1886 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1887 | return atoi(s); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1891 | * Produce a single quoted string suitable as input to the shell. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1892 | * The return string is allocated on the stack. |
| 1893 | */ |
| 1894 | static char * |
| 1895 | single_quote(const char *s) |
| 1896 | { |
| 1897 | char *p; |
| 1898 | |
| 1899 | STARTSTACKSTR(p); |
| 1900 | |
| 1901 | do { |
| 1902 | char *q; |
| 1903 | size_t len; |
| 1904 | |
| 1905 | len = strchrnul(s, '\'') - s; |
| 1906 | |
| 1907 | q = p = makestrspace(len + 3, p); |
| 1908 | |
| 1909 | *q++ = '\''; |
Denys Vlasenko | 94af83e | 2017-07-23 21:55:40 +0200 | [diff] [blame] | 1910 | q = (char *)mempcpy(q, s, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1911 | *q++ = '\''; |
| 1912 | s += len; |
| 1913 | |
| 1914 | STADJUST(q - p, p); |
| 1915 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1916 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1917 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1918 | len = 0; |
| 1919 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1920 | |
| 1921 | q = p = makestrspace(len + 3, p); |
| 1922 | |
| 1923 | *q++ = '"'; |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 1924 | q = (char *)mempcpy(q, s - len, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1925 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1926 | |
| 1927 | STADJUST(q - p, p); |
| 1928 | } while (*s); |
| 1929 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1930 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1931 | |
| 1932 | return stackblock(); |
| 1933 | } |
| 1934 | |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1935 | /* |
| 1936 | * Produce a possibly single quoted string suitable as input to the shell. |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 1937 | * If quoting was done, the return string is allocated on the stack, |
| 1938 | * otherwise a pointer to the original string is returned. |
| 1939 | */ |
| 1940 | static const char * |
| 1941 | maybe_single_quote(const char *s) |
| 1942 | { |
| 1943 | const char *p = s; |
| 1944 | |
| 1945 | while (*p) { |
| 1946 | /* Assuming ACSII */ |
| 1947 | /* quote ctrl_chars space !"#$%&'()* */ |
| 1948 | if (*p < '+') |
| 1949 | goto need_quoting; |
| 1950 | /* quote ;<=>? */ |
| 1951 | if (*p >= ';' && *p <= '?') |
| 1952 | goto need_quoting; |
| 1953 | /* quote `[\ */ |
| 1954 | if (*p == '`') |
| 1955 | goto need_quoting; |
| 1956 | if (*p == '[') |
| 1957 | goto need_quoting; |
| 1958 | if (*p == '\\') |
| 1959 | goto need_quoting; |
| 1960 | /* quote {|}~ DEL and high bytes */ |
| 1961 | if (*p > 'z') |
| 1962 | goto need_quoting; |
| 1963 | /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */ |
| 1964 | /* TODO: maybe avoid quoting % */ |
| 1965 | p++; |
| 1966 | } |
| 1967 | return s; |
| 1968 | |
| 1969 | need_quoting: |
| 1970 | return single_quote(s); |
| 1971 | } |
| 1972 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1973 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1974 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1975 | |
| 1976 | static char **argptr; /* argument list for builtin commands */ |
| 1977 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1978 | static char *optptr; /* used by nextopt */ |
| 1979 | |
| 1980 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1981 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1982 | * The library getopt must have the BSD extension static variable |
| 1983 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1984 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1985 | * Standard option processing (a la getopt) for builtin routines. |
| 1986 | * The only argument that is passed to nextopt is the option string; |
| 1987 | * the other arguments are unnecessary. It returns the character, |
| 1988 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1989 | */ |
| 1990 | static int |
| 1991 | nextopt(const char *optstring) |
| 1992 | { |
| 1993 | char *p; |
| 1994 | const char *q; |
| 1995 | char c; |
| 1996 | |
| 1997 | p = optptr; |
| 1998 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1999 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2000 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 2001 | if (p == NULL) |
| 2002 | return '\0'; |
| 2003 | if (*p != '-') |
| 2004 | return '\0'; |
| 2005 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2006 | return '\0'; |
| 2007 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 2008 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2009 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 2010 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2011 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 2012 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2013 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 2014 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2015 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 2016 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2017 | if (*++q == ':') |
| 2018 | q++; |
| 2019 | } |
| 2020 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 2021 | if (*p == '\0') { |
| 2022 | p = *argptr++; |
| 2023 | if (p == NULL) |
| 2024 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 2025 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2026 | optionarg = p; |
| 2027 | p = NULL; |
| 2028 | } |
| 2029 | optptr = p; |
| 2030 | return c; |
| 2031 | } |
| 2032 | |
| 2033 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2034 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2035 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2036 | struct shparam { |
| 2037 | int nparam; /* # of positional parameters (without $0) */ |
| 2038 | #if ENABLE_ASH_GETOPTS |
| 2039 | int optind; /* next parameter to be processed by getopts */ |
| 2040 | int optoff; /* used by getopts */ |
| 2041 | #endif |
| 2042 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 2043 | char **p; /* parameter list */ |
| 2044 | }; |
| 2045 | |
| 2046 | /* |
| 2047 | * Free the list of positional parameters. |
| 2048 | */ |
| 2049 | static void |
| 2050 | freeparam(volatile struct shparam *param) |
| 2051 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2052 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 2053 | char **ap, **ap1; |
| 2054 | ap = ap1 = param->p; |
| 2055 | while (*ap) |
| 2056 | free(*ap++); |
| 2057 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2062 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2063 | #endif |
| 2064 | |
| 2065 | struct var { |
| 2066 | struct var *next; /* next entry in hash list */ |
| 2067 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2068 | const char *var_text; /* name=value */ |
| 2069 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2070 | /* the variable gets set/unset */ |
| 2071 | }; |
| 2072 | |
| 2073 | struct localvar { |
| 2074 | struct localvar *next; /* next local variable in list */ |
| 2075 | struct var *vp; /* the variable that was made local */ |
| 2076 | int flags; /* saved flags */ |
| 2077 | const char *text; /* saved text */ |
| 2078 | }; |
| 2079 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2080 | /* flags */ |
| 2081 | #define VEXPORT 0x01 /* variable is exported */ |
| 2082 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 2083 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 2084 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 2085 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 2086 | #define VUNSET 0x20 /* the variable is not set */ |
| 2087 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 2088 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 2089 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2090 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2091 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 2092 | #else |
| 2093 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2094 | #endif |
| 2095 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2096 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2097 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2098 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2099 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2100 | change_lc_all(const char *value) |
| 2101 | { |
| 2102 | if (value && *value != '\0') |
| 2103 | setlocale(LC_ALL, value); |
| 2104 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 2105 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 2106 | change_lc_ctype(const char *value) |
| 2107 | { |
| 2108 | if (value && *value != '\0') |
| 2109 | setlocale(LC_CTYPE, value); |
| 2110 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 2111 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2112 | #if ENABLE_ASH_MAIL |
| 2113 | static void chkmail(void); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 2114 | static void changemail(const char *var_value) FAST_FUNC; |
| 2115 | #else |
| 2116 | # define chkmail() ((void)0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2117 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2118 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2119 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2120 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2121 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2122 | #if BASH_EPOCH_VARS |
| 2123 | static void change_seconds(const char *) FAST_FUNC; |
| 2124 | static void change_realtime(const char *) FAST_FUNC; |
| 2125 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2126 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2127 | static const struct { |
| 2128 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2129 | const char *var_text; |
| 2130 | void (*var_func)(const char *) FAST_FUNC; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 2131 | } varinit_data[] ALIGN_PTR = { |
Denys Vlasenko | 566a313 | 2012-07-07 21:40:35 +0200 | [diff] [blame] | 2132 | /* |
| 2133 | * Note: VEXPORT would not work correctly here for NOFORK applets: |
| 2134 | * some environment strings may be constant. |
| 2135 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2136 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2137 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2138 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 2139 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2140 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2141 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 2142 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 2143 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 2144 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2145 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 2146 | { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2147 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2148 | { VSTRFIXED|VTEXTFIXED , NULL /* inited to linenovar */, NULL }, |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2149 | { VSTRFIXED|VTEXTFIXED , NULL /* inited to funcnamevar */, NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2150 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2151 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2152 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2153 | #if BASH_EPOCH_VARS |
| 2154 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHSECONDS", change_seconds }, |
| 2155 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHREALTIME", change_realtime }, |
| 2156 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2157 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2158 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 2159 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2160 | #endif |
| 2161 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2162 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2163 | #endif |
| 2164 | }; |
| 2165 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2166 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2167 | |
| 2168 | struct globals_var { |
| 2169 | struct shparam shellparam; /* $@ current positional parameters */ |
| 2170 | struct redirtab *redirlist; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 2171 | int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2172 | struct var *vartab[VTABSIZE]; |
| 2173 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2174 | int lineno; |
| 2175 | char linenovar[sizeof("LINENO=") + sizeof(int)*3]; |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2176 | char funcnamevar[sizeof("FUNCNAME=") + 64]; |
| 2177 | char *funcname; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 2178 | unsigned trap_depth; |
| 2179 | bool in_trap_ERR; /* ERR cannot recurse, no need to be a counter */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2180 | }; |
Denys Vlasenko | 6f9442f | 2018-01-28 20:41:23 +0100 | [diff] [blame] | 2181 | extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 2182 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2183 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 2184 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2185 | #define preverrout_fd (G_var.preverrout_fd) |
| 2186 | #define vartab (G_var.vartab ) |
| 2187 | #define varinit (G_var.varinit ) |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2188 | #define lineno (G_var.lineno ) |
| 2189 | #define linenovar (G_var.linenovar ) |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2190 | #define funcnamevar (G_var.funcnamevar ) |
| 2191 | #define funcname (G_var.funcname ) |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 2192 | #define trap_depth (G_var.trap_depth ) |
| 2193 | #define in_trap_ERR (G_var.in_trap_ERR ) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2194 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2195 | #if ENABLE_ASH_MAIL |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2196 | # define vmail varinit[1] |
| 2197 | # define vmpath varinit[2] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2198 | #endif |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2199 | #define VAR_OFFSET1 (ENABLE_ASH_MAIL*2) |
| 2200 | #define vpath varinit[VAR_OFFSET1 + 1] |
| 2201 | #define vps1 varinit[VAR_OFFSET1 + 2] |
| 2202 | #define vps2 varinit[VAR_OFFSET1 + 3] |
| 2203 | #define vps4 varinit[VAR_OFFSET1 + 4] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2204 | #if ENABLE_ASH_GETOPTS |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2205 | # define voptind varinit[VAR_OFFSET1 + 5] |
| 2206 | #endif |
| 2207 | #define VAR_OFFSET2 (VAR_OFFSET1 + ENABLE_ASH_GETOPTS) |
| 2208 | #define vlineno varinit[VAR_OFFSET2 + 5] |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2209 | #define vfuncname varinit[VAR_OFFSET2 + 6] |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2210 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2211 | # define vrandom varinit[VAR_OFFSET2 + 7] |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2212 | #endif |
| 2213 | #define VAR_OFFSET3 (VAR_OFFSET2 + ENABLE_ASH_RANDOM_SUPPORT) |
| 2214 | #if BASH_EPOCH_VARS |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2215 | # define vepochs varinit[VAR_OFFSET3 + 7] |
| 2216 | # define vepochr varinit[VAR_OFFSET3 + 8] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2217 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2218 | #define INIT_G_var() do { \ |
| 2219 | unsigned i; \ |
YU Jincheng | 5156b24 | 2021-10-10 02:19:51 +0800 | [diff] [blame] | 2220 | XZALLOC_CONST_PTR(&ash_ptr_to_globals_var, sizeof(G_var)); \ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2221 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
| 2222 | varinit[i].flags = varinit_data[i].flags; \ |
| 2223 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 2224 | varinit[i].var_func = varinit_data[i].var_func; \ |
| 2225 | } \ |
| 2226 | strcpy(linenovar, "LINENO="); \ |
| 2227 | vlineno.var_text = linenovar; \ |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2228 | strcpy(funcnamevar, "FUNCNAME="); \ |
| 2229 | vfuncname.var_text = funcnamevar; \ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2230 | } while (0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2231 | |
| 2232 | /* |
| 2233 | * The following macros access the values of the above variables. |
| 2234 | * They have to skip over the name. They return the null string |
| 2235 | * for unset variables. |
| 2236 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2237 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2238 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2239 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2240 | # define mailval() (vmail.var_text + 5) |
| 2241 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2242 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 2243 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2244 | #define pathval() (vpath.var_text + 5) |
| 2245 | #define ps1val() (vps1.var_text + 4) |
| 2246 | #define ps2val() (vps2.var_text + 4) |
| 2247 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2248 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2249 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2250 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2251 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2252 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2253 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2254 | getoptsreset(const char *value) |
| 2255 | { |
Denys Vlasenko | 4628945 | 2017-08-11 00:59:36 +0200 | [diff] [blame] | 2256 | shellparam.optind = 1; |
| 2257 | if (is_number(value)) |
| 2258 | shellparam.optind = number(value) ?: 1; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2259 | shellparam.optoff = -1; |
| 2260 | } |
| 2261 | #endif |
| 2262 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2263 | /* |
| 2264 | * Compares two strings up to the first = or '\0'. The first |
| 2265 | * string must be terminated by '='; the second may be terminated by |
| 2266 | * either '=' or '\0'. |
| 2267 | */ |
| 2268 | static int |
| 2269 | varcmp(const char *p, const char *q) |
| 2270 | { |
| 2271 | int c, d; |
| 2272 | |
| 2273 | while ((c = *p) == (d = *q)) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2274 | if (c == '\0' || c == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2275 | goto out; |
| 2276 | p++; |
| 2277 | q++; |
| 2278 | } |
| 2279 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2280 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2281 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2282 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2283 | out: |
| 2284 | return c - d; |
| 2285 | } |
| 2286 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2287 | /* |
| 2288 | * Find the appropriate entry in the hash table from the name. |
| 2289 | */ |
| 2290 | static struct var ** |
| 2291 | hashvar(const char *p) |
| 2292 | { |
| 2293 | unsigned hashval; |
| 2294 | |
| 2295 | hashval = ((unsigned char) *p) << 4; |
| 2296 | while (*p && *p != '=') |
| 2297 | hashval += (unsigned char) *p++; |
| 2298 | return &vartab[hashval % VTABSIZE]; |
| 2299 | } |
| 2300 | |
| 2301 | static int |
| 2302 | vpcmp(const void *a, const void *b) |
| 2303 | { |
| 2304 | return varcmp(*(const char **)a, *(const char **)b); |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | * This routine initializes the builtin variables. |
| 2309 | */ |
| 2310 | static void |
| 2311 | initvar(void) |
| 2312 | { |
| 2313 | struct var *vp; |
| 2314 | struct var *end; |
| 2315 | struct var **vpp; |
| 2316 | |
| 2317 | /* |
| 2318 | * PS1 depends on uid |
| 2319 | */ |
| 2320 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2321 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2322 | #else |
| 2323 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2324 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2325 | #endif |
| 2326 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2327 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2328 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2329 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2330 | vp->next = *vpp; |
| 2331 | *vpp = vp; |
| 2332 | } while (++vp < end); |
| 2333 | } |
| 2334 | |
| 2335 | static struct var ** |
| 2336 | findvar(struct var **vpp, const char *name) |
| 2337 | { |
| 2338 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2339 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | return vpp; |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Find the value of a variable. Returns NULL if not set. |
| 2348 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2349 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2350 | lookupvar(const char *name) |
| 2351 | { |
| 2352 | struct var *v; |
| 2353 | |
| 2354 | v = *findvar(hashvar(name), name); |
| 2355 | if (v) { |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2356 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2357 | /* |
| 2358 | * Dynamic variables are implemented roughly the same way they are |
| 2359 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2360 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2361 | * lookup will no longer happen at that point. -- PFM. |
| 2362 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2363 | if (v->flags & VDYNAMIC) |
| 2364 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2365 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2366 | if (!(v->flags & VUNSET)) { |
Denys Vlasenko | d6c9cbc | 2021-09-07 18:01:49 +0200 | [diff] [blame] | 2367 | if (v->var_text == linenovar) { |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2368 | fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno); |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 2369 | } else |
| 2370 | if (v->var_text == funcnamevar) { |
| 2371 | safe_strncpy(funcnamevar+9, funcname ? funcname : "", sizeof(funcnamevar)-9); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2372 | } |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2373 | return var_end(v->var_text); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 2374 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2375 | } |
| 2376 | return NULL; |
| 2377 | } |
| 2378 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2379 | #if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 2380 | static void |
| 2381 | reinit_unicode_for_ash(void) |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2382 | { |
| 2383 | /* Unicode support should be activated even if LANG is set |
| 2384 | * _during_ shell execution, not only if it was set when |
| 2385 | * shell was started. Therefore, re-check LANG every time: |
| 2386 | */ |
| 2387 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2388 | || ENABLE_UNICODE_USING_LOCALE |
| 2389 | ) { |
| 2390 | const char *s = lookupvar("LC_ALL"); |
| 2391 | if (!s) s = lookupvar("LC_CTYPE"); |
| 2392 | if (!s) s = lookupvar("LANG"); |
| 2393 | reinit_unicode(s); |
| 2394 | } |
| 2395 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 2396 | #else |
| 2397 | # define reinit_unicode_for_ash() ((void)0) |
| 2398 | #endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2399 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2400 | /* |
| 2401 | * Search the environment of a builtin command. |
| 2402 | */ |
Denys Vlasenko | 488e609 | 2017-07-26 23:08:36 +0200 | [diff] [blame] | 2403 | static ALWAYS_INLINE const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2404 | bltinlookup(const char *name) |
| 2405 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2406 | return lookupvar(name); |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * Same as setvar except that the variable and value are passed in |
| 2411 | * the first argument as name=value. Since the first argument will |
| 2412 | * be actually stored in the table, it should not be a string that |
| 2413 | * will go away. |
| 2414 | * Called with interrupts off. |
| 2415 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2416 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2417 | setvareq(char *s, int flags) |
| 2418 | { |
| 2419 | struct var *vp, **vpp; |
| 2420 | |
| 2421 | vpp = hashvar(s); |
| 2422 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2423 | vpp = findvar(vpp, s); |
| 2424 | vp = *vpp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2425 | if (vp) { |
| 2426 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2427 | const char *n; |
| 2428 | |
| 2429 | if (flags & VNOSAVE) |
| 2430 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2431 | n = vp->var_text; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 2432 | exitstatus = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2433 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2434 | } |
| 2435 | |
| 2436 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2437 | goto out; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2438 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2439 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2440 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2441 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2442 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2443 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2444 | |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2445 | if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) { |
| 2446 | *vpp = vp->next; |
| 2447 | free(vp); |
| 2448 | out_free: |
| 2449 | if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE) |
| 2450 | free(s); |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2451 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2452 | } |
| 2453 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2454 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 2455 | #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS |
Ron Yorston | d96c69d | 2019-04-15 10:49:35 +0100 | [diff] [blame] | 2456 | if (flags & VUNSET) |
| 2457 | flags &= ~VDYNAMIC; |
| 2458 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2459 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2460 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2461 | if (flags & VNOSET) |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2462 | goto out; |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2463 | if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 2464 | goto out_free; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2465 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2466 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2467 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2468 | *vpp = vp; |
| 2469 | } |
| 2470 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2471 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2472 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2473 | vp->flags = flags; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2474 | |
| 2475 | out: |
| 2476 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | * Set the value of a variable. The flags argument is ored with the |
| 2481 | * flags of the variable. If val is NULL, the variable is unset. |
| 2482 | */ |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2483 | static struct var * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2484 | setvar(const char *name, const char *val, int flags) |
| 2485 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2486 | const char *q; |
| 2487 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2488 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2489 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2490 | size_t vallen; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2491 | struct var *vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2492 | |
| 2493 | q = endofname(name); |
| 2494 | p = strchrnul(q, '='); |
| 2495 | namelen = p - name; |
| 2496 | if (!namelen || p != q) |
| 2497 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2498 | vallen = 0; |
| 2499 | if (val == NULL) { |
| 2500 | flags |= VUNSET; |
| 2501 | } else { |
| 2502 | vallen = strlen(val); |
| 2503 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2504 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2505 | INT_OFF; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2506 | nameeq = ckzalloc(namelen + vallen + 2); |
Denys Vlasenko | da2244f | 2017-07-21 18:51:29 +0200 | [diff] [blame] | 2507 | p = mempcpy(nameeq, name, namelen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2508 | if (val) { |
| 2509 | *p++ = '='; |
Ron Yorston | e6a63bf | 2018-11-12 21:10:54 +0000 | [diff] [blame] | 2510 | memcpy(p, val, vallen); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2511 | } |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2512 | vp = setvareq(nameeq, flags | VNOSAVE); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2513 | INT_ON; |
Denys Vlasenko | d04fc71 | 2017-07-26 20:06:48 +0200 | [diff] [blame] | 2514 | |
| 2515 | return vp; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2518 | static void FAST_FUNC |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2519 | setvar0(const char *name, const char *val) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2520 | { |
| 2521 | setvar(name, val, 0); |
| 2522 | } |
| 2523 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2524 | /* |
| 2525 | * Unset the specified variable. |
| 2526 | */ |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 2527 | static void |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2528 | unsetvar(const char *s) |
| 2529 | { |
Denys Vlasenko | cf3a796 | 2017-07-26 14:38:19 +0200 | [diff] [blame] | 2530 | setvar(s, NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2534 | * Generate a list of variables satisfying the given conditions. |
| 2535 | */ |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2536 | #if !ENABLE_FEATURE_SH_NOFORK |
| 2537 | # define listvars(on, off, lp, end) listvars(on, off, end) |
| 2538 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2539 | static char ** |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2540 | listvars(int on, int off, struct strlist *lp, char ***end) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2541 | { |
| 2542 | struct var **vpp; |
| 2543 | struct var *vp; |
| 2544 | char **ep; |
| 2545 | int mask; |
| 2546 | |
| 2547 | STARTSTACKSTR(ep); |
| 2548 | vpp = vartab; |
| 2549 | mask = on | off; |
| 2550 | do { |
| 2551 | for (vp = *vpp; vp; vp = vp->next) { |
| 2552 | if ((vp->flags & mask) == on) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2553 | #if ENABLE_FEATURE_SH_NOFORK |
| 2554 | /* If variable with the same name is both |
| 2555 | * exported and temporarily set for a command: |
| 2556 | * export ZVAR=5 |
| 2557 | * ZVAR=6 printenv |
| 2558 | * then "ZVAR=6" will be both in vartab and |
| 2559 | * lp lists. Do not pass it twice to printenv. |
| 2560 | */ |
| 2561 | struct strlist *lp1 = lp; |
| 2562 | while (lp1) { |
| 2563 | if (strcmp(lp1->text, vp->var_text) == 0) |
| 2564 | goto skip; |
| 2565 | lp1 = lp1->next; |
| 2566 | } |
| 2567 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2568 | if (ep == stackstrend()) |
| 2569 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2570 | *ep++ = (char*)vp->var_text; |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2571 | #if ENABLE_FEATURE_SH_NOFORK |
| 2572 | skip: ; |
| 2573 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2574 | } |
| 2575 | } |
| 2576 | } while (++vpp < vartab + VTABSIZE); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 2577 | |
| 2578 | #if ENABLE_FEATURE_SH_NOFORK |
| 2579 | while (lp) { |
| 2580 | if (ep == stackstrend()) |
| 2581 | ep = growstackstr(); |
| 2582 | *ep++ = lp->text; |
| 2583 | lp = lp->next; |
| 2584 | } |
| 2585 | #endif |
| 2586 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2587 | if (ep == stackstrend()) |
| 2588 | ep = growstackstr(); |
| 2589 | if (end) |
| 2590 | *end = ep; |
| 2591 | *ep++ = NULL; |
| 2592 | return grabstackstr(ep); |
| 2593 | } |
| 2594 | |
| 2595 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2596 | /* ============ Path search helper */ |
| 2597 | static const char * |
| 2598 | legal_pathopt(const char *opt, const char *term, int magic) |
| 2599 | { |
| 2600 | switch (magic) { |
| 2601 | case 0: |
| 2602 | opt = NULL; |
| 2603 | break; |
| 2604 | |
| 2605 | case 1: |
| 2606 | opt = prefix(opt, "builtin") ?: prefix(opt, "func"); |
| 2607 | break; |
| 2608 | |
| 2609 | default: |
| 2610 | opt += strcspn(opt, term); |
| 2611 | break; |
| 2612 | } |
| 2613 | |
| 2614 | if (opt && *opt == '%') |
| 2615 | opt++; |
| 2616 | |
| 2617 | return opt; |
| 2618 | } |
| 2619 | |
| 2620 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2621 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2622 | * of the path before the first call; padvance will update |
| 2623 | * this value as it proceeds. Successive calls to padvance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2624 | * the possible path expansions in sequence. If an option (indicated by |
| 2625 | * a percent sign) appears in the path entry then the global variable |
| 2626 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2627 | * NULL. |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2628 | * |
| 2629 | * If magic is 0 then pathopt recognition will be disabled. If magic is |
| 2630 | * 1 we shall recognise %builtin/%func. Otherwise we shall accept any |
| 2631 | * pathopt. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2632 | */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2633 | static const char *pathopt; /* set by padvance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2634 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2635 | static int |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2636 | padvance_magic(const char **path, const char *name, int magic) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2637 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2638 | const char *term = "%:"; |
| 2639 | const char *lpathopt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2640 | const char *p; |
| 2641 | char *q; |
| 2642 | const char *start; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2643 | size_t qlen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2644 | size_t len; |
| 2645 | |
| 2646 | if (*path == NULL) |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2647 | return -1; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2648 | |
| 2649 | lpathopt = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2650 | start = *path; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2651 | |
| 2652 | if (*start == '%' && (p = legal_pathopt(start + 1, term, magic))) { |
| 2653 | lpathopt = start + 1; |
| 2654 | start = p; |
| 2655 | term = ":"; |
| 2656 | } |
| 2657 | |
| 2658 | len = strcspn(start, term); |
| 2659 | p = start + len; |
| 2660 | |
| 2661 | if (*p == '%') { |
| 2662 | size_t extra = strchrnul(p, ':') - p; |
| 2663 | |
| 2664 | if (legal_pathopt(p + 1, term, magic)) |
| 2665 | lpathopt = p + 1; |
| 2666 | else |
| 2667 | len += extra; |
| 2668 | |
| 2669 | p += extra; |
| 2670 | } |
| 2671 | |
| 2672 | pathopt = lpathopt; |
| 2673 | *path = *p == ':' ? p + 1 : NULL; |
| 2674 | |
| 2675 | /* "2" is for '/' and '\0' */ |
| 2676 | qlen = len + strlen(name) + 2; |
| 2677 | q = growstackto(qlen); |
| 2678 | |
| 2679 | if (len) { |
| 2680 | q = mempcpy(q, start, len); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2681 | *q++ = '/'; |
| 2682 | } |
| 2683 | strcpy(q, name); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 2684 | |
| 2685 | return qlen; |
| 2686 | } |
| 2687 | |
| 2688 | static int |
| 2689 | padvance(const char **path, const char *name) |
| 2690 | { |
| 2691 | return padvance_magic(path, name, 1); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | |
| 2695 | /* ============ Prompt */ |
| 2696 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2697 | static smallint doprompt; /* if set, prompt the user */ |
| 2698 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2699 | |
| 2700 | #if ENABLE_FEATURE_EDITING |
| 2701 | static line_input_t *line_input_state; |
| 2702 | static const char *cmdedit_prompt; |
| 2703 | static void |
| 2704 | putprompt(const char *s) |
| 2705 | { |
| 2706 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2707 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2708 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2709 | return; |
| 2710 | } |
| 2711 | cmdedit_prompt = s; |
| 2712 | } |
| 2713 | #else |
| 2714 | static void |
| 2715 | putprompt(const char *s) |
| 2716 | { |
| 2717 | out2str(s); |
| 2718 | } |
| 2719 | #endif |
| 2720 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2721 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2722 | static const char *expandstr(const char *ps, int syntax_type); |
| 2723 | /* Values for syntax param */ |
| 2724 | #define BASESYNTAX 0 /* not in quotes */ |
| 2725 | #define DQSYNTAX 1 /* in double quotes */ |
| 2726 | #define SQSYNTAX 2 /* in single quotes */ |
| 2727 | #define ARISYNTAX 3 /* in arithmetic */ |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 2728 | #if ENABLE_ASH_EXPAND_PRMT |
| 2729 | # define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
| 2730 | #endif |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2731 | /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2732 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2733 | /* |
| 2734 | * called by editline -- any expansions to the prompt should be added here. |
| 2735 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2736 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2737 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2738 | { |
| 2739 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2740 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2741 | |
| 2742 | if (!do_set) |
| 2743 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2744 | |
| 2745 | needprompt = 0; |
| 2746 | |
| 2747 | switch (whichprompt) { |
| 2748 | case 1: |
| 2749 | prompt = ps1val(); |
| 2750 | break; |
| 2751 | case 2: |
| 2752 | prompt = ps2val(); |
| 2753 | break; |
| 2754 | default: /* 0 */ |
| 2755 | prompt = nullstr; |
| 2756 | } |
| 2757 | #if ENABLE_ASH_EXPAND_PRMT |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 2758 | pushstackmark(&smark, stackblocksize()); |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 2759 | putprompt(expandstr(prompt, PSSYNTAX)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2760 | popstackmark(&smark); |
Denys Vlasenko | 48c803a | 2017-07-01 23:24:48 +0200 | [diff] [blame] | 2761 | #else |
| 2762 | putprompt(prompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2763 | #endif |
| 2764 | } |
| 2765 | |
| 2766 | |
| 2767 | /* ============ The cd and pwd commands */ |
| 2768 | |
| 2769 | #define CD_PHYSICAL 1 |
| 2770 | #define CD_PRINT 2 |
| 2771 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2772 | static int |
| 2773 | cdopt(void) |
| 2774 | { |
| 2775 | int flags = 0; |
| 2776 | int i, j; |
| 2777 | |
| 2778 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2779 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2780 | if (i != j) { |
| 2781 | flags ^= CD_PHYSICAL; |
| 2782 | j = i; |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | return flags; |
| 2787 | } |
| 2788 | |
| 2789 | /* |
| 2790 | * Update curdir (the name of the current directory) in response to a |
| 2791 | * cd command. |
| 2792 | */ |
| 2793 | static const char * |
| 2794 | updatepwd(const char *dir) |
| 2795 | { |
| 2796 | char *new; |
| 2797 | char *p; |
| 2798 | char *cdcomppath; |
| 2799 | const char *lim; |
| 2800 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 2801 | cdcomppath = sstrdup(dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2802 | STARTSTACKSTR(new); |
| 2803 | if (*dir != '/') { |
| 2804 | if (curdir == nullstr) |
| 2805 | return 0; |
| 2806 | new = stack_putstr(curdir, new); |
| 2807 | } |
| 2808 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2809 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2810 | if (*dir != '/') { |
| 2811 | if (new[-1] != '/') |
| 2812 | USTPUTC('/', new); |
| 2813 | if (new > lim && *lim == '/') |
| 2814 | lim++; |
| 2815 | } else { |
| 2816 | USTPUTC('/', new); |
| 2817 | cdcomppath++; |
| 2818 | if (dir[1] == '/' && dir[2] != '/') { |
| 2819 | USTPUTC('/', new); |
| 2820 | cdcomppath++; |
| 2821 | lim++; |
| 2822 | } |
| 2823 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2824 | p = strtok_r(cdcomppath, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2825 | while (p) { |
| 2826 | switch (*p) { |
| 2827 | case '.': |
| 2828 | if (p[1] == '.' && p[2] == '\0') { |
| 2829 | while (new > lim) { |
| 2830 | STUNPUTC(new); |
| 2831 | if (new[-1] == '/') |
| 2832 | break; |
| 2833 | } |
| 2834 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2835 | } |
| 2836 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2837 | break; |
| 2838 | /* fall through */ |
| 2839 | default: |
| 2840 | new = stack_putstr(p, new); |
| 2841 | USTPUTC('/', new); |
| 2842 | } |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 2843 | p = strtok_r(NULL, "/", &cdcomppath); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2844 | } |
| 2845 | if (new > lim) |
| 2846 | STUNPUTC(new); |
| 2847 | *new = 0; |
| 2848 | return stackblock(); |
| 2849 | } |
| 2850 | |
| 2851 | /* |
| 2852 | * Find out what the current directory is. If we already know the current |
| 2853 | * directory, this routine returns immediately. |
| 2854 | */ |
| 2855 | static char * |
| 2856 | getpwd(void) |
| 2857 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2858 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2859 | return dir ? dir : nullstr; |
| 2860 | } |
| 2861 | |
| 2862 | static void |
| 2863 | setpwd(const char *val, int setold) |
| 2864 | { |
| 2865 | char *oldcur, *dir; |
| 2866 | |
| 2867 | oldcur = dir = curdir; |
| 2868 | |
| 2869 | if (setold) { |
| 2870 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2871 | } |
| 2872 | INT_OFF; |
| 2873 | if (physdir != nullstr) { |
| 2874 | if (physdir != oldcur) |
| 2875 | free(physdir); |
| 2876 | physdir = nullstr; |
| 2877 | } |
| 2878 | if (oldcur == val || !val) { |
| 2879 | char *s = getpwd(); |
| 2880 | physdir = s; |
| 2881 | if (!val) |
| 2882 | dir = s; |
| 2883 | } else |
| 2884 | dir = ckstrdup(val); |
| 2885 | if (oldcur != dir && oldcur != nullstr) { |
| 2886 | free(oldcur); |
| 2887 | } |
| 2888 | curdir = dir; |
| 2889 | INT_ON; |
| 2890 | setvar("PWD", dir, VEXPORT); |
| 2891 | } |
| 2892 | |
| 2893 | static void hashcd(void); |
| 2894 | |
| 2895 | /* |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 2896 | * Actually do the chdir. We also call hashcd to let other routines |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2897 | * know that the current directory has changed. |
| 2898 | */ |
| 2899 | static int |
| 2900 | docd(const char *dest, int flags) |
| 2901 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2902 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2903 | int err; |
| 2904 | |
| 2905 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2906 | |
| 2907 | INT_OFF; |
| 2908 | if (!(flags & CD_PHYSICAL)) { |
| 2909 | dir = updatepwd(dest); |
| 2910 | if (dir) |
| 2911 | dest = dir; |
| 2912 | } |
| 2913 | err = chdir(dest); |
| 2914 | if (err) |
| 2915 | goto out; |
| 2916 | setpwd(dir, 1); |
| 2917 | hashcd(); |
| 2918 | out: |
| 2919 | INT_ON; |
| 2920 | return err; |
| 2921 | } |
| 2922 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2923 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2924 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2925 | { |
| 2926 | const char *dest; |
| 2927 | const char *path; |
| 2928 | const char *p; |
| 2929 | char c; |
| 2930 | struct stat statb; |
| 2931 | int flags; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2932 | int len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2933 | |
| 2934 | flags = cdopt(); |
| 2935 | dest = *argptr; |
| 2936 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2937 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2938 | else if (LONE_DASH(dest)) { |
| 2939 | dest = bltinlookup("OLDPWD"); |
| 2940 | flags |= CD_PRINT; |
| 2941 | } |
| 2942 | if (!dest) |
| 2943 | dest = nullstr; |
| 2944 | if (*dest == '/') |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2945 | goto step6; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2946 | if (*dest == '.') { |
| 2947 | c = dest[1]; |
| 2948 | dotdot: |
| 2949 | switch (c) { |
| 2950 | case '\0': |
| 2951 | case '/': |
| 2952 | goto step6; |
| 2953 | case '.': |
| 2954 | c = dest[2]; |
| 2955 | if (c != '.') |
| 2956 | goto dotdot; |
| 2957 | } |
| 2958 | } |
| 2959 | if (!*dest) |
| 2960 | dest = "."; |
| 2961 | path = bltinlookup("CDPATH"); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 2962 | while (p = path, (len = padvance(&path, dest)) >= 0) { |
| 2963 | c = *p; |
| 2964 | p = stalloc(len); |
| 2965 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2966 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2967 | if (c && c != ':') |
| 2968 | flags |= CD_PRINT; |
| 2969 | docd: |
| 2970 | if (!docd(p, flags)) |
| 2971 | goto out; |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2972 | goto err; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2973 | } |
Denys Vlasenko | 0e081d0 | 2016-10-26 19:56:05 +0200 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | step6: |
| 2977 | p = dest; |
| 2978 | goto docd; |
| 2979 | |
| 2980 | err: |
Johannes Schindelin | 687aac0 | 2017-08-22 22:03:22 +0200 | [diff] [blame] | 2981 | ash_msg_and_raise_perror("can't cd to %s", dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2982 | /* NOTREACHED */ |
| 2983 | out: |
| 2984 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2985 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2986 | return 0; |
| 2987 | } |
| 2988 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2989 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2990 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2991 | { |
| 2992 | int flags; |
| 2993 | const char *dir = curdir; |
| 2994 | |
| 2995 | flags = cdopt(); |
| 2996 | if (flags) { |
| 2997 | if (physdir == nullstr) |
| 2998 | setpwd(dir, 0); |
| 2999 | dir = physdir; |
| 3000 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 3001 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 3002 | return 0; |
| 3003 | } |
| 3004 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3005 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 3006 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3007 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 3008 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 3009 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3010 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3011 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 3012 | #define CWORD 0 /* character is nothing special */ |
| 3013 | #define CNL 1 /* newline character */ |
| 3014 | #define CBACK 2 /* a backslash character */ |
| 3015 | #define CSQUOTE 3 /* single quote */ |
| 3016 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3017 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 3018 | #define CBQUOTE 6 /* backwards single quote */ |
| 3019 | #define CVAR 7 /* a dollar sign */ |
| 3020 | #define CENDVAR 8 /* a '}' character */ |
| 3021 | #define CLP 9 /* a left paren in arithmetic */ |
| 3022 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3023 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 3024 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 3025 | #define CSPCL 13 /* these terminate a word */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3026 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3027 | #define PEOF 256 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3028 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3029 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3030 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 3031 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3032 | # 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] | 3033 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3034 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3035 | #endif |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 3036 | static const uint16_t S_I_T[] ALIGN2 = { |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3037 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 0, ' ' */ |
| 3038 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 1, \n */ |
| 3039 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 2, !*-/:=?[]~ */ |
| 3040 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 3, '"' */ |
| 3041 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 4, $ */ |
| 3042 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 5, "'" */ |
| 3043 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 6, ( */ |
| 3044 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 7, ) */ |
| 3045 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 8, \ */ |
| 3046 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 9, ` */ |
| 3047 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 10, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3048 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3049 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 11, PEOF */ |
| 3050 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 12, 0-9A-Za-z */ |
| 3051 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 13, CTLESC ... */ |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3052 | #endif |
| 3053 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3054 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3055 | /* Constants below must match table above */ |
| 3056 | enum { |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3057 | CSPCL_CWORD_CWORD_CWORD , /* 0 */ |
| 3058 | CNL_CNL_CNL_CNL , /* 1 */ |
| 3059 | CWORD_CCTL_CCTL_CWORD , /* 2 */ |
| 3060 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 3 */ |
| 3061 | CVAR_CVAR_CWORD_CVAR , /* 4 */ |
| 3062 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 5 */ |
| 3063 | CSPCL_CWORD_CWORD_CLP , /* 6 */ |
| 3064 | CSPCL_CWORD_CWORD_CRP , /* 7 */ |
| 3065 | CBACK_CBACK_CCTL_CBACK , /* 8 */ |
| 3066 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 9 */ |
| 3067 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 10 */ |
| 3068 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 11 */ |
| 3069 | CWORD_CWORD_CWORD_CWORD , /* 12 */ |
| 3070 | CCTL_CCTL_CCTL_CCTL , /* 13 */ |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3071 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3072 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3073 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOF, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3074 | * caller must ensure proper cast on it if c is *char_ptr! |
| 3075 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3076 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3077 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3078 | static int |
| 3079 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3080 | { |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3081 | /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */ |
| 3082 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~"; |
| 3083 | /* |
| 3084 | * This causes '/' to be prepended with CTLESC in dquoted string, |
| 3085 | * making "./file"* treated incorrectly because we feed |
| 3086 | * ".\/file*" string to glob(), confusing it (see expandmeta func). |
| 3087 | * The "homegrown" glob implementation is okay with that, |
| 3088 | * but glibc one isn't. With '/' always treated as CWORD, |
| 3089 | * both work fine. |
| 3090 | */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3091 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3092 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3093 | 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3094 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 3095 | 10, 2 /* "}~" */ |
| 3096 | }; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3097 | const char *s; |
| 3098 | int indx; |
| 3099 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3100 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3101 | return CENDFILE; |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3102 | /* Cast is purely for paranoia here, |
| 3103 | * just in case someone passed signed char to us */ |
| 3104 | if ((unsigned char)c >= CTL_FIRST |
| 3105 | && (unsigned char)c <= CTL_LAST |
| 3106 | ) { |
| 3107 | return CCTL; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 3108 | } |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 3109 | s = strchrnul(spec_symbls, c); |
| 3110 | if (*s == '\0') |
| 3111 | return CWORD; |
| 3112 | indx = syntax_index_table[s - spec_symbls]; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3113 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3116 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 3117 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 3118 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3119 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3120 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 3121 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 3122 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 3123 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 3124 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 3125 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 3126 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 3127 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 3128 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 3129 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 3130 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 3131 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 3132 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 3133 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 3134 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 3135 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 3136 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 3137 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 3138 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 3139 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 3140 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 3141 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 3142 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 3143 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 3144 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 3145 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 3146 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 3147 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 3148 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 3149 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 3150 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 3151 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 3152 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 3153 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 3154 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 3155 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 3156 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 3157 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 3158 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 3159 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 3160 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 3161 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 3162 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 3163 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 3164 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 3165 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 3166 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 3167 | /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */ |
| 3168 | /* 47 "/" */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3169 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 3170 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 3171 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 3172 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 3173 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 3174 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 3175 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 3176 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 3177 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 3178 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 3179 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 3180 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 3181 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 3182 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 3183 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 3184 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 3185 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 3186 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 3187 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 3188 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 3189 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 3190 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 3191 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 3192 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 3193 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 3194 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 3195 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 3196 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 3197 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 3198 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 3199 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 3200 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 3201 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 3202 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 3203 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 3204 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 3205 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 3206 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 3207 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 3208 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 3209 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 3210 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 3211 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 3212 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 3213 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 3214 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 3215 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 3216 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 3217 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 3218 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 3219 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 3220 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 3221 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 3222 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 3223 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 3224 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 3225 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 3226 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 3227 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 3228 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 3229 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 3230 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 3231 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 3232 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 3233 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 3234 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 3235 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 3236 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 3237 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 3238 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 3239 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 3240 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 3241 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 3242 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 3243 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 3244 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 3245 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 3246 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 3247 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 3248 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 3249 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 3250 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 3251 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3252 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 3253 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 3254 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 3255 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 3256 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 3257 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 3258 | #if BASH_PROCESS_SUBST |
| 3259 | /* 137 CTLTOPROC */ CCTL_CCTL_CCTL_CCTL, |
| 3260 | /* 138 CTLFROMPROC */ CCTL_CCTL_CCTL_CCTL, |
| 3261 | #else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3262 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 3263 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 3264 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3265 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 3266 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 3267 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 3268 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 3269 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 3270 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 3271 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 3272 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 3273 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 3274 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 3275 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 3276 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 3277 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 3278 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 3279 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 3280 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 3281 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 3282 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 3283 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 3284 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 3285 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 3286 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 3287 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3288 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3289 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3290 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3291 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3292 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3293 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3294 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3295 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3296 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3297 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3298 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3299 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3300 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3301 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3302 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3303 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3304 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3305 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3306 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3307 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3308 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3309 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3310 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3311 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3312 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3313 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3314 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3315 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3316 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3317 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3318 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3319 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3320 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3321 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3322 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3323 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3324 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3325 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3326 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3327 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3328 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3329 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3330 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3331 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3332 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3333 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3334 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3335 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3336 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3337 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3338 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3339 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3340 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3341 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3342 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3343 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3344 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3345 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3346 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3347 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3348 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3349 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3350 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3351 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3352 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3353 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3354 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3355 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3356 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3357 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3358 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3359 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3360 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3361 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3362 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3363 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3364 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3365 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3366 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3367 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3368 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3369 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3370 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3371 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3372 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3373 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3374 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3375 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3376 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3377 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3378 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3379 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3380 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3381 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3382 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3383 | }; |
| 3384 | |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3385 | #if 1 |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3386 | # 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] | 3387 | #else /* debug version, caught one signed char bug */ |
| 3388 | # define SIT(c, syntax) \ |
| 3389 | ({ \ |
| 3390 | if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \ |
| 3391 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 3392 | if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \ |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 3393 | bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \ |
| 3394 | ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \ |
| 3395 | }) |
| 3396 | #endif |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3397 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3398 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3399 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3400 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3401 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3402 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3403 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3404 | |
| 3405 | #define ALIASINUSE 1 |
| 3406 | #define ALIASDEAD 2 |
| 3407 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3408 | struct alias { |
| 3409 | struct alias *next; |
| 3410 | char *name; |
| 3411 | char *val; |
| 3412 | int flag; |
| 3413 | }; |
| 3414 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3415 | |
| 3416 | static struct alias **atab; // [ATABSIZE]; |
| 3417 | #define INIT_G_alias() do { \ |
| 3418 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3419 | } while (0) |
| 3420 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3421 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3422 | static struct alias ** |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 3423 | __lookupalias(const char *name) |
| 3424 | { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3425 | unsigned int hashval; |
| 3426 | struct alias **app; |
| 3427 | const char *p; |
| 3428 | unsigned int ch; |
| 3429 | |
| 3430 | p = name; |
| 3431 | |
| 3432 | ch = (unsigned char)*p; |
| 3433 | hashval = ch << 4; |
| 3434 | while (ch) { |
| 3435 | hashval += ch; |
| 3436 | ch = (unsigned char)*++p; |
| 3437 | } |
| 3438 | app = &atab[hashval % ATABSIZE]; |
| 3439 | |
| 3440 | for (; *app; app = &(*app)->next) { |
| 3441 | if (strcmp(name, (*app)->name) == 0) { |
| 3442 | break; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | return app; |
| 3447 | } |
| 3448 | |
| 3449 | static struct alias * |
| 3450 | lookupalias(const char *name, int check) |
| 3451 | { |
| 3452 | struct alias *ap = *__lookupalias(name); |
| 3453 | |
| 3454 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3455 | return NULL; |
| 3456 | return ap; |
| 3457 | } |
| 3458 | |
| 3459 | static struct alias * |
| 3460 | freealias(struct alias *ap) |
| 3461 | { |
| 3462 | struct alias *next; |
| 3463 | |
| 3464 | if (ap->flag & ALIASINUSE) { |
| 3465 | ap->flag |= ALIASDEAD; |
| 3466 | return ap; |
| 3467 | } |
| 3468 | |
| 3469 | next = ap->next; |
| 3470 | free(ap->name); |
| 3471 | free(ap->val); |
| 3472 | free(ap); |
| 3473 | return next; |
| 3474 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3475 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3476 | static void |
| 3477 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3478 | { |
| 3479 | struct alias *ap, **app; |
| 3480 | |
| 3481 | app = __lookupalias(name); |
| 3482 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3483 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3484 | if (ap) { |
| 3485 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3486 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3487 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3488 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3489 | ap->flag &= ~ALIASDEAD; |
| 3490 | } else { |
| 3491 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3492 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3493 | ap->name = ckstrdup(name); |
| 3494 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3495 | /*ap->flag = 0; - ckzalloc did it */ |
| 3496 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3497 | *app = ap; |
| 3498 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3499 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3502 | static int |
| 3503 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3504 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3505 | struct alias **app; |
| 3506 | |
| 3507 | app = __lookupalias(name); |
| 3508 | |
| 3509 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3510 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3511 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3512 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3513 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3516 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3519 | static void |
| 3520 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3521 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3522 | struct alias *ap, **app; |
| 3523 | int i; |
| 3524 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3525 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3526 | for (i = 0; i < ATABSIZE; i++) { |
| 3527 | app = &atab[i]; |
| 3528 | for (ap = *app; ap; ap = *app) { |
| 3529 | *app = freealias(*app); |
| 3530 | if (ap == *app) { |
| 3531 | app = &ap->next; |
| 3532 | } |
| 3533 | } |
| 3534 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3535 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3538 | static void |
| 3539 | printalias(const struct alias *ap) |
| 3540 | { |
| 3541 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3542 | } |
| 3543 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3544 | /* |
| 3545 | * TODO - sort output |
| 3546 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3547 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3548 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3549 | { |
| 3550 | char *n, *v; |
| 3551 | int ret = 0; |
| 3552 | struct alias *ap; |
| 3553 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3554 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3555 | int i; |
| 3556 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3557 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3558 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3559 | printalias(ap); |
| 3560 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3561 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3562 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3563 | } |
| 3564 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3565 | v = strchr(n+1, '='); |
| 3566 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3567 | ap = *__lookupalias(n); |
| 3568 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3569 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3570 | ret = 1; |
| 3571 | } else |
| 3572 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3573 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3574 | *v++ = '\0'; |
| 3575 | setalias(n, v); |
| 3576 | } |
| 3577 | } |
| 3578 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3579 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3582 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3583 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3584 | { |
| 3585 | int i; |
| 3586 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 3587 | while (nextopt("a") != '\0') { |
| 3588 | rmaliases(); |
| 3589 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3590 | } |
| 3591 | for (i = 0; *argptr; argptr++) { |
| 3592 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3593 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3594 | i = 1; |
| 3595 | } |
| 3596 | } |
| 3597 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3598 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3599 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3600 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3601 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3602 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3603 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3604 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3605 | #define FORK_FG 0 |
| 3606 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3607 | #define FORK_NOJOB 2 |
| 3608 | |
| 3609 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3610 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3611 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3612 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3613 | #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3614 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3615 | /* |
| 3616 | * A job structure contains information about a job. A job is either a |
| 3617 | * single process or a set of processes contained in a pipeline. In the |
| 3618 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3619 | * array of pids. |
| 3620 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3621 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3622 | pid_t ps_pid; /* process id */ |
| 3623 | int ps_status; /* last process status from wait() */ |
| 3624 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3625 | }; |
| 3626 | |
| 3627 | struct job { |
| 3628 | struct procstat ps0; /* status of process */ |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 3629 | struct procstat *ps; /* status of processes when more than one */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3630 | #if JOBS |
| 3631 | int stopstatus; /* status of a stopped job */ |
| 3632 | #endif |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3633 | unsigned nprocs; /* number of processes */ |
| 3634 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3635 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3636 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3637 | #define JOBDONE 2 /* all procs are completed */ |
Denys Vlasenko | 4c17937 | 2017-01-11 18:44:15 +0100 | [diff] [blame] | 3638 | unsigned |
| 3639 | state: 8, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3640 | #if JOBS |
| 3641 | sigint: 1, /* job was killed by SIGINT */ |
| 3642 | jobctl: 1, /* job running under job control */ |
| 3643 | #endif |
| 3644 | waited: 1, /* true if this entry has been waited for */ |
| 3645 | used: 1, /* true if this entry is in used */ |
| 3646 | changed: 1; /* true if status has changed */ |
| 3647 | struct job *prev_job; /* previous job */ |
| 3648 | }; |
| 3649 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3650 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3651 | static int forkshell(struct job *, union node *, int); |
| 3652 | static int waitforjob(struct job *); |
| 3653 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3654 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3655 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3656 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3657 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3658 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3659 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3660 | #endif |
| 3661 | |
| 3662 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3663 | * Ignore a signal. |
| 3664 | */ |
| 3665 | static void |
| 3666 | ignoresig(int signo) |
| 3667 | { |
| 3668 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3669 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3670 | /* No, need to do it */ |
| 3671 | signal(signo, SIG_IGN); |
| 3672 | } |
| 3673 | sigmode[signo - 1] = S_HARD_IGN; |
| 3674 | } |
| 3675 | |
| 3676 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3677 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3678 | */ |
| 3679 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3680 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3681 | { |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3682 | if (signo == SIGCHLD) { |
| 3683 | got_sigchld = 1; |
| 3684 | if (!trap[SIGCHLD]) |
| 3685 | return; |
| 3686 | } |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 3687 | #if ENABLE_FEATURE_EDITING |
| 3688 | bb_got_signal = signo; /* for read_line_input: "we got a signal" */ |
| 3689 | #endif |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3690 | gotsig[signo - 1] = 1; |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3691 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3692 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3693 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3694 | if (!suppress_int) { |
| 3695 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3696 | raise_interrupt(); /* does not return */ |
| 3697 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3698 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3703 | * Set the signal handler for the specified signal. The routine figures |
| 3704 | * out what it should be set to. |
| 3705 | */ |
| 3706 | static void |
| 3707 | setsignal(int signo) |
| 3708 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3709 | char *t; |
| 3710 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3711 | struct sigaction act; |
| 3712 | |
| 3713 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3714 | new_act = S_DFL; |
| 3715 | if (t != NULL) { /* trap for this sig is set */ |
| 3716 | new_act = S_CATCH; |
| 3717 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3718 | new_act = S_IGN; |
| 3719 | } |
| 3720 | |
| 3721 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3722 | switch (signo) { |
| 3723 | case SIGINT: |
| 3724 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3725 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3726 | break; |
| 3727 | case SIGQUIT: |
| 3728 | #if DEBUG |
| 3729 | if (debug) |
| 3730 | break; |
| 3731 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3732 | /* man bash: |
| 3733 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3734 | * commands run by bash have signal handlers |
| 3735 | * set to the values inherited by the shell |
| 3736 | * from its parent". */ |
| 3737 | new_act = S_IGN; |
| 3738 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3739 | case SIGTERM: |
| 3740 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3741 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3742 | break; |
| 3743 | #if JOBS |
| 3744 | case SIGTSTP: |
| 3745 | case SIGTTOU: |
| 3746 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3747 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | #endif |
| 3750 | } |
| 3751 | } |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3752 | /* if !rootshell, we reset SIGQUIT to DFL, |
| 3753 | * whereas we have to restore it to what shell got on entry. |
| 3754 | * This is handled by the fact that if signal was IGNored on entry, |
| 3755 | * then cur_act is S_HARD_IGN and we never change its sigaction |
| 3756 | * (see code below). |
| 3757 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3758 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 3759 | if (signo == SIGCHLD) |
| 3760 | new_act = S_CATCH; |
| 3761 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3762 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3763 | cur_act = *t; |
| 3764 | if (cur_act == 0) { |
| 3765 | /* current setting is not yet known */ |
| 3766 | if (sigaction(signo, NULL, &act)) { |
| 3767 | /* pretend it worked; maybe we should give a warning, |
| 3768 | * but other shells don't. We don't alter sigmode, |
| 3769 | * so we retry every time. |
| 3770 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3771 | return; |
| 3772 | } |
| 3773 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3774 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3775 | if (mflag |
| 3776 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3777 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3778 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3779 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3780 | } |
Denys Vlasenko | 0f14f41 | 2017-08-06 20:06:19 +0200 | [diff] [blame] | 3781 | if (act.sa_handler == SIG_DFL && new_act == S_DFL) { |
| 3782 | /* installing SIG_DFL over SIG_DFL is a no-op */ |
| 3783 | /* saves one sigaction call in each "sh -c SCRIPT" invocation */ |
| 3784 | *t = S_DFL; |
| 3785 | return; |
| 3786 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3787 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3788 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3789 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3790 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3791 | *t = new_act; |
| 3792 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3793 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3794 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3795 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3796 | act.sa_handler = signal_handler; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3797 | break; |
| 3798 | case S_IGN: |
| 3799 | act.sa_handler = SIG_IGN; |
| 3800 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3801 | } |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3802 | /* flags and mask matter only if !DFL and !IGN, but we do it |
| 3803 | * for all cases for more deterministic behavior: |
| 3804 | */ |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 3805 | act.sa_flags = 0; //TODO: why not SA_RESTART? |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3806 | sigfillset(&act.sa_mask); |
| 3807 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3808 | sigaction_set(signo, &act); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | /* mode flags for set_curjob */ |
| 3812 | #define CUR_DELETE 2 |
| 3813 | #define CUR_RUNNING 1 |
| 3814 | #define CUR_STOPPED 0 |
| 3815 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3816 | #if JOBS |
| 3817 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3818 | static int initialpgrp; //references:2 |
| 3819 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3820 | #endif |
| 3821 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3822 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3823 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3824 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3825 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3826 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3827 | |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 3828 | #if 0 |
| 3829 | /* Bash has a feature: it restores termios after a successful wait for |
| 3830 | * a foreground job which had at least one stopped or sigkilled member. |
| 3831 | * The probable rationale is that SIGSTOP and SIGKILL can preclude task from |
| 3832 | * properly restoring tty state. Should we do this too? |
| 3833 | * A reproducer: ^Z an interactive python: |
| 3834 | * |
| 3835 | * # python |
| 3836 | * Python 2.7.12 (...) |
| 3837 | * >>> ^Z |
| 3838 | * { python leaves tty in -icanon -echo state. We do survive that... } |
| 3839 | * [1]+ Stopped python |
| 3840 | * { ...however, next program (python #2) does not survive it well: } |
| 3841 | * # python |
| 3842 | * Python 2.7.12 (...) |
| 3843 | * >>> Traceback (most recent call last): |
| 3844 | * { above, I typed "qwerty<CR>", but -echo state is still in effect } |
| 3845 | * File "<stdin>", line 1, in <module> |
| 3846 | * NameError: name 'qwerty' is not defined |
| 3847 | * |
| 3848 | * The implementation below is modeled on bash code and seems to work. |
| 3849 | * However, I'm not sure we should do this. For one: what if I'd fg |
| 3850 | * the stopped python instead? It'll be confused by "restored" tty state. |
| 3851 | */ |
| 3852 | static struct termios shell_tty_info; |
| 3853 | static void |
| 3854 | get_tty_state(void) |
| 3855 | { |
| 3856 | if (rootshell && ttyfd >= 0) |
| 3857 | tcgetattr(ttyfd, &shell_tty_info); |
| 3858 | } |
| 3859 | static void |
| 3860 | set_tty_state(void) |
| 3861 | { |
| 3862 | /* if (rootshell) - caller ensures this */ |
| 3863 | if (ttyfd >= 0) |
| 3864 | tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info); |
| 3865 | } |
| 3866 | static int |
| 3867 | job_signal_status(struct job *jp) |
| 3868 | { |
| 3869 | int status; |
| 3870 | unsigned i; |
| 3871 | struct procstat *ps = jp->ps; |
| 3872 | for (i = 0; i < jp->nprocs; i++) { |
| 3873 | status = ps[i].ps_status; |
| 3874 | if (WIFSIGNALED(status) || WIFSTOPPED(status)) |
| 3875 | return status; |
| 3876 | } |
| 3877 | return 0; |
| 3878 | } |
| 3879 | static void |
| 3880 | restore_tty_if_stopped_or_signaled(struct job *jp) |
| 3881 | { |
| 3882 | //TODO: check what happens if we come from waitforjob() in expbackq() |
| 3883 | if (rootshell) { |
| 3884 | int s = job_signal_status(jp); |
| 3885 | if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */ |
| 3886 | set_tty_state(); |
| 3887 | } |
| 3888 | } |
| 3889 | #else |
| 3890 | # define get_tty_state() ((void)0) |
| 3891 | # define restore_tty_if_stopped_or_signaled(jp) ((void)0) |
| 3892 | #endif |
| 3893 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3894 | static void |
| 3895 | set_curjob(struct job *jp, unsigned mode) |
| 3896 | { |
| 3897 | struct job *jp1; |
| 3898 | struct job **jpp, **curp; |
| 3899 | |
| 3900 | /* first remove from list */ |
| 3901 | jpp = curp = &curjob; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3902 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3903 | jp1 = *jpp; |
| 3904 | if (jp1 == jp) |
| 3905 | break; |
| 3906 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3907 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3908 | *jpp = jp1->prev_job; |
| 3909 | |
| 3910 | /* Then re-insert in correct position */ |
| 3911 | jpp = curp; |
| 3912 | switch (mode) { |
| 3913 | default: |
| 3914 | #if DEBUG |
| 3915 | abort(); |
| 3916 | #endif |
| 3917 | case CUR_DELETE: |
| 3918 | /* job being deleted */ |
| 3919 | break; |
| 3920 | case CUR_RUNNING: |
| 3921 | /* newly created job or backgrounded job, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 3922 | * put after all stopped jobs. |
| 3923 | */ |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3924 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3925 | jp1 = *jpp; |
| 3926 | #if JOBS |
| 3927 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3928 | #endif |
| 3929 | break; |
| 3930 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3931 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3932 | /* FALLTHROUGH */ |
| 3933 | #if JOBS |
| 3934 | case CUR_STOPPED: |
| 3935 | #endif |
| 3936 | /* newly stopped job - becomes curjob */ |
| 3937 | jp->prev_job = *jpp; |
| 3938 | *jpp = jp; |
| 3939 | break; |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | #if JOBS || DEBUG |
| 3944 | static int |
| 3945 | jobno(const struct job *jp) |
| 3946 | { |
| 3947 | return jp - jobtab + 1; |
| 3948 | } |
| 3949 | #endif |
| 3950 | |
| 3951 | /* |
| 3952 | * Convert a job name to a job structure. |
| 3953 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3954 | #if !JOBS |
| 3955 | #define getjob(name, getctl) getjob(name) |
| 3956 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3957 | static struct job * |
| 3958 | getjob(const char *name, int getctl) |
| 3959 | { |
| 3960 | struct job *jp; |
| 3961 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3962 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3963 | unsigned num; |
| 3964 | int c; |
| 3965 | const char *p; |
| 3966 | char *(*match)(const char *, const char *); |
| 3967 | |
| 3968 | jp = curjob; |
| 3969 | p = name; |
| 3970 | if (!p) |
| 3971 | goto currentjob; |
| 3972 | |
| 3973 | if (*p != '%') |
| 3974 | goto err; |
| 3975 | |
| 3976 | c = *++p; |
| 3977 | if (!c) |
| 3978 | goto currentjob; |
| 3979 | |
| 3980 | if (!p[1]) { |
| 3981 | if (c == '+' || c == '%') { |
| 3982 | currentjob: |
| 3983 | err_msg = "No current job"; |
| 3984 | goto check; |
| 3985 | } |
| 3986 | if (c == '-') { |
| 3987 | if (jp) |
| 3988 | jp = jp->prev_job; |
| 3989 | err_msg = "No previous job"; |
| 3990 | check: |
| 3991 | if (!jp) |
| 3992 | goto err; |
| 3993 | goto gotit; |
| 3994 | } |
| 3995 | } |
| 3996 | |
| 3997 | if (is_number(p)) { |
| 3998 | num = atoi(p); |
Denys Vlasenko | 46a45ce | 2016-09-29 01:10:08 +0200 | [diff] [blame] | 3999 | if (num > 0 && num <= njobs) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4000 | jp = jobtab + num - 1; |
| 4001 | if (jp->used) |
| 4002 | goto gotit; |
| 4003 | goto err; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | match = prefix; |
| 4008 | if (*p == '?') { |
| 4009 | match = strstr; |
| 4010 | p++; |
| 4011 | } |
| 4012 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 4013 | found = NULL; |
| 4014 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4015 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4016 | if (found) |
| 4017 | goto err; |
| 4018 | found = jp; |
| 4019 | err_msg = "%s: ambiguous"; |
| 4020 | } |
| 4021 | jp = jp->prev_job; |
| 4022 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 4023 | if (!found) |
| 4024 | goto err; |
| 4025 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4026 | |
| 4027 | gotit: |
| 4028 | #if JOBS |
| 4029 | err_msg = "job %s not created under job control"; |
| 4030 | if (getctl && jp->jobctl == 0) |
| 4031 | goto err; |
| 4032 | #endif |
| 4033 | return jp; |
| 4034 | err: |
| 4035 | ash_msg_and_raise_error(err_msg, name); |
| 4036 | } |
| 4037 | |
| 4038 | /* |
| 4039 | * Mark a job structure as unused. |
| 4040 | */ |
| 4041 | static void |
| 4042 | freejob(struct job *jp) |
| 4043 | { |
| 4044 | struct procstat *ps; |
| 4045 | int i; |
| 4046 | |
| 4047 | INT_OFF; |
| 4048 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4049 | if (ps->ps_cmd != nullstr) |
| 4050 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4051 | } |
| 4052 | if (jp->ps != &jp->ps0) |
| 4053 | free(jp->ps); |
| 4054 | jp->used = 0; |
| 4055 | set_curjob(jp, CUR_DELETE); |
| 4056 | INT_ON; |
| 4057 | } |
| 4058 | |
| 4059 | #if JOBS |
| 4060 | static void |
| 4061 | xtcsetpgrp(int fd, pid_t pgrp) |
| 4062 | { |
| 4063 | if (tcsetpgrp(fd, pgrp)) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 4064 | ash_msg_and_raise_perror("can't set tty process group"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | /* |
| 4068 | * Turn job control on and off. |
| 4069 | * |
| 4070 | * Note: This code assumes that the third arg to ioctl is a character |
| 4071 | * pointer, which is true on Berkeley systems but not System V. Since |
| 4072 | * System V doesn't have job control yet, this isn't a problem now. |
| 4073 | * |
| 4074 | * Called with interrupts off. |
| 4075 | */ |
| 4076 | static void |
| 4077 | setjobctl(int on) |
| 4078 | { |
| 4079 | int fd; |
| 4080 | int pgrp; |
| 4081 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4082 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4083 | return; |
| 4084 | if (on) { |
| 4085 | int ofd; |
| 4086 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 4087 | if (fd < 0) { |
| 4088 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 4089 | * That sometimes helps to acquire controlling tty. |
| 4090 | * Obviously, a workaround for bugs when someone |
| 4091 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4092 | fd = 2; |
| 4093 | while (!isatty(fd)) |
| 4094 | if (--fd < 0) |
| 4095 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4096 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4097 | /* fd is a tty at this point */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4098 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 4099 | 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] | 4100 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4101 | if (fd < 0) |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 4102 | goto out; /* F_DUPFD failed */ |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 4103 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 4104 | close_on_exec_on(fd); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4105 | while (1) { /* while we are in the background */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4106 | pgrp = tcgetpgrp(fd); |
| 4107 | if (pgrp < 0) { |
| 4108 | out: |
| 4109 | ash_msg("can't access tty; job control turned off"); |
| 4110 | mflag = on = 0; |
| 4111 | goto close; |
| 4112 | } |
| 4113 | if (pgrp == getpgrp()) |
| 4114 | break; |
| 4115 | killpg(0, SIGTTIN); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 4116 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4117 | initialpgrp = pgrp; |
| 4118 | |
| 4119 | setsignal(SIGTSTP); |
| 4120 | setsignal(SIGTTOU); |
| 4121 | setsignal(SIGTTIN); |
| 4122 | pgrp = rootpid; |
| 4123 | setpgid(0, pgrp); |
| 4124 | xtcsetpgrp(fd, pgrp); |
| 4125 | } else { |
| 4126 | /* turning job control off */ |
| 4127 | fd = ttyfd; |
| 4128 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4129 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4130 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 4131 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4132 | setpgid(0, pgrp); |
| 4133 | setsignal(SIGTSTP); |
| 4134 | setsignal(SIGTTOU); |
| 4135 | setsignal(SIGTTIN); |
| 4136 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 4137 | if (fd >= 0) |
| 4138 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4139 | fd = -1; |
| 4140 | } |
| 4141 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4142 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4145 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4146 | killcmd(int argc, char **argv) |
| 4147 | { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4148 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4149 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4150 | do { |
| 4151 | if (argv[i][0] == '%') { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4152 | /* |
| 4153 | * "kill %N" - job kill |
| 4154 | * Converting to pgrp / pid kill |
| 4155 | */ |
| 4156 | struct job *jp; |
| 4157 | char *dst; |
| 4158 | int j, n; |
| 4159 | |
| 4160 | jp = getjob(argv[i], 0); |
| 4161 | /* |
| 4162 | * In jobs started under job control, we signal |
| 4163 | * entire process group by kill -PGRP_ID. |
| 4164 | * This happens, f.e., in interactive shell. |
| 4165 | * |
| 4166 | * Otherwise, we signal each child via |
| 4167 | * kill PID1 PID2 PID3. |
| 4168 | * Testcases: |
| 4169 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 4170 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 4171 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 4172 | */ |
| 4173 | n = jp->nprocs; /* can't be 0 (I hope) */ |
| 4174 | if (jp->jobctl) |
| 4175 | n = 1; |
| 4176 | dst = alloca(n * sizeof(int)*4); |
| 4177 | argv[i] = dst; |
| 4178 | for (j = 0; j < n; j++) { |
| 4179 | struct procstat *ps = &jp->ps[j]; |
| 4180 | /* Skip non-running and not-stopped members |
| 4181 | * (i.e. dead members) of the job |
| 4182 | */ |
| 4183 | if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status)) |
| 4184 | continue; |
| 4185 | /* |
| 4186 | * kill_main has matching code to expect |
| 4187 | * leading space. Needed to not confuse |
| 4188 | * negative pids with "kill -SIGNAL_NO" syntax |
| 4189 | */ |
| 4190 | dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid); |
| 4191 | } |
| 4192 | *dst = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4193 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4194 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4195 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4196 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4200 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4201 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4202 | struct procstat *ps; |
| 4203 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4204 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4205 | psend = jp->ps + jp->nprocs; |
| 4206 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 4207 | printf(" | %s", ps->ps_cmd); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4208 | newline_and_flush(stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4209 | flush_stdout_stderr(); |
| 4210 | } |
| 4211 | |
| 4212 | |
| 4213 | static int |
| 4214 | restartjob(struct job *jp, int mode) |
| 4215 | { |
| 4216 | struct procstat *ps; |
| 4217 | int i; |
| 4218 | int status; |
| 4219 | pid_t pgid; |
| 4220 | |
| 4221 | INT_OFF; |
| 4222 | if (jp->state == JOBDONE) |
| 4223 | goto out; |
| 4224 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4225 | pgid = jp->ps[0].ps_pid; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4226 | if (mode == FORK_FG) { |
| 4227 | get_tty_state(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4228 | xtcsetpgrp(ttyfd, pgid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4229 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4230 | killpg(pgid, SIGCONT); |
| 4231 | ps = jp->ps; |
| 4232 | i = jp->nprocs; |
| 4233 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4234 | if (WIFSTOPPED(ps->ps_status)) { |
| 4235 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4236 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 4237 | ps++; |
| 4238 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4239 | out: |
| 4240 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 4241 | INT_ON; |
| 4242 | return status; |
| 4243 | } |
| 4244 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4245 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4246 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4247 | { |
| 4248 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4249 | int mode; |
| 4250 | int retval; |
| 4251 | |
| 4252 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 4253 | nextopt(nullstr); |
| 4254 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4255 | do { |
| 4256 | jp = getjob(*argv, 1); |
| 4257 | if (mode == FORK_BG) { |
| 4258 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4259 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4260 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4261 | out1str(jp->ps[0].ps_cmd); |
| 4262 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4263 | retval = restartjob(jp, mode); |
| 4264 | } while (*argv && *++argv); |
| 4265 | return retval; |
| 4266 | } |
| 4267 | #endif |
| 4268 | |
| 4269 | static int |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4270 | sprint_status48(char *os, int status, int sigonly) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4271 | { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4272 | char *s = os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4273 | int st; |
| 4274 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4275 | if (!WIFEXITED(status)) { |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4276 | #if JOBS |
| 4277 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4278 | st = WSTOPSIG(status); |
| 4279 | else |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4280 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4281 | st = WTERMSIG(status); |
| 4282 | if (sigonly) { |
| 4283 | if (st == SIGINT || st == SIGPIPE) |
| 4284 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4285 | #if JOBS |
| 4286 | if (WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4287 | goto out; |
Johannes Schindelin | 9d4dc84 | 2017-07-14 22:25:58 +0200 | [diff] [blame] | 4288 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4289 | } |
| 4290 | st &= 0x7f; |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 4291 | s = stpncpy(s, strsignal(st), 32); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4292 | if (WCOREDUMP(status)) { |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4293 | s = stpcpy(s, " (core dumped)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } else if (!sigonly) { |
| 4296 | st = WEXITSTATUS(status); |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4297 | s += fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4298 | } |
| 4299 | out: |
Denys Vlasenko | 2bad3a3 | 2020-02-16 18:23:43 +0100 | [diff] [blame] | 4300 | return s - os; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4303 | #define DOWAIT_NONBLOCK 0 |
| 4304 | #define DOWAIT_BLOCK 1 |
| 4305 | #define DOWAIT_BLOCK_OR_SIG 2 |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4306 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4307 | # define DOWAIT_JOBSTATUS 0x10 /* OR this to get job's exitstatus instead of pid */ |
| 4308 | #endif |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4309 | |
| 4310 | static int |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4311 | waitproc(int block, int *status) |
| 4312 | { |
| 4313 | sigset_t oldmask; |
| 4314 | int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG; |
| 4315 | int err; |
| 4316 | |
| 4317 | #if JOBS |
| 4318 | if (doing_jobctl) |
| 4319 | flags |= WUNTRACED; |
| 4320 | #endif |
| 4321 | |
| 4322 | do { |
| 4323 | got_sigchld = 0; |
| 4324 | do |
| 4325 | err = waitpid(-1, status, flags); |
| 4326 | while (err < 0 && errno == EINTR); |
| 4327 | |
| 4328 | if (err || (err = -!block)) |
| 4329 | break; |
| 4330 | |
| 4331 | sigfillset(&oldmask); |
| 4332 | sigprocmask2(SIG_SETMASK, &oldmask); /* mask is updated */ |
| 4333 | while (!got_sigchld && !pending_sig) |
| 4334 | sigsuspend(&oldmask); |
| 4335 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
| 4336 | //simpler, but unsafe: a signal can set pending_sig after check, but before pause(): |
| 4337 | //while (!got_sigchld && !pending_sig) |
| 4338 | // pause(); |
| 4339 | |
| 4340 | } while (got_sigchld); |
| 4341 | |
| 4342 | return err; |
| 4343 | } |
| 4344 | |
| 4345 | static int |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4346 | waitone(int block, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4347 | { |
| 4348 | int pid; |
| 4349 | int status; |
| 4350 | struct job *jp; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4351 | struct job *thisjob = NULL; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4352 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4353 | bool want_jobexitstatus = (block & DOWAIT_JOBSTATUS); |
| 4354 | block = (block & ~DOWAIT_JOBSTATUS); |
| 4355 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4356 | |
Denys Vlasenko | b543bda | 2016-10-27 20:08:28 +0200 | [diff] [blame] | 4357 | TRACE(("dowait(0x%x) called\n", block)); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 4358 | |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4359 | /* It's wrong to call waitpid() outside of INT_OFF region: |
| 4360 | * signal can arrive just after syscall return and handler can |
| 4361 | * longjmp away, losing stop/exit notification processing. |
| 4362 | * Thus, for "jobs" builtin, and for waiting for a fg job, |
| 4363 | * we call waitpid() (blocking or non-blocking) inside INT_OFF. |
| 4364 | * |
| 4365 | * However, for "wait" builtin it is wrong to simply call waitpid() |
| 4366 | * in INT_OFF region: "wait" needs to wait for any running job |
| 4367 | * to change state, but should exit on any trap too. |
| 4368 | * In INT_OFF region, a signal just before syscall entry can set |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4369 | * pending_sig variables, but we can't check them, and we would |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4370 | * either enter a sleeping waitpid() (BUG), or need to busy-loop. |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4371 | * |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4372 | * 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] | 4373 | * which combines waitpid() and sigsuspend(). |
Denys Vlasenko | 8f7b024 | 2016-10-28 17:16:11 +0200 | [diff] [blame] | 4374 | * 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] | 4375 | * SIG_DFL handler does not wake sigsuspend(). |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4376 | */ |
| 4377 | INT_OFF; |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4378 | pid = waitproc(block, &status); |
| 4379 | TRACE(("wait returns pid %d, status=%d\n", pid, status)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4380 | if (pid <= 0) |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4381 | goto out; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4382 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4383 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4384 | int jobstate; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4385 | struct procstat *ps; |
| 4386 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4387 | if (jp->state == JOBDONE) |
| 4388 | continue; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4389 | jobstate = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4390 | ps = jp->ps; |
| 4391 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4392 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4393 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4394 | TRACE(("Job %d: changing status of proc %d " |
| 4395 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4396 | jobno(jp), pid, ps->ps_status, status)); |
| 4397 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4398 | thisjob = jp; |
| 4399 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4400 | if (ps->ps_status == -1) |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4401 | jobstate = JOBRUNNING; |
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 == JOBRUNNING) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4404 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4405 | if (WIFSTOPPED(ps->ps_status)) { |
| 4406 | jp->stopstatus = ps->ps_status; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4407 | jobstate = JOBSTOPPED; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4408 | } |
| 4409 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4410 | } while (++ps < psend); |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4411 | if (!thisjob) |
| 4412 | continue; |
| 4413 | |
| 4414 | /* Found the job where one of its processes changed its state. |
| 4415 | * Is there at least one live and running process in this job? */ |
| 4416 | if (jobstate != JOBRUNNING) { |
| 4417 | /* No. All live processes in the job are stopped |
| 4418 | * (JOBSTOPPED) or there are no live processes (JOBDONE) |
| 4419 | */ |
| 4420 | thisjob->changed = 1; |
| 4421 | if (thisjob->state != jobstate) { |
| 4422 | TRACE(("Job %d: changing state from %d to %d\n", |
| 4423 | jobno(thisjob), thisjob->state, jobstate)); |
| 4424 | thisjob->state = jobstate; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4425 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4426 | if (jobstate == JOBSTOPPED) |
| 4427 | set_curjob(thisjob, CUR_STOPPED); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4428 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4429 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4430 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4431 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4432 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4433 | /* The process wasn't found in job list */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4434 | out: |
| 4435 | INT_ON; |
| 4436 | |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4437 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4438 | if (want_jobexitstatus) { |
| 4439 | pid = -1; |
| 4440 | if (thisjob && thisjob->state == JOBDONE) |
| 4441 | pid = thisjob->ps[thisjob->nprocs - 1].ps_status; |
| 4442 | } |
| 4443 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4444 | if (thisjob && thisjob == job) { |
| 4445 | char s[48 + 1]; |
| 4446 | int len; |
| 4447 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4448 | len = sprint_status48(s, status, 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4449 | if (len) { |
| 4450 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4451 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4452 | out2str(s); |
| 4453 | } |
| 4454 | } |
| 4455 | return pid; |
| 4456 | } |
| 4457 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4458 | static int |
| 4459 | dowait(int block, struct job *jp) |
| 4460 | { |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4461 | smallint gotchld = *(volatile smallint *)&got_sigchld; |
| 4462 | int rpid; |
| 4463 | int pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4464 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4465 | if (jp && jp->state != JOBRUNNING) |
| 4466 | block = DOWAIT_NONBLOCK; |
| 4467 | |
| 4468 | if (block == DOWAIT_NONBLOCK && !gotchld) |
| 4469 | return 1; |
| 4470 | |
| 4471 | rpid = 1; |
| 4472 | |
| 4473 | do { |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4474 | pid = waitone(block, jp); |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4475 | rpid &= !!pid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4476 | |
Denys Vlasenko | 8d5f465 | 2020-09-29 16:44:46 +0200 | [diff] [blame] | 4477 | if (!pid || (jp && jp->state != JOBRUNNING)) |
| 4478 | block = DOWAIT_NONBLOCK; |
| 4479 | } while (pid >= 0); |
| 4480 | |
| 4481 | return rpid; |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4482 | } |
| 4483 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4484 | #if JOBS |
| 4485 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4486 | showjob(struct job *jp, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4487 | { |
| 4488 | struct procstat *ps; |
| 4489 | struct procstat *psend; |
| 4490 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4491 | int indent_col; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4492 | char s[16 + 16 + 48]; |
| 4493 | FILE *out = (mode & SHOW_STDERR ? stderr : stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4494 | |
| 4495 | ps = jp->ps; |
| 4496 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4497 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4498 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4499 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4500 | return; |
| 4501 | } |
| 4502 | |
| 4503 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4504 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4505 | |
| 4506 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4507 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4508 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4509 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4510 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4511 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4512 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4513 | |
| 4514 | psend = ps + jp->nprocs; |
| 4515 | |
| 4516 | if (jp->state == JOBRUNNING) { |
| 4517 | strcpy(s + col, "Running"); |
| 4518 | col += sizeof("Running") - 1; |
| 4519 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4520 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4521 | if (jp->state == JOBSTOPPED) |
| 4522 | status = jp->stopstatus; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4523 | col += sprint_status48(s + col, status, 0); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4524 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4525 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4526 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4527 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4528 | * or prints several "PID | <cmdN>" lines, |
| 4529 | * depending on SHOW_PIDS bit. |
| 4530 | * We do not print status of individual processes |
| 4531 | * between PID and <cmdN>. bash does it, but not very well: |
| 4532 | * first line shows overall job status, not process status, |
| 4533 | * making it impossible to know 1st process status. |
| 4534 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4535 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4536 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4537 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4538 | s[0] = '\0'; |
| 4539 | col = 33; |
| 4540 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4541 | 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] | 4542 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4543 | fprintf(out, "%s%*c%s%s", |
| 4544 | s, |
| 4545 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4546 | ps == jp->ps ? "" : "| ", |
| 4547 | ps->ps_cmd |
| 4548 | ); |
| 4549 | } while (++ps != psend); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4550 | newline_and_flush(out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4551 | |
| 4552 | jp->changed = 0; |
| 4553 | |
| 4554 | if (jp->state == JOBDONE) { |
| 4555 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4556 | freejob(jp); |
| 4557 | } |
| 4558 | } |
| 4559 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4560 | /* |
| 4561 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4562 | * statuses have changed since the last call to showjobs. |
| 4563 | */ |
| 4564 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4565 | showjobs(int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4566 | { |
| 4567 | struct job *jp; |
| 4568 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4569 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4570 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4571 | /* Handle all finished jobs */ |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4572 | dowait(DOWAIT_NONBLOCK, NULL); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4573 | |
| 4574 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4575 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4576 | showjob(jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4577 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4578 | } |
| 4579 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4580 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4581 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4582 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4583 | { |
| 4584 | int mode, m; |
| 4585 | |
| 4586 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4587 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4588 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4589 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4590 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4591 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | argv = argptr; |
| 4595 | if (*argv) { |
| 4596 | do |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4597 | showjob(getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4598 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4599 | } else { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4600 | showjobs(mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4601 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4602 | |
| 4603 | return 0; |
| 4604 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4605 | #endif /* JOBS */ |
| 4606 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4607 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4608 | static int |
| 4609 | getstatus(struct job *job) |
| 4610 | { |
| 4611 | int status; |
| 4612 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4613 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4614 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4615 | /* Fetch last member's status */ |
| 4616 | ps = job->ps + job->nprocs - 1; |
| 4617 | status = ps->ps_status; |
| 4618 | if (pipefail) { |
| 4619 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4620 | while (status == 0 && --ps >= job->ps) |
| 4621 | status = ps->ps_status; |
| 4622 | } |
| 4623 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4624 | retval = WEXITSTATUS(status); |
| 4625 | if (!WIFEXITED(status)) { |
| 4626 | #if JOBS |
| 4627 | retval = WSTOPSIG(status); |
| 4628 | if (!WIFSTOPPED(status)) |
| 4629 | #endif |
| 4630 | { |
| 4631 | /* XXX: limits number of signals */ |
| 4632 | retval = WTERMSIG(status); |
| 4633 | #if JOBS |
| 4634 | if (retval == SIGINT) |
| 4635 | job->sigint = 1; |
| 4636 | #endif |
| 4637 | } |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4638 | retval |= 128; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4639 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4640 | 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] | 4641 | jobno(job), job->nprocs, status, retval)); |
| 4642 | return retval; |
| 4643 | } |
| 4644 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4645 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4646 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4647 | { |
| 4648 | struct job *job; |
| 4649 | int retval; |
| 4650 | struct job *jp; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4651 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4652 | int status; |
| 4653 | char one = nextopt("n"); |
| 4654 | #else |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4655 | nextopt(nullstr); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4656 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4657 | retval = 0; |
| 4658 | |
| 4659 | argv = argptr; |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4660 | if (!argv[0]) { |
| 4661 | /* wait for all jobs / one job if -n */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4662 | for (;;) { |
| 4663 | jp = curjob; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4664 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4665 | if (one && !jp) |
| 4666 | /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */ |
| 4667 | retval = 127; |
| 4668 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4669 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4670 | if (!jp) /* no running procs */ |
| 4671 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4672 | if (jp->state == JOBRUNNING) |
| 4673 | break; |
| 4674 | jp->waited = 1; |
| 4675 | jp = jp->prev_job; |
| 4676 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4677 | /* man bash: |
| 4678 | * "When bash is waiting for an asynchronous command via |
| 4679 | * the wait builtin, the reception of a signal for which a trap |
| 4680 | * has been set will cause the wait builtin to return immediately |
| 4681 | * with an exit status greater than 128, immediately after which |
| 4682 | * the trap is executed." |
Denys Vlasenko | 6918811 | 2016-10-27 20:18:18 +0200 | [diff] [blame] | 4683 | */ |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4684 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4685 | status = dowait(DOWAIT_BLOCK_OR_SIG | DOWAIT_JOBSTATUS, NULL); |
| 4686 | #else |
Denys Vlasenko | 458c1f2 | 2016-10-27 23:51:19 +0200 | [diff] [blame] | 4687 | dowait(DOWAIT_BLOCK_OR_SIG, NULL); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4688 | #endif |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4689 | /* if child sends us a signal *and immediately exits*, |
| 4690 | * dowait() returns pid > 0. Check this case, |
| 4691 | * not "if (dowait() < 0)"! |
| 4692 | */ |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4693 | if (pending_sig) |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4694 | goto sigout; |
Ron Yorston | e48559e | 2019-03-31 09:27:09 +0100 | [diff] [blame] | 4695 | #if BASH_WAIT_N |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4696 | if (one) { |
| 4697 | /* wait -n waits for one _job_, not one _process_. |
| 4698 | * date; sleep 3 & sleep 2 | sleep 1 & wait -n; date |
| 4699 | * should wait for 2 seconds. Not 1 or 3. |
| 4700 | */ |
| 4701 | if (status != -1 && !WIFSTOPPED(status)) { |
| 4702 | retval = WEXITSTATUS(status); |
| 4703 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4704 | retval = 128 | WTERMSIG(status); |
Denys Vlasenko | 966f087 | 2019-03-27 15:51:42 +0100 | [diff] [blame] | 4705 | goto ret; |
| 4706 | } |
| 4707 | } |
| 4708 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | retval = 127; |
| 4713 | do { |
| 4714 | if (**argv != '%') { |
| 4715 | pid_t pid = number(*argv); |
| 4716 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4717 | while (1) { |
| 4718 | if (!job) |
| 4719 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4720 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4721 | break; |
| 4722 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4723 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4724 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4725 | job = getjob(*argv, 0); |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4726 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4727 | /* loop until process terminated or stopped */ |
Denys Vlasenko | 91e11eb | 2020-09-29 20:35:55 +0200 | [diff] [blame] | 4728 | dowait(DOWAIT_BLOCK_OR_SIG, job); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 4729 | if (pending_sig) |
| 4730 | goto sigout; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4731 | job->waited = 1; |
| 4732 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4733 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4734 | } while (*++argv); |
| 4735 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4736 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4737 | return retval; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4738 | sigout: |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 4739 | retval = 128 | pending_sig; |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 4740 | return retval; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | static struct job * |
| 4744 | growjobtab(void) |
| 4745 | { |
| 4746 | size_t len; |
| 4747 | ptrdiff_t offset; |
| 4748 | struct job *jp, *jq; |
| 4749 | |
| 4750 | len = njobs * sizeof(*jp); |
| 4751 | jq = jobtab; |
| 4752 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4753 | |
| 4754 | offset = (char *)jp - (char *)jq; |
| 4755 | if (offset) { |
| 4756 | /* Relocate pointers */ |
| 4757 | size_t l = len; |
| 4758 | |
| 4759 | jq = (struct job *)((char *)jq + l); |
| 4760 | while (l) { |
| 4761 | l -= sizeof(*jp); |
| 4762 | jq--; |
| 4763 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4764 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4765 | if (joff(jp)->ps == &jq->ps0) |
| 4766 | jmove(joff(jp)->ps); |
| 4767 | if (joff(jp)->prev_job) |
| 4768 | jmove(joff(jp)->prev_job); |
| 4769 | } |
| 4770 | if (curjob) |
| 4771 | jmove(curjob); |
| 4772 | #undef joff |
| 4773 | #undef jmove |
| 4774 | } |
| 4775 | |
| 4776 | njobs += 4; |
| 4777 | jobtab = jp; |
| 4778 | jp = (struct job *)((char *)jp + len); |
| 4779 | jq = jp + 3; |
| 4780 | do { |
| 4781 | jq->used = 0; |
| 4782 | } while (--jq >= jp); |
| 4783 | return jp; |
| 4784 | } |
| 4785 | |
| 4786 | /* |
| 4787 | * Return a new job structure. |
| 4788 | * Called with interrupts off. |
| 4789 | */ |
| 4790 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4791 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4792 | { |
| 4793 | int i; |
| 4794 | struct job *jp; |
| 4795 | |
| 4796 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4797 | if (--i < 0) { |
| 4798 | jp = growjobtab(); |
| 4799 | break; |
| 4800 | } |
| 4801 | if (jp->used == 0) |
| 4802 | break; |
| 4803 | if (jp->state != JOBDONE || !jp->waited) |
| 4804 | continue; |
| 4805 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4806 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4807 | continue; |
| 4808 | #endif |
| 4809 | freejob(jp); |
| 4810 | break; |
| 4811 | } |
| 4812 | memset(jp, 0, sizeof(*jp)); |
| 4813 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4814 | /* jp->jobctl is a bitfield. |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 4815 | * "jp->jobctl |= doing_jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4816 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4817 | jp->jobctl = 1; |
| 4818 | #endif |
| 4819 | jp->prev_job = curjob; |
| 4820 | curjob = jp; |
| 4821 | jp->used = 1; |
| 4822 | jp->ps = &jp->ps0; |
| 4823 | if (nprocs > 1) { |
| 4824 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4825 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4826 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4827 | jobno(jp))); |
| 4828 | return jp; |
| 4829 | } |
| 4830 | |
| 4831 | #if JOBS |
| 4832 | /* |
| 4833 | * Return a string identifying a command (to be printed by the |
| 4834 | * jobs command). |
| 4835 | */ |
| 4836 | static char *cmdnextc; |
| 4837 | |
| 4838 | static void |
| 4839 | cmdputs(const char *s) |
| 4840 | { |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 4841 | static const char vstype[VSTYPE + 1][3] ALIGN1 = { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4842 | "", "}", "-", "+", "?", "=", |
| 4843 | "%", "%%", "#", "##" |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 4844 | IF_BASH_SUBSTR(, ":") |
| 4845 | IF_BASH_PATTERN_SUBST(, "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4846 | }; |
| 4847 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4848 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4849 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4850 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4851 | unsigned char c; |
| 4852 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4853 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4854 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4855 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4856 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4857 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4858 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4859 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4860 | switch (c) { |
| 4861 | case CTLESC: |
| 4862 | c = *p++; |
| 4863 | break; |
| 4864 | case CTLVAR: |
| 4865 | subtype = *p++; |
| 4866 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4867 | str = "${#"; |
| 4868 | else |
| 4869 | str = "${"; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 4870 | goto dostr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4871 | case CTLENDVAR: |
Denys Vlasenko | 3f4847b | 2020-02-16 19:06:42 +0100 | [diff] [blame] | 4872 | str = "\"}"; |
| 4873 | str += !(quoted & 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4874 | quoted >>= 1; |
| 4875 | subtype = 0; |
| 4876 | goto dostr; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 4877 | #if BASH_PROCESS_SUBST |
| 4878 | case CTLBACKQ: |
| 4879 | c = '$'; |
| 4880 | str = "(...)"; |
| 4881 | break; |
| 4882 | case CTLTOPROC: |
| 4883 | c = '>'; |
| 4884 | str = "(...)"; |
| 4885 | break; |
| 4886 | case CTLFROMPROC: |
| 4887 | c = '<'; |
| 4888 | str = "(...)"; |
| 4889 | break; |
| 4890 | #else |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4891 | case CTLBACKQ: |
| 4892 | str = "$(...)"; |
| 4893 | goto dostr; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 4894 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4895 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4896 | case CTLARI: |
| 4897 | str = "$(("; |
| 4898 | goto dostr; |
| 4899 | case CTLENDARI: |
| 4900 | str = "))"; |
| 4901 | goto dostr; |
| 4902 | #endif |
| 4903 | case CTLQUOTEMARK: |
| 4904 | quoted ^= 1; |
| 4905 | c = '"'; |
| 4906 | break; |
| 4907 | case '=': |
| 4908 | if (subtype == 0) |
| 4909 | break; |
| 4910 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4911 | quoted <<= 1; |
| 4912 | str = vstype[subtype & VSTYPE]; |
| 4913 | if (subtype & VSNUL) |
| 4914 | c = ':'; |
| 4915 | else |
| 4916 | goto checkstr; |
| 4917 | break; |
| 4918 | case '\'': |
| 4919 | case '\\': |
| 4920 | case '"': |
| 4921 | case '$': |
| 4922 | /* These can only happen inside quotes */ |
| 4923 | cc[0] = c; |
| 4924 | str = cc; |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 4925 | //FIXME: |
| 4926 | // $ true $$ & |
| 4927 | // $ <cr> |
| 4928 | // [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] | 4929 | c = '\\'; |
| 4930 | break; |
| 4931 | default: |
| 4932 | break; |
| 4933 | } |
| 4934 | USTPUTC(c, nextc); |
| 4935 | checkstr: |
| 4936 | if (!str) |
| 4937 | continue; |
| 4938 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4939 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4940 | USTPUTC(c, nextc); |
| 4941 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4942 | } /* while *p++ not NUL */ |
| 4943 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4944 | if (quoted & 1) { |
| 4945 | USTPUTC('"', nextc); |
| 4946 | } |
| 4947 | *nextc = 0; |
| 4948 | cmdnextc = nextc; |
| 4949 | } |
| 4950 | |
| 4951 | /* cmdtxt() and cmdlist() call each other */ |
| 4952 | static void cmdtxt(union node *n); |
| 4953 | |
| 4954 | static void |
| 4955 | cmdlist(union node *np, int sep) |
| 4956 | { |
| 4957 | for (; np; np = np->narg.next) { |
| 4958 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4959 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4960 | cmdtxt(np); |
| 4961 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4962 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | static void |
| 4967 | cmdtxt(union node *n) |
| 4968 | { |
| 4969 | union node *np; |
| 4970 | struct nodelist *lp; |
| 4971 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4972 | |
| 4973 | if (!n) |
| 4974 | return; |
| 4975 | switch (n->type) { |
| 4976 | default: |
| 4977 | #if DEBUG |
| 4978 | abort(); |
| 4979 | #endif |
| 4980 | case NPIPE: |
| 4981 | lp = n->npipe.cmdlist; |
| 4982 | for (;;) { |
| 4983 | cmdtxt(lp->n); |
| 4984 | lp = lp->next; |
| 4985 | if (!lp) |
| 4986 | break; |
| 4987 | cmdputs(" | "); |
| 4988 | } |
| 4989 | break; |
| 4990 | case NSEMI: |
| 4991 | p = "; "; |
| 4992 | goto binop; |
| 4993 | case NAND: |
| 4994 | p = " && "; |
| 4995 | goto binop; |
| 4996 | case NOR: |
| 4997 | p = " || "; |
| 4998 | binop: |
| 4999 | cmdtxt(n->nbinary.ch1); |
| 5000 | cmdputs(p); |
| 5001 | n = n->nbinary.ch2; |
| 5002 | goto donode; |
| 5003 | case NREDIR: |
| 5004 | case NBACKGND: |
| 5005 | n = n->nredir.n; |
| 5006 | goto donode; |
| 5007 | case NNOT: |
| 5008 | cmdputs("!"); |
| 5009 | n = n->nnot.com; |
| 5010 | donode: |
| 5011 | cmdtxt(n); |
| 5012 | break; |
| 5013 | case NIF: |
| 5014 | cmdputs("if "); |
| 5015 | cmdtxt(n->nif.test); |
| 5016 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5017 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 5018 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5019 | cmdputs("; else "); |
| 5020 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 5021 | } else { |
| 5022 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5023 | } |
| 5024 | p = "; fi"; |
| 5025 | goto dotail; |
| 5026 | case NSUBSHELL: |
| 5027 | cmdputs("("); |
| 5028 | n = n->nredir.n; |
| 5029 | p = ")"; |
| 5030 | goto dotail; |
| 5031 | case NWHILE: |
| 5032 | p = "while "; |
| 5033 | goto until; |
| 5034 | case NUNTIL: |
| 5035 | p = "until "; |
| 5036 | until: |
| 5037 | cmdputs(p); |
| 5038 | cmdtxt(n->nbinary.ch1); |
| 5039 | n = n->nbinary.ch2; |
| 5040 | p = "; done"; |
| 5041 | dodo: |
| 5042 | cmdputs("; do "); |
| 5043 | dotail: |
| 5044 | cmdtxt(n); |
| 5045 | goto dotail2; |
| 5046 | case NFOR: |
| 5047 | cmdputs("for "); |
| 5048 | cmdputs(n->nfor.var); |
| 5049 | cmdputs(" in "); |
| 5050 | cmdlist(n->nfor.args, 1); |
| 5051 | n = n->nfor.body; |
| 5052 | p = "; done"; |
| 5053 | goto dodo; |
| 5054 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 5055 | cmdputs(n->ndefun.text); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5056 | p = "() { ... }"; |
| 5057 | goto dotail2; |
| 5058 | case NCMD: |
| 5059 | cmdlist(n->ncmd.args, 1); |
| 5060 | cmdlist(n->ncmd.redirect, 0); |
| 5061 | break; |
| 5062 | case NARG: |
| 5063 | p = n->narg.text; |
| 5064 | dotail2: |
| 5065 | cmdputs(p); |
| 5066 | break; |
| 5067 | case NHERE: |
| 5068 | case NXHERE: |
| 5069 | p = "<<..."; |
| 5070 | goto dotail2; |
| 5071 | case NCASE: |
| 5072 | cmdputs("case "); |
| 5073 | cmdputs(n->ncase.expr->narg.text); |
| 5074 | cmdputs(" in "); |
| 5075 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 5076 | cmdtxt(np->nclist.pattern); |
| 5077 | cmdputs(") "); |
| 5078 | cmdtxt(np->nclist.body); |
| 5079 | cmdputs(";; "); |
| 5080 | } |
| 5081 | p = "esac"; |
| 5082 | goto dotail2; |
| 5083 | case NTO: |
| 5084 | p = ">"; |
| 5085 | goto redir; |
| 5086 | case NCLOBBER: |
| 5087 | p = ">|"; |
| 5088 | goto redir; |
| 5089 | case NAPPEND: |
| 5090 | p = ">>"; |
| 5091 | goto redir; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5092 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5093 | case NTO2: |
| 5094 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5095 | case NTOFD: |
| 5096 | p = ">&"; |
| 5097 | goto redir; |
| 5098 | case NFROM: |
| 5099 | p = "<"; |
| 5100 | goto redir; |
| 5101 | case NFROMFD: |
| 5102 | p = "<&"; |
| 5103 | goto redir; |
| 5104 | case NFROMTO: |
| 5105 | p = "<>"; |
| 5106 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5107 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5108 | cmdputs(p); |
| 5109 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denys Vlasenko | d0fff91 | 2017-07-31 14:32:18 +0200 | [diff] [blame] | 5110 | if (n->ndup.dupfd >= 0) |
| 5111 | cmdputs(utoa(n->ndup.dupfd)); |
| 5112 | else |
| 5113 | cmdputs("-"); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5114 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5115 | } |
| 5116 | n = n->nfile.fname; |
| 5117 | goto donode; |
| 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | static char * |
| 5122 | commandtext(union node *n) |
| 5123 | { |
| 5124 | char *name; |
| 5125 | |
| 5126 | STARTSTACKSTR(cmdnextc); |
| 5127 | cmdtxt(n); |
| 5128 | name = stackblock(); |
Denys Vlasenko | 6a94cee | 2016-10-25 17:40:25 +0200 | [diff] [blame] | 5129 | TRACE(("commandtext: name %p, end %p\n", name, cmdnextc)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5130 | return ckstrdup(name); |
| 5131 | } |
| 5132 | #endif /* JOBS */ |
| 5133 | |
| 5134 | /* |
| 5135 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 5136 | * own process group. Jp is a job structure that the job is to be added to. |
| 5137 | * N is the command that will be evaluated by the child. Both jp and n may |
| 5138 | * be NULL. The mode parameter can be one of the following: |
| 5139 | * FORK_FG - Fork off a foreground process. |
| 5140 | * FORK_BG - Fork off a background process. |
| 5141 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 5142 | * process group even if job control is on. |
| 5143 | * |
| 5144 | * When job control is turned off, background processes have their standard |
| 5145 | * input redirected to /dev/null (except for the second and later processes |
| 5146 | * in a pipeline). |
| 5147 | * |
| 5148 | * Called with interrupts off. |
| 5149 | */ |
| 5150 | /* |
| 5151 | * Clear traps on a fork. |
| 5152 | */ |
| 5153 | static void |
| 5154 | clear_traps(void) |
| 5155 | { |
| 5156 | char **tp; |
| 5157 | |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5158 | INT_OFF; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 5159 | for (tp = trap; tp <= &trap[NTRAP_LAST]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5160 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5161 | if (trap_ptr == trap) |
| 5162 | free(*tp); |
| 5163 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5164 | *tp = NULL; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 5165 | if ((tp - trap) != 0 && (tp - trap) < NSIG) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5166 | setsignal(tp - trap); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5167 | } |
| 5168 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 5169 | may_have_traps = 0; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 5170 | INT_ON; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5171 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5172 | |
| 5173 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5174 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 5175 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5176 | /* Called after fork(), in child */ |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5177 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 5178 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5179 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5180 | { |
| 5181 | int oldlvl; |
| 5182 | |
| 5183 | TRACE(("Child shell %d\n", getpid())); |
| 5184 | oldlvl = shlvl; |
| 5185 | shlvl++; |
| 5186 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5187 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 5188 | * set to the values inherited by the shell from its parent". |
| 5189 | * Do we do it correctly? */ |
| 5190 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5191 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5192 | |
| 5193 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 5194 | && n && n->type == NCMD /* is it single cmd? */ |
| 5195 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5196 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5197 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 5198 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 5199 | ) { |
| 5200 | TRACE(("Trap hack\n")); |
| 5201 | /* Awful hack for `trap` or $(trap). |
| 5202 | * |
| 5203 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5204 | * contains an example where "trap" is executed in a subshell: |
| 5205 | * |
| 5206 | * save_traps=$(trap) |
| 5207 | * ... |
| 5208 | * eval "$save_traps" |
| 5209 | * |
| 5210 | * Standard does not say that "trap" in subshell shall print |
| 5211 | * parent shell's traps. It only says that its output |
| 5212 | * must have suitable form, but then, in the above example |
| 5213 | * (which is not supposed to be normative), it implies that. |
| 5214 | * |
| 5215 | * bash (and probably other shell) does implement it |
| 5216 | * (traps are reset to defaults, but "trap" still shows them), |
| 5217 | * but as a result, "trap" logic is hopelessly messed up: |
| 5218 | * |
| 5219 | * # trap |
| 5220 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5221 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5222 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5223 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5224 | * trap -- 'echo Ho' SIGWINCH |
| 5225 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5226 | * trap -- 'echo Ho' SIGWINCH |
| 5227 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5228 | * trap -- 'echo Ho' SIGWINCH |
| 5229 | * |
| 5230 | * The rules when to forget and when to not forget traps |
| 5231 | * get really complex and nonsensical. |
| 5232 | * |
| 5233 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5234 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5235 | /* Save trap handler strings for trap builtin to print */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 5236 | trap_ptr = xmemdup(trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 5237 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5238 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 5239 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5240 | #if JOBS |
| 5241 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5242 | doing_jobctl = 0; |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5243 | if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5244 | pid_t pgrp; |
| 5245 | |
| 5246 | if (jp->nprocs == 0) |
| 5247 | pgrp = getpid(); |
| 5248 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5249 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5250 | /* this can fail because we are doing it in the parent also */ |
| 5251 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5252 | if (mode == FORK_FG) |
| 5253 | xtcsetpgrp(ttyfd, pgrp); |
| 5254 | setsignal(SIGTSTP); |
| 5255 | setsignal(SIGTTOU); |
| 5256 | } else |
| 5257 | #endif |
| 5258 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5259 | /* man bash: "When job control is not in effect, |
| 5260 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5261 | ignoresig(SIGINT); |
| 5262 | ignoresig(SIGQUIT); |
| 5263 | if (jp->nprocs == 0) { |
| 5264 | close(0); |
| 5265 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5266 | ash_msg_and_raise_perror("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5267 | } |
| 5268 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 5269 | if (oldlvl == 0) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5270 | if (iflag) { /* why if iflag only? */ |
| 5271 | setsignal(SIGINT); |
| 5272 | setsignal(SIGTERM); |
| 5273 | } |
| 5274 | /* man bash: |
| 5275 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 5276 | * commands run by bash have signal handlers |
| 5277 | * set to the values inherited by the shell |
| 5278 | * from its parent". |
| 5279 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5280 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5281 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5282 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5283 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 5284 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5285 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5286 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 5287 | /* "jobs": we do not want to clear job list for it, |
| 5288 | * instead we remove only _its_ own_ job from job list. |
| 5289 | * This makes "jobs .... | cat" more useful. |
| 5290 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5291 | freejob(curjob); |
| 5292 | return; |
| 5293 | } |
| 5294 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5295 | for (jp = curjob; jp; jp = jp->prev_job) |
| 5296 | freejob(jp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 5299 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 5300 | #if !JOBS |
| 5301 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 5302 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5303 | static void |
| 5304 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 5305 | { |
| 5306 | TRACE(("In parent shell: child = %d\n", pid)); |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5307 | if (!jp) /* jp is NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5308 | return; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5309 | #if JOBS |
| 5310 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 5311 | int pgrp; |
| 5312 | |
| 5313 | if (jp->nprocs == 0) |
| 5314 | pgrp = pid; |
| 5315 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5316 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5317 | /* This can fail because we are doing it in the child also */ |
| 5318 | setpgid(pid, pgrp); |
| 5319 | } |
| 5320 | #endif |
| 5321 | if (mode == FORK_BG) { |
| 5322 | backgndpid = pid; /* set $! */ |
| 5323 | set_curjob(jp, CUR_RUNNING); |
| 5324 | } |
| 5325 | if (jp) { |
| 5326 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5327 | ps->ps_pid = pid; |
| 5328 | ps->ps_status = -1; |
| 5329 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5330 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5331 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 5332 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5333 | #endif |
| 5334 | } |
| 5335 | } |
| 5336 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5337 | /* jp and n are NULL when called by openhere() for heredoc support */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5338 | static int |
| 5339 | forkshell(struct job *jp, union node *n, int mode) |
| 5340 | { |
| 5341 | int pid; |
| 5342 | |
| 5343 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 5344 | pid = fork(); |
| 5345 | if (pid < 0) { |
| 5346 | TRACE(("Fork failed, errno=%d", errno)); |
| 5347 | if (jp) |
| 5348 | freejob(jp); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5349 | ash_msg_and_raise_perror("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5350 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5351 | if (pid == 0) { |
| 5352 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 5353 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5354 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5355 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5356 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5357 | return pid; |
| 5358 | } |
| 5359 | |
| 5360 | /* |
| 5361 | * Wait for job to finish. |
| 5362 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5363 | * Under job control we have the problem that while a child process |
| 5364 | * is running interrupts generated by the user are sent to the child |
| 5365 | * but not to the shell. This means that an infinite loop started by |
| 5366 | * an interactive user may be hard to kill. With job control turned off, |
| 5367 | * an interactive user may place an interactive program inside a loop. |
| 5368 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5369 | * these interrupts to also abort the loop. The approach we take here |
| 5370 | * is to have the shell ignore interrupt signals while waiting for a |
| 5371 | * foreground process to terminate, and then send itself an interrupt |
| 5372 | * signal if the child process was terminated by an interrupt signal. |
| 5373 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 5374 | * exit on interrupt; unless these processes terminate themselves by |
| 5375 | * sending a signal to themselves (instead of calling exit) they will |
| 5376 | * confuse this approach. |
| 5377 | * |
| 5378 | * Called with interrupts off. |
| 5379 | */ |
| 5380 | static int |
| 5381 | waitforjob(struct job *jp) |
| 5382 | { |
| 5383 | int st; |
| 5384 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 5385 | TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0)); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5386 | |
Denys Vlasenko | 47eb979 | 2020-02-18 15:37:43 +0100 | [diff] [blame] | 5387 | /* In non-interactive shells, we _can_ get |
| 5388 | * a keyboard signal here and be EINTRed, but we just loop |
| 5389 | * inside dowait(), waiting for command to complete. |
| 5390 | * |
| 5391 | * man bash: |
| 5392 | * "If bash is waiting for a command to complete and receives |
| 5393 | * a signal for which a trap has been set, the trap |
| 5394 | * will not be executed until the command completes." |
| 5395 | * |
| 5396 | * Reality is that even if trap is not set, bash |
| 5397 | * will not act on the signal until command completes. |
| 5398 | * Try this. sleep5intoff.c: |
| 5399 | * #include <signal.h> |
| 5400 | * #include <unistd.h> |
| 5401 | * int main() { |
| 5402 | * sigset_t set; |
| 5403 | * sigemptyset(&set); |
| 5404 | * sigaddset(&set, SIGINT); |
| 5405 | * sigaddset(&set, SIGQUIT); |
| 5406 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 5407 | * sleep(5); |
| 5408 | * return 0; |
| 5409 | * } |
| 5410 | * $ bash -c './sleep5intoff; echo hi' |
| 5411 | * ^C^C^C^C <--- pressing ^C once a second |
| 5412 | * $ _ |
| 5413 | * $ bash -c './sleep5intoff; echo hi' |
| 5414 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 5415 | * $ _ |
| 5416 | */ |
| 5417 | dowait(jp ? DOWAIT_BLOCK : DOWAIT_NONBLOCK, jp); |
| 5418 | if (!jp) |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 5419 | return exitstatus; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5420 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5421 | st = getstatus(jp); |
| 5422 | #if JOBS |
| 5423 | if (jp->jobctl) { |
| 5424 | xtcsetpgrp(ttyfd, rootpid); |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 5425 | restore_tty_if_stopped_or_signaled(jp); |
| 5426 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5427 | /* |
| 5428 | * This is truly gross. |
| 5429 | * If we're doing job control, then we did a TIOCSPGRP which |
| 5430 | * caused us (the shell) to no longer be in the controlling |
| 5431 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 5432 | * intuit from the subprocess exit status whether a SIGINT |
| 5433 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 5434 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 5435 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 5436 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5437 | } |
| 5438 | if (jp->state == JOBDONE) |
| 5439 | #endif |
| 5440 | freejob(jp); |
| 5441 | return st; |
| 5442 | } |
| 5443 | |
| 5444 | /* |
| 5445 | * return 1 if there are stopped jobs, otherwise 0 |
| 5446 | */ |
| 5447 | static int |
| 5448 | stoppedjobs(void) |
| 5449 | { |
| 5450 | struct job *jp; |
| 5451 | int retval; |
| 5452 | |
| 5453 | retval = 0; |
Ron Yorston | 50239a6 | 2021-09-12 11:21:08 +0100 | [diff] [blame] | 5454 | if (!iflag || job_warning) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 5455 | goto out; |
| 5456 | jp = curjob; |
| 5457 | if (jp && jp->state == JOBSTOPPED) { |
| 5458 | out2str("You have stopped jobs.\n"); |
| 5459 | job_warning = 2; |
| 5460 | retval++; |
| 5461 | } |
| 5462 | out: |
| 5463 | return retval; |
| 5464 | } |
| 5465 | |
| 5466 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 5467 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5468 | * Code for dealing with input/output redirection. |
| 5469 | */ |
| 5470 | |
Denys Vlasenko | 8d0e0cd | 2011-01-25 23:21:46 +0100 | [diff] [blame] | 5471 | #undef EMPTY |
| 5472 | #undef CLOSED |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5473 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5474 | #define CLOSED -1 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5475 | |
| 5476 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5477 | * Handle here documents. Normally we fork off a process to write the |
| 5478 | * data to a pipe. If the document is short, we can stuff the data in |
| 5479 | * the pipe without forking. |
| 5480 | */ |
| 5481 | /* openhere needs this forward reference */ |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5482 | static void expandhere(union node *arg); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5483 | static int |
| 5484 | openhere(union node *redir) |
| 5485 | { |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5486 | char *p; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5487 | int pip[2]; |
| 5488 | size_t len = 0; |
| 5489 | |
| 5490 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 5491 | ash_msg_and_raise_perror("can't create pipe"); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5492 | |
| 5493 | p = redir->nhere.doc->narg.text; |
| 5494 | if (redir->type == NXHERE) { |
| 5495 | expandhere(redir->nhere.doc); |
| 5496 | p = stackblock(); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5497 | } |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5498 | |
| 5499 | len = strlen(p); |
| 5500 | if (len <= PIPE_BUF) { |
| 5501 | xwrite(pip[1], p, len); |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5505 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5506 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5507 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5508 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5509 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5510 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5511 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5512 | signal(SIGPIPE, SIG_DFL); |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 5513 | xwrite(pip[1], p, len); |
Denys Vlasenko | db5546c | 2022-01-05 22:16:06 +0100 | [diff] [blame] | 5514 | _exit_SUCCESS(); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5515 | } |
| 5516 | out: |
| 5517 | close(pip[1]); |
| 5518 | return pip[0]; |
| 5519 | } |
| 5520 | |
| 5521 | static int |
| 5522 | openredirect(union node *redir) |
| 5523 | { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5524 | struct stat sb; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5525 | char *fname; |
| 5526 | int f; |
| 5527 | |
| 5528 | switch (redir->nfile.type) { |
Denys Vlasenko | 557482c | 2016-09-25 21:24:04 +0200 | [diff] [blame] | 5529 | /* Can't happen, our single caller does this itself */ |
| 5530 | // case NTOFD: |
| 5531 | // case NFROMFD: |
| 5532 | // return -1; |
| 5533 | case NHERE: |
| 5534 | case NXHERE: |
| 5535 | return openhere(redir); |
| 5536 | } |
| 5537 | |
| 5538 | /* For N[X]HERE, reading redir->nfile.expfname would touch beyond |
| 5539 | * allocated space. Do it only when we know it is safe. |
| 5540 | */ |
| 5541 | fname = redir->nfile.expfname; |
| 5542 | |
| 5543 | switch (redir->nfile.type) { |
| 5544 | default: |
| 5545 | #if DEBUG |
| 5546 | abort(); |
| 5547 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5548 | case NFROM: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5549 | f = open(fname, O_RDONLY); |
| 5550 | if (f < 0) |
| 5551 | goto eopen; |
| 5552 | break; |
| 5553 | case NFROMTO: |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5554 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5555 | if (f < 0) |
| 5556 | goto ecreate; |
| 5557 | break; |
| 5558 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5559 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5560 | case NTO2: |
| 5561 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5562 | /* Take care of noclobber mode. */ |
| 5563 | if (Cflag) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5564 | if (stat(fname, &sb) < 0) { |
| 5565 | f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 5566 | if (f < 0) |
| 5567 | goto ecreate; |
| 5568 | } else if (!S_ISREG(sb.st_mode)) { |
| 5569 | f = open(fname, O_WRONLY, 0666); |
| 5570 | if (f < 0) |
| 5571 | goto ecreate; |
Denys Vlasenko | 355ec35 | 2018-04-02 13:34:57 +0200 | [diff] [blame] | 5572 | if (!fstat(f, &sb) && S_ISREG(sb.st_mode)) { |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5573 | close(f); |
| 5574 | errno = EEXIST; |
| 5575 | goto ecreate; |
| 5576 | } |
| 5577 | } else { |
| 5578 | errno = EEXIST; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5579 | goto ecreate; |
Denys Vlasenko | f1a5cb0 | 2017-07-25 17:47:48 +0200 | [diff] [blame] | 5580 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5581 | break; |
| 5582 | } |
| 5583 | /* FALLTHROUGH */ |
| 5584 | case NCLOBBER: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5585 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5586 | if (f < 0) |
| 5587 | goto ecreate; |
| 5588 | break; |
| 5589 | case NAPPEND: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5590 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5591 | if (f < 0) |
| 5592 | goto ecreate; |
| 5593 | break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | return f; |
| 5597 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5598 | 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] | 5599 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5600 | 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] | 5601 | } |
| 5602 | |
| 5603 | /* |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5604 | * Copy a file descriptor to be >= 10. Throws exception on error. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5605 | */ |
| 5606 | static int |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5607 | savefd(int from) |
| 5608 | { |
| 5609 | int newfd; |
| 5610 | int err; |
| 5611 | |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5612 | newfd = fcntl(from, F_DUPFD_CLOEXEC, 10); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5613 | err = newfd < 0 ? errno : 0; |
| 5614 | if (err != EBADF) { |
| 5615 | if (err) |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5616 | ash_msg_and_raise_perror("%d", from); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5617 | close(from); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5618 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5619 | close_on_exec_on(newfd); |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | return newfd; |
| 5623 | } |
| 5624 | static int |
| 5625 | dup2_or_raise(int from, int to) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5626 | { |
| 5627 | int newfd; |
| 5628 | |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5629 | newfd = (from != to) ? dup2(from, to) : to; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5630 | if (newfd < 0) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5631 | /* Happens when source fd is not open: try "echo >&99" */ |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 5632 | ash_msg_and_raise_perror("%d", from); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5633 | } |
| 5634 | return newfd; |
| 5635 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5636 | static int |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5637 | dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5638 | { |
| 5639 | int newfd; |
| 5640 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5641 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 5642 | if (newfd >= 0) { |
| 5643 | 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] | 5644 | close_on_exec_on(newfd); |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5645 | } else { /* newfd < 0 */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5646 | if (errno == EBUSY) |
| 5647 | goto repeat; |
| 5648 | if (errno == EINTR) |
| 5649 | goto repeat; |
| 5650 | } |
| 5651 | return newfd; |
| 5652 | } |
| 5653 | static int |
| 5654 | xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
| 5655 | { |
| 5656 | int newfd; |
| 5657 | repeat: |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5658 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5659 | if (newfd < 0) { |
| 5660 | if (errno == EBUSY) |
| 5661 | goto repeat; |
| 5662 | if (errno == EINTR) |
| 5663 | goto repeat; |
| 5664 | /* fd was not open? */ |
| 5665 | if (errno == EBADF) |
| 5666 | return fd; |
| 5667 | ash_msg_and_raise_perror("%d", newfd); |
| 5668 | } |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 5669 | if (F_DUPFD_CLOEXEC == F_DUPFD) |
| 5670 | close_on_exec_on(newfd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5671 | close(fd); |
| 5672 | return newfd; |
| 5673 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5674 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5675 | /* Struct def and variable are moved down to the first usage site */ |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5676 | struct squirrel { |
| 5677 | int orig_fd; |
| 5678 | int moved_to; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5679 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5680 | struct redirtab { |
| 5681 | struct redirtab *next; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5682 | int pair_count; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5683 | struct squirrel two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5684 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5685 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5686 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5687 | static void |
| 5688 | add_squirrel_closed(struct redirtab *sq, int fd) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5689 | { |
| 5690 | int i; |
| 5691 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5692 | if (!sq) |
| 5693 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5694 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5695 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5696 | /* If we collide with an already moved fd... */ |
| 5697 | if (fd == sq->two_fd[i].orig_fd) { |
| 5698 | /* Examples: |
| 5699 | * "echo 3>FILE 3>&- 3>FILE" |
| 5700 | * "echo 3>&- 3>FILE" |
| 5701 | * No need for last redirect to insert |
| 5702 | * another "need to close 3" indicator. |
| 5703 | */ |
| 5704 | TRACE(("redirect_fd %d: already moved or closed\n", fd)); |
| 5705 | return; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5706 | } |
| 5707 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5708 | TRACE(("redirect_fd %d: previous fd was closed\n", fd)); |
| 5709 | sq->two_fd[i].orig_fd = fd; |
| 5710 | sq->two_fd[i].moved_to = CLOSED; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 5713 | static int |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5714 | save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5715 | { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5716 | int i, new_fd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5717 | |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5718 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 5719 | avoid_fd = 9; |
| 5720 | |
| 5721 | #if JOBS |
| 5722 | if (fd == ttyfd) { |
| 5723 | /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */ |
| 5724 | ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd); |
| 5725 | TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd)); |
| 5726 | return 1; /* "we closed fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5727 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5728 | #endif |
| 5729 | /* Are we called from redirect(0)? E.g. redirect |
| 5730 | * in a forked child. No need to save fds, |
| 5731 | * we aren't going to use them anymore, ok to trash. |
| 5732 | */ |
| 5733 | if (!sq) |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5734 | return 0; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5735 | |
| 5736 | /* If this one of script's fds? */ |
| 5737 | if (fd != 0) { |
| 5738 | struct parsefile *pf = g_parsefile; |
| 5739 | while (pf) { |
| 5740 | /* We skip fd == 0 case because of the following: |
| 5741 | * $ ash # running ash interactively |
| 5742 | * $ . ./script.sh |
| 5743 | * and in script.sh: "exec 9>&0". |
| 5744 | * Even though top-level pf_fd _is_ 0, |
| 5745 | * it's still ok to use it: "read" builtin uses it, |
| 5746 | * why should we cripple "exec" builtin? |
| 5747 | */ |
| 5748 | if (fd == pf->pf_fd) { |
| 5749 | pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 5750 | return 1; /* "we closed fd" */ |
| 5751 | } |
| 5752 | pf = pf->prev; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5753 | } |
| 5754 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5755 | |
| 5756 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 5757 | |
| 5758 | /* First: do we collide with some already moved fds? */ |
| 5759 | for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5760 | /* If we collide with an already moved fd... */ |
| 5761 | if (fd == sq->two_fd[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 5762 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5763 | sq->two_fd[i].moved_to = new_fd; |
| 5764 | TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd)); |
| 5765 | if (new_fd < 0) /* what? */ |
| 5766 | xfunc_die(); |
| 5767 | return 0; /* "we did not close fd" */ |
| 5768 | } |
| 5769 | if (fd == sq->two_fd[i].orig_fd) { |
| 5770 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 5771 | TRACE(("redirect_fd %d: already moved\n", fd)); |
| 5772 | return 0; /* "we did not close fd" */ |
| 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | /* 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] | 5777 | new_fd = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5778 | TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd)); |
| 5779 | if (new_fd < 0) { |
| 5780 | if (errno != EBADF) |
| 5781 | xfunc_die(); |
| 5782 | /* new_fd = CLOSED; - already is -1 */ |
| 5783 | } |
| 5784 | sq->two_fd[i].moved_to = new_fd; |
| 5785 | sq->two_fd[i].orig_fd = fd; |
| 5786 | |
| 5787 | /* if we move stderr, let "set -x" code know */ |
| 5788 | if (fd == preverrout_fd) |
| 5789 | preverrout_fd = new_fd; |
| 5790 | |
| 5791 | return 0; /* "we did not close fd" */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5794 | static int |
| 5795 | internally_opened_fd(int fd, struct redirtab *sq) |
| 5796 | { |
| 5797 | int i; |
| 5798 | #if JOBS |
| 5799 | if (fd == ttyfd) |
| 5800 | return 1; |
| 5801 | #endif |
| 5802 | /* If this one of script's fds? */ |
| 5803 | if (fd != 0) { |
| 5804 | struct parsefile *pf = g_parsefile; |
| 5805 | while (pf) { |
| 5806 | if (fd == pf->pf_fd) |
| 5807 | return 1; |
| 5808 | pf = pf->prev; |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) { |
| 5813 | if (fd == sq->two_fd[i].moved_to) |
| 5814 | return 1; |
| 5815 | } |
| 5816 | return 0; |
| 5817 | } |
| 5818 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5819 | /* |
| 5820 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5821 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5822 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5823 | */ |
| 5824 | /* flags passed to redirect */ |
| 5825 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5826 | static void |
| 5827 | redirect(union node *redir, int flags) |
| 5828 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5829 | struct redirtab *sv; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5830 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5831 | if (!redir) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5832 | return; |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5833 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5834 | sv = NULL; |
| 5835 | INT_OFF; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5836 | if (flags & REDIR_PUSH) |
| 5837 | sv = redirlist; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5838 | do { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5839 | int fd; |
| 5840 | int newfd; |
| 5841 | int close_fd; |
| 5842 | int closed; |
| 5843 | |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5844 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5845 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5846 | //bb_error_msg("doing %d > %d", fd, newfd); |
| 5847 | newfd = redir->ndup.dupfd; |
| 5848 | close_fd = -1; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5849 | } else { |
| 5850 | newfd = openredirect(redir); /* always >= 0 */ |
| 5851 | if (fd == newfd) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5852 | /* open() gave us precisely the fd we wanted. |
| 5853 | * This means that this fd was not busy |
| 5854 | * (not opened to anywhere). |
| 5855 | * Remember to close it on restore: |
| 5856 | */ |
| 5857 | add_squirrel_closed(sv, fd); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5858 | continue; |
| 5859 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5860 | close_fd = newfd; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5861 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5862 | |
| 5863 | if (fd == newfd) |
| 5864 | continue; |
| 5865 | |
| 5866 | /* if "N>FILE": move newfd to fd */ |
| 5867 | /* if "N>&M": dup newfd to fd */ |
| 5868 | /* if "N>&-": close fd (newfd is -1) */ |
| 5869 | |
| 5870 | IF_BASH_REDIR_OUTPUT(redirect_more:) |
| 5871 | |
| 5872 | closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv); |
| 5873 | if (newfd == -1) { |
| 5874 | /* "N>&-" means "close me" */ |
| 5875 | if (!closed) { |
| 5876 | /* ^^^ optimization: saving may already |
| 5877 | * have closed it. If not... */ |
| 5878 | close(fd); |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5879 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5880 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 5881 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 5882 | if (internally_opened_fd(newfd, sv)) { |
| 5883 | errno = EBADF; |
| 5884 | ash_msg_and_raise_perror("%d", newfd); |
| 5885 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 5886 | dup2_or_raise(newfd, fd); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5887 | if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */ |
| 5888 | close(close_fd); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 5889 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5890 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5891 | /* ">&FILE". we already redirected to 1, now copy 1 to 2 */ |
| 5892 | fd = 2; |
| 5893 | newfd = 1; |
| 5894 | close_fd = -1; |
| 5895 | goto redirect_more; |
| 5896 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5897 | #endif |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5898 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5899 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5900 | INT_ON; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5901 | |
| 5902 | //dash:#define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5903 | #define REDIR_SAVEFD2 0 |
| 5904 | // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test |
| 5905 | // triggers for pure REDIR_PUSH too. Thus, this is done almost always, |
| 5906 | // not only for calls with flags containing REDIR_SAVEFD2. |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5907 | // We do this unconditionally (see save_fd_on_redirect()). |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 5908 | //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5909 | // preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5912 | static int |
| 5913 | redirectsafe(union node *redir, int flags) |
| 5914 | { |
| 5915 | int err; |
| 5916 | volatile int saveint; |
| 5917 | struct jmploc *volatile savehandler = exception_handler; |
| 5918 | struct jmploc jmploc; |
| 5919 | |
| 5920 | SAVE_INT(saveint); |
| 5921 | /* "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] | 5922 | err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */ |
Denys Vlasenko | 170f93e | 2017-07-29 18:54:53 +0200 | [diff] [blame] | 5923 | if (!err) { |
| 5924 | exception_handler = &jmploc; |
| 5925 | redirect(redir, flags); |
| 5926 | } |
| 5927 | exception_handler = savehandler; |
| 5928 | if (err && exception_type != EXERROR) |
| 5929 | longjmp(exception_handler->loc, 1); |
| 5930 | RESTORE_INT(saveint); |
| 5931 | return err; |
| 5932 | } |
| 5933 | |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 5934 | #if BASH_PROCESS_SUBST |
| 5935 | static void |
| 5936 | pushfd(int fd) |
| 5937 | { |
| 5938 | struct redirtab *sv; |
| 5939 | |
| 5940 | sv = ckzalloc(sizeof(*sv) + sizeof(sv->two_fd[0])); |
| 5941 | sv->pair_count = 1; |
| 5942 | sv->two_fd[0].orig_fd = fd; |
| 5943 | sv->two_fd[0].moved_to = CLOSED; |
| 5944 | sv->next = redirlist; |
| 5945 | redirlist = sv; |
| 5946 | } |
| 5947 | #endif |
| 5948 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 5949 | static struct redirtab* |
| 5950 | pushredir(union node *redir) |
| 5951 | { |
| 5952 | struct redirtab *sv; |
| 5953 | int i; |
| 5954 | |
| 5955 | if (!redir) |
| 5956 | return redirlist; |
| 5957 | |
| 5958 | i = 0; |
| 5959 | do { |
| 5960 | i++; |
| 5961 | #if BASH_REDIR_OUTPUT |
| 5962 | if (redir->nfile.type == NTO2) |
| 5963 | i++; |
| 5964 | #endif |
| 5965 | redir = redir->nfile.next; |
| 5966 | } while (redir); |
| 5967 | |
| 5968 | sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0])); |
| 5969 | sv->pair_count = i; |
| 5970 | while (--i >= 0) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5971 | 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] | 5972 | sv->next = redirlist; |
| 5973 | redirlist = sv; |
| 5974 | return sv->next; |
| 5975 | } |
| 5976 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5977 | /* |
| 5978 | * Undo the effects of the last redirection. |
| 5979 | */ |
| 5980 | static void |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5981 | popredir(int drop) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5982 | { |
| 5983 | struct redirtab *rp; |
| 5984 | int i; |
| 5985 | |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 5986 | if (redirlist == NULL) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5987 | return; |
| 5988 | INT_OFF; |
| 5989 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5990 | for (i = 0; i < rp->pair_count; i++) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5991 | int fd = rp->two_fd[i].orig_fd; |
| 5992 | int copy = rp->two_fd[i].moved_to; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5993 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5994 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5995 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5996 | continue; |
| 5997 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5998 | if (copy != EMPTY) { |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 5999 | if (!drop) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 6000 | /*close(fd);*/ |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 6001 | dup2_or_raise(copy, fd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 6002 | } |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 6003 | close(copy); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 6004 | } |
| 6005 | } |
| 6006 | redirlist = rp->next; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 6007 | free(rp); |
| 6008 | INT_ON; |
| 6009 | } |
| 6010 | |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 6011 | static void |
| 6012 | unwindredir(struct redirtab *stop) |
| 6013 | { |
| 6014 | while (redirlist != stop) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 6015 | popredir(/*drop:*/ 0); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 6016 | } |
| 6017 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 6018 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6019 | /* ============ Routines to expand arguments to commands |
| 6020 | * |
| 6021 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 6022 | */ |
| 6023 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6024 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 6025 | static arith_t |
| 6026 | ash_arith(const char *s) |
| 6027 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6028 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 6029 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 6030 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6031 | math_state.lookupvar = lookupvar; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 6032 | math_state.setvar = setvar0; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6033 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 6034 | |
| 6035 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6036 | result = arith(&math_state, s); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6037 | if (math_state.errmsg) |
| 6038 | ash_msg_and_raise_error(math_state.errmsg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 6039 | INT_ON; |
| 6040 | |
| 6041 | return result; |
| 6042 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 6043 | #endif |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 6044 | #if BASH_SUBSTR |
| 6045 | # if ENABLE_FEATURE_SH_MATH |
| 6046 | static int substr_atoi(const char *s) |
| 6047 | { |
| 6048 | arith_t t = ash_arith(s); |
| 6049 | if (sizeof(t) > sizeof(int)) { |
| 6050 | /* clamp very large or very large negative nums for ${v:N:M}: |
| 6051 | * else "${v:0:0x100000001}" would work as "${v:0:1}" |
| 6052 | */ |
| 6053 | if (t > INT_MAX) |
| 6054 | t = INT_MAX; |
| 6055 | if (t < INT_MIN) |
| 6056 | t = INT_MIN; |
| 6057 | } |
| 6058 | return t; |
| 6059 | } |
| 6060 | # else |
| 6061 | # define substr_atoi(s) number(s) |
| 6062 | # endif |
| 6063 | #endif |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 6064 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6065 | /* |
| 6066 | * expandarg flags |
| 6067 | */ |
| 6068 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 6069 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 6070 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 6071 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
Denys Vlasenko | db74c6c | 2016-10-24 21:12:33 +0200 | [diff] [blame] | 6072 | /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt" |
| 6073 | * POSIX says for this case: |
| 6074 | * Pathname expansion shall not be performed on the word by a |
| 6075 | * non-interactive shell; an interactive shell may perform it, but shall |
| 6076 | * do so only when the expansion would result in one word. |
| 6077 | * Currently, our code complies to the above rule by never globbing |
| 6078 | * redirection filenames. |
| 6079 | * Bash performs globbing, unless it is non-interactive and in POSIX mode. |
| 6080 | * (this means that on a typical Linux distro, bash almost always |
| 6081 | * performs globbing, and thus diverges from what we do). |
| 6082 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6083 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6084 | #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */ |
| 6085 | #define EXP_WORD 0x40 /* expand word in parameter expansion */ |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 6086 | #define EXP_QUOTED 0x100 /* expand word in double quotes */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6087 | #define EXP_KEEPNUL 0x200 /* do not skip NUL characters */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6088 | #define EXP_DISCARD 0x400 /* discard result of expansion */ |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6089 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6090 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6091 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6092 | */ |
| 6093 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 6094 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6095 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 6096 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 6097 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6098 | /* Add CTLESC when necessary. */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6099 | #define QUOTES_ESC (EXP_FULL | EXP_CASE) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6100 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6101 | /* |
| 6102 | * Structure specifying which parts of the string should be searched |
| 6103 | * for IFS characters. |
| 6104 | */ |
| 6105 | struct ifsregion { |
| 6106 | struct ifsregion *next; /* next region in list */ |
| 6107 | int begoff; /* offset of start of region */ |
| 6108 | int endoff; /* offset of end of region */ |
| 6109 | int nulonly; /* search for nul bytes only */ |
| 6110 | }; |
| 6111 | |
| 6112 | struct arglist { |
| 6113 | struct strlist *list; |
| 6114 | struct strlist **lastp; |
| 6115 | }; |
| 6116 | |
| 6117 | /* output of current string */ |
| 6118 | static char *expdest; |
| 6119 | /* list of back quote expressions */ |
| 6120 | static struct nodelist *argbackq; |
| 6121 | /* first struct in list of ifs regions */ |
| 6122 | static struct ifsregion ifsfirst; |
| 6123 | /* last struct in list */ |
| 6124 | static struct ifsregion *ifslastp; |
| 6125 | /* holds expanded arg list */ |
| 6126 | static struct arglist exparg; |
| 6127 | |
| 6128 | /* |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6129 | * Break the argument string into pieces based upon IFS and add the |
| 6130 | * strings to the argument list. The regions of the string to be |
| 6131 | * searched for IFS characters have been stored by recordregion. |
| 6132 | */ |
| 6133 | static void |
| 6134 | ifsbreakup(char *string, struct arglist *arglist) |
| 6135 | { |
| 6136 | struct ifsregion *ifsp; |
| 6137 | struct strlist *sp; |
| 6138 | char *start; |
| 6139 | char *p; |
| 6140 | char *q; |
| 6141 | const char *ifs, *realifs; |
| 6142 | int ifsspc; |
| 6143 | int nulonly; |
| 6144 | |
| 6145 | start = string; |
| 6146 | if (ifslastp != NULL) { |
| 6147 | ifsspc = 0; |
| 6148 | nulonly = 0; |
| 6149 | realifs = ifsset() ? ifsval() : defifs; |
| 6150 | ifsp = &ifsfirst; |
| 6151 | do { |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6152 | int afternul; |
| 6153 | |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6154 | p = string + ifsp->begoff; |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6155 | afternul = nulonly; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6156 | nulonly = ifsp->nulonly; |
| 6157 | ifs = nulonly ? nullstr : realifs; |
| 6158 | ifsspc = 0; |
| 6159 | while (p < string + ifsp->endoff) { |
| 6160 | q = p; |
| 6161 | if ((unsigned char)*p == CTLESC) |
| 6162 | p++; |
| 6163 | if (!strchr(ifs, *p)) { |
| 6164 | p++; |
| 6165 | continue; |
| 6166 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6167 | if (!(afternul || nulonly)) |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6168 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6169 | /* Ignore IFS whitespace at start */ |
| 6170 | if (q == start && ifsspc) { |
| 6171 | p++; |
| 6172 | start = p; |
| 6173 | continue; |
| 6174 | } |
| 6175 | *q = '\0'; |
| 6176 | sp = stzalloc(sizeof(*sp)); |
| 6177 | sp->text = start; |
| 6178 | *arglist->lastp = sp; |
| 6179 | arglist->lastp = &sp->next; |
| 6180 | p++; |
| 6181 | if (!nulonly) { |
| 6182 | for (;;) { |
| 6183 | if (p >= string + ifsp->endoff) { |
| 6184 | break; |
| 6185 | } |
| 6186 | q = p; |
| 6187 | if ((unsigned char)*p == CTLESC) |
| 6188 | p++; |
| 6189 | if (strchr(ifs, *p) == NULL) { |
| 6190 | p = q; |
| 6191 | break; |
| 6192 | } |
| 6193 | if (strchr(defifs, *p) == NULL) { |
| 6194 | if (ifsspc) { |
| 6195 | p++; |
| 6196 | ifsspc = 0; |
| 6197 | } else { |
| 6198 | p = q; |
| 6199 | break; |
| 6200 | } |
| 6201 | } else |
| 6202 | p++; |
| 6203 | } |
| 6204 | } |
| 6205 | start = p; |
| 6206 | } /* while */ |
| 6207 | ifsp = ifsp->next; |
| 6208 | } while (ifsp != NULL); |
| 6209 | if (nulonly) |
| 6210 | goto add; |
| 6211 | } |
| 6212 | |
| 6213 | if (!*start) |
| 6214 | return; |
| 6215 | |
| 6216 | add: |
| 6217 | sp = stzalloc(sizeof(*sp)); |
| 6218 | sp->text = start; |
| 6219 | *arglist->lastp = sp; |
| 6220 | arglist->lastp = &sp->next; |
| 6221 | } |
| 6222 | |
| 6223 | static void |
| 6224 | ifsfree(void) |
| 6225 | { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6226 | struct ifsregion *p = ifsfirst.next; |
| 6227 | |
| 6228 | if (!p) |
| 6229 | goto out; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6230 | |
| 6231 | INT_OFF; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6232 | do { |
| 6233 | struct ifsregion *ifsp; |
| 6234 | ifsp = p->next; |
| 6235 | free(p); |
| 6236 | p = ifsp; |
| 6237 | } while (p); |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6238 | ifsfirst.next = NULL; |
| 6239 | INT_ON; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6240 | out: |
| 6241 | ifslastp = NULL; |
Denys Vlasenko | 455e422 | 2016-10-27 14:45:13 +0200 | [diff] [blame] | 6242 | } |
| 6243 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6244 | static size_t |
| 6245 | esclen(const char *start, const char *p) |
| 6246 | { |
| 6247 | size_t esc = 0; |
| 6248 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6249 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6250 | esc++; |
| 6251 | } |
| 6252 | return esc; |
| 6253 | } |
| 6254 | |
| 6255 | /* |
| 6256 | * Remove any CTLESC characters from a string. |
| 6257 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6258 | #if !BASH_PATTERN_SUBST |
| 6259 | #define rmescapes(str, flag, slash_position) \ |
| 6260 | rmescapes(str, flag) |
| 6261 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6262 | static char * |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6263 | rmescapes(char *str, int flag, int *slash_position) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6264 | { |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6265 | static const char qchars[] ALIGN1 = { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6266 | IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 6267 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6268 | char *p, *q, *r; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6269 | unsigned protect_against_glob; |
| 6270 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6271 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6272 | p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash_position)); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6273 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6274 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6275 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6276 | q = p; |
| 6277 | r = str; |
| 6278 | if (flag & RMESCAPE_ALLOC) { |
| 6279 | size_t len = p - str; |
| 6280 | size_t fulllen = len + strlen(p) + 1; |
| 6281 | |
| 6282 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6283 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6284 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 6285 | /* p and str may be invalidated by makestrspace */ |
| 6286 | str = (char *)stackblock() + strloc; |
| 6287 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6288 | } else if (flag & RMESCAPE_HEAP) { |
| 6289 | r = ckmalloc(fulllen); |
| 6290 | } else { |
| 6291 | r = stalloc(fulllen); |
| 6292 | } |
| 6293 | q = r; |
| 6294 | if (len > 0) { |
Denys Vlasenko | 5ace96a | 2017-07-23 21:46:02 +0200 | [diff] [blame] | 6295 | q = (char *)mempcpy(q, str, len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6296 | } |
| 6297 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6298 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6299 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6300 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6301 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6302 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6303 | // Note: protect_against_glob only affect whether |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6304 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6305 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6306 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6307 | continue; |
| 6308 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 6309 | if (*p == '\\') { |
| 6310 | /* naked back slash */ |
| 6311 | protect_against_glob = 0; |
| 6312 | goto copy; |
| 6313 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6314 | if ((unsigned char)*p == CTLESC) { |
| 6315 | p++; |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 6316 | #if DEBUG |
| 6317 | if (*p == '\0') |
| 6318 | ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)"); |
| 6319 | #endif |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6320 | if (protect_against_glob) { |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6321 | /* |
| 6322 | * We used to trust glob() and fnmatch() to eat |
| 6323 | * superfluous escapes (\z where z has no |
| 6324 | * special meaning anyway). But this causes |
| 6325 | * bugs such as string of one greek letter rho |
Denys Vlasenko | ed79a63 | 2017-07-05 19:20:43 +0200 | [diff] [blame] | 6326 | * (unicode-encoded as two bytes "cf,81") |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6327 | * getting encoded as "cf,CTLESC,81" |
| 6328 | * and here, converted to "cf,\,81" - |
| 6329 | * which does not go well with some flavors |
Denys Vlasenko | 92b8d9c | 2017-07-05 19:13:44 +0200 | [diff] [blame] | 6330 | * of fnmatch() in unicode locales |
| 6331 | * (for example, glibc <= 2.22). |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6332 | * |
| 6333 | * Lets add "\" only on the chars which need it. |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6334 | * Testcases for less obvious chars are shown. |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6335 | */ |
| 6336 | if (*p == '*' |
| 6337 | || *p == '?' |
| 6338 | || *p == '[' |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6339 | || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */ |
| 6340 | || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */ |
| 6341 | || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */ |
| 6342 | || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | 4142f01 | 2017-07-05 22:19:28 +0200 | [diff] [blame] | 6343 | /* Some libc support [^negate], that's why "^" also needs love */ |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 6344 | || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */ |
Denys Vlasenko | fda9faf | 2017-07-05 19:10:21 +0200 | [diff] [blame] | 6345 | ) { |
| 6346 | *q++ = '\\'; |
| 6347 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6348 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6349 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 6350 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6351 | else if (slash_position && p == str + *slash_position) { |
| 6352 | /* stop handling globbing */ |
| 6353 | globbing = 0; |
| 6354 | *slash_position = q - r; |
| 6355 | slash_position = NULL; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6356 | } |
| 6357 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6358 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6359 | copy: |
| 6360 | *q++ = *p++; |
| 6361 | } |
| 6362 | *q = '\0'; |
| 6363 | if (flag & RMESCAPE_GROW) { |
| 6364 | expdest = r; |
| 6365 | STADJUST(q - r + 1, expdest); |
| 6366 | } |
| 6367 | return r; |
| 6368 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6369 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 6370 | |
| 6371 | /* |
| 6372 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 6373 | * |
| 6374 | * Returns an stalloced string. |
| 6375 | */ |
| 6376 | static char * |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6377 | preglob(const char *pattern, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6378 | { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 6379 | return rmescapes((char *)pattern, flag | RMESCAPE_GLOB, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | /* |
| 6383 | * Put a string on the stack. |
| 6384 | */ |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6385 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6386 | memtodest(const char *p, size_t len, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6387 | { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6388 | int syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6389 | char *q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6390 | char *s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6391 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6392 | if (!len) |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6393 | return 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6394 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6395 | q = makestrspace(len * 2, expdest); |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6396 | s = q; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6397 | |
| 6398 | do { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6399 | unsigned char c = *p++; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6400 | if (c) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6401 | if (flags & QUOTES_ESC) { |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6402 | int n = SIT(c, syntax); |
| 6403 | if (n == CCTL |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6404 | || ((flags & EXP_QUOTED) && n == CBACK) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 6405 | ) { |
| 6406 | USTPUTC(CTLESC, q); |
| 6407 | } |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 6408 | } |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6409 | } else if (!(flags & EXP_KEEPNUL)) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6410 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6411 | USTPUTC(c, q); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6412 | } while (--len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6413 | |
| 6414 | expdest = q; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6415 | return q - s; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6418 | static size_t |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6419 | strtodest(const char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6420 | { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6421 | size_t len = strlen(p); |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6422 | memtodest(p, len, flags); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6423 | return len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | /* |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 6427 | * Our own itoa(). |
| 6428 | * cvtnum() is used even if math support is off (to prepare $? values and such). |
| 6429 | */ |
| 6430 | static int |
| 6431 | cvtnum(arith_t num, int flags) |
| 6432 | { |
| 6433 | /* 32-bit and wider ints require buffer size of bytes*3 (or less) */ |
| 6434 | /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */ |
| 6435 | int len = (sizeof(arith_t) >= 4) ? sizeof(arith_t) * 3 : sizeof(arith_t) * 3 + 2; |
| 6436 | char buf[len]; |
| 6437 | |
| 6438 | len = fmtstr(buf, len, ARITH_FMT, num); |
| 6439 | return memtodest(buf, len, flags); |
| 6440 | } |
| 6441 | |
| 6442 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6443 | * Record the fact that we have to scan this region of the |
| 6444 | * string for IFS characters. |
| 6445 | */ |
| 6446 | static void |
| 6447 | recordregion(int start, int end, int nulonly) |
| 6448 | { |
| 6449 | struct ifsregion *ifsp; |
| 6450 | |
| 6451 | if (ifslastp == NULL) { |
| 6452 | ifsp = &ifsfirst; |
| 6453 | } else { |
| 6454 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6455 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 6456 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6457 | ifslastp->next = ifsp; |
| 6458 | INT_ON; |
| 6459 | } |
| 6460 | ifslastp = ifsp; |
| 6461 | ifslastp->begoff = start; |
| 6462 | ifslastp->endoff = end; |
| 6463 | ifslastp->nulonly = nulonly; |
| 6464 | } |
| 6465 | |
| 6466 | static void |
| 6467 | removerecordregions(int endoff) |
| 6468 | { |
| 6469 | if (ifslastp == NULL) |
| 6470 | return; |
| 6471 | |
| 6472 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6473 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6474 | struct ifsregion *ifsp; |
| 6475 | INT_OFF; |
| 6476 | ifsp = ifsfirst.next->next; |
| 6477 | free(ifsfirst.next); |
| 6478 | ifsfirst.next = ifsp; |
| 6479 | INT_ON; |
| 6480 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6481 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6482 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6483 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6484 | ifslastp = &ifsfirst; |
| 6485 | ifsfirst.endoff = endoff; |
| 6486 | } |
| 6487 | return; |
| 6488 | } |
| 6489 | |
| 6490 | ifslastp = &ifsfirst; |
| 6491 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6492 | ifslastp = ifslastp->next; |
| 6493 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6494 | struct ifsregion *ifsp; |
| 6495 | INT_OFF; |
| 6496 | ifsp = ifslastp->next->next; |
| 6497 | free(ifslastp->next); |
| 6498 | ifslastp->next = ifsp; |
| 6499 | INT_ON; |
| 6500 | } |
| 6501 | if (ifslastp->endoff > endoff) |
| 6502 | ifslastp->endoff = endoff; |
| 6503 | } |
| 6504 | |
| 6505 | static char * |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6506 | exptilde(char *startp, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6507 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6508 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6509 | char *name; |
| 6510 | struct passwd *pw; |
| 6511 | const char *home; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6512 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6513 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6514 | p = startp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6515 | name = p + 1; |
| 6516 | |
| 6517 | while ((c = *++p) != '\0') { |
| 6518 | switch (c) { |
| 6519 | case CTLESC: |
| 6520 | return startp; |
| 6521 | case CTLQUOTEMARK: |
| 6522 | return startp; |
| 6523 | case ':': |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6524 | if (flag & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6525 | goto done; |
| 6526 | break; |
| 6527 | case '/': |
| 6528 | case CTLENDVAR: |
| 6529 | goto done; |
| 6530 | } |
| 6531 | } |
| 6532 | done: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6533 | if (flag & EXP_DISCARD) |
| 6534 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6535 | *p = '\0'; |
| 6536 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 6537 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6538 | } else { |
| 6539 | pw = getpwnam(name); |
Denys Vlasenko | 5fe20cf | 2022-03-01 10:08:59 +0100 | [diff] [blame] | 6540 | home = pw ? pw->pw_dir : NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6541 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6542 | *p = c; |
Denys Vlasenko | e880b1f | 2020-02-16 18:31:05 +0100 | [diff] [blame] | 6543 | if (!home) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6544 | goto lose; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6545 | strtodest(home, flag | EXP_QUOTED); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6546 | out: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6547 | return p; |
| 6548 | lose: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6549 | return startp; |
| 6550 | } |
| 6551 | |
| 6552 | /* |
| 6553 | * Execute a command inside back quotes. If it's a builtin command, we |
| 6554 | * want to save its output in a block obtained from malloc. Otherwise |
| 6555 | * we fork off a subprocess and get the output of the command via a pipe. |
| 6556 | * Should be called with interrupts off. |
| 6557 | */ |
| 6558 | struct backcmd { /* result of evalbackcmd */ |
| 6559 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6560 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 6561 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6562 | struct job *jp; /* job structure for command */ |
| 6563 | }; |
| 6564 | |
| 6565 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 6566 | /* flags in argument to evaltree */ |
| 6567 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 6568 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 6569 | static int evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6570 | |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6571 | /* An evaltree() which is known to never return. |
| 6572 | * Used to use an alias: |
| 6573 | * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__)); |
| 6574 | * but clang was reported to "transfer" noreturn-ness to evaltree() as well. |
| 6575 | */ |
| 6576 | static ALWAYS_INLINE NORETURN void |
| 6577 | evaltreenr(union node *n, int flags) |
| 6578 | { |
| 6579 | evaltree(n, flags); |
| 6580 | bb_unreachable(abort()); |
| 6581 | /* NOTREACHED */ |
| 6582 | } |
| 6583 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 6584 | static void FAST_FUNC |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6585 | evalbackcmd(union node *n, struct backcmd *result |
| 6586 | IF_BASH_PROCESS_SUBST(, int ctl)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6587 | { |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6588 | int pip[2]; |
| 6589 | struct job *jp; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6590 | #if BASH_PROCESS_SUBST |
| 6591 | /* determine end of pipe used by parent (ip) and child (ic) */ |
| 6592 | const int ip = (ctl == CTLTOPROC); |
| 6593 | const int ic = !(ctl == CTLTOPROC); |
| 6594 | #else |
| 6595 | const int ctl = CTLBACKQ; |
| 6596 | const int ip = 0; |
| 6597 | const int ic = 1; |
| 6598 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6599 | |
| 6600 | result->fd = -1; |
| 6601 | result->buf = NULL; |
| 6602 | result->nleft = 0; |
| 6603 | result->jp = NULL; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6604 | if (n == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6605 | goto out; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6606 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6607 | |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6608 | if (pipe(pip) < 0) |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 6609 | ash_msg_and_raise_perror("can't create pipe"); |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6610 | /* process substitution uses NULL job/node, like openhere() */ |
| 6611 | jp = (ctl == CTLBACKQ) ? makejob(/*n,*/ 1) : NULL; |
| 6612 | if (forkshell(jp, (ctl == CTLBACKQ) ? n : NULL, FORK_NOJOB) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6613 | /* child */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6614 | FORCE_INT_ON; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6615 | close(pip[ip]); |
| 6616 | /* ic is index of child end of pipe *and* fd to connect it to */ |
| 6617 | if (pip[ic] != ic) { |
| 6618 | /*close(ic);*/ |
| 6619 | dup2_or_raise(pip[ic], ic); |
| 6620 | close(pip[ic]); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6621 | } |
Denys Vlasenko | 960ca38 | 2016-10-25 18:12:15 +0200 | [diff] [blame] | 6622 | /* TODO: eflag clearing makes the following not abort: |
| 6623 | * ash -c 'set -e; z=$(false;echo foo); echo $z' |
| 6624 | * which is what bash does (unless it is in POSIX mode). |
| 6625 | * dash deleted "eflag = 0" line in the commit |
| 6626 | * Date: Mon, 28 Jun 2010 17:11:58 +1000 |
| 6627 | * [EVAL] Don't clear eflag in evalbackcmd |
| 6628 | * For now, preserve bash-like behavior, it seems to be somewhat more useful: |
| 6629 | */ |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6630 | eflag = 0; |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 6631 | ifsfree(); |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 6632 | evaltreenr(n, EV_EXIT); |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6633 | /* NOTREACHED */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6634 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 6635 | /* parent */ |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6636 | #if BASH_PROCESS_SUBST |
| 6637 | if (ctl != CTLBACKQ) { |
| 6638 | int fd = fcntl(pip[ip], F_DUPFD, 64); |
| 6639 | if (fd > 0) { |
| 6640 | close(pip[ip]); |
| 6641 | pip[ip] = fd; |
| 6642 | } |
| 6643 | pushfd(pip[ip]); |
| 6644 | } |
| 6645 | #endif |
| 6646 | close(pip[ic]); |
| 6647 | result->fd = pip[ip]; |
Denys Vlasenko | 579ad10 | 2016-10-25 21:10:20 +0200 | [diff] [blame] | 6648 | result->jp = jp; |
| 6649 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6650 | out: |
| 6651 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 6652 | result->fd, result->buf, result->nleft, result->jp)); |
| 6653 | } |
| 6654 | |
| 6655 | /* |
| 6656 | * Expand stuff in backwards quotes. |
| 6657 | */ |
| 6658 | static void |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6659 | expbackq(union node *cmd, int flag IF_BASH_PROCESS_SUBST(, int ctl)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6660 | { |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6661 | #if !BASH_PROCESS_SUBST |
| 6662 | const int ctl = CTLBACKQ; |
| 6663 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6664 | struct backcmd in; |
| 6665 | int i; |
| 6666 | char buf[128]; |
| 6667 | char *p; |
| 6668 | char *dest; |
| 6669 | int startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6670 | struct stackmark smark; |
| 6671 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6672 | if (flag & EXP_DISCARD) |
| 6673 | goto out; |
| 6674 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6675 | INT_OFF; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 6676 | startloc = expdest - (char *)stackblock(); |
| 6677 | pushstackmark(&smark, startloc); |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6678 | evalbackcmd(cmd, &in IF_BASH_PROCESS_SUBST(, ctl)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6679 | popstackmark(&smark); |
| 6680 | |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6681 | if (ctl != CTLBACKQ) { |
| 6682 | sprintf(buf, DEV_FD_PREFIX"%d", in.fd); |
| 6683 | strtodest(buf, BASESYNTAX); |
| 6684 | goto done; |
| 6685 | } |
| 6686 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6687 | p = in.buf; |
| 6688 | i = in.nleft; |
| 6689 | if (i == 0) |
| 6690 | goto read; |
| 6691 | for (;;) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 6692 | memtodest(p, i, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6693 | read: |
| 6694 | if (in.fd < 0) |
| 6695 | break; |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 6696 | i = nonblock_immune_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6697 | TRACE(("expbackq: read returns %d\n", i)); |
| 6698 | if (i <= 0) |
| 6699 | break; |
| 6700 | p = buf; |
| 6701 | } |
| 6702 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 6703 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6704 | if (in.fd >= 0) { |
| 6705 | close(in.fd); |
| 6706 | back_exitstatus = waitforjob(in.jp); |
| 6707 | } |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6708 | done: |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6709 | INT_ON; |
| 6710 | |
| 6711 | /* Eat all trailing newlines */ |
| 6712 | dest = expdest; |
Denys Vlasenko | 9ee5892 | 2020-02-17 10:24:32 +0100 | [diff] [blame] | 6713 | for (; dest > ((char *)stackblock() + startloc) && dest[-1] == '\n';) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6714 | STUNPUTC(dest); |
| 6715 | expdest = dest; |
| 6716 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6717 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6718 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6719 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 6720 | (int)((dest - (char *)stackblock()) - startloc), |
| 6721 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6722 | stackblock() + startloc)); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6723 | |
| 6724 | out: |
| 6725 | argbackq = argbackq->next; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6728 | /* expari needs it */ |
| 6729 | static char *argstr(char *p, int flag); |
| 6730 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6731 | #if ENABLE_FEATURE_SH_MATH |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6732 | /* |
| 6733 | * Expand arithmetic expression. Backup to start of expression, |
| 6734 | * evaluate, place result in (backed up) result, adjust string position. |
| 6735 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6736 | static char * |
| 6737 | expari(char *start, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6738 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6739 | struct stackmark sm; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6740 | int begoff; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6741 | int endoff; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6742 | int len; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6743 | arith_t result; |
| 6744 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6745 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6746 | p = stackblock(); |
| 6747 | begoff = expdest - p; |
| 6748 | p = argstr(start, flag & EXP_DISCARD); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6749 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6750 | if (flag & EXP_DISCARD) |
| 6751 | goto out; |
| 6752 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6753 | start = stackblock(); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6754 | endoff = expdest - start; |
| 6755 | start += begoff; |
| 6756 | STADJUST(start - expdest, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6757 | |
| 6758 | removerecordregions(begoff); |
| 6759 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6760 | if (flag & QUOTES_ESC) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6761 | rmescapes(start, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6762 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6763 | pushstackmark(&sm, endoff); |
| 6764 | result = ash_arith(start); |
| 6765 | popstackmark(&sm); |
| 6766 | |
| 6767 | len = cvtnum(result, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6768 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6769 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6770 | recordregion(begoff, begoff + len, 0); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6771 | |
| 6772 | out: |
| 6773 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6774 | } |
| 6775 | #endif |
| 6776 | |
| 6777 | /* argstr needs it */ |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 6778 | static char *evalvar(char *p, int flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6779 | |
| 6780 | /* |
| 6781 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 6782 | * characters to allow for further processing. Otherwise treat |
| 6783 | * $@ like $* since no splitting will be performed. |
| 6784 | */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6785 | static char * |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6786 | argstr(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6787 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6788 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6789 | '=', |
| 6790 | ':', |
| 6791 | CTLQUOTEMARK, |
| 6792 | CTLENDVAR, |
| 6793 | CTLESC, |
| 6794 | CTLVAR, |
| 6795 | CTLBACKQ, |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6796 | #if BASH_PROCESS_SUBST |
| 6797 | CTLTOPROC, |
| 6798 | CTLFROMPROC, |
| 6799 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6800 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6801 | CTLARI, |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6802 | CTLENDARI, |
| 6803 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6804 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6805 | }; |
| 6806 | const char *reject = spclchars; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6807 | int breakall = (flag & (EXP_WORD | EXP_QUOTED)) == EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6808 | int inquotes; |
| 6809 | size_t length; |
| 6810 | int startloc; |
| 6811 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6812 | reject += !!(flag & EXP_VARTILDE2); |
| 6813 | reject += flag & EXP_VARTILDE ? 0 : 2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6814 | inquotes = 0; |
| 6815 | length = 0; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6816 | if (flag & EXP_TILDE) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6817 | flag &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6818 | tilde: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6819 | if (*p == '~') |
| 6820 | p = exptilde(p, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6821 | } |
| 6822 | start: |
| 6823 | startloc = expdest - (char *)stackblock(); |
| 6824 | for (;;) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6825 | int end; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6826 | unsigned char c; |
| 6827 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6828 | length += strcspn(p + length, reject); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6829 | end = 0; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6830 | c = p[length]; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6831 | if (!(c & 0x80) |
| 6832 | IF_FEATURE_SH_MATH(|| c == CTLENDARI) |
| 6833 | || c == CTLENDVAR |
| 6834 | ) { |
| 6835 | /* |
| 6836 | * c == '=' || c == ':' || c == '\0' || |
| 6837 | * c == CTLENDARI || c == CTLENDVAR |
| 6838 | */ |
| 6839 | length++; |
| 6840 | /* c == '\0' || c == CTLENDARI || c == CTLENDVAR */ |
| 6841 | end = !!((c - 1) & 0x80); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6842 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6843 | if (length > 0 && !(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6844 | int newloc; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6845 | char *q; |
| 6846 | |
| 6847 | q = stnputs(p, length, expdest); |
| 6848 | q[-1] &= end - 1; |
| 6849 | expdest = q - (flag & EXP_WORD ? end : 0); |
| 6850 | newloc = q - (char *)stackblock() - end; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6851 | if (breakall && !inquotes && newloc > startloc) { |
| 6852 | recordregion(startloc, newloc, 0); |
| 6853 | } |
| 6854 | startloc = newloc; |
| 6855 | } |
| 6856 | p += length + 1; |
| 6857 | length = 0; |
| 6858 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6859 | if (end) |
| 6860 | break; |
| 6861 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6862 | switch (c) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6863 | case '=': |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6864 | flag |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6865 | reject++; |
| 6866 | /* fall through */ |
| 6867 | case ':': |
| 6868 | /* |
| 6869 | * sort of a hack - expand tildes in variable |
| 6870 | * assignments (after the first '=' and after ':'s). |
| 6871 | */ |
| 6872 | if (*--p == '~') { |
| 6873 | goto tilde; |
| 6874 | } |
| 6875 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6876 | case CTLQUOTEMARK: |
| 6877 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6878 | if (!inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) { |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6879 | p = evalvar(p + 1, flag | EXP_QUOTED) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6880 | goto start; |
| 6881 | } |
Denys Vlasenko | 9a95df9 | 2018-04-02 14:27:50 +0200 | [diff] [blame] | 6882 | inquotes ^= EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6883 | addquote: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6884 | if (flag & QUOTES_ESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6885 | p--; |
| 6886 | length++; |
| 6887 | startloc++; |
| 6888 | } |
| 6889 | break; |
| 6890 | case CTLESC: |
| 6891 | startloc++; |
| 6892 | length++; |
| 6893 | goto addquote; |
| 6894 | case CTLVAR: |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6895 | TRACE(("argstr: evalvar('%s')\n", p)); |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 6896 | p = evalvar(p, flag | inquotes); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6897 | TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock())); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6898 | goto start; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6899 | #if BASH_PROCESS_SUBST |
| 6900 | case CTLTOPROC: |
| 6901 | case CTLFROMPROC: |
| 6902 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6903 | case CTLBACKQ: |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 6904 | expbackq(argbackq->n, flag | inquotes IF_BASH_PROCESS_SUBST(, c)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6905 | goto start; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6906 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6907 | case CTLARI: |
| 6908 | p = expari(p, flag | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6909 | goto start; |
| 6910 | #endif |
| 6911 | } |
| 6912 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 6913 | return p - 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
| 6916 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6917 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6918 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6919 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6920 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6921 | char c; |
| 6922 | |
| 6923 | loc = startp; |
| 6924 | loc2 = rmesc; |
| 6925 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6926 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6927 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6928 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6929 | c = *loc2; |
| 6930 | if (zero) { |
| 6931 | *loc2 = '\0'; |
| 6932 | s = rmesc; |
| 6933 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6934 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6935 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6936 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6937 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6938 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6939 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6940 | loc++; |
| 6941 | loc++; |
| 6942 | loc2++; |
| 6943 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6944 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
| 6947 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6948 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6949 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6950 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6951 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6952 | int try2optimize = match_at_start; |
| 6953 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6954 | int esc = 0; |
| 6955 | char *loc; |
| 6956 | char *loc2; |
| 6957 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6958 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6959 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6960 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6961 | * Logic: |
| 6962 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6963 | * and on each iteration they go back two/one char until they reach the beginning. |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 6964 | * We try to match "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6965 | * If one of these matches, return pointer past last matched char in startp. |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6966 | */ |
| 6967 | /* TODO: document in what other circumstances we are called. */ |
| 6968 | |
| 6969 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6970 | int match; |
| 6971 | char c = *loc2; |
| 6972 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6973 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6974 | *loc2 = '\0'; |
| 6975 | s = rmesc; |
| 6976 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6977 | match = pmatch(pattern, s); |
| 6978 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6979 | *loc2 = c; |
| 6980 | if (match) |
| 6981 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6982 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6983 | if (try2optimize) { |
| 6984 | /* Maybe we can optimize this: |
| 6985 | * if pattern ends with unescaped *, we can avoid checking |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6986 | * shorter strings: if "foo*" doesn't match "raw_value_of_v", |
| 6987 | * it won't match truncated "raw_value_of_" strings too. |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6988 | */ |
| 6989 | unsigned plen = strlen(pattern); |
| 6990 | /* Does it end with "*"? */ |
| 6991 | if (plen != 0 && pattern[--plen] == '*') { |
| 6992 | /* "xxxx*" is not escaped */ |
| 6993 | /* "xxx\*" is escaped */ |
| 6994 | /* "xx\\*" is not escaped */ |
| 6995 | /* "x\\\*" is escaped */ |
| 6996 | int slashes = 0; |
| 6997 | while (plen != 0 && pattern[--plen] == '\\') |
| 6998 | slashes++; |
| 6999 | if (!(slashes & 1)) |
| 7000 | break; /* ends with unescaped "*" */ |
| 7001 | } |
| 7002 | try2optimize = 0; |
| 7003 | } |
| 7004 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7005 | loc--; |
| 7006 | if (quotes) { |
| 7007 | if (--esc < 0) { |
| 7008 | esc = esclen(startp, loc); |
| 7009 | } |
| 7010 | if (esc % 2) { |
| 7011 | esc--; |
| 7012 | loc--; |
| 7013 | } |
| 7014 | } |
| 7015 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 7016 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7017 | } |
| 7018 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7019 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7020 | static void |
| 7021 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 7022 | { |
| 7023 | const char *msg; |
| 7024 | const char *tail; |
| 7025 | |
| 7026 | tail = nullstr; |
| 7027 | msg = "parameter not set"; |
| 7028 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7029 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7030 | if (varflags & VSNUL) |
| 7031 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7032 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7033 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7034 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7035 | } |
Denys Vlasenko | 1c54552 | 2022-08-02 11:13:44 +0200 | [diff] [blame] | 7036 | ifsfree(); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 7037 | 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] | 7038 | } |
| 7039 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7040 | static char * |
| 7041 | subevalvar(char *start, char *str, int strloc, |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7042 | int startloc, int varflags, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7043 | { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7044 | int subtype = varflags & VSTYPE; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7045 | int quotes = flag & QUOTES_ESC; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7046 | char *startp; |
| 7047 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7048 | char *rmesc, *rmescend; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7049 | long amount; |
| 7050 | int resetloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7051 | int argstr_flags; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7052 | IF_BASH_PATTERN_SUBST(int workloc;) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7053 | IF_BASH_PATTERN_SUBST(int slash_pos;) |
| 7054 | IF_BASH_PATTERN_SUBST(char *repl;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7055 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7056 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7057 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7058 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7059 | //bb_error_msg("subevalvar(start:'%s',str:'%s',strloc:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 7060 | // start, str, strloc, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7061 | |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7062 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 7063 | /* For "${v/pattern/repl}", we must find the delimiter _before_ |
| 7064 | * argstr() call expands possible variable references in pattern: |
| 7065 | * think about "v=a; a=a/; echo ${v/$a/r}" case. |
| 7066 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7067 | repl = NULL; |
| 7068 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
| 7069 | /* Find '/' and replace with NUL */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7070 | repl = start; |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 7071 | /* The pattern can't be empty. |
| 7072 | * IOW: if the first char after "${v//" is a slash, |
| 7073 | * it does not terminate the pattern - it's the first char of the pattern: |
| 7074 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 7075 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 7076 | */ |
| 7077 | if (*repl == '/') |
| 7078 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7079 | for (;;) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7080 | if (*repl == '\0') { |
| 7081 | repl = NULL; |
| 7082 | break; |
| 7083 | } |
| 7084 | if (*repl == '/') { |
| 7085 | *repl = '\0'; |
| 7086 | break; |
| 7087 | } |
Sören Tempel | fa52ac9 | 2022-02-28 08:36:50 +0100 | [diff] [blame] | 7088 | if ((unsigned char)*repl == CTLENDVAR) { /* ${v/pattern} (no trailing /, no repl) */ |
| 7089 | repl = NULL; |
| 7090 | break; |
| 7091 | } |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 7092 | /* 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] | 7093 | if ((unsigned char)*repl == CTLESC && repl[1]) |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7094 | repl++; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7095 | repl++; |
| 7096 | } |
| 7097 | } |
| 7098 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7099 | argstr_flags = (flag & EXP_DISCARD) | EXP_TILDE; |
| 7100 | if (!str |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7101 | #if BASH_SUBSTR |
| 7102 | && subtype != VSSUBSTR |
| 7103 | #endif |
| 7104 | ) { |
| 7105 | /* EXP_CASE keeps CTLESC's */ |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7106 | argstr_flags |= EXP_CASE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7107 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7108 | p = argstr(start, argstr_flags); |
| 7109 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7110 | //bb_error_msg("str0:'%s'", (char *)stackblock() + strloc); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7111 | #if BASH_PATTERN_SUBST |
| 7112 | slash_pos = -1; |
| 7113 | if (repl) { |
| 7114 | slash_pos = expdest - ((char *)stackblock() + strloc); |
Denys Vlasenko | 883cdb7 | 2021-01-09 08:27:37 +0100 | [diff] [blame] | 7115 | if (!(flag & EXP_DISCARD)) |
| 7116 | STPUTC('/', expdest); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 7117 | //bb_error_msg("repl+1:'%s'", repl + 1); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7118 | 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] | 7119 | *repl = '/'; |
| 7120 | } |
| 7121 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7122 | if (flag & EXP_DISCARD) |
| 7123 | return p; |
| 7124 | |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7125 | startp = (char *)stackblock() + startloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7126 | //bb_error_msg("str1:'%s'", (char *)stackblock() + strloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7127 | |
| 7128 | switch (subtype) { |
| 7129 | case VSASSIGN: |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7130 | setvar0(str, startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7131 | |
| 7132 | loc = startp; |
| 7133 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7134 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7135 | case VSQUESTION: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7136 | varunset(start, str, startp, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7137 | /* NOTREACHED */ |
| 7138 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7139 | #if BASH_SUBSTR |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7140 | case VSSUBSTR: { |
| 7141 | int pos, len, orig_len; |
| 7142 | char *colon; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7143 | char *vstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7144 | |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7145 | loc = vstr = stackblock() + strloc; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7146 | |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7147 | /* Read POS in ${var:POS:LEN} */ |
| 7148 | colon = strchr(loc, ':'); |
| 7149 | if (colon) *colon = '\0'; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7150 | pos = substr_atoi(loc); |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7151 | if (colon) *colon = ':'; |
| 7152 | |
| 7153 | /* Read LEN in ${var:POS:LEN} */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7154 | len = vstr - startp - 1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7155 | /* *loc != '\0', guaranteed by parser */ |
| 7156 | if (quotes) { |
| 7157 | char *ptr; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7158 | /* Adjust the length by the number of escapes */ |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7159 | for (ptr = startp; ptr < (vstr - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7160 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7161 | len--; |
| 7162 | ptr++; |
| 7163 | } |
| 7164 | } |
| 7165 | } |
| 7166 | orig_len = len; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7167 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7168 | /* ${var::LEN} */ |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7169 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7170 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7171 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7172 | len = orig_len; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7173 | while (*loc && *loc != ':') |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7174 | loc++; |
Denys Vlasenko | baa41c7 | 2018-01-10 13:22:25 +0100 | [diff] [blame] | 7175 | if (*loc++ == ':') |
| 7176 | len = substr_atoi(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7177 | } |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7178 | if (pos < 0) { |
| 7179 | /* ${VAR:$((-n)):l} starts n chars from the end */ |
| 7180 | pos = orig_len + pos; |
| 7181 | } |
| 7182 | if ((unsigned)pos >= orig_len) { |
| 7183 | /* apart from obvious ${VAR:999999:l}, |
| 7184 | * covers ${VAR:$((-9999999)):l} - result is "" |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7185 | * (bash compat) |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 7186 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7187 | pos = 0; |
| 7188 | len = 0; |
| 7189 | } |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7190 | if (len < 0) { |
| 7191 | /* ${VAR:N:-M} sets LEN to strlen()-M */ |
| 7192 | len = (orig_len - pos) + len; |
| 7193 | } |
| 7194 | if ((unsigned)len > (orig_len - pos)) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7195 | len = orig_len - pos; |
| 7196 | |
Alin Mr | 21e8dbf | 2021-07-28 11:40:01 +0300 | [diff] [blame] | 7197 | if (!quotes) { |
Denys Vlasenko | 7750b5a | 2022-03-01 09:56:54 +0100 | [diff] [blame] | 7198 | /* want: loc = mempcpy(startp, startp + pos, len) |
| 7199 | * but it does not allow overlapping arguments */ |
| 7200 | loc = startp; |
| 7201 | while (--len >= 0) { |
| 7202 | *loc = loc[pos]; |
| 7203 | loc++; |
| 7204 | } |
Alin Mr | 21e8dbf | 2021-07-28 11:40:01 +0300 | [diff] [blame] | 7205 | } else { |
| 7206 | for (vstr = startp; pos != 0; pos--) { |
| 7207 | if ((unsigned char)*vstr == CTLESC) |
| 7208 | vstr++; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7209 | vstr++; |
Alin Mr | 21e8dbf | 2021-07-28 11:40:01 +0300 | [diff] [blame] | 7210 | } |
| 7211 | for (loc = startp; len != 0; len--) { |
| 7212 | if ((unsigned char)*vstr == CTLESC) |
| 7213 | *loc++ = *vstr++; |
Denys Vlasenko | 7f19848 | 2020-02-24 09:57:08 +0100 | [diff] [blame] | 7214 | *loc++ = *vstr++; |
Alin Mr | 21e8dbf | 2021-07-28 11:40:01 +0300 | [diff] [blame] | 7215 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7216 | } |
| 7217 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7218 | goto out; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7219 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7220 | #endif /* BASH_SUBSTR */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7221 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7222 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7223 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7224 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7225 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7226 | repl = NULL; |
| 7227 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7228 | /* We'll comeback here if we grow the stack while handling |
| 7229 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 7230 | * stack will need rebasing, and we'll need to remove our work |
| 7231 | * areas each time |
| 7232 | */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7233 | restart: |
| 7234 | #endif |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7235 | |
| 7236 | amount = expdest - ((char *)stackblock() + resetloc); |
| 7237 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7238 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7239 | |
| 7240 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7241 | rmescend = (char *)stackblock() + strloc; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7242 | //bb_error_msg("str7:'%s'", rmescend); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7243 | if (quotes) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7244 | //TODO: how to handle slash_pos here if string changes (shortens?) |
| 7245 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7246 | if (rmesc != startp) { |
| 7247 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7248 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7249 | } |
| 7250 | } |
| 7251 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 7252 | str = (char *)stackblock() + strloc; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7253 | /* |
| 7254 | * Example: v='a\bc'; echo ${v/\\b/_\\_\z_} |
| 7255 | * The result is a_\_z_c (not a\_\_z_c)! |
| 7256 | * |
| 7257 | * The search pattern and replace string treat backslashes differently! |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7258 | * "&slash_pos" causes rmescapes() to work differently on the pattern |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7259 | * and string. It's only used on the first call. |
| 7260 | */ |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7261 | //bb_error_msg("str8:'%s' slash_pos:%d", str, slash_pos); |
| 7262 | rmescapes(str, RMESCAPE_GLOB, |
| 7263 | repl ? NULL : (slash_pos < 0 ? NULL : &slash_pos) |
| 7264 | ); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7265 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7266 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 7267 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7268 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7269 | size_t no_meta_len, first_escaped; |
Denys Vlasenko | 826360f | 2017-07-17 17:49:11 +0200 | [diff] [blame] | 7270 | int len; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7271 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7272 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 7273 | if (!repl) { |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7274 | //bb_error_msg("str9:'%s' slash_pos:%d", str, slash_pos); |
Denys Vlasenko | d1df1a7 | 2018-01-09 17:25:58 +0100 | [diff] [blame] | 7275 | repl = nullstr; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7276 | if (slash_pos >= 0) { |
| 7277 | repl = str + slash_pos; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7278 | *repl++ = '\0'; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7279 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7280 | } |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 7281 | //bb_error_msg("str:'%s' repl:'%s'", str, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7282 | |
| 7283 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7284 | if (str[0] == '\0') |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7285 | goto out1; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7286 | |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7287 | first_escaped = (str[0] == '\\' && str[1]); |
| 7288 | /* "first_escaped" trick allows to treat e.g. "\*no_glob_chars" |
| 7289 | * as literal too (as it is semi-common, and easy to accomodate |
| 7290 | * by just using str + 1). |
| 7291 | */ |
| 7292 | no_meta_len = strpbrk(str + first_escaped * 2, "*?[\\") ? 0 : strlen(str); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7293 | len = 0; |
| 7294 | idx = startp; |
| 7295 | end = str - 1; |
Denys Vlasenko | 2b7c1aa | 2021-01-09 08:46:54 +0100 | [diff] [blame] | 7296 | while (idx <= end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7297 | try_to_match: |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7298 | if (no_meta_len == 0) { |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7299 | /* pattern has meta chars, have to glob */ |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7300 | loc = scanright(idx, rmesc, rmescend, str, quotes, /*match_at_start:*/ 1); |
| 7301 | } else { |
| 7302 | /* Testcase for very slow replace (performs about 22k replaces): |
| 7303 | * x=:::::::::::::::::::::: |
| 7304 | * x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;echo ${#x} |
| 7305 | * echo "${x//:/|}" |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7306 | * To test "first_escaped" logic, replace : with *. |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7307 | */ |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7308 | if (strncmp(rmesc, str + first_escaped, no_meta_len - first_escaped) != 0) |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7309 | goto no_match; |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7310 | loc = idx; |
Denys Vlasenko | 53d45c9 | 2021-07-25 21:54:14 +0200 | [diff] [blame] | 7311 | if (!quotes) { |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7312 | loc += no_meta_len - first_escaped; |
Denys Vlasenko | 53d45c9 | 2021-07-25 21:54:14 +0200 | [diff] [blame] | 7313 | } else { |
Denys Vlasenko | 5aaeb55 | 2021-10-09 03:32:20 +0200 | [diff] [blame] | 7314 | size_t n = no_meta_len - first_escaped; |
Denys Vlasenko | 53d45c9 | 2021-07-25 21:54:14 +0200 | [diff] [blame] | 7315 | do { |
| 7316 | if ((unsigned char)*loc == CTLESC) |
| 7317 | loc++; |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7318 | loc++; |
Denys Vlasenko | 53d45c9 | 2021-07-25 21:54:14 +0200 | [diff] [blame] | 7319 | } while (--n != 0); |
| 7320 | } |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7321 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7322 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7323 | if (!loc) { |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7324 | char *restart_detect; |
| 7325 | no_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7326 | /* No match, advance */ |
Denys Vlasenko | 1310d7b | 2021-07-22 18:15:59 +0200 | [diff] [blame] | 7327 | restart_detect = stackblock(); |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7328 | skip_matching: |
Denys Vlasenko | 2b7c1aa | 2021-01-09 08:46:54 +0100 | [diff] [blame] | 7329 | if (idx >= end) |
| 7330 | break; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7331 | STPUTC(*idx, expdest); |
Denys Vlasenko | daa66ed | 2022-08-02 12:41:18 +0200 | [diff] [blame] | 7332 | if (stackblock() != restart_detect) |
| 7333 | goto restart; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7334 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7335 | idx++; |
| 7336 | len++; |
| 7337 | STPUTC(*idx, expdest); |
Denys Vlasenko | daa66ed | 2022-08-02 12:41:18 +0200 | [diff] [blame] | 7338 | if (stackblock() != restart_detect) |
| 7339 | goto restart; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7340 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7341 | idx++; |
| 7342 | len++; |
| 7343 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7344 | /* continue; - prone to quadratic behavior, smarter code: */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7345 | if (str[0] == '*') { |
| 7346 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 7347 | * it would never match "ong_string" etc, no point in trying. |
| 7348 | */ |
| 7349 | goto skip_matching; |
| 7350 | } |
| 7351 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
| 7354 | if (subtype == VSREPLACEALL) { |
| 7355 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 7356 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7357 | idx++; |
| 7358 | idx++; |
| 7359 | rmesc++; |
| 7360 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7361 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7362 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 7363 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7364 | |
Sören Tempel | 7c2a3bd | 2022-08-02 18:23:32 +0200 | [diff] [blame] | 7365 | /* The STPUTC invocations above may resize and move the |
| 7366 | * stack via realloc(3). Since repl is a pointer into the |
| 7367 | * stack, we need to reconstruct it relative to stackblock(). |
| 7368 | */ |
| 7369 | if (slash_pos >= 0) |
| 7370 | repl = (char *)stackblock() + strloc + slash_pos + 1; |
| 7371 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7372 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7373 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7374 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7375 | if (quotes && *loc == '\\') { |
| 7376 | STPUTC(CTLESC, expdest); |
| 7377 | len++; |
| 7378 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7379 | STPUTC(*loc, expdest); |
| 7380 | if (stackblock() != restart_detect) |
| 7381 | goto restart; |
| 7382 | len++; |
| 7383 | } |
| 7384 | |
| 7385 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 7386 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7387 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7388 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7389 | STPUTC(*idx, expdest); |
| 7390 | if (stackblock() != restart_detect) |
| 7391 | goto restart; |
| 7392 | len++; |
| 7393 | idx++; |
| 7394 | } |
| 7395 | break; |
| 7396 | } |
| 7397 | } |
| 7398 | |
| 7399 | /* We've put the replaced text into a buffer at workloc, now |
| 7400 | * move it to the right place and adjust the stack. |
| 7401 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7402 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7403 | startp = (char *)stackblock() + startloc; |
| 7404 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 7405 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7406 | loc = startp + len; |
| 7407 | goto out; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7408 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7409 | #endif /* BASH_PATTERN_SUBST */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7410 | |
| 7411 | subtype -= VSTRIMRIGHT; |
| 7412 | #if DEBUG |
| 7413 | if (subtype < 0 || subtype > 7) |
| 7414 | abort(); |
| 7415 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 7416 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7417 | zero = subtype >> 1; |
| 7418 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 7419 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 7420 | |
| 7421 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 7422 | if (loc) { |
| 7423 | if (zero) { |
| 7424 | memmove(startp, loc, str - loc); |
| 7425 | loc = startp + (str - loc) - 1; |
| 7426 | } |
| 7427 | *loc = '\0'; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7428 | } else |
| 7429 | loc = str - 1; |
| 7430 | |
| 7431 | out: |
| 7432 | amount = loc - expdest; |
| 7433 | STADJUST(amount, expdest); |
Cristian Ionescu-Idbohrn | ddfdf68 | 2020-11-18 10:41:14 +0100 | [diff] [blame] | 7434 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7435 | out1: |
Cristian Ionescu-Idbohrn | ddfdf68 | 2020-11-18 10:41:14 +0100 | [diff] [blame] | 7436 | #endif |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7437 | /* Remove any recorded regions beyond start of variable */ |
| 7438 | removerecordregions(startloc); |
| 7439 | |
| 7440 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | /* |
| 7444 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7445 | * name parameter (examples): |
| 7446 | * ash -c 'echo $1' name:'1=' |
| 7447 | * ash -c 'echo $qwe' name:'qwe=' |
| 7448 | * ash -c 'echo $$' name:'$=' |
| 7449 | * ash -c 'echo ${$}' name:'$=' |
| 7450 | * ash -c 'echo ${$##q}' name:'$=q' |
| 7451 | * ash -c 'echo ${#$}' name:'$=' |
| 7452 | * note: examples with bad shell syntax: |
| 7453 | * ash -c 'echo ${#$1}' name:'$=1' |
| 7454 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7455 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 7456 | static NOINLINE ssize_t |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7457 | varvalue(char *name, int varflags, int flags, int quoted) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7458 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7459 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7460 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7461 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7462 | ssize_t len = 0; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7463 | int sep; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7464 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7465 | int discard = (subtype == VSPLUS || subtype == VSLENGTH) | (flags & EXP_DISCARD); |
| 7466 | |
| 7467 | if (!subtype) { |
| 7468 | if (discard) |
| 7469 | return -1; |
| 7470 | |
Denys Vlasenko | 1c54552 | 2022-08-02 11:13:44 +0200 | [diff] [blame] | 7471 | ifsfree(); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7472 | raise_error_syntax("bad substitution"); |
| 7473 | } |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7474 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7475 | flags |= EXP_KEEPNUL; |
| 7476 | flags &= discard ? ~QUOTES_ESC : ~0; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7477 | sep = (flags & EXP_FULL) << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7478 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7479 | switch (*name) { |
| 7480 | case '$': |
| 7481 | num = rootpid; |
| 7482 | goto numvar; |
| 7483 | case '?': |
| 7484 | num = exitstatus; |
| 7485 | goto numvar; |
| 7486 | case '#': |
| 7487 | num = shellparam.nparam; |
| 7488 | goto numvar; |
| 7489 | case '!': |
| 7490 | num = backgndpid; |
| 7491 | if (num == 0) |
| 7492 | return -1; |
| 7493 | numvar: |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7494 | len = cvtnum(num, flags); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7495 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7496 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7497 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7498 | for (i = NOPTS - 1; i >= 0; i--) { |
Martijn Dekker | ad4e961 | 2018-03-31 18:15:59 +0200 | [diff] [blame] | 7499 | if (optlist[i] && optletters(i)) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 7500 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7501 | len++; |
| 7502 | } |
| 7503 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 7504 | check_1char_name: |
| 7505 | #if 0 |
| 7506 | /* handles cases similar to ${#$1} */ |
| 7507 | if (name[2] != '\0') |
| 7508 | raise_error_syntax("bad substitution"); |
| 7509 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7510 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7511 | case '@': |
| 7512 | if (quoted && sep) |
| 7513 | goto param; |
| 7514 | /* fall through */ |
| 7515 | case '*': { |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7516 | char **ap; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7517 | char sepc; |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7518 | char c; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 7519 | |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7520 | /* We will set c to 0 or ~0 depending on whether |
| 7521 | * we're doing field splitting. We won't do field |
| 7522 | * splitting if either we're quoted or sep is zero. |
| 7523 | * |
| 7524 | * Instead of testing (quoted || !sep) the following |
| 7525 | * trick optimises away any branches by using the |
| 7526 | * fact that EXP_QUOTED (which is the only bit that |
| 7527 | * can be set in quoted) is the same as EXP_FULL << |
| 7528 | * CHAR_BIT (which is the only bit that can be set |
| 7529 | * in sep). |
| 7530 | */ |
| 7531 | #if EXP_QUOTED >> CHAR_BIT != EXP_FULL |
| 7532 | #error The following two lines expect EXP_QUOTED == EXP_FULL << CHAR_BIT |
| 7533 | #endif |
| 7534 | c = !((quoted | ~sep) & EXP_QUOTED) - 1; |
| 7535 | sep &= ~quoted; |
| 7536 | sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7537 | param: |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7538 | sepc = sep; |
Denys Vlasenko | 0dd8e45 | 2016-10-01 21:02:06 +0200 | [diff] [blame] | 7539 | ap = shellparam.p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7540 | if (!ap) |
| 7541 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7542 | while ((p = *ap++) != NULL) { |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7543 | len += strtodest(p, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7544 | |
| 7545 | if (*ap && sep) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7546 | len++; |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7547 | memtodest(&sepc, 1, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7548 | } |
| 7549 | } |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7550 | break; |
Denys Vlasenko | 0aaaa50 | 2016-10-02 02:46:56 +0200 | [diff] [blame] | 7551 | } /* case '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7552 | case '0': |
| 7553 | case '1': |
| 7554 | case '2': |
| 7555 | case '3': |
| 7556 | case '4': |
| 7557 | case '5': |
| 7558 | case '6': |
| 7559 | case '7': |
| 7560 | case '8': |
| 7561 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 7562 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7563 | if (num < 0 || num > shellparam.nparam) |
| 7564 | return -1; |
| 7565 | p = num ? shellparam.p[num - 1] : arg0; |
| 7566 | goto value; |
| 7567 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7568 | /* NB: name has form "VAR=..." */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7569 | p = lookupvar(name); |
| 7570 | value: |
| 7571 | if (!p) |
| 7572 | return -1; |
| 7573 | |
Denys Vlasenko | ecc8583 | 2020-02-20 10:06:20 +0100 | [diff] [blame] | 7574 | len = strtodest(p, flags); |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7575 | #if ENABLE_UNICODE_SUPPORT |
| 7576 | if (subtype == VSLENGTH && len > 0) { |
| 7577 | reinit_unicode_for_ash(); |
| 7578 | if (unicode_status == UNICODE_ON) { |
Ron Yorston | 3e3bfb8 | 2016-03-18 11:29:19 +0000 | [diff] [blame] | 7579 | STADJUST(-len, expdest); |
| 7580 | discard = 0; |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 7581 | len = unicode_strlen(p); |
| 7582 | } |
| 7583 | } |
| 7584 | #endif |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7585 | break; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 7588 | if (discard) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7589 | STADJUST(-len, expdest); |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7590 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7591 | return len; |
| 7592 | } |
| 7593 | |
| 7594 | /* |
| 7595 | * Expand a variable, and return a pointer to the next character in the |
| 7596 | * input string. |
| 7597 | */ |
| 7598 | static char * |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 7599 | evalvar(char *p, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7600 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7601 | char varflags; |
| 7602 | char subtype; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7603 | char *var; |
| 7604 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7605 | int startloc; |
| 7606 | ssize_t varlen; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7607 | int discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7608 | int quoted; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7609 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 7610 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7611 | subtype = varflags & VSTYPE; |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 7612 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7613 | quoted = flag & EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7614 | var = p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7615 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 7616 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7617 | |
| 7618 | again: |
Denys Vlasenko | 440da97 | 2018-08-05 14:29:58 +0200 | [diff] [blame] | 7619 | varlen = varvalue(var, varflags, flag, quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7620 | if (varflags & VSNUL) |
| 7621 | varlen--; |
| 7622 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7623 | discard = varlen < 0 ? EXP_DISCARD : 0; |
| 7624 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7625 | switch (subtype) { |
| 7626 | case VSPLUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7627 | discard ^= EXP_DISCARD; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7628 | /* fall through */ |
| 7629 | case 0: |
| 7630 | case VSMINUS: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7631 | p = argstr(p, flag | EXP_TILDE | EXP_WORD | (discard ^ EXP_DISCARD)); |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7632 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7633 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7634 | case VSASSIGN: |
| 7635 | case VSQUESTION: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7636 | p = subevalvar(p, var, 0, startloc, varflags, |
| 7637 | (flag & ~QUOTES_ESC) | (discard ^ EXP_DISCARD)); |
| 7638 | |
| 7639 | if ((flag | ~discard) & EXP_DISCARD) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7640 | goto record; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7641 | |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7642 | varflags &= ~VSNUL; |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7643 | subtype = VSNORMAL; |
Denys Vlasenko | 88ac97d | 2016-10-01 20:55:02 +0200 | [diff] [blame] | 7644 | goto again; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7647 | if ((discard & ~flag) && uflag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7648 | varunset(p, var, 0, 0); |
| 7649 | |
| 7650 | if (subtype == VSLENGTH) { |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7651 | p++; |
| 7652 | if (flag & EXP_DISCARD) |
| 7653 | return p; |
Denys Vlasenko | 45dd87a | 2020-02-21 16:30:44 +0100 | [diff] [blame] | 7654 | cvtnum(varlen > 0 ? varlen : 0, flag); |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7655 | goto really_record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7658 | if (subtype == VSNORMAL) |
| 7659 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7660 | |
| 7661 | #if DEBUG |
| 7662 | switch (subtype) { |
| 7663 | case VSTRIMLEFT: |
| 7664 | case VSTRIMLEFTMAX: |
| 7665 | case VSTRIMRIGHT: |
| 7666 | case VSTRIMRIGHTMAX: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7667 | #if BASH_SUBSTR |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7668 | case VSSUBSTR: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 7669 | #endif |
| 7670 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 7671 | case VSREPLACE: |
| 7672 | case VSREPLACEALL: |
| 7673 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7674 | break; |
| 7675 | default: |
| 7676 | abort(); |
| 7677 | } |
| 7678 | #endif |
| 7679 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7680 | flag |= discard; |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7681 | if (!(flag & EXP_DISCARD)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7682 | /* |
| 7683 | * Terminate the string and start recording the pattern |
| 7684 | * right after it |
| 7685 | */ |
| 7686 | STPUTC('\0', expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7689 | patloc = expdest - (char *)stackblock(); |
| 7690 | p = subevalvar(p, NULL, patloc, startloc, varflags, flag); |
Denys Vlasenko | 4ace385 | 2020-02-16 18:42:50 +0100 | [diff] [blame] | 7691 | |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7692 | record: |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7693 | if ((flag | discard) & EXP_DISCARD) |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7694 | return p; |
| 7695 | |
Denys Vlasenko | 1555895 | 2020-02-22 19:38:40 +0100 | [diff] [blame] | 7696 | really_record: |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7697 | if (quoted) { |
| 7698 | quoted = *var == '@' && shellparam.nparam; |
| 7699 | if (!quoted) |
| 7700 | return p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7701 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 7702 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7703 | return p; |
| 7704 | } |
| 7705 | |
| 7706 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7707 | * Add a file name to the list. |
| 7708 | */ |
| 7709 | static void |
| 7710 | addfname(const char *name) |
| 7711 | { |
| 7712 | struct strlist *sp; |
| 7713 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7714 | sp = stzalloc(sizeof(*sp)); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 7715 | sp->text = sstrdup(name); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7716 | *exparg.lastp = sp; |
| 7717 | exparg.lastp = &sp->next; |
| 7718 | } |
| 7719 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7720 | /* Avoid glob() (and thus, stat() et al) for words like "echo" */ |
| 7721 | static int |
| 7722 | hasmeta(const char *p) |
| 7723 | { |
| 7724 | static const char chars[] ALIGN1 = { |
| 7725 | '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0 |
| 7726 | }; |
| 7727 | |
| 7728 | for (;;) { |
| 7729 | p = strpbrk(p, chars); |
| 7730 | if (!p) |
| 7731 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7732 | switch ((unsigned char)*p) { |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7733 | case CTLQUOTEMARK: |
| 7734 | for (;;) { |
| 7735 | p++; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7736 | if ((unsigned char)*p == CTLQUOTEMARK) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7737 | break; |
Denys Vlasenko | ac61f44 | 2018-03-30 23:03:29 +0200 | [diff] [blame] | 7738 | if ((unsigned char)*p == CTLESC) |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7739 | p++; |
| 7740 | if (*p == '\0') /* huh? */ |
| 7741 | return 0; |
| 7742 | } |
| 7743 | break; |
| 7744 | case '\\': |
| 7745 | case CTLESC: |
| 7746 | p++; |
| 7747 | if (*p == '\0') |
| 7748 | return 0; |
| 7749 | break; |
| 7750 | case '[': |
| 7751 | if (!strchr(p + 1, ']')) { |
| 7752 | /* It's not a properly closed [] pattern, |
| 7753 | * but other metas may follow. Continue checking. |
| 7754 | * my[file* _is_ globbed by bash |
| 7755 | * and matches filenames like "my[file1". |
| 7756 | */ |
| 7757 | break; |
| 7758 | } |
| 7759 | /* fallthrough */ |
| 7760 | default: |
| 7761 | /* case '*': */ |
| 7762 | /* case '?': */ |
| 7763 | return 1; |
| 7764 | } |
| 7765 | p++; |
| 7766 | } |
| 7767 | |
| 7768 | return 0; |
| 7769 | } |
| 7770 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7771 | /* If we want to use glob() from libc... */ |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7772 | #if !ENABLE_ASH_INTERNAL_GLOB |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7773 | |
| 7774 | /* Add the result of glob() to the list */ |
| 7775 | static void |
| 7776 | addglob(const glob_t *pglob) |
| 7777 | { |
| 7778 | char **p = pglob->gl_pathv; |
| 7779 | |
| 7780 | do { |
| 7781 | addfname(*p); |
| 7782 | } while (*++p); |
| 7783 | } |
| 7784 | static void |
| 7785 | expandmeta(struct strlist *str /*, int flag*/) |
| 7786 | { |
| 7787 | /* TODO - EXP_REDIR */ |
| 7788 | |
| 7789 | while (str) { |
| 7790 | char *p; |
| 7791 | glob_t pglob; |
| 7792 | int i; |
| 7793 | |
| 7794 | if (fflag) |
| 7795 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7796 | |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 7797 | if (!hasmeta(str->text)) |
| 7798 | goto nometa; |
Denys Vlasenko | d4f3db9 | 2016-10-30 18:41:01 +0100 | [diff] [blame] | 7799 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7800 | INT_OFF; |
| 7801 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7802 | // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match |
| 7803 | // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?) |
| 7804 | // |
| 7805 | // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping: |
| 7806 | // if you pass it "file\?", it returns "file\?", not "file?", if no match. |
| 7807 | // Which means you need to unescape the string, right? Not so fast: |
| 7808 | // if there _is_ a file named "file\?" (with backslash), it is returned |
| 7809 | // as "file\?" too (whichever pattern you used to find it, say, "file*"). |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7810 | // 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] | 7811 | // |
| 7812 | // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?", |
| 7813 | // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file. |
| 7814 | // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match). |
| 7815 | // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly. |
| 7816 | // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob); |
| 7817 | // i = glob(p, GLOB_NOMAGIC, NULL, &pglob); |
| 7818 | i = glob(p, 0, NULL, &pglob); |
| 7819 | //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] | 7820 | if (p != str->text) |
| 7821 | free(p); |
| 7822 | switch (i) { |
| 7823 | case 0: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7824 | #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] | 7825 | /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */ |
| 7826 | if (!(pglob.gl_flags & GLOB_MAGCHAR)) |
| 7827 | goto nometa2; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7828 | #endif |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7829 | addglob(&pglob); |
| 7830 | globfree(&pglob); |
| 7831 | INT_ON; |
| 7832 | break; |
| 7833 | case GLOB_NOMATCH: |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7834 | //nometa2: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7835 | globfree(&pglob); |
| 7836 | INT_ON; |
Denys Vlasenko | 8e2c9cc | 2016-10-02 15:17:15 +0200 | [diff] [blame] | 7837 | nometa: |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7838 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 7839 | rmescapes(str->text, 0, NULL); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7840 | exparg.lastp = &str->next; |
| 7841 | break; |
| 7842 | default: /* GLOB_NOSPACE */ |
| 7843 | globfree(&pglob); |
| 7844 | INT_ON; |
| 7845 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 7846 | } |
| 7847 | str = str->next; |
| 7848 | } |
| 7849 | } |
| 7850 | |
| 7851 | #else |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 7852 | /* 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] | 7853 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7854 | /* |
| 7855 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 7856 | */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7857 | typedef struct exp_t { |
| 7858 | char *dir; |
| 7859 | unsigned dir_max; |
| 7860 | } exp_t; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7861 | static void |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7862 | expmeta(exp_t *exp, char *name, unsigned name_len, unsigned expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7863 | { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7864 | #define expdir exp->dir |
| 7865 | #define expdir_max exp->dir_max |
| 7866 | char *enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7867 | char *p; |
| 7868 | const char *cp; |
| 7869 | char *start; |
| 7870 | char *endname; |
| 7871 | int metaflag; |
| 7872 | struct stat statb; |
| 7873 | DIR *dirp; |
| 7874 | struct dirent *dp; |
| 7875 | int atend; |
| 7876 | int matchdot; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7877 | int esc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7878 | |
| 7879 | metaflag = 0; |
| 7880 | start = name; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7881 | for (p = name; esc = 0, *p; p += esc + 1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7882 | if (*p == '*' || *p == '?') |
| 7883 | metaflag = 1; |
| 7884 | else if (*p == '[') { |
| 7885 | char *q = p + 1; |
| 7886 | if (*q == '!') |
| 7887 | q++; |
| 7888 | for (;;) { |
| 7889 | if (*q == '\\') |
| 7890 | q++; |
| 7891 | if (*q == '/' || *q == '\0') |
| 7892 | break; |
| 7893 | if (*++q == ']') { |
| 7894 | metaflag = 1; |
| 7895 | break; |
| 7896 | } |
| 7897 | } |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7898 | } else { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7899 | if (*p == '\\' && p[1]) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7900 | esc++; |
| 7901 | if (p[esc] == '/') { |
| 7902 | if (metaflag) |
| 7903 | break; |
| 7904 | start = p + esc + 1; |
| 7905 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7906 | } |
| 7907 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7908 | if (metaflag == 0) { /* we've reached the end of the file name */ |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7909 | if (!expdir_len) |
| 7910 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7911 | p = name; |
| 7912 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7913 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7914 | p++; |
| 7915 | *enddir++ = *p; |
| 7916 | } while (*p++); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7917 | if (lstat(expdir, &statb) == 0) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7918 | addfname(expdir); |
| 7919 | return; |
| 7920 | } |
| 7921 | endname = p; |
| 7922 | if (name < start) { |
| 7923 | p = name; |
| 7924 | do { |
Denys Vlasenko | eb54ca8 | 2018-08-07 18:54:52 +0200 | [diff] [blame] | 7925 | if (*p == '\\' && p[1]) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7926 | p++; |
| 7927 | *enddir++ = *p++; |
| 7928 | } while (p < start); |
| 7929 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7930 | *enddir = '\0'; |
| 7931 | cp = expdir; |
| 7932 | expdir_len = enddir - cp; |
| 7933 | if (!expdir_len) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7934 | cp = "."; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7935 | dirp = opendir(cp); |
| 7936 | if (dirp == NULL) |
| 7937 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7938 | if (*endname == 0) { |
| 7939 | atend = 1; |
| 7940 | } else { |
| 7941 | atend = 0; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7942 | *endname = '\0'; |
| 7943 | endname += esc + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7944 | } |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7945 | name_len -= endname - name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7946 | matchdot = 0; |
| 7947 | p = start; |
| 7948 | if (*p == '\\') |
| 7949 | p++; |
| 7950 | if (*p == '.') |
| 7951 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7952 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7953 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7954 | continue; |
| 7955 | if (pmatch(start, dp->d_name)) { |
| 7956 | if (atend) { |
| 7957 | strcpy(enddir, dp->d_name); |
| 7958 | addfname(expdir); |
| 7959 | } else { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7960 | unsigned offset; |
| 7961 | unsigned len; |
| 7962 | |
| 7963 | p = stpcpy(enddir, dp->d_name); |
| 7964 | *p = '/'; |
| 7965 | |
| 7966 | offset = p - expdir + 1; |
| 7967 | len = offset + name_len + NAME_MAX; |
| 7968 | if (len > expdir_max) { |
| 7969 | len += PATH_MAX; |
| 7970 | expdir = ckrealloc(expdir, len); |
| 7971 | expdir_max = len; |
| 7972 | } |
| 7973 | |
| 7974 | expmeta(exp, endname, name_len, offset); |
| 7975 | enddir = expdir + expdir_len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7976 | } |
| 7977 | } |
| 7978 | } |
| 7979 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7980 | if (!atend) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7981 | endname[-esc - 1] = esc ? '\\' : '/'; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 7982 | #undef expdir |
| 7983 | #undef expdir_max |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | static struct strlist * |
| 7987 | msort(struct strlist *list, int len) |
| 7988 | { |
| 7989 | struct strlist *p, *q = NULL; |
| 7990 | struct strlist **lpp; |
| 7991 | int half; |
| 7992 | int n; |
| 7993 | |
| 7994 | if (len <= 1) |
| 7995 | return list; |
| 7996 | half = len >> 1; |
| 7997 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7998 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7999 | q = p; |
| 8000 | p = p->next; |
| 8001 | } |
| 8002 | q->next = NULL; /* terminate first half of list */ |
| 8003 | q = msort(list, half); /* sort first half of list */ |
| 8004 | p = msort(p, len - half); /* sort second half */ |
| 8005 | lpp = &list; |
| 8006 | for (;;) { |
| 8007 | #if ENABLE_LOCALE_SUPPORT |
| 8008 | if (strcoll(p->text, q->text) < 0) |
| 8009 | #else |
| 8010 | if (strcmp(p->text, q->text) < 0) |
| 8011 | #endif |
| 8012 | { |
| 8013 | *lpp = p; |
| 8014 | lpp = &p->next; |
| 8015 | p = *lpp; |
| 8016 | if (p == NULL) { |
| 8017 | *lpp = q; |
| 8018 | break; |
| 8019 | } |
| 8020 | } else { |
| 8021 | *lpp = q; |
| 8022 | lpp = &q->next; |
| 8023 | q = *lpp; |
| 8024 | if (q == NULL) { |
| 8025 | *lpp = p; |
| 8026 | break; |
| 8027 | } |
| 8028 | } |
| 8029 | } |
| 8030 | return list; |
| 8031 | } |
| 8032 | |
| 8033 | /* |
| 8034 | * Sort the results of file name expansion. It calculates the number of |
| 8035 | * strings to sort and then calls msort (short for merge sort) to do the |
| 8036 | * work. |
| 8037 | */ |
| 8038 | static struct strlist * |
| 8039 | expsort(struct strlist *str) |
| 8040 | { |
| 8041 | int len; |
| 8042 | struct strlist *sp; |
| 8043 | |
| 8044 | len = 0; |
| 8045 | for (sp = str; sp; sp = sp->next) |
| 8046 | len++; |
| 8047 | return msort(str, len); |
| 8048 | } |
| 8049 | |
| 8050 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8051 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8052 | { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8053 | /* TODO - EXP_REDIR */ |
| 8054 | |
| 8055 | while (str) { |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 8056 | exp_t exp; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8057 | struct strlist **savelastp; |
| 8058 | struct strlist *sp; |
| 8059 | char *p; |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 8060 | unsigned len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8061 | |
| 8062 | if (fflag) |
| 8063 | goto nometa; |
Felix Fietkau | b5b2112 | 2017-01-31 21:58:55 +0100 | [diff] [blame] | 8064 | if (!hasmeta(str->text)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8065 | goto nometa; |
| 8066 | savelastp = exparg.lastp; |
| 8067 | |
| 8068 | INT_OFF; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 8069 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denys Vlasenko | d5f5045 | 2018-04-14 14:50:47 +0200 | [diff] [blame] | 8070 | len = strlen(p); |
| 8071 | exp.dir_max = len + PATH_MAX; |
| 8072 | exp.dir = ckmalloc(exp.dir_max); |
| 8073 | |
| 8074 | expmeta(&exp, p, len, 0); |
| 8075 | free(exp.dir); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8076 | if (p != str->text) |
| 8077 | free(p); |
| 8078 | INT_ON; |
| 8079 | if (exparg.lastp == savelastp) { |
| 8080 | /* |
| 8081 | * no matches |
| 8082 | */ |
| 8083 | nometa: |
| 8084 | *exparg.lastp = str; |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 8085 | rmescapes(str->text, 0, NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8086 | exparg.lastp = &str->next; |
| 8087 | } else { |
| 8088 | *exparg.lastp = NULL; |
| 8089 | *savelastp = sp = expsort(*savelastp); |
| 8090 | while (sp->next != NULL) |
| 8091 | sp = sp->next; |
| 8092 | exparg.lastp = &sp->next; |
| 8093 | } |
| 8094 | str = str->next; |
| 8095 | } |
| 8096 | } |
Denys Vlasenko | 514b51d | 2016-10-01 14:33:08 +0200 | [diff] [blame] | 8097 | #endif /* ENABLE_ASH_INTERNAL_GLOB */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8098 | |
| 8099 | /* |
| 8100 | * Perform variable substitution and command substitution on an argument, |
| 8101 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 8102 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 8103 | * here document expansion. |
| 8104 | */ |
| 8105 | static void |
| 8106 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 8107 | { |
| 8108 | struct strlist *sp; |
| 8109 | char *p; |
| 8110 | |
| 8111 | argbackq = arg->narg.backquote; |
| 8112 | STARTSTACKSTR(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 8113 | TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag)); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 8114 | argstr(arg->narg.text, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8115 | if (arglist == NULL) { |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 8116 | /* here document expanded */ |
| 8117 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8118 | } |
Denys Vlasenko | 8233188 | 2020-02-24 10:02:50 +0100 | [diff] [blame] | 8119 | p = grabstackstr(expdest); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 8120 | TRACE(("expandarg: p:'%s'\n", p)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8121 | exparg.lastp = &exparg.list; |
| 8122 | /* |
| 8123 | * TODO - EXP_REDIR |
| 8124 | */ |
| 8125 | if (flag & EXP_FULL) { |
| 8126 | ifsbreakup(p, &exparg); |
| 8127 | *exparg.lastp = NULL; |
| 8128 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8129 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8130 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 8131 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8132 | sp->text = p; |
| 8133 | *exparg.lastp = sp; |
| 8134 | exparg.lastp = &sp->next; |
| 8135 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8136 | *exparg.lastp = NULL; |
| 8137 | if (exparg.list) { |
| 8138 | *arglist->lastp = exparg.list; |
| 8139 | arglist->lastp = exparg.lastp; |
| 8140 | } |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 8141 | |
| 8142 | out: |
| 8143 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | /* |
| 8147 | * Expand shell variables and backquotes inside a here document. |
| 8148 | */ |
| 8149 | static void |
Denys Vlasenko | c2058ec | 2020-02-22 20:25:03 +0100 | [diff] [blame] | 8150 | expandhere(union node *arg) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8151 | { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 8152 | expandarg(arg, (struct arglist *)NULL, EXP_QUOTED); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
| 8155 | /* |
| 8156 | * Returns true if the pattern matches the string. |
| 8157 | */ |
| 8158 | static int |
| 8159 | patmatch(char *pattern, const char *string) |
| 8160 | { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8161 | char *p = preglob(pattern, 0); |
Denys Vlasenko | 4476c70 | 2017-08-15 15:27:41 +0200 | [diff] [blame] | 8162 | int r = pmatch(p, string); |
| 8163 | //bb_error_msg("!fnmatch(pattern:'%s',str:'%s',0):%d", p, string, r); |
| 8164 | return r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | /* |
| 8168 | * See if a pattern matches in a case statement. |
| 8169 | */ |
| 8170 | static int |
| 8171 | casematch(union node *pattern, char *val) |
| 8172 | { |
| 8173 | struct stackmark smark; |
| 8174 | int result; |
| 8175 | |
| 8176 | setstackmark(&smark); |
| 8177 | argbackq = pattern->narg.backquote; |
| 8178 | STARTSTACKSTR(expdest); |
Denys Vlasenko | b8c0bc1 | 2017-07-26 23:03:21 +0200 | [diff] [blame] | 8179 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); |
Denys Vlasenko | 5ac04f2 | 2016-10-27 14:46:50 +0200 | [diff] [blame] | 8180 | ifsfree(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8181 | result = patmatch(stackblock(), val); |
| 8182 | popstackmark(&smark); |
| 8183 | return result; |
| 8184 | } |
| 8185 | |
| 8186 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8187 | /* ============ find_command */ |
| 8188 | |
| 8189 | struct builtincmd { |
| 8190 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8191 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8192 | /* unsigned flags; */ |
| 8193 | }; |
| 8194 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 8195 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8196 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8197 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8198 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8199 | |
| 8200 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8201 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8202 | union param { |
| 8203 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8204 | /* index >= 0 for commands without path (slashes) */ |
| 8205 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 8206 | /* index == -1 for commands with slashes */ |
| 8207 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8208 | const struct builtincmd *cmd; |
| 8209 | struct funcnode *func; |
| 8210 | } u; |
| 8211 | }; |
| 8212 | /* values of cmdtype */ |
| 8213 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 8214 | #define CMDNORMAL 0 /* command is an executable program */ |
| 8215 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 8216 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 8217 | |
| 8218 | /* action to find_command() */ |
| 8219 | #define DO_ERR 0x01 /* prints errors */ |
| 8220 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 8221 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 8222 | #define DO_ALTPATH 0x08 /* using alternate path */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8223 | #define DO_REGBLTIN 0x10 /* regular built-ins and functions only */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8224 | |
| 8225 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 8226 | |
| 8227 | |
| 8228 | /* ============ Hashing commands */ |
| 8229 | |
| 8230 | /* |
| 8231 | * When commands are first encountered, they are entered in a hash table. |
| 8232 | * This ensures that a full path search will not have to be done for them |
| 8233 | * on each invocation. |
| 8234 | * |
| 8235 | * We should investigate converting to a linear search, even though that |
| 8236 | * would make the command name "hash" a misnomer. |
| 8237 | */ |
| 8238 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8239 | struct tblentry { |
| 8240 | struct tblentry *next; /* next entry in hash chain */ |
| 8241 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8242 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8243 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8244 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8245 | }; |
| 8246 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8247 | static struct tblentry **cmdtable; |
| 8248 | #define INIT_G_cmdtable() do { \ |
| 8249 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 8250 | } while (0) |
| 8251 | |
| 8252 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 8253 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8254 | |
| 8255 | static void |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8256 | 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] | 8257 | { |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8258 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8259 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8260 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 8261 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8262 | while (*envp) |
| 8263 | putenv(*envp++); |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 8264 | popredir(/*drop:*/ 1); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8265 | run_noexec_applet_and_exit(applet_no, cmd, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 8266 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 8267 | /* re-exec ourselves with the new arguments */ |
| 8268 | execve(bb_busybox_exec_path, argv, envp); |
| 8269 | /* If they called chroot or otherwise made the binary no longer |
| 8270 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8271 | } |
| 8272 | #endif |
| 8273 | |
| 8274 | repeat: |
| 8275 | #ifdef SYSV |
| 8276 | do { |
| 8277 | execve(cmd, argv, envp); |
| 8278 | } while (errno == EINTR); |
| 8279 | #else |
| 8280 | execve(cmd, argv, envp); |
| 8281 | #endif |
Ron Yorston | ca82b53 | 2018-11-01 11:45:03 +0100 | [diff] [blame] | 8282 | |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8283 | if (cmd != bb_busybox_exec_path && errno == ENOEXEC) { |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8284 | /* Run "cmd" as a shell script: |
| 8285 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 8286 | * "If the execve() function fails with ENOEXEC, the shell |
| 8287 | * shall execute a command equivalent to having a shell invoked |
| 8288 | * with the command name as its first operand, |
| 8289 | * with any remaining arguments passed to the new shell" |
| 8290 | * |
| 8291 | * That is, do not use $SHELL, user's shell, or /bin/sh; |
| 8292 | * just call ourselves. |
Denys Vlasenko | 2bef526 | 2011-12-16 00:25:17 +0100 | [diff] [blame] | 8293 | * |
| 8294 | * Note that bash reads ~80 chars of the file, and if it sees |
| 8295 | * a zero byte before it sees newline, it doesn't try to |
| 8296 | * interpret it, but fails with "cannot execute binary file" |
Denys Vlasenko | cda6ea9 | 2011-12-16 00:44:36 +0100 | [diff] [blame] | 8297 | * message and exit code 126. For one, this prevents attempts |
| 8298 | * to interpret foreign ELF binaries as shell scripts. |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 8299 | */ |
Denys Vlasenko | 00a1dbd | 2017-07-29 01:20:53 +0200 | [diff] [blame] | 8300 | argv[0] = (char*) cmd; |
| 8301 | cmd = bb_busybox_exec_path; |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8302 | /* NB: this is only possible because all callers of shellexec() |
| 8303 | * ensure that the argv[-1] slot exists! |
| 8304 | */ |
| 8305 | argv--; |
| 8306 | argv[0] = (char*) "ash"; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8307 | goto repeat; |
| 8308 | } |
| 8309 | } |
| 8310 | |
| 8311 | /* |
| 8312 | * Exec a program. Never returns. If you change this routine, you may |
| 8313 | * have to change the find_command routine as well. |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 8314 | * argv[-1] must exist and be writable! See tryexec() for why. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8315 | */ |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8316 | static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN; |
| 8317 | static void shellexec(char *prog, char **argv, const char *path, int idx) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8318 | { |
| 8319 | char *cmdname; |
| 8320 | int e; |
| 8321 | char **envp; |
| 8322 | int exerrno; |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8323 | int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8324 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 8325 | envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL); |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8326 | if (strchr(prog, '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 8327 | #if ENABLE_FEATURE_SH_STANDALONE |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8328 | || (applet_no = find_applet_by_name(prog)) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8329 | #endif |
| 8330 | ) { |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8331 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8332 | if (applet_no >= 0) { |
| 8333 | /* We tried execing ourself, but it didn't work. |
| 8334 | * Maybe /proc/self/exe doesn't exist? |
| 8335 | * Try $PATH search. |
| 8336 | */ |
| 8337 | goto try_PATH; |
| 8338 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8339 | e = errno; |
| 8340 | } else { |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 8341 | try_PATH: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8342 | e = ENOENT; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8343 | while (padvance(&path, argv[0]) >= 0) { |
| 8344 | cmdname = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8345 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 8346 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8347 | if (errno != ENOENT && errno != ENOTDIR) |
| 8348 | e = errno; |
| 8349 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | /* Map to POSIX errors */ |
| 8354 | switch (e) { |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8355 | default: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8356 | exerrno = 126; |
| 8357 | break; |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8358 | case ELOOP: |
| 8359 | case ENAMETOOLONG: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8360 | case ENOENT: |
Denys Vlasenko | 2596f41 | 2018-08-05 18:04:09 +0200 | [diff] [blame] | 8361 | case ENOTDIR: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8362 | exerrno = 127; |
| 8363 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8364 | } |
| 8365 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8366 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 8367 | prog, e, suppress_int)); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 8368 | ash_msg_and_raise(EXEND, "%s: %s", prog, errmsg(e, "not found")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8369 | /* NOTREACHED */ |
| 8370 | } |
| 8371 | |
| 8372 | static void |
| 8373 | printentry(struct tblentry *cmdp) |
| 8374 | { |
| 8375 | int idx; |
| 8376 | const char *path; |
| 8377 | char *name; |
| 8378 | |
| 8379 | idx = cmdp->param.index; |
| 8380 | path = pathval(); |
| 8381 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8382 | padvance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8383 | } while (--idx >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8384 | name = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8385 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 8386 | } |
| 8387 | |
| 8388 | /* |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8389 | * Clear out command entries. |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8390 | */ |
| 8391 | static void |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8392 | clearcmdentry(void) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8393 | { |
| 8394 | struct tblentry **tblp; |
| 8395 | struct tblentry **pp; |
| 8396 | struct tblentry *cmdp; |
| 8397 | |
| 8398 | INT_OFF; |
| 8399 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 8400 | pp = tblp; |
| 8401 | while ((cmdp = *pp) != NULL) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8402 | if (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8403 | || (cmdp->cmdtype == CMDBUILTIN |
| 8404 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8405 | && builtinloc > 0 |
| 8406 | ) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8407 | ) { |
| 8408 | *pp = cmdp->next; |
| 8409 | free(cmdp); |
| 8410 | } else { |
| 8411 | pp = &cmdp->next; |
| 8412 | } |
| 8413 | } |
| 8414 | } |
| 8415 | INT_ON; |
| 8416 | } |
| 8417 | |
| 8418 | /* |
| 8419 | * Locate a command in the command hash table. If "add" is nonzero, |
| 8420 | * add the command to the table if it is not already present. The |
| 8421 | * variable "lastcmdentry" is set to point to the address of the link |
| 8422 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 8423 | * entry. |
| 8424 | * |
| 8425 | * Interrupts must be off if called with add != 0. |
| 8426 | */ |
| 8427 | static struct tblentry **lastcmdentry; |
| 8428 | |
| 8429 | static struct tblentry * |
| 8430 | cmdlookup(const char *name, int add) |
| 8431 | { |
| 8432 | unsigned int hashval; |
| 8433 | const char *p; |
| 8434 | struct tblentry *cmdp; |
| 8435 | struct tblentry **pp; |
| 8436 | |
| 8437 | p = name; |
| 8438 | hashval = (unsigned char)*p << 4; |
| 8439 | while (*p) |
| 8440 | hashval += (unsigned char)*p++; |
| 8441 | hashval &= 0x7FFF; |
| 8442 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 8443 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8444 | if (strcmp(cmdp->cmdname, name) == 0) |
| 8445 | break; |
| 8446 | pp = &cmdp->next; |
| 8447 | } |
| 8448 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8449 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 8450 | + strlen(name) |
| 8451 | /* + 1 - already done because |
| 8452 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 8453 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8454 | cmdp->cmdtype = CMDUNKNOWN; |
| 8455 | strcpy(cmdp->cmdname, name); |
| 8456 | } |
| 8457 | lastcmdentry = pp; |
| 8458 | return cmdp; |
| 8459 | } |
| 8460 | |
| 8461 | /* |
| 8462 | * Delete the command entry returned on the last lookup. |
| 8463 | */ |
| 8464 | static void |
| 8465 | delete_cmd_entry(void) |
| 8466 | { |
| 8467 | struct tblentry *cmdp; |
| 8468 | |
| 8469 | INT_OFF; |
| 8470 | cmdp = *lastcmdentry; |
| 8471 | *lastcmdentry = cmdp->next; |
| 8472 | if (cmdp->cmdtype == CMDFUNCTION) |
| 8473 | freefunc(cmdp->param.func); |
| 8474 | free(cmdp); |
| 8475 | INT_ON; |
| 8476 | } |
| 8477 | |
| 8478 | /* |
| 8479 | * Add a new command entry, replacing any existing command entry for |
| 8480 | * the same name - except special builtins. |
| 8481 | */ |
| 8482 | static void |
| 8483 | addcmdentry(char *name, struct cmdentry *entry) |
| 8484 | { |
| 8485 | struct tblentry *cmdp; |
| 8486 | |
| 8487 | cmdp = cmdlookup(name, 1); |
| 8488 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 8489 | freefunc(cmdp->param.func); |
| 8490 | } |
| 8491 | cmdp->cmdtype = entry->cmdtype; |
| 8492 | cmdp->param = entry->u; |
| 8493 | cmdp->rehash = 0; |
| 8494 | } |
| 8495 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8496 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8497 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8498 | { |
| 8499 | struct tblentry **pp; |
| 8500 | struct tblentry *cmdp; |
| 8501 | int c; |
| 8502 | struct cmdentry entry; |
| 8503 | char *name; |
| 8504 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8505 | if (nextopt("r") != '\0') { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8506 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8507 | return 0; |
| 8508 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8509 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8510 | if (*argptr == NULL) { |
| 8511 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8512 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 8513 | if (cmdp->cmdtype == CMDNORMAL) |
| 8514 | printentry(cmdp); |
| 8515 | } |
| 8516 | } |
| 8517 | return 0; |
| 8518 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8519 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8520 | c = 0; |
| 8521 | while ((name = *argptr) != NULL) { |
| 8522 | cmdp = cmdlookup(name, 0); |
| 8523 | if (cmdp != NULL |
| 8524 | && (cmdp->cmdtype == CMDNORMAL |
Denys Vlasenko | 22c7592 | 2020-02-17 16:20:05 +0100 | [diff] [blame] | 8525 | || (cmdp->cmdtype == CMDBUILTIN |
| 8526 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 8527 | && builtinloc > 0 |
| 8528 | ) |
| 8529 | ) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8530 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8531 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8532 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8533 | find_command(name, &entry, DO_ERR, pathval()); |
| 8534 | if (entry.cmdtype == CMDUNKNOWN) |
| 8535 | c = 1; |
| 8536 | argptr++; |
| 8537 | } |
| 8538 | return c; |
| 8539 | } |
| 8540 | |
| 8541 | /* |
| 8542 | * Called when a cd is done. Marks all commands so the next time they |
| 8543 | * are executed they will be rehashed. |
| 8544 | */ |
| 8545 | static void |
| 8546 | hashcd(void) |
| 8547 | { |
| 8548 | struct tblentry **pp; |
| 8549 | struct tblentry *cmdp; |
| 8550 | |
| 8551 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 8552 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8553 | if (cmdp->cmdtype == CMDNORMAL |
| 8554 | || (cmdp->cmdtype == CMDBUILTIN |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 8555 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8556 | && builtinloc > 0) |
| 8557 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8558 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8559 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8560 | } |
| 8561 | } |
| 8562 | } |
| 8563 | |
| 8564 | /* |
| 8565 | * Fix command hash table when PATH changed. |
| 8566 | * Called before PATH is changed. The argument is the new value of PATH; |
| 8567 | * pathval() still returns the old value at this point. |
| 8568 | * Called with interrupts off. |
| 8569 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8570 | static void FAST_FUNC |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8571 | changepath(const char *newval) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8572 | { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8573 | const char *new; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 8574 | int idx; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8575 | int bltin; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8576 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8577 | new = newval; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8578 | idx = 0; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8579 | bltin = -1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8580 | for (;;) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8581 | if (*new == '%' && prefix(new + 1, "builtin")) { |
| 8582 | bltin = idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8583 | break; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8584 | } |
| 8585 | new = strchr(new, ':'); |
| 8586 | if (!new) |
| 8587 | break; |
| 8588 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 8589 | new++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8590 | } |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 8591 | builtinloc = bltin; |
| 8592 | clearcmdentry(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8593 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8594 | enum { |
| 8595 | TEOF, |
| 8596 | TNL, |
| 8597 | TREDIR, |
| 8598 | TWORD, |
| 8599 | TSEMI, |
| 8600 | TBACKGND, |
| 8601 | TAND, |
| 8602 | TOR, |
| 8603 | TPIPE, |
| 8604 | TLP, |
| 8605 | TRP, |
| 8606 | TENDCASE, |
| 8607 | TENDBQUOTE, |
| 8608 | TNOT, |
| 8609 | TCASE, |
| 8610 | TDO, |
| 8611 | TDONE, |
| 8612 | TELIF, |
| 8613 | TELSE, |
| 8614 | TESAC, |
| 8615 | TFI, |
| 8616 | TFOR, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8617 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8618 | TFUNCTION, |
| 8619 | #endif |
| 8620 | TIF, |
| 8621 | TIN, |
| 8622 | TTHEN, |
| 8623 | TUNTIL, |
| 8624 | TWHILE, |
| 8625 | TBEGIN, |
| 8626 | TEND |
| 8627 | }; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8628 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8629 | |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8630 | /* Nth bit indicates if token marks the end of a list */ |
| 8631 | enum { |
| 8632 | tokendlist = 0 |
| 8633 | /* 0 */ | (1u << TEOF) |
| 8634 | /* 1 */ | (0u << TNL) |
| 8635 | /* 2 */ | (0u << TREDIR) |
| 8636 | /* 3 */ | (0u << TWORD) |
| 8637 | /* 4 */ | (0u << TSEMI) |
| 8638 | /* 5 */ | (0u << TBACKGND) |
| 8639 | /* 6 */ | (0u << TAND) |
| 8640 | /* 7 */ | (0u << TOR) |
| 8641 | /* 8 */ | (0u << TPIPE) |
| 8642 | /* 9 */ | (0u << TLP) |
| 8643 | /* 10 */ | (1u << TRP) |
| 8644 | /* 11 */ | (1u << TENDCASE) |
| 8645 | /* 12 */ | (1u << TENDBQUOTE) |
| 8646 | /* 13 */ | (0u << TNOT) |
| 8647 | /* 14 */ | (0u << TCASE) |
| 8648 | /* 15 */ | (1u << TDO) |
| 8649 | /* 16 */ | (1u << TDONE) |
| 8650 | /* 17 */ | (1u << TELIF) |
| 8651 | /* 18 */ | (1u << TELSE) |
| 8652 | /* 19 */ | (1u << TESAC) |
| 8653 | /* 20 */ | (1u << TFI) |
| 8654 | /* 21 */ | (0u << TFOR) |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8655 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8656 | /* 22 */ | (0u << TFUNCTION) |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8657 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8658 | /* 23 */ | (0u << TIF) |
| 8659 | /* 24 */ | (0u << TIN) |
| 8660 | /* 25 */ | (1u << TTHEN) |
| 8661 | /* 26 */ | (0u << TUNTIL) |
| 8662 | /* 27 */ | (0u << TWHILE) |
| 8663 | /* 28 */ | (0u << TBEGIN) |
| 8664 | /* 29 */ | (1u << TEND) |
| 8665 | , /* thus far 29 bits used */ |
| 8666 | }; |
| 8667 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 8668 | static const char *const tokname_array[] ALIGN_PTR = { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8669 | "end of file", |
| 8670 | "newline", |
| 8671 | "redirection", |
| 8672 | "word", |
| 8673 | ";", |
| 8674 | "&", |
| 8675 | "&&", |
| 8676 | "||", |
| 8677 | "|", |
| 8678 | "(", |
| 8679 | ")", |
| 8680 | ";;", |
| 8681 | "`", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8682 | #define KWDOFFSET 13 |
| 8683 | /* the following are keywords */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8684 | "!", |
| 8685 | "case", |
| 8686 | "do", |
| 8687 | "done", |
| 8688 | "elif", |
| 8689 | "else", |
| 8690 | "esac", |
| 8691 | "fi", |
| 8692 | "for", |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8693 | #if BASH_FUNCTION |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8694 | "function", |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 8695 | #endif |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8696 | "if", |
| 8697 | "in", |
| 8698 | "then", |
| 8699 | "until", |
| 8700 | "while", |
| 8701 | "{", |
| 8702 | "}", |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8703 | }; |
| 8704 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8705 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 8706 | static int |
| 8707 | pstrcmp(const void *a, const void *b) |
| 8708 | { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 8709 | return strcmp((char*)a, *(char**)b); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8710 | } |
| 8711 | |
| 8712 | static const char *const * |
| 8713 | findkwd(const char *s) |
| 8714 | { |
| 8715 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 8716 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 8717 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
| 8720 | /* |
| 8721 | * Locate and print what a word is... |
| 8722 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8723 | static int |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8724 | describe_command(char *command, const char *path, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8725 | { |
| 8726 | struct cmdentry entry; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8727 | #if ENABLE_ASH_ALIAS |
| 8728 | const struct alias *ap; |
| 8729 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8730 | |
| 8731 | path = path ? path : pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8732 | |
| 8733 | if (describe_command_verbose) { |
| 8734 | out1str(command); |
| 8735 | } |
| 8736 | |
| 8737 | /* First look at the keywords */ |
| 8738 | if (findkwd(command)) { |
| 8739 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 8740 | goto out; |
| 8741 | } |
| 8742 | |
| 8743 | #if ENABLE_ASH_ALIAS |
| 8744 | /* Then look at the aliases */ |
| 8745 | ap = lookupalias(command, 0); |
| 8746 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8747 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8748 | out1str("alias "); |
| 8749 | printalias(ap); |
| 8750 | return 0; |
| 8751 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8752 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8753 | goto out; |
| 8754 | } |
| 8755 | #endif |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8756 | /* Brute force */ |
| 8757 | find_command(command, &entry, DO_ABS, path); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8758 | |
| 8759 | switch (entry.cmdtype) { |
| 8760 | case CMDNORMAL: { |
| 8761 | int j = entry.u.index; |
| 8762 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 8763 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8764 | p = command; |
| 8765 | } else { |
| 8766 | do { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8767 | padvance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8768 | } while (--j >= 0); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 8769 | p = stackblock(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8770 | } |
| 8771 | if (describe_command_verbose) { |
Youfu Zhang | 6683d1c | 2017-05-26 15:31:29 +0800 | [diff] [blame] | 8772 | out1fmt(" is %s", p); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8773 | } else { |
| 8774 | out1str(p); |
| 8775 | } |
| 8776 | break; |
| 8777 | } |
| 8778 | |
| 8779 | case CMDFUNCTION: |
| 8780 | if (describe_command_verbose) { |
Denys Vlasenko | 63c42af | 2018-07-24 17:08:04 +0200 | [diff] [blame] | 8781 | /*out1str(" is a shell function");*/ |
| 8782 | out1str(" is a function"); /* bash says this */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8783 | } else { |
| 8784 | out1str(command); |
| 8785 | } |
| 8786 | break; |
| 8787 | |
| 8788 | case CMDBUILTIN: |
| 8789 | if (describe_command_verbose) { |
| 8790 | out1fmt(" is a %sshell builtin", |
| 8791 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 8792 | "special " : nullstr |
| 8793 | ); |
| 8794 | } else { |
| 8795 | out1str(command); |
| 8796 | } |
| 8797 | break; |
| 8798 | |
| 8799 | default: |
| 8800 | if (describe_command_verbose) { |
| 8801 | out1str(": not found\n"); |
| 8802 | } |
| 8803 | return 127; |
| 8804 | } |
| 8805 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 8806 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8807 | return 0; |
| 8808 | } |
| 8809 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8810 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8811 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8812 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8813 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8814 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8815 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8816 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8817 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 8818 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8819 | i++; |
| 8820 | verbose = 0; |
| 8821 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8822 | while (argv[i]) { |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8823 | err |= describe_command(argv[i++], NULL, verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8824 | } |
| 8825 | return err; |
| 8826 | } |
| 8827 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8828 | static struct strlist * |
| 8829 | fill_arglist(struct arglist *arglist, union node **argpp) |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8830 | { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8831 | struct strlist **lastp = arglist->lastp; |
| 8832 | union node *argp; |
| 8833 | |
| 8834 | while ((argp = *argpp) != NULL) { |
| 8835 | expandarg(argp, arglist, EXP_FULL | EXP_TILDE); |
| 8836 | *argpp = argp->narg.next; |
| 8837 | if (*lastp) |
| 8838 | break; |
| 8839 | } |
| 8840 | |
| 8841 | return *lastp; |
| 8842 | } |
| 8843 | |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 8844 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8845 | /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */ |
| 8846 | static int |
| 8847 | parse_command_args(struct arglist *arglist, union node **argpp, const char **path) |
| 8848 | { |
| 8849 | struct strlist *sp = arglist->list; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8850 | char *cp, c; |
| 8851 | |
| 8852 | for (;;) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8853 | sp = sp->next ? sp->next : fill_arglist(arglist, argpp); |
| 8854 | if (!sp) |
| 8855 | return 0; |
| 8856 | cp = sp->text; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8857 | if (*cp++ != '-') |
| 8858 | break; |
| 8859 | c = *cp++; |
| 8860 | if (!c) |
| 8861 | break; |
| 8862 | if (c == '-' && !*cp) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8863 | if (!sp->next && !fill_arglist(arglist, argpp)) |
| 8864 | return 0; |
| 8865 | sp = sp->next; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8866 | break; |
| 8867 | } |
| 8868 | do { |
| 8869 | switch (c) { |
| 8870 | case 'p': |
| 8871 | *path = bb_default_path; |
| 8872 | break; |
| 8873 | default: |
| 8874 | /* run 'typecmd' for other options */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8875 | return 0; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8876 | } |
| 8877 | c = *cp++; |
| 8878 | } while (c); |
| 8879 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 8880 | |
| 8881 | arglist->list = sp; |
| 8882 | return DO_NOFUNC; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8883 | } |
| 8884 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8885 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8886 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8887 | { |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8888 | char *cmd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8889 | int c; |
| 8890 | enum { |
| 8891 | VERIFY_BRIEF = 1, |
| 8892 | VERIFY_VERBOSE = 2, |
| 8893 | } verify = 0; |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8894 | const char *path = NULL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8895 | |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8896 | /* "command [-p] PROG ARGS" (that is, without -V or -v) |
| 8897 | * never reaches this function. |
| 8898 | */ |
| 8899 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8900 | while ((c = nextopt("pvV")) != '\0') |
| 8901 | if (c == 'V') |
| 8902 | verify |= VERIFY_VERBOSE; |
| 8903 | else if (c == 'v') |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8904 | /*verify |= VERIFY_BRIEF*/; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8905 | #if DEBUG |
| 8906 | else if (c != 'p') |
| 8907 | abort(); |
| 8908 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8909 | else |
| 8910 | path = bb_default_path; |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8911 | |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 8912 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
Denys Vlasenko | cac4d00 | 2016-10-01 03:02:25 +0200 | [diff] [blame] | 8913 | cmd = *argptr; |
| 8914 | if (/*verify && */ cmd) |
| 8915 | return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8916 | |
| 8917 | return 0; |
| 8918 | } |
| 8919 | #endif |
| 8920 | |
| 8921 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8922 | /*static int funcblocksize; // size of structures in function */ |
| 8923 | /*static int funcstringsize; // size of strings in node */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8924 | static void *funcblock; /* block to allocate function from */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8925 | static char *funcstring_end; /* end of block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8926 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 8927 | static const uint8_t nodesize[N_NUMBER] ALIGN1 = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8928 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 8929 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 8930 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8931 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8932 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8933 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8934 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8935 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8936 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 8937 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8938 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8939 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 8940 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 8941 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 8942 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8943 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8944 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 8945 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8946 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8947 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8948 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8949 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8950 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8951 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8952 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8953 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8954 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8955 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8956 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8957 | }; |
| 8958 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8959 | static int calcsize(int funcblocksize, union node *n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8960 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8961 | static int |
| 8962 | sizenodelist(int funcblocksize, struct nodelist *lp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8963 | { |
| 8964 | while (lp) { |
| 8965 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8966 | funcblocksize = calcsize(funcblocksize, lp->n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8967 | lp = lp->next; |
| 8968 | } |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8969 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8970 | } |
| 8971 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8972 | static int |
| 8973 | calcsize(int funcblocksize, union node *n) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8974 | { |
| 8975 | if (n == NULL) |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8976 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8977 | funcblocksize += nodesize[n->type]; |
| 8978 | switch (n->type) { |
| 8979 | case NCMD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8980 | funcblocksize = calcsize(funcblocksize, n->ncmd.redirect); |
| 8981 | funcblocksize = calcsize(funcblocksize, n->ncmd.args); |
| 8982 | funcblocksize = calcsize(funcblocksize, n->ncmd.assign); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8983 | break; |
| 8984 | case NPIPE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8985 | funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8986 | break; |
| 8987 | case NREDIR: |
| 8988 | case NBACKGND: |
| 8989 | case NSUBSHELL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8990 | funcblocksize = calcsize(funcblocksize, n->nredir.redirect); |
| 8991 | funcblocksize = calcsize(funcblocksize, n->nredir.n); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8992 | break; |
| 8993 | case NAND: |
| 8994 | case NOR: |
| 8995 | case NSEMI: |
| 8996 | case NWHILE: |
| 8997 | case NUNTIL: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 8998 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch2); |
| 8999 | funcblocksize = calcsize(funcblocksize, n->nbinary.ch1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9000 | break; |
| 9001 | case NIF: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9002 | funcblocksize = calcsize(funcblocksize, n->nif.elsepart); |
| 9003 | funcblocksize = calcsize(funcblocksize, n->nif.ifpart); |
| 9004 | funcblocksize = calcsize(funcblocksize, n->nif.test); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9005 | break; |
| 9006 | case NFOR: |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 9007 | funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9008 | funcblocksize = calcsize(funcblocksize, n->nfor.body); |
| 9009 | funcblocksize = calcsize(funcblocksize, n->nfor.args); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9010 | break; |
| 9011 | case NCASE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9012 | funcblocksize = calcsize(funcblocksize, n->ncase.cases); |
| 9013 | funcblocksize = calcsize(funcblocksize, n->ncase.expr); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9014 | break; |
| 9015 | case NCLIST: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9016 | funcblocksize = calcsize(funcblocksize, n->nclist.body); |
| 9017 | funcblocksize = calcsize(funcblocksize, n->nclist.pattern); |
| 9018 | funcblocksize = calcsize(funcblocksize, n->nclist.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9019 | break; |
| 9020 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9021 | funcblocksize = calcsize(funcblocksize, n->ndefun.body); |
| 9022 | funcblocksize += SHELL_ALIGN(strlen(n->ndefun.text) + 1); |
| 9023 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9024 | case NARG: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9025 | funcblocksize = sizenodelist(funcblocksize, n->narg.backquote); |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 9026 | funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */ |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9027 | funcblocksize = calcsize(funcblocksize, n->narg.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9028 | break; |
| 9029 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9030 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9031 | case NTO2: |
| 9032 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9033 | case NCLOBBER: |
| 9034 | case NFROM: |
| 9035 | case NFROMTO: |
| 9036 | case NAPPEND: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9037 | funcblocksize = calcsize(funcblocksize, n->nfile.fname); |
| 9038 | funcblocksize = calcsize(funcblocksize, n->nfile.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9039 | break; |
| 9040 | case NTOFD: |
| 9041 | case NFROMFD: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9042 | funcblocksize = calcsize(funcblocksize, n->ndup.vname); |
| 9043 | funcblocksize = calcsize(funcblocksize, n->ndup.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9044 | break; |
| 9045 | case NHERE: |
| 9046 | case NXHERE: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9047 | funcblocksize = calcsize(funcblocksize, n->nhere.doc); |
| 9048 | funcblocksize = calcsize(funcblocksize, n->nhere.next); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9049 | break; |
| 9050 | case NNOT: |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9051 | funcblocksize = calcsize(funcblocksize, n->nnot.com); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9052 | break; |
| 9053 | }; |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9054 | return funcblocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9055 | } |
| 9056 | |
| 9057 | static char * |
| 9058 | nodeckstrdup(char *s) |
| 9059 | { |
Denys Vlasenko | 561639a | 2016-10-07 04:28:33 +0200 | [diff] [blame] | 9060 | funcstring_end -= SHELL_ALIGN(strlen(s) + 1); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9061 | return strcpy(funcstring_end, s); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | static union node *copynode(union node *); |
| 9065 | |
| 9066 | static struct nodelist * |
| 9067 | copynodelist(struct nodelist *lp) |
| 9068 | { |
| 9069 | struct nodelist *start; |
| 9070 | struct nodelist **lpp; |
| 9071 | |
| 9072 | lpp = &start; |
| 9073 | while (lp) { |
| 9074 | *lpp = funcblock; |
| 9075 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 9076 | (*lpp)->n = copynode(lp->n); |
| 9077 | lp = lp->next; |
| 9078 | lpp = &(*lpp)->next; |
| 9079 | } |
| 9080 | *lpp = NULL; |
| 9081 | return start; |
| 9082 | } |
| 9083 | |
| 9084 | static union node * |
| 9085 | copynode(union node *n) |
| 9086 | { |
| 9087 | union node *new; |
| 9088 | |
| 9089 | if (n == NULL) |
| 9090 | return NULL; |
| 9091 | new = funcblock; |
| 9092 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 9093 | |
| 9094 | switch (n->type) { |
| 9095 | case NCMD: |
| 9096 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 9097 | new->ncmd.args = copynode(n->ncmd.args); |
| 9098 | new->ncmd.assign = copynode(n->ncmd.assign); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9099 | new->ncmd.linno = n->ncmd.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9100 | break; |
| 9101 | case NPIPE: |
| 9102 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9103 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9104 | break; |
| 9105 | case NREDIR: |
| 9106 | case NBACKGND: |
| 9107 | case NSUBSHELL: |
| 9108 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 9109 | new->nredir.n = copynode(n->nredir.n); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9110 | new->nredir.linno = n->nredir.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9111 | break; |
| 9112 | case NAND: |
| 9113 | case NOR: |
| 9114 | case NSEMI: |
| 9115 | case NWHILE: |
| 9116 | case NUNTIL: |
| 9117 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 9118 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 9119 | break; |
| 9120 | case NIF: |
| 9121 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 9122 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 9123 | new->nif.test = copynode(n->nif.test); |
| 9124 | break; |
| 9125 | case NFOR: |
| 9126 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 9127 | new->nfor.body = copynode(n->nfor.body); |
| 9128 | new->nfor.args = copynode(n->nfor.args); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9129 | new->nfor.linno = n->nfor.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9130 | break; |
| 9131 | case NCASE: |
| 9132 | new->ncase.cases = copynode(n->ncase.cases); |
| 9133 | new->ncase.expr = copynode(n->ncase.expr); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9134 | new->ncase.linno = n->ncase.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9135 | break; |
| 9136 | case NCLIST: |
| 9137 | new->nclist.body = copynode(n->nclist.body); |
| 9138 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 9139 | new->nclist.next = copynode(n->nclist.next); |
| 9140 | break; |
| 9141 | case NDEFUN: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9142 | new->ndefun.body = copynode(n->ndefun.body); |
| 9143 | new->ndefun.text = nodeckstrdup(n->ndefun.text); |
| 9144 | new->ndefun.linno = n->ndefun.linno; |
| 9145 | break; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9146 | case NARG: |
| 9147 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 9148 | new->narg.text = nodeckstrdup(n->narg.text); |
| 9149 | new->narg.next = copynode(n->narg.next); |
| 9150 | break; |
| 9151 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9152 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9153 | case NTO2: |
| 9154 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9155 | case NCLOBBER: |
| 9156 | case NFROM: |
| 9157 | case NFROMTO: |
| 9158 | case NAPPEND: |
| 9159 | new->nfile.fname = copynode(n->nfile.fname); |
| 9160 | new->nfile.fd = n->nfile.fd; |
| 9161 | new->nfile.next = copynode(n->nfile.next); |
| 9162 | break; |
| 9163 | case NTOFD: |
| 9164 | case NFROMFD: |
| 9165 | new->ndup.vname = copynode(n->ndup.vname); |
| 9166 | new->ndup.dupfd = n->ndup.dupfd; |
| 9167 | new->ndup.fd = n->ndup.fd; |
| 9168 | new->ndup.next = copynode(n->ndup.next); |
| 9169 | break; |
| 9170 | case NHERE: |
| 9171 | case NXHERE: |
| 9172 | new->nhere.doc = copynode(n->nhere.doc); |
| 9173 | new->nhere.fd = n->nhere.fd; |
| 9174 | new->nhere.next = copynode(n->nhere.next); |
| 9175 | break; |
| 9176 | case NNOT: |
| 9177 | new->nnot.com = copynode(n->nnot.com); |
| 9178 | break; |
| 9179 | }; |
| 9180 | new->type = n->type; |
| 9181 | return new; |
| 9182 | } |
| 9183 | |
| 9184 | /* |
| 9185 | * Make a copy of a parse tree. |
| 9186 | */ |
| 9187 | static struct funcnode * |
| 9188 | copyfunc(union node *n) |
| 9189 | { |
| 9190 | struct funcnode *f; |
| 9191 | size_t blocksize; |
| 9192 | |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9193 | /*funcstringsize = 0;*/ |
| 9194 | blocksize = offsetof(struct funcnode, n) + calcsize(0, n); |
| 9195 | f = ckzalloc(blocksize /* + funcstringsize */); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9196 | funcblock = (char *) f + offsetof(struct funcnode, n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9197 | funcstring_end = (char *) f + blocksize; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9198 | copynode(n); |
Denys Vlasenko | 4c438b5 | 2016-10-07 04:05:15 +0200 | [diff] [blame] | 9199 | /* f->count = 0; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9200 | return f; |
| 9201 | } |
| 9202 | |
| 9203 | /* |
| 9204 | * Define a shell function. |
| 9205 | */ |
| 9206 | static void |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9207 | defun(union node *func) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9208 | { |
| 9209 | struct cmdentry entry; |
| 9210 | |
| 9211 | INT_OFF; |
| 9212 | entry.cmdtype = CMDFUNCTION; |
| 9213 | entry.u.func = copyfunc(func); |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9214 | addcmdentry(func->ndefun.text, &entry); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9215 | INT_ON; |
| 9216 | } |
| 9217 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9218 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9219 | #define SKIPBREAK (1 << 0) |
| 9220 | #define SKIPCONT (1 << 1) |
| 9221 | #define SKIPFUNC (1 << 2) |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9222 | #define SKIPFUNCDEF (1 << 3) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9223 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9224 | static int skipcount; /* number of levels to skip */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 9225 | static int loopnest; /* current loop nesting level */ |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9226 | 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] | 9227 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9228 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9229 | static int evalstring(char *s, int flags); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9230 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9231 | /* Called to execute a trap. |
| 9232 | * Single callsite - at the end of evaltree(). |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9233 | * If we return non-zero, evaltree raises EXEXIT exception. |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9234 | * |
| 9235 | * Perhaps we should avoid entering new trap handlers |
| 9236 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9237 | */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9238 | static void |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9239 | dotrap(void) |
| 9240 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9241 | uint8_t *g; |
| 9242 | int sig; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9243 | int status, last_status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9244 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9245 | if (!pending_sig) |
| 9246 | return; |
| 9247 | |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9248 | status = savestatus; |
| 9249 | last_status = status; |
| 9250 | if (status < 0) { |
| 9251 | status = exitstatus; |
| 9252 | savestatus = status; |
| 9253 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9254 | pending_sig = 0; |
Denys Vlasenko | de89205 | 2016-10-02 01:49:13 +0200 | [diff] [blame] | 9255 | barrier(); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9256 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9257 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9258 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9259 | char *p; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9260 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9261 | if (!*g) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9262 | continue; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9263 | |
| 9264 | if (evalskip) { |
| 9265 | pending_sig = sig; |
| 9266 | break; |
| 9267 | } |
| 9268 | |
| 9269 | p = trap[sig]; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9270 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 9271 | * don't upset it by resetting gotsig[SIGINT-1] */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9272 | if (sig == SIGINT && !p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9273 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9274 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9275 | TRACE(("sig %d is active, will run handler '%s'\n", sig, p)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9276 | *g = 0; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9277 | if (!p) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 9278 | continue; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9279 | trap_depth++; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9280 | evalstring(p, 0); |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9281 | trap_depth--; |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9282 | if (evalskip != SKIPFUNC) |
| 9283 | exitstatus = status; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9284 | } |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 9285 | |
| 9286 | savestatus = last_status; |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9287 | TRACE(("dotrap returns\n")); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 9288 | } |
| 9289 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9290 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9291 | static int evalloop(union node *, int); |
| 9292 | static int evalfor(union node *, int); |
| 9293 | static int evalcase(union node *, int); |
| 9294 | static int evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9295 | static void expredir(union node *); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9296 | static int evalpipe(union node *, int); |
| 9297 | static int evalcommand(union node *, int); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 9298 | static int evalbltin(const struct builtincmd *, int, char **, int); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9299 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9300 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9301 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9302 | * Evaluate a parse tree. The value is left in the global variable |
| 9303 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9304 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9305 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9306 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9307 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9308 | int checkexit = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9309 | int (*evalfn)(union node *, int); |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9310 | struct stackmark smark; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9311 | int status = 0; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9312 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9313 | setstackmark(&smark); |
| 9314 | |
Denys Vlasenko | 41beb53 | 2021-09-07 01:52:21 +0200 | [diff] [blame] | 9315 | if (nflag) |
| 9316 | goto out; |
| 9317 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9318 | if (n == NULL) { |
| 9319 | TRACE(("evaltree(NULL) called\n")); |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 9320 | goto out; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9321 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9322 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9323 | |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9324 | dotrap(); |
| 9325 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9326 | switch (n->type) { |
| 9327 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9328 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9329 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9330 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9331 | break; |
| 9332 | #endif |
| 9333 | case NNOT: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9334 | status = !evaltree(n->nnot.com, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9335 | goto setstatus; |
| 9336 | case NREDIR: |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9337 | errlinno = lineno = n->nredir.linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9338 | expredir(n->nredir.redirect); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 9339 | pushredir(n->nredir.redirect); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9340 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 9341 | if (!status) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9342 | status = evaltree(n->nredir.n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9343 | } |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 9344 | if (n->nredir.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 9345 | popredir(/*drop:*/ 0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9346 | goto setstatus; |
| 9347 | case NCMD: |
| 9348 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9349 | checkexit: |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9350 | checkexit = ~flags & EV_TESTED; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9351 | goto calleval; |
| 9352 | case NFOR: |
| 9353 | evalfn = evalfor; |
| 9354 | goto calleval; |
| 9355 | case NWHILE: |
| 9356 | case NUNTIL: |
| 9357 | evalfn = evalloop; |
| 9358 | goto calleval; |
| 9359 | case NSUBSHELL: |
| 9360 | case NBACKGND: |
| 9361 | evalfn = evalsubshell; |
Denys Vlasenko | cf98b0c | 2016-10-25 18:19:39 +0200 | [diff] [blame] | 9362 | goto checkexit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9363 | case NPIPE: |
| 9364 | evalfn = evalpipe; |
| 9365 | goto checkexit; |
| 9366 | case NCASE: |
| 9367 | evalfn = evalcase; |
| 9368 | goto calleval; |
| 9369 | case NAND: |
| 9370 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9371 | case NSEMI: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9372 | #if NAND + 1 != NOR |
| 9373 | #error NAND + 1 != NOR |
| 9374 | #endif |
| 9375 | #if NOR + 1 != NSEMI |
| 9376 | #error NOR + 1 != NSEMI |
| 9377 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 9378 | unsigned is_or = n->type - NAND; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9379 | status = evaltree( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9380 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9381 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9382 | ); |
Denys Vlasenko | bc1a008 | 2016-10-02 15:31:33 +0200 | [diff] [blame] | 9383 | if ((!status) == is_or || evalskip) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9384 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9385 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9386 | evaln: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9387 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9388 | calleval: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9389 | status = evalfn(n, flags); |
| 9390 | goto setstatus; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 9391 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9392 | case NIF: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9393 | status = evaltree(n->nif.test, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9394 | if (evalskip) |
| 9395 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9396 | if (!status) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9397 | n = n->nif.ifpart; |
| 9398 | goto evaln; |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9399 | } else if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9400 | n = n->nif.elsepart; |
| 9401 | goto evaln; |
| 9402 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9403 | status = 0; |
| 9404 | goto setstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9405 | case NDEFUN: |
Denys Vlasenko | 7aec868 | 2016-10-25 20:26:02 +0200 | [diff] [blame] | 9406 | defun(n); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9407 | /* Not necessary. To test it: |
| 9408 | * "false; f() { qwerty; }; echo $?" should print 0. |
| 9409 | */ |
| 9410 | /* status = 0; */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9411 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9412 | exitstatus = status; |
| 9413 | break; |
| 9414 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9415 | out: |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9416 | /* Order of checks below is important: |
| 9417 | * signal handlers trigger before exit caused by "set -e". |
| 9418 | */ |
Denys Vlasenko | b98b4c1 | 2016-10-01 23:25:12 +0200 | [diff] [blame] | 9419 | dotrap(); |
| 9420 | |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9421 | if (checkexit && status) { |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9422 | if (trap[NTRAP_ERR] && !in_trap_ERR) { |
| 9423 | int err; |
| 9424 | struct jmploc *volatile savehandler = exception_handler; |
| 9425 | struct jmploc jmploc; |
| 9426 | |
| 9427 | in_trap_ERR = 1; |
| 9428 | trap_depth++; |
| 9429 | err = setjmp(jmploc.loc); |
| 9430 | if (!err) { |
| 9431 | exception_handler = &jmploc; |
| 9432 | savestatus = exitstatus; |
| 9433 | evalstring(trap[NTRAP_ERR], 0); |
| 9434 | } |
| 9435 | trap_depth--; |
| 9436 | in_trap_ERR = 0; |
| 9437 | |
| 9438 | exception_handler = savehandler; |
| 9439 | if (err && exception_type != EXERROR) |
| 9440 | longjmp(exception_handler->loc, 1); |
| 9441 | |
| 9442 | exitstatus = savestatus; |
| 9443 | } |
| 9444 | if (eflag) |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9445 | goto exexit; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9446 | } |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9447 | if (flags & EV_EXIT) { |
| 9448 | exexit: |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 9449 | raise_exception(EXEND); |
Denys Vlasenko | f415e21 | 2021-09-07 01:54:23 +0200 | [diff] [blame] | 9450 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9451 | |
Ron Yorston | f55161a | 2019-02-25 08:29:38 +0000 | [diff] [blame] | 9452 | popstackmark(&smark); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9453 | TRACE(("leaving evaltree (no interrupts)\n")); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9454 | return exitstatus; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9455 | } |
| 9456 | |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 9457 | static int |
| 9458 | skiploop(void) |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9459 | { |
| 9460 | int skip = evalskip; |
| 9461 | |
| 9462 | switch (skip) { |
| 9463 | case 0: |
| 9464 | break; |
| 9465 | case SKIPBREAK: |
| 9466 | case SKIPCONT: |
| 9467 | if (--skipcount <= 0) { |
| 9468 | evalskip = 0; |
| 9469 | break; |
| 9470 | } |
| 9471 | skip = SKIPBREAK; |
| 9472 | break; |
| 9473 | } |
| 9474 | return skip; |
| 9475 | } |
| 9476 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9477 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9478 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9479 | { |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9480 | int skip; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9481 | int status; |
| 9482 | |
| 9483 | loopnest++; |
| 9484 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9485 | flags &= EV_TESTED; |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9486 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9487 | int i; |
| 9488 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9489 | i = evaltree(n->nbinary.ch1, EV_TESTED); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9490 | skip = skiploop(); |
| 9491 | if (skip == SKIPFUNC) |
| 9492 | status = i; |
| 9493 | if (skip) |
| 9494 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9495 | if (n->type != NWHILE) |
| 9496 | i = !i; |
| 9497 | if (i != 0) |
| 9498 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9499 | status = evaltree(n->nbinary.ch2, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9500 | skip = skiploop(); |
| 9501 | } while (!(skip & ~SKIPCONT)); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9502 | loopnest--; |
| 9503 | |
| 9504 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9505 | } |
| 9506 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9507 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9508 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9509 | { |
| 9510 | struct arglist arglist; |
| 9511 | union node *argp; |
| 9512 | struct strlist *sp; |
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 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9515 | errlinno = lineno = n->ncase.linno; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9516 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9517 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9518 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9519 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 9520 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9521 | } |
| 9522 | *arglist.lastp = NULL; |
| 9523 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9524 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9525 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9526 | for (sp = arglist.list; sp; sp = sp->next) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9527 | setvar0(n->nfor.var, sp->text); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9528 | status = evaltree(n->nfor.body, flags); |
Denys Vlasenko | 35ec818 | 2016-10-01 19:56:52 +0200 | [diff] [blame] | 9529 | if (skiploop() & ~SKIPCONT) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9530 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9531 | } |
| 9532 | loopnest--; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9533 | |
| 9534 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9535 | } |
| 9536 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9537 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9538 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9539 | { |
| 9540 | union node *cp; |
| 9541 | union node *patp; |
| 9542 | struct arglist arglist; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9543 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9544 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9545 | errlinno = lineno = n->ncase.linno; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9546 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9547 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9548 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9549 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9550 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 9551 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9552 | if (casematch(patp, arglist.list->text)) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9553 | /* Ensure body is non-empty as otherwise |
| 9554 | * EV_EXIT may prevent us from setting the |
| 9555 | * exit status. |
| 9556 | */ |
| 9557 | if (evalskip == 0 && cp->nclist.body) { |
| 9558 | status = evaltree(cp->nclist.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9559 | } |
| 9560 | goto out; |
| 9561 | } |
| 9562 | } |
| 9563 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9564 | out: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9565 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9568 | /* |
| 9569 | * Kick off a subshell to evaluate a tree. |
| 9570 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9571 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9572 | evalsubshell(union node *n, int flags) |
| 9573 | { |
| 9574 | struct job *jp; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9575 | 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] | 9576 | int status; |
| 9577 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9578 | errlinno = lineno = n->nredir.linno; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9579 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9580 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9581 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9582 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9583 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9584 | if (backgnd == FORK_FG) |
| 9585 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9586 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9587 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9588 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9589 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9590 | flags |= EV_EXIT; |
| 9591 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9592 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 9593 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9594 | redirect(n->nredir.redirect, 0); |
| 9595 | evaltreenr(n->nredir.n, flags); |
| 9596 | /* never returns */ |
| 9597 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9598 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9599 | status = 0; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9600 | if (backgnd == FORK_FG) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9601 | status = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9602 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9603 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9606 | /* |
| 9607 | * Compute the names of the files in a redirection list. |
| 9608 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9609 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9610 | static void |
| 9611 | expredir(union node *n) |
| 9612 | { |
| 9613 | union node *redir; |
| 9614 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9615 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9616 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9617 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 9618 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9619 | fn.lastp = &fn.list; |
| 9620 | switch (redir->type) { |
| 9621 | case NFROMTO: |
| 9622 | case NFROM: |
| 9623 | case NTO: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9624 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9625 | case NTO2: |
| 9626 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9627 | case NCLOBBER: |
| 9628 | case NAPPEND: |
| 9629 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 9630 | TRACE(("expredir expanded to '%s'\n", fn.list->text)); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9631 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9632 | store_expfname: |
| 9633 | #endif |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9634 | #if 0 |
| 9635 | // By the design of stack allocator, the loop of this kind: |
| 9636 | // while true; do while true; do break; done </dev/null; done |
| 9637 | // will look like a memory leak: ash plans to free expfname's |
| 9638 | // of "/dev/null" as soon as it finishes running the loop |
| 9639 | // (in this case, never). |
| 9640 | // This "fix" is wrong: |
Jon Tollefson | 4ba6c5d | 2012-11-13 19:26:53 +0100 | [diff] [blame] | 9641 | if (redir->nfile.expfname) |
| 9642 | stunalloc(redir->nfile.expfname); |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 9643 | // It results in corrupted state of stacked allocations. |
| 9644 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9645 | redir->nfile.expfname = fn.list->text; |
| 9646 | break; |
| 9647 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9648 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9649 | if (redir->ndup.vname) { |
Denys Vlasenko | e368d85 | 2020-02-16 19:02:22 +0100 | [diff] [blame] | 9650 | expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9651 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9652 | ash_msg_and_raise_error("redir error"); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 9653 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 9654 | if (!isdigit_str9(fn.list->text)) { |
| 9655 | /* >&file, not >&fd */ |
| 9656 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 9657 | ash_msg_and_raise_error("redir error"); |
| 9658 | redir->type = NTO2; |
| 9659 | goto store_expfname; |
| 9660 | } |
| 9661 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9662 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9663 | } |
| 9664 | break; |
| 9665 | } |
| 9666 | } |
| 9667 | } |
| 9668 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9669 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9670 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 9671 | * of the process creating the pipeline. (This differs from some versions |
| 9672 | * of the shell, which make the last process in a pipeline the parent |
| 9673 | * of all the rest.) |
| 9674 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9675 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9676 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9677 | { |
| 9678 | struct job *jp; |
| 9679 | struct nodelist *lp; |
| 9680 | int pipelen; |
| 9681 | int prevfd; |
| 9682 | int pip[2]; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9683 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9684 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9685 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9686 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9687 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9688 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9689 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9690 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 9691 | if (n->npipe.pipe_backgnd == 0) |
| 9692 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9693 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9694 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9695 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9696 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9697 | pip[1] = -1; |
| 9698 | if (lp->next) { |
| 9699 | if (pipe(pip) < 0) { |
| 9700 | close(prevfd); |
Denys Vlasenko | 12ffefb | 2017-08-23 14:52:56 +0200 | [diff] [blame] | 9701 | ash_msg_and_raise_perror("can't create pipe"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9702 | } |
| 9703 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9704 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9705 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9706 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9707 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9708 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9709 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9710 | if (prevfd > 0) { |
| 9711 | dup2(prevfd, 0); |
| 9712 | close(prevfd); |
| 9713 | } |
| 9714 | if (pip[1] > 1) { |
| 9715 | dup2(pip[1], 1); |
| 9716 | close(pip[1]); |
| 9717 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9718 | evaltreenr(lp->n, flags); |
| 9719 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9720 | } |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 9721 | /* parent */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9722 | if (prevfd >= 0) |
| 9723 | close(prevfd); |
| 9724 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 9725 | /* Don't want to trigger debugging */ |
| 9726 | if (pip[1] != -1) |
| 9727 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9728 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 9729 | if (n->npipe.pipe_backgnd == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9730 | status = waitforjob(jp); |
| 9731 | TRACE(("evalpipe: job done exit status %d\n", status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9732 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9733 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9734 | |
| 9735 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9736 | } |
| 9737 | |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9738 | /* setinteractive needs this forward reference */ |
Denys Vlasenko | 27be0e8 | 2023-01-03 08:28:16 +0100 | [diff] [blame] | 9739 | #if ENABLE_FEATURE_TAB_COMPLETION |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9740 | static const char *get_builtin_name(int i) FAST_FUNC; |
| 9741 | #endif |
| 9742 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9743 | /* |
| 9744 | * Controls whether the shell is interactive or not. |
| 9745 | */ |
| 9746 | static void |
| 9747 | setinteractive(int on) |
| 9748 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9749 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9750 | |
| 9751 | if (++on == is_interactive) |
| 9752 | return; |
| 9753 | is_interactive = on; |
| 9754 | setsignal(SIGINT); |
| 9755 | setsignal(SIGQUIT); |
| 9756 | setsignal(SIGTERM); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9757 | if (is_interactive > 1) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9758 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9759 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9760 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9761 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9762 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9763 | /* note: ash and hush share this string */ |
| 9764 | out1fmt("\n\n%s %s\n" |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9765 | IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9766 | "\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9767 | bb_banner, |
| 9768 | "built-in shell (ash)" |
| 9769 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 9770 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9771 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9772 | #endif |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9773 | #if ENABLE_FEATURE_EDITING |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9774 | if (!line_input_state) { |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9775 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
Denys Vlasenko | 27be0e8 | 2023-01-03 08:28:16 +0100 | [diff] [blame] | 9776 | # if ENABLE_FEATURE_TAB_COMPLETION |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9777 | line_input_state->get_exe_name = get_builtin_name; |
Denys Vlasenko | 27be0e8 | 2023-01-03 08:28:16 +0100 | [diff] [blame] | 9778 | # endif |
| 9779 | # if EDITING_HAS_sh_get_var |
Ron Yorston | 7d1c7d8 | 2022-03-24 12:17:25 +0000 | [diff] [blame] | 9780 | line_input_state->sh_get_var = lookupvar; |
Denys Vlasenko | 27be0e8 | 2023-01-03 08:28:16 +0100 | [diff] [blame] | 9781 | # endif |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 9782 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9783 | #endif |
| 9784 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9785 | } |
| 9786 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9787 | static void |
| 9788 | optschanged(void) |
| 9789 | { |
| 9790 | #if DEBUG |
| 9791 | opentrace(); |
| 9792 | #endif |
| 9793 | setinteractive(iflag); |
| 9794 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9795 | #if ENABLE_FEATURE_EDITING_VI |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 9796 | if (line_input_state) { |
| 9797 | if (viflag) |
| 9798 | line_input_state->flags |= VI_MODE; |
| 9799 | else |
| 9800 | line_input_state->flags &= ~VI_MODE; |
| 9801 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9802 | #else |
| 9803 | viflag = 0; /* forcibly keep the option off */ |
| 9804 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9805 | } |
| 9806 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9807 | struct localvar_list { |
| 9808 | struct localvar_list *next; |
| 9809 | struct localvar *lv; |
| 9810 | }; |
| 9811 | |
| 9812 | static struct localvar_list *localvar_stack; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9813 | |
| 9814 | /* |
| 9815 | * Called after a function returns. |
| 9816 | * Interrupts must be off. |
| 9817 | */ |
| 9818 | static void |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9819 | poplocalvars(int keep) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9820 | { |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9821 | struct localvar_list *ll; |
| 9822 | struct localvar *lvp, *next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9823 | struct var *vp; |
| 9824 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9825 | INT_OFF; |
| 9826 | ll = localvar_stack; |
| 9827 | localvar_stack = ll->next; |
| 9828 | |
| 9829 | next = ll->lv; |
| 9830 | free(ll); |
| 9831 | |
| 9832 | while ((lvp = next) != NULL) { |
| 9833 | next = lvp->next; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9834 | vp = lvp->vp; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 9835 | TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-")); |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 9836 | if (keep) { |
| 9837 | int bits = VSTRFIXED; |
| 9838 | |
| 9839 | if (lvp->flags != VUNSET) { |
| 9840 | if (vp->var_text == lvp->text) |
| 9841 | bits |= VTEXTFIXED; |
| 9842 | else if (!(lvp->flags & (VTEXTFIXED|VSTACK))) |
| 9843 | free((char*)lvp->text); |
| 9844 | } |
| 9845 | |
| 9846 | vp->flags &= ~bits; |
| 9847 | vp->flags |= (lvp->flags & bits); |
| 9848 | |
| 9849 | if ((vp->flags & |
| 9850 | (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET) |
| 9851 | unsetvar(vp->var_text); |
| 9852 | } else if (vp == NULL) { /* $- saved */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9853 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 9854 | free((char*)lvp->text); |
| 9855 | optschanged(); |
Denys Vlasenko | d5b500c | 2017-07-26 19:25:40 +0200 | [diff] [blame] | 9856 | } else if (lvp->flags == VUNSET) { |
| 9857 | vp->flags &= ~(VSTRFIXED|VREADONLY); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9858 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9859 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9860 | if (vp->var_func) |
| 9861 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9862 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9863 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9864 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9865 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9866 | } |
| 9867 | free(lvp); |
| 9868 | } |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9869 | INT_ON; |
| 9870 | } |
| 9871 | |
| 9872 | /* |
| 9873 | * Create a new localvar environment. |
| 9874 | */ |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9875 | static struct localvar_list * |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9876 | pushlocalvars(int push) |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9877 | { |
| 9878 | struct localvar_list *ll; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9879 | struct localvar_list *top; |
| 9880 | |
| 9881 | top = localvar_stack; |
| 9882 | if (!push) |
| 9883 | goto out; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9884 | |
| 9885 | INT_OFF; |
| 9886 | ll = ckzalloc(sizeof(*ll)); |
| 9887 | /*ll->lv = NULL; - zalloc did it */ |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9888 | ll->next = top; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9889 | localvar_stack = ll; |
| 9890 | INT_ON; |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 9891 | out: |
| 9892 | return top; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 9893 | } |
| 9894 | |
| 9895 | static void |
| 9896 | unwindlocalvars(struct localvar_list *stop) |
| 9897 | { |
| 9898 | while (localvar_stack != stop) |
| 9899 | poplocalvars(0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9900 | } |
| 9901 | |
| 9902 | static int |
| 9903 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 9904 | { |
| 9905 | volatile struct shparam saveparam; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9906 | struct jmploc *volatile savehandler; |
| 9907 | struct jmploc jmploc; |
| 9908 | int e; |
Denys Vlasenko | d6c9cbc | 2021-09-07 18:01:49 +0200 | [diff] [blame] | 9909 | int savelineno; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9910 | int savefuncline; |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 9911 | char *savefuncname; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9912 | char *savetrap = NULL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9913 | |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9914 | if (!Eflag) { |
| 9915 | savetrap = trap[NTRAP_ERR]; |
| 9916 | trap[NTRAP_ERR] = NULL; |
| 9917 | } |
Denys Vlasenko | d6c9cbc | 2021-09-07 18:01:49 +0200 | [diff] [blame] | 9918 | savelineno = lineno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9919 | saveparam = shellparam; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9920 | savefuncline = funcline; |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 9921 | savefuncname = funcname; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 9922 | savehandler = exception_handler; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9923 | e = setjmp(jmploc.loc); |
| 9924 | if (e) { |
| 9925 | goto funcdone; |
| 9926 | } |
| 9927 | INT_OFF; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9928 | exception_handler = &jmploc; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9929 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9930 | func->count++; |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 9931 | funcname = func->n.ndefun.text; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9932 | funcline = func->n.ndefun.linno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9933 | INT_ON; |
| 9934 | shellparam.nparam = argc - 1; |
| 9935 | shellparam.p = argv + 1; |
| 9936 | #if ENABLE_ASH_GETOPTS |
| 9937 | shellparam.optind = 1; |
| 9938 | shellparam.optoff = -1; |
| 9939 | #endif |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9940 | evaltree(func->n.ndefun.body, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9941 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9942 | INT_OFF; |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 9943 | funcname = savefuncname; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 9944 | if (savetrap) { |
| 9945 | if (!trap[NTRAP_ERR]) |
| 9946 | trap[NTRAP_ERR] = savetrap; |
| 9947 | else |
| 9948 | free(savetrap); |
| 9949 | } |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 9950 | funcline = savefuncline; |
Denys Vlasenko | d6c9cbc | 2021-09-07 18:01:49 +0200 | [diff] [blame] | 9951 | lineno = savelineno; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9952 | freefunc(func); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9953 | freeparam(&shellparam); |
| 9954 | shellparam = saveparam; |
| 9955 | exception_handler = savehandler; |
| 9956 | INT_ON; |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 9957 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9958 | return e; |
| 9959 | } |
| 9960 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9961 | /* |
| 9962 | * Make a variable a local variable. When a variable is made local, it's |
| 9963 | * value and flags are saved in a localvar structure. The saved values |
| 9964 | * will be restored when the shell function returns. We handle the name |
Denys Vlasenko | e0a4e10 | 2015-05-13 02:20:14 +0200 | [diff] [blame] | 9965 | * "-" as a special case: it makes changes to "set +-options" local |
| 9966 | * (options will be restored on return from the function). |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9967 | */ |
| 9968 | static void |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 9969 | mklocal(char *name, int flags) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9970 | { |
| 9971 | struct localvar *lvp; |
| 9972 | struct var **vpp; |
| 9973 | struct var *vp; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9974 | char *eq = strchr(name, '='); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9975 | |
| 9976 | INT_OFF; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9977 | /* Cater for duplicate "local". Examples: |
| 9978 | * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x |
| 9979 | * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x |
| 9980 | */ |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 9981 | lvp = localvar_stack->lv; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9982 | while (lvp) { |
Eugene Rudoy | 1285aa6 | 2015-04-26 23:32:00 +0200 | [diff] [blame] | 9983 | if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9984 | if (eq) |
| 9985 | setvareq(name, 0); |
| 9986 | /* else: |
| 9987 | * it's a duplicate "local VAR" declaration, do nothing |
| 9988 | */ |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 9989 | goto ret; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9990 | } |
| 9991 | lvp = lvp->next; |
| 9992 | } |
| 9993 | |
| 9994 | lvp = ckzalloc(sizeof(*lvp)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9995 | if (LONE_DASH(name)) { |
| 9996 | char *p; |
| 9997 | p = ckmalloc(sizeof(optlist)); |
| 9998 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 9999 | vp = NULL; |
| 10000 | } else { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10001 | vpp = hashvar(name); |
| 10002 | vp = *findvar(vpp, name); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10003 | if (vp == NULL) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10004 | /* variable did not exist yet */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10005 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10006 | vp = setvareq(name, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10007 | else |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10008 | vp = setvar(name, NULL, VSTRFIXED | flags); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10009 | lvp->flags = VUNSET; |
| 10010 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10011 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10012 | lvp->flags = vp->flags; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10013 | /* make sure neither "struct var" nor string gets freed |
| 10014 | * during (un)setting: |
| 10015 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10016 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 10017 | if (eq) |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10018 | setvareq(name, flags); |
Denys Vlasenko | 109ee5d | 2014-03-16 18:41:11 +0100 | [diff] [blame] | 10019 | else |
| 10020 | /* "local VAR" unsets VAR: */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10021 | setvar0(name, NULL); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10022 | } |
| 10023 | } |
| 10024 | lvp->vp = vp; |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 10025 | lvp->next = localvar_stack->lv; |
| 10026 | localvar_stack->lv = lvp; |
Denys Vlasenko | 06b1149 | 2016-11-04 16:43:18 +0100 | [diff] [blame] | 10027 | ret: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10028 | INT_ON; |
| 10029 | } |
| 10030 | |
| 10031 | /* |
| 10032 | * The "local" command. |
| 10033 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10034 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10035 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10036 | { |
| 10037 | char *name; |
| 10038 | |
Denys Vlasenko | b8ab27b | 2017-07-26 19:22:34 +0200 | [diff] [blame] | 10039 | if (!localvar_stack) |
Ron Yorston | ef2386b | 2015-10-29 16:19:14 +0000 | [diff] [blame] | 10040 | ash_msg_and_raise_error("not in a function"); |
| 10041 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10042 | argv = argptr; |
| 10043 | while ((name = *argv++) != NULL) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10044 | mklocal(name, 0); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10045 | } |
| 10046 | return 0; |
| 10047 | } |
| 10048 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10049 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10050 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10051 | { |
| 10052 | return 1; |
| 10053 | } |
| 10054 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10055 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10056 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10057 | { |
| 10058 | return 0; |
| 10059 | } |
| 10060 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10061 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10062 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10063 | { |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 10064 | optionarg = NULL; |
| 10065 | while (nextopt("a:") != '\0') |
| 10066 | /* nextopt() sets optionarg to "-a ARGV0" */; |
| 10067 | |
| 10068 | argv = argptr; |
| 10069 | if (argv[0]) { |
| 10070 | char *prog; |
| 10071 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10072 | iflag = 0; /* exit on error */ |
| 10073 | mflag = 0; |
| 10074 | optschanged(); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 10075 | /* We should set up signals for "exec CMD" |
| 10076 | * the same way as for "CMD" without "exec". |
| 10077 | * But optschanged->setinteractive->setsignal |
| 10078 | * still thought we are a root shell. Therefore, for example, |
| 10079 | * SIGQUIT is still set to IGN. Fix it: |
| 10080 | */ |
| 10081 | shlvl++; |
| 10082 | setsignal(SIGQUIT); |
| 10083 | /*setsignal(SIGTERM); - unnecessary because of iflag=0 */ |
| 10084 | /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */ |
| 10085 | /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */ |
| 10086 | |
Denys Vlasenko | 6c149f4 | 2017-04-12 21:31:32 +0200 | [diff] [blame] | 10087 | prog = argv[0]; |
| 10088 | if (optionarg) |
| 10089 | argv[0] = optionarg; |
| 10090 | shellexec(prog, argv, pathval(), 0); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 10091 | /* NOTREACHED */ |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10092 | } |
| 10093 | return 0; |
| 10094 | } |
| 10095 | |
| 10096 | /* |
| 10097 | * The return command. |
| 10098 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10099 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10100 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10101 | { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 10102 | int skip; |
| 10103 | int status; |
| 10104 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10105 | /* |
| 10106 | * If called outside a function, do what ksh does; |
| 10107 | * skip the rest of the file. |
| 10108 | */ |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 10109 | if (argv[1]) { |
| 10110 | skip = SKIPFUNC; |
| 10111 | status = number(argv[1]); |
| 10112 | } else { |
| 10113 | skip = SKIPFUNCDEF; |
| 10114 | status = exitstatus; |
| 10115 | } |
| 10116 | evalskip = skip; |
| 10117 | |
| 10118 | return status; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10119 | } |
| 10120 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10121 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10122 | static int breakcmd(int, char **) FAST_FUNC; |
| 10123 | static int dotcmd(int, char **) FAST_FUNC; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10124 | static int evalcmd(int, char **, int) FAST_FUNC; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10125 | static int exitcmd(int, char **) FAST_FUNC; |
| 10126 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10127 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10128 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10129 | #endif |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 10130 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10131 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 10132 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 10133 | #if MAX_HISTORY |
| 10134 | static int historycmd(int, char **) FAST_FUNC; |
| 10135 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 10136 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10137 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10138 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10139 | static int readcmd(int, char **) FAST_FUNC; |
| 10140 | static int setcmd(int, char **) FAST_FUNC; |
| 10141 | static int shiftcmd(int, char **) FAST_FUNC; |
| 10142 | static int timescmd(int, char **) FAST_FUNC; |
| 10143 | static int trapcmd(int, char **) FAST_FUNC; |
| 10144 | static int umaskcmd(int, char **) FAST_FUNC; |
| 10145 | static int unsetcmd(int, char **) FAST_FUNC; |
| 10146 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10147 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10148 | #define BUILTIN_NOSPEC "0" |
| 10149 | #define BUILTIN_SPECIAL "1" |
| 10150 | #define BUILTIN_REGULAR "2" |
| 10151 | #define BUILTIN_SPEC_REG "3" |
| 10152 | #define BUILTIN_ASSIGN "4" |
| 10153 | #define BUILTIN_SPEC_ASSG "5" |
| 10154 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10155 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 10156 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10157 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10158 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10159 | 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] | 10160 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10161 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10162 | 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] | 10163 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10164 | #if ENABLE_ASH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10165 | 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] | 10166 | #endif |
Shawn Landden | 58598eb | 2022-08-27 19:56:21 +0200 | [diff] [blame] | 10167 | #if ENABLE_ASH_SLEEP |
| 10168 | static int FAST_FUNC sleepcmd(int argc, char **argv) { return sleep_main(argc, argv); } |
| 10169 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 10170 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10171 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10172 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10173 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 10174 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10175 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10176 | { BUILTIN_REGULAR "[" , testcmd }, |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10177 | #endif |
| 10178 | #if BASH_TEST2 |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10179 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10180 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10181 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10182 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10183 | #endif |
| 10184 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10185 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10186 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10187 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 10188 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 10189 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10190 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10191 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10192 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10193 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10194 | #if ENABLE_ASH_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10195 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10196 | #endif |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10197 | { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/ |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10198 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 10199 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 10200 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 10201 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10202 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10203 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10204 | #endif |
| 10205 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10206 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10207 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10208 | { BUILTIN_REGULAR "hash" , hashcmd }, |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 10209 | #if ENABLE_ASH_HELP |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10210 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10211 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 10212 | #if MAX_HISTORY |
| 10213 | { BUILTIN_NOSPEC "history" , historycmd }, |
| 10214 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10215 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10216 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 10217 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10218 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 10219 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10220 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10221 | #endif |
Denys Vlasenko | 85241c7 | 2017-07-26 20:00:08 +0200 | [diff] [blame] | 10222 | { BUILTIN_SPEC_REG_ASSG "local" , localcmd }, |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10223 | #if ENABLE_ASH_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10224 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 10225 | #endif |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10226 | { BUILTIN_REGULAR "pwd" , pwdcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10227 | { BUILTIN_REGULAR "read" , readcmd }, |
| 10228 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 10229 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 10230 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 10231 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Shawn Landden | 58598eb | 2022-08-27 19:56:21 +0200 | [diff] [blame] | 10232 | #if ENABLE_ASH_SLEEP |
| 10233 | { BUILTIN_REGULAR "sleep" , sleepcmd }, |
| 10234 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10235 | #if BASH_SOURCE |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10236 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 10237 | #endif |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10238 | #if ENABLE_ASH_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10239 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10240 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10241 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 10242 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 10243 | { BUILTIN_REGULAR "true" , truecmd }, |
Denys Vlasenko | a7b97e3 | 2020-02-16 18:29:52 +0100 | [diff] [blame] | 10244 | { BUILTIN_REGULAR "type" , typecmd }, |
| 10245 | { BUILTIN_REGULAR "ulimit" , ulimitcmd }, |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10246 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10247 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10248 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10249 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 10250 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 10251 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10252 | }; |
| 10253 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10254 | /* Should match the above table! */ |
| 10255 | #define COMMANDCMD (builtintab + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10256 | /* . : */ 2 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10257 | /* [ */ 1 * ENABLE_ASH_TEST + \ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 10258 | /* [[ */ 1 * BASH_TEST2 + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10259 | /* alias */ 1 * ENABLE_ASH_ALIAS + \ |
| 10260 | /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 10261 | /* break cd cddir */ 3) |
| 10262 | #define EVALCMD (COMMANDCMD + \ |
| 10263 | /* command */ 1 * ENABLE_ASH_CMDCMD + \ |
| 10264 | /* continue */ 1 + \ |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10265 | /* echo */ 1 * ENABLE_ASH_ECHO + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 10266 | 0) |
| 10267 | #define EXECCMD (EVALCMD + \ |
| 10268 | /* eval */ 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10269 | |
| 10270 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10271 | * Search the table of builtin commands. |
| 10272 | */ |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10273 | static int |
| 10274 | pstrcmp1(const void *a, const void *b) |
| 10275 | { |
| 10276 | return strcmp((char*)a, *(char**)b + 1); |
| 10277 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10278 | static struct builtincmd * |
| 10279 | find_builtin(const char *name) |
| 10280 | { |
| 10281 | struct builtincmd *bp; |
| 10282 | |
| 10283 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 10284 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 10285 | pstrcmp1 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10286 | ); |
| 10287 | return bp; |
| 10288 | } |
| 10289 | |
Denys Vlasenko | 27be0e8 | 2023-01-03 08:28:16 +0100 | [diff] [blame] | 10290 | #if ENABLE_FEATURE_TAB_COMPLETION |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 10291 | static const char * FAST_FUNC |
| 10292 | get_builtin_name(int i) |
| 10293 | { |
| 10294 | return /*i >= 0 &&*/ i < ARRAY_SIZE(builtintab) ? builtintab[i].name + 1 : NULL; |
| 10295 | } |
| 10296 | #endif |
| 10297 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 10298 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10299 | * Execute a simple command. |
| 10300 | */ |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10301 | static void unwindfiles(struct parsefile *stop); |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10302 | static int |
| 10303 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10304 | { |
| 10305 | const char *q = endofname(p); |
| 10306 | if (p == q) |
| 10307 | return 0; |
| 10308 | return *q == '='; |
| 10309 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10310 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10311 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10312 | { |
| 10313 | /* Preserve exitstatus of a previous possible redirection |
| 10314 | * as POSIX mandates */ |
| 10315 | return back_exitstatus; |
| 10316 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10317 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10318 | evalcommand(union node *cmd, int flags) |
| 10319 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10320 | static const struct builtincmd null_bltin = { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10321 | BUILTIN_REGULAR "", bltincmd |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 10322 | }; |
Denys Vlasenko | 484fc20 | 2017-07-26 19:55:31 +0200 | [diff] [blame] | 10323 | struct localvar_list *localvar_stop; |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10324 | struct parsefile *file_stop; |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10325 | struct redirtab *redir_stop; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10326 | union node *argp; |
| 10327 | struct arglist arglist; |
| 10328 | struct arglist varlist; |
| 10329 | char **argv; |
| 10330 | int argc; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10331 | struct strlist *osp; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10332 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10333 | struct cmdentry cmdentry; |
| 10334 | struct job *jp; |
| 10335 | char *lastarg; |
| 10336 | const char *path; |
| 10337 | int spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10338 | int cmd_flag; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10339 | int status; |
| 10340 | char **nargv; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 10341 | smallint cmd_is_exec; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10342 | int vflags; |
| 10343 | int vlocal; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10344 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10345 | errlinno = lineno = cmd->ncmd.linno; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10346 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10347 | /* First expand the arguments. */ |
| 10348 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
Ron Yorston | 4a36ef1 | 2021-08-30 20:31:42 +0100 | [diff] [blame] | 10349 | #if BASH_PROCESS_SUBST |
| 10350 | redir_stop = redirlist; |
| 10351 | #endif |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10352 | file_stop = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10353 | back_exitstatus = 0; |
| 10354 | |
| 10355 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10356 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10357 | varlist.lastp = &varlist.list; |
| 10358 | *varlist.lastp = NULL; |
| 10359 | arglist.lastp = &arglist.list; |
| 10360 | *arglist.lastp = NULL; |
| 10361 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10362 | cmd_flag = 0; |
| 10363 | cmd_is_exec = 0; |
| 10364 | spclbltin = -1; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10365 | vflags = 0; |
| 10366 | vlocal = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10367 | path = NULL; |
| 10368 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10369 | argc = 0; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10370 | argp = cmd->ncmd.args; |
| 10371 | osp = fill_arglist(&arglist, &argp); |
| 10372 | if (osp) { |
| 10373 | int pseudovarflag = 0; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10374 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10375 | for (;;) { |
| 10376 | find_command(arglist.list->text, &cmdentry, |
| 10377 | cmd_flag | DO_REGBLTIN, pathval()); |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10378 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10379 | vlocal++; |
| 10380 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10381 | /* implement bltin and command here */ |
| 10382 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 10383 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10384 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10385 | pseudovarflag = IS_BUILTIN_ASSIGN(cmdentry.u.cmd); |
| 10386 | if (spclbltin < 0) { |
| 10387 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10388 | vlocal = !spclbltin; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10389 | } |
| 10390 | cmd_is_exec = cmdentry.u.cmd == EXECCMD; |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10391 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10392 | if (cmdentry.u.cmd != COMMANDCMD) |
| 10393 | break; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 10394 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10395 | cmd_flag = parse_command_args(&arglist, &argp, &path); |
| 10396 | if (!cmd_flag) |
Ron Yorston | da7a6db | 2020-02-27 09:50:18 +0000 | [diff] [blame] | 10397 | #endif |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10398 | break; |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10399 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10400 | |
| 10401 | for (; argp; argp = argp->narg.next) |
| 10402 | expandarg(argp, &arglist, |
| 10403 | pseudovarflag && |
| 10404 | isassignment(argp->narg.text) ? |
| 10405 | EXP_VARTILDE : EXP_FULL | EXP_TILDE); |
| 10406 | |
| 10407 | for (sp = arglist.list; sp; sp = sp->next) |
| 10408 | argc++; |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10409 | |
| 10410 | if (cmd_is_exec && argc > 1) |
| 10411 | vflags = VEXPORT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10412 | } |
| 10413 | |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10414 | localvar_stop = pushlocalvars(vlocal); |
| 10415 | |
Denys Vlasenko | 65a8b85 | 2016-10-26 22:29:11 +0200 | [diff] [blame] | 10416 | /* Reserve one extra spot at the front for shellexec. */ |
| 10417 | nargv = stalloc(sizeof(char *) * (argc + 2)); |
| 10418 | argv = ++nargv; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10419 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10420 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 10421 | *nargv++ = sp->text; |
| 10422 | } |
| 10423 | *nargv = NULL; |
| 10424 | |
| 10425 | lastarg = NULL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 10426 | if (iflag && funcline == 0 && argc > 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10427 | lastarg = nargv[-1]; |
| 10428 | |
| 10429 | expredir(cmd->ncmd.redirect); |
Ron Yorston | 4a36ef1 | 2021-08-30 20:31:42 +0100 | [diff] [blame] | 10430 | #if !BASH_PROCESS_SUBST |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10431 | redir_stop = pushredir(cmd->ncmd.redirect); |
Ron Yorston | 4a36ef1 | 2021-08-30 20:31:42 +0100 | [diff] [blame] | 10432 | #else |
| 10433 | pushredir(cmd->ncmd.redirect); |
| 10434 | #endif |
Denys Vlasenko | d07a15b | 2017-07-30 16:51:05 +0200 | [diff] [blame] | 10435 | preverrout_fd = 2; |
Denys Vlasenko | f8cdc7a | 2017-08-04 15:24:49 +0200 | [diff] [blame] | 10436 | if (BASH_XTRACEFD && xflag) { |
| 10437 | /* NB: bash closes fd == $BASH_XTRACEFD when it is changed. |
| 10438 | * we do not emulate this. We only use its value. |
| 10439 | */ |
| 10440 | const char *xtracefd = lookupvar("BASH_XTRACEFD"); |
| 10441 | if (xtracefd && is_number(xtracefd)) |
| 10442 | preverrout_fd = atoi(xtracefd); |
| 10443 | |
| 10444 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10445 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10446 | |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10447 | if (status) { |
| 10448 | bail: |
| 10449 | exitstatus = status; |
| 10450 | |
| 10451 | /* We have a redirection error. */ |
| 10452 | if (spclbltin > 0) |
| 10453 | raise_exception(EXERROR); |
| 10454 | |
| 10455 | goto out; |
| 10456 | } |
| 10457 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10458 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 10459 | struct strlist **spp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10460 | |
| 10461 | spp = varlist.lastp; |
| 10462 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 10463 | |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10464 | if (vlocal) |
| 10465 | mklocal((*spp)->text, VEXPORT); |
| 10466 | else |
| 10467 | setvareq((*spp)->text, vflags); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
| 10470 | /* Print the command if xflag is set. */ |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 10471 | if (xflag && !inps4) { |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10472 | const char *pfx = ""; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10473 | |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 10474 | inps4 = 1; |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 10475 | fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX)); |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 10476 | inps4 = 0; |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10477 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10478 | sp = varlist.list; |
Denys Vlasenko | 42ba757 | 2017-07-21 13:20:14 +0200 | [diff] [blame] | 10479 | while (sp) { |
| 10480 | char *varval = sp->text; |
| 10481 | char *eq = strchrnul(varval, '='); |
| 10482 | if (*eq) |
| 10483 | eq++; |
| 10484 | fdprintf(preverrout_fd, "%s%.*s%s", |
| 10485 | pfx, |
| 10486 | (int)(eq - varval), varval, |
| 10487 | maybe_single_quote(eq) |
| 10488 | ); |
| 10489 | sp = sp->next; |
| 10490 | pfx = " "; |
| 10491 | } |
| 10492 | |
| 10493 | sp = arglist.list; |
| 10494 | while (sp) { |
| 10495 | fdprintf(preverrout_fd, "%s%s", |
| 10496 | pfx, |
| 10497 | /* always quote if matches reserved word: */ |
| 10498 | findkwd(sp->text) |
| 10499 | ? single_quote(sp->text) |
| 10500 | : maybe_single_quote(sp->text) |
| 10501 | ); |
| 10502 | sp = sp->next; |
| 10503 | pfx = " "; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 10504 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 10505 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10508 | /* Now locate the command. */ |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 10509 | if (cmdentry.cmdtype != CMDBUILTIN |
| 10510 | || !(IS_BUILTIN_REGULAR(cmdentry.u.cmd)) |
| 10511 | ) { |
Denys Vlasenko | 3e72910 | 2020-02-19 17:33:44 +0100 | [diff] [blame] | 10512 | path = path ? path : pathval(); |
| 10513 | find_command(argv[0], &cmdentry, cmd_flag | DO_ERR, path); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10516 | jp = NULL; |
| 10517 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10518 | /* Execute the command. */ |
| 10519 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 54bef2a | 2020-02-19 15:30:20 +0100 | [diff] [blame] | 10520 | case CMDUNKNOWN: |
| 10521 | status = 127; |
| 10522 | flush_stdout_stderr(); |
| 10523 | goto bail; |
| 10524 | |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10525 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 10526 | |
Denys Vlasenko | 1750d3a | 2018-01-15 00:41:04 +0100 | [diff] [blame] | 10527 | #if ENABLE_FEATURE_SH_STANDALONE \ |
| 10528 | && ENABLE_FEATURE_SH_NOFORK \ |
| 10529 | && NUM_APPLETS > 1 |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10530 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 10531 | * around run_nofork_applet() call. |
| 10532 | * (2) Should this check also be done in forkshell()? |
| 10533 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 10534 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 10535 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 10536 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10537 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10538 | char **sv_environ; |
| 10539 | |
| 10540 | INT_OFF; |
| 10541 | sv_environ = environ; |
| 10542 | environ = listvars(VEXPORT, VUNSET, varlist.list, /*end:*/ NULL); |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10543 | /* |
| 10544 | * Run <applet>_main(). |
| 10545 | * Signals (^C) can't interrupt here. |
| 10546 | * Otherwise we can mangle stdio or malloc internal state. |
| 10547 | * This makes applets which can run for a long time |
| 10548 | * and/or wait for user input ineligible for NOFORK: |
| 10549 | * for example, "yes" or "rm" (rm -i waits for input). |
| 10550 | */ |
Ron Yorston | d5bfe26 | 2020-02-20 08:23:03 +0000 | [diff] [blame] | 10551 | exitstatus = run_nofork_applet(applet_no, argv); |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 10552 | environ = sv_environ; |
Denys Vlasenko | d329e34 | 2017-08-04 14:50:03 +0200 | [diff] [blame] | 10553 | /* |
| 10554 | * Try enabling NOFORK for "yes" applet. |
| 10555 | * ^C _will_ stop it (write returns EINTR), |
| 10556 | * but this causes stdout FILE to be stuck |
| 10557 | * and needing clearerr(). What if other applets |
| 10558 | * also can get EINTRs? Do we need to switch |
| 10559 | * our signals to SA_RESTART? |
| 10560 | */ |
| 10561 | /*clearerr(stdout);*/ |
| 10562 | INT_ON; |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10563 | break; |
| 10564 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 10565 | #endif |
Denys Vlasenko | cfd392b | 2017-08-03 19:56:29 +0200 | [diff] [blame] | 10566 | /* Can we avoid forking? For example, very last command |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10567 | * in a script or a subshell does not need forking, |
| 10568 | * we can just exec it. |
| 10569 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10570 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10571 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10572 | INT_OFF; |
Denys Vlasenko | 098b713 | 2017-01-11 19:59:03 +0100 | [diff] [blame] | 10573 | get_tty_state(); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10574 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10575 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10576 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10577 | break; |
| 10578 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 10579 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10580 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 10581 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10582 | } |
Denys Vlasenko | e139ae3 | 2017-04-12 21:02:33 +0200 | [diff] [blame] | 10583 | shellexec(argv[0], argv, path, cmdentry.u.index); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10584 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10585 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10586 | case CMDBUILTIN: |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10587 | if (evalbltin(cmdentry.u.cmd, argc, argv, flags) |
| 10588 | && !(exception_type == EXERROR && spclbltin <= 0) |
| 10589 | ) { |
Denys Vlasenko | c0663c7 | 2016-10-27 21:09:01 +0200 | [diff] [blame] | 10590 | raise: |
| 10591 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10592 | } |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10593 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10594 | |
| 10595 | case CMDFUNCTION: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10596 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 10597 | goto raise; |
| 10598 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 10599 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10600 | |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10601 | status = waitforjob(jp); |
Ron Yorston | 6cda0b0 | 2020-02-21 16:16:56 +0000 | [diff] [blame] | 10602 | if (jp) |
| 10603 | TRACE(("forked child exited with %d\n", status)); |
Denys Vlasenko | 97edfc4 | 2020-02-18 14:37:56 +0100 | [diff] [blame] | 10604 | FORCE_INT_ON; |
Denys Vlasenko | d81af72 | 2020-02-18 14:28:30 +0100 | [diff] [blame] | 10605 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10606 | out: |
Denys Vlasenko | eaf9436 | 2016-10-25 21:46:03 +0200 | [diff] [blame] | 10607 | if (cmd->ncmd.redirect) |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 10608 | popredir(/*drop:*/ cmd_is_exec); |
Denys Vlasenko | 1c79aeb | 2017-07-29 22:51:52 +0200 | [diff] [blame] | 10609 | unwindredir(redir_stop); |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 10610 | unwindfiles(file_stop); |
Denys Vlasenko | e2dd2af | 2020-02-20 10:33:38 +0100 | [diff] [blame] | 10611 | unwindlocalvars(localvar_stop); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10612 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10613 | /* dsl: I think this is intended to be used to support |
| 10614 | * '_' in 'vi' command mode during line editing... |
| 10615 | * However I implemented that within libedit itself. |
| 10616 | */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 10617 | setvar0("_", lastarg); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 10618 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 10619 | |
| 10620 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10621 | } |
| 10622 | |
| 10623 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10624 | evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10625 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10626 | char *volatile savecmdname; |
| 10627 | struct jmploc *volatile savehandler; |
| 10628 | struct jmploc jmploc; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10629 | int status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10630 | int i; |
| 10631 | |
| 10632 | savecmdname = commandname; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 10633 | savehandler = exception_handler; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10634 | i = setjmp(jmploc.loc); |
| 10635 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10636 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10637 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10638 | commandname = argv[0]; |
| 10639 | argptr = argv + 1; |
| 10640 | optptr = NULL; /* initialize nextopt */ |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10641 | if (cmd == EVALCMD) |
| 10642 | status = evalcmd(argc, argv, flags); |
| 10643 | else |
| 10644 | status = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10645 | flush_stdout_stderr(); |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 10646 | status |= ferror(stdout); |
| 10647 | exitstatus = status; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10648 | cmddone: |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 10649 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10650 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10651 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10652 | |
| 10653 | return i; |
| 10654 | } |
| 10655 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10656 | static int |
| 10657 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10658 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 10659 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10660 | } |
| 10661 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10662 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10663 | /* |
| 10664 | * Search for a command. This is called before we fork so that the |
| 10665 | * 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] | 10666 | * the child. The check for "goodname" is an overly conservative |
| 10667 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10668 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10669 | static void |
| 10670 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10671 | { |
| 10672 | struct cmdentry entry; |
| 10673 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10674 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 10675 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 10676 | } |
| 10677 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10678 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10679 | /* ============ Builtin commands |
| 10680 | * |
| 10681 | * Builtin commands whose functions are closely tied to evaluation |
| 10682 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10683 | */ |
| 10684 | |
| 10685 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10686 | * Handle break and continue commands. Break, continue, and return are |
| 10687 | * all handled by setting the evalskip flag. The evaluation routines |
| 10688 | * above all check this flag, and if it is set they start skipping |
| 10689 | * commands rather than executing them. The variable skipcount is |
| 10690 | * the number of loops to break/continue, or the number of function |
| 10691 | * levels to return. (The latter is always 1.) It should probably |
| 10692 | * be an error to break out of more loops than exist, but it isn't |
| 10693 | * in the standard shell so we don't make it one here. |
| 10694 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10695 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10696 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10697 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10698 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10699 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 10700 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 10701 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10702 | if (n > loopnest) |
| 10703 | n = loopnest; |
| 10704 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 10705 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10706 | skipcount = n; |
| 10707 | } |
| 10708 | return 0; |
| 10709 | } |
| 10710 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10711 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 10712 | /* |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 10713 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10714 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10715 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10716 | enum { |
| 10717 | INPUT_PUSH_FILE = 1, |
| 10718 | INPUT_NOFILE_OK = 2, |
| 10719 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10720 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10721 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10722 | /* values of checkkwd variable */ |
| 10723 | #define CHKALIAS 0x1 |
| 10724 | #define CHKKWD 0x2 |
| 10725 | #define CHKNL 0x4 |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 10726 | #define CHKEOFMARK 0x8 |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10727 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10728 | /* |
| 10729 | * Push a string back onto the input at this current parsefile level. |
| 10730 | * We handle aliases this way. |
| 10731 | */ |
| 10732 | #if !ENABLE_ASH_ALIAS |
| 10733 | #define pushstring(s, ap) pushstring(s) |
| 10734 | #endif |
| 10735 | static void |
| 10736 | pushstring(char *s, struct alias *ap) |
| 10737 | { |
| 10738 | struct strpush *sp; |
| 10739 | int len; |
| 10740 | |
| 10741 | len = strlen(s); |
| 10742 | INT_OFF; |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10743 | if (g_parsefile->strpush || g_parsefile->spfree) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10744 | sp = ckzalloc(sizeof(*sp)); |
| 10745 | sp->prev = g_parsefile->strpush; |
| 10746 | } else { |
| 10747 | sp = &(g_parsefile->basestrpush); |
| 10748 | } |
| 10749 | g_parsefile->strpush = sp; |
| 10750 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 10751 | sp->prev_left_in_line = g_parsefile->left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10752 | sp->unget = g_parsefile->unget; |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10753 | sp->spfree = g_parsefile->spfree; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10754 | memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10755 | #if ENABLE_ASH_ALIAS |
| 10756 | sp->ap = ap; |
| 10757 | if (ap) { |
| 10758 | ap->flag |= ALIASINUSE; |
| 10759 | sp->string = s; |
| 10760 | } |
| 10761 | #endif |
| 10762 | g_parsefile->next_to_pgetc = s; |
| 10763 | g_parsefile->left_in_line = len; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10764 | g_parsefile->unget = 0; |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10765 | g_parsefile->spfree = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10766 | INT_ON; |
| 10767 | } |
| 10768 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10769 | static void popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10770 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10771 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10772 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10773 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10774 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10775 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10776 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 10777 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 10778 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10779 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10780 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10781 | if (sp->string != sp->ap->val) { |
| 10782 | free(sp->string); |
| 10783 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 10784 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10785 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10786 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 10787 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10788 | g_parsefile->unget = sp->unget; |
| 10789 | memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10790 | g_parsefile->strpush = sp->prev; |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10791 | g_parsefile->spfree = sp; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10792 | INT_ON; |
| 10793 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10794 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10795 | static int |
| 10796 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10797 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10798 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 10799 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10800 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10801 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 10802 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 4ac35a3 | 2020-11-16 13:09:37 +0100 | [diff] [blame] | 10803 | /* retry: */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10804 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10805 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10806 | else { |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10807 | # if ENABLE_ASH_IDLE_TIMEOUT |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10808 | int timeout = -1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 10809 | const char *tmout_var = lookupvar("TMOUT"); |
| 10810 | if (tmout_var) { |
| 10811 | timeout = atoi(tmout_var) * 1000; |
| 10812 | if (timeout <= 0) |
| 10813 | timeout = -1; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10814 | } |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10815 | line_input_state->timeout = timeout; |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10816 | # endif |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10817 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10818 | line_input_state->path_lookup = pathval(); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 10819 | # endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 10820 | reinit_unicode_for_ash(); |
Denys Vlasenko | 68b402e | 2022-01-13 01:05:03 +0100 | [diff] [blame] | 10821 | again: |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10822 | /* For shell, LI_INTERRUPTIBLE is set: |
| 10823 | * read_line_input will abort on either |
| 10824 | * getting EINTR in poll(), or if it sees bb_got_signal != 0 |
| 10825 | * (IOW: if signal arrives before poll() is reached). |
| 10826 | * Interactive testcases: |
| 10827 | * (while kill -INT $$; do sleep 1; done) & |
| 10828 | * #^^^ prints ^C, prints prompt, repeats |
| 10829 | * trap 'echo I' int; (while kill -INT $$; do sleep 1; done) & |
| 10830 | * #^^^ prints ^C, prints "I", prints prompt, repeats |
| 10831 | * trap 'echo T' term; (while kill $$; do sleep 1; done) & |
| 10832 | * #^^^ prints "T", prints prompt, repeats |
| 10833 | * #(bash 5.0.17 exits after first "T", looks like a bug) |
| 10834 | */ |
| 10835 | bb_got_signal = 0; |
| 10836 | INT_OFF; /* no longjmp'ing out of read_line_input please */ |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 10837 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ); |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10838 | if (bb_got_signal == SIGINT) |
| 10839 | write(STDOUT_FILENO, "^C\n", 3); |
| 10840 | INT_ON; /* here non-blocked SIGINT will longjmp */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10841 | if (nr == 0) { |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 10842 | /* ^C pressed, "convert" to SIGINT */ |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10843 | write(STDOUT_FILENO, "^C\n", 3); |
| 10844 | raise(SIGINT); /* here non-blocked SIGINT will longjmp */ |
Denys Vlasenko | 68b402e | 2022-01-13 01:05:03 +0100 | [diff] [blame] | 10845 | /* raise(SIGINT) did not work! (e.g. if SIGINT |
Denys Vlasenko | 931c55f | 2022-01-13 12:50:48 +0100 | [diff] [blame] | 10846 | * is SIG_IGNed on startup, it stays SIG_IGNed) |
Denys Vlasenko | 68b402e | 2022-01-13 01:05:03 +0100 | [diff] [blame] | 10847 | */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10848 | if (trap[SIGINT]) { |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10849 | empty_line_input: |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10850 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 10851 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 10852 | return 1; |
| 10853 | } |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 10854 | exitstatus = 128 + SIGINT; |
Denys Vlasenko | 68b402e | 2022-01-13 01:05:03 +0100 | [diff] [blame] | 10855 | /* bash behavior on ^C + ignored SIGINT: */ |
Denys Vlasenko | 68b402e | 2022-01-13 01:05:03 +0100 | [diff] [blame] | 10856 | goto again; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10857 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10858 | if (nr < 0) { |
| 10859 | if (errno == 0) { |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10860 | /* ^D pressed */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10861 | nr = 0; |
| 10862 | } |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 10863 | else if (errno == EINTR) { /* got signal? */ |
| 10864 | if (bb_got_signal != SIGINT) |
| 10865 | write(STDOUT_FILENO, "\n", 1); |
| 10866 | goto empty_line_input; |
| 10867 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10868 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 10869 | else if (errno == EAGAIN && timeout > 0) { |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 10870 | puts("\007timed out waiting for input: auto-logout"); |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 10871 | exitshell(); |
| 10872 | } |
| 10873 | # endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10874 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10875 | } |
| 10876 | #else |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 10877 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10878 | #endif |
| 10879 | |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 10880 | #if 0 /* disabled: nonblock_immune_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10882 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 10883 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 10884 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 10885 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10886 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 10887 | out2str("sh: turning off NDELAY mode\n"); |
| 10888 | goto retry; |
| 10889 | } |
| 10890 | } |
| 10891 | } |
| 10892 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 10893 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10894 | return nr; |
| 10895 | } |
| 10896 | |
| 10897 | /* |
| 10898 | * Refill the input buffer and return the next input character: |
| 10899 | * |
| 10900 | * 1) If a string was pushed back on the input, pop it; |
Ron Yorston | 0beee20 | 2021-09-12 11:20:33 +0100 | [diff] [blame] | 10901 | * 2) If we are reading from a string we can't refill the buffer, return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 10902 | * 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] | 10903 | * 4) Process input up to the next newline, deleting nul characters. |
| 10904 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10905 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 10906 | #define pgetc_debug(...) ((void)0) |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10907 | static int __pgetc(void); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10908 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10909 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10910 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10911 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10912 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10913 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10914 | if (unlikely(g_parsefile->strpush)) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10915 | popstring(); |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10916 | return __pgetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10917 | } |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10918 | |
Ron Yorston | 0beee20 | 2021-09-12 11:20:33 +0100 | [diff] [blame] | 10919 | if (g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10920 | pgetc_debug("preadbuffer PEOF1"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10921 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10922 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10923 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10924 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10925 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10926 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10927 | again: |
| 10928 | more = preadfd(); |
| 10929 | if (more <= 0) { |
Ron Yorston | 0beee20 | 2021-09-12 11:20:33 +0100 | [diff] [blame] | 10930 | g_parsefile->left_in_buffer = g_parsefile->left_in_line = 0; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10931 | pgetc_debug("preadbuffer PEOF2"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10932 | return PEOF; |
| 10933 | } |
| 10934 | } |
| 10935 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10936 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10937 | * Set g_parsefile->left_in_line |
| 10938 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10939 | * NUL chars are deleted. |
| 10940 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10941 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10942 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10943 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10944 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10945 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10946 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10947 | c = *q; |
| 10948 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10949 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10950 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10951 | q++; |
| 10952 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10953 | 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] | 10954 | break; |
| 10955 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10956 | } |
| 10957 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10958 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10959 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 10960 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10961 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10962 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10963 | } |
| 10964 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10965 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10966 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10967 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10968 | char save = *q; |
| 10969 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10970 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10971 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10972 | } |
| 10973 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10974 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 10975 | g_parsefile->left_in_line, |
| 10976 | g_parsefile->next_to_pgetc, |
| 10977 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10978 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10979 | } |
| 10980 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10981 | static void |
| 10982 | nlprompt(void) |
| 10983 | { |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 10984 | if (trap_depth == 0) |
| 10985 | g_parsefile->linno++; |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10986 | setprompt_if(doprompt, 2); |
| 10987 | } |
| 10988 | static void |
| 10989 | nlnoprompt(void) |
| 10990 | { |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 10991 | if (trap_depth == 0) |
| 10992 | g_parsefile->linno++; |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 10993 | needprompt = doprompt; |
| 10994 | } |
| 10995 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 10996 | static void freestrings(struct strpush *sp) |
| 10997 | { |
| 10998 | INT_OFF; |
| 10999 | do { |
| 11000 | struct strpush *psp; |
Denys Vlasenko | 5b026d1 | 2021-09-28 17:41:56 +0200 | [diff] [blame] | 11001 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11002 | if (sp->ap) { |
| 11003 | sp->ap->flag &= ~ALIASINUSE; |
| 11004 | if (sp->ap->flag & ALIASDEAD) { |
| 11005 | unalias(sp->ap->name); |
| 11006 | } |
| 11007 | } |
Denys Vlasenko | 5b026d1 | 2021-09-28 17:41:56 +0200 | [diff] [blame] | 11008 | #endif |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11009 | psp = sp; |
| 11010 | sp = sp->spfree; |
| 11011 | |
| 11012 | if (psp != &(g_parsefile->basestrpush)) |
| 11013 | free(psp); |
| 11014 | } while (sp); |
| 11015 | |
| 11016 | g_parsefile->spfree = NULL; |
| 11017 | INT_ON; |
| 11018 | } |
| 11019 | |
| 11020 | static int __pgetc(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11021 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11022 | int c; |
| 11023 | |
| 11024 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11025 | g_parsefile->left_in_line, |
| 11026 | g_parsefile->next_to_pgetc, |
| 11027 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11028 | if (g_parsefile->unget) |
| 11029 | return g_parsefile->lastc[--g_parsefile->unget]; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11030 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11031 | if (--g_parsefile->left_in_line >= 0) |
Denys Vlasenko | 2fe66b1 | 2016-12-12 17:39:12 +0100 | [diff] [blame] | 11032 | c = (unsigned char)*g_parsefile->next_to_pgetc++; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11033 | else |
| 11034 | c = preadbuffer(); |
| 11035 | |
| 11036 | g_parsefile->lastc[1] = g_parsefile->lastc[0]; |
| 11037 | g_parsefile->lastc[0] = c; |
| 11038 | |
| 11039 | return c; |
| 11040 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11041 | |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11042 | /* |
| 11043 | * Read a character from the script, returning PEOF on end of file. |
| 11044 | * Nul characters in the input are silently discarded. |
| 11045 | */ |
| 11046 | static int pgetc(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11047 | { |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11048 | struct strpush *sp = g_parsefile->spfree; |
| 11049 | |
| 11050 | if (unlikely(sp)) |
| 11051 | freestrings(sp); |
| 11052 | |
| 11053 | return __pgetc(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11054 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11055 | |
| 11056 | /* |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11057 | * Undo a call to pgetc. Only two characters may be pushed back. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11058 | * PEOF may be pushed back. |
| 11059 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11060 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11061 | pungetc(void) |
| 11062 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11063 | g_parsefile->unget++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11064 | } |
| 11065 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11066 | /* This one eats backslash+newline */ |
| 11067 | static int |
| 11068 | pgetc_eatbnl(void) |
| 11069 | { |
| 11070 | int c; |
| 11071 | |
| 11072 | while ((c = pgetc()) == '\\') { |
| 11073 | if (pgetc() != '\n') { |
| 11074 | pungetc(); |
| 11075 | break; |
| 11076 | } |
| 11077 | |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 11078 | nlprompt(); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11079 | } |
| 11080 | |
| 11081 | return c; |
| 11082 | } |
| 11083 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 11084 | struct synstack { |
| 11085 | smalluint syntax; |
| 11086 | uint8_t innerdq :1; |
| 11087 | uint8_t varpushed :1; |
| 11088 | uint8_t dblquote :1; |
| 11089 | int varnest; /* levels of variables expansion */ |
| 11090 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 11091 | int parenlevel; /* levels of parens in arithmetic */ |
| 11092 | struct synstack *prev; |
| 11093 | struct synstack *next; |
| 11094 | }; |
| 11095 | |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 11096 | static int |
| 11097 | pgetc_top(struct synstack *stack) |
| 11098 | { |
| 11099 | return stack->syntax == SQSYNTAX ? pgetc() : pgetc_eatbnl(); |
| 11100 | } |
| 11101 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 11102 | static void |
| 11103 | synstack_push(struct synstack **stack, struct synstack *next, int syntax) |
| 11104 | { |
| 11105 | memset(next, 0, sizeof(*next)); |
| 11106 | next->syntax = syntax; |
| 11107 | next->next = *stack; |
| 11108 | (*stack)->prev = next; |
| 11109 | *stack = next; |
| 11110 | } |
| 11111 | |
| 11112 | static ALWAYS_INLINE void |
| 11113 | synstack_pop(struct synstack **stack) |
| 11114 | { |
| 11115 | *stack = (*stack)->next; |
| 11116 | } |
| 11117 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11118 | /* |
| 11119 | * To handle the "." command, a stack of input files is used. Pushfile |
| 11120 | * adds a new entry to the stack and popfile restores the previous level. |
| 11121 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11122 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11123 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11124 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11125 | struct parsefile *pf; |
| 11126 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11127 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11128 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 11129 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11130 | /*pf->strpush = NULL; - ckzalloc did it */ |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11131 | /*pf->spfree = NULL;*/ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11132 | /*pf->basestrpush.prev = NULL;*/ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11133 | /*pf->unget = 0;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11134 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11135 | } |
| 11136 | |
| 11137 | static void |
| 11138 | popfile(void) |
| 11139 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11140 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11141 | |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 11142 | if (pf == &basepf) |
| 11143 | return; |
| 11144 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11145 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 11146 | if (pf->pf_fd >= 0) |
| 11147 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11148 | free(pf->buf); |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11149 | if (g_parsefile->spfree) |
| 11150 | freestrings(g_parsefile->spfree); |
| 11151 | while (pf->strpush) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11152 | popstring(); |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 11153 | freestrings(g_parsefile->spfree); |
| 11154 | } |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11155 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11156 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11157 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11158 | } |
| 11159 | |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 11160 | static void |
| 11161 | unwindfiles(struct parsefile *stop) |
| 11162 | { |
| 11163 | while (g_parsefile != stop) |
| 11164 | popfile(); |
| 11165 | } |
| 11166 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11167 | /* |
| 11168 | * Return to top level. |
| 11169 | */ |
| 11170 | static void |
| 11171 | popallfiles(void) |
| 11172 | { |
Denys Vlasenko | 1c5eb88 | 2018-08-05 17:07:26 +0200 | [diff] [blame] | 11173 | unwindfiles(&basepf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11174 | } |
| 11175 | |
| 11176 | /* |
| 11177 | * Close the file(s) that the shell is reading commands from. Called |
| 11178 | * after a fork is done. |
| 11179 | */ |
| 11180 | static void |
| 11181 | closescript(void) |
| 11182 | { |
| 11183 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 11184 | if (g_parsefile->pf_fd > 0) { |
| 11185 | close(g_parsefile->pf_fd); |
| 11186 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11187 | } |
| 11188 | } |
| 11189 | |
| 11190 | /* |
| 11191 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 11192 | * interrupts off. |
| 11193 | */ |
| 11194 | static void |
| 11195 | setinputfd(int fd, int push) |
| 11196 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11197 | if (push) { |
| 11198 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 11199 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11200 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 11201 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11202 | if (g_parsefile->buf == NULL) |
| 11203 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11204 | g_parsefile->left_in_buffer = 0; |
| 11205 | g_parsefile->left_in_line = 0; |
| 11206 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 11207 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11208 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11209 | /* |
| 11210 | * Set the input to take input from a file. If push is set, push the |
| 11211 | * old input onto the stack first. |
| 11212 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11213 | static int |
| 11214 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11215 | { |
| 11216 | int fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11217 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11218 | INT_OFF; |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 11219 | fd = open(fname, O_RDONLY | O_CLOEXEC); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11220 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11221 | if (flags & INPUT_NOFILE_OK) |
| 11222 | goto out; |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 11223 | exitstatus = 127; |
Johannes Schindelin | 20a63b2 | 2017-08-22 22:03:17 +0200 | [diff] [blame] | 11224 | ash_msg_and_raise_perror("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11225 | } |
Denys Vlasenko | 6477460 | 2016-10-26 15:24:30 +0200 | [diff] [blame] | 11226 | if (fd < 10) |
| 11227 | fd = savefd(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 11228 | else if (O_CLOEXEC == 0) /* old libc */ |
Denys Vlasenko | e19923f | 2016-10-26 15:38:44 +0200 | [diff] [blame] | 11229 | close_on_exec_on(fd); |
Denys Vlasenko | 60fb98e | 2018-03-30 22:15:14 +0200 | [diff] [blame] | 11230 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11231 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11232 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11233 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 11234 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11235 | } |
| 11236 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11237 | /* |
| 11238 | * Like setinputfile, but takes input from a string. |
| 11239 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11240 | static void |
| 11241 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11242 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11243 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11244 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11245 | g_parsefile->next_to_pgetc = string; |
| 11246 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11247 | g_parsefile->buf = NULL; |
Denys Vlasenko | d6c9cbc | 2021-09-07 18:01:49 +0200 | [diff] [blame] | 11248 | g_parsefile->linno = lineno; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11249 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11250 | } |
| 11251 | |
| 11252 | |
Denys Vlasenko | 7039233 | 2016-10-27 02:31:55 +0200 | [diff] [blame] | 11253 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11254 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11255 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11256 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11257 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11258 | |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11259 | /* Hash of mtimes of mailboxes */ |
| 11260 | static unsigned mailtime_hash; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11261 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11262 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11263 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11264 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | * Print appropriate message(s) if mail has arrived. |
| 11266 | * If mail_var_path_changed is set, |
| 11267 | * then the value of MAIL has mail_var_path_changed, |
| 11268 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11269 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11270 | static void |
| 11271 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11272 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11273 | const char *mpath; |
| 11274 | char *p; |
| 11275 | char *q; |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11276 | unsigned new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11277 | struct stackmark smark; |
| 11278 | struct stat statb; |
| 11279 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11280 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11281 | mpath = mpathset() ? mpathval() : mailval(); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11282 | new_hash = 0; |
| 11283 | for (;;) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11284 | int len; |
| 11285 | |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 11286 | len = padvance_magic(&mpath, nullstr, 2); |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 11287 | if (!len) |
| 11288 | break; |
| 11289 | p = stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11290 | break; |
| 11291 | if (*p == '\0') |
| 11292 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11293 | for (q = p; *q; q++) |
| 11294 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 11295 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11296 | if (q[-1] != '/') |
| 11297 | abort(); |
| 11298 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11299 | q[-1] = '\0'; /* delete trailing '/' */ |
| 11300 | if (stat(p, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11301 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11302 | } |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11303 | /* Very simplistic "hash": just a sum of all mtimes */ |
| 11304 | new_hash += (unsigned)statb.st_mtime; |
| 11305 | } |
| 11306 | if (!mail_var_path_changed && mailtime_hash != new_hash) { |
Denys Vlasenko | 4cd99e7 | 2015-10-09 16:02:53 +0200 | [diff] [blame] | 11307 | if (mailtime_hash != 0) |
| 11308 | out2str("you have mail\n"); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 11309 | mailtime_hash = new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11310 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11311 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11312 | popstackmark(&smark); |
| 11313 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11314 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11315 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11316 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11317 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11318 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11319 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11320 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11321 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11322 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11323 | |
| 11324 | /* ============ ??? */ |
| 11325 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11326 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11327 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11328 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11329 | static void |
| 11330 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11331 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11332 | char **newparam; |
| 11333 | char **ap; |
| 11334 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11335 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11336 | for (nparam = 0; argv[nparam]; nparam++) |
| 11337 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11338 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 11339 | while (*argv) { |
| 11340 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11341 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11342 | *ap = NULL; |
| 11343 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11344 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11345 | shellparam.nparam = nparam; |
| 11346 | shellparam.p = newparam; |
| 11347 | #if ENABLE_ASH_GETOPTS |
| 11348 | shellparam.optind = 1; |
| 11349 | shellparam.optoff = -1; |
| 11350 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11353 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 11354 | * Process shell options. The global variable argptr contains a pointer |
| 11355 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11356 | * |
| 11357 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 11358 | * For a non-interactive shell, an error condition encountered |
| 11359 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 11360 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 11361 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 11362 | * ... |
| 11363 | * Utility syntax error (option or operand error) Shall exit |
| 11364 | * ... |
| 11365 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 11366 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 11367 | * and shell continues), and people rely on this behavior! |
| 11368 | * Testcase: |
| 11369 | * set -o barfoo 2>/dev/null |
| 11370 | * echo $? |
| 11371 | * |
| 11372 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 11373 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11374 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11375 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11376 | { |
| 11377 | int i; |
| 11378 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11379 | if (name) { |
| 11380 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11381 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11382 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11383 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11384 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11385 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11386 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11387 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11388 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11389 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | 2f9c124 | 2019-08-02 16:43:36 +0200 | [diff] [blame] | 11390 | if (optnames(i)[0] == '\0') |
| 11391 | continue; |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11392 | if (val) { |
| 11393 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 11394 | } else { |
| 11395 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 11396 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 11397 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11398 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 11399 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11400 | static void |
| 11401 | setoption(int flag, int val) |
| 11402 | { |
| 11403 | int i; |
| 11404 | |
| 11405 | for (i = 0; i < NOPTS; i++) { |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11406 | if (optletters(i) == flag && optnames(i)[0] != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11407 | optlist[i] = val; |
| 11408 | return; |
| 11409 | } |
| 11410 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11411 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11412 | /* NOTREACHED */ |
| 11413 | } |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11414 | /* If login_sh is not NULL, we are called to parse command line opts, |
| 11415 | * not "set -opts" |
| 11416 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11417 | static int |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11418 | options(int *login_sh) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11419 | { |
| 11420 | char *p; |
| 11421 | int val; |
| 11422 | int c; |
| 11423 | |
Denys Vlasenko | 004cefa | 2022-01-12 17:21:14 +0100 | [diff] [blame] | 11424 | if (login_sh != NULL) /* if we came from startup code */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11425 | minusc = NULL; |
| 11426 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11427 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11428 | if (c != '-' && c != '+') |
| 11429 | break; |
| 11430 | argptr++; |
| 11431 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11432 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11433 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11434 | if (p[0] == '\0' || LONE_DASH(p)) { |
Denys Vlasenko | 004cefa | 2022-01-12 17:21:14 +0100 | [diff] [blame] | 11435 | if (login_sh == NULL) { /* we came from setcmd() */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11436 | /* "-" means turn off -x and -v */ |
| 11437 | if (p[0] == '\0') |
| 11438 | xflag = vflag = 0; |
| 11439 | /* "--" means reset params */ |
| 11440 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11441 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11442 | } |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11443 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11444 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11445 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 11446 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11447 | while ((c = *p++) != '\0') { |
Denys Vlasenko | 004cefa | 2022-01-12 17:21:14 +0100 | [diff] [blame] | 11448 | if (login_sh != NULL) { /* if we came from startup code */ |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11449 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
| 11450 | if (c == 'c') { |
| 11451 | minusc = p; /* command is after shell args */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 11452 | cflag = 1; |
| 11453 | continue; |
| 11454 | } |
| 11455 | if (c == 's') { /* -s, +s */ |
| 11456 | sflag = 1; |
| 11457 | continue; |
| 11458 | } |
| 11459 | if (c == 'i') { /* -i, +i */ |
| 11460 | iflag = 1; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11461 | continue; |
| 11462 | } |
| 11463 | if (c == 'l') { |
| 11464 | *login_sh = 1; /* -l or +l == --login */ |
| 11465 | continue; |
| 11466 | } |
| 11467 | /* bash does not accept +-login, we also won't */ |
| 11468 | if (val && (c == '-')) { /* long options */ |
| 11469 | if (strcmp(p, "login") == 0) { |
| 11470 | *login_sh = 1; |
| 11471 | } |
Denys Vlasenko | 004cefa | 2022-01-12 17:21:14 +0100 | [diff] [blame] | 11472 | /* TODO: --noprofile: e.g. if I want to run emergency shell from sulogin, |
| 11473 | * I want minimal/no shell init scripts - but it insists on running it as "-ash"... |
| 11474 | */ |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11475 | break; |
| 11476 | } |
| 11477 | } |
| 11478 | if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 11479 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11480 | /* it already printed err message */ |
| 11481 | return 1; /* error */ |
| 11482 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11483 | if (*argptr) |
| 11484 | argptr++; |
| 11485 | } else { |
| 11486 | setoption(c, val); |
| 11487 | } |
| 11488 | } |
| 11489 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11490 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11491 | } |
| 11492 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11493 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11494 | * The shift builtin command. |
| 11495 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11496 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11497 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11498 | { |
| 11499 | int n; |
| 11500 | char **ap1, **ap2; |
| 11501 | |
| 11502 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11503 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11504 | n = number(argv[1]); |
| 11505 | if (n > shellparam.nparam) |
Ingo van Lil | 9c8e94b | 2018-01-05 15:04:23 +0100 | [diff] [blame] | 11506 | return 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11507 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11508 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11509 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 11510 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11511 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11512 | } |
| 11513 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11514 | while ((*ap2++ = *ap1++) != NULL) |
| 11515 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11516 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11517 | shellparam.optind = 1; |
| 11518 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11519 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11520 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11521 | return 0; |
| 11522 | } |
| 11523 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11524 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11525 | * POSIX requires that 'set' (but not export or readonly) output the |
| 11526 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 11527 | * Maybe we could keep them in an ordered balanced binary tree |
| 11528 | * instead of hashed lists. |
| 11529 | * For now just roll 'em through qsort for printing... |
| 11530 | */ |
| 11531 | static int |
| 11532 | showvars(const char *sep_prefix, int on, int off) |
| 11533 | { |
| 11534 | const char *sep; |
| 11535 | char **ep, **epend; |
| 11536 | |
Denys Vlasenko | a5060b8 | 2017-11-03 14:16:25 +0100 | [diff] [blame] | 11537 | ep = listvars(on, off, /*strlist:*/ NULL, &epend); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11538 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 11539 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 11540 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11541 | |
| 11542 | for (; ep < epend; ep++) { |
| 11543 | const char *p; |
| 11544 | const char *q; |
| 11545 | |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11546 | p = endofname(*ep); |
| 11547 | /* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this |
| 11548 | * makes "export -p" to have output not suitable for "eval": |
| 11549 | * import os |
| 11550 | * os.environ["test-test"]="test" |
| 11551 | * if os.fork() == 0: |
| 11552 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 11553 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) |
| 11554 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11555 | q = nullstr; |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 11556 | if (*p == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11557 | q = single_quote(++p); |
| 11558 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 11559 | } |
| 11560 | return 0; |
| 11561 | } |
| 11562 | |
| 11563 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11564 | * The set command builtin. |
| 11565 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11566 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11567 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11568 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11569 | int retval; |
| 11570 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 11571 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11572 | return showvars(nullstr, 0, VUNSET); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11573 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11574 | INT_OFF; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 11575 | retval = options(/*login_sh:*/ NULL); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 11576 | if (retval == 0) { /* if no parse error... */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11577 | optschanged(); |
| 11578 | if (*argptr != NULL) { |
| 11579 | setparam(argptr); |
| 11580 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11581 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11582 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 11583 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11584 | } |
| 11585 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11586 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11587 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 11588 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11589 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11590 | uint32_t t; |
| 11591 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11592 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11593 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11594 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11595 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 11596 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11597 | vrandom.flags &= ~VNOFUNC; |
| 11598 | } else { |
| 11599 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 11600 | t = strtoul(value, NULL, 10); |
| 11601 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11602 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 11603 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 11604 | #endif |
| 11605 | |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11606 | #if BASH_EPOCH_VARS |
| 11607 | static void FAST_FUNC |
| 11608 | change_epoch(struct var *vepoch, const char *fmt) |
| 11609 | { |
| 11610 | struct timeval tv; |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11611 | char buffer[sizeof("%llu.nnnnnn") + sizeof(long long)*3]; |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11612 | |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11613 | xgettimeofday(&tv); |
| 11614 | 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] | 11615 | setvar(vepoch->var_text, buffer, VNOFUNC); |
| 11616 | vepoch->flags &= ~VNOFUNC; |
| 11617 | } |
| 11618 | |
| 11619 | static void FAST_FUNC |
| 11620 | change_seconds(const char *value UNUSED_PARAM) |
| 11621 | { |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11622 | change_epoch(&vepochs, "%llu"); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11623 | } |
| 11624 | |
| 11625 | static void FAST_FUNC |
| 11626 | change_realtime(const char *value UNUSED_PARAM) |
| 11627 | { |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 11628 | change_epoch(&vepochr, "%llu.%06u"); |
Ron Yorston | 1d37186 | 2019-04-15 10:52:05 +0100 | [diff] [blame] | 11629 | } |
| 11630 | #endif |
| 11631 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11632 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11633 | static int |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11634 | getopts(char *optstr, char *optvar, char **optfirst) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11635 | { |
| 11636 | char *p, *q; |
| 11637 | char c = '?'; |
| 11638 | int done = 0; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11639 | char sbuf[2]; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11640 | char **optnext; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11641 | int ind = shellparam.optind; |
| 11642 | int off = shellparam.optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11643 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11644 | sbuf[1] = '\0'; |
| 11645 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11646 | shellparam.optind = -1; |
| 11647 | optnext = optfirst + ind - 1; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 11648 | |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11649 | if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11650 | p = NULL; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11651 | else |
| 11652 | p = optnext[-1] + off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11653 | if (p == NULL || *p == '\0') { |
| 11654 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11655 | p = *optnext; |
| 11656 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11657 | atend: |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11658 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11659 | p = NULL; |
| 11660 | done = 1; |
| 11661 | goto out; |
| 11662 | } |
| 11663 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 11664 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11665 | goto atend; |
| 11666 | } |
| 11667 | |
| 11668 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 11669 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11670 | if (*q == '\0') { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11671 | /* OPTERR is a bashism */ |
| 11672 | const char *cp = lookupvar("OPTERR"); |
| 11673 | if ((cp && LONE_CHAR(cp, '0')) |
| 11674 | || (optstr[0] == ':') |
| 11675 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11676 | sbuf[0] = c; |
| 11677 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11678 | setvar0("OPTARG", sbuf); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11679 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11680 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11681 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11682 | } |
| 11683 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11684 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11685 | } |
| 11686 | if (*++q == ':') |
| 11687 | q++; |
| 11688 | } |
| 11689 | |
| 11690 | if (*++q == ':') { |
| 11691 | if (*p == '\0' && (p = *optnext) == NULL) { |
Denys Vlasenko | 9832bba | 2017-08-15 15:44:41 +0200 | [diff] [blame] | 11692 | /* OPTERR is a bashism */ |
| 11693 | const char *cp = lookupvar("OPTERR"); |
| 11694 | if ((cp && LONE_CHAR(cp, '0')) |
| 11695 | || (optstr[0] == ':') |
| 11696 | ) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11697 | sbuf[0] = c; |
| 11698 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11699 | setvar0("OPTARG", sbuf); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11700 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11701 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11702 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11703 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11704 | c = '?'; |
| 11705 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11706 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
| 11709 | if (p == *optnext) |
| 11710 | optnext++; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11711 | setvar0("OPTARG", p); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11712 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11713 | } else |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11714 | setvar0("OPTARG", nullstr); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11715 | out: |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11716 | ind = optnext - optfirst + 1; |
| 11717 | setvar("OPTIND", itoa(ind), VNOFUNC); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 11718 | sbuf[0] = c; |
| 11719 | /*sbuf[1] = '\0'; - already is */ |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11720 | setvar0(optvar, sbuf); |
| 11721 | |
| 11722 | shellparam.optoff = p ? p - *(optnext - 1) : -1; |
| 11723 | shellparam.optind = ind; |
| 11724 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11725 | return done; |
| 11726 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11727 | |
| 11728 | /* |
| 11729 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 11730 | * to be processed. Shellparam.optptr points to the next character to |
| 11731 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 11732 | * then it's the first time getopts has been called. |
| 11733 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11734 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11735 | getoptscmd(int argc, char **argv) |
| 11736 | { |
| 11737 | char **optbase; |
| 11738 | |
| 11739 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 11740 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11741 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11742 | optbase = shellparam.p; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11743 | if ((unsigned)shellparam.optind > shellparam.nparam + 1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11744 | shellparam.optind = 1; |
| 11745 | shellparam.optoff = -1; |
| 11746 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11747 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11748 | optbase = &argv[3]; |
Denys Vlasenko | dbef38a | 2016-10-26 17:54:32 +0200 | [diff] [blame] | 11749 | if ((unsigned)shellparam.optind > argc - 2) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11750 | shellparam.optind = 1; |
| 11751 | shellparam.optoff = -1; |
| 11752 | } |
| 11753 | } |
| 11754 | |
Denys Vlasenko | 35c2a13 | 2016-10-26 17:34:26 +0200 | [diff] [blame] | 11755 | return getopts(argv[1], argv[2], optbase); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11756 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11757 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11758 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11759 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11760 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11761 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11762 | struct heredoc { |
| 11763 | struct heredoc *next; /* next here document in list */ |
| 11764 | union node *here; /* redirection node */ |
| 11765 | char *eofmark; /* string indicating end of input */ |
| 11766 | smallint striptabs; /* if set, strip leading tabs */ |
| 11767 | }; |
| 11768 | |
| 11769 | static smallint tokpushback; /* last token pushed back */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 11770 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 11771 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 11772 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 11773 | static char *wordtext; /* text of last word returned by readtoken */ |
| 11774 | static struct nodelist *backquotelist; |
| 11775 | static union node *redirnode; |
| 11776 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11777 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11778 | static const char * |
| 11779 | tokname(char *buf, int tok) |
| 11780 | { |
| 11781 | if (tok < TSEMI) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 11782 | return tokname_array[tok]; |
| 11783 | sprintf(buf, "\"%s\"", tokname_array[tok]); |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11784 | return buf; |
| 11785 | } |
| 11786 | |
| 11787 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11788 | * Called when an unexpected token is read during the parse. The argument |
| 11789 | * is the token that is expected, or -1 if more than one type of token can |
| 11790 | * occur at this point. |
| 11791 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 11792 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11793 | static void |
| 11794 | raise_error_unexpected_syntax(int token) |
| 11795 | { |
| 11796 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11797 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11798 | int l; |
| 11799 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11800 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11801 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11802 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11803 | raise_error_syntax(msg); |
| 11804 | /* NOTREACHED */ |
| 11805 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11806 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11807 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 11808 | static union node *andor(void); |
| 11809 | static union node *pipeline(void); |
| 11810 | static union node *parse_command(void); |
| 11811 | static void parseheredoc(void); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11812 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11813 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11814 | static union node * |
| 11815 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11816 | { |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11817 | int chknl = nlflag & 1 ? 0 : CHKNL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11818 | union node *n1, *n2, *n3; |
| 11819 | int tok; |
| 11820 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11821 | n1 = NULL; |
| 11822 | for (;;) { |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11823 | checkkwd = chknl | CHKKWD | CHKALIAS; |
| 11824 | tok = readtoken(); |
| 11825 | switch (tok) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11826 | case TNL: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11827 | parseheredoc(); |
| 11828 | return n1; |
| 11829 | |
| 11830 | case TEOF: |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11831 | if (!n1 && !chknl) |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11832 | n1 = NODE_EOF; |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11833 | out_eof: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11834 | parseheredoc(); |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11835 | tokpushback++; |
| 11836 | lasttoken = TEOF; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11837 | return n1; |
| 11838 | } |
| 11839 | |
Denys Vlasenko | c08993f | 2020-02-22 17:26:23 +0100 | [diff] [blame] | 11840 | tokpushback++; |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11841 | if (nlflag == 2 && ((1 << tok) & tokendlist)) |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11842 | return n1; |
| 11843 | nlflag |= 2; |
| 11844 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11845 | n2 = andor(); |
| 11846 | tok = readtoken(); |
| 11847 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11848 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11849 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11850 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11851 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11852 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11853 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11854 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11855 | n2 = n3; |
| 11856 | } |
| 11857 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11858 | } |
| 11859 | } |
| 11860 | if (n1 == NULL) { |
| 11861 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11862 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11863 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11864 | n3->type = NSEMI; |
| 11865 | n3->nbinary.ch1 = n1; |
| 11866 | n3->nbinary.ch2 = n2; |
| 11867 | n1 = n3; |
| 11868 | } |
| 11869 | switch (tok) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11870 | case TEOF: |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11871 | goto out_eof; |
| 11872 | case TNL: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 11873 | tokpushback = 1; |
| 11874 | /* fall through */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11875 | case TBACKGND: |
| 11876 | case TSEMI: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11877 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11878 | default: |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 11879 | if (!chknl) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11880 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11881 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11882 | return n1; |
| 11883 | } |
| 11884 | } |
| 11885 | } |
| 11886 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11887 | static union node * |
| 11888 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11889 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11890 | union node *n1, *n2, *n3; |
| 11891 | int t; |
| 11892 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11893 | n1 = pipeline(); |
| 11894 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11895 | t = readtoken(); |
| 11896 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11897 | t = NAND; |
| 11898 | } else if (t == TOR) { |
| 11899 | t = NOR; |
| 11900 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11901 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11902 | return n1; |
| 11903 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11904 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11905 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11906 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11907 | n3->type = t; |
| 11908 | n3->nbinary.ch1 = n1; |
| 11909 | n3->nbinary.ch2 = n2; |
| 11910 | n1 = n3; |
| 11911 | } |
| 11912 | } |
| 11913 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11914 | static union node * |
| 11915 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11916 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11917 | union node *n1, *n2, *pipenode; |
| 11918 | struct nodelist *lp, *prev; |
| 11919 | int negate; |
| 11920 | |
| 11921 | negate = 0; |
| 11922 | TRACE(("pipeline: entered\n")); |
| 11923 | if (readtoken() == TNOT) { |
| 11924 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11925 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11926 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11927 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11928 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11929 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11930 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11931 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11932 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11933 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11934 | pipenode->npipe.cmdlist = lp; |
| 11935 | lp->n = n1; |
| 11936 | do { |
| 11937 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11938 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11939 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11940 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11941 | prev->next = lp; |
| 11942 | } while (readtoken() == TPIPE); |
| 11943 | lp->next = NULL; |
| 11944 | n1 = pipenode; |
| 11945 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11946 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11947 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11948 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11949 | n2->type = NNOT; |
| 11950 | n2->nnot.com = n1; |
| 11951 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11952 | } |
| 11953 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11954 | } |
| 11955 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11956 | static union node * |
| 11957 | makename(void) |
| 11958 | { |
| 11959 | union node *n; |
| 11960 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11961 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11962 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11963 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11964 | n->narg.text = wordtext; |
| 11965 | n->narg.backquote = backquotelist; |
| 11966 | return n; |
| 11967 | } |
| 11968 | |
| 11969 | static void |
| 11970 | fixredir(union node *n, const char *text, int err) |
| 11971 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11972 | int fd; |
| 11973 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11974 | TRACE(("Fix redir %s %d\n", text, err)); |
| 11975 | if (!err) |
| 11976 | n->ndup.vname = NULL; |
| 11977 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11978 | fd = bb_strtou(text, NULL, 10); |
| 11979 | if (!errno && fd >= 0) |
| 11980 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11981 | else if (LONE_DASH(text)) |
| 11982 | n->ndup.dupfd = -1; |
| 11983 | else { |
| 11984 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11985 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11986 | n->ndup.vname = makename(); |
| 11987 | } |
| 11988 | } |
| 11989 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11990 | static void |
| 11991 | parsefname(void) |
| 11992 | { |
| 11993 | union node *n = redirnode; |
| 11994 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 11995 | if (n->type == NHERE) |
| 11996 | checkkwd = CHKEOFMARK; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11997 | if (readtoken() != TWORD) |
| 11998 | raise_error_unexpected_syntax(-1); |
| 11999 | if (n->type == NHERE) { |
| 12000 | struct heredoc *here = heredoc; |
| 12001 | struct heredoc *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12002 | |
| 12003 | if (quoteflag == 0) |
| 12004 | n->type = NXHERE; |
| 12005 | TRACE(("Here document %d\n", n->type)); |
Denys Vlasenko | 740058b | 2018-01-09 17:01:00 +0100 | [diff] [blame] | 12006 | rmescapes(wordtext, 0, NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12007 | here->eofmark = wordtext; |
| 12008 | here->next = NULL; |
| 12009 | if (heredoclist == NULL) |
| 12010 | heredoclist = here; |
| 12011 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12012 | for (p = heredoclist; p->next; p = p->next) |
| 12013 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12014 | p->next = here; |
| 12015 | } |
| 12016 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 12017 | fixredir(n, wordtext, 0); |
| 12018 | } else { |
| 12019 | n->nfile.fname = makename(); |
| 12020 | } |
| 12021 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12022 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12023 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12024 | simplecmd(void) |
| 12025 | { |
| 12026 | union node *args, **app; |
| 12027 | union node *n = NULL; |
| 12028 | union node *vars, **vpp; |
| 12029 | union node **rpp, *redir; |
| 12030 | int savecheckkwd; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12031 | int savelinno; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12032 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12033 | smallint double_brackets_flag = 0; |
| 12034 | #endif |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12035 | IF_BASH_FUNCTION(smallint function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12036 | |
| 12037 | args = NULL; |
| 12038 | app = &args; |
| 12039 | vars = NULL; |
| 12040 | vpp = &vars; |
| 12041 | redir = NULL; |
| 12042 | rpp = &redir; |
| 12043 | |
| 12044 | savecheckkwd = CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12045 | savelinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12046 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12047 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12048 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12049 | t = readtoken(); |
| 12050 | switch (t) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12051 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12052 | case TFUNCTION: |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 12053 | if (readtoken() != TWORD) |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12054 | raise_error_unexpected_syntax(TWORD); |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 12055 | tokpushback = 1; |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12056 | function_flag = 1; |
| 12057 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12058 | #endif |
| 12059 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12060 | case TAND: /* "&&" */ |
| 12061 | case TOR: /* "||" */ |
| 12062 | if (!double_brackets_flag) { |
| 12063 | tokpushback = 1; |
| 12064 | goto out; |
| 12065 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 12066 | /* pass "&&" or "||" to [[ ]] as literal args */ |
| 12067 | wordtext = (char *) (t == TAND ? "&&" : "||"); |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12068 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12069 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12070 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12071 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12072 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12073 | n->narg.text = wordtext; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12074 | #if BASH_TEST2 |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 12075 | if (strcmp("[[", wordtext) == 0) |
| 12076 | double_brackets_flag = 1; |
| 12077 | else if (strcmp("]]", wordtext) == 0) |
| 12078 | double_brackets_flag = 0; |
| 12079 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12080 | n->narg.backquote = backquotelist; |
| 12081 | if (savecheckkwd && isassignment(wordtext)) { |
| 12082 | *vpp = n; |
| 12083 | vpp = &n->narg.next; |
| 12084 | } else { |
| 12085 | *app = n; |
| 12086 | app = &n->narg.next; |
| 12087 | savecheckkwd = 0; |
| 12088 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12089 | #if BASH_FUNCTION |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12090 | if (function_flag) { |
| 12091 | checkkwd = CHKNL | CHKKWD; |
Denys Vlasenko | 30af593 | 2021-09-08 00:39:16 +0200 | [diff] [blame] | 12092 | t = readtoken(); |
| 12093 | tokpushback = 1; |
| 12094 | switch (t) { |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12095 | case TBEGIN: |
| 12096 | case TIF: |
| 12097 | case TCASE: |
| 12098 | case TUNTIL: |
| 12099 | case TWHILE: |
| 12100 | case TFOR: |
| 12101 | goto do_func; |
| 12102 | case TLP: |
| 12103 | function_flag = 0; |
| 12104 | break; |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 12105 | # if BASH_TEST2 |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12106 | case TWORD: |
| 12107 | if (strcmp("[[", wordtext) == 0) |
| 12108 | goto do_func; |
| 12109 | /* fall through */ |
Denys Vlasenko | e93031e | 2018-04-10 01:23:19 +0200 | [diff] [blame] | 12110 | # endif |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 12111 | default: |
| 12112 | raise_error_unexpected_syntax(-1); |
| 12113 | } |
| 12114 | } |
| 12115 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12116 | break; |
| 12117 | case TREDIR: |
| 12118 | *rpp = n = redirnode; |
| 12119 | rpp = &n->nfile.next; |
| 12120 | parsefname(); /* read name of redirection file */ |
| 12121 | break; |
| 12122 | case TLP: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12123 | IF_BASH_FUNCTION(do_func:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12124 | if (args && app == &args->narg.next |
| 12125 | && !vars && !redir |
| 12126 | ) { |
| 12127 | struct builtincmd *bcmd; |
| 12128 | const char *name; |
| 12129 | |
| 12130 | /* We have a function */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12131 | if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12132 | raise_error_unexpected_syntax(TRP); |
| 12133 | name = n->narg.text; |
| 12134 | if (!goodname(name) |
| 12135 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 12136 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12137 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12138 | } |
| 12139 | n->type = NDEFUN; |
| 12140 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12141 | n->ndefun.text = n->narg.text; |
| 12142 | n->ndefun.linno = g_parsefile->linno; |
| 12143 | n->ndefun.body = parse_command(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12144 | return n; |
| 12145 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12146 | IF_BASH_FUNCTION(function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12147 | /* fall through */ |
| 12148 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12149 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12150 | goto out; |
| 12151 | } |
| 12152 | } |
| 12153 | out: |
| 12154 | *app = NULL; |
| 12155 | *vpp = NULL; |
| 12156 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12157 | n = stzalloc(sizeof(struct ncmd)); |
Denys Vlasenko | 57b7efb | 2018-04-10 01:20:26 +0200 | [diff] [blame] | 12158 | if (NCMD != 0) |
| 12159 | n->type = NCMD; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12160 | n->ncmd.linno = savelinno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12161 | n->ncmd.args = args; |
| 12162 | n->ncmd.assign = vars; |
| 12163 | n->ncmd.redirect = redir; |
| 12164 | return n; |
| 12165 | } |
| 12166 | |
| 12167 | static union node * |
| 12168 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12169 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12170 | union node *n1, *n2; |
| 12171 | union node *ap, **app; |
| 12172 | union node *cp, **cpp; |
| 12173 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12174 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12175 | int t; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12176 | int savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12177 | |
| 12178 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12179 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 12180 | |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12181 | savelinno = g_parsefile->linno; |
| 12182 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12183 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12184 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12185 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12186 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12187 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12188 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12189 | n1->type = NIF; |
| 12190 | n1->nif.test = list(0); |
| 12191 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12192 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12193 | n1->nif.ifpart = list(0); |
| 12194 | n2 = n1; |
| 12195 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12196 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12197 | n2 = n2->nif.elsepart; |
| 12198 | n2->type = NIF; |
| 12199 | n2->nif.test = list(0); |
| 12200 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12201 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12202 | n2->nif.ifpart = list(0); |
| 12203 | } |
| 12204 | if (lasttoken == TELSE) |
| 12205 | n2->nif.elsepart = list(0); |
| 12206 | else { |
| 12207 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12208 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12209 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12210 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12211 | break; |
| 12212 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12213 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12214 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12215 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12216 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12217 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12218 | got = readtoken(); |
| 12219 | if (got != TDO) { |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 12220 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got], |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12221 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12222 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12223 | } |
| 12224 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12225 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12226 | break; |
| 12227 | } |
| 12228 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 12229 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12230 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12231 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12232 | n1->type = NFOR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12233 | n1->nfor.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12234 | n1->nfor.var = wordtext; |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 12235 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12236 | if (readtoken() == TIN) { |
| 12237 | app = ≈ |
| 12238 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12239 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12240 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12241 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12242 | n2->narg.text = wordtext; |
| 12243 | n2->narg.backquote = backquotelist; |
| 12244 | *app = n2; |
| 12245 | app = &n2->narg.next; |
| 12246 | } |
| 12247 | *app = NULL; |
| 12248 | n1->nfor.args = ap; |
| 12249 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12250 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12251 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12252 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12253 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12254 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12255 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12256 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12257 | n1->nfor.args = n2; |
| 12258 | /* |
| 12259 | * Newline or semicolon here is optional (but note |
| 12260 | * that the original Bourne shell only allowed NL). |
| 12261 | */ |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 12262 | if (lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12263 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12264 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12265 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12266 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12267 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12268 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12269 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12270 | break; |
| 12271 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12272 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12273 | n1->type = NCASE; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12274 | n1->ncase.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12275 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12276 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12277 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12278 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12279 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12280 | n2->narg.text = wordtext; |
| 12281 | n2->narg.backquote = backquotelist; |
Ron Yorston | 383b885 | 2015-08-03 13:46:25 +0100 | [diff] [blame] | 12282 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 12283 | if (readtoken() != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12284 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12285 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12286 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12287 | checkkwd = CHKNL | CHKKWD; |
| 12288 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12289 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12290 | if (lasttoken == TLP) |
| 12291 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12292 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12293 | cp->type = NCLIST; |
| 12294 | app = &cp->nclist.pattern; |
| 12295 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12296 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12297 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12298 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12299 | ap->narg.text = wordtext; |
| 12300 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12301 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12302 | break; |
| 12303 | app = &ap->narg.next; |
| 12304 | readtoken(); |
| 12305 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12306 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12307 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12308 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12309 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12310 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12311 | cpp = &cp->nclist.next; |
| 12312 | |
| 12313 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12314 | t = readtoken(); |
| 12315 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12316 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12317 | raise_error_unexpected_syntax(TENDCASE); |
| 12318 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12319 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12320 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12321 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12322 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12323 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12324 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12325 | n1->type = NSUBSHELL; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12326 | n1->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12327 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12328 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12329 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12330 | break; |
| 12331 | case TBEGIN: |
| 12332 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12333 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12334 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12335 | IF_BASH_FUNCTION(case TFUNCTION:) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12336 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12337 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12338 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12339 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12340 | } |
| 12341 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12342 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12343 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12344 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12345 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12346 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12347 | checkkwd = CHKKWD | CHKALIAS; |
| 12348 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12349 | while (readtoken() == TREDIR) { |
| 12350 | *rpp = n2 = redirnode; |
| 12351 | rpp = &n2->nfile.next; |
| 12352 | parsefname(); |
| 12353 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12354 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12355 | *rpp = NULL; |
| 12356 | if (redir) { |
| 12357 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12358 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12359 | n2->type = NREDIR; |
Denys Vlasenko | 675d24a | 2018-01-27 22:02:05 +0100 | [diff] [blame] | 12360 | n2->nredir.linno = savelinno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12361 | n2->nredir.n = n1; |
| 12362 | n1 = n2; |
| 12363 | } |
| 12364 | n1->nredir.redirect = redir; |
| 12365 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12366 | return n1; |
| 12367 | } |
| 12368 | |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12369 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 37dc08b | 2016-10-02 04:38:07 +0200 | [diff] [blame] | 12370 | static int |
| 12371 | decode_dollar_squote(void) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12372 | { |
| 12373 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 12374 | int c, cnt; |
| 12375 | char *p; |
| 12376 | char buf[4]; |
| 12377 | |
| 12378 | c = pgetc(); |
| 12379 | p = strchr(C_escapes, c); |
| 12380 | if (p) { |
| 12381 | buf[0] = c; |
| 12382 | p = buf; |
| 12383 | cnt = 3; |
| 12384 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 12385 | do { |
| 12386 | c = pgetc(); |
| 12387 | *++p = c; |
| 12388 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 12389 | pungetc(); |
| 12390 | } else if (c == 'x') { /* \xHH */ |
| 12391 | do { |
| 12392 | c = pgetc(); |
| 12393 | *++p = c; |
| 12394 | } while (isxdigit(c) && --cnt); |
| 12395 | pungetc(); |
| 12396 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 12397 | c = 'x'; |
| 12398 | goto unrecognized; |
| 12399 | } |
| 12400 | } else { /* simple seq like \\ or \t */ |
| 12401 | p++; |
| 12402 | } |
| 12403 | *p = '\0'; |
| 12404 | p = buf; |
| 12405 | c = bb_process_escape_sequence((void*)&p); |
| 12406 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 12407 | if (c != '\'' && c != '"') { |
| 12408 | unrecognized: |
| 12409 | c |= 0x100; /* "please encode \, then me" */ |
| 12410 | } |
| 12411 | } |
| 12412 | return c; |
| 12413 | } |
| 12414 | #endif |
| 12415 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12416 | /* Used by expandstr to get here-doc like behaviour. */ |
| 12417 | #define FAKEEOFMARK ((char*)(uintptr_t)1) |
| 12418 | |
| 12419 | static ALWAYS_INLINE int |
| 12420 | realeofmark(const char *eofmark) |
| 12421 | { |
| 12422 | return eofmark && eofmark != FAKEEOFMARK; |
| 12423 | } |
| 12424 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12425 | /* |
| 12426 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 12427 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 12428 | * 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] | 12429 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12430 | * is the first character of the input token or document. |
| 12431 | * |
| 12432 | * Because C does not have internal subroutines, I have simulated them |
| 12433 | * using goto's to implement the subroutine linkage. The following macros |
| 12434 | * will run code that appears at the end of readtoken1. |
| 12435 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12436 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 12437 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 12438 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 12439 | #define PARSEBACKQOLD() {style = OLD; goto parsebackq; parsebackq_oldreturn:;} |
| 12440 | #define PARSEBACKQNEW() {style = NEW; goto parsebackq; parsebackq_newreturn:;} |
| 12441 | #define PARSEPROCSUB() {style = PSUB; goto parsebackq; parsebackq_psreturn:;} |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 12442 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12443 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12444 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 12445 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12446 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12447 | /* c parameter is an unsigned char or PEOF */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12448 | char *out; |
Denys Vlasenko | 50e6d42 | 2016-09-30 11:35:54 +0200 | [diff] [blame] | 12449 | size_t len; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12450 | struct nodelist *bqlist; |
| 12451 | smallint quotef; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 12452 | smallint style; |
| 12453 | enum { OLD, NEW, PSUB }; |
| 12454 | #define oldstyle (style == OLD) |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12455 | smallint pssyntax; /* we are expanding a prompt string */ |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12456 | IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12457 | /* syntax stack */ |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12458 | struct synstack synbase = { }; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12459 | struct synstack *synstack = &synbase; |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12460 | |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12461 | #if ENABLE_ASH_EXPAND_PRMT |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12462 | pssyntax = (syntax == PSSYNTAX); |
| 12463 | if (pssyntax) |
| 12464 | syntax = DQSYNTAX; |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12465 | #else |
| 12466 | pssyntax = 0; /* constant */ |
| 12467 | #endif |
Denys Vlasenko | ee1fd12 | 2018-04-04 13:59:53 +0200 | [diff] [blame] | 12468 | synstack->syntax = syntax; |
| 12469 | |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12470 | if (syntax == DQSYNTAX) |
| 12471 | synstack->dblquote = 1; |
| 12472 | quotef = 0; |
| 12473 | bqlist = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12474 | |
| 12475 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12476 | loop: |
| 12477 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12478 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 12479 | for (;;) { /* until end of line or end of word */ |
| 12480 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12481 | switch (SIT(c, synstack->syntax)) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12482 | case CNL: /* '\n' */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12483 | if (synstack->syntax == BASESYNTAX |
| 12484 | && !synstack->varnest |
| 12485 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12486 | goto endword; /* exit outer loop */ |
Denys Vlasenko | 680c301 | 2018-04-11 12:39:18 +0200 | [diff] [blame] | 12487 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12488 | USTPUTC(c, out); |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 12489 | nlprompt(); |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12490 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12491 | goto loop; /* continue outer loop */ |
| 12492 | case CWORD: |
| 12493 | USTPUTC(c, out); |
| 12494 | break; |
| 12495 | case CCTL: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12496 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12497 | if (c == '\\' && bash_dollar_squote) { |
| 12498 | c = decode_dollar_squote(); |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12499 | if (c == '\0') { |
| 12500 | /* skip $'\000', $'\x00' (like bash) */ |
| 12501 | break; |
| 12502 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12503 | if (c & 0x100) { |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12504 | /* Unknown escape. Encode as '\z' */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12505 | c = (unsigned char)c; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12506 | if (eofmark == NULL || synstack->dblquote) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12507 | USTPUTC(CTLESC, out); |
| 12508 | USTPUTC('\\', out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12509 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12510 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12511 | #endif |
Denys Vlasenko | c4c2012 | 2018-04-02 13:29:20 +0200 | [diff] [blame] | 12512 | if (!eofmark || synstack->dblquote || synstack->varnest) |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 12513 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12514 | USTPUTC(c, out); |
| 12515 | break; |
| 12516 | case CBACK: /* backslash */ |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12517 | c = pgetc(); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12518 | if (c == PEOF) { |
| 12519 | USTPUTC(CTLESC, out); |
| 12520 | USTPUTC('\\', out); |
| 12521 | pungetc(); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12522 | } else { |
Denys Vlasenko | 5f0a75f | 2017-07-29 22:58:44 +0200 | [diff] [blame] | 12523 | if (pssyntax && c == '$') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12524 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12525 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12526 | } |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12527 | /* Backslash is retained if we are in "str" |
| 12528 | * and next char isn't dquote-special. |
| 12529 | */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12530 | if (synstack->dblquote |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12531 | && c != '\\' |
| 12532 | && c != '`' |
| 12533 | && c != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12534 | && (c != '"' || (eofmark != NULL && !synstack->varnest)) |
| 12535 | && (c != '}' || !synstack->varnest) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12536 | ) { |
Denys Vlasenko | 8de5b9f | 2018-02-13 14:43:29 +0100 | [diff] [blame] | 12537 | USTPUTC(CTLESC, out); /* protect '\' from glob */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12538 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12539 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12540 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 12541 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12542 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12543 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12544 | break; |
| 12545 | case CSQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12546 | synstack->syntax = SQSYNTAX; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12547 | quotemark: |
| 12548 | if (eofmark == NULL) { |
| 12549 | USTPUTC(CTLQUOTEMARK, out); |
| 12550 | } |
| 12551 | break; |
| 12552 | case CDQUOTE: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12553 | synstack->syntax = DQSYNTAX; |
| 12554 | synstack->dblquote = 1; |
| 12555 | toggledq: |
| 12556 | if (synstack->varnest) |
| 12557 | synstack->innerdq ^= 1; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12558 | goto quotemark; |
| 12559 | case CENDQUOTE: |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12560 | IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12561 | if (eofmark != NULL && synstack->varnest == 0) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12562 | USTPUTC(c, out); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12563 | break; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12564 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12565 | |
| 12566 | if (synstack->dqvarnest == 0) { |
| 12567 | synstack->syntax = BASESYNTAX; |
| 12568 | synstack->dblquote = 0; |
| 12569 | } |
| 12570 | |
| 12571 | quotef = 1; |
| 12572 | |
| 12573 | if (c == '"') |
| 12574 | goto toggledq; |
| 12575 | |
| 12576 | goto quotemark; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12577 | case CVAR: /* '$' */ |
| 12578 | PARSESUB(); /* parse substitution */ |
| 12579 | break; |
| 12580 | case CENDVAR: /* '}' */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12581 | if (!synstack->innerdq && synstack->varnest > 0) { |
| 12582 | if (!--synstack->varnest && synstack->varpushed) |
| 12583 | synstack_pop(&synstack); |
| 12584 | else if (synstack->dqvarnest > 0) |
| 12585 | synstack->dqvarnest--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12586 | c = CTLENDVAR; |
| 12587 | } |
| 12588 | USTPUTC(c, out); |
| 12589 | break; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12590 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12591 | case CLP: /* '(' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12592 | synstack->parenlevel++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12593 | USTPUTC(c, out); |
| 12594 | break; |
| 12595 | case CRP: /* ')' in arithmetic */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12596 | if (synstack->parenlevel > 0) { |
| 12597 | synstack->parenlevel--; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12598 | } else { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12599 | if (pgetc_eatbnl() == ')') { |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 12600 | c = CTLENDARI; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12601 | synstack_pop(&synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12602 | } else { |
| 12603 | /* |
| 12604 | * unbalanced parens |
| 12605 | * (don't 2nd guess - no error) |
| 12606 | */ |
| 12607 | pungetc(); |
| 12608 | } |
| 12609 | } |
| 12610 | USTPUTC(c, out); |
| 12611 | break; |
| 12612 | #endif |
| 12613 | case CBQUOTE: /* '`' */ |
Denys Vlasenko | 41fddb4 | 2018-04-01 16:38:32 +0200 | [diff] [blame] | 12614 | if (checkkwd & CHKEOFMARK) { |
| 12615 | quotef = 1; |
| 12616 | USTPUTC('`', out); |
| 12617 | break; |
| 12618 | } |
| 12619 | |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12620 | PARSEBACKQOLD(); |
| 12621 | break; |
| 12622 | case CENDFILE: |
| 12623 | goto endword; /* exit outer loop */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12624 | default: |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12625 | if (synstack->varnest == 0) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12626 | #if BASH_REDIR_OUTPUT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12627 | if (c == '&') { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 12628 | //Can't call pgetc_eatbnl() here, this requires three-deep pungetc() |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12629 | if (pgetc() == '>') |
| 12630 | c = 0x100 + '>'; /* flag &> */ |
| 12631 | pungetc(); |
| 12632 | } |
| 12633 | #endif |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 12634 | #if BASH_PROCESS_SUBST |
| 12635 | if (c == '<' || c == '>') { |
| 12636 | if (pgetc() == '(') { |
| 12637 | PARSEPROCSUB(); |
| 12638 | break; |
| 12639 | } |
| 12640 | pungetc(); |
| 12641 | } |
| 12642 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12643 | goto endword; /* exit outer loop */ |
| 12644 | } |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12645 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12646 | } |
Denys Vlasenko | f7eea8c | 2020-02-14 16:16:34 +0100 | [diff] [blame] | 12647 | c = pgetc_top(synstack); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12648 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12649 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12650 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12651 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12652 | if (synstack->syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12653 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12654 | #endif |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12655 | if (synstack->syntax != BASESYNTAX && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12656 | raise_error_syntax("unterminated quoted string"); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12657 | if (synstack->varnest != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12658 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12659 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12660 | } |
| 12661 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12662 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12663 | out = stackblock(); |
| 12664 | if (eofmark == NULL) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12665 | if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12666 | && quotef == 0 |
| 12667 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12668 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12669 | PARSEREDIR(); /* passed as params: out, c */ |
| 12670 | lasttoken = TREDIR; |
| 12671 | return lasttoken; |
| 12672 | } |
| 12673 | /* else: non-number X seen, interpret it |
| 12674 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12675 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12676 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12677 | } |
| 12678 | quoteflag = quotef; |
| 12679 | backquotelist = bqlist; |
| 12680 | grabstackblock(len); |
| 12681 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12682 | lasttoken = TWORD; |
| 12683 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12684 | /* end of readtoken routine */ |
| 12685 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12686 | /* |
| 12687 | * Check to see whether we are at the end of the here document. When this |
| 12688 | * is called, c is set to the first character of the next input line. If |
| 12689 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 12690 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12691 | checkend: { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 12692 | if (realeofmark(eofmark)) { |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12693 | int markloc; |
| 12694 | char *p; |
| 12695 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12696 | if (striptabs) { |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12697 | while (c == '\t') |
| 12698 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12699 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12700 | |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12701 | markloc = out - (char *)stackblock(); |
| 12702 | for (p = eofmark; STPUTC(c, out), *p; p++) { |
| 12703 | if (c != *p) |
| 12704 | goto more_heredoc; |
Denys Vlasenko | 35e349d | 2019-09-05 14:31:49 +0200 | [diff] [blame] | 12705 | /* FIXME: fails for backslash-newlined terminator: |
| 12706 | * cat <<EOF |
| 12707 | * ... |
| 12708 | * EO\ |
| 12709 | * F |
| 12710 | * (see heredoc_bkslash_newline2.tests) |
| 12711 | */ |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12712 | c = pgetc(); |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12713 | } |
| 12714 | |
| 12715 | if (c == '\n' || c == PEOF) { |
| 12716 | c = PEOF; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 12717 | if (trap_depth == 0) |
| 12718 | g_parsefile->linno++; |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12719 | needprompt = doprompt; |
| 12720 | } else { |
| 12721 | int len_here; |
| 12722 | |
| 12723 | more_heredoc: |
| 12724 | p = (char *)stackblock() + markloc + 1; |
| 12725 | len_here = out - p; |
| 12726 | |
| 12727 | if (len_here) { |
| 12728 | len_here -= (c >= PEOF); |
| 12729 | c = p[-1]; |
| 12730 | |
| 12731 | if (len_here) { |
| 12732 | char *str; |
| 12733 | |
| 12734 | str = alloca(len_here + 1); |
| 12735 | *(char *)mempcpy(str, p, len_here) = '\0'; |
| 12736 | |
| 12737 | pushstring(str, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12738 | } |
| 12739 | } |
| 12740 | } |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12741 | |
| 12742 | STADJUST((char *)stackblock() + markloc - out, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12743 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12744 | goto checkend_return; |
| 12745 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12746 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12747 | /* |
| 12748 | * Parse a redirection operator. The variable "out" points to a string |
| 12749 | * specifying the fd to be redirected. The variable "c" contains the |
| 12750 | * first character of the redirection operator. |
| 12751 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12752 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12753 | /* out is already checked to be a valid number or "" */ |
| 12754 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12755 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12756 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12757 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12758 | if (c == '>') { |
| 12759 | np->nfile.fd = 1; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12760 | c = pgetc_eatbnl(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12761 | if (c == '>') |
| 12762 | np->type = NAPPEND; |
| 12763 | else if (c == '|') |
| 12764 | np->type = NCLOBBER; |
| 12765 | else if (c == '&') |
| 12766 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 12767 | /* 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] | 12768 | else { |
| 12769 | np->type = NTO; |
| 12770 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12771 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12772 | } |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12773 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12774 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 12775 | np->nfile.fd = 1; |
| 12776 | pgetc(); /* this is '>', no need to check */ |
| 12777 | np->type = NTO2; |
| 12778 | } |
| 12779 | #endif |
| 12780 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12781 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12782 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12783 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12784 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 12785 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12786 | np = stzalloc(sizeof(struct nhere)); |
| 12787 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12788 | } |
| 12789 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12790 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12791 | heredoc->here = np; |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 12792 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12793 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12794 | heredoc->striptabs = 1; |
| 12795 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12796 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12797 | pungetc(); |
| 12798 | } |
| 12799 | break; |
| 12800 | |
| 12801 | case '&': |
| 12802 | np->type = NFROMFD; |
| 12803 | break; |
| 12804 | |
| 12805 | case '>': |
| 12806 | np->type = NFROMTO; |
| 12807 | break; |
| 12808 | |
| 12809 | default: |
| 12810 | np->type = NFROM; |
| 12811 | pungetc(); |
| 12812 | break; |
| 12813 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12814 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 12815 | if (fd >= 0) |
| 12816 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12817 | redirnode = np; |
| 12818 | goto parseredir_return; |
| 12819 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12820 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12821 | /* |
| 12822 | * Parse a substitution. At this point, we have read the dollar sign |
| 12823 | * and nothing else. |
| 12824 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12825 | |
| 12826 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 12827 | * (assuming ascii char codes, as the original implementation did) */ |
| 12828 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12829 | (((unsigned)(c) - 33 < 32) \ |
| 12830 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12831 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 12832 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12833 | int typeloc; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12834 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12835 | c = pgetc_eatbnl(); |
Denys Vlasenko | a732898 | 2017-07-29 19:57:28 +0200 | [diff] [blame] | 12836 | if ((checkkwd & CHKEOFMARK) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12837 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12838 | ) { |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12839 | #if BASH_DOLLAR_SQUOTE |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12840 | if (synstack->syntax != DQSYNTAX && c == '\'') |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 12841 | bash_dollar_squote = 1; |
| 12842 | else |
| 12843 | #endif |
| 12844 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12845 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12846 | } else if (c == '(') { |
| 12847 | /* $(command) or $((arith)) */ |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12848 | if (pgetc_eatbnl() == '(') { |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 12849 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12850 | PARSEARITH(); |
| 12851 | #else |
Denys Vlasenko | 4f8079d | 2017-07-17 17:11:48 +0200 | [diff] [blame] | 12852 | raise_error_syntax("support for $((arith)) is disabled"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12853 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 12854 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12855 | pungetc(); |
| 12856 | PARSEBACKQNEW(); |
| 12857 | } |
| 12858 | } else { |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 12859 | /* $VAR, $<specialchar>, ${...}, or PEOF */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12860 | smalluint newsyn = synstack->syntax; |
| 12861 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12862 | USTPUTC(CTLVAR, out); |
| 12863 | typeloc = out - (char *)stackblock(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12864 | STADJUST(1, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12865 | subtype = VSNORMAL; |
| 12866 | if (c == '{') { |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12867 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12868 | subtype = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12869 | } |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12870 | varname: |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12871 | if (is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12872 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12873 | do { |
| 12874 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12875 | c = pgetc_eatbnl(); |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12876 | } while (is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12877 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12878 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12879 | do { |
| 12880 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12881 | c = pgetc_eatbnl(); |
Denys Vlasenko | 53a7a9c | 2021-06-25 02:09:41 +0200 | [diff] [blame] | 12882 | } while ((subtype == 0 || subtype == VSLENGTH) && isdigit(c)); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12883 | } else if (c != '}') { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12884 | /* $[{[#]]<specialchar>[}] */ |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12885 | int cc = c; |
| 12886 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12887 | c = pgetc_eatbnl(); |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12888 | if (!subtype && cc == '#') { |
| 12889 | subtype = VSLENGTH; |
| 12890 | if (c == '_' || isalnum(c)) |
| 12891 | goto varname; |
| 12892 | cc = c; |
| 12893 | c = pgetc_eatbnl(); |
| 12894 | if (cc == '}' || c != '}') { |
| 12895 | pungetc(); |
| 12896 | subtype = 0; |
| 12897 | c = cc; |
| 12898 | cc = '#'; |
| 12899 | } |
| 12900 | } |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12901 | |
| 12902 | if (!is_special(cc)) { |
| 12903 | if (subtype == VSLENGTH) |
| 12904 | subtype = 0; |
| 12905 | goto badsub; |
| 12906 | } |
| 12907 | |
Denys Vlasenko | f15aa57 | 2016-10-26 15:56:53 +0200 | [diff] [blame] | 12908 | USTPUTC(cc, out); |
Denys Vlasenko | 58eb805 | 2018-08-05 15:58:13 +0200 | [diff] [blame] | 12909 | } else |
| 12910 | goto badsub; |
Denys Vlasenko | 452cc1d | 2017-08-14 14:23:45 +0200 | [diff] [blame] | 12911 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12912 | if (subtype == 0) { |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12913 | static const char types[] ALIGN1 = "}-+?="; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12914 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 12915 | /* c == first char after VAR */ |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12916 | int cc = c; |
| 12917 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12918 | switch (c) { |
| 12919 | case ':': |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12920 | c = pgetc_eatbnl(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12921 | #if BASH_SUBSTR |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 12922 | /* This check is only needed to not misinterpret |
| 12923 | * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD} |
| 12924 | * constructs. |
| 12925 | */ |
| 12926 | if (!strchr(types, c)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12927 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12928 | pungetc(); |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12929 | break; /* "goto badsub" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12930 | } |
| 12931 | #endif |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12932 | subtype = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12933 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12934 | default: { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12935 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12936 | if (p == NULL) |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12937 | break; |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12938 | subtype |= p - types + VSNORMAL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12939 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12940 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12941 | case '%': |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12942 | case '#': |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12943 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12944 | c = pgetc_eatbnl(); |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12945 | if (c == cc) |
| 12946 | subtype++; |
| 12947 | else |
| 12948 | pungetc(); |
| 12949 | |
| 12950 | newsyn = BASESYNTAX; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12951 | break; |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 12952 | #if BASH_PATTERN_SUBST |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12953 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 12954 | /* ${v/[/]pattern/repl} */ |
| 12955 | //TODO: encode pattern and repl separately. |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12956 | // Currently cases like: v=1;echo ${v/$((1/1))/ONE} |
| 12957 | // are broken (should print "ONE") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12958 | subtype = VSREPLACE; |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12959 | newsyn = BASESYNTAX; |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 12960 | c = pgetc_eatbnl(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12961 | if (c != '/') |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12962 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 12963 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 12964 | break; |
| 12965 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12966 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12967 | } else { |
Denys Vlasenko | 53a7a9c | 2021-06-25 02:09:41 +0200 | [diff] [blame] | 12968 | if (subtype == VSLENGTH && c != '}') |
| 12969 | subtype = 0; |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12970 | badsub: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12971 | pungetc(); |
| 12972 | } |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12973 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12974 | if (newsyn == ARISYNTAX) |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12975 | newsyn = DQSYNTAX; |
| 12976 | |
Denys Vlasenko | f50e146 | 2018-04-02 21:00:59 +0200 | [diff] [blame] | 12977 | if ((newsyn != synstack->syntax || synstack->innerdq) |
| 12978 | && subtype != VSNORMAL |
| 12979 | ) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12980 | synstack_push(&synstack, |
| 12981 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 12982 | newsyn); |
| 12983 | |
| 12984 | synstack->varpushed = 1; |
| 12985 | synstack->dblquote = newsyn != BASESYNTAX; |
| 12986 | } |
| 12987 | |
Denys Vlasenko | 3df1410 | 2016-10-26 16:41:13 +0200 | [diff] [blame] | 12988 | ((unsigned char *)stackblock())[typeloc] = subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12989 | if (subtype != VSNORMAL) { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 12990 | synstack->varnest++; |
| 12991 | if (synstack->dblquote) |
| 12992 | synstack->dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12993 | } |
Denys Vlasenko | 88e1570 | 2016-10-26 01:55:56 +0200 | [diff] [blame] | 12994 | STPUTC('=', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12995 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12996 | goto parsesub_return; |
| 12997 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12998 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12999 | /* |
| 13000 | * Called to parse command substitutions. Newstyle is set if the command |
| 13001 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 13002 | * list of commands (passed by reference), and savelen is the number of |
| 13003 | * characters on the top of the stack which must be preserved. |
| 13004 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13005 | parsebackq: { |
| 13006 | struct nodelist **nlpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13007 | union node *n; |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 13008 | char *str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13009 | size_t savelen; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13010 | struct heredoc *saveheredoclist; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13011 | smallint saveprompt = 0; |
| 13012 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13013 | str = NULL; |
| 13014 | savelen = out - (char *)stackblock(); |
| 13015 | if (savelen > 0) { |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 13016 | /* |
| 13017 | * FIXME: this can allocate very large block on stack and SEGV. |
| 13018 | * Example: |
| 13019 | * echo "..<100kbytes>..`true` $(true) `true` ..." |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 13020 | * allocates 100kb for every command subst. With about |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 13021 | * a hundred command substitutions stack overflows. |
| 13022 | * With larger prepended string, SEGV happens sooner. |
| 13023 | */ |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 13024 | str = alloca(savelen); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13025 | memcpy(str, stackblock(), savelen); |
| 13026 | } |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 13027 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13028 | if (oldstyle) { |
| 13029 | /* We must read until the closing backquote, giving special |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 13030 | * treatment to some slashes, and then push the string and |
| 13031 | * reread it as input, interpreting it normally. |
| 13032 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13033 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13034 | size_t psavelen; |
| 13035 | char *pstr; |
| 13036 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13037 | STARTSTACKSTR(pout); |
| 13038 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13039 | int pc; |
| 13040 | |
| 13041 | setprompt_if(needprompt, 2); |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13042 | pc = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13043 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13044 | case '`': |
| 13045 | goto done; |
| 13046 | |
| 13047 | case '\\': |
Denys Vlasenko | 777a635 | 2020-09-29 16:25:32 +0200 | [diff] [blame] | 13048 | pc = pgetc(); /* not pgetc_eatbnl! */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13049 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 13050 | && (!synstack->dblquote || pc != '"') |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 13051 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13052 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 13053 | } |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 13054 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13055 | |
| 13056 | case PEOF: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13057 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13058 | |
| 13059 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 13060 | nlnoprompt(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13061 | break; |
| 13062 | |
| 13063 | default: |
| 13064 | break; |
| 13065 | } |
| 13066 | STPUTC(pc, pout); |
| 13067 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 13068 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13069 | STPUTC('\0', pout); |
| 13070 | psavelen = pout - (char *)stackblock(); |
| 13071 | if (psavelen > 0) { |
| 13072 | pstr = grabstackstr(pout); |
| 13073 | setinputstring(pstr); |
| 13074 | } |
| 13075 | } |
| 13076 | nlpp = &bqlist; |
| 13077 | while (*nlpp) |
| 13078 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13079 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 13080 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13081 | |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13082 | saveheredoclist = heredoclist; |
| 13083 | heredoclist = NULL; |
| 13084 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13085 | if (oldstyle) { |
| 13086 | saveprompt = doprompt; |
| 13087 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13088 | } |
| 13089 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13090 | n = list(2); |
| 13091 | |
| 13092 | if (oldstyle) |
| 13093 | doprompt = saveprompt; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13094 | else { |
| 13095 | if (readtoken() != TRP) |
| 13096 | raise_error_unexpected_syntax(TRP); |
| 13097 | setinputstring(nullstr); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13098 | } |
| 13099 | |
Denys Vlasenko | 9a1a659 | 2020-02-22 16:39:27 +0100 | [diff] [blame] | 13100 | parseheredoc(); |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13101 | heredoclist = saveheredoclist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13102 | |
| 13103 | (*nlpp)->n = n; |
Denys Vlasenko | 74aaf05 | 2020-02-17 12:11:26 +0100 | [diff] [blame] | 13104 | /* Start reading from old file again. */ |
| 13105 | popfile(); |
| 13106 | /* Ignore any pushed back tokens left from the backquote parsing. */ |
| 13107 | if (oldstyle) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13108 | tokpushback = 0; |
Denys Vlasenko | c55847f | 2020-02-17 15:59:08 +0100 | [diff] [blame] | 13109 | out = growstackto(savelen + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13110 | if (str) { |
| 13111 | memcpy(out, str, savelen); |
| 13112 | STADJUST(savelen, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13113 | } |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 13114 | #if BASH_PROCESS_SUBST |
| 13115 | if (style == PSUB) |
| 13116 | USTPUTC(c == '<' ? CTLFROMPROC : CTLTOPROC, out); |
| 13117 | else |
| 13118 | #endif |
| 13119 | USTPUTC(CTLBACKQ, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13120 | if (oldstyle) |
| 13121 | goto parsebackq_oldreturn; |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 13122 | #if BASH_PROCESS_SUBST |
| 13123 | else if (style == PSUB) |
| 13124 | goto parsebackq_psreturn; |
| 13125 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13126 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13127 | } |
| 13128 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 13129 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13130 | /* |
| 13131 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 13132 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13133 | parsearith: { |
Denys Vlasenko | 216913c | 2018-04-02 12:35:04 +0200 | [diff] [blame] | 13134 | |
| 13135 | synstack_push(&synstack, |
| 13136 | synstack->prev ?: alloca(sizeof(*synstack)), |
| 13137 | ARISYNTAX); |
| 13138 | synstack->dblquote = 1; |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 13139 | USTPUTC(CTLARI, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13140 | goto parsearith_return; |
| 13141 | } |
| 13142 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13143 | } /* end of readtoken */ |
| 13144 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13145 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13146 | * Read the next input token. |
| 13147 | * If the token is a word, we set backquotelist to the list of cmds in |
| 13148 | * backquotes. We set quoteflag to true if any part of the word was |
| 13149 | * quoted. |
| 13150 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 13151 | * the redirection. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13152 | * |
| 13153 | * [Change comment: here documents and internal procedures] |
| 13154 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 13155 | * word parsing code into a separate routine. In this case, readtoken |
| 13156 | * doesn't need to have any internal procedures, but parseword does. |
| 13157 | * We could also make parseoperator in essence the main routine, and |
| 13158 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13159 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13160 | #define NEW_xxreadtoken |
| 13161 | #ifdef NEW_xxreadtoken |
| 13162 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13163 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13164 | '\n', '(', ')', /* singles */ |
| 13165 | '&', '|', ';', /* doubles */ |
| 13166 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13167 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13168 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13169 | #define xxreadtoken_singles 3 |
| 13170 | #define xxreadtoken_doubles 3 |
| 13171 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13172 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13173 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 13174 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 13175 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13176 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13177 | }; |
| 13178 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13179 | static int |
| 13180 | xxreadtoken(void) |
| 13181 | { |
| 13182 | int c; |
| 13183 | |
| 13184 | if (tokpushback) { |
| 13185 | tokpushback = 0; |
| 13186 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13187 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13188 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13189 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13190 | c = pgetc_eatbnl(); |
Denys Vlasenko | 48cb983 | 2021-09-08 09:52:04 +0200 | [diff] [blame] | 13191 | if (c == ' ' || c == '\t') |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13192 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13193 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13194 | if (c == '#') { |
| 13195 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 13196 | continue; |
| 13197 | pungetc(); |
| 13198 | } else if (c == '\\') { |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13199 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13200 | } else { |
| 13201 | const char *p; |
| 13202 | |
| 13203 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 13204 | if (c != PEOF) { |
| 13205 | if (c == '\n') { |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 13206 | nlnoprompt(); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13207 | } |
| 13208 | |
| 13209 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13210 | if (p == NULL) |
| 13211 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13212 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13213 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 13214 | int cc = pgetc_eatbnl(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13215 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13216 | p += xxreadtoken_doubles + 1; |
| 13217 | } else { |
| 13218 | pungetc(); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 13219 | #if BASH_REDIR_OUTPUT |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13220 | if (c == '&' && cc == '>') /* &> */ |
| 13221 | break; /* return readtoken1(...) */ |
| 13222 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13223 | } |
| 13224 | } |
| 13225 | } |
| 13226 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 13227 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13228 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13229 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 13230 | |
| 13231 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13232 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13233 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13234 | #define RETURN(token) return lasttoken = token |
| 13235 | static int |
| 13236 | xxreadtoken(void) |
| 13237 | { |
| 13238 | int c; |
| 13239 | |
| 13240 | if (tokpushback) { |
| 13241 | tokpushback = 0; |
| 13242 | return lasttoken; |
| 13243 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13244 | setprompt_if(needprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13245 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13246 | c = pgetc_eatbnl(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13247 | switch (c) { |
| 13248 | case ' ': case '\t': |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13249 | continue; |
| 13250 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 13251 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 13252 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13253 | pungetc(); |
| 13254 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13255 | case '\n': |
Denys Vlasenko | ce332a2 | 2016-10-02 23:47:34 +0200 | [diff] [blame] | 13256 | nlnoprompt(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13257 | RETURN(TNL); |
| 13258 | case PEOF: |
| 13259 | RETURN(TEOF); |
| 13260 | case '&': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13261 | if (pgetc_eatbnl() == '&') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13262 | RETURN(TAND); |
| 13263 | pungetc(); |
| 13264 | RETURN(TBACKGND); |
| 13265 | case '|': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13266 | if (pgetc_eatbnl() == '|') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13267 | RETURN(TOR); |
| 13268 | pungetc(); |
| 13269 | RETURN(TPIPE); |
| 13270 | case ';': |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13271 | if (pgetc_eatbnl() == ';') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13272 | RETURN(TENDCASE); |
| 13273 | pungetc(); |
| 13274 | RETURN(TSEMI); |
| 13275 | case '(': |
| 13276 | RETURN(TLP); |
| 13277 | case ')': |
| 13278 | RETURN(TRP); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13279 | } |
Denys Vlasenko | 220be53 | 2018-03-31 19:21:31 +0200 | [diff] [blame] | 13280 | break; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13281 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13282 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 13283 | #undef RETURN |
| 13284 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 13285 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13286 | |
| 13287 | static int |
| 13288 | readtoken(void) |
| 13289 | { |
| 13290 | int t; |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13291 | int kwd = checkkwd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13292 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 13293 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13294 | #endif |
| 13295 | |
| 13296 | #if ENABLE_ASH_ALIAS |
| 13297 | top: |
| 13298 | #endif |
| 13299 | |
| 13300 | t = xxreadtoken(); |
| 13301 | |
| 13302 | /* |
| 13303 | * eat newlines |
| 13304 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13305 | if (kwd & CHKNL) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13306 | while (t == TNL) { |
| 13307 | parseheredoc(); |
Denys Vlasenko | 8c68ae8 | 2021-09-08 01:43:12 +0200 | [diff] [blame] | 13308 | checkkwd = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13309 | t = xxreadtoken(); |
| 13310 | } |
| 13311 | } |
| 13312 | |
Denys Vlasenko | 8c68ae8 | 2021-09-08 01:43:12 +0200 | [diff] [blame] | 13313 | kwd |= checkkwd; |
| 13314 | checkkwd = 0; |
| 13315 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13316 | if (t != TWORD || quoteflag) { |
| 13317 | goto out; |
| 13318 | } |
| 13319 | |
| 13320 | /* |
| 13321 | * check for keywords |
| 13322 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 13323 | if (kwd & CHKKWD) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13324 | const char *const *pp; |
| 13325 | |
| 13326 | pp = findkwd(wordtext); |
| 13327 | if (pp) { |
| 13328 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13329 | TRACE(("keyword '%s' recognized\n", tokname_array[t])); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13330 | goto out; |
| 13331 | } |
| 13332 | } |
| 13333 | |
Denys Vlasenko | 8c68ae8 | 2021-09-08 01:43:12 +0200 | [diff] [blame] | 13334 | if (kwd & CHKALIAS) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13335 | #if ENABLE_ASH_ALIAS |
| 13336 | struct alias *ap; |
| 13337 | ap = lookupalias(wordtext, 1); |
| 13338 | if (ap != NULL) { |
| 13339 | if (*ap->val) { |
| 13340 | pushstring(ap->val, ap); |
| 13341 | } |
| 13342 | goto top; |
| 13343 | } |
| 13344 | #endif |
| 13345 | } |
| 13346 | out: |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13347 | #if DEBUG |
| 13348 | if (!alreadyseen) |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13349 | TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13350 | else |
Denys Vlasenko | 888527c | 2016-10-02 16:54:17 +0200 | [diff] [blame] | 13351 | TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13352 | #endif |
| 13353 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13354 | } |
| 13355 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13356 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13357 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 13358 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13359 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13360 | static union node * |
| 13361 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 13362 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13363 | tokpushback = 0; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 13364 | checkkwd = 0; |
| 13365 | heredoclist = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13366 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13367 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13368 | needprompt = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13369 | return list(1); |
| 13370 | } |
| 13371 | |
| 13372 | /* |
| 13373 | * Input any here documents. |
| 13374 | */ |
| 13375 | static void |
| 13376 | parseheredoc(void) |
| 13377 | { |
| 13378 | struct heredoc *here; |
| 13379 | union node *n; |
| 13380 | |
| 13381 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 13382 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13383 | |
| 13384 | while (here) { |
Christoph Schulz | 03ad7ae | 2018-11-20 17:45:52 +0100 | [diff] [blame] | 13385 | tokpushback = 0; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 13386 | setprompt_if(needprompt, 2); |
Denys Vlasenko | acf79f9 | 2020-02-14 16:12:06 +0100 | [diff] [blame] | 13387 | if (here->here->type == NHERE) |
| 13388 | readtoken1(pgetc(), SQSYNTAX, here->eofmark, here->striptabs); |
| 13389 | else |
| 13390 | readtoken1(pgetc_eatbnl(), DQSYNTAX, here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13391 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13392 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 13393 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 13394 | n->narg.text = wordtext; |
| 13395 | n->narg.backquote = backquotelist; |
| 13396 | here->here->nhere.doc = n; |
| 13397 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13398 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13399 | } |
| 13400 | |
| 13401 | |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13402 | static const char * |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13403 | expandstr(const char *ps, int syntax_type) |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13404 | { |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13405 | struct parsefile *file_stop; |
| 13406 | struct jmploc *volatile savehandler; |
| 13407 | struct heredoc *saveheredoclist; |
| 13408 | const char *result; |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13409 | int saveprompt; |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13410 | struct jmploc jmploc; |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13411 | union node n; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13412 | int err; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13413 | |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13414 | file_stop = g_parsefile; |
| 13415 | |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 13416 | /* XXX Fix (char *) cast. */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13417 | setinputstring((char *)ps); |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13418 | |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13419 | saveheredoclist = heredoclist; |
| 13420 | heredoclist = NULL; |
Denys Vlasenko | 2a6d29a | 2016-10-25 21:17:52 +0200 | [diff] [blame] | 13421 | saveprompt = doprompt; |
| 13422 | doprompt = 0; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13423 | result = ps; |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13424 | savehandler = exception_handler; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13425 | err = setjmp(jmploc.loc); |
| 13426 | if (err) |
| 13427 | goto out; |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13428 | |
| 13429 | /* readtoken1() might die horribly. |
Denys Vlasenko | 3c183a8 | 2017-10-12 19:35:42 +0200 | [diff] [blame] | 13430 | * Try a prompt with syntactically wrong command: |
Denys Vlasenko | a2e32b3 | 2017-10-12 19:20:13 +0200 | [diff] [blame] | 13431 | * PS1='$(date "+%H:%M:%S) > ' |
| 13432 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13433 | exception_handler = &jmploc; |
Denys Vlasenko | c540256 | 2021-09-08 01:03:57 +0200 | [diff] [blame] | 13434 | readtoken1(pgetc_eatbnl(), syntax_type, FAKEEOFMARK, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13435 | |
| 13436 | n.narg.type = NARG; |
| 13437 | n.narg.next = NULL; |
| 13438 | n.narg.text = wordtext; |
| 13439 | n.narg.backquote = backquotelist; |
| 13440 | |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13441 | /* expandarg() might fail too: |
| 13442 | * PS1='$((123+))' |
| 13443 | */ |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13444 | expandarg(&n, NULL, EXP_QUOTED); |
| 13445 | result = stackblock(); |
| 13446 | |
| 13447 | out: |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13448 | exception_handler = savehandler; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13449 | if (err && exception_type != EXERROR) |
| 13450 | longjmp(exception_handler->loc, 1); |
Ron Yorston | d1a2fa2 | 2019-04-18 09:49:13 +0100 | [diff] [blame] | 13451 | |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13452 | doprompt = saveprompt; |
| 13453 | /* Try: PS1='`xxx(`' */ |
| 13454 | unwindfiles(file_stop); |
Denys Vlasenko | 1c06ddd | 2021-09-08 00:56:53 +0200 | [diff] [blame] | 13455 | heredoclist = saveheredoclist; |
Ron Yorston | b0c711e | 2020-01-23 11:26:08 +0000 | [diff] [blame] | 13456 | |
| 13457 | return result; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13458 | } |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 13459 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13460 | static inline int |
| 13461 | parser_eof(void) |
| 13462 | { |
| 13463 | return tokpushback && lasttoken == TEOF; |
| 13464 | } |
| 13465 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13466 | /* |
| 13467 | * Execute a command or commands contained in a string. |
| 13468 | */ |
| 13469 | static int |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13470 | evalstring(char *s, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13471 | { |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13472 | struct jmploc *volatile savehandler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13473 | struct jmploc jmploc; |
| 13474 | int ex; |
| 13475 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13476 | union node *n; |
| 13477 | struct stackmark smark; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13478 | int status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13479 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13480 | s = sstrdup(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13481 | setinputstring(s); |
| 13482 | setstackmark(&smark); |
| 13483 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13484 | status = 0; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13485 | /* On exception inside execution loop, we must popfile(). |
| 13486 | * Try interactively: |
| 13487 | * readonly a=a |
| 13488 | * command eval "a=b" # throws "is read only" error |
| 13489 | * "command BLTIN" is not supposed to abort (even in non-interactive use). |
| 13490 | * But if we skip popfile(), we hit EOF in eval's string, and exit. |
| 13491 | */ |
| 13492 | savehandler = exception_handler; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13493 | ex = setjmp(jmploc.loc); |
| 13494 | if (ex) |
| 13495 | goto out; |
Denys Vlasenko | 493b9ca | 2016-10-30 18:27:14 +0100 | [diff] [blame] | 13496 | exception_handler = &jmploc; |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13497 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13498 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13499 | int i; |
| 13500 | |
Denys Vlasenko | 1e3e2cc | 2017-07-25 20:31:14 +0200 | [diff] [blame] | 13501 | i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT)); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13502 | if (n) |
| 13503 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13504 | popstackmark(&smark); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13505 | if (evalskip) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13506 | break; |
| 13507 | } |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13508 | out: |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13509 | popstackmark(&smark); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13510 | popfile(); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 13511 | stunalloc(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13512 | |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13513 | exception_handler = savehandler; |
| 13514 | if (ex) |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 13515 | longjmp(exception_handler->loc, ex); |
Denys Vlasenko | d81e9f5 | 2016-10-28 15:43:50 +0200 | [diff] [blame] | 13516 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13517 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13518 | } |
| 13519 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13520 | /* |
| 13521 | * The eval command. |
| 13522 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13523 | static int FAST_FUNC |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13524 | evalcmd(int argc UNUSED_PARAM, char **argv, int flags) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13525 | { |
| 13526 | char *p; |
| 13527 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13528 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13529 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13530 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13531 | argv += 2; |
| 13532 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13533 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13534 | for (;;) { |
| 13535 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13536 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13537 | if (p == NULL) |
| 13538 | break; |
| 13539 | STPUTC(' ', concat); |
| 13540 | } |
| 13541 | STPUTC('\0', concat); |
| 13542 | p = grabstackstr(concat); |
| 13543 | } |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 13544 | return evalstring(p, flags & EV_TESTED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13545 | } |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 13546 | return 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13547 | } |
| 13548 | |
| 13549 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 13550 | * Read and execute commands. |
| 13551 | * "Top" is nonzero for the top level command loop; |
| 13552 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13553 | */ |
| 13554 | static int |
| 13555 | cmdloop(int top) |
| 13556 | { |
| 13557 | union node *n; |
| 13558 | struct stackmark smark; |
| 13559 | int inter; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13560 | int status = 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13561 | int numeof = 0; |
| 13562 | |
| 13563 | TRACE(("cmdloop(%d) called\n", top)); |
| 13564 | for (;;) { |
| 13565 | int skip; |
| 13566 | |
| 13567 | setstackmark(&smark); |
| 13568 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 13569 | if (doing_jobctl) |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 13570 | showjobs(SHOW_CHANGED|SHOW_STDERR); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13571 | #endif |
| 13572 | inter = 0; |
| 13573 | if (iflag && top) { |
| 13574 | inter++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13575 | chkmail(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13576 | } |
| 13577 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 13578 | #if DEBUG |
| 13579 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 13580 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 13581 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 13582 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13583 | if (!top || numeof >= 50) |
| 13584 | break; |
| 13585 | if (!stoppedjobs()) { |
Ron Yorston | 5726df5 | 2021-09-12 11:21:48 +0100 | [diff] [blame] | 13586 | if (!iflag) |
| 13587 | break; |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13588 | if (!Iflag) { |
Ron Yorston | 5726df5 | 2021-09-12 11:21:48 +0100 | [diff] [blame] | 13589 | newline_and_flush(stderr); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13590 | break; |
Denys Vlasenko | 226b8a1 | 2020-02-16 18:57:53 +0100 | [diff] [blame] | 13591 | } |
Ron Yorston | 5726df5 | 2021-09-12 11:21:48 +0100 | [diff] [blame] | 13592 | /* "set -o ignoreeof" active, do not exit command loop on ^D */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13593 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 13594 | } |
| 13595 | numeof++; |
Denys Vlasenko | 41beb53 | 2021-09-07 01:52:21 +0200 | [diff] [blame] | 13596 | } else { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13597 | int i; |
| 13598 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 13599 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 13600 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13601 | numeof = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13602 | i = evaltree(n, 0); |
| 13603 | if (n) |
| 13604 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13605 | } |
| 13606 | popstackmark(&smark); |
| 13607 | skip = evalskip; |
| 13608 | |
| 13609 | if (skip) { |
Denys Vlasenko | cd24a50 | 2020-02-20 16:47:01 +0100 | [diff] [blame] | 13610 | evalskip &= ~(SKIPFUNC | SKIPFUNCDEF); |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 13611 | break; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13612 | } |
| 13613 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 13614 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13615 | } |
| 13616 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13617 | /* |
| 13618 | * Take commands from a file. To be compatible we should do a path |
| 13619 | * search for the file, which is necessary to find sub-commands. |
| 13620 | */ |
| 13621 | static char * |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13622 | find_dot_file(char *basename) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13623 | { |
| 13624 | char *fullname; |
| 13625 | const char *path = pathval(); |
| 13626 | struct stat statb; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13627 | int len; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13628 | |
| 13629 | /* don't try this for absolute or relative paths */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13630 | if (strchr(basename, '/')) |
| 13631 | return basename; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13632 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13633 | while ((len = padvance(&path, basename)) >= 0) { |
| 13634 | fullname = stackblock(); |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13635 | if ((!pathopt || *pathopt == 'f') |
| 13636 | && !stat(fullname, &statb) && S_ISREG(statb.st_mode) |
| 13637 | ) { |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13638 | /* This will be freed by the caller. */ |
| 13639 | return stalloc(len); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13640 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13641 | } |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13642 | /* not found in PATH */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13643 | |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13644 | #if ENABLE_ASH_BASH_SOURCE_CURDIR |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13645 | return basename; |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13646 | #else |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13647 | ash_msg_and_raise_error("%s: not found", basename); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13648 | /* NOTREACHED */ |
Denys Vlasenko | 01f7b9e | 2018-01-26 15:15:43 +0100 | [diff] [blame] | 13649 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 13650 | } |
| 13651 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13652 | static int FAST_FUNC |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13653 | dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13654 | { |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13655 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 13656 | int status = 0; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13657 | char *fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13658 | char **argv; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13659 | char *args_need_save; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13660 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13661 | |
Denys Vlasenko | 981a056 | 2017-07-26 19:53:11 +0200 | [diff] [blame] | 13662 | //??? |
| 13663 | // struct strlist *sp; |
| 13664 | // for (sp = cmdenviron; sp; sp = sp->next) |
| 13665 | // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13666 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13667 | nextopt(nullstr); /* handle possible "--" */ |
| 13668 | argv = argptr; |
| 13669 | |
| 13670 | if (!argv[0]) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13671 | /* bash says: "bash: .: filename argument required" */ |
| 13672 | return 2; /* bash compat */ |
| 13673 | } |
| 13674 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13675 | /* This aborts if file isn't found, which is POSIXly correct. |
| 13676 | * bash returns exitcode 1 instead. |
| 13677 | */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13678 | fullname = find_dot_file(argv[0]); |
| 13679 | argv++; |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13680 | args_need_save = argv[0]; |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 13681 | if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */ |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13682 | int argc; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13683 | saveparam = shellparam; |
| 13684 | shellparam.malloced = 0; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13685 | argc = 1; |
| 13686 | while (argv[argc]) |
| 13687 | argc++; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13688 | shellparam.nparam = argc; |
| 13689 | shellparam.p = argv; |
| 13690 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13691 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 13692 | /* This aborts if file can't be opened, which is POSIXly correct. |
| 13693 | * bash returns exitcode 1 instead. |
| 13694 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13695 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 13696 | commandname = fullname; |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13697 | status = cmdloop(0); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13698 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13699 | |
Denys Vlasenko | fb87d93 | 2017-01-09 08:22:06 +0100 | [diff] [blame] | 13700 | if (args_need_save) { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 13701 | freeparam(&shellparam); |
| 13702 | shellparam = saveparam; |
| 13703 | }; |
| 13704 | |
Denys Vlasenko | 0cdb7ea | 2016-10-02 03:16:00 +0200 | [diff] [blame] | 13705 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13706 | } |
| 13707 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13708 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13709 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13710 | { |
| 13711 | if (stoppedjobs()) |
| 13712 | return 0; |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13713 | |
Denys Vlasenko | 970470e | 2020-02-14 17:32:22 +0100 | [diff] [blame] | 13714 | if (argv[1]) |
| 13715 | savestatus = number(argv[1]); |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 13716 | |
Denys Vlasenko | 704c596 | 2021-09-15 19:31:44 +0200 | [diff] [blame] | 13717 | //TODO: this script |
| 13718 | // trap 'echo trap:$FUNCNAME' EXIT |
| 13719 | // f() { exit; } |
| 13720 | // f |
| 13721 | //prints "trap:f" in bash. We can call exitshell() here to achieve this. |
| 13722 | //For now, keeping dash code: |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13723 | raise_exception(EXEXIT); |
| 13724 | /* NOTREACHED */ |
| 13725 | } |
| 13726 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13727 | /* |
| 13728 | * Read a file containing shell functions. |
| 13729 | */ |
| 13730 | static void |
| 13731 | readcmdfile(char *name) |
| 13732 | { |
| 13733 | setinputfile(name, INPUT_PUSH_FILE); |
| 13734 | cmdloop(0); |
| 13735 | popfile(); |
| 13736 | } |
| 13737 | |
| 13738 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13739 | /* ============ find_command inplementation */ |
| 13740 | |
| 13741 | /* |
| 13742 | * Resolve a command name. If you change this routine, you may have to |
| 13743 | * change the shellexec routine as well. |
| 13744 | */ |
| 13745 | static void |
| 13746 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 13747 | { |
| 13748 | struct tblentry *cmdp; |
| 13749 | int idx; |
| 13750 | int prev; |
| 13751 | char *fullname; |
| 13752 | struct stat statb; |
| 13753 | int e; |
| 13754 | int updatetbl; |
| 13755 | struct builtincmd *bcmd; |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13756 | int len; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13757 | |
| 13758 | /* If name contains a slash, don't use PATH or hash table */ |
| 13759 | if (strchr(name, '/') != NULL) { |
| 13760 | entry->u.index = -1; |
| 13761 | if (act & DO_ABS) { |
| 13762 | while (stat(name, &statb) < 0) { |
| 13763 | #ifdef SYSV |
| 13764 | if (errno == EINTR) |
| 13765 | continue; |
| 13766 | #endif |
| 13767 | entry->cmdtype = CMDUNKNOWN; |
| 13768 | return; |
| 13769 | } |
| 13770 | } |
| 13771 | entry->cmdtype = CMDNORMAL; |
| 13772 | return; |
| 13773 | } |
| 13774 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13775 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13776 | |
| 13777 | updatetbl = (path == pathval()); |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13778 | if (!updatetbl) |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13779 | act |= DO_ALTPATH; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13780 | |
| 13781 | /* If name is in the table, check answer will be ok */ |
| 13782 | cmdp = cmdlookup(name, 0); |
| 13783 | if (cmdp != NULL) { |
| 13784 | int bit; |
| 13785 | |
| 13786 | switch (cmdp->cmdtype) { |
| 13787 | default: |
| 13788 | #if DEBUG |
| 13789 | abort(); |
| 13790 | #endif |
| 13791 | case CMDNORMAL: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13792 | bit = DO_ALTPATH | DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13793 | break; |
| 13794 | case CMDFUNCTION: |
| 13795 | bit = DO_NOFUNC; |
| 13796 | break; |
| 13797 | case CMDBUILTIN: |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13798 | bit = IS_BUILTIN_REGULAR(cmdp->param.cmd) ? 0 : DO_REGBLTIN; |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13799 | break; |
| 13800 | } |
| 13801 | if (act & bit) { |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13802 | if (act & bit & DO_REGBLTIN) |
| 13803 | goto fail; |
| 13804 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13805 | updatetbl = 0; |
| 13806 | cmdp = NULL; |
| 13807 | } else if (cmdp->rehash == 0) |
| 13808 | /* if not invalidated by cd, we're done */ |
| 13809 | goto success; |
| 13810 | } |
| 13811 | |
| 13812 | /* If %builtin not in path, check for builtin next */ |
| 13813 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13814 | if (bcmd) { |
| 13815 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 13816 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13817 | if (act & DO_ALTPATH) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13818 | goto builtin_success; |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13819 | if (builtinloc <= 0) |
| 13820 | goto builtin_success; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 13821 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13822 | |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13823 | if (act & DO_REGBLTIN) |
| 13824 | goto fail; |
| 13825 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13826 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 13827 | { |
| 13828 | int applet_no = find_applet_by_name(name); |
| 13829 | if (applet_no >= 0) { |
| 13830 | entry->cmdtype = CMDNORMAL; |
| 13831 | entry->u.index = -2 - applet_no; |
| 13832 | return; |
| 13833 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13834 | } |
| 13835 | #endif |
| 13836 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13837 | /* We have to search path. */ |
| 13838 | prev = -1; /* where to start */ |
| 13839 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 13840 | if (cmdp->cmdtype == CMDBUILTIN) |
| 13841 | prev = builtinloc; |
| 13842 | else |
| 13843 | prev = cmdp->param.index; |
| 13844 | } |
| 13845 | |
| 13846 | e = ENOENT; |
| 13847 | idx = -1; |
| 13848 | loop: |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13849 | while ((len = padvance(&path, name)) >= 0) { |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13850 | const char *lpathopt = pathopt; |
| 13851 | |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13852 | fullname = stackblock(); |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13853 | idx++; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13854 | if (lpathopt) { |
| 13855 | if (*lpathopt == 'b') { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13856 | if (bcmd) |
| 13857 | goto builtin_success; |
| 13858 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13859 | } else if (!(act & DO_NOFUNC)) { |
| 13860 | /* handled below */ |
| 13861 | } else { |
| 13862 | /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13863 | continue; |
| 13864 | } |
| 13865 | } |
| 13866 | /* if rehash, don't redo absolute path names */ |
| 13867 | if (fullname[0] == '/' && idx <= prev) { |
| 13868 | if (idx < prev) |
| 13869 | continue; |
| 13870 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 13871 | goto success; |
| 13872 | } |
| 13873 | while (stat(fullname, &statb) < 0) { |
| 13874 | #ifdef SYSV |
| 13875 | if (errno == EINTR) |
| 13876 | continue; |
| 13877 | #endif |
| 13878 | if (errno != ENOENT && errno != ENOTDIR) |
| 13879 | e = errno; |
| 13880 | goto loop; |
| 13881 | } |
| 13882 | e = EACCES; /* if we fail, this will be the error */ |
| 13883 | if (!S_ISREG(statb.st_mode)) |
| 13884 | continue; |
Denys Vlasenko | 6c4f87e | 2020-02-17 16:02:40 +0100 | [diff] [blame] | 13885 | if (lpathopt) { /* this is a %func directory */ |
Denys Vlasenko | b0d2dc7 | 2020-02-17 15:27:41 +0100 | [diff] [blame] | 13886 | stalloc(len); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 13887 | /* NB: stalloc will return space pointed by fullname |
| 13888 | * (because we don't have any intervening allocations |
| 13889 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13890 | readcmdfile(fullname); |
| 13891 | cmdp = cmdlookup(name, 0); |
| 13892 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 13893 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 13894 | stunalloc(fullname); |
| 13895 | goto success; |
| 13896 | } |
| 13897 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 13898 | if (!updatetbl) { |
| 13899 | entry->cmdtype = CMDNORMAL; |
| 13900 | entry->u.index = idx; |
| 13901 | return; |
| 13902 | } |
| 13903 | INT_OFF; |
| 13904 | cmdp = cmdlookup(name, 1); |
| 13905 | cmdp->cmdtype = CMDNORMAL; |
| 13906 | cmdp->param.index = idx; |
| 13907 | INT_ON; |
| 13908 | goto success; |
| 13909 | } |
| 13910 | |
| 13911 | /* We failed. If there was an entry for this command, delete it */ |
| 13912 | if (cmdp && updatetbl) |
| 13913 | delete_cmd_entry(); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13914 | if (act & DO_ERR) { |
| 13915 | #if ENABLE_ASH_BASH_NOT_FOUND_HOOK |
| 13916 | struct tblentry *hookp = cmdlookup("command_not_found_handle", 0); |
| 13917 | if (hookp && hookp->cmdtype == CMDFUNCTION) { |
| 13918 | char *argv[3]; |
| 13919 | argv[0] = (char*) "command_not_found_handle"; |
| 13920 | argv[1] = name; |
| 13921 | argv[2] = NULL; |
| 13922 | evalfun(hookp->param.func, 2, argv, 0); |
| 13923 | entry->cmdtype = CMDUNKNOWN; |
| 13924 | return; |
| 13925 | } |
| 13926 | #endif |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13927 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
William Pitcock | d8fd88a | 2018-01-24 18:33:18 +0100 | [diff] [blame] | 13928 | } |
Denys Vlasenko | 7eb8eec | 2020-02-19 15:15:13 +0100 | [diff] [blame] | 13929 | fail: |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 13930 | entry->cmdtype = CMDUNKNOWN; |
| 13931 | return; |
| 13932 | |
| 13933 | builtin_success: |
| 13934 | if (!updatetbl) { |
| 13935 | entry->cmdtype = CMDBUILTIN; |
| 13936 | entry->u.cmd = bcmd; |
| 13937 | return; |
| 13938 | } |
| 13939 | INT_OFF; |
| 13940 | cmdp = cmdlookup(name, 1); |
| 13941 | cmdp->cmdtype = CMDBUILTIN; |
| 13942 | cmdp->param.cmd = bcmd; |
| 13943 | INT_ON; |
| 13944 | success: |
| 13945 | cmdp->rehash = 0; |
| 13946 | entry->cmdtype = cmdp->cmdtype; |
| 13947 | entry->u = cmdp->param; |
| 13948 | } |
| 13949 | |
| 13950 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13951 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13952 | * The trap builtin. |
| 13953 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13954 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13955 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13956 | { |
| 13957 | char *action; |
| 13958 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13959 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13960 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13961 | nextopt(nullstr); |
| 13962 | ap = argptr; |
| 13963 | if (!*ap) { |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 13964 | for (signo = 0; signo <= NTRAP_LAST; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13965 | char *tr = trap_ptr[signo]; |
| 13966 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13967 | /* note: bash adds "SIG", but only if invoked |
| 13968 | * as "bash". If called as "sh", or if set -o posix, |
| 13969 | * then it prints short signal names. |
| 13970 | * We are printing short names: */ |
| 13971 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13972 | single_quote(tr), |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 13973 | (signo == NTRAP_ERR) ? "ERR" : get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13974 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 13975 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 13976 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13977 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13978 | } |
| 13979 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13980 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13981 | if (trap_ptr != trap) { |
| 13982 | free(trap_ptr); |
| 13983 | trap_ptr = trap; |
| 13984 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 13985 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13986 | return 0; |
| 13987 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 13988 | |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13989 | /* Why the second check? |
| 13990 | * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..." |
| 13991 | * In this case, NUM is signal no, not an action. |
| 13992 | */ |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 13993 | action = NULL; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13994 | if (ap[1] && !is_number(ap[0])) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13995 | action = *ap++; |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 13996 | |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 13997 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 13998 | while (*ap) { |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 13999 | signo = strcmp(*ap, "ERR") == 0 ? NTRAP_ERR : get_signum(*ap); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 14000 | if (signo < 0) { |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 14001 | /* Mimic bash message exactly */ |
| 14002 | ash_msg("%s: invalid signal specification", *ap); |
| 14003 | exitcode = 1; |
| 14004 | goto next; |
| 14005 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14006 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14007 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 14008 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14009 | action = NULL; |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 14010 | else { |
| 14011 | if (action[0]) /* not NULL and not "" and not "-" */ |
| 14012 | may_have_traps = 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 14013 | action = ckstrdup(action); |
Denys Vlasenko | b4f51d3 | 2016-10-27 12:55:09 +0200 | [diff] [blame] | 14014 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14015 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 14016 | free(trap[signo]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14017 | trap[signo] = action; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 14018 | if (signo != 0 && signo < NSIG) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14019 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14020 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 14021 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14022 | ap++; |
| 14023 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 14024 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14025 | } |
| 14026 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14027 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14028 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14029 | |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 14030 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14031 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14032 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14033 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 14034 | unsigned col; |
| 14035 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14036 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 14037 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 14038 | "Built-in commands:\n" |
| 14039 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 14040 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14041 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 14042 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14043 | if (col > 60) { |
| 14044 | out1fmt("\n"); |
| 14045 | col = 0; |
| 14046 | } |
| 14047 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 14048 | # if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 14049 | { |
| 14050 | const char *a = applet_names; |
| 14051 | while (*a) { |
| 14052 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 14053 | if (col > 60) { |
| 14054 | out1fmt("\n"); |
| 14055 | col = 0; |
| 14056 | } |
Ron Yorston | 2b91958 | 2016-04-08 11:57:20 +0100 | [diff] [blame] | 14057 | while (*a++ != '\0') |
| 14058 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14059 | } |
| 14060 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 14061 | # endif |
Denys Vlasenko | ebedb94 | 2016-10-02 18:45:09 +0200 | [diff] [blame] | 14062 | newline_and_flush(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14063 | return EXIT_SUCCESS; |
| 14064 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 14065 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14066 | |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 14067 | #if MAX_HISTORY |
| 14068 | static int FAST_FUNC |
| 14069 | historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 14070 | { |
Ron Yorston | 9f3b410 | 2019-12-16 09:31:10 +0000 | [diff] [blame] | 14071 | show_history(line_input_state); |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 14072 | return EXIT_SUCCESS; |
| 14073 | } |
| 14074 | #endif |
| 14075 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14076 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14077 | * The export and readonly commands. |
| 14078 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14079 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14080 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14081 | { |
| 14082 | struct var *vp; |
| 14083 | char *name; |
| 14084 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14085 | char **aptr; |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 14086 | char opt; |
| 14087 | int flag; |
| 14088 | int flag_off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14089 | |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 14090 | /* "readonly" in bash accepts, but ignores -n. |
| 14091 | * We do the same: it saves a conditional in nextopt's param. |
| 14092 | */ |
| 14093 | flag_off = 0; |
| 14094 | while ((opt = nextopt("np")) != '\0') { |
| 14095 | if (opt == 'n') |
| 14096 | flag_off = VEXPORT; |
| 14097 | } |
| 14098 | flag = VEXPORT; |
| 14099 | if (argv[0][0] == 'r') { |
| 14100 | flag = VREADONLY; |
| 14101 | flag_off = 0; /* readonly ignores -n */ |
| 14102 | } |
| 14103 | flag_off = ~flag_off; |
| 14104 | |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 14105 | /*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] | 14106 | { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 14107 | aptr = argptr; |
| 14108 | name = *aptr; |
| 14109 | if (name) { |
| 14110 | do { |
| 14111 | p = strchr(name, '='); |
| 14112 | if (p != NULL) { |
| 14113 | p++; |
| 14114 | } else { |
| 14115 | vp = *findvar(hashvar(name), name); |
| 14116 | if (vp) { |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 14117 | vp->flags = ((vp->flags | flag) & flag_off); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 14118 | continue; |
| 14119 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14120 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 14121 | setvar(name, p, (flag & flag_off)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 14122 | } while ((name = *++aptr) != NULL); |
| 14123 | return 0; |
| 14124 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14125 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 14126 | |
| 14127 | /* No arguments. Show the list of exported or readonly vars. |
| 14128 | * -n is ignored. |
| 14129 | */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 14130 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14131 | return 0; |
| 14132 | } |
| 14133 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14134 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 14135 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14136 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 14137 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 14138 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 14139 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 14140 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14141 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 14142 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 14143 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 14144 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14145 | } |
| 14146 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14147 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14148 | * The unset builtin command. We unset the function before we unset the |
| 14149 | * variable to allow a function to be unset when there is a readonly variable |
| 14150 | * with the same name. |
| 14151 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14152 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14153 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14154 | { |
| 14155 | char **ap; |
| 14156 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14157 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14158 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 14159 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14160 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14161 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14162 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 14163 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14164 | if (flag != 'f') { |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 14165 | unsetvar(*ap); |
| 14166 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14167 | } |
| 14168 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14169 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14170 | } |
Denys Vlasenko | b28d4c3 | 2017-07-25 16:29:36 +0200 | [diff] [blame] | 14171 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14172 | } |
| 14173 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 14174 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14175 | ' ', offsetof(struct tms, tms_utime), |
| 14176 | '\n', offsetof(struct tms, tms_stime), |
| 14177 | ' ', offsetof(struct tms, tms_cutime), |
| 14178 | '\n', offsetof(struct tms, tms_cstime), |
| 14179 | 0 |
| 14180 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14181 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14182 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14183 | { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 14184 | unsigned clk_tck; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14185 | const unsigned char *p; |
| 14186 | struct tms buf; |
| 14187 | |
Bartosz Golaszewski | 5d2e409 | 2014-06-22 14:01:13 +0200 | [diff] [blame] | 14188 | clk_tck = bb_clk_tck(); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14189 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 14190 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14191 | p = timescmd_str; |
| 14192 | do { |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 14193 | unsigned sec, frac; |
| 14194 | unsigned long t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14195 | t = *(clock_t *)(((char *) &buf) + p[1]); |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 14196 | sec = t / clk_tck; |
| 14197 | frac = t % clk_tck; |
| 14198 | out1fmt("%um%u.%03us%c", |
| 14199 | sec / 60, sec % 60, |
| 14200 | (frac * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14201 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 14202 | p += 2; |
| 14203 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 14204 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 14205 | return 0; |
| 14206 | } |
| 14207 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 14208 | #if ENABLE_FEATURE_SH_MATH |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14209 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 14210 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 14211 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 14212 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 14213 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14214 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14215 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14216 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14217 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14218 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14219 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14220 | argv++; |
| 14221 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14222 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14223 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 14224 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14225 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14226 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 14227 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14228 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14229 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 14230 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14231 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14232 | * The read builtin. Options: |
| 14233 | * -r Do not interpret '\' specially |
| 14234 | * -s Turn off echo (tty only) |
| 14235 | * -n NCHARS Read NCHARS max |
| 14236 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 14237 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 14238 | * -u FD Read from given FD instead of fd 0 |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14239 | * -d DELIM End on DELIM char, not newline |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14240 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14241 | * TODO: bash also has: |
| 14242 | * -a ARRAY Read into array[0],[1],etc |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14243 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14244 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14245 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14246 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14247 | { |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14248 | struct builtin_read_params params; |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14249 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14250 | int i; |
| 14251 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14252 | memset(¶ms, 0, sizeof(params)); |
| 14253 | |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14254 | while ((i = nextopt("p:u:rt:n:sd:")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 14255 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14256 | case 'p': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14257 | params.opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14258 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14259 | case 'n': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14260 | params.opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14261 | break; |
| 14262 | case 's': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14263 | params.read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14264 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14265 | case 't': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14266 | params.opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14267 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14268 | case 'r': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14269 | params.read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14270 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14271 | case 'u': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14272 | params.opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 14273 | break; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14274 | #if BASH_READ_D |
| 14275 | case 'd': |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14276 | params.opt_d = optionarg; |
Johannes Schindelin | 3bef5d8 | 2017-08-08 16:46:39 +0200 | [diff] [blame] | 14277 | break; |
| 14278 | #endif |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14279 | default: |
| 14280 | break; |
| 14281 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14282 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 14283 | |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 14284 | if (!ENABLE_ASH_BASH_COMPAT && !argptr) { |
| 14285 | bb_simple_error_msg("read: need variable name"); |
| 14286 | return 1; |
| 14287 | } |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14288 | params.argv = argptr; |
| 14289 | params.setvar = setvar0; |
| 14290 | params.ifs = bltinlookup("IFS"); /* can be NULL */ |
| 14291 | |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14292 | /* "read -s" needs to save/restore termios, can't allow ^C |
| 14293 | * to jump out of it. |
| 14294 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14295 | again: |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14296 | INT_OFF; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 14297 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 14298 | INT_ON; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 14299 | |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14300 | if ((uintptr_t)r == 1 && errno == EINTR) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14301 | /* To get SIGCHLD: sleep 1 & read x; echo $x |
| 14302 | * Correct behavior is to not exit "read" |
| 14303 | */ |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 14304 | if (pending_sig == 0) |
| 14305 | goto again; |
| 14306 | } |
| 14307 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14308 | if ((uintptr_t)r > 1) |
| 14309 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 14310 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 14311 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14312 | } |
| 14313 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14314 | static int FAST_FUNC |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14315 | umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14316 | { |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14317 | static const char permuser[3] ALIGN1 = "ogu"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14318 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14319 | mode_t mask; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14320 | int symbolic_mode = 0; |
| 14321 | |
| 14322 | while (nextopt("S") != '\0') { |
| 14323 | symbolic_mode = 1; |
| 14324 | } |
| 14325 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14326 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14327 | mask = umask(0); |
| 14328 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 14329 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14330 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14331 | if (*argptr == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14332 | if (symbolic_mode) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14333 | char buf[sizeof(",u=rwx,g=rwx,o=rwx")]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14334 | char *p = buf; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14335 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14336 | |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14337 | i = 2; |
| 14338 | for (;;) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14339 | *p++ = ','; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14340 | *p++ = permuser[i]; |
| 14341 | *p++ = '='; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14342 | /* mask is 0..0uuugggooo. i=2 selects uuu bits */ |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14343 | if (!(mask & 0400)) *p++ = 'r'; |
| 14344 | if (!(mask & 0200)) *p++ = 'w'; |
| 14345 | if (!(mask & 0100)) *p++ = 'x'; |
| 14346 | mask <<= 3; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14347 | if (--i < 0) |
| 14348 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14349 | } |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 14350 | *p = '\0'; |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 14351 | puts(buf + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14352 | } else { |
Denys Vlasenko | ec046f7 | 2015-10-07 17:57:53 +0200 | [diff] [blame] | 14353 | out1fmt("%04o\n", mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14354 | } |
| 14355 | } else { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14356 | char *modestr = *argptr; |
Denys Vlasenko | 14c85eb | 2017-10-12 19:40:47 +0200 | [diff] [blame] | 14357 | /* numeric umasks are taken as-is */ |
| 14358 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14359 | if (!isdigit(modestr[0])) |
| 14360 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 14361 | mask = bb_parse_mode(modestr, mask); |
| 14362 | if ((unsigned)mask > 0777) { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14363 | ash_msg_and_raise_error("illegal mode: %s", modestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14364 | } |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 14365 | if (!isdigit(modestr[0])) |
| 14366 | mask ^= 0777; |
| 14367 | umask(mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14368 | } |
| 14369 | return 0; |
| 14370 | } |
| 14371 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 14372 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14373 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14374 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 14375 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 14376 | } |
| 14377 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14378 | /* ============ main() and helpers */ |
| 14379 | |
| 14380 | /* |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14381 | * This routine is called when an error or an interrupt occurs in an |
| 14382 | * interactive shell and control is returned to the main command loop |
| 14383 | * but prior to exitshell. |
| 14384 | */ |
| 14385 | static void |
| 14386 | exitreset(void) |
| 14387 | { |
| 14388 | /* from eval.c: */ |
| 14389 | if (savestatus >= 0) { |
| 14390 | if (exception_type == EXEXIT || evalskip == SKIPFUNCDEF) |
| 14391 | exitstatus = savestatus; |
| 14392 | savestatus = -1; |
| 14393 | } |
| 14394 | evalskip = 0; |
| 14395 | loopnest = 0; |
Denys Vlasenko | eb60777 | 2021-09-09 16:26:41 +0200 | [diff] [blame] | 14396 | inps4 = 0; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14397 | |
| 14398 | /* from expand.c: */ |
| 14399 | ifsfree(); |
| 14400 | |
| 14401 | /* from redir.c: */ |
| 14402 | unwindredir(NULL); |
| 14403 | } |
| 14404 | |
| 14405 | /* |
| 14406 | * This routine is called when an error or an interrupt occurs in an |
| 14407 | * interactive shell and control is returned to the main command loop. |
| 14408 | * (In dash, this function is auto-generated by build machinery). |
| 14409 | */ |
| 14410 | static void |
| 14411 | reset(void) |
| 14412 | { |
| 14413 | /* from input.c: */ |
| 14414 | g_parsefile->left_in_buffer = 0; |
| 14415 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denys Vlasenko | 51a471d | 2020-12-24 00:22:24 +0100 | [diff] [blame] | 14416 | g_parsefile->unget = 0; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14417 | popallfiles(); |
| 14418 | |
| 14419 | /* from var.c: */ |
| 14420 | unwindlocalvars(NULL); |
| 14421 | } |
| 14422 | |
| 14423 | /* |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14424 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14425 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14426 | static void |
| 14427 | exitshell(void) |
| 14428 | { |
| 14429 | struct jmploc loc; |
| 14430 | char *p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14431 | |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14432 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 14433 | save_history(line_input_state); /* may be NULL */ |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 14434 | #endif |
Denys Vlasenko | 4ccddc8 | 2020-02-14 17:27:18 +0100 | [diff] [blame] | 14435 | savestatus = exitstatus; |
| 14436 | TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus)); |
| 14437 | if (setjmp(loc.loc)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14438 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14439 | exception_handler = &loc; |
| 14440 | p = trap[0]; |
| 14441 | if (p) { |
| 14442 | trap[0] = NULL; |
Denys Vlasenko | 7b3fa1e | 2016-10-01 15:10:16 +0200 | [diff] [blame] | 14443 | evalskip = 0; |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 14444 | trap_depth++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14445 | evalstring(p, 0); |
Roberto A. Foglietta | e0bf3df | 2021-09-07 01:19:31 +0200 | [diff] [blame] | 14446 | trap_depth--; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14447 | evalskip = SKIPFUNCDEF; |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14448 | /*free(p); - we'll exit soon */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14449 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14450 | out: |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14451 | exitreset(); |
Denys Vlasenko | f37e115 | 2016-10-07 03:17:28 +0200 | [diff] [blame] | 14452 | /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}". |
| 14453 | * our setjobctl(0) does not panic if tcsetpgrp fails inside it. |
| 14454 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14455 | setjobctl(0); |
Denys Vlasenko | caee80c | 2016-10-25 20:49:53 +0200 | [diff] [blame] | 14456 | flush_stdout_stderr(); |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14457 | _exit(exitstatus); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14458 | /* NOTREACHED */ |
| 14459 | } |
| 14460 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14461 | /* Don't inline: conserve stack of caller from having our locals too */ |
| 14462 | static NOINLINE void |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 14463 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14464 | { |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 14465 | /* we will never free this */ |
| 14466 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denys Vlasenko | 0485b67 | 2017-08-14 19:46:56 +0200 | [diff] [blame] | 14467 | basepf.linno = 1; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14468 | |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14469 | 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] | 14470 | setsignal(SIGCHLD); |
| 14471 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14472 | { |
| 14473 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14474 | const char *p; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14475 | |
| 14476 | initvar(); |
| 14477 | for (envp = environ; envp && *envp; envp++) { |
Denys Vlasenko | 9c143ce | 2017-11-02 12:56:24 +0100 | [diff] [blame] | 14478 | /* Used to have |
| 14479 | * p = endofname(*envp); |
| 14480 | * if (p != *envp && *p == '=') { |
| 14481 | * here to weed out badly-named variables, but this breaks |
| 14482 | * scenarios where people do want them passed to children: |
| 14483 | * import os |
| 14484 | * os.environ["test-test"]="test" |
| 14485 | * if os.fork() == 0: |
| 14486 | * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this |
| 14487 | * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) # breaks this |
| 14488 | */ |
| 14489 | if (strchr(*envp, '=')) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14490 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 14491 | } |
| 14492 | } |
| 14493 | |
Denys Vlasenko | 67dae15 | 2018-08-05 13:59:35 +0200 | [diff] [blame] | 14494 | setvareq((char*)defifsvar, VTEXTFIXED); |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 14495 | setvareq((char*)defoptindvar, VTEXTFIXED); |
| 14496 | |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14497 | setvar0("PPID", utoa(getppid())); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14498 | #if BASH_SHLVL_VAR |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14499 | p = lookupvar("SHLVL"); |
Denys Vlasenko | 5680e98 | 2014-01-07 16:12:48 +0100 | [diff] [blame] | 14500 | setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT); |
Denys Vlasenko | 7d4aec0 | 2017-01-11 14:00:38 +0100 | [diff] [blame] | 14501 | #endif |
| 14502 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14503 | if (!lookupvar("HOSTNAME")) { |
| 14504 | struct utsname uts; |
| 14505 | uname(&uts); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14506 | setvar0("HOSTNAME", uts.nodename); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 14507 | } |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 14508 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14509 | p = lookupvar("PWD"); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14510 | if (p) { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 14511 | struct stat st1, st2; |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14512 | if (p[0] != '/' || stat(p, &st1) || stat(".", &st2) |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14513 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino |
| 14514 | ) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 14515 | p = NULL; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14516 | } |
| 14517 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14518 | setpwd(p, 0); |
| 14519 | } |
| 14520 | } |
| 14521 | |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14522 | |
| 14523 | //usage:#define ash_trivial_usage |
Denys Vlasenko | aaf3d5b | 2021-10-13 11:15:52 +0200 | [diff] [blame] | 14524 | //usage: "[-il] [-|+Cabefmnuvx] [-|+o OPT]... [-c 'SCRIPT' [ARG0 ARGS] | FILE ARGS | -s ARGS]" |
Denys Vlasenko | 4e039ba | 2021-01-04 03:50:38 +0100 | [diff] [blame] | 14525 | //////// comes from ^^^^^^^^^^optletters |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 14526 | //usage:#define ash_full_usage "\n\n" |
| 14527 | //usage: "Unix shell interpreter" |
| 14528 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14529 | /* |
| 14530 | * Process the shell command line arguments. |
| 14531 | */ |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14532 | static int |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14533 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14534 | { |
| 14535 | int i; |
| 14536 | const char *xminusc; |
| 14537 | char **xargv; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14538 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14539 | |
| 14540 | xargv = argv; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14541 | login_sh = xargv[0] && xargv[0][0] == '-'; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14542 | #if NUM_SCRIPTS > 0 |
| 14543 | if (minusc) |
| 14544 | goto setarg0; |
| 14545 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14546 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14547 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14548 | xargv++; |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14549 | argptr = xargv; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14550 | for (i = 0; i < NOPTS; i++) |
| 14551 | optlist[i] = 2; |
Denys Vlasenko | 897475a | 2019-06-01 16:35:09 +0200 | [diff] [blame] | 14552 | if (options(&login_sh)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 14553 | /* it already printed err message */ |
| 14554 | raise_exception(EXERROR); |
| 14555 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14556 | xargv = argptr; |
| 14557 | xminusc = minusc; |
| 14558 | if (*xargv == NULL) { |
| 14559 | if (xminusc) |
| 14560 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 14561 | sflag = 1; |
| 14562 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14563 | if (iflag == 2 /* no explicit -i given */ |
| 14564 | && sflag == 1 /* -s given (or implied) */ |
| 14565 | && !minusc /* bash compat: ash -sc 'echo $-' is not interactive (dash is) */ |
| 14566 | && isatty(0) && isatty(1) /* we are on tty */ |
| 14567 | ) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14568 | iflag = 1; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14569 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14570 | if (mflag == 2) |
| 14571 | mflag = iflag; |
Denys Vlasenko | 85158b6 | 2021-01-03 12:14:58 +0100 | [diff] [blame] | 14572 | /* Unset options which weren't explicitly set or unset */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14573 | for (i = 0; i < NOPTS; i++) |
Denys Vlasenko | 85158b6 | 2021-01-03 12:14:58 +0100 | [diff] [blame] | 14574 | 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] | 14575 | #if DEBUG == 2 |
| 14576 | debug = 1; |
| 14577 | #endif |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14578 | /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14579 | if (xminusc) { |
| 14580 | minusc = *xargv++; |
| 14581 | if (*xargv) |
| 14582 | goto setarg0; |
| 14583 | } else if (!sflag) { |
| 14584 | setinputfile(*xargv, 0); |
| 14585 | setarg0: |
| 14586 | arg0 = *xargv++; |
| 14587 | commandname = arg0; |
| 14588 | } |
| 14589 | |
| 14590 | shellparam.p = xargv; |
| 14591 | #if ENABLE_ASH_GETOPTS |
| 14592 | shellparam.optind = 1; |
| 14593 | shellparam.optoff = -1; |
| 14594 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14595 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14596 | while (*xargv) { |
| 14597 | shellparam.nparam++; |
| 14598 | xargv++; |
| 14599 | } |
Denys Vlasenko | 31df5a3 | 2020-12-13 16:36:28 +0100 | [diff] [blame] | 14600 | |
| 14601 | /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 14602 | * Try: |
| 14603 | * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect |
| 14604 | * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed |
| 14605 | * NB: must do it before setting up signals (in optschanged()) |
| 14606 | * and reading .profile etc (after we return from here): |
| 14607 | */ |
| 14608 | if (iflag) |
| 14609 | signal(SIGHUP, SIG_DFL); |
| 14610 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14611 | optschanged(); |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14612 | |
| 14613 | return login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14614 | } |
| 14615 | |
| 14616 | /* |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14617 | * Read /etc/profile, ~/.profile, $ENV. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14618 | */ |
| 14619 | static void |
| 14620 | read_profile(const char *name) |
| 14621 | { |
Denys Vlasenko | 4699980 | 2017-07-29 21:12:29 +0200 | [diff] [blame] | 14622 | name = expandstr(name, DQSYNTAX); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14623 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 14624 | return; |
Denys Vlasenko | 0840c91 | 2016-10-01 15:27:44 +0200 | [diff] [blame] | 14625 | cmdloop(0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14626 | popfile(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14627 | } |
| 14628 | |
| 14629 | #if PROFILE |
| 14630 | static short profile_buf[16384]; |
| 14631 | extern int etext(); |
| 14632 | #endif |
| 14633 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 14634 | /* |
| 14635 | * Main routine. We initialize things, parse the arguments, execute |
| 14636 | * profiles if we're a login shell, and then call cmdloop to execute |
| 14637 | * commands. The setjmp call sets up the location to jump to when an |
| 14638 | * exception occurs. When an exception occurs the variable "state" |
| 14639 | * is used to figure out how far we had gotten. |
| 14640 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14641 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14642 | #if NUM_SCRIPTS > 0 |
| 14643 | int ash_main(int argc, char **argv) |
| 14644 | #else |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14645 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Ron Yorston | 8767c12 | 2018-11-05 13:13:08 +0000 | [diff] [blame] | 14646 | #endif |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14647 | /* note: 'argc' is used only if embedded scripts are enabled */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14648 | { |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14649 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14650 | struct jmploc jmploc; |
| 14651 | struct stackmark smark; |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14652 | int login_sh; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14653 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14654 | /* Initialize global data */ |
| 14655 | INIT_G_misc(); |
| 14656 | INIT_G_memstack(); |
| 14657 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14658 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14659 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 14660 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 14661 | INIT_G_cmdtable(); |
| 14662 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14663 | #if PROFILE |
| 14664 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 14665 | #endif |
| 14666 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14667 | state = 0; |
| 14668 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14669 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 14670 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14671 | |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14672 | exitreset(); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14673 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14674 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14675 | s = state; |
Denys Vlasenko | f977e00 | 2020-02-20 16:54:29 +0100 | [diff] [blame] | 14676 | if (e == EXEND || e == EXEXIT || s == 0 || iflag == 0 || shlvl) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14677 | exitshell(); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14678 | } |
Denys Vlasenko | afc91fa | 2020-02-17 11:22:59 +0100 | [diff] [blame] | 14679 | |
| 14680 | reset(); |
| 14681 | |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14682 | if (e == EXINT) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 14683 | newline_and_flush(stderr); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14684 | } |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 14685 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14686 | popstackmark(&smark); |
| 14687 | FORCE_INT_ON; /* enable interrupts */ |
| 14688 | if (s == 1) |
| 14689 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14690 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14691 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14692 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14693 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 14694 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14695 | } |
| 14696 | exception_handler = &jmploc; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14697 | rootpid = getpid(); |
| 14698 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14699 | init(); |
| 14700 | setstackmark(&smark); |
Denys Vlasenko | 4f2ef4a | 2018-11-01 09:53:25 +0100 | [diff] [blame] | 14701 | |
| 14702 | #if NUM_SCRIPTS > 0 |
| 14703 | if (argc < 0) |
| 14704 | /* Non-NULL minusc tells procargs that an embedded script is being run */ |
| 14705 | minusc = get_script_content(-argc - 1); |
| 14706 | #endif |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14707 | login_sh = procargs(argv); |
Denys Vlasenko | 474ed06 | 2016-10-30 18:30:29 +0100 | [diff] [blame] | 14708 | #if DEBUG |
| 14709 | TRACE(("Shell args: ")); |
| 14710 | trace_puts_args(argv); |
| 14711 | #endif |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 14712 | |
Denys Vlasenko | ec05df1 | 2017-07-31 19:43:47 +0200 | [diff] [blame] | 14713 | if (login_sh) { |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14714 | const char *hp; |
| 14715 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14716 | state = 1; |
| 14717 | read_profile("/etc/profile"); |
| 14718 | state1: |
| 14719 | state = 2; |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14720 | hp = lookupvar("HOME"); |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14721 | if (hp) |
| 14722 | read_profile("$HOME/.profile"); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14723 | } |
| 14724 | state2: |
| 14725 | state = 3; |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 14726 | if (iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14727 | #ifndef linux |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 14728 | && getuid() == geteuid() && getgid() == getegid() |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14729 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14730 | ) { |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14731 | const char *shinit = lookupvar("ENV"); |
| 14732 | if (shinit != NULL && *shinit != '\0') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14733 | read_profile(shinit); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14734 | } |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 14735 | popstackmark(&smark); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14736 | state3: |
| 14737 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14738 | if (minusc) { |
| 14739 | /* evalstring pushes parsefile stack. |
| 14740 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 14741 | * is one of stacked source fds. |
| 14742 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14743 | |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 14744 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 14745 | // ^^ not necessary since now we special-case fd 0 |
Denys Vlasenko | 035486c | 2017-07-31 04:09:19 +0200 | [diff] [blame] | 14746 | // in save_fd_on_redirect() |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14747 | |
Denys Vlasenko | 64aa86b | 2021-09-07 18:16:45 +0200 | [diff] [blame] | 14748 | lineno = 0; // bash compat |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 14749 | // dash: evalstring(minusc, sflag ? 0 : EV_EXIT); |
| 14750 | // The above makes |
| 14751 | // ash -sc 'echo $-' |
| 14752 | // continue reading input from stdin after running 'echo'. |
| 14753 | // bash does not do this: it prints "hBcs" and exits. |
| 14754 | evalstring(minusc, EV_EXIT); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14755 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14756 | |
| 14757 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 14758 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 3f8ec00 | 2021-01-03 10:55:39 +0100 | [diff] [blame] | 14759 | if (line_input_state) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14760 | const char *hp = lookupvar("HISTFILE"); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14761 | if (!hp) { |
| 14762 | hp = lookupvar("HOME"); |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 14763 | if (hp) { |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14764 | INT_OFF; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14765 | hp = concat_path_file(hp, ".ash_history"); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 14766 | setvar0("HISTFILE", hp); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14767 | free((char*)hp); |
Denys Vlasenko | 5f7c82b | 2017-02-03 13:00:06 +0100 | [diff] [blame] | 14768 | INT_ON; |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 14769 | hp = lookupvar("HISTFILE"); |
| 14770 | } |
| 14771 | } |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 14772 | if (hp) |
Denys Vlasenko | 3f8ec00 | 2021-01-03 10:55:39 +0100 | [diff] [blame] | 14773 | line_input_state->hist_file = xstrdup(hp); |
Denys Vlasenko | 2c4de5b | 2011-03-31 13:16:52 +0200 | [diff] [blame] | 14774 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 14775 | hp = lookupvar("HISTFILESIZE"); |
| 14776 | line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 14777 | # endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14778 | } |
| 14779 | #endif |
| 14780 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 14781 | cmdloop(1); |
| 14782 | } |
| 14783 | #if PROFILE |
| 14784 | monitor(0); |
| 14785 | #endif |
| 14786 | #ifdef GPROF |
| 14787 | { |
| 14788 | extern void _mcleanup(void); |
| 14789 | _mcleanup(); |
| 14790 | } |
| 14791 | #endif |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 14792 | TRACE(("End of main reached\n")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14793 | exitshell(); |
| 14794 | /* NOTREACHED */ |
| 14795 | } |
| 14796 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 14797 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14798 | /*- |
| 14799 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 14800 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14801 | * |
| 14802 | * This code is derived from software contributed to Berkeley by |
| 14803 | * Kenneth Almquist. |
| 14804 | * |
| 14805 | * Redistribution and use in source and binary forms, with or without |
| 14806 | * modification, are permitted provided that the following conditions |
| 14807 | * are met: |
| 14808 | * 1. Redistributions of source code must retain the above copyright |
| 14809 | * notice, this list of conditions and the following disclaimer. |
| 14810 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14811 | * notice, this list of conditions and the following disclaimer in the |
| 14812 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 14813 | * 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] | 14814 | * may be used to endorse or promote products derived from this software |
| 14815 | * without specific prior written permission. |
| 14816 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 14817 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 14818 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 14819 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 14820 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 14821 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 14822 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 14823 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 14824 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 14825 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 14826 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 14827 | * SUCH DAMAGE. |
| 14828 | */ |