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));
334 return (EXECUTION_FAILURE);
339 /* **************************************************************** */
341 /* Shell positional parameter manipulation */
343 /* **************************************************************** */
345 /* Convert a WORD_LIST into a C-style argv. Return the number of elements
346 in the list in *IP, if IP is non-null. A convenience function for
347 loadable builtins; also used by `test'. */
349 make_builtin_argv (list, ip)
355 argv = strvec_from_word_list (list, 0, 1, ip);
356 argv[0] = this_command_name;
360 /* Remember LIST in $0 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
361 non-zero, then discard whatever the existing arguments are, else
362 only discard the ones that are to be replaced. */
364 remember_args (list, destructive)
370 for (i = 1; i < 10; i++)
372 if ((destructive || list) && dollar_vars[i])
374 free (dollar_vars[i]);
375 dollar_vars[i] = (char *)NULL;
380 dollar_vars[i] = savestring (list->word->word);
385 /* If arguments remain, assign them to REST_OF_ARGS.
386 Note that copy_word_list (NULL) returns NULL, and
387 that dispose_words (NULL) does nothing. */
388 if (destructive || list)
390 dispose_words (rest_of_args);
391 rest_of_args = copy_word_list (list);
395 set_dollar_vars_changed ();
398 static int changed_dollar_vars;
400 /* Have the dollar variables been reset to new values since we last
403 dollar_vars_changed ()
405 return (changed_dollar_vars);
409 set_dollar_vars_unchanged ()
411 changed_dollar_vars = 0;
415 set_dollar_vars_changed ()
417 if (variable_context)
418 changed_dollar_vars |= ARGS_FUNC;
419 else if (this_shell_builtin == set_builtin)
420 changed_dollar_vars |= ARGS_SETBLTIN;
422 changed_dollar_vars |= ARGS_INVOC;
425 /* **************************************************************** */
427 /* Validating numeric input and arguments */
429 /* **************************************************************** */
431 /* Read a numeric arg for this_command_name, the name of the shell builtin
432 that wants it. LIST is the word list that the arg is to come from.
433 Accept only the numeric argument; report an error if other arguments
434 follow. If FATAL is 1, call throw_to_top_level, which exits the
435 shell; if it's 2, call jump_to_top_level (DISCARD), which aborts the
436 current command; if FATAL is 0, return an indication of an invalid
437 number by setting *NUMOK == 0 and return -1. */
439 get_numeric_arg (list, fatal, count)
449 if (list && list->word && ISOPTION (list->word->word, '-'))
454 arg = list->word->word;
455 if (arg == 0 || (legal_number (arg, count) == 0))
457 sh_neednumarg (list->word->word ? list->word->word : "`'");
460 else if (fatal == 1) /* fatal == 1; abort */
461 throw_to_top_level ();
462 else /* fatal == 2; discard current command */
464 top_level_cleanup ();
465 jump_to_top_level (DISCARD);
468 no_args (list->next);
474 /* Get an eight-bit status value from LIST */
483 if (list && list->word && ISOPTION (list->word->word, '-'))
487 return (last_command_exit_value);
489 arg = list->word->word;
490 if (arg == 0 || legal_number (arg, &sval) == 0)
492 sh_neednumarg (list->word->word ? list->word->word : "`'");
495 no_args (list->next);
501 /* Return the octal number parsed from STRING, or -1 to indicate
502 that the string contained a bad number. */
510 while (*string && ISOCTAL (*string))
513 result = (result * 8) + (*string++ - '0');
518 if (digits == 0 || *string)
524 /* **************************************************************** */
526 /* Manipulating the current working directory */
528 /* **************************************************************** */
530 /* Return a consed string which is the current working directory.
531 FOR_WHOM is the name of the caller for error printing. */
532 char *the_current_working_directory = (char *)NULL;
535 get_working_directory (for_whom)
538 if (no_symbolic_links)
540 FREE (the_current_working_directory);
541 the_current_working_directory = (char *)NULL;
544 if (the_current_working_directory == 0)
546 #if defined (GETCWD_BROKEN)
547 the_current_working_directory = getcwd (0, PATH_MAX);
549 the_current_working_directory = getcwd (0, 0);
551 if (the_current_working_directory == 0)
553 fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
554 (for_whom && *for_whom) ? for_whom : get_name_for_error (),
555 _(bash_getcwd_errstr), strerror (errno));
560 return (savestring (the_current_working_directory));
563 /* Make NAME our internal idea of the current working directory. */
565 set_working_directory (name)
568 FREE (the_current_working_directory);
569 the_current_working_directory = savestring (name);
572 /* **************************************************************** */
574 /* Job control support functions */
576 /* **************************************************************** */
578 #if defined (JOB_CONTROL)
580 get_job_by_name (name, flags)
584 register int i, wl, cl, match, job;
590 for (i = js.j_jobslots - 1; i >= 0; i--)
592 j = get_job_by_jid (i);
593 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
599 if (flags & JM_EXACT)
601 cl = strlen (p->command);
602 match = STREQN (p->command, name, cl);
604 else if (flags & JM_SUBSTRING)
605 match = strindex (p->command, name) != (char *)0;
607 match = STREQN (p->command, name, wl);
614 else if (flags & JM_FIRSTMATCH)
615 return i; /* return first match */
616 else if (job != NO_JOB)
618 if (this_shell_builtin)
619 builtin_error (_("%s: ambiguous job spec"), name);
621 report_error (_("%s: ambiguous job spec"), name);
627 while (p != j->pipe);
633 /* Return the job spec found in LIST. */
642 return (js.j_current);
644 word = list->word->word;
652 if (DIGIT (*word) && all_digits (word))
655 return (job > js.j_jobslots ? NO_JOB : job - 1);
664 return (js.j_current);
667 return (js.j_previous);
669 case '?': /* Substring search requested. */
670 jflags |= JM_SUBSTRING;
675 return get_job_by_name (word, jflags);
678 #endif /* JOB_CONTROL */
681 * NOTE: `kill' calls this function with forcecols == 0
684 display_signal_list (list, forcecols)
688 register int i, column;
690 int result, signum, dflags;
693 result = EXECUTION_SUCCESS;
696 for (i = 1, column = 0; i < NSIG; i++)
698 name = signal_name (i);
699 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
702 if (posixly_correct && !forcecols)
704 /* This is for the kill builtin. POSIX.2 says the signal names
705 are displayed without the `SIG' prefix. */
706 if (STREQN (name, "SIG", 3))
708 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
712 printf ("%2d) %s", i, name);
724 if ((posixly_correct && !forcecols) || column != 0)
729 /* List individual signal names or numbers. */
732 if (legal_number (list->word->word, &lsignum))
734 /* This is specified by Posix.2 so that exit statuses can be
735 mapped into signal numbers. */
738 if (lsignum < 0 || lsignum >= NSIG)
740 sh_invalidsig (list->word->word);
741 result = EXECUTION_FAILURE;
747 name = signal_name (signum);
748 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
753 #if defined (JOB_CONTROL)
754 /* POSIX.2 says that `kill -l signum' prints the signal name without
756 printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
758 printf ("%s\n", name);
763 dflags = DSIG_NOCASE;
764 if (posixly_correct == 0 || this_shell_builtin != kill_builtin)
765 dflags |= DSIG_SIGPREFIX;
766 signum = decode_signal (list->word->word, dflags);
767 if (signum == NO_SIG)
769 sh_invalidsig (list->word->word);
770 result = EXECUTION_FAILURE;
774 printf ("%d\n", signum);
781 /* **************************************************************** */
783 /* Finding builtin commands and their functions */
785 /* **************************************************************** */
787 /* Perform a binary search and return the address of the builtin function
788 whose name is NAME. If the function couldn't be found, or the builtin
789 is disabled or has no function associated with it, return NULL.
790 Return the address of the builtin.
791 DISABLED_OKAY means find it even if the builtin is disabled. */
793 builtin_address_internal (name, disabled_okay)
799 hi = num_shell_builtins - 1;
806 j = shell_builtins[mid].name[0] - name[0];
809 j = strcmp (shell_builtins[mid].name, name);
813 /* It must have a function pointer. It must be enabled, or we
814 must have explicitly allowed disabled functions to be found,
815 and it must not have been deleted. */
816 if (shell_builtins[mid].function &&
817 ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
818 ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
819 return (&shell_builtins[mid]);
821 return ((struct builtin *)NULL);
828 return ((struct builtin *)NULL);
831 /* Return the pointer to the function implementing builtin command NAME. */
833 find_shell_builtin (name)
836 current_builtin = builtin_address_internal (name, 0);
837 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
840 /* Return the address of builtin with NAME, whether it is enabled or not. */
842 builtin_address (name)
845 current_builtin = builtin_address_internal (name, 1);
846 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
849 /* Return the function implementing the builtin NAME, but only if it is a
850 POSIX.2 special builtin. */
852 find_special_builtin (name)
855 current_builtin = builtin_address_internal (name, 0);
856 return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ?
857 current_builtin->function :
858 (sh_builtin_func_t *)NULL);
862 shell_builtin_compare (sbp1, sbp2)
863 struct builtin *sbp1, *sbp2;
867 if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
868 result = strcmp (sbp1->name, sbp2->name);
873 /* Sort the table of shell builtins so that the binary search will work
874 in find_shell_builtin. */
876 initialize_shell_builtins ()
878 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
879 (QSFUNC *)shell_builtin_compare);