Skip to content

Commit 11d0754

Browse files
committed
Port texmf_log_name, log_opened, input_stack, and buffer variables
1 parent 4b50d75 commit 11d0754

File tree

10 files changed

+264
-130
lines changed

10 files changed

+264
-130
lines changed

crates/engine_xetex/cbindgen.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include_guard = "TECTONIC_ENGINE_XETEX_BINDGEN_H"
88
"Diagnostic" = "ttbc_diagnostic_t"
99
"FileFormat" = "ttbc_file_format"
1010
"OutputId" = "rust_output_handle_t"
11+
"InputState" = "input_state_t"
1112

1213
[export]
1314
exclude = ["Option_OutputId", "Option_InputId"]

crates/engine_xetex/src/c_api/engine.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,33 @@ pub struct EngineCtx {
4545
pub(crate) name_in_progress: bool,
4646
pub(crate) stop_at_space: bool,
4747
pub(crate) quoted_filename: bool,
48+
pub(crate) texmf_log_name: StrNumber,
49+
pub(crate) log_opened: bool,
50+
pub(crate) input_stack: Vec<InputState>,
4851

4952
pub(crate) eqtb: Vec<MemoryWord>,
5053
pub(crate) prim: Box<[B32x2; PRIM_SIZE + 1]>,
5154
/// An arena of TeX nodes
5255
pub(crate) mem: Vec<MemoryWord>,
56+
pub(crate) buffer: Vec<char>,
57+
}
58+
59+
#[derive(Clone, Default, PartialEq)]
60+
#[repr(C)]
61+
pub struct InputState {
62+
/// tokenizer state: mid_line, skip_blanks, new_line
63+
state: u16,
64+
/// index of this level of input in input_file array
65+
index: u16,
66+
/// position of beginning of current line in `buffer`
67+
start: i32,
68+
/// position of next character to read in `buffer`
69+
loc: i32,
70+
/// position of end of line in `buffer`
71+
limit: i32,
72+
/// name of current file or magic value for terminal, etc.
73+
name: StrNumber,
74+
synctex_tag: i32,
5375
}
5476

5577
struct NodeError {
@@ -78,10 +100,14 @@ impl EngineCtx {
78100
name_in_progress: false,
79101
stop_at_space: false,
80102
quoted_filename: false,
103+
texmf_log_name: 0,
104+
log_opened: false,
105+
input_stack: Vec::new(),
81106

82107
eqtb: Vec::new(),
83108
prim: Box::new([B32x2 { s0: 0, s1: 0 }; PRIM_SIZE + 1]),
84109
mem: Vec::new(),
110+
buffer: Vec::new(),
85111
}
86112
}
87113

@@ -405,6 +431,46 @@ pub extern "C" fn set_quoted_filename(val: bool) {
405431
ENGINE_CTX.with_borrow_mut(|engine| engine.quoted_filename = val)
406432
}
407433

434+
#[no_mangle]
435+
pub extern "C" fn texmf_log_name() -> StrNumber {
436+
ENGINE_CTX.with_borrow(|engine| engine.texmf_log_name)
437+
}
438+
439+
#[no_mangle]
440+
pub extern "C" fn set_texmf_log_name(val: StrNumber) {
441+
ENGINE_CTX.with_borrow_mut(|engine| engine.texmf_log_name = val)
442+
}
443+
444+
#[no_mangle]
445+
pub extern "C" fn log_opened() -> bool {
446+
ENGINE_CTX.with_borrow(|engine| engine.log_opened)
447+
}
448+
449+
#[no_mangle]
450+
pub extern "C" fn set_log_opened(val: bool) {
451+
ENGINE_CTX.with_borrow_mut(|engine| engine.log_opened = val)
452+
}
453+
454+
#[no_mangle]
455+
pub extern "C" fn resize_input_stack(len: usize) {
456+
ENGINE_CTX.with_borrow_mut(|engine| engine.input_stack.resize(len, InputState::default()))
457+
}
458+
459+
#[no_mangle]
460+
pub extern "C" fn input_stack(idx: usize) -> InputState {
461+
ENGINE_CTX.with_borrow(|engine| engine.input_stack[idx].clone())
462+
}
463+
464+
#[no_mangle]
465+
pub extern "C" fn set_input_stack(idx: usize, state: InputState) {
466+
ENGINE_CTX.with_borrow_mut(|engine| engine.input_stack[idx] = state)
467+
}
468+
469+
#[no_mangle]
470+
pub extern "C" fn clear_input_stack() {
471+
ENGINE_CTX.with_borrow_mut(|engine| engine.input_stack.clear())
472+
}
473+
408474
#[no_mangle]
409475
pub extern "C" fn eqtb(idx: usize) -> MemoryWord {
410476
ENGINE_CTX.with_borrow(|engine| engine.eqtb[idx])
@@ -484,6 +550,33 @@ pub extern "C" fn prim_ptr(idx: usize) -> *mut B32x2 {
484550
ENGINE_CTX.with_borrow_mut(|engine| ptr::from_mut(&mut engine.prim[idx]))
485551
}
486552

553+
#[no_mangle]
554+
pub extern "C" fn resize_buffer(len: usize) {
555+
ENGINE_CTX.with_borrow_mut(|engine| engine.buffer.resize(len, '\0'))
556+
}
557+
558+
#[no_mangle]
559+
pub extern "C" fn buffer_ptr() -> *mut char {
560+
ENGINE_CTX.with_borrow_mut(|engine| engine.buffer.as_mut_ptr())
561+
}
562+
563+
#[no_mangle]
564+
pub extern "C" fn buffer(idx: usize) -> char {
565+
ENGINE_CTX.with_borrow(|engine| engine.buffer[idx])
566+
}
567+
568+
#[no_mangle]
569+
pub extern "C" fn set_buffer(idx: usize, val: u32) {
570+
ENGINE_CTX.with_borrow_mut(|engine| {
571+
engine.buffer[idx] = char::from_u32(val).unwrap_or(char::REPLACEMENT_CHARACTER)
572+
})
573+
}
574+
575+
#[no_mangle]
576+
pub extern "C" fn clear_buffer() {
577+
ENGINE_CTX.with_borrow_mut(|engine| engine.buffer.clear())
578+
}
579+
487580
fn checkpool_pointer(pool: &mut StringPool, pool_ptr: usize, len: usize) {
488581
if pool_ptr + len >= pool.pool_size {
489582
panic!("string pool overflow [{} bytes]", pool.pool_size);

crates/engine_xetex/xetex/xetex-errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pre_error_message (void)
1616
{
1717
/* FKA normalize_selector(): */
1818

19-
if (log_opened)
19+
if (log_opened())
2020
set_selector(SELECTOR_TERM_AND_LOG);
2121
else
2222
set_selector(SELECTOR_TERM_ONLY);
@@ -40,7 +40,7 @@ post_error_message(int need_to_print_it)
4040
if (interaction == ERROR_STOP_MODE)
4141
interaction = SCROLL_MODE;
4242

43-
if (need_to_print_it && log_opened)
43+
if (need_to_print_it && log_opened())
4444
error();
4545

4646
history = HISTORY_FATAL_ERROR;

crates/engine_xetex/xetex/xetex-ini.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
/* All the following variables are declared in xetex-xetexd.h */
2020
bool shell_escape_enabled = false;
2121
int32_t bad;
22-
UnicodeScalar *buffer;
2322
int32_t first;
2423
int32_t last;
2524
int32_t max_buf_stack;
@@ -100,7 +99,6 @@ eight_bits cur_cmd;
10099
int32_t cur_chr;
101100
int32_t cur_cs;
102101
int32_t cur_tok;
103-
input_state_t *input_stack;
104102
int32_t input_ptr;
105103
int32_t max_in_stack;
106104
input_state_t cur_input;
@@ -137,9 +135,7 @@ int32_t if_line;
137135
int32_t skip_line;
138136
int32_t format_default_length;
139137
char *TEX_format_default;
140-
bool log_opened;
141138
const char* output_file_extension;
142-
str_number texmf_log_name;
143139
memory_word *font_info;
144140
font_index fmem_ptr;
145141
internal_font_number font_ptr;
@@ -573,7 +569,7 @@ primitive(const char* ident, uint16_t c, int32_t o)
573569
overflow("buffer size", buf_size);
574570

575571
for (int i = 0; i < len; i++)
576-
buffer[first + i] = ident[i];
572+
set_buffer(first + i, ident[i]);
577573

578574
cur_val = id_lookup(first, len);
579575
set_str_ptr(str_ptr()-1);
@@ -2061,7 +2057,7 @@ store_fmt_file(void)
20612057

20622058
if (interaction == ERROR_STOP_MODE)
20632059
interaction = SCROLL_MODE;
2064-
if (log_opened)
2060+
if (log_opened())
20652061
error();
20662062

20672063
history = HISTORY_FATAL_ERROR;
@@ -3006,7 +3002,7 @@ init_io(void)
30063002
stdin_ufile.conversionData = 0;
30073003
input_file[0] = &stdin_ufile;
30083004

3009-
buffer[first] = 0;
3005+
set_buffer(first, 0);
30103006
last = first;
30113007
cur_input.loc = first;
30123008
cur_input.limit = last;
@@ -3461,10 +3457,10 @@ tt_cleanup(void) {
34613457
}
34623458

34633459
// Free the big allocated arrays
3464-
free(buffer);
3460+
clear_buffer();
34653461
free(nest);
34663462
free(save_stack);
3467-
free(input_stack);
3463+
clear_input_stack();
34683464
free(input_file);
34693465
clear_line_stack();
34703466
free(eof_seen);
@@ -3569,10 +3565,10 @@ tt_run_engine(const char *dump_name, const char *input_file_name, time_t build_d
35693565

35703566
/* Allocate many of our big arrays. */
35713567

3572-
buffer = xmalloc_array(UnicodeScalar, buf_size);
3568+
resize_buffer(buf_size);
35733569
nest = xmalloc_array(list_state_record, nest_size);
35743570
save_stack = xmalloc_array(memory_word, save_size);
3575-
input_stack = xmalloc_array(input_state_t, stack_size);
3571+
resize_input_stack(stack_size);
35763572
input_file = xmalloc_array(UFILE *, max_in_open);
35773573
eof_seen = xmalloc_array(bool, max_in_open);
35783574
grp_stack = xmalloc_array(save_pointer, max_in_open);
@@ -3672,7 +3668,7 @@ tt_run_engine(const char *dump_name, const char *input_file_name, time_t build_d
36723668
set_file_offset(0);
36733669
set_job_name(0);
36743670
set_name_in_progress(false);
3675-
log_opened = false;
3671+
set_log_opened(false);
36763672

36773673
if (semantic_pagination_enabled)
36783674
output_file_extension = ".spx";
@@ -3693,7 +3689,7 @@ tt_run_engine(const char *dump_name, const char *input_file_name, time_t build_d
36933689
used_tectonic_coda_tokens = false;
36943690
gave_char_warning_help = false;
36953691

3696-
memset(buffer, 0, buf_size * sizeof(buffer[0]));
3692+
memset(buffer_ptr(), 0, buf_size * sizeof(buffer(0)));
36973693
first = 0;
36983694

36993695
scanner_status = NORMAL;
@@ -3725,7 +3721,7 @@ tt_run_engine(const char *dump_name, const char *input_file_name, time_t build_d
37253721
if (INTPAR(end_line_char) < 0 || INTPAR(end_line_char) > BIGGEST_CHAR)
37263722
cur_input.limit--;
37273723
else
3728-
buffer[cur_input.limit] = INTPAR(end_line_char);
3724+
set_buffer(cur_input.limit, INTPAR(end_line_char));
37293725

37303726
if (in_initex_mode) {
37313727
/* TeX initializes with the real date and time, but for format file

crates/engine_xetex/xetex/xetex-io.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ apply_normalization(uint32_t* buf, int len, int norm)
209209
}
210210

211211
status = TECkit_ConvertBuffer(*normPtr, (Byte*)buf, len * sizeof(UInt32), &inUsed,
212-
(Byte*)&buffer[first], sizeof(*buffer) * (buf_size - first), &outUsed, 1);
212+
(Byte*)&buffer_ptr()[first], sizeof(buffer(0)) * (buf_size - first), &outUsed, 1);
213213
TECkit_ResetConverter(*normPtr);
214214
if (status != kStatus_NoError)
215215
buffer_overflow();
216-
last = first + outUsed / sizeof(*buffer);
216+
last = first + outUsed / sizeof(buffer(0));
217217
}
218218

219219

@@ -279,13 +279,13 @@ input_line(UFILE* f)
279279

280280
default: // none
281281
outLen = ucnv_toAlgorithmic(UCNV_UTF32_NativeEndian, cnv,
282-
(char*)&buffer[first], sizeof(*buffer) * (buf_size - first),
282+
(char*)&buffer_ptr()[first], sizeof(buffer(0)) * (buf_size - first),
283283
byteBuffer, bytesRead, &errorCode);
284284
if (errorCode != 0) {
285285
conversion_error((int)errorCode);
286286
return false;
287287
}
288-
outLen /= sizeof(*buffer);
288+
outLen /= sizeof(buffer(0));
289289
last = first + outLen;
290290
break;
291291
}
@@ -322,10 +322,10 @@ input_line(UFILE* f)
322322

323323
default: // none
324324
if (last < buf_size && i != EOF && i != '\n' && i != '\r')
325-
buffer[last++] = i;
325+
set_buffer(last++, i);
326326
if (i != EOF && i != '\n' && i != '\r')
327327
while (last < buf_size && (i = get_uni_c(f)) != EOF && i != '\n' && i != '\r')
328-
buffer[last++] = i;
328+
set_buffer(last++, i);
329329

330330
if (i == EOF && errno != EINTR && last == first)
331331
return false;
@@ -341,13 +341,13 @@ input_line(UFILE* f)
341341
if (i == '\r')
342342
f->skipNextLF = 1;
343343

344-
buffer[last] = ' ';
344+
set_buffer(last, ' ');
345345
if (last >= max_buf_stack)
346346
max_buf_stack = last;
347347

348348
/* Trim trailing space or EOL characters. */
349349
#define IS_SPC_OR_EOL(c) ((c) == ' ' || (c) == '\r' || (c) == '\n')
350-
while (last > first && IS_SPC_OR_EOL(buffer[last - 1]))
350+
while (last > first && IS_SPC_OR_EOL(buffer(last - 1)))
351351
--last;
352352

353353
return true;

crates/engine_xetex/xetex/xetex-shipout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ out_what(int32_t p)
17651765

17661766
write_open[j] = true;
17671767

1768-
if (log_opened) {
1768+
if (log_opened()) {
17691769
old_setting = selector();
17701770
if (INTPAR(tracing_online) <= 0)
17711771
set_selector(SELECTOR_LOG_ONLY);
@@ -2174,7 +2174,7 @@ write_out(int32_t p)
21742174
else
21752175
set_selector(SELECTOR_TERM_AND_LOG);
21762176

2177-
if (!log_opened)
2177+
if (!log_opened())
21782178
set_selector(SELECTOR_TERM_ONLY);
21792179

21802180
// Tectonic: don't emit warnings for shell-escape invocations when

crates/engine_xetex/xetex/xetex-stringpool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ str_eq_buf(str_number s, int32_t k)
7070
j = str_start(s - 65536L);
7171

7272
while (j < str_start(s + 1 - 65536L)) {
73-
if (buffer[k] >= 65536L) {
74-
if (str_pool(j) != 55296L + (buffer[k] - 65536L) / 1024) {
73+
if (buffer(k) >= 65536L) {
74+
if (str_pool(j) != 55296L + (buffer(k) - 65536L) / 1024) {
7575
return false;
76-
} else if (str_pool(j + 1) != 56320L + (buffer[k] - 65536L) % 1024) {
76+
} else if (str_pool(j + 1) != 56320L + (buffer(k) - 65536L) % 1024) {
7777
return false;
7878
} else {
7979
j++;
8080
}
81-
} else if (str_pool(j) != buffer[k]) {
81+
} else if (str_pool(j) != buffer(k)) {
8282
return false;
8383
}
8484

0 commit comments

Comments
 (0)