Imported from ../bash-2.04.tar.gz.
[platform/upstream/bash.git] / builtins / source.def
1 This file is source.def, from which is created source.c.
2 It implements the builtins "." and  "source" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING.  If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22 $PRODUCES source.c
23
24 $BUILTIN source
25 $FUNCTION source_builtin
26 $SHORT_DOC source filename
27 Read and execute commands from FILENAME and return.  The pathnames
28 in $PATH are used to find the directory containing FILENAME.
29 $END
30 $BUILTIN .
31 $DOCNAME dot
32 $FUNCTION source_builtin
33 $SHORT_DOC . filename
34 Read and execute commands from FILENAME and return.  The pathnames
35 in $PATH are used to find the directory containing FILENAME.
36 $END
37 /* source.c - Implements the `.' and `source' builtins. */
38
39 #include <config.h>
40
41 #include "../bashtypes.h"
42 #include "posixstat.h"
43 #include "filecntl.h"
44 #ifndef _MINIX
45 #  include <sys/file.h>
46 #endif
47 #include <errno.h>
48
49 #if defined (HAVE_UNISTD_H)
50 #  include <unistd.h>
51 #endif
52
53 #include "../bashansi.h"
54
55 #include "../shell.h"
56 #include "../findcmd.h"
57 #include "common.h"
58
59 #if !defined (errno)
60 extern int errno;
61 #endif /* !errno */
62
63 #if defined (RESTRICTED_SHELL)
64 extern int restricted;
65 #endif
66
67 /* If non-zero, `.' uses $PATH to look up the script to be sourced. */
68 int source_uses_path = 1;
69
70 /* If this . script is supplied arguments, we save the dollar vars and
71    replace them with the script arguments for the duration of the script's
72    execution.  If the script does not change the dollar vars, we restore
73    what we saved.  If the dollar vars are changed in the script, we leave
74    the new values alone and free the saved values. */
75 static void
76 maybe_pop_dollar_vars ()
77 {
78   if (dollar_vars_changed ())
79     {
80       dispose_saved_dollar_vars ();
81       set_dollar_vars_unchanged ();
82     }
83   else
84     pop_dollar_vars ();
85 }
86
87 /* Read and execute commands from the file passed as argument.  Guess what.
88    This cannot be done in a subshell, since things like variable assignments
89    take place in there.  So, I open the file, place it into a large string,
90    close the file, and then execute the string. */
91 int
92 source_builtin (list)
93      WORD_LIST *list;
94 {
95   int result;
96   char *filename;
97
98   if (list == 0)
99     {
100       builtin_error ("filename argument required");
101       builtin_usage ();
102       return (EX_USAGE);
103     }
104
105   if (no_options (list))
106     return (EX_USAGE);
107
108 #if defined (RESTRICTED_SHELL)
109   if (restricted && strchr (list->word->word, '/'))
110     {
111       builtin_error ("%s: restricted", list->word->word);
112       return (EXECUTION_FAILURE);
113     }
114 #endif
115
116   filename = (char *)NULL;
117   if (source_uses_path)
118     filename = find_path_file (list->word->word);
119   if (filename == 0)
120     filename = savestring (list->word->word);
121
122   begin_unwind_frame ("source");
123   add_unwind_protect ((Function *)xfree, filename);
124
125   if (list->next)
126     {
127       push_dollar_vars ();
128       add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
129       remember_args (list->next, 1);
130     }
131   set_dollar_vars_unchanged ();
132
133   result = source_file (filename);
134
135   run_unwind_frame ("source");
136
137   return (result);
138 }