-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.c
378 lines (310 loc) · 6.34 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/** \file
* Key/value parser until we have a proper config
*/
/*
* Copyright (C) 2009 Trammell Hudson <[email protected]>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "dryos.h"
#include "config.h"
#include "version.h"
// Don't use isspace since we don't have it
static inline int
is_space( char c )
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
struct config *
config_parse_line(
const char * line
)
{
int name_len = 0;
int value_len = 0;
struct config * config = malloc( sizeof(*config) );
if( !config )
goto malloc_error;
config->next = 0;
// Trim any leading whitespace
int i = 0;
while( line[i] && is_space( line[i] ) )
i++;
// Copy the name to the name buffer
while( line[i]
&& !is_space( line[i] )
&& line[i] != '='
&& name_len < MAX_NAME_LEN
)
config->name[ name_len++ ] = line[i++];
if( name_len == MAX_NAME_LEN )
goto parse_error;
// And nul terminate it
config->name[ name_len ] = '\0';
// Skip any white space and = signs
while( line[i] && is_space( line[i] ) )
i++;
if( line[i++] != '=' )
goto parse_error;
while( line[i] && is_space( line[i] ) )
i++;
// Copy the value to the value buffer
while( line[i] && value_len < MAX_VALUE_LEN )
config->value[ value_len++ ] = line[ i++ ];
// Back up to trim any white space
while( value_len > 0 && is_space( config->value[ value_len-1 ] ) )
value_len--;
// And nul terminate it
config->value[ value_len ] = '\0';
DebugMsg( DM_MAGIC, 3,
"%s: '%s' => '%s'",
__func__,
config->name,
config->value
);
return config;
parse_error:
DebugMsg( DM_MAGIC, 3,
"%s: PARSE ERROR: len=%d,%d string='%s'",
__func__,
name_len,
value_len,
line
);
free( config );
malloc_error:
return 0;
}
int
read_line(
FILE * file,
char * buf,
size_t size
)
{
size_t len = 0;
while( len < size )
{
int rc = FIO_ReadFile( file, buf+len, 1 );
if( rc <= 0 )
return -1;
if( buf[len] == '\r' )
continue;
if( buf[len] == '\n' )
{
buf[len] = '\0';
return len;
}
len++;
}
return -1;
}
extern struct config_var _config_vars_start[];
extern struct config_var _config_vars_end[];
static void
config_auto_parse(
struct config * config
)
{
struct config_var * var = _config_vars_start;
for( ; var < _config_vars_end ; var++ )
{
if( !streq( var->name, config->name ) )
continue;
DebugMsg( DM_MAGIC, 3,
"%s: '%s' => '%s'",
__func__,
config->name,
config->value
);
if( var->type == 0 )
{
*(unsigned*) var->value = atoi( config->value );
} else {
*(char **) var->value = config->value;
}
return;
}
DebugMsg( DM_MAGIC, 3,
"%s: '%s' unused?",
__func__,
config->name
);
}
int
config_save_file(
struct config * config,
const char * filename
)
{
FILE * file = FIO_CreateFile( filename );
if( file == INVALID_PTR )
return -1;
struct config_var * var = _config_vars_start;
int count = 0;
DebugMsg( DM_MAGIC, 3, "%s: saving to %s", __func__, filename );
fprintf( file,
"# Magic Lantern %s (%s)\n"
"# Build on %s by %s\n",
build_version,
build_id,
build_date,
build_user
);
struct tm now;
LoadCalendarFromRTC( &now );
fprintf( file,
"# Configuration saved on %04d/%02d/%02d %02d:%02d:%02d\n",
now.tm_year + 1900,
now.tm_mon + 1,
now.tm_mday,
now.tm_hour,
now.tm_min,
now.tm_sec
);
for( ; var < _config_vars_end ; var++ )
{
if( var->type == 0 )
fprintf( file,
"%s = %d\n",
var->name,
*(unsigned*) var->value
);
else
fprintf( file,
"%s = %s\n",
var->name,
*(const char**) var->value
);
count++;
}
FIO_CloseFile( file );
return count;
}
struct config *
config_parse(
FILE * file
) {
char line_buf[ MAX_NAME_LEN + MAX_VALUE_LEN ];
struct config * config = 0;
int count = 0;
while( read_line( file, line_buf, sizeof(line_buf) ) >= 0 )
{
// Ignore any line that begins with # or is empty
if( line_buf[0] == '#'
|| line_buf[0] == '\0' )
continue;
struct config * new_config = config_parse_line( line_buf );
if( !new_config )
goto error;
new_config->next = config;
config = new_config;
count++;
config_auto_parse( config );
}
DebugMsg( DM_MAGIC, 3, "%s: Read %d config values", __func__, count );
return config;
error:
DebugMsg( DM_MAGIC, 3, "%s: ERROR Deleting config", __func__ );
while( config )
{
struct config * next = config->next;
DebugMsg( DM_MAGIC, 3, "%s: Deleting '%s' => '%s'",
__func__,
config->name,
config->value
);
free( config );
config = next;
}
return NULL;
}
char *
config_value(
struct config * config,
const char * name
)
{
while( config )
{
if( streq( config->name, name ) )
return config->value;
config = config->next;
}
return NULL;
}
int
config_int(
struct config * config,
const char * name,
int def
)
{
const char * str = config_value( config, name );
if( !str )
{
DebugMsg( DM_MAGIC, 3,
"Config '%s', using default %d",
name,
def
);
return def;
}
def = atoi( str );
DebugMsg( DM_MAGIC, 3,
"Config '%s', using user value %d ('%s')",
name,
def,
str
);
return def;
}
struct config head = { .name = "config.file", .value = "" };
struct config fail = { .name = "config.failure", .value = "1" };
struct config *
config_parse_file(
const char * filename
)
{
FILE * file = FIO_Open( filename, O_SYNC );
strcpy( head.value, filename );
if( file == INVALID_PTR )
{
head.next = &fail;
return &head;
}
struct config * config = config_parse( file );
FIO_CloseFile( file );
head.next = config;
return &head;
}
int
atoi(
const char * s
)
{
int value = 0;
// Only handles base ten for now
while( 1 )
{
char c = *s++;
if( !c || c < '0' || c > '9' )
break;
value = value * 10 + c - '0';
}
return value;
}