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 array_needs_making;
38 extern char *this_command_name;
41 $FUNCTION export_builtin
42 $SHORT_DOC export [-nf] [name ...] or export -p
43 NAMEs are marked for automatic export to the environment of
44 subsequently executed commands. If the -f option is given,
45 the NAMEs refer to functions. If no NAMEs are given, or if `-p'
46 is given, a list of all names that are exported in this shell is
47 printed. An argument of `-n' says to remove the export property
48 from subsequent NAMEs. An argument of `--' disables further option
52 /* For each variable name in LIST, make that variable appear in the
53 environment passed to simple commands. If there is no LIST, then
54 print all such variables. An argument of `-n' says to remove the
55 exported attribute from variables named in LIST. An argument of
56 -f indicates that the names present in LIST refer to functions. */
59 register WORD_LIST *list;
61 return (set_or_show_attributes (list, att_exported, 0));
65 $FUNCTION readonly_builtin
66 $SHORT_DOC readonly [-anf] [name ...] or readonly -p
67 The given NAMEs are marked readonly and the values of these NAMEs may
68 not be changed by subsequent assignment. If the -f option is given,
69 then functions corresponding to the NAMEs are so marked. If no
70 arguments are given, or if `-p' is given, a list of all readonly names
71 is printed. An argument of `-n' says to remove the readonly property
72 from subsequent NAMEs. The `-a' option means to treat each NAME as
73 an array variable. An argument of `--' disables further option
77 /* For each variable name in LIST, make that variable readonly. Given an
78 empty LIST, print out all existing readonly variables. */
80 readonly_builtin (list)
81 register WORD_LIST *list;
83 return (set_or_show_attributes (list, att_readonly, 0));
86 /* For each variable name in LIST, make that variable have the specified
87 ATTRIBUTE. An arg of `-n' says to remove the attribute from the the
88 remaining names in LIST. */
90 set_or_show_attributes (list, attribute, nodefs)
91 register WORD_LIST *list;
92 int attribute, nodefs;
94 register SHELL_VAR *var;
95 int assign, undo, functions_only, arrays_only, any_failed, assign_error, opt;
98 undo = functions_only = arrays_only = any_failed = assign_error = 0;
99 /* Read arguments from the front of the list. */
100 reset_internal_getopt ();
101 while ((opt = internal_getopt (list, "anfp")) != -1)
111 #if defined (ARRAY_VARS)
127 if (attribute & att_exported)
128 array_needs_making = 1;
130 /* Cannot undo readonly status, silently disallowed. */
131 if (undo && (attribute & att_readonly))
132 attribute &= ~att_readonly;
136 name = list->word->word;
138 if (functions_only) /* xxx -f name */
140 var = find_function (name);
143 builtin_error ("%s: not a function", name);
147 SETVARATTR (var, attribute, undo);
153 /* xxx [-np] name[=value] */
154 assign = assignment (name);
159 if (legal_identifier (name) == 0)
161 builtin_error ("`%s': not a valid identifier", name);
167 if (assign) /* xxx [-np] name=value */
170 /* This word has already been expanded once with command
171 and parameter expansion. Call do_assignment_no_expand (),
172 which does not do command or parameter substitution. If
173 the assignment is not performed correctly, flag an error. */
174 if (do_assignment_no_expand (name) == 0)
179 set_var_attribute (name, attribute, undo);
185 SHELL_VAR **variable_list;
188 if ((attribute & att_function) || functions_only)
190 variable_list = all_shell_functions ();
191 if (attribute != att_function)
192 attribute &= ~att_function; /* so declare -xf works, for example */
195 variable_list = all_shell_variables ();
197 #if defined (ARRAY_VARS)
198 if (attribute & att_array)
201 if (attribute != att_array)
202 attribute &= ~att_array;
208 for (i = 0; var = variable_list[i]; i++)
210 #if defined (ARRAY_VARS)
211 if (arrays_only && array_p (var) == 0)
214 if ((var->attributes & attribute) && invisible_p (var) == 0)
215 show_var_attributes (var, nodefs);
217 free (variable_list);
221 return (assign_error ? EX_BADASSIGN
222 : ((any_failed == 0) ? EXECUTION_SUCCESS
223 : EXECUTION_FAILURE));
227 show_var_attributes (var, nodefs)
236 #if defined (ARRAY_VARS)
241 if (function_p (var))
247 if (readonly_p (var))
250 if (exported_p (var))
255 printf ("declare -%s ", i ? flags : "-");
257 #if defined (ARRAY_VARS)
259 print_array_assignment (var, 1);
263 printf ("%s\n", var->name);
264 else if (function_p (var))
265 printf ("%s\n", named_function_string (var->name, function_cell (var), 1));
268 x = double_quote (value_cell (var));
269 printf ("%s=%s\n", var->name, x);
276 show_name_attributes (name, nodefs)
282 var = find_tempenv_variable (name);
284 var = find_variable (name);
286 if (var && invisible_p (var) == 0)
288 show_var_attributes (var, nodefs);
296 set_var_attribute (name, attribute, undo)
303 var = find_variable (name);
306 if (tv = find_tempenv_variable (name))
308 var = bind_variable (tv->name, tv->value);
309 dispose_variable (tv);
312 var = find_variable (name);
316 var = bind_variable (name, (char *)NULL);
317 var->attributes |= att_invisible;
322 SETVARATTR (var, attribute, undo);
324 if (var && ((var->attributes & att_exported) || (attribute & att_exported)))
325 array_needs_making++; /* XXX */