Sunday, December 14, 2008

Writing a Starcraft CD-Key Generator

For the benefit of those who didn't know, Starcraft is an old RTS game, the 2nd one I've been playing since 1997, and the only one that I'm still playing on and off after 10 years. It's so old that I don't even know where my box and manuals for it (most likely tossed away when I moved a long time ago).

And that's annoying if you ever wanted to play old games you've bought.

While CD-Key generators and hack tools for Starcraft have been floating on the net since time immemorial (remember Razor 1911 anyone?), I've always been interested in how it's been done. Well, someone has recently wrote about reverse-engineering Starcraft's CD key algorithm, which answered my question, and I'm quite surprised that the algorithm is really quite straightforward.

Just for interest, I wrote an online implementation of the algorithm, but since my physical box is being shipped and I don't have a hosting provider at the moment, I'll rely on the only freely available service I know of, Google's App Engine.

This forces me to pick up Python because Google App Engine does not support Ruby. Luckily, computer languages are usually near dialects of each other and Python is quite similar to Ruby or Perl, which makes learning easier.

All in all, it took me a day just to learn Python/Django Templates/App Engine and writing up the application, so I am rather pleased with the result. Here's the web implementation of the algorithm hosted at http://cd-keys.appspot.com. The online application has additional code that showcases the database API for App Engine. The code is GPL'ed and freely available here.

While I think the simplicity of Google's Application hosting is great, but in terms of having to be forced into a different environment. I didn't really enjoy Django's templating engine, compared to Haml/Sass, which I feel is more powerful and expressive, and that's the same comparing Google's Database API with Ruby Sequel. But the trade-off to using Google's API is that I don't need to monkey-wrench the guts of an RDBMS myself.

On the overall, Google App Engine is really not a bad service, and I really hope they'll start supporting Ruby as an operating environment in the future. That will tip the scales to their favour for me to consider their hosted service rather than running a custom server on my own.
Sunday, November 30, 2008

My Last Evening in San Francisco

A picture is worth a thousand words:


I will miss this place.
Thursday, November 06, 2008

If you think Rubygem is slow, that's because it is


Update: This there is a solution for the slow rubygem problem now, please see Ways of Solving the Slow Gem Problem instead.


If there is a good example of bad software, Ruby's Gem package management system has to be it. I can't say how much irritation I've had every single time I had to do a gem installation, and I've really lost count of the times that I had given up waiting for it to complete execution.

It's incredible just how much memory that rubygem requires to work. I can't even use it on my laptop with 512M of memory, when if it's enough to run an entire operating system with all daily use applications, it should be enough for a package management system. But how very wrong I am! Rubygem uses so much memory that it swaps a gigantic amount of data into virtual memory, trashing my harddisk and driving my laptop into a state of near unresponsiveness. (Of course, that would have meant nothing if I was running Windows, but it's certainly nothing less of an amazing feat when it can bring down a Linux box.)

Perhaps it's time to start classifying rubygem as a DOS malware?

Since I couldn't get the bloody thing to run on my laptop to show you a measurement of how slow it is, I had to resort to using my higher-end desktop that has 1.5GB of RAM in it. Here's the result from the output from 'top':


To help you understand how much of a memory hog rubygem is, I've underlined in red how much memory it's been consuming. If you take a glance at the row underlined in red, that indicates rubygem using up 359M of RAM, which is even more than a well-known memory-hog, Netbeans, which uses up 273M of RAM (underlined in blue, where Netbeans is shown as a single Java instance.) Also, as you see from the screenshot, besides being a memory hog, rubygem was 47 minutes in execution and it still hadn't completed installing the gem that I've instructed it to.

I'm certainly not the only person encountering problems with rubygem, and so far, there aren't any solutions in resolving the situation. Plenty of unhelpful advices I've been able to google up, which include things like updating rubygems (I already have, till the latest version of 1.1.1), and other discussions and solutions that just don't work[1][2].

To quote Charles Nutter, a core developer of JRuby:
"Even on a high-end box, it's so intolerably slow that there's got to be a key fault keeping the speed down."

That observation was made in 2006. If you've noticed the result from his execution, it took rubygem 62 minutes to complete execution, not unlike what I've observed myself, but two years later. And that's just dismaying.

To put it simply, Rubygem is just a piece of rotten software - avoid using it like plague. If a package management system like Debian's Apt can deal with the entire ecosystem of Linux software packages without chewing up massive amount of computer resources to do it, it's certainly no excuse for a smaller universe of packages like Ruby to perform that much substantially worse.

References:
[1] gem install SUPER SLOW in linux virtual machine
[2] trouble with slow rubygems
Saturday, October 18, 2008

An Emotion I Much Rather Not Have

Love, that is.

A friend left, and on my own, I sat down by myself and watched 'Becoming Jane' tonight, which had still left me with a barrage of thoughts as I type this. The film has accomplished what it had set out to do; to evoke emotions and feelings, in this case, a sense of empathy towards one's unrequited love, as if you were living in the same moment with Jane Austen, sharing her emotions of wanting but not having. Like all good movies on the genre of love, it leaves one with a feeling of longing for something more.

I could still feel the raw emotion coursing through, that feeling of want, the need to be desired and be loved. Ironically, as with the irony the film featured, love is exactly the feeling if given a choice, something that I willingly forgo.

Reel life, unlike real life, compresses a lifetime into a span of mere hours that is made up of only dear tender moments, and the soothing, lulling message that leads us to believe that life is an everlasting bliss of joy and happiness. Even as we accept it not to be so, it doesn't preclude us from wishing, and even willing to believe possibly that at a given moment in time, when we look deeply into the endearing eyes of that perfect someone, which at that precise moment of sheer intensity, it will be of an eternity.

If it's hard to pause for a moment to not believe in the eternity of love, then it is even harder to contemplate on just how transient it is. But love is exactly just that: as much as food is constantly on the thoughts of a hungry man, a man starved of love is just as much be seeking to satisfy his emotional fulfilment. But it is from this derision of hunger that we lose sight of the fact that satiety that is the nemesis of all wants. It is when we get enough of something that makes it so much less desirable. The scrumptious meal is merely a palatable one to the full tummy; the pair of endearing eyes is much less so for the umpteenth time; and all that is exciting simply becomes the mundane.

Some say that love is what makes us human, but perhaps what makes us human is what makes us weak. If we are always aspiring to be reach our potential each day, then maybe we should be trying to surpass the weaknesses of ours, rather than to be merely succumbing to it?

But even if it is a weakness, I have no qualms if love was merely a tempered emotion. However, it is nothing like the tame beast that we all wish it to be; rather it is more like a raging wildfire that engulfs us, leaving us at the mercy of its whims and fancy. I would have certainly been less disagreeable about the notion of romantic love had it been of one that leaves us with some measure of control and self-dignity in tact.

If romantic love was to be more like the selfless love and compassion to the fellow man, it would have been one that I would have preferenced towards, for it is unlike the selfishness of personal desire that leaves no room for rationality, and one that transcends above language, culture or creed.

But as much as my own personal internal intellectual discourse will and want to rationalise that love is merely a barbaric relic that is passed down for generations for the purpose of continuing our progeny, I know there'll be no way of not be ceding control to this ever insatiable feeling to want and be wanted, to desire and be desired. But had it been a matter of rational choice, it will have been an emotion that I'd much rather not have wanted.
Thursday, September 04, 2008

Japanese vs. Singaporean Women

I'm not sure about the rest of Asia, but when I was growing up in Singapore, there was a huge consensus among Singaporean males that Japanese females are much prettier than our Singaporean counterparts. Given that most of my friends who claimed it as a fact only came to that conclusion based on seeing pretty Japanese actresses on TV, I've always felt quite ambivalent about this point - come on, people always put on prettier than average faces on TV, but does that prove the case for the general population?

Given that I recently had the opportunity to visit Japan, I decided to put the claims to the test and do some observations on my own, through the perennial hobby of mine - people watching. Given that I had time to spare on Sunday noon after checking out, and too lazy to lug my luggage around to do sightseeing before hopping on the train to Narita, I decided to sit down in Tokyo subway station and record my observations. It isn't surprising that I get funny stares by people, wondering what I was doing, but predictably, nobody did ever approach me and asked questions (not that I'll be able to answer them anyway, given my command of Japanese is utterly atrocious!)

Exactly where I sat in Tokyo JR Station during my observation

I've done the same measurement at the linkway to Suntec City along the way from City Hall MRT station as a comparison, which my observation is carried out on a Thursday evening at around 6pm. Both measurements are timed at a 50 minute interval. There's nothing scientific about why 50-minutes was chosen, I would have much rather made it an hour if I could. But in Tokyo, I had to take off to catch my train to Narita, which explains the duration. You might think that it's a fun exercise to do, but it does get tiresome past the 40 minute mark. Try it out if you aren't convinced!

In my observations, only females are observed and also, only East Asian looking women are considered in the study, so that it's a fair comparison between apples with apples. I've also excluded young children who I deemed below the ages of 14 and people who looked over their 40's. It should however be noted that this process isn't scientific, given that there's no reasonable way to assess their age without any verification. This and other potential biased-ness are all recorded duly after the results presented. The observation is segregated into roughly 4 categories, which I'll briefly explain below:

  1. Definitely Good Looking - People I consider good looking, in which my opinion is unlikely to be subjected to change.
  2. Considered Good Looking - People I think are good looking, but which my opinion may change depending on conditions.
  3. Considered Not Good Looking - People I think are not good looking, but which may change depending on conditions.
  4. Definitely Not Good Looking - People I think who are not good looking, in which my opinion is unlikely to be subjected to change.
The conditions mentioned in points 2 and 3, are related to things like the mood of the day, and physical conditions such as viewing angle, lighting etc. The volatility in 2 and 3, simply means that I'm factoring into account that some people may look better if I am having a good day, and vice-versa. It was necessary, given that my observations are fleeting - people don't stand around all day at thoroughfares for you to observe, but for the purposes of simplicity, people in category 2 should be considered as just good looking, and not good looking in category 3.

The results are tabulated below:


Japan
Singapore
Definitely Good Looking70
Considered Good Looking14996
Considered Not Good Looking130157
Definitely Not Good Looking49151
Total People Sighted335404


Before we go into the analysis of the results, I'll just like to note all the potential biases that can occur in this observation.
  1. Beauty is always in the eyes of the beholder. I'm judging beauty by my standards, so this is definitely a case of 'your mileage may vary'.
  2. I've noticed that internally, if I had a period when I'm seeing a string of consecutive good looking faces, I'll have the tendency of becoming stricter in my grading criteria. The same principle applies in reverse, which means that the results may be possibly skewed, but it should hopefully be equally skewed in both observations.
  3. Given there is no means of checking the ages of all the people I've scanned, I did notice my tendency of adding attractive middle-aged women, and possibly excluding less good looking women who may be in actuality, under the 40's criteria. This has myself convinced that the perception of age is highly dependent on how a person looks, however unfair it sounds!
  4. The stream of commuters are not uniform, and are more spiky in nature, given the fact of increases in traffic only happens during the arrivals of trains. This results in uneven observation times, where I have longer periods to scan during non-peak periods vs peak periods.

Analysis

When it comes to beauty, I've always maintained my thesis that the ratio of beautiful women to the general population should remain roughly the same, irrespective of regions - which means it is the size of the population that determines the number of pretty faces that you'll see on the streets, which I've reasoned that it is why you naturally see more pretty faces in populous countries like Japan, China and Korea, than say, a small country like Singapore. I had classified 46.5% of the Japanese females sighted as pretty, compared to 23.7% for Singaporean females.

A possible factor may be in the differences in their attitudes towards cosmetics and dressing. From casual observation, I had the impression that Japanese women put on much more make up than Singaporeans. Almost all Japanese women, young and old, are dabbed in cosmetics of some sort. The second observation I had, is that Singaporeans have the tendency to dress much more casually than the Japanese, which may have also have been a factor. Both trends seem to be more pronounced with younger Singaporean teenagers and women in their early 20's, maybe it's because they either have less spending power on such 'frivolities', or they are letting their youthfulness compensate for the lack of need to dress up.

Talking about women is always a touchy issue, and that said, I'll profess that my opinions are not meant to be taken as fact of any sort, and neither do I imply any sort of preference toward women of either country. My observations are solely done for fun, and mainly in satisfying my personal curiosity of trying to debunk the broad assertions of attractiveness that Singaporean males seem to profess over Japanese women. I'm not sure how much of that actually correlates with reality, even though my observations seem to affirm such a bias. I suppose if any of my Singaporean mates sprout such 'nonsense' again, I should learn to just keep my mouth shut!
Thursday, June 26, 2008

Show Tables equivalent in Postgresql

The equivalent of mysql 'show tables' command in postgresql:

\dt



To see more information about the table, do \dt with the table name:

\dt table_name



If you want to see more than just tables (eg. sequences), use:

\d



To see all databases, like 'show databases' in mysql:

select datname from pg_database;

Wednesday, June 25, 2008

Printing Syntax Highlighted Source Code

Sometimes, reading code from paper can be easier than reading it from the screen. However, if you printed it straight with the 'lpr' command, you'll lose all your syntax highlighting. There is a way of solving this problem by using GNU 'enscript'.

For example, if you wanted to print syntax highlighted ruby code:


enscript --color=1 -Eruby your_source_code.rb


The '-E' flag tells enscript that the code is ruby, while '-color' is self-explanatory. If you wanted to find out what other syntax highlightings are availabe, use this command:


enscript --help-highlight


However enscript is more than just that. For example, if I wanted it with line numbering, landscaped and in two-columned format (thus saving space), I can do this:


enscript --color=1 -Eruby -c2 -C1 -r -j your_source_code.rb


where:
-c2: 2 columns
-C1: start line numbering from 1 onwards
-r: print in landscape
-j: print border around the columns

It can even generate syntax highlighted code in html, which makes it useful when you want to blog about source code:


enscript --color=1 -w html -Eruby your_source_code.rb


Its 'man page' describes it as "convert text files to PostScript, HTML, RTF, ANSI, and overstrikes" probably doesn't do it much justice, given that it's capable of doing much more; my examples have barely skimmed its surface, so I highly recommend you read more into it to find out enscript's full capabilities.
Saturday, June 14, 2008

Installing OpenGrok On Ubuntu Linux

I am really impressed with OpenGrok, a web-based source code search engine that I've found while I was trying to look up OpenJDK's source code. It is pretty cool as OpenGrok allows you to point your browser into an exact line of source code in your respository, allowing citations directly for discussion using hyperlinks, rather than cutting and pasting chunks of code. I find useful for annotating code, like when I'm using a wiki in conjunction with it to document design considerations for the source.

I'm only supplementing OpenGrok documentation because there were some parts of it which were less clear, taking longer than I expected to get it running. Hopefully, these instructions will help you cut down your setup time.

The assumption is that you're installing from a bare-bones Ubuntu system, and all commands here assume that you are root, which if you're like me, coming from a Gentoo background and sick of typing sudo all the time, you stay rooted all the time by using:


sudo -i


The next thing to do is to get all the relevant software via aptitude. I'll be using Apache Tomcat as my application server:


aptitude install sun-java6-jdk tomcat5.5 exuberant-ctags


Before we set up OpenGrok, we need to create the directory structure to store the files. For the sake of brevity, I'll use the same directory structure from OpenGrok's EXAMPLE.txt:


/opengrok
|-- bin
|-- data
`-- source


Download the tar.gz archive from its website, unpack and extract it. Copy the OpenGrok binaries into the /opengrok/bin:


# cp -r run.sh opengrok.jar lib /opengrok/bin


Edit run.sh and setup up the following parameters:


SRC_ROOT=/opengrok/source
DATA_ROOT=/opengrok/data
EXUB_CTAGS=/usr/bin/ctags


Note that I have put in the default location for the installed ctags for Ubuntu, you may have different locations/application names depending on your Linux distro. You'll then have to configure the web application. Go to the directory where you've downloaded your files, and unzip source.war to make modifications:


# mkdir source
# cd source
# unzip /path/to/opengrok-release/source.war


And make changes into WEB-INF/web.xml. The completed changes look like this:


<context-param>
<param-name>DATA_ROOT</param-name>
<param-value>/opengrok/data</param-value>
<description>REQUIRED: Full path of the directory where data files generated by OpenGrok are stored</description>
</context-param>

<context-param>
<param-name>SRC_ROOT</param-name>
<param-value>/opengrok/src</param-value>
<description>REQUIRED: Full path to source tree</description>
</context-param>

<context-param>
<param-name>SCAN_REPOS</param-name>
<param-value>false</param-value>
<description>Set this variable to true if you would like the web application to scan for external repositories (Mercurial)</description>
</context-param>


The text coloured red are the parts where you need to make modifications. The tags in blue indicate where the XML has originally been commented out by <!-- and --> you'll have to take them away.

Once that's done, you'll have to rezip the .war file back in place, and put it into Tomcat's webapps directory:


# zip -r source.war ./
# mv source.war /usr/share/tomcat5.5/webapps


After which, we'll need to configure our source code for OpenGrok to use, and set it up:


# cd /opengrok/source
# cp -r /your/source/code/ .
# java -Xmx1524m -jar opengrok.jar -W /opengrok/configuration.xml -P -S -v -s /opengrok/source -d /opengrok/data


This will generate the list of indices that allows OpenGrok to cross reference your source code. With that done, the final task is to setup Tomcat so that it has correct privileges. Append the following lines to /etc/tomcat5.5/04webapps.policy:


grant codeBase "file:${catalina.home}/webapps/source/-" {
permission java.security.AllPermission;
};

grant codeBase "file:${catalina.home}/webapps/source/WEB-INF/lib/-" {
permission java.security.AllPermission;
};


I'm just being cavalier here by giving full security access to OpenGrok, which should be safe. But I only doing it given because my application is firewalled from the outside world, so do make your own security adjustments appropriately! Once that's done, restart Tomcat:


# /etc/init.d/tomcat5.5 restart


You should now have a functioning own OpenGrok respository to play with! However if you get an error with the stack trace showing Apache Lucene not being able to create a file, grant full permissions to the data directory:


# chmod -R 777 /opengrok/data/


 
Monday, June 02, 2008

The Four-Letter Word of Independence

For a long time I've been rather vague in describing my ultimate goal - the attainment of freedom - only because the definition of freedom itself is rather broad and I had found it difficult to frame it in my own way. But I've actually found the version of 'freedom' that matches closely to what I'm thinking of from Nassim Taleb's book 'The Black Swan' (I'm retyping from his book, so there might be inaccuracies from me. Also the emphasis in bold is mine):

... It was hard to tell my friends, all hurt in some manner by the crash[1], about this feeling of vindication. Bonuses at the time were a fraction of what they are today, but if my employer, First Boston, and the financial system survived until year-end, I would get the equivalent of a fellowship. This is sometimes called "f*** you money," which, in spite of its coarseness, means that it allows you to act like a Victorian gentleman, free from slavery. It is a psychological buffer: the capital is not so large as to make you spoiled-rich, but large enough to give you the freedom to choose a new occupation without excessive consideration of the financial rewards. It shields you from prostituting your mind and frees you from outside authority--any outside authority. (Independence is person-specific: I have always been taken aback at the high number of people in whom an astonishingly high income led to additional sycophancy as they became more dependent on their clients and employers and more addicted to making even more money.) While not substantial by some standards, it literally cured me of all financial ambition--it made me feel ashamed whenever I diverted time away from the study for the pursuit of material wealth. Note that the designation f*** you corresponds to the exhilarating ability to pronounce that compact phrase before hanging up the phone. ...

Yes, I'm looking for that same psychological buffer to ease me from having to worry about the vicissitudes of everyday life, and to attain the ultimate goal of being the master of my own fate, rather than a serf of others. I'm not sure if this applies to any of you out there, but the very quality of independence is one that I see as keystone - nothing else ranks higher (except good health maybe), given most other things in life can be subsequently attained once you have the freedom to seek them out.

And like how Taleb describes the rich, I do not understand why are there people out there who, even after having an obscene amount of money, will still need to prostate themselves in the never-ending quest to seek out more. I have a target, and its reasonably modest compared to most people - a number of people I know had a definition of wealth that's in the range of $6m and above (don't ask me how they came up with that figure - ask them!), the ability to own at least a few Lamborghini-type cars, a private yacht and a Gulfstream jet.

Well, I'll be quite happy if I have the capability to pay for my own place to live in, a recurring income stream that satisfies my daily expenditure (which is modest, compared to most people), and possibly some excess money left to afford a yearly vacation (business-class tickets are nice, but not necessary!). Given I'm not the kind of person who needs to rinse my mouth with 'Chateau Lafite' every morning, mandate my meals to be caviar and foie gras every day, I hope it is a goal that I can attain at a reasonable young age, so that I can become a flâneur - someone who has attained the freedom to seek out whatever experiences he desires and being able to live by his own rules.


[1] Referring to the stock market crash of 1987 ( Black Monday )
Saturday, May 31, 2008

Vim: Editing Command History

One of the few oft-forgotten commands that are rather handy:


q:


Opens the command line buffer for editing


q/


Open the previous regular expression search buffer for editing.
Wednesday, May 28, 2008

Ubuntu 8.04 Crashes Upon Shutdown

My laptop used to hang intermittently during shutdown when using Ubuntu Linux 7.10. Now the problem has become worse after the upgrade to 8.04 - it's almost always reproducible on my Dell 700m - my screen just turns black at shutdown. Everything freezes, even on the hardware level: when the low-level 'Caps Lock' and 'Number Lock' LEDs don't even respond to keypresses, it's almost guaranteed that the hardware is locked up.

It is a critical problem, and something that has caught me by surprise. Initially, I assumed that the problem is some minor buggy code in Xserver that's causing a race condition resulting in the hangups, something I assumed will be fixed with the upgrade. It turns out that I was wrong - the upgrade has actually made it worse!

It's unacceptable that I have to hard reset my laptop every single time I shutdown. Often when it goes back up again, the ext3 journaling system indicates that I had orphaned inodes to be cleaned up, or in other words, uncompleted data writes to harddisk suggesting possible data corruption.

While looking for a solution, I was surprised that the problem seems to be quite widespread; there are plenty of bug submissions (like this, this or this) that has been lingering around for Intel's 8xx/9xx series of graphics cards already.

It seems that the current 'xserver-xorg-video-intel' package is relatively new and for some reason does not initialize the graphics card properly. The current fix that works for me is to make some modifications to Xorg.conf, presumably to either force it to initialize itself properly or avoid using the garbaged area:


Section "Device"
Identifier "Intel Corporation 82852/855GM Integrated Graphics Device"
Driver "intel"
BusID "PCI:0:2:0"
Option "ForceEnablePipeA" "true"
EndSection


If you're having the same problems with your Intel-based graphics card on Ubuntu, adding the line in red might help you.

I'm still annoyed by the fact that Ubuntu has decided to ship and use 'xserver-xorg-video-intel' instead of 'xserver-xorg-video-i810' (which was directly contributed by Intel, methinks) that is more stable, and may have avoided the problem in the first place.

Maybe the 'intel' driver has more features and improvements than the 'i810', and more actively maintained, but it is not right that end-users have to bear the brunt of critical bugs that freezes the computer - end users are not guinea pigs for faulty drivers that are not ready for prime time yet, irregardless of whatever new features that is touted to have. If it's not ready, it's not ready, geddit?

I don't think the Open Source guys want to tarred together with a the same brush as a buggy incomplete operating system like Windows Vista, right? :P
Saturday, May 24, 2008

Multiple return values and assignments in Ruby

One of the things that I really liked about Ruby which extends to those non old-school computing languages as well, is the capability to return multiple values. For example:
def foo(x)
return x, x+1
end

a, b = foo (10)
#=> [10, 11]


a
and b automatically gets assigned to the values returned by the function. Besides this, Ruby has the ability to define how variables are being assigned as well. For example:

a, b, c = [1, 2, 3, 4]
p a, b, c
1
2
3

a, b, *c = [1, 2, 3, 4]
p a, b, c
1
2
[3,4]


Notice the asterisk(*) before the c variable, where the remaining values of 3 and 4 got assigned into the variable c as an array. Neat!
Wednesday, May 21, 2008

Values Investing: Nicolas Berggruen

I don't normally copy and paste news articles verbatim, but here's an article worthy of note about another reclusive, wealthy philanthropic person. Like Chuck Feeney, he's putting his money into good causes. Certainly a person worthy of emulation, I hope I'll be able to be in his shoes someday, and have his kind of wealth to spend my life working for the benefit of society too. From WSJ:

Putting His Money Where His Values Are --- Restless Billionaire Now Prefers to Invest 'In the Real World'

Nicolas Berggruen became a billionaire through classic value investing. Now, he is switching to a new strategy. Call it "values investing."

With rice farms in Cambodia, windmill farms in Turkey, an ethanol plant in Oregon and glittering new skyscrapers in poor inner cities around the world, Mr. Berggruen is pumping millions of dollars into projects that he hopes will both expand his fortune and alleviate social ills.

"Historically, I've made my money in financials," says Mr. Berggruen, 46 years old, whose net worth is estimated at more than $3 billion. "Now, I'm investing in the real world. I'm investing in the ground, in things that will last for generations and improve people's lives."

Mr. Berggruen's big bet on social investing isn't unique. Richard Branson, the Google founders, Ted Turner and a vast new generation of eco-investors have all espoused world-friendly investing.

The quest is more personal for Mr. Berggruen (pronounced: BerGREWin). After amassing billions and buying all the usual trophies of success -- a Florida mansion on a private island, a luxury condo in New York -- Mr. Berggruen is paring down his material life. He has sold his properties and now lives in hotels. He is about to sell his only car. Because he doesn't have children and is unmarried, he is planning to leave his fortune to a personal foundation and an art museum.

"Living in a grand environment to show myself and others that I have wealth has zero appeal," he says in an interview, standing in a hotel room in New York's Upper East Side. "Whatever I own is temporary, since we're only here for a short period of time. It's what we do and produce, it's our actions, that will last forever. That's real value."

The obsession with legacy is increasingly common among today's super-rich -- even for relatively young billionaires like Mr. Berggruen. "For some of these people, they're growing concerned about how they're going to be remembered," says Russ Alan Prince, president of Prince & Associates, a wealth-research firm that conducted a recent study on legacy. "For others, they've always wanted to do something and they realize that if they don't do it now, they're never going to do it."

For Mr. Berggruen, the transformation follows a life full of eccentricities and unconventional success. The son of Heinz Berggruen, the famed Germany-born art collector who befriended Pablo Picasso, Nicolas Berggruen grew up in France and Switzerland hoping to become a writer. He studied Albert Camus, Jean-Paul Sartre and other existentialists and rebelled against his privileged upbringing.

When he was 17 years old, he moved to New York City to attend New York University. He also started investing with a few thousand dollars of his own money. After graduating -- in two years -- he started investing in stocks, bonds and early forms of private equity.

Soon, he was buying entire businesses. Berggruen Holdings, his wholly owned investing vehicle, has net assets of more than $3 billion, according to Mr. Berggruen, business associates and corporate documents.

One of his biggest victories was FGX, the eyewear company formerly known as Foster Grant, which he acquired when it was declining in value. After making acquisitions, expanding the product lines and shoring up management, he took it public for a big profit.

He also created Media Capital, one of Portugal's largest media companies, after acquiring newspapers, television, radio, magazine and Internet assets. He has since sold the company.

"He's a disciplined buyer," says Martin Franklin, the chief executive of Jarden Corp., the consumer-products giant, and a partner with Mr. Berggruen in several businesses. "Nicolas is one of those guys who turns lemons into lemonade."

Mr. Berggruen was also behind two of the world's largest special-purpose acquisition companies, or SPACs. These so-called blank-check companies raise money through initial public offerings to make acquisitions. A U.S. SPAC he helped launch in December, called Liberty Acquisition Holdings Corp. raised more than $1 billion. A European SPAC he launched in February, called Liberty International Acquisition Co. raised $878 million. Mr. Berggruen is still shopping for companies to buy with both funds.

He has experienced his share of setbacks. One of his earlier SPACs, Freedom Acquisition Holdings, merged with the British hedge-fund GLG Partners Inc. in 2007 to take the fund public. The stock surged initially, but plunged more than 30% in recent weeks after one of GLG's top traders abruptly resigned. Mr. Berggruen, who owns about 6% of the company, says he is disappointed in the loss but confident the stock will bounce back. In 4 p.m. New York Stock Exchange composite trading Friday GLG's stock was down 11 cents to $7.78.

Despite his wealth, the boyish-looking Mr. Berggruen remains a mystery. He has avoided the press and has never appeared on the Forbes list of wealthiest individuals, although he would likely qualify. When a Dutch magazine tried to publish a profile of him several years ago, Mr. Berggruen bought up all the copies and destroyed them.

His personal habits are legendary among friends and colleagues. He works 12-to-14-hour days. He rarely visits his offices around the world, preferring to work in hotel rooms and restaurants. When he is in New York, he does most of his work on his BlackBerry while speed-walking around Central Park.

For Mr. Berggruen, chocolate is a primary food group. He eats two meals a day, one of which usually consists of chocolate cake. When David Bonderman, founder of TPG, the private-equity firm, went trekking with Mr. Berggruen in the Himalayas, Mr. Bonderman rode a horse. Mr. Berggruen bounded up the mountain fueled on chocolate bars.

He is restless to an extreme, logging 250 hours on his Gulfstream IV last year (his biggest indulgence) and visiting more than 80 cities around the world.

Mr. Berggruen's shift to socially responsible investing was gradual. When oil prices started soaring a few years ago, he looked into alternative energy sources. He acquired the Cascade Grain ethanol plan in Port Westward, Ore., the largest ethanol plant on the West Coast.

In researching ethanol, Mr. Berggruen realized that the world's food production -- which was increasingly being used for fuel -- wasn't keeping pace with demand. He formed a team of top agricultural experts and started researching ways of boosting farming productivity.

He bought up hundreds of thousands of acres in Australia, where he plans to grow grains. He is in talks to buy land in various other areas of the world, and he is negotiating with several governments to lease land for farming cassava, corn, rice, olives and other crops.

After his food ventures, Mr. Berggruen realized how many similar social problems could be solved -- or at least targeted -- through investing.

"Government wasn't solving these problems," he said. "So the market has to step in."

One area was real estate. An avowed urbanist, Mr. Berggruen started investing in projects aimed at reviving decaying inner cities. He is working with partners to buy up large parcels in downtown Newark, N.J., to build a mixed-use development with offices, homes and retail. He is launching similar developments in India, Turkey and Israel, working with top architects such as Richard Meier, David Chipperfield and Kazuyo Sejima.

Mr. Berggruen is also making plans for his foundation, which will target a wide array of social problems. One of the few things he is still acquiring for his personal life is art, which he says will withstand the test of time and eventually be given back to the public through a museum.

"The art I buy now goes to storage," he says. "I don't have a home to hang it in."

Tuesday, May 20, 2008

The Devout Atheist

Just for kicks, I decided to label myself as a 'devout atheist', which is an oxymoron - being 'devout' is meant for people of faith, and an atheist symbolizes the lack of it. Surely they cannot co-exist?

It might, since there are varying semantic interpretations of 'atheist' itself. Some defines an atheist as 'an active disbeliever in god'[1] - which sounds ridiculous, because it exactly describes what the religions do: actively promoting a faith, in this case the faith that 'god doesn't exist'. How is this any different to having 'an active belief in god (or the Flying Spaghetti Monster)' itself?

The background to the atheist's mindset is based upon the fact that god's existence is unprovable - nobody has yet been able to scientifically witness and/or peer-review the presence of god. Yet we have to give it a benefit of a doubt, because we cannot disprove that the possibility exists: the rule 'you cannot prove a negative' applies.

But even so, what sets the atheist apart, is while he is certain about the unprovability of god, he does not see the need to hold onto a belief that god is, or will be provable (or not). Given there are an infinitely limitless number of things that are unprovable, the atheist, unlike the theist, does not arbitrarily chooses or selectively accepts the presence of one god, while discriminating the others. The atheist accepts all gods as equally unprovable, and unlike the agnostic, has no need for beliefs in any or all them. Yet, that does not preclude him from changing his mind, when there is evidence that proves otherwise.

'An atheist is always willing to change his mind should convincing evidence of God actually come to light'

- Stephen Hawkings

So by definition, I am an atheist, but what is with the 'devout' part? The lack of beliefs does not indicate I reject the values espoused by most religions, and certainly doesn't mean that I am opposed to it. In most cases, I am happy with many tenets that most religions share; they are generally good codes of conduct to live by for a harmonious society to function anyway.

"The word god is for me nothing more than the expression and product of human weaknesses, the Bible a collection of honourable, but still primitive legends which are nevertheless pretty childish."

- Albert Einstein

As childish a religion may be, childishness is also attributed with to the connotation of fun. That quality of fun, while considered as trivial for someone as great as Einstein, is something that general mortals relish in, and in some ways a representation of our society today. There is too much history that is related to religion that's pervasive in our way of life; without it, we would have lost a fair amount of art, literature, music and culture that we know of. If atheists do rule the world, what is it will they be replacing that void with?

The only thing I'm dead set against religions on, is of the absolutism and fanaticism of some individuals toward their beliefs - their assertions that 'I'm right and you're wrong', when it comes to their god - only theirs is the sole representation of truth. They choose to live and die by their faiths, consuming them where their existences are solely defined and dictated by religion alone. This is when the irrationals and the insane arise.

Even though I am weary of religion, I consider myself 'devout' as an atheist, for even with my lack of need for faith, there shouldn't be a need to pass judgement on others' need to believe. To promote atheism and denying the need for faith in individuals where they rely upon as an emotional crutch, or as a means of keeping themselves from straying into their bad side, is no better than proselytising religion itself.

Practitioners of faith may appear Machiavellian, but the trade off is to be able to keep society functioning in a relatively good order. A world full of atheists without good moral sense is no better than having no religion at all: there is nothing that stops them from being guided by only their own selfish needs. Religions should be viewed as a bootstrap for getting to the atheist's state of self-realisation - sure, only some will get out of that dreamy, 'religious state of mind', but ultimately one will come to appreciate atheism to be a 'beautiful thing' only when he arrives to that conclusion himself.


[1] The usage of 'god' should be gods. In this case, I mean any god, rather than the singular God from the monotheist's perspective.
Monday, May 12, 2008

I Just Don't Like Children!

I have said it before and I will say it again, I do not like children. I don't hate them, but I sure don't have much affinity for them. I remembered a candid moment when I remarked to a friend on how 'cute' a kid we saw on the street was, to which she immediately gave me a rather curt retort - haven't I said before that 'I didn't like kids?'

Sure I did, but it doesn't mean they can't be cute right? Puppies are cute too, and I don't see the need to have them either. But if I had to choose, puppies might just as well be a more desirable alternative to babies - at least you can always rely on them for unconditional, non-judgemental love!

A lecturer of mine remarked on his website that 'the only good baby... is a working baby', which drew quite a vitriol from a classmate of mine, but funnily it was his strong reaction that was more appalling to me than my lecturer's view.

Him, like most people, perceives that having a child as a 'god given right' [1], for otherwise, nature wouldn't have given us the ability to procreate in the first place.

Yet, nature had also given us 'consciousness' of free will - humans are given the ability to make conscious choices that enables us to override our primitive desires, which if not the case, we will still have mating seasons, bright red bosoms, and and irresistible desire to copulate when it happens. Can't say I don't miss those good ol' days, since it is never less than difficult to read the right signals from women anymore! But since we have evolved to make procreation a function of conscious choice rather than an involuntary reflex action, I don't see why an individual should not make his choice based on his own personal convictions.

Yet, the more interesting reason I have, is the fact that all life, will ultimately come to an end, where our destruction is a foregone conclusion. The only thing that's variable in our destruction, is just on which timescale of that it will happen to us. And given that we are living in a world that resource scarcity is increasingly become a more tangible reality, true irresponsibility comes from the fact that we are bringing more life into this world than we can sustainably maintain, rather than not having children.

To frame it in context, it took humans millennias to reach the population of 1 billion people in the 18th century, and only a century later grow this figure by an additional 5 billion. There are plenty of detractors who refutes the Malthusian theory that such permanent exponential growth can be possible, and many citing that throughout the timeline when man has dwelled this Earth, it hasn't happened. But it is misleading, and misguided to reach the conclusion by measuring it on a timescale - the mistake is that we've gotten the timings wrong, not that the eventuality will not happen. Perhaps the recent increases in the prices in oil, gas, wheat, rice, corn, poultry and other food staples is just a testament where this eventuality may be much closer than we'll like to think.

But finally, from a more 'selfish' perspective, why shouldn't we not have children?

This, improbable as it may seem, somehow links us back to the reason why nature had given us the ability to have children in the first place. Perhaps the more interesting question we haven't been asking ourselves is 'why are we able to procreate in the first place?'

'It just is', is not good enough, even though many might think it's an adequate answer. If you are familiar with the works of Richard Dawkins, his book, 'The Selfish Gene' suggests that our need for progeny is simply the gene's mechanism to ensure the continuation of its existence. That double helix structure is the sole reason why we have the ability to bear offsprings, and why the need to make us need, feel and want it.

And to come back to notion of 'cuteness' that I've alluded to earlier, surprisingly, it is just another example of the 'Selfish Gene' in action - a peculiar trait that serves no function other than eliciting a strong emotional response that is steeped deeply within our psyche - the 'cuteness' that we perceive, be it in a baby tiger or human, is intentional by design so that we will develop a sense of affection to the baby to take care of it. Yet it is irrational, for the case of a tiger, to grow affectionate to it presents a life threatening risk to oneself should it grow to full maturity - and yet the inherent cuteness is what inhibits our minds from perceiving the actual threat. Cuteness acts in same way in human babies, that when parenting have reached past its point of usefulness, the 'cuteness' trait subsides in a child as he grows into an adult, when he has reached self-sufficiency. It is not hard to infer then, that 'cuteness' is a form of emotional parasitism that confers the host no benefit in return in its raw, biological state.

But the good news is, since we humans are unlike the rest of the animal kingdom, we are able to choose between succumbing to our 'Selfish Gene', or my own 'Selfish Utilitarian Individualism'(TM), which is why I have chosen the latter. That all shouldn't be too surprising, given that I am supposed follower of Schopenhauer's school of thought, which seems just like the perfect, natural thing for me to do! But the next time someone asks me on my view about children again, well, I'll tell them 'it's a looooong story'.

And maybe, they should just read my blog. ;-)



[1] 'god' as an abstract construct, given the idea to the right of childbirth is inherently present in all cultures and not just limited to people who believed in the Christian God!
Thursday, May 08, 2008

My Investment Journey So Far

I haven't traded in the stock market in the last couple of years - the last time I checked, my trade history was probably about 3 years ago, which only amounted to just about 4-5 transactions.

To tell you the truth, I started investing without knowing much about anything at all, and had a lot of apprehension to the idea of putting money into the stock market, given that I had neither any prior experience, nor knowing anybody who could help me with learning how to invest in the stock market.

And what complicates matters, it is an area that I wasn't going to get much familial support from - mom had taught me the virtue of being sensible and frugal with money, but she has regarded the stock market as nothing more than just a big glorified casino, and would had probably balked at the idea of me throwing money into buying stocks.

Anyhow, since I was quite intent on taking control of my own financial matters, I was rather tight-lipped on what I was doing, and saving off a portion of my salary even though it would have been more tempting to spend it, and learning about whatever I can about stock investing from books and off the Internet.

The investment world to me then, was still an enigma. Names like Benjamin Graham, Buffett, and Munger were relatively new to me, but for all that they had been saying, I had distilled it down to a single rule to follow - 'buy good companies, at a good price'. But I didn't know what 'a good price' was, and certainly did not understand much to know what to look for.

At that time, P/E, Beta, Black-Scholes or various esoteric methodologies were just funny sounding words that were totally alien to me. Even though I have better conceptual understanding of certain financial indicators now, things like the Black-Scholes model still remains just a financial buzzword - till today, I still am clueless to what its significance is.

The only homework I did, was to understand what business the company was in, looked at their financial reports to find out if they were or going to be profitable, did a bit of comparison among their peers to see how they fared, and decided if the sector would do well on the whole based on the social-economic factors that would influence the business the company was in. The last point was probably my strongest and my only saving grace, given that I have an avid interest in keeping up with current affairs. There wasn't really any fixed form or methodology in the way I picked my stock. I guess the only thing I was really looking for was that I was comfortable that the company was having a productive business that I understood.

So far, my hazy, hodgepodge methodology hasn't been too bad - one stock has grown into a two-bagger, and another, from relative obscurity into a mid-cap company that's now in the ASX200. Given that my investment capital was from my hard earned savings from my initial few years of employment, it has been a bit of a personal vindication.

But I should still thank my lucky stars, given that 'Mr. Market' had been rather merciful in the last few years, without going into too many schizophrenic bouts volatility we've been seeing since the tail end of 2007. I am probably quite risk-adverse by nature, and if the market had been as tumultuous as it currently is, the volatility itself might have just made my stomach turn enough to put me off from investing altogether.

Well, at least now I can somewhat justify myself as being an investor, rather than a speculator after 3 years years of holding onto my stocks, and watching them grow. And I'll likely to continue holding them unless I really needed the money, or when the sentiment of the company's prospect changes.

These days, the financial markets have caught my interest once again, mostly for the second reason of the rule - 'good price'. The markets have been battered quite badly, especially the financial sector, from credit crunch that the US sub-prime crisis has caused.

Admittedly, even with the downward-spiralling market sentiment, 'good price' may still not be 'good enough' yet, as I'm mindful of the words of Buffett, Soros, and the various problems caused by rising oil prices, its ripple down effects on inflation, interest rates and the economy in general, the effects from the largest reset of sub-prime loans in the US and the ramping down of China's economic activity from the Olympics that are set to occur simultaneously post-August. To it all, we may have yet to encounter the next 'perfect storm' that's brewing ahead.

I can only hope that my foresight is 'right enough', given it is tempting to start accumulating 'bargains' during this period of negative market sentiment. I can't be certain if I am able to avoid stomach churning volatility that may be ahead, and that the good run I had for the last few years will continue, given that it is not the typical of the maniac-depressive behaviour that I've often read about stock markets. Hope I'll have the conviction to hold it on if things do get rough and not lose my pants in the process!
Tuesday, May 06, 2008

Satisfaction, Happiness and Suffering

These days I have nothing much that worries me, and there's really isn't any much that I'm really wanting in life - satisfaction, to put it in a word. But it seems like satisfaction isn't true happiness, or at least in a rather interesting philosophical conversation with a friend of mine.

It came about when I was explaining my ease of not having to be in a relationship, which comes as a disbelief that is shared by many of my other friends. They usually misconstrue what I meant by 'at ease', which they take it that I want to remain single, which is far from it - I don't discount the possibly of having a partner if I do meet someone nice, but I'm not going to sweat it even if I never meet 'the right one'[1] in my lifetime.

Surprisingly, my satisfaction with status quo seems to trouble a lot of people more than me (usually singles I observed, but there is an odd couple here and there who will raise hackles), that they see something is 'wrong' with me if I am not actively looking for that 'someone' in my life. And that slights me, given that it whiffs a hint that my 'happiness' is just a state of self-denial.

It's not that my friend is being pushy or obnoxious, and I do understand what he's getting at - most people want to get hitched and 'live happily ever after'[2]. Well, most people anyway. But in order to do that, one cannot do avoid the 'trials and tribulations' of courtship, or the proverbial slaying of the dragon to get the princess. Apparently, it's nothing more than a hollow victory if princesses come stocked at your nearest Wal-Mart store, or available through phone delivery!

While I don't disagree that there are some sacrifices to be made in order to get there, like doing things that you don't particularly take interest in, or even things that you detest but you're doing just because she does; it is precisely my refusal to partake in such 'silly' activities, or in his view, my 'avoidance of suffering' through this rite of passage that formed the basis of this lively philosophical debate.

He starts by quoting Nietzsche philosophical leanings, towards the idea that suffering is necessary, and quotes:

"To those human beings who are of any concern to me I wish suffering, desolation, sickness, ill-treatment, indignities - I wish that they should not remain unfamiliar with profound self-contempt, the torture of self-mistrust, the wretchedness of the vanquished."

And went on to say that suffering is good, for without it, we do not know the true meaning of happiness. Now the idea wasn't hard to accept, and got me pondering for a couple of days, literally. After all, it does have kernels of truth in it - given that best things in life are usually hard won, without suffering in its way, we will not attain a sense of fulfillment. Satisfaction of status quo is, as he puts it, a synonym for mediocrity.

But is mediocrity also a synonym for unhappiness? I'll get onto that later. Let's continue with the discussion.

He then quotes Schopenhauer's explanation of Aristotle's remark:

"The prudent man strives for freedom from pain, not pleasure."

Schopenhauer's philosophy is summarised into the point that it is impossible to fulfill all our desires and so we should avoid the troubles and anxiety that we go through in it's pursuit. Which is the stance that I supposedly adopted.

While I do not disagree with Schopenhauer's school of thought, but I didn't realise that it wasn't exactly what I believed only until later. Furthermore, I mistook what my friend had said as to have meant that he believed in the duality of happiness and suffering, ie. that you are happy only in the absence of suffering, and vice-versa, suffering in the absence of happiness. In fact, only at afterthought did I realise that he meant something more subtle, that in the absence of suffering, you will not know what happiness is.

But I still find Nietzsche's viewpoint too extreme to have any sensibility - instead of just facing adversity as it comes, Nietzsche encouraged the seeking out of suffering. The thought, while plausible, was an idea that was unpalatable subconsciously, even while my conscious thought wasn't settled enough to gather full clarity. Maybe it was just my friend's rather enthusiastic (or forceful) sell of his views that had formed a mental gag reflex, after being having it shoved down my throat.

We moved on and formulated a hypothetical example by comparing a starving child with one who has always had 3 square meals on the table. He asserts, and it is plausible to believe, that the starving child, when offered the same meal, will be more grateful to having the food when compared to the child who never had to starve. It is then, he argued, that because the starving child had suffered, and hence he is able to understand the joy of having food in the way that other child will never fathom, which I agreed, only to come to reject and refute the argument a day later.

There are two things that we have to factor into account here, which characterises our 'human nature', and incidentally, refuting Nietzsche's argument as absolute truth. One, the 'law of diminishing returns' inherent in human nature. Given that if the starving child was to given the same treatment as the child who never went hungry before, he will too, grow content of his 3 square meals, and over time, not derive the same level of happiness he had when he was first starving. Two, our nature of empathy - if you are the satisfied boy, and after seeing the suffering of the starving boy, will you not understand how lucky you are and to enjoy your meal better than before?

Hence it is not of the need to suffer in order to attain happiness, but rather the mere knowledge of suffering that will be sufficient. If that's the case, why is there a need to assiduously seek pain in order to gain pleasure?

In fact, I realised that neither the viewpoints of Nietzsche and Schopenhauer are absolute truths in itself, but rather they are two extreme points of view of the same issue: as much as it is insane to seek pain intentionally (you like cooking your hand in boiling water?), it is equally futile to avoid it (show me an exception, and I'll punch him in the face, effectively negating your exception), that in life we will be sitting within a spectrum in between both ends of it.

Suffering is sometimes, even good for you, like exercise is - while unenjoyable, and sometimes painful, you still do it anyway, and so the assiduous avoidance of pain is not the answer too. If you think about the risk to reward ratio, or couching it in the terms of happiness versus suffering, if you're well rewarded by the happiness for the amount of suffering you'll be getting, by all means, you should attempt it.

Applying that in the context of my philosophy towards relationship, if you do find the 'right one', you'll have much in common, therefore automatically have a better payout between happiness and suffering. Conversely, if you happen to find a bitch in your life who makes your life a living hell, you're probably better off without her.

Choice is, ultimately a test of your own judgement - but anxiety about not having a choice, or seeking to make the best one, is exactly what Nietzsche alludes to, that you're seeking suffering, albeit unconsciously, yet without any promise of happiness in return. Which is why I ascribe it is as 'silly'. There is nothing wrong with being content, and just because I don't show the anxieties that others have, doesn't mean I have to.

Only a day after did my friend pass me Alain De Botton's book - "The Consolations of Philosophy", the source where the majority of his philosophical arguments came from. While flipping through the book and reading about Montaigne, only did I realise that it is he that strikes the balance between the two extreme schools of thought, which resonated with me the most:

"We must learn to suffer whatever we cannot avoid. Our life is composed, like the harmony of the world, of discords as well as of different tones, sweet and harsh, sharp and flat, soft and loud. If a musician liked only some of them, what could he sing? He has got to know how to use all of them and blend them together. So too must we with good and ill, which are of one substance with our life."

Sneaky bastard[3]!



[1]: I only use the phrase 'the right one' as a well understood idea, but I am a not a believer of the idea of the 'right one', but rather that of a 'right compromise' :)
[2]: Sorry, but it's just another myth!
[3]: For trying to lull me into feeling insecure about myself!
Friday, May 02, 2008

Hello World

Or more rightly, 'Hello World' _again_.

I have stopped writing for a while. That is intentional. There has just been a feeling of a general lack of focus on the things that I write about, and to no big surprise, given that's exactly the same way how I feel about life, a core reason among others that I've taken a break.

After some introspection, I've come to terms that I have always had a 'jack of all trades' kind of personality, especially when it comes to interests: spanning from science and technology, to software development, sociopolitics in Singapore [which is inherently due to the poor treatment to the poor, and various forms of social unfairness/un-niceties I've endured/witnessed during my 20-odd years living there], philosophy, finance, and random musings that come up now and then. That all, is generally a tangled ball of mess that is hard to tidy it up, to pigeon-hole it into a blog nicely.

I've been thinking about my raison d'ĂŞtre for writing in the first place: to share the bits and pieces of what I know, for expressing my views, which in turn, to receive feedback and critique in sharpening my own thoughts. That all, besides my exhibitionist tendencies to share any interesting happenings around me! To put it more generally, it is what I do to entertain myself, but even more importantly, to be myself.

And that, is exactly what I'm resuming to do. :)
Thursday, April 10, 2008

Disabling Terminal Flow Control Keystrokes

If you've ever encountered the situation where you've accidentally typed the <ctrl-s> keystroke and your terminal seems to 'hang', that's because you've typed the 'XOFF' special character which tells the terminal not to accept any more key inputs.

The way to restore terminal responsiveness is by typing the <ctrl-q> keystroke ('XON') to restore flow control. If you want do disable this feature, use the following command on your shell:


stty -ixon


 

The Olympic Torch in San Francisco

There's a lot of excitement all around the city today, with the commotion coming from the carrying of the Olympic torch. Probably what's added to the excitement was the disruption that happened over the weekend along London and Paris legs where people tried to snatch the torch away from the runners.

Due to this incident, additional police forces are being deployed on the scene, at a cost of probably a million dollars from what I gather from the news last night, to ensure that the event will go on without causing a repeat of the weekend's fiasco.

From what I've glanced from the TV broadcast in the pantry earlier the morning, the run itself is very uncoordinated, due to the fact that the route has been shroulded in secrecy, even from the runners till the exact moment, in order to minimize the chances that the protesters will disrupt the event.

As much as I don't agree with China's actions with respect to Tibet, I just don't understand the rationale for disrupting the Olympic torch bearing marathon. I can't even see any possible positive outcome of these actions: just assume that someone did manage to snatch the torch, douse the fire, or smash it on the ground. What next?

*Nyeh Nyeh*, I win and you lose?

Does getting the torch change anything material for the struggle to liberate Tibet from China? Sure, it may make the protesters 'feel good' about whatever they've done, but these actions contribute nothing to help their cause, besides just irking the CCP even more. By putting an egg on their faces, they're likely to respond with hostile reactions more so than ever. I don't see much diplomacy in such actions, and they remind me of the guerrilla tactics Greenpeace pulls to gain media attention (and notoriety).

Getting media time to highlight the plight of your people is a good thing, but only when it's deservingly so. I just don't see the need for the mass disruption, media whoring, and expending resources to maintain law and order, especially when there is no positive outcome that can be derived from it.
Friday, March 28, 2008

Backing Up and Restoring Postgresql Databases

I recently have defected from Gentoo to Ubuntu, (*Gasp*, but I'm finally sick of the constant waiting from compiling and re-compiling, or suffering from dependencies breakage or conflicting masks preventing me from updating between new and old packages and various other complaints, but I'll leave that to another day), which one of the things that I had to do was to perform a backup of all my stuffs, including the data on my databases.

For Postgresql, it is really straightforward, and if anybody did a RTFM of Postgresql's man pages, you'll have probably found the answer. But if you're just lazy and wanted to consult the 'Oracle of Mountain View' and be spoonfed, here's how. To back it up:

pg_dumpall > db.out


Where db.out is your output file. Here's how you restore the database back into it's original state:

psql -f db.out postgres

Saturday, February 23, 2008

Ruby Code Snippets #3

Here's a simple Ruby code puzzle for you: if an argument is optional in Ruby, what do you think the result of the method would be if you passed 'nil' to arg2?
def mymethod(arg1, arg2='default')
arg2
end
Well, (as expected?) if you pass a nil as an argument, then there is something being passed, and hence 'default' won't be passed:
mymethod('foo', nil) #=> nil
And if you passed it just a single argument, here's what it'll look like:
mymethod('foo') #=> 'default'
Friday, February 22, 2008

Helpful Methods for Ruby Debugging

Just something which may help you when trying to figure out what Ruby object you're dealing with:
object.methods   #-> returns all the available methods
object.inspect #-> returns information about the object
object.ancestors #-> where the ruby object inherits from
Thursday, February 21, 2008

Ruby Code Snippets #2

One thing I like about more 'recent' programming languages like Ruby is the ability to have multiple argument assignments, ie:
 var1, var2 = %w{ a b c }
#=> var1 = "a", var2 = "b"

var1, *var2 = %w { a b c }
#=> a = "a", b = [ "b", "c" ]
The first example, "c" vanished into the ether, while the second example, the remainder of [ "b", "c" ] is stored into var2. Multiple variable assignments isn't unique to Ruby by the way, I do know that Perl does it too, but I think that it is just that it always defaults to the second example behaviourally.

It's nice that the syntax allows you to choose between what you want and do not want.
Tuesday, February 19, 2008

Ruby Code Snippets #1

Here's yet another reason I find ruby so easy to code. Lets assume that you want to do filtering of a certain kind of input that is demarcated by 'START' and 'END' on a separate line by itself. Let's use an example like the data shown below:
This is an unwanted line 1.
This is an unwanted line 2.
START
This is a wanted line 1.
This is a wanted line 2.
END
This is an unwanted line 3.
This is an unwanted line 4.
Normally, the typical way of segregating the wanted lines with the unwanted ones is probably via writing code that logically looks something like this:
in_header = false         # variable to remember whether if in_header
while line = STDIN.gets
if line =~/^START$/ # found header, start trapping
in_header = true
elsif line =~/^END$/ # end header, stop trapping
print line # have to print the line boundary inclusive
in_header = false
end
if in_header # still in boundary, print line
print line
end
end
But in ruby, the idea of applying ranges on regular expression matches simplifies the syntax down to a bare minimum:
while line = STDIN.gets
if line =~ /^START$/ .. line =~ /^END$/
print line
end
end
This is one of the things that I have seen that's unlike any other language I've encountered before. Amazing and totally delightful!
Friday, February 15, 2008

Visualizing Differences Between Source Modifications in Vim

There's always something new to learn from #vim, and here's another gem that allows you to use vim's built-in diff capability to visually inspect the code changes on your source inline. Add the following into your .vimrc:


function() DiffPreview()
diffthis | vsplit | enew | set buftype=nofile | read # | 1delete | diffthis
endfunction
map <F9> :call DiffPreview()<CR>


In this case, I've mapped it to the <F9> function key. How to use this: once you made all the changes you wanted, DON'T save the current buffer yet, and invoke the DiffPreview() function. It'll provide you with a highlighted 'diff' of all the changes you've made. Have fun!
Thursday, February 14, 2008

Hacking MacOS X For Password Retrival

I had to resort to this trick as I have inherited an old Mac machine from a friend of mine, who in turn had inherited it from her children. The annoying thing is that the machine's password is lost, and I can't get access to updates or make configuration changes. Since I've gotten the 'green-light' to break into it, it's going to be a 'no-holds-barred'. Sorry Mac, but you're going down!

Mac Zealots are going to kill me for saying this, but breaking access to the Mac OS is incredibly trivial. In this case, I do not require and external medium for the exploit, but just the magic keystroke of '<Apple-s>', in order to get into it's 'single-user mode'. Do this by rebooting your machine and holding down your 'Apple' and 's' key simultaneously. You should get to a screen below:
standard timeslicing quantum is 10000 us
vm_page_bootstrap: 124982 free pages
mig_table_max_displ = 70
92 prelinked modules
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California. All rights reserved.

using 1310 buffer headers and 1310 cluster IO buffer headers
ADB present:84
FireWire (OHCI) Apple ID 31 built-in now active, GUID 001451ff fe32b0e0; max speed s400.
CSRHIDTransitionDriver::probe: -s
CSRHIDTransitionDriver::probe booting in single user .. do not match
Security auditing service present
BSM auditing present
disabled
rooting via boot-uuid from /chosen: A827D24C-C773-3C25-8D8F-E027C540A4B8
Waiting on IOProviderClassIOResourcesIOResourceMatchboot-uuid-media
Got boot device = IOService:/MacRISC2PE/pci@f4000000/AppleMacRiscPCI/ata-6@D/AppleKauaiATA/ATADeviceNub@0/IOAT
ABlockStorageDriver/IOATABlockStorageDevice/IOBlockStorageDriver/TOSHIBA MK4025GAS Media/IOApplePartitionSchem
e/Untitled@3
BSD root: disk0s3, major 14, minor 2

If you want to make modifications to files:
/sbin/fsck -fy
/sbin/mount -uw /

If you wish to boot the system, but stay in single user mode
sh /etc/rc

%
You might as well call this the 'super user mode', given that it gives out 'root privileges' on the Mac. While it may not be immediately apparent in Mac OS, its UNIX heritage is still apparent. (See the 'The Regents of University of California' copyright message above.)

The message shown above is not reproduced from fidelity given that there's no way to intercept stdout during startup, but rather roughly constituted from the output from 'dmesg', so don't panic if you don't see exactly the same messages.

And if you noticed the instructions given, the nice thing about the Mac's user friendliness is that it even tells you what to do on the command prompt on how to start all the Mac services required!

There's really no need to follow the instructions verbatim, given that we didn't boot into a single user because the harddisk has crashed, there no need to run 'fsck', which is just a fairly time-consuming process that checks your disk for errors. Just mount your root file system in write mode and start the Mac services:

% /sbin/mount -uw /
% sh /etc/rc
Checking disk
fsck_hfs: Volume is journaled. No checking performed.
fsck_hfs: Use the -f option to force checking.
Mounting local filesystems
kern.sysv.shmmax: -1 -> 4194304
kern.sysv.shmmin: -1 -> 1
kern.sysv.shmmni: -1 -> 32
kern.sysv.shmseg: -1 -> 8
kern.sysv.shmall: -1 -> 1024
Resetting files and devices
Starting virtual memory
Removing /mach.sym /var/tmp/folders.501 /etc/nologin /private/_tmp_ /var/_run_ /
private/var/vm/swapfile0
Configuring kernel extensions

[... Other messages truncated ...]
%
There are more messages to that screen, but because I didn't trap the output from stderr, some of them are missing here. But they are not essential for our purpose.

And if you haven't realised it, at this moment you're in 'God Mode'. You have now full file, and system access to all accounts on the Mac OS system, just without the facade of the GUI.

If you have lost your 'Administrator' password, changing it is just a breeze, because you are 'root', you can just change the password of any account you want. However your 'Administrator' account is not actually called 'Administrator', and it does have it's own login name, so you'll need to find that out by checking your '/Users/' directory. For purposes of illustration, I'll assume that your Administrator's username is 'vincent'
% passwd vincent
Enter your new UNIX password: it's-a-secret!
Re-type your new UNIX password: it's-a-secret!
As expected, it doesn't even asks you what your old password is for the change! Well if really wanted to be nasty and insidious, you can create a 'backdoor' account, by changing the password just for the 'root' user, and you'll actually be able to access the computer even without the actual user knowing it!

Once you're done with the fiddling, type 'exit' to get out of the command line and back into the fancy GUI mode. Get into your 'System Preferences' and click on 'Accounts' icon. You'll probably see the screen like this:

As you see, the 'root' account is invisible to Mac OS, i.e. you won't see it as a valid user on your account panel. As typical Mac users, you won't be able to detect the intrusion, given that it doesn't show up as an additional user, normally a tell-tale sign that your machine is compromised.

Of course, you wouldn't have this problem if there is a way to disable the '<Apple-S>' keystroke. Well I don't for now, so it still poses a security risk for my home machine, which is why it remains for casual use as a really expensive music player.

Non-Greedy Regular Expression Matching

This applies to Ruby, and many other languages too (eg. Perl), just that I keep forgetting it. In order to have non-greedy matching enabled, append the question mark after the quantifiers, ie.


* (0 or more) greedy matching
+ (1 or more) greedy matching
*? (0 or more) non-greedy matching
+? (1 or more) non-greedy matching


For vim, the syntax is slightly more convoluted, so it's important to put a note here for my own reminder:


* (0 or more) greedy matching
\+ (1 or more) greedy matching
\{-} (0 or more) non-greedy matching
\{-n,} (at least n) non-greedy matching


There's no direct '(1 or more) non-greedy matching' in vim, but the 'at least n' operation should be the equivalent. For vim, these should normally be default and independent of whether if the 'very magic' or 'very nomagic' mode is set. See help in pattern.txt, for more pattern matching details.
Tuesday, February 12, 2008

Command Line Parsing using JFlex

What started out as a small set of commands for a tool I'm writing is slowing growing unwieldy to have to warrant enough repetitious code to parse the command line manually, and to wade through lines of if/else or switch statements (Don't you preach to me about the virtues of using the Command design pattern, for it is still unwieldy because it does not handle the parsing of arguments even the hash saves you from having long branching segments of code, which I don't mind. In my opinion, it's visually easier for me using folds, rather than to have file fragmentation of one command per file.)

Rather than having to deal with the unwieldy mess of buggy, manual coding using an ad-hoc mixture of Regular Expressions and StringTokenizers, I decided to start using a lexical analyzer instead. The one that I'm using is called JFlex, which is probably the most popular (or only?) one around.

Barring the initial learning curve, certainly having the lexical analyzer certainly makes life much easier, by automatically breaking down the command string into tokens each, without having to intervene to deal with handling white spaces and separators and such. A simple example for a lexical analyser that breaks up commands and arguments looks something like this:


/** The lexer for scanning command tokens. */
%%

%class CommandLexer

Parameter = [:jletterdigit:]+
WhiteSpace = [ \n\t\f]

%%

[:digit:]+ { return new Yytoken(Integer.parseInt(yytext())); }
{Parameter} { return new Yytoken(yytext()); }
{WhiteSpace} { /* Ignore Whitespace */ }
"-" { return new Yytoken('-'); }
"," { return new Yytoken(','); }


The example should hopefully be simple enough not to cause a 'cringe factor' or the need to refer to the Dragon Book.

There are 3 different sections in JFlex's definition file, separated by '%%' symbols. The first section is straightforward, it just allows you to include whatever that you wanted to include in the generated file.

The second section, is a list of definitions and directives that tells JFlex what to do. In this case, I've told JFlex to generate the the output to a file called 'CommandLexer[.java]'. Subsequently, the next two lines allows me to put in what I defined as 'WhiteSpace' or 'Parameter'.

The last section is where you define the grammar that helps the generated scanner code to discern what is a token, and in my case, what type of a token it is. In my example, rule 1 '[:digit:]+', matches 1 or more number and transforms that into a token, rule 2, matches what I call a parameter (which has either one or more digits or letters, and contains at least 1 letter in it). Rule 3, just tells the scanner to ignore all WhiteSpace characters, while Rule 4, 5 indicates what I define as separators, in my case the characters '-' and ','.

It must be noted that ordering is important. If I actually swapped the order of rule 2 with 1, because numbers will match the {Parameter} rule first, the [:digit:]+ rule will never match. JFlex will tell you that if that's the case (highlighted in red below):


Reading "commandlexer.jflex"
Constructing NFA : 16 states in NFA
Converting NFA to DFA :
.....

Warning in file "commandlexer.jflex" (line 13):
Rule can never be matched:
[:digit:]+ { return new Yytoken(Integer.parseInt(yytext())); }

7 states before minimization, 5 states in minimized DFA
Old file "CommandLexer.java" saved as "CommandLexer.java~"
Writing code to "CommandLexer.java"


The next thing to do is to actually create a actual token class, which is called Yytoken by default. An example of a typical Yytoken.java file looks somewhat like this:


/** A single scanner token. */
public class Yytoken {
public boolean is_separator = false;
public boolean is_int = false;
public boolean is_token = false;

public char separator;
public String token = null;
public int value = 0;

/** Default for range separator. */
public Yytoken(char c) {
is_separator = true;
separator = c;
}

public Yytoken(int value) {
is_int = true;
this.value = value;
}

public Yytoken(String token) {
is_token = true;
this.token = token;
}

public String toString() {
if (is_separator) return "Range Token("+separator+")";
else if (is_int) return "Int Token("+value+")";
else return "Token ("+token+")";
}
}


To test it, you can write a simple harness to read from stdin:


/** Test class to try out the command lexer. */
public class UseCommandLexer {
public static void main(String args[]) throws Exception {
CommandLexer command_lexer = new CommandLexer(System.in);
Yytoken token = null;
do {
token = command_lexer.yylex();
System.out.println("token = " + token);
}
while (token!=null);
}
}


That's probably a really basic tutorial in using JFlex, and to learn all of it probably requires having more of RTFM, but in the meantime, have fun in processing your command line!