Debian Codegraph @

texk/web2c/luatexdir/tex/dumpdata.c

raw
1 /*
2
3 dumpdata.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 /*tex
27
28 We use a magic number to register the version of the format. Normally this
29 number only increments when we add a new primitive of change command codes.
30 We start with 907 which is the sum of the values of the bytes of \quote
31 {don knuth}.
32
33 */
34
35 #define FORMAT_ID (907+60)
36 #if ((FORMAT_ID>=0) && (FORMAT_ID<=256))
37 #error Wrong value for FORMAT_ID.
38 #endif
39
40 /*tex
41
42 After \.{INITEX} has seen a collection of fonts and macros, it can write all the
43 necessary information on an auxiliary file so that production versions of \TeX\
44 are able to initialize their memory at high speed. The present section of the
45 program takes care of such output and input. We shall consider simultaneously the
46 processes of storing and restoring, so that the inverse relation between them is
47 clear. @.INITEX@>
48
49 The global variable |format_ident| is a string that is printed right after the
50 |banner| line when \TeX\ is ready to start. For \.{INITEX} this string says
51 simply `\.{(INITEX)}'; for other versions of \TeX\ it says, for example,
52 `\.{(preloaded format=plain 1982.11.19)}', showing the year, month, and day that
53 the format file was created. We have |format_ident=0| before \TeX's tables are
54 loaded. |FORMAT_ID| is a new field of type int suitable for the identification of
55 a format: values between 0 and 256 (included) can not be used because in the
56 previous format they are used for the length of the name of the engine.
57
58 */
59
60 str_number format_ident;
61 str_number format_name;
62
63
64 /*tex
65
66 Format files consist of |memory_word| items, and we use the following macros to
67 dump words of different types:
68
69 */
70
71 FILE *fmt_file;
72
73 void store_fmt_file(void)
74 {
75 int j, k, l, x;
76 halfword p;
77 char *format_engine;
78 int callback_id;
79 char *fmtname = NULL;
80 /*tex
81 If dumping is not allowed, abort. The user is not allowed to dump a
82 format file unless |save_ptr=0|. This condition implies that
83 |cur_level=level_one|, hence the |xeq_level| array is constant and it
84 need not be dumped.
85 */
86 if (save_ptr != 0) {
87 print_err("You can't dump inside a group");
88 help1("`{...\\dump}' is a no-no.");
89 succumb();
90 }
91 /*tex
92 Create the |format_ident|, open the format file, and inform the user that
93 dumping has begun.
94 */
95 callback_id = callback_defined(pre_dump_callback);
96 if (callback_id > 0) {
97 (void) run_callback(callback_id, "->");
98 }
99 selector = new_string;
100 tprint(" (format=");
101 print(job_name);
102 print_char(' ');
103 print_int(year_par);
104 print_char('.');
105 print_int(month_par);
106 print_char('.');
107 print_int(day_par);
108 print_char(')');
109 str_room(2);
110 format_ident = make_string();
111 print(job_name);
112 format_name = make_string();
113 if (interaction == batch_mode) {
114 selector = log_only;
115 } else {
116 selector = term_and_log;
117 }
118 fmtname = pack_job_name(format_extension);
119 while (!zopen_w_output(&fmt_file, fmtname, FOPEN_WBIN_MODE)) {
120 fmtname = prompt_file_name("format file name", format_extension);
121 }
122 tprint_nl("Beginning to dump on file ");
123 tprint(fmtname);
124 free(fmtname);
125 tprint_nl("");
126 print(format_ident);
127 /*tex
128 Dump constants for consistency check. The next few sections of the
129 program should make it clear how we use the dump/undump macros. First
130 comes Web2C \TeX's magic constant: "W2TX"
131 */
132 dump_int(0x57325458);
133 dump_int(FORMAT_ID);
134 /*tex
135 We align |engine_name| to 4 bytes with one or more trailing |NUL|.
136 */
137 x = (int) strlen(engine_name);
138 format_engine = xmalloc((unsigned) (x + 4));
139 strcpy(format_engine, engine_name);
140 for (k = x; k <= x + 3; k++)
141 format_engine[k] = 0;
142 x = x + 4 - (x % 4);
143 dump_int(x);
144 dump_things(format_engine[0], x);
145 xfree(format_engine);
146 dump_int(0x57325458);
147 dump_int(max_halfword);
148 dump_int(hash_high);
149 dump_int(eqtb_size);
150 dump_int(hash_prime);
151 /*tex Dump the string pool. */
152 k = dump_string_pool();
153 print_ln();
154 print_int(k);
155 tprint(" strings using ");
156 print_int((longinteger) pool_size);
157 tprint(" bytes");
158 /*tex
159 Dump the dynamic memory. By sorting the list of available spaces in the
160 variable-size portion of |mem|, we are usually able to get by without
161 having to dump very much of the dynamic memory.
162
163 We recompute |var_used| and |dyn_used|, so that \.{INITEX} dumps valid
164 information even when it has not been gathering statistics.
165 */
166 dump_node_mem();
167 dump_int(temp_token_head);
168 dump_int(hold_token_head);
169 dump_int(omit_template);
170 dump_int(null_list);
171 dump_int(backup_head);
172 dump_int(garbage);
173 x = (int) fix_mem_min;
174 dump_int(x);
175 x = (int) fix_mem_max;
176 dump_int(x);
177 x = (int) fix_mem_end;
178 dump_int(x);
179 dump_int(avail);
180 dyn_used = (int) fix_mem_end + 1;
181 dump_things(fixmem[fix_mem_min], fix_mem_end - fix_mem_min + 1);
182 x = x + (int) (fix_mem_end + 1 - fix_mem_min);
183 p = avail;
184 while (p != null) {
185 decr(dyn_used);
186 p = token_link(p);
187 }
188 dump_int(dyn_used);
189 print_ln();
190 print_int(x);
191 tprint(" memory locations dumped; current usage is ");
192 print_int(var_used);
193 print_char('&');
194 print_int(dyn_used);
195 /*tex
196 Dump regions 1 to 4 of |eqtb|, the table of equivalents. The table of
197 equivalents usually contains repeated information, so we dump it in
198 compressed form: The sequence of $n+2$ values $(n,x_1,\ldots,x_n,m)$ in
199 the format file represents $n+m$ consecutive entries of |eqtb|, with |m|
200 extra copies of $x_n$, namely $(x_1,\ldots,x_n,x_n,\ldots,x_n)$.
201 */
202 k = null_cs;
203 do {
204 j = k;
205 while (j < int_base - 1) {
206 if ((equiv(j) == equiv(j + 1)) && (eq_type(j) == eq_type(j + 1)) &&
207 (eq_level(j) == eq_level(j + 1)))
208 goto FOUND1;
209 incr(j);
210 }
211 l = int_base;
212 /*tex |j=int_base-1| */
213 goto DONE1;
214 FOUND1:
215 incr(j);
216 l = j;
217 while (j < int_base - 1) {
218 if ((equiv(j) != equiv(j + 1)) || (eq_type(j) != eq_type(j + 1)) ||
219 (eq_level(j) != eq_level(j + 1)))
220 goto DONE1;
221 incr(j);
222 }
223 DONE1:
224 dump_int(l - k);
225 dump_things(eqtb[k], l - k);
226 k = j + 1;
227 dump_int(k - l);
228 } while (k != int_base);
229 /*tex Dump regions 5 and 6 of |eqtb|. */
230 do {
231 j = k;
232 while (j < eqtb_size) {
233 if (eqtb[j].cint == eqtb[j + 1].cint)
234 goto FOUND2;
235 incr(j);
236 }
237 l = eqtb_size + 1;
238 /*tex |j=eqtb_size| */
239 goto DONE2;
240 FOUND2:
241 incr(j);
242 l = j;
243 while (j < eqtb_size) {
244 if (eqtb[j].cint != eqtb[j + 1].cint)
245 goto DONE2;
246 incr(j);
247 }
248 DONE2:
249 dump_int(l - k);
250 dump_things(eqtb[k], l - k);
251 k = j + 1;
252 dump_int(k - l);
253 } while (k <= eqtb_size);
254 if (hash_high > 0) {
255 /*tex Dump the |hash_extra| part: */
256 dump_things(eqtb[eqtb_size + 1], hash_high);
257 }
258 dump_int(par_loc);
259 dump_int(write_loc);
260 dump_math_codes();
261 dump_text_codes();
262 /*tex
263 Dump the hash table, A different scheme is used to compress the hash
264 table, since its lower region is usually sparse. When |text(p)<>0| for
265 |p<=hash_used|, we output two words, |p| and |hash[p]|. The hash table
266 is, of course, densely packed for |p>=hash_used|, so the remaining
267 entries are output in a~block.
268 */
269 dump_primitives();
270 dump_int(hash_used);
271 cs_count = frozen_control_sequence - 1 - hash_used + hash_high;
272 for (p = hash_base; p <= hash_used; p++) {
273 if (cs_text(p) != 0) {
274 dump_int(p);
275 dump_hh(hash[p]);
276 incr(cs_count);
277 }
278 }
279 dump_things(hash[hash_used + 1],undefined_control_sequence - 1 - hash_used);
280 if (hash_high > 0) {
281 dump_things(hash[eqtb_size + 1], hash_high);
282 }
283 dump_int(cs_count);
284 print_ln();
285 print_int(cs_count);
286 tprint(" multiletter control sequences");
287 /*tex Dump the font information. */
288 dump_int(max_font_id());
289 for (k = 0; k <= max_font_id(); k++) {
290 /*tex Dump the array info for internal font number |k|. */
291 dump_font(k);
292 tprint_nl("\\font");
293 print_esc(font_id_text(k));
294 print_char('=');
295 tprint_file_name((unsigned char *) font_name(k),
296 (unsigned char *) font_area(k), NULL);
297 if (font_size(k) != font_dsize(k)) {
298 tprint(" at ");
299 print_scaled(font_size(k));
300 tprint("pt");
301 }
302 }
303 print_ln();
304 print_int(max_font_id());
305 tprint(" preloaded font");
306 if (max_font_id() != 1)
307 print_char('s');
308 dump_math_data();
309 /*tex Dump the hyphenation tables. */
310 dump_language_data();
311 /*tex Dump a couple more things and the closing check word. */
312 dump_int(interaction);
313 dump_int(format_ident);
314 dump_int(format_name);
315 dump_int(69069);
316 /*tex
317 We have already printed a lot of statistics, so we set |tracing_stats:=0|
318 to prevent them from appearing again.
319 */
320 tracing_stats_par = 0;
321 /*tex Dump the \LUA\ bytecodes. */
322 dump_luac_registers();
323 /*tex Close the format file. */
324 zwclose(fmt_file);
325 }
326
327 /*tex
328
329 Corresponding to the procedure that dumps a format file, we have a function that
330 reads one in. The function returns |false| if the dumped format is incompatible
331 with the present \TeX\ table sizes, etc.
332
333 */
334
335 #define too_small(A) do { \
336 wake_up_terminal(); \
337 wterm_cr(); \
338 fprintf(term_out,"---! Must increase the %s",(A)); \
339 goto BAD_FMT; \
340 } while (0)
341
342 /*tex
343
344 The inverse macros are slightly more complicated, since we need to check the
345 range of the values we are reading in. We say `|undump(a)(b)(x)|' to read an
346 integer value |x| that is supposed to be in the range |a<=x<=b|.
347
348 */
349
350 #define undump(A,B,C) do { \
351 undump_int(x); \
352 if (x<(A) || x>(B)) \
353 goto BAD_FMT; \
354 else \
355 (C) = x; \
356 } while (0)
357
358 #define format_debug(A,B) do { \
359 if (debug_format_file) { \
360 fprintf (stderr, "fmtdebug: %s=%d", (A), (int)(B)); \
361 } \
362 } while (0)
363
364 #define undump_size(A,B,C,D) do { \
365 undump_int(x); \
366 if (x<(A)) \
367 goto BAD_FMT; \
368 if (x>(B)) \
369 too_small(C); \
370 else \
371 format_debug (C,x); \
372 (D) = x; \
373 } while (0)
374
375 boolean load_fmt_file(const char *fmtname)
376 {
377 int j, k, x;
378 halfword p;
379 char *format_engine;
380 /*tex Undump constants for consistency check .*/
381 if (ini_version) {
382 libcfree(hash);
383 libcfree(eqtb);
384 libcfree(fixmem);
385 libcfree(varmem);
386 }
387 undump_int(x);
388 format_debug("format magic number", x);
389 if (x != 0x57325458) {
390 /*tex it's not a format file. */
391 goto BAD_FMT;
392 }
393 undump_int(x);
394 format_debug("format id", x);
395 if (x != FORMAT_ID) {
396 /*tex We have a |FORMAT_ID| mismatch. */
397 goto BAD_FMT;
398 }
399 undump_int(x);
400 format_debug("engine name size", x);
401 if ((x < 0) || (x > 256)) {
402 /*tex The format file is corrupt. */
403 goto BAD_FMT;
404 }
405 format_engine = xmalloc((unsigned) x);
406 undump_things(format_engine[0], x);
407 format_engine[x - 1] = 0;
408 if (strcmp(engine_name, format_engine)) {
409 wake_up_terminal();
410 wterm_cr();
411 fprintf(term_out, "---! %s was written by %s", fmtname, format_engine);
412 xfree(format_engine);
413 goto BAD_FMT;
414 }
415 xfree(format_engine);
416 undump_int(x);
417 format_debug("string pool checksum", x);
418 if (x != 0x57325458) {
419 wake_up_terminal();
420 wterm_cr();
421 fprintf(term_out, "---! %s was written by a different version",
422 fmtname);
423 goto BAD_FMT;
424 }
425 undump_int(x);
426 if (x != max_halfword)
427 goto BAD_FMT;
428 undump_int(hash_high);
429 if ((hash_high < 0) || (hash_high > sup_hash_extra))
430 goto BAD_FMT;
431 if (hash_extra < hash_high)
432 hash_extra = hash_high;
433 eqtb_top = eqtb_size + hash_extra;
434 if (hash_extra == 0)
435 hash_top = undefined_control_sequence;
436 else
437 hash_top = eqtb_top;
438 hash = xmallocarray(two_halves, (unsigned) (1 + hash_top));
439 memset(hash, 0, sizeof(two_halves) * (unsigned) (hash_top + 1));
440 eqtb = xmallocarray(memory_word, (unsigned) (eqtb_top + 1));
441 set_eq_type(undefined_control_sequence, undefined_cs_cmd);
442 set_equiv(undefined_control_sequence, null);
443 set_eq_level(undefined_control_sequence, level_zero);
444 for (x = eqtb_size + 1; x <= eqtb_top; x++)
445 eqtb[x] = eqtb[undefined_control_sequence];
446 undump_int(x);
447 if (x != eqtb_size)
448 goto BAD_FMT;
449 undump_int(x);
450 if (x != hash_prime)
451 goto BAD_FMT;
452 /*tex Undump the string pool */
453 str_ptr = undump_string_pool();
454 /*tex Undump the dynamic memory */
455 undump_node_mem();
456 undump_int(temp_token_head);
457 undump_int(hold_token_head);
458 undump_int(omit_template);
459 undump_int(null_list);
460 undump_int(backup_head);
461 undump_int(garbage);
462 undump_int(fix_mem_min);
463 undump_int(fix_mem_max);
464 fixmem = xmallocarray(smemory_word, fix_mem_max + 1);
465 memset(voidcast(fixmem), 0, (fix_mem_max + 1) * sizeof(smemory_word));
466 undump_int(fix_mem_end);
467 undump_int(avail);
468 undump_things(fixmem[fix_mem_min], fix_mem_end - fix_mem_min + 1);
469 undump_int(dyn_used);
470 /*tex Undump regions 1 to 6 of the table of equivalents |eqtb|. */
471 k = null_cs;
472 do {
473 undump_int(x);
474 if ((x < 1) || (k + x > eqtb_size + 1))
475 goto BAD_FMT;
476 undump_things(eqtb[k], x);
477 k = k + x;
478 undump_int(x);
479 if ((x < 0) || (k + x > eqtb_size + 1))
480 goto BAD_FMT;
481 for (j = k; j <= k + x - 1; j++)
482 eqtb[j] = eqtb[k - 1];
483 k = k + x;
484 } while (k <= eqtb_size);
485 if (hash_high > 0) {
486 /*tex undump |hash_extra| part */
487 undump_things(eqtb[eqtb_size + 1], hash_high);
488 }
489 undump(hash_base, hash_top, par_loc);
490 par_token = cs_token_flag + par_loc;
491 undump(hash_base, hash_top, write_loc);
492 undump_math_codes();
493 undump_text_codes();
494 /*tex Undump the hash table */
495 undump_primitives();
496 undump(hash_base, frozen_control_sequence, hash_used);
497 p = hash_base - 1;
498 do {
499 undump(p + 1, hash_used, p);
500 undump_hh(hash[p]);
501 } while (p != hash_used);
502 undump_things(hash[hash_used + 1], undefined_control_sequence - 1 - hash_used);
503 if (debug_format_file)
504 print_csnames(hash_base, undefined_control_sequence - 1);
505 if (hash_high > 0) {
506 undump_things(hash[eqtb_size + 1], hash_high);
507 if (debug_format_file)
508 print_csnames(eqtb_size + 1, hash_high - (eqtb_size + 1));
509 }
510 undump_int(cs_count);
511 /*tex Undump the font information */
512 undump_int(x);
513 set_max_font_id(x);
514 for (k = 0; k <= max_font_id(); k++) {
515 /*tex Undump the array info for internal font number |k| */
516 undump_font(k);
517 }
518 undump_math_data();
519 /*tex Undump the hyphenation tables */
520 undump_language_data();
521 /*tex Undump a couple more things and the closing check word */
522 undump(batch_mode, error_stop_mode, interaction);
523 if (interactionoption != unspecified_mode)
524 interaction = interactionoption;
525 undump(0, str_ptr, format_ident);
526 undump(0, str_ptr, format_name);
527 undump_int(x);
528 if (x != 69069)
529 goto BAD_FMT;
530 /*tex Undump the lua bytecodes. */
531 undump_luac_registers();
532 prev_depth_par = ignore_depth;
533 return true;
534 BAD_FMT:
535 wake_up_terminal();
536 wterm_cr();
537 fprintf(term_out, "(Fatal format file error; I'm stymied)");
538 return false;
539 }