home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / NODECFG.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  7KB  |  202 lines

  1. /******************************************************************************
  2. NODECFG.C    Node configuration file processor.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module contains the code to load and process NODES.CFG.
  20. ******************************************************************************/
  21.  
  22. #include "top.h"
  23.  
  24. /* loadnodecfg() - Loads and processes NODES.CFG.
  25.    Parameters:  exepath - Path (including trailing \) to TOP.EXE.
  26.    Returns:  TRUE on success, FALSE on error.
  27. */
  28. XINT loadnodecfg(unsigned char *exepath)
  29.     {
  30.     unsigned char loadstr[256 + 1]; /* Line input buffer. */
  31.     XINT code, flag = 0; /* Node code, finished flag. */
  32.     FILE *fil = NULL; /* File stream for loading NODES.CFG. */
  33.  
  34.     /* Open the configuration file. */
  35.     strcat(exepath, "NODES.CFG");
  36.     fil = fopen(exepath, "rt");
  37.     if (fil == NULL)
  38.         {
  39.         return 0;
  40.         }
  41.  
  42.     // Need err on fgets()
  43.  
  44.     /* Loop until end of file or this node's configuration is found. */
  45.     while(!feof(fil) && !flag)
  46.         {
  47.         /* Read a line and split it into keyword and options. */
  48.         fgets(loadstr, 256, fil);
  49.         stripcr(loadstr);
  50.         code = splitnodecfgline(loadstr);
  51.         /* Look for a "Node" line in the configuration file. */
  52.         if (code == NODECODE_NUMBER)
  53.             {
  54.             /* If the line bears our node's number, we can start
  55.                processing. */
  56.             if (atoi(loadstr) == od_control.od_node)
  57.                 {
  58.                 flag = 1;
  59.                 }
  60.             }
  61.         }
  62.  
  63.     /* Node configuration not found. */
  64.     if (!flag)
  65.         {
  66.         fclose(fil);
  67.         return 0;
  68.         }
  69.  
  70.     /* Initialize variables. */
  71.     memset(nodecfg, 0, sizeof(TOP_nodecfg_typ));
  72.     flag = 0;
  73.  
  74.     /* Loop until end of file or until processing is done. */
  75.     while(!feof(fil) && !flag)
  76.         {
  77.         /* Read a line and split it into keyword and options. */
  78.         fgets(loadstr, 256, fil);
  79.         stripcr(loadstr);
  80.         code = splitnodecfgline(loadstr);
  81.         /* React to each type of configuration line.  See the
  82.            splitnodecfgline() function or TOPCFG.H to see which node code
  83.            corresponds to which line. */
  84.         switch(code)
  85.             {
  86.             case NODECODE_TYPE:
  87.                 strupr(loadstr);
  88.                 if (strstr(loadstr, "REMOTE")) nodecfg->type = NODE_REMOTE;
  89.                 if (strstr(loadstr, "LOCAL")) nodecfg->type = NODE_LOCAL;
  90.                 if (strstr(loadstr, "LAN")) nodecfg->type = NODE_LAN;
  91.                 break;
  92.             case NODECODE_DROPFILE:
  93.                 strupr(loadstr);
  94.                 strcpy(nodecfg->dropfilepath, loadstr);
  95.                 verifypath(nodecfg->dropfilepath);
  96.                 break;
  97.             case NODECODE_CFGFILE:
  98.                 strupr(loadstr);
  99.                 strcpy(nodecfg->cfgfile, loadstr);
  100.                 break;
  101.             case NODECODE_LOGFILE:
  102.                 strupr(loadstr);
  103.                 strcpy(nodecfg->logfile, loadstr);
  104.                 break;
  105.             case NODECODE_SOLID:
  106.                 nodecfg->solidnode = seektruth(loadstr);
  107.                 break;
  108.             case NODECODE_FOSSIL:
  109.                 nodecfg->fossil = seektruth(loadstr);
  110.                 break;
  111.             case NODECODE_PORT:
  112.                 nodecfg->port = atoi(loadstr);
  113.                 break;
  114.             case NODECODE_SPEED:
  115.                 nodecfg->speed = atoi(loadstr);
  116.                 break;
  117.             case NODECODE_ADDRESS:
  118.                 nodecfg->portaddress = atoi(loadstr);
  119.                 break;
  120.             case NODECODE_IRQ:
  121.                 nodecfg->portirq = atoi(loadstr);
  122.                 break;
  123.             case NODECODE_FIFO:
  124.                 nodecfg->usefifo = seektruth(loadstr);
  125.                 break;
  126.             case NODECODE_FIFOTRIG:
  127.                 nodecfg->fifotrigger = atoi(loadstr);
  128.                 break;
  129.             case NODECODE_RXBUF:
  130.                 nodecfg->rxbufsize = atoi(loadstr);
  131.                 break;
  132.             case NODECODE_TXBUF:
  133.                 nodecfg->txbufsize = atoi(loadstr);
  134.                 break;
  135.             case NODECODE_RTSCTS:
  136.                 nodecfg->usertscts = !(seektruth(loadstr)) + 1;
  137.                 break;
  138.             case NODECODE_END:
  139.                 /* Flag so the loop will stop processing. */
  140.                 flag = 1;
  141.                 break;
  142.             }
  143.         }
  144.  
  145.     fclose(fil);
  146.  
  147.     return 1;
  148.     }
  149.  
  150. /* splitnodecfgline() - Splits a NODES.CFG line into keyword and options.
  151.    Parameters:  ncline - Line to split.
  152.    Returns:  Node code value (see NODECODE_ constants in TOPCFG.H).
  153.    Notes:  See NODES.CFG for details about each keyword.
  154. */
  155. XINT splitnodecfgline(unsigned char *ncline)
  156.     {
  157.     XINT wdcount; /* Word count holder. */
  158.     XINT nodecode = NODECODE_UNKNOWN; /* Node code. */
  159.  
  160.     if (ncline[0] == ';')
  161.         {
  162.         return NODECODE_COMMENT; /* Comment. */
  163.         }
  164.  
  165.     /* Split the line into words. */
  166.     wdcount = split_string(ncline);
  167.  
  168.     if (wdcount > 1)
  169.         {
  170.         /* Grab everything after the first word from the word string. */
  171.         strcpy(ncline, &word_str[word_pos[1]]);
  172.         }
  173.     else
  174.         {
  175.         /* No second word available. */
  176.         ncline[0] = '\0';
  177.         }
  178.  
  179.     /* Set the node code based on the keyword type.  Again, see NODES.CFG
  180.        comments for keyword descriptions. */
  181.     if (!stricmp(get_word(0), "Node")) nodecode = NODECODE_NUMBER;
  182.     if (!stricmp(get_word(0), "Type")) nodecode = NODECODE_TYPE;
  183.     if (!stricmp(get_word(0), "DropFilePath")) nodecode = NODECODE_DROPFILE;
  184.     if (!stricmp(get_word(0), "ConfigFile")) nodecode = NODECODE_CFGFILE;
  185.     if (!stricmp(get_word(0), "LogFile")) nodecode = NODECODE_LOGFILE;
  186.     if (!stricmp(get_word(0), "SolidNode")) nodecode = NODECODE_SOLID;
  187.     if (!stricmp(get_word(0), "FOSSIL")) nodecode = NODECODE_FOSSIL;
  188.     if (!stricmp(get_word(0), "Port")) nodecode = NODECODE_PORT;
  189.     if (!stricmp(get_word(0), "Speed")) nodecode = NODECODE_SPEED;
  190.     if (!stricmp(get_word(0), "PortAddress")) nodecode = NODECODE_ADDRESS;
  191.     if (!stricmp(get_word(0), "PortIRQ")) nodecode = NODECODE_IRQ;
  192.     if (!stricmp(get_word(0), "UseFIFO")) nodecode = NODECODE_FIFO;
  193.     if (!stricmp(get_word(0), "FIFOTrigger")) nodecode = NODECODE_FIFOTRIG;
  194.     if (!stricmp(get_word(0), "RxBufSize")) nodecode = NODECODE_RXBUF;
  195.     if (!stricmp(get_word(0), "TxBufSize")) nodecode = NODECODE_TXBUF;
  196.     if (!stricmp(get_word(0), "UseRTSCTS")) nodecode = NODECODE_RTSCTS;
  197.     if (!stricmp(get_word(0), "EndNode")) nodecode = NODECODE_END;
  198.  
  199.     return nodecode;
  200.     }
  201.  
  202.