home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / WNWATTCP.ZIP / SRC / PCCONFIG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-10  |  5.1 KB  |  213 lines

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