1 /* Environment and environment operation definitions for GNU Pies.
2    Copyright (C) 2019-2020 Sergey Poznyakoff
3 
4    GNU Pies is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Pies is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 /* Environment structure */
18 struct environ
19 {
20   char **env_base;
21   size_t env_count;
22   size_t env_max;
23 };
24 typedef struct environ environ_t;
25 
26 environ_t *environ_create (char **);
27 void environ_free (environ_t *env);
28 int environ_add (environ_t *env, char const *def);
29 int environ_set (environ_t *env, char const *name, char const *val);
30 int environ_unset (environ_t *env, char const *name, char const *val);
31 int environ_unset_glob (environ_t *env, const char *pattern);
32 static inline char **
environ_ptr(environ_t * env)33 environ_ptr (environ_t *env)
34 {
35   return env->env_base;
36 }
37 
38 /* Environment operation codes.
39    Elements in a oplist are sorted in that order. */
40 enum envop_code
41   {
42     envop_clear,   /* Clear environment */
43     envop_keep,    /* Keep variable when clearing */
44     envop_set,     /* Set variable */
45     envop_unset    /* Unset variable */
46   };
47 
48 struct envop_entry    /* Environment operation entry */
49 {
50   struct envop_entry *next; /* Next entry in the list */
51   enum envop_code code;     /* Operation code */
52   char *name;               /* Variable name (or globbing pattern) */
53   char *value;              /* Value of the variable */
54 };
55 
56 typedef struct envop_entry envop_t;
57 
58 int wildmatch (char const *expr, char const *name, size_t len);
59 
60 int envop_entry_add (envop_t **head,
61 		     enum envop_code code,
62 		     char const *name, char const *value);
63 
64 int envop_exec (envop_t *op, environ_t *env);
65 void envop_free (envop_t *op);
66 
67 
68 
69