option.c 4.24 KB
/*==============================================================================
   option.c
==============================================================================*/
#include <string.h>
#include <malloc.h>

#include "global.h"
#include "const.h"

Option option;
Status status = {
    FALSE,FALSE};

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
static void init_options(void)
{
    option.in_file = (char *) NULL;
    option.out_file = (char *) NULL;
    option.merge_file = (char *) NULL;
    option.interp = (char *) NULL;

    option.debug = 0;		/* no debug */
    option.verbose = FALSE;
    option.r4000_fix = FALSE;
    option.big_procedure_list = FALSE;
}

/*------------------------------------------------------------------------------
  check error in options
------------------------------------------------------------------------------*/
static void check_options(void)
{
    if (option.in_file == NULL){
	error("program name not specified");
    }
}

/*------------------------------------------------------------------------------
  allocate space to store the option parameters.
------------------------------------------------------------------------------*/
static char **alloc_opt_parm( char **pp, char *s)
{
    int i;
    int first_null;

    if (pp == NULL){
	pp = (char **) realloc(pp, 10*sizeof(char*));
	pp[0] = s;
	for(i=1;i<10;i++)
	    pp[i] = (char *) NULL;
    } else{
	/* look for null pointer */
	for (first_null=0; pp[first_null]!= NULL;first_null++);

	/* allocate 10 at a time */
	if (first_null%10 == 9){
	    pp = (char **) realloc(pp, (first_null+1+10)
	        *sizeof(char*));
	    for(i=first_null+1;i<=first_null+10;i++)
		pp[i] = (char *) NULL;
	}
	pp[first_null] = s;
    }
    return pp;
}

/*------------------------------------------------------------------------------
  set defaults or implied options
------------------------------------------------------------------------------*/
static void set_implied_options(void)
{
    char *basename;

    /*------------------
	  find the base name of input file
	------------------*/
    if((basename = strrchr(option.in_file, '/')) != NULL)
	basename++;
    else
	basename = option.in_file;

    if (option.reorder_file == NULL){
	/*------------------
		  set default reorder filename
		------------------*/
	char *name;

	name = malloc(strlen(basename) + strlen(REORDER_SUFFIX));
	strcpy(name, basename);
	strcat(name, REORDER_SUFFIX);
	option.reorder_file = 
	    alloc_opt_parm(option.reorder_file,name);
    }

    if (option.out_file == NULL){
	/*------------------
		  set default output filename
		------------------*/
	char *outname;

	outname = malloc(strlen(basename) + strlen(CORD_SUFFIX));
	strcpy(outname, basename);
	strcat(outname, CORD_SUFFIX);
	option.out_file = outname;
    }
}

/*------------------------------------------------------------------------------
  parse command line options
------------------------------------------------------------------------------*/
void parse_command_line(int argc, char **argv)
{
    int i;

    init_options();

    for (i = 1; i < argc; i++) {
	char *arg = argv[i];

	if (strcmp(arg, "-verbose") == 0 || 
	    strcmp(arg, "-v") == 0) {
	    option.verbose = TRUE;
	} else if (strcmp(arg, "-flip") == 0) {
	    option.flip = TRUE;
	} else if (strcmp(arg, "-B") == 0) {
	    option.big_procedure_list = TRUE;
	} else if (strcmp(arg, "-o") == 0) {
	    if (i == (argc-1))
		error("file not specified with -o");
	    option.out_file = argv[++i];
	} else if (strcmp(arg, "-merge") == 0) {
	    if (i == (argc-1))
		error("file not specified with -merge");
	    option.merge_file = argv[++i];
	} else if (strcmp(arg, "-interp") == 0) {
	    option.interp = argv[++i];
	} else if (strcmp(arg, "-debug") == 0) {
	    option.debug = 1;
	} else if (strcmp(arg, "-r4000_fix") == 0) {
	    option.r4000_fix = TRUE;
	} else if (strcmp(arg, "-no_r4000_fix") == 0) {
	    option.r4000_fix = FALSE;
	} else if (!*arg || *arg == '-'){
	    error("Incorrect option %s", arg);

	} else if  (!option.in_file){
	    option.in_file = arg;
	} else{
	    option.reorder_file = 
	        alloc_opt_parm(option.reorder_file, arg);
	}
    }

    check_options();
    set_implied_options();
}