Skip to content

Commit 08f76ad

Browse files
committed
Port prepare_mag, geq_word_define, int_error
1 parent b672f23 commit 08f76ad

File tree

5 files changed

+105
-65
lines changed

5 files changed

+105
-65
lines changed

crates/engine_xetex/src/c_api/engine.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ use std::{ptr, slice};
66

77
mod memory;
88

9+
use crate::c_api::errors::rs_int_error;
910
use crate::c_api::is_dir_sep;
1011
use crate::c_api::output::{
11-
rs_print, rs_print_bytes, rs_print_char, rs_print_cs, rs_print_esc_bytes, rs_print_int,
12-
rs_print_ln, rs_print_nl, rs_print_nl_bytes, rs_print_raw_char,
12+
rs_capture_to_diagnostic, rs_error_here_with_diagnostic, rs_print, rs_print_bytes,
13+
rs_print_char, rs_print_cs, rs_print_esc_bytes, rs_print_int, rs_print_ln, rs_print_nl,
14+
rs_print_nl_bytes, rs_print_raw_char,
1315
};
1416
use crate::c_api::pool::{
1517
rs_make_string, rs_search_string, rs_slow_make_string, StringPool, EMPTY_STRING, TOO_BIG_CHAR,
1618
};
1719
pub use memory::*;
1820

21+
pub const LEVEL_ZERO: u16 = 0;
22+
pub const LEVEL_ONE: u16 = 1;
23+
1924
pub const NULL_CS: usize = 0x220001;
2025
pub const PRIM_SIZE: usize = 2100;
2126
pub const UNDEFINED_CONTROL_SEQUENCE: usize = 0x226603;
@@ -1726,3 +1731,73 @@ pub fn rs_token_show(globals: &mut Globals<'_, '_>, p: usize) {
17261731
pub extern "C" fn token_show(p: i32) {
17271732
Globals::with(|globals| rs_token_show(globals, p as usize))
17281733
}
1734+
1735+
pub fn rs_geq_word_define(globals: &mut Globals<'_, '_>, p: usize, w: i32) {
1736+
globals.engine.eqtb[p].b32.s1 = w;
1737+
globals.engine.set_xeq_level(p, LEVEL_ONE);
1738+
}
1739+
1740+
#[no_mangle]
1741+
pub extern "C" fn geq_word_define(p: i32, w: i32) {
1742+
Globals::with(|globals| rs_geq_word_define(globals, p as usize, w))
1743+
}
1744+
1745+
pub fn rs_prepare_mag(globals: &mut Globals<'_, '_>) -> Option<Box<dyn Fn()>> {
1746+
if globals.engine.mag_set > 0 && globals.engine.int_par(IntPar::Mag) != globals.engine.mag_set {
1747+
rs_error_here_with_diagnostic(globals, b"Incompatible magnification (");
1748+
rs_print_int(globals, globals.engine.int_par(IntPar::Mag));
1749+
rs_print_bytes(globals, b")");
1750+
rs_print_nl_bytes(globals, b" the previous value will be retained");
1751+
1752+
globals
1753+
.out
1754+
.current_diagnostic
1755+
.as_deref_mut()
1756+
.unwrap()
1757+
.append(format!(" {}", globals.engine.mag_set));
1758+
rs_capture_to_diagnostic(globals, None);
1759+
1760+
globals.engine.help_ptr = 2;
1761+
globals.engine.help_line[1] =
1762+
c"I can handle only one magnification ratio per job. So I've".as_ptr();
1763+
globals.engine.help_line[0] =
1764+
c"reverted to the magnification you used earlier on this run.".as_ptr();
1765+
1766+
if let Some(f) = rs_int_error(globals, globals.engine.mag_set) {
1767+
return Some(f);
1768+
}
1769+
rs_geq_word_define(
1770+
globals,
1771+
INT_BASE + IntPar::Mag as usize,
1772+
globals.engine.mag_set,
1773+
);
1774+
}
1775+
1776+
if globals.engine.int_par(IntPar::Mag) <= 0 || globals.engine.int_par(IntPar::Mag) > 32768 {
1777+
rs_error_here_with_diagnostic(globals, b"Illegal magnification has been changed to 1000");
1778+
globals
1779+
.out
1780+
.current_diagnostic
1781+
.as_deref_mut()
1782+
.unwrap()
1783+
.append(format!(" {}", globals.engine.int_par(IntPar::Mag)));
1784+
rs_capture_to_diagnostic(globals, None);
1785+
1786+
globals.engine.help_ptr = 1;
1787+
globals.engine.help_line[0] =
1788+
c"The magnification ratio must be between 1 and 32768.".as_ptr();
1789+
if let Some(f) = rs_int_error(globals, globals.engine.int_par(IntPar::Mag)) {
1790+
return Some(f);
1791+
}
1792+
rs_geq_word_define(globals, INT_BASE + IntPar::Mag as usize, 1000);
1793+
}
1794+
1795+
globals.engine.mag_set = globals.engine.int_par(IntPar::Mag);
1796+
None
1797+
}
1798+
1799+
#[no_mangle]
1800+
pub extern "C" fn prepare_mag() {
1801+
let outside_lock = Globals::with(|globals| rs_prepare_mag(globals));
1802+
outside_lock.map(|f| f());
1803+
}

crates/engine_xetex/src/c_api/errors.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::c_api::engine::{
44
use crate::c_api::globals::Globals;
55
use crate::c_api::output::{
66
rs_capture_to_diagnostic, rs_error_here_with_diagnostic, rs_print_bytes, rs_print_char,
7-
rs_print_ln, rs_print_nl_bytes,
7+
rs_print_int, rs_print_ln, rs_print_nl_bytes,
88
};
99
use std::ffi::CStr;
1010

@@ -39,7 +39,7 @@ pub fn give_err_help(globals: &mut Globals<'_, '_>) {
3939
rs_token_show(globals, globals.engine.local(Local::ErrHelp) as usize);
4040
}
4141

42-
pub fn rs_error(globals: &mut Globals<'_, '_>) -> Box<dyn Fn()> {
42+
pub fn rs_error(globals: &mut Globals<'_, '_>) -> Option<Box<dyn Fn()>> {
4343
if globals.engine.history < History::ErrorIssued {
4444
globals.engine.history = History::ErrorIssued;
4545
}
@@ -49,10 +49,10 @@ pub fn rs_error(globals: &mut Globals<'_, '_>) -> Box<dyn Fn()> {
4949
if globals.engine.halt_on_error_p != 0 {
5050
globals.engine.history = History::FatalError;
5151
// Execute this outside the globals lock for now
52-
return Box::new(|| {
52+
return Some(Box::new(|| {
5353
post_error_message(0);
5454
panic!("halted on potentially-recoverable error as specified");
55-
});
55+
}));
5656
}
5757

5858
/* This used to be where there was a bunch of code if "interaction ==
@@ -63,10 +63,10 @@ pub fn rs_error(globals: &mut Globals<'_, '_>) -> Box<dyn Fn()> {
6363
if globals.engine.error_count == 100 {
6464
rs_print_nl_bytes(globals, b"(That makes 100 errors; please try again.)");
6565
globals.engine.history = History::FatalError;
66-
return Box::new(|| {
66+
return Some(Box::new(|| {
6767
post_error_message(0);
6868
panic!("halted after 100 potentially-recoverable errors");
69-
});
69+
}));
7070
}
7171

7272
if globals.engine.interaction != InteractionMode::Batch {
@@ -107,13 +107,13 @@ pub fn rs_error(globals: &mut Globals<'_, '_>) -> Box<dyn Fn()> {
107107
}
108108
}
109109
rs_print_ln(globals);
110-
Box::new(|| ())
110+
None
111111
}
112112

113113
#[no_mangle]
114114
extern "C-unwind" fn error() {
115115
let out_of_lock = Globals::with(|globals| rs_error(globals));
116-
out_of_lock();
116+
out_of_lock.map(|f| f());
117117
}
118118

119119
#[no_mangle]
@@ -128,9 +128,9 @@ extern "C" fn post_error_message(need_to_print_it: i32) {
128128
return rs_error(globals);
129129
}
130130
globals.engine.history = History::FatalError;
131-
Box::new(|| ())
131+
None
132132
});
133-
out_of_lock();
133+
out_of_lock.map(|f| f());
134134
unsafe { close_files_and_terminate() };
135135
unsafe { tt_cleanup() };
136136
Globals::with(|globals| {
@@ -162,6 +162,19 @@ pub extern "C" fn fatal_error(s: *const libc::c_char) {
162162
});
163163
}
164164

165+
pub fn rs_int_error(globals: &mut Globals<'_, '_>, n: i32) -> Option<Box<dyn Fn()>> {
166+
rs_print_bytes(globals, b" (");
167+
rs_print_int(globals, n);
168+
rs_print_char(globals, ')' as i32);
169+
rs_error(globals)
170+
}
171+
172+
#[no_mangle]
173+
pub extern "C" fn int_error(n: i32) {
174+
let outside_lock = Globals::with(|globals| rs_int_error(globals, n));
175+
outside_lock.map(|f| f());
176+
}
177+
165178
// TODO: Use the Rust versions directly once they're ported. These just rely indirectly on this
166179
// function, making it easier to port piecemeal.
167180
extern "C" {

crates/engine_xetex/src/c_api/output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ thread_local! {
2222
}
2323

2424
pub struct OutputCtx {
25-
current_diagnostic: Option<Box<Diagnostic>>,
25+
pub(crate) current_diagnostic: Option<Box<Diagnostic>>,
2626
file_line_error_style_p: i32,
2727
term_offset: i32,
2828
file_offset: i32,

crates/engine_xetex/xetex/xetex-xetex0.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@
1212

1313
#define IS_LC_HEX(c) (((c) >= '0' && (c) <= '9' ) || ((c) >= 'a' && (c) <= 'f' ))
1414

15-
static void
16-
int_error(int32_t n)
17-
{
18-
print_cstr(" (");
19-
print_int(n);
20-
print_char(')');
21-
error();
22-
}
23-
24-
2515
int32_t
2616
badness(scaled_t t, scaled_t s)
2717
{
@@ -4351,13 +4341,6 @@ void geq_define(int32_t p, uint16_t t, int32_t e)
43514341
eqtb_ptr(p)->b32.s1 = e;
43524342
}
43534343

4354-
void geq_word_define(int32_t p, int32_t w)
4355-
{
4356-
4357-
eqtb_ptr(p)->b32.s1 = w;
4358-
XEQ_LEVEL(p) = LEVEL_ONE;
4359-
}
4360-
43614344
void save_for_after(int32_t t)
43624345
{
43634346
if (cur_level > LEVEL_ONE) {
@@ -4446,41 +4429,6 @@ void unsave(void)
44464429
confusion("curlevel");
44474430
}
44484431

4449-
void prepare_mag(void)
4450-
{
4451-
4452-
if (mag_set() > 0 && INTPAR(mag) != mag_set()) {
4453-
ttbc_diagnostic_t *errmsg = error_here_with_diagnostic("Incompatible magnification (");
4454-
print_int(INTPAR(mag));
4455-
print_cstr(");");
4456-
print_nl_cstr(" the previous value will be retained");
4457-
4458-
ttstub_diag_printf(errmsg, " (%d)", mag_set());
4459-
capture_to_diagnostic(NULL);
4460-
4461-
{
4462-
set_help_ptr(2);
4463-
set_help_line(1, "I can handle only one magnification ratio per job. So I've");
4464-
set_help_line(0, "reverted to the magnification you used earlier on this run.");
4465-
}
4466-
int_error(mag_set());
4467-
geq_word_define(INT_BASE + INT_PAR__mag, mag_set());
4468-
}
4469-
if ((INTPAR(mag) <= 0) || (INTPAR(mag) > 32768L)) {
4470-
ttbc_diagnostic_t *errmsg = error_here_with_diagnostic("Illegal magnification has been changed to 1000");
4471-
ttstub_diag_printf(errmsg, " (%d)", INTPAR(mag));
4472-
capture_to_diagnostic(NULL);
4473-
4474-
{
4475-
set_help_ptr(1);
4476-
set_help_line(0, "The magnification ratio must be between 1 and 32768.");
4477-
}
4478-
int_error(INTPAR(mag));
4479-
geq_word_define(INT_BASE + INT_PAR__mag, 1000);
4480-
}
4481-
set_mag_set(INTPAR(mag));
4482-
}
4483-
44844432
void print_meaning(void)
44854433
{
44864434
print_cmd_chr(cur_cmd, cur_chr);

crates/engine_xetex/xetex/xetex_bindings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
#define FNT_NUM_0 171
2626

27+
#define LEVEL_ZERO 0
28+
29+
#define LEVEL_ONE 1
30+
2731
#define NULL_CS 2228225
2832

2933
#define PRIM_SIZE 2100

0 commit comments

Comments
 (0)