9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Douglas A. Gwyn" <DAGwyn@null.net>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] weird print(2) problems...
Date: Thu,  6 Sep 2001 16:18:35 +0000	[thread overview]
Message-ID: <3B978E5A.262D53E9@null.net> (raw)
In-Reply-To: <20010905191944.AC31319A04@mail.cse.psu.edu>

Russ Cox wrote:
> > print() shouldn't call write(), as there's nothing to write.
> then {echo ''} shouldn't print a newline, as there's nothing to echo.

No, the semantics for "echo" are traditionally defined as simply:
printing its arguments separated by spaces and followed by a newline.
So even "echo" with no additional arguments must print a newline.
(Unless, of course, told to suppress the newline, as with the -n
option.)  Echo with a 0-length string argument should print the
0-length string argument, separated from adjacent arguments in
the output by spaces, then NL.  "echo a '' '' b" should print
"a<space><space><space>b<newline>".

The problem with "higher-level" library functions such as print()
is that, generally, there is no clear specification relating their
invocation and when they call low-level functions such as write().

> there's no obvious answer except that we shouldn't try to "fix" it.

write() with buffer length 0 should of course write an empty data
packet.  Whether a subsequent read will see the packet boundaries
depends on several things, and whether a 0-length packet is
interpreted as "end of file" depends on several *other* things.

We ran into all this on early versions of UNIX, where a ^D on a
terminal input actually just delimited (sent along from raw to
canonical queue) the current input, same as a new-line input,
except of course without the embedded newline character.  The
UNIX convention was that read() returning 0 characters, such as
it would for a terminal *iff* a delimiter immediately followed
a previous delimiter (newline or ^D-activated), was interpreted
as end of file.  This was consistent with reading characters
beyond the last one present in a static (disk) file.  On pipes,
it required preservation of written record lengths so a 0-length
record could be detected by read().

Ultimately, many flavors of I/O on UNIX-based systems grew more
complicated, and record lengths were harder to preserve.  EOF
was therefore sometimes indicated by out-of-band information
instead of by a 0-length data packet.  However, the 0-length
idea has considerable appeal, and whatever problems there are
seem to be due to assuming semantics for an "end of file"
indication that are more heavyweight ("sticky") than the
condition sometimes indicates.  I would say that a "solution"
would be for read() from a source that (1) has no more data,
(2) has already reported 0-length for end of data, and (3) can
*never* have any more data to return a distinct "attempted read
past end of data" error, instead of the less informative 0-long
buffer.  That would not affect applications that interpret a
0-length return from read() as EOF, but would allow apps to
do the "natural" thing with empty data packets (i.e. process no
data) instead of having to assume they mean permanent EOF.

>                     The Plan 9 and the Echo

The parable is silly.  The traditional semantics of "echo" have
a simple, regular specification, which is not true of an "echo"
that outputs nothing when supplied with no non-option arguments.


  parent reply	other threads:[~2001-09-06 16:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-05 19:19 Russ Cox
2001-09-05 19:40 ` Boyd Roberts
2001-09-06  8:19 ` Matthew Hannigan
2001-09-06 16:18 ` Douglas A. Gwyn [this message]
2001-09-06 20:45   ` Boyd Roberts
  -- strict thread matches above, loose matches on Subject: below --
2001-09-11 11:16 rob pike
     [not found] <rsc@plan9.bell-labs.com>
2001-09-05 21:11 ` Russ Cox
2001-09-05 21:24   ` Scott Schwartz
2001-09-05 19:46 Russ Cox
2001-09-05 18:40 Russ Cox
2001-09-05 19:58 ` Dan Cross
2001-09-05 15:11 forsyth
2001-09-05 18:23 ` Dan Cross
2001-09-05 19:10   ` Boyd Roberts
     [not found] <cross@math.psu.edu>
2001-09-04 19:02 ` Dan Cross
2001-09-04 19:08   ` Scott Schwartz
2001-09-05 14:22     ` Dan Cross
2001-09-05 19:01       ` Boyd Roberts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3B978E5A.262D53E9@null.net \
    --to=dagwyn@null.net \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).