2 * env.c - environment variables storage
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * @brief Environment support
34 #include <environment.h>
36 #define VARIABLE_D_SIZE(name, value) (sizeof(struct variable_d) + strlen(name) + strlen(value) + 2)
38 static struct env_context
*context
;
41 * Remove a list of environment variables
42 * @param[in] v Variable anchor to remove
44 static void free_variables(struct variable_d
*v
)
46 struct variable_d
*next
;
55 /** Read back current context */
56 struct env_context
*get_current_context(void)
60 EXPORT_SYMBOL(get_current_context
);
66 int env_push_context(void)
68 struct env_context
*c
= xzalloc(sizeof(struct env_context
));
70 c
->local
= xzalloc(VARIABLE_D_SIZE("", ""));
71 c
->global
= xzalloc(VARIABLE_D_SIZE("", ""));
84 late_initcall(env_push_context
);
89 int env_pop_context(void)
91 struct env_context
*c
= context
;
93 if (context
->parent
) {
95 free_variables(context
->local
);
96 free_variables(context
->global
);
105 * Return variable's value
106 * @param[in] var Variable of interest
107 * @return Value as text
109 char *var_val(struct variable_d
*var
)
111 return &var
->data
[strlen(var
->data
) + 1];
115 * Return variable's name
116 * @param[in] var Variable of interest
117 * @return Name as text
119 char *var_name(struct variable_d
*var
)
124 static const char *getenv_raw(struct variable_d
*var
, const char *name
)
127 if (!strcmp(var_name(var
), name
))
134 const char *getenv (const char *name
)
136 struct env_context
*c
;
139 if (strchr(name
, '.')) {
141 char *devstr
= strdup(name
);
142 char *par
= strchr(devstr
, '.');
143 struct device_d
*dev
;
145 dev
= get_device_by_name(devstr
);
148 ret
= dev_get_param(dev
, par
);
156 val
= getenv_raw(c
->local
, name
);
161 val
= getenv_raw(c
->global
, name
);
168 EXPORT_SYMBOL(getenv
);
170 static int setenv_raw(struct variable_d
*var
, const char *name
, const char *value
)
172 struct variable_d
*newvar
= NULL
;
175 newvar
= xzalloc(VARIABLE_D_SIZE(name
, value
));
176 strcpy(&newvar
->data
[0], name
);
177 strcpy(&newvar
->data
[strlen(name
) + 1], value
);
181 if (!strcmp(var
->next
->data
, name
)) {
183 newvar
->next
= var
->next
->next
;
188 struct variable_d
*tmp
;
190 var
->next
= var
->next
->next
;
201 int setenv(const char *_name
, const char *value
)
203 char *name
= strdup(_name
);
205 struct variable_d
*var
;
212 if ((par
= strchr(name
, '.'))) {
213 struct device_d
*dev
;
216 dev
= get_device_by_name(name
);
218 ret
= dev_set_param(dev
, par
, value
);
225 perror("set parameter");
230 if (getenv_raw(context
->global
, name
))
231 var
= context
->global
;
233 var
= context
->local
;
235 ret
= setenv_raw(var
, name
, value
);
241 EXPORT_SYMBOL(setenv
);
243 int export(const char *varname
)
245 const char *val
= getenv_raw(context
->local
, varname
);
248 setenv_raw(context
->global
, varname
, val
);
249 setenv_raw(context
->local
, varname
, NULL
);
254 EXPORT_SYMBOL(export
);