Lingua-Jspell

 view release on metacpan or  search on metacpan

src/jspell.c  view on Meta::CPAN

    oflag = 1;
    o_form2 = (*argv)+2;
    if (*o_form2 == '\0') {
	argv++; argc--;
	add_inc = 1;
	if (argc == 0)
	    usage(cmd);
	o_form2 = *argv;
	if (*o_form2 == '\0')
	    o_form2 = NULL;
    }
    if (o_form2) strcpy(o_form, o_form2);
    else o_form[0] = '\0';
}

/* g - display "good" options only - do not display "near misses" */
static void option_g(char *cmd, int arglen)
{
    if (arglen > 2) usage(cmd);
    gflag = 1;
}

/* y - do not display typing errors in "near misses" */
static void option_y(char *cmd, int arglen)
{
    if (arglen > 2) usage(cmd);
    yflag = 1;
}

/* u - no punct - signal is not word */
static void option_u(char *cmd, int arglen)
{
    if (arglen > 2) usage(cmd);
    signal_is_word = 0;
}

/* z - */
static void option_z(char *cmd, int arglen)
{
    if (arglen > 2) usage(cmd);
    showflags = 1;
}

static void verify_files(int argc, char *argv[])
{
    /* Because of the high cost of reading the dictionary, we stat the
     * files specified first to see if they exist. If at least one
     * exists, we continue.
     */
    int argno;

    for (argno = 0; argno < argc; argno++) {
	if (access(argv[argno], R_OK) >= 0) break;
    }
    if (argno >= argc  &&  !lflag  &&  !aflag  &&  !eflag  &&  !dumpflag) {
	fprintf(stderr, argc == 1 ? ISPELL_C_NO_FILE : ISPELL_C_NO_FILES);
	exit(1);
    }
}

static void det_prefstringchar(char *preftype)
{
    if (preftype != NULL) {
	prefstringchar = findfiletype(preftype, 1, &deftflag);
	if (prefstringchar < 0              &&  
            strcmp(preftype, "tex") != 0    && 
            strcmp(preftype, "nroff") != 0)
        {
	    fprintf(stderr, ISPELL_C_BAD_TYPE, preftype);
	    exit(1);
	}
    }
}

static void process_LibDict(char *LibDict, char *cpd)
{
	char *p;
	static char libdictname[sizeof DEFHASH];

	if (LibDict == NULL) {
		strcpy(libdictname, DEFHASH);
		LibDict = libdictname;
		p = rindex(libdictname, '.');
		if (p != NULL  &&  strcmp(p, ".hash") == 0)
	    	*p = '\0'; /* Don't want ext. in LibDict */
    }
    if (!nodictflag)
		treeinit(cpd, LibDict);
}

static int process_a_e_and_d_flags(void) 
{
	int res;
    
	res = 0;
	if (aflag) {
		if (!islib) {
			askmode();
			treeoutput();
		}
	} else if (eflag) {
		expandmode(eflag);
	} else if (dumpflag) {
		dumpmode();
	} else {
		res = 1;		
	}
    return res;
}

/**
 * @brief Max number of...
 */
#define MAX_SOL 15

/**
 * @brief ...
 * @param argc
 * @param argv
 * @param lib
 */
int my_main(int argc, char *argv[], char lib)
{
    char *wchars = NULL;
    char *preftype = NULL;

src/jspell.c  view on Meta::CPAN

		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; 
}

/**
 * @brief ...
 * @param opt
 */
void init_jspell(char *opt)
{
    int i, argc;
    static char aux[1];
    char options[255];
    static char *argv[MAX_SOL];
    
    strcpy(options, opt);
    argc = 1;
    aux[0] = '\0';
    argv[0] = aux;
    i = 0;
    while (options[i] != '\0') {
        argv[argc] = options+i;
        argc++;
        /* advance letters */
        while (options[i] != ' ' && options[i] != '\0')
            i++;
        /* advance spaces */
        while (options[i] == ' ') {
            options[i] = '\0';
            i++;
        }
    }
    /*   for (i = 0; i < argc; i++)
         printf("%d %s\n", i, argv[i]); */
    my_main(argc, argv, 1);
}



static void det_tflag(char *filename)
{
    char *cp;

    /* See if the file is a .tex file.  If so, set the appropriate flags. */
    tflag = deftflag;
    if ((cp = rindex(filename, '.')) != NULL  &&  strcmp(cp, ".tex") == 0)
        tflag = 1;
}

static void det_defdupchar(char *filename)
{
    if (prefstringchar < 0) {
        defdupchar = findfiletype(filename, 0, &tflag);
        if (defdupchar < 0) defdupchar = 0;
    }
}

static void det_readonly_access(char *filename)
{
    readonly = access(filename, W_OK) < 0;
    if (readonly) {
        fprintf(stderr, ISPELL_C_CANT_WRITE, filename);
        sleep((unsigned) 2);
    }
}

static void open_outfile(struct stat *statbuf)
{
    int file_descriptor;
    
    fstat(fileno(infile), statbuf);
    strcpy(tempfile, TEMPNAME);
#ifdef __WIN__
	file_descriptor = open(mktemp(tempfile),O_CREAT | O_RDWR | O_BINARY);
#else
    file_descriptor = mkstemp(tempfile);
#endif
    if ((outfile = fdopen(file_descriptor, "w")) == NULL) {
		fprintf(stderr, CANT_CREATE, tempfile);
		sleep((unsigned) 2);
		return;
    }
    chmod(tempfile, statbuf->st_mode);
}

static void update_file(char *filename, struct stat *statbuf)
{
    char bakfile[256];
    int c;

    if ((infile = fopen(tempfile, "r")) == NULL) {
		fprintf(stderr, ISPELL_C_TEMP_DISAPPEARED, tempfile);
		sleep((unsigned) 2);
		return;
    }

    sprintf(bakfile, "%s%s", filename, BAKEXT);

    if (strncmp(filename, bakfile, MAXNAMLEN) != 0)
		unlink(bakfile);        /* unlink so we can write a new one. */
#ifdef __WIN__
#else
    if (link(filename, bakfile) == 0)
		unlink(filename);
#endif
    /* if we can't write new, preserve .bak regardless of xflag */
    if ((outfile = fopen(filename, "w")) == NULL) {
		fprintf(stderr, CANT_CREATE, filename);
		sleep((unsigned) 2);
		return;
    }



( run in 0.328 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )