1 | /* |
1 | /* |
2 | configuration.c - Part of psiconv, a PSION 5 file formats converter |
2 | configuration.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999, 2000,2003 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "config.h" |
20 | #include "config.h" |
|
|
21 | #include "compat.h" |
21 | #include "error.h" |
22 | #include "error.h" |
22 | #include "compat.h" |
|
|
23 | #include "unicode.h" |
23 | #include "unicode.h" |
24 | |
24 | |
25 | #include <stdio.h> |
25 | #include <stdio.h> |
26 | #include <stdlib.h> |
26 | #include <stdlib.h> |
27 | #include <string.h> |
27 | #include <string.h> |
|
|
28 | #include <strings.h> |
28 | #include <sys/types.h> |
29 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
30 | #include <sys/stat.h> |
30 | #include <unistd.h> |
31 | #include <unistd.h> |
31 | #include <fcntl.h> |
32 | #include <fcntl.h> |
32 | |
33 | |
… | |
… | |
35 | #ifdef DMALLOC |
36 | #ifdef DMALLOC |
36 | #include <dmalloc.h> |
37 | #include <dmalloc.h> |
37 | #endif |
38 | #endif |
38 | |
39 | |
39 | #ifndef CONFIGURATION_SEARCH_PATH |
40 | #ifndef CONFIGURATION_SEARCH_PATH |
40 | #define CONFIGURATION_SEARCH_PATH "/etc/psiconv.conf:~/.psiconv.conf" |
41 | #define CONFIGURATION_SEARCH_PATH PSICONVETCDIR "/psiconv.conf:~/.psiconv.conf" |
41 | #endif |
42 | #endif |
42 | static struct psiconv_config_s default_config = |
43 | static struct psiconv_config_s default_config = |
43 | { PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL,'?' }; |
44 | { PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL,'?' }; |
44 | |
45 | |
45 | static void psiconv_config_parse_statement(const char *filename, |
46 | static void psiconv_config_parse_statement(const char *filename, |
… | |
… | |
59 | result = malloc(sizeof(*result)); |
60 | result = malloc(sizeof(*result)); |
60 | *result = default_config; |
61 | *result = default_config; |
61 | return result; |
62 | return result; |
62 | } |
63 | } |
63 | |
64 | |
|
|
65 | void psiconv_config_free(psiconv_config config) |
|
|
66 | { |
|
|
67 | free(config); |
|
|
68 | } |
|
|
69 | |
64 | void psiconv_config_parse_statement(const char *filename, |
70 | void psiconv_config_parse_statement(const char *filename, |
65 | int linenr, |
71 | int linenr, |
66 | const char *var, int value, |
72 | const char *var, int value, |
67 | psiconv_config *config) |
73 | psiconv_config *config) |
68 | { |
74 | { |
69 | int charnr; |
75 | int charnr; |
70 | |
76 | |
71 | if (!(strcasecmp(var,"verbosity"))) { |
77 | if (!(strcasecmp(var,"verbosity"))) { |
72 | if ((value >= 1) && (value <= 4)) |
78 | if ((value >= 1) && (value <= 5)) |
73 | (*config)->verbosity = value; |
79 | (*config)->verbosity = value; |
74 | else |
80 | else |
75 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
81 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
76 | "Verbosity should be between 1 and 4",filename,linenr); |
82 | "Verbosity should be between 1 and 5",filename,linenr); |
77 | } else if (!(strcasecmp(var,"color"))) { |
83 | } else if (!(strcasecmp(var,"color"))) { |
78 | if ((value == 0) || (value == 1)) |
84 | if ((value == 0) || (value == 1)) |
79 | (*config)->color = value; |
85 | (*config)->color = value; |
80 | else |
86 | else |
81 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
87 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
82 | "Color should be 0 or 1",filename,linenr); |
88 | "Color should be 0 or 1",filename,linenr); |
83 | } else if (!(strcasecmp(var,"colordepth"))) { |
89 | } else if (!(strcasecmp(var,"colordepth"))) { |
84 | if ((value > 0) && (value <= 32)) |
90 | if ((value > 0) && (value <= 32)) |
85 | (*config)->colordepth = value; |
91 | (*config)->colordepth = value; |
86 | else |
92 | else |
87 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
93 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
88 | "ColorDepth should be between 1 and 32",filename,linenr); |
94 | "ColorDepth should be between 1 and 32",filename,linenr); |
89 | } else if (!(strcasecmp(var,"redbits"))) { |
95 | } else if (!(strcasecmp(var,"redbits"))) { |
90 | if ((value >= 0) && (value <= 32)) |
96 | if ((value >= 0) && (value <= 32)) |
91 | (*config)->redbits = value; |
97 | (*config)->redbits = value; |
92 | else |
98 | else |
93 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
99 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
94 | "RedBits should be between 1 and 32 or 0",filename,linenr); |
100 | "RedBits should be between 1 and 32 or 0",filename,linenr); |
95 | } else if (!(strcasecmp(var,"greenbits"))) { |
101 | } else if (!(strcasecmp(var,"greenbits"))) { |
96 | if ((value >= 0) && (value <= 32)) |
102 | if ((value >= 0) && (value <= 32)) |
97 | (*config)->greenbits = value; |
103 | (*config)->greenbits = value; |
98 | else |
104 | else |
99 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
105 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
100 | "GreenBits should be between 1 and 32 or 0",filename,linenr); |
106 | "GreenBits should be between 1 and 32 or 0",filename,linenr); |
101 | } else if (!(strcasecmp(var,"bluebits"))) { |
107 | } else if (!(strcasecmp(var,"bluebits"))) { |
102 | if ((value >= 0) && (value <= 32)) |
108 | if ((value >= 0) && (value <= 32)) |
103 | (*config)->bluebits = value; |
109 | (*config)->bluebits = value; |
104 | else |
110 | else |
105 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
111 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
106 | "BlueBits should be between 1 and 32 or 0",filename,linenr); |
112 | "BlueBits should be between 1 and 32 or 0",filename,linenr); |
107 | } else if (!(strcasecmp(var,"characterset"))) { |
113 | } else if (!(strcasecmp(var,"characterset"))) { |
108 | if ((value >= 0) && (value <= 1)) |
114 | if ((value >= 0) && (value <= 1)) |
109 | psiconv_unicode_select_characterset(*config,value); |
115 | psiconv_unicode_select_characterset(*config,value); |
110 | else |
116 | else |
111 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
117 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
112 | "CharacterSet should be between 0 and 0", |
118 | "CharacterSet should be between 0 and 1", |
113 | filename,linenr); |
119 | filename,linenr); |
114 | } else if (!(strcasecmp(var,"unknownunicodechar"))) { |
120 | } else if (!(strcasecmp(var,"unknownunicodechar"))) { |
115 | if ((value >= 1) && (value < 0x10000)) |
121 | if ((value >= 1) && (value < 0x10000)) |
116 | (*config)->unknown_unicode_char = value; |
122 | (*config)->unknown_unicode_char = value; |
117 | else |
123 | else |
118 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
124 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
119 | "UnknownUnicodeChar should be between 1 and 65535", |
125 | "UnknownUnicodeChar should be between 1 and 65535", |
120 | filename,linenr); |
126 | filename,linenr); |
121 | } else if (!(strcasecmp(var,"unknownepocchar"))) { |
127 | } else if (!(strcasecmp(var,"unknownepocchar"))) { |
122 | if ((value >= 1) && (value < 0x100)) |
128 | if ((value >= 1) && (value < 0x100)) |
123 | (*config)->unknown_epoc_char = value; |
129 | (*config)->unknown_epoc_char = value; |
124 | else |
130 | else |
125 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
131 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
126 | "UnknownEPOCChar should be between 1 and 255", |
132 | "UnknownEPOCChar should be between 1 and 255", |
127 | filename,linenr); |
133 | filename,linenr); |
128 | } else if (sscanf(var,"char%d",&charnr) == strlen(var)) { |
134 | } else if (sscanf(var,"char%d",&charnr) == strlen(var)) { |
129 | if ((charnr < 0) || (charnr > 255)) |
135 | if ((charnr < 0) || (charnr > 255)) |
130 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
136 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
131 | "CharXXX should have XXX between 0 and 255", |
137 | "CharXXX should have XXX between 0 and 255", |
132 | filename,linenr); |
138 | filename,linenr); |
133 | if ((value >= 1) && (value <= 0x10000)) |
139 | if ((value >= 1) && (value <= 0x10000)) |
134 | (*config)->unicode_table[charnr] = value; |
140 | (*config)->unicode_table[charnr] = value; |
135 | else |
141 | else |
136 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
142 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
137 | "CharXXX should be between 1 and 65535", |
143 | "CharXXX should be between 1 and 65535", |
138 | filename,linenr); |
144 | filename,linenr); |
139 | } else { |
145 | } else { |
140 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
146 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
141 | "Unknown variable %s",filename,linenr,var); |
147 | "Unknown variable %s",filename,linenr,var); |
142 | } |
148 | } |
143 | psiconv_debug(*config,0,0,"Configuration file %s, line %d: " |
149 | psiconv_debug(*config,0,0,"Configuration file %s, line %d: " |
144 | "Set variable %s to %d",filename,linenr,var,value); |
150 | "Set variable %s to %d",filename,linenr,var,value); |
145 | } |
151 | } |
… | |
… | |
161 | return; |
167 | return; |
162 | eovar = sovar; |
168 | eovar = sovar; |
163 | while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || |
169 | while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || |
164 | ((line[eovar] >= 'a') && (line[eovar] <= 'z')))) |
170 | ((line[eovar] >= 'a') && (line[eovar] <= 'z')))) |
165 | eovar ++; |
171 | eovar ++; |
166 | if (sovar == eovar) |
172 | if (sovar == eovar) { |
167 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
173 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
168 | "Syntax error (no variable found)",filename,linenr); |
174 | "Syntax error (no variable found)",filename,linenr); |
|
|
175 | return; |
|
|
176 | } |
169 | soval = eovar; |
177 | soval = eovar; |
170 | while (line[soval] && (line[soval] <= 32)) |
178 | while (line[soval] && (line[soval] <= 32)) |
171 | soval ++; |
179 | soval ++; |
172 | if (line[soval] != '=') |
180 | if (line[soval] != '=') { |
173 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
181 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
174 | "Syntax error (no = token found)",filename,linenr); |
182 | "Syntax error (no = token found)",filename,linenr); |
|
|
183 | return; |
|
|
184 | } |
175 | soval ++; |
185 | soval ++; |
176 | while (line[soval] && (line[soval] <= 32)) |
186 | while (line[soval] && (line[soval] <= 32)) |
177 | soval ++; |
187 | soval ++; |
178 | eoval = soval; |
188 | eoval = soval; |
179 | while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9'))) |
189 | while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9'))) |
180 | eoval ++; |
190 | eoval ++; |
181 | if (eoval == soval) |
191 | if (eoval == soval) { |
182 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
192 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
183 | "Syntax error (no value found)",filename,linenr); |
193 | "Syntax error (no value found)",filename,linenr); |
|
|
194 | return; |
|
|
195 | } |
184 | if (soval - eoval > 7) |
196 | if (soval - eoval > 7) { |
185 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
197 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
186 | "Syntax error (value too large)",filename,linenr); |
198 | "Syntax error (value too large)",filename,linenr); |
|
|
199 | return; |
|
|
200 | } |
187 | eol = eoval; |
201 | eol = eoval; |
188 | while (line[eol] && (line[eol] < 32)) |
202 | while (line[eol] && (line[eol] < 32)) |
189 | eol ++; |
203 | eol ++; |
190 | if (line[eol]) |
204 | if (line[eol]) { |
191 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
205 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
192 | "Syntax error (trailing garbage)",filename,linenr); |
206 | "Syntax error (trailing garbage)",filename,linenr); |
|
|
207 | return; |
|
|
208 | } |
193 | |
209 | |
194 | var = malloc(eovar - sovar + 1); |
210 | var = malloc(eovar - sovar + 1); |
195 | memcpy(var,line + sovar, eovar - sovar); |
211 | memcpy(var,line + sovar, eovar - sovar); |
196 | var[eovar-sovar] = 0; |
212 | var[eovar-sovar] = 0; |
197 | |
213 | |
… | |
… | |
212 | psiconv_progress(*config,0,0, |
228 | psiconv_progress(*config,0,0, |
213 | "Going to access configuration file %s",filename); |
229 | "Going to access configuration file %s",filename); |
214 | |
230 | |
215 | /* Try to open the file; it may fail, if it does not exist for example */ |
231 | /* Try to open the file; it may fail, if it does not exist for example */ |
216 | if ((file = open(filename,O_RDONLY)) == -1) |
232 | if ((file = open(filename,O_RDONLY)) == -1) |
217 | return; |
233 | goto ERROR0; |
218 | |
234 | |
219 | /* Read the contents of the file into filebuffer. This may fail */ |
235 | /* Read the contents of the file into filebuffer. This may fail */ |
220 | if (fstat(file,&stat_buf)) { |
236 | if (fstat(file,&stat_buf)) { |
221 | if (close(file)) |
237 | if (close(file)) |
222 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
238 | psiconv_error(*config,0,0,"Configuration file %s: " |
223 | "Couldn't close file",filename); |
239 | "Couldn't close file",filename); |
224 | return; |
240 | return; |
225 | } |
241 | } |
226 | |
242 | |
227 | filesize = stat_buf.st_size; |
243 | filesize = stat_buf.st_size; |
228 | if (!(filebuffer = malloc(filesize + 1))) |
244 | if (!(filebuffer = malloc(filesize + 1))) { |
229 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
245 | psiconv_error(*config,0,0,"Configuration file %s: " |
230 | "Out of memory error",filename); |
246 | "Out of memory error",filename); |
|
|
247 | goto ERROR1; |
|
|
248 | } |
|
|
249 | |
231 | filebuffer_ptr = filebuffer; |
250 | filebuffer_ptr = filebuffer; |
232 | bytes_left = filesize; |
251 | bytes_left = filesize; |
233 | bytes_read = 1; /* Dummy for the first time through the loop */ |
252 | bytes_read = 1; /* Dummy for the first time through the loop */ |
234 | while ((bytes_read > 0) && bytes_left) { |
253 | while ((bytes_read > 0) && bytes_left) { |
235 | bytes_read = read(file,filebuffer_ptr,bytes_left); |
254 | bytes_read = read(file,filebuffer_ptr,bytes_left); |
… | |
… | |
239 | } |
258 | } |
240 | } |
259 | } |
241 | |
260 | |
242 | /* On NFS, the first read may fail and this is not fatal */ |
261 | /* On NFS, the first read may fail and this is not fatal */ |
243 | if (bytes_left && (bytes_left != filesize)) { |
262 | if (bytes_left && (bytes_left != filesize)) { |
244 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
263 | psiconv_error(*config,0,0,"Configuration file %s: " |
245 | "Couldn't read file into memory",filename); |
264 | "Couldn't read file into memory",filename); |
|
|
265 | goto ERROR2; |
246 | } |
266 | } |
247 | |
267 | |
248 | if (close(file)) |
268 | if (close(file)) { |
249 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
269 | psiconv_error(*config,0,0,"Configuration file %s: " |
250 | "Couldn't close file",filename); |
270 | "Couldn't close file",filename); |
|
|
271 | file = -1; |
|
|
272 | goto ERROR2; |
|
|
273 | } |
|
|
274 | file = -1; |
251 | |
275 | |
252 | psiconv_progress(*config,0,0, |
276 | psiconv_progress(*config,0,0, |
253 | "Going to parse configuration file %s: ",filename); |
277 | "Going to parse configuration file %s: ",filename); |
254 | /* Now we walk through the file to isolate lines */ |
278 | /* Now we walk through the file to isolate lines */ |
255 | linenr = 0; |
279 | linenr = 0; |
… | |
… | |
260 | eol = sol; |
284 | eol = sol; |
261 | while ((eol < filesize) && (filebuffer[eol] != 13) && |
285 | while ((eol < filesize) && (filebuffer[eol] != 13) && |
262 | (filebuffer[eol] != 10) && (filebuffer[eol] != 0)) |
286 | (filebuffer[eol] != 10) && (filebuffer[eol] != 0)) |
263 | eol ++; |
287 | eol ++; |
264 | |
288 | |
265 | if ((eol < filesize) && (filebuffer[eol] == 0)) |
289 | if ((eol < filesize) && (filebuffer[eol] == 0)) { |
266 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
290 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
267 | "Unexpected character \000 found",filename,linenr); |
291 | "Unexpected character \000 found",filename,linenr); |
|
|
292 | goto ERROR2; |
|
|
293 | } |
268 | if ((eol < filesize + 1) && |
294 | if ((eol < filesize + 1) && |
269 | (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || |
295 | (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || |
270 | ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { |
296 | ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { |
271 | filebuffer[eol] = 0; |
297 | filebuffer[eol] = 0; |
272 | eol ++; |
298 | eol ++; |
… | |
… | |
274 | filebuffer[eol] = 0; |
300 | filebuffer[eol] = 0; |
275 | psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); |
301 | psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); |
276 | sol = eol+1; |
302 | sol = eol+1; |
277 | } |
303 | } |
278 | free(filebuffer); |
304 | free(filebuffer); |
|
|
305 | return; |
|
|
306 | |
|
|
307 | ERROR2: |
|
|
308 | free(filebuffer); |
|
|
309 | ERROR1: |
|
|
310 | if ((file != -1) && close(file)) |
|
|
311 | psiconv_error(*config,0,0,"Configuration file %s: " |
|
|
312 | "Couldn't close file",filename); |
|
|
313 | ERROR0: |
|
|
314 | return; |
279 | } |
315 | } |
280 | |
316 | |
281 | void psiconv_config_read(const char *extra_config_files, |
317 | void psiconv_config_read(const char *extra_config_files, |
282 | psiconv_config *config) |
318 | psiconv_config *config) |
283 | { |
319 | { |
… | |
… | |
321 | } |
357 | } |
322 | |
358 | |
323 | psiconv_config_parse_file(filename,config); |
359 | psiconv_config_parse_file(filename,config); |
324 | free(filename); |
360 | free(filename); |
325 | } |
361 | } |
|
|
362 | free(path); |
326 | } |
363 | } |