159243Sobrien/*
259243Sobrien * tw.spell.c: Spell check words
359243Sobrien */
459243Sobrien/*-
559243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
659243Sobrien * All rights reserved.
759243Sobrien *
859243Sobrien * Redistribution and use in source and binary forms, with or without
959243Sobrien * modification, are permitted provided that the following conditions
1059243Sobrien * are met:
1159243Sobrien * 1. Redistributions of source code must retain the above copyright
1259243Sobrien *    notice, this list of conditions and the following disclaimer.
1359243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1459243Sobrien *    notice, this list of conditions and the following disclaimer in the
1559243Sobrien *    documentation and/or other materials provided with the distribution.
16100616Smp * 3. Neither the name of the University nor the names of its contributors
1759243Sobrien *    may be used to endorse or promote products derived from this software
1859243Sobrien *    without specific prior written permission.
1959243Sobrien *
2059243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2159243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2259243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2359243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2459243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2559243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2659243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2759243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2859243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2959243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3059243Sobrien * SUCH DAMAGE.
3159243Sobrien */
3259243Sobrien#include "sh.h"
3359243Sobrien#include "tw.h"
3459243Sobrien
3559243Sobrien/* spell_me : return corrrectly spelled filename.  From K&P spname */
3659243Sobrienint
37167465Smpspell_me(struct Strbuf *oldname, int looking, Char *pat, eChar suf)
3859243Sobrien{
39167465Smp    struct Strbuf guess = Strbuf_INIT, newname = Strbuf_INIT;
40167465Smp    const Char *old = oldname->s;
41167465Smp    size_t ws;
42145479Smp    int    foundslash = 0;
4359243Sobrien    int     retval;
4459243Sobrien
45167465Smp    cleanup_push(&guess, Strbuf_cleanup);
46167465Smp    cleanup_push(&newname, Strbuf_cleanup);
4759243Sobrien    for (;;) {
4859243Sobrien	while (*old == '/') {	/* skip '/' */
49167465Smp	    Strbuf_append1(&newname, *old++);
5059243Sobrien	    foundslash = 1;
5159243Sobrien	}
5259243Sobrien	/* do not try to correct spelling of single letter words */
5359243Sobrien	if (*old != '\0' && old[1] == '\0')
54167465Smp	    Strbuf_append1(&newname, *old++);
55167465Smp	Strbuf_terminate(&newname);
5659243Sobrien	if (*old == '\0') {
57167465Smp	    retval = (StrQcmp(oldname->s, newname.s) != 0);
58167465Smp	    cleanup_ignore(&newname);
59167465Smp	    xfree(oldname->s);
60167465Smp	    *oldname = newname; /* shove it back. */
61167465Smp	    cleanup_until(&guess);
6259243Sobrien	    return retval;
6359243Sobrien	}
64167465Smp	guess.len = 0;		/* start at beginning of buf */
65167465Smp	Strbuf_append(&guess, newname.s); /* add current dir if any */
66167465Smp	ws = guess.len;
6759243Sobrien	for (; *old != '/' && *old != '\0'; old++)/* add current file name */
68167465Smp	    Strbuf_append1(&guess, *old);
69167465Smp	Strbuf_terminate(&guess);
7059243Sobrien
7159243Sobrien	/*
7259243Sobrien	 * Don't tell t_search we're looking for cmd if no '/' in the name so
7359243Sobrien	 * far but there are later - or it will look for *all* commands
7459243Sobrien	 */
7559243Sobrien	/* (*should* say "looking for directory" whenever '/' is next...) */
76167465Smp	retval = t_search(&guess, SPELL,
7759243Sobrien			  looking == TW_COMMAND && (foundslash || *old != '/') ?
7859243Sobrien			  TW_COMMAND : looking, 1, pat, suf);
79167465Smp	if (retval >= 4 || retval < 0) {
80167465Smp	    cleanup_until(&guess);
8159243Sobrien	    return -1;		/* hopeless */
82167465Smp	}
83167465Smp	Strbuf_append(&newname, guess.s + ws);
8459243Sobrien    }
8559243Sobrien/*NOTREACHED*/
8659243Sobrien#ifdef notdef
8759243Sobrien    return (0);			/* lint on the vax under mtXinu complains! */
8859243Sobrien#endif
8959243Sobrien}
9059243Sobrien
9159243Sobrien#define EQ(s,t)	(StrQcmp(s,t) == 0)
9259243Sobrien
9359243Sobrien/*
9459243Sobrien * spdist() is taken from Kernighan & Pike,
9559243Sobrien *  _The_UNIX_Programming_Environment_
9659243Sobrien * and adapted somewhat to correspond better to psychological reality.
9759243Sobrien * (Note the changes to the return values)
9859243Sobrien *
9959243Sobrien * According to Pollock and Zamora, CACM April 1984 (V. 27, No. 4),
10059243Sobrien * page 363, the correct order for this is:
10159243Sobrien * OMISSION = TRANSPOSITION > INSERTION > SUBSTITUTION
10259243Sobrien * thus, it was exactly backwards in the old version. -- PWP
10359243Sobrien */
10459243Sobrien
10559243Sobrienint
106167465Smpspdist(const Char *s, const Char *t)
10759243Sobrien{
10859243Sobrien    for (; (*s & TRIM) == (*t & TRIM); t++, s++)
10959243Sobrien	if (*t == '\0')
11059243Sobrien	    return 0;		/* exact match */
11159243Sobrien    if (*s) {
11259243Sobrien	if (*t) {
11359243Sobrien	    if (s[1] && t[1] && (*s & TRIM) == (t[1] & TRIM) &&
11459243Sobrien		(*t & TRIM) == (s[1] & TRIM) && EQ(s + 2, t + 2))
11559243Sobrien		return 1;	/* transposition */
11659243Sobrien	    if (EQ(s + 1, t + 1))
11759243Sobrien		return 3;	/* 1 char mismatch */
11859243Sobrien	}
11959243Sobrien	if (EQ(s + 1, t))
12059243Sobrien	    return 2;		/* extra character */
12159243Sobrien    }
12259243Sobrien    if (*t && EQ(s, t + 1))
12359243Sobrien	return 1;		/* missing character */
12459243Sobrien    return 4;
12559243Sobrien}
12659243Sobrien
12759243Sobrienint
128167465Smpspdir(struct Strbuf *extended_name, const Char *tilded_dir, const Char *item,
129167465Smp      Char *name)
13059243Sobrien{
131167465Smp    Char *path, *s, oldch;
132167465Smp    char *p;
13359243Sobrien
13459243Sobrien    if (ISDOT(item) || ISDOTDOT(item))
13559243Sobrien	return 0;
13659243Sobrien
13759243Sobrien    for (s = name; *s != 0 && (*s & TRIM) == (*item & TRIM); s++, item++)
13859243Sobrien	continue;
13959243Sobrien    if (*s == 0 || s[1] == 0 || *item != 0)
14059243Sobrien	return 0;
14159243Sobrien
142167465Smp    path = xmalloc((Strlen(tilded_dir) + Strlen(name) + 1) * sizeof (*path));
14359243Sobrien    (void) Strcpy(path, tilded_dir);
14459243Sobrien    oldch = *s;
14559243Sobrien    *s = '/';
146167465Smp    Strcat(path, name);
147167465Smp    p = short2str(path);
148167465Smp    xfree(path);
149167465Smp    if (access(p, F_OK) == 0) {
150167465Smp	extended_name->len = 0;
151167465Smp	Strbuf_append(extended_name, name);
152167465Smp	Strbuf_terminate(extended_name);
153167465Smp	/* FIXME: *s = oldch? */
15459243Sobrien	return 1;
15559243Sobrien    }
15659243Sobrien    *s = oldch;
15759243Sobrien    return 0;
15859243Sobrien}
159