comparison cfgparser.c @ 150:2f3e01a1fd87

cfgparse fixes
author szabii
date Mon, 19 Mar 2001 01:49:44 +0000
parents 0a0d7dd8fb51
children 9708d4b2765b
comparison
equal deleted inserted replaced
149:5762a1c7247b 150:2f3e01a1fd87
12 #include <string.h> 12 #include <string.h>
13 13
14 #define ERR_NOT_AN_OPTION -1 14 #define ERR_NOT_AN_OPTION -1
15 #define ERR_MISSING_PARAM -2 15 #define ERR_MISSING_PARAM -2
16 #define ERR_OUT_OF_RANGE -3 16 #define ERR_OUT_OF_RANGE -3
17 #define ERR_FUNC_ERR -4
17 18
18 #define COMMAND_LINE 0 19 #define COMMAND_LINE 0
19 #define CONFIG_FILE 1 20 #define CONFIG_FILE 1
20 21
21 #ifdef DEBUG 22 #ifdef DEBUG
22 #include <assert.h> 23 #include <assert.h>
23 #endif 24 #endif
24 25
25 #include "cfgparser.h" 26 #include "cfgparser.h"
26 #include "version.h"
27 #include "help_mp.h"
28 27
29 static struct config *config; 28 static struct config *config;
30 static int nr_options; /* number of options in 'conf' */ 29 static int nr_options; /* number of options in 'conf' */
31 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */ 30 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
32 31
41 /* NOTHING */; 40 /* NOTHING */;
42 41
43 config = conf; 42 config = conf;
44 #ifdef DEBUG 43 #ifdef DEBUG
45 if (mode != COMMAND_LINE && mode != CONFIG_FILE) { 44 if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
46 printf("init_conf: wrong flag!\n"); 45 printf("init_conf: wrong mode!\n");
47 return -1; 46 return -1;
48 } 47 }
49 #endif 48 #endif
50 parser_mode = mode; 49 parser_mode = mode;
51 return 1; 50 return 1;
61 for (i = 0; i < nr_options; i++) { 60 for (i = 0; i < nr_options; i++) {
62 if (!strcasecmp(opt, config[i].name)) 61 if (!strcasecmp(opt, config[i].name))
63 break; 62 break;
64 } 63 }
65 if (i == nr_options) 64 if (i == nr_options)
65 return ERR_NOT_AN_OPTION;
66
67 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE)
68 return ERR_NOT_AN_OPTION;
69 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE)
66 return ERR_NOT_AN_OPTION; 70 return ERR_NOT_AN_OPTION;
67 71
68 switch (config[i].type) { 72 switch (config[i].type) {
69 case CONF_TYPE_FLAG: 73 case CONF_TYPE_FLAG:
70 /* flags need a parameter in config file */ 74 /* flags need a parameter in config file */
140 return ERR_OUT_OF_RANGE; 144 return ERR_OUT_OF_RANGE;
141 145
142 *((char **) config[i].p) = strdup(param); 146 *((char **) config[i].p) = strdup(param);
143 need_param = 1; 147 need_param = 1;
144 break; 148 break;
149 case CONF_TYPE_FUNC:
150 if (config[i].flags & CONF_FUNC_PARAM) {
151 if (param == NULL)
152 return ERR_MISSING_PARAM;
153 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0)
154 return ERR_FUNC_ERR;
155 need_param = 1;
156 } else {
157 if ((((cfg_func_t) config[i].p)(config + i)) < 0)
158 return ERR_FUNC_ERR;
159 need_param = 0;
160 }
161 break;
145 default: 162 default:
146 printf("picsaba\n"); 163 printf("picsaba\n");
147 break; 164 break;
148 } 165 }
149 return need_param; 166 return need_param;
288 PRINT_LINENUM; 305 PRINT_LINENUM;
289 printf("parameter of %s out of range\n", opt); 306 printf("parameter of %s out of range\n", opt);
290 ret = -1; 307 ret = -1;
291 continue; 308 continue;
292 /* break; */ 309 /* break; */
310 case ERR_FUNC_ERR:
311 PRINT_LINENUM;
312 printf("parser function returned error: %s\n", opt);
313 ret = -1;
314 continue;
315 /* break */
293 } 316 }
294 } 317 }
295 318
296 free(line); 319 free(line);
297 fclose(fp); 320 fclose(fp);
319 if (*opt != '-') 342 if (*opt != '-')
320 goto not_an_option; 343 goto not_an_option;
321 344
322 /* remove trailing '-' */ 345 /* remove trailing '-' */
323 opt++; 346 opt++;
324 347 #if 0
325 /* check for --help, -h, and --version */ 348 /* check for --help, -h, and --version */
326 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) { 349 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
327 printf("%s%s", banner_text, help_text); 350 printf("%s%s", banner_text, help_text);
328 continue; 351 continue;
329 } 352 }
330 if (!strcasecmp(opt, "-version")) { 353 if (!strcasecmp(opt, "-version")) {
331 printf("%s", banner_text); 354 printf("%s", banner_text);
332 continue; 355 continue;
333 } 356 }
357 #endif
334 358
335 tmp = read_option(opt, argv[i + 1]); 359 tmp = read_option(opt, argv[i + 1]);
336 360
337 switch (tmp) { 361 switch (tmp) {
338 case ERR_NOT_AN_OPTION: 362 case ERR_NOT_AN_OPTION:
355 /* break; */ 379 /* break; */
356 case ERR_OUT_OF_RANGE: 380 case ERR_OUT_OF_RANGE:
357 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]); 381 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]);
358 return -1; 382 return -1;
359 /* break; */ 383 /* break; */
384 case ERR_FUNC_ERR:
385 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
386 return -1;
387 /* break; */
360 } 388 }
361 389
362 i += tmp; /* we already processed the params (if there was any) */ 390 i += tmp; /* we already processed the params (if there was any) */
363 } 391 }
364 return found_filename; 392 return found_filename;