home *** CD-ROM | disk | FTP | other *** search
/ Oracle Video Server 3.0.3.1 / OVS_3031_NT.iso / win32 / sqlnet / net23 / client / cmdsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-27  |  3.1 KB  |  160 lines

  1. /*
  2.   $Header: /netrcs/RCS/oracle/network/tns/tnsapi/RCS/cmdsubs.c,v 1.2 1995/09/12 21:32:13 mhill Exp $
  3. */
  4.  
  5. #ifndef CMD
  6. #include "cmd.h"
  7. #endif
  8.  
  9. int cmd_ascii();
  10. int cmd_binary();
  11. int cmd_connect();
  12. int cmd_exit();
  13. int cmd_get();
  14. int cmd_put();
  15. int cmd_help();
  16. int cmd_mode();
  17. int cmd_status();
  18. int cmd_trace();
  19. /*int cmd_verbose();*/
  20.  
  21. Cmds commands[] = {
  22.   "?",          cmd_help,
  23.   "ascii",      cmd_ascii,
  24.   "binary",     cmd_binary,
  25.   "connect",    cmd_connect,
  26.   "exit",       cmd_exit,
  27.   "get",        cmd_get,
  28.   "help",       cmd_help,
  29.   "mode",       cmd_mode,
  30.   "put",        cmd_put,
  31.   "quit",       cmd_exit,
  32.   "status",     cmd_status,
  33.   "trace",      cmd_trace
  34. /*  "verbose",    cmd_verbose*/
  35. };
  36. #define NCMDS (sizeof(commands) / sizeof(Cmds))
  37.  
  38. int ncmds = NCMDS;
  39. static char   line[MAXLINE] = { 0 };
  40. static  char *lineptr = NULL;
  41.  
  42. /*
  43.  * Fetch the next command line
  44.  *
  45.  * Return 1 if ok, else 0 on error or EOF
  46.  */
  47.  
  48. int getline(fp)
  49. FILE *fp;
  50. {
  51.   if (fgets(line, MAXLINE, fp) == NULL)
  52.     return(0);
  53.   lineptr = line;
  54.   return(1);
  55. }
  56.  
  57. /*
  58.  * Fetch the next token from the input stream.
  59.  * We use the line that was set up in the most recent call to
  60.  * getline()
  61.  *
  62.  * Return a pointer to the token (argument), or NULL if no more exist
  63.  */
  64. char *gettoken(token)
  65. char token[];
  66. {
  67.   register int c;
  68.   register char *tokenptr;
  69.  
  70.   while ((c = *lineptr++) == ' ' || c == '\t')
  71.     ;   /* skip leading white space */
  72.  
  73.   if (c == '\0' || c == '\n')
  74.     return(NULL);
  75.  
  76.   tokenptr = token;
  77.   *tokenptr++ = c;   /* first char of token */
  78.  
  79.   /*
  80.    * collect everything up to the next space, tab, newline, or null
  81.    */
  82.   while (( c  = *lineptr++) != ' ' && c != '\t' && c != '\n' && c != '\0')
  83.     *tokenptr++ = c;
  84.  
  85.   *tokenptr ='\0';   /* null terminate token */
  86.   return(token);
  87. }
  88.  
  89.  
  90. /*
  91.  * verify there is no more token left on command line
  92.  */
  93. void checkend()
  94. {
  95.   if (gettoken(temptoken) != NULL)
  96.     err_cmd("trailing garbage");
  97. }
  98.  
  99. /*
  100.  * Execute a command.
  101.  * Call the appropiate function. If all goes well, that function will
  102.  * return, otherwise, that function may call an error handler, which
  103.  * will call longjmp() and branch back to the main command processing
  104.  * loop.
  105.  */
  106.  
  107. docmd(cmdptr)
  108. char *cmdptr;
  109. {
  110.   register int i;
  111.  
  112.   if  ((i = binary(cmdptr, ncmds)) < 0)
  113.       err_cmd(cmdptr);
  114.  
  115.       (*commands[i].cmd_func)();
  116.       
  117.       checkend();
  118. }
  119.  
  120. /* 
  121.  * Perform a binary search of the command table
  122.  * to see if a given token is a valid command
  123.  */
  124. int binary(word, n)
  125. char *word;
  126. int n;
  127. {
  128.   register int low, high, mid, cond;
  129.  
  130.   low = 0;
  131.   high = n - 1;
  132.   while (low <= high)
  133.   {
  134.     mid = (low + high)/2;
  135.     if ( (cond = strcmp(word, commands[mid].cmd_name)) < 0)
  136.       high = mid - 1;
  137.     else if (cond > 0)
  138.       low = mid + 1;
  139.     else
  140.       return(mid);
  141.   }
  142.   return(-1);  /* not found */
  143. }
  144.  
  145. /*
  146.  * User command error
  147.  */
  148. err_cmd(str)
  149. char *str;
  150. {
  151.   fprintf(stderr, "%s: '%s' command error ", pname, command);
  152.   if (strlen(str) > 0)
  153.     fprintf(stderr, "%s", str);
  154.   fprintf(stderr, "\n");
  155.   fflush(stderr);
  156.  
  157.   longjmp(jmp_mainloop, 1);
  158. }
  159.  
  160.