Skip to content

Commit 91688f3

Browse files
committed
Add format! macro
1 parent a3df2d4 commit 91688f3

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/grub_lib.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ use alloc::ffi::CString;
1414
use core::ffi::CStr;
1515
use core::panic::PanicInfo;
1616

17+
extern "C" {
18+
static grub_xputs: extern "C" fn(stri: *const c_char);
19+
fn grub_abort();
20+
fn grub_malloc(sz: usize) -> *mut u8;
21+
fn grub_free(ptr: *mut u8);
22+
fn grub_register_command_prio (name: *const c_char,
23+
func: extern "C" fn (cmd: *const GrubCommand,
24+
argc: c_int, argv: *const *const c_char) -> u32,
25+
summary: *const c_char,
26+
description: *const c_char,
27+
prio: c_int) -> *mut GrubCommand;
28+
fn grub_strlen (s: *const c_char) -> usize;
29+
fn grub_unregister_command (cmd: *const GrubCommand);
30+
fn grub_refresh ();
31+
fn grub_debug_enabled(cond: *const c_char) -> bool;
32+
fn grub_error (n: u32, fmt: *const c_char, args: ...) -> u32;
33+
}
34+
1735
#[macro_export]
1836
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
1937
macro_rules! print {
@@ -47,22 +65,18 @@ macro_rules! eformat {
4765
}
4866
}
4967

50-
extern "C" {
51-
static grub_xputs: extern "C" fn(stri: *const c_char);
52-
fn grub_abort();
53-
fn grub_malloc(sz: usize) -> *mut u8;
54-
fn grub_free(ptr: *mut u8);
55-
fn grub_register_command_prio (name: *const c_char,
56-
func: extern "C" fn (cmd: *const GrubCommand,
57-
argc: c_int, argv: *const *const c_char) -> u32,
58-
summary: *const c_char,
59-
description: *const c_char,
60-
prio: c_int) -> *mut GrubCommand;
61-
fn grub_strlen (s: *const c_char) -> usize;
62-
fn grub_unregister_command (cmd: *const GrubCommand);
63-
fn grub_refresh ();
64-
fn grub_debug_enabled(cond: *const c_char) -> bool;
65-
fn grub_error (n: u32, fmt: *const c_char, args: ...) -> u32;
68+
#[macro_export]
69+
macro_rules! format {
70+
($($args: expr),*) => {
71+
$crate::grub_lib::format(format_args!($($args)*))
72+
}
73+
}
74+
75+
pub fn format(args: Arguments<'_>) -> String {
76+
let mut w = StrWriter {output: "".to_string()};
77+
let _ = fmt::write(&mut w, args);
78+
79+
return w.output;
6680
}
6781

6882
#[no_mangle]

0 commit comments

Comments
 (0)