| 1 |
/* cnf.c: read config files. |
| 2 |
|
| 3 |
Copyright 1994, 1995, 1996, 1997, 2008, 2009, 2011, 2012, 2016, |
| 4 |
2017, 2018, 2019, 2025 Karl Berry. |
| 5 |
Copyright 1997-2005 Olaf Weber. |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Lesser General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 2.1 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
This library is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
Lesser General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Lesser General Public License |
| 18 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include <kpathsea/config.h> |
| 21 |
#include <kpathsea/c-fopen.h> |
| 22 |
#include <kpathsea/c-ctype.h> |
| 23 |
#include <kpathsea/c-pathch.h> |
| 24 |
#include <kpathsea/cnf.h> |
| 25 |
#include <kpathsea/db.h> |
| 26 |
#include <kpathsea/hash.h> |
| 27 |
#include <kpathsea/line.h> |
| 28 |
#include <kpathsea/paths.h> |
| 29 |
#include <kpathsea/pathsearch.h> |
| 30 |
#include <kpathsea/tex-file.h> |
| 31 |
#include <kpathsea/variable.h> |
| 32 |
|
| 33 |
/* By using our own hash table, instead of the environment, we |
| 34 |
complicate variable expansion (because we have to look in two |
| 35 |
places), but we don't bang so much on the system. DOS and System V |
| 36 |
have very limited environment space. Also, this way |
| 37 |
`kpse_init_format' can distinguish between values originating from |
| 38 |
the cnf file and ones from environment variables, which can be useful |
| 39 |
for users trying to figure out what's going on. */ |
| 40 |
|
| 41 |
#define CNF_HASH_SIZE 751 |
| 42 |
#define CNF_NAME "texmf.cnf" |
| 43 |
|
| 44 |
|
| 45 |
/* Do a single line LINE in a cnf file: if it's blank or a comment or |
| 46 |
erroneous, skip it. Otherwise, parse |
| 47 |
<variable>[.<program>] [=] <value> |
| 48 |
and insert it either into the cnf_hash structure in KPSE (if |
| 49 |
ENV_PROGNAME is false), or in the environment (if ENV_PROGNAME is true). |
| 50 |
|
| 51 |
Furthermore, if ENV_PROGNAME is true, and there is no .<program> in |
| 52 |
LINE, then also insert <variable> "_" `kpse->program_name' with <value> |
| 53 |
into the environment; see kpathsea_cnf_line_env_progname in cnf.h. |
| 54 |
|
| 55 |
The name without _<progname> will usually never be looked up, but |
| 56 |
just in case the program name changes, or whatever. |
| 57 |
|
| 58 |
We return NULL if ok, an error string otherwise. */ |
| 59 |
|
| 60 |
static string |
| 61 |
do_line (kpathsea kpse, string line, boolean env_progname) |
| 62 |
{ |
| 63 |
unsigned len; |
| 64 |
string start; |
| 65 |
string value, var; |
| 66 |
string prog = NULL; |
| 67 |
|
| 68 |
/* Skip leading whitespace. */ |
| 69 |
while (*line && ISSPACE (*line)) |
| 70 |
line++; |
| 71 |
|
| 72 |
/* More to do only if we have non-comment material left. */ |
| 73 |
if (*line == 0 || *line == '%' || *line == '#') |
| 74 |
return NULL; |
| 75 |
|
| 76 |
/* Remove trailing comment: a % or # preceded by whitespace. Also |
| 77 |
remove any whitespace before that. For example, the value for |
| 78 |
foo = a#b %something |
| 79 |
is a#b. */ |
| 80 |
value = line + strlen (line) - 1; /* start at end of line */ |
| 81 |
while (value > line) { |
| 82 |
if (*value == '%' || *value == '#') { |
| 83 |
value--; /* move before comment char */ |
| 84 |
while (ISSPACE (*value)) |
| 85 |
*value-- = 0; /* wipe out as much preceding whitespace |
| 86 |
continue; (and comment) as we find */ |
| 87 |
} |
| 88 |
value--; /* move before the new null byte */ |
| 89 |
} |
| 90 |
|
| 91 |
/* The variable name is everything up to the next space or = or `.'. */ |
| 92 |
start = line; |
| 93 |
while (*line && !ISSPACE (*line) && *line != '=' && *line != '.') |
| 94 |
line++; |
| 95 |
|
| 96 |
/* `line' is now one character past the end of the variable name. */ |
| 97 |
len = line - start; |
| 98 |
if (len == 0) { |
| 99 |
return ("No cnf variable name"); |
| 100 |
} |
| 101 |
|
| 102 |
var = (string) xmalloc (len + 1); |
| 103 |
strncpy (var, start, len); |
| 104 |
var[len] = 0; |
| 105 |
|
| 106 |
/* If the variable is qualified with a program name, extract it. */ |
| 107 |
while (*line && ISSPACE (*line)) |
| 108 |
line++; |
| 109 |
if (*line == '.') { |
| 110 |
/* Skip spaces, then everything up to the next space or =. */ |
| 111 |
line++; |
| 112 |
while (ISSPACE (*line)) |
| 113 |
line++; |
| 114 |
start = line; |
| 115 |
while (*line && !ISSPACE (*line) && *line != '=') |
| 116 |
line++; |
| 117 |
|
| 118 |
/* The program name is what's in between. */ |
| 119 |
len = line - start; |
| 120 |
prog = (string) xmalloc (len + 1); |
| 121 |
strncpy (prog, start, len); |
| 122 |
prog[len] = 0; |
| 123 |
/* If the name is empty, or contains one of our usual special |
| 124 |
characters, it's probably a mistake. For instance, a cnf line |
| 125 |
foo .;bar |
| 126 |
is interpreted as a program name ";bar", because the = between |
| 127 |
the variable name and value is optional. We don't try to guess |
| 128 |
the user's intentions, but just give a warning. */ |
| 129 |
if (len == 0) { |
| 130 |
return ("Empty program name qualifier"); |
| 131 |
} else { |
| 132 |
unsigned i; |
| 133 |
for (i = 0; i < len; i++) { |
| 134 |
if (prog[i] == '$' || prog[i] == '{' || prog[i] == '}' |
| 135 |
|| IS_KPSE_SEP (prog[i])) { |
| 136 |
string msg = xmalloc (50); |
| 137 |
sprintf (msg, "Unlikely character %c in program name", prog[i]); |
| 138 |
return msg; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* Skip whitespace, an optional =, more whitespace. */ |
| 145 |
while (*line && ISSPACE (*line)) |
| 146 |
line++; |
| 147 |
if (*line == '=') { |
| 148 |
line++; |
| 149 |
while (*line && ISSPACE (*line)) |
| 150 |
line++; |
| 151 |
} |
| 152 |
|
| 153 |
/* The value is whatever remains. Remove trailing whitespace. */ |
| 154 |
start = line; |
| 155 |
len = strlen (start); |
| 156 |
while (len > 0 && ISSPACE (start[len - 1])) |
| 157 |
len--; |
| 158 |
if (len == 0) { |
| 159 |
return ("No cnf value"); |
| 160 |
} |
| 161 |
|
| 162 |
value = (string) xmalloc (len + 1); |
| 163 |
strncpy (value, start, len); |
| 164 |
value[len] = 0; |
| 165 |
|
| 166 |
/* Suppose we want to write a single texmf.cnf that can be used under |
| 167 |
both Windows and Unix. This is feasible except for the path |
| 168 |
separators: : on Unix, ; on Windows. We can't switch Windows to |
| 169 |
allowing :, since : is the drive separator. So we switch Unix to |
| 170 |
allowing ;. On the other hand, we don't want to change IS_ENV_SEP |
| 171 |
and all the rest. |
| 172 |
|
| 173 |
So, translate all ;'s in the path values to :'s if we'd normally |
| 174 |
use :. (Fortunately we don't use ; as a normal character in values.) */ |
| 175 |
|
| 176 |
if (IS_ENV_SEP(':')) { |
| 177 |
string loc; |
| 178 |
for (loc = value; *loc; loc++) { |
| 179 |
if (*loc == ';') |
| 180 |
*loc = ':'; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* If we're supposed to make the setting in the environment ... */ |
| 185 |
if (env_progname) { |
| 186 |
string this_prog = prog ? prog : kpse->program_name; /* maybe with .prog */ |
| 187 |
/* last-ditch debug */ |
| 188 |
/* fprintf (stderr, "kpse/cnf.c xputenv(%s,%s)\n", var, value); */ |
| 189 |
xputenv (var, value); |
| 190 |
/* Use the .prog name on the input line if specified. Otherwise, |
| 191 |
although kpse->program_name should always be set at this point, |
| 192 |
check just in case. */ |
| 193 |
if (this_prog) { |
| 194 |
string var_prog = concat3 (var, "_", this_prog); |
| 195 |
/* fprintf (stderr, "kpse/cnf.c xputenv(%s,%s) [implicit]\n", |
| 196 |
var_prog, value); */ |
| 197 |
xputenv (var_prog, value); |
| 198 |
free (var_prog); /* xputenv allocates its own */ |
| 199 |
} |
| 200 |
free (var); /* again, xputenv allocated a copy */ |
| 201 |
|
| 202 |
} else { |
| 203 |
/* Normal case of not ENV_PROGNAME, insert in cnf_hash, with .prog |
| 204 |
if specified, which will override non-.prog. */ |
| 205 |
string lhs = prog ? concat3 (var, ".", prog) : var; |
| 206 |
/* last-ditch debug */ |
| 207 |
/* fprintf (stderr, "kpse/cnf.c hash_insert(%s,%s)\n", lhs, value); */ |
| 208 |
hash_insert (&(kpse->cnf_hash), lhs, value); |
| 209 |
if (prog) { /* the lhs string is new memory if we had .prog */ |
| 210 |
free (var); |
| 211 |
/* If there was no .prog on the line, the original `var' memory gets |
| 212 |
inserted into the hash table, so do not free. */ |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/* We should check that anything remaining is preceded by a comment |
| 217 |
character, but we don't. Sorry. */ |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/* Just passing along env_progname = true to do_line. */ |
| 223 |
|
| 224 |
void |
| 225 |
kpathsea_cnf_line_env_progname (kpathsea kpse, string line) |
| 226 |
{ |
| 227 |
string msg = do_line (kpse, line, /* env_progname = */ true); |
| 228 |
if (msg) { |
| 229 |
WARNING2 ("command line (kpathsea): %s in argument: %s", |
| 230 |
msg, line); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/* Read all the kpathsea configuration files in the path. */ |
| 236 |
|
| 237 |
static void |
| 238 |
read_all_cnf (kpathsea kpse) |
| 239 |
{ |
| 240 |
string *cnf_files; |
| 241 |
string *cnf; |
| 242 |
const_string cnf_path = kpathsea_init_format (kpse, kpse_cnf_format); |
| 243 |
|
| 244 |
cnf_files = kpathsea_all_path_search (kpse, cnf_path, CNF_NAME); |
| 245 |
if (cnf_files && *cnf_files) { |
| 246 |
for (cnf = cnf_files; *cnf; cnf++) { |
| 247 |
string line; |
| 248 |
string msg; |
| 249 |
unsigned lineno = 0; |
| 250 |
FILE *cnf_file = xfopen (*cnf, FOPEN_R_MODE); |
| 251 |
if (kpse->record_input) |
| 252 |
kpse->record_input (*cnf); |
| 253 |
|
| 254 |
while ((line = read_line (cnf_file)) != NULL) { |
| 255 |
unsigned len; |
| 256 |
lineno++; |
| 257 |
len = strlen (line); |
| 258 |
/* Strip trailing spaces. */ |
| 259 |
while (len > 0 && ISSPACE(line[len-1])) { |
| 260 |
line[len - 1] = 0; |
| 261 |
--len; |
| 262 |
} |
| 263 |
/* Concatenate consecutive lines that end with \. */ |
| 264 |
while (len > 0 && line[len - 1] == '\\') { |
| 265 |
string next_line = read_line (cnf_file); |
| 266 |
lineno++; |
| 267 |
line[len - 1] = 0; |
| 268 |
if (!next_line) { |
| 269 |
WARNING2 ("%s:%d: (kpathsea) Last line of file ends with \\", |
| 270 |
*cnf, lineno); |
| 271 |
} else { |
| 272 |
string new_line; |
| 273 |
new_line = concat (line, next_line); |
| 274 |
free (line); |
| 275 |
line = new_line; |
| 276 |
len = strlen (line); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
msg = do_line (kpse, line, /* env_progname= */ false); |
| 281 |
if (msg) { |
| 282 |
WARNING4 ("%s:%d: (kpathsea) %s on line: %s", |
| 283 |
*cnf, lineno, msg, line); |
| 284 |
} |
| 285 |
free (line); |
| 286 |
} |
| 287 |
|
| 288 |
xfclose (cnf_file, *cnf); |
| 289 |
free (*cnf); |
| 290 |
} |
| 291 |
free (cnf_files); |
| 292 |
} else { |
| 293 |
string warn = getenv ("KPATHSEA_WARNING"); |
| 294 |
if (!(warn && STREQ (warn, "0"))) { |
| 295 |
WARNING1 |
| 296 |
("kpathsea: configuration file texmf.cnf not found in these directories: %s", |
| 297 |
cnf_path); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/* Read the cnf files on the first call. Return the first value in the |
| 303 |
returned list -- this will be from the last-read cnf file. */ |
| 304 |
|
| 305 |
const_string |
| 306 |
kpathsea_cnf_get (kpathsea kpse, const_string name) |
| 307 |
{ |
| 308 |
string ctry; |
| 309 |
const_string ret, *ret_list; |
| 310 |
|
| 311 |
/* When we expand the compile-time value for DEFAULT_TEXMFCNF, |
| 312 |
we end up needing the value for assorted variables, |
| 313 |
so kpse_var_expand ends up calling us again. Just return. */ |
| 314 |
if (kpse->doing_cnf_init) |
| 315 |
return NULL; |
| 316 |
|
| 317 |
/* If no cnf hash yet, initialize. */ |
| 318 |
if (kpse->cnf_hash.size == 0) { |
| 319 |
kpse->cnf_hash = hash_create (CNF_HASH_SIZE); |
| 320 |
|
| 321 |
/* Read configuration files and initialize databases. */ |
| 322 |
kpse->doing_cnf_init = true; |
| 323 |
read_all_cnf (kpse); |
| 324 |
kpse->doing_cnf_init = false; |
| 325 |
|
| 326 |
/* Since `kpse_init_db' recursively calls us, we must call it from |
| 327 |
outside a `kpse_path_element' loop (namely, the one in |
| 328 |
`read_all_cnf' above): `kpse_path_element' is not reentrant. */ |
| 329 |
kpathsea_init_db (kpse); |
| 330 |
} |
| 331 |
|
| 332 |
/* First look up NAME.`kpse->program_name', then NAME. */ |
| 333 |
assert (kpse->program_name); |
| 334 |
ctry = concat3 (name, ".", kpse->program_name); |
| 335 |
ret_list = hash_lookup (kpse->cnf_hash, ctry); |
| 336 |
free (ctry); |
| 337 |
if (ret_list) { |
| 338 |
ret = *ret_list; |
| 339 |
free (ret_list); |
| 340 |
} else { |
| 341 |
ret_list = hash_lookup (kpse->cnf_hash, name); |
| 342 |
if (ret_list) { |
| 343 |
ret = *ret_list; |
| 344 |
free (ret_list); |
| 345 |
} else { |
| 346 |
ret = NULL; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return ret; |
| 351 |
} |
| 352 |
|
| 353 |
#if defined(KPSE_COMPAT_API) |
| 354 |
const_string |
| 355 |
kpse_cnf_get (const_string name) |
| 356 |
{ |
| 357 |
return kpathsea_cnf_get(kpse_def, name); |
| 358 |
} |
| 359 |
#endif |
| 360 |
|
| 361 |
|
| 362 |
/* Return true if S is (loosely) true. This function exists since a |
| 363 |
macro unavoidably has to evaluate the argument multiple times, which |
| 364 |
generates an irritating number of debugging messages. */ |
| 365 |
|
| 366 |
boolean |
| 367 |
kpse_cnf_p (const_string s) |
| 368 |
{ |
| 369 |
return s && *s && *(s) != 'f' && *(s) != '0'; |
| 370 |
} |