Until now, that is.
I plan on posting about my history and MO as a programmer, and about some of the things I've come up with along the way to make my job easier and/or more fun.
Also, I'm a total geek who loves movies, tech gadgets, politics, cars, etc. etc. etc... I'm sure I'll fold thoughts and observations completely unrelated to programming here as well.
Here's a little morsel to kick things off...
#define MAXCMDLINE 1024
int simple_cli(char *filename,int (*parser)(char *cmdline),int count)
FILE *ifile = NULL;
int status = 0;
for (cmdline=0;count-- && !status && (ifile = fopen(filename,"r")) != NULL;fclose(ifile),cmdline=0)
while (fgets(cmdline,sizeof(cmdline)-1,ifile) && (!(status=parser(cmdline))));
int parser(char *cmdline)
main(int argc, char *argv)
(Note: My preferred environment is GNU/Linux, that's what I'll target in this blog.)
This little program should compile with gcc into an executable with no problem...
The meat of it is the function "simple_cli". All it does is:
- a filename,
- a pointer to a function that takes a char* and returns an int (the line-processor),
- and a repetition count,
- it opens the file,
- reads it line-by-line, executing the line-processor on each line,
- until it reaches either
- end-of-file, or
- the line-processor returns a non-zero value,
- whereupon it closes the file, and
- returns the last return value of the line-processor.
Well, the magic isn't really in this program, but in the environment it runs in, in the file it reads from to be precise;
A simple invocation could look like this:
bash-3.2$ echo Hello, world\! > infileBut here's a trickier one:
bash-3.2$ cat infile
bash-3.2$ a.out infile
>> Hello, world!
bash-3.2$ mkfifo infifo(Just before that last line, I hit
bash-3.2$ a.out infifo &
bash-3.2$ cat > infifo
>> Hello world!
>> Hello again!
+ Done a.out infifo
In the second example, the simple_cli function opens whatever filename you passed in, and in this case it's a special file known as a fifo, a.k.a. a "named pipe." The cool thing about fifos is that fifo readers and writers block (by default) until the peer attaches to the fifo; in that second run of a.out above, the program was sitting there waiting for me to type in "Hello world!" and "Hello again!", and used the "parser" function to immediately spit them back out.
So, depending upon how smart your "parser" routine is, and where you call "simple_cli", you can do all kinds of neat tricks; Smart "asserts," simple config-file readers, embedded CLIs or extension languages... These are things I'll get into down the road.