From 436d5c73cbe2b28f63864db3286c20a627604353 Mon Sep 17 00:00:00 2001 From: mdw Date: Tue, 27 Jul 1999 13:38:27 +0000 Subject: [PATCH] Cauterized out the low-level environment operations and put them in mLib. --- src/sw_env.c | 178 +++-------------------------------------------------------- src/sw_env.h | 83 +++------------------------- 2 files changed, 16 insertions(+), 245 deletions(-) diff --git a/src/sw_env.c b/src/sw_env.c index 797a4ea..7580a24 100644 --- a/src/sw_env.c +++ b/src/sw_env.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sw_env.c,v 1.1 1999/06/02 16:53:35 mdw Exp $ + * $Id: sw_env.c,v 1.2 1999/07/27 13:38:27 mdw Exp $ * * Mangling of environment variables * @@ -29,8 +29,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: sw_env.c,v $ - * Revision 1.1 1999/06/02 16:53:35 mdw - * Initial revision + * Revision 1.2 1999/07/27 13:38:27 mdw + * Cauterized out the low-level environment operations and put them in + * mLib. + * + * Revision 1.1.1.1 1999/06/02 16:53:35 mdw + * Initial import. * */ @@ -53,180 +57,14 @@ #include #include +#include #include #include #include "sw_env.h" -/*----- Data structures ---------------------------------------------------*/ - -typedef struct var { - sym_base _base; - char *v; -} var; - /*----- Main code ---------------------------------------------------------*/ -/* --- @env_get@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @const char *name@ = pointer to variable name to look up - * - * Returns: Pointer to corresponding value string, or null. - * - * Use: Looks up an environment variable in the table and returns its - * value. If the variable can't be found, a null pointer is - * returned. - */ - -char *env_get(sym_table *t, const char *name) -{ - var *e = sym_find(t, name, -1, 0, 0); - return (e ? e->v : 0); -} - -/* --- @env_put@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @const char *name@ = pointer to variable name to set - * @const char *value@ = pointer to value string to assign - * - * Returns: --- - * - * Use: Assigns a value to a variable. If the @name@ contains an - * equals character, then it's assumed to be of the form - * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if - * @value@ is null, the variable is deleted. Finally, the - * normal case: @name@ is a plain name, and @value@ is a normal - * string causes the variable to be assigned the value in the - * way you'd expect. - */ - -void env_put(sym_table *t, const char *name, const char *value) -{ - char *q = 0; - - /* --- Sort out the mess with `NAME=VALUE' forms --- */ - - { - size_t eq = strcspn(name, "="); - if (name[eq] == '=') { - q = xmalloc(eq + 1); - memcpy(q, name, eq); - q[eq] = 0; - value = name + eq + 1; - name = q; - } - } - - /* --- Read the current value --- */ - - if (!value) { - var *v; - if ((v = sym_find(t, name, -1, 0, 0)) != 0) { - free(v->v); - sym_remove(t, v); - } - } else { - unsigned found; - var *v = sym_find(t, name, -1, sizeof(*v), &found); - if (found) - free(v->v); - v->v = xstrdup(value); - } - - /* --- Tidying --- */ - - if (q) - free(q); -} - -/* --- @env_import@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @char **env@ = pointer to an environment list - * - * Returns: --- - * - * Use: Inserts all of the environment variables listed into a symbol - * table for rapid access. Equivalent to a lot of calls to - * @env_put@. - */ - -void env_import(sym_table *t, char **env) -{ - while (*env) { - env_put(t, *env, 0); - env++; - } -} - -/* --- @env_export@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * - * Returns: A big environment list. - * - * Use: Extracts an environment table from a symbol table - * representation of an environment. The table and all of the - * strings are in one big block allocated from the heap. - */ - -char **env_export(sym_table *t) -{ - size_t n = 1; - size_t sz = 0; - sym_iter i; - var *v; - char **env; - char *p, **pp; - - /* --- Work out sizes for everything --- */ - - for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) { - n++; - sz += strlen(SYM_NAME(v)) + strlen(v->v) + 2; - } - - /* --- Allocate the big chunk of memory --- */ - - env = pp = xmalloc(n * sizeof(char *) + sz); - p = (char *)(env + n); - - /* --- Dump the output in the big chunk of memory --- */ - - for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) { - const char *name = SYM_NAME(v); - size_t nlen = strlen(name), vlen = strlen(v->v); - *pp++ = p; - memcpy(p, name, nlen); p += nlen; - *p++ = '='; - memcpy(p, v->v, vlen); p += vlen; - *p++ = 0; - } - *pp++ = 0; - return (env); -} - -/* --- @env_destroy@ --- * - * - * Arguments: @sym_table *t@ = pointer to symbol table - * - * Returns: --- - * - * Use: Destroys all the variables in the symbol table. - */ - -void env_destroy(sym_table *t) -{ - sym_iter i; - var *v; - - for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; ) - free(v->v); - sym_destroy(t); -} - /* --- @env_error@ --- * * * Arguments: @int e@ = error code diff --git a/src/sw_env.h b/src/sw_env.h index c14ca1c..9aa207e 100644 --- a/src/sw_env.h +++ b/src/sw_env.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sw_env.h,v 1.1 1999/06/02 16:53:35 mdw Exp $ + * $Id: sw_env.h,v 1.2 1999/07/27 13:38:27 mdw Exp $ * * Mangling of environment variables * @@ -29,8 +29,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: sw_env.h,v $ - * Revision 1.1 1999/06/02 16:53:35 mdw - * Initial revision + * Revision 1.2 1999/07/27 13:38:27 mdw + * Cauterized out the low-level environment operations and put them in + * mLib. + * + * Revision 1.1.1.1 1999/06/02 16:53:35 mdw + * Initial import. * */ @@ -46,6 +50,7 @@ #include #include +#include #include /*----- Important constants -----------------------------------------------*/ @@ -70,78 +75,6 @@ enum { /*----- Functions provided ------------------------------------------------*/ -/* --- @env_get@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @const char *name@ = pointer to variable name to look up - * - * Returns: Pointer to corresponding value string, or null. - * - * Use: Looks up an environment variable in the table and returns its - * value. If the variable can't be found, a null pointer is - * returned. - */ - -extern char *env_get(sym_table */*t*/, const char */*name*/); - -/* --- @env_put@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @const char *name@ = pointer to variable name to set - * @const char *value@ = pointer to value string to assign - * - * Returns: --- - * - * Use: Assigns a value to a variable. If the @name@ contains an - * equals character, then it's assumed to be of the form - * `VAR=VALUE' and @value@ argument is ignored. Otherwise, if - * @value@ is null, the variable is deleted. Finally, the - * normal case: @name@ is a plain name, and @value@ is a normal - * string causes the variable to be assigned the value in the - * way you'd expect. - */ - -extern void env_put(sym_table */*t*/, - const char */*name*/, const char */*value*/); - -/* --- @env_import@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * @char **env@ = pointer to an environment list - * - * Returns: --- - * - * Use: Inserts all of the environment variables listed into a symbol - * table for rapid access. Equivalent to a lot of calls to - * @env_put@. - */ - -extern void env_import(sym_table */*t*/, char **/*env*/); - -/* --- @env_export@ --- * - * - * Arguments: @sym_table *t@ = pointer to a symbol table - * - * Returns: A big environment list. - * - * Use: Extracts an environment table from a symbol table - * representation of an environment. The table and all of the - * strings are in one big block allocated from the heap. - */ - -extern char **env_export(sym_table */*t*/); - -/* --- @env_destroy@ --- * - * - * Arguments: @sym_table *t@ = pointer to symbol table - * - * Returns: --- - * - * Use: Destroys all the variables in the symbol table. - */ - -extern void env_destroy(sym_table */*t*/); - /* --- @env_error@ --- * * * Arguments: @int e@ = error code -- 2.11.0