1 |
frodo |
168 |
/* |
2 |
|
|
configuration.c - Part of psiconv, a PSION 5 file formats converter |
3 |
frodo |
270 |
Copyright (c) 1999-2005 Frodo Looijaard <frodo@frodo.looijaard.name> |
4 |
frodo |
168 |
|
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 |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
frodo |
184 |
#include "compat.h" |
22 |
frodo |
173 |
#include "error.h" |
23 |
frodo |
181 |
#include "unicode.h" |
24 |
frodo |
180 |
|
25 |
frodo |
168 |
#include <stdio.h> |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#include <string.h> |
28 |
frodo |
193 |
#include <strings.h> |
29 |
frodo |
180 |
#include <sys/types.h> |
30 |
|
|
#include <sys/stat.h> |
31 |
|
|
#include <unistd.h> |
32 |
|
|
#include <fcntl.h> |
33 |
frodo |
168 |
|
34 |
|
|
#include "configuration.h" |
35 |
|
|
|
36 |
|
|
#ifdef DMALLOC |
37 |
|
|
#include <dmalloc.h> |
38 |
|
|
#endif |
39 |
|
|
|
40 |
|
|
#ifndef CONFIGURATION_SEARCH_PATH |
41 |
frodo |
197 |
#define CONFIGURATION_SEARCH_PATH PSICONVETCDIR "/psiconv.conf:~/.psiconv.conf" |
42 |
frodo |
168 |
#endif |
43 |
frodo |
180 |
static struct psiconv_config_s default_config = |
44 |
frodo |
218 |
{ PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL,'?','?',{ 0 },psiconv_bool_false }; |
45 |
frodo |
168 |
|
46 |
frodo |
180 |
static void psiconv_config_parse_statement(const char *filename, |
47 |
|
|
int linenr, |
48 |
|
|
const char *var, int value, |
49 |
|
|
psiconv_config *config); |
50 |
|
|
|
51 |
|
|
static void psiconv_config_parse_line(const char *filename, int linenr, |
52 |
|
|
const char *line, psiconv_config *config); |
53 |
|
|
|
54 |
|
|
static void psiconv_config_parse_file(const char *filename, |
55 |
|
|
psiconv_config *config); |
56 |
|
|
|
57 |
|
|
psiconv_config psiconv_config_default(void) |
58 |
frodo |
168 |
{ |
59 |
frodo |
180 |
psiconv_config result; |
60 |
|
|
result = malloc(sizeof(*result)); |
61 |
|
|
*result = default_config; |
62 |
frodo |
218 |
psiconv_unicode_select_characterset(result,1); |
63 |
frodo |
180 |
return result; |
64 |
frodo |
168 |
} |
65 |
|
|
|
66 |
frodo |
208 |
void psiconv_config_free(psiconv_config config) |
67 |
frodo |
204 |
{ |
68 |
|
|
free(config); |
69 |
|
|
} |
70 |
|
|
|
71 |
frodo |
180 |
void psiconv_config_parse_statement(const char *filename, |
72 |
|
|
int linenr, |
73 |
|
|
const char *var, int value, |
74 |
|
|
psiconv_config *config) |
75 |
|
|
{ |
76 |
frodo |
181 |
int charnr; |
77 |
|
|
|
78 |
frodo |
180 |
if (!(strcasecmp(var,"verbosity"))) { |
79 |
frodo |
217 |
if ((value >= 1) && (value <= 5)) |
80 |
frodo |
180 |
(*config)->verbosity = value; |
81 |
|
|
else |
82 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
83 |
frodo |
185 |
"Verbosity should be between 1 and 5",filename,linenr); |
84 |
frodo |
180 |
} else if (!(strcasecmp(var,"color"))) { |
85 |
|
|
if ((value == 0) || (value == 1)) |
86 |
|
|
(*config)->color = value; |
87 |
|
|
else |
88 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
89 |
frodo |
180 |
"Color should be 0 or 1",filename,linenr); |
90 |
|
|
} else if (!(strcasecmp(var,"colordepth"))) { |
91 |
|
|
if ((value > 0) && (value <= 32)) |
92 |
|
|
(*config)->colordepth = value; |
93 |
|
|
else |
94 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
95 |
frodo |
180 |
"ColorDepth should be between 1 and 32",filename,linenr); |
96 |
|
|
} else if (!(strcasecmp(var,"redbits"))) { |
97 |
|
|
if ((value >= 0) && (value <= 32)) |
98 |
|
|
(*config)->redbits = value; |
99 |
|
|
else |
100 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
101 |
frodo |
180 |
"RedBits should be between 1 and 32 or 0",filename,linenr); |
102 |
|
|
} else if (!(strcasecmp(var,"greenbits"))) { |
103 |
|
|
if ((value >= 0) && (value <= 32)) |
104 |
|
|
(*config)->greenbits = value; |
105 |
|
|
else |
106 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
107 |
frodo |
180 |
"GreenBits should be between 1 and 32 or 0",filename,linenr); |
108 |
|
|
} else if (!(strcasecmp(var,"bluebits"))) { |
109 |
|
|
if ((value >= 0) && (value <= 32)) |
110 |
|
|
(*config)->bluebits = value; |
111 |
|
|
else |
112 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
113 |
frodo |
180 |
"BlueBits should be between 1 and 32 or 0",filename,linenr); |
114 |
frodo |
181 |
} else if (!(strcasecmp(var,"characterset"))) { |
115 |
frodo |
182 |
if ((value >= 0) && (value <= 1)) |
116 |
frodo |
181 |
psiconv_unicode_select_characterset(*config,value); |
117 |
|
|
else |
118 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
119 |
frodo |
217 |
"CharacterSet should be between 0 and 1", |
120 |
frodo |
181 |
filename,linenr); |
121 |
|
|
} else if (!(strcasecmp(var,"unknownunicodechar"))) { |
122 |
|
|
if ((value >= 1) && (value < 0x10000)) |
123 |
|
|
(*config)->unknown_unicode_char = value; |
124 |
|
|
else |
125 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
126 |
frodo |
181 |
"UnknownUnicodeChar should be between 1 and 65535", |
127 |
|
|
filename,linenr); |
128 |
|
|
} else if (!(strcasecmp(var,"unknownepocchar"))) { |
129 |
|
|
if ((value >= 1) && (value < 0x100)) |
130 |
|
|
(*config)->unknown_epoc_char = value; |
131 |
|
|
else |
132 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
133 |
frodo |
181 |
"UnknownEPOCChar should be between 1 and 255", |
134 |
|
|
filename,linenr); |
135 |
|
|
} else if (sscanf(var,"char%d",&charnr) == strlen(var)) { |
136 |
|
|
if ((charnr < 0) || (charnr > 255)) |
137 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
138 |
frodo |
181 |
"CharXXX should have XXX between 0 and 255", |
139 |
|
|
filename,linenr); |
140 |
|
|
if ((value >= 1) && (value <= 0x10000)) |
141 |
|
|
(*config)->unicode_table[charnr] = value; |
142 |
|
|
else |
143 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
144 |
frodo |
181 |
"CharXXX should be between 1 and 65535", |
145 |
|
|
filename,linenr); |
146 |
frodo |
180 |
} else { |
147 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
148 |
frodo |
180 |
"Unknown variable %s",filename,linenr,var); |
149 |
|
|
} |
150 |
|
|
psiconv_debug(*config,0,0,"Configuration file %s, line %d: " |
151 |
|
|
"Set variable %s to %d",filename,linenr,var,value); |
152 |
|
|
} |
153 |
frodo |
168 |
|
154 |
|
|
|
155 |
frodo |
180 |
void psiconv_config_parse_line(const char *filename, int linenr, |
156 |
|
|
const char *line, psiconv_config *config) |
157 |
frodo |
168 |
{ |
158 |
frodo |
180 |
|
159 |
|
|
int sovar,eovar,soval,eoval,eol; |
160 |
|
|
char *var; |
161 |
|
|
long val; |
162 |
|
|
|
163 |
|
|
psiconv_debug(*config,0,0,"Going to parse line %d: %s",linenr,line); |
164 |
|
|
sovar = 0; |
165 |
|
|
while (line[sovar] && (line[sovar] < 32)) |
166 |
|
|
sovar ++; |
167 |
|
|
if (!line[sovar] || line[sovar] == '#') |
168 |
|
|
return; |
169 |
|
|
eovar = sovar; |
170 |
|
|
while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || |
171 |
|
|
((line[eovar] >= 'a') && (line[eovar] <= 'z')))) |
172 |
|
|
eovar ++; |
173 |
frodo |
200 |
if (sovar == eovar) { |
174 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
175 |
frodo |
180 |
"Syntax error (no variable found)",filename,linenr); |
176 |
frodo |
200 |
return; |
177 |
|
|
} |
178 |
frodo |
180 |
soval = eovar; |
179 |
|
|
while (line[soval] && (line[soval] <= 32)) |
180 |
|
|
soval ++; |
181 |
frodo |
200 |
if (line[soval] != '=') { |
182 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
183 |
frodo |
180 |
"Syntax error (no = token found)",filename,linenr); |
184 |
frodo |
200 |
return; |
185 |
|
|
} |
186 |
frodo |
180 |
soval ++; |
187 |
|
|
while (line[soval] && (line[soval] <= 32)) |
188 |
|
|
soval ++; |
189 |
|
|
eoval = soval; |
190 |
frodo |
220 |
while (line[eoval] && ((line[eoval] >= '0') && (line[eoval] <= '9'))) |
191 |
frodo |
180 |
eoval ++; |
192 |
frodo |
200 |
if (eoval == soval) { |
193 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
194 |
frodo |
180 |
"Syntax error (no value found)",filename,linenr); |
195 |
frodo |
200 |
return; |
196 |
|
|
} |
197 |
|
|
if (soval - eoval > 7) { |
198 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
199 |
frodo |
180 |
"Syntax error (value too large)",filename,linenr); |
200 |
frodo |
200 |
return; |
201 |
|
|
} |
202 |
frodo |
180 |
eol = eoval; |
203 |
|
|
while (line[eol] && (line[eol] < 32)) |
204 |
|
|
eol ++; |
205 |
frodo |
200 |
if (line[eol]) { |
206 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
207 |
frodo |
180 |
"Syntax error (trailing garbage)",filename,linenr); |
208 |
frodo |
200 |
return; |
209 |
|
|
} |
210 |
frodo |
180 |
|
211 |
|
|
var = malloc(eovar - sovar + 1); |
212 |
|
|
memcpy(var,line + sovar, eovar - sovar); |
213 |
|
|
var[eovar-sovar] = 0; |
214 |
|
|
|
215 |
|
|
val = atol(line + soval); |
216 |
|
|
|
217 |
|
|
psiconv_config_parse_statement(filename,linenr,var,val,config); |
218 |
|
|
free(var); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
|
222 |
|
|
void psiconv_config_parse_file(const char *filename, psiconv_config *config) |
223 |
|
|
{ |
224 |
|
|
int file,linenr; |
225 |
frodo |
168 |
struct stat stat_buf; |
226 |
frodo |
180 |
off_t filesize,bytes_left,bytes_read,sol,eol; |
227 |
|
|
char *filebuffer,*filebuffer_ptr; |
228 |
frodo |
168 |
|
229 |
frodo |
180 |
psiconv_progress(*config,0,0, |
230 |
|
|
"Going to access configuration file %s",filename); |
231 |
|
|
|
232 |
|
|
/* Try to open the file; it may fail, if it does not exist for example */ |
233 |
|
|
if ((file = open(filename,O_RDONLY)) == -1) |
234 |
frodo |
200 |
goto ERROR0; |
235 |
frodo |
180 |
|
236 |
|
|
/* Read the contents of the file into filebuffer. This may fail */ |
237 |
|
|
if (fstat(file,&stat_buf)) { |
238 |
|
|
if (close(file)) |
239 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s: " |
240 |
frodo |
180 |
"Couldn't close file",filename); |
241 |
|
|
return; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
filesize = stat_buf.st_size; |
245 |
frodo |
200 |
if (!(filebuffer = malloc(filesize + 1))) { |
246 |
|
|
psiconv_error(*config,0,0,"Configuration file %s: " |
247 |
frodo |
180 |
"Out of memory error",filename); |
248 |
frodo |
200 |
goto ERROR1; |
249 |
|
|
} |
250 |
|
|
|
251 |
frodo |
180 |
filebuffer_ptr = filebuffer; |
252 |
|
|
bytes_left = filesize; |
253 |
|
|
bytes_read = 1; /* Dummy for the first time through the loop */ |
254 |
|
|
while ((bytes_read > 0) && bytes_left) { |
255 |
|
|
bytes_read = read(file,filebuffer_ptr,bytes_left); |
256 |
|
|
if (bytes_read > 0) { |
257 |
|
|
filebuffer_ptr += bytes_read; |
258 |
|
|
bytes_left -= bytes_read; |
259 |
|
|
} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
/* On NFS, the first read may fail and this is not fatal */ |
263 |
|
|
if (bytes_left && (bytes_left != filesize)) { |
264 |
frodo |
200 |
psiconv_error(*config,0,0,"Configuration file %s: " |
265 |
frodo |
180 |
"Couldn't read file into memory",filename); |
266 |
frodo |
200 |
goto ERROR2; |
267 |
frodo |
180 |
} |
268 |
|
|
|
269 |
frodo |
200 |
if (close(file)) { |
270 |
|
|
psiconv_error(*config,0,0,"Configuration file %s: " |
271 |
frodo |
180 |
"Couldn't close file",filename); |
272 |
frodo |
200 |
file = -1; |
273 |
|
|
goto ERROR2; |
274 |
|
|
} |
275 |
|
|
file = -1; |
276 |
frodo |
180 |
|
277 |
|
|
psiconv_progress(*config,0,0, |
278 |
|
|
"Going to parse configuration file %s: ",filename); |
279 |
|
|
/* Now we walk through the file to isolate lines */ |
280 |
|
|
linenr = 0; |
281 |
|
|
sol = 0; |
282 |
|
|
|
283 |
|
|
while (sol < filesize) { |
284 |
|
|
linenr ++; |
285 |
|
|
eol = sol; |
286 |
|
|
while ((eol < filesize) && (filebuffer[eol] != 13) && |
287 |
|
|
(filebuffer[eol] != 10) && (filebuffer[eol] != 0)) |
288 |
|
|
eol ++; |
289 |
|
|
|
290 |
frodo |
200 |
if ((eol < filesize) && (filebuffer[eol] == 0)) { |
291 |
|
|
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
292 |
frodo |
180 |
"Unexpected character \000 found",filename,linenr); |
293 |
frodo |
200 |
goto ERROR2; |
294 |
|
|
} |
295 |
frodo |
180 |
if ((eol < filesize + 1) && |
296 |
|
|
(((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || |
297 |
|
|
((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { |
298 |
|
|
filebuffer[eol] = 0; |
299 |
|
|
eol ++; |
300 |
|
|
} |
301 |
|
|
filebuffer[eol] = 0; |
302 |
|
|
psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); |
303 |
|
|
sol = eol+1; |
304 |
|
|
} |
305 |
|
|
free(filebuffer); |
306 |
frodo |
200 |
return; |
307 |
|
|
|
308 |
|
|
ERROR2: |
309 |
|
|
free(filebuffer); |
310 |
|
|
ERROR1: |
311 |
|
|
if ((file != -1) && close(file)) |
312 |
|
|
psiconv_error(*config,0,0,"Configuration file %s: " |
313 |
|
|
"Couldn't close file",filename); |
314 |
|
|
ERROR0: |
315 |
|
|
return; |
316 |
frodo |
180 |
} |
317 |
|
|
|
318 |
|
|
void psiconv_config_read(const char *extra_config_files, |
319 |
|
|
psiconv_config *config) |
320 |
|
|
{ |
321 |
|
|
char *path,*pathptr,*filename,*filename_old; |
322 |
|
|
const char *home; |
323 |
|
|
int filename_len; |
324 |
|
|
|
325 |
frodo |
168 |
/* Make path be the complete search path, colon separated */ |
326 |
|
|
if (extra_config_files && strlen(extra_config_files)) { |
327 |
|
|
path = malloc(strlen(CONFIGURATION_SEARCH_PATH) + |
328 |
|
|
strlen(extra_config_files) + 2); |
329 |
|
|
strcpy(path,CONFIGURATION_SEARCH_PATH); |
330 |
|
|
strcat(path,":"); |
331 |
frodo |
180 |
strcat(path,extra_config_files); |
332 |
frodo |
168 |
} else { |
333 |
frodo |
180 |
path = strdup(CONFIGURATION_SEARCH_PATH); |
334 |
frodo |
168 |
} |
335 |
|
|
|
336 |
|
|
pathptr = path; |
337 |
|
|
while (strlen(pathptr)) { |
338 |
|
|
/* Isolate the next filename */ |
339 |
frodo |
180 |
filename_len = (index(pathptr,':')?(index(pathptr,':') - pathptr): |
340 |
|
|
strlen(pathptr)); |
341 |
frodo |
168 |
filename = malloc(filename_len + 1); |
342 |
frodo |
180 |
filename = strncpy(filename,pathptr,filename_len); |
343 |
|
|
filename[filename_len] = 0; |
344 |
frodo |
168 |
pathptr += filename_len; |
345 |
|
|
if (strlen(pathptr)) |
346 |
|
|
pathptr ++; |
347 |
|
|
|
348 |
frodo |
180 |
/* Do ~ substitution */ |
349 |
|
|
if ((filename[0] == '~') && ((filename[1] == '/') || filename[1] == 0)) { |
350 |
|
|
home = getenv("HOME"); |
351 |
|
|
if (home) { |
352 |
|
|
filename_old = filename; |
353 |
|
|
filename = malloc(strlen(filename_old) + strlen(home)); |
354 |
|
|
strcpy(filename,home); |
355 |
|
|
strcpy(filename + strlen(filename),filename_old+1); |
356 |
|
|
free(filename_old); |
357 |
frodo |
168 |
} |
358 |
|
|
} |
359 |
frodo |
180 |
|
360 |
|
|
psiconv_config_parse_file(filename,config); |
361 |
|
|
free(filename); |
362 |
frodo |
168 |
} |
363 |
frodo |
208 |
free(path); |
364 |
frodo |
168 |
} |