/**************************************************************************** * tcsio.c - command line io to talk to tcs3's IC command rpc. **************************************************************************** */ #define EXTERN #define MAIN 1 #define DEBUG 0 /*-------------------------- * Standard include files *-------------------------- */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*----------------------------- * Non-standard include files *----------------------------- */ #include "rpc_ic.h" /*----------------------------- * Prototypes *----------------------------- */ int main (int argc, char *argv[]); void usage (void); int tcsic_cmd (char *cmd, char *reply, int reply_size, char *hostname); CLIENT *rpc_connect (char *hostname, unsigned long program, unsigned long version, int seconds); char * strxcpy ( char * dest, const char * src, int maxlen); /*----------------------------- * Global Variable *----------------------------- */ char *ProgramName; int Verbose; /* verbose flag */ /*-------------------------------------------------------------------*/ /* clo options structure*/ /*------------------------------------------------------------------*/ struct clo_option_t { int type; /* CLO_TYPE_ - type of argument */ char * flag; /* command line flag string, ie: "-v" */ char * default_str; /* Default value, as a sting */ void * user_var; /* pointer to user variable to hold option's value (must match type) */ int sizeof_var; /* size of user_var (only important for char []) */ }; /* clo defines */ #define CLO_TYPE_NO_ARG 0 /* no argument - boolean type (assume int). */ #define CLO_TYPE_LONG 1 /* long - 32bit int */ #define CLO_TYPE_DOUBLE 2 /* double - double float. */ #define CLO_TYPE_STRING 3 /* char - character string */ /* clo function prototypes */ int clo_parse( int *argc, char *argv[], struct clo_option_t clo_options[], int num_clo_options); /* Error codes */ #define ERR_NONE (0) /* No error */ #define ERR_INV_RNG (-2) /* Invalid Range */ #define ERR_INV_FORMAT (-3) /* Invalid Format or syntax error */ #define ERR_SOCKET_ERR ( -4) /* Socket communication error */ #define ERR_NOT_AVAILABLE (-39) /* Reqested action or service is not available */ /*----------------------------------------------------------------------- ** main() **----------------------------------------------------------------------- */ int main (int argc, char *argv[]) { int rc, i; char cmd[512]; /* command to send */ char reply[512]; /* reply */ char hostname[50]; /* host for tcsic */ struct clo_option_t clo_options[] = { /* ____type______ _flag_ _default__ _user_var_ _sizeof_user_var */ { CLO_TYPE_NO_ARG, "-v", "0", (void *)(&Verbose), sizeof(Verbose)}, { CLO_TYPE_STRING, "-h", "tcs3_host", (void *)(hostname), sizeof(hostname)}, }; ProgramName = argv[0]; /* parse command line arguments */ rc = clo_parse( &argc, argv, clo_options, sizeof(clo_options)/sizeof(struct clo_option_t)); if( rc ) { usage (); exit(1); } /* must have at least 1 argument left (the tcs3 commmand) */ if ( argc <= 1 ) { usage (); exit (1); } /* * append the rest of the parameter into a single string. * This should be the command */ strcpy (cmd, ""); for (i = 1; i < argc; i++) { strcat (cmd, argv[i]); strcat (cmd, " "); } if (Verbose) printf (" %s: command [%s] to %s\n", ProgramName, cmd, hostname); /* * send command */ rc = tcsic_cmd (cmd, reply, sizeof (reply), hostname); if (rc == ERR_NONE) { printf ("OK %s\n", reply); } else { printf ("ERR %d (%s)\n", rc, reply ); } return 0; } /*------------------------------------------------------------------------------- ** usage() - display usage to user. **------------------------------------------------------------------------------- */ void usage (void) { char *msg = " t3io sends a single command to the TCS3 IC. \n" " An 'OK', followed by the reply is output for successful commands.\n" " Otherwise, 'ERR' followed by an error code is printed.\n" "\n" " usage: %s [-v] [-h hostname] command \n" "\n"; printf (msg, ProgramName); } /*------------------------------------------------------------------------------- ** tcsic_cmd() **------------------------------------------------------------------------------- */ int tcsic_cmd (char *cmd, char *reply, int reply_size, char *hostname) { int rc; struct msg_t in; struct msg_t *out; CLIENT *CL; /* open connect to TCS3 ic server */ if(Verbose)printf("t3io tcsic_cmd: calling rpc_connect to %s\n", hostname ); if (!(CL = rpc_connect (hostname, TCS_IC_RPC, TCS_IC_RPC_VER01, 5))) { rc = ERR_NOT_AVAILABLE; if( Verbose ) printf("t3io tcsic_cmd: unable to connect to %s\n", hostname ); goto ldone; } /* send command via rpc */ *reply = 0; strncpy (in.txt, cmd, sizeof (in.txt)); if(Verbose)printf("t3io tcsic_cmd: sending command '%s'\n", in.txt ); if ((out = tcs_ic_cmd_io_1 (&in, CL)) == NULL) { rc = ERR_SOCKET_ERR; if( Verbose ) printf("t3io tcsic_cmd: socket error on send\n"); goto ldone; } /* copy result to caller */ if(Verbose)printf("t3io tcsic_cmd: got reply '%s'\n", out->txt ); strncpy (reply, out->txt, reply_size); rc = out->rc; ldone: if (CL) clnt_destroy (CL); return rc; } /*--------------------------------------------------------------------------- ** rpc_connect() - connect & sets timeout **--------------------------------------------------------------------------- */ CLIENT *rpc_connect (char *hostname, /* server host for rpc functions */ unsigned long program, /* rpc program number */ unsigned long version, /* rpc version number */ int seconds) { /* rpc timeout in seconds */ CLIENT *CL; /* Connect to RPC server */ if (!(CL = clnt_create (hostname, program, version, "udp"))) return CL; /* Set RPC timeout seconds */ { struct timeval rpc_timeout; rpc_timeout.tv_sec = seconds; rpc_timeout.tv_usec = 0; clnt_control (CL, CLSET_TIMEOUT, (char *)&rpc_timeout); } return CL; } /********************************************************************************** ** Code from ir1lib (include here so t3io doesn't depend on any other libaries) *********************************************************************************** */ /*--------------------------------- ** clo_parse() **--------------------------------- */ int clo_parse( int *argc, /* process's argc variable */ char * argv[], /* process's argv[] variable */ struct clo_option_t clo_options[], /* command line option structure */ int num_clo_options /* number of options */ ) { int i, j, k, err; int found; /* Set default values for options */ err = ERR_NONE; for( k=0; k