Replace fprintf with Rust standard write

This commit is contained in:
David Tolnay 2022-07-06 16:09:07 -07:00
parent d0342cd486
commit 5d8cb6a469
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
2 changed files with 34 additions and 55 deletions

View file

@ -2,7 +2,7 @@
#![allow(non_camel_case_types, non_snake_case, unused_assignments, unused_mut)]
use std::env;
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::io::{self, Write as _};
use std::mem::MaybeUninit;
use std::process::{self, ExitCode};
@ -22,10 +22,8 @@ use unsafe_libyaml::externs::{__assert_fail, memcpy, strlen, strncmp};
use unsafe_libyaml::*;
extern "C" {
pub type FILE;
static mut stderr: *mut FILE;
fn fclose(__stream: *mut FILE) -> libc::c_int;
fn fopen(_: *const libc::c_char, _: *const libc::c_char) -> *mut FILE;
fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;
fn fgets(__s: *mut libc::c_char, __n: libc::c_int, __stream: *mut FILE) -> *mut libc::c_char;
fn strchr(_: *const libc::c_char, _: libc::c_int) -> *mut libc::c_char;
}
@ -63,10 +61,7 @@ unsafe fn unsafe_main() -> ExitCode {
);
}
if yaml_emitter_initialize(emitter) == 0 {
fprintf(
stderr,
b"Could not initalize the emitter object\n\0" as *const u8 as *const libc::c_char,
);
let _ = writeln!(io::stderr(), "Could not initalize the emitter object");
return ExitCode::FAILURE;
}
unsafe extern "C" fn write_to_stdout(
@ -230,10 +225,10 @@ unsafe fn unsafe_main() -> ExitCode {
) as *mut yaml_char_t,
);
} else {
fprintf(
stderr,
b"Unknown event: '%s'\n\0" as *const u8 as *const libc::c_char,
line.as_mut_ptr(),
let _ = writeln!(
io::stderr(),
"Unknown event: '{}'",
CStr::from_ptr(line.as_mut_ptr()).to_string_lossy(),
);
return ExitCode::FAILURE;
}
@ -248,10 +243,9 @@ unsafe fn unsafe_main() -> ExitCode {
}
match current_block {
13850764817919632987 => {
fprintf(
stderr,
b"Memory error: Not enough memory for creating an event\n\0" as *const u8
as *const libc::c_char,
let _ = writeln!(
io::stderr(),
"Memory error: Not enough memory for creating an event",
);
yaml_emitter_delete(emitter);
return ExitCode::FAILURE;
@ -259,31 +253,24 @@ unsafe fn unsafe_main() -> ExitCode {
6684355725484023210 => {
match (*emitter).error as libc::c_uint {
1 => {
fprintf(
stderr,
b"Memory error: Not enough memory for emitting\n\0" as *const u8
as *const libc::c_char,
);
let _ = writeln!(io::stderr(), "Memory error: Not enough memory for emitting");
}
6 => {
fprintf(
stderr,
b"Writer error: %s\n\0" as *const u8 as *const libc::c_char,
(*emitter).problem,
let _ = writeln!(
io::stderr(),
"Writer error: {}",
CStr::from_ptr((*emitter).problem).to_string_lossy(),
);
}
7 => {
fprintf(
stderr,
b"Emitter error: %s\n\0" as *const u8 as *const libc::c_char,
(*emitter).problem,
let _ = writeln!(
io::stderr(),
"Emitter error: {}",
CStr::from_ptr((*emitter).problem).to_string_lossy(),
);
}
_ => {
fprintf(
stderr,
b"Internal error\n\0" as *const u8 as *const libc::c_char,
);
let _ = writeln!(io::stderr(), "Internal error");
}
}
yaml_emitter_delete(emitter);
@ -318,10 +305,10 @@ pub unsafe extern "C" fn get_line(
}
newline = strchr(line, '\n' as i32);
if newline.is_null() {
fprintf(
stderr,
b"Line too long: '%s'\0" as *const u8 as *const libc::c_char,
line,
let _ = writeln!(
io::stderr(),
"Line too long: '{}'",
CStr::from_ptr(line).to_string_lossy(),
);
process::abort();
}

View file

@ -1,9 +1,10 @@
#![feature(extern_types)]
#![allow(non_camel_case_types, non_snake_case, unused_assignments, unused_mut)]
use std::cmp;
use std::env;
use std::ffi::CStr;
use std::fs;
use std::io::{self, Write as _};
use std::mem::MaybeUninit;
use std::process::{self, ExitCode};
use std::ptr;
@ -14,9 +15,6 @@ use unsafe_libyaml::externs::__assert_fail;
use unsafe_libyaml::parser::yaml_parser_parse;
use unsafe_libyaml::*;
extern "C" {
pub type FILE;
static mut stderr: *mut FILE;
fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn unsafe_main() -> ExitCode {
@ -44,10 +42,7 @@ unsafe fn unsafe_main() -> ExitCode {
)
});
if yaml_parser_initialize(parser) == 0 {
fprintf(
stderr,
b"Could not initialize the parser object\n\0" as *const u8 as *const libc::c_char,
);
let _ = writeln!(io::stderr(), "Could not initialize the parser object");
return ExitCode::FAILURE;
}
unsafe extern "C" fn read_from_file(
@ -72,15 +67,15 @@ unsafe fn unsafe_main() -> ExitCode {
loop {
let mut type_0: yaml_event_type_t = YAML_NO_EVENT;
if yaml_parser_parse(parser, event) == 0 {
fprintf(
stderr,
b"Parse error: %s\n\0" as *const u8 as *const libc::c_char,
(*parser).problem,
let _ = writeln!(
io::stderr(),
"Parse error: {}",
CStr::from_ptr((*parser).problem).to_string_lossy(),
);
if (*parser).problem_mark.line != 0 || (*parser).problem_mark.column != 0 {
fprintf(
stderr,
b"Line: %lu Column: %lu\n\0" as *const u8 as *const libc::c_char,
let _ = writeln!(
io::stderr(),
"Line: {} Column: {}",
((*parser).problem_mark.line).wrapping_add(1 as libc::c_int as libc::c_ulong),
((*parser).problem_mark.column).wrapping_add(1 as libc::c_int as libc::c_ulong),
);
@ -225,10 +220,7 @@ pub unsafe extern "C" fn print_escaped(mut str: *mut yaml_char_t, mut length: si
}
}
unsafe fn usage(ret: ExitCode) -> ExitCode {
fprintf(
stderr,
b"Usage: libyaml-parser [<input-file>]\n\0" as *const u8 as *const libc::c_char,
);
let _ = writeln!(io::stderr(), "Usage: libyaml-parser [<input-file>]");
return ret;
}
fn main() -> ExitCode {