Next: , Previous: , Up: Making Genparse Files   [Contents][Index]

2.1 A Simple Application

Suppose that we want to write a C program that outputs a given text file some number of times. This is a simple, and perhaps not terribly useful program, but its simplicity will help illustrate the utility of Genparse.

Our program, mycopy1, might look like this:

/* mycopy1.c */

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
  int c, i, n;
  FILE *fp;

  n = atoi (argv[1]);
  fp = fopen (argv[2],"r");
  
  for (i = 0; i < n; i++)
    {
      while ((c = fgetc (fp)) != EOF)
	fputc (c, stdout);
      rewind (fp);
    }
  
  fclose (fp);
  return 0;
}

The user is expected to invoke mycopy1 with two items on the command line: an integer followed by a filename. The former is the number of times that the latter should be displayed. While this program accomplishes what we set out to do, it is not very robust nor user friendly. For example, if the user specifies a negative integer, the program does not display a warning or error message (in fact, it displays nothing). If the user does not know what is expected on the command line, how will he or she find this information out (assuming that nice documentation, such as what you are now reading, does not exist for mycopy1). Furthermore, wouldn’t this program be more flexible if there were a default number of iterations, the user could specify the command line parameters in any order or omit some altogether?

All of these issues, and perhaps others, can be addressed in a number of ways. Traditionally, the author of mycopy1 would publish the command line format, typically in a man page, and write a routine to pull the command line parameters out of the argv array, assuming that the format was followed (not unlike what we’ve done for mycopy1). However, as the number of command line parameters increases, this task becomes much more difficult and cumbersome.

With the introduction of the getopt () and getopt_long () functions, now part of the GNU C Library, a great deal of the command line parsing burden was lifted from programmers. The getopt () function takes in an argv-style command line and assumes that it contains a series of command line options. An option is indicated with a single character preceded by a dash - for example, -o or -Z. These options may be followed by a command line parameter - for example, -o or -Z 3. Using getopt we could add an -i to allow the number of iterations to be specified. The advantage to doing so is that it would no longer matter where on the command line -i appears, and if -i does not appear at all, we can assign a default number of iterations.

The getopt_long () function extends getopt (), by allowing long options to coexist with single character options. Long options are preceded by two dashes and may be more than one character long - for example --iterations. Long options may also take parameters, in the form --option param or --option=param.


Next: , Previous: , Up: Making Genparse Files   [Contents][Index]