home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B95.ZIP / WATTSRC.ZIP / PCCONFIG.C < prev    next >
Text File  |  1994-11-28  |  6KB  |  220 lines

  1. #include <copyright.h>
  2. #include <wattcp.h>
  3.  
  4. #include <io.h>
  5. #include <dos.h>    /* for _argv */
  6. #include <fcntl.h>    /* open modes */
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #define MY_IP        "MY_IP"
  12. #define IP              "IP"
  13. #define NETMASK     "NETMASK"
  14. #define COOKIE        "COOKIE"
  15. #define NAMESERVER    "NAMESERVER"
  16. #define GATEWAY     "GATEWAY"
  17. #define DOMAINS        "DOMAINSLIST"
  18. #define HOSTNAME    "HOSTNAME"
  19. #define SOCKDELAY       "SOCKDELAY"
  20. #define ETHIP        "ETHIP"
  21. #define MSS             "MSS"
  22. #define BOOTP        "BOOTP"
  23. #define BOOTPTO        "BOOTPTO"
  24. #define DOMTO           "DOMAINTO"
  25. #define PRINT        "PRINT"
  26. #define INACTIVE        "INACTIVE"
  27. #define INCLUDE         "INCLUDE"
  28. #define MULTIHOMES      "MULTIHOMES"
  29.  
  30.  
  31. #define is_it( x ) if (!strcmp(name,x))
  32.  
  33. /*
  34.  * _inet_atoeth - read src, dump to ethernet buffer
  35.  *          and return pointer to end of text string
  36.  */
  37. char *_inet_atoeth( char *src, byte *eth )
  38. {
  39.     word count, val;
  40.     byte ch, icount;
  41.  
  42.     val = count = icount = 0;
  43.     while ((ch = toupper(*src++)) != 0 ) {
  44.         if (ch == ':' ) continue;
  45.         if (ch == ',' ) break;
  46.  
  47.         if ((ch -= '0') > 9) ch -= 7;
  48.         val = (val << 4) + ch;
  49.  
  50.         if ( icount ) {
  51.             eth[ count++ ] = val;
  52.             if (count > 5) break;
  53.             val = icount = 0;
  54.         } else
  55.             icount = 1;
  56.     }
  57.     if ( *src == ',' ) ++src;
  58.     return( src );
  59. }
  60.  
  61. static void ethip( char *s )
  62. {
  63.     eth_address temp_eth;
  64.     char *temp;
  65.  
  66.     if (( temp = _inet_atoeth( s, (byte*)&temp_eth )) != NULL ) {
  67.     if (!memcmp( &temp_eth, &_eth_addr, sizeof( eth_address ))) {
  68.             my_ip_addr = inet_addr( temp );
  69.         }
  70.     }
  71. }
  72.  
  73. void _add_server( int *counter, int max, longword *array, longword value )
  74. {
  75.     int i,duplicate=0;
  76.  
  77.     if ( value && ( *counter < max )) {
  78.         for (i=0;i<*counter; i++) {
  79.             if ( array[i] == value)
  80.                 duplicate=1;
  81.         }
  82.         if (!duplicate)
  83.              array[ (*counter)++ ] = value;
  84.     }
  85. }
  86.  
  87. word sock_delay = 30;
  88. word sock_inactive = 0;  /* defaults to forever */
  89. word multihomes = 0;
  90. char defaultdomain[ 80 ];
  91. longword _cookie[ MAX_COOKIES ];
  92. int _last_cookie;
  93. extern _domaintimeout;
  94. void (*usr_init)(char *name, char *value) = NULL;
  95.  
  96. static void set_values(char *name, char *value )
  97. {
  98.     char *p;
  99. /*    longword temp; */
  100.     word i;
  101.  
  102.     strupr(name);
  103.     is_it( MY_IP ) {
  104.     if ( toupper( *value ) == 'B') _bootpon = 1;
  105.     else my_ip_addr = resolve( value );
  106.     } else is_it( IP ) {
  107.     if ( toupper( *value ) == 'B') _bootpon = 1;
  108.     else my_ip_addr = resolve( value );
  109.     }
  110.     else is_it( NETMASK) sin_mask = resolve( value );
  111.     else is_it( GATEWAY)
  112.     /* accept gateip[,subnet[,mask]]  */
  113.     _arp_add_gateway( value , 0L );
  114.     else is_it( NAMESERVER )  _add_server( &_last_nameserver,
  115.         MAX_NAMESERVERS, def_nameservers, resolve(value));
  116.     else is_it( COOKIE ) _add_server( &_last_cookie, MAX_COOKIES,
  117.         _cookie, resolve( value ));
  118.     else is_it( DOMAINS ) def_domain = strcpy( defaultdomain, value );
  119.     else is_it( HOSTNAME ) strncpy(_hostname, value, MAX_STRING );
  120.     else is_it( SOCKDELAY ) sock_delay = atoi( value );
  121.     else is_it( ETHIP )  ethip( value );
  122.     else is_it( MSS ) _mss = atoi( value );
  123.     else is_it( BOOTP ) _bootphost = resolve( value );
  124.     else is_it( BOOTPTO) _bootptimeout = atoi( value );
  125.     else is_it( DOMTO ) _domaintimeout = atoi( value );
  126.     else is_it( INACTIVE ) sock_inactive = atoi( value );
  127.     else is_it( MULTIHOMES ) multihomes = atoi( value );
  128.     else is_it( PRINT ) {
  129.     outs( value );
  130.     outs( "\n\r" );
  131.     }
  132.     else is_it( INCLUDE ) {
  133.         if ( *(p = value) == '?') p++;
  134.         if ((i = _open( p, O_RDONLY | O_TEXT )) != 0xffff ) {
  135.             _close( i );
  136.             tcp_config( p );
  137.         } else if ( *value != '?' ) {
  138.             outs("\n\rUnable to open '");
  139.             outs( p );
  140.             outs("'\n\r");
  141.         }
  142.     }
  143.     else {
  144.     if ( usr_init )
  145.         (*usr_init)(name, value);
  146.     }
  147. }
  148.  
  149. char *watfname = "WATTCP.CFG";
  150.  
  151. int tcp_config( char *path )
  152. {
  153.     char name[80], *temp;
  154.     char value[80], ch[2];
  155.     int  quotemode;
  156.     int f, mode;
  157.  
  158.     if (!path) {
  159.     if ((path = getenv( watfname )) != NULL ) {
  160.         path = strcpy( name, path );
  161.         strcat( name, "\\");
  162.     } else {
  163.         strcpy( name, _argv[0] );
  164.         path = ( *name && (name[1] == ':')) ? &name[2] : name;
  165.         if ((temp = strrchr( path, '\\' )) == NULL) temp = path;
  166.         *(++temp) = 0;
  167.     }
  168. /*
  169.         strcpy( name, path );
  170.         strcat( name, "\\");
  171. */
  172.         strcat( name, watfname );
  173.     } else
  174.         strcpy( name, path );
  175.  
  176.     if ( ( f = _open( name, O_RDONLY | O_TEXT )) == -1 ) {
  177.     /* try local subdirectory */
  178.         if (( f = _open( watfname, O_RDONLY | O_TEXT )) == -1 ){
  179.         outs( watfname );
  180.         outs(" not found\n\r");
  181.         return( -1 );
  182.     }
  183.     }
  184.     *name = *value = mode = ch[1] = quotemode = 0;
  185.     while ( _read( f, &ch, 1 ) == 1) {
  186.     switch( *ch ) {
  187.         case  '\"': quotemode ^= 1;
  188.             break;
  189.         case  ' ' :
  190.         case  '\t': if (quotemode) goto addit;
  191.             break;
  192.  
  193.         case  '=' : if (quotemode) goto addit;
  194.             if (!mode) mode = 1;
  195.             break;
  196.         case  '#' :
  197.         case  ';' : if (quotemode) goto addit;
  198.             mode = 2;
  199.             break;
  200.         case  '\n':
  201.         case  '\r': if (*name && *value)
  202.                 set_values(name, value);
  203.             *name = *value = quotemode = mode = 0;
  204.             break;
  205.         default   :
  206. addit:
  207.                   switch (mode ) {
  208.             case 0 : strcat(name, ch);
  209.                  break;
  210.             case 1 : strcat(value, ch);
  211.                  break;
  212.             }
  213.             break;
  214.     }
  215.     }
  216.     _close(f );
  217.     return( 0 );
  218. }
  219.  
  220.