Monday, May 11, 2020

Programming in FreeDOS - reading the command line

I wanted to do a few videos about how to do programming in FreeDOS. Since my favorite DOS programming language is C, this video is about C. You can follow along with any of our C compilers. You can install them if you do a full install, or you can run FDIMPLES and install the compilers from there.

People sometimes ask, "How can I write a program in FreeDOS that reads the command line?" And that's an important question, because FreeDOS is primarily a command line operating system. In this video, I'll go over two simple methods to parse a command line in C.


Here's the basic method:

You want to write a program that loops over the command line arguments. The arguments are stored in a vector (an array of strings) called argv in this program. The number of the arguments is passed in an integer variable called argc. You could name these something else if that's better for you, such as arguments and argcount, but here I'm using argv and argc.

In the simplest example, we'll just read the command line options:
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv)
{
  int i;

  for (i = 1; i < argc; i++)
    {
      printf ("%d: %s\n", i, argv[i]);
    }

  exit (0);
}
If you wanted to parse the command line for any options passed to your program, you would need to examine each argument in turn and evaluate it for a possible option.

Because parsing the command line is a common thing, you might not be surprised that there's a library to do that for you. getopt does the hard work of looking for options. You need to pass it the argument count argc and the argument vector argv, and a list of valid options that your program would accept. getopt returns any options it finds from that list. When it's done finding options, it returns a special code and sets optind to the next index in argv that doesn't contain an option (usually the start of a file list or other things you might put on a command line).

Modifying the above sample code:
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int
main (int argc, char **argv)
{
  int i;

  while ( (i = getopt(argc, argv, "hHQq") ) != EOF )
    {
      switch (i)
 {
 case 'h':
 case 'H':
   puts("HELLO");
   break;

 case 'q':
 case 'Q':
   puts("QUICK");
   break

 default:
     puts("unknown command line option");
 }
    }

  for (i = optind; i < argc; i++)
    {
      printf ("%d: %s\n", i, argv[i]);
    }

  exit (0);
}
Note the switch statement inside the while block. That evaluates the return value from getopt. At each pass, getopt returns a command line option. If it's q or Q, then the program prints QUICK. If it's h or H, the program prints HELLO.

getopt is a standard Unix C library. Unix is case sensitive everywhere, including its command line options. But DOS is usually case insensitive, so here we had to define h and H (and q and Q) to do the same behavior, to make the command line options case insensitive.

Also note that getopt acts like a standard Unix command line parser, so these command line options are set off with a hyphen. For example, use -q or -Q to print QUICK. On a typical DOS program, you would use the slash as the option character, so /s or /S.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.