Sample IFDIAL procedure

From m204wiki
Jump to navigation Jump to search

This IFDIAL program uses the C routines from a workstation to upload terminal input lines to the Model 204 server address space.

#include <stdio.h> #include <manifest.h> #include <ifdial.h> int main(argc, argv) /* Main batch2 program */ int argc; /* Number of arguments */ char **argv; /* Pointers to argument pointers */ { char input[256]; char infile[256], outfile[256]; int i, rc, port; int cnum; char *host; char *host_data; FILE *inf, *outf; if (argc < 3) { /* If no host name */ printf("%s - Command format is %s filename host ( port ).\n", argv[0], argv[0]); return(4); /* Leave in shame */ } inf = NULL; outf = NULL; if (strlen(argv[1]) < 252) { /* If reasonable file name */ strcpy(infile, argv[1]); /* Copy filename */ strcpy(outfile, argv[1]); /* Copy filename */ strcat(infile, ".m204"); /* Tack on input suffix */ strcat(outfile, ".lst"); /* Tack on output suffix */ inf = fopen(infile, "r"); /* Open the input file */ outf = fopen(outfile, "w"); /* Open the output file */ } if (inf == NULL) { /* If error opening input file */ printf("%s - Unable to open %s.\n", argv[0], infile); return(24); /* Leave in shame */ } if (outf == NULL) { /* If error opening output file */ printf("%s - Unable to open %s.\n", argv[0], outfile); return(24); /* Leave in shame */ } port = 0; /* Assume port will default */ if (argc >= 4) { /* If have port number */ rc = sscanf(argv[3], "%d", &port); /* Get port number */ if (rc < 1) { /* If port number invalid */ printf("%s - Invalid port number - %s.\n", argv[0], argv[3]); return(8); /* Leave in shame */ } } cnum = ifdial(argv[2], port, 0, 0, 0); /* Connect to server */ if (cnum < 0) { /* If invalid return code */ printf("%s - Unable to connect to %s. ifdial = %d\n", argv[0], argv[2], cnum); /* Explain the situation */ return(-cnum); /* Leave in disgrace */ } while (fgets(input, sizeof(input), inf) /* Get next line */ != NULL) { /* Until no more data to read */ input[strlen(input) - 1] = '\0'; /* Get rid of \n */ rc = ifwrite(cnum, input); /* Send data to 204 */ if (rc == 1) /* If more data required */ continue; /* Get another line */ if (rc != 2) /* If no ifread data */ break; /* Something's gone wrong */ do { /* Until no more data */ rc = ifread(cnum, &host_data); /* Get the response line */ if (rc > 2) /* If problem */ break; /* Bail out */ fputs(host_data, outf); /* Output the returned data */ putc('\n', outf); /* Out with a new line */ } while (rc == 2); if (rc != 1) /* If problem */ break; /* Bail out */ } if (rc == 200) /* If connection lost */ printf("%s - Connection lost.\n", argv[0]); iffinish(); return(rc); /* All done */ }

See also