/**---------------------------------------------------------------------------
 ** 
 ** option.c -- Additionnal option parsing utilities
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/14 20:18:41
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/07/15 21:07:14
 ** Update Count    : 8
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/plugs/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    This file contains additionnal utilities for parsing options from a
 **    control file. It is included by the original option.c file if macro
 **    USE_GFI_OPTIONS is defined at compile time. 
 ** 
 **    Other exented options can be made available if macro USE_GFI_XOPTIONS
 **    is defined at compile time. These are included from file xoption.c.
 **
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/

#ifndef gfiplug_option_c_already_included
#define gfiplug_option_c_already_included

#ifdef USE_GFI_OPTIONS

/* ------------------------------------------------------------------
   This function is similar to execute_options(), with the difference that the 
   option list passed to it is declared as a pointer to char. This allows to
   specify it in a list of options. This makes it easier to parse sub-options
   in a control file. If function execute_options() would be defined the same
   way, this function would not be necessary.

   RETURNS: The number of options matched.
   ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int execute_sub_options(/* Descriptor to control file */
                        FILE *fp, 
                        /* Pointer to new list of options */
                        char *sub_list, 
                        /* Echo flag */
                        int echo_flag)
#else
int execute_sub_options(fp, sub_list, echo_flag)
FILE *fp;
char *sub_list;
int echo_flag;
#endif
{
   char word[80];
   int index = 0;
   int end = 0;
   int match = 0;   /* count of the number of words matched */
	 OPTION_TYPE *list;
	 
	 list = (OPTION_TYPE *) sub_list;

   echo = echo_flag;
   if (echo == 1) report_msg("\n%\n% SUB OPTIONS:\n% -----------");
   while (!end)
   {
      if (fscanf(fp, " %79s", word) != 1) return(-1);    /* scanf error exit */
      index = get_option_index(list, word);
      if (index < 0)
      {
         report_msg("\n% ERROR: Unrecognized option => ");
         report_msg(word);
         list_options(list);
         return(0);             /* error exit: word not found */
      }
      if (index == 0) 
      {
         end = 1;
         if (echo == 1) report_msg("\n% -----------\n");
      }
      else
      {
         if (echo == 1) 
         {
	    report_msg("\n% ");
	    report_msg(word);
         }
         if (list[index].function != NULL)
            (*(list[index].function))(fp, list[index].argument, list[index].code);
      }
      match++;
   }
   return(match);       /* normal exit */
}                       /* execute_sub_options */

#endif /* USE_GFI_OPTIONS */

#ifdef USE_GFI_XOPTIONS

#include "xoption.c"

#endif /* USE_GFI_XOPTIONS */

#endif /* gfiplug_option_c_already_included */

