path: root/lib/envop.h
diff options
authorSergey Poznyakoff <gray@gnu.org>2019-06-02 07:40:11 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-06-02 08:03:16 +0300
commitcc298a11a131b162f291d6ee27ba1d7598a1b6c4 (patch)
tree4d82a9a5da05fbfdce0c9bf03e605ecf00baa07a /lib/envop.h
parent1f28935841a69aa8be7eb2e88f580df9c95206d2 (diff)
New configuration statement for manipulating the environment.
The "env" statement is now a compound statement. It can contain four substatements: "clear" to clear the environment, "keep" to retain certain variable while clearing it, "set" to set a variable, and "unset" to unset a variable or variables. Both "keep" and "unset" can take globbing pattern as their argument, in which case they affect all variable matching that pattern. The value part in the "set" statement is subject to variable expansion, e.g. set "MANPATH=$MANPATH${MANPATH:+:}/usr/local/man" The support for the old one-line syntax of "env" is retained for backward compatibility. This commit also fixes redirection to a file: new data are appended to the file, instead of overwriting it. * lib/Makefile.am: Add new files. * lib/envop.c: New file. * lib/envop.h: New file. * lib/wildmatch.c: New file. * src/comp.c (component_free): Update. * src/pies.c (argv_free): New function. (parse_legacy_env): New function. (_cb_env): Remove. (cb_env_section_parser): New function. (cb_env_keywords): New keywords for the "env" block statement: clear, keep, set, unset. (component_keywords): New compount statement: env. Old usage retained for backward compatibility. * src/pies.h: Include "envop.h" (component)<env>: Remove. <envop>: New field. * src/prog.h (prog)<env>: New field. * src/progman.c (redirect_to_file): Position to the end of file. (DEBUG_ENVIRON): Remove macro. (debug_environ): New function. (environ_setup): Remove. (prog_sockenv): Use environ_set to modify local copy of environment. (prog_start_prologue): Use environ_create + envop_exec to create and modify the environment. (prog_execute): Set environment to prog-local copy. * tests/.gitignore: Build envtest * tests/Makefile.am: Add new tests. * tests/testsuite.at: Add environment tests. * tests/envop.at: New file. * tests/envtest.c: New file. * tests/env.at: New file. * tests/redirect.at: Check first two lines of the output file.
Diffstat (limited to 'lib/envop.h')
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/envop.h b/lib/envop.h
new file mode 100644
index 0000000..054152e
--- /dev/null
+++ b/lib/envop.h
@@ -0,0 +1,68 @@
+/* Environment and environment operation definitions for GNU Pies.
+ Copyright (C) 2019 Sergey Poznyakoff
+ GNU Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+ GNU Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ GNU General Public License for more details.
+ You should have received a copy of the GNU General Public License
+ along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
+/* Environment structure */
+struct environ
+ char **env_base;
+ size_t env_count;
+ size_t env_max;
+typedef struct environ environ_t;
+environ_t *environ_create (char **);
+void environ_free (environ_t *env);
+int environ_add (environ_t *env, char const *def);
+int environ_set (environ_t *env, char const *name, char const *val);
+int environ_unset (environ_t *env, char const *name);
+int environ_unset_glob (environ_t *env, const char *pattern);
+static inline char **
+environ_ptr (environ_t *env)
+ return env->env_base;
+/* Environment operation codes.
+ Elements in a oplist are sorted in that order. */
+enum envop_code
+ {
+ envop_clear, /* Clear environment */
+ envop_keep, /* Keep variable when clearing */
+ envop_set, /* Set variable */
+ envop_unset /* Unset variable */
+ };
+struct envop_entry /* Environment operation entry */
+ struct envop_entry *next; /* Next entry in the list */
+ enum envop_code code; /* Operation code */
+ char *name; /* Variable name (or globbing pattern) */
+ char *value; /* Value of the variable */
+typedef struct envop_entry envop_t;
+int wildmatch (char const *expr, char const *name, size_t len);
+int envop_entry_add (envop_t **head,
+ enum envop_code code,
+ char const *name, char const *value);
+int envop_exec (envop_t *op, environ_t *env);
+void envop_free (envop_t *op);

Return to:

Send suggestions and report system problems to the System administrator.