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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// @adjivas - github.com/adjivas. See the LICENSE
// file at the top-level directory of this distribution and at
// https://github.com/adjivas/io
//
// This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]
#![allow(non_camel_case_types)]

/// The `NCCS` const is the default number of character
/// parsed by the password input.

pub const NCCS: usize = 32;

/// The `BUFF` and `STDIN_FILENO` const
/// are default values for macros.

pub const BUFF: usize = 2048;
pub const BUFF_READ_NUMBER: usize = 17;
pub const BUFF_READ_COMMAND: usize = 9;
pub const STDIN_FILENO: i32 = 0;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Termios {
    pub c_iflag: u32,  // input mode flags.
    pub c_oflag: u32,  // output mode flags.
    pub c_cflag: u32,  // control mode flags.
    pub c_lflag: u32,  // local mode flags.
    pub c_line: u8,       // line discipline.
    pub c_cc: [u8; NCCS], // control characters.
    pub c_ispeed: u32,  // input speed.
    pub c_ospeed: u32,  // output speed.
}

/// All the `const *` are default values
/// of c_cc.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum ControlCharacter {
    VINTR    =  0,
    VQUIT    =  1,
    VERASE   =  2,
    VKILL    =  3,
    VEOF     =  4,
    VTIME    =  5,
    VMIN     =  6,
    VSWTC    =  7,
    VSTART   =  8,
    VSTOP    =  9,
    VSUSP    = 10,
    VEOL     = 11,
    VREPRINT = 12,
    VDISCARD = 13,
    VWERASE  = 14,
    VLNEXT   = 15,
    VEOL2    = 16,
}

/// All the `const *` are default values
/// of c_iflag.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum InputModes {
    IGNBRK  = 0o000001,
    BRKINT  = 0o000002,
    IGNPAR  = 0o000004,
    PARMRK  = 0o000010,
    INPCK   = 0o000020,
    ISTRIP  = 0o000040,
    INLCR   = 0o000100,
    IGNCR   = 0o000200,
    ICRNL   = 0o000400,
    IUCLC   = 0o001000,
    IXON    = 0o002000,
    IXANY   = 0o004000,
    IXOFF   = 0o010000,
    IMAXBEL = 0o020000,
    IUTF8   = 0o040000,
}

/// All the `const *` are default values
/// of c_oflag.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum OutputModes {
    OPOST  = 0o000001,
    OLCUC  = 0o000002,
    ONLCR  = 0o000004,
    OCRNL  = 0o000010,
    ONOCR  = 0o000020,
    ONLRET = 0o000040,
    OFILL  = 0o000100,
    OFDEL  = 0o000200,
    VT0    = 0o000000,
    VT1    = 0o040000,
}

/// All the `const *` are default values
/// of c_cflag.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum Speed {
    B0       = 0o000000,     /* hang up */
    B50      = 0o000001,
    B75      = 0o000002,
    B110     = 0o000003,
    B134     = 0o000004,
    B150     = 0o000005,
    B200     = 0o000006,
    B300     = 0o000007,
    B600     = 0o000010,
    B1200    = 0o000011,
    B1800    = 0o000012,
    B2400    = 0o000013,
    B4800    = 0o000014,
    B9600    = 0o000015,
    B19200   = 0o000016,
    B38400   = 0o000017,
    B57600   = 0o010001,
    B115200  = 0o010002,
    B230400  = 0o010003,
    B460800  = 0o010004,
    B500000  = 0o010005,
    B576000  = 0o010006,
    B921600  = 0o010007,
    B1000000 = 0o010010,
    B1152000 = 0o010011,
    B1500000 = 0o010012,
    B2000000 = 0o010013,
    B2500000 = 0o010014,
    B3000000 = 0o010015,
    B3500000 = 0o010016,
    B4000000 = 0o010017,
}

/// All the `const *` are default values
/// of c_lflag.

#[repr(C)]
#[derive(Copy, Clone)]
pub enum LocalModes {
    ECHO   = 0o000010,
    ECHOE  = 0o000020,
    ECHOK  = 0o000040,
    ECHONL = 0o000100,
    NOFLSH = 0o000200,
    TOSTOP = 0o000400,
    ISIG   = 0o000001,
    ICANON = 0o000002,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum TermiosControl {
    GETS  = 0x5401,
    SETS  = 0x5402,
    SETSW = 0x5403,
    SETSF = 0x5404,
    GETA  = 0x5405,
    SETA  = 0x5406,
    SETAW = 0x5407,
    SETAF = 0x5408,
    SBRK  = 0x5409,
    XONC  = 0x540a,
    FLSH  = 0x540b,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum Seek {
    SET = 0,
    CUR = 1,
    END = 2,
}

/// The `C` extern is list of system call from libc required
/// by the standard in/out put.

#[cfg(any(unix))]
extern "C" {
    pub fn write(fd: i32, buf: *const i8, len: i32) -> i32;
    pub fn lseek(fd: i32, offset: u64, whence: i32) -> u64;
    pub fn read(fd: i32, buf: *mut i8, len: i32) -> i32;
    pub fn ioctl(fd: i32, req: u64, term: *mut Termios) -> i32;
}