annotate cfgparser.c @ 151:9708d4b2765b

cfgparser fix
author szabii
date Mon, 19 Mar 2001 02:29:37 +0000
parents 2f3e01a1fd87
children 372a9a836e86
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
106 if (config[i].flags & CONF_CHK_MIN)
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
110 if (config[i].flags & CONF_CHK_MAX)
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
125 if (config[i].flags & CONF_CHK_MIN)
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
129 if (config[i].flags & CONF_CHK_MAX)
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
140 if (config[i].flags & CONF_CHK_MIN)
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
144 if (config[i].flags & CONF_CHK_MAX)
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;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
163 default:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
164 printf("picsaba\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
165 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
166 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
167 return need_param;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
168 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
169
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
170 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
171 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
172 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
173 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
174 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
175 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
176 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
177 char *line;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
178 char opt[MAX_OPT_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
179 char param[MAX_PARAM_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
180 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
182 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
183 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
184 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
185 int ret = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
186
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
187 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
188 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
189 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
190 printf("Reading config file: %s\n", conffile);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
191
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
192 if (init_conf(conf, CONFIG_FILE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
193 return -1;
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 ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
196 perror("parse_config_file: can't get memory for 'line'");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
197 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
198 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
199
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
200 if ((fp = fopen(conffile, "r")) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
201 perror("parse_config_file: can't open filename");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
202 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
203 return 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
204 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
205
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
206 while (fgets(line, MAX_LINE_LEN, fp)) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
207 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
208 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
209
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
210 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
211 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
212 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
213
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
214 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
215 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
216 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
217
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
218 /* read option. accept char if isalnum(char) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
219 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
220 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
221 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
222 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
223 printf("too long option\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
224 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
225 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
226 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
227 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
228 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
229 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
230 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
231 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
232 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
233 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
234 opt[opt_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
235 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
236 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
237 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
238 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
239
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
240 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
241 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
242 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
243
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
244 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
245 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
246 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
247 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
248 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
249 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
250 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
251
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
252 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
253 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
254 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
255
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
256 /* read the parameter */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
257 for (param_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
258 param[param_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
259 if (param_pos >= MAX_PARAM_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
260 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
261 printf("too long parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
262 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
263 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
264 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
265 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
266 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
267
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
268 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
269 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
270 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
271 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
272 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
273 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
274 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
275 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
276 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
277 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
278 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
279 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
280 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
281 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
282 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
283
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
284 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
285 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
286 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
287 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
288 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
289 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
290
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
291 tmp = read_option(opt, param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
292 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
293 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
294 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
295 printf("invalid option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
296 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
297 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
298 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
299 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
300 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301 printf("missing parameter: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
302 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
303 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
304 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
305 case ERR_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
306 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
307 printf("parameter of %s out of range\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
310 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
311 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
312 PRINT_LINENUM;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
313 printf("parser function returned error: %s\n", opt);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
314 ret = -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
315 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
316 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
317 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
318 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
319
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
320 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
321 fclose(fp);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
322 return ret;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
323 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
324
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
325 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
326 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 int i;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
328 int found_filename = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
330 char *opt;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
332 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
333 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
334 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
335 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
336 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
337
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
338 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
339 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
340
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341 for (i = 1; i < argc; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
342 opt = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
343 if (*opt != '-')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
344 goto not_an_option;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 /* remove trailing '-' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
347 opt++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
348 #if 0
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
349 /* check for --help, -h, and --version */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351 printf("%s%s", banner_text, help_text);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
352 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
353 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 if (!strcasecmp(opt, "-version")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
355 printf("%s", banner_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 }
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
358 #endif
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
359
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
360 tmp = read_option(opt, argv[i + 1]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
361
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
362 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
363 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
364 not_an_option:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
365 /* opt is not an option -> treat it as a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
366 if (found_filename) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
367 /* we already have a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
368 printf("parse_command_line: invalid option: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
369 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
370 } else {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
371 found_filename = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
372 *filename = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
373 printf("parse_command_line: found filename: %s\n", *filename);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
374 continue; /* next option */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
375 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
376 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
377 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
378 printf("parse_command_line: missing parameter: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
379 return -1;
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_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
382 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
383 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
384 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
385 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
386 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
387 return -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
388 /* break; */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
390
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
391 i += tmp; /* we already processed the params (if there was any) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 return found_filename;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
394 }