annotate cfgparser.c @ 2557:6a74454ea121

some cleanup
author arpi
date Tue, 30 Oct 2001 17:04:59 +0000
parents c1f71b8190d1
children fc7985beff39
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
1 /*
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
2 * command line and config file parser
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
3 * by Szabolcs Berecz <szabi@inf.elte.hu>
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
4 * (C) 2001
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
5 */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
6
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
7 //#define DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
8
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
9 #include <stdlib.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
10 #include <stdio.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
11 #include <ctype.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
12 #include <unistd.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
13 #include <fcntl.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
14 #include <string.h>
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
15 #include <errno.h>
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
16
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
17
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
18 #define COMMAND_LINE 0
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
19 #define CONFIG_FILE 1
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
20
177
szabii
parents: 167
diff changeset
21 #define MAX_RECURSION_DEPTH 8
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
22
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
23 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
24 #include <assert.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
25 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
26
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
27 #include "cfgparser.h"
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
28
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
29 static struct config *config;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
30 static int nr_options; /* number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
31 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
32 static int recursion_depth = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
33
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
34 static int init_conf(struct config *conf, int mode)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
35 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
36 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
37 assert(conf != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
38 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
39
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
40 /* calculate the number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
41 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
42 /* NOTHING */;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
43
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
44 config = conf;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
45 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
46 if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
47 printf("init_conf: wrong mode!\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
48 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
49 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
50 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
51 parser_mode = mode;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
52 return 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
53 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
54
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
55 static int read_option(char *opt, char *param)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
56 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
57 int i;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
58 long tmp_int;
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
59 double tmp_float;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
60 int ret = -1;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
61 char *endptr;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
62
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
63 for (i = 0; i < nr_options; i++) {
1536
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
64 int namelength;
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
65 /* allow 'aa*' in config.name */
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
66 namelength=strlen(config[i].name);
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
67 if ( (config[i].name[namelength-1]=='*') &&
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
68 !memcmp(opt, config[i].name, namelength-1))
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
69 break;
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
70
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
71
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
72 if (!strcasecmp(opt, config[i].name))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
73 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
74 }
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
75 if (i == nr_options) {
2380
c1f71b8190d1 fix error reporting bug
szabi
parents: 2031
diff changeset
76 if (parser_mode == CONFIG_FILE)
c1f71b8190d1 fix error reporting bug
szabi
parents: 2031
diff changeset
77 printf("invalid option:\n");
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
78 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
79 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
80 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
81 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
82 printf("this option can only be used on command line:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
83 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
84 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
85 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
86 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
87 printf("this option can only be used in config file:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
88 ret = ERR_NOT_AN_OPTION;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
89 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
90 }
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
91
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
92 switch (config[i].type) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
93 case CONF_TYPE_FLAG:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
94 /* flags need a parameter in config file */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
95 if (parser_mode == CONFIG_FILE) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
96 if (!strcasecmp(param, "yes") || /* any other language? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
97 !strcasecmp(param, "ja") ||
161
9008302e2dc0 Added support for spanish (Yes, I'm a C programer also ;o)
telenieko
parents: 160
diff changeset
98 !strcasecmp(param, "si") ||
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
99 !strcasecmp(param, "igen") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
100 !strcasecmp(param, "y") ||
1536
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
101 !strcasecmp(param, "j") ||
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
102 !strcasecmp(param, "i") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
103 !strcmp(param, "1"))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
104 *((int *) config[i].p) = config[i].max;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
105 else if (!strcasecmp(param, "no") ||
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
106 !strcasecmp(param, "nein") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
107 !strcasecmp(param, "nicht") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
108 !strcasecmp(param, "nem") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
109 !strcasecmp(param, "n") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
110 !strcmp(param, "0"))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
111 *((int *) config[i].p) = config[i].min;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
112 else {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
113 printf("invalid parameter for flag:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
114 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
115 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
116 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
117 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
118 } else { /* parser_mode == COMMAND_LINE */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
119 *((int *) config[i].p) = config[i].max;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
120 ret = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
121 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
122 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
123 case CONF_TYPE_INT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
124 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
125 goto err_missing_param;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
126
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
127 tmp_int = strtol(param, &endptr, 0);
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
128 if (*endptr) {
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
129 printf("parameter must be an integer:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
130 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
131 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
132 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
133
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
134 if (config[i].flags & CONF_MIN)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
135 if (tmp_int < config[i].min) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
136 printf("parameter must be >= %d:\n", (int) config[i].min);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
137 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
138 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
139 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
140
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
141 if (config[i].flags & CONF_MAX)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
142 if (tmp_int > config[i].max) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
143 printf("parameter must be <= %d:\n", (int) config[i].max);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
144 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
145 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
146 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
148 *((int *) config[i].p) = tmp_int;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
149 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
150 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
151 case CONF_TYPE_FLOAT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
152 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
153 goto err_missing_param;
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
154
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
155 tmp_float = strtod(param, &endptr);
2031
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
156
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
157 if ((*endptr == ':') || (*endptr == '/'))
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
158 tmp_float /= strtod(endptr+1, &endptr);
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
159
195
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
160 if (*endptr) {
2031
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
161 printf("parameter must be a floating point number"
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
162 " or a ratio (numerator[:/]denominator):\n");
624df8ea0e0e New aspect prescale code, parses aspect value from mpeg sequence header or commandline.
atmos4
parents: 1629
diff changeset
163
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
164 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
165 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
166 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
167
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
168 if (config[i].flags & CONF_MIN)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
169 if (tmp_float < config[i].min) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
170 printf("parameter must be >= %f:\n", config[i].min);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
171 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
172 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
173 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
174
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
175 if (config[i].flags & CONF_MAX)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
176 if (tmp_float > config[i].max) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
177 printf("parameter must be <= %f:\n", config[i].max);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
178 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
179 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
180 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
182 *((float *) config[i].p) = tmp_float;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
183 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
184 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
185 case CONF_TYPE_STRING:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
186 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
187 goto err_missing_param;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
188
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
189 if (config[i].flags & CONF_MIN)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
190 if (strlen(param) < config[i].min) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
191 printf("parameter must be >= %d chars:\n",
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
192 (int) config[i].min);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
193 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
194 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
195 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
196
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
197 if (config[i].flags & CONF_MAX)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
198 if (strlen(param) > config[i].max) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
199 printf("parameter must be <= %d chars:\n",
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
200 (int) config[i].max);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
201 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
202 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
203 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
204
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
205 *((char **) config[i].p) = strdup(param);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
206 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
207 break;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
208 case CONF_TYPE_FUNC_PARAM:
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
209 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
210 goto err_missing_param;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
211 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
212 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
213 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
214 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
215 ret = 1;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
216 break;
1536
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
217 case CONF_TYPE_FUNC_FULL:
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
218 if (param!=NULL && param[0]=='-'){
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
219 ret=((cfg_func_arg_param_t) config[i].p)(config + i, opt, NULL);
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
220 if (ret>=0) ret=0;
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
221 /* if we return >=0: param is processed again (if there is any) */
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
222 }else{
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
223 ret=((cfg_func_arg_param_t) config[i].p)(config + i, opt, param);
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
224 /* if we return 0: need no param, precess it again */
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
225 /* if we return 1: accepted param */
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
226 }
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
227 break;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
228 case CONF_TYPE_FUNC:
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
229 if ((((cfg_func_t) config[i].p)(config + i)) < 0) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
230 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
231 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
232 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
233 ret = 0;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
234 break;
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
235 case CONF_TYPE_PRINT:
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
236 printf("%s", (char *) config[i].p);
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
237 exit(1);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
238 default:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
239 printf("picsaba\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
240 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
241 }
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
242 out:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
243 return ret;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
244 err_missing_param:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
245 printf("missing parameter:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
246 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
247 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
248 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
249
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
250 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
251 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
252 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
253 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
254 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
255 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
256 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
257 char *line;
179
szabii
parents: 177
diff changeset
258 char opt[MAX_OPT_LEN + 1];
szabii
parents: 177
diff changeset
259 char param[MAX_PARAM_LEN + 1];
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
260 char c; /* for the "" and '' check */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
261 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
262 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
263 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
264 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
265 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
266 int ret = 1;
1090
szabii
parents: 1089
diff changeset
267 int errors = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
268
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
269 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
270 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
271 #endif
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
272 if (++recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
273 printf("Reading config file: %s", conffile);
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
274
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
275 if (recursion_depth > MAX_RECURSION_DEPTH) {
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
276 printf(": too deep 'include'. check your configfiles\n");
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
277 ret = -1;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
278 goto out;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
279 }
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
280
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
281 if (init_conf(conf, CONFIG_FILE) == -1) {
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
282 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
283 goto out;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
284 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
285
179
szabii
parents: 177
diff changeset
286 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
287 perror("\ncan't get memory for 'line'");
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
288 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
289 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
290 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
291
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
292 if ((fp = fopen(conffile, "r")) == NULL) {
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
293 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
294 printf(": %s\n", strerror(errno));
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
295 free(line);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
296 ret = 0;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
297 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
298 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
299 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
300 printf("\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
302 while (fgets(line, MAX_LINE_LEN, fp)) {
1090
szabii
parents: 1089
diff changeset
303 if (errors >= 16) {
szabii
parents: 1089
diff changeset
304 printf("too many errors\n");
szabii
parents: 1089
diff changeset
305 goto out;
szabii
parents: 1089
diff changeset
306 }
szabii
parents: 1089
diff changeset
307
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
311 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
312 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
313 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
314
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
315 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
316 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
317 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
318
480
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
319 /* read option. */
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
320 for (opt_pos = 0; isprint(line[line_pos]) &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
321 line[line_pos] != ' ' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
322 line[line_pos] != '#' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
323 line[line_pos] != '='; /* NOTHING */) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 printf("too long option\n");
1090
szabii
parents: 1089
diff changeset
328 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 ret = -1;
1090
szabii
parents: 1089
diff changeset
330 goto nextline;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
332 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
333 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
334 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
335 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
336 ret = -1;
1090
szabii
parents: 1089
diff changeset
337 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
338 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
339 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
340 opt[opt_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
342 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
343 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
344 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
347 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
348 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
349
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
352 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
353 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 ret = -1;
1090
szabii
parents: 1089
diff changeset
355 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
356 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
357 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
358
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
359 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
360 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
361 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
362
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
363 /* read the parameter */
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
364 if (line[line_pos] == '"' || line[line_pos] == '\'') {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
365 c = line[line_pos];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
366 ++line_pos;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
367 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
368 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
369 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
370 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
371 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
372 ret = -1;
1090
szabii
parents: 1089
diff changeset
373 errors++;
szabii
parents: 1089
diff changeset
374 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
375 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
376 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
377 line_pos++; /* skip the closing " or ' */
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
378 } else {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
379 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos])
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
380 && line[line_pos] != '#'; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
381 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
382 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
383 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
384 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
385 ret = -1;
1090
szabii
parents: 1089
diff changeset
386 errors++;
szabii
parents: 1089
diff changeset
387 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
388 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
390 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
391 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
395 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
396 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
397 ret = -1;
1090
szabii
parents: 1089
diff changeset
398 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
399 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
400 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
401 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
402 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
403 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
404 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
405 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
406 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
407 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
408 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
409
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
410 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
411 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
412 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
413 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
414 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
415 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
416
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
417 tmp = read_option(opt, param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
418 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
419 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
420 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
421 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
422 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
423 PRINT_LINENUM;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
424 printf("%s\n", opt);
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
425 ret = -1;
1090
szabii
parents: 1089
diff changeset
426 errors++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
427 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
428 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
429 }
1093
szabii
parents: 1090
diff changeset
430 nextline:
1304
ecb834719dc9 fix gcc-3.0 warning
jkeil
parents: 1093
diff changeset
431 ;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
432 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
433
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
434 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
435 fclose(fp);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
436 out:
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
437 --recursion_depth;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
438 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
439 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
440
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
441 int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
442 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
443 int i;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
444 char **f = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
445 int f_nr = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
446 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
447 char *opt;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
448
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
449 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
450 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
451 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
452 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
453 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
454
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
455 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
456 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
457
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
458 /* in order to work recursion detection properly in parse_config_file */
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
459 ++recursion_depth;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
460
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
461 for (i = 1; i < argc; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
462 opt = argv[i];
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
463 if (*opt != '-')
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
464 goto filename;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
465
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
466 /* remove trailing '-' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
467 opt++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
468
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
469 tmp = read_option(opt, argv[i + 1]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
470
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
471 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
472 case ERR_NOT_AN_OPTION:
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
473 filename:
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
474 /* opt is not an option -> treat it as a filename */
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
475 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2))))
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
476 goto err_out_mem;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
477
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
478 f[f_nr++] = argv[i];
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
479 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
480 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
481 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
482 case ERR_FUNC_ERR:
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
483 goto err_out;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
484 /* break; */
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
485 default:
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
486 i += tmp;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
487 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
488 }
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
489 if (f)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
490 f[f_nr] = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
491 if (filenames)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
492 *filenames = f;
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
493 --recursion_depth;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
494 return f_nr; //filenames_nr;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
495 err_out_mem:
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
496 printf("can't allocate memory for filenames\n");
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
497 err_out:
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
498 --recursion_depth;
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
499 printf("command line: %s\n", argv[i]);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
500 return -1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
501 }