This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pykpathsea.c
345 lines (258 loc) · 8.99 KB
/
pykpathsea.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* kpsewhich -- standalone path lookup and variable expansion for Kpathsea.
Ideas from Thomas Esser, Pierre MacKay, and many others.
Copyright 1995-2016 Karl Berry & Olaf Weber.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, see <http://www.gnu.org/licenses/>. */
#include <kpathsea/config.h>
#include <kpathsea/c-ctype.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/expand.h>
#include <kpathsea/getopt.h>
#include <kpathsea/line.h>
#include <kpathsea/pathsearch.h>
#include <kpathsea/proginit.h>
#include <kpathsea/str-list.h>
#include <kpathsea/tex-file.h>
#include <kpathsea/tex-glyph.h>
#include <kpathsea/variable.h>
#include <kpathsea/version.h>
#include <string.h>
#include <Python.h>
kpathsea kpse = NULL;
/* Base resolution. (-D, -dpi) */
unsigned dpi = 600;
/* Search the disk as well as ls-R? (-must-exist, -mktex) */
boolean must_exist = false;
/* The file type and path for lookups. (-format, -path) */
/* Define one-word abbreviations for those format types which
can otherwise only be specified by strings containing spaces. */
typedef struct
{
const_string abbr;
kpse_file_format_type format;
} format_abbr_type;
static format_abbr_type format_abbrs[]
= { { "bitmapfont", kpse_any_glyph_format },
{ "mpsupport", kpse_mpsupport_format },
{ "doc", kpse_texdoc_format },
{ "source", kpse_texsource_format },
{ "trofffont", kpse_troff_font_format },
{ "dvipsconfig", kpse_dvips_config_format },
{ "web2c", kpse_web2c_format },
{ "othertext", kpse_program_text_format },
{ "otherbin", kpse_program_binary_format },
{ "miscfont", kpse_miscfonts_format },
{ "cmap", kpse_cmap_format },
{ "pdftexconfig", kpse_pdftex_config_format },
{ NULL, kpse_last_format } };
/* The function to look up STR in the abbr table above.
This is called only on a user-specified format string.
Return `kpse_last_format' if no match. */
static kpse_file_format_type
format_abbr (const_string str)
{
kpse_file_format_type ret = kpse_last_format;
unsigned a = 0;
while (format_abbrs[a].abbr != NULL) {
if (STREQ (str, format_abbrs[a].abbr)) {
ret = format_abbrs[a].format;
break;
}
a++;
}
return ret;
}
/* Return the <number> substring in `<name>.<number><stuff>', if S has
that form. If it doesn't, return 0. */
static unsigned
find_dpi (string s)
{
unsigned dpi_number = 0;
const_string extension = find_suffix (s);
if (extension != NULL)
sscanf (extension, "%u", &dpi_number);
return dpi_number;
}
/* Return true if FTRY (the candidate suffix) matches NAME. If
IS_FILENAME is true, the check is simply that FTRY is a suffix of
NAME. If false (that is, NAME is a format), then FTRY and NAME must
be entirely equal. */
static boolean
try_suffix (boolean is_filename, string name, unsigned name_len,
const_string ftry)
{
unsigned try_len;
if (!ftry || ! *ftry) {
return false;
}
try_len = strlen (ftry);
if (try_len > name_len) {
/* Candidate is longer than what we're looking for. */
return false;
}
if (!is_filename && try_len < name_len) {
/* We're doing format names, not file names, and candidate is
shorter than what we're looking for. E.g., do not find `lua'
when looking for `clua'. */
return false;
}
if (FILESTRCASEEQ (name + name_len - try_len, ftry)) {
return true;
}
return false;
}
/* Use the file type from -format if that was previously determined
(i.e., the user_format global variable), else guess dynamically from
NAME. Return kpse_last_format if undeterminable. This function is
also used to parse the -format string, a case we distinguish via
is_filename being false.
A few filenames have been hard-coded for format types that
differ from what would be inferred from their extensions. */
static kpse_file_format_type
find_format (kpathsea kpse, string name, boolean is_filename)
{
kpse_file_format_type ret = kpse_last_format;
if (FILESTRCASEEQ (name, "config.ps")) {
ret = kpse_dvips_config_format;
} else if (FILESTRCASEEQ (name, "fmtutil.cnf")) {
ret = kpse_web2c_format;
} else if (FILESTRCASEEQ (name, "glyphlist.txt")) {
ret = kpse_fontmap_format;
} else if (FILESTRCASEEQ (name, "mktex.cnf")) {
ret = kpse_web2c_format;
} else if (FILESTRCASEEQ (name, "pdfglyphlist.txt")) {
ret = kpse_fontmap_format;
} else if (FILESTRCASEEQ (name, "pdftex.cfg")) {
ret = kpse_pdftex_config_format;
} else if (FILESTRCASEEQ (name, "texglyphlist.txt")) {
ret = kpse_fontmap_format;
} else if (FILESTRCASEEQ (name, "texmf.cnf")) {
ret = kpse_cnf_format;
} else if (FILESTRCASEEQ (name, "updmap.cfg")) {
ret = kpse_web2c_format;
} else if (FILESTRCASEEQ (name, "XDvi")) {
ret = kpse_program_text_format;
} else {
if (!is_filename) {
/* Look for kpsewhich-specific format abbreviations. */
ret = format_abbr (name);
}
if (ret == kpse_last_format) {
int f = 0; /* kpse_file_format_type */
unsigned name_len = strlen (name);
while (f != kpse_last_format) {
const_string *ext;
const_string ftry;
boolean found = false;
if (!kpse->format_info[f].type)
kpathsea_init_format (kpse, (kpse_file_format_type) f);
/* Just to abbreviate this lengthy call. */
#define TRY_SUFFIX(ftry) try_suffix (is_filename, name, name_len, (ftry))
if (!is_filename) {
/* Allow the long name, but only in the format options. We don't
want a filename confused with a format name. */
ftry = kpse->format_info[f].type;
found = TRY_SUFFIX (ftry);
}
for (ext = kpse->format_info[f].suffix; !found && ext && *ext; ext++) {
found = TRY_SUFFIX (*ext);
//printf("Trying1 %s %d\n", *ext, found);
}
for (ext=kpse->format_info[f].alt_suffix; !found && ext && *ext;ext++){
found = TRY_SUFFIX (*ext);
//printf("Trying2 %s %d\n", *ext, found);
}
if (found)
break;
f++;
}
ret = f;
}
}
return ret;
}
/* Look up a single filename NAME. Return 0 if success, 1 if failure. */
static string
lookup (kpathsea kpse, string name)
{
string ret = NULL;
/* No user-specified search path, check user format or guess from NAME. */
kpse_file_format_type fmt = find_format (kpse, name, true);
switch (fmt) {
case kpse_pk_format:
case kpse_gf_format:
case kpse_any_glyph_format:
{
kpse_glyph_file_type glyph_ret;
string temp = remove_suffix (name);
/* Try to extract the resolution from the name. */
unsigned local_dpi = find_dpi (name);
if (!local_dpi)
local_dpi = dpi;
ret = kpathsea_find_glyph (kpse, temp,
local_dpi, fmt, &glyph_ret);
if (temp != name)
free (temp);
}
break;
case kpse_last_format:
/* If the suffix isn't recognized, assume it's a tex file. */
fmt = kpse_tex_format;
/* fall through */
default:
ret = kpathsea_find_file (kpse, name, fmt, must_exist);
}
return ret;
}
static PyObject *py_kpse_find_file(PyObject *self, PyObject *args)
{
char *filename;
char *completefilename;
PyObject *returnvalue;
if (PyArg_ParseTuple(args, "s", &filename)) {
completefilename = lookup(kpse, filename);
returnvalue = Py_BuildValue("s", completefilename);
if(completefilename != NULL)
free(completefilename);
return returnvalue;
}
return NULL;
}
/* exported methods */
static PyMethodDef pykpathsea_methods[] = {
{"find_file", (PyCFunction) py_kpse_find_file, METH_VARARGS, NULL},
{NULL, NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pykpathsea",
NULL,
-1,
pykpathsea_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_pykpathsea(void)
{
PyObject *module = PyModule_Create(&moduledef);
if (module == NULL)
return NULL;
kpse = kpathsea_new();
kpathsea_set_program_name (kpse, "pdftex", "pdftex");
kpathsea_set_program_enabled (kpse, kpse_pk_format, true, kpse_src_cmdline - 1);
kpathsea_set_program_enabled (kpse, kpse_tfm_format, true, kpse_src_cmdline - 1);
kpathsea_xputenv (kpse, "engine", "pdftex");
kpathsea_init_prog (kpse, uppercasify (kpse->program_name), dpi, NULL, NULL);
return module;
}