A Django site.
July 1, 2009

Byron Clark
byronc
byronc bits
» gnome-screensaver and alternative window managers

I've been using gnome-screensaver with awesome for a while without any problems. Unfortunately that all came to an end when GNOME 2.26 hit Debian Sid last week. Just so that no-one else has to dig for this, gnome-screensaver now uses gnome-session to determine idle time. gnome-screensaver will run without gnome-session, but the screensaver and locking mechanism will never kick in. Fortunately, there is an easy fix. I changed my

gnome-power-manager &

line to

gnome-session &

in ~/.xsession and everything works now.

May 21, 2009

Byron Clark
byronc
byronc bits
» pcapy

So, pcapy is a great, simple library to interface with libpcap. It works well for reading live packets and dump files. There's only one problem: there is no way to modify or create a struct pcap_header from Python code. So there's really no way to add data to the packets and add them to a dump file.

May 20, 2009

Byron Clark
byronc
byronc bits
» View a Certificate

Yet another command I'll never remember unless I write it down. Here's how to view a certificate. In my case I was viewing a certificate that Mutt had stored:

openssl x509 -in .mutt_certs -noout -text

April 1, 2009

Byron Clark
byronc
byronc bits
» _hg_ps1()

In honor of the decision to move Python development to Mercurial, I decided to write something helpful. I've found __git_ps1() to be incredibly useful, so, here's my take on _hg_ps1():

_hg_root()
{
    local basedir=$(pwd)

    while [ '/' != "${basedir}" ]; do
        if [ -d "${basedir}/.hg" ]; then
            break;
        fi

        basedir="$(readlink -f "${basedir}/..")"
    done

    [ '/' == "${basedir}" ] || echo -n "${basedir}"
}

_hg_ps1()
{
    local g=$(_hg_root)
    if [ -n "${g}" ]; then
        local branch

        branch=$(hg branch)
        if [ -f "${g}/.hg/bookmarks.current" ]; then
            branch="$(< "${g}/.hg/bookmarks.current"):${branch}"
        fi

        if [ -n "${1-}" ]; then
            printf "$1" "${branch}"
        else
            printf " (%s)" "${branch}"
        fi
    fi
}

Yes, I know that the hg root command does the same thing as my _hg_root(), but it felt slow running mercurial before printing each prompt. The only thing that's missing is showing when merges are in progress. I'll try to add that the next time I need to do a manual merge.

Update: It seems that some of my repositories don't have a .hg/branch file so I'm calling hg branch for that info. Also, the readlink line in _hg_root() did not properly handle spaces.

March 11, 2009

Byron Clark
byronc
byronc bits
» Saving ViM Macros

I use ViM macros (aka complex-repeat) all the time. Occasionally, I find that it would be nice to reuse one of my macros across editing sessions. Turns out it's pretty easy to save the macro. Just add a line like the following to a file that gets sourced when your file is opened by ViM (This example is from ~/.vim/ftplugin/rst.vim).

let @h = "yypVr"

Now, whenever I open a ReStructured Text file, I can hit @h and my macro will run.

March 9, 2009

Byron Clark
byronc
byronc bits
» Sparse Files and tar

GNU tar provides the -S option to efficiently handle sparse files. Of course it only works if you create the tarball with the -S option. That is all.

March 5, 2009

Byron Clark
byronc
byronc bits
» More Useful ViM Tags

Frequently, I find myself writing C code that requires the use of struct ifreq. There are plenty of fields there and of course there are other structures that I never remember. I could always look in the headers, but it ends up being a good amount of digging before I find the real definition and all its accompanying pieces. So, I decided to let ctags and ViM do the work for me. I created ~/bin/update_local_tags with the following contents:

#!/bin/sh
[ -d ~/.localtags ] || mkdir ~/.localtags
ctags -f ~/.localtags/usrinclude.ctags --exclude=vector\*.hpp -R /usr/include >/dev/null 2>&1

Then I created ~/.vim/ftplugin/c.vim with the following contents:

setlocal tags+=$HOME/.localtags/usrinclude.ctags

For good measure I copied c.vim to cpp.vim so it would be loaded for C++ file types also.

Now I just have to run update_local_tags to generate a tag file for everything in /usr/include and ViM automatically includes that list whenever I edit a C or C++ file. Updating the tags file is still a manual process, I should probably attempt to hook it into apt. Observant readers will have noticed that I exclude vector*.hpp when generating the tags. Boost includes a few generated header files that match this pattern and swell the tag file to almost 750 MiB.

October 7, 2008

Byron Clark
byronc
byronc bits
» application/vnd.fdf and pdftk

So, if a webserver ever serves you a fdf file, you can view the resulting form with evince or your favorite pdf viewer. Just check the beginning of the fdf file to see which form the fields should be applied to and then run

pdftk form.pdf fill_form my.fdf output filledform.pdf

August 15, 2008

Byron Clark
byronc
byronc bits
» Mercurial Presentation

I'm presenting on Mercurial at the Utah Python Users Group tonight. Probably not as useful without the demos, but here are my slides: mercurial.pdf. Sadly, the slides really just seem to be my notes on a pretty background.

August 13, 2008

Byron Clark
byronc
byronc bits
» Using the verbatim Environment and listings Package with Beamer

I finally figured out how to make the verbatim environment work with Beamer. Beamer can't handle the following code:

\begin{frame}{A Title}
    \begin{verbatim}
    Don't mess with my text.
    \end{verbatim}
\end{frame}

To make it work, you need to include the fragile option for the frame. Example:

\begin{frame}[fragile]{A Title}
    \begin{verbatim}
    Don't mess with my text.
    \end{verbatim}
\end{frame}

If you use the listings package, the same trick works for the lstlistings environment.

July 4, 2008

Byron Clark
byronc
byronc bits
» VirtualBox OSE Additions 1.6.2

Better late than never, right? I finally updated the VirtualBox OSE Guest Additions for Windows to the 1.6.2 release last night. Nothing too exciting, although the installer actually supports upgrades now and the tray icon shipped with VirtualBox looks better.

March 28, 2008

Byron Clark
byronc
byronc bits
» The Vimperator Strikes Back

I don't know why I put off trying it for so long, but Vimperator is incredibly cool. Now I really can drive almost everything I use with the keyboard, and with Vi-like keybindings to boot.

February 23, 2008

Byron Clark
byronc
byronc bits
» Utah Sales Tax Information

It seems to me that sales tax in Utah used to be 6.25%. Then an extra 1% was added to prepared food. Then other small chunks were added in. If you've ever wanted to see the sales tax breakdown for where you live, there's a helpful chart on the Utah state website.

April 12, 2007

Byron Clark
byronc
byronc bits
» VirtualBox OSE Additions Installer

If you've been running Windows in VirtualBox OSE you've probably noticed that it doesn't include a prebuilt copy of the guest additions. I've been working on a project so that you won't have to setup a build environment on a Windows box and then install the drivers by hand on your virtual machine. So, here it is, a VirtualBox OSE Windows Guest Additions Installer. It should work on Windows 2000, XP, and Vista, but has only been tested on XP.

March 17, 2007

Byron Clark
byronc
byronc bits
» Editing ViM Macros

At the BYU UUG meeting this week, Peter mentioned a great ViM trick that I've never seen before. Here's my feeble attempt to document it.

Because ViM macros are stored in registers they can be edited. This means that if you create a long macro and then realize that you forgot to send the cursor to the beginning of the line before finishing the recording, you don't have to create the entire macro again, you can just add the motion command. You use it like this:

  1. Start recording your macro by typing q and then the single character ([0-9a-zA-Z"] are allowed). The single character is the register your macro will be stored in.
  2. Enter the commands you want included in the macro.
  3. Type q to finish recording the macro.
  4. At this point you could run the macro by using @ and the register name.
  5. To modify the macro, move to a blank line and type "Rp replacing R with the register name of your macro. This will paste all the commands in your macro to the current line.
  6. After making the changes you need, save the macro by typing 0"Ry$ replacing R with the register name you want to use for the macro. The register name does not have to be the same as the original.

February 14, 2007

Byron Clark
byronc
byronc bits
» Python TUI Programming

I've heard rumblings about Urwid occasionally; today I actually had a chance to use it. For those who haven't seen it before, Urwid is a pure Python library for console user interfaces. It can uses curses as the backend or speak directly to the terminal emulator. The library isn't too complex and as long as you can pick colors better than I can the TUIs look pretty nice. Check it out the next time you don't want to write a GUI.

August 14, 2008

Byron Clark
byronc
byronc bits
» Mercurial Presentation

I'm presenting on Mercurial at the Utah Python Users Group tonight. Probably not as useful without the demos, but here are my slides: mercurial.pdf. Sadly, the slides really just seem to be my notes on a pretty background.

August 13, 2008

Byron Clark
byronc
byronc bits
» Using the verbatim Environment and listings Package with Beamer

I finally figured out how to make the verbatim environment work with Beamer. Beamer can't handle the following code:

\begin{frame}{A Title}
    \begin{verbatim}
    Don't mess with my text.
    \end{verbatim}
\end{frame}

To make it work, you need to include the fragile option for the frame. Example:

\begin{frame}[fragile]{A Title}
    \begin{verbatim}
    Don't mess with my text.
    \end{verbatim}
\end{frame}

If you use the listings package, the same trick works for the lstlistings environment.

July 4, 2008

Byron Clark
byronc
byronc bits
» VirtualBox OSE Additions 1.6.2

Better late than never, right? I finally updated the VirtualBox OSE Guest Additions for Windows to the 1.6.2 release last night. Nothing too exciting, although the installer actually supports upgrades now and the tray icon shipped with VirtualBox looks better.

March 28, 2008

Byron Clark
byronc
byronc bits
» The Vimperator Strikes Back

I don't know why I put off trying it for so long, but Vimperator is incredibly cool. Now I really can drive almost everything I use with the keyboard, and with Vi-like keybindings to boot.

February 23, 2008

Byron Clark
byronc
byronc bits
» Utah Sales Tax Information

It seems to me that sales tax in Utah used to be 6.25%. Then an extra 1% was added to prepared food. Then other small chunks were added in. If you've ever wanted to see the sales tax breakdown for where you live, there's a helpful chart on the Utah state website.

April 12, 2007

Byron Clark
byronc
byronc bits
» VirtualBox OSE Additions Installer

If you've been running Windows in VirtualBox OSE you've probably noticed that it doesn't include a prebuilt copy of the guest additions. I've been working on a project so that you won't have to setup a build environment on a Windows box and then install the drivers by hand on your virtual machine. So, here it is, a VirtualBox OSE Windows Guest Additions Installer. It should work on Windows 2000, XP, and Vista, but has only been tested on XP.

March 17, 2007

Byron Clark
byronc
byronc bits
» Editing ViM Macros

At the BYU UUG meeting this week, Peter mentioned a great ViM trick that I've never seen before. Here's my feeble attempt to document it.

Because ViM macros are stored in registers they can be edited. This means that if you create a long macro and then realize that you forgot to send the cursor to the beginning of the line before finishing the recording, you don't have to create the entire macro again, you can just add the motion command. You use it like this:

  1. Start recording your macro by typing q and then the single character ([0-9a-zA-Z"] are allowed). The single character is the register your macro will be stored in.
  2. Enter the commands you want included in the macro.
  3. Type q to finish recording the macro.
  4. At this point you could run the macro by using @ and the register name.
  5. To modify the macro, move to a blank line and type "Rp replacing R with the register name of your macro. This will paste all the commands in your macro to the current line.
  6. After making the changes you need, save the macro by typing 0"Ry$ replacing R with the register name you want to use for the macro. The register name does not have to be the same as the original.

February 14, 2007

Byron Clark
byronc
byronc bits
» Python TUI Programming

I've heard rumblings about Urwid occasionally; today I actually had a chance to use it. For those who haven't seen it before, Urwid is a pure Python library for console user interfaces. It can uses curses as the backend or speak directly to the terminal emulator. The library isn't too complex and as long as you can pick colors better than I can the TUIs look pretty nice. Check it out the next time you don't want to write a GUI.

April 13, 2007

Byron Clark
byronc
byronc bits
» VirtualBox OSE Additions Installer

If you’ve been running Windows in VirtualBox OSE you’ve probably noticed that it doesn’t include a prebuilt copy of the guest additions. I’ve been working on a project so that you won’t have to setup a build environment on a Windows box and then install the drivers by hand on your virtual machine. So, here it is, a VirtualBox OSE Windows Guest Additions Installer. It should work on Windows 2000, XP, and Vista, but has only been tested on XP.

March 17, 2007

Byron Clark
byronc
byronc bits
» Editing ViM Macros

At the BYU UUG meeting this week, Peter mentioned a great ViM trick that I’ve never seen before. Here’s my feeble attempt to document it.

Because ViM macros are stored in registers they can be edited. This means that if you create a long macro and then realize that you forgot to send the cursor to the beginning of the line before finishing the recording, you don’t have to create the entire macro again, you can just add the motion command. You use it like this:

  1. Start recording your macro by typing q and then the single character ([0-9a-zA-Z"] are allowed). The single character is the register your macro will be stored in.
  2. Enter the commands you want included in the macro.
  3. Type q to finish recording the macro.
  4. At this point you could run the macro by using @ and the register name.
  5. To modify the macro, move to a blank line and type "Rp replacing R with the register name of your macro. This will paste all the commands in your macro to the current line.
  6. After making the changes you need, save the macro by typing 0"Ry$ replacing R with the register name you want to use for the macro. The register name does not have to be the same as the original.

February 15, 2007

Byron Clark
byronc
byronc bits
» Python TUI Programming

I’ve heard rumblings about Urwid occasionally; today I actually had a chance to use it. For those who haven’t seen it before, Urwid is a pure Python library for console user interfaces. It can uses curses as the backend or speak directly to the terminal emulator. The library isn’t too complex and as long as you can pick colors better than I can the TUIs look pretty nice. Check it out the next time you don’t want to write a GUI.

February 8, 2007

Byron Clark
byronc
byronc bits
» Digital Design for Cheapskates

I’m usually a software guy, but I find myself doing more and more digital design these days. One common question I hear get is “What’s a good way for a programmer to get started with digital design?” The good news is that if you get to use an HDL like VHDL or verilog, digital design can be somewhat similar to writing software. Actually writing the HDL code will be the subject of later posts; the point of this post is to help you set up a fairly simple digital design environment using open source software. Please note that this is an area where there are very high quality proprietary tools that you may want to use.

First, we need to decide whether to use VHDL or verilog. These languages look different but the concepts and constructs are very similar so it’s mostly a matter of taste and the tools you have available. In the open source world we have Icarus Verilog for verilog and gHDL for VHDL. After checking the package repository on my Debian box and seeing that gHDL probably isn’t going to make it into the next release it would appear that verilog is the best choice for now. Besides, Sun released the design for the OpenSPARC-T1 as verilog code. We’ll also need a waveform viewer to view simulation results. GTKWave seems to work fairly well with Icarus Verilog.

After installing both of those packages, it’s time to write some code. I wrote a simple D Flip-Flop for demonstration. Since wordpress will probably eat the code if I include it in the post you can download the module and testbench with these links: module: dff.v, testbench: dff_tb.v. Please don’t mock my verilog code too much, I haven’t written any before today.

After downloading the two files you can build them with Icarus Verilog using the command: iverilog -odff dff.v dff_tb.v.

You can now run the simulation:

$ vvp dff
VCD info: dumpfile dff.vcd opened for output.
d=0, q=x
d=0, q=0
d=1, q=0
d=1, q=1
d=0, q=1
d=0, q=0

After running the simulation, you can use GTKWave to examine the internal state:

  1. gtkwave dff.vcd
  2. Add the waves you want to see by clicking on Search->Signal Search Regexp
  3. Use the signal search box to select all the signals in the d_flip_flop instance.
    GTKWave Signal Search Window
  4. Examine the waveform to make sure the module worked properly.
    D Flip Flop Waveform

January 21, 2007

Byron Clark
byronc
byronc bits
» Getting Started with VirtualBox

Earlier this week, VirtualBox was released under the GPL (with one piece under the LGPL). I had a chance to try it out today and was very pleasantly surprised. There are no official tarballs, so I had to use subversion to check out a copy of the code. I followed the Linux host build instructions and had a working install in under 25 minutes. I had been worried that the build would take forever because they include a portion of the Mozilla sources, but my fears were unfounded. Note that your build times should be much better, my build machine is a Pentium 3 1.13 GHz laptop.

The GUI looked good and finding what I needed was easy enough. Creating and running a virtual machine was simple. I immediately installed Windows XP and didn’t run into a single problem. The virtual machine feels more than fast enough for my needs.

The only thing I needed that was not included in the open source edition was a pre-built version of the Windows Guest Addition. I mostly wanted the tool to resize the virtual machine display to the current window size. I’ll be working on setting up the build environment for that in the next few days. It’s too bad they can’t be cross compiled but understandable because the required tools include the Windows Driver Development Kit.

Thank you, InnoTek, for a great tool.

January 19, 2007

Byron Clark
byronc
byronc bits
» Happy Iceweasel Day

In honor of Iceweasel landing in Debian etch it’s time for a list of the iceweasel/firefox extensions that I just can’t live without.

  1. Adblock Plus. That one should be pretty obvious.
  2. Download Statusbar. The download manager window gets on my nerves, especially because ion3 and non-transient windows like the download manager become full frame windows. Still, I like to see progress on my downloads.
  3. Fission. Shows page loading progress in the address bar, somewhat like Safari. For those of us stuck on 1024×768 (or smaller) monitors it means some saved screen real estate because the status bar can go away.

January 18, 2007

Byron Clark
byronc
byronc bits
» How do I do that in a C Program?

Yesterday, Topher asked the plug mailing list how to get your IP address from C. He got the answer on the mailing list, but I wanted to add a little bit about how to find answers to questions like this.

When I want to figure out how to get or set some bit of system information programmatically it always helps to try to rephrase the question. Instead of asking “How do I do foo programmatically?” I like to ask “Is there a program that does foo?” If there is a program that has similar functionality, here are three helps to find out how it works, in order of increasing difficulty.

  1. strace
  2. browse manual pages in section 2
  3. use the source

strace

strace will show all the system calls a program makes, along with many of the arguments passed to them.

If I wanted to programmatically find the target of a symbolic link and didn’t know the system call to use, here is how I would use strace to find out.

byron@thinktank:~$ touch foo
byron@thinktank:~$ ln -s foo bar
byron@thinktank:~$ ls -l bar
lrwxrwxrwx 1 byron byron 3 2007-01-17 21:56 bar -> foo
byron@thinktank:~$ strace -o ls.strace ls -l bar
lrwxrwxrwx 1 byron byron 3 2007-01-17 21:56 bar -> foo

I start by creating a symbolic link bar that points to foo. I then find a utility that has the functionality I want, in this case ls finds the target of a symbolic link and prints it. The next step is to run strace, the -o option sends the output to a file instead of stderr. The next step is to examine the ls.strace file created by strace. The file is a little long, 204 lines on my system so I won’t post it all here. The first part of most strace output is a series of calls to access, open, and some form of mmap; this is dynamic linking in action and, unless this is the part of the trace you cared about, can be skipped. Because we told ls to only list file bar we can search for that filename to see if it is ever used; it is, and only on the following lines:

lstat64("bar", {st_mode=S_IFLNK|0777, st_size=3, ...}) = 0
readlink("bar", "foo", 4) = 3

Checking the man pages, it would appear that readlink(2) is the system call we need to use to find the target of a symbolic link.

That’s a simple example, but the same principles apply for any application, just be prepared to wade through a lot of system calls.

Manual pages in section 2

You can greatly reduce the time spent reading strace dumps if you are familiar with the system calls in manual page section 2. All it takes is ‘ls /usr/share/man/man2‘ and some reading. Of particular interest is the ioctl_list(2) manual page. Operations that don’t map cleanly to a read or write are often implemented using ioctl(2). While ioctl_list(2) doesn’t document how to use all of the ioctls it does at least list the codes and the arguments they expect. For real documentation your best bet is to grab the Linux kernel source and search for the ioctl you want to use. Of special note are the network ioctls, they are always performed on a socket and are of the form SIOCG.* for getting information and SIOCS.* for setting information.

Use the source

If the other methods fail and you have the source to the application, don’t be afraid to use it. I list this method last because I can usually find what I need with a quick strace and sometimes it takes quite a bit longer to get into the source for an application. Still, the source is the ultimate reference and isn’t hard to read.

A better example

If you have a better example for me to strace and dissect, let me know.

January 1, 2007

Byron Clark
byronc
byronc bits
» My Favorite Wireless Card

I recently found myself in the market for a new wireless card for my linux laptop. Getting a card that worked well with a minimum of hassle was not as obvious as I had hoped, so I thought I should share.

Here’s what I wanted:

  • PCMCIA or CardBus (the laptop doesn’t have a mini PCI port)
  • Supports WPA/WPA2 (mostly depends on the driver)
  • Working driver, not named ndiswrapper, in the mainline kernel

These requirements limited my choices significantly. The only real options for drivers in the mainline kernel that support WPA are the Intel cards and Prism 2/2.5/3 cards. I’ve never seen one of the Intel cards except as mini PCI so I decided to get a Prism based card. The downside is that they only do 802.11b, but the upside is that they use the hostap driver which works beautifully. It even allows using hostapd to turn your laptop into an access point if needed. I consulted the somewhat out of date chipset list at http://www.linux-wlan.org/docs/wlan_adapters.html.gz and eBay and decided on the Linksys WPC11 v2.5.

Once the card arrived, I immediately updated the primary firmware to version 1.1.1 and the station firmware to version 1.8.2. I later tried station firmware version 1.8.4 and experienced serious packet loss that went away on reverting to version 1.8.2.

The card works perfectly and I don’t think I’ve ever been this happy with wireless on linux before.

December 18, 2006

Byron Clark
byronc
byronc bits
» aspell and LaTeX

I write a lot of things in LaTeX and always get annoyed when aspell complains about my LaTeX tags being mispelled. Imagine my surprise when I discovered that aspell has a TeX mode so that it will ignore the tags. 'aspell -c -t <myfile.tex>' invokes the proper magic.

December 16, 2006

Byron Clark
byronc
byronc bits
» orpie

Chances are that if you ever used an HP-48 series calculator you’ve never wanted to use anything else. Well now you can save space in your laptop bag by replacing the calculator with orpie. orpie is a terminal based RPN calculator that will make you curse all the time you wasted trying to use bc, dc, and calc.exe.

ps - The cygwin version works just fine if you’re stuck on Windows.