annotate cfgparser.c @ 150:2f3e01a1fd87

cfgparse fixes
author szabii
date Mon, 19 Mar 2001 01:49:44 +0000
parents 0a0d7dd8fb51
children 9708d4b2765b
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;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
83 if (!strcasecmp(param, "no") ||
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;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
90 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
91 } else { /* parser_mode == COMMAND_LINE */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
92 *((int *) config[i].p) = config[i].max;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
93 need_param = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
94 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
95 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
96 case CONF_TYPE_INT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
97 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
98 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
99 if (!isdigit(*param))
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
102 tmp_int = atoi(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 if (config[i].flags & CONF_CHK_MIN)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
105 if (tmp_int < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
106 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
107
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
108 if (config[i].flags & CONF_CHK_MAX)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
109 if (tmp_int > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
110 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
111
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
112 *((int *) config[i].p) = tmp_int;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
113 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
114 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
115 case CONF_TYPE_FLOAT:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
116 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
117 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
118 if (!isdigit(*param))
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
121 tmp_float = atof(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 if (config[i].flags & CONF_CHK_MIN)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
124 if (tmp_float < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
125 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
126
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
127 if (config[i].flags & CONF_CHK_MAX)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
128 if (tmp_float > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
129 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
130
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
131 *((float *) config[i].p) = tmp_float;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
132 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
133 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
134 case CONF_TYPE_STRING:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
135 if (param == NULL)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
136 return ERR_MISSING_PARAM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
137
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
138 if (config[i].flags & CONF_CHK_MIN)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
139 if (strlen(param) < config[i].min)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
140 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
141
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
142 if (config[i].flags & CONF_CHK_MAX)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
143 if (strlen(param) > config[i].max)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
144 return ERR_OUT_OF_RANGE;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
145
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
146 *((char **) config[i].p) = strdup(param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
147 need_param = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
148 break;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
149 case CONF_TYPE_FUNC:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
150 if (config[i].flags & CONF_FUNC_PARAM) {
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
151 if (param == NULL)
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
152 return ERR_MISSING_PARAM;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
153 if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0)
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
154 return ERR_FUNC_ERR;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
155 need_param = 1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
156 } else {
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
157 if ((((cfg_func_t) config[i].p)(config + i)) < 0)
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
158 return ERR_FUNC_ERR;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
159 need_param = 0;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
160 }
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
161 break;
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
162 default:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
163 printf("picsaba\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
164 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
165 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
166 return need_param;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
167 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
168
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
169 int parse_config_file(struct config *conf, char *conffile)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
170 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
171 #define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
172 #define MAX_LINE_LEN 1000
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
173 #define MAX_OPT_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
174 #define MAX_PARAM_LEN 100
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
175 FILE *fp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
176 char *line;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
177 char opt[MAX_OPT_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
178 char param[MAX_PARAM_LEN];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
179 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
180 int line_num = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
181 int line_pos; /* line pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
182 int opt_pos; /* opt pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
183 int param_pos; /* param pos */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
184 int ret = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
185
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
186 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
187 assert(conffile != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
188 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
189 printf("Reading config file: %s\n", conffile);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
190
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
191 if (init_conf(conf, CONFIG_FILE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
192 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
193
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
194 if ((line = (char *) malloc(MAX_LINE_LEN)) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
195 perror("parse_config_file: can't get memory for 'line'");
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
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
199 if ((fp = fopen(conffile, "r")) == NULL) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
200 perror("parse_config_file: can't open filename");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
201 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
202 return 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
203 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
204
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
205 while (fgets(line, MAX_LINE_LEN, fp)) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
206 line_num++;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
207 line_pos = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
208
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
209 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
210 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
211 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
212
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
213 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
214 if (line[line_pos] == '\0' || line[line_pos] == '#')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
215 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
216
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
217 /* read option. accept char if isalnum(char) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
218 for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
219 opt[opt_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
220 if (opt_pos >= MAX_OPT_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
221 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
222 printf("too long option\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
223 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
224 continue;
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 if (opt_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
228 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
229 printf("parse error\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
230 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
231 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
232 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
233 opt[opt_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
234 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
235 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
236 printf("option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
237 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
238
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
239 /* skip whitespaces */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
240 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
241 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
242
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
243 /* check '=' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
244 if (line[line_pos++] != '=') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
245 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
246 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
247 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
248 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
249 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
250
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
251 /* whitespaces... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
252 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
253 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
254
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
255 /* read the parameter */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
256 for (param_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
257 param[param_pos++] = line[line_pos++];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
258 if (param_pos >= MAX_PARAM_LEN) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
259 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
260 printf("too long parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
261 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
262 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
263 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
264 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
265 param[param_pos] = '\0';
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
266
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
267 /* did we read a parameter? */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
268 if (param_pos == 0) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
269 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
270 printf("option without parameter\n");
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
271 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
272 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
273 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
274 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
275 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
276 printf("parameter: %s\n", param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
277 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
278 /* now, check if we have some more chars on the line */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
279 /* whitespace... */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
280 while (isspace(line[line_pos]))
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
281 ++line_pos;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
282
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
283 /* EOL / comment */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
284 if (line[line_pos] != '\0' && line[line_pos] != '#') {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
285 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
286 printf("extra characters on line: %s\n", line+line_pos);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
287 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
288 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
289
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
290 tmp = read_option(opt, param);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
291 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
292 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
293 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
294 printf("invalid option: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
295 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
296 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
297 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
298 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
299 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
300 printf("missing parameter: %s\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
301 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
302 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
303 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
304 case ERR_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
305 PRINT_LINENUM;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
306 printf("parameter of %s out of range\n", opt);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
307 ret = -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
308 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
309 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
310 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
311 PRINT_LINENUM;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
312 printf("parser function returned error: %s\n", opt);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
313 ret = -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
314 continue;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
315 /* break */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
316 }
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 free(line);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
320 fclose(fp);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
321 return ret;
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 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
325 {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
326 int i;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
327 int found_filename = 0;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
328 int tmp;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
329 char *opt;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
330
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
331 #ifdef DEBUG
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
332 assert(argv != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
333 assert(envp != NULL);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
334 assert(argc >= 1);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
335 #endif
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
336
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
337 if (init_conf(conf, COMMAND_LINE) == -1)
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
338 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
339
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
340 for (i = 1; i < argc; i++) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
341 opt = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
342 if (*opt != '-')
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
343 goto not_an_option;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
344
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
345 /* remove trailing '-' */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
346 opt++;
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
347 #if 0
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
348 /* check for --help, -h, and --version */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
349 if (!strcasecmp(opt, "-help") || !strcasecmp(opt, "h")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
350 printf("%s%s", banner_text, help_text);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
351 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
352 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
353 if (!strcasecmp(opt, "-version")) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
354 printf("%s", banner_text);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
355 continue;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
356 }
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
357 #endif
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
358
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
359 tmp = read_option(opt, argv[i + 1]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
360
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
361 switch (tmp) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
362 case ERR_NOT_AN_OPTION:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
363 not_an_option:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
364 /* opt is not an option -> treat it as a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
365 if (found_filename) {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
366 /* we already have a filename */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
367 printf("parse_command_line: invalid option: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
368 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
369 } else {
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
370 found_filename = 1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
371 *filename = argv[i];
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
372 printf("parse_command_line: found filename: %s\n", *filename);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
373 continue; /* next option */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
374 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
375 break;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
376 case ERR_MISSING_PARAM:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
377 printf("parse_command_line: missing parameter: %s\n", argv[i]);
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
378 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
379 /* break; */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
380 case ERR_OUT_OF_RANGE:
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
381 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
382 return -1;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
383 /* break; */
150
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
384 case ERR_FUNC_ERR:
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
385 printf("parse_command_line: parser function returned error: %s\n", argv[i]);
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
386 return -1;
2f3e01a1fd87 cfgparse fixes
szabii
parents: 147
diff changeset
387 /* break; */
147
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
388 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
389
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
390 i += tmp; /* we already processed the params (if there was any) */
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
391 }
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
392 return found_filename;
0a0d7dd8fb51 new command line/config file parser
szabii
parents:
diff changeset
393 }