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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! The LaTeX layer texrs can honour.
//!
//! LaTeX is a program written in TeX, so this carries it as TeX. There are two
//! files of it, and the difference between them is the whole shape of this
//! layer:
//!
//! * `prelude.tex` is the STAND-IN. A macro that would have drawn something
//! yields its text instead; a macro whose effect the stomach would have read
//! consumes its arguments and produces nothing. Nothing in it is LaTeX's own
//! definition, and it is not meant to be.
//! * `kernel.tex` is the PORT. Every definition in it is the one `latex.ltx`
//! makes, with the place it came from written above it, and every place a
//! definition could NOT be carried over says which primitive is missing.
//! Counters, cross references, `\newenvironment`, the option and file
//! machinery and the footnote and caption numbering are there.
//!
//! The kernel is read after the stand-in, so a name in both ends up with the
//! ported definition and the stand-in stays for the names the port has not
//! reached.
//!
//! Four things are decided in Rust because the engine has no primitive for
//! them, each in its own module:
//!
//! * [`load`] finds the real `.sty` and `.cls` with `kpsewhich` and reads them
//! through the engine, and REPORTS by name every package that would not go
//! through and the control sequence that stopped it.
//! * [`counters`] allocates a `\count` register per counter the document
//! declares. It predates the frontend following register values
//! (`crate::known`), when `\newcount`'s allocator could not freeze a number
//! into a name; the stand-in kernel still relies on it, and the real
//! `latex.ltx` (run with `-ini`) allocates for itself.
//! * [`allocate`] does the same for `\newcount` itself and its eleven
//! relatives, for the same reason: the allocator's whole job is to freeze
//! the next free register number into a name.
//! * [`aux`] is the `.aux` round trip `\ref` rests on, because there is no
//! `\write`, no `\openin` and no `\read`.
/// The prelude and the kernel, compiled into the binary so a run needs no
/// support files.
///
/// One constant rather than two because every caller wants both and in this
/// order: the ported definitions have to win over the stand-ins they replace.
pub const PRELUDE: &str = concat!;
/// Which of the two things a run is producing.
///
/// It matters to exactly one definition, `\@ctrtotext` -- `kernel.tex` says why
/// at length -- and the two entry points below differ only in that.
/// Everything a LaTeX document needs read before itself.
///
/// The prelude and the kernel, then the register allocations for the counters
/// this document declares, then the packages that really load. The order is the
/// one the pieces depend on: `\@namedef` is the kernel's, and a counter's
/// register has to exist before `\newcounter` names it.
/// The same, for a run that is setting the document's own words.
/// The environment a document is read in, minus the document and its packages.
///
/// Split out because `load::attempt` has to build the same thing before it can
/// say whether a `.sty` really loads: a probe running with LESS than the real
/// preamble would report a package as refused over a macro the real run would
/// have given it, and the report is the whole point of the probe. `about` is
/// the text about to be read, scanned for the declarations Rust has to make on
/// its behalf -- the counter registers and the `\newif` switches.
/// The preamble, with the labels of `previous` -- the `.aux` a run left last
/// time -- seeded after the unresolved ones when there is one.
///
/// The scans of the document are independent of each other: the package load
/// and the `support` built over what it loaded, the class's numbering, and the
/// two label seedings each read `src` (and the `.aux`) and write nothing
/// another reads. So the three small ones run on threads of their own while
/// the package load runs on the CALLING thread -- which reports on stderr,
/// exactly as before, and whose probes of the packages run side by side (see
/// `load::preamble`) -- and the pieces are joined in the fixed order below.
/// A scoped thread's answer, its panic carried on to the caller as its own.
/// The preamble with the labels a previous run left in `document.aux` seeded in.
///
/// Separate because it needs to know which FILE the document is, and the entry
/// points in `lib.rs` that take a string and nothing else cannot say.
/// The part of amsmath this engine carries itself: `\numberwithin`, `\eqref`
/// and the `\tagform@` it sets the number with. The displays amsmath numbers
/// -- `align`, `gather`, `\tag` -- are read by `src/math.rs`.
///
/// amsmath.sty does not load here (`texrs: package amsmath needs ...`), and
/// none of the three is LaTeX's: `\numberwithin` is amsmath.sty's
/// `\DeclareCommandCopy\numberwithin\counterwithin`, so a document that does
/// not load amsmath has no `\numberwithin` under pdflatex either. The kernel
/// defines `\counterwithin` (ltcounts.dtx, 2018) and the other two under names
/// of its own, and this puts them in place.
const AMSMATH: &str = "\
\\catcode`\\@=11\n\
\\let\\numberwithin\\counterwithin\n\
\\let\\eqref\\@texrs@eqref\n\
\\let\\tagform@\\@texrs@tagform\n\
\\catcode`\\@=12\n";
/// Whether `src` loads amsmath -- itself, or through a class or package that
/// requires it, as mathtools and amsart do -- and amsmath.sty did not load.
/// The kernel definitions a CLASS overwrites with box machinery, taken back.
///
/// The kernel is read before the class and a standard class defines some of the
/// same names -- `\thebibliography` is article.cls:717, and what it defines is a
/// `\list`: a box, with the `\usecounter` and the `\renewcommand\theenumiv` the
/// entries are numbered by INSIDE it. There is no `\list` here, so the class's
/// definition stopped the document at `! Undefined control sequence \list.` the
/// moment the class started loading all the way through.
///
/// `numbering` above is the same move for the same reason -- a definition the
/// engine owns, re-stated after the file that would clobber it -- and the `\let`
/// rather than a second copy of the body keeps `kernel.tex` the only place the
/// definition is written.
///
/// Deliberately short. Every name here is one a class may no longer define for
/// itself, so it holds only what the engine cannot run at all: the floats and
/// the list environments, which are `\@float`, `\@dblfloat` and `\list` -- a
/// box each, and none of the three exists here.
///
/// The `\relax` ones say exactly what `\begin` said about them before a class
/// could reach its own definition: `\def\begin#1{\csname #1\endcsname}` makes
/// an undefined name `\relax`, so the body between the two still sets and the
/// environment itself does nothing. `\figure` and `\table` are `\newcommand`
/// rather than `\let` because both take the optional placement argument --
/// `\begin{figure}[htbp]` -- and `\let` copies a meaning without the optional
/// argument that `\newcommand` recorded beside it. Each float still says which
/// counter its `\caption` refsteps -- `\@captype`, which latex.ltx's `\@float`
/// defines -- or every table caption was numbered as a figure.
const RECLAIMED: &str = "\
\\catcode`\\@=11\n\
\\let\\thebibliography\\@texrs@thebibliography\n\
\\def\\endthebibliography{}\n\
\\let\\fnum@figure\\@texrs@fnum@figure\n\
\\let\\fnum@table\\@texrs@fnum@table\n\
\\newcommand{\\figure}[1][]{\\def\\@captype{figure}}\n\
\\newcommand{\\endfigure}{}\n\
\\newcommand{\\table}[1][]{\\def\\@captype{table}}\n\
\\newcommand{\\endtable}{}\n\
\\expandafter\\def\\csname figure*\\endcsname{\\def\\@captype{figure}}\n\
\\expandafter\\let\\csname endfigure*\\endcsname\\relax\n\
\\expandafter\\def\\csname table*\\endcsname{\\def\\@captype{table}}\n\
\\expandafter\\let\\csname endtable*\\endcsname\\relax\n\
\\let\\description\\relax \\let\\enddescription\\relax\n\
\\let\\abstract\\relax \\let\\endabstract\\relax\n\
\\let\\verse\\relax \\let\\endverse\\relax\n\
\\let\\quotation\\relax \\let\\endquotation\\relax\n\
\\let\\quote\\relax \\let\\endquote\\relax\n\
\\let\\titlepage\\relax \\let\\endtitlepage\\relax\n\
\\let\\theindex\\relax \\let\\endtheindex\\relax\n\
\\catcode`\\@=12\n";
/// The `\the<counter>` definitions that belong to a class with no chapters.
///
/// `kernel.tex` writes report's: `\thesection` is `\thechapter.\arabic{section}`
/// and a figure is numbered `2.4`. article numbers a section `2` and a figure
/// `4`, and the difference is visible the moment a document says `\ref` -- an
/// article's first section came back as `0.1`.
///
/// Which one a document gets is decided by the CLASS FILE rather than by a list
/// of class names -- [`class_declares_chapters`] is the question, and it says
/// how it is asked. A class that is neither one of LaTeX's own nor findable by
/// `kpsewhich` is read as report's, which is what this layer carried before
/// there was a choice to make.
/// Whether the class `src` names declares a `chapter` counter.
///
/// `report.cls` and `book.cls` say `\newcounter {chapter}` and `article.cls`
/// does not, and neither do the classes built on article that a name-matching
/// rule would have to be told about one at a time -- so the CLASS FILE is what
/// is read, through the same scan `counters.rs` uses. A class file that cannot
/// be read falls back to `base_class_declares_chapters`; `None` is a class
/// outside that list which `kpsewhich` cannot find either, or a source with no
/// `\documentclass` at all.
/// Whether one of the classes LaTeX itself ships declares a `chapter` counter,
/// answered from the name because there is no file to read.
///
/// A machine with no TeX installation has no `kpsewhich` and so no class file
/// at all, and reading every unfindable class as report's numbered an
/// `article`'s first section `0.1` -- the reference resolved to a chapter the
/// document has not got. These five names are LaTeX's own classes and their
/// answer is a fact about LaTeX rather than about the installation, so it can
/// be stated here; anything else is still `None`, because a class built on
/// article is exactly what a name-matching rule cannot be told about.
thread_local!
/// Run `f`, holding back every line [`report`] would have printed meanwhile.
///
/// Two runs that overlap (`aux::update_then`) print in an order of their own
/// choosing; holding both and releasing them in the order the runs would have
/// taken one after the other keeps stderr what it was.
pub
/// Print lines [`held_reports`] held back, as [`report`] would have.
pub
/// Say, once each, what would not load.
///
/// On stderr and by name, because the failure this replaces is the silent one:
/// a package that did not load, a document that ran anyway, and output that was
/// wrong with nothing to say why. Once per process, since a corpus sweep asks
/// the same question of the same package in every document.
/// Whether a source looks like a LaTeX document.
///
/// Keyed on the preamble directives rather than on a flag, because a user does
/// not think of their file as needing a mode -- `\documentclass` IS the
/// statement that this is LaTeX. A plain TeX document contains none of these
/// and is unaffected, which matters: the prelude redefines names like
/// `\section` that a plain document may have defined for itself.