1 /* common.c - utility functions for all builtins */
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
31 #include <chartypes.h>
32 #include "../bashtypes.h"
33 #include "posixstat.h"
38 #if defined (PREFER_STDARG)
44 #include "../bashansi.h"
45 #include "../bashintl.h"
47 #define NEED_FPURGE_DECL
53 #include "../builtins.h"
55 #include "../execute_cmd.h"
57 #include "bashgetopt.h"
60 #include <tilde/tilde.h>
63 # include "../bashhist.h"
70 extern int indirection_level, subshell_environment;
71 extern int line_number;
72 extern int last_command_exit_value;
73 extern int running_trap;
74 extern int posixly_correct;
75 extern char *this_command_name, *shell_name;
76 extern const char * const bash_getcwd_errstr;
78 /* Used by some builtins and the mainline code. */
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;
82 /* **************************************************************** */
84 /* Error reporting, usage, and option processing */
86 /* **************************************************************** */
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
93 builtin_error_prolog ()
97 name = get_name_for_error ();
98 fprintf (stderr, "%s: ", name);
100 if (interactive_shell == 0)
101 fprintf (stderr, _("line %d: "), executing_line_number ());
103 if (this_command_name && *this_command_name)
104 fprintf (stderr, "%s: ", this_command_name);
108 #if defined (PREFER_STDARG)
109 builtin_error (const char *format, ...)
111 builtin_error (format, va_alist)
118 builtin_error_prolog ();
120 SH_VA_START (args, format);
122 vfprintf (stderr, format, args);
124 fprintf (stderr, "\n");
128 #if defined (PREFER_STDARG)
129 builtin_warning (const char *format, ...)
131 builtin_warning (format, va_alist)
138 builtin_error_prolog ();
139 fprintf (stderr, _("warning: "));
141 SH_VA_START (args, format);
143 vfprintf (stderr, format, args);
145 fprintf (stderr, "\n");
148 /* Print a usage summary for the currently-executing builtin command. */
152 if (this_command_name && *this_command_name)
153 fprintf (stderr, _("%s: usage: "), this_command_name);
154 fprintf (stderr, "%s\n", current_builtin->short_doc);
158 /* Return if LIST is NULL else barf and jump to top_level. Used by some
159 builtins that do not accept arguments. */
166 builtin_error (_("too many arguments"));
167 top_level_cleanup ();
168 jump_to_top_level (DISCARD);
172 /* Check that no options were given to the currently-executing builtin,
173 and return 0 if there were options. */
178 reset_internal_getopt ();
179 if (internal_getopt (list, "") != -1)
191 builtin_error (_("%s: option requires an argument"), s);
198 builtin_error (_("%s: numeric argument required"), s);
205 builtin_error (_("%s: not found"), s);
208 /* Function called when one of the builtin commands detects an invalid
214 builtin_error (_("%s: invalid option"), s);
218 sh_invalidoptname (s)
221 builtin_error (_("%s: invalid option name"), s);
228 builtin_error (_("`%s': not a valid identifier"), s);
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");
242 msg = _("invalid number");
243 builtin_error ("%s: %s", s, msg);
250 builtin_error (_("%s: invalid signal specification"), s);
257 builtin_error (_("`%s': not a pid or valid job spec"), s);
264 builtin_error (_("%s: readonly variable"), s);
272 builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
274 builtin_error (_("%s out of range"), desc ? desc : _("argument"));
277 #if defined (JOB_CONTROL)
282 builtin_error (_("%s: no such job"), s);
290 builtin_error (_("%s: no job control"), s);
292 builtin_error (_("no job control"));
296 #if defined (RESTRICTED_SHELL)
302 builtin_error (_("%s: restricted"), s);
304 builtin_error (_("restricted"));
312 builtin_error (_("%s: not a shell builtin"), s);
318 #if defined (DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS) && defined (EPIPE)
320 #endif /* DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS && EPIPE */
321 builtin_error (_("write error: %s"), strerror (errno));
329 builtin_error (_("error setting terminal attributes: %s"), strerror (errno));
331 builtin_error (_("error getting terminal attributes: %s"), strerror (errno));
344 return (EXECUTION_FAILURE);
349 /* **************************************************************** */
351 /* Shell positional parameter manipulation */
353 /* **************************************************************** */
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'. */
359 make_builtin_argv (list, ip)
365 argv = strvec_from_word_list (list, 0, 1, ip);
366 argv[0] = this_command_name;
370 /* Remember LIST in $0 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
371 non-zero, then discard whatever the existing arguments are, else
372 only discard the ones that are to be replaced. */
374 remember_args (list, destructive)
380 for (i = 1; i < 10; i++)
382 if ((destructive || list) && dollar_vars[i])
384 free (dollar_vars[i]);
385 dollar_vars[i] = (char *)NULL;
390 dollar_vars[i] = savestring (list->word->word);
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)
400 dispose_words (rest_of_args);
401 rest_of_args = copy_word_list (list);
405 set_dollar_vars_changed ();
408 static int changed_dollar_vars;
410 /* Have the dollar variables been reset to new values since we last
413 dollar_vars_changed ()
415 return (changed_dollar_vars);
419 set_dollar_vars_unchanged ()
421 changed_dollar_vars = 0;
425 set_dollar_vars_changed ()
427 if (variable_context)
428 changed_dollar_vars |= ARGS_FUNC;
429 else if (this_shell_builtin == set_builtin)
430 changed_dollar_vars |= ARGS_SETBLTIN;
432 changed_dollar_vars |= ARGS_INVOC;
435 /* **************************************************************** */
437 /* Validating numeric input and arguments */
439 /* **************************************************************** */
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
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. */
449 get_numeric_arg (list, fatal, count)
459 if (list && list->word && ISOPTION (list->word->word, '-'))
464 arg = list->word->word;
465 if (arg == 0 || (legal_number (arg, count) == 0))
467 sh_neednumarg (list->word->word ? list->word->word : "`'");
470 else if (fatal == 1) /* fatal == 1; abort */
471 throw_to_top_level ();
472 else /* fatal == 2; discard current command */
474 top_level_cleanup ();
475 jump_to_top_level (DISCARD);
478 no_args (list->next);
484 /* Get an eight-bit status value from LIST */
493 if (list && list->word && ISOPTION (list->word->word, '-'))
497 return (last_command_exit_value);
499 arg = list->word->word;
500 if (arg == 0 || legal_number (arg, &sval) == 0)
502 sh_neednumarg (list->word->word ? list->word->word : "`'");
505 no_args (list->next);
511 /* Return the octal number parsed from STRING, or -1 to indicate
512 that the string contained a bad number. */
520 while (*string && ISOCTAL (*string))
523 result = (result * 8) + (*string++ - '0');
528 if (digits == 0 || *string)
534 /* **************************************************************** */
536 /* Manipulating the current working directory */
538 /* **************************************************************** */
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;
545 get_working_directory (for_whom)
548 if (no_symbolic_links)
550 FREE (the_current_working_directory);
551 the_current_working_directory = (char *)NULL;
554 if (the_current_working_directory == 0)
556 #if defined (GETCWD_BROKEN)
557 the_current_working_directory = getcwd (0, PATH_MAX);
559 the_current_working_directory = getcwd (0, 0);
561 if (the_current_working_directory == 0)
563 fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
564 (for_whom && *for_whom) ? for_whom : get_name_for_error (),
565 _(bash_getcwd_errstr), strerror (errno));
570 return (savestring (the_current_working_directory));
573 /* Make NAME our internal idea of the current working directory. */
575 set_working_directory (name)
578 FREE (the_current_working_directory);
579 the_current_working_directory = savestring (name);
582 /* **************************************************************** */
584 /* Job control support functions */
586 /* **************************************************************** */
588 #if defined (JOB_CONTROL)
590 get_job_by_name (name, flags)
594 register int i, wl, cl, match, job;
600 for (i = js.j_jobslots - 1; i >= 0; i--)
602 j = get_job_by_jid (i);
603 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
609 if (flags & JM_EXACT)
611 cl = strlen (p->command);
612 match = STREQN (p->command, name, cl);
614 else if (flags & JM_SUBSTRING)
615 match = strcasestr (p->command, name) != (char *)0;
617 match = STREQN (p->command, name, wl);
624 else if (flags & JM_FIRSTMATCH)
625 return i; /* return first match */
626 else if (job != NO_JOB)
628 if (this_shell_builtin)
629 builtin_error (_("%s: ambiguous job spec"), name);
631 report_error (_("%s: ambiguous job spec"), name);
637 while (p != j->pipe);
643 /* Return the job spec found in LIST. */
652 return (js.j_current);
654 word = list->word->word;
662 if (DIGIT (*word) && all_digits (word))
665 return (job > js.j_jobslots ? NO_JOB : job - 1);
674 return (js.j_current);
677 return (js.j_previous);
679 case '?': /* Substring search requested. */
680 jflags |= JM_SUBSTRING;
685 return get_job_by_name (word, jflags);
688 #endif /* JOB_CONTROL */
691 * NOTE: `kill' calls this function with forcecols == 0
694 display_signal_list (list, forcecols)
698 register int i, column;
700 int result, signum, dflags;
703 result = EXECUTION_SUCCESS;
706 for (i = 1, column = 0; i < NSIG; i++)
708 name = signal_name (i);
709 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
712 if (posixly_correct && !forcecols)
714 /* This is for the kill builtin. POSIX.2 says the signal names
715 are displayed without the `SIG' prefix. */
716 if (STREQN (name, "SIG", 3))
718 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
722 printf ("%2d) %s", i, name);
734 if ((posixly_correct && !forcecols) || column != 0)
739 /* List individual signal names or numbers. */
742 if (legal_number (list->word->word, &lsignum))
744 /* This is specified by Posix.2 so that exit statuses can be
745 mapped into signal numbers. */
748 if (lsignum < 0 || lsignum >= NSIG)
750 sh_invalidsig (list->word->word);
751 result = EXECUTION_FAILURE;
757 name = signal_name (signum);
758 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
763 #if defined (JOB_CONTROL)
764 /* POSIX.2 says that `kill -l signum' prints the signal name without
766 printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
768 printf ("%s\n", name);
773 dflags = DSIG_NOCASE;
774 if (posixly_correct == 0 || this_shell_builtin != kill_builtin)
775 dflags |= DSIG_SIGPREFIX;
776 signum = decode_signal (list->word->word, dflags);
777 if (signum == NO_SIG)
779 sh_invalidsig (list->word->word);
780 result = EXECUTION_FAILURE;
784 printf ("%d\n", signum);
791 /* **************************************************************** */
793 /* Finding builtin commands and their functions */
795 /* **************************************************************** */
797 /* Perform a binary search and return the address of the builtin function
798 whose name is NAME. If the function couldn't be found, or the builtin
799 is disabled or has no function associated with it, return NULL.
800 Return the address of the builtin.
801 DISABLED_OKAY means find it even if the builtin is disabled. */
803 builtin_address_internal (name, disabled_okay)
809 hi = num_shell_builtins - 1;
816 j = shell_builtins[mid].name[0] - name[0];
819 j = strcmp (shell_builtins[mid].name, name);
823 /* It must have a function pointer. It must be enabled, or we
824 must have explicitly allowed disabled functions to be found,
825 and it must not have been deleted. */
826 if (shell_builtins[mid].function &&
827 ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
828 ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
829 return (&shell_builtins[mid]);
831 return ((struct builtin *)NULL);
838 return ((struct builtin *)NULL);
841 /* Return the pointer to the function implementing builtin command NAME. */
843 find_shell_builtin (name)
846 current_builtin = builtin_address_internal (name, 0);
847 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
850 /* Return the address of builtin with NAME, whether it is enabled or not. */
852 builtin_address (name)
855 current_builtin = builtin_address_internal (name, 1);
856 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
859 /* Return the function implementing the builtin NAME, but only if it is a
860 POSIX.2 special builtin. */
862 find_special_builtin (name)
865 current_builtin = builtin_address_internal (name, 0);
866 return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ?
867 current_builtin->function :
868 (sh_builtin_func_t *)NULL);
872 shell_builtin_compare (sbp1, sbp2)
873 struct builtin *sbp1, *sbp2;
877 if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
878 result = strcmp (sbp1->name, sbp2->name);
883 /* Sort the table of shell builtins so that the binary search will work
884 in find_shell_builtin. */
886 initialize_shell_builtins ()
888 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
889 (QSFUNC *)shell_builtin_compare);