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