/* ** File name: PRINT.C ** ** Copyright (c) 1991, by ** Process Software Corporation ** Framingham, Massachusetts */ #include #include #include #include "TCPWARE_INCLUDE:RPC.H" #include "PRINT.H" #include "PRINT_DEF.H" /* ** This macro sets the character pointer x to the address of the string ** containing the parameter's argument. The argument may be specified as ** either: -xargument or -x argument. */ #define get_arg_string( x) \ {if( (*argv)[2]) (x) = *argv+2; \ else if( argc > 1) { argc--; (x) = *++argv;}} char *host = 0, *file = 0, host_buf[256 + 1]; static longw parse_command_line(); static struct timeval timeout = { 25, 0 }, /* normal call timeout */ zero = { 0, 0 }; /* batching timeout */ static readonly char usage[] = "Command format: print [-d#] [-hhost] file\n", INV_PARAM[] = "Invalid parameter: %s\n"; /* **************************************** ** ** Main program ** ** Abstract: ** This program demonstrates the use of the RPC batching facility */ main( argc, argv) int argc; char *argv[]; { longw status, /* VMS status code */ no_records; /* number of records the server displayed */ FILE *fp; /* file to be sent to the server */ CLIENT *clnt; /* client handle */ a_record the_record; /* one record sent to the server */ char buffer[ MAX_STRING_LEN+1]; /* for reading from the file */ /* ** Parse the command line that the user entered. */ _error( status = parse_command_line( argc, argv)) { if( status == SS$_BADPARAM) printf( usage); exit( status); } /* ** Create the client handle to send the file to the server. Batching requires ** the use of the TCP transport. */ clnt = clnt_create( host, PRINT_FILE_PROG, PRINT_FILE_VERS_1, "TCP"); if( clnt == 0) { clnt_pcreateerror( "Unable to create client"); exit( SS$_OPINCOMPL); } /* ** Open the file for reading. */ if( (fp = fopen( file, "r")) == 0) exit( vaxc$errno); the_record.ar_buffer = buffer; /* ** Send each record in the file to the server. A batching call is just a ** client call that has an xdr_result routine of NULL and a zero timeout. */ while( fgets( buffer, MAX_STRING_LEN, fp) != 0) { buffer[ MAX_STRING_LEN] = 0; if( clnt_call( clnt, PRINT_RECORD, xdr_a_record, &the_record, 0, 0, zero) != RPC_SUCCESS) { clnt_perror( clnt, "Unable to send a record to the server"); exit( SS$_OPINCOMPL); } } /* ** After all the records have been sent, the client must make a normal RPC ** call. In this case, call the procedure to return the number of records that ** the server received. */ if( clnt_call( clnt, SHOW_COUNT, xdr_void, 0, xdr_u_long, &no_records, timeout) != RPC_SUCCESS) { clnt_perror( clnt, "Unable to get the number of records displayed"); exit( SS$_OPINCOMPL); } printf( "%d records were sent to the server.\n", no_records); fclose( fp); clnt_destroy( clnt); exit( SS$_NORMAL); } /* end the main program */ /* **************************************** ** ** Function: parse_command_line() ** ** Abstract: ** This function parses the command line in the following form: ** ** print [-d#] [-hhost] file ** -d# is the optional RPC debugging value to use. ** -hhost is the name of the host on which the server resides. The ** default is the same as the cluster node. If this is not given, then ** the server is assumed to be on this node. ** ** Return value: ** A VMS status code. If the status code SS$_BADPARAM is returned, the ** main program displays the correct command format. */ static longw parse_command_line( argc, argv) int argc; char *argv[]; { longw debug = 0; char *cp; while( --argc > 0 && **++argv == '-') { switch( (*argv)[1]) { case 'd': /* specifying a debugging value */ case 'D': get_arg_string( cp); sscanf( cp, "%x", &debug); break; case 'h': /* the server's host */ case 'H': if( host != 0) goto invalid_parameter; get_arg_string( host); break; default: goto invalid_parameter; } } if( (argc == 0) || argc > 1) /* user must give a filename */ return( SS$_BADPARAM); file = *argv; /* ** If the user did not specify a host name, use this host's name. */ if( host == 0) { if( gethostname( host_buf, 256) == 0) /* use the local host name */ { printf( "Local node name is unavailable\n"); return( SS$_NOSUCHNODE); } host = host_buf; } /* ** If the debugging value is non-zero, set it. */ if( debug) oncrpc_set_char( RPCCHAR__DEBUG, &debug); /* ** The client will use non-privileged ports. */ oncrpc_set_char( RPCCHAR__DEFPORTS, &RPCPORTS__NORMAL); return( SS$_NORMAL); invalid_parameter: printf( INV_PARAM, *argv); return( SS$_BADPARAM); } /* end function parse_command_line() */ /* end file SYSINFO.C */