home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / mscwattc / src / pcconfig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-13  |  5.0 KB  |  206 lines

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