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