June 21, 2004

CLI magic: Hack the comment

Author: Joe Barr

Ok, you GUI-potatoes, listen up. I'm not talking about hacking an
RFC (Request for Comment) here. I'm talking about a program called Comment that you can run on your own system. Comment is a free software program released under the BSD license that lets you enter and recall notes from the command line in the blink of an eye -- or, if you type as slowly as I do, in several blinks. So grab a Jolt and get thyself to a console window, we've got some CLI to do.

The first thing you need to do is grab the program's source code. Once you've downloaded and decompressed the tarball, enter the comment.0.1-dev directory and run the make command. When make has finished, su to super-user mode and run code>make install. That's it, you've finished the installation.

What it does

The Comment program takes input from you via the command comment add 'text' and stores it in two places. It inserts the text you've given it in a .comment file in whatever directory you happen to be in when you make the comment, and it also records it in a .comment file in your home directory.

The reasoning behind the double-entry notetaking is to allow you to later retrieve comments on specific subjects. Notes about your bank balance, for example, could be kept in the same directory as your CBTracker files, while a memo to yourself to email Aunt Nadine a specific image from your digital camera would stay in the same directory as your family photos.

There is no man page for comment, but the program gives you a rundown on usage if you type comment all by itself. Let's take a look:


Comment v0.1.1 - by Paul L Daniels - March 16, 2003 - http://www.pldaniels.com/comment
Usage:
comment [--verbose] [--debug]

add : Add a new comment to the present working directory .comment
     database and the ~/.comments database
find|findhome: Look for 'findme' in the ~/.comments database
findhere: Look in the current directory's .comment database
findall: Look both in the current directory and the ~/.comments database

Let's start by adding a comment about the game HetHack. We'll put it in /usr/games, where NetHack lives, like this:


warthawg@linux:/usr/games> comment add 'nethack is hard'

Whoops! Comment doesn't like that! It complains:


libcomment.c:428:COMMENTLIB_file_prefix: ERROR: Unable to open temporary file '/usr/games/.comment.tmp'(Permission denied)

Permission denied? Oh, yeah. That curse of noobies. We didn't ask "mother, may I?" And in fact, we should probably leave the permissions on system directories as they are, rather than start changing them for the sake of our comments database.

Checking later, I learned two things. First, there is a bug in either the code or the usage instructions. As noted above, both find and findhome should search the
.comment file in my home directory, but only findhome actually does. The find option apparently does the same thing as findhere rather than findhome. I also learned that Comment did record the entry in my home directory in spite of the error about trying to write to a directory without permission.

Let's hack this comment

You could take the easy way out, and go into the source code and fix it there by changing the usage printed for the find option to match its behavior. Or, if you want to take a chance, you could change the behavior. If you want to do that, open comment.c in your favorite editor and find line number 134. It reads tmpHOME=0;, and it should read tmpHOME=1;. Make that change and save the file, then rerun make and make install as you did originally.

But that's not as much fun as my original plan, which was to use the alias command to replace two or three of the comment commands. We could replace comment add with note, comment findhere with lookhere, and comment findall with look. That would save keystrokes and make the application quicker and easier to use. To add those aliases, put the following three lines in the .bashrc file in your home directory:


alias note='comment add'
alias lookhere='comment findhere'
alias look='comment findall'

Either way you have hacked the application, because you have modified it to make it work better in some way.

OK, you've gone from noobies to hackers in one short column. That's enough for this week. Go out there and hack the planet.

Click Here!