annotate cfgparser.c @ 147:0a0d7dd8fb51

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