next up previous contents index
Next: Monitor files Up: Programs Previous: Labels, conditional jumps and   Contents   Index


Questions and answers inside programs

It is possible that inside a function called by a command some data are asked to the user of the program. A standard way to do this is as follows :

int i;
float x;
printf("Enter an integer : ");
scanf("%d", &i);
printf("Enter a number : ");
scanf("%f", &x);

This is not good inside a program of the command interpreter, if you don't want it to stop to wait the answers. Instead of scanf one can use the library functions

void  read_int(int *, flow_data *);
void  read_float(float *, flow_data *);
void  read_char(char *, flow_data *);

(the last to read character strings). Here flow_data is a structure type used to describe threads (cf. 10.4). The program becomes

int i;
float x;
printf("Enter an integer : ");
read_int(&i, flow_interp);
printf("Enter a number : ");
read_float(&x, flow_interp);

(in 10.4 we explain how to recover the flow_data structure corresponding to the running thread inside a command). In this case, the functions will behave like scanf if they are used in a command in a interactive way (i.e not in a program). Moreover the entered values will be parsed by the expression evaluator; you can enter for instance :

Enter an integer : 
2*k+5
Enter a number : 
cos(y)+2*c-4.

If the command corresponding to the function containing this piece of code is used inside a program, the interpreter will read the following lines of the program (2 in this example). So the program of the command interpreter could look like

.
.
command_name arg1 arg2 ....
2*k+5
cos(y)+2*c-4.
.
.


next up previous contents index
Next: Monitor files Up: Programs Previous: Labels, conditional jumps and   Contents   Index
2009-11-12