1 This file is setattr.def, from which is created setattr.c.
2 It implements the builtins "export" and "readonly", in Bash.
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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
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
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.
26 #if defined (HAVE_UNISTD_H)
31 #include "../bashansi.h"
35 #include "bashgetopt.h"
37 extern int posixly_correct;
38 extern int array_needs_making;
39 extern char *this_command_name;
40 extern Function *this_shell_builtin;
43 extern int declare_builtin ();
46 #define READONLY_OR_EXPORT \
47 (this_shell_builtin == readonly_builtin || this_shell_builtin == export_builtin)
50 $FUNCTION export_builtin
51 $SHORT_DOC export [-nf] [name ...] or export -p
52 NAMEs are marked for automatic export to the environment of
53 subsequently executed commands. If the -f option is given,
54 the NAMEs refer to functions. If no NAMEs are given, or if `-p'
55 is given, a list of all names that are exported in this shell is
56 printed. An argument of `-n' says to remove the export property
57 from subsequent NAMEs. An argument of `--' disables further option
61 /* For each variable name in LIST, make that variable appear in the
62 environment passed to simple commands. If there is no LIST, then
63 print all such variables. An argument of `-n' says to remove the
64 exported attribute from variables named in LIST. An argument of
65 -f indicates that the names present in LIST refer to functions. */
68 register WORD_LIST *list;
70 return (set_or_show_attributes (list, att_exported, 0));
74 $FUNCTION readonly_builtin
75 $SHORT_DOC readonly [-anf] [name ...] or readonly -p
76 The given NAMEs are marked readonly and the values of these NAMEs may
77 not be changed by subsequent assignment. If the -f option is given,
78 then functions corresponding to the NAMEs are so marked. If no
79 arguments are given, or if `-p' is given, a list of all readonly names
80 is printed. An argument of `-n' says to remove the readonly property
81 from subsequent NAMEs. The `-a' option means to treat each NAME as
82 an array variable. An argument of `--' disables further option
86 /* For each variable name in LIST, make that variable readonly. Given an
87 empty LIST, print out all existing readonly variables. */
89 readonly_builtin (list)
90 register WORD_LIST *list;
92 return (set_or_show_attributes (list, att_readonly, 0));
95 /* For each variable name in LIST, make that variable have the specified
96 ATTRIBUTE. An arg of `-n' says to remove the attribute from the the
97 remaining names in LIST. */
99 set_or_show_attributes (list, attribute, nodefs)
100 register WORD_LIST *list;
101 int attribute, nodefs;
103 register SHELL_VAR *var;
104 int assign, undo, functions_only, arrays_only, any_failed, assign_error, opt;
106 #if defined (ARRAY_VARS)
107 WORD_LIST *nlist, *tlist;
111 undo = functions_only = arrays_only = any_failed = assign_error = 0;
112 /* Read arguments from the front of the list. */
113 reset_internal_getopt ();
114 while ((opt = internal_getopt (list, "anfp")) != -1)
124 #if defined (ARRAY_VARS)
140 if (attribute & att_exported)
141 array_needs_making = 1;
143 /* Cannot undo readonly status, silently disallowed. */
144 if (undo && (attribute & att_readonly))
145 attribute &= ~att_readonly;
149 name = list->word->word;
151 if (functions_only) /* xxx -f name */
153 var = find_function (name);
156 builtin_error ("%s: not a function", name);
160 SETVARATTR (var, attribute, undo);
166 /* xxx [-np] name[=value] */
167 assign = assignment (name);
172 if (legal_identifier (name) == 0)
174 builtin_error ("`%s': not a valid identifier", name);
183 if (assign) /* xxx [-np] name=value */
186 #if defined (ARRAY_VARS)
187 /* Let's try something here. Turn readonly -a xxx=yyy into
188 declare -ra xxx=yyy and see what that gets us. */
192 list->next = (WORD_LIST *)NULL;
193 w = make_word ("-ra");
194 nlist = make_word_list (w, list);
195 opt = declare_builtin (nlist);
196 if (opt != EXECUTION_SUCCESS)
204 /* This word has already been expanded once with command
205 and parameter expansion. Call do_assignment_no_expand (),
206 which does not do command or parameter substitution. If
207 the assignment is not performed correctly, flag an error. */
208 if (do_assignment_no_expand (name) == 0)
213 set_var_attribute (name, attribute, undo);
219 SHELL_VAR **variable_list;
222 if ((attribute & att_function) || functions_only)
224 variable_list = all_shell_functions ();
225 if (attribute != att_function)
226 attribute &= ~att_function; /* so declare -xf works, for example */
229 variable_list = all_shell_variables ();
231 #if defined (ARRAY_VARS)
232 if (attribute & att_array)
235 if (attribute != att_array)
236 attribute &= ~att_array;
242 for (i = 0; var = variable_list[i]; i++)
244 #if defined (ARRAY_VARS)
245 if (arrays_only && array_p (var) == 0)
248 if ((var->attributes & attribute) && invisible_p (var) == 0)
249 show_var_attributes (var, READONLY_OR_EXPORT, nodefs);
251 free (variable_list);
255 return (assign_error ? EX_BADASSIGN
256 : ((any_failed == 0) ? EXECUTION_SUCCESS
257 : EXECUTION_FAILURE));
260 /* Show the attributes for shell variable VAR. If NODEFS is non-zero,
261 don't show function definitions along with the name. If PATTR is
262 non-zero, it indicates we're being called from `export' or `readonly'.
263 In POSIX mode, this prints the name of the calling builtin (`export'
264 or `readonly') instead of `declare', and doesn't print function defs
265 when called by `export' or `readonly'. */
267 show_var_attributes (var, pattr, nodefs)
276 /* pattr == 0 means we are called from `declare'. */
277 if (pattr == 0 || posixly_correct == 0)
279 #if defined (ARRAY_VARS)
284 if (function_p (var))
290 if (readonly_p (var))
293 if (exported_p (var))
298 #if defined (ARRAY_VARS)
303 if (function_p (var))
309 if (pattr == 0 || posixly_correct == 0)
310 printf ("declare -%s ", i ? flags : "-");
312 printf ("%s -%s ", this_command_name, flags);
314 printf ("%s ", this_command_name);
316 #if defined (ARRAY_VARS)
318 print_array_assignment (var, 1);
321 /* force `readline' and `export' to not print out function definitions
322 when in POSIX mode. */
323 if (nodefs || (function_p (var) && pattr != 0 && posixly_correct))
324 printf ("%s\n", var->name);
325 else if (function_p (var))
326 printf ("%s\n", named_function_string (var->name, function_cell (var), 1));
329 x = double_quote (value_cell (var));
330 printf ("%s=%s\n", var->name, x);
337 show_name_attributes (name, nodefs)
343 var = find_tempenv_variable (name);
345 var = find_variable (name);
347 if (var && invisible_p (var) == 0)
349 show_var_attributes (var, READONLY_OR_EXPORT, nodefs);
357 set_var_attribute (name, attribute, undo)
364 var = find_variable (name);
367 if (tv = find_tempenv_variable (name))
369 var = bind_variable (tv->name, tv->value);
370 dispose_variable (tv);
373 var = find_variable (name);
377 var = bind_variable (name, (char *)NULL);
378 var->attributes |= att_invisible;
383 SETVARATTR (var, attribute, undo);
385 if (var && ((var->attributes & att_exported) || (attribute & att_exported)))
386 array_needs_making++; /* XXX */