| 1 |
// -*- related-file-name: "../include/efont/psres.hh" -*- |
| 2 |
|
| 3 |
/* psres.{cc,hh} -- PSres.upr files |
| 4 |
* |
| 5 |
* Copyright (c) 1999-2019 Eddie Kohler |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. This program is distributed in the hope that it will be |
| 11 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 13 |
* Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
#include <efont/psres.hh> |
| 20 |
#include <lcdf/slurper.hh> |
| 21 |
/* Get the correct functions for directory searching */ |
| 22 |
#if HAVE_DIRENT_H |
| 23 |
# include <dirent.h> |
| 24 |
# define DIR_NAMLEN(dirent) strlen((dirent)->d_name) |
| 25 |
#else |
| 26 |
# define dirent direct |
| 27 |
# define DIR_NAMLEN(dirent) (dirent)->d_namlen |
| 28 |
# if HAVE_SYS_NDIR_H |
| 29 |
# include <sys/ndir.h> |
| 30 |
# endif |
| 31 |
# if HAVE_SYS_DIR_H |
| 32 |
# include <sys/dir.h> |
| 33 |
# endif |
| 34 |
# if HAVE_NDIR_H |
| 35 |
# include <ndir.h> |
| 36 |
# endif |
| 37 |
#endif |
| 38 |
#include <string.h> |
| 39 |
namespace Efont { |
| 40 |
|
| 41 |
PsresDatabase::PsresDatabase() |
| 42 |
: _section_map(0) |
| 43 |
{ |
| 44 |
_sections.push_back((PsresDatabaseSection *)0); |
| 45 |
} |
| 46 |
|
| 47 |
PsresDatabase::~PsresDatabase() |
| 48 |
{ |
| 49 |
for (int i = 1; i < _sections.size(); i++) |
| 50 |
delete _sections[i]; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
PsresDatabaseSection::PsresDatabaseSection(PermString section_name) |
| 55 |
: _section_name(section_name), _map(0) |
| 56 |
{ |
| 57 |
_directories.push_back(PermString()); |
| 58 |
_values.push_back(String()); |
| 59 |
_value_escaped.push_back(true); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/* read_ps_line - gathers a line in `slurper', returns true if it is a normal |
| 64 |
line, false on end of file or a separator line (starts with `.'). Handles |
| 65 |
continuation lines but not backslash escaping. Stores location of first |
| 66 |
non-backslashed equals sign in `*equals_pos', or -1 if none. */ |
| 67 |
|
| 68 |
static bool |
| 69 |
read_psres_line(Slurper &slurper, int *equals_pos) |
| 70 |
{ |
| 71 |
if (equals_pos) *equals_pos = -1; |
| 72 |
|
| 73 |
char *s = slurper.next_line(); |
| 74 |
if (!s) return false; |
| 75 |
|
| 76 |
bool is_terminator = s[0] == '.'; |
| 77 |
unsigned len, pos = 0, last_escape = ~0; |
| 78 |
bool found_eq = false; |
| 79 |
|
| 80 |
while (true) { |
| 81 |
len = slurper.cur_line_length(); |
| 82 |
if (len == 0) break; |
| 83 |
|
| 84 |
// process backslash escapes |
| 85 |
for (; pos < len - 1; pos++) { |
| 86 |
if (s[pos] == '\\') { |
| 87 |
// quote the next character |
| 88 |
pos++; |
| 89 |
last_escape = pos; |
| 90 |
} else if (!found_eq && s[pos] == '=') { |
| 91 |
// an equals sign: store its position |
| 92 |
if (equals_pos) *equals_pos = pos; |
| 93 |
found_eq = true; |
| 94 |
} else if (s[pos] == '%') { |
| 95 |
// unescaped '%' is a comment; return immediately after shortening line |
| 96 |
len = pos; |
| 97 |
goto done; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// stop processing if not a continuation line |
| 102 |
if (pos == len || s[pos] != '\\') break; |
| 103 |
|
| 104 |
slurper.shorten_line(pos); |
| 105 |
s = slurper.append_next_line(); |
| 106 |
} |
| 107 |
|
| 108 |
if (pos < len && !found_eq && s[pos] == '=' && equals_pos) |
| 109 |
*equals_pos = pos; |
| 110 |
|
| 111 |
done: |
| 112 |
// eat trailing whitespace, except for the space in `\ ' if it ends the line |
| 113 |
// -- unless the `\' is the second `\' in a `\\'! |
| 114 |
for (pos = len; pos > 0 && (s[pos-1] == ' ' || s[pos-1] == '\t'); pos--) |
| 115 |
/* nada */; |
| 116 |
if (pos == last_escape) |
| 117 |
pos++; |
| 118 |
slurper.shorten_line(pos); |
| 119 |
return !is_terminator; |
| 120 |
} |
| 121 |
|
| 122 |
/* psres_escape: backslash escaping */ |
| 123 |
|
| 124 |
static unsigned |
| 125 |
psres_escape(char *s, unsigned len) |
| 126 |
{ |
| 127 |
unsigned pos = 0, delta = 0; |
| 128 |
while (pos < len) { |
| 129 |
if (s[pos] == '\\') pos++, delta++; |
| 130 |
if (delta) s[pos-delta] = s[pos]; |
| 131 |
pos++; |
| 132 |
} |
| 133 |
s[pos-delta] = 0; |
| 134 |
return len-delta; |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
PsresDatabaseSection::add_psres_file_section |
| 139 |
(Slurper &slurper, PermString directory, bool override) |
| 140 |
{ |
| 141 |
int equals_pos; |
| 142 |
bool first_line = true; |
| 143 |
|
| 144 |
while (read_psres_line(slurper, &equals_pos)) { |
| 145 |
char *s = slurper.cur_line(); |
| 146 |
|
| 147 |
// check for a directory line |
| 148 |
if (first_line) { |
| 149 |
first_line = false; |
| 150 |
if (s[0] == '/') { |
| 151 |
psres_escape(s + 1, slurper.cur_line_length() - 1); |
| 152 |
directory = PermString(s + 1); |
| 153 |
continue; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (equals_pos < 0) { |
| 158 |
// report error? |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
// get the key |
| 163 |
unsigned len = psres_escape(s, equals_pos); |
| 164 |
PermString key = PermString(s, len); |
| 165 |
int index = _map[key]; |
| 166 |
if (!override && index > 0) |
| 167 |
continue; |
| 168 |
|
| 169 |
// double equals means absolute pathname |
| 170 |
if (s[equals_pos + 1] == '=') |
| 171 |
equals_pos++; |
| 172 |
|
| 173 |
// get the value. Don't escape it yet |
| 174 |
len = slurper.cur_line_length() - (equals_pos + 1); |
| 175 |
String value = String(s + equals_pos + 1, len); |
| 176 |
|
| 177 |
// stick key and value into our data structure |
| 178 |
if (index == 0) { |
| 179 |
index = _directories.size(); |
| 180 |
_directories.push_back(directory); |
| 181 |
_values.push_back(value); |
| 182 |
_value_escaped.push_back(false); |
| 183 |
_map.insert(key, index); |
| 184 |
} else { |
| 185 |
_directories[index] = directory; |
| 186 |
_values[index] = value; |
| 187 |
_value_escaped[index] = false; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
PsresDatabaseSection * |
| 194 |
PsresDatabase::force_section(PermString name) |
| 195 |
{ |
| 196 |
if (_section_map[name] > 0) |
| 197 |
return _sections[_section_map[name]]; |
| 198 |
else { |
| 199 |
PsresDatabaseSection *s = new PsresDatabaseSection(name); |
| 200 |
int index = _sections.size(); |
| 201 |
_sections.push_back(s); |
| 202 |
_section_map.insert(name, index); |
| 203 |
return s; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
bool |
| 208 |
PsresDatabase::add_one_psres_file(Slurper &slurper, bool override) |
| 209 |
{ |
| 210 |
if (!read_psres_line(slurper, 0)) |
| 211 |
return /* error */ false; |
| 212 |
|
| 213 |
char *s = slurper.cur_line(); |
| 214 |
unsigned len = slurper.cur_line_length(); |
| 215 |
if (len < 12 || memcmp(s, "PS-Resources", 12) != 0) |
| 216 |
return /* error */ false; |
| 217 |
|
| 218 |
bool exclusive = (len >= 22 && memcmp(s+12, "-Exclusive", 10) == 0); |
| 219 |
|
| 220 |
// skip list of sections |
| 221 |
while (read_psres_line(slurper, 0)) |
| 222 |
/* nada */; |
| 223 |
|
| 224 |
// now, read each section |
| 225 |
PermString directory = slurper.filename().directory(); |
| 226 |
|
| 227 |
while (read_psres_line(slurper, 0)) { |
| 228 |
s = slurper.cur_line(); |
| 229 |
len = psres_escape(s, slurper.cur_line_length()); |
| 230 |
PsresDatabaseSection *section = force_section(PermString(s, len)); |
| 231 |
section->add_psres_file_section(slurper, directory, override); |
| 232 |
} |
| 233 |
|
| 234 |
return exclusive; |
| 235 |
} |
| 236 |
|
| 237 |
bool |
| 238 |
PsresDatabase::add_psres_file(Filename &filename, bool override) |
| 239 |
{ |
| 240 |
Slurper slurpy(filename); |
| 241 |
return add_one_psres_file(slurpy, override); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
#ifndef WIN32 |
| 246 |
|
| 247 |
void |
| 248 |
PsresDatabase::add_psres_directory(PermString directory) |
| 249 |
{ |
| 250 |
DIR *dir = opendir(directory.c_str()); |
| 251 |
if (!dir) return; |
| 252 |
|
| 253 |
while (struct dirent *dirent = readdir(dir)) { |
| 254 |
int len = DIR_NAMLEN(dirent); |
| 255 |
if (len > 4 && dirent->d_name[0] != '.' |
| 256 |
&& memcmp(dirent->d_name + len - 4, ".upr", 4) == 0 |
| 257 |
&& (len != 9 || memcmp(dirent->d_name, "PSres.upr", 9) != 0)) { |
| 258 |
Filename fn(directory, PermString(dirent->d_name, len)); |
| 259 |
add_psres_file(fn, false); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
closedir(dir); |
| 264 |
} |
| 265 |
|
| 266 |
#else /* WIN32 */ |
| 267 |
|
| 268 |
void |
| 269 |
PsresDatabase::add_psres_directory(PermString directory) |
| 270 |
{ |
| 271 |
WIN32_FIND_DATA find_file_data; |
| 272 |
HANDLE hnd; |
| 273 |
int proceed = TRUE; |
| 274 |
|
| 275 |
PermString search_str = permcat(directory, "/*.*"); |
| 276 |
hnd = FindFirstFile(search_str.c_str(), &find_file_data); |
| 277 |
|
| 278 |
if (hnd == INVALID_HANDLE_VALUE) { |
| 279 |
return; |
| 280 |
} |
| 281 |
while (proceed) { |
| 282 |
int len = strlen(find_file_data.cFileName); |
| 283 |
if (len > 4 && find_file_data.cFileName[0] != '.' |
| 284 |
&& _strnicmp(find_file_data.cFileName + len - 4, ".upr", 4) == 0 |
| 285 |
&& (len != 9 || _strnicmp(find_file_data.cFileName, "PSres.upr", 9) != 0)) { |
| 286 |
Filename fn(directory, PermString(find_file_data.cFileName, len)); |
| 287 |
add_psres_file(fn, false); |
| 288 |
} |
| 289 |
proceed = FindNextFile (hnd, &find_file_data); |
| 290 |
} |
| 291 |
FindClose(hnd); |
| 292 |
} |
| 293 |
|
| 294 |
#endif /* WIN32 */ |
| 295 |
|
| 296 |
|
| 297 |
void |
| 298 |
PsresDatabase::add_psres_path(const char *path, const char *default_path, |
| 299 |
bool override) |
| 300 |
{ |
| 301 |
if (!path && !default_path) |
| 302 |
return; |
| 303 |
else if (!path) { |
| 304 |
path = default_path; |
| 305 |
default_path = 0; |
| 306 |
} |
| 307 |
|
| 308 |
if (override && _sections.size() > 1) { |
| 309 |
PsresDatabase new_db; |
| 310 |
new_db.add_psres_path(path, default_path, false); |
| 311 |
add_database(&new_db, true); |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
while (*path) { |
| 316 |
const char *epath = path; |
| 317 |
while (*epath && *epath != ':') epath++; |
| 318 |
|
| 319 |
PermString directory(path, epath - path); |
| 320 |
Filename filename(directory, "PSres.upr"); |
| 321 |
if (epath == path) { |
| 322 |
add_psres_path(default_path, 0, false); |
| 323 |
default_path = 0; // don't use default path twice |
| 324 |
} else if (!filename.readable() || !add_psres_file(filename, false)) |
| 325 |
add_psres_directory(directory); |
| 326 |
|
| 327 |
path = (*epath ? epath+1 : epath); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
void |
| 333 |
PsresDatabase::add_database(PsresDatabase *db, bool override) |
| 334 |
{ |
| 335 |
for (int i = 1; i < db->_sections.size(); i++) { |
| 336 |
PermString section_name = db->_sections[i]->section_name(); |
| 337 |
PsresDatabaseSection *section = force_section(section_name); |
| 338 |
section->add_section(db->_sections[i], override); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
PsresDatabaseSection::add_section(PsresDatabaseSection *s, bool override) |
| 344 |
{ |
| 345 |
for (HashMap<PermString, int>::const_iterator i = s->_map.begin(); i; i++) { |
| 346 |
int value = i.value(); |
| 347 |
if (_map[i.key()] <= 0) { |
| 348 |
int my_index = _directories.size(); |
| 349 |
_directories.push_back(s->_directories[value]); |
| 350 |
_values.push_back(s->_values[value]); |
| 351 |
_value_escaped.push_back(s->_value_escaped[value]); |
| 352 |
_map.insert(i.key(), my_index); |
| 353 |
} else if (override) { |
| 354 |
int my_index = _map[i.key()]; |
| 355 |
_directories[my_index] = s->_directories[value]; |
| 356 |
_values[my_index] = s->_values[value]; |
| 357 |
_value_escaped[my_index] = s->_value_escaped[value]; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
const String & |
| 364 |
PsresDatabaseSection::value(int index) |
| 365 |
{ |
| 366 |
if (_value_escaped[index]) |
| 367 |
return _values[index]; |
| 368 |
else { |
| 369 |
char *data = _values[index].mutable_data(); |
| 370 |
int len = psres_escape(data, _values[index].length()); |
| 371 |
_values[index] = _values[index].substring(0, len); |
| 372 |
_value_escaped[index] = true; |
| 373 |
return _values[index]; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
Filename |
| 378 |
PsresDatabaseSection::filename_value(PermString key) |
| 379 |
{ |
| 380 |
int index = _map[key]; |
| 381 |
if (!index) |
| 382 |
return Filename(); |
| 383 |
else if (!_directories[index]) |
| 384 |
return Filename(value(index)); |
| 385 |
else |
| 386 |
return Filename(_directories[index], value(index)); |
| 387 |
} |
| 388 |
|
| 389 |
const String & |
| 390 |
PsresDatabase::value(PermString sec, PermString key) const |
| 391 |
{ |
| 392 |
PsresDatabaseSection *s = section(sec); |
| 393 |
if (s) |
| 394 |
return s->value(key); |
| 395 |
else |
| 396 |
return String::make_empty(); |
| 397 |
} |
| 398 |
|
| 399 |
const String & |
| 400 |
PsresDatabase::unescaped_value(PermString sec, PermString key) const |
| 401 |
{ |
| 402 |
PsresDatabaseSection *s = section(sec); |
| 403 |
if (s) |
| 404 |
return s->unescaped_value(key); |
| 405 |
else |
| 406 |
return String::make_empty(); |
| 407 |
} |
| 408 |
|
| 409 |
Filename |
| 410 |
PsresDatabase::filename_value(PermString sec, PermString key) const |
| 411 |
{ |
| 412 |
PsresDatabaseSection *s = section(sec); |
| 413 |
if (s) |
| 414 |
return s->filename_value(key); |
| 415 |
else |
| 416 |
return Filename(); |
| 417 |
} |
| 418 |
|
| 419 |
} |