Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bibtex cleanups / type improvements #1163

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 93 additions & 50 deletions crates/bridge_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
};
use tectonic_status_base::{tt_error, tt_warning, MessageKind, StatusBackend};

/// The ID of an InputHandle, used for Rust core state
#[derive(Copy, Clone, PartialEq)]
pub struct InputId(*mut InputHandle);

Check warning on line 59 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L58-L59

Added lines #L58 - L59 were not covered by tests

/// The ID of an OutputHandle, used for Rust core state
#[derive(Copy, Clone, PartialEq)]
pub struct OutputId(*mut OutputHandle);

Check warning on line 63 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L62-L63

Added lines #L62 - L63 were not covered by tests

/// Possible failures for "system request" calls to the driver.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SystemRequestError {
Expand Down Expand Up @@ -479,16 +487,29 @@
error_occurred
}

fn output_open(&mut self, name: &str, is_gz: bool) -> *mut OutputHandle {
fn output_to_id(&self, output: *mut OutputHandle) -> OutputId {
OutputId(output)
}

/// Get a mutable reference to an [`OutputHandle`] associated with an [`OutputId`]
pub fn get_output(&mut self, id: OutputId) -> &mut OutputHandle {
self.output_handles
.iter_mut()
.find(|o| ptr::addr_eq(&***o, id.0))
.unwrap()
}

/// Open a new output, provided the output name and whether it is gzipped.
pub fn output_open(&mut self, name: &str, is_gz: bool) -> Option<OutputId> {
let io = self.hooks.io();
let name = normalize_tex_path(name);

let mut oh = match io.output_open_name(&name) {
OpenResult::Ok(oh) => oh,
OpenResult::NotAvailable => return ptr::null_mut(),
OpenResult::NotAvailable => return None,

Check warning on line 509 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L509

Added line #L509 was not covered by tests
OpenResult::Err(e) => {
tt_warning!(self.status, "open of output {} failed", name; e);
return ptr::null_mut();
return None;

Check warning on line 512 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L512

Added line #L512 was not covered by tests
}
};

Expand All @@ -501,23 +522,24 @@
}

self.output_handles.push(Box::new(oh));
&mut **self.output_handles.last_mut().unwrap()
Some(OutputId(&mut **self.output_handles.last_mut().unwrap()))
}

fn output_open_stdout(&mut self) -> *mut OutputHandle {
/// Open a new stdout output.
pub fn output_open_stdout(&mut self) -> Option<OutputId> {
let io = self.hooks.io();

let oh = match io.output_open_stdout() {
OpenResult::Ok(oh) => oh,
OpenResult::NotAvailable => return ptr::null_mut(),
OpenResult::NotAvailable => return None,

Check warning on line 534 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L534

Added line #L534 was not covered by tests
OpenResult::Err(e) => {
tt_warning!(self.status, "open of stdout failed"; e);
return ptr::null_mut();
return None;

Check warning on line 537 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L537

Added line #L537 was not covered by tests
}
};

self.output_handles.push(Box::new(oh));
&mut **self.output_handles.last_mut().unwrap()
Some(OutputId(&mut **self.output_handles.last_mut().unwrap()))
}

fn output_write(&mut self, handle: *mut OutputHandle, buf: &[u8]) -> bool {
Expand Down Expand Up @@ -546,45 +568,57 @@
}
}

fn output_close(&mut self, handle: *mut OutputHandle) -> bool {
let len = self.output_handles.len();
/// Close the provided output, flushing it and performing any necessary handling.
pub fn output_close(&mut self, id: OutputId) -> bool {
let mut rv = false;

for i in 0..len {
let p: *const OutputHandle = &*self.output_handles[i];

if p == handle {
let mut oh = self.output_handles.swap_remove(i);
if let Err(e) = oh.flush() {
tt_warning!(self.status, "error when closing output {}", oh.name(); e.into());
rv = true;
}
let (name, digest) = oh.into_name_digest();
self.hooks.event_output_closed(name, digest, self.status);
break;
let pos = self
.output_handles
.iter()
.position(|o| ptr::addr_eq(&**o, id.0));
if let Some(pos) = pos {
let mut oh = self.output_handles.swap_remove(pos);
if let Err(e) = oh.flush() {
tt_warning!(self.status, "error when closing output {}", oh.name(); e.into());
rv = true;

Check warning on line 583 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L582-L583

Added lines #L582 - L583 were not covered by tests
}
let (name, digest) = oh.into_name_digest();
self.hooks.event_output_closed(name, digest, self.status);
}

rv
}

fn input_open(&mut self, name: &str, format: FileFormat, is_gz: bool) -> *mut InputHandle {
fn input_to_id(&self, input: *mut InputHandle) -> InputId {
InputId(input)
}

/// Get a mutable reference to an [`InputHandle`] associated with an [`InputId`]
pub fn get_input(&mut self, input: InputId) -> &mut InputHandle {
self.input_handles
.iter_mut()
.find(|i| ptr::addr_eq(&***i, input.0))
.unwrap()
}

/// Open a new input, provided the input name, the file format, and whether it is gzipped.
pub fn input_open(&mut self, name: &str, format: FileFormat, is_gz: bool) -> Option<InputId> {
let name = normalize_tex_path(name);

let (ih, path) = match self.input_open_name_format_gz(&name, format, is_gz) {
OpenResult::Ok(tup) => tup,
OpenResult::NotAvailable => {
return ptr::null_mut();
return None;
}
OpenResult::Err(e) => {
tt_warning!(self.status, "open of input {} failed", name; e);
return ptr::null_mut();
return None;

Check warning on line 615 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L615

Added line #L615 was not covered by tests
}
};

self.input_handles.push(Box::new(ih));
self.latest_input_path = path;
&mut **self.input_handles.last_mut().unwrap()
Some(InputId(&mut **self.input_handles.last_mut().unwrap()))
}

fn input_open_primary(&mut self) -> *mut InputHandle {
Expand Down Expand Up @@ -660,32 +694,31 @@
rhandle.ungetc(byte)
}

fn input_close(&mut self, handle: *mut InputHandle) -> bool {
let len = self.input_handles.len();

for i in 0..len {
let p: *const InputHandle = &*self.input_handles[i];

if p == handle {
let mut ih = self.input_handles.swap_remove(i);
let mut rv = false;

if let Err(e) = ih.scan_remainder() {
tt_warning!(self.status, "error closing out input {}", ih.name(); e);
rv = true;
}

let (name, digest_opt) = ih.into_name_digest();
self.hooks.event_input_closed(name, digest_opt, self.status);
return rv;
/// Close the provided output, performing any necessary handling.
pub fn input_close(&mut self, id: InputId) -> bool {
let pos = self
.input_handles
.iter()
.position(|i| ptr::addr_eq(&**i, id.0));
if let Some(pos) = pos {
let mut ih = self.input_handles.swap_remove(pos);
let mut rv = false;

if let Err(e) = ih.scan_remainder() {
tt_warning!(self.status, "error closing out input {}", ih.name(); e);
rv = true;

Check warning on line 709 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L708-L709

Added lines #L708 - L709 were not covered by tests
}

let (name, digest_opt) = ih.into_name_digest();
self.hooks.event_input_closed(name, digest_opt, self.status);
return rv;
}

// TODO: Handle the error better. This indicates a bug in the engine.
tt_error!(
self.status,
"serious internal bug: unexpected handle in input close: {:?}",
handle
id.0
);

true
Expand Down Expand Up @@ -910,13 +943,19 @@
let rname = CStr::from_ptr(name).to_string_lossy();
let ris_gz = is_gz != 0;

es.output_open(&rname, ris_gz)
match es.output_open(&rname, ris_gz) {
Some(id) => es.get_output(id),
None => ptr::null_mut(),

Check warning on line 948 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L948

Added line #L948 was not covered by tests
}
}

/// Open the general user output stream as a Tectonic output file.
#[no_mangle]
pub extern "C" fn ttbc_output_open_stdout(es: &mut CoreBridgeState) -> *mut OutputHandle {
es.output_open_stdout()
match es.output_open_stdout() {
Some(id) => es.get_output(id),
None => ptr::null_mut(),

Check warning on line 957 in crates/bridge_core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bridge_core/src/lib.rs#L957

Added line #L957 was not covered by tests
}
}

/// Write a single character to a Tectonic output file.
Expand Down Expand Up @@ -977,7 +1016,7 @@
return 0; // This is/was the behavior of close_file() in C.
}

libc::c_int::from(es.output_close(handle))
libc::c_int::from(es.output_close(es.output_to_id(handle)))
}

/// Open a Tectonic file for input.
Expand All @@ -994,7 +1033,11 @@
) -> *mut InputHandle {
let rname = CStr::from_ptr(name).to_string_lossy();
let ris_gz = is_gz != 0;
es.input_open(&rname, format, ris_gz)
let id = es.input_open(&rname, format, ris_gz);
match id {
Some(id) => es.get_input(id),
None => ptr::null_mut(),
}
}

/// Open the "primary input" file.
Expand Down Expand Up @@ -1179,7 +1222,7 @@
return 0; // This is/was the behavior of close_file() in C.
}

libc::c_int::from(es.input_close(handle))
libc::c_int::from(es.input_close(es.input_to_id(handle)))
}

/// A buffer for diagnostic messages. Rust code does not need to use this type.
Expand Down
4 changes: 4 additions & 0 deletions crates/engine_bibtex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ tectonic_io_base = { path = "../io_base", version = '0.0.0-dev.0' }
tectonic_bridge_core = { path = "../bridge_core", version = "0.0.0-dev.0" }
tectonic_errors = { path = "../errors", version = "0.0.0-dev.0" }

[dev-dependencies]
tectonic_status_base = { path = "../status_base", version = "0.0.0-dev.0" }

[package.metadata.internal_dep_versions]
tectonic_bridge_core = "thiscommit:2021-01-17:fohCh1sh"
tectonic_errors = "5c9ba661edf5ef669f24f9904f99cca369d999e7"
tectonic_io_base = "0d9169ef44b2652d6d70308a83022bfd60358e71"
tectonic_status_base = "0d9169ef44b2652d6d70308a83022bfd60358e71"