diff --git a/src/macros.rs b/src/macros.rs index ecd4d96..e655955 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -24,23 +24,23 @@ /// ``` #[macro_export] macro_rules! syscall { - ($nr:expr) => { + ($nr:expr $(,)?) => { $crate::syscall0($nr) }; - ($nr:expr, $a1:expr) => { + ($nr:expr, $a1:expr $(,)?) => { $crate::syscall1($nr, $a1 as usize) }; - ($nr:expr, $a1:expr, $a2:expr) => { + ($nr:expr, $a1:expr, $a2:expr $(,)?) => { $crate::syscall2($nr, $a1 as usize, $a2 as usize) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr $(,)?) => { $crate::syscall3($nr, $a1 as usize, $a2 as usize, $a3 as usize) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr $(,)?) => { $crate::syscall4( $nr, $a1 as usize, @@ -50,7 +50,7 @@ macro_rules! syscall { ) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr $(,)?) => { $crate::syscall5( $nr, $a1 as usize, @@ -61,7 +61,7 @@ macro_rules! syscall { ) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr $(,)?) => { $crate::syscall6( $nr, $a1 as usize, @@ -90,19 +90,19 @@ macro_rules! syscall { /// ``` #[macro_export] macro_rules! raw_syscall { - ($nr:expr) => { + ($nr:expr $(,)?) => { $crate::raw::syscall0($nr as usize) }; - ($nr:expr, $a1:expr) => { + ($nr:expr, $a1:expr $(,)?) => { $crate::raw::syscall1($nr as usize, $a1 as usize) }; - ($nr:expr, $a1:expr, $a2:expr) => { + ($nr:expr, $a1:expr, $a2:expr $(,)?) => { $crate::raw::syscall2($nr as usize, $a1 as usize, $a2 as usize) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr $(,)?) => { $crate::raw::syscall3( $nr as usize, $a1 as usize, @@ -111,7 +111,7 @@ macro_rules! raw_syscall { ) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr $(,)?) => { $crate::raw::syscall4( $nr as usize, $a1 as usize, @@ -121,7 +121,7 @@ macro_rules! raw_syscall { ) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr $(,)?) => { $crate::raw::syscall5( $nr as usize, $a1 as usize, @@ -132,7 +132,7 @@ macro_rules! raw_syscall { ) }; - ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr) => { + ($nr:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr $(,)?) => { $crate::raw::syscall6( $nr as usize, $a1 as usize, @@ -144,3 +144,57 @@ macro_rules! raw_syscall { ) }; } + +/// Defines a set of syscalls using a macro. +/// +/// # Example +/// ``` +/// use syscalls::{define_syscalls, Sysno}; +/// +/// define_syscalls! { +/// fn write(fd: RawFd, buf: *const u8, count: usize) -> usize; +/// pub fn exit(status: i32); +/// } +/// ``` +/// +#[macro_export] +macro_rules! define_syscalls { + ( + $pub: vis + fn + $name: ident + ($($iden: ident: $type: ty),*) + -> + $ret: ty + ) => { + #[inline] + $pub unsafe fn $name($($iden: $type),*) -> core::result::Result<$ret, $crate::Errno> { + unsafe { $crate::syscall!($crate::Sysno::$name, $($iden),*).map(|x| x as $ret) } + } + }; + ( + $pub: vis + fn + $name: ident + ($($iden: ident: $type: ty),*) + ) => { + #[inline] + $pub unsafe fn $name($($iden: $type),*) -> core::result::Result<(), $crate::Errno> { + unsafe { $crate::syscall!($crate::Sysno::$name, $($iden),*).map(|_| ()) } + } + }; + ( + $( + $pub: vis + fn + $name: ident + ($($iden: ident: $type: ty $(,)?)*) + $(-> $ret: ty)? + ; + )* + ) => { + $( + $crate::define_syscalls!($pub fn $name($($iden: $type),*) $(-> $ret)?); + )* + } +}