Subject: Lines and last lines.

From: Alan Watson <[email-masked]>
Date: Mon, 30 Nov 1992 19:40:45 -0500

I'm struggling with sam's supposed break from the line-oriented nature
of most Unix utilities.

Relevant parts of the man page are [1]

     \n         Match newline.
     ^          Match the null string immediately after a newline.
     $          Match the null string immediately before a newline.

and later [2]

     (The peculiar properties of a last line without a newline are tem-
     porarily undefined.)

and later still [3]

     x/regexp/ command
          For each match of the regular expression in the range, run the com-
          mand with dot set to the match.  Set dot to the last match.  If the
          regular expression and its slashes are omitted, /.*\n/ is assumed.
          Null string matches potentially occur before every character of the
          range and at the end of the range.

My first comment is that [2] is somewhat against sam's philosophy that
files are arbitrary byte streams; it does impose a structure on the
file (i.e., that the final character must be \n).

Secondly, let's investigate the interactions of [2] and [3]:

	; sam -d
	a/abcd\n/
	a/efgh\n/
	a/ijkl/

	,p
	abcd
	efgh
	ijkl	<- cursor here

	,x/.*\n/p
	abcd
	efgh
		<- cursor here

	,x p
	abcd
	efgh
	ijkl	<- cursor here

These last two might be expected to give the same output, given the
stated default for x.

Finally, lets investigate the interaction of [1] and [2]:

	,x /^.*/p
	efghijklabcd	<- cursor here

	,x /.*$/p
	efghabcdabcd	<- cursor here
	
Okay, my point is that it might have been better to define different
semantics for ^ and $, namely that they match the start of a line
(i.e., the null string at the start of the file and the null string
after a \n not at the end of the file) and the end of a line (i.e.,
the null string before a \n not at the end of the file and the null
string at the end of the file).

The default for x might then have been /^.*$/, and this might have
helped to define the semantics of a final line without a newline.

---

From: Chris Siebenmann <cks>
Date: Tue, 1 Dec 1992 00:46:48 -0500

sam is happy with lines that don't end in newlines; it warns when
you write them out, but otherwise leaves them alone. The 'peculiar
properties' are probably that '$' does not match the end of a line
not terminated in one.

 The manual page is not quite correct as to x/y's default selection;
there is an explicit routine that loops over all lines, using the usual
uncommented code.

	- cks