annotate cfgparser.c @ 2281:faf96aeb93ef

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