[Toybox] Looking at nl

idunham at lavabit.com idunham at lavabit.com
Sat May 25 16:36:49 PDT 2013


I thought I'd look at what nl takes, since it's not much more than
looping over some lines, incrementing, and formatting the output.
The POSIX reference page is:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/nl.html

There are a few little "fun" details, though.
-Fixed length strings.
 -n should be one of 3 2-char strings, and -d should be at most 2 chars.
 Is there a way to indicate this in NEWTOY or GLOBALS, or should we just
 check for matches in the main loop?
 I'm half tempted to just ignore length and assume the default if an invalid
 arg is specified. So -n lna would be treated as -n ln, and -n asd would be
 treated as -n rn.  But that's probably a little too liberal in
 accepting bad flags...
-Variable format specifiers:
 -w 5 means printing roughly this:
 printf("%5d%s%s", linecount, sep, toybuf)
 But -n ln -w 7 makes it %-7d...

-When to start a new page.
A page contains a header (delimiter occurs 3x), 
body (delimiter occurs 2x), and footer (delimiter occurs once).

POSIX specifies that line numbering shall be reset at the start of each
logical page, and that "Unless otherwise specified, nl shall assume the
text being read is in a single logical page body."
The obvious approach to me is to say that if you go to a new page section 
no lower on the page, you've started a new page.
So 
\:\:
Text of Page 1
\:\:
More text.

is 2 page body sections, numbered thus:
     1  Text of Page 1

     1  More text.

However, GNU nl assumes that a new page starts with a new header, and
treats two consecutive body sections as one...except it prints a blank
line between them as a section separation should. So the sample above 
becomes:
     1  Text of Page 

     2  More text.

On other topics...
Would initializing GLOBALS to the defaults be something sane to allow,
or would it complicate the build system too much?
Example:
USE_NL(NEWTOY(wc, "1b:d:f:h:i#l#n:ps:v#w#", TOYBOX_USR | TOYBOX_BIN))
..
GLOBALS(
char *btype = "t";
char *delim = "\:";
char *ftype = "n";
char *htype = "n";
long incr = 1;
long maxblank = 1;
char *fmt = "rn";
char *sep = "\t";
long startnum = 1;
long width = 6;
)


Thanks,
Isaac Dunham



More information about the Toybox mailing list