Archive for January, 2006

Wisdom of the Documentation

Today, I spent some time staring at an old piece of code that I had written at least a year ago. It’s been in testing several times, but never put into production (the project it is tied to has been bumped on several occaisions). Today, it was back in testing.

The code is a failry simple web service, written in Perl. I like Perl. I have no illusions that I’m a fantastic Perl hacker, but I know the language well, though both experience and reading. I’ve read most of the O’Reilly Perl titles, including Programming Perl (“The Camel”), which I’ve read cover to cover at least three times. I still find myself looking things up, usually to refresh my memory about something I can remember reading, or some syntax detail I can’t get right (one of the perils of working in multiple languages). At least I generally know where to look.

So this web service has been tested before. It works in a browser, and it works when called by my test client. It’s been tested with a third-party bit of code. Today, it was tested by Dave, using some custom client code he had written in C#. And it worked… if he told his http library to ignore HTTP protocol errors. If he didn’t, his library complained.

And so I stared at the code for a while. By coincidence, I’d been reading the HTTP Spec over the weekend (yes, I’m a geek), and was pretty sure my response was good- a bare-minumum response, along the lines of:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8

Single Line Response

I double checked the spec anyway, and kept staring at the code. I was about to start grasping at straws and adding additional entity headers to the response (such as Content-Length), when I finally stared at the code long enough. I saw something like this:

print "HTTP/1.1 $status\n"
print "Content-Type: text/plain; charset=utf-8\n"

Then it hit me- "\n" in perl is a “magic newline”- it conforms to the newline convention on the system in question. HTTP, on the other hand, requires ASCII CR+LF (Cariage Return + Line Feed, or "\r\n" in C) as a line terminator. Apparently all of the code thrown at the service before today was a bit forgiving. I changed the strings to send CRLF using octal escape sequences ("\015\012"), and everything was fine. I was a bit ticked about the mistake… I new both the HTTP requirement for CRLF and the Perl treatment of "\n" when I originally wrote the code; it was a dumb mistake. Also aggravating that it took so long to spot.

And there my tale should end. But this evening, I started wondering if the octal sequence was the most Perlish way to send a CRLF. I knew that "\r" is fine for the CR, but you can’t use "\n" for the LF – it’s magic in perl, and behaves differently on different platforms. I began to wonder if Perl has a backslash-escape for LF that is always LF. Eventually, I had to check for myself, so I referred to the Quote and Quote-like Operators section of the perlop man page. (Sadly, I knew right where to look, right down to the name of the section. Geek, remember?)

Turns out the manpage specifically recommends the octal form for networking applications (at least I got that right), but then it twists the knife:

If you get in the habit of using "\n" for networking, you may be burned some day.


HOWTO (Build and) Install Carbon XEmacs on OS X

Update: added instructions for installing elisp packages after building.

There are as many Emacsen as there are grains of sand in the desert. For various reason (largely habit) I prefer XEmacs to Emacs, when given a choice- even when in text mode (xemacs -nw for the uninitiated; I usually alias this to emacs). And while I love a good command prompt as much as the next geek, I do own a Mac, and sometimes I want a more Mac-like experience.

Carbon XEmacs is a derivative of XEmacs, developed by Andrew Choi. Carbon XEmacs does not require X11, but instead uses the OS X Carbon libraries for its GUI display. Of the various OS X-specific Emacsen I have tried, it is my current favorite. At the time of this writing, the current version of Carbon XEmacs is Beta 4, and is available as a series of patches against the XEmacs source. This HOWTO describes how to build Carbon XEmacs Beta 4 for Mac OS X 10.4.4, and attempts to describe more generally how to build the latest Carbon XEmacs for any OS X 10.3+. Since many Mac users (myself included) are spoiled by the many binary packages and package managers (such as Fink and Darwin Ports) available for OS X, this HOWTO goes into some detail about how to download, patch, and build the source. If you already know this stuff, feel free to skim.

(If you’ve never done so before, you’ll need to install the XCode developer tools from your OS X install CD/DVD. If you’ve lost these, it’s available from Apple’s Developer site. It’s basically a click-and-forget installer package.)

Let’s do this

To begin, you will need to download the XEmacs source for the same XEmacs version that your Carbon XEmacs patches apply to. At the time of the writing, the latest Carbon XEmacs is Beta 4, which is supplied as a diff (set of patches) to xemacs-21.5.23. Visit the download page to locate a suitable mirror, and download the source package for the correct version. As of this writing, all 21.5.x releases are considered Beta releases of XEmacs, so I chose the beta download page, and a local mirror therein.

When you arrive at the mirror, it will be a directory on an FTP server, full of files. look for a tarball- a file ending with .tar.gz– matching the xemacs version you need. In this case, I’ll be downloading xemacs-21.5.23.tar.gz. You will also see files with similar names ending in .patch.gz, .asc, and .md5. You will not need these (unless you need them- in which case you already know you need them. The rest of us will soldier on).

After downloading the file, open Terminal and change to the directory where the .tar.gz file was downloaded. I downloaded mine to my ~/ext directory:

cd ~/ext

If you don’t know where your downloads go, they probably go to your desktop; try cd ~/Desktop (capitalization counts). Now we’ll extract the source code:

tar xzf xemacs-21.5.23.tar.gz

Download the diff file from the Carbon XEmacs page to the same directory where you downloaded the xemacs source. Uncompress the diff file:

bunzip2 xemacs-21.5.23-carbon-b4.diff.bz2

And patch the original sources:

patch -p0 < xemacs-21.5.23-carbon-b4.diff

An aside: this is some pretty cool stuff if you’re not familiar with diffing and patching; the diff file is just a list of the changes between the original and Andrew Choi’s version, patch applies these changes to our original files, resulting in files that match Choi’s modified source. For the curious, -p0 (essentially) tells patch that it doesn’t have to adjust the paths; that’s why we made sure we put the patch in the same directory as the original zipped sources.

Now, time to make the donuts, er, software:

cd xemacs-21.5.23/carbon

And now, the waiting. The hardest part, if we are to believe Tom Petty. This will take a while. On my 1.8 GHz iMac G5 (1.5GB RAM), it took around 7 minutes or so; it would probably take twice as long on my old G4 Powerbook.

When it’s done, you’ll have a shiny new in the current directory (carbon). Unfortunately, your shiny new doesn’t do a whole lot- none of the lisp packages that provide a significant portion of XEmacs’ features are included in the source code package. Instead, you’ll need to download them separately. The easiest way is to download the “xemacs-sumo” package, which contains all of the official packages in a single download. For multibyte character set support, you’ll also want the “xemacs-mule-sumo” package (mule is the emacs/xemacs multibyte subsystem).

From the download page, find the link to download Official Packages, and select a mirror. You’ll end up in an FTP directory, as before. Download the following packages into the same directory as your other downloads:


The packages can live a couple of different location, but I prefer to keep the packages directly inside the application bundle. To unpack the tarballs to the right place run the following (remember to change the path if your downloads aren’t in ~/ext):

cd ~/ext/xemacs-21.5.23/carbon/
tar xzf ~/ext/xemacs-sumo.tar.gz 
tar xzf ~/ext/xemacs-mule-sumo.tar.gz

Your is now fully loaded. To move this to your Application directory, you can drag-n-drop in Finder, or just:

cd ~/ext/xemacs-21.5.23/carbon
mv /Applications/

Just double click the teddybear icon to launch. If you want to see it run in a terminal window, use this (assuming you moved it to your Applications folder):

/Applications/ -nw

As additional (Carbon) patches against the same base version of xemacs are released, you can download and apply them in the same manner, so keep the directory around. Remember to re-build the app (and copy it to Applications) after each patching. If a new patch-bundle is release against a newer XEmacs version, just dump the old source code directory and start over from the top.

Tech Support Soundbite

Actual diagnosis from help desk staff member, responding to my report of recurring system lockups:

“You’ve got bulging capacitors.”

Not so loud, pal. That’s how rumors get started.

Pocketful of Links

Just as afternoon shopping leads to a pocketful of change, a busy day’s webrowsing leads to many new shinys in my bookmarks. Here’s a handful I’ll toss on the dresser at the end of the day.

  • Rivinus Photography Gallery – Regular readers have seen Sean’s work before. He’s recently made the move to full-time photographer, which is fantastic. His gallery includes many samples of his fine art photography, including some fascinating macro work- M.C. Escher fans be sure to look for his piece “Stacked Nuts.”

  • The Annotated XML Spec – If you’re an XML geek (or just play one at work), here’s a chance to read over the shoulder with one of the fathers of XML, Tim Bray.

  • Al Gore: “Restoring the Rule of Law” – Text of a speech to the Liberty Coalition on Jan 16, 2006. I’ve never discussed politics on this site- mine or anyone else’s. This time I’ll make an exception. I consider myself a conservative. I never voted for Al Gore. I think this essay is spot on, and everyone should read this and actually think about it. [via douglas.nerad].

  • Life in Text Mode – Because GUIs are for sissies. [via 0xDECAFBAD]

  • Do-it-Yourself .Mac – If it was easy, everyone would do it.

  • The iRex Iliad – Not-yet-to-market eBook reader that uses the same eInk technology as the Sony Librié, but supports a number of non-proprietary document types. I’ll be watching to see how it’s priced; I’ve always wanted a device with that display.

iPhoto ’06 Drive-by Review

Since I was at the mall anyway Staurday night getting new cell phones & service (from Cingular- but that’s another post), I stopped by the Apple Store to check the new Intel iMacs. Needing something to use to judge speed, I fired up iPhoto and proceeded to zoom and scroll about. Snappy. Snappy enough that I had to attribute at least part of the performance to iPhoto ’06. My iPhoto ’05 install, containing only around 3700 of the rated 25,000 picture capacity, had been so prone to beachballitis as to be nearly unusable. Although I was a bit leary that my year-old G5 wouldn’t perform as well as the cooler-than-thou CoreDuo chunk o’ geek-porn I tried at the store, I went ahead and dropped the 79.00 USD to pick up iLife ’06.

In a nutshell: worth every cent- and that’s just based on 10 minutes of playing with iPhoto ’06. So. Much. Faster. No really- it is. I can actually use iPhoto again without force-quitting it every 10 minutes. And the “Scroll Guide”, a see-through pop-up bezel that shows the Month/Year as you scroll, is a nice touch. Rotating photos in gallery mode is much faster as well- nearly instantaneous, even with a dozen pictures selected.

I’ll try to post a few words about the other iApps when I fire them up (except iWeb… don’t think I’ll need that).