authorgravatar for ariaoverskies@gmail.comnofmal <ariaoverskies@gmail.com> 2020-01-30 13:36:28+07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-04 14:09:57-05:00
loga697de3eac7fb4bb09e7f2aaee18c916a4eacc07
tree9a1829e3af21524d5ec957fdbeb80fc27bc3257c
parent0fdcd5c4cb335fcb2d637b891e60094b7a34e2b5

Add basic linux termios implementation


4 files changed, 116 insertions(+), 0 deletions(-)

lib/std/c.zig+2
...@@ -119,6 +119,8 @@ pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;...@@ -119,6 +119,8 @@ pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
119pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;119pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
120pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;120pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
121pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;121pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
122pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
123pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
122124
123pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;125pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
124pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;126pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
lib/std/os.zig+32
...@@ -3327,3 +3327,35 @@ pub fn getrusage(who: i32) rusage {...@@ -3327,3 +3327,35 @@ pub fn getrusage(who: i32) rusage {
3327 else => unreachable,3327 else => unreachable,
3328 }3328 }
3329}3329}
3330
3331pub const TermiosGetError = error{
3332 NotATerminal,
3333} || UnexpectedError;
3334
3335pub fn tcgetattr(handle: fd_t) TermiosGetError!termios {
3336 var term: termios = undefined;
3337 switch (errno(system.tcgetattr(handle, &term))) {
3338 0 => return term,
3339 EBADF => unreachable,
3340 ENOTTY => return error.NotATerminal,
3341 else => |err| return unexpectedErrno(err),
3342 }
3343}
3344
3345pub const TermiosSetError = TermiosGetError || error{
3346 ProcessOrphaned,
3347};
3348
3349pub fn tcsetattr(handle: fd_t, optional_action: TCSA, termios_p: termios) TermiosSetError!void {
3350 while (true) {
3351 switch (errno(system.tcsetattr(handle, optional_action, &termios_p))) {
3352 0 => return,
3353 EBADF => unreachable,
3354 EINTR => continue,
3355 EINVAL => unreachable,
3356 ENOTTY => return error.NotATerminal,
3357 EIO => return error.ProcessOrphaned,
3358 else => |err| return unexpectedErrno(err),
3359 }
3360 }
3361}
lib/std/os/bits/linux.zig+74
...@@ -1515,3 +1515,77 @@ pub const rusage = extern struct {...@@ -1515,3 +1515,77 @@ pub const rusage = extern struct {
1515 nivcsw: isize,1515 nivcsw: isize,
1516 __reserved: [16]isize = [1]isize{0} ** 16,1516 __reserved: [16]isize = [1]isize{0} ** 16,
1517};1517};
1518
1519pub const cc_t = u8;
1520pub const speed_t = u32;
1521pub const tcflag_t = u32;
1522
1523pub const NCCS = 32;
1524
1525pub const IGNBRK = 1;
1526pub const BRKINT = 2;
1527pub const IGNPAR = 4;
1528pub const PARMRK = 8;
1529pub const INPCK = 16;
1530pub const ISTRIP = 32;
1531pub const INLCR = 64;
1532pub const IGNCR = 128;
1533pub const ICRNL = 256;
1534pub const IUCLC = 512;
1535pub const IXON = 1024;
1536pub const IXANY = 2048;
1537pub const IXOFF = 4096;
1538pub const IMAXBEL = 8192;
1539pub const IUTF8 = 16384;
1540
1541pub const OPOST = 1;
1542pub const OLCUC = 2;
1543pub const ONLCR = 4;
1544pub const OCRNL = 8;
1545pub const ONOCR = 16;
1546pub const ONLRET = 32;
1547pub const OFILL = 64;
1548pub const OFDEL = 128;
1549pub const VTDLY = 16384;
1550pub const VT0 = 0;
1551pub const VT1 = 16384;
1552
1553pub const CSIZE = 48;
1554pub const CS5 = 0;
1555pub const CS6 = 16;
1556pub const CS7 = 32;
1557pub const CS8 = 48;
1558pub const CSTOPB = 64;
1559pub const CREAD = 128;
1560pub const PARENB = 256;
1561pub const PARODD = 512;
1562pub const HUPCL = 1024;
1563pub const CLOCAL = 2048;
1564
1565pub const ISIG = 1;
1566pub const ICANON = 2;
1567pub const ECHO = 8;
1568pub const ECHOE = 16;
1569pub const ECHOK = 32;
1570pub const ECHONL = 64;
1571pub const NOFLSH = 128;
1572pub const TOSTOP = 256;
1573pub const IEXTEN = 32768;
1574
1575pub const TCSA = extern enum(usize) {
1576 NOW,
1577 DRAIN,
1578 FLUSH,
1579 _,
1580};
1581
1582pub const termios = extern struct {
1583 iflag: tcflag_t,
1584 oflag: tcflag_t,
1585 cflag: tcflag_t,
1586 lflag: tcflag_t,
1587 line: cc_t,
1588 cc: [NCCS]cc_t,
1589 ispeed: speed_t,
1590 ospeed: speed_t,
1591};
lib/std/os/linux.zig+8
...@@ -1061,6 +1061,14 @@ pub fn getrusage(who: i32, usage: *rusage) usize {...@@ -1061,6 +1061,14 @@ pub fn getrusage(who: i32, usage: *rusage) usize {
1061 return syscall2(SYS_getrusage, @bitCast(usize, @as(isize, who)), @ptrToInt(usage));1061 return syscall2(SYS_getrusage, @bitCast(usize, @as(isize, who)), @ptrToInt(usage));
1062}1062}
10631063
1064pub fn tcgetattr(fd: fd_t, termios_p: *termios) usize {
1065 return syscall3(SYS_ioctl, @bitCast(usize, @as(isize, fd)), TCGETS, @ptrToInt(termios_p));
1066}
1067
1068pub fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) usize {
1069 return syscall3(SYS_ioctl, @bitCast(usize, @as(isize, fd)), TCSETS + @enumToInt(optional_action), @ptrToInt(termios_p));
1070}
1071
1064test "" {1072test "" {
1065 if (builtin.os == .linux) {1073 if (builtin.os == .linux) {
1066 _ = @import("linux/test.zig");1074 _ = @import("linux/test.zig");