[net.sources] correct.csh

alan@ucf-cs.UUCP (11/02/83)

#
#  This program interfaces spell, grep, and look to aid in
#     the correction of spelling errors in files.
#
#   INSTRUCTIONS-
#	Delete news header.
#	chmod +x thisfile  /* note: do not call this file correct.*/
#	thisfile
#
#   This will create a directory called correct that will
#   contain the Makefile, README, correct.c and correct.1 files.
#
mkdir correct
cat << `EOF` > correct/README
This program interfaces spell, grep, and look to aid in
the correction of spelling errors in files.

The files that should have been placed in this directory
are: Makefile, README, correct.c, correct.1

Potential changes necessary for particular installations
might be:

(1) In correct.c the paths to the spell, ex, grep and look commands
    are for 4.1bsd.  You may have to alter the corresponding defines
    found at the beginning of the program.  The "ed" editor could
    probably be substituted for "ex".

(2) In Makefile, the binary is defaulted to /usr/local and
    the manual entry to /usr/man/manl.
`EOF`
cat << `EOF` > correct/Makefile
BIN = /usr/local
MAN = /usr/man/manl
ROFF = /usr/ucb/vtroff
SRCS = correct.c
CFLAGS =
all:	$(SRCS)
	cc $(CFLAGS) $(SRCS) -o correct
install:
	strip correct
	mv correct $(BIN)
	cp correct.1 $(MAN)
manl:   correct.1
	$(ROFF) -man correct.1
`EOF`
cat << `EOF` > correct/correct.c
/*
 *   correct.c -
 *       This program can be redistributed without charge
 *       with credit to the author.
 *
 *       R. Alan Eustace
 *          alan.ucf-cs@rand-relay
 *          decvax!ucf-cs!alan
 */
#define	SPELL	"/usr/bin/spell"
#define	LOOK	"/usr/bin/look"
#define	GREP	"/usr/ucb/grep"
#define	EX	"/usr/ucb/ex"
#include <stdio.h>
/*#define DEBUG*/
#define SYNTAX "usage: correct [-d locdict] file"
#define STRLEN 100
#define NOWORDS 100
#define LocdictMAX 500
#define TRUE 	1
#define FALSE 	0

FILE *popen();
FILE *fpspell;
FILE *fplook;
FILE *fpgrep;
FILE *fpex;
FILE *pfLdfile,*fopen();

char command[STRLEN];
char word[NOWORDS][STRLEN];
char correct[NOWORDS][STRLEN];
char asLocdict[LocdictMAX][STRLEN];
int iasLast;
char *psFile;
char *psLdfile;
char *index();
char sDelim[] = "/%$#*[]";

main(argc, argv)
int argc;
char *argv[];
    {
    char *first;
    char try[STRLEN];
    char lookword[STRLEN];
    char psAns[STRLEN];
    int i,ips,ias;
    int iNext;
    char bCorrflag,bLocdictflag;
    char sLine[STRLEN];
    int Compare();
    char *pc;

    if (argc < 2) {
	fprintf(stderr,"%s\n",SYNTAX);
	exit(1);
	}

    psFile = NULL;
    psLdfile = NULL;
    for(ips=1;ips<argc;ips++) {
	if (argv[ips][0] == '-') {
	    switch (argv[ips][1]) {
		case 'd':
		    if (argc > ips) {
		       psLdfile = argv[++ips];
		       }
		    else Faterr(SYNTAX);
		    break;
		default :
		    Faterr(SYNTAX);
		    break;
		}
	    }
	else {
	    psFile = argv[ips];
	    }
	}
    if (psFile == NULL) Faterr(SYNTAX);
    if (psLdfile != NULL) InitasLocdict(psLdfile);

    bCorrflag = bLocdictflag = 0;

    sprintf(command,"%s %s",SPELL,psFile);
    printf("Running spell ....\n");
    if ((fpspell = popen(command,"r")) == NULL)
	{
	fprintf(stderr,"Cannot open %s\n",argv[1]);
	exit(1);
	}
    for (iNext=0;;)
	{
	if (fscanf(fpspell,"%s",word[iNext])==EOF) break;
	if (psLdfile!=NULL &&
	    SearchasLocdict(word[iNext])==TRUE) continue;
	printf("%s\n(CR, -, @, prefix? or word):",word[iNext]);

	while (TRUE) {
	    gets(try);
	    if ((first = index(try,'?')) != NULL) {
		*first = '\0';
		sprintf(command,"%s %s",LOOK,try);
		if ((fplook = popen(command,"r")) == NULL)
		    {
		    fprintf(stderr,"look command failure..strange \n");
		    exit(1);
		    }
		for(;;)
		    {
		    if(fscanf(fplook,"%s",lookword)==EOF) break;
		    printf("%s\n",lookword);
		    }
		pclose(fplook);
		}
	    else if (strcmp(try,"@")==0) {
		sprintf(command,"%s \"\\<%s\\>\" %s",
		    GREP,word[iNext],psFile);
#ifdef DEBUG
		printf("%s\n",command);
#endif
		if ((fpgrep = popen(command,"r")) == NULL)
		    {
		    fprintf(stderr,"grep command failure..strange \n");
		    exit(1);
		    }
		for(;;)
		    {
		    if(fgets(sLine,100,fpgrep) == NULL) break;
		    fputs(sLine,stdout);
		    }
		pclose(fpgrep);
		}
	    else break;

	    printf("\n");
	    printf("%s\n(CR, -, @, prefix?, or word):",word[iNext]);
	    }

	if (try[0] == '\0') {
	    AddtoasLocdict(word[iNext]);
	    bLocdictflag++;
	    }
	else if (try[0] != '-') {
	    bCorrflag++;
	    strcpy(correct[iNext],try);
#ifdef DEBUG
	printf("corr = %s\n",correct[iNext]);
#endif
	    iNext++;
	    }
	}
    pclose(fpspell);

    if (bCorrflag) {
	printf("Corrections made?");
	scanf("%s",psAns);
	if (psAns[0] == 'y')
	    {
	    sprintf(command,"%s - %s",EX,psFile);
#ifdef DEBUG
	    printf("%s\n",command);
#endif
	    if ((fpex = popen(command,"w")) == NULL)
		{
		fprintf(stderr,"ex command failure..strange \n");
		exit(1);
		}
	    for (i=0;i<=iNext-1;i++)
		{
		for(pc=sDelim;*pc!='\0';pc++) {
		    if (index(correct[i],*pc)==NULL) break;
		    }
#ifdef DEBUG
		printf("fin-char=%c\nstring=%s\n",*pc,correct[i]);
#endif
		if (*pc == '\0')
		     Faterr("Corrected word contains all known delimeters");
#ifdef DEBUG
		printf("1,$s%c\\<%s\\>%c%s%cg\n",*pc,word[i],*pc,correct[i],*pc);
#endif
#ifndef DEBUG
		fprintf(fpex,"1,$s%c\\<%s\\>%c%s%cg\n",*pc,
			word[i],*pc,correct[i],*pc);
#endif
		}
	    fprintf(fpex,"w\nq\n");
	    pclose(fpex);
	    }
	}

    if (psLdfile != NULL && bLocdictflag) {
	printf("Local Dictionary updated?");
	scanf("%s",psAns);
	if (psAns[0] == 'y')
	    {
	    qsort(asLocdict,iasLast,STRLEN,Compare);
	    if ((pfLdfile = fopen(psLdfile,"w"))==NULL)
		{
		fprintf("%s\n",psLdfile);
		Faterr("Local dictionary not found... see a wizard");
		}

	    for(ias=0;ias<=iasLast;ias++) {
		fprintf(pfLdfile,"%s\n",asLocdict[ias]);
		}
	    fclose(pfLdfile);
	    }
	}

    exit(0);
    }

Compare(ps1,ps2)
char *ps1,*ps2;
    {
    return(strcmp(ps1,ps2));
    }

InitasLocdict(psLdfile)
char *psLdfile;
    {
    int i;

    iasLast = 0;
    if ((pfLdfile = fopen(psLdfile,"r"))==NULL) {
	fprintf(stderr,"Opening new local dictionary\n");
	return;
	}

    for (i=0;;i++) {
	if (i >= LocdictMAX)
	    Faterr("Local Dictionary size exceeded(LocdictMAX)");
	if (fscanf(pfLdfile,"%s",asLocdict[i])==EOF) break;
	}
    iasLast = i-1;
    fclose(pfLdfile);
    }

Faterr(psErrmsg)
char *psErrmsg;
    {
    fprintf(stderr,"%s\n",psErrmsg);
    exit(1);
    }

SearchasLocdict(psWord)
char *psWord;
    {
    int ias;
    for(ias=0;ias<=iasLast;ias++) {
	if (strcmp(asLocdict[ias],psWord)==0) return(TRUE);
	}
    return(FALSE);
    }

AddtoasLocdict(psWord)
char *psWord;
    {
    if(iasLast++ >= LocdictMAX)
	Faterr("Local dictionart size exceeded(LocdictMAX)");
    strcpy(asLocdict[iasLast],psWord);
    }
`EOF`
cat << `EOF` > correct/correct.1
.TH CORRECT 1 VAX/11
.UC 4
.SH NAME
.B
correct
\- corrects spelling mistakes
.SH SYNOPSIS
.B correct
[-d
.I
locdict
]
.I
file
.SH DESCRIPTION
.PP
Correct takes
.I
file
as input, runs
.I
spell
to find spelling errors and prompts for corrections.
If the
.B
-d
flag is specified,
the file
.I
locdict
will be used as a local dictionary.  Correct will
filter prompts for those words
contained in this file. If
.I
locdict
is not found, it will be created.
.PP
The prompt has the form:
(CR, -, @, prefix?, or word).
.PP
.B
CR
(carriage return) indicates that the word is spelled correctly.
If the
.B
-d
option is used, the word is placed in the local dictionary.
.PP
.B
-
ignores the word.  The word is not placed in the local dictionary.
.PP
.B
@
runs
.I
grep
to determine the context the word is used.
.PP
.I
prefix
.B
?
If the correct spelling
is not known, typing the largest known prefix followed by a question
mark will invoke
.I
look
and output all words that begin with that prefix.
.PP
.B
word
is the correct spelling of the word.
.PP
At the end of making corrections, you will be prompted whether
or not you want the corrections made.  A yes (or 'y') answer will
change all occurrences of the incorrect spellings to the new value
you entered.
.SH SEE ALSO
spell(1), grep(1), look(1), ex(1)
.SH AUTHOR
R. Alan Eustace (alan.ucf-cs@rand-relay or decvax!ucf-cs!alan).
.SH BUGS
`EOF`
--
Alan Eustace

alan@ucf-cs.UUCP (11/03/83)

#
#  This program interfaces spell, grep, and look to aid in
#     the correction of spelling errors in files.
#
#   INSTRUCTIONS-
#	Delete news header.
#	chmod +x thisfile  /* note: do not call this file correct.*/
#	thisfile
#
#   This will create a directory called correct that will
#   contain the Makefile, README, correct.c and correct.1 files.
#
mkdir correct
cat << `EOF` > correct/README
This program interfaces spell, grep, and look to aid in
the correction of spelling errors in files.

The files that should have been placed in this directory
are: Makefile, README, correct.c, correct.1

Potential changes necessary for particular installations
might be:

(1) In correct.c the paths to the spell, ex, grep and look commands
    are for 4.1bsd.  You may have to alter the corresponding defines
    found at the beginning of the program.  The "ed" editor could
    probably be substituted for "ex".

(2) In Makefile, the binary is defaulted to /usr/local and
    the manual entry to /usr/man/manl.
`EOF`
cat << `EOF` > correct/Makefile
BIN = /usr/local
MAN = /usr/man/manl
ROFF = /usr/ucb/vtroff
SRCS = correct.c
CFLAGS =
all:	$(SRCS)
	cc $(CFLAGS) $(SRCS) -o correct
install:
	strip correct
	mv correct $(BIN)
	cp correct.1 $(MAN)
manl:   correct.1
	$(ROFF) -man correct.1
`EOF`
cat << `EOF` > correct/correct.c
/*
 *   correct.c -
 *       This program can be redistributed without charge
 *       with credit to the author.
 *
 *       R. Alan Eustace
 *          alan.ucf-cs@rand-relay
 *          decvax!ucf-cs!alan
 */
#define	SPELL	"/usr/bin/spell"
#define	LOOK	"/usr/bin/look"
#define	GREP	"/usr/ucb/grep"
#define	EX	"/usr/ucb/ex"
#include <stdio.h>
/*#define DEBUG*/
#define SYNTAX "usage: correct [-d locdict] file"
#define STRLEN 100
#define NOWORDS 200
#define LocdictMAX 500
#define TRUE 	1
#define FALSE 	0

FILE *popen();
FILE *fpspell;
FILE *fplook;
FILE *fpgrep;
FILE *fpex;
FILE *pfLdfile,*fopen();

char command[STRLEN];
char word[NOWORDS][STRLEN];
char correct[NOWORDS][STRLEN];
char asLocdict[LocdictMAX][STRLEN];
int iasLast;
char *psFile;
char *psLdfile;
char *index();
char sDelim[] = "/%$#*[]";

main(argc, argv)
int argc;
char *argv[];
    {
    char *first;
    char try[STRLEN];
    char lookword[STRLEN];
    char psAns[STRLEN];
    int i,ips,ias;
    int iNext;
    char bCorrflag,bLocdictflag;
    char sLine[STRLEN];
    int Compare();
    char *pc;

    if (argc < 2) {
	fprintf(stderr,"%s\n",SYNTAX);
	exit(1);
	}

    psFile = NULL;
    psLdfile = NULL;
    for(ips=1;ips<argc;ips++) {
	if (argv[ips][0] == '-') {
	    switch (argv[ips][1]) {
		case 'd':
		    if (argc > ips) {
		       psLdfile = argv[++ips];
		       }
		    else Faterr(SYNTAX);
		    break;
		default :
		    Faterr(SYNTAX);
		    break;
		}
	    }
	else {
	    psFile = argv[ips];
	    }
	}
    if (psFile == NULL) Faterr(SYNTAX);
    if (psLdfile != NULL) InitasLocdict(psLdfile);

    bCorrflag = bLocdictflag = 0;

    sprintf(command,"%s %s",SPELL,psFile);
    printf("Running spell ....\n");
    if ((fpspell = popen(command,"r")) == NULL)
	{
	fprintf(stderr,"Cannot open %s\n",argv[1]);
	exit(1);
	}
    for (iNext=0;iNext<NOWORDS-1;) {
	if (fscanf(fpspell,"%s",word[iNext])==EOF) break;
	if (psLdfile!=NULL &&
	    SearchasLocdict(word[iNext])==TRUE) continue;
	printf("%s\n(CR, -, @, prefix? or word):",word[iNext]);

	while (TRUE) {
	    gets(try);
	    if ((first = index(try,'?')) != NULL) {
		*first = '\0';
		sprintf(command,"%s %s",LOOK,try);
		if ((fplook = popen(command,"r")) == NULL)
		    {
		    fprintf(stderr,"look command failure..strange \n");
		    exit(1);
		    }
		for(;;)
		    {
		    if(fscanf(fplook,"%s",lookword)==EOF) break;
		    printf("%s\n",lookword);
		    }
		pclose(fplook);
		}
	    else if (strcmp(try,"@")==0) {
		sprintf(command,"%s \"\\<%s\\>\" %s",
		    GREP,word[iNext],psFile);
#ifdef DEBUG
		printf("%s\n",command);
#endif
		if ((fpgrep = popen(command,"r")) == NULL)
		    {
		    fprintf(stderr,"grep command failure..strange \n");
		    exit(1);
		    }
		for(;;)
		    {
		    if(fgets(sLine,100,fpgrep) == NULL) break;
		    fputs(sLine,stdout);
		    }
		pclose(fpgrep);
		}
	    else break;

	    printf("\n");
	    printf("%s\n(CR, -, @, prefix?, or word):",word[iNext]);
	    }

	if (try[0] == '\0') {
	    AddtoasLocdict(word[iNext]);
	    bLocdictflag++;
	    }
	else if (try[0] != '-') {
	    bCorrflag++;
	    strcpy(correct[iNext],try);
#ifdef DEBUG
	printf("corr = %s\n",correct[iNext]);
#endif
	    iNext++;
	    }
	}
    if (iNext >= NOWORDS-1) {
	fprintf(stderr,"Warning:  Maximum correction number exceeded\n");
	fprintf(stderr,"          Current corrections being processed\n");
	}
    pclose(fpspell);

    if (bCorrflag) {
	printf("Corrections made?");
	scanf("%s",psAns);
	if (psAns[0] == 'y')
	    {
	    sprintf(command,"%s - %s",EX,psFile);
#ifdef DEBUG
	    printf("%s\n",command);
#endif
	    if ((fpex = popen(command,"w")) == NULL)
		{
		fprintf(stderr,"ex command failure..strange \n");
		exit(1);
		}
	    for (i=0;i<=iNext-1;i++)
		{
		for(pc=sDelim;*pc!='\0';pc++) {
		    if (index(correct[i],*pc)==NULL) break;
		    }
#ifdef DEBUG
		printf("fin-char=%c\nstring=%s\n",*pc,correct[i]);
#endif
		if (*pc == '\0')
		     Faterr("Corrected word contains all known delimeters");
#ifdef DEBUG
		printf("1,$s%c\\<%s\\>%c%s%cg\n",*pc,word[i],*pc,correct[i],*pc);
#endif
#ifndef DEBUG
		fprintf(fpex,"1,$s%c\\<%s\\>%c%s%cg\n",*pc,
			word[i],*pc,correct[i],*pc);
#endif
		}
	    fprintf(fpex,"w\nq\n");
	    pclose(fpex);
	    }
	}

    if (psLdfile != NULL && bLocdictflag) {
	printf("Local Dictionary updated?");
	scanf("%s",psAns);
	if (psAns[0] == 'y')
	    {
	    qsort(asLocdict,iasLast,STRLEN,Compare);
	    if ((pfLdfile = fopen(psLdfile,"w"))==NULL)
		{
		fprintf("%s\n",psLdfile);
		Faterr("Local dictionary not found... see a wizard");
		}

	    for(ias=0;ias<=iasLast;ias++) {
		fprintf(pfLdfile,"%s\n",asLocdict[ias]);
		}
	    fclose(pfLdfile);
	    }
	}

    exit(0);
    }

Compare(ps1,ps2)
char *ps1,*ps2;
    {
    return(strcmp(ps1,ps2));
    }

InitasLocdict(psLdfile)
char *psLdfile;
    {
    int i;

    iasLast = 0;
    if ((pfLdfile = fopen(psLdfile,"r"))==NULL) {
	fprintf(stderr,"Opening new local dictionary\n");
	return;
	}

    for (i=0;;i++) {
	if (i >= LocdictMAX)
	    Faterr("Local Dictionary size exceeded(LocdictMAX)");
	if (fscanf(pfLdfile,"%s",asLocdict[i])==EOF) break;
	}
    iasLast = i-1;
    fclose(pfLdfile);
    }

Faterr(psErrmsg)
char *psErrmsg;
    {
    fprintf(stderr,"%s\n",psErrmsg);
    exit(1);
    }

SearchasLocdict(psWord)
char *psWord;
    {
    int ias;
    for(ias=0;ias<=iasLast;ias++) {
	if (strcmp(asLocdict[ias],psWord)==0) return(TRUE);
	}
    return(FALSE);
    }

AddtoasLocdict(psWord)
char *psWord;
    {
    if(iasLast++ >= LocdictMAX)
	Faterr("Local dictionart size exceeded(LocdictMAX)");
    strcpy(asLocdict[iasLast],psWord);
    }
`EOF`
cat << `EOF` > correct/correct.1
.TH CORRECT 1 VAX/11
.UC 4
.SH NAME
.B
correct
\- corrects spelling mistakes
.SH SYNOPSIS
.B correct
[-d
.I
locdict
]
.I
file
.SH DESCRIPTION
.PP
Correct takes
.I
file
as input, runs
.I
spell
to find spelling errors and prompts for corrections.
If the
.B
-d
flag is specified,
the file
.I
locdict
will be used as a local dictionary.  Correct will
filter prompts for those words
contained in this file. If
.I
locdict
is not found, it will be created.
.PP
The prompt has the form:
(CR, -, @, prefix?, or word).
.PP
.B
CR
(carriage return) indicates that the word is spelled correctly.
If the
.B
-d
option is used, the word is placed in the local dictionary.
.PP
.B
-
ignores the word.  The word is not placed in the local dictionary.
.PP
.B
@
runs
.I
grep
to determine the context the word is used.
.PP
.I
prefix
.B
?
If the correct spelling
is not known, typing the largest known prefix followed by a question
mark will invoke
.I
look
and output all words that begin with that prefix.
.PP
.B
word
is the correct spelling of the word.
.PP
At the end of making corrections, you will be prompted whether
or not you want the corrections made.  A yes (or 'y') answer will
change all occurrences of the incorrect spellings to the new value
you entered.
.SH SEE ALSO
spell(1), grep(1), look(1), ex(1)
.SH AUTHOR
R. Alan Eustace (alan.ucf-cs@rand-relay or decvax!ucf-cs!alan).
.SH BUGS
`EOF`
--
Alan Eustace