1 /* cnf.c: read config files.
3 Copyright 1994, 1995, 1996, 1997, 2008 Karl Berry.
4 Copyright 1997-2005 Olaf Weber.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19 #include <kpathsea/config.h>
20 #include <kpathsea/c-fopen.h>
21 #include <kpathsea/c-ctype.h>
22 #include <kpathsea/c-pathch.h>
23 #include <kpathsea/cnf.h>
24 #include <kpathsea/db.h>
25 #include <kpathsea/hash.h>
26 #include <kpathsea/line.h>
27 #include <kpathsea/paths.h>
28 #include <kpathsea/pathsearch.h>
29 #include <kpathsea/progname.h>
30 #include <kpathsea/recorder.h>
31 #include <kpathsea/tex-file.h>
32 #include <kpathsea/variable.h>
34 /* By using our own hash table, instead of the environment, we
35 complicate variable expansion (because we have to look in two
36 places), but we don't bang so much on the system. DOS and System V
37 have very limited environment space. Also, this way
38 `kpse_init_format' can distinguish between values originating from
39 the cnf file and ones from environment variables, which can be useful
40 for users trying to figure out what's going on. */
41 static hash_table_type cnf_hash;
42 #define CNF_HASH_SIZE 751
43 #define CNF_NAME "texmf.cnf"
45 /* Do a single line in a cnf file: if it's blank or a comment, skip it.
46 Otherwise, parse <variable>[.<program>] [=] <value>. Do
47 this even if the <variable> is already set in the environment, since
48 the envvalue might contain a trailing :, in which case we'll be
49 looking for the cnf value. */
52 do_line P1C(string, line)
59 /* Skip leading whitespace. */
60 while (ISSPACE (*line))
63 /* More to do only if we have non-comment material left. */
64 if (*line == 0 || *line == '%' || *line == '#')
67 /* The variable name is everything up to the next space or = or `.'. */
69 while (!ISSPACE (*line) && *line != '=' && *line != '.')
72 /* `line' is now one character past the end of the variable name. */
74 var = (string)xmalloc (len + 1);
75 strncpy (var, start, len);
78 /* If the variable is qualified with a program name, find out which. */
79 while (ISSPACE (*line))
82 /* Skip spaces, then everything up to the next space or =. */
84 while (ISSPACE (*line))
87 while (!ISSPACE (*line) && *line != '=')
90 /* It's annoying to repeat all this, but making a tokenizing
91 subroutine would be just as long and annoying. */
93 prog = (string)xmalloc (len + 1);
94 strncpy (prog, start, len);
98 /* Skip whitespace, an optional =, more whitespace. */
99 while (ISSPACE (*line))
103 while (ISSPACE (*line))
107 /* The value is whatever remains. Remove trailing whitespace. */
109 len = strlen (start);
110 while (len > 0 && ISSPACE (start[len - 1]))
113 value = (string)xmalloc (len + 1);
114 strncpy (value, start, len);
117 /* Suppose we want to write a single texmf.cnf that can be used under
118 both NT and Unix. This is feasible except for the path separators
119 : on Unix, ; on NT. We can't switch NT to allowing :'s, since :
120 is the drive separator. So we switch Unix to allowing ;'s. On the
121 other hand, we don't want to change IS_ENV_SEP and all the rest.
123 So, simply translate all ;'s in the path
124 values to :'s if we are a Unix binary. (Fortunately we don't use ;
125 in other kinds of texmf.cnf values.) */
127 if (IS_ENV_SEP(':')) {
129 for (loc = value; *loc; loc++) {
135 /* We want TEXINPUTS.prog to override plain TEXINPUTS. The simplest
136 way is to put both in the hash table (so we don't have to write
137 hash_delete and hash_replace, and keep track of values' sources),
138 and then look up the .prog version first in `kpse_cnf_get'. */
140 string lhs = concat3 (var, ".", prog);
145 hash_insert (&cnf_hash, var, value);
147 /* We could check that anything remaining is preceded by a comment
148 character, but let's not bother. */
151 /* Read all the configuration files in the path. */
154 read_all_cnf P1H(void)
158 const_string cnf_path = kpse_init_format (kpse_cnf_format);
160 cnf_hash = hash_create (CNF_HASH_SIZE);
162 cnf_files = kpse_all_path_search (cnf_path, CNF_NAME);
163 if (cnf_files && *cnf_files) {
164 for (cnf = cnf_files; *cnf; cnf++) {
166 FILE *cnf_file = xfopen (*cnf, FOPEN_R_MODE);
167 if (kpse_record_input)
168 kpse_record_input (*cnf);
170 while ((line = read_line (cnf_file)) != NULL) {
171 unsigned len = strlen (line);
172 /* Strip trailing spaces. */
173 while (len > 0 && ISSPACE(line[len-1])) {
177 /* Concatenate consecutive lines that end with \. */
178 while (len > 0 && line[len - 1] == '\\') {
179 string next_line = read_line (cnf_file);
182 WARNING1 ("%s: Last line ends with \\", *cnf);
185 new_line = concat (line, next_line);
196 xfclose (cnf_file, *cnf);
201 WARNING1 ("Configuration file texmf.cnf not found! Searched these directories:\n%s\nTrying to proceed..", cnf_path);
204 /* Read the cnf files on the first call. Return the first value in the
205 returned list -- this will be from the last-read cnf file. */
208 kpse_cnf_get P1C(const_string, name)
212 static boolean doing_cnf_init = false;
214 /* When we expand the compile-time value for DEFAULT_TEXMFCNF,
215 we end up needing the value for TETEXDIR and other variables,
216 so kpse_var_expand ends up calling us again. No good. */
220 if (cnf_hash.size == 0) {
221 doing_cnf_init = true;
223 doing_cnf_init = false;
225 /* Here's a pleasant kludge: Since `kpse_init_dbs' recursively calls
226 us, we must call it from outside a `kpse_path_element' loop
227 (namely, the one in `read_all_cnf' above): `kpse_path_element' is
232 /* First look up NAME.`kpse_program_name', then NAME. */
233 assert (kpse_program_name);
234 ctry = concat3 (name, ".", kpse_program_name);
235 ret_list = hash_lookup (cnf_hash, ctry);
241 ret_list = hash_lookup (cnf_hash, name);