]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/common.c
Bash-4.3 patch 32
[thirdparty/bash.git] / builtins / common.c
1 /* common.c - utility functions for all builtins */
2
3 /* Copyright (C) 1987-2010 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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.
11
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.
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 */
20
21 #include <config.h>
22
23 #if defined (HAVE_UNISTD_H)
24 # ifdef _MINIX
25 # include <sys/types.h>
26 # endif
27 # include <unistd.h>
28 #endif
29
30 #include <stdio.h>
31 #include <chartypes.h>
32 #include "../bashtypes.h"
33 #include "posixstat.h"
34 #include <signal.h>
35
36 #include <errno.h>
37
38 #if defined (PREFER_STDARG)
39 # include <stdarg.h>
40 #else
41 # include <varargs.h>
42 #endif
43
44 #include "../bashansi.h"
45 #include "../bashintl.h"
46
47 #define NEED_FPURGE_DECL
48
49 #include "../shell.h"
50 #include "maxpath.h"
51 #include "../flags.h"
52 #include "../jobs.h"
53 #include "../builtins.h"
54 #include "../input.h"
55 #include "../execute_cmd.h"
56 #include "../trap.h"
57 #include "bashgetopt.h"
58 #include "common.h"
59 #include "builtext.h"
60 #include <tilde/tilde.h>
61
62 #if defined (HISTORY)
63 # include "../bashhist.h"
64 #endif
65
66 #if !defined (errno)
67 extern int errno;
68 #endif /* !errno */
69
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;
77
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;
81
82 /* **************************************************************** */
83 /* */
84 /* Error reporting, usage, and option processing */
85 /* */
86 /* **************************************************************** */
87
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. */
91
92 static void
93 builtin_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
107 void
108 #if defined (PREFER_STDARG)
109 builtin_error (const char *format, ...)
110 #else
111 builtin_error (format, va_alist)
112 const char *format;
113 va_dcl
114 #endif
115 {
116 va_list args;
117
118 builtin_error_prolog ();
119
120 SH_VA_START (args, format);
121
122 vfprintf (stderr, format, args);
123 va_end (args);
124 fprintf (stderr, "\n");
125 }
126
127 void
128 #if defined (PREFER_STDARG)
129 builtin_warning (const char *format, ...)
130 #else
131 builtin_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: "));
140
141 SH_VA_START (args, format);
142
143 vfprintf (stderr, format, args);
144 va_end (args);
145 fprintf (stderr, "\n");
146 }
147
148 /* Print a usage summary for the currently-executing builtin command. */
149 void
150 builtin_usage ()
151 {
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));
155 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. */
160 void
161 no_args (list)
162 WORD_LIST *list;
163 {
164 if (list)
165 {
166 builtin_error (_("too many arguments"));
167 top_level_cleanup ();
168 jump_to_top_level (DISCARD);
169 }
170 }
171
172 /* Check that no options were given to the currently-executing builtin,
173 and return 0 if there were options. */
174 int
175 no_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
187 void
188 sh_needarg (s)
189 char *s;
190 {
191 builtin_error (_("%s: option requires an argument"), s);
192 }
193
194 void
195 sh_neednumarg (s)
196 char *s;
197 {
198 builtin_error (_("%s: numeric argument required"), s);
199 }
200
201 void
202 sh_notfound (s)
203 char *s;
204 {
205 builtin_error (_("%s: not found"), s);
206 }
207
208 /* Function called when one of the builtin commands detects an invalid
209 option. */
210 void
211 sh_invalidopt (s)
212 char *s;
213 {
214 builtin_error (_("%s: invalid option"), s);
215 }
216
217 void
218 sh_invalidoptname (s)
219 char *s;
220 {
221 builtin_error (_("%s: invalid option name"), s);
222 }
223
224 void
225 sh_invalidid (s)
226 char *s;
227 {
228 builtin_error (_("`%s': not a valid identifier"), s);
229 }
230
231 void
232 sh_invalidnum (s)
233 char *s;
234 {
235 char *msg;
236
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);
244 }
245
246 void
247 sh_invalidsig (s)
248 char *s;
249 {
250 builtin_error (_("%s: invalid signal specification"), s);
251 }
252
253 void
254 sh_badpid (s)
255 char *s;
256 {
257 builtin_error (_("`%s': not a pid or valid job spec"), s);
258 }
259
260 void
261 sh_readonly (s)
262 const char *s;
263 {
264 builtin_error (_("%s: readonly variable"), s);
265 }
266
267 void
268 sh_erange (s, desc)
269 char *s, *desc;
270 {
271 if (s)
272 builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
273 else
274 builtin_error (_("%s out of range"), desc ? desc : _("argument"));
275 }
276
277 #if defined (JOB_CONTROL)
278 void
279 sh_badjob (s)
280 char *s;
281 {
282 builtin_error (_("%s: no such job"), s);
283 }
284
285 void
286 sh_nojobs (s)
287 char *s;
288 {
289 if (s)
290 builtin_error (_("%s: no job control"), s);
291 else
292 builtin_error (_("no job control"));
293 }
294 #endif
295
296 #if defined (RESTRICTED_SHELL)
297 void
298 sh_restricted (s)
299 char *s;
300 {
301 if (s)
302 builtin_error (_("%s: restricted"), s);
303 else
304 builtin_error (_("restricted"));
305 }
306 #endif
307
308 void
309 sh_notbuiltin (s)
310 char *s;
311 {
312 builtin_error (_("%s: not a shell builtin"), s);
313 }
314
315 void
316 sh_wrerror ()
317 {
318 #if defined (DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS) && defined (EPIPE)
319 if (errno != EPIPE)
320 #endif /* DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS && EPIPE */
321 builtin_error (_("write error: %s"), strerror (errno));
322 }
323
324 void
325 sh_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
334 int
335 sh_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
349 /* **************************************************************** */
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'. */
358 char **
359 make_builtin_argv (list, ip)
360 WORD_LIST *list;
361 int *ip;
362 {
363 char **argv;
364
365 argv = strvec_from_word_list (list, 0, 1, ip);
366 argv[0] = this_command_name;
367 return argv;
368 }
369
370 /* Remember LIST in $1 ... $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. */
373 void
374 remember_args (list, destructive)
375 WORD_LIST *list;
376 int destructive;
377 {
378 register int i;
379
380 for (i = 1; i < 10; i++)
381 {
382 if ((destructive || list) && dollar_vars[i])
383 {
384 free (dollar_vars[i]);
385 dollar_vars[i] = (char *)NULL;
386 }
387
388 if (list)
389 {
390 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
408 static int changed_dollar_vars;
409
410 /* Have the dollar variables been reset to new values since we last
411 checked? */
412 int
413 dollar_vars_changed ()
414 {
415 return (changed_dollar_vars);
416 }
417
418 void
419 set_dollar_vars_unchanged ()
420 {
421 changed_dollar_vars = 0;
422 }
423
424 void
425 set_dollar_vars_changed ()
426 {
427 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;
433 }
434
435 /* **************************************************************** */
436 /* */
437 /* Validating numeric input and arguments */
438 /* */
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
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. */
448 int
449 get_numeric_arg (list, fatal, count)
450 WORD_LIST *list;
451 int fatal;
452 intmax_t *count;
453 {
454 char *arg;
455
456 if (count)
457 *count = 1;
458
459 if (list && list->word && ISOPTION (list->word->word, '-'))
460 list = list->next;
461
462 if (list)
463 {
464 arg = list->word->word;
465 if (arg == 0 || (legal_number (arg, count) == 0))
466 {
467 sh_neednumarg (list->word->word ? list->word->word : "`'");
468 if (fatal == 0)
469 return 0;
470 else if (fatal == 1) /* fatal == 1; abort */
471 throw_to_top_level ();
472 else /* fatal == 2; discard current command */
473 {
474 top_level_cleanup ();
475 jump_to_top_level (DISCARD);
476 }
477 }
478 no_args (list->next);
479 }
480
481 return (1);
482 }
483
484 /* Get an eight-bit status value from LIST */
485 int
486 get_exitstat (list)
487 WORD_LIST *list;
488 {
489 int status;
490 intmax_t sval;
491 char *arg;
492
493 if (list && list->word && ISOPTION (list->word->word, '-'))
494 list = list->next;
495
496 if (list == 0)
497 return (last_command_exit_value);
498
499 arg = list->word->word;
500 if (arg == 0 || legal_number (arg, &sval) == 0)
501 {
502 sh_neednumarg (list->word->word ? list->word->word : "`'");
503 return EX_BADUSAGE;
504 }
505 no_args (list->next);
506
507 status = sval & 255;
508 return status;
509 }
510
511 /* Return the octal number parsed from STRING, or -1 to indicate
512 that the string contained a bad number. */
513 int
514 read_octal (string)
515 char *string;
516 {
517 int result, digits;
518
519 result = digits = 0;
520 while (*string && ISOCTAL (*string))
521 {
522 digits++;
523 result = (result * 8) + (*string++ - '0');
524 if (result > 0777)
525 return -1;
526 }
527
528 if (digits == 0 || *string)
529 result = -1;
530
531 return (result);
532 }
533
534 /* **************************************************************** */
535 /* */
536 /* Manipulating the current working directory */
537 /* */
538 /* **************************************************************** */
539
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;
543
544 char *
545 get_working_directory (for_whom)
546 char *for_whom;
547 {
548 if (no_symbolic_links)
549 {
550 FREE (the_current_working_directory);
551 the_current_working_directory = (char *)NULL;
552 }
553
554 if (the_current_working_directory == 0)
555 {
556 #if defined (GETCWD_BROKEN)
557 the_current_working_directory = getcwd (0, PATH_MAX);
558 #else
559 the_current_working_directory = getcwd (0, 0);
560 #endif
561 if (the_current_working_directory == 0)
562 {
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));
566 return (char *)NULL;
567 }
568 }
569
570 return (savestring (the_current_working_directory));
571 }
572
573 /* Make NAME our internal idea of the current working directory. */
574 void
575 set_working_directory (name)
576 char *name;
577 {
578 FREE (the_current_working_directory);
579 the_current_working_directory = savestring (name);
580 }
581
582 /* **************************************************************** */
583 /* */
584 /* Job control support functions */
585 /* */
586 /* **************************************************************** */
587
588 #if defined (JOB_CONTROL)
589 int
590 get_job_by_name (name, flags)
591 const char *name;
592 int flags;
593 {
594 register int i, wl, cl, match, job;
595 register PROCESS *p;
596 register JOB *j;
597
598 job = NO_JOB;
599 wl = strlen (name);
600 for (i = js.j_jobslots - 1; i >= 0; i--)
601 {
602 j = get_job_by_jid (i);
603 if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
604 continue;
605
606 p = j->pipe;
607 do
608 {
609 if (flags & JM_EXACT)
610 {
611 cl = strlen (p->command);
612 match = STREQN (p->command, name, cl);
613 }
614 else if (flags & JM_SUBSTRING)
615 match = strcasestr (p->command, name) != (char *)0;
616 else
617 match = STREQN (p->command, name, wl);
618
619 if (match == 0)
620 {
621 p = p->next;
622 continue;
623 }
624 else if (flags & JM_FIRSTMATCH)
625 return i; /* return first match */
626 else if (job != NO_JOB)
627 {
628 if (this_shell_builtin)
629 builtin_error (_("%s: ambiguous job spec"), name);
630 else
631 internal_error (_("%s: ambiguous job spec"), name);
632 return (DUP_JOB);
633 }
634 else
635 job = i;
636 }
637 while (p != j->pipe);
638 }
639
640 return (job);
641 }
642
643 /* Return the job spec found in LIST. */
644 int
645 get_job_spec (list)
646 WORD_LIST *list;
647 {
648 register char *word;
649 int job, jflags;
650
651 if (list == 0)
652 return (js.j_current);
653
654 word = list->word->word;
655
656 if (*word == '\0')
657 return (NO_JOB);
658
659 if (*word == '%')
660 word++;
661
662 if (DIGIT (*word) && all_digits (word))
663 {
664 job = atoi (word);
665 return (job > js.j_jobslots ? NO_JOB : job - 1);
666 }
667
668 jflags = 0;
669 switch (*word)
670 {
671 case 0:
672 case '%':
673 case '+':
674 return (js.j_current);
675
676 case '-':
677 return (js.j_previous);
678
679 case '?': /* Substring search requested. */
680 jflags |= JM_SUBSTRING;
681 word++;
682 /* FALLTHROUGH */
683
684 default:
685 return get_job_by_name (word, jflags);
686 }
687 }
688 #endif /* JOB_CONTROL */
689
690 /*
691 * NOTE: `kill' calls this function with forcecols == 0
692 */
693 int
694 display_signal_list (list, forcecols)
695 WORD_LIST *list;
696 int forcecols;
697 {
698 register int i, column;
699 char *name;
700 int result, signum, dflags;
701 intmax_t lsignum;
702
703 result = EXECUTION_SUCCESS;
704 if (!list)
705 {
706 for (i = 1, column = 0; i < NSIG; i++)
707 {
708 name = signal_name (i);
709 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
710 continue;
711
712 if (posixly_correct && !forcecols)
713 {
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))
717 name += 3;
718 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
719 }
720 else
721 {
722 printf ("%2d) %s", i, name);
723
724 if (++column < 5)
725 printf ("\t");
726 else
727 {
728 printf ("\n");
729 column = 0;
730 }
731 }
732 }
733
734 if ((posixly_correct && !forcecols) || column != 0)
735 printf ("\n");
736 return result;
737 }
738
739 /* List individual signal names or numbers. */
740 while (list)
741 {
742 if (legal_number (list->word->word, &lsignum))
743 {
744 /* This is specified by Posix.2 so that exit statuses can be
745 mapped into signal numbers. */
746 if (lsignum > 128)
747 lsignum -= 128;
748 if (lsignum < 0 || lsignum >= NSIG)
749 {
750 sh_invalidsig (list->word->word);
751 result = EXECUTION_FAILURE;
752 list = list->next;
753 continue;
754 }
755
756 signum = lsignum;
757 name = signal_name (signum);
758 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
759 {
760 list = list->next;
761 continue;
762 }
763 #if defined (JOB_CONTROL)
764 /* POSIX.2 says that `kill -l signum' prints the signal name without
765 the `SIG' prefix. */
766 printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
767 #else
768 printf ("%s\n", name);
769 #endif
770 }
771 else
772 {
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)
778 {
779 sh_invalidsig (list->word->word);
780 result = EXECUTION_FAILURE;
781 list = list->next;
782 continue;
783 }
784 printf ("%d\n", signum);
785 }
786 list = list->next;
787 }
788 return (result);
789 }
790
791 /* **************************************************************** */
792 /* */
793 /* Finding builtin commands and their functions */
794 /* */
795 /* **************************************************************** */
796
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. */
802 struct builtin *
803 builtin_address_internal (name, disabled_okay)
804 char *name;
805 int disabled_okay;
806 {
807 int hi, lo, mid, j;
808
809 hi = num_shell_builtins - 1;
810 lo = 0;
811
812 while (lo <= hi)
813 {
814 mid = (lo + hi) / 2;
815
816 j = shell_builtins[mid].name[0] - name[0];
817
818 if (j == 0)
819 j = strcmp (shell_builtins[mid].name, name);
820
821 if (j == 0)
822 {
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]);
830 else
831 return ((struct builtin *)NULL);
832 }
833 if (j > 0)
834 hi = mid - 1;
835 else
836 lo = mid + 1;
837 }
838 return ((struct builtin *)NULL);
839 }
840
841 /* Return the pointer to the function implementing builtin command NAME. */
842 sh_builtin_func_t *
843 find_shell_builtin (name)
844 char *name;
845 {
846 current_builtin = builtin_address_internal (name, 0);
847 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
848 }
849
850 /* Return the address of builtin with NAME, whether it is enabled or not. */
851 sh_builtin_func_t *
852 builtin_address (name)
853 char *name;
854 {
855 current_builtin = builtin_address_internal (name, 1);
856 return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
857 }
858
859 /* Return the function implementing the builtin NAME, but only if it is a
860 POSIX.2 special builtin. */
861 sh_builtin_func_t *
862 find_special_builtin (name)
863 char *name;
864 {
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);
869 }
870
871 static int
872 shell_builtin_compare (sbp1, sbp2)
873 struct builtin *sbp1, *sbp2;
874 {
875 int result;
876
877 if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
878 result = strcmp (sbp1->name, sbp2->name);
879
880 return (result);
881 }
882
883 /* Sort the table of shell builtins so that the binary search will work
884 in find_shell_builtin. */
885 void
886 initialize_shell_builtins ()
887 {
888 qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
889 (QSFUNC *)shell_builtin_compare);
890 }