Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* common.c - utility functions for all builtins */ |
| 2 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1987-2010 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | #include <config.h> |
| 22 | |
| 23 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 24 | # ifdef _MINIX |
| 25 | # include <sys/types.h> |
| 26 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 27 | # include <unistd.h> |
| 28 | #endif |
| 29 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 31 | #include <chartypes.h> |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 32 | #include "../bashtypes.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 33 | #include "posixstat.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 34 | #include <signal.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 35 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 38 | #if defined (PREFER_STDARG) |
| 39 | # include <stdarg.h> |
| 40 | #else |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 41 | # include <varargs.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 45 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 46 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 47 | #define NEED_FPURGE_DECL |
| 48 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | #include "../shell.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 50 | #include "maxpath.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | #include "../flags.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | #include "../jobs.h" |
| 53 | #include "../builtins.h" |
| 54 | #include "../input.h" |
| 55 | #include "../execute_cmd.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 56 | #include "../trap.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | #include "common.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 59 | #include "builtext.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 60 | #include <tilde/tilde.h> |
| 61 | |
| 62 | #if defined (HISTORY) |
| 63 | # include "../bashhist.h" |
| 64 | #endif |
| 65 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 66 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 67 | #include <errno.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 68 | #endif /* !errno */ |
| 69 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 70 | extern int indirection_level, subshell_environment; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | extern int line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | extern int last_command_exit_value; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 73 | extern int running_trap; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | extern int posixly_correct; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 75 | extern char *this_command_name, *shell_name; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 76 | extern const char * const bash_getcwd_errstr; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 77 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 78 | /* Used by some builtins and the mainline code. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 79 | sh_builtin_func_t *last_shell_builtin = (sh_builtin_func_t *)NULL; |
| 80 | sh_builtin_func_t *this_shell_builtin = (sh_builtin_func_t *)NULL; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 81 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | /* **************************************************************** */ |
| 83 | /* */ |
| 84 | /* Error reporting, usage, and option processing */ |
| 85 | /* */ |
| 86 | /* **************************************************************** */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 87 | |
| 88 | /* This is a lot like report_error (), but it is for shell builtins |
| 89 | instead of shell control structures, and it won't ever exit the |
| 90 | shell. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 91 | |
| 92 | static void |
| 93 | builtin_error_prolog () |
| 94 | { |
| 95 | char *name; |
| 96 | |
| 97 | name = get_name_for_error (); |
| 98 | fprintf (stderr, "%s: ", name); |
| 99 | |
| 100 | if (interactive_shell == 0) |
| 101 | fprintf (stderr, _("line %d: "), executing_line_number ()); |
| 102 | |
| 103 | if (this_command_name && *this_command_name) |
| 104 | fprintf (stderr, "%s: ", this_command_name); |
| 105 | } |
| 106 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 107 | void |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 108 | #if defined (PREFER_STDARG) |
| 109 | builtin_error (const char *format, ...) |
| 110 | #else |
| 111 | builtin_error (format, va_alist) |
| 112 | const char *format; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 113 | va_dcl |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 114 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 115 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | va_list args; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 118 | builtin_error_prolog (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 119 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 120 | SH_VA_START (args, format); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 121 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 122 | vfprintf (stderr, format, args); |
| 123 | va_end (args); |
| 124 | fprintf (stderr, "\n"); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | #if defined (PREFER_STDARG) |
| 129 | builtin_warning (const char *format, ...) |
| 130 | #else |
| 131 | builtin_warning (format, va_alist) |
| 132 | const char *format; |
| 133 | va_dcl |
| 134 | #endif |
| 135 | { |
| 136 | va_list args; |
| 137 | |
| 138 | builtin_error_prolog (); |
| 139 | fprintf (stderr, _("warning: ")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 140 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 141 | SH_VA_START (args, format); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 142 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 143 | vfprintf (stderr, format, args); |
| 144 | va_end (args); |
| 145 | fprintf (stderr, "\n"); |
| 146 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 147 | |
| 148 | /* Print a usage summary for the currently-executing builtin command. */ |
| 149 | void |
| 150 | builtin_usage () |
| 151 | { |
| 152 | if (this_command_name && *this_command_name) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 153 | fprintf (stderr, _("%s: usage: "), this_command_name); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 154 | fprintf (stderr, "%s\n", _(current_builtin->short_doc)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 155 | fflush (stderr); |
| 156 | } |
| 157 | |
| 158 | /* Return if LIST is NULL else barf and jump to top_level. Used by some |
| 159 | builtins that do not accept arguments. */ |
| 160 | void |
| 161 | no_args (list) |
| 162 | WORD_LIST *list; |
| 163 | { |
| 164 | if (list) |
| 165 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 166 | builtin_error (_("too many arguments")); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 167 | top_level_cleanup (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 168 | jump_to_top_level (DISCARD); |
| 169 | } |
| 170 | } |
| 171 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 172 | /* Check that no options were given to the currently-executing builtin, |
| 173 | and return 0 if there were options. */ |
| 174 | int |
| 175 | no_options (list) |
| 176 | WORD_LIST *list; |
| 177 | { |
| 178 | reset_internal_getopt (); |
| 179 | if (internal_getopt (list, "") != -1) |
| 180 | { |
| 181 | builtin_usage (); |
| 182 | return (1); |
| 183 | } |
| 184 | return (0); |
| 185 | } |
| 186 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 187 | void |
| 188 | sh_needarg (s) |
| 189 | char *s; |
| 190 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 191 | builtin_error (_("%s: option requires an argument"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
| 195 | sh_neednumarg (s) |
| 196 | char *s; |
| 197 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 198 | builtin_error (_("%s: numeric argument required"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
| 202 | sh_notfound (s) |
| 203 | char *s; |
| 204 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 205 | builtin_error (_("%s: not found"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* Function called when one of the builtin commands detects an invalid |
| 209 | option. */ |
| 210 | void |
| 211 | sh_invalidopt (s) |
| 212 | char *s; |
| 213 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 214 | builtin_error (_("%s: invalid option"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
| 218 | sh_invalidoptname (s) |
| 219 | char *s; |
| 220 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 221 | builtin_error (_("%s: invalid option name"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void |
| 225 | sh_invalidid (s) |
| 226 | char *s; |
| 227 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 228 | builtin_error (_("`%s': not a valid identifier"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
| 232 | sh_invalidnum (s) |
| 233 | char *s; |
| 234 | { |
Patrick Schaaf | ba43bf0 | 2012-04-02 12:07:46 +0200 | [diff] [blame] | 235 | const char *msg; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 236 | |
| 237 | if (*s == '0' && isdigit (s[1])) |
| 238 | msg = _("invalid octal number"); |
| 239 | else if (*s == '0' && s[1] == 'x') |
| 240 | msg = _("invalid hex number"); |
| 241 | else |
| 242 | msg = _("invalid number"); |
| 243 | builtin_error ("%s: %s", s, msg); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | sh_invalidsig (s) |
| 248 | char *s; |
| 249 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 250 | builtin_error (_("%s: invalid signal specification"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | sh_badpid (s) |
| 255 | char *s; |
| 256 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 257 | builtin_error (_("`%s': not a pid or valid job spec"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void |
| 261 | sh_readonly (s) |
| 262 | const char *s; |
| 263 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 264 | builtin_error (_("%s: readonly variable"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void |
| 268 | sh_erange (s, desc) |
| 269 | char *s, *desc; |
| 270 | { |
| 271 | if (s) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 272 | builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 273 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 274 | builtin_error (_("%s out of range"), desc ? desc : _("argument")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | #if defined (JOB_CONTROL) |
| 278 | void |
| 279 | sh_badjob (s) |
| 280 | char *s; |
| 281 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 282 | builtin_error (_("%s: no such job"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void |
| 286 | sh_nojobs (s) |
| 287 | char *s; |
| 288 | { |
| 289 | if (s) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 290 | builtin_error (_("%s: no job control"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 291 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 292 | builtin_error (_("no job control")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #if defined (RESTRICTED_SHELL) |
| 297 | void |
| 298 | sh_restricted (s) |
| 299 | char *s; |
| 300 | { |
| 301 | if (s) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 302 | builtin_error (_("%s: restricted"), s); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 303 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 304 | builtin_error (_("restricted")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 305 | } |
| 306 | #endif |
| 307 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 308 | void |
| 309 | sh_notbuiltin (s) |
| 310 | char *s; |
| 311 | { |
| 312 | builtin_error (_("%s: not a shell builtin"), s); |
| 313 | } |
| 314 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 315 | void |
| 316 | sh_wrerror () |
| 317 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 318 | #if defined (DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS) && defined (EPIPE) |
| 319 | if (errno != EPIPE) |
| 320 | #endif /* DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS && EPIPE */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 321 | builtin_error (_("write error: %s"), strerror (errno)); |
| 322 | } |
| 323 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 324 | void |
| 325 | sh_ttyerror (set) |
| 326 | int set; |
| 327 | { |
| 328 | if (set) |
| 329 | builtin_error (_("error setting terminal attributes: %s"), strerror (errno)); |
| 330 | else |
| 331 | builtin_error (_("error getting terminal attributes: %s"), strerror (errno)); |
| 332 | } |
| 333 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 334 | int |
| 335 | sh_chkwrite (s) |
| 336 | int s; |
| 337 | { |
| 338 | fflush (stdout); |
| 339 | if (ferror (stdout)) |
| 340 | { |
| 341 | sh_wrerror (); |
| 342 | fpurge (stdout); |
| 343 | clearerr (stdout); |
| 344 | return (EXECUTION_FAILURE); |
| 345 | } |
| 346 | return (s); |
| 347 | } |
| 348 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 349 | /* **************************************************************** */ |
| 350 | /* */ |
| 351 | /* Shell positional parameter manipulation */ |
| 352 | /* */ |
| 353 | /* **************************************************************** */ |
| 354 | |
| 355 | /* Convert a WORD_LIST into a C-style argv. Return the number of elements |
| 356 | in the list in *IP, if IP is non-null. A convenience function for |
| 357 | loadable builtins; also used by `test'. */ |
| 358 | char ** |
| 359 | make_builtin_argv (list, ip) |
| 360 | WORD_LIST *list; |
| 361 | int *ip; |
| 362 | { |
| 363 | char **argv; |
| 364 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 365 | argv = strvec_from_word_list (list, 0, 1, ip); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 366 | argv[0] = this_command_name; |
| 367 | return argv; |
| 368 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 369 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 370 | /* Remember LIST in $1 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 371 | non-zero, then discard whatever the existing arguments are, else |
| 372 | only discard the ones that are to be replaced. */ |
| 373 | void |
| 374 | remember_args (list, destructive) |
| 375 | WORD_LIST *list; |
| 376 | int destructive; |
| 377 | { |
| 378 | register int i; |
| 379 | |
| 380 | for (i = 1; i < 10; i++) |
| 381 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 382 | if ((destructive || list) && dollar_vars[i]) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 383 | { |
| 384 | free (dollar_vars[i]); |
| 385 | dollar_vars[i] = (char *)NULL; |
| 386 | } |
| 387 | |
| 388 | if (list) |
| 389 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 390 | dollar_vars[i] = savestring (list->word->word); |
| 391 | list = list->next; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* If arguments remain, assign them to REST_OF_ARGS. |
| 396 | Note that copy_word_list (NULL) returns NULL, and |
| 397 | that dispose_words (NULL) does nothing. */ |
| 398 | if (destructive || list) |
| 399 | { |
| 400 | dispose_words (rest_of_args); |
| 401 | rest_of_args = copy_word_list (list); |
| 402 | } |
| 403 | |
| 404 | if (destructive) |
| 405 | set_dollar_vars_changed (); |
| 406 | } |
| 407 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 408 | static int changed_dollar_vars; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 409 | |
| 410 | /* Have the dollar variables been reset to new values since we last |
| 411 | checked? */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 412 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 413 | dollar_vars_changed () |
| 414 | { |
| 415 | return (changed_dollar_vars); |
| 416 | } |
| 417 | |
| 418 | void |
| 419 | set_dollar_vars_unchanged () |
| 420 | { |
| 421 | changed_dollar_vars = 0; |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | set_dollar_vars_changed () |
| 426 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 427 | if (variable_context) |
| 428 | changed_dollar_vars |= ARGS_FUNC; |
| 429 | else if (this_shell_builtin == set_builtin) |
| 430 | changed_dollar_vars |= ARGS_SETBLTIN; |
| 431 | else |
| 432 | changed_dollar_vars |= ARGS_INVOC; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 435 | /* **************************************************************** */ |
| 436 | /* */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 437 | /* Validating numeric input and arguments */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 438 | /* */ |
| 439 | /* **************************************************************** */ |
| 440 | |
| 441 | /* Read a numeric arg for this_command_name, the name of the shell builtin |
| 442 | that wants it. LIST is the word list that the arg is to come from. |
| 443 | Accept only the numeric argument; report an error if other arguments |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 444 | follow. If FATAL is 1, call throw_to_top_level, which exits the |
| 445 | shell; if it's 2, call jump_to_top_level (DISCARD), which aborts the |
| 446 | current command; if FATAL is 0, return an indication of an invalid |
| 447 | number by setting *NUMOK == 0 and return -1. */ |
| 448 | int |
| 449 | get_numeric_arg (list, fatal, count) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 450 | WORD_LIST *list; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 451 | int fatal; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 452 | intmax_t *count; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 453 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 454 | char *arg; |
| 455 | |
| 456 | if (count) |
| 457 | *count = 1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 458 | |
| 459 | if (list && list->word && ISOPTION (list->word->word, '-')) |
| 460 | list = list->next; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 461 | |
| 462 | if (list) |
| 463 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 464 | arg = list->word->word; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 465 | if (arg == 0 || (legal_number (arg, count) == 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 466 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 467 | sh_neednumarg (list->word->word ? list->word->word : "`'"); |
| 468 | if (fatal == 0) |
| 469 | return 0; |
| 470 | else if (fatal == 1) /* fatal == 1; abort */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 471 | throw_to_top_level (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 472 | else /* fatal == 2; discard current command */ |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 473 | { |
| 474 | top_level_cleanup (); |
| 475 | jump_to_top_level (DISCARD); |
| 476 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 477 | } |
| 478 | no_args (list->next); |
| 479 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 480 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 481 | return (1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 484 | /* Get an eight-bit status value from LIST */ |
| 485 | int |
| 486 | get_exitstat (list) |
| 487 | WORD_LIST *list; |
| 488 | { |
| 489 | int status; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 490 | intmax_t sval; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 491 | char *arg; |
| 492 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 493 | if (list && list->word && ISOPTION (list->word->word, '-')) |
| 494 | list = list->next; |
| 495 | |
| 496 | if (list == 0) |
| 497 | return (last_command_exit_value); |
| 498 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 499 | arg = list->word->word; |
| 500 | if (arg == 0 || legal_number (arg, &sval) == 0) |
| 501 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 502 | sh_neednumarg (list->word->word ? list->word->word : "`'"); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 503 | return EX_BADUSAGE; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 504 | } |
| 505 | no_args (list->next); |
| 506 | |
| 507 | status = sval & 255; |
| 508 | return status; |
| 509 | } |
| 510 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 511 | /* Return the octal number parsed from STRING, or -1 to indicate |
| 512 | that the string contained a bad number. */ |
| 513 | int |
| 514 | read_octal (string) |
| 515 | char *string; |
| 516 | { |
| 517 | int result, digits; |
| 518 | |
| 519 | result = digits = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 520 | while (*string && ISOCTAL (*string)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 521 | { |
| 522 | digits++; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 523 | result = (result * 8) + (*string++ - '0'); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 524 | if (result > 0777) |
| 525 | return -1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 528 | if (digits == 0 || *string) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 529 | result = -1; |
| 530 | |
| 531 | return (result); |
| 532 | } |
| 533 | |
| 534 | /* **************************************************************** */ |
| 535 | /* */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 536 | /* Manipulating the current working directory */ |
| 537 | /* */ |
| 538 | /* **************************************************************** */ |
| 539 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 540 | /* Return a consed string which is the current working directory. |
| 541 | FOR_WHOM is the name of the caller for error printing. */ |
| 542 | char *the_current_working_directory = (char *)NULL; |
| 543 | |
| 544 | char * |
| 545 | get_working_directory (for_whom) |
| 546 | char *for_whom; |
| 547 | { |
| 548 | if (no_symbolic_links) |
| 549 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 550 | FREE (the_current_working_directory); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 551 | the_current_working_directory = (char *)NULL; |
| 552 | } |
| 553 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 554 | if (the_current_working_directory == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 555 | { |
cvpcs | d158645 | 2010-06-06 18:06:59 -0500 | [diff] [blame] | 556 | #if defined (__BIONIC__) |
| 557 | /** |
| 558 | * This is a really lame work around to try to stop bash from flipping |
| 559 | * out if PWD isn't defined when starting it up on bionic |
| 560 | */ |
| 561 | char *d = (char *)malloc(sizeof(char) * PATH_MAX); |
Patrick Schaaf | ba43bf0 | 2012-04-02 12:07:46 +0200 | [diff] [blame] | 562 | if (d) { |
| 563 | the_current_working_directory = getcwd (d, sizeof(char) * PATH_MAX); |
| 564 | if (the_current_working_directory) |
| 565 | the_current_working_directory = d; |
| 566 | else |
| 567 | FREE (d); |
| 568 | } |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 569 | #else |
cvpcs | d158645 | 2010-06-06 18:06:59 -0500 | [diff] [blame] | 570 | # if defined (GETCWD_BROKEN) |
| 571 | the_current_working_directory = getcwd (0, PATH_MAX); |
| 572 | # else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 573 | the_current_working_directory = getcwd (0, 0); |
cvpcs | d158645 | 2010-06-06 18:06:59 -0500 | [diff] [blame] | 574 | # endif |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 575 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 576 | if (the_current_working_directory == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 577 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 578 | fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"), |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 579 | (for_whom && *for_whom) ? for_whom : get_name_for_error (), |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 580 | _(bash_getcwd_errstr), strerror (errno)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 581 | return (char *)NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | return (savestring (the_current_working_directory)); |
| 586 | } |
| 587 | |
| 588 | /* Make NAME our internal idea of the current working directory. */ |
| 589 | void |
| 590 | set_working_directory (name) |
| 591 | char *name; |
| 592 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 593 | FREE (the_current_working_directory); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 594 | the_current_working_directory = savestring (name); |
| 595 | } |
| 596 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 597 | /* **************************************************************** */ |
| 598 | /* */ |
| 599 | /* Job control support functions */ |
| 600 | /* */ |
| 601 | /* **************************************************************** */ |
| 602 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 603 | #if defined (JOB_CONTROL) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 604 | int |
| 605 | get_job_by_name (name, flags) |
| 606 | const char *name; |
| 607 | int flags; |
| 608 | { |
| 609 | register int i, wl, cl, match, job; |
| 610 | register PROCESS *p; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 611 | register JOB *j; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 612 | |
| 613 | job = NO_JOB; |
| 614 | wl = strlen (name); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 615 | for (i = js.j_jobslots - 1; i >= 0; i--) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 616 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 617 | j = get_job_by_jid (i); |
| 618 | if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 619 | continue; |
| 620 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 621 | p = j->pipe; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 622 | do |
| 623 | { |
| 624 | if (flags & JM_EXACT) |
| 625 | { |
| 626 | cl = strlen (p->command); |
| 627 | match = STREQN (p->command, name, cl); |
| 628 | } |
| 629 | else if (flags & JM_SUBSTRING) |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 630 | match = strcasestr (p->command, name) != (char *)0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 631 | else |
| 632 | match = STREQN (p->command, name, wl); |
| 633 | |
| 634 | if (match == 0) |
| 635 | { |
| 636 | p = p->next; |
| 637 | continue; |
| 638 | } |
| 639 | else if (flags & JM_FIRSTMATCH) |
| 640 | return i; /* return first match */ |
| 641 | else if (job != NO_JOB) |
| 642 | { |
| 643 | if (this_shell_builtin) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 644 | builtin_error (_("%s: ambiguous job spec"), name); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 645 | else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 646 | internal_error (_("%s: ambiguous job spec"), name); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 647 | return (DUP_JOB); |
| 648 | } |
| 649 | else |
| 650 | job = i; |
| 651 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 652 | while (p != j->pipe); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | return (job); |
| 656 | } |
| 657 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 658 | /* Return the job spec found in LIST. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 659 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 660 | get_job_spec (list) |
| 661 | WORD_LIST *list; |
| 662 | { |
| 663 | register char *word; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 664 | int job, jflags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 665 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 666 | if (list == 0) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 667 | return (js.j_current); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 668 | |
| 669 | word = list->word->word; |
| 670 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 671 | if (*word == '\0') |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 672 | return (NO_JOB); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 673 | |
| 674 | if (*word == '%') |
| 675 | word++; |
| 676 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 677 | if (DIGIT (*word) && all_digits (word)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 678 | { |
| 679 | job = atoi (word); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 680 | return (job > js.j_jobslots ? NO_JOB : job - 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 681 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 682 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 683 | jflags = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 684 | switch (*word) |
| 685 | { |
| 686 | case 0: |
| 687 | case '%': |
| 688 | case '+': |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 689 | return (js.j_current); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 690 | |
| 691 | case '-': |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 692 | return (js.j_previous); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 693 | |
| 694 | case '?': /* Substring search requested. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 695 | jflags |= JM_SUBSTRING; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 696 | word++; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 697 | /* FALLTHROUGH */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 698 | |
| 699 | default: |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 700 | return get_job_by_name (word, jflags); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | #endif /* JOB_CONTROL */ |
| 704 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 705 | /* |
| 706 | * NOTE: `kill' calls this function with forcecols == 0 |
| 707 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 708 | int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 709 | display_signal_list (list, forcecols) |
| 710 | WORD_LIST *list; |
| 711 | int forcecols; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 712 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 713 | register int i, column; |
| 714 | char *name; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 715 | int result, signum, dflags; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 716 | intmax_t lsignum; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 717 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 718 | result = EXECUTION_SUCCESS; |
| 719 | if (!list) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 720 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 721 | for (i = 1, column = 0; i < NSIG; i++) |
| 722 | { |
| 723 | name = signal_name (i); |
| 724 | if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7)) |
| 725 | continue; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 726 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 727 | if (posixly_correct && !forcecols) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 728 | { |
| 729 | /* This is for the kill builtin. POSIX.2 says the signal names |
| 730 | are displayed without the `SIG' prefix. */ |
| 731 | if (STREQN (name, "SIG", 3)) |
| 732 | name += 3; |
| 733 | printf ("%s%s", name, (i == NSIG - 1) ? "" : " "); |
| 734 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 735 | else |
| 736 | { |
| 737 | printf ("%2d) %s", i, name); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 738 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 739 | if (++column < 5) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 740 | printf ("\t"); |
| 741 | else |
| 742 | { |
| 743 | printf ("\n"); |
| 744 | column = 0; |
| 745 | } |
| 746 | } |
| 747 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 748 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 749 | if ((posixly_correct && !forcecols) || column != 0) |
| 750 | printf ("\n"); |
| 751 | return result; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 754 | /* List individual signal names or numbers. */ |
| 755 | while (list) |
| 756 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 757 | if (legal_number (list->word->word, &lsignum)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 758 | { |
| 759 | /* This is specified by Posix.2 so that exit statuses can be |
| 760 | mapped into signal numbers. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 761 | if (lsignum > 128) |
| 762 | lsignum -= 128; |
| 763 | if (lsignum < 0 || lsignum >= NSIG) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 764 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 765 | sh_invalidsig (list->word->word); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | result = EXECUTION_FAILURE; |
| 767 | list = list->next; |
| 768 | continue; |
| 769 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 770 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 771 | signum = lsignum; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 772 | name = signal_name (signum); |
| 773 | if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7)) |
| 774 | { |
| 775 | list = list->next; |
| 776 | continue; |
| 777 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 778 | #if defined (JOB_CONTROL) |
| 779 | /* POSIX.2 says that `kill -l signum' prints the signal name without |
| 780 | the `SIG' prefix. */ |
| 781 | printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name); |
| 782 | #else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 783 | printf ("%s\n", name); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 784 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 788 | dflags = DSIG_NOCASE; |
| 789 | if (posixly_correct == 0 || this_shell_builtin != kill_builtin) |
| 790 | dflags |= DSIG_SIGPREFIX; |
| 791 | signum = decode_signal (list->word->word, dflags); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 792 | if (signum == NO_SIG) |
| 793 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 794 | sh_invalidsig (list->word->word); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 795 | result = EXECUTION_FAILURE; |
| 796 | list = list->next; |
| 797 | continue; |
| 798 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 799 | printf ("%d\n", signum); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 800 | } |
| 801 | list = list->next; |
| 802 | } |
| 803 | return (result); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 806 | /* **************************************************************** */ |
| 807 | /* */ |
| 808 | /* Finding builtin commands and their functions */ |
| 809 | /* */ |
| 810 | /* **************************************************************** */ |
| 811 | |
| 812 | /* Perform a binary search and return the address of the builtin function |
| 813 | whose name is NAME. If the function couldn't be found, or the builtin |
| 814 | is disabled or has no function associated with it, return NULL. |
| 815 | Return the address of the builtin. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 816 | DISABLED_OKAY means find it even if the builtin is disabled. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 817 | struct builtin * |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 818 | builtin_address_internal (name, disabled_okay) |
| 819 | char *name; |
| 820 | int disabled_okay; |
| 821 | { |
| 822 | int hi, lo, mid, j; |
| 823 | |
| 824 | hi = num_shell_builtins - 1; |
| 825 | lo = 0; |
| 826 | |
| 827 | while (lo <= hi) |
| 828 | { |
| 829 | mid = (lo + hi) / 2; |
| 830 | |
| 831 | j = shell_builtins[mid].name[0] - name[0]; |
| 832 | |
| 833 | if (j == 0) |
| 834 | j = strcmp (shell_builtins[mid].name, name); |
| 835 | |
| 836 | if (j == 0) |
| 837 | { |
| 838 | /* It must have a function pointer. It must be enabled, or we |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 839 | must have explicitly allowed disabled functions to be found, |
| 840 | and it must not have been deleted. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 841 | if (shell_builtins[mid].function && |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 842 | ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) && |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 843 | ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 844 | return (&shell_builtins[mid]); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 845 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 846 | return ((struct builtin *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 847 | } |
| 848 | if (j > 0) |
| 849 | hi = mid - 1; |
| 850 | else |
| 851 | lo = mid + 1; |
| 852 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 853 | return ((struct builtin *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 856 | /* Return the pointer to the function implementing builtin command NAME. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 857 | sh_builtin_func_t * |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 858 | find_shell_builtin (name) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 859 | char *name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 860 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 861 | current_builtin = builtin_address_internal (name, 0); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 862 | return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 865 | /* Return the address of builtin with NAME, whether it is enabled or not. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 866 | sh_builtin_func_t * |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 867 | builtin_address (name) |
| 868 | char *name; |
| 869 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 870 | current_builtin = builtin_address_internal (name, 1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 871 | return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 874 | /* Return the function implementing the builtin NAME, but only if it is a |
| 875 | POSIX.2 special builtin. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 876 | sh_builtin_func_t * |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 877 | find_special_builtin (name) |
| 878 | char *name; |
| 879 | { |
| 880 | current_builtin = builtin_address_internal (name, 0); |
| 881 | return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ? |
| 882 | current_builtin->function : |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 883 | (sh_builtin_func_t *)NULL); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 886 | static int |
| 887 | shell_builtin_compare (sbp1, sbp2) |
| 888 | struct builtin *sbp1, *sbp2; |
| 889 | { |
| 890 | int result; |
| 891 | |
| 892 | if ((result = sbp1->name[0] - sbp2->name[0]) == 0) |
| 893 | result = strcmp (sbp1->name, sbp2->name); |
| 894 | |
| 895 | return (result); |
| 896 | } |
| 897 | |
| 898 | /* Sort the table of shell builtins so that the binary search will work |
| 899 | in find_shell_builtin. */ |
| 900 | void |
| 901 | initialize_shell_builtins () |
| 902 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 903 | qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin), |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 904 | (QSFUNC *)shell_builtin_compare); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 905 | } |