raiguard/tcom
git clone https://git.raiguard.me/raiguard/tcom
d0570cc
trunk
1// TODO: Finish porting 2 3#+ignore 4 5// Based on implementations from musl libc: https://musl.libc.org 6// 7// MIT License: 8// 9// Copyright © 2005-2020 Rich Felker, et al. 10// 11// Permission is hereby granted, free of charge, to any person obtaining 12// a copy of this software and associated documentation files (the 13// "Software"), to deal in the Software without restriction, including 14// without limitation the rights to use, copy, modify, merge, publish, 15// distribute, sublicense, and/or sell copies of the Software, and to 16// permit persons to whom the Software is furnished to do so, subject to 17// the following conditions: 18 19// The above copyright notice and this permission notice shall be 20// included in all copies or substantial portions of the Software. 21 22// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 30package tcom 31 32import "core:fmt" 33import "core:sys/posix" 34import "core:sys/linux" 35 36openpty :: proc(pm, ps: ^posix.FD, tio: ^posix.termios, allocator := context.temp_allocator) -> (success: bool) { 37 cs: posix.Cancel_State 38 39 m := posix.open("/dev/ptmx", {.RDWR, .NOCTTY}); 40 if m < 0 do return false; 41 defer if !success do posix.close(m) 42 43 posix.pthread_setcancelstate(.DISABLE, &cs); 44 defer posix.pthread_setcancelstate(cs, nil) 45 46 TIOCSPTLCK :: 0x40045431 47 TIOCGPTN :: 0x80045430 48 49 n: int 50 if linux.ioctl(linux.Fd(m), TIOCSPTLCK, uintptr(&n)) != 0 || linux.ioctl(linux.Fd(m), TIOCGPTN, uintptr(&n)) != 0 { 51 return false 52 } 53 54 buf: [20]byte 55 fmt.bprintf(buf[:], "/dev/pts/%d", n); 56 buf[len(buf)-1] = 0 // Ensure null terminator 57 58 s := posix.open(cstring(raw_data(buf[:])), {.RDWR, .NOCTTY}) 59 if s < 0 do return false 60 61 // if tio != nil do libc.tcsetattr(s, TCSANOW, tio) 62 // // if (ws) ioctl(s, TIOCSWINSZ, ws); 63 64 pm^ = m 65 ps^ = s 66 67 return true; 68} 69 70forkpty :: proc()