hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

pty_common.ha (1015B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bufio;
      5 use fmt;
      6 use fs;
      7 use io;
      8 use os;
      9 use strings;
     10 
     11 // Opens an available pseudoterminal and returns the file descriptors of the
     12 // master and slave.
     13 export fn openpty() ((io::file, io::file) | fs::error) = {
     14 	let master = open_master()?;
     15 	let slave = match (get_slave(master)) {
     16 	case let e: fs::error =>
     17 		io::close(master)!;
     18 		return e;
     19 	case let s: io::file =>
     20 		yield s;
     21 	};
     22 
     23 	return (master, slave);
     24 };
     25 
     26 @test fn pty() void = {
     27 	let pty = openpty()!;
     28 	defer io::close(pty.1)!;
     29 	defer io::close(pty.0)!;
     30 
     31 	assert(fs::exists(os::cwd, ptsname(pty.0)!));
     32 
     33 	for (let i: u16 = 5; i < 100; i += 1) {
     34 		let sz1 = ttysize { rows = i, columns = i };
     35 		set_winsize(pty.1, sz1)!;
     36 		let sz2 = winsize(pty.1)!;
     37 		assert(sz2.rows == sz1.rows);
     38 		assert(sz2.columns == sz1.columns);
     39 	};
     40 
     41 	fmt::fprintln(pty.0, "hello, world")!;
     42 	let s = strings::fromutf8(bufio::read_line(pty.1) as []u8)!;
     43 	defer free(s);
     44 	assert(s == "hello, world");
     45 };