1bb3e7315ef1289f99cb267a32bfcbbad18f00f7
[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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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 #include <sys/file.h>
45 #include <errno.h>
46
47 #if defined (HAVE_UNISTD_H)
48 #  include <unistd.h>
49 #endif
50
51 #include "../bashansi.h"
52
53 #include "../shell.h"
54 #include "../execute_cmd.h"
55 #include "common.h"
56
57 #if !defined (errno)
58 extern int errno;
59 #endif /* !errno */
60
61 #if defined (RESTRICTED_SHELL)
62 extern int restricted;
63 #endif
64
65 /* If non-zero, `.' uses $PATH to look up the script to be sourced. */
66 int source_uses_path = 1;
67
68 /* If this . script is supplied arguments, we save the dollar vars and
69    replace them with the script arguments for the duration of the script's
70    execution.  If the script does not change the dollar vars, we restore
71    what we saved.  If the dollar vars are changed in the script, we leave
72    the new values alone and free the saved values. */
73 static void
74 maybe_pop_dollar_vars ()
75 {
76   if (dollar_vars_changed ())
77     {
78       dispose_saved_dollar_vars ();
79       set_dollar_vars_unchanged ();
80     }
81   else
82     pop_dollar_vars ();
83 }
84
85 /* Read and execute commands from the file passed as argument.  Guess what.
86    This cannot be done in a subshell, since things like variable assignments
87    take place in there.  So, I open the file, place it into a large string,
88    close the file, and then execute the string. */
89 int
90 source_builtin (list)
91      WORD_LIST *list;
92 {
93   int result;
94   char *filename;
95
96   if (list == 0)
97     {
98       builtin_error ("filename argument required");
99       builtin_usage ();
100       return (EX_USAGE);
101     }
102
103   if (no_options (list))
104     return (EX_USAGE);
105
106 #if defined (RESTRICTED_SHELL)
107   if (restricted && strchr (list->word->word, '/'))
108     {
109       builtin_error ("%s: restricted", list->word->word);
110       return (EXECUTION_FAILURE);
111     }
112 #endif
113
114   filename = (char *)NULL;
115   if (source_uses_path)
116     filename = find_path_file (list->word->word);
117   if (filename == 0)
118     filename = savestring (list->word->word);
119
120   begin_unwind_frame ("source");
121   add_unwind_protect ((Function *)xfree, filename);
122
123   if (list->next)
124     {
125       push_dollar_vars ();
126       add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
127       remember_args (list->next, 1);
128     }
129   set_dollar_vars_unchanged ();
130
131   result = source_file (filename);
132
133   run_unwind_frame ("source");
134
135   return (result);
136 }