This Space for Rent

New Code!

I’ve been following the Plan 9 mailing list for a few years now, not so much because I use the OS (I’ve tried, but Plan 9 is much too gui-centric for me to tolerate for any period of time) but because they are so minimalist in other ways that it makes me look like someone who writes code for the FSF.

A couple of days ago, a new contributor to the list posted – as part of a thread where the mailing list had been bashing happily away at the Unix version of echo – a manpage describing a ridiculous imaginary version of echo.

The ultimate echo, actually useful, but no one wants it.

NAME
echo: echo arguments

SYNOPSIS
echo [-1abCDEeilmNnOqrtuVvwXx] [-B base] [-c cmd] [-d char] [-f file] [-L len] [-o file] [-S voice] [-s char] [args…]

DESCRIPTION
echo outputs its arguments. It takes the following switches:

-1
One argument per line.
-a
Output in ASCII. The default.
-B base
Output in given base, 2..32. Unless -u also given, base > 10 shows lowercase.
-b
Output in binary.
-C
Don’t echo anything, just print the number of fields.
-c cmd
Run cmd on each argument, replacing $? with the argument itself.
-D
Output in decimal.
-d char
Field delimiter. Default is end of argument.
-E
Print to standard error instead of to standard output.
-e
Allow escape sequences
-f file
Read from file, then from command line (if any).
-i
Read arguments from standard input.
-L len
Line width set to len. Default is to ignore line lengths.
-l
Turn uppercase to lowercase.
-m
Multi-column output.
-N
One field per line, numbering each field.
-n
Suppress newline.
-O
Output in octal.
-o file
Write to file instead of standard output.
-q
“Quiet mode:” redirect output to /dev/null if not to a file.
-r RE
Print every string that matches each regular expression. Regular expressions cannot contain “+” or “*” modifiers.
-S voice
Send to speaker, having the given voice say it. If voice is a null string, use the default voice.
-s char
Separate fields with char, default space.
-t
Separate fields with tabs.
-u
Convert lowercase to uppercase. With -B, output in upper case letters for base > 10.
-V
Strip non-printing characters.
-v
Make non-printing characters visible.
-w
If -l is given, word wrap instead of character wrap. Otherwise, ignored.
-X
Output in uppercase hexadecimal.
-x
Output in lowercase hexadecimal.

Test for everyone: write this echo in as little code as possible. C
or rc is permitted. The rules:

  • for C: either Standard C (no other libraries) or only libc (no other Plan 9 libraries)
  • for rc: only use programs in the core Plan 9 distribution - no programs that I have to get myself
  • match the behavior EXACTLY as above
  • shortest code and fastest run time wins

Winner gets something cool.

This is obviously not a serious offer, because nobody would be silly enough to actually write such a thing.

Oh ho,” said I, “it’s a challenge.”

Secho (silly echo) implements most of the options listed in the quoted message. It doesn’t implement -L, -m, and -w because that would involve some actual mental effort, it doesn’t implement -S because in the land of Unix there are many ways of doing speech, all of which involve mental effort; the regular expressions supported here (maximum of 10) are just plain old REG_BASIC expressions from Henry Spencer’s BSD regex library, so * works despite the pleading of the message. I’ve also added two more options – -0, which means that the input delimiter is a null, so you can do a

find / -print0 | secho -0 -c "file $?"

instead of using xargs, and -9 tells it to do Plan 9 compatability and not print anything when no arguments are there.

So, yes, it’s New Code! in the finest tradition of Open Source ®™© taking a usable idea and cramming so many rococco features onto it so that it rivals the Vasa for seaworthiness. I’ll probably put it into Mastodon, in the o god my eyes! add-on package, too.

UPDATE: Oh, what the hell, let’s go in and add some features to this turkey.