/*  SVGATextMode -- An SVGA textmode manipulation/enhancement tool
 *
 *  Copyright (C) 1995  Koen Gadeyne
 *
 *  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.
 */


/***
 *** parse_opt.c: option parsing routine for SVGATextMode
 *** (c) 1995 Koen Gadeyne (kmg@barco.be)
 ***/


#include "misc.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "confdefs.h"
#include "configfile.h"
#include "parse_opt.h"
#include "messages.h"

int parse_opt(FILE* param_file, int chipset, const char* str_chipset)
{
  const char *str_options[NUM_OPTIONS] = OPTION_STRINGS;
  const int str_allowedopts[NUM_CHIPSETS] = OPTIONS_ALLOWED;
  int optionmask, opt;
  char* arg_str;

  optionmask = 0;
  if ( (arg_str = findlabel(param_file, "Option", LABEL_OPTIONAL+LABEL_FROMSTART)) )
  {
    strtok(arg_str," ");  /* loose the 'Option' keyword */
    do
    { 
      if (!(arg_str=strtok(NULL," "))) PERROR(("Empty 'Option' line in config file '%s'.", ConfigFile));
      opt = findoption(arg_str, str_options, NUM_OPTIONS, "Option");
      if (opt >= NUM_OPTIONS) PERROR(("Illegal Option line in config file"));
      /* check if option allowed for selected chip set */
      if ( !(str_allowedopts[chipset] & (1<<opt)))
        PERROR(("Option '%s' not allowed for chipset '%s'.", str_options[opt], str_chipset));
      optionmask |= (1<<opt);  /* make optionvalues bitmask */
    } while ((arg_str = strtok(findlabel(param_file, "Option", LABEL_OPTIONAL+LABEL_FROMCURRENT)," ")));
  }
  PDEBUG(("Options selected (bit mask): 0x%x", optionmask));
  return(optionmask);
}
