| 1 |
/* |
| 2 |
configuration.c - Part of psiconv, a PSION 5 file formats converter |
| 3 |
Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
| 4 |
|
| 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 |
#include "compat.h" |
| 22 |
#include "error.h" |
| 23 |
#include "unicode.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <strings.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <fcntl.h> |
| 33 |
|
| 34 |
#include "configuration.h" |
| 35 |
|
| 36 |
#ifdef DMALLOC |
| 37 |
#include <dmalloc.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifndef CONFIGURATION_SEARCH_PATH |
| 41 |
#define CONFIGURATION_SEARCH_PATH PSICONVETCDIR "/psiconv.conf:~/.psiconv.conf" |
| 42 |
#endif |
| 43 |
static struct psiconv_config_s default_config = |
| 44 |
{ PSICONV_VERB_WARN, 2, 0,0,0,psiconv_bool_false,NULL,'?','?',{ 0 },psiconv_bool_false }; |
| 45 |
|
| 46 |
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 |
{ |
| 59 |
psiconv_config result; |
| 60 |
result = malloc(sizeof(*result)); |
| 61 |
*result = default_config; |
| 62 |
psiconv_unicode_select_characterset(result,1); |
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
void psiconv_config_free(psiconv_config config) |
| 67 |
{ |
| 68 |
free(config); |
| 69 |
} |
| 70 |
|
| 71 |
void psiconv_config_parse_statement(const char *filename, |
| 72 |
int linenr, |
| 73 |
const char *var, int value, |
| 74 |
psiconv_config *config) |
| 75 |
{ |
| 76 |
int charnr; |
| 77 |
|
| 78 |
if (!(strcasecmp(var,"verbosity"))) { |
| 79 |
if ((value >= 1) && (value <= 5)) |
| 80 |
(*config)->verbosity = value; |
| 81 |
else |
| 82 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 83 |
"Verbosity should be between 1 and 5",filename,linenr); |
| 84 |
} else if (!(strcasecmp(var,"color"))) { |
| 85 |
if ((value == 0) || (value == 1)) |
| 86 |
(*config)->color = value; |
| 87 |
else |
| 88 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 89 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 95 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 101 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 107 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 113 |
"BlueBits should be between 1 and 32 or 0",filename,linenr); |
| 114 |
} else if (!(strcasecmp(var,"characterset"))) { |
| 115 |
if ((value >= 0) && (value <= 1)) |
| 116 |
psiconv_unicode_select_characterset(*config,value); |
| 117 |
else |
| 118 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 119 |
"CharacterSet should be between 0 and 1", |
| 120 |
filename,linenr); |
| 121 |
} else if (!(strcasecmp(var,"unknownunicodechar"))) { |
| 122 |
if ((value >= 1) && (value < 0x10000)) |
| 123 |
(*config)->unknown_unicode_char = value; |
| 124 |
else |
| 125 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 126 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 133 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 138 |
"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 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 144 |
"CharXXX should be between 1 and 65535", |
| 145 |
filename,linenr); |
| 146 |
} else { |
| 147 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 148 |
"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 |
|
| 154 |
|
| 155 |
void psiconv_config_parse_line(const char *filename, int linenr, |
| 156 |
const char *line, psiconv_config *config) |
| 157 |
{ |
| 158 |
|
| 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 |
if (sovar == eovar) { |
| 174 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 175 |
"Syntax error (no variable found)",filename,linenr); |
| 176 |
return; |
| 177 |
} |
| 178 |
soval = eovar; |
| 179 |
while (line[soval] && (line[soval] <= 32)) |
| 180 |
soval ++; |
| 181 |
if (line[soval] != '=') { |
| 182 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 183 |
"Syntax error (no = token found)",filename,linenr); |
| 184 |
return; |
| 185 |
} |
| 186 |
soval ++; |
| 187 |
while (line[soval] && (line[soval] <= 32)) |
| 188 |
soval ++; |
| 189 |
eoval = soval; |
| 190 |
while (line[eoval] && ((line[eoval] >= '0') && (line[eoval] <= '9'))) |
| 191 |
eoval ++; |
| 192 |
if (eoval == soval) { |
| 193 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 194 |
"Syntax error (no value found)",filename,linenr); |
| 195 |
return; |
| 196 |
} |
| 197 |
if (soval - eoval > 7) { |
| 198 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 199 |
"Syntax error (value too large)",filename,linenr); |
| 200 |
return; |
| 201 |
} |
| 202 |
eol = eoval; |
| 203 |
while (line[eol] && (line[eol] < 32)) |
| 204 |
eol ++; |
| 205 |
if (line[eol]) { |
| 206 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 207 |
"Syntax error (trailing garbage)",filename,linenr); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 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 |
struct stat stat_buf; |
| 226 |
off_t filesize,bytes_left,bytes_read,sol,eol; |
| 227 |
char *filebuffer,*filebuffer_ptr; |
| 228 |
|
| 229 |
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 |
goto ERROR0; |
| 235 |
|
| 236 |
/* Read the contents of the file into filebuffer. This may fail */ |
| 237 |
if (fstat(file,&stat_buf)) { |
| 238 |
if (close(file)) |
| 239 |
psiconv_error(*config,0,0,"Configuration file %s: " |
| 240 |
"Couldn't close file",filename); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
filesize = stat_buf.st_size; |
| 245 |
if (!(filebuffer = malloc(filesize + 1))) { |
| 246 |
psiconv_error(*config,0,0,"Configuration file %s: " |
| 247 |
"Out of memory error",filename); |
| 248 |
goto ERROR1; |
| 249 |
} |
| 250 |
|
| 251 |
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 |
psiconv_error(*config,0,0,"Configuration file %s: " |
| 265 |
"Couldn't read file into memory",filename); |
| 266 |
goto ERROR2; |
| 267 |
} |
| 268 |
|
| 269 |
if (close(file)) { |
| 270 |
psiconv_error(*config,0,0,"Configuration file %s: " |
| 271 |
"Couldn't close file",filename); |
| 272 |
file = -1; |
| 273 |
goto ERROR2; |
| 274 |
} |
| 275 |
file = -1; |
| 276 |
|
| 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 |
if ((eol < filesize) && (filebuffer[eol] == 0)) { |
| 291 |
psiconv_error(*config,0,0,"Configuration file %s, line %d: " |
| 292 |
"Unexpected character \000 found",filename,linenr); |
| 293 |
goto ERROR2; |
| 294 |
} |
| 295 |
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 |
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 |
} |
| 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 |
/* 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 |
strcat(path,extra_config_files); |
| 332 |
} else { |
| 333 |
path = strdup(CONFIGURATION_SEARCH_PATH); |
| 334 |
} |
| 335 |
|
| 336 |
pathptr = path; |
| 337 |
while (strlen(pathptr)) { |
| 338 |
/* Isolate the next filename */ |
| 339 |
filename_len = (index(pathptr,':')?(index(pathptr,':') - pathptr): |
| 340 |
strlen(pathptr)); |
| 341 |
filename = malloc(filename_len + 1); |
| 342 |
filename = strncpy(filename,pathptr,filename_len); |
| 343 |
filename[filename_len] = 0; |
| 344 |
pathptr += filename_len; |
| 345 |
if (strlen(pathptr)) |
| 346 |
pathptr ++; |
| 347 |
|
| 348 |
/* 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 |
} |
| 358 |
} |
| 359 |
|
| 360 |
psiconv_config_parse_file(filename,config); |
| 361 |
free(filename); |
| 362 |
} |
| 363 |
free(path); |
| 364 |
} |