04-04-09-Options C

From Msim

Jump to: navigation, search

This refers to the fixes to options.c to handle config files generated from dumpconfig and removal of an unnecessary goto


The revised format of the configuration generated configuration files that were not readable by the simulator (old config files were not a problem):

The original code from options.c, process_file(...):

while(*p)
{
 //skip whitespace
 while(*p != '\0' && (*p == ' ' || *p == '\t'))
 {
  p++;
 }
 ...

Revised code:

while(*p)
{
 //skip whitespace, '[' and ']'
 while(*p != '\0' && (*p == ' ' || *p == '\t' || *p == '[' || *p == ']'))
 {
  p++;
 }
 ...

The second issue is merely an unneeded goto in options.c, __opt_process_options(...):
Originally:

//visit all command line arguments
while(index < argv.size())
{
 //process any encountered orphans
 while(index < argv.size() && argv[index][0] != '-')
 {
  if(depth > 0)
  {
   //orphans are not allowed during config file processing
   fatal("orphan `%s' encountered during config file processing", argv[index].c_str());
  }
  //else, call the user-stalled orphan handler
  if(odb->orphan_fn)
  {
   if(!odb->orphan_fn(index+1, argv.size(), argv))
   {
    //done processing command line
    goto done_processing;
   }
  }
  else
  {
   //no one claimed this option
   fatal("orphan argument `%s' was unclaimed", argv[index].c_str());
  }
  //go to next option
 }
 //done with command line?
 if(index == argv.size())
 {
  //done processing command line
  goto done_processing;
 }
 //when finished, argv[index] is an option to parse
 
 //process builtin options
 if(argv[index]== "-config")
 {
  //handle `-config' builtin option
  index++;
  if(index >= argv.size() || argv[index][0] == '-')
  {
   //no arguments available
   fatal("option `-config' requires an argument");
  }
  process_file(odb, argv[index], depth);
  index++;
 }
 else if(argv[index]=="-dumpconfig")
 {
  //this is performed last
  do_dumpconfig = TRUE;
  //handle `-dumpconfig' builtin option
  index++;
  if(index >= argv.size() || (argv[index][0] == '-' && argv[index][1] != '\0'))
  {
   //no arguments available
   fatal("option `-dumpconfig' requires an argument");
  }
  dumpconfig_name = argv[index];
  index++;
 }
 else
 {
  //process user-installed option
  index = process_option(odb, index, argv);
 }
}

done_processing:
if(do_dumpconfig)
{
 dump_config(odb, dumpconfig_name);
}



Revisions:

//visit all command line arguments
while(index < argv.size())
{
 //process any encountered orphans
 while(index < argv.size() && argv[index][0] != '-')
 {
  if(depth > 0)
  {
   //orphans are not allowed during config file processing
   fatal("orphan `%s' encountered during config file processing", argv[index].c_str());
  }
  //else, call the user-stalled orphan handler
  if(odb->orphan_fn)
  {
   if(!odb->orphan_fn(index+1, argv.size(), argv))
   {
    //done processing command line
    if(do_dumpconfig)
    {
     dump_config(odb, dumpconfig_name);
    }
    return;
   }
  }
  else
  {
   //no one claimed this option
   fatal("orphan argument `%s' was unclaimed", argv[index].c_str());
  }
  //go to next option
 }
 //if not done with command line?
 if(index < argv.size())
 {
  //finished, argv[index] is an option to parse
  
  //process builtin options
  if(argv[index]== "-config")
  {
   //handle `-config' builtin option
   index++;
   if(index >= argv.size() || argv[index][0] == '-')
   {
    //no arguments available
    fatal("option `-config' requires an argument");
   }
   process_file(odb, argv[index], depth);
   index++;
  }
  else if(argv[index]=="-dumpconfig")
  {
   //this is performed last
   do_dumpconfig = TRUE;
   //handle `-dumpconfig' builtin option
   index++;
   if(index >= argv.size() || (argv[index][0] == '-' && argv[index][1] != '\0'))
   {
    //no arguments available
    fatal("option `-dumpconfig' requires an argument");
   }
   dumpconfig_name = argv[index];
   index++;
  }
  else
  {
   //process user-installed option
   index = process_option(odb, index, argv);
  }
 }
}

if(do_dumpconfig)
{
 dump_config(odb, dumpconfig_name);
}
Personal tools