Wednesday, September 20, 2006

Magical Unix Part-A of Z

Dont know if there are going to be A to Z versions of Magical Unix.... but it has to start with A only ;)

There was a problem kicked in my mind long ago --
"How can you write a dynamic program ?"
I did solve this problem after lots of thoughts ... today I just thought of preserving it through blogging .. and thus making this blog feel little active ;)

Lets simplify the problem --
The program while compiling should take a C program as the input, compiles that C program, and produces a.out of this new C program ?
Example --

$ gcc
... <--------- waiting for an input which should be a C program .. So I write - main() { printf("hello there\n");} EOF (or Ctrl-D) $ ./a.out hello there $ So how it can be done ??
$ cat dynamic_prog.c
#include "/dev/tty"

$

THATs ALL !!!!! :-) wasnt that a easy task ... How can we explain this -

In unix, current terminal is /dev/tty so when we include this file, the preprosessor tries to include this file, it actually replaces /dev/tty with the current terminal, and so expects an input terminated by EOF (Ctrl-D in unix). And when user enters a C program and as soon as it presses C-d , gcc compiles the C program entered by the user ..... and prepares the
magic a.out ........

Question 1 : How should we achieve the same in Windows ?
Question 2: How should one give beautiful messages on the screen for user to type in a C program... :-) ... like -

$ gcc dynamic.c
Please type a C program here ........ then press C-d to compile that


Well I dont have an answer to this ... Any takers ?