@@ -12,19 +12,19 @@ use crate::{
1212 log_pr_bst_name, print_a_pool_str, print_a_token, print_aux_name, print_bib_name,
1313 print_confusion, print_overflow, write_log_file, write_logs, AuxTy ,
1414 } ,
15- peekable:: { peekable_close , peekable_open, PeekableInput } ,
15+ peekable:: { peekable_open, PeekableInput } ,
1616 pool:: StringPool ,
1717 scan:: Scan ,
1818 AuxNumber , Bibtex , BibtexError , GlobalItems , StrIlk , StrNumber ,
1919} ;
20- use std:: { cell:: RefCell , ffi:: CString , ptr, ptr :: NonNull } ;
20+ use std:: { cell:: RefCell , ffi:: CString , ptr:: NonNull } ;
2121use tectonic_bridge_core:: FileFormat ;
2222
2323const AUX_STACK_SIZE : usize = 20 ;
2424
2525pub ( crate ) struct AuxData {
2626 aux_list : [ StrNumber ; AUX_STACK_SIZE + 1 ] ,
27- aux_file : [ * mut PeekableInput ; AUX_STACK_SIZE + 1 ] ,
27+ aux_file : [ Option < Box < PeekableInput > > ; AUX_STACK_SIZE + 1 ] ,
2828 aux_ln_stack : [ i32 ; AUX_STACK_SIZE + 1 ] ,
2929 aux_ptr : AuxNumber ,
3030}
@@ -33,7 +33,7 @@ impl AuxData {
3333 fn new ( ) -> AuxData {
3434 AuxData {
3535 aux_list : [ 0 ; AUX_STACK_SIZE + 1 ] ,
36- aux_file : [ ptr :: null_mut ( ) ; AUX_STACK_SIZE + 1 ] ,
36+ aux_file : [ ( ) ; AUX_STACK_SIZE + 1 ] . map ( |_| None ) ,
3737 aux_ln_stack : [ 0 ; AUX_STACK_SIZE + 1 ] ,
3838 aux_ptr : 0 ,
3939 }
@@ -55,12 +55,19 @@ impl AuxData {
5555 self . aux_list [ self . aux_ptr ] = num;
5656 }
5757
58- pub fn file_at_ptr ( & self ) -> * mut PeekableInput {
59- self . aux_file [ self . aux_ptr ]
58+ pub fn file_at_ptr ( & mut self ) -> & mut PeekableInput {
59+ self . aux_file [ self . aux_ptr ] . as_mut ( ) . unwrap ( )
6060 }
6161
62- pub fn set_file_at_ptr ( & mut self , file : * mut PeekableInput ) {
63- self . aux_file [ self . aux_ptr ] = file;
62+ pub fn set_file_at_ptr ( & mut self , file : Box < PeekableInput > ) {
63+ self . aux_file [ self . aux_ptr ] = Some ( file) ;
64+ }
65+
66+ pub fn pop_file ( & mut self ) -> ( Box < PeekableInput > , bool ) {
67+ let out = self . aux_file [ self . aux_ptr ] . take ( ) . unwrap ( ) ;
68+ let last = self . aux_ptr == 0 ;
69+ self . aux_ptr = self . aux_ptr . saturating_sub ( 1 ) ;
70+ ( out, last)
6471 }
6572
6673 pub fn ln_at_ptr ( & self ) -> i32 {
@@ -398,15 +405,19 @@ fn aux_input_command(
398405
399406 let name = pool. get_str ( aux. at_ptr ( ) ) ;
400407 let fname = CString :: new ( name) . unwrap ( ) ;
401- let ptr = peekable_open ( ctx, & fname, FileFormat :: Tex ) ;
402- if ptr. is_null ( ) {
403- write_logs ( "I couldn't open auxiliary file " ) ;
404- print_aux_name ( aux, pool) ?;
405- aux. set_ptr ( aux. ptr ( ) - 1 ) ;
406- aux_err_print ( buffers, aux, pool) ?;
407- return Ok ( ( ) ) ;
408+ let file = PeekableInput :: open ( ctx, & fname, FileFormat :: Tex ) ;
409+ match file {
410+ Err ( _) => {
411+ write_logs ( "I couldn't open auxiliary file " ) ;
412+ print_aux_name ( aux, pool) ?;
413+ aux. set_ptr ( aux. ptr ( ) - 1 ) ;
414+ aux_err_print ( buffers, aux, pool) ?;
415+ return Ok ( ( ) ) ;
416+ }
417+ Ok ( file) => {
418+ aux. set_file_at_ptr ( file) ;
419+ }
408420 }
409- aux. set_file_at_ptr ( ptr) ;
410421
411422 write_logs ( & format ! ( "A level-{} auxiliary file: " , aux. ptr( ) ) ) ;
412423 log_pr_aux_name ( aux, pool) ?;
@@ -474,15 +485,9 @@ pub(crate) fn get_aux_command_and_process(
474485}
475486
476487pub ( crate ) fn pop_the_aux_stack ( ctx : & mut Bibtex < ' _ , ' _ > , aux : & mut AuxData ) -> bool {
477- // SAFETY: Aux file at pointer guaranteed valid at this point
478- unsafe { peekable_close ( ctx, NonNull :: new ( aux. file_at_ptr ( ) ) ) } ;
479- aux. set_file_at_ptr ( ptr:: null_mut ( ) ) ;
480- if aux. ptr ( ) == 0 {
481- true
482- } else {
483- aux. set_ptr ( aux. ptr ( ) - 1 ) ;
484- false
485- }
488+ let ( file, last) = aux. pop_file ( ) ;
489+ file. close ( ctx) . unwrap ( ) ;
490+ last
486491}
487492
488493pub ( crate ) fn last_check_for_aux_errors (
0 commit comments