cat head > /dev/www From my head to the web

14Jun/150

fish

I started using the Friendly Interactive Shell. Here's a screenshot showing the git branch and error code of the last run program at the command prompt.
fish

Code is on github

Filed under: Uncategorized No Comments
10Jun/150

less is more

'less is more' except when LESS_IS_MORE, then less really is more!

The above is a nerdy joke I made up. Pretty pleased with myself. I'm fairly sure I made it up. Let me know if you've seen it before.

Filed under: Uncategorized No Comments
25Jan/150

5 years

I've been running my server, the one that hosts this webpage, for 5 years. It was running on Centos 5 and I kept telling myself to upgrade it and rebuild the server but kept postponing the task because I knew what a pain it would be to move all the config from apache, postfix, mysql etc on to the new server. Fortunately, for people who have to do this regularly there is Puppet and Saltstack which integrates with the applications like apache and merge their config from the old to the new server. Me - I've been spending the weekend doing this manually. I backed up what I thought I needed from /etc and the wordpress database, rebuilt the server to run Centos 7, discovered more than I wanted to know about systemd, discovered that mysql is now called maria and now wordpress looks like it's up and running. But shit it got me thinking about config management. The approach Puppet takes is fine but I wonder about another approach that allows tighter integration. Anyway this post is more of a test to see if wordpress is working again.

Filed under: Uncategorized No Comments
1May/120

CtrlP

Recently stumbled across this plugin for vim. It’s replaced my use of command-t and fuzzyfinder. CtrlP is able to find files or buffers and let you switch to them in an intuitive manner. It also keeps a list of most recently used files or buffers. For example entering the command :CtrlPMRU brings up the list of MRU buffers.

Filed under: programming, vim No Comments
25Apr/120

Bochs and a Kernel

There’s a basic tutorial here here on how to get started with a kernel. It uses Bochs as 32 bit emulator. However recent versions of Bochs have been updated so that this line in the config file:

romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xf0000

must change to this:

romimage: file=/usr/share/bochs/BIOS-bochs-latest
Filed under: Uncategorized No Comments
12Dec/110

old website

Finally uploaded content of my old website here

Filed under: Uncategorized No Comments
18Sep/110

Expert F# Errata

It’s good when you find errors in text books. It means you understand the subject matter. On page 231 of Expert FSharp 2.0

attempt { let! n1 = failIfBig inp1
let! n2 = failIfBig inp2
let sum = n1 + n2
return sum };;

desugars to this:

attempt.Bind( failIfBig inp1,(fun n1 ->
attempt.Bind(failIfBig inp2,(fun n2 ->
attempt.Return sum)))))

but it should be this:

attempt.Bind( failIfBig inp1,(fun n1 ->
attempt.Bind(failIfBig inp2,(fun n2 -> let sum = n1 +  n2 in
attempt.Return sum)))))

I confirmed it on stackoverflow (http://stackoverflow.com/questions/6182055/is-this-a-correct-desugaring-of-the-computation-workflow) and via email with one of the authors (Don Syme).

10Jul/114

Code completion in Vim for iOS

I recently started doing iOS developement. It’s great to be starting something new and learning new things. I’ve enjoyed learning Objective-C although I have a strong C++ background. But one thing that is annoying is having to learn the key bindings of yet another IDE. I’ve had to learn the bindings of Visual Studio, Eclipse and this time it’s XCode. Why can’t I just use a single editor that I know well enough that works on all platforms to do my coding? And given that my work patterns at the moment require me to chop and change between developing on Windows to Linux to Mac OS X daily, the most irritating thing is having to switch editing environments. It’s really frustrating when you press a key combination that’s been wired to your muscle memory to find out it does the wrong thing because you’re not editing in the editor you thought you were. I know Vim fairly well and am a relatively fluent user of it so naturally I wanted to use it for iOS devlepement. But XCode has such nice code completion? Enter clang_complete. It’s a plugin for Vim that uses clang to perform code completion. clang is also used by XCode for the same reason. Eventually it will replace the gcc-llvm compiler. I downloaded and installed the script but it didn’t work for my objective-c code for iOS. By running xcodebuild in verbose mode to see what commands were passed through to the compiler I was able to come up with the command line arguments to configure clang_complete with.

CompileC build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/Objects-normal/i386/main.o CrimeBook/main.m normal i386 objective-c com.apple.compilers.llvmgcc42
    cd /Users/sashan/code/CrimeBook
    setenv LANG en_US.US-ASCII
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=gnu99 -Wno-trigraphs -fpascal-strings -Os -mdynamic-no-pic -Wreturn-type -Wunused-variable -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -fexceptions -fasm-blocks -mmacosx-version-min=10.6 -gdwarf-2 -fvisibility=hidden -fobjc-abi-version=2 -fobjc-legacy-dispatch -D__IPHONE_OS_VERSION_MIN_REQUIRED=40300 -iquote /Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/CrimeBook-generated-files.hmap -I/Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/CrimeBook-own-target-headers.hmap -I/Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/CrimeBook-all-target-headers.hmap -iquote /Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/CrimeBook-project-headers.hmap -I/Users/sashan/code/CrimeBook/build/Release-iphonesimulator/include -I/Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/DerivedSources/i386 -I/Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/DerivedSources -F/Users/sashan/code/CrimeBook/build/Release-iphonesimulator -DNS_BLOCK_ASSERTIONS=1 -include /var/folders/vx/vx-91r-0HPqbIKIRlmQhbk+++TI/-Caches-/com.apple.Xcode.501/SharedPrecompiledHeaders/CrimeBook-Prefix-bemjhtzaxsuzvgcgukqqlhmzivkm/CrimeBook-Prefix.pch -c /Users/sashan/code/CrimeBook/CrimeBook/main.m -o /Users/sashan/code/CrimeBook/build/CrimeBook.build/Release-iphonesimulator/CrimeBook.build/Objects-normal/i386/main.o

Stripping away much of the above by trial and error revealed the pertinent options to pass to clang that would allow a single file to compile were:

clang -fblocks -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -D__IPHONE_OS_VERSION_MIN_REQUIRED=40300 main.m

I placed this in the clang_user_options variable in my .vimrc and code completion worked.

let g:clang_user_options='-fblocks -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -D__IPHONE_OS_VERSION_MIN_REQUIRED=40300'

Then I added a function to vim to run xcodebuild. This function runs xcodebuild and puts the output from the run into the quickfix list. From there you can navigate the build errors by using :cwindow. Call the function from the vim command line like :call XCodeBuild

function! XCodeBuild()
  let l:command = 'xcodebuild -sdk iphonesimulator4.3'
  let l:out = system(l:command)
  cexpr l:out
endfunction
15Apr/112

Exception unsafe

I was reading up about exception safe code in C++ and the difference between the strong and basic exception guarantees. The article here (http://www.gotw.ca/gotw/082.htm) outlines what it means for code to satisfy the basic exception safety guarantee, however it doesn’t give an example. So I came up with something that is exception unsafe. The code below doesn’t satisfy the basic guarantee because it fails to delete the memory allocated to the pointer.

Exception unsafe.

#include <iostream>
#include <stdexcept>

using namespace std;

void h()
{
  throw logic_error((string("Failure in ") + string(__func__)));
}

void g()
{
  int* pi = new int;
  h();
  delete pi;
}

void f()
{
  try
  {
    g();
  }
  catch (const exception& e)
  {
    cout << e.what() << endl;
  }
}

int main(int argc, char** argv)
{
  f();
}

To ensure that it meets the basic guarantee the exception needs to be caught in g() and the memory deallocated. The code below does this. Unfortunately there is a bit of awkwardness here: note the double delete. One is in the bad path where the exception is caught and the other is in the good code path. It would be nice if the double delete wasn’t necessary. Other languages have a finally statement that is executed in both the good and bad case, however C\++ doesn’t have this but the problem is still solvable as shown below.

Basic exception safety met.

#include <iostream>
#include <stdexcept>

using namespace std;

void h()
{
  throw logic_error((string("Bad logic in ") + string(__func__)));
}

void g()
{
  int* pi = new int;
  try
  {
    h();
  }
  catch (const exception& e)
  {
    cout << "Caught exception: \"" << e.what() << "\" in " << __func__  << endl;
    delete pi;
    throw;
  }
  delete pi;
}

void f()
{
  try
  {
    g();
  }
  catch (const exception& e)
  {
    cout << "Caught exception: \"" << e.what() << "\" in " << __func__  << endl;
  }
}

int main(int argc, char** argv)
{
  f();
}

The lack of a finally statement in C++ isn’t a problem as long as the principle Resource Acquisition is Initialization is applied. It’s fairly straightforward and with a little bit of thought I think that most good programmers would realize what to do here in order to avoid having a double delete. The trick is to write a wrapper class to manage the resource and in the destructor clean up any allocated memory. If you follow this guideline then there is no need for C++ to have a finally statement. This is mentioned in the the C++ FAQ in section 17.6. The following code applies RAII. I’ve found myself doing this in code I’ve writted without explicitly realizing the technique I was applying had a name assoiciated with it.

Basic exception safety with RAII.

#include <iostream>
#include <stdexcept>

using namespace std;

class Resource
{
  private:
    int* pi;

  public:
    Resource()
    {
      cout << __func__ << endl;
      pi = new int;
    }

    ~Resource()
    {
      cout << __func__ << endl;
      delete pi;
    }
};

void h()
{
  throw logic_error((string("Bad logic in ") + string(__func__)));
}

void g()
{
  Resource r;
  h();
}

void f()
{
  try
  {
    g();
  }
  catch (const exception& e)
  {
    cout << "Caught exception: \"" << e.what() << "\" in " << __func__  << endl;
  }
}

int main(int argc, char** argv)
{
  f();
}
28Mar/110

Java JDKS

I’ve been doing some Java development recently mainly remedial work. I’ve also learnt some lessons about what Java promises and fails to deliver. Anyway the build system we’re using is Maven and it was building things fine on one of my development machines. However I tried to build the project on another host and got this error:

ERROR] execute error
java.lang.IllegalStateException: no source files
        at com.sun.tools.javac.main.Main.error(Main.java:162)
        at com.sun.tools.javac.main.Main.compile(Main.java:347)
        at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)
        at com.mysema.maven.apt.AbstractProcessorMojo.execute(AbstractProcessorMojo.java:193)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkProjectLifecycle(DefaultLifecycleExecutor.java:1205)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkLifecycle(DefaultLifecycleExecutor.java:1038)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:643)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

This had me puzzled for hours. I cleaned the tree and did a rebuild but still got the same error. I rm -rf’ed the tree, got the latest from source control, did a rebuild but still got the same error. Then I had thought maybe it’s the different JDK’s installed. The machine that the build worked on had the Oracle JDK installed whereas the machine that I got this exception on had the OpenJDK installed. But aren’t the JDK’s meant to be compatible? Isn’t that what Java promises - the ability to run anywhere and it will output the same shit? I installed the Oracle JDK and reran the build and to my amusement it worked validating my cynicism about Java. So much for Java’s platform independence - it can’t even do the same thing on the same platform.

Filed under: java, programming No Comments