annotate cfgparser.c @ 1093:545a55a50885

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