Quick Hacks

Every now and then, I need to do something that I can’t find a program for, or the programs I can find don’t do the work I need (and aren’t free software.) So, if the job is small enough (and sometimes if it isn’t; cwatch, postoffice, and markdown started out as quick hacks,) I’ll reinvent the wheel for the fun of it.

Seq
dump a sequence of things to stdout, one per line. idea by Kernighan & Pike, in The UNIX Programming Environment, filtered through Copeland & Haemer, in Server/Workstation Expert (Vol 11 #2) as a perl program, then redone by me in C.
Jot

Jot prints out numbers (in a variety of forms, either randomly or in sequence) within a specified range. It exists on FreeBSD, but not on mastodon, so I spent an afternoon fixing that wagon.

This version of jot can be renamed (or linked to as) yes or seq, so it actually kills 4 birds with one stone.

To compile jot, you’ll need a random number generator, and I’ve written a manpage and a html page for it.

Sbs

Stupid Block Sort – sbs for short – sorts, in the slowest manner possible, lines of text by line length. I wrote it because I wanted to see how long some of the filenames in my home directory were. At midnight. In a house with 2 babies.

Amazingly, I provided it with command-line options;

-n
Prefix each line with the line length
-u max
Don’t display lines longer than max characters.
-l min
Don’t display lines shorter than min characters.
-F char
Use char as the line separator instead of \n.
-r
Sort in the reverse order.
A Mail forwarder for mutt

Mutt is a pretty good mail program, but it’s got a couple of whopping deficiencies. One of them is that for some inexplicable reason it doesn’t talk smtp but instead requires you to use an external program to get your mail to your local friendly smtp server. Several of these programs exist, but they’re written with the modern version of portability in mind – they work on both redhat and Debian Linux, but don’t work very well on other platforms.

One of these other platforms is DEC OSF-1, which is able to reliably kill GNU configure dead. On 15-Jan-2004, I was using mutt on an Alpha box and I needed to send mail to the corporate mail (exchange. *shudder*) server, and after a quick tour of “lets see just how confused configure can get when it’s running on a box that doesn’t have Linux on it!,” I wrote a little mail forwarder that uses my network client framework to send mail from a non-Linux machine.

Website nutritional label
I wanted to build a colophon for this site, but didn’t want to to be static. This shell script generates an list of ingredients that attempts to summarize all the files on the website.
A barcode generator
The powdered Oregon Chai (Horrible flash-ridden website, so I won’t be linking to it) box that I was looking at when I was thinking of what to make the colophon look like included a UPC barcode. So I wrote a barcode program so I could drop a barcode onto the ingredients page. This program doesn’t generate UPC barcodes; it generates Code 128 barcodes instead (because code128 allows all the ascii characters and arbitrarily long codes.)
A getopt() with long options
I went through a phase where I thought long options were pretty neat, so I wrote my own function to process them in the style of getopt(3).
A simple http redirect server

After a hardware failure, I had to move a web server to a different machine, and when checking my access logs to make sure I’d changed all of the appropriate cnames to point at the new machine, I discovered a large wad of hits directed not at the nicely cnameded weblog.pell.portland.or.us, but at the physical machine that this cname used to point to. This is not the best sort of situation to be in, particularly when the web server is no longer on that particular machine.

I didn’t want to install a full-blown web server to manage the 300-400 redirects I’d need per day, but the only pre-written redirector I could find was a chunk of Perl code written for Perl 5.6 when all I’ve got is Perl 5.0005somethingoranother (and that only because the Linux Counter ticker is written in that language.)

So, rather than attempt to put Perl 5.6 onto my a.out-based SLS development machine, I hacked up this tiny little redirection server, which is now running on pell, 302ing people off to weblog.pell.portland.or.us, where they should have been in the first place.

A lines-of-code counter for C programs
As I’ve been working on cron, I’ve gradually been commenting it up. This is a good thing, but the cloud around it is that comments make it difficult for me to count code bloat versus comment bloat. I wrote this little piece of code that runs cpp, then counts the lines of code that are left in the source file after being decommented, macroed, and otherwise massaged by cpp. It understands the # lineno “file” directives that cpp (or at least the gcc version of cpp; the gcc maintainers don’t seem to be very well wrapped on the C programming language, so it’s very possible sloc won’t work on systems that have real C compilers) spits out to help the compiler generate intelligible diagnostics, and uses them to make sure it’s only counting the lines-of-code that code from the file being counted.