/[public]/psiconv/trunk/lib/psiconv/configuration.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/configuration.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 180 Revision 200
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 24
24#include <stdio.h> 25#include <stdio.h>
25#include <stdlib.h> 26#include <stdlib.h>
26#include <string.h> 27#include <string.h>
28#include <strings.h>
27#include <sys/types.h> 29#include <sys/types.h>
28#include <sys/stat.h> 30#include <sys/stat.h>
29#include <unistd.h> 31#include <unistd.h>
30#include <fcntl.h> 32#include <fcntl.h>
31 33
34#ifdef DMALLOC 36#ifdef DMALLOC
35#include <dmalloc.h> 37#include <dmalloc.h>
36#endif 38#endif
37 39
38#ifndef CONFIGURATION_SEARCH_PATH 40#ifndef CONFIGURATION_SEARCH_PATH
39#define CONFIGURATION_SEARCH_PATH "/etc/psiconv.conf:~/.psiconv.conf" 41#define CONFIGURATION_SEARCH_PATH PSICONVETCDIR "/psiconv.conf:~/.psiconv.conf"
40#endif 42#endif
41static struct psiconv_config_s default_config = 43static struct psiconv_config_s default_config =
42 { PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL }; 44 { PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL,'?' };
43 45
44static void psiconv_config_parse_statement(const char *filename, 46static void psiconv_config_parse_statement(const char *filename,
45 int linenr, 47 int linenr,
46 const char *var, int value, 48 const char *var, int value,
47 psiconv_config *config); 49 psiconv_config *config);
63void psiconv_config_parse_statement(const char *filename, 65void psiconv_config_parse_statement(const char *filename,
64 int linenr, 66 int linenr,
65 const char *var, int value, 67 const char *var, int value,
66 psiconv_config *config) 68 psiconv_config *config)
67{ 69{
70 int charnr;
71
68 if (!(strcasecmp(var,"verbosity"))) { 72 if (!(strcasecmp(var,"verbosity"))) {
69 if ((value >= 1) && (value <= 4)) 73 if ((value >= 1) && (value <= 4))
70 (*config)->verbosity = value; 74 (*config)->verbosity = value;
71 else 75 else
72 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 76 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
73 "Verbosity should be between 1 and 4",filename,linenr); 77 "Verbosity should be between 1 and 5",filename,linenr);
74 } else if (!(strcasecmp(var,"color"))) { 78 } else if (!(strcasecmp(var,"color"))) {
75 if ((value == 0) || (value == 1)) 79 if ((value == 0) || (value == 1))
76 (*config)->color = value; 80 (*config)->color = value;
77 else 81 else
78 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 82 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
79 "Color should be 0 or 1",filename,linenr); 83 "Color should be 0 or 1",filename,linenr);
80 } else if (!(strcasecmp(var,"colordepth"))) { 84 } else if (!(strcasecmp(var,"colordepth"))) {
81 if ((value > 0) && (value <= 32)) 85 if ((value > 0) && (value <= 32))
82 (*config)->colordepth = value; 86 (*config)->colordepth = value;
83 else 87 else
84 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 88 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
85 "ColorDepth should be between 1 and 32",filename,linenr); 89 "ColorDepth should be between 1 and 32",filename,linenr);
86 } else if (!(strcasecmp(var,"redbits"))) { 90 } else if (!(strcasecmp(var,"redbits"))) {
87 if ((value >= 0) && (value <= 32)) 91 if ((value >= 0) && (value <= 32))
88 (*config)->redbits = value; 92 (*config)->redbits = value;
89 else 93 else
90 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 94 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
91 "RedBits should be between 1 and 32 or 0",filename,linenr); 95 "RedBits should be between 1 and 32 or 0",filename,linenr);
92 } else if (!(strcasecmp(var,"greenbits"))) { 96 } else if (!(strcasecmp(var,"greenbits"))) {
93 if ((value >= 0) && (value <= 32)) 97 if ((value >= 0) && (value <= 32))
94 (*config)->greenbits = value; 98 (*config)->greenbits = value;
95 else 99 else
96 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 100 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
97 "GreenBits should be between 1 and 32 or 0",filename,linenr); 101 "GreenBits should be between 1 and 32 or 0",filename,linenr);
98 } else if (!(strcasecmp(var,"bluebits"))) { 102 } else if (!(strcasecmp(var,"bluebits"))) {
99 if ((value >= 0) && (value <= 32)) 103 if ((value >= 0) && (value <= 32))
100 (*config)->bluebits = value; 104 (*config)->bluebits = value;
101 else 105 else
102 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 106 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
103 "BlueBits should be between 1 and 32 or 0",filename,linenr); 107 "BlueBits should be between 1 and 32 or 0",filename,linenr);
108 } else if (!(strcasecmp(var,"characterset"))) {
109 if ((value >= 0) && (value <= 1))
110 psiconv_unicode_select_characterset(*config,value);
111 else
112 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
113 "CharacterSet should be between 0 and 0",
114 filename,linenr);
115 } else if (!(strcasecmp(var,"unknownunicodechar"))) {
116 if ((value >= 1) && (value < 0x10000))
117 (*config)->unknown_unicode_char = value;
118 else
119 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
120 "UnknownUnicodeChar should be between 1 and 65535",
121 filename,linenr);
122 } else if (!(strcasecmp(var,"unknownepocchar"))) {
123 if ((value >= 1) && (value < 0x100))
124 (*config)->unknown_epoc_char = value;
125 else
126 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
127 "UnknownEPOCChar should be between 1 and 255",
128 filename,linenr);
129 } else if (sscanf(var,"char%d",&charnr) == strlen(var)) {
130 if ((charnr < 0) || (charnr > 255))
131 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
132 "CharXXX should have XXX between 0 and 255",
133 filename,linenr);
134 if ((value >= 1) && (value <= 0x10000))
135 (*config)->unicode_table[charnr] = value;
136 else
137 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
138 "CharXXX should be between 1 and 65535",
139 filename,linenr);
104 } else { 140 } else {
105 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 141 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
106 "Unknown variable %s",filename,linenr,var); 142 "Unknown variable %s",filename,linenr,var);
107 } 143 }
108 psiconv_debug(*config,0,0,"Configuration file %s, line %d: " 144 psiconv_debug(*config,0,0,"Configuration file %s, line %d: "
109 "Set variable %s to %d",filename,linenr,var,value); 145 "Set variable %s to %d",filename,linenr,var,value);
110} 146}
126 return; 162 return;
127 eovar = sovar; 163 eovar = sovar;
128 while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) || 164 while (line[eovar] && (((line[eovar] >= 'A') && (line[eovar] <= 'Z')) ||
129 ((line[eovar] >= 'a') && (line[eovar] <= 'z')))) 165 ((line[eovar] >= 'a') && (line[eovar] <= 'z'))))
130 eovar ++; 166 eovar ++;
131 if (sovar == eovar) 167 if (sovar == eovar) {
132 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 168 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
133 "Syntax error (no variable found)",filename,linenr); 169 "Syntax error (no variable found)",filename,linenr);
170 return;
171 }
134 soval = eovar; 172 soval = eovar;
135 while (line[soval] && (line[soval] <= 32)) 173 while (line[soval] && (line[soval] <= 32))
136 soval ++; 174 soval ++;
137 if (line[soval] != '=') 175 if (line[soval] != '=') {
138 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 176 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
139 "Syntax error (no = token found)",filename,linenr); 177 "Syntax error (no = token found)",filename,linenr);
178 return;
179 }
140 soval ++; 180 soval ++;
141 while (line[soval] && (line[soval] <= 32)) 181 while (line[soval] && (line[soval] <= 32))
142 soval ++; 182 soval ++;
143 eoval = soval; 183 eoval = soval;
144 while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9'))) 184 while (line[eoval] && ((line[eoval] >= '0') && (line[eovar] <= '9')))
145 eoval ++; 185 eoval ++;
146 if (eoval == soval) 186 if (eoval == soval) {
147 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 187 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
148 "Syntax error (no value found)",filename,linenr); 188 "Syntax error (no value found)",filename,linenr);
189 return;
190 }
149 if (soval - eoval > 7) 191 if (soval - eoval > 7) {
150 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 192 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
151 "Syntax error (value too large)",filename,linenr); 193 "Syntax error (value too large)",filename,linenr);
194 return;
195 }
152 eol = eoval; 196 eol = eoval;
153 while (line[eol] && (line[eol] < 32)) 197 while (line[eol] && (line[eol] < 32))
154 eol ++; 198 eol ++;
155 if (line[eol]) 199 if (line[eol]) {
156 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 200 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
157 "Syntax error (trailing garbage)",filename,linenr); 201 "Syntax error (trailing garbage)",filename,linenr);
202 return;
203 }
158 204
159 var = malloc(eovar - sovar + 1); 205 var = malloc(eovar - sovar + 1);
160 memcpy(var,line + sovar, eovar - sovar); 206 memcpy(var,line + sovar, eovar - sovar);
161 var[eovar-sovar] = 0; 207 var[eovar-sovar] = 0;
162 208
177 psiconv_progress(*config,0,0, 223 psiconv_progress(*config,0,0,
178 "Going to access configuration file %s",filename); 224 "Going to access configuration file %s",filename);
179 225
180 /* Try to open the file; it may fail, if it does not exist for example */ 226 /* Try to open the file; it may fail, if it does not exist for example */
181 if ((file = open(filename,O_RDONLY)) == -1) 227 if ((file = open(filename,O_RDONLY)) == -1)
182 return; 228 goto ERROR0;
183 229
184 /* Read the contents of the file into filebuffer. This may fail */ 230 /* Read the contents of the file into filebuffer. This may fail */
185 if (fstat(file,&stat_buf)) { 231 if (fstat(file,&stat_buf)) {
186 if (close(file)) 232 if (close(file))
187 psiconv_fatal(*config,0,0,"Configuration file %s: " 233 psiconv_error(*config,0,0,"Configuration file %s: "
188 "Couldn't close file",filename); 234 "Couldn't close file",filename);
189 return; 235 return;
190 } 236 }
191 237
192 filesize = stat_buf.st_size; 238 filesize = stat_buf.st_size;
193 if (!(filebuffer = malloc(filesize + 1))) 239 if (!(filebuffer = malloc(filesize + 1))) {
194 psiconv_fatal(*config,0,0,"Configuration file %s: " 240 psiconv_error(*config,0,0,"Configuration file %s: "
195 "Out of memory error",filename); 241 "Out of memory error",filename);
242 goto ERROR1;
243 }
244
196 filebuffer_ptr = filebuffer; 245 filebuffer_ptr = filebuffer;
197 bytes_left = filesize; 246 bytes_left = filesize;
198 bytes_read = 1; /* Dummy for the first time through the loop */ 247 bytes_read = 1; /* Dummy for the first time through the loop */
199 while ((bytes_read > 0) && bytes_left) { 248 while ((bytes_read > 0) && bytes_left) {
200 bytes_read = read(file,filebuffer_ptr,bytes_left); 249 bytes_read = read(file,filebuffer_ptr,bytes_left);
204 } 253 }
205 } 254 }
206 255
207 /* On NFS, the first read may fail and this is not fatal */ 256 /* On NFS, the first read may fail and this is not fatal */
208 if (bytes_left && (bytes_left != filesize)) { 257 if (bytes_left && (bytes_left != filesize)) {
209 psiconv_fatal(*config,0,0,"Configuration file %s: " 258 psiconv_error(*config,0,0,"Configuration file %s: "
210 "Couldn't read file into memory",filename); 259 "Couldn't read file into memory",filename);
260 goto ERROR2;
211 } 261 }
212 262
213 if (close(file)) 263 if (close(file)) {
214 psiconv_fatal(*config,0,0,"Configuration file %s: " 264 psiconv_error(*config,0,0,"Configuration file %s: "
215 "Couldn't close file",filename); 265 "Couldn't close file",filename);
266 file = -1;
267 goto ERROR2;
268 }
269 file = -1;
216 270
217 psiconv_progress(*config,0,0, 271 psiconv_progress(*config,0,0,
218 "Going to parse configuration file %s: ",filename); 272 "Going to parse configuration file %s: ",filename);
219 /* Now we walk through the file to isolate lines */ 273 /* Now we walk through the file to isolate lines */
220 linenr = 0; 274 linenr = 0;
225 eol = sol; 279 eol = sol;
226 while ((eol < filesize) && (filebuffer[eol] != 13) && 280 while ((eol < filesize) && (filebuffer[eol] != 13) &&
227 (filebuffer[eol] != 10) && (filebuffer[eol] != 0)) 281 (filebuffer[eol] != 10) && (filebuffer[eol] != 0))
228 eol ++; 282 eol ++;
229 283
230 if ((eol < filesize) && (filebuffer[eol] == 0)) 284 if ((eol < filesize) && (filebuffer[eol] == 0)) {
231 psiconv_fatal(*config,0,0,"Configuration file %s, line %d: " 285 psiconv_error(*config,0,0,"Configuration file %s, line %d: "
232 "Unexpected character \000 found",filename,linenr); 286 "Unexpected character \000 found",filename,linenr);
287 goto ERROR2;
288 }
233 if ((eol < filesize + 1) && 289 if ((eol < filesize + 1) &&
234 (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) || 290 (((filebuffer[eol] == 13) && (filebuffer[eol+1] == 10)) ||
235 ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) { 291 ((filebuffer[eol] == 10) && (filebuffer[eol+1] == 13)))) {
236 filebuffer[eol] = 0; 292 filebuffer[eol] = 0;
237 eol ++; 293 eol ++;
239 filebuffer[eol] = 0; 295 filebuffer[eol] = 0;
240 psiconv_config_parse_line(filename,linenr,filebuffer + sol,config); 296 psiconv_config_parse_line(filename,linenr,filebuffer + sol,config);
241 sol = eol+1; 297 sol = eol+1;
242 } 298 }
243 free(filebuffer); 299 free(filebuffer);
300 return;
301
302ERROR2:
303 free(filebuffer);
304ERROR1:
305 if ((file != -1) && close(file))
306 psiconv_error(*config,0,0,"Configuration file %s: "
307 "Couldn't close file",filename);
308ERROR0:
309 return;
244} 310}
245 311
246void psiconv_config_read(const char *extra_config_files, 312void psiconv_config_read(const char *extra_config_files,
247 psiconv_config *config) 313 psiconv_config *config)
248{ 314{

Legend:
Removed from v.180  
changed lines
  Added in v.200

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26