annotate cfgparser.c @ 158:05e9c8083922

parameter reading fixed in parse_config_file
author szabii
date Mon, 19 Mar 2001 13:12:48 +0000
parents 8e55121885b2
children 5f0c50a9e347
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
3 */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
4
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
5 //#define DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
6
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
7 #include <stdlib.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
8 #include <stdio.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
9 #include <ctype.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
10 #include <unistd.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
11 #include <fcntl.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
12 #include <string.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
13
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
14 #define ERR_NOT_AN_OPTION -1
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
15 #define ERR_MISSING_PARAM -2
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
16 #define ERR_OUT_OF_RANGE -3
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
17 #define ERR_FUNC_ERR -4
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
18
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
19 #define COMMAND_LINE 0
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
20 #define CONFIG_FILE 1
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
21
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
22 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
23 #include <assert.h>
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
24 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
25
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
26 #include "cfgparser.h"
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
27
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
28 static struct config *config;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
29 static int nr_options; /* number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
30 static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
31
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
32 static int init_conf(struct config *conf, int mode)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
33 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
34 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
35 assert(conf != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
36 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
37
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
38 /* calculate the number of options in 'conf' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
39 for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
40 /* NOTHING */;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
41
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
42 config = conf;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
43 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
44 if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
45 printf("init_conf: wrong mode!\n");
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
46 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
47 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
48 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
49 parser_mode = mode;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
50 return 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
51 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
52
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
53 static int read_option(char *opt, char *param)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
54 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
55 int i;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
56 int need_param = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
57 int tmp_int;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
58 float tmp_float;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
59
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
60 for (i = 0; i < nr_options; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
61 if (!strcasecmp(opt, config[i].name))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
62 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
63 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
64 if (i == nr_options)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
65 return ERR_NOT_AN_OPTION;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
66
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
67 if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE)
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
68 return ERR_NOT_AN_OPTION;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
69 if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE)
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
70 return ERR_NOT_AN_OPTION;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
71
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
72 switch (config[i].type) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
73 case CONF_TYPE_FLAG:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
74 /* flags need a parameter in config file */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
75 if (parser_mode == CONFIG_FILE) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
76 if (!strcasecmp(param, "yes") || /* any other language? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
77 !strcasecmp(param, "ja") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
78 !strcasecmp(param, "igen") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
79 !strcasecmp(param, "y") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
80 !strcasecmp(param, "i") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
81 !strcmp(param, "1"))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
82 *((int *) config[i].p) = config[i].max;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
83 else if (!strcasecmp(param, "no") ||
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
84 !strcasecmp(param, "nein") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
85 !strcasecmp(param, "nicht") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
86 !strcasecmp(param, "nem") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
87 !strcasecmp(param, "n") ||
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
88 !strcmp(param, "0"))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
89 *((int *) config[i].p) = config[i].min;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
90 else
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
91 return ERR_OUT_OF_RANGE;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
92 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
93 } else { /* parser_mode == COMMAND_LINE */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
94 *((int *) config[i].p) = config[i].max;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
95 need_param = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
96 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
97 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
98 case CONF_TYPE_INT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
99 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
100 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
101 if (!isdigit(*param))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
102 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
103
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
104 tmp_int = atoi(param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
105
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
106 if (config[i].flags & CONF_MIN)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
107 if (tmp_int < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
108 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
109
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
110 if (config[i].flags & CONF_MAX)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
111 if (tmp_int > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
112 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
113
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
114 *((int *) config[i].p) = tmp_int;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
115 need_param = 1;
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_FLOAT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
118 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
119 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
120 if (!isdigit(*param))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
121 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
122
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
123 tmp_float = atof(param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
124
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
125 if (config[i].flags & CONF_MIN)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
126 if (tmp_float < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
127 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
128
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
129 if (config[i].flags & CONF_MAX)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
130 if (tmp_float > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
131 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
132
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
133 *((float *) config[i].p) = tmp_float;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
134 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
135 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
136 case CONF_TYPE_STRING:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
137 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
138 return ERR_MISSING_PARAM;
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_MIN)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
141 if (strlen(param) < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
142 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
143
153
8e55121885b2 new configfile/cmdline parser
arpi_esp
parents: 152
diff changeset
144 if (config[i].flags & CONF_MAX)
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
145 if (strlen(param) > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
146 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
148 *((char **) config[i].p) = strdup(param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
149 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
150 break;
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
151 case CONF_TYPE_FUNC_PARAM:
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
152 if (param == NULL)
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
153 return ERR_MISSING_PARAM;
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
154 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0)
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
155 return ERR_FUNC_ERR;
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
156 need_param = 1;
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
157 break;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
158 case CONF_TYPE_FUNC:
151
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
159 if ((((cfg_func_t) config[i].p)(config + i)) < 0)
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
160 return ERR_FUNC_ERR;
9708d4b2765b cfgparser fix
szabii
parents: 150
diff changeset
161 need_param = 0;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
162 break;
152
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
163 case CONF_TYPE_PRINT:
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
164 printf("%s", (char *) config[i].p);
372a9a836e86 cfgparser fix
szabii
parents: 151
diff changeset
165 exit(1);
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
166 default:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
167 printf("picsaba\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
168 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
169 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
170 return need_param;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
171 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
172
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
173 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
174 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
175 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
176 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
177 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
178 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
179 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
180 char *line;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181 char opt[MAX_OPT_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
182 char param[MAX_PARAM_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
183 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
184 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
185 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
186 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
187 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
188 int ret = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
189
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
190 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
191 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
192 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
193 printf("Reading config file: %s\n", conffile);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
194
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
195 if (init_conf(conf, CONFIG_FILE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
196 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
197
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
198 if ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
199 perror("parse_config_file: can't get memory for 'line'");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
200 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
201 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
202
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
203 if ((fp = fopen(conffile, "r")) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
204 perror("parse_config_file: can't open filename");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
205 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
206 return 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
207 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
208
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
209 while (fgets(line, MAX_LINE_LEN, fp)) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
210 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
211 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
212
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
213 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
214 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
215 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
216
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
217 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
218 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
219 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
220
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
221 /* read option. accept char if isalnum(char) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
222 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
223 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
224 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
225 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
226 printf("too long option\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
227 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
228 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
229 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
230 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
231 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
232 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
233 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
234 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
235 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
236 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
237 opt[opt_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
238 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
239 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
240 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
241 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
242
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
243 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
244 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
245 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
246
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
247 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
248 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
249 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
250 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
251 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
252 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
253 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
254
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
255 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
256 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
257 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
258
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
259 /* read the parameter */
158
05e9c8083922 parameter reading fixed in parse_config_file
szabii
parents: 153
diff changeset
260 for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]);
05e9c8083922 parameter reading fixed in parse_config_file
szabii
parents: 153
diff changeset
261 /* NOTHING */) {
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
262 param[param_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
263 if (param_pos >= MAX_PARAM_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
264 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
265 printf("too long parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
266 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
267 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
268 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
269 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
270 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
271
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
272 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
273 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
274 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
275 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
276 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
277 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
278 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
279 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
280 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
281 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
282 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
283 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
284 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
285 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
286 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
287
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
288 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
289 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
290 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
291 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
292 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
293 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
294
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
295 tmp = read_option(opt, param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
296 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
297 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
298 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
299 printf("invalid option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
300 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
302 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
303 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
304 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
305 printf("missing parameter: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
306 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
307 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 case ERR_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
311 printf("parameter of %s out of range\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
312 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
313 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
314 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
315 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
316 PRINT_LINENUM;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
317 printf("parser function returned error: %s\n", opt);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
318 ret = -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
319 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
320 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
321 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
322 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
323
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 fclose(fp);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
328
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 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
330 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331 int i;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
332 int found_filename = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
333 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
334 char *opt;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
335
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
336 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
337 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
338 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
339 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
340 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
342 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
343 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
344
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345 for (i = 1; i < argc; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 opt = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
347 if (*opt != '-')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
348 goto not_an_option;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
349
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 /* remove trailing '-' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351 opt++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
352 #if 0
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
353 /* check for --help, -h, and --version */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
355 printf("%s%s", banner_text, help_text);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
356 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
357 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
358 if (!strcasecmp(opt, "-version")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
359 printf("%s", banner_text);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
360 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
361 }
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
362 #endif
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
363
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
364 tmp = read_option(opt, argv[i + 1]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
365
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
366 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
367 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
368 not_an_option:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
369 /* opt is not an option -> treat it as a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
370 if (found_filename) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
371 /* we already have a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
372 printf("parse_command_line: invalid option: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
373 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
374 } else {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
375 found_filename = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
376 *filename = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
377 printf("parse_command_line: found filename: %s\n", *filename);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
378 continue; /* next option */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
379 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
380 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
381 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
382 printf("parse_command_line: missing parameter: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
383 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
384 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
385 case ERR_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
386 printf("parse_command_line: parameter of '%s' is out of range\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
387 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
388 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
389 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
390 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
391 return -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
392 /* break; */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
395 i += tmp; /* we already processed the params (if there was any) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
396 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
397 return found_filename;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
398 }