Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 68 additions & 14 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,7 +50,7 @@
)
};

($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,
Expand All @@ -61,7 +61,7 @@
)
};

($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,
Expand Down Expand Up @@ -90,19 +90,19 @@
/// ```
#[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,
Expand All @@ -111,7 +111,7 @@
)
};

($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,
Expand All @@ -121,7 +121,7 @@
)
};

($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,
Expand All @@ -132,7 +132,7 @@
)
};

($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,
Expand All @@ -144,3 +144,57 @@
)
};
}

/// 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);

Check failure on line 156 in src/macros.rs

View workflow job for this annotation

GitHub Actions / Test Suite (x86_64-unknown-linux-gnu)

cannot find type `RawFd` in this scope

Check failure on line 156 in src/macros.rs

View workflow job for this annotation

GitHub Actions / Test Suite (Nightly) (x86_64-unknown-linux-gnu)

cannot find type `RawFd` in this scope
/// }
/// ```
///
#[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)?);
)*
}
}
Loading