/**
***  IPDial - SanaConfig.c
***
***  Tries to parse a SANA configuration file in ENV:sana2/.
***
***
***  This program is free software; you can redistribute it and/or modify
***  it under the terms of the GNU General Public License as published by
***  the Free Software Foundation; either version 2 of the License, or
***  (at your option) any later version.
***
***  This program is distributed in the hope that it will be useful,
***  but WITHOUT ANY WARRANTY; without even the implied warranty of
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***  GNU General Public License for more details.
***
***  You should have received a copy of the GNU General Public License
***  along with this program; if not, write to the Free Software
***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***
***  Authors: Jochen Wiedmann <wiedmann@neckar-alb.de>
***           Stefan Gybas <cab@studbox.uni-stuttgart.de>
**/



/**
***  Include files
**/
#ifndef IPDIAL_H
#include "IPDial.h"
#endif


/*** ReadSanaConfig() function
***
***  read device, unit, baud and handshake from SANA configuration file
**/
STRPTR ReadSanaConfig(STRPTR SanaDevName)
{
  FILE *ConfigFile;
  UWORD linenum=0;
  struct {
      STRPTR device;
      ULONG *unit;
      ULONG *baud;
      ULONG  rtscts;
      ULONG  xonxoff;
      STRPTR *crap;                    /* to filter out other options */
  } SanaArgs;

  SanaArgs.device  = NULL;
  SanaArgs.unit    = NULL;
  SanaArgs.baud    = NULL;
  SanaArgs.rtscts  = 0;
  SanaArgs.xonxoff = 0;
  SanaArgs.crap    = NULL;

  /* Create name of config file and open it */

  sprintf(ScratchBuffer,"ENV:sana2/%s.config", SanaDevName);
  if(ConfigFile = fopen((char *) ScratchBuffer, "r"))
  {
    while(fgets((char *) ScratchBuffer, ScBufSize, ConfigFile))
    {
      linenum++;

      /* Skip empty or comment lines */

      if(*ScratchBuffer == '#' || *ScratchBuffer == '\r' || *ScratchBuffer == '\n')
        continue;

      /* Not a comment: parse line */

      if (!StrReadArgs( ScratchBuffer,
                        (LONG *) &SanaArgs,
                        (STRPTR) "DEVICE/A,UNIT/A/N,BAUD/A/N,RTSCTS=7WIRE/S,XONXOFF/S,CRAP/M"))
      {
        fprintf(stderr, "Error in SANA config file line %d\n", linenum);
        CleanupAndExit(10);
      }

      if (SanaArgs.rtscts & SanaArgs.xonxoff)
      {
        fprintf(stderr, "Error in SANA config file: 7WIRE and XONXOFF both set\n");
        CleanupAndExit(10);
      }

      if (SanaArgs.rtscts)
      {
        SerialOpen(SanaArgs.device, *SanaArgs.unit, *SanaArgs.baud, (STRPTR)"7WIRE");
      }
      else if (SanaArgs.xonxoff)
      {
        SerialOpen(SanaArgs.device, *SanaArgs.unit, *SanaArgs.baud, (STRPTR)"XONXOFF");
      }
      else
      {
        SerialOpen(SanaArgs.device, *SanaArgs.unit, *SanaArgs.baud, (STRPTR)"NONE");
      }

      SerialSetBaud(*SanaArgs.baud);
      break;
    }
  }
  else
  {
    fprintf(stderr, "SANA config file %s not found\n", ScratchBuffer);
    CleanupAndExit(10);
  }
  fclose(ConfigFile);
  return(SanaArgs.device);
}
