An extreme example of C preprocessor (ab)use

June 22, 2006

A while back, ran this entry on C preprocessor abuse. Unfortunately (and in fact as noted in the comments), this is nothing to the true devotee of turning C into a different language.

This preprocessor trail was blazed early on, in the V7 Bourne shell, written by Steve Bourne. The story goes that Bourne didn't really like C all that much, and instead much preferred Algol. Armed with the new C preprocessor he saw the opportunity to fix this problem and created what later generations would come to call 'Bournegol'.

For a long time this could only be properly appreciated by people with a V7 source license. These days, has changed all that, so I can show you this 100% genuine typical excerpt (taken from cmd.c):

LOCAL VOID    prsym(sym)
    IF sym&SYMFLG
    THEN    REG SYSPTR    sp=reserved;
        WHILE sp->sysval
            ANDF sp->sysval!=sym
        DO sp++ OD
    ELIF sym==EOFSYM
    THEN    prs(endoffile);
    ELSE    IF sym&SYMREP THEN prc(sym) FI
        IF sym==NL
        THEN    prs("newline");
        ELSE    prc(sym);

(I have changed the original tab-based indentation to four spaces, so that this fits better on your screen.)

The Bourne shell was also infamous for its clever approach to memory allocation, where it didn't so much allocate memory as use it and wait for the SIGSEGVs to come rolling in as a sign that it should expand the process's memory with setbrk(). One result of this was that sh served as an excellent stress test for a new Unix port's signal handling; another was that if you hit a real SIGSEGV due to a Bourne shell bug (and there were a few), you (eventually) got an error message about being out of memory instead of anything useful like a core dump.

Written on 22 June 2006.
« A year (and a bit) of WanderingThoughts
Respecting Unix signals »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Jun 22 02:07:48 2006
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.