annotate cfgparser.c @ 1896:e6acbf9e9b53

Translated by me, but not checked
author jaf
date Sun, 16 Sep 2001 08:53:30 +0000
parents 13aeaa05ac5e
children 624df8ea0e0e
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);
452453f82bfa strto* int/float reader
szabii
parents: 191
diff changeset
155 if (*endptr) {
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
156 printf("parameter must be a floating point number:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
157 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
158 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
159 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
160
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
161 if (config[i].flags & CONF_MIN)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
162 if (tmp_float < config[i].min) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
163 printf("parameter must be >= %f:\n", config[i].min);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
164 ret = ERR_OUT_OF_RANGE;
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_MAX)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
169 if (tmp_float > config[i].max) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
170 printf("parameter must be <= %f:\n", config[i].max);
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
175 *((float *) config[i].p) = tmp_float;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
176 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
177 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
178 case CONF_TYPE_STRING:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
179 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
180 goto err_missing_param;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
182 if (config[i].flags & CONF_MIN)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
183 if (strlen(param) < config[i].min) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
184 printf("parameter must be >= %d chars:\n",
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
185 (int) config[i].min);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
186 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
187 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
188 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
189
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
190 if (config[i].flags & CONF_MAX)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
191 if (strlen(param) > config[i].max) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
192 printf("parameter must be <= %d chars:\n",
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
193 (int) config[i].max);
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
194 ret = ERR_OUT_OF_RANGE;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
195 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
196 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
197
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
198 *((char **) config[i].p) = strdup(param);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
199 ret = 1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
200 break;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
201 case CONF_TYPE_FUNC_PARAM:
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
202 if (param == NULL)
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
203 goto err_missing_param;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
204 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
205 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
206 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
207 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
208 ret = 1;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
209 break;
1536
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
210 case CONF_TYPE_FUNC_FULL:
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
211 if (param!=NULL && param[0]=='-'){
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
212 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
213 if (ret>=0) ret=0;
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
214 /* 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
215 }else{
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
216 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
217 /* 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
218 /* if we return 1: accepted param */
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
219 }
e89233dab4da New feature for option processing: CONF_TYPE_FUNC_FULL
folke
parents: 1304
diff changeset
220 break;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
221 case CONF_TYPE_FUNC:
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
222 if ((((cfg_func_t) config[i].p)(config + i)) < 0) {
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
223 ret = ERR_FUNC_ERR;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
224 goto out;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
225 }
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
226 ret = 0;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
227 break;
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
228 case CONF_TYPE_PRINT:
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
229 printf("%s", (char *) config[i].p);
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
230 exit(1);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
231 default:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
232 printf("picsaba\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
233 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
234 }
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
235 out:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
236 return ret;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
237 err_missing_param:
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
238 printf("missing parameter:\n");
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
239 ret = ERR_MISSING_PARAM;
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
240 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
241 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
242
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
243 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
244 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
245 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
246 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
247 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
248 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
249 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
250 char *line;
179
szabii
parents: 177
diff changeset
251 char opt[MAX_OPT_LEN + 1];
szabii
parents: 177
diff changeset
252 char param[MAX_PARAM_LEN + 1];
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
253 char c; /* for the "" and '' check */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
254 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
255 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
256 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
257 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
258 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
259 int ret = 1;
1090
szabii
parents: 1089
diff changeset
260 int errors = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
261
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
262 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
263 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
264 #endif
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
265 if (++recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
266 printf("Reading config file: %s", conffile);
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
267
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
268 if (recursion_depth > MAX_RECURSION_DEPTH) {
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
269 printf(": too deep 'include'. check your configfiles\n");
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
270 ret = -1;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
271 goto out;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
272 }
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
273
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
274 if (init_conf(conf, CONFIG_FILE) == -1) {
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
275 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
276 goto out;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
277 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
278
179
szabii
parents: 177
diff changeset
279 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
336
e24fe1b5b918 less output
szabii
parents: 195
diff changeset
280 perror("\ncan't get memory for 'line'");
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
281 ret = -1;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
282 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
283 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
284
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
285 if ((fp = fopen(conffile, "r")) == NULL) {
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
286 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
287 printf(": %s\n", strerror(errno));
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
288 free(line);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
289 ret = 0;
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
290 goto out;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
291 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
292 if (recursion_depth > 1)
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
293 printf("\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
294
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
295 while (fgets(line, MAX_LINE_LEN, fp)) {
1090
szabii
parents: 1089
diff changeset
296 if (errors >= 16) {
szabii
parents: 1089
diff changeset
297 printf("too many errors\n");
szabii
parents: 1089
diff changeset
298 goto out;
szabii
parents: 1089
diff changeset
299 }
szabii
parents: 1089
diff changeset
300
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
302 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
303
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
304 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
305 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
306 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
307
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
311
480
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
312 /* read option. */
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
313 for (opt_pos = 0; isprint(line[line_pos]) &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
314 line[line_pos] != ' ' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
315 line[line_pos] != '#' &&
787f2792beeb now it accepts monitor_hfreq...
szabii
parents: 336
diff changeset
316 line[line_pos] != '='; /* NOTHING */) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
317 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
318 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
319 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
320 printf("too long option\n");
1090
szabii
parents: 1089
diff changeset
321 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
322 ret = -1;
1090
szabii
parents: 1089
diff changeset
323 goto nextline;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
328 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 ret = -1;
1090
szabii
parents: 1089
diff changeset
330 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
332 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
333 opt[opt_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
334 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
335 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
336 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
337 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
338
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
339 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
340 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
342
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
343 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
344 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
347 ret = -1;
1090
szabii
parents: 1089
diff changeset
348 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
349 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
352 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
353 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
355
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
356 /* read the parameter */
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
357 if (line[line_pos] == '"' || line[line_pos] == '\'') {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
358 c = line[line_pos];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
359 ++line_pos;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
360 for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
361 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
362 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
363 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
364 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
365 ret = -1;
1090
szabii
parents: 1089
diff changeset
366 errors++;
szabii
parents: 1089
diff changeset
367 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
368 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
369 }
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
370 line_pos++; /* skip the closing " or ' */
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
371 } else {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
372 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
373 && line[line_pos] != '#'; /* NOTHING */) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
374 param[param_pos++] = line[line_pos++];
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
375 if (param_pos >= MAX_PARAM_LEN) {
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
376 PRINT_LINENUM;
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
377 printf("too long parameter\n");
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
378 ret = -1;
1090
szabii
parents: 1089
diff changeset
379 errors++;
szabii
parents: 1089
diff changeset
380 goto nextline;
167
53f289e99102 parameter can be in quotes in config file
szabii
parents: 166
diff changeset
381 }
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
382 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
383 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
384 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
385
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
386 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
387 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
388 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
390 ret = -1;
1090
szabii
parents: 1089
diff changeset
391 errors++;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394 #ifdef DEBUG
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("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
397 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
398 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
399 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
400 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
401 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
402
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
403 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
404 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
405 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
406 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
407 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
408 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
409
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
410 tmp = read_option(opt, param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
411 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
412 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
413 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
414 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
415 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
416 PRINT_LINENUM;
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
417 printf("%s\n", opt);
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
418 ret = -1;
1090
szabii
parents: 1089
diff changeset
419 errors++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
420 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
421 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
422 }
1093
szabii
parents: 1090
diff changeset
423 nextline:
1304
ecb834719dc9 fix gcc-3.0 warning
jkeil
parents: 1093
diff changeset
424 ;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
425 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
426
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
427 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
428 fclose(fp);
166
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
429 out:
6b79d801e183 include recursion check
szabii
parents: 161
diff changeset
430 --recursion_depth;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
431 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
432 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
433
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
434 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
435 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
436 int i;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
437 char **f = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
438 int f_nr = 0;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
439 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
440 char *opt;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
441
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
442 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
443 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
444 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
445 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
446 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
447
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
448 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
449 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
450
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
451 /* in order to work recursion detection properly in parse_config_file */
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
452 ++recursion_depth;
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
453
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
454 for (i = 1; i < argc; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
455 opt = argv[i];
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
456 if (*opt != '-')
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
457 goto filename;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
458
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
459 /* remove trailing '-' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
460 opt++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
461
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
462 tmp = read_option(opt, argv[i + 1]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
463
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
464 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
465 case ERR_NOT_AN_OPTION:
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
466 filename:
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
467 /* opt is not an option -> treat it as a filename */
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
468 if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2))))
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
469 goto err_out_mem;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
470
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
471 f[f_nr++] = argv[i];
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
472 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
473 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
474 case ERR_OUT_OF_RANGE:
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
475 case ERR_FUNC_ERR:
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
476 goto err_out;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
477 /* break; */
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
478 default:
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
479 i += tmp;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
480 }
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
481 }
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
482 if (f)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
483 f[f_nr] = NULL;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
484 if (filenames)
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
485 *filenames = f;
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
486 --recursion_depth;
1629
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
487 return f_nr; //filenames_nr;
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
488 err_out_mem:
13aeaa05ac5e multifile support in config parser
arpi
parents: 1536
diff changeset
489 printf("can't allocate memory for filenames\n");
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
490 err_out:
1089
b2a29e1224e4 some fix
szabii
parents: 1077
diff changeset
491 --recursion_depth;
1075
a981413af7cd : No such... fix
szabii
parents: 1062
diff changeset
492 printf("command line: %s\n", argv[i]);
160
5f0c50a9e347 cfgparse fix...
szabii
parents: 158
diff changeset
493 return -1;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
494 }