Fingering->pointers
Sudheendra Hangal's randomly updated weblog

20041028 Thursday October 28, 2004

From here to paternity

Paul Reiser's book on Babyhood is one of my favourite baby books. It's funny and insightful at the same time, and written from dad's point of view. Strongly recommended for fellow to-be dads.


(2004-10-28 06:46:57.0) Permalink

20041025 Monday October 25, 2004

V.S. Naipaul in Bangalore

V.S. Naipaul was in Bangalore for the second time this year, promoting his new book, Magic Seeds. The Landmark bookstore had arranged a reading last week. The moment he arrived at the venue, he was mobbed by hordes of photographers. The last time he was in Bangalore, he seemed to be enjoying the adulation, but this time, he just blinked lazily at the cameras, looking a bit like a fat Wiltshire cat. His hands shook quite a lot, and he was not enjoying himself. He looked hot and bothered - one wonders why he has to dress up in hot weather - and he probably just wanted to get the session over with.

The reading was disastrous. Someone decided to get two other people to read from the book instead of Naipaul himself. That was disappointing, for the man speaks almost as well as he writes. He picks his words carefully, and has a considered manner which, though not condescending, appears to be gently appending to every sentence, "Do you understand, my child ?"

The reading was followed by a question-and-answer session. Sensibly, no one wasted time asking him anything about the book. Magic Seeds is something of a sequel to the already boring Half a Life. I asked him, having read him complain in his books about the shoddiness of Indian products, what he thought had effected the creation of the rather posh mall and bookstore we were in. His take was to the effect that "If people need things badly enough, they will happen."  It's not clear that people don't need good roads in Bangalore badly enough, though. Other people asked him how he approached death (with great pleasure), who he kept in mind as he was writing (often one specific person) and how difficult writing gets as one grows older (a lot).

(2004-10-25 06:35:55.0) Permalink

20041013 Wednesday October 13, 2004

Roses are red, and so should be warnings

I am always surprised that text-based user interfaces in computers have not fully utilized the power of colour. Ever tried to scroll through 10,000 lines of history in an xterm trying to locate that particular error message, or the command you typed ? So why don't shells emit user input and program output in different colours ? Why don't programs emit errors and warnings in colours that stand out amid a sea of text ? If only K&R had provided a colour argument to printf...

Anyway, here's one sample implementation of a "warning printf". It makes warnings stand out by printing them in red. It's pretty portable and works on every unix terminal (that I use :-)

// include stdio and stdarg
void wfprintf(FILE *fp, char *format, ...)
{
    int is_tty = isatty (fileno(fp));
    va_list ap;
    va_start(ap, format);
    // ANSI escape for the colour
    if (is_tty)
        fprintf(fp, "%c[31m", 0x1B); // 31 is colour red

    /* print out real message */
    (void) vfprintf(fp, format, ap);

    // reset terminal
    if (is_tty)
        fprintf (fp, "%c[m", 0x1B);
}

Unfortunately, though, I haven't discovered any way to check isatty() on the Java platform.

(2004-10-13 03:56:32.0) Permalink Comments [1]

20041005 Tuesday October 05, 2004

Forgot a case ?

It was surprising to see that the following compiles without warning using both the Sun C compiler and gcc:

#include <stdio.h>
int main()
{
    enum {RED, GREEN} color = RED;
    int x = 0;
    switch (color)
    {
      RED: x = 1; break;
      GREEN: x = 2; break;
    }
    printf ("x = %d\n", x);
    return 0;
}

Missed the case keyword and it still compiles fine ?!
(You write this kind of code after staring at too much Verilog)

The semantics of such code appears to be that nothing inside the switch statement is executed. This caused some head-scratching in one of our programs recently. gcc -Wall does report it though; yet another reason to always run it with every compile.

Wonder why the C syntax explicitly permits this:

<selection-statement> ::= if ( <expression> ) <statement>
                        | if ( <expression> ) <statement> else <statement>
                        | switch ( <expression> ) <statement>

<statement> ::= <labeled-statement>
              | <expression-statement>
              | ...

<labeled-statement> ::= <identifier> : <statement>
                      | case <constant-expression> : <statement>
                      | default : <statement>

And as usual, the Java language does not allow you to write nonsense as easily as C:

<switch-statement> ::= switch ( <expression> ) <switch-block>

<switch-block> ::= { {<switch-block-statement-group>}* {<switch-label>}* }

<switch-block-statement-group> ::= {<switch-label>}+ {<block-statement>}+

<switch-label> ::= case <constant-expression> :
                 | default :

(2004-10-05 23:59:48.0) Permalink Comments [1]


archives
links