1 /* Copyright (C) 1987-2007 Free Software Foundation, Inc.
3 This file is part of GNU Bash, the Bourne Again SHell.
5 Bash is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
10 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with Bash; see the file COPYING. If not, write to the Free Software
17 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 #if defined (HAVE_UNISTD_H)
23 # include <sys/types.h>
29 #include <chartypes.h>
30 #include "../bashtypes.h"
31 #include "posixstat.h"
36 #if defined (PREFER_STDARG)
42 #include "../bashansi.h"
43 #include "../bashintl.h"
49 #include "../builtins.h"
51 #include "../execute_cmd.h"
53 #include "bashgetopt.h"
56 #include <tilde/tilde.h>
59 # include "../bashhist.h"
66 extern int indirection_level, subshell_environment;
67 extern int line_number;
68 extern int last_command_exit_value;
69 extern int running_trap;
70 extern int posixly_correct;
71 extern char *this_command_name, *shell_name;
72 extern char *bash_getcwd_errstr;
74 /* Used by some builtins and the mainline code. */
75 sh_builtin_func_t *last_shell_builtin = (sh_builtin_func_t *)NULL;
76 sh_builtin_func_t *this_shell_builtin = (sh_builtin_func_t *)NULL;
78 /* **************************************************************** */
80 /* Error reporting, usage, and option processing */
82 /* **************************************************************** */
84 /* This is a lot like report_error (), but it is for shell builtins
85 instead of shell control structures, and it won't ever exit the
88 #if defined (PREFER_STDARG)
89 builtin_error (const char *format, ...)
91 builtin_error (format, va_alist)
99 name = get_name_for_error ();
100 fprintf (stderr, "%s: ", name);
102 if (interactive_shell == 0)
103 fprintf (stderr, "line %d: ", executing_line_number ());
105 if (this_command_name && *this_command_name)
106 fprintf (stderr, "%s: ", this_command_name);
108 SH_VA_START (args, format);
110 vfprintf (stderr, format, args);
112 fprintf (stderr, "\n");
115 /* Print a usage summary for the currently-executing builtin command. */
119 if (this_command_name && *this_command_name)
120 fprintf (stderr, "%s: usage: ", this_command_name);
121 fprintf (stderr, "%s\n", current_builtin->short_doc);
125 /* Return if LIST is NULL else barf and jump to top_level. Used by some
126 builtins that do not accept arguments. */
133 builtin_error (_("too many arguments"));
134 top_level_cleanup ();
135 jump_to_top_level (DISCARD);
139 /* Check that no options were given to the currently-executing builtin,
140 and return 0 if there were options. */
145 reset_internal_getopt ();
146 if (internal_getopt (list, "") != -1)
158 builtin_error (_("%s: option requires an argument"), s);
165 builtin_error (_("%s: numeric argument required"), s);
172 builtin_error (_("%s: not found"), s);
175 /* Function called when one of the builtin commands detects an invalid
181 builtin_error (_("%s: invalid option"), s);
185 sh_invalidoptname (s)
188 builtin_error (_("%s: invalid option name"), s);
195 builtin_error (_("`%s': not a valid identifier"), s);
202 builtin_error (_("%s: invalid number"), s);
209 builtin_error (_("%s: invalid signal specification"), s);
216 builtin_error (_("`%s': not a pid or valid job spec"), s);
223 builtin_error (_("%s: readonly variable"), s);
231 builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
233 builtin_error (_("%s out of range"), desc ? desc : _("argument"));
236 #if defined (JOB_CONTROL)
241 builtin_error (_("%s: no such job"), s);
249 builtin_error (_("%s: no job control"), s);
251 builtin_error (_("no job control"));
255 #if defined (RESTRICTED_SHELL)
261 builtin_error (_("%s: restricted"), s);
263 builtin_error (_("restricted"));
271 builtin_error (_("%s: not a shell builtin"), s);
277 builtin_error (_("write error: %s"), strerror (errno));
280 /* **************************************************************** */
282 /* Shell positional parameter manipulation */
284 /* **************************************************************** */
286 /* Convert a WORD_LIST into a C-style argv. Return the number of elements
287 in the list in *IP, if IP is non-null. A convenience function for
288 loadable builtins; also used by `test'. */
290 make_builtin_argv (list, ip)
296 argv = strvec_from_word_list (list, 0, 1, ip);
297 argv[0] = this_command_name;
301 /* Remember LIST in $0 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
302 non-zero, then discard whatever the existing arguments are, else
303 only discard the ones that are to be replaced. */
305 remember_args (list, destructive)
311 for (i = 1; i < 10; i++)
313 if ((destructive || list) && dollar_vars[i])
315 free (dollar_vars[i]);
316 dollar_vars[i] = (char *)NULL;
321 dollar_vars[i] = savestring (list->word->word);
326 /* If arguments remain, assign them to REST_OF_ARGS.
327 Note that copy_word_list (NULL) returns NULL, and
328 that dispose_words (NULL) does nothing. */
329 if (destructive || list)
331 dispose_words (rest_of_args);
332 rest_of_args = copy_word_list (list);
336 set_dollar_vars_changed ();
339 static int changed_dollar_vars;
341 /* Have the dollar variables been reset to new values since we last
344 dollar_vars_changed ()
346 return (changed_dollar_vars);
350 set_dollar_vars_unchanged ()
352 changed_dollar_vars = 0;
356 set_dollar_vars_changed ()
358 if (variable_context)
359 changed_dollar_vars |= ARGS_FUNC;
360 else if (this_shell_builtin == set_builtin)
361 changed_dollar_vars |= ARGS_SETBLTIN;
363 changed_dollar_vars |= ARGS_INVOC;
366 /* **************************************************************** */
368 /* Validating numeric input and arguments */
370 /* **************************************************************** */
372 /* Read a numeric arg for this_command_name, the name of the shell builtin
373 that wants it. LIST is the word list that the arg is to come from.
374 Accept only the numeric argument; report an error if other arguments
375 follow. If FATAL is true, call throw_to_top_level, which exits the
376 shell; if not, call jump_to_top_level (DISCARD), which aborts the
379 get_numeric_arg (list, fatal)
385 if (list && list->word && ISOPTION (list->word->word, '-'))
392 arg = list->word->word;
393 if (arg == 0 || (legal_number (arg, &count) == 0))
395 sh_neednumarg (list->word->word);
397 throw_to_top_level ();
400 top_level_cleanup ();
401 jump_to_top_level (DISCARD);
404 no_args (list->next);
410 /* Get an eight-bit status value from LIST */
419 if (list && list->word && ISOPTION (list->word->word, '-'))
423 return (last_command_exit_value);
425 arg = list->word->word;
426 if (arg == 0 || legal_number (arg, &sval) == 0)
428 sh_neednumarg (list->word->word ? list->word->word : "`'");
431 no_args (list->next);
437 /* Return the octal number parsed from STRING, or -1 to indicate
438 that the string contained a bad number. */
446 while (*string && ISOCTAL (*string))
449 result = (result * 8) + (*string++ - '0');
454 if (digits == 0 || *string)
460 /* **************************************************************** */
462 /* Manipulating the current working directory */
464 /* **************************************************************** */
466 /* Return a consed string which is the current working directory.
467 FOR_WHOM is the name of the caller for error printing. */
468 char *the_current_working_directory = (char *)NULL;
471 get_working_directory (for_whom)
474 if (no_symbolic_links)
476 FREE (the_current_working_directory);
477 the_current_working_directory = (char *)NULL;
480 if (the_current_working_directory == 0)
482 #if defined (GETCWD_BROKEN)
483 the_current_working_directory = getcwd (0, PATH_MAX);
485 the_current_working_directory = getcwd (0, 0);
487 if (the_current_working_directory == 0)
489 fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
490 (for_whom && *for_whom) ? for_whom : get_name_for_error (),
491 _(bash_getcwd_errstr), strerror (errno));
496 return (savestring (the_current_working_directory));
499 /* Make NAME our internal idea of the current working directory. */
501 set_working_directory (name)
504 FREE (the_current_working_directory);
505 the_current_working_directory = savestring (name);
508 /* **************************************************************** */
510 /* Job control support functions */
512 /* **************************************************************** */
514 #if defined (JOB_CONTROL)
516 get_job_by_name (name, flags)
520 register int i, wl, cl, match, job;
526 for (i = js.j_jobslots - 1; i >= 0; i--)
528 j = get_job_by_jid (i);
529 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
535 if (flags & JM_EXACT)
537 cl = strlen (p->command);
538 match = STREQN (p->command, name, cl);
540 else if (flags & JM_SUBSTRING)
541 match = strindex (p->command, name) != (char *)0;
543 match = STREQN (p->command, name, wl);
550 else if (flags & JM_FIRSTMATCH)
551 return i; /* return first match */
552 else if (job != NO_JOB)
554 if (this_shell_builtin)
555 builtin_error (_("%s: ambiguous job spec"), name);
557 report_error (_("%s: ambiguous job spec"), name);
563 while (p != j->pipe);
569 /* Return the job spec found in LIST. */
578 return (js.j_current);
580 word = list->word->word;
588 if (DIGIT (*word) && all_digits (word))
591 return (job > js.j_jobslots ? NO_JOB : job - 1);
600 return (js.j_current);
603 return (js.j_previous);
605 case '?': /* Substring search requested. */
606 jflags |= JM_SUBSTRING;
611 return get_job_by_name (word, jflags);
614 #endif /* JOB_CONTROL */
617 * NOTE: `kill' calls this function with forcecols == 0
620 display_signal_list (list, forcecols)
624 register int i, column;
626 int result, signum, dflags;
629 result = EXECUTION_SUCCESS;
632 for (i = 1, column = 0; i < NSIG; i++)
634 name = signal_name (i);
635 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
638 if (posixly_correct && !forcecols)
640 /* This is for the kill builtin. POSIX.2 says the signal names
641 are displayed without the `SIG' prefix. */
642 if (STREQN (name, "SIG", 3))
644 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
648 printf ("%2d) %s", i, name);
660 if ((posixly_correct && !forcecols) || column != 0)
665 /* List individual signal names or numbers. */
668 if (legal_number (list->word->word, &lsignum))
670 /* This is specified by Posix.2 so that exit statuses can be
671 mapped into signal numbers. */
674 if (lsignum < 0 || lsignum >= NSIG)
676 sh_invalidsig (list->word->word);
677 result = EXECUTION_FAILURE;
683 name = signal_name (signum);
684 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
689 #if defined (JOB_CONTROL)
690 /* POSIX.2 says that `kill -l signum' prints the signal name without
692 printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
694 printf ("%s\n", name);
699 dflags = DSIG_NOCASE;
700 if (posixly_correct == 0 || this_shell_builtin != kill_builtin)
701 dflags |= DSIG_SIGPREFIX;
702 signum = decode_signal (list->word->word, dflags);
703 if (signum == NO_SIG)
705 sh_invalidsig (list->word->word);
706 result = EXECUTION_FAILURE;
710 printf ("%d\n", signum);
717 /* **************************************************************** */
719 /* Finding builtin commands and their functions */
721 /* **************************************************************** */
723 /* Perform a binary search and return the address of the builtin function
724 whose name is NAME. If the function couldn't be found, or the builtin
725 is disabled or has no function associated with it, return NULL.
726 Return the address of the builtin.
727 DISABLED_OKAY means find it even if the builtin is disabled. */
729 builtin_address_internal (name, disabled_okay)
735 hi = num_shell_builtins - 1;
742 j = shell_builtins[mid].name[0] - name[0];
745 j = strcmp (shell_builtins[mid].name, name);
749 /* It must have a function pointer. It must be enabled, or we
750 must have explicitly allowed disabled functions to be found,
751 and it must not have been deleted. */
752 if (shell_builtins[mid].function &&
753 ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
754 ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
755 return (&shell_builtins[mid]);
757 return ((struct builtin *)NULL);
764 return ((struct builtin *)NULL);
767 /* Return the pointer to the function implementing builtin command NAME. */
769 find_shell_builtin (name)
772 current_builtin = builtin_address_internal (name, 0);
773 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
776 /* Return the address of builtin with NAME, whether it is enabled or not. */
778 builtin_address (name)
781 current_builtin = builtin_address_internal (name, 1);
782 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
785 /* Return the function implementing the builtin NAME, but only if it is a
786 POSIX.2 special builtin. */
788 find_special_builtin (name)
791 current_builtin = builtin_address_internal (name, 0);
792 return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ?
793 current_builtin->function :
794 (sh_builtin_func_t *)NULL);
798 shell_builtin_compare (sbp1, sbp2)
799 struct builtin *sbp1, *sbp2;
803 if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
804 result = strcmp (sbp1->name, sbp2->name);
809 /* Sort the table of shell builtins so that the binary search will work
810 in find_shell_builtin. */
812 initialize_shell_builtins ()
814 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
815 (QSFUNC *)shell_builtin_compare);