Debian Codegraph @

texk/web2c/luatexdir/tex/filename.c

raw
1 /*
2
3 filename.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26
27 /*tex
28
29 In order to isolate the system-dependent aspects of file names, the @^system
30 dependencies@> system-independent parts of \TeX\ are expressed in terms of
31 three system-dependent procedures called |begin_name|, |more_name|, and
32 |end_name|. In essence, if the user-specified characters of the file name are
33 $c_1\ldots c_n$, the system-independent driver program does the operations
34 $$|begin_name|;\,|more_name|(c_1);\,\ldots\,;\,|more_name|(c_n);
35 \,|end_name|.$$
36
37 These three procedures communicate with each other via global variables.
38 Afterwards the file name will appear in the string pool as three strings
39 called |cur_name|\penalty10000\hskip-.05em, |cur_area|, and |cur_ext|; the
40 latter two are null (i.e., |""|), unless they were explicitly specified by
41 the user.
42
43 Actually the situation is slightly more complicated, because \TeX\ needs to
44 know when the file name ends. The |more_name| routine is a function (with
45 side effects) that returns |true| on the calls |more_name|$(c_1)$, \dots,
46 |more_name|$(c_{n-1})$. The final call |more_name|$(c_n)$ returns |false|;
47 or, it returns |true| and the token following $c_n$ is something like
48 `\.{\\hbox}' (i.e., not a character). In other words, |more_name| is supposed
49 to return |true| unless it is sure that the file name has been completely
50 scanned; and |end_name| is supposed to be able to finish the assembly of
51 |cur_name|, |cur_area|, and |cur_ext| regardless of whether
52 $|more_name|(c_n)$ returned |true| or |false|.
53
54
55 Here now is the first of the system-dependent routines for file name
56 scanning. @^system dependencies@>
57
58 */
59
60 static void begin_name(void)
61 {
62 area_delimiter = 0;
63 ext_delimiter = 0;
64 quoted_filename = false;
65 }
66
67 /*tex
68
69 And here's the second. The string pool might change as the file name is being
70 scanned, since a new \.{\\csname} might be entered; therefore we keep
71 |area_delimiter| and |ext_delimiter| relative to the beginning of the current
72 string, instead of assigning an absolute address like |pool_ptr| to them.
73 @^system dependencies@>
74
75 */
76
77 static boolean more_name(ASCII_code c)
78 {
79 if (c == ' ' && stop_at_space && (!quoted_filename)) {
80 return false;
81 } else if (c == '"') {
82 quoted_filename = !quoted_filename;
83 return true;
84 } else {
85 str_room(1);
86 append_char(c);
87 if (IS_DIR_SEP(c)) {
88 area_delimiter = (pool_pointer) cur_length;
89 ext_delimiter = 0;
90 } else if (c == '.')
91 ext_delimiter = (pool_pointer) cur_length;
92 return true;
93 }
94 }
95
96 /*tex
97
98 The third. @^system dependencies@>
99
100 */
101
102 static void end_name(void)
103 {
104 unsigned char *s;
105 if (str_ptr + 3 > (max_strings + STRING_OFFSET))
106 overflow(
107 "number of strings",
108 (unsigned) (max_strings - init_str_ptr + STRING_OFFSET)
109 );
110 /*tex At this point, the full string lives in |cur_string|. */
111 if (area_delimiter == 0) {
112 cur_area = get_nullstr();
113 } else {
114 s = (unsigned char *) xstrdup((char *) (cur_string + area_delimiter));
115 cur_string[area_delimiter] = '\0';
116 cur_length = (unsigned) strlen((char *) cur_string);
117 cur_area = make_string();
118 xfree(cur_string);
119 cur_length = (unsigned) strlen((char *) s);
120 cur_string = s;
121 }
122 if (ext_delimiter == 0) {
123 cur_name = make_string();
124 cur_ext = get_nullstr();
125 } else {
126 int l = (ext_delimiter - area_delimiter - 1);
127 s = (unsigned char *) xstrdup((char *) (cur_string + l));
128 cur_string[l] = '\0';
129 cur_length = (unsigned) strlen((char *) cur_string);
130 cur_name = make_string();
131 xfree(cur_string);
132 cur_length = (unsigned) strlen((char *) s);
133 cur_string = s;
134 cur_ext = make_string();
135 }
136 }
137
138 /*tex
139
140 Now let's consider the ``driver'' routines by which \TeX\ deals with file
141 names in a system-independent manner. First comes a procedure that looks for a
142 file name in the input by calling |get_x_token| for the information.
143
144 */
145
146 void scan_file_name(void)
147 {
148 str_number u = 0;
149 name_in_progress = true;
150 begin_name();
151 /*tex Get the next non-blank non-call token: */
152 do {
153 get_x_token();
154 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
155
156 while (true) {
157 if ((cur_cmd > other_char_cmd) || (cur_chr > biggest_char)) { /* not a character */
158 back_input();
159 break;
160 }
161 /*tex
162 If |cur_chr| is a space and we're not scanning a token list, check
163 whether we're at the end of the buffer. Otherwise we end up adding
164 spurious spaces to file names in some cases.
165 */
166 if ((cur_chr == ' ') && (istate != token_list) && (iloc > ilimit)
167 && !quoted_filename)
168 break;
169 if (cur_chr > 127) {
170 unsigned char *bytes;
171 unsigned char *thebytes;
172 thebytes = uni2str((unsigned) cur_chr);
173 bytes = thebytes;
174 while (*bytes) {
175 if (!more_name(*bytes))
176 break;
177 bytes++;
178 }
179 xfree(thebytes);
180 } else {
181 if (!more_name(cur_chr))
182 break;
183 }
184 u = save_cur_string();
185 get_x_token();
186 restore_cur_string(u);
187 }
188 end_name();
189 name_in_progress = false;
190 }
191
192 /*
193 This function constructs a the three file name strings from a token list.
194 */
195
196 void scan_file_name_toks(void)
197 {
198 char *a, *n, *e, *s = NULL;
199 int i, l = 0;
200 int save_scanner_status = scanner_status;
201 halfword save_def_ref = def_ref;
202 (void) scan_toks(false, true);
203 s = tokenlist_to_cstring(def_ref, true, &l);
204 a = n = s;
205 e = NULL;
206 for (i = 0; i < l; i++) {
207 if (IS_DIR_SEP(s[i])) {
208 n = s + i + 1;
209 e = NULL;
210 } else if (s[i] == '.') {
211 e = s + i;
212 }
213 }
214 if (n != s) {
215 /*tex explicit area */
216 cur_area = maketexlstring(a, (size_t) (n - a));
217 } else {
218 cur_area = get_nullstr();
219 }
220 if (e != NULL) {
221 /*tex explicit extension */
222 cur_name = maketexlstring(n, (size_t) (e - n));
223 cur_ext = maketexstring(e);
224 } else {
225 cur_name = maketexstring(n);
226 cur_ext = get_nullstr();
227 }
228 flush_list(def_ref);
229 scanner_status = save_scanner_status;
230 def_ref = save_def_ref;
231 xfree(s);
232 }
233
234 /*tex
235
236 Here is a routine that manufactures the output file names, assuming that
237 |job_name<>0|. It ignores and changes the current settings of |cur_area| and
238 |cur_ext|; |s = ".log"|, |".dvi"|, or |format_extension|
239 */
240
241 char *pack_job_name(const char *s)
242 {
243 cur_area = get_nullstr();
244 cur_ext = maketexstring(s);
245 cur_name = job_name;
246 return pack_file_name(cur_name, cur_area, cur_ext);
247 }
248
249 /*tex
250
251 If some trouble arises when \TeX\ tries to open a file, the following routine
252 calls upon the user to supply another file name. Parameter~|s| is used in the
253 error message to identify the type of file; parameter~|e| is the default
254 extension if none is given. Upon exit from the routine, variables |cur_name|,
255 |cur_area|, and |cur_ext| are ready for another attempt at file opening.
256
257 */
258
259 char *prompt_file_name(const char *s, const char *e)
260 {
261 int k; /* index into |buffer| */
262 str_number saved_cur_name; /* to catch empty terminal input */
263 int callback_id ;
264 char prompt[256];
265 char *ar, *na, *ex;
266 saved_cur_name = cur_name;
267 if (interaction == scroll_mode) {
268 wake_up_terminal();
269 }
270 ar = makecstring(cur_area);
271 na = makecstring(cur_name);
272 ex = makecstring(cur_ext);
273 if (strcmp(s, "input file name") == 0) {
274 snprintf(prompt, 255, "I can't find file `%s%s%s'.", ar, na, ex);
275 } else {
276 snprintf(prompt, 255, "I can't write on file `%s%s%s'.", ar, na, ex);
277 }
278 free(ar);
279 free(na);
280 free(ex);
281 print_err(prompt);
282 callback_id = callback_defined(show_error_hook_callback);
283 if (callback_id > 0) {
284 flush_err();
285 run_callback(callback_id, "->");
286 } else {
287 if ((strcmp(e, ".tex") == 0) || (strcmp(e, "") == 0))
288 show_context();
289 if (strcmp(s, "input file name") == 0)
290 tprint_nl(promptfilenamehelpmsg ")");
291 }
292 tprint_nl("Please type another ");
293 tprint(s);
294 if (interaction < scroll_mode)
295 fatal_error("*** (job aborted, file error in nonstop mode)");
296 clear_terminal();
297 prompt_input(": ");
298 begin_name();
299 k = first;
300 while ((buffer[k] == ' ') && (k < last))
301 k++;
302 while (true) {
303 if (k == last)
304 break;
305 if (!more_name(buffer[k]))
306 break;
307 k++;
308 }
309 end_name();
310 if (cur_ext == get_nullstr())
311 cur_ext = maketexstring(e);
312 if (str_length(cur_name) == 0)
313 cur_name = saved_cur_name;
314 return pack_file_name(cur_name, cur_area, cur_ext);
315 }
316
317 void tprint_file_name(unsigned char *n, unsigned char *a, unsigned char *e)
318 {
319 boolean must_quote; /* whether to quote the filename */
320 unsigned char *j; /* index into string */
321 must_quote = false;
322 if (a != NULL) {
323 j = a;
324 while ((!must_quote) && (*j)) {
325 must_quote = (*j == ' ');
326 j++;
327 }
328 }
329 if (n != NULL) {
330 j = n;
331 while ((!must_quote) && (*j)) {
332 must_quote = (*j == ' ');
333 j++;
334 }
335 }
336 if (e != NULL) {
337 j = e;
338 while ((!must_quote) && (*j)) {
339 must_quote = (*j == ' ');
340 j++;
341 }
342 }
343 /*tex
344 Alternative is to assume that any filename that has to be quoted has at
345 least one quoted component...if we pick this, a number of insertions of
346 |print_file_name| should go away.
347 */
348 if (must_quote)
349 print_char('"');
350 if (a != NULL) {
351 for (j = a; *j; j++)
352 if (*j != '"')
353 print_char(*j);
354 }
355 if (n != NULL) {
356 for (j = n; *j; j++)
357 if (*j != '"')
358 print_char(*j);
359 }
360 if (e != NULL) {
361 for (j = e; *j; j++)
362 if (*j != '"')
363 print_char(*j);
364 }
365 if (must_quote)
366 print_char('"');
367 }
368
369 void print_file_name(str_number n, str_number a, str_number e)
370 {
371 char *nam, *are, *ext;
372 nam = makecstring(n);
373 are = makecstring(a);
374 ext = makecstring(e);
375 tprint_file_name(
376 (unsigned char *) nam,
377 (unsigned char *) are,
378 (unsigned char *) ext
379 );
380 free(nam);
381 free(are);
382 free(ext);
383 }