… | |
… | |
60 | result = malloc(sizeof(*result)); |
60 | result = malloc(sizeof(*result)); |
61 | *result = default_config; |
61 | *result = default_config; |
62 | return result; |
62 | return result; |
63 | } |
63 | } |
64 | |
64 | |
|
|
65 | void psiconv_config_free(psiconv_config config) |
|
|
66 | { |
|
|
67 | free(config); |
|
|
68 | } |
|
|
69 | |
65 | void psiconv_config_parse_statement(const char *filename, |
70 | void psiconv_config_parse_statement(const char *filename, |
66 | int linenr, |
71 | int linenr, |
67 | const char *var, int value, |
72 | const char *var, int value, |
68 | psiconv_config *config) |
73 | psiconv_config *config) |
69 | { |
74 | { |
… | |
… | |
71 | |
76 | |
72 | if (!(strcasecmp(var,"verbosity"))) { |
77 | if (!(strcasecmp(var,"verbosity"))) { |
73 | if ((value >= 1) && (value <= 4)) |
78 | if ((value >= 1) && (value <= 4)) |
74 | (*config)->verbosity = value; |
79 | (*config)->verbosity = value; |
75 | else |
80 | else |
76 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
81 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
77 | "Verbosity should be between 1 and 5",filename,linenr); |
82 | "Verbosity should be between 1 and 5",filename,linenr); |
78 | } else if (!(strcasecmp(var,"color"))) { |
83 | } else if (!(strcasecmp(var,"color"))) { |
79 | if ((value == 0) || (value == 1)) |
84 | if ((value == 0) || (value == 1)) |
80 | (*config)->color = value; |
85 | (*config)->color = value; |
81 | else |
86 | else |
82 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
87 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
83 | "Color should be 0 or 1",filename,linenr); |
88 | "Color should be 0 or 1",filename,linenr); |
84 | } else if (!(strcasecmp(var,"colordepth"))) { |
89 | } else if (!(strcasecmp(var,"colordepth"))) { |
85 | if ((value > 0) && (value <= 32)) |
90 | if ((value > 0) && (value <= 32)) |
86 | (*config)->colordepth = value; |
91 | (*config)->colordepth = value; |
87 | else |
92 | else |
88 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
93 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
89 | "ColorDepth should be between 1 and 32",filename,linenr); |
94 | "ColorDepth should be between 1 and 32",filename,linenr); |
90 | } else if (!(strcasecmp(var,"redbits"))) { |
95 | } else if (!(strcasecmp(var,"redbits"))) { |
91 | if ((value >= 0) && (value <= 32)) |
96 | if ((value >= 0) && (value <= 32)) |
92 | (*config)->redbits = value; |
97 | (*config)->redbits = value; |
93 | else |
98 | else |
94 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
99 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
95 | "RedBits should be between 1 and 32 or 0",filename,linenr); |
100 | "RedBits should be between 1 and 32 or 0",filename,linenr); |
96 | } else if (!(strcasecmp(var,"greenbits"))) { |
101 | } else if (!(strcasecmp(var,"greenbits"))) { |
97 | if ((value >= 0) && (value <= 32)) |
102 | if ((value >= 0) && (value <= 32)) |
98 | (*config)->greenbits = value; |
103 | (*config)->greenbits = value; |
99 | else |
104 | else |
100 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
105 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
101 | "GreenBits should be between 1 and 32 or 0",filename,linenr); |
106 | "GreenBits should be between 1 and 32 or 0",filename,linenr); |
102 | } else if (!(strcasecmp(var,"bluebits"))) { |
107 | } else if (!(strcasecmp(var,"bluebits"))) { |
103 | if ((value >= 0) && (value <= 32)) |
108 | if ((value >= 0) && (value <= 32)) |
104 | (*config)->bluebits = value; |
109 | (*config)->bluebits = value; |
105 | else |
110 | else |
106 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
111 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
107 | "BlueBits should be between 1 and 32 or 0",filename,linenr); |
112 | "BlueBits should be between 1 and 32 or 0",filename,linenr); |
108 | } else if (!(strcasecmp(var,"characterset"))) { |
113 | } else if (!(strcasecmp(var,"characterset"))) { |
109 | if ((value >= 0) && (value <= 1)) |
114 | if ((value >= 0) && (value <= 1)) |
110 | psiconv_unicode_select_characterset(*config,value); |
115 | psiconv_unicode_select_characterset(*config,value); |
111 | else |
116 | else |
112 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
117 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
113 | "CharacterSet should be between 0 and 0", |
118 | "CharacterSet should be between 0 and 0", |
114 | filename,linenr); |
119 | filename,linenr); |
115 | } else if (!(strcasecmp(var,"unknownunicodechar"))) { |
120 | } else if (!(strcasecmp(var,"unknownunicodechar"))) { |
116 | if ((value >= 1) && (value < 0x10000)) |
121 | if ((value >= 1) && (value < 0x10000)) |
117 | (*config)->unknown_unicode_char = value; |
122 | (*config)->unknown_unicode_char = value; |
118 | else |
123 | else |
119 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
124 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
120 | "UnknownUnicodeChar should be between 1 and 65535", |
125 | "UnknownUnicodeChar should be between 1 and 65535", |
121 | filename,linenr); |
126 | filename,linenr); |
122 | } else if (!(strcasecmp(var,"unknownepocchar"))) { |
127 | } else if (!(strcasecmp(var,"unknownepocchar"))) { |
123 | if ((value >= 1) && (value < 0x100)) |
128 | if ((value >= 1) && (value < 0x100)) |
124 | (*config)->unknown_epoc_char = value; |
129 | (*config)->unknown_epoc_char = value; |
125 | else |
130 | else |
126 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
131 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
127 | "UnknownEPOCChar should be between 1 and 255", |
132 | "UnknownEPOCChar should be between 1 and 255", |
128 | filename,linenr); |
133 | filename,linenr); |
129 | } else if (sscanf(var,"char%d",&charnr) == strlen(var)) { |
134 | } else if (sscanf(var,"char%d",&charnr) == strlen(var)) { |
130 | if ((charnr < 0) || (charnr > 255)) |
135 | if ((charnr < 0) || (charnr > 255)) |
131 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
136 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
132 | "CharXXX should have XXX between 0 and 255", |
137 | "CharXXX should have XXX between 0 and 255", |
133 | filename,linenr); |
138 | filename,linenr); |
134 | if ((value >= 1) && (value <= 0x10000)) |
139 | if ((value >= 1) && (value <= 0x10000)) |
135 | (*config)->unicode_table[charnr] = value; |
140 | (*config)->unicode_table[charnr] = value; |
136 | else |
141 | else |
137 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
142 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
138 | "CharXXX should be between 1 and 65535", |
143 | "CharXXX should be between 1 and 65535", |
139 | filename,linenr); |
144 | filename,linenr); |
140 | } else { |
145 | } else { |
141 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
146 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
142 | "Unknown variable %s",filename,linenr,var); |
147 | "Unknown variable %s",filename,linenr,var); |
143 | } |
148 | } |
144 | psiconv_debug(*config,0,0,"Configuration file %s, line %d: " |
149 | psiconv_debug(*config,0,0,"Configuration file %s, line %d: " |
145 | "Set variable %s to %d",filename,linenr,var,value); |
150 | "Set variable %s to %d",filename,linenr,var,value); |
146 | } |
151 | } |
… | |
… | |
162 | return; |
167 | return; |
163 | eovar = sovar; |
168 | eovar = sovar; |
164 | while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || |
169 | while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || |
165 | ((line[eovar] >= 'a') && (line[eovar] <= 'z')))) |
170 | ((line[eovar] >= 'a') && (line[eovar] <= 'z')))) |
166 | eovar ++; |
171 | eovar ++; |
167 | if (sovar == eovar) |
172 | if (sovar == eovar) { |
168 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
173 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
169 | "Syntax error (no variable found)",filename,linenr); |
174 | "Syntax error (no variable found)",filename,linenr); |
|
|
175 | return; |
|
|
176 | } |
170 | soval = eovar; |
177 | soval = eovar; |
171 | while (line[soval] && (line[soval] <= 32)) |
178 | while (line[soval] && (line[soval] <= 32)) |
172 | soval ++; |
179 | soval ++; |
173 | if (line[soval] != '=') |
180 | if (line[soval] != '=') { |
174 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
181 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
175 | "Syntax error (no = token found)",filename,linenr); |
182 | "Syntax error (no = token found)",filename,linenr); |
|
|
183 | return; |
|
|
184 | } |
176 | soval ++; |
185 | soval ++; |
177 | while (line[soval] && (line[soval] <= 32)) |
186 | while (line[soval] && (line[soval] <= 32)) |
178 | soval ++; |
187 | soval ++; |
179 | eoval = soval; |
188 | eoval = soval; |
180 | while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9'))) |
189 | while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9'))) |
181 | eoval ++; |
190 | eoval ++; |
182 | if (eoval == soval) |
191 | if (eoval == soval) { |
183 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
192 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
184 | "Syntax error (no value found)",filename,linenr); |
193 | "Syntax error (no value found)",filename,linenr); |
|
|
194 | return; |
|
|
195 | } |
185 | if (soval - eoval > 7) |
196 | if (soval - eoval > 7) { |
186 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
197 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
187 | "Syntax error (value too large)",filename,linenr); |
198 | "Syntax error (value too large)",filename,linenr); |
|
|
199 | return; |
|
|
200 | } |
188 | eol = eoval; |
201 | eol = eoval; |
189 | while (line[eol] && (line[eol] < 32)) |
202 | while (line[eol] && (line[eol] < 32)) |
190 | eol ++; |
203 | eol ++; |
191 | if (line[eol]) |
204 | if (line[eol]) { |
192 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
205 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
193 | "Syntax error (trailing garbage)",filename,linenr); |
206 | "Syntax error (trailing garbage)",filename,linenr); |
|
|
207 | return; |
|
|
208 | } |
194 | |
209 | |
195 | var = malloc(eovar - sovar + 1); |
210 | var = malloc(eovar - sovar + 1); |
196 | memcpy(var,line + sovar, eovar - sovar); |
211 | memcpy(var,line + sovar, eovar - sovar); |
197 | var[eovar-sovar] = 0; |
212 | var[eovar-sovar] = 0; |
198 | |
213 | |
… | |
… | |
213 | psiconv_progress(*config,0,0, |
228 | psiconv_progress(*config,0,0, |
214 | "Going to access configuration file %s",filename); |
229 | "Going to access configuration file %s",filename); |
215 | |
230 | |
216 | /* 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 */ |
217 | if ((file = open(filename,O_RDONLY)) == -1) |
232 | if ((file = open(filename,O_RDONLY)) == -1) |
218 | return; |
233 | goto ERROR0; |
219 | |
234 | |
220 | /* Read the contents of the file into filebuffer. This may fail */ |
235 | /* Read the contents of the file into filebuffer. This may fail */ |
221 | if (fstat(file,&stat_buf)) { |
236 | if (fstat(file,&stat_buf)) { |
222 | if (close(file)) |
237 | if (close(file)) |
223 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
238 | psiconv_error(*config,0,0,"Configuration file %s: " |
224 | "Couldn't close file",filename); |
239 | "Couldn't close file",filename); |
225 | return; |
240 | return; |
226 | } |
241 | } |
227 | |
242 | |
228 | filesize = stat_buf.st_size; |
243 | filesize = stat_buf.st_size; |
229 | if (!(filebuffer = malloc(filesize + 1))) |
244 | if (!(filebuffer = malloc(filesize + 1))) { |
230 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
245 | psiconv_error(*config,0,0,"Configuration file %s: " |
231 | "Out of memory error",filename); |
246 | "Out of memory error",filename); |
|
|
247 | goto ERROR1; |
|
|
248 | } |
|
|
249 | |
232 | filebuffer_ptr = filebuffer; |
250 | filebuffer_ptr = filebuffer; |
233 | bytes_left = filesize; |
251 | bytes_left = filesize; |
234 | bytes_read = 1; /* Dummy for the first time through the loop */ |
252 | bytes_read = 1; /* Dummy for the first time through the loop */ |
235 | while ((bytes_read > 0) && bytes_left) { |
253 | while ((bytes_read > 0) && bytes_left) { |
236 | bytes_read = read(file,filebuffer_ptr,bytes_left); |
254 | bytes_read = read(file,filebuffer_ptr,bytes_left); |
… | |
… | |
240 | } |
258 | } |
241 | } |
259 | } |
242 | |
260 | |
243 | /* 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 */ |
244 | if (bytes_left && (bytes_left != filesize)) { |
262 | if (bytes_left && (bytes_left != filesize)) { |
245 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
263 | psiconv_error(*config,0,0,"Configuration file %s: " |
246 | "Couldn't read file into memory",filename); |
264 | "Couldn't read file into memory",filename); |
|
|
265 | goto ERROR2; |
247 | } |
266 | } |
248 | |
267 | |
249 | if (close(file)) |
268 | if (close(file)) { |
250 | psiconv_fatal(*config,0,0,"Configuration file %s: " |
269 | psiconv_error(*config,0,0,"Configuration file %s: " |
251 | "Couldn't close file",filename); |
270 | "Couldn't close file",filename); |
|
|
271 | file = -1; |
|
|
272 | goto ERROR2; |
|
|
273 | } |
|
|
274 | file = -1; |
252 | |
275 | |
253 | psiconv_progress(*config,0,0, |
276 | psiconv_progress(*config,0,0, |
254 | "Going to parse configuration file %s: ",filename); |
277 | "Going to parse configuration file %s: ",filename); |
255 | /* Now we walk through the file to isolate lines */ |
278 | /* Now we walk through the file to isolate lines */ |
256 | linenr = 0; |
279 | linenr = 0; |
… | |
… | |
261 | eol = sol; |
284 | eol = sol; |
262 | while ((eol < filesize) && (filebuffer[eol] != 13) && |
285 | while ((eol < filesize) && (filebuffer[eol] != 13) && |
263 | (filebuffer[eol] != 10) && (filebuffer[eol] != 0)) |
286 | (filebuffer[eol] != 10) && (filebuffer[eol] != 0)) |
264 | eol ++; |
287 | eol ++; |
265 | |
288 | |
266 | if ((eol < filesize) && (filebuffer[eol] == 0)) |
289 | if ((eol < filesize) && (filebuffer[eol] == 0)) { |
267 | psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " |
290 | psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
268 | "Unexpected character \000 found",filename,linenr); |
291 | "Unexpected character \000 found",filename,linenr); |
|
|
292 | goto ERROR2; |
|
|
293 | } |
269 | if ((eol < filesize + 1) && |
294 | if ((eol < filesize + 1) && |
270 | (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || |
295 | (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || |
271 | ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { |
296 | ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { |
272 | filebuffer[eol] = 0; |
297 | filebuffer[eol] = 0; |
273 | eol ++; |
298 | eol ++; |
… | |
… | |
275 | filebuffer[eol] = 0; |
300 | filebuffer[eol] = 0; |
276 | psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); |
301 | psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); |
277 | sol = eol+1; |
302 | sol = eol+1; |
278 | } |
303 | } |
279 | 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; |
280 | } |
315 | } |
281 | |
316 | |
282 | void psiconv_config_read(const char *extra_config_files, |
317 | void psiconv_config_read(const char *extra_config_files, |
283 | psiconv_config *config) |
318 | psiconv_config *config) |
284 | { |
319 | { |
… | |
… | |
322 | } |
357 | } |
323 | |
358 | |
324 | psiconv_config_parse_file(filename,config); |
359 | psiconv_config_parse_file(filename,config); |
325 | free(filename); |
360 | free(filename); |
326 | } |
361 | } |
|
|
362 | free(path); |
327 | } |
363 | } |