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