예제 #1
0
int parse_command_line(int argc, char *argv[], FF_STD_ARGS_PTR std_args)
{
	int i;
	int error;
	int num_flags = 0;
	int last_error = 0;

	assert(std_args);
	
	if (!std_args)
		return(err_push(ERR_MEM_LACK, "standard args structure"));
	
	error = 0;

	/* Has user specified no command line parameters but is redirecting stdin? 
	   A format file will have to specified -- err-out when failing to create formats.
	*/

	if (argc == 1 && is_redirecting_stdin())
		std_args->user.is_stdin_redirected = 1;
	
	/* Interpret the command line */
	for (i = 1; i < argc; i++)
	{
		switch (argv[i][0]) /* ? */
		{
			case '-' :
				num_flags++;
				
				switch (toupper(argv[i][1])) /* -? */
				{
					case 'B' : /* cache size */
						error = option_B(argv, std_args, &i);
					break;
					
					case 'C' : /* head 'n tail count */
						error = option_C(argv, std_args, &i);
					break;

					case 'D' : /* data file name */
						error = option_D(argv, std_args, &i);
					break;

					case 'E' : /* error logging option */
						error = option_E_(argv, std_args, &i);
					break;
				
					case 'F' : /* single format file/title */
						error = option_F_(argv, std_args, &i);
					break;
					
					case 'G' : /* SDE grid sizes */
						error = option_G(argv, std_args, &i);
					break;
					
					case 'I' : /* input format file/title */
						error = option_I__(argv, std_args, &i);
					break;
	
					case 'M' : /* Checkvar maxbins, missing data, or max/min only */
						error = option_M_(argv, std_args, &i);
					break;
					
					case 'O' : /* output data file, or output format file/title */
						error = option_O__(argv, std_args, &i);
					break;
					
					case 'P' : /* Checkvar precision */
						error = option_P(argv, std_args, &i);
					break;
					
					case 'S' : /* Checkvar subsetting */
						error = option_S(argv, std_args, &i);
					break;
					
					case 'T' : /* FF2PSDTS terms file */
						error = option_T(argv, std_args, &i);
					break;
					
					case 'Q' : /* query file */
						error = option_Q(argv, std_args, &i);
					break;
					
					case 'V' : /* variable file */
						error = option_V(argv, std_args, &i);
					break;
					
					default :
						error = err_push(ERR_UNKNOWN_OPTION, "==> %s <==", argv[i]);
					break;
				} /* switch on flag letter code */
				
				if (error)
					last_error = error;
			break; /* case '-' */

			default :
		
				/* Has user omitted the optional -D option flag? */
				if (i == 1)
				{
					if (!os_file_exist(argv[1]))
						last_error = err_push(ERR_OPEN_FILE, argv[1]);

					std_args->input_file = argv[1];
				}
				else
					last_error = err_push(ERR_UNKNOWN_OPTION, "Expecting an option flag (beginning with '-')\n==> %s <==", argv[i]);
			break;
		} /* switch on argv[?][0] */
	} /* End of Command line interpretation (for loop)*/

	/* Is user redirecting stdin? */
	if (!std_args->input_file)
	{
		if (is_redirecting_stdin())
			std_args->user.is_stdin_redirected = 1;
		else if (argc > 1)
			last_error = err_push(ERR_GENERAL, "Expecting a data file to process");
	}

	if (!std_args->output_file && is_redirecting_stdout())
		std_args->user.is_stdout_redirected = 1;

	if (last_error)
		show_command_line(argc, argv);

	/* Has user requested a log file but not an error log?  Why make errors interactive
	   when they are logging?  So send errors and log to the same file
	*/
	if (!std_args->error_log && std_args->log_file)
		std_args->error_log = std_args->log_file;

	return(last_error);
}
예제 #2
0
/**
 * @brief ...
 * @param argc
 * @param argv
 * @param lib
 */
int my_main(int argc, char *argv[], char lib)
{
    char *wchars = NULL;
    char *preftype = NULL;
    static char outbuf[BUFSIZ];
    int arglen, old_argc;
    char *cpd = NULL;
    char *Cmd = *argv;
    /* Pointer to name of $(LIBDIR)/dict */
    char *LibDict = NULL; 

    islib = lib;
    old_argc = argc;
    Trynum = 0;
    sprintf(hashname, "%s/%s", LIBDIR, DEFHASH);
    strcpy(signs, DEFAULT_SIGNS);

    argv++;
    argc--;
    while (argc && **argv == '-') {
		/*
		 * Trying to add a new flag?  Can't remember what's been used?
		 * Here's a handy guide:
		 *
		 * Used:
		 *        ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
		 *        ^^^^       ^^^ ^  ^^ ^^
		 *        abcdefghijklmnopqrstuvwxyz
		 *        ^^^^^^*    ^^^*^  ^^*^^^*
		 */
		arglen = strlen(*argv);
		add_inc = 0;
	
		switch ((*argv)[1]) {
		case 'v':
		    option_v(Cmd, argc, argv, arglen);
		    break;
		case 'n':
		    preftype = option_n(Cmd, preftype, arglen);
		    break;
		case 't': /* TeX mode */
		    preftype = option_t(Cmd, preftype, arglen);
		    break;
		case 'T': /* Set preferred file type */
		    preftype = option_T(Cmd, argc, argv);
		    break;
		case 'A':
		    option_A(Cmd, arglen);
		    break;
		case 'a':
		    option_a(Cmd, arglen);
		    break;
		case 'D':
		    option_D(Cmd, arglen);
		    break;
		case 'J':
		    option_J(Cmd, arglen);
		    break;
		case 'e':
		    option_e(Cmd, arglen, argv);
		    break;
		case 'c':
		    option_c(Cmd, arglen, argv);
		    break;
		case 'b':
		    option_b(Cmd, arglen);
		    break;
		case 'x':
		    option_x(Cmd, arglen);
		    break;
		case 'f':
		    option_f(Cmd, argc, argv); 
		    break;
		case 'L':
		    option_L(Cmd, argc, argv);
		    break;
		case 'l':
		    option_l(Cmd, arglen);
		    break;
		case 'S':
		    option_S(Cmd, arglen);
		    break;
		case 'B':   /* -B: report missing blanks */
		    option_B(Cmd, arglen);
		    break;
		case 'C':   /* -C: compound words are acceptable */
		    option_C(Cmd, arglen);
		    break;
		case 'P':   /* -P: don't gen non-dict poss's */
		    option_P(Cmd, arglen);
		    break;
		case 'm':   /* -m: make all poss affix combos*/
		    option_m(Cmd, arglen);
		    break;
		case 'N':   /* -N:  suppress minimenu */
		    option_N(Cmd, arglen);
		    break;
		case 'M':
		    option_M(Cmd, arglen);
		    break;   /* -M:  force minimenu */
		case 'p':
		    option_p(LibDict, &cpd, Cmd, argc, argv);
		    break;
		case 'd':
		    option_d(LibDict, cpd, Cmd, argc, argv);
		    break;
		case 'V':    /* Display 8-bit characters as M-xxx */
		    option_V(Cmd, arglen);
		    break;
		case 'w':
		    wchars = (*argv)+2;
		    if (*wchars == '\0') {
				argv++; argc--;
				if (argc == 0)
				    usage(Cmd);
				wchars = *argv;
		    }
		    break;
		case 'W':
		    option_W(Cmd, argc, argv);
		    break;
		case 'o':
		    option_o(Cmd, argc, argv); 
		    break;
		case 'g':
		    option_g(Cmd, arglen);
		    break;
		case 'u':
		    option_u(Cmd, arglen);
		    break;
		case 'y':
		    option_y(Cmd, arglen);
		    break;
		case 'z':
		    option_z(Cmd, arglen);
		    break;
		default:
		    usage(Cmd);
		}  /* end switch */
	
		if (add_inc) {
		    argv++; argc--;
		}
		argv++; argc--;
    }

    if (!argc  &&  !lflag  &&  !aflag   &&  !eflag  &&  !dumpflag)
		usage(Cmd);

    verify_files(argc, argv);

    if (!oflag) strcpy(o_form, DEF_OUT);
	if (linit() < 0) exit(1); /* Force an error */

    det_prefstringchar(preftype);

    if (prefstringchar < 0)
		defdupchar = 0;
    else
		defdupchar = prefstringchar;

    if (missingspaceflag < 0)
		missingspaceflag = hashheader.defspaceflag;
    if (tryhardflag < 0)
		tryhardflag = hashheader.defhardflag;

    initckch(wchars);

    process_LibDict(LibDict, cpd);

    if (process_a_e_and_d_flags() == 0)
		return 0;

    if (!islib)
		setbuf(stdout, outbuf);

    /* process lflag (also used with the -c option) */
    if (lflag) {
		infile = stdin;
		outfile = stdout;
		if (!islib)
	    	checkfile();
		return 0;
    }

    /* n. of parameters advanced */
    return old_argc - argc; 
}