| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: sw_env.h,v 1.1 1999/06/02 16:53:35 mdw Exp $ |
| 4 | * |
| 5 | * Mangling of environment variables |
| 6 | * |
| 7 | * (c) 1999 EBI |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of sw-tools. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: sw_env.h,v $ |
| 32 | * Revision 1.1 1999/06/02 16:53:35 mdw |
| 33 | * Initial revision |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | #ifndef SW_ENV_H |
| 38 | #define SW_ENV_H |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /*----- Header files ------------------------------------------------------*/ |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | |
| 48 | #include <mLib/dstr.h> |
| 49 | #include <mLib/sym.h> |
| 50 | |
| 51 | /*----- Important constants -----------------------------------------------*/ |
| 52 | |
| 53 | enum { |
| 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 */ |
| 62 | }; |
| 63 | |
| 64 | enum { |
| 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 */ |
| 69 | }; |
| 70 | |
| 71 | /*----- Functions provided ------------------------------------------------*/ |
| 72 | |
| 73 | /* --- @env_get@ --- * |
| 74 | * |
| 75 | * Arguments: @sym_table *t@ = pointer to a symbol table |
| 76 | * @const char *name@ = pointer to variable name to look up |
| 77 | * |
| 78 | * Returns: Pointer to corresponding value string, or null. |
| 79 | * |
| 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 |
| 82 | * returned. |
| 83 | */ |
| 84 | |
| 85 | extern char *env_get(sym_table */*t*/, const char */*name*/); |
| 86 | |
| 87 | /* --- @env_put@ --- * |
| 88 | * |
| 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 |
| 92 | * |
| 93 | * Returns: --- |
| 94 | * |
| 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 |
| 101 | * way you'd expect. |
| 102 | */ |
| 103 | |
| 104 | extern void env_put(sym_table */*t*/, |
| 105 | const char */*name*/, const char */*value*/); |
| 106 | |
| 107 | /* --- @env_import@ --- * |
| 108 | * |
| 109 | * Arguments: @sym_table *t@ = pointer to a symbol table |
| 110 | * @char **env@ = pointer to an environment list |
| 111 | * |
| 112 | * Returns: --- |
| 113 | * |
| 114 | * Use: Inserts all of the environment variables listed into a symbol |
| 115 | * table for rapid access. Equivalent to a lot of calls to |
| 116 | * @env_put@. |
| 117 | */ |
| 118 | |
| 119 | extern void env_import(sym_table */*t*/, char **/*env*/); |
| 120 | |
| 121 | /* --- @env_export@ --- * |
| 122 | * |
| 123 | * Arguments: @sym_table *t@ = pointer to a symbol table |
| 124 | * |
| 125 | * Returns: A big environment list. |
| 126 | * |
| 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. |
| 130 | */ |
| 131 | |
| 132 | extern char **env_export(sym_table */*t*/); |
| 133 | |
| 134 | /* --- @env_destroy@ --- * |
| 135 | * |
| 136 | * Arguments: @sym_table *t@ = pointer to symbol table |
| 137 | * |
| 138 | * Returns: --- |
| 139 | * |
| 140 | * Use: Destroys all the variables in the symbol table. |
| 141 | */ |
| 142 | |
| 143 | extern void env_destroy(sym_table */*t*/); |
| 144 | |
| 145 | /* --- @env_error@ --- * |
| 146 | * |
| 147 | * Arguments: @int e@ = error code |
| 148 | * |
| 149 | * Returns: String representation of error. |
| 150 | * |
| 151 | * Use: Transforms an error into something a user can understand. |
| 152 | */ |
| 153 | |
| 154 | extern const char *env_error(int /*e*/); |
| 155 | |
| 156 | /* --- @env_var@ --- * |
| 157 | * |
| 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 |
| 161 | * |
| 162 | * Returns: One of the @ENV_@ constants. |
| 163 | * |
| 164 | * Use: Scans a variable name from the input stream. |
| 165 | */ |
| 166 | |
| 167 | extern int env_var(sym_table */*t*/, FILE */*fp*/, dstr */*d*/); |
| 168 | |
| 169 | /* --- @env_value@ --- * |
| 170 | * |
| 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 |
| 175 | * |
| 176 | * Returns: 0 if OK, @EOF@ if end-of-file encountered, or >0 on error. |
| 177 | * |
| 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. |
| 182 | */ |
| 183 | |
| 184 | extern int env_value(sym_table */*t*/, FILE */*fp*/, |
| 185 | dstr */*d*/, unsigned /*f*/); |
| 186 | |
| 187 | /* --- @env_read@ --- * |
| 188 | * |
| 189 | * Arguments: @sym_table *t@ = pointer to symbol table |
| 190 | * @FILE *fp@ = file handle to read |
| 191 | * @unsigned f@ = various flags |
| 192 | * |
| 193 | * Returns: Zero if OK, @EOF@ for end-of-file, or error code. |
| 194 | * |
| 195 | * Use: Reads the environment assignment statements in the file. |
| 196 | */ |
| 197 | |
| 198 | extern int env_read(sym_table */*t*/, FILE */*fp*/, unsigned /*f*/); |
| 199 | |
| 200 | /* --- @env_file@ --- * |
| 201 | * |
| 202 | * Arguments: @sym_table *t@ = pointer to symbol table |
| 203 | * @const char *name@ = pointer to filename |
| 204 | * |
| 205 | * Returns: Zero if OK, or an error code. |
| 206 | * |
| 207 | * Use: Reads a named file of environment assignments. |
| 208 | */ |
| 209 | |
| 210 | extern int env_file(sym_table */*t*/, const char */*name*/); |
| 211 | |
| 212 | /*----- That's all, folks -------------------------------------------------*/ |
| 213 | |
| 214 | #ifdef __cplusplus |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | #endif |