home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rxtelnet.zip / nvt / rnvt.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  5KB  |  242 lines

  1. #define INCL_DOS
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define INCL_REXXSAA
  9. #include <rexxsaa.h>
  10. #include <rexxstr.h>
  11.  
  12. //nclude "wto.h"
  13. #include "nvt.h"
  14.  
  15.  
  16. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  17.  
  18. int numeric( char *str, int len, int *number )
  19. {
  20.   int minus = 0;
  21.   int value = 0;
  22.  
  23.   if ( !len ) return 0;
  24.  
  25.   if ( *str == '+' ) { minus = 0; str++; len--; }
  26.   else
  27.   if ( *str == '-' ) { minus = 1; str++; len--; }
  28.  
  29.   if ( !len ) return 0;
  30.  
  31.   while ( *str && len )
  32.   {
  33.     if ( *str < '0' || *str > '9' ) return 0;
  34.  
  35.     value = ( value * 10 ) + ( *str - '0' );
  36.     str++; len--;
  37.   }
  38.  
  39.   *number = minus ? -value : value;
  40.   return 1;
  41. }
  42.  
  43. HNVT rnvthandle( RXSTRING *var )
  44. {
  45.   HNVT hnvt = 0;
  46.  
  47.   if ( var->strptr[0] >= '0' && var->strptr[0] <= '9' )
  48.     numeric( var->strptr, var->strlength, (int*)&hnvt );
  49.  
  50.   return hnvt;
  51. }
  52.  
  53. int rnvtvar( PRXSTRING var, HNVT hnvt )
  54. {
  55.   _ultoa( (unsigned long)hnvt, var->strptr, 10 );
  56.   var->strlength = strlen(var->strptr);
  57.  
  58.   return var->strlength;
  59. }
  60.  
  61. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  62.  
  63.  #define INVALID_CALL_TO_FUNCTION 40
  64.  
  65.  #define RexxFunction(k) \
  66.              APIRET APIENTRY  k  ( PSZ function,        \
  67.                                    LONG argc,           \
  68.                                    RXSTRING argv[],     \
  69.                                    PSZ queue,           \
  70.                                    PRXSTRING result )
  71.  
  72.  
  73. RexxFunction( Telnet )
  74. {
  75.   int rc = 1;
  76.  
  77.   if ( argc == 1 || argc == 2 )
  78.   {
  79.     int port = DEFAULTPORT;
  80.  
  81.     if ( argc > 1 && argv[1].strlength )
  82.       if ( numeric( argv[1].strptr, argv[1].strlength, &port ) )
  83.         { if ( !port ) port = DEFAULTPORT; }
  84.       else
  85.         { port = 0; }
  86.  
  87.     if ( argv[0].strlength && port )
  88.     {
  89.       HNVT hnvt = nvtopen( argv[0].strptr, port );
  90.  
  91.       if ( hnvt )
  92.         rnvtvar( result, hnvt );
  93.       else
  94.         result->strptr[0] = result->strlength = 0;
  95.  
  96.       rc = 0;
  97.     }
  98.   }
  99.  
  100.   return rc ? INVALID_CALL_TO_FUNCTION : 0;
  101. }
  102.  
  103. RexxFunction( Tquit )
  104. {
  105.   int rc = 1;
  106.  
  107.   if ( argc == 1 )
  108.   {
  109.     HNVT hnvt = rnvthandle( argv+0 );
  110.  
  111.     if ( hnvt )
  112.       rc = nvtclose( hnvt );
  113.  
  114.     result->strptr[0] = rc ? '1' : '0';
  115.     result->strptr[1] = '\0';
  116.     result->strlength = 1;
  117.     rc = 0;
  118.   }
  119.  
  120.   return rc ? INVALID_CALL_TO_FUNCTION : 0;
  121. }
  122.  
  123. RexxFunction( Tget )
  124. {
  125.   int rc = 1;
  126.  
  127.   if ( argc == 1 || argc == 2 )
  128.   {
  129.     int timeout = 0;
  130.  
  131.     rc = ( argc == 1 ) ? 0 : numeric( argv[1].strptr,
  132.                                       argv[1].strlength,
  133.                                       &timeout ) ? 0 : 1;
  134.     if ( !rc )
  135.     {
  136.       HNVT hnvt = rnvthandle( argv+0 );
  137.  
  138.       if ( hnvt )
  139.       {
  140.         if ( timeout )
  141.         {
  142.           if ( nvtpeek( hnvt, timeout ) )
  143.             timeout = 0;
  144.           else
  145.             result->strptr[0] = result->strlength = 0;
  146.         }
  147.  
  148.         if ( !timeout )
  149.           result->strlength = nvtgets( hnvt, result->strptr,
  150.                                              result->strlength );
  151.       }
  152.     }
  153.   }
  154.  
  155.   return rc ? INVALID_CALL_TO_FUNCTION : 0;
  156. }
  157.  
  158. RexxFunction( Tput )
  159. {
  160.   int rc = 1;
  161.  
  162.   if ( argc == 2 )
  163.   {
  164.     if ( argv[1].strlength <= MAXLINESIZE )
  165.     {
  166.       HNVT hnvt = rnvthandle( argv+0 );
  167.  
  168.       if ( hnvt )
  169.       {
  170.         int count = nvtputs( hnvt, argv[1].strptr, argv[1].strlength );
  171.  
  172.         result->strptr[0] = ( count > 0 ) ? '0' : '1';
  173.         result->strptr[1] = '\0';
  174.         result->strlength = 1;
  175.         rc = 0;
  176.       }
  177.     }
  178.   }
  179.  
  180.   return rc ? INVALID_CALL_TO_FUNCTION : 0;
  181. }
  182.  
  183. RexxFunction( Tctl )
  184. {
  185.   int rc = 1;
  186.  
  187.   if ( argc == 1 )
  188.   {
  189.     /* not yet implemented                        */
  190.  
  191.     /* calls nvtquery()                           */
  192.     /* returns ipaddress:port:socket:errno string */
  193.  
  194.     result->strptr[1] = '\0';
  195.     result->strlength = 0;
  196.     rc = 0;
  197.   }
  198.   else
  199.   if ( argc == 2 )
  200.   {
  201.     HNVT hnvt = rnvthandle( argv+0 );
  202.  
  203.     if ( hnvt )
  204.     {
  205.       int count = nvtcommand( hnvt, argv[1].strptr );
  206.  
  207.       result->strptr[0] = ( count > 0 ) ? '0' : '1';
  208.       result->strptr[1] = '\0';
  209.       result->strlength = 1;
  210.       rc = 0;
  211.     }
  212.   }
  213.  
  214.   return rc ? INVALID_CALL_TO_FUNCTION : 0;
  215. }
  216.  
  217. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  218.  
  219. RexxFunction( load_routine )
  220. {
  221.   RexxRegisterFunctionDll( "Telnet", "Nvt", "Telnet" );
  222.   RexxRegisterFunctionDll( "Tget",   "Nvt", "Tget"   );
  223.   RexxRegisterFunctionDll( "Tput",   "Nvt", "Tput"   );
  224.   RexxRegisterFunctionDll( "Tctl",   "Nvt", "Tctl"   );
  225.   RexxRegisterFunctionDll( "Tquit",  "Nvt", "Tquit"  );
  226.  
  227.   result->strptr[0] = result->strlength = 0;
  228.   return 0;
  229. }
  230.  
  231. RexxFunction( drop_routine )
  232. {
  233.   RexxDeregisterFunction( "Telnet" );
  234.   RexxDeregisterFunction( "Tget"   );
  235.   RexxDeregisterFunction( "Tput"   );
  236.   RexxDeregisterFunction( "Tctl"   );
  237.   RexxDeregisterFunction( "Tquit"  );
  238.  
  239.   result->strptr[0] = result->strlength = 0;
  240.   return 0;
  241. }
  242.