2 * tw.spell.c: Spell check words
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /* spell_me : return corrrectly spelled filename. From K&P spname */
37 spell_me(struct Strbuf *oldname, int looking, Char *pat, eChar suf)
39 struct Strbuf guess = Strbuf_INIT, newname = Strbuf_INIT;
40 const Char *old = oldname->s;
45 cleanup_push(&guess, Strbuf_cleanup);
46 cleanup_push(&newname, Strbuf_cleanup);
48 while (*old == '/') { /* skip '/' */
49 Strbuf_append1(&newname, *old++);
52 /* do not try to correct spelling of single letter words */
53 if (*old != '\0' && old[1] == '\0')
54 Strbuf_append1(&newname, *old++);
55 Strbuf_terminate(&newname);
57 retval = (StrQcmp(oldname->s, newname.s) != 0);
58 cleanup_ignore(&newname);
60 *oldname = newname; /* shove it back. */
61 cleanup_until(&guess);
64 guess.len = 0; /* start at beginning of buf */
65 Strbuf_append(&guess, newname.s); /* add current dir if any */
67 for (; *old != '/' && *old != '\0'; old++)/* add current file name */
68 Strbuf_append1(&guess, *old);
69 Strbuf_terminate(&guess);
72 * Don't tell t_search we're looking for cmd if no '/' in the name so
73 * far but there are later - or it will look for *all* commands
75 /* (*should* say "looking for directory" whenever '/' is next...) */
76 retval = t_search(&guess, SPELL,
77 looking == TW_COMMAND && (foundslash || *old != '/') ?
78 TW_COMMAND : looking, 1, pat, suf);
79 if (retval >= 4 || retval < 0) {
80 cleanup_until(&guess);
81 return -1; /* hopeless */
83 Strbuf_append(&newname, guess.s + ws);
87 return (0); /* lint on the vax under mtXinu complains! */
91 #define EQ(s,t) (StrQcmp(s,t) == 0)
94 * spdist() is taken from Kernighan & Pike,
95 * _The_UNIX_Programming_Environment_
96 * and adapted somewhat to correspond better to psychological reality.
97 * (Note the changes to the return values)
99 * According to Pollock and Zamora, CACM April 1984 (V. 27, No. 4),
100 * page 363, the correct order for this is:
101 * OMISSION = TRANSPOSITION > INSERTION > SUBSTITUTION
102 * thus, it was exactly backwards in the old version. -- PWP
106 spdist(const Char *s, const Char *t)
108 for (; (*s & TRIM) == (*t & TRIM); t++, s++)
110 return 0; /* exact match */
113 if (s[1] && t[1] && (*s & TRIM) == (t[1] & TRIM) &&
114 (*t & TRIM) == (s[1] & TRIM) && EQ(s + 2, t + 2))
115 return 1; /* transposition */
116 if (EQ(s + 1, t + 1))
117 return 3; /* 1 char mismatch */
120 return 2; /* extra character */
122 if (*t && EQ(s, t + 1))
123 return 1; /* missing character */
128 spdir(struct Strbuf *extended_name, const Char *tilded_dir, const Char *item,
131 Char *path, *s, oldch;
134 if (ISDOT(item) || ISDOTDOT(item))
137 for (s = name; *s != 0 && (*s & TRIM) == (*item & TRIM); s++, item++)
139 if (*s == 0 || s[1] == 0 || *item != 0)
142 path = xmalloc((Strlen(tilded_dir) + Strlen(name) + 1) * sizeof (*path));
143 (void) Strcpy(path, tilded_dir);
149 if (access(p, F_OK) == 0) {
150 extended_name->len = 0;
151 Strbuf_append(extended_name, name);
152 Strbuf_terminate(extended_name);
153 /* FIXME: *s = oldch? */