bash_builtins/
log.rs

1//! Functions to write log messages.
2
3use crate::ffi;
4use std::os::raw::{c_char, c_int};
5
6/// Shows the usage text for this builtin.
7///
8/// This function should be used when an invalid option is found.
9#[inline]
10pub fn show_usage() {
11    unsafe {
12        ffi::builtin_usage();
13    }
14}
15
16/// Shows the help text for this builtin.
17#[inline]
18pub fn show_help() {
19    unsafe {
20        ffi::builtin_help();
21    }
22}
23
24/// Display an error when an argument is missing.
25///
26/// It uses the `sh_needarg` function from Bash.
27pub fn missing_argument() {
28    unsafe {
29        let msg = [ffi::list_opttype as _, ffi::list_optopt as _, 0];
30        ffi::sh_needarg(msg.as_ptr());
31    }
32}
33
34macro_rules! log_fn {
35    ($name:ident, $bash_fn:ident, $doc:literal) => {
36        #[inline]
37        #[doc = $doc]
38        pub fn $name<S: AsRef<[u8]>>(msg: S) {
39            const MSG_FORMAT: *const c_char = b"%.*s\0".as_ptr().cast();
40            let bytes = msg.as_ref();
41            unsafe {
42                $crate::ffi::$bash_fn(MSG_FORMAT, bytes.len() as c_int, bytes.as_ptr());
43            }
44        }
45    };
46}
47
48// Errors.
49
50log_fn!(
51    error,
52    builtin_error,
53    "Shows an error message using the `builtin_error()` function from Bash."
54);
55
56/// Macro to use [`error()`] with a [`format`](std::format) string.
57///
58/// # Example
59///
60/// See the example in the [`warning!()`] documentation.
61#[macro_export]
62macro_rules! error {
63    ($($arg:tt)+) => {
64        $crate::log::error(format!($($arg)+))
65    }
66}
67
68// Warnings.
69
70log_fn!(
71    warning,
72    builtin_warning,
73    "Shows a warning message using the `builtin_warning()` function from Bash."
74);
75
76/// Macro to use [`warning()`] with a [`format`](std::format) string.
77///
78/// # Example
79///
80/// ```
81/// # use bash_builtins::*;
82/// # use std::{fs, io::{self, BufWriter, Write}};
83/// struct FileSize;
84///
85/// impl Builtin for FileSize {
86///     fn call(&mut self, args: &mut Args) -> Result<()> {
87///         let stdout_handle = io::stdout();
88///         let mut output = BufWriter::new(stdout_handle.lock());
89///
90///         let mut result = Ok(());
91///
92///         for path in args.path_arguments() {
93///             match fs::metadata(&path) {
94///                 Ok(m) => {
95///                     writeln!(&mut output, "{}\t{}", m.len(), path.display())?;
96///                 }
97///
98///                 Err(e) => {
99///                     warning!("{}: {}", path.display(), e);
100///                     result = Err(Error::ExitCode(1));
101///                 }
102///             }
103///         }
104///
105///         result
106///     }
107/// }
108/// ```
109#[macro_export]
110macro_rules! warning {
111    ($($arg:tt)+) => {
112        $crate::log::warning(format!($($arg)+))
113    }
114}