blob: c728e0ba6426fafe180817e3f76af28839747733 [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* common.c - utility functions for all builtins */
2
Chet Ramey495aee42011-11-22 19:11:26 -05003/* Copyright (C) 1987-2010 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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 Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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 Aalto726f6381996-08-26 18:22:31 +000020
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#include <config.h>
22
23#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000024# ifdef _MINIX
25# include <sys/types.h>
26# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000027# include <unistd.h>
28#endif
29
Jari Aalto726f6381996-08-26 18:22:31 +000030#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000031#include <chartypes.h>
Jari Aaltod166f041997-06-05 14:59:13 +000032#include "../bashtypes.h"
Jari Aaltobb706242000-03-17 21:46:59 +000033#include "posixstat.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000034#include <signal.h>
Jari Aalto726f6381996-08-26 18:22:31 +000035
Jari Aaltocce855b1998-04-17 19:52:44 +000036#include <errno.h>
37
Jari Aaltoccc6cda1996-12-23 17:02:34 +000038#if defined (PREFER_STDARG)
39# include <stdarg.h>
40#else
Jari Aalto7117c2d2002-07-17 14:10:11 +000041# include <varargs.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000042#endif
43
44#include "../bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000045#include "../bashintl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000046
Jari Aalto31859422009-01-12 13:36:28 +000047#define NEED_FPURGE_DECL
48
Jari Aalto726f6381996-08-26 18:22:31 +000049#include "../shell.h"
Jari Aaltobb706242000-03-17 21:46:59 +000050#include "maxpath.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051#include "../flags.h"
Jari Aalto726f6381996-08-26 18:22:31 +000052#include "../jobs.h"
53#include "../builtins.h"
54#include "../input.h"
55#include "../execute_cmd.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000056#include "../trap.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057#include "bashgetopt.h"
Jari Aalto726f6381996-08-26 18:22:31 +000058#include "common.h"
Jari Aaltod166f041997-06-05 14:59:13 +000059#include "builtext.h"
Jari Aalto726f6381996-08-26 18:22:31 +000060#include <tilde/tilde.h>
61
62#if defined (HISTORY)
63# include "../bashhist.h"
64#endif
65
Jari Aaltocce855b1998-04-17 19:52:44 +000066#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010067#include <errno.h>
Jari Aaltocce855b1998-04-17 19:52:44 +000068#endif /* !errno */
69
Jari Aalto7117c2d2002-07-17 14:10:11 +000070extern int indirection_level, subshell_environment;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000071extern int line_number;
Jari Aalto726f6381996-08-26 18:22:31 +000072extern int last_command_exit_value;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000073extern int running_trap;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074extern int posixly_correct;
Jari Aalto726f6381996-08-26 18:22:31 +000075extern char *this_command_name, *shell_name;
Jari Aalto31859422009-01-12 13:36:28 +000076extern const char * const bash_getcwd_errstr;
Jari Aalto726f6381996-08-26 18:22:31 +000077
Jari Aaltod166f041997-06-05 14:59:13 +000078/* Used by some builtins and the mainline code. */
Jari Aaltof73dda02001-11-13 17:56:06 +000079sh_builtin_func_t *last_shell_builtin = (sh_builtin_func_t *)NULL;
80sh_builtin_func_t *this_shell_builtin = (sh_builtin_func_t *)NULL;
Jari Aaltod166f041997-06-05 14:59:13 +000081
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082/* **************************************************************** */
83/* */
84/* Error reporting, usage, and option processing */
85/* */
86/* **************************************************************** */
Jari Aalto726f6381996-08-26 18:22:31 +000087
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 Aalto31859422009-01-12 13:36:28 +000091
92static void
93builtin_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 Aalto726f6381996-08-26 18:22:31 +0000107void
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108#if defined (PREFER_STDARG)
109builtin_error (const char *format, ...)
110#else
111builtin_error (format, va_alist)
112 const char *format;
Jari Aalto726f6381996-08-26 18:22:31 +0000113 va_dcl
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000114#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000115{
Jari Aalto726f6381996-08-26 18:22:31 +0000116 va_list args;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000117
Jari Aalto31859422009-01-12 13:36:28 +0000118 builtin_error_prolog ();
Jari Aalto726f6381996-08-26 18:22:31 +0000119
Jari Aalto31859422009-01-12 13:36:28 +0000120 SH_VA_START (args, format);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000121
Jari Aalto31859422009-01-12 13:36:28 +0000122 vfprintf (stderr, format, args);
123 va_end (args);
124 fprintf (stderr, "\n");
125}
126
127void
128#if defined (PREFER_STDARG)
129builtin_warning (const char *format, ...)
130#else
131builtin_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 Aalto726f6381996-08-26 18:22:31 +0000140
Jari Aalto7117c2d2002-07-17 14:10:11 +0000141 SH_VA_START (args, format);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000142
Jari Aalto726f6381996-08-26 18:22:31 +0000143 vfprintf (stderr, format, args);
144 va_end (args);
145 fprintf (stderr, "\n");
146}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000147
148/* Print a usage summary for the currently-executing builtin command. */
149void
150builtin_usage ()
151{
152 if (this_command_name && *this_command_name)
Jari Aalto31859422009-01-12 13:36:28 +0000153 fprintf (stderr, _("%s: usage: "), this_command_name);
Chet Ramey495aee42011-11-22 19:11:26 -0500154 fprintf (stderr, "%s\n", _(current_builtin->short_doc));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000155 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. */
160void
161no_args (list)
162 WORD_LIST *list;
163{
164 if (list)
165 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000166 builtin_error (_("too many arguments"));
Jari Aaltof1be6662008-11-18 13:15:12 +0000167 top_level_cleanup ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000168 jump_to_top_level (DISCARD);
169 }
170}
171
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000172/* Check that no options were given to the currently-executing builtin,
173 and return 0 if there were options. */
174int
175no_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 Aalto7117c2d2002-07-17 14:10:11 +0000187void
188sh_needarg (s)
189 char *s;
190{
Jari Aaltob80f6442004-07-27 13:29:18 +0000191 builtin_error (_("%s: option requires an argument"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000192}
193
194void
195sh_neednumarg (s)
196 char *s;
197{
Jari Aaltob80f6442004-07-27 13:29:18 +0000198 builtin_error (_("%s: numeric argument required"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000199}
200
201void
202sh_notfound (s)
203 char *s;
204{
Jari Aaltob80f6442004-07-27 13:29:18 +0000205 builtin_error (_("%s: not found"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000206}
207
208/* Function called when one of the builtin commands detects an invalid
209 option. */
210void
211sh_invalidopt (s)
212 char *s;
213{
Jari Aaltob80f6442004-07-27 13:29:18 +0000214 builtin_error (_("%s: invalid option"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000215}
216
217void
218sh_invalidoptname (s)
219 char *s;
220{
Jari Aaltob80f6442004-07-27 13:29:18 +0000221 builtin_error (_("%s: invalid option name"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000222}
223
224void
225sh_invalidid (s)
226 char *s;
227{
Jari Aaltob80f6442004-07-27 13:29:18 +0000228 builtin_error (_("`%s': not a valid identifier"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000229}
230
231void
232sh_invalidnum (s)
233 char *s;
234{
Patrick Schaafba43bf02012-04-02 12:07:46 +0200235 const char *msg;
Jari Aalto31859422009-01-12 13:36:28 +0000236
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 Aalto7117c2d2002-07-17 14:10:11 +0000244}
245
246void
247sh_invalidsig (s)
248 char *s;
249{
Jari Aaltob80f6442004-07-27 13:29:18 +0000250 builtin_error (_("%s: invalid signal specification"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000251}
252
253void
254sh_badpid (s)
255 char *s;
256{
Jari Aaltob80f6442004-07-27 13:29:18 +0000257 builtin_error (_("`%s': not a pid or valid job spec"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000258}
259
260void
261sh_readonly (s)
262 const char *s;
263{
Jari Aaltob80f6442004-07-27 13:29:18 +0000264 builtin_error (_("%s: readonly variable"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000265}
266
267void
268sh_erange (s, desc)
269 char *s, *desc;
270{
271 if (s)
Jari Aaltob80f6442004-07-27 13:29:18 +0000272 builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000273 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000274 builtin_error (_("%s out of range"), desc ? desc : _("argument"));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000275}
276
277#if defined (JOB_CONTROL)
278void
279sh_badjob (s)
280 char *s;
281{
Jari Aaltob80f6442004-07-27 13:29:18 +0000282 builtin_error (_("%s: no such job"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000283}
284
285void
286sh_nojobs (s)
287 char *s;
288{
289 if (s)
Jari Aaltob80f6442004-07-27 13:29:18 +0000290 builtin_error (_("%s: no job control"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000291 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000292 builtin_error (_("no job control"));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000293}
294#endif
295
296#if defined (RESTRICTED_SHELL)
297void
298sh_restricted (s)
299 char *s;
300{
301 if (s)
Jari Aaltob80f6442004-07-27 13:29:18 +0000302 builtin_error (_("%s: restricted"), s);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000303 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000304 builtin_error (_("restricted"));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000305}
306#endif
307
Jari Aaltob80f6442004-07-27 13:29:18 +0000308void
309sh_notbuiltin (s)
310 char *s;
311{
312 builtin_error (_("%s: not a shell builtin"), s);
313}
314
Jari Aalto95732b42005-12-07 14:08:12 +0000315void
316sh_wrerror ()
317{
Jari Aalto31859422009-01-12 13:36:28 +0000318#if defined (DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS) && defined (EPIPE)
319 if (errno != EPIPE)
320#endif /* DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS && EPIPE */
Jari Aalto95732b42005-12-07 14:08:12 +0000321 builtin_error (_("write error: %s"), strerror (errno));
322}
323
Jari Aalto17345e52009-02-19 22:21:29 +0000324void
325sh_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 Aalto31859422009-01-12 13:36:28 +0000334int
335sh_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 Aaltoccc6cda1996-12-23 17:02:34 +0000349/* **************************************************************** */
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'. */
358char **
359make_builtin_argv (list, ip)
360 WORD_LIST *list;
361 int *ip;
362{
363 char **argv;
364
Jari Aalto7117c2d2002-07-17 14:10:11 +0000365 argv = strvec_from_word_list (list, 0, 1, ip);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000366 argv[0] = this_command_name;
367 return argv;
368}
Jari Aalto726f6381996-08-26 18:22:31 +0000369
Chet Ramey495aee42011-11-22 19:11:26 -0500370/* Remember LIST in $1 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
Jari Aalto726f6381996-08-26 18:22:31 +0000371 non-zero, then discard whatever the existing arguments are, else
372 only discard the ones that are to be replaced. */
373void
374remember_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 Aaltoccc6cda1996-12-23 17:02:34 +0000382 if ((destructive || list) && dollar_vars[i])
Jari Aalto726f6381996-08-26 18:22:31 +0000383 {
384 free (dollar_vars[i]);
385 dollar_vars[i] = (char *)NULL;
386 }
387
388 if (list)
389 {
Jari Aalto726f6381996-08-26 18:22:31 +0000390 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 Aaltoccc6cda1996-12-23 17:02:34 +0000408static int changed_dollar_vars;
Jari Aalto726f6381996-08-26 18:22:31 +0000409
410/* Have the dollar variables been reset to new values since we last
411 checked? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000412int
Jari Aalto726f6381996-08-26 18:22:31 +0000413dollar_vars_changed ()
414{
415 return (changed_dollar_vars);
416}
417
418void
419set_dollar_vars_unchanged ()
420{
421 changed_dollar_vars = 0;
422}
423
424void
425set_dollar_vars_changed ()
426{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000427 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 Aalto726f6381996-08-26 18:22:31 +0000433}
434
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000435/* **************************************************************** */
436/* */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000437/* Validating numeric input and arguments */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000438/* */
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 Aalto31859422009-01-12 13:36:28 +0000444 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. */
448int
449get_numeric_arg (list, fatal, count)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000450 WORD_LIST *list;
Jari Aaltod166f041997-06-05 14:59:13 +0000451 int fatal;
Jari Aalto31859422009-01-12 13:36:28 +0000452 intmax_t *count;
Jari Aalto726f6381996-08-26 18:22:31 +0000453{
Jari Aalto31859422009-01-12 13:36:28 +0000454 char *arg;
455
456 if (count)
457 *count = 1;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000458
459 if (list && list->word && ISOPTION (list->word->word, '-'))
460 list = list->next;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000461
462 if (list)
463 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000464 arg = list->word->word;
Jari Aalto31859422009-01-12 13:36:28 +0000465 if (arg == 0 || (legal_number (arg, count) == 0))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000466 {
Jari Aalto31859422009-01-12 13:36:28 +0000467 sh_neednumarg (list->word->word ? list->word->word : "`'");
468 if (fatal == 0)
469 return 0;
470 else if (fatal == 1) /* fatal == 1; abort */
Jari Aaltod166f041997-06-05 14:59:13 +0000471 throw_to_top_level ();
Jari Aalto31859422009-01-12 13:36:28 +0000472 else /* fatal == 2; discard current command */
Jari Aaltof1be6662008-11-18 13:15:12 +0000473 {
474 top_level_cleanup ();
475 jump_to_top_level (DISCARD);
476 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000477 }
478 no_args (list->next);
479 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000480
Jari Aalto31859422009-01-12 13:36:28 +0000481 return (1);
Jari Aalto726f6381996-08-26 18:22:31 +0000482}
483
Jari Aaltof73dda02001-11-13 17:56:06 +0000484/* Get an eight-bit status value from LIST */
485int
486get_exitstat (list)
487 WORD_LIST *list;
488{
489 int status;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000490 intmax_t sval;
Jari Aaltof73dda02001-11-13 17:56:06 +0000491 char *arg;
492
Jari Aalto7117c2d2002-07-17 14:10:11 +0000493 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 Aaltof73dda02001-11-13 17:56:06 +0000499 arg = list->word->word;
500 if (arg == 0 || legal_number (arg, &sval) == 0)
501 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000502 sh_neednumarg (list->word->word ? list->word->word : "`'");
Chet Rameyac50fba2014-02-26 09:36:43 -0500503 return EX_BADUSAGE;
Jari Aaltof73dda02001-11-13 17:56:06 +0000504 }
505 no_args (list->next);
506
507 status = sval & 255;
508 return status;
509}
510
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000511/* Return the octal number parsed from STRING, or -1 to indicate
512 that the string contained a bad number. */
513int
514read_octal (string)
515 char *string;
516{
517 int result, digits;
518
519 result = digits = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000520 while (*string && ISOCTAL (*string))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000521 {
522 digits++;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000523 result = (result * 8) + (*string++ - '0');
Jari Aaltof73dda02001-11-13 17:56:06 +0000524 if (result > 0777)
525 return -1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000526 }
527
Jari Aaltof73dda02001-11-13 17:56:06 +0000528 if (digits == 0 || *string)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000529 result = -1;
530
531 return (result);
532}
533
534/* **************************************************************** */
535/* */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000536/* Manipulating the current working directory */
537/* */
538/* **************************************************************** */
539
Jari Aalto726f6381996-08-26 18:22:31 +0000540/* Return a consed string which is the current working directory.
541 FOR_WHOM is the name of the caller for error printing. */
542char *the_current_working_directory = (char *)NULL;
543
544char *
545get_working_directory (for_whom)
546 char *for_whom;
547{
548 if (no_symbolic_links)
549 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000550 FREE (the_current_working_directory);
Jari Aalto726f6381996-08-26 18:22:31 +0000551 the_current_working_directory = (char *)NULL;
552 }
553
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000554 if (the_current_working_directory == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000555 {
cvpcsd1586452010-06-06 18:06:59 -0500556#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 Schaafba43bf02012-04-02 12:07:46 +0200562 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 Aaltof1be6662008-11-18 13:15:12 +0000569#else
cvpcsd1586452010-06-06 18:06:59 -0500570# if defined (GETCWD_BROKEN)
571 the_current_working_directory = getcwd (0, PATH_MAX);
572# else
Jari Aaltob80f6442004-07-27 13:29:18 +0000573 the_current_working_directory = getcwd (0, 0);
cvpcsd1586452010-06-06 18:06:59 -0500574# endif
Jari Aaltof1be6662008-11-18 13:15:12 +0000575#endif
Jari Aaltob80f6442004-07-27 13:29:18 +0000576 if (the_current_working_directory == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000577 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000578 fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000579 (for_whom && *for_whom) ? for_whom : get_name_for_error (),
Jari Aaltob80f6442004-07-27 13:29:18 +0000580 _(bash_getcwd_errstr), strerror (errno));
Jari Aalto726f6381996-08-26 18:22:31 +0000581 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. */
589void
590set_working_directory (name)
591 char *name;
592{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000593 FREE (the_current_working_directory);
Jari Aalto726f6381996-08-26 18:22:31 +0000594 the_current_working_directory = savestring (name);
595}
596
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000597/* **************************************************************** */
598/* */
599/* Job control support functions */
600/* */
601/* **************************************************************** */
602
Jari Aalto726f6381996-08-26 18:22:31 +0000603#if defined (JOB_CONTROL)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000604int
605get_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 Aalto95732b42005-12-07 14:08:12 +0000611 register JOB *j;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000612
613 job = NO_JOB;
614 wl = strlen (name);
Jari Aalto95732b42005-12-07 14:08:12 +0000615 for (i = js.j_jobslots - 1; i >= 0; i--)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000616 {
Jari Aalto95732b42005-12-07 14:08:12 +0000617 j = get_job_by_jid (i);
618 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000619 continue;
620
Jari Aalto95732b42005-12-07 14:08:12 +0000621 p = j->pipe;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000622 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 Ramey00018032011-11-21 20:51:19 -0500630 match = strcasestr (p->command, name) != (char *)0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000631 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 Aaltob80f6442004-07-27 13:29:18 +0000644 builtin_error (_("%s: ambiguous job spec"), name);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000645 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500646 internal_error (_("%s: ambiguous job spec"), name);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000647 return (DUP_JOB);
648 }
649 else
650 job = i;
651 }
Jari Aalto95732b42005-12-07 14:08:12 +0000652 while (p != j->pipe);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000653 }
654
655 return (job);
656}
657
Jari Aalto726f6381996-08-26 18:22:31 +0000658/* Return the job spec found in LIST. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000659int
Jari Aalto726f6381996-08-26 18:22:31 +0000660get_job_spec (list)
661 WORD_LIST *list;
662{
663 register char *word;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000664 int job, jflags;
Jari Aalto726f6381996-08-26 18:22:31 +0000665
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000666 if (list == 0)
Jari Aalto95732b42005-12-07 14:08:12 +0000667 return (js.j_current);
Jari Aalto726f6381996-08-26 18:22:31 +0000668
669 word = list->word->word;
670
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000671 if (*word == '\0')
Jari Aaltob80f6442004-07-27 13:29:18 +0000672 return (NO_JOB);
Jari Aalto726f6381996-08-26 18:22:31 +0000673
674 if (*word == '%')
675 word++;
676
Jari Aaltof73dda02001-11-13 17:56:06 +0000677 if (DIGIT (*word) && all_digits (word))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000678 {
679 job = atoi (word);
Jari Aalto95732b42005-12-07 14:08:12 +0000680 return (job > js.j_jobslots ? NO_JOB : job - 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000681 }
Jari Aalto726f6381996-08-26 18:22:31 +0000682
Jari Aalto7117c2d2002-07-17 14:10:11 +0000683 jflags = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000684 switch (*word)
685 {
686 case 0:
687 case '%':
688 case '+':
Jari Aalto95732b42005-12-07 14:08:12 +0000689 return (js.j_current);
Jari Aalto726f6381996-08-26 18:22:31 +0000690
691 case '-':
Jari Aalto95732b42005-12-07 14:08:12 +0000692 return (js.j_previous);
Jari Aalto726f6381996-08-26 18:22:31 +0000693
694 case '?': /* Substring search requested. */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000695 jflags |= JM_SUBSTRING;
Jari Aalto726f6381996-08-26 18:22:31 +0000696 word++;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000697 /* FALLTHROUGH */
Jari Aalto726f6381996-08-26 18:22:31 +0000698
699 default:
Jari Aalto7117c2d2002-07-17 14:10:11 +0000700 return get_job_by_name (word, jflags);
Jari Aalto726f6381996-08-26 18:22:31 +0000701 }
702}
703#endif /* JOB_CONTROL */
704
Jari Aaltob80f6442004-07-27 13:29:18 +0000705/*
706 * NOTE: `kill' calls this function with forcecols == 0
707 */
Jari Aalto726f6381996-08-26 18:22:31 +0000708int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000709display_signal_list (list, forcecols)
710 WORD_LIST *list;
711 int forcecols;
Jari Aalto726f6381996-08-26 18:22:31 +0000712{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000713 register int i, column;
714 char *name;
Jari Aaltob80f6442004-07-27 13:29:18 +0000715 int result, signum, dflags;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000716 intmax_t lsignum;
Jari Aalto726f6381996-08-26 18:22:31 +0000717
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000718 result = EXECUTION_SUCCESS;
719 if (!list)
Jari Aalto726f6381996-08-26 18:22:31 +0000720 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000721 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 Aalto726f6381996-08-26 18:22:31 +0000726
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000727 if (posixly_correct && !forcecols)
Jari Aaltob80f6442004-07-27 13:29:18 +0000728 {
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 Aaltoccc6cda1996-12-23 17:02:34 +0000735 else
736 {
737 printf ("%2d) %s", i, name);
Jari Aalto726f6381996-08-26 18:22:31 +0000738
Jari Aalto31859422009-01-12 13:36:28 +0000739 if (++column < 5)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000740 printf ("\t");
741 else
742 {
743 printf ("\n");
744 column = 0;
745 }
746 }
747 }
Jari Aalto726f6381996-08-26 18:22:31 +0000748
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000749 if ((posixly_correct && !forcecols) || column != 0)
750 printf ("\n");
751 return result;
Jari Aalto726f6381996-08-26 18:22:31 +0000752 }
753
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754 /* List individual signal names or numbers. */
755 while (list)
756 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000757 if (legal_number (list->word->word, &lsignum))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000758 {
759 /* This is specified by Posix.2 so that exit statuses can be
760 mapped into signal numbers. */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000761 if (lsignum > 128)
762 lsignum -= 128;
763 if (lsignum < 0 || lsignum >= NSIG)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000764 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000765 sh_invalidsig (list->word->word);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000766 result = EXECUTION_FAILURE;
767 list = list->next;
768 continue;
769 }
Jari Aalto726f6381996-08-26 18:22:31 +0000770
Jari Aalto7117c2d2002-07-17 14:10:11 +0000771 signum = lsignum;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000772 name = signal_name (signum);
773 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
774 {
775 list = list->next;
776 continue;
777 }
Jari Aaltod166f041997-06-05 14:59:13 +0000778#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 Aaltoccc6cda1996-12-23 17:02:34 +0000783 printf ("%s\n", name);
Jari Aaltod166f041997-06-05 14:59:13 +0000784#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000785 }
786 else
787 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000788 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 Aaltoccc6cda1996-12-23 17:02:34 +0000792 if (signum == NO_SIG)
793 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000794 sh_invalidsig (list->word->word);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000795 result = EXECUTION_FAILURE;
796 list = list->next;
797 continue;
798 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000799 printf ("%d\n", signum);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000800 }
801 list = list->next;
802 }
803 return (result);
Jari Aalto726f6381996-08-26 18:22:31 +0000804}
805
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000806/* **************************************************************** */
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 Aalto726f6381996-08-26 18:22:31 +0000816 DISABLED_OKAY means find it even if the builtin is disabled. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000817struct builtin *
Jari Aalto726f6381996-08-26 18:22:31 +0000818builtin_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 Aaltoccc6cda1996-12-23 17:02:34 +0000839 must have explicitly allowed disabled functions to be found,
840 and it must not have been deleted. */
Jari Aalto726f6381996-08-26 18:22:31 +0000841 if (shell_builtins[mid].function &&
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000842 ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
Jari Aalto726f6381996-08-26 18:22:31 +0000843 ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000844 return (&shell_builtins[mid]);
Jari Aalto726f6381996-08-26 18:22:31 +0000845 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000846 return ((struct builtin *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000847 }
848 if (j > 0)
849 hi = mid - 1;
850 else
851 lo = mid + 1;
852 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000853 return ((struct builtin *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000854}
855
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000856/* Return the pointer to the function implementing builtin command NAME. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000857sh_builtin_func_t *
Jari Aalto726f6381996-08-26 18:22:31 +0000858find_shell_builtin (name)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000859 char *name;
Jari Aalto726f6381996-08-26 18:22:31 +0000860{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000861 current_builtin = builtin_address_internal (name, 0);
Jari Aaltof73dda02001-11-13 17:56:06 +0000862 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000863}
864
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000865/* Return the address of builtin with NAME, whether it is enabled or not. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000866sh_builtin_func_t *
Jari Aalto726f6381996-08-26 18:22:31 +0000867builtin_address (name)
868 char *name;
869{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000870 current_builtin = builtin_address_internal (name, 1);
Jari Aaltof73dda02001-11-13 17:56:06 +0000871 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000872}
873
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000874/* Return the function implementing the builtin NAME, but only if it is a
875 POSIX.2 special builtin. */
Jari Aaltof73dda02001-11-13 17:56:06 +0000876sh_builtin_func_t *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000877find_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 Aaltof73dda02001-11-13 17:56:06 +0000883 (sh_builtin_func_t *)NULL);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000884}
885
Jari Aalto726f6381996-08-26 18:22:31 +0000886static int
887shell_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. */
900void
901initialize_shell_builtins ()
902{
Jari Aaltocce855b1998-04-17 19:52:44 +0000903 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
Jari Aaltobb706242000-03-17 21:46:59 +0000904 (QSFUNC *)shell_builtin_compare);
Jari Aalto726f6381996-08-26 18:22:31 +0000905}