tcl – choirs of singing angels

seriously, I have not had this much of a eureka moment since I ran my first rails application.

TCL is an ancient, Lisp-inspired, unix based scripting language – what’s so exciting about that?! Let me tell you…

TCLKIT is a redistributable binary version of TCL/TK, prepackaged for Windows, Mac, Linux and Solaris on multiple architectures (and, if you can find a platform they haven’t built it for yet, you can always download the code and build it yourself :)). A quick test of mine has shown a (very basic) GUI prompt is possible on Windows 7 64-bit, Windows XP 32-bit, Mac OS X (10.6), openSUSE 64-bit (gnome) and ubuntu 32-bit (kde), all using ONLY the appropriate TCLKIT binary and the following three lines of code:

package require Tk
label .hello -text "Hello World"
pack .hello

More importantly, it also has a kickass interface for catching stdout from preinstalled command line utilities, namely:

set output [exec ls -la /]

will do exactly what it looks like – run the shell command and store the stdout in a variable called output. (which you have to call $output for the rest of the script.)

That is all for now, more posts if I find anything else awesome (or terrible) about this way of writing short cross platform scripts to wrap command line calls in string parsing complex logic, and occasionally firing simple dialogs.


.NET command line compiler – embedding .ico files as resources

Clearly C# / .NET is missing all the purity of a proper programming language – when has any developer sat down with an editor and a terminal and made something with it?

Well, I am currently trying (it’s an open source app and I want it to build from a batch file without needing a proprietory IDE) and it’s hard.

The particular thing that irritated me today was embedding an ICO file into an assembly as a resource. I tried plain old
csc /resource:subdir/file.ico
I tried
resgen subdir/file.ico subdir/file.ico.resource

BUT NO, you can NOT has cheeseburger (said the compiler to the lolcat). I finally solved the issue by realising the following:

  • you need to embed the item at compile time
  • you need to extract it at runtime using an identifier
  • you need to specify the identifier when you embed at compile time.

Great Scott!! so, in build.bat:

csc [snip] /res:subdir/file.ico,app.file.ico [snip]

This embeds the resource with identifier "app.file.ico". In your application when you want to create an Icon instance from the file:

Icon i = new Icon(this.GetType().Assembly

There didn’t seem to be a clear answer on the subject on Google, so here you go internets! ­čÖé

(by the way this is using .NET 2.0, the standard SDK.)

How to make ubuntu SO fast

So, I am running Ubuntu netbook remix, lucid lynx, on my Eee PC 1005HA (Seashell, black). It runs pretty fast, but I’m always looking for that extra bit of juice, especially given the “mere” single core processor and 1GB RAM it makes do with, compared to my Desktop which runs win 7 64-bit on an AMD64x2 with 8GB.

I originally went down this route to get my webcam working better – at the moment it can only do stills but not video – but the result was so much faster that I would recommend this to anyone with a sluggish netbook and a 24 hour period where they don’t need it.

Building a Kernel is EASY on Ubuntu

Your first reference is Ubuntu’s own docs on building a kernel. This link is the dependency installation, and this one will take you straight to the relevant paragraphs – but don’t go there yet! Ubuntu makes this process much easier than it is traditionally (anyone who’s ever tried Gentoo will know what I mean). You proceed as follows:

1: Install dependencies

You’ll be needing a few things before you can build C code, like GCC, Make, and various other developer utilities. Copy and paste the apt-get commands for doing this from the docs above before you start (you can paste in a terminal using Ctrl+Shift+V). Make sure you also install the ncurses libraries from the second set of instructions too.

2: Download the Kernel source code

This need not be scary, and while the link above recommends grabbing the latest source from their git server, this is almost certainly a rubbish idea (unless you are interested in writing some low low low level C code as a developer…) the way to do it is to install a package using aptitude, which will give you a zip of the source (specifically a “BZIP2”) so that you don’t need to install versioning tools (like git-core) just to grab a file.

3: Extract the source

Now DON’T do this in the place where it has been dropped for you. You need to be root (sudo) and there is NO point building a kernel as root; you can build as an ordinary user (and you’re much less likely to break everything in the process too.) Make yourself a directory in your home folder (cd ~) called src (mkdir ~/src), and extract the zip into there, just like it says to on the link above.

4: Configuration

DON’T grab the current configuration, the script may or may not pick up everything you need, just cd straight in and “make menuconfig”. If you want to go hunting for a particular driver, feel free (although basically everything is in there by default.) The thing to do to get a massive speed increase is to head into

Processor type and features —>
  Processor family (something) —>

and find the Intel Atom option (or whatever your processor is). This will optimise all your OSs most central and low level libraries for your processor specifically, which will make the whole thing MASSIVELY faster to run.

5: Build into a package (the step that takes about 24 hours on a wee netbook)

Normally, you’d build Linux kernels into a bzImage and place it in your /boot folder manually, but on Ubuntu there is a better way with many fewer pitfalls. What you are doing is building an installable .deb package that you can install in a single command later, when you are ready.

Run the command starting “fakeroot make-kpkg” from the docs, making sure you put a nice recognisable string in for the image name, and go to sleep. when you get up, check for errors and go to work (restart if you’ve hit an error). When you get home, you should have a pair of debs (linux-image-something and linux-headers-something) sitting in that ~/src folder you made. Install as the docs say, using dpkg -i.

5a: They had to include one tricky step…

You will now hit a horrid error if you try and boot your shiny new kernel – you haven’t put a RAMFS image (with the separate modules) in the same folder. (apparently this “feature” is new for lucid – YAY!) so run the two cp commands on the docs to put an appropriate script in /etc/default/kernel/ .

this is apparently the bit they didn’t want you to know — to run the install script manually, you need to supply two arguments. The first is the bit after the vmlinuz- in your kernel name (“ls /boot” will let you find it, Ctrl+Shift+C to copy from terminal,) and the second is the path to that kernel file. The script doesn’t print any error messages if you don’t specify these arguments, it just fails silently, so take care!

6: Configuring your bootloader

Run sudo update-grub, and note the position in the ordering of your vmlinuz. (you should also see an initramfs file with a similar suffix, if you don’t, try running the undocumented script again, properly). use sudo gedit /etc/default/grub to change the GRUB_DEFAULT=0 line to a different number to make your new kernel the default. The images will appear in the order update-grub spat them out, and each of the Ubuntu ones at the top will have 2 entries, one for it and one for the failsafe mode version. And, of course, you are counting from 0 not 1.

run sudo update-grub one more time to commit that change, and then restart and see what you get! If you get an error (it will almost certainly be some form of Kernel Panic, which means the one you tried to boot is broken,) you can always use the old one to see what went wrong – just select it from the boot menu before the count down hits 0.

That’s all folks!

When you get kernel-updates you’ll have to recompile again, alas, but this is what we geeks put up with to have laptops that outperform everyone else’s by a mile.

Let me know if you have a problem with this SPECIFIC method (which worked for me) and I’ll see if I can help, but as Ubuntu say this is an advanced option and help is pretty sparse, so make sure you understand enough of what I’ve said to undo it! or you’ll need to reinstall from scratch….

LaTeX and fonts

I recently came across the Liberation fonts (serif, sans, mono) which I rather like. I wanted to use the serif version in my LaTeX documents, but this seemed complicated (just google “latex fonts” and you’ll see what I mean.) The solution I found was on┬áthis web site, but I am always suspicious of this stuff whenever I see it as it is never as simple as it looks.

In the event, however, it seems to have been relatively painless to deal with. If you are on Windows and using MiKTeX (the standard 1-click installer for LaTeX on that platform) you already have both XeLaTeX and the fontspec package installed, allowing you to use any truetype font in your document.


Activates the appropriate module, and

\setmainfont{Liberation Serif}

firstly compensates for a sizing error on truetype fonts and secondly sets the main font. Compile and see your new shiny wonderfulness!

You can also mess with the other default fonts like so:

\setsansfont{Liberation Sans}
\setmonofont[SmallCapsFont={Liberation Mono}]{Liberation Mono}

or substitute in your own font names as required.

Getting an HTML webpage to centre in all browsers

This is something I’ve got cross about faaaar too many times, so here is a simple explanation, for my reference and yours.

1) IE (being completely pants) requires that you text-align: center; the body element. You can do this in CSS:

body {
text-align: center; /* align for IE */

…or in the tag itself:
<body style="margin:0px;text-align:center;">

2) Next, you need to undo that so that your text is left-aligned and actually centre your main wrapper div (using auto margins), as follows (again in a stylesheet or in the HTML itself):

div#maindiv {
margin:0 auto; /* align for good browsers */
text-align: left; /* counter the body center */
width:1000px; /* or some other value */

I like making my main div as thin as possible for the content, specified in pixels, so that the page works seamlessly on any size screen above that value. At the moment I tend to use a value around 800; wide enough for a sidebar and content, keeping up the good old LaTeX tradition of only having a thin column of text for readability, but that’s just a personal preference.

The choice of netbook OS…

So, having just ordered my Eee PC 1005HA (black), I am wondering what I should load onto it’s tidy 160GB hard disk. I will be using it for blogging, web browsing, writing (in LaTeX) and email, in particular while on the move (train carriages, the bus, etc,) so it need to be an OS with established credentials in these areas.

It comes with windows XP home, which I will shrink down to 20GB and leave there for emergency “this needs to be done in windows” situations (sad, but unfortunately required…) but XP simply fails the LaTeX test without any further questions.

So, where from there? Well, Chrome OS isn’t quite ready yet (and there is no way my primary OS is going to be beta – this thing has to be stable), so I had a look at Ubuntu Netbook Remix. I like the idea of window switching being relegated to a corner of the screen (given that you are only ever going to have 3 or 4 things open at once, tops,) and it comes with special Eee PC hacks (woo!), and of course, being Ubuntu underneath, it has access to all of Donald Knuth’s finest typesetting packages.

…and it looks shiny…

We will see when it arrives whether or not it is speedy like the wind or it sucks. If the worst comes to the worst I can always fall back on debian or gentoo with XFCE, and have my ultra optimised little beast ready with a few hours of painful command line wrangling. I think that openSUSE would be overkill (installed size of like 4 GB usually) for such a petite laptop of awesome. I will of course install with seperate / and home partitions, just so I can switch OS without the need for backups and other nonsense (loyalty to a particular flavour of Linux?! Never!)

After it’s arrival (being cheap that could be any time between now and this time next week, as I refuse to pay postage where possible out of principal…) and my successful installation I will blog again form the device itself with instructions for the intrepid netbook explorers among you, who wish to follow in my┬ájungle-like┬ápath.

XML Serialisation in ActionScript 3

XML Serialisation – a nest of rubbishness no more.

In my experience, code to do this takes the form of massive source files devoted solely to mechanically copying properties out of one type of entity into another, which needed to be updated every time you tweaked the data model. blergh.

As such, I have written an ActionScript 3 helper library (h2j9k.XMLSerialiser) which dynamically serialises any in-memory object, and can de-serialise it back again into the same state from that XML (as long as all the important data is held in public fields or read-write properties, obviously). It preserves types, and includes child objects inline, and requires no configuration whatsoever.

I’m hosting the project at my site for open source code, so why not go take a look? It is MIT licensed so you can use it in your code at work…although it would be nice if you credited it back to