Search code examples
bashshellioposixposix-api

How does shell preserve command file input line boundary?


The description of STDIN of sh in SUSv4 2016 edition says

It shall not read ahead in such a manner that any characters intended to be read by the invoked command are consumed by the shell

I did an experiment to see it in action, by redirecting the following script file into sh -s:

#!/bin/sh
./rewind # a c program that reads up its stdin, prints it to stdout, before SEEK_SET its standard input to 0. 
echo 1234-1234

And it keeps printing out "echo 1234-1234". (Had shell consumed whole block of file, it would only print out "1234-1234")

So obviously, shells (at least my ones) do read at line boundaries.

But however, when I examined the FreeBSD ash "input.c" source codes, it reads in BUFSIZ-byte blocks, and I don't understand how it preserves line boundaries.

What I want to know is: How does shells preserve line boundaries when their source codes apparently shows that they read in blocks?


Solution

  • When I compiled the FreeBSD ash with NO_HISTORY macro defined to 1 in "shell.h", it consumes the whole file and outputs only 1234-1234 on my rewind testing program. Apparently, the FreeBSD ash relies on libedit to preserve IO line boundaries.