Saturday, January 10, 2015

How to solve simple linear equations

It may come as a surprise to some people, but in high school this guy was not good at math at all. The point is that while I'm pretty good in designing algorithms, my calculations are always a bit off. Nowadays that's not much of a problem: you just grab a calculator or (even better) a computer, but the school system in those days was focused on doing it by hand. It took ages to calculate and plot a graph manually, which in my case was already completely clouded by tiny calculation errors. On top of that, it was too time consuming to make only one graph, let alone a whole series of them, so I couldn't get a feel of the dynamics involved.

Fortunately, that all changed when the computer came in. And then I started to understand all those things that had eluded me for years. Like solving linear equations.

There are several methods to solve them. One involves eliminating terms by applying the same operation at both sides of the equation. When I was a youngster, that only resulted in more complex equations than the ones I started with. Sometimes it didn't even resemble the original equation. The other one is switching terms from one side of the equation to the other side, while changing the sign. That one is already much better and the foundation of the method I will be presenting here.

You only need to fill in a simple table and then do some additions. If you manage to do that, you're left with one single division - and that's it. Let's take the following equation:
2x + 2 - 76 = -12x + 10
The table is a no-brainer. Make two columns and name the first one "+x" and the other one "-n". Got that? Note that even if you don't get the signs right, it's not a problem. Now, fill in the numbers associated with "x" in the left column and all others in the right column. If a number is on the right part of a subtraction, add a minus sign, e.g. "12 - 4x", will result in "12" in the right column and "-4" in the left column.

Once you hit the "equals" sign, subtotal both columns. Now write two new labels, "-x" in the left column and "+n" in the right column. Then continue until you're done. So now we've got something like this:


Now negate the number in the top right column and the bottom left column and add the subtotals in both columns:
(2 + 12)x = 74 + 10 =>
14x = 84
I promised you, you would be left with one single division. I didn't lie, here it is:
x = 84/14 =>
x = 6
That's it! It's so simple even a computer can do it. And that's exactly what I did in just 40 lines of Forth code - and that includes the entire parser:


Sure, you always risk making some errors while adding or dividing, but apart from that it's pretty bulletproof. So if your kid is eluded by linear equations, give it a try! Just make sure he (or she) understands that equal signs ("+ +" or "- -") make up a positive number, while unequal signs ("+ -" or "- +") result in a negative number.

But I'm sure he (or she) will manage that - since I mastered that one too forty years ago! ;-)

Sunday, October 20, 2013

Hardening your Raspberry Pi

When I had traded in my Microsoft box for a Linux box, I wanted it to look and behave like the large $100K Unix boxes I had had the pleasure to work with before. For instance, I bought a tape unit to make backups. It was terribly slow, but I kind of liked the whirring of the tape when you entered rewind at the console. That was cool.

So I was overjoyed when my brother came up with an old Wyse25 serial terminal. It took some fiddling with serial cables, inittab and settings, but somehow I got the beast to cooperate with my Linux box. It silently displayed kernel messages for years.

Sometimes, when my box hang, I logged in to shut it down properly. And at one time it started to spit out "Sense errors" from my SCSI drive. That got my attention. I backed it up and restarted - but it never came up again. Fortunately, my data was safe.

But after a decade, the Wyse25 began to behave erratically. The screen blinked and became unreadable at times. Most of the time a good whack fixed it - until the moment that was no longer sufficient. Maybe it was the dust that had accumulated over time, I dunno. Anyway, I needed a replacement.

But nowadays these things are hard to get. They are out of production and even a refurbished one would set me back a few hundred quid - and I needed it shipped overseas. That wasn't going to be cheap because these things are heavy, darn!

After a while I decided a Raspberry Pi and a cheap monitor would do the job just fine, setting me back about 200 Euros. I already had several parts lying around, like a keyboard and a mouse. It would not only allow me to show kernel messages, I could use it as a cheap workstation as well. That sounded like a good deal, so I ordered one.

It came in after a few days. Setting it up was a breeze (although I could not get it to accept the full 1080p resolution - but who cares) and within 20 minutes the kernel messages were scrolling down the screen. I pimped my 16G SD image with LibreOffice and a few other packages that seemed useful at the time and was quite happy with my low-cost workstation.

I put the monitor in "ECO-mode", so the whole contraption was using a mere 18W (which was 10W less than my Wyse25). Of course the Pi was on 24/7, because I wanted it to be available straight away - booting it every time would defeat the whole purpose. Problem solved. I thought.

After a few months I wanted to demonstrate the thing to a friend of mine, so I started up LibreOffice. Nothing. I rebooted it. It came up, but with errors. Fortunately ssh was still working, so it was not too big a problem. Until another reboot several months later when it just showed the dreaded kbd> prompt. That was no good..

I started googling and found out I had become the victim of "SD card corruption". It seemed to be quite common. Fortunately, there are two partitions on a Raspi SD card and my config.txt was on the first VFAT partition, so I could refrain from setting up my monitor all over again. This time I used another, smaller class 10 card and concentrated on its primary purpose - which was a console. Within half an hour I was up and running again.

But where did this corruption come from? I had been using SD cards for years (e.g. on my EeePC 701) and had never experienced corruption. Good powersupply? Got it! Overclocking? Not me. Good quality SD card? Sure! Power failure? Not that I can remember. Pulling the powercord? Are you nuts - I was an system admin! Still, I didn't want to repeat that procedure over and over again. So what could I do?

There are several things I don't understand about the Raspbian distribution:

  • Why is there no separate /home partition;
  • Why is there no separate swap partition;
  • Why use a journaling filesystem, EXT4.
With a bit of tinkering, one could even mount the root filesystem read-only and temporarily remount it read-write if one installed new packages. But - that was not the case. I had to work something out myself.

Note that these instructions are for the 2013-09-25 Raspbian distribution, which has already been tweaked to minimize SD card corruption by setting noatime to the root filesystem and moving a whole lot of stuff to tmpfs. But more could be done.

The first thing was to remove the swapfile. A box that just does a little ssh would probably not need it. That was simple.

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

The next thing was to move /tmp to memory. Just issue:

sudo vi /etc/default/tmpfs

And then set this parameter:

RAMTMP=yes

Finally, I wanted the logfiles in memory too. Note that they quietly disappear every time you shutdown, so they are of very little use when your Raspi has crashed. Just issue:

sudo vi /etc/fstab

And make sure it looks like this:

proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults,noatime 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
logfs /var/log tmpfs size=10M,noatime 0 0

However, this is not enough to get rid of all writes to the SD card. resolv.conf will still be rewritten each and every time - and of course, we still got the journal. But - it's not too bad for a start.

The Raspi has been working without a hitch for several weeks now. Rebooting was not a problem. But I still feel the whole story will pop up again some time or another, so I have been contemplating what my next move will be. The most painless one is to do what I suggested before: make the root filesystem read-only. There is even a special Raspbian for that one.

You could also try using a USB stick or USB harddisk as well. Just be sure that the Raspi gets enough power, which often means you need a powered USB hub. Another drawback is that you have to fork out a load of cash for this solution and that I've read reports about corrupted USB sticks as well. No guarantees here, so I can't really recommend that.

The bottom line is that the Raspi can boot from a whole bunch of devices, so you're free to make your own variant of Raspbian (e.g. on EXT2) or even try booting from NFS. It's all not too difficult as I will show you in the next post, where I'll explain how to move Raspbian to another medium - and how to emulate it. Which I do as well, because I don't feel happy developing on a physical Raspi for obvious reasons.

Although some may claim I have given you the impression that the Raspi is unreliable or a money pit, be assured that is clearly not my intention. I think it's a very nice machine with an incredible value-for-money, but it may need some tinkering to make it work for you in a particular situation, especially if you use it for production purposes - like a server that has to run 24/7.

And that's the beauty of the whole concept: you can!

Friday, May 4, 2012

My life with Coherent, part 2

In the first post of these series I've shown you it's possible to make Coherent 4.2.10 run under the newest version of QEMU. Now it's time to take it one step further.

I've had permission from Robert Swartz himself to distribute the first perfectly legal Coherent 4.2.10 QEMU image as long as you comply with the following conditions:
You may use the software on this image free of charge for personal, non-commercial use. You may NOT redistribute this image or the software it contains without written permission from the copyright holder. The software is provided on an "as is" basis without warranty of any kind.

Getting started
Download and unpack the archive. Included are two images, coherent.public.img and fat16.dd. The first one contains the Coherent image, the second one an empty floppy. If you want to, you can start them right away:
qemu-system-i386 -hda coherent.public.img -fda fat16.dd -m 16
First, don't try to crank up the amount of memory, Coherent won't use it - at least not this kernel. Second, you just have to be patient to attempt this, because it may take up to 10 minutes before Coherent is up and running. The good news is that you can monitor how far it is by pressing [CTRL]-[ALT]-2 in QEMU and typing:
info blockstats
If rd_bytes reaches 600,000 you can probably login. There are two login entries, the obvious root with password rootroot and a normal user named habe with password habehabe. Pick any one.

When you've logged in you might want to try the usual stuff like ls -l or ps -eaf, maybe you're even so bold to start up vi or cc. In any case, you will find that everything is painfully slow. If you want to get out, you can do so, but you'll have to be root in order to do it properly. Just issue:
cd; /etc/shutdown halt 0
Note it doesn't shutdown immediately! You'll have to wait until Coherent tells you it's safe to power down. Be patient! You can also exit QEMU without shutting down, but in that case you may have corrupted your image. And believe me, at these speeds you do not want to run fsck..

So that's it? Coherent runs, but it is completely unusable? No, not quite. Coherent does some disk caching, so after a while it becomes responsive. That is: until you reboot. But how can you avoid a reboot? Well, it's quite simple. QEMU allows you to make snapshots, so if you need Coherent you don't boot it, but simply load the snapshot.

QEMU only allows snapshots when you're using the so called "qcow2" image type, so we have to convert the files you just downloaded. That's not too hard, since QEMU comes with a handy utility for that. Just issue:
qemu-img convert -c -O qcow2 coherent.public.img Coherent.dsk
qemu-img convert -c -O qcow2 fat16.dd fat16.dsk
The latter may give you a warning, but don't worry, we'll only use it as a placeholder. Ok, we're done, let's start it up again:
qemu-system-i386 -hda Coherent.dsk -fda fat16.dsk -m 16
Agreed, you're in for the same wait. Sorry for that. But I promise you next time it'll be much faster. Just stay with me for a little while longer. When Coherent comes up, log in as usual. Then press [CTRL]-[ALT]-2 to enter the monitor. Now type:
savevm test0
This will save the current state of Coherent in a QEMU snapshot, so you'll end up here next time you start it. Return to the emulation by pressing [CTRL]-[ALT]-1 fiddle around a little bit, start up your favorite commands a few times and then save a new snapshot:
savevm test1
Shutdown Coherent properly and start it again, but this time with the command:
qemu-system-i386 -hda Coherent.dsk -fda fat16.dsk -m 16 -loadvm test1
Ok, that's more like it, isn't it? Ok, shut it down again. This time you'll be able to start it up much faster. Let's try something fancy now and invoke QEMU with:
qemu-system-i386 -hda Coherent.dsk -fda fat16.dsk -m 16 -serial telnet:localhost:4444,server,nowait -loadvm test1
Log in as root and type:
enable /dev/com1l
Now start up a terminal session on your host and type:
telnet localhost 4444
Yes, you can login to Coherent from your host Operating System. But that's about everything you can do as far as networking under Coherent is concerned.

There is a full TCP/IP stack available for Coherent, but you'll need a special kernel in order to make it work - which doesn't seem to be available. So I haven't succeeded to do much more in this area. If you have, please leave a comment.



Installing Coherent
Maybe you'll think this small system is so much fun that you want to give it a permanent place on your desktop. That is where the coherent.sh script comes in. It works with both KDE (KDialog) and Gnome (Zenity) and it makes it easier to launch the QEMU/Coherent combination. The only thing you have to do is to state where your disks are located:
COHAT0A=/opt/qemu/coherent.dsk
COHFD0=/opt/qemu/null.dsk
If you want to exchange files between Coherent and your host, you'll have to change /etc/fstab as well:
/opt/qemu/fat16.dd /mnt/coherent msdos rw,user,noauto,loop 0 0
Note this is the uncompressed, original file! You can mount the uncompressed Coherent image as well (as sysv), but you won't be able to write to it. I don't think I'll have to tell you how to mount a floppy image, do I? Once the VM is up and running you can exchange the virtual, empty floppy for the raw version by switching to the QEMU monitor and issuing:
change floppy0 fat16.dd
However, while the raw floppy is online you can't make a snapshot. When you want to take a snapshot you simply return to the dummy floppy by issuing:
change floppy0 fat16.dsk
Ok, now we've cleared that one we can concentrate on the real issue here: how do we get any files from the floppy? That's not as difficult as you think. You'll only need to edit one file, /etc/default/msdos. Make sure the appropriate section reads:
# This is for a system with a 3.5 inch A drive and a 5.25 inch B drive
a=/dev/fva0
a9=/dev/fqa0
b=/dev/fha1
b9=/dev/f9a1
And you're ready to rock 'n roll. There is a whole slew of MS-DOS related commands at your disposal like:
  • dosls
  • doscp
  • dosrm
  • dosrmdir
E.g. copying the /etc/default/msdos file to the floppy is simply:
doscp /etc/default/msdos a:
And copying it back is done by:
doscp a:/msdos /etc/default
Well, that ain't rocket science, is it? We can use it right away to fix some Y2K issues. Yes, this version of Coherent won't go past the 31st of December 1999, but it is easily fixed by ATclock and date. Copy them to the /bin directory and reboot. That'll fix it.

You may also be tempted to try another kernel. Several ones are available here. Just copy it to the root and hard-link it to autoboot, e.g.:
ln cohat0 autoboot
Note you can also select a kernel when starting up - just press [SPACE] immediately. Coherent will answer:
If installing COHERENT, please type "begin".
But it is actually a prompt for the kernel name. Obviously, if you type autoboot it will boot the default kernel - just in case you don't know how to get out of there ;-)

Final notes
  • Although Coherent comes with a full fledged C compiler, it won't compile your ANSI-C sources, since it is strictly K&R C. However, GCC - and all the GNU stuff that comes along with it - is available. Still, I like the little beast, since it makes small and fast compilants.
  • Coherent does have virtual consoles. Press [CTRL]-[NUM0], [CTRL]-[NUM1], etc.
  • The archives of comp.os.coherent are compelling reading, featuring Linus Torvalds on who's got the best OS.
  • Additional notes and links on the subject are appreciated. Just leave them in the comments.

Useful links
Good sources for additional software are:
Additional information can be obtained:

Update
There have been consistent reports on Coherent being considerably faster on QEMU 1.7 and up. Well, I tested it and even if there may be some truth to it, it's not very significant nor consistent. If you can deal with it, convert your QCOW2 image to a raw one, because that will give you the required speedup.

But the biggest drawback is that your C compiler will go haywire, spitting out errors on perfectly fine include files. This behavior was still present on the QEMU 2.0.2 version, although slightly less prominent.

The last QEMU version known to work flawlessly is 1.4.

Update
Coherent is Open Source now (thank you, Jason Stevens)!

Saturday, February 25, 2012

Hollywood still doesn't get it

The Dutch Anti-Piracy organization "Brein" has decided to sue ISP's UPC, KPN, T-Mobile and Tele2 after their recent victory in court. I really don't understand why. First of all, blocking a handful of IP addresses hasn't had any effect at all.

But most of all, if you really want to fight piracy you got to have support from the public. And that support is crumbling with each and every effort to enforce compliance to that 10 minutes of pesky messages you get when you insert a "legal" DVD. Yeah, you name them all: SOPA, PIPA, ACTA or whatever they may be called. Crowds are cheering on the streets and can't wait to have them ratified.

Metallica, once one of the most fierce fighters of piracy, has seen the sign on the wall and radically changed its position. These guys are not stupid. They know their stand on piracy affected their popularity, so they took the only decision they could take.

I've never been a downloader. I simply don't like the hassle that comes with it. But I have friends who are. One of them is a gray haired hippie, who also happens to be a record collector. Consequently, a burned CD has little value to him. But he is also a great fan of vintage science fiction movies. You hardly find those movies in the local stores and when you do the prices are outrageous. So every now and then he ordered one at Amazon. When you add all the additional costs they're not quite cheap either.

So when he wanted "to go on the Internet" in the early 2000's I told him to buy a Mac and get XS4ALL. A whole new world opened up to him. One night when we were having an beer and he told me how hard it was to get a decent copy of "Jason and the Argonauts" for a reasonable price. So I introduced him to the world of torrents, clearly stating that although downloading wasn't illegal in the Netherlands, it wasn't quite legal as well.

"Unlawful" isn't black and white down here, but has quite a few shades of gray. E.g. contrary to popular belief marijuana isn't legal here, it's just not.. completely illegal. It's - as we Dutch say - "condoned", which means you aren't prosecuted.

We quickly found a viable torrent and started downloading. It trickled down at about 10 kB/s, but he wasn't in a hurry. And a day or so later it was there. The weeks that followed he went into a kind of download frenzy, but then it settled down. I mean, they don't make movies like that anymore and the more recent ones you can get in the shop.

We met on the street a few weeks ago and we quickly landed on the subject of the recent Ziggo/XS4ALL verdict. He was furious. "Who are they to tell me where I can or cannot surf?!" he said. I told him he still could. I told him to take a look at my blog and simply follow the links.

That evening he phoned me to say everything worked fine and he was currently downloading "Captain America, the first avenger". "I'm gonna boycott them!" he said "I spend about 20 Euros a week on DVD's and if this is how they're treating me, they're gonna lose a customer! That thing is in the store for about 15 Euros - that's too expensive for my taste, but I'm gonna watch it tonight! You won't believe the download speed I'm getting, about 500 kB/s!! It's even got Dutch subtitles!"

I wasn't surprised. Everyone knows that the more popular a title is, the more seeders and leechers are offering it, which really helps to speed up the download. He had already downloaded "The Thing 2", "The Green Lantern" and I'm sure more were coming. So that is in effect what Mr. Kuik, spokesman of "BREIN", achieved. And he's making himself more popular by the minute.

The point is that the entertainment industry seems to be unable to listen to their best customers. They want the world to play by their rules, but every enterpreneur knows that's a very bad business model. Studies prove that the entertainment industry can survive and even make money, but they simply have to start to use their brains ("BREIN" means "BRAIN" in Dutch).

One of these pioneers is "Iron Sky", that partly uses "crowd funding" to raise money. And they will offer the movie for download once it has been in the theatres. Now that's creative thinking. I won't say it will work, but at least they're trying. Most importantly, they have the support of the community.

In contrast, the music industry have tried to tie down their customers with DRM. Needless to say they failed miserably - as I predicted - and nowadays it is very hard to find a CD or download with DRM. It simply doesn't work that way, despite state-of-the-art technology and elaborate schemes to "hide" the disadvantages from DRM to the public.

Now they're relying again on technology to fight piracy, but this time the technology is not in their hands, so it is even easier to find a way around it. Technology has always been a double edged sword for Hollywood. The introduction of the TV almost brought it to its knees, CGI on the other hand, produced some of its most famous blockbusters.

However, it has to realize that the Internet is nothing more than the 21st century equivalent of the TV. It can't be controlled and you can't legislate it away. Hollywood will have to change its game. Mikhail Gorbachev once said "Those who come too late will be punished by life itself".

Hollywood may not realize it, but the Internet is not the last challenge it has to face. In 15, 20 years, may be sooner, every kid with a computer will be able to create his own Hollywood grade movies. There will be digitilized Marilyn Monroes, James Deans, Humphrey Bogarts, landscapes from all over the world, cars from every era. Of course, most of these movies will be very, very bad. But some of them will be great. Most important of all: they will be free. And when they're not ready for the Internet, they aren't prepared for that.

With the turn of the millennium I expected "20th Century Fox" to change its name. It didn't. Now I understand why.

Wednesday, February 8, 2012

My life with Coherent, part 1

In the early 1990ies my company switched from an S/36 system to a Unix system. No one had any knowledge of that "alien" platform, so I became the expert to be. It was not entirely voluntary: I either made it work or I was fired.

Needless to say I spend lots of time behind the terminal, but it just wasn't enough. Working behind a $100,000 machine on a root account while barely knowing what you were doing was kind of intimidating. I was simply too reluctant to perform the necessary experiments.

In order to get more time and a less intimidating environment I started looking for alternatives, but I quickly found out there were few options. There were some MS-DOS based products that allowed me to write shell scripts, but that just wasn't the real deal. Xenix and SCO were too expensive. And then I found out about Coherent.

Coherent is a full fledged Unix that runs on a simple 386 with a few megabytes of memory - incredible, but true. The kernel is just a few hundred KB, so it boots in an instant. It lived happy together with MS-DOS in its own 40 MB partition. But the best thing was its price: only $100. Needless to say I spend a lot of hours with that little beast, porting my C programs and UUCPing with that "monster" machine back at work.

But every machine comes to a grinding halt at some moment in time. Getting a new PC wasn't a problem, but when I tried to reinstall Coherent I found that it didn't support my new IDE drive. Too bad, but since I had become a proficient Unix system admin I really didn't need it anymore.

Still, with every new release of QEMU or VMware I tried to reinstall it - with little result. That is, until QEMU 0.14 was released. After the arduous task of installing Coherent it bombed out just before the final reboot. But the Dutch don't throw away things that easily. I didn't delete the image, instead I simply waited for the next iteration of QEMU.

When I started it, it seemed it hang right away, but the QEMU monitor told another story. It was still alive. It was reading the disk, but very, very slowly.

Hours later the login prompt appeared and I logged in. The whole thing worked, but it was very sluggish. After having worked with it for a little while it became a bit more responsive. It didn't take me too long to figure out it was the IDE emulation that was slowing it down, reading about 1K a second. But once a file was in cache there were hardly any performance issues.

However, when starting up Coherent performed an 'fsck', which made booting a bit tiresome - I assure you that reading 7 megs at 1 KB/s is no fun. The easiest thing was to remove the 'fsck' check, which was easy enough. A quick edit of 'brc' was sufficient. Now I could login after Coherent had read only 150 KB, which was a significant improvement.

But I still wasn't happy. Every time I rebooted I had an empty cache. There had to be another way. Yes, there was, but for that I had to turn to QEMU. QEMU allows you to make a snapshot of the whole virtual machine - memory, CPU, the whole shebang. That not only allowed me to "boot" instantly, but also with the disk cache intact. Ok, now we're talking!



Since the standard Coherent C compiler only supports K&R C, almost all my C programs have a "-DARCHAIC" switch, which enables K&R prototypes. Even after 15 years I still maintain them, so porting my 4tH compiler shouldn't be very difficult. It wasn't. In these 15 years after more than doubling the code base I had only made about 10 syntax errors, which were quickly fixed.

But still there was a problem. Sure, I could use a raw floppy image to exchange files between the host and the VM, but since you can't make a snaphot of a raw floppy I had to reboot the VM after each and every transfer, which kind of defeated the sense of making snapshots.

The solution was to startup the VM with a QCOW2 floppy, which served no other purpose than to define a floppy device in the VM. You can not easily mount a QCOW2 image and so it is consequently not very well suited for file transfer.

Once the VM is up and running you can exchange this virtual floppy for a raw version by switching to the QEMU monitor and issuing:
change floppy0 fat16.img
When you want to take a snapshot you simply return to the dummy floppy by issuing:
change floppy0 dummy.dsk
And subsequently save the snapshot by issuing:
savevm test0
After that I was ready to rock 'n roll. It took me about an hour or so to port my compiler and transfer the executables back to the host. Furthermore, I installed another more modern kernel, applied some Y2K patches and customized my Coherent 4.2 installation.

In the next part of this series I will describe this in more detail and - even better - give you the opportunity to try it for yourself. All perfectly legal, that is..!

Wednesday, February 1, 2012

Venn diagrams: the intersection of morons and judges

Few people know it, but the Internet as we know it, has its roots in military technology. It's predecessor was called ARPANET, which was funded by the Defense Advanced Research Projects Agency of the United States Department of Defense. Some of its design objectives were:
  • Providing for host-to-host "pipelining" so that multiple packets could be rerouted from source to destination at the discretion of the participating hosts, if the intermediate networks allowed it.
  • Gateway functions to allow it to forward packets appropriately. This included interpreting IP headers for routing, handling interfaces, breaking packets into smaller pieces if necessary, etc.
  • Each distinct network would have to stand on its own and no internal changes could be required to any such network to connect it to the Internet.
  • Communications would be on a best effort basis. If a packet didn't make it to the final destination, it would shortly be retransmitted from the source.
  • There would be no global control at the operations level.
During the Gulf war the US army wondered why it was so difficult to take out the Iraqi network. What were these guys using? Later they found it were just off the shelf hardware and software components, stuff any ordinary consumer could get his hands on.

A few years later politicians thought it was a good idea to unleash this military grade technology onto the world, just like they thought it was a good idea to introduce the Euro. In both cases, without thinking about the consequences. The impact of a decentralized, open architecture has become clear now - and they don't like it. And worse, they are unable to keep up.

Yes, Pandora's box is wide open and they are struggling to contain the monster. But they try and manage to make themselves ridiculous and less credible with every step of the way. You can't complain about states censoring the Internet - for different reasons, agreed - and proposing similar legislation at the same time. In the US you have SOPA, PIPA and ACTA and in the Netherlands we have judges like mr. P.H. Blok, mr. R. Kalden and mr. M.P.M. Loos.

They decided not only that IP addresses 194.71.107.15, 194.71.107.18 and 194.71.107.19 had be blocked by ISP's, but also gave BREIN (the Dutch equivalent of the MPAA) the authority to add any IP address they want to that list, which means that in principle they can take out any website at will without ever having to go to court again. Needless to say that these judges not only proved they are completely clueless about the nature of the Internet, but they also violated every rule in the book.

Imagine there was a bookstore selling counterfeit books, would those same judges give a plaintiff the authority to simply close up any bookstore he wants? Of course not! As a matter of fact, they gave BREIN the possibility to change the verdict of the court. Which - by the way - is in conflict with European jurisdiction. Speaking of Europe, Neelie Kroes, the grand lady who got Microsoft on its knees, commented: "Speeding is illegal too: but you don't put speedbumps on the motorway".

That the court came to this decision was no big surprise. One of the judges, P.H. Blok, is also employed (for a fee) by Wolters Kluwer, one of the largest publishing companies in the world. Why he was wasn't substituted by another judge is anybodies guess. If this isn't a conflict of interest, what is?

But we cloggies don't take such violation of our rights laying down. In the days that followed, tutorials popped up on how to install TOR and VPN networks or use proxies and anonymizers. Mirrors were created, proxies installed, by the time the "offical" site went on black there had never been so many ways to reach the Pirate Bay.

Tim Kuik, spokesman of BREIN, commented: "Smart hackers will always find ways to circumvent measures like this." So according to Tim Kuik, if you're able to click this link, you're an accomplished hacker.



What were these guys thinking? Either they knew that their measures were ineffective and consequently merely symbolic or they were completely ignorant of what the Internet is and how its community acts on threats. Either way, it is unworthy of a judge who considers himself to be an expert on the field of IT and law.

So what's next? Outlaw links to proxies and anonymizers? Outlaw access to proxies and anonymizers? Outlaw sites who offer proxies, anonymizers, TOR or VPN? Outlaw technologies like proxies, anonymizers, TOR and VPN? Outlaw writing about proxies, anonymizers, TOR and VPN? Maybe I should emigrate to North-Korea or China. As long as you leave politics alone, you can at least blog about technology!

Of course it doesn't stop there. The weakest link in the current torrent architecture are the centralized torrent repositories. However, other technologies will emerge that eradicate this flaw as well and become completely decentralized. All that is left then is deep packet inspection, a technology that ironically has recently been banned by that same juridical system.

End of story.

Update: One of the blocked IP addresses: a list of computers. The other one: the word "Yeah". This proves how little the verdict has to do with the actual content of a site. There is nothing illegal down there. It has just been taken down because they are in the IP range of the Pirate Bay. And yes, you XS4ALL and Ziggo subscribers can click too..



Update: This almost forces me to change the title of this post: it turns out that we're dealing here with blatant corruption. Read this and this (Dutch).

Update: I wondered several times why the lawyers of the ISP's didn't try to substitute the judges. Well, as a matter of fact, they did! But they were turned down, because "there just aren't enough judges". Go figure.. (Dutch).

Friday, January 20, 2012

Fight SOPA/PIPA, change your license

All over the free world, government laws and court decisions are limiting our access to free Internet. Not that these measures are very useful, most of us hackers are able to circumvent them within minutes. But in essence, these counter measures are simply work-arounds - they do not eliminate the root cause.

The root cause are the big media moguls who successfully lobby our governments and judges to adopt unconstitutional new laws and regulations. Consequently, they are by definition an anti-democratic force. No, it's not about the artists (as a matter of fact, most of them can be considered victims of this industry), it's not about IP (they violate IP laws themselves on a daily basis - just count the lawsuits), it's just about MONEY. Think about it: these industries are obviously willing to trade in our most fundamental civil rights just for a little extra cash. No, they're not the kind of people only a mother can love - they already sold their mothers.

Our politicians are no better. Instead of defying the pressure of a dying industry they have chosen to simply sell out their voters. Often without even having any idea which consequences their decisions may have on their citizens, their own information industry or even the very reputation of the nations they should represent. Ignorance may be a bliss, but not in this case.

So far the community has responded in a variety of ways:
  • Shutting down their own websites in protest;
  • Petitioning;
  • Attacking government and industry websites;
  • Educating users on how to circumvent blocks;
  • Developing new ways to make it even more difficult to block free Internet access.
However, there is one card we haven't played yet. The FOSS community has achieved complete independence from proprietary software. I predict the music and movie industry will follow in the next few decades. They won't disappear, but they will have to compete and one day we will win.

In the meantime we will have to ensure that we will continue to have free and unlimited access to the Internet, our information highway. It is no secret that governments and even the music and entertainment industry itself have benefited from everything the FOSS community has produced. And we can stop that.

GPLv3 was created to defeat any attempts to make free software less free. A new license may prevent governments and the industry to make the Internet less free. Simply don't allow them to use our software anymore. Change the license.

I know that it is one of the pillars of Free Software - anyone may use it, no restrictions. But as I said, I don't consider it a permanent measure. Just until every attempt to restrict the Internet has ceased permanently. Just until the right to free access to the Internet is part of every international treaty and constitution.

I don't know about you, but I just don't feel comfortable someone who denies me to exercise one of my most basic rights is happily using my software..

Thursday, January 12, 2012

Surf naar de Pirate Bay in 5 minuten

Voor degenen, die geen zin hebben om een VPN of TOR in te richten en na het effectief worden van de blokkade van Ziggo en XS4ALL toch naar de Pirate Bay willen surfen zijn er een paar eenvoudige oplossingen - zelfs zonder iets te installeren.

De eenvoudigste manier is om gebruik te maken van een zogenaamde "Anonymizer". Dat is een computer, die vanuit Nederland vrij te benaderen is en voor jou contact maakt met de Pirate Bay of welke andere computer dan ook. Een aardige lijst van gratis anonymizers is hier te vinden. Tegenwoordig zijn er trouwens een groot aantal gespecialiseerde proxies, zoals deze. Het gebruiken ervan is doorgaans erg eenvoudig: tik de URL in en de computer verbindt je door. Een klein voorbeeldje is hier te vinden. Voila!

Een meer permanente oplossing is beschikbaar voor gebruikers van Firefox. De "Stealthy" add-on tovert een F-117 naast de taakbalk, die bij het opstarten rood is. Klik 'm aan en hij wordt groen. Je kunt dan gewoon weer naar de Pirate Bay surfen. Lukt het eerst even niet, klik dan op het pijltje-naar-beneden naast de F-117 en klik op "Configure". Klik dan het vinkje "Something went wrong?" aan en druk op "Ok". Dat zou het een en ander op moeten lossen.

De meest briljante methode is toch wel gebruik maken van je bloedeigen /etc/hosts file (dat op Windows overigens c:\windows\system32\drivers\etc\hosts heet). Voeg daar de volgende regels aan toe:
178.73.210.219 thepiratebay.se
178.73.210.219 www.thepiratebay.se
178.73.210.219 thepiratebay.org
178.73.210.219 www.thepiratebay.org
Ook deze truuk werkt met een proxy uiteraard, maar je merkt er gewoon niks meer van. Tenslotte kun je ook gewoon http://194.71.107.80 intikken, maar waarschijnlijk is die URL alweer geblokkeerd als je dit leest.

Waarom ik dit met u deel? Omdat het zoveelste een bewijs is van de debiliteit van zowel de klagers (de media concerns) en vooral ook de Nederlandse rechterlijke macht. "Content" is allang niet meer gebonden aan het medium, waarop het opgeslagen is en ondanks het feit, dat deze ontwikkeling al twintig jaar gaande is, is de industrie er nog steeds niet in geslaagd om een business model te vinden, dat hierbij aansluit.

Nee, nog steeds klampt ze zich krampachtig vast aan een paradigma, dat de facto allang achterhaald is. Een industrie die zo dom is, verdient het gewoon ten onder te gaan. Ter vergelijking: voerden bioscopen in de jaren '50 en '60 processen tegen TV maatschappijen? Laten oliemaatschappijen electrische auto's verbieden?

Naief is dat de rechtbanken van Nederland zich voor het karretje van deze concerns laten spannen en maatregelen eisen, die technisch gezien neerkomen op het slaan van spijkers in een wegdek. Weliswaar zullen er een paar mensen met een lekke band eindigen, maar na verloop van tijd rijdt iedereen er omheen.

Nog triester is, dat dat de rechter aangeeft dat er nog meer spijkers in het wegdek geslagen mogen worden als "dat nodig is". Trekken we deze uitspraak in "extremio ad absurdum" verder dan eindigen we met een weg, die zo vol spijkers zit, dat zij niet meer de functie van weg kan vervullen.

Daarom vertel ik dit u.

Update: Het linken naar deze pagina is illegaal geworden in Nederland. Overigens ook de links naar het "Tor" netwerk en zelfs naar de Opera webbrowser.

Update: Een paar nieuwe methoden toegevoegd. We blijven scherp!

Saturday, April 16, 2011

The biggest PR clanger in the history of the WWF

We will, wherever possible, seek dialogue and avoid confrontation.
We will record and respond to criticisms we receive, in order to learn from, and avoid repeating mistakes.

From: WWF Code of Ethics

In the past, the WWF has had its fair share of controversies. Condoning and even promoting animal testing and hunting (the former chairman of the WWF, the late Prince Bernard of the Netherlands, was known to take a shot every once in a while himself), interfering in the internal affairs of sovereign states, deporting indigenous peoples, neo-colonialism, collaborating with large companies like Coca Cola, Microsoft, Proctor & Gamble, Hewlett-Packard and Eneco in order to - you won't believe it - "protect the environment", not being so green all of a sudden when it comes to raising cash, supporting genetically modified crops, manipulating scientific research, and finally: making not very tasteful adds.

With a list of controversies like that you start to wonder how they survived. Well, very easy: by having a very good PR department. Whenever a controversy pops up WWF acts like a turtle. It minimizes communication as much as possible and hopes the whole thing blows over. It tries to silence, marginalize or intimidate its critics, but in such a clever way that it doesn't make too many waves. Disputes between its chapters are kept indoors as much as possible. Bluntly lying - if required - is an accepted practice.

Being one of the opponents in their latest controversy - the infamous WWF format - I experienced these tactics first hand. This is my story.

When the "Save as WWF" campaign was launched, WWF Germany created a Facebook page. I guess they never expected that it would cause so much controversy, because critics like me could vent their objections freely and add all the links and pictures we wanted.

The first indication that something fishy was going on came on December 21st when Jon Fenell reported that some contributions were missing. WWF denied everything, saying that were not censoring the site. They had just changed the settings.



In short, only the contributions of "Save As WWF" were shown by default, which meant that each time you had clicked a link, you would have to reset it again. It made browsing, let's say, a little uncomfortable. WWF pulled off that trick several times, changing layout and settings with some bizarre defaults, although - admittedly - Facebook made changes to the page in the meanwhile as well. The first thing going (of course) was the "links" section.



In the first week of January I released the .wwf toolkit, which the WWF initially seemed to applaud. Maybe later they realized that this terribly simple piece of software allowed conversion both ways or maybe it was because they had tried to make their audience believe that supporting Linux was incredibly complex, I don't know. Anyway, after that everything changed.



Sabine Schikorra-Känder tried to convince me that is was all about cuddly panda bears - and who is against that. Of course, I didn't buy that and repeated my arguments again. Note that my objective was to get the WWF around the table and discuss their actions. Despite the many letters and invitations from various organizations and individuals that never happened, but at that moment I was still optimistic. Sabine Schikorra-Känder remained polite, which could not be said of Romy Mlinzk. She called me a troll and despite my efforts to discuss the issue at hand, she persisted in her behavior.



So I set out to find who Romy Mlinzk was. After working my way through many uninteresting personal pages with many flagrant spelling errors, I found out that she was in fact a professional. Not a very good one though, because she should know as a self-proclaimed "social media expert" that it is not wise to leave so many intimate personal details lingering in cyberspace. But I also found the smoking gun: at the time she was working for Dederichs Reinecke & Partner, the company that handles the social media for "Save as WWF". If that isn't enough evidence for you, here is a message she left admitting she was directly involved with the "Save as WWF" campaign.



There even is a presentation of her on the internet, where she describes her experiences with this campaign in detail and has the nerve to call me her "favorite troll" - obviously miss Mlinzk has neither a clue of what the definition of a troll is nor of what "social media" (her so-called "expertise") are all about: interaction. Even with those who don't agree with you.

As if insulting me wasn't enough, "Save as WWF" threatened me the very same day to kick me off their Facebook page. Things were getting ugly. In the final weeks of January I was cranking out new releases of the .wwf toolkit by the minute, getting lots of feedback of obviously satisfied users.



In the meanwhile, the paper industry had launched their own "green initiative" which was a direct response to the "Save as WWF" campaign. WWF Italy started a collaboration with forest managers to "develop awareness on the issues of sustainable management of forest resources" and "promote (..) the purchase of forest products"!

I was quietly hacking .wwf tools to make them work under Windows as one of my users had requested. Since "Save as WWF" had initially shown so much enthusiasm for my initiative, I decided to post several links and pictures at their site - just to see that they disappeared almost instantly! Wow.. I thought they didn't censor or was I being a bit too naive?

So I decided to take my business elsewhere and complain at a site which another user accurately described as "the place where WWF complaints go to die". This is where the WWF channels all criticism. You are kindly treated by a spin doctor who gives you a lot of credit for your remarks, let's you blow off some steam and that's it. I'd rather be talking to ELIZA, she's a much better listener. To give you some idea of how this works, here is a little sample:
Thanks for the heads up on this. We're in the process of correcting it to give credit where credit is definitely due. Apologies for the oversight.

I see you have also posted your comments there. Many thanks for doing that. It is good to have your point of view in there.

But it is great that you air these opinions and get the opposite point of view across. We link to Get Satisfaction from every page on our site - so people can find this string and weigh up the arguments for themselves.

Thanks for giving your input. I know it may seem easy to say, but truly: it is valued and it is heard.

I had also pointed out on Facebook that most users didn't like the WWF drivers, so "Save as WWF" decided to spike the results by asking their friends to push "the green thumb" button, a method well known by the readers of the Dutch "Geen stijl" blog, who are known to sabotage just about every single poll in the Netherlands - and abroad if they get the chance. Not something you would expect of the respectable WWF, wouldn't you?



Until recently, most of the criticism came from information technology blogs and the paper industry. But even advertising agencies have started to pick up the "Save as WWF" debacle. Also the mainstream media are slicing and dicing the initiative, saying the people behind it "have an arrogance that doesn’t help their cause" and that "whole concept of unprintable files is ridiculous" in itself.

The latest scam of "Save as WWF" was exposed by Marco Fioretti who showed that WWF Germany is still frantically trying to save face by slightly modifying publications of WWF International. Still, the influx of "friends" at their Facebook page has been reduced to a trickle. Although "Save as WWF" have promised to come out with a new version shortly - as they have numerous times - the question is: will anybody remember or even care enough to make it worth the effort? Or does it even make it through the virus scanner?



It seems that what has been called "the biggest PR clanger in the history of the WWF" is slowly coming to an end. Finally. Rest in peace.

In the end, where was the FOSS all about, after over 12,000 Facebook friends and these "hundreds of thousands" of downloads? 394 documents. Yes, that's it: 394 documents. Count them.

And yeah, a handful of them are mine. Go figure..

Update: Even though the "Save as WWF" campaign was nominated twice it don't win any of these awards. I wonder why..

Update: Jung von Matt won the ADC Grand Prix 2011, I'm not going to deny that - nor attempt to hide my astonishment. I don't know where these people live, it must be a different planet.

Update: Still, the WWF won't go all the way to preserve forests:
In its first year of existence, the organisation's website netted over $100,000 for rain forest protection through the sale of paid memberships. In their first six months of existence the group received seed funding from the government of Norway. They are the world's first eco-porn organization. However, the organisation's unorthodox methods have made it difficult to distribute the money it makes. The Norwegian chapter of the Rainforest Foundation Fund as well as the WWF both in the Netherlands and in Norway have refused to accept donations from FFF. As a result, F**k for Forest is working on a project to work directly with indigenous communities in Costa Rica and the Brazilian Amazon rainforest.

At least the members of this organization are doing it to each other: not to their donors or indigenous people. The link is environmentally safe: it points to Wikipedia.

Update: Those who think I've simply thrown all the dirt I could find to the WWF, think again. Recently, the German WDR network released a documentary which proved I didn't even have to search that long. It caused a major embarrassment to the WWF in Germany, causing it to lose many donors. Try to print yourself out of that one!









Update: This comment was published here. It is allegedly written by someone working at Jung von Matt. Although I have been unable to confirm it, it gives a unique insight on how these marketing agencies think and operate:
(Disclaimer: This is an idea which originated in an advertising agency from Germany and was later brought forward to the WWF. I work for that agency, but had nothing to do with the project.)

WWF is available for Windows, but it's only prominently presented for OS/X on the website. Of course a roll-out covering all OS's would be great, but that's unfortunately not how advertising agencies work. While the idea is in it's core great for the planet, the motifs are mostly utilitarian. "Clever" ideas like this score high at award shows and push the agency up in the creative rankings. Therefore it's important for the agency to pump out projects like this onto the market, just developed enough to be regarded as a "real" product which can be entered into award shows.

But upon detailed inspection these ideas are often still lacking in execution. In this case it was important to launch as quickly as possible and just having an Mac version (which was easiest to produce) was good enough. Oftentimes that's where development stops and when the awards are won the projects will no longer be cared for. In the case of .wwf the agency had to provide a Windows version just because people demanded it and the WWF probably said so. That's why the layout for the website hasn't changed to accompany prominent buttons for both Mac and Windows versions, but just received a measly text-link update.

This of course is a pretty f**ked up way of seeing things and I totally disagree with it. I have always tried to put real meaning into the advertising I co-create, sometimes successfully, sometimes less so. The big and established advertising agencies have an insanely hard time to part with their (long gone) successes of showing the message on to the consumer. They just don't get that the Internet is a completely new way for people to communicate and that consumers want to be talked to like intelligent people with a will of their own.

But all is not bleak. There is a new generation of insanely talented and motivated people who see the need to change this old thinking really quickly, or risk a great fall.

Anyways, that's the reason why the project feels as if it's not completely thought through and will probably not grow any bigger. At least not as long as someone else than the agency is in charge.

Well.. That explains a lot! The WWF page, their Facebook page and the software themselves have never been updated after they collected their prizes - despite all their lofty promises. Again: as I predicted.

Update: LOL! I announced the minor update of the .wwf toolkit in the "Save as WWF" Facebook page, which hadn't been updated for months. Next day, the "Discussions" tab was removed. Obviously, nobody needs support anymore. You're on your own!

Update: It seems there are two winners here: the advertising industry and the printing industry. Losers are the WWF, who really took a beating last year and the poor users who believed an organization that never seriously considered to support them. Net result: lots of frustration and some broken machines. Pity.

Update: A list of over 50 incidents concerning WWF (German). When you're contemplating to give this rather murky organization another dime.. I wonder whether Doutzen Kroes is aware of that or whether she's just in it for the money.

Sunday, February 13, 2011

Using original WWF artwork on your Linux box

Disclaimer: This information is for educational purposes only. If you perform the procedure described here, you assume full responsibility for any and all consequences, including legal action from the copyright holder.

Of course you can create original artwork for .wwf on your Linux box, but wouldn't it be nice if you can use the professional artwork Matt von Jung created for this? But how do you obtain it? Very simple: use the OS/X package. First, download it. Then unzip it:
unzip SAVE_AS_WWF_1.0.pkg.zip
cd SAVE_AS_WWF_1.0.pkg/Contents

The next step is to decompress the .pax archive:
gzip -d Archive.pax.gz

This seems like a dead end, but it isn't. The .pax archive is simply a cpio file:
cpio -i < Archive.pax
cd 'SAVE AS WWF.app/Contents/Resources'

We need the .icns files. That seems like a dead end as well, but not quite. We're almost ready. If you want to extract the icons on your own machine you have to install icns2png, which can be obtained here. Installing it is pretty straight forward:
tar -xzvf libicns-0.7.1.tar.gz
cd libicns-0.7.1
./configure
make
su
make install

It prefers you install Jasper or OpenJPEG, but I didn't have much luck with that. You don't need it either unless you want to extract the high resolution icons. After you've installed it, enter:
icns2png -x wwf.icns
----------------------------------------------------
Reading icns family from wwf.icns...
  Saved 'is32' element to wwf_16x16x32.png.
  Saved 'il32' element to wwf_32x32x32.png.
  Saved 'it32' element to wwf_128x128x32.png.

Done! You can also extract the icons online. It depends on what you prefer, the result is the same.

If you need still another format, use ImageMagick better known to some as convert. Now install the icons in the appropriate directory (that depends on your desktop environment) and you're done. Enjoy!

Monday, February 7, 2011

How to reheat 30 year old spaghetti

A long time ago, when I was still a student, I got my hands on a very fascinating book called "The effects of nuclear weapons" from the Atomic Energy Commission. It was filled with diagrams and tables and enabled you to calculate the effects of a nuclear attack. After five long hours, I got my first results. And it was 4 AM. I knew when I stayed on working and calculating, I was gonna lose a lot of sleep.

However, it was 1981 and at my college we had a highly modern machine: a real PDP-11 with a massive memory of 256 kB and two (floppy) disk drives. Those were the days of the NATO Double-Track Decision, which large parts of the Dutch population opposed. I decided this program was going to be my contribution to that discussion. I called it TEONW (The Effects Of Nuclear Weapons). It is full of cynic puns against the Reagan administration.

I spend nights digitizing the graphs in that book, and coding them in BASIC V10B-02. I had never heard of structured programming and to debug the program I needed a room as long as the listing. If the program said GOTO 5670 I really crawled to line 5670! I never got all the bugs ironed out.

After I had finished college I no longer had access to a computer, but I printed a listing which I entered line by line in the Sinclair ZX Spectrum. I also added some assembly to mimic the green-on-black terminal of the PDP-11.

When the Z80 Spectrum emulator of Gerton Lunter came out, I took the Betadisk, wrote BDDE (BetaDisk Dump Extractor) and transferred the program to a .Z80 file, which may still be found on the Internet. Ten years had gone by.

The .Z80 file was moved from MS-DOS to Linux. I extracted the code with FUSE-utils "listbasic" and made it run under "blassic". By then twenty years had passed.

In the meanwhile, I had been busy writing my 4tH compiler, which is a highly portable bytecode Forth compiler. A few years ago, I added floating point support. What had begun its life as a little toy was now powerful enough to run a program like TEONW. Thirty years later.

TEONW is a relatively small program, just 13 kB source, but it is such an awful mess that I was barely able to understand and code the entry of the basic parameters: yield, altitude and population density. If I ever wanted to convert this program I needed some help. Badly..

TEONW consists of about 500 lines, each with its own line number. Since it is written in Minimal BASIC, every IF-statement is followed by a GOTO. No ELSE, sorry. In order to expose the structure, I needed to get rid of all superfluous labels, i.e. the labels which were not jumped to by either a GOSUB or GOTO. For that I wrote a simple 4tH program, the "uBasic unlabeler" or ubulabel.4th for short. It parses the BASIC program, makes a list of all GOTO and GOSUB labels and then removes all unused labels. That reduced the number of labels to about 100.

But I had still very little insight in the structure of the overall program. Comment was scarce and terse. Instead of making a flow diagram myself, I decided to let the computer do that for me. If you talk about generating diagrams, you talk about Graphviz. This indispensable tool has saved my life more than once - and it would save it once again.

Generating Graphviz code is trivial. I had written Graphviz converters before and I didn't doubt for a moment that I could pull off this one. 4tH excels in parsing text and I didn't even need a full parser here. Just REM, GOSUB and GOTO. Since 4tH also features a conversion program template, all I needed to do was to fill in the blanks. ub2dot.4th was born.

It is basically a very simple 50-lines program. It keeps track of the line it is parsing and when it encounters a GOSUB or GOTO it generates a Graphviz line. Of course, if no GOSUB or GOTO is encountered, it simply executes the next line. That had to be taken into account as well. But this rule has an exception as well: if the last statement is a GOTO or RETURN, the next line will never be reached from that point.

I generated the diagram from the .DOT source, which resulted in this diagram. The black lines are jumps, the red ones are subroutine calls and the blue ones represent normal program flow.



That may not give you much insight at first glance, but when you look carefully, you see some patterns arise. E.g. the code to present the results of the calculation are at the bottom. You can clearly see the different sections for underground explosions and air blasts. At least it helped to chop up the code into manageable chunks.

Since the underground explosions took the least effort, I decided to code that one first. At least it allowed me to set up a basic structure for the program and test it. But first I had a few design decisions to make.

Since this was an all floating point program I decided to use the 4tH Preprocessor (pp4th). Floating point support is coded in high level 4tH, which is not supported by 4tH itself. This meant the code would be rather unreadable. The preprocessor however does offer special floating point facilities, like the entry of floating point numbers without tedious conversions.

4tH offers two floating point libraries. One is rather bulky and fast with a high precision and its own floating point stack, the other is lean and slow and uses the standard data stack. The latter stores the numbers on the stack in standard form, i.e. mantissa and exponent, which is easy to read - if you know what you're looking at. Another added advantage is that it is easier to convert to the dedicated floating point stack version than the other way around.

Forth is a language that is centered around a data stack. It is considered good style to use as few variables as possible. There are non-trivial Forth programs that use only one or two variables or even none at all! However, given the complexity of the task at hand, I decided to use the stack as little as possible and revert to the BASIC variables instead, 25 in all. I never regretted that decision.

Note I had digitized a lot of graphs and these now popped up as clusters of jump instructions, e.g.:
587 IF J2<=-5 THEN GO TO 2007
590 IF J2<=0 THEN GO TO 690
600 IF J2<=5 THEN GO TO 730
605 IF J2<=12.5 THEN GO TO 770
610 IF J2<=25 THEN GO TO 810
630 IF J2<=40 THEN GO TO 840
640 IF J2<=55 THEN GO TO 880
650 IF J2<=62 THEN GO TO 920
660 IF J2<=84 THEN GO TO 960

These were restored to their original table form:
create calc-under
  620 , ' under>62 ,
  550 , ' under>55 ,
  400 , ' under>40 ,
  250 , ' under>25 ,
  125 , ' under>12.5 ,
   50 , ' under>5 ,
    0 , ' under>0 ,
  -50 , ' under>-5 ,

:this calc-under does>
  >r begin fdup r@ @c s>f f% 10e f/ f<=
  while r> cell+ cell+ >r
  repeat
  fdrop r> cell+ @c execute
;

The second entry in the table is a pointer to function, in short: a jump table. The greatest challenge however, was to recreate the spaghetti program flow in structured programming, e.g. from this:
1592 IF J2>=-5 THEN GO TO 1594
1593 GO TO 1596
1594 IF J2<=0 THEN GO TO 1980
1595 GO TO 1600
1596 IF -J5/(J1^.4)>200 THEN GO TO 1600
1597 IF M(1)>M(2) THEN GO TO 1980
1599 IF M(2)<200*(J1^(.4)) THEN GO TO 1980
1600 LET S5=INT ((((((M(2)/1000)*(M(2)/1000))*PI)-(S4/J3))*(J3*.75))+S4)

To this:
J2 f@ f% -5e f<
if
  height f@ fnegate yield f@ f% 0.4e f** f/ f% 200e f>
  if
    S4 f@ density f@ fover fover f/
    1 m f@ f% 1000e f/ fdup f* pi f*
    fswap f- fswap f% 0.75e f* f* f+ ftrunc S5 f!
  else
    1 m f@ 0 m f@ fover f> >r
    yield f@ f% 0.4e f** f% 200e f* f< r> or
    if casualty-corrections then
  then
else
  J2 f@ f% 0e f>
  if
    S4 f@ density f@ fover fover f/
    1 m f@ f% 1000e f/ fdup f* pi f*
    fswap f- fswap f% 0.75e f* f* f+ ftrunc S5 f!
  else
    casualty-corrections
  then
then

I must admit, sometimes I was so desperate that I took refuse to some unconventional techniques in order to get an idea what for Petes sake I was trying to do - thirty years ago.



When I first ran it, it failed obviously. The tedious task of debugging was about to begin. I quickly decided that I would need some special debugging aids in order to complete this task, so I wrote a short routine in both BASIC and Forth that allowed me to examine the variables at certain stages of execution:
9000 PRINT "yield=";J1;" J2=";J2;" density=";J3;" J4=";J4
PRINT "height=";J5
PRINT "P1=";P1;" P2=";P2;" 210J=";R1;" 42J=";R2
PRINT "crater-radius=";S1
PRINT "crater-depth=";S2;" crater-rim=";S3;" S4=";S4;" S5=";S5
PRINT "V1=";V1
PRINT "V2=";V2;" V3=";V3;" V4=";V4;" V8=";V8
PRINT "W1=";W1
PRINT "W2=";W2;" W4=";W4;" Z1=";Z1;" Z2=";Z2
PRINT "Z4=";Z4
FOR N=1 TO 5: PRINT "M(";N;")=";M(N);" "; : NEXT N: PRINT: RETURN

Now you understand why I was so happy that I kept with the original variables instead of going for the full Forth conversion! All in all the basic conversion proved to be pretty good. Only one piece of code needed a full rewrite.

Am I finished yet? No, debugging takes a lot of time - but I'm not in a hurry. At some point in time I will have enough confidence to send it into the world. Hopefully, I will have given it another thirty years of useful life. Time, it wouldn't have had if I had left it in this state.

And that would have been a pity, because it is the oldest program of my hand that survived this long. The rest was left decaying on an ancient 8" floppy. Missing in action, presumably dead. But even if they had survived, would I be likely to repeat this exercise? No, probably not.

Reheating spaghetti takes a lot of time. It's better to cook some fresh pasta. ;-)

Thursday, February 3, 2011

Running shellscripts under Windows

Although many programmers look down on shell scripts, they can be an effective way to quickly realize the required functionality in a minimal amount of time. Especially if you want to tie several already available utilities together.

So when I set down to make a FOSS equivalent of the abysmal and closed source WWF drivers, shell scripts seemed the way to go. I already got PDFtk, Ghostscript and the Poppler utils, so all I needed was a bit of glue between them.

The people at the "Save as WWF" Facebook page wanted a GUI, so I had to throw one in. That wasn't much of a problem either, because we got KDialog, Zenity and Dialog, which cover pretty much all the desktop environments around. Starting with a skeleton script I had once posted I was able to release the first version at Freshmeat within a single day. Several releases followed after the first enhancement requests came in. Until that day..

A user asked me whether a Windows port was possible. Wow.. MS-DOS batchfiles are so crippled that converting them was not an option and I am unfamiliar with the newer Windows scripting facilities like PowerShell. Since I use Windows very little I didn't feel like learning that one. So I asked myself the question, can I run the scripts within Windows?

Running them within Windows means you have to fool them into thinking they're running in a Unix environment. Since it seemed pointless to let users install hundreds of megabytes just to run a couple of shellscripts I settled for MSYS. MSYS is just a few megabytes and offers almost all the Unix commands and facilities you're familiar with. Furthermore, it does a pretty good job of silent conversion between the Unix and Windows PATH conventions.

The next question was, can I get Windows ports of all the utilities required. The answer is yes, there are ports of Zenity, WGET, Poppler utilities, Ghostscript and the PDF toolkit. All bases are covered, now let's rock 'n roll!

To my surprise the installation script almost worked out of the box. The only thing was that whoami is not part of MSYS. I know that you use id -un nowadays, but that wasn't much help, because although id correctly returns my Windows login, I used whoami to see if the script was running with root privileges. I decided to make my own whoami in /usr/local/bin, doing a simple echo. That didn't seem like much of a problem, because if the Windows user in question wasn't running with admin privileges, he wouldn't come that far anyway.

The second thing I had to fix was the #!/bin/bash line. MSYS doesn't "have" bash, only sh. Since sh is simply a link to bash, I decided to change it to #!/bin/sh. A few unquoted environment variables posed another problem, but that was quickly fixed. Finally, Ghostscript comes with a whole lot of Unix shellscripts, which I decided to use. Unfortunately, there are two annoying problems:
  1. Ghostscript isn't added to the PATH;
  2. The Ghostscript shellscripts assume gs is installed, which isn't.
The first one was fixed by adding the lib directory of Ghostscript to the PATH and the second was fixed by scanning for the Windows executable and create a small gs shellscript in /usr/local/bin. Then it ran like a charm.. from the prompt!

The Windows version of Zenity is very good, installing it is a breeze. It adds the Zenity executable to the path automatically. But in order to make it run as advertised you have to wrap the whole thing in an MS-DOS batchfile. Since the shellscripts are installed in /usr/local/bin I needed that path in the PATH, which you achieve by adding the --login switch to sh.

I decided to generate all these tiny scripts and batchfiles from the Windows installation script and that was the moment I really stumbled into the annoyance of Windows backslashes. The backslash is Unix' escape character, which makes it particularly difficult to handle. To write a Windows batchfile I decided to use the following sequence:
echo -n "bg_start ..\..\bin\sh.exe --login -c wwf2pdf" > /usr/share/wwftk/bg_wwf2pdf.bat
echo -e "\r" >> /usr/share/wwftk/bg_wwf2pdf.bat

The -n switch suppresses the terminating linefeed, while the -e switch enables the expansion of special control sequences. Which in this case results in the familiar carriage return-linefeed sequence.



The BGstart utility is required to suppress the launching of the sh window, which is featured so prominently in some ported FOSS programs. A few shortcuts and we're done. The icons for the shortcuts were converted online. Quickly and painlessly. And then it ran. Or did it?

The backslashes were back with a vengeance. Zenity returns a Windows path, which is gracefully handled by MSYS, but when it was displayed all backslashes disappeared or made the messagebox look very queer.



I never wanted special Windows versions of the scripts, the idea was that one version runs anywhere. So, how could I transparently handle this one? The answer was one single line:
SHOW=`echo "$3" | sed 's|\\\\|/|g'`

This one converts any embedded backslashes to slashes. Why slashes, you may ask. Well, first it settles the matter once and for all and second, it reminds the Windows user he owes this functionality to a Unix script.

Of course, there are many things that could be improved. A true Windows installer may make it all much easier for the casual user, but again, I'm not prepared to put so much effort into a skill I rarely use. Furthermore, if people think it is important they will step up and solve it. Because this is FOSS.

I'm now looking for someone who does the port to OS/X, so we cover the entire spectrum. If only to prove that the community does a better and faster job than a closed source company..