1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// @adjivas - github.com/adjivas. See the LICENSE
// file at the top-level directory of this distribution and at
// https://github.com/adjivas/msg
//
// This file may not be copied, modified, or distributed
// except according to those terms.

/// The `ftok` macro returns the System-V'IPC
/// key from pathname.

#[macro_export]
macro_rules! ftok {
    () => ({
        extern crate msg;
        match unsafe {
          msg::ffi::ftok (
            msg::ffi::TOK_PATHNAME.as_ptr() as *mut i8,
            msg::ffi::TOK_PROJ_ID as i32,
          )
        } {
            -1 => None,
            key => Some(key as u64),
        }
    });
    ($pathname: expr) => ({
        extern crate msg;
        match unsafe {
            msg::ffi::ftok (
                $pathname.as_ptr() as *mut i8,
                msg::ffi::TOK_PROJ_ID as i32
            )
        } {
            -1 => None,
            key => Some(key as u64),
        }
    });
}

/// The `msgget` macro returns identifiant of
/// XSI message queue.

#[macro_export]
macro_rules! msgget {
    ($key: expr) => ({
        extern crate msg;
        msgget! (
            $key,
            msg::ffi::Ipc::CREAT as i32 | 0o0666
        )
    });
    ($key: expr, $msgfl: expr) => ({
        extern crate msg;
        match unsafe {
          msg::ffi::msgget (
            $key as i32,
            $msgfl,
          )
        } {
            -1 => None,
            id => Some(id as i32),
        }
    });
}

/// The `msgsnd` macro sends a new message
/// to the XSI queue.

#[macro_export]
macro_rules! msgsnd {
    ($id: expr, $at: expr, $text: expr) => ({
        extern crate msg;
        let mut p = $text.as_ptr();
        let mut buf = msg::ffi::MsgBuf {
            mtype: $at as i64,
            mtext: *unsafe {
                let aref = &*(p as *const [i8; msg::ffi::MSG_BUFF as usize]);

                p = p.offset(msg::ffi::MSG_BUFF as isize);
                aref
            },
        };

        match unsafe {
            msg::ffi::msgsnd (
                $id as i32,
                &mut buf,
                msg::ffi::MSG_BUFF as u64,
                msg::ffi::Ipc::NOWAIT as i32,
            )
        } {
            -1 => None,
            xsi => Some(xsi as i32),
        }
    });
}

/// The `msgrcv` macro recuperates a new message
/// according to $from variable.

#[macro_export]
macro_rules! msgrcv {
    ($id: expr, $from: expr) => ({
        extern crate msg;
        let mut rcv = msg::ffi::MsgBuf {
            mtype: $from as i64,
            mtext: [0 as i8; msg::ffi::MSG_BUFF as usize],
        };

        match unsafe {
            msg::ffi::msgrcv (
                $id as i32,
                &mut rcv,
                msg::ffi::MSG_BUFF as u64 ,
                $from as i64,
                msg::ffi::Ipc::NOWAIT as i32,
            )
        } {
            -1 => None,
            _ => Some(rcv.mtext),
        }
    });
}

/// The `msgclr` macro returns a information
/// according the argument command.

#[macro_export]
macro_rules! msgctl {
    ($id: expr, $cmd: expr) => ({
        extern crate std;
        msgctl!($id, $cmd, std::ptr::null_mut())
    });
    ($id: expr, $cmd: expr, $info: expr) => ({
        extern crate msg;
        match unsafe {
            msg::ffi::msgctl (
                $id,
                $cmd as i32,
                $info
            )
        } {
            -1 => false,
            _ => true,
        }
    });
}