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 #include "bashgetopt.h"
28 extern int array_needs_making;
29 extern char *this_command_name;
32 $FUNCTION export_builtin
33 $SHORT_DOC export [-n] [-f] [name ...] or export -p
34 NAMEs are marked for automatic export to the environment of
35 subsequently executed commands. If the -f option is given,
36 the NAMEs refer to functions. If no NAMEs are given, or if `-p'
37 is given, a list of all names that are exported in this shell is
38 printed. An argument of `-n' says to remove the export property
39 from subsequent NAMEs. An argument of `--' disables further option
43 /* For each variable name in LIST, make that variable appear in the
44 environment passed to simple commands. If there is no LIST, then
45 print all such variables. An argument of `-n' says to remove the
46 exported attribute from variables named in LIST. An argument of
47 -f indicates that the names present in LIST refer to functions. */
49 register WORD_LIST *list;
51 return (set_or_show_attributes (list, att_exported));
55 $FUNCTION readonly_builtin
56 $SHORT_DOC readonly [-n] [-f] [name ...] or readonly -p
57 The given NAMEs are marked readonly and the values of these NAMEs may
58 not be changed by subsequent assignment. If the -f option is given,
59 then functions corresponding to the NAMEs are so marked. If no
60 arguments are given, or if `-p' is given, a list of all readonly names
61 is printed. An argument of `-n' says to remove the readonly property
62 from subsequent NAMEs. An argument of `--' disables further option
66 /* For each variable name in LIST, make that variable readonly. Given an
67 empty LIST, print out all existing readonly variables. */
68 readonly_builtin (list)
69 register WORD_LIST *list;
71 return (set_or_show_attributes (list, att_readonly));
74 /* For each variable name in LIST, make that variable have the specified
75 ATTRIBUTE. An arg of `-n' says to remove the attribute from the the
76 remaining names in LIST. */
78 set_or_show_attributes (list, attribute)
79 register WORD_LIST *list;
82 register SHELL_VAR *var;
83 int assign, undo = 0, functions_only = 0, any_failed = 0, opt;
85 /* Read arguments from the front of the list. */
86 reset_internal_getopt ();
87 while ((opt = internal_getopt (list, "nfp")) != -1)
100 builtin_error ("usage: %s [-nfp] [varname]", this_command_name);
108 if (attribute & att_exported)
109 array_needs_making = 1;
111 /* Cannot undo readonly status. */
112 if (undo && (attribute & att_readonly))
113 attribute &= ~att_readonly;
117 register char *name = list->word->word;
121 var = find_function (name);
124 builtin_error ("%s: not a function", name);
130 var->attributes &= ~attribute;
132 var->attributes |= attribute;
135 if (attribute == att_exported)
136 array_needs_making++;
140 assign = assignment (name);
144 if (legal_identifier (name) == 0)
146 builtin_error ("%s: not a legal variable name", name);
155 /* This word has already been expanded once with command
156 and parameter expansion. Call do_assignment_no_expand (),
157 which does not do command or parameter substitution. */
158 do_assignment_no_expand (name);
164 var = find_variable (name);
166 var->attributes &= ~attribute;
170 SHELL_VAR *find_tempenv_variable (), *tv;
172 if (tv = find_tempenv_variable (name))
174 var = bind_variable (tv->name, tv->value);
175 dispose_variable (tv);
178 var = find_variable (name);
182 var = bind_variable (name, (char *)NULL);
183 var->attributes |= att_invisible;
186 var->attributes |= attribute;
189 array_needs_making++; /* XXX */
195 SHELL_VAR **variable_list;
198 if ((attribute & att_function) || functions_only)
200 variable_list = all_shell_functions ();
201 if (attribute != att_function)
202 attribute &= ~att_function; /* so declare -xf works, for example */
205 variable_list = all_shell_variables ();
209 for (i = 0; var = variable_list[i]; i++)
211 if ((var->attributes & attribute) && !invisible_p (var))
217 if (exported_p (var))
220 if (readonly_p (var))
223 if (function_p (var))
231 printf ("declare -%s ", flags);
233 if (!function_p (var))
235 char *x = double_quote (value_cell (var));
236 printf ("%s=%s\n", var->name, x);
241 char *named_function_string ();
243 printf ("%s\n", named_function_string
244 (var->name, function_cell (var), 1));
249 free (variable_list);
252 return (any_failed == 0 ? EXECUTION_SUCCESS : EXECUTION_FAILURE);