1 /* Copyright (C) 1987-2005 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 jump_to_top_level (DISCARD);
138 /* Check that no options were given to the currently-executing builtin,
139 and return 0 if there were options. */
144 reset_internal_getopt ();
145 if (internal_getopt (list, "") != -1)
157 builtin_error (_("%s: option requires an argument"), s);
164 builtin_error (_("%s: numeric argument required"), s);
171 builtin_error (_("%s: not found"), s);
174 /* Function called when one of the builtin commands detects an invalid
180 builtin_error (_("%s: invalid option"), s);
184 sh_invalidoptname (s)
187 builtin_error (_("%s: invalid option name"), s);
194 builtin_error (_("`%s': not a valid identifier"), s);
201 builtin_error (_("%s: invalid number"), s);
208 builtin_error (_("%s: invalid signal specification"), s);
215 builtin_error (_("`%s': not a pid or valid job spec"), s);
222 builtin_error (_("%s: readonly variable"), s);
230 builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
232 builtin_error (_("%s out of range"), desc ? desc : _("argument"));
235 #if defined (JOB_CONTROL)
240 builtin_error (_("%s: no such job"), s);
248 builtin_error (_("%s: no job control"), s);
250 builtin_error (_("no job control"));
254 #if defined (RESTRICTED_SHELL)
260 builtin_error (_("%s: restricted"), s);
262 builtin_error (_("restricted"));
270 builtin_error (_("%s: not a shell builtin"), s);
276 builtin_error (_("write error: %s"), strerror (errno));
279 /* **************************************************************** */
281 /* Shell positional parameter manipulation */
283 /* **************************************************************** */
285 /* Convert a WORD_LIST into a C-style argv. Return the number of elements
286 in the list in *IP, if IP is non-null. A convenience function for
287 loadable builtins; also used by `test'. */
289 make_builtin_argv (list, ip)
295 argv = strvec_from_word_list (list, 0, 1, ip);
296 argv[0] = this_command_name;
300 /* Remember LIST in $0 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
301 non-zero, then discard whatever the existing arguments are, else
302 only discard the ones that are to be replaced. */
304 remember_args (list, destructive)
310 for (i = 1; i < 10; i++)
312 if ((destructive || list) && dollar_vars[i])
314 free (dollar_vars[i]);
315 dollar_vars[i] = (char *)NULL;
320 dollar_vars[i] = savestring (list->word->word);
325 /* If arguments remain, assign them to REST_OF_ARGS.
326 Note that copy_word_list (NULL) returns NULL, and
327 that dispose_words (NULL) does nothing. */
328 if (destructive || list)
330 dispose_words (rest_of_args);
331 rest_of_args = copy_word_list (list);
335 set_dollar_vars_changed ();
338 static int changed_dollar_vars;
340 /* Have the dollar variables been reset to new values since we last
343 dollar_vars_changed ()
345 return (changed_dollar_vars);
349 set_dollar_vars_unchanged ()
351 changed_dollar_vars = 0;
355 set_dollar_vars_changed ()
357 if (variable_context)
358 changed_dollar_vars |= ARGS_FUNC;
359 else if (this_shell_builtin == set_builtin)
360 changed_dollar_vars |= ARGS_SETBLTIN;
362 changed_dollar_vars |= ARGS_INVOC;
365 /* **************************************************************** */
367 /* Validating numeric input and arguments */
369 /* **************************************************************** */
371 /* Read a numeric arg for this_command_name, the name of the shell builtin
372 that wants it. LIST is the word list that the arg is to come from.
373 Accept only the numeric argument; report an error if other arguments
374 follow. If FATAL is true, call throw_to_top_level, which exits the
375 shell; if not, call jump_to_top_level (DISCARD), which aborts the
378 get_numeric_arg (list, fatal)
384 if (list && list->word && ISOPTION (list->word->word, '-'))
391 arg = list->word->word;
392 if (arg == 0 || (legal_number (arg, &count) == 0))
394 sh_neednumarg (list->word->word);
396 throw_to_top_level ();
398 jump_to_top_level (DISCARD);
400 no_args (list->next);
406 /* Get an eight-bit status value from LIST */
415 if (list && list->word && ISOPTION (list->word->word, '-'))
419 return (last_command_exit_value);
421 arg = list->word->word;
422 if (arg == 0 || legal_number (arg, &sval) == 0)
424 sh_neednumarg (list->word->word ? list->word->word : "`'");
427 no_args (list->next);
433 /* Return the octal number parsed from STRING, or -1 to indicate
434 that the string contained a bad number. */
442 while (*string && ISOCTAL (*string))
445 result = (result * 8) + (*string++ - '0');
450 if (digits == 0 || *string)
456 /* **************************************************************** */
458 /* Manipulating the current working directory */
460 /* **************************************************************** */
462 /* Return a consed string which is the current working directory.
463 FOR_WHOM is the name of the caller for error printing. */
464 char *the_current_working_directory = (char *)NULL;
467 get_working_directory (for_whom)
473 if (no_symbolic_links)
475 FREE (the_current_working_directory);
476 the_current_working_directory = (char *)NULL;
479 if (the_current_working_directory == 0)
481 the_current_working_directory = getcwd (0, 0);
482 if (the_current_working_directory == 0)
484 fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
485 (for_whom && *for_whom) ? for_whom : get_name_for_error (),
486 _(bash_getcwd_errstr), strerror (errno));
491 return (savestring (the_current_working_directory));
494 /* Make NAME our internal idea of the current working directory. */
496 set_working_directory (name)
499 FREE (the_current_working_directory);
500 the_current_working_directory = savestring (name);
503 /* **************************************************************** */
505 /* Job control support functions */
507 /* **************************************************************** */
509 #if defined (JOB_CONTROL)
511 get_job_by_name (name, flags)
515 register int i, wl, cl, match, job;
521 for (i = js.j_jobslots - 1; i >= 0; i--)
523 j = get_job_by_jid (i);
524 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
530 if (flags & JM_EXACT)
532 cl = strlen (p->command);
533 match = STREQN (p->command, name, cl);
535 else if (flags & JM_SUBSTRING)
536 match = strindex (p->command, name) != (char *)0;
538 match = STREQN (p->command, name, wl);
545 else if (flags & JM_FIRSTMATCH)
546 return i; /* return first match */
547 else if (job != NO_JOB)
549 if (this_shell_builtin)
550 builtin_error (_("%s: ambiguous job spec"), name);
552 report_error (_("%s: ambiguous job spec"), name);
558 while (p != j->pipe);
564 /* Return the job spec found in LIST. */
573 return (js.j_current);
575 word = list->word->word;
583 if (DIGIT (*word) && all_digits (word))
586 return (job > js.j_jobslots ? NO_JOB : job - 1);
595 return (js.j_current);
598 return (js.j_previous);
600 case '?': /* Substring search requested. */
601 jflags |= JM_SUBSTRING;
606 return get_job_by_name (word, jflags);
609 #endif /* JOB_CONTROL */
612 * NOTE: `kill' calls this function with forcecols == 0
615 display_signal_list (list, forcecols)
619 register int i, column;
621 int result, signum, dflags;
624 result = EXECUTION_SUCCESS;
627 for (i = 1, column = 0; i < NSIG; i++)
629 name = signal_name (i);
630 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
633 if (posixly_correct && !forcecols)
635 /* This is for the kill builtin. POSIX.2 says the signal names
636 are displayed without the `SIG' prefix. */
637 if (STREQN (name, "SIG", 3))
639 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
643 printf ("%2d) %s", i, name);
655 if ((posixly_correct && !forcecols) || column != 0)
660 /* List individual signal names or numbers. */
663 if (legal_number (list->word->word, &lsignum))
665 /* This is specified by Posix.2 so that exit statuses can be
666 mapped into signal numbers. */
669 if (lsignum < 0 || lsignum >= NSIG)
671 sh_invalidsig (list->word->word);
672 result = EXECUTION_FAILURE;
678 name = signal_name (signum);
679 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
684 #if defined (JOB_CONTROL)
685 /* POSIX.2 says that `kill -l signum' prints the signal name without
687 printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
689 printf ("%s\n", name);
694 dflags = DSIG_NOCASE;
695 if (posixly_correct == 0 || this_shell_builtin != kill_builtin)
696 dflags |= DSIG_SIGPREFIX;
697 signum = decode_signal (list->word->word, dflags);
698 if (signum == NO_SIG)
700 sh_invalidsig (list->word->word);
701 result = EXECUTION_FAILURE;
705 printf ("%d\n", signum);
712 /* **************************************************************** */
714 /* Finding builtin commands and their functions */
716 /* **************************************************************** */
718 /* Perform a binary search and return the address of the builtin function
719 whose name is NAME. If the function couldn't be found, or the builtin
720 is disabled or has no function associated with it, return NULL.
721 Return the address of the builtin.
722 DISABLED_OKAY means find it even if the builtin is disabled. */
724 builtin_address_internal (name, disabled_okay)
730 hi = num_shell_builtins - 1;
737 j = shell_builtins[mid].name[0] - name[0];
740 j = strcmp (shell_builtins[mid].name, name);
744 /* It must have a function pointer. It must be enabled, or we
745 must have explicitly allowed disabled functions to be found,
746 and it must not have been deleted. */
747 if (shell_builtins[mid].function &&
748 ((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
749 ((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
750 return (&shell_builtins[mid]);
752 return ((struct builtin *)NULL);
759 return ((struct builtin *)NULL);
762 /* Return the pointer to the function implementing builtin command NAME. */
764 find_shell_builtin (name)
767 current_builtin = builtin_address_internal (name, 0);
768 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
771 /* Return the address of builtin with NAME, whether it is enabled or not. */
773 builtin_address (name)
776 current_builtin = builtin_address_internal (name, 1);
777 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
780 /* Return the function implementing the builtin NAME, but only if it is a
781 POSIX.2 special builtin. */
783 find_special_builtin (name)
786 current_builtin = builtin_address_internal (name, 0);
787 return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ?
788 current_builtin->function :
789 (sh_builtin_func_t *)NULL);
793 shell_builtin_compare (sbp1, sbp2)
794 struct builtin *sbp1, *sbp2;
798 if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
799 result = strcmp (sbp1->name, sbp2->name);
804 /* Sort the table of shell builtins so that the binary search will work
805 in find_shell_builtin. */
807 initialize_shell_builtins ()
809 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
810 (QSFUNC *)shell_builtin_compare);