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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Hardware control of terminal

use std::fmt;
use std::ops::{Index, IndexMut};

use Termios;
use raw::{cc_t, tcflag_t, self};
use self::CSIZE::*;
use self::Char::*;
use self::Flag::*;
use traits::{Clear, Contains, Get, GetFrom, Set};

/// Control flags
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Flags(tcflag_t);

impl fmt::Debug for Flags {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "{:?}", CSIZE::from_raw(self.0 & CSIZE_MASK)));

        for &flag in &FLAGS {
            let value = flag.to_raw();

            if self.0 & value == value {
                try!(write!(f, " | {:?}", flag))
            }
        }

        Ok(())
    }
}

const FLAGS: [Flag; 7] = [
    CLOCAL,
    CREAD,
    CRTSCTS,
    CSTOPB,
    HUPCL,
    PARENB,
    PARODD,
];

/// Standard control flags
#[derive(Clone, Copy, Debug)]
pub enum Flag {
    /// Ignore modem status lines
    CLOCAL,
    /// Enable receiver
    CREAD,
    /// RTS/CTS full-duplex flow control
    CRTSCTS,
    /// Send 2 STOP bits
    CSTOPB,
    /// Hang up on last close
    HUPCL,
    /// Parity enable
    PARENB,
    /// Odd parity, else even
    PARODD,
}

impl Flag {
    fn to_raw(&self) -> tcflag_t {
        match *self {
            CLOCAL => raw::CLOCAL,
            CREAD => raw::CREAD,
            CRTSCTS => raw::CRTSCTS,
            CSTOPB => raw::CSTOPB,
            HUPCL => raw::HUPCL,
            PARENB => raw::PARENB,
            PARODD => raw::PARODD,
        }
    }
}

impl Clear<Flag> for Termios {
    fn clear(&mut self, flag: Flag) {
        self.cflag.0 &= !flag.to_raw()
    }
}

impl Contains<Flag> for Termios {
    fn contains(&self, flag: Flag) -> bool {
        let flag = flag.to_raw();

        self.cflag.0 & flag == flag
    }
}

impl Set<Flag> for Termios {
    fn set(&mut self, flag: Flag) {
        self.cflag.0 |= flag.to_raw()
    }
}

/// Character size
#[derive(Clone, Copy, Debug)]
pub enum CSIZE {
    /// 5 bits (pseudo)
    CS5,
    /// 6 bits
    CS6,
    /// 7 bits
    CS7,
    /// 8 bits
    CS8,
}

impl CSIZE {
    fn to_raw(&self) -> tcflag_t {
        match *self {
            CS5 => raw::CS5,
            CS6 => raw::CS6,
            CS7 => raw::CS7,
            CS8 => raw::CS8,
        }
    }

    fn from_raw(csize: tcflag_t) -> CSIZE {
        match csize {
            raw::CS5 => CS5,
            raw::CS6 => CS6,
            raw::CS7 => CS7,
            raw::CS8 => CS8,
            _ => panic!("Unknown CSIZE state: {}", csize),
        }
    }
}

const CSIZE_MASK: tcflag_t = raw::CS5 | raw::CS6 | raw::CS7 | raw::CS8;

impl Get for Termios {}

impl GetFrom<Termios> for CSIZE {
    fn get_from(termios: &Termios) -> CSIZE {
        CSIZE::from_raw(termios.cflag.0 & CSIZE_MASK)
    }
}

impl Set<CSIZE> for Termios {
    fn set(&mut self, csize: CSIZE) {
        self.cflag.0 &= !CSIZE_MASK;
        self.cflag.0 |= csize.to_raw();
    }
}

/// Control chars
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Chars([cc_t; raw::NCCS as usize]);

impl Index<Char> for Chars {
    type Output = cc_t;

    fn index(&self, char: Char) -> &cc_t {
        &self.0[char.to_raw()]
    }
}

impl IndexMut<Char> for Chars {
    fn index_mut(&mut self, char: Char) -> &mut cc_t {
        &mut self.0[char.to_raw()]
    }
}

impl fmt::Debug for Chars {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut is_first = true;

        for &char in &CHARS {
            if is_first {
                is_first = false;

                try!(write!(f, "{:?}: {:?}", char, self[char]));
            } else {
                try!(write!(f, ", {:?}: {:?}", char, self[char]));
            }
        }

        Ok(())
    }
}

const CHARS: [Char; 16] = [
    VDISCARD,
    VEOF,
    VEOL2,
    VEOL,
    VERASE,
    VINTR,
    VKILL,
    VLNEXT,
    VMIN,
    VQUIT,
    VREPRINT,
    VSTART,
    VSTOP,
    VSUSP,
    VTIME,
    VWERASE,
];

/// Standard control chars
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum Char {
    VDISCARD,
    VEOF,
    VEOL2,
    VEOL,
    VERASE,
    VINTR,
    VKILL,
    VLNEXT,
    VMIN,
    VQUIT,
    VREPRINT,
    VSTART,
    VSTOP,
    VSUSP,
    VTIME,
    VWERASE,
}

impl Char {
    fn to_raw(&self) -> usize {
        (match *self {
            VDISCARD => raw::VDISCARD,
            VEOF => raw::VEOF,
            VEOL => raw::VEOL,
            VEOL2 => raw::VEOL2,
            VERASE => raw::VERASE,
            VINTR => raw::VINTR,
            VKILL => raw::VKILL,
            VLNEXT => raw::VLNEXT,
            VMIN => raw::VMIN,
            VQUIT => raw::VQUIT,
            VREPRINT => raw::VREPRINT,
            VSTART => raw::VSTART,
            VSTOP => raw::VSTOP,
            VSUSP => raw::VSUSP,
            VTIME => raw::VTIME,
            VWERASE => raw::VWERASE,
        }) as usize
    }
}