3 * $Id: sw_env.h,v 1.1 1999/06/02 16:53:35 mdw Exp $
5 * Mangling of environment variables
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1999/06/02 16:53:35 mdw
44 /*----- Header files ------------------------------------------------------*/
48 #include <mLib/dstr.h>
51 /*----- Important constants -----------------------------------------------*/
54 ENV_OK
= 0, /* Parsed OK */
55 ENV_EOF
, /* End-of-file encountered */
56 ENV_VCHAR
, /* Bad character in variable name */
57 ENV_SUBST
, /* Bad parameter substitution */
58 ENV_QUOTE
, /* Mismatched quote */
59 ENV_BRACE
, /* Missing brace character */
60 ENV_SYSTEM
, /* System error (see @errno@) */
61 ENV_INTERNAL
/* Internal error */
65 EVF_INCSPC
= 1, /* Allow spaces in values */
66 EVF_BACKTICK
= 2, /* Make backtick self-delimiting */
67 EVF_SKIP
= 4, /* Skipping this section of text */
68 EVF_INITSPC
= 8 /* Allow and ignore leading space */
71 /*----- Functions provided ------------------------------------------------*/
73 /* --- @env_get@ --- *
75 * Arguments: @sym_table *t@ = pointer to a symbol table
76 * @const char *name@ = pointer to variable name to look up
78 * Returns: Pointer to corresponding value string, or null.
80 * Use: Looks up an environment variable in the table and returns its
81 * value. If the variable can't be found, a null pointer is
85 extern char *env_get(sym_table */
*t*/
, const char */
*name*/
);
87 /* --- @env_put@ --- *
89 * Arguments: @sym_table *t@ = pointer to a symbol table
90 * @const char *name@ = pointer to variable name to set
91 * @const char *value@ = pointer to value string to assign
95 * Use: Assigns a value to a variable. If the @name@ contains an
96 * equals character, then it's assumed to be of the form
97 * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if
98 * @value@ is null, the variable is deleted. Finally, the
99 * normal case: @name@ is a plain name, and @value@ is a normal
100 * string causes the variable to be assigned the value in the
104 extern void env_put(sym_table */
*t*/
,
105 const char */
*name*/
, const char */
*value*/
);
107 /* --- @env_import@ --- *
109 * Arguments: @sym_table *t@ = pointer to a symbol table
110 * @char **env@ = pointer to an environment list
114 * Use: Inserts all of the environment variables listed into a symbol
115 * table for rapid access. Equivalent to a lot of calls to
119 extern void env_import(sym_table */
*t*/
, char **/
*env*/
);
121 /* --- @env_export@ --- *
123 * Arguments: @sym_table *t@ = pointer to a symbol table
125 * Returns: A big environment list.
127 * Use: Extracts an environment table from a symbol table
128 * representation of an environment. The table and all of the
129 * strings are in one big block allocated from the heap.
132 extern char **env_export(sym_table */
*t*/
);
134 /* --- @env_destroy@ --- *
136 * Arguments: @sym_table *t@ = pointer to symbol table
140 * Use: Destroys all the variables in the symbol table.
143 extern void env_destroy(sym_table */
*t*/
);
145 /* --- @env_error@ --- *
147 * Arguments: @int e@ = error code
149 * Returns: String representation of error.
151 * Use: Transforms an error into something a user can understand.
154 extern const char *env_error(int /*e*/);
156 /* --- @env_var@ --- *
158 * Arguments: @sym_table *t@ = pointer to symbol table
159 * @FILE *fp@ = pointer to stream to read from
160 * @dstr *d@ = pointer to output variable
162 * Returns: One of the @ENV_@ constants.
164 * Use: Scans a variable name from the input stream.
167 extern int env_var(sym_table */
*t*/
, FILE */
*fp*/
, dstr */
*d*/
);
169 /* --- @env_value@ --- *
171 * Arguments: @sym_table *t@ = pointer to symbol table
172 * @FILE *fp@ = pointer to stream to read from
173 * @dstr *d@ = pointer to output variable
174 * @unsigned f@ = various interesting flags
176 * Returns: 0 if OK, @EOF@ if end-of-file encountered, or >0 on error.
178 * Use: Scans a value from the input stream. The value read may be
179 * quoted in a Bourne-shell sort of a way, and contain Bourne-
180 * shell-like parameter substitutions. Some substitutions
181 * aren't available because they're too awkward to implement.
184 extern int env_value(sym_table */
*t*/
, FILE */
*fp*/
,
185 dstr */
*d*/
, unsigned /*f*/);
187 /* --- @env_read@ --- *
189 * Arguments: @sym_table *t@ = pointer to symbol table
190 * @FILE *fp@ = file handle to read
191 * @unsigned f@ = various flags
193 * Returns: Zero if OK, @EOF@ for end-of-file, or error code.
195 * Use: Reads the environment assignment statements in the file.
198 extern int env_read(sym_table */
*t*/
, FILE */
*fp*/
, unsigned /*f*/);
200 /* --- @env_file@ --- *
202 * Arguments: @sym_table *t@ = pointer to symbol table
203 * @const char *name@ = pointer to filename
205 * Returns: Zero if OK, or an error code.
207 * Use: Reads a named file of environment assignments.
210 extern int env_file(sym_table */
*t*/
, const char */
*name*/
);
212 /*----- That's all, folks -------------------------------------------------*/