Dual Boot Magic

I dual boot on two out of three computers but I really only spend less than 1% of the time in Windows. Occasionally I do find the occasion to bring the red headed stepchild out of the basement. As if this weren’t annoying enough I would often reboot, lack the patience to babysit the reboot, and come back just in time to see that I missed the grub menu and now I have to reboot again. Well some time ago I found a solution to this mess. Enter grub-set-default <num>—this nifty command sets the grub entry to load by default for the next boot only. So I count my grub entries and put the proper number in a nifty reboot script. Alas, by the next time I use this script various Ubuntu updates have changed my grub menu such that now I get some old kernel instead of Windows like I wanted. This clearly wouldn’t do, so I fixed it. Now you can do the same.

Save the following to a script, such as ~/bin/windows.

#! /bin/bash
TARGET=windows
MENU=/boot/grub/menu.lst
ENTRY=$[`grep ^title $MENU | grep -n -i $TARGET | cut -d: -f1` - 1]
sudo umount /media/surfer
sudo grub-set-default $ENTRY
sudo reboot

The umount /media/surfer line just unmounts my network media, speeding up the reboot process working around an annoying Ubuntu bug where the shutdown hangs waiting for network drives even as the network is already down, hence no response from the share and a long timeout.

The ENTRY= line is the interesting part. First grep ^title grabs all the grub entries from which we will count the indexes. Next, grep -n $TARGET prints the line number of the entry we are interested in (Windows) along with the line. cut -d: -f1 splits the line on ‘:’ and returns the first field, the number we want to boot. Finally, grep counts from 1, but grub counts from 0, so $[<stuff> - 1] does bash math, wich is like normal math mostly, and hence we have the proper argument to give grub-set-default, et voila.

You can even put an icon in your panel or desktop that will run this script, giving you Windows in one easy click.

24 Dec 02:48 :: 0 comments :: Comment
Tags: linux, computers, annoyances, cool

Mysterious strftime segfault

Say you're working on a big project. Then at some point you call strftime or maybe ctime and what do you get but a nasty and mysterious SEGFAULT?? What's more you are absolutely sure you have no memory leaks, nothing is wrong with the arguments to the call. Then gdb tells you only that the segfault is happening somewhere deep inside the library call. Furthermore, it works on some platforms but not others, indicating again that memory leak you are sure you don't have. So what do you do but spend three hours tracking down your much doubted memory leak? Well hopefully you find this very post and save those three hours. You see, if time.h is included in another source file, you may have forgotten to include it where you use it. The compiler won't blink an eye. The linker doesn't even complain. Yet somehow it all works dandy until you try it on the TA's computer. So long story short, check your includes.

19 Sep 09:49 :: 0 comments :: Comment
Tags: programming, c, troubleshooting, annoyances, computers, linux

Chicken Little?

Edit: I got confused on the dates of my sources, which then used relative dates to refer to the event. It was in fact on Wednesday, 18 Mar 2009, that the Fed's actions took place.

Last ThursdayWednesday, as Glenn Beck calls it, was history. Not good history, no this is bad. Remember Zimbabwe? Ever heard the phrase "Not worth a continental")? Well welcome to the future, hyperinflation.

What happened ThursdayWednesday? $1 trillion dollars, new dollars, were printed, IN ONE DAY! The Federal Reserve "bought" a bunch of IOU notes from the US Treasury, and for it paid $1 trillion, in money it just printed out of thin air. "What?? " you say? Yes, the Fed bought debt the US Government didn't have, using money the Fed didn't have, all so that we can be poorer for it.

On ThursdayWednesday gold shot up over 6% in one hour. Likewise silver shot up over 8% in one hour. This is not because the value of gold and silver went up. The value of the dollar went down, dramatically. If you thought your 401K evaporated with the banking crisis, you ain't seen nothin' yet. Get out of the dollar, and then like me you can welcome the inevitable collapse, and finally a return to sanity!

Bye Bye smbfs

Please, Von, please, next time you set up a samba mount, just use cifs and not smbfs. Save yourself a lot of headache.

14 Apr 23:42 :: 0 comments :: Comment
Tags: computers, linux, annoyances, troubleshooting, reference

Tricking Darcs, or How to Make a Common Branch

Imagine you have a repository. At time t0 someone copies the code from the repository, starts another repository from there, and commences development. You also continue development. At time t you want to pull in the changes the other guy has made.

One approach could be you simply merge other guys code into yours with one big commit calculated from time t0. Now suppose you want to keep other guy’s history from t0 on, or even worse, you want to pull only some changes and you want to push some of your changes to other guy.

This is a true story. It all started with CVS. A webapp was developed, cloned and branched. Now there are 4 of them, mostly the same, but subtly different, and each with it’s own CVS repo. Each one at some point in time is identical to another, but the history behind those points in time are different, thanks to copy and commit cloning.

So after converting the CVS to Darcs, I came up with a way of tricking darcs into treating time t0 in each repository as the same t0, thus enabling cross-pollination, and here I describe that method.

First you find t0, and tag liberally. By liberally I mean twice. I’ll explain why later. Of course you put the same two tags on both repos, both at the same point in time. Then you do a darcs opt --checkpoint on each so you can do a partial get on them.

Now you have to decide which repo you’re going to keep, and which one you are bringing in. You probably want to keep the one with past history, and bring in the one that did copy and commit cloning. Do a darcs get --partial on each repo, keeping track of which is what. I like to use a for the destination, and b for the bad repo.

Edit: The below paragraph is a bit confusing. Keep in mind that to make b think it’s a, you just replace @b@’s guts with those of a, but this only works for the partial at time t0. So we make a snapshot for reference, and then pull in @b@’s downstream stuff (because we can’t do this after b thinks it’s a).

Now go into b/_darcs and we’ll play around with these guts to make it think it’s a, but we want it to have it’s future patches first. So we take a snapshot before pulling the other stuff. I just made a temp directory and did cp -r inv* patches temp, these are the three things in the _darcs directory we care about. Here’s where the liberal tagging comes in, the partial at the later tag only depends on the first tag, so that makes your inventory and patches much simpler. Now you’re ready to pull in all the subsequent patches from the cloned branch.

Once that’s done, you go back into b/_darcs, this time you take anything in temp/patches and temp/inventories and remove the corresponding files from patches and inventories. Then you replace those files with the files from a/_darcs/{inventories,patches}. These two steps I accomplish like so:

ls temp/inventories | while read i; do rm inventories/$i; done

ls temp/patches | while read i; do rm patches/$i; done

for f in inventories patches; do cp ../../a/_darcs/$f/* $f; done

One last step remains. Now you edit inventory and replace that very first patch number, the one that’s a tag, with the one that’s in the inventory file from a. Now b effectively has the exact same past history as a, and has all it’s future patches, and now you can share between your repo and other guy’s repo freely.

One might think this is an uncommon situation, but I can’t help but think it’s more common than I think… Hopefully this guide will save others the headache of trying to wrap their minds around the darcs inner guts, as well as save me some headache should I ever encounter this again (which I’m pretty sure I will, in only a few weeks!).

27 Feb 11:06 :: 0 comments :: Comment
Tags: darcs, programming, annoyances, work, tricks, hacks

Some _Times_ You Just Want _Things_

Working on an AES implementation in ruby, I’ve had many occasion to do:

n.times {|i| some calculations based on i}

I’m sure many of you have used the Integer#times method much to your glee. I’m sure many of you have also had the occasion where you realize that you want those resultant calculations to end up in an array of some sort. At this point you have two options: create an array before hand and << those results in, or change your iterator to (0...n).map {|i| ...}. Both options are severely lacking in taste, IMO. So I came up with a third option. Enter Integer#things.

ary = n.things {|i| ...}

Notice how the syntax here remains consistent with the semantics. Before you wanted to do something n times. Here you want the same, but you want to store the results, you want n results, or n things.

Don’t get me wrong, I love Range and it certainly has it’s place. I would never argue that 26.things { make a letter } is a better approach than ('a'..'z') { just have your letter already }. Like I said, it’s about semantics, do you really want that range or is it more about the number of things? And as we all know deep down, the syntactics should match the semantics wherever possible.

So here is my implementation for Integer#things and I really hope something like this becomes included in future ruby.

@
class Integer
def things &b
b ||= proc {|i| i}
ary = []
times do |i|
ary << yield(i)
end
ary
end
end
@
16 Jan 10:39 :: 0 comments :: Comment
Tags: ruby, annoyances, programming, fun

Rails :memory: revisited

As I was creating a new rails app, I used this previous post of mine to set up the in memory database. Well it’s time for an update. Apparently now the rails config/environments/*.rb are run in the context of Rails::Initializer. So instead of creating that context as in the previous post, you simply do:

<pre> alias initialize_database_old initialize_database def initialize_database *args initialize_database_old load "#{RAILS_ROOT}/db/schema.rb" end</pre>

02 Dec 22:14 :: 0 comments :: Comment
Tags: programming, ruby, annoyances, rails

Rails and SQLite3: Default Values

Here’s another rails fix. This one is for SQLite3 >= 3.3.8. SQLite decided to return sql strings for the default values shown in table definitions. This broke rails, the rails people got mad and won’t introduce a workaround. Well here’s my workaround.

sqlite_fix.rb:

module ActiveRecord
  module ConnectionAdapters
    class SQLiteAdapter < AbstractAdapter
      def columns table_name, name=nil
        table_structure(table_name).map do |field|
          /^'?(.*)'?$/.match field['dflt_value']
          field['dflt_value'] = $1
          SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'] == "0")
        end
      end
    end
  end
end

Include it at the end of environment.rb. That’s it!
10 Aug 16:30 :: 0 comments :: Comment
Tags: programming, annoyances, troubleshooting, sqlite3, ruby, rails

Rails :memory: testing in SqLite

I wanted to get that nifty :memory: db thing working for the test database in Rails. There are a sparse few articles floating around telling you how to do this. All of them, IMHO, are kinda ugly, or at least not-so-elegant. Here’s my answer, which took a lot of digging around in the inner workings of rails to come by.

First, do what everyone else tells you, put this in your config/database.yml:
<pre> test: adapter: sqlite3 database: ":memory:" </pre>
Once you got that, you have to have some way of loading the schema into your memory db every time you run the test. I found that the best place to do this is in config/environments/test.rb. If you have an even better place, please let me know.

At first I thought I could just load "#{RAILS_ROOT}/db/schema.rb" and be done with it. Alas, at this point there is no established connection to the db. So we have to modify things in such a way that this cute little command will get run just after establishing the db connection, but before the db is accessed. I did it like so:
<pre> module Rails class Initializer alias initialize_database_old initialize_database def initialize_database initialize_database_old load "#{RAILS_ROOT}/db/schema.rb" end end end </pre>
Then don’t forget to rake db:schema:dump before running rake test.

There’s probably other things you could do, like use ActiveRecord::Base.establish_connection instead of Initializer.initialize_database.

I break the above code block into lib/memdb.rb and simply require that inside of config/environments/test.rb. I tried to just add a metaclass to the config variable you see in test.rb but that is apparently some kind of impostor, because it didn’t work. Anyway, armed with this knowledge someone could probably make a pretty sane plugin to cleanly bring you :memory: testing.

04 Aug 21:58 :: 0 comments :: Comment
Tags: annoyances, ruby, programming, rails

Acts as Dead?

Been a while… So my blog was broken just now, not sure for how long. Not more than a month I think. The problem was in the acts_as_taggable plugin, which rumor has it is dead. For now I will still use it, and describe the fix in case it also is broken for you.
The problem is in the join table. The geniuses decided to make the join table <tag class><taggable class>. This is backwards from the regular has_and_belongs_to_many convention in activerecord. Somewhere, somehow, this backwardsness was reversed. I had it using it’s default join table before (tags_posts) and it broke as now it’s looking for poststags. So I add an option to the call:

acts_as_taggable :join_table => :tags_posts

I suppose I could have changed the table name, but at least this way it will never (hopefully) break again. I’m not quite sure how this default behavior changed in the first place.

04 Jul 13:25 :: 0 comments :: Comment
Tags: annoyances, blog, rails, programming

Ruby GTK Blocks

As I was exploring gtk in ruby I found myself frustrated with the inability to create nested widgets in equally nested code. Such as:


$main = Gtk::Window.new
$main << Gtk::HBox.new do |b|
b << Gtk::Button.new(“click me”)
b << Gtk::VBox.new do |v|
… etc etc
end
end

No problem, eigenclasses to the rescue! I just add this simple thing (to a lib or just the beginning of my file):


class << Gtk::Widget
def new *args, &block
x = super
yield x if block
x
end
end

That’s all, now you can use good ol’ ruby blocks to make GUI programming less obscene.

07 Nov 01:51 :: 1 comment :: Comment
Tags: programming, annoyances, ruby

MP3 Woes

IMMS was complaining to me that sox couldn’t read mp3 files to alalyze them. I wasn’t about to let this go on too long, I want IMMS to work to it’s full potential. I tried installing liblame0 and liblame-dev, I even tried libmad but all to no avail. Finally I did apt-cache policy sox. The installed version was some studio version from DeMuDi. I thought if any of the available packages would support mp3 it would be the one from demudi, alas this was not the case. I installed the version from ftp.easynet.fr (PLF) and now I have (readonly) support for mp3. Good enough for me.

15 Jul 12:35 :: 2 comments :: Comment
Tags: copyright, linux, debian, annoyances, troubleshooting, music, computers, entertainment

Instant Unicode Gratification

I was trying to look up some unicode characters and google searches for "unicode this" and "unicode that" weren’t getting the kind of results I wanted. So instead I googled for "unicode lookup" and found this.
This site is just so perfect for a firefox quicksearch. So I added "http://www.fileformat.info/info/unicode/char/search.htm?preview=entity&q=%s" as a quicksearch bookmark, with unicode as the keyword. Now I’ll never have to fuddle around looking for unicode characters again.

05 Jul 16:56 :: 0 comments :: Comment
Tags: web, annoyances, computers

Inline DOC

“Wouldn’t it be nice if I could see an inline text version of .doc attachments?” I thought. That wish quickly became reality in this well-ended story.

Finding a sufficiently constrained search term proved difficult, so I resorted to #utah. I soon received an answer from spr (thanks man!) citing wv. I installed it with apt-get install wv. Then I constructed this line for ~/.mailcap:

application/msword; /usr/bin/wvText '%s' /dev/stdout; copiousoutput; desciption=DOC Text; nametemplate=%s.doc

Now fire up mutt and read that darned email-with-a-doc-attachment with nary a care.

15 Jun 18:52 :: 0 comments :: Comment
Tags: debian, computers, mutt, linux, annoyances