1/* source.c, created from source.def. */ 2#line 23 "source.def" 3 4#line 41 "source.def" 5/* source.c - Implements the `.' and `source' builtins. */ 6 7#include <config.h> 8 9#include "../bashtypes.h" 10#include "posixstat.h" 11#include "filecntl.h" 12#if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H) 13# include <sys/file.h> 14#endif 15#include <errno.h> 16 17#if defined (HAVE_UNISTD_H) 18# include <unistd.h> 19#endif 20 21#include "../bashansi.h" 22#include "../bashintl.h" 23 24#include "../shell.h" 25#include "../flags.h" 26#include "../findcmd.h" 27#include "common.h" 28#include "bashgetopt.h" 29#include "../trap.h" 30 31#if !defined (errno) 32extern int errno; 33#endif /* !errno */ 34 35static void maybe_pop_dollar_vars __P((void)); 36 37/* If non-zero, `.' uses $PATH to look up the script to be sourced. */ 38int source_uses_path = 1; 39 40/* If non-zero, `.' looks in the current directory if the filename argument 41 is not found in the $PATH. */ 42int source_searches_cwd = 1; 43 44/* If this . script is supplied arguments, we save the dollar vars and 45 replace them with the script arguments for the duration of the script's 46 execution. If the script does not change the dollar vars, we restore 47 what we saved. If the dollar vars are changed in the script, and we are 48 not executing a shell function, we leave the new values alone and free 49 the saved values. */ 50static void 51maybe_pop_dollar_vars () 52{ 53 if (variable_context == 0 && (dollar_vars_changed () & ARGS_SETBLTIN)) 54 dispose_saved_dollar_vars (); 55 else 56 pop_dollar_vars (); 57 if (debugging_mode) 58 pop_args (); /* restore BASH_ARGC and BASH_ARGV */ 59 set_dollar_vars_unchanged (); 60} 61 62/* Read and execute commands from the file passed as argument. Guess what. 63 This cannot be done in a subshell, since things like variable assignments 64 take place in there. So, I open the file, place it into a large string, 65 close the file, and then execute the string. */ 66int 67source_builtin (list) 68 WORD_LIST *list; 69{ 70 int result; 71 char *filename, *debug_trap; 72 73 if (no_options (list)) 74 return (EX_USAGE); 75 list = loptend; 76 77 if (list == 0) 78 { 79 builtin_error (_("filename argument required")); 80 builtin_usage (); 81 return (EX_USAGE); 82 } 83 84#if defined (RESTRICTED_SHELL) 85 if (restricted && strchr (list->word->word, '/')) 86 { 87 sh_restricted (list->word->word); 88 return (EXECUTION_FAILURE); 89 } 90#endif 91 92 filename = (char *)NULL; 93 if (source_uses_path) 94 filename = find_path_file (list->word->word); 95 if (filename == 0) 96 { 97 if (source_searches_cwd == 0) 98 { 99 builtin_error (_("%s: file not found"), list->word->word); 100 return (EXECUTION_FAILURE); 101 } 102 else 103 filename = savestring (list->word->word); 104 } 105 106 begin_unwind_frame ("source"); 107 add_unwind_protect ((Function *)xfree, filename); 108 109 if (list->next) 110 { 111 push_dollar_vars (); 112 add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL); 113 remember_args (list->next, 1); 114 if (debugging_mode) 115 push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */ 116 } 117 set_dollar_vars_unchanged (); 118 119 /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded) 120 is set. XXX - should sourced files inherit the RETURN trap? Functions 121 don't. */ 122 debug_trap = TRAP_STRING (DEBUG_TRAP); 123 if (debug_trap && function_trace_mode == 0) 124 { 125 debug_trap = savestring (debug_trap); 126 add_unwind_protect (xfree, debug_trap); 127 add_unwind_protect (set_debug_trap, debug_trap); 128 restore_default_signal (DEBUG_TRAP); 129 } 130 131 result = source_file (filename, (list && list->next)); 132 133 run_unwind_frame ("source"); 134 135 return (result); 136} 137