6
6
Copyright (C) 2004 Marc Singer
8
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of the GNU General Public License as
10
published by the Free Software Foundation; either version 2 of the
11
License, or (at your option) any later version.
13
This program is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
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
9
modify it under the terms of the GNU General Public License
10
version 2 as published by the Free Software Foundation.
11
Please refer to the file debian/copyright for further details.
20
In order to expand variables recursively, the expand_variables()
21
function is recursive. It copies the characters from one string to
22
another, expanding variables as they are found. When a variable is
23
detected, it is looked-up and then expanded using the buffer space
24
following the current point in the expansion buffer.
26
The only hazard is that the function will silently fail if an
27
expansion would overflow the buffer. In this case, the user will
28
see an unexpanded variable in the output.
29
34
#include <linux/types.h>
30
35
#include <linux/string.h>
49
55
#define TIMECMD_REPORT
52
#if defined (CONFIG_ALIAS) || defined (CONFIG_ENV)
54
static char* expand_variables (const char* rgbSrc)
58
#if defined (CONFIG_ALIASES) || defined (CONFIG_ENV)
59
# define USE_EXPAND_VARIABLES
64
#if defined (USE_EXPAND_VARIABLES)
66
static char* _expand_variables (const char* rgbSrc,
67
char* rgbExpand, size_t cbExpand)
56
69
const char* pchSrc;
57
static char __xbss(command) rgb[CB_COMMAND_MAX];
70
char* pch = rgbExpand; /* Destination pointer, this step */
59
71
char* pchKey = NULL;
64
for (pchSrc = rgbSrc; pch < rgb + sizeof (rgb) - 1; ++pchSrc) {
75
for (pchSrc = rgbSrc; pch < rgbExpand + cbExpand - 1; ++pchSrc) {
66
77
case 0: /* Beginning of word */
67
78
if (*pchSrc == '"')
83
94
case 2: /* Scanning variable key */
84
if (isalpha (*pchSrc) || *pchSrc == '_' || *pchSrc == '-')
96
|| *pchSrc == '_' || *pchSrc == '-') /* key ::= [A-Za-z_-]+ */
87
value = lookup_alias_or_env (pchKey + 1, 0);
89
if (pch + strlen (value) + 1 >= rgb + sizeof (rgb) - 1)
90
return 0; /* Simple failure on overflow */
91
strcpy (pchKey, value);
92
pch = pchKey + strlen (value);
98
*pch = 0; /* Terminate string so lookup can succeed */
100
const char* value = lookup_alias_or_env (pchKey + 1, 0);
101
const char* value_expanded;
103
if (pchKey + strlen (value) >= rgbExpand + cbExpand - 1)
104
return 0; /* Simple overflow */
105
value_expanded = _expand_variables (value, pch + 1,
106
rgbExpand + cbExpand - 1
109
value = value_expanded;
111
/* Successful expansion, so replace
112
variable with substitution */
113
memmove (pchKey, value, strlen (value) + 1);
114
pch = pchKey + strlen (pchKey);
119
state = 0; /* In any case, return to normal parsing */
98
122
case 11: /* Quoting */
113
return fChanged && state == 0 ? rgb : NULL;
137
return fChanged && state == 0 ? rgbExpand : NULL;
140
static char* expand_variables (const char* rgbSrc)
142
static char __xbss(command) rgbExpand[2*1024]; /* Ridiculously large */
143
return _expand_variables (rgbSrc, rgbExpand, sizeof (rgbExpand));
211
error_description = NULL;
212
result = command_match->func (argc, argv);
213
if (result < ERROR_IMPORTANT) {
214
printf ("Error %d", result);
241
error_description = NULL;
242
result = command_match
243
? command_match->func (argc, argv)
245
if (result < ERROR_IMPORTANT) {
246
printf ("Error %d", result);
215
247
#if !defined (CONFIG_SMALL)
216
if (error_description == NULL) {
219
error_description = "parameter error"; break;
221
error_description = "error on open"; break;
222
case ERROR_AMBIGUOUS:
223
error_description = "ambiguous"; break;
225
error_description = "no driver"; break;
226
case ERROR_UNSUPPORTED:
227
error_description = "unsupported"; break;
228
case ERROR_BADPARTITION:
229
error_description = "bad partition"; break;
230
case ERROR_FILENOTFOUND:
231
error_description = "file not found"; break;
232
case ERROR_IOFAILURE:
233
error_description = "i/o failure"; break;
235
error_description = "break"; break;
248
if (error_description == NULL) {
251
error_description = "command failure"; break;
253
error_description = "parameter error"; break;
255
error_description = "error on open"; break;
256
case ERROR_AMBIGUOUS:
257
error_description = "ambiguous"; break;
259
error_description = "no driver"; break;
260
case ERROR_UNSUPPORTED:
261
error_description = "unsupported"; break;
262
case ERROR_BADPARTITION:
263
error_description = "bad partition"; break;
264
case ERROR_FILENOTFOUND:
265
error_description = "file not found"; break;
266
case ERROR_IOFAILURE:
267
error_description = "i/o failure"; break;
269
error_description = "break"; break;
270
case ERROR_NOCOMMAND:
271
error_description = "no such command"; break;
273
error_description = "timeout"; break;
274
case ERROR_UNRECOGNIZED:
275
error_description = "unrecognized operand"; break;
278
if (error_description)
239
279
printf (" (%s)", error_description);
244
return ERROR_NOCOMMAND;
247
287
void exec_monitor (void)
258
298
const char** argv;
259
299
strcpy (sz, szStartup);
301
DBG (1, "%s: startup '%s'\n", __FUNCTION__, sz);
261
303
while (pch && *pch) {
263
305
char* pchEnd = strchr (pch, ';');
306
DBG (1, " pchEnd 0x%p *pchEnd %x\n", pchEnd, pchEnd ? *pchEnd : 0);
266
309
while (*pch == ' ')
268
printf ("\r# %s\n", pch);
269
result = parse_command (pch, &argc, &argv);
270
if (result >= 0 && (call_command (argc, argv) && result != 1))
312
printf ("\r# %s\n", pch);
313
result = parse_command (pch, &argc, &argv);
314
if (result >= 0 && (call_command (argc, argv) && result != 1))
272
317
pch = (pchEnd ? pchEnd + 1 : 0);
318
DBG (1, " pch 0x%p *pch %x\n", pch, pch ? *pch : 0);