twitter
    Find out what I'm doing, Follow Me :)

whatnext.c - The Batch File Interrogator

This is an example of how to read the data on the command line following the function call. Notice that there are two variables listed within the parentheses following the main() call. The first variable is a count of words in the entire command line including the command itself and the second variable is a pointer to an array of pointers defining the actual words on the command line.

First the question on the command line, made up of some number of words, is displayed on the monitor and the program waits for the operator to hit a key. If the key hit is one of those in the last "word" of the group of words on the command line, the number of the character within the group is returned to the program where it can be tested with the "errorlevel" command in the batch file. You could use this technique to create a variable AUTOEXEC.BAT file or any other batch file can use this for a many way branch. Compile and run this file with TEST.BAT for an example of how it works in practice. You may find this technique useful in one of your batch files and you will almost certainly need to read in the command line parameters someday. Since 1616/OS doesn’t use batch files like MS-DOS ones, this program isn’t all that much use, but it is left in as an example of how to pass parameters from a command line, and use them in a program.

An interesting alternative would be for you to write a program named "would.c" that would return a 1 if a "Y" or "y" were typed and a zero if any other key were hit. Then your batch file could have a line including such a test.

/* ******************************************************************* */
/* This program reads a series of words from the command line,and
*/
/* displays all but the last on the monitor. The last is a series of */
/* characters which are used as input comparisons. One character is */
/* read from the keyboard. If it is one of the characters in the */
/* comparision list, its number is returned to DOS as the errorlevel */
/* command. If the character does not exist in the list, a zero is
*/
/* returned. Example follows; */
/* */
/* WHATNEXT What model do you want? ALR%3T */
/* If key a or A is hit,errorlevel 1 is returned. */
/* If key l or L is hit,errorlevel 2 is returned. */
/* If key r or R is hit,errorlevel 3 is returned. */
/* If key % is hit, errorlevel 4 is returned. */
/* If key 3 is hit, errorlevel 5 is returned. */
/* If key t or T is hit,errorlevel 6 is returned. */
/* If any other key is hit, errorlevel 0 is returned. */
/* */
/* The question must be on one line. */
/* Up to nine different keys can be used. */
/* The errorlevel can be interpreted in a batchfile. */

/* *************************************************************** */
#include "/sys/stdio.h"
#include "/sys/ctype.h"
main(number,name)
int number; /* total number of words on command line */
char *name[];
{
int index; /* a counter and incrementing variable */
int c; /* the character read in for comparison */
int code; /* the resulting errorlevel returned to */
char next_char; /* used for the comparison loop */
char *point; /* a dummy pointer used for convenience */
/* At least one group must be used for this */
/* filename,and one group used for the */
/* required fields, so less than three allows */
/* for no questions. */
if (number < 3) {
printf("No question given on command line\n");
exit(0);
}
/* print out words 2 to n-1, the question */
number--;
for(index = 1;index < number;index++) {
printf("%s ",name[index]);
}
/* get the users response and make it uppercase */
c = getch();
printf("%c\n",c);
if (islower(c))
c = toupper(c);
point = name[number]; /* point to the last pointer on the inputs */
Example Programs C Tutorial 14-5
code = 0;
index = 0;
do { /* search across allowed responses in last word */
next_char = *(point + index);
if (islower(next_char))
next_char = toupper(next_char); /* make it uppercase */
if(next_char == c) /* if a match is found */
code = index + 1; /* save the number of the match */
index++;
} while (*(point + index)); /* until NULL terminator found */
exit(code); /* return the errorcode to the system */
}