C-PLOT

Scientific Graphics and Data Analysis

E.1.10. - Getting command line options and keyboard input



Several subroutines are provided to simplify dialogue with the user of your C-PLOT user functions. Type 1 to 4 user functions do not read from a C-PLOT command file. To control the options within a user function when running from command files, extract fn command-line arguments using the get_cmdbuf() or get_args() routines provided. When the user function is run interactively the three get_?num() functions provide a convenient way of obtaining options from the keyboard.

 Name   What it Does 
 get_cmdbuf(b  Copy command line to character buffer b 
 get_args(fmt [, ptr ...])   Scan command line for arguments 
 get_dnum(prompt, dptr  Input double from keyboard 
 get_inum(prompt, iptr  Input integer from keyboard 
 get_snum(prompt, sptr  Input string from keyboard 

In the above function calls, b, fmt, prompt and sptr are type (char *), dptr is type (double *) and iptr is type (int *). The optional arguments to get_args() are all pointers with their type depending on the contents of the fmt string. There can be no more than 26 of these pointers.

The size of the character buffer b used in get_cmdbuf() should be at least CMD_LEN bytes, where CMD_LEN is a manifest constant that is declared in the include files.

To clarify, here are examples of the usage of these functions. For get_args(), the rules for fmt, the optional pointers and the return value are just as they are for the standard sscanf() routine in the C library. If a function is invoked from the plot program as
fn name.1 arg1 arg2 arg3 arg4 ...
the scanning specified by format begins at arg1. Here is an example
setup() {
        int     args;
        double  p1, p2;

        args = get_args("%lf %lf", &p1, &p2);
        ...
}
For each of the last three routines in the above table, prompt, if nonzero, should point to a string that will be printed along with the current value of what is pointed to by the second argument. When the routine is called, a line of text will be read from the keyboard and scanned for something to stuff into the location pointed to by the second argument. If no appropriate object is found on the line of text, the contents of the location pointed to by the second argument remain unchanged. For the following code,
{
        static  char    file[64] = "data";
        static  double  temp = 98.6;

        get_snum("What file", file);
        get_dnum("What temperature", &temp);
        ...
}
the output is
What file (data)? <return>
What temperature (98.6)? <return>
The return values for each of these three functions are 1 if the user simply hits return, 0 if the user types some input and -1 on end-of-file (the user hit a ^D).