xref: /freebsd/tests/sys/kern/tty/test_canon.orch (revision 096c39fae4ad5135a317925d8749b7d83f65ebf8)
1#!/usr/bin/env -S porch -f
2--
3-- Copyright (c) 2024 Kyle Evans <kevans@FreeBSD.org>
4--
5-- SPDX-License-Identifier: BSD-2-Clause
6--
7
8timeout(3)
9
10spawn("cat")
11
12write "Complete\r"
13match "Complete\r"
14
15write "Basic\rIncomplete"
16match "Basic\r"
17
18-- We shouldn't see any of the "Incomplete" line
19fail(function()
20end)
21
22match "Incomp" {
23	callback = function()
24		exit(1)
25	end
26}
27
28fail(nil)
29
30-- Pushing a ^D along should force a flush of the tty, cat(1) will write the
31-- result without a trailing newline.
32write " line^D"
33match "Incomplete line$"
34
35-- Erase!
36write "Dog^H^D"
37match "Do$"
38
39-- More erase!
40write "Cat Dog^W^D"
41match "Cat $"
42
43write "^D"
44eof()
45
46local function fionread_test(str, expected)
47	spawn("fionread")
48
49	write(str)
50	match(expected)
51end
52
53-- Incomplete line
54fionread_test("Hello", "0")
55-- VEOF does not count
56fionread_test("Hello^D", "5")
57-- VEOF still doesn't count, even if the next line is an extra VEOF later
58fionread_test("Hello^D^D", "5")
59-- read(2) definitely won't return the second incomplete line
60fionread_test("Hello^Dther", "5")
61-- read(2) also won't return a second complete line at once
62fionread_test("Hello^Dthere^D", "5")
63-- Finally, send a VEOF to terminate a blank line and signal EOF in read(2)
64fionread_test("^D", "0")
65
66-- \r will instead show up in the input stream to the application, so we must
67-- make sure those are counted where VEOF generally wouldn't be.
68fionread_test("Hello\r", "6")
69fionread_test("Hello\rther", "6")
70fionread_test("Hello\rthere\r", "6")
71fionread_test("\r", "1")
72
73local function readsz_test(str, arg, expected)
74	spawn("readsz", table.unpack(arg))
75
76	if type(str) == "table" then
77		assert(#str == 2)
78		write(str[1])
79		release()
80
81		-- Give readsz a chance to consume the partial input before we send more
82		-- along.
83		sleep(1)
84		write(str[2])
85	else
86		write(str)
87	end
88	match(expected)
89end
90
91readsz_test("partial", {"-b", 3}, "^$")
92readsz_test("partial^D", {"-b", 3}, "^par$")
93readsz_test("partial^D", {"-c", 1}, "^partial$")
94for s = 1, #"partial" do
95		readsz_test("partial^D", {"-s", s}, "^partial$")
96end
97-- Send part of the line, release and pause, then finish it.
98readsz_test({"par", "tial^D"}, {"-c", 1}, "^partial$")
99-- line is incomplete, so we'll just see the "partial" even if we want two
100readsz_test("partial^Dline", {"-c", 2}, "^partial$")
101readsz_test("partial^Dline^D", {"-c", 1}, "^partial$")
102readsz_test("partial^Dline^D", {"-c", 2}, "^partialline$")
103