xref: /freebsd/contrib/tcsh/tw.spell.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
1 /* $Header: /p/tcsh/cvsroot/tcsh/tw.spell.c,v 3.21 2006/03/02 18:46:45 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("$tcsh: tw.spell.c,v 3.21 2006/03/02 18:46:45 christos Exp $")
36 
37 #include "tw.h"
38 
39 /* spell_me : return corrrectly spelled filename.  From K&P spname */
40 int
41 spell_me(struct Strbuf *oldname, int looking, Char *pat, eChar suf)
42 {
43     struct Strbuf guess = Strbuf_INIT, newname = Strbuf_INIT;
44     const Char *old = oldname->s;
45     size_t ws;
46     int    foundslash = 0;
47     int     retval;
48 
49     cleanup_push(&guess, Strbuf_cleanup);
50     cleanup_push(&newname, Strbuf_cleanup);
51     for (;;) {
52 	while (*old == '/') {	/* skip '/' */
53 	    Strbuf_append1(&newname, *old++);
54 	    foundslash = 1;
55 	}
56 	/* do not try to correct spelling of single letter words */
57 	if (*old != '\0' && old[1] == '\0')
58 	    Strbuf_append1(&newname, *old++);
59 	Strbuf_terminate(&newname);
60 	if (*old == '\0') {
61 	    retval = (StrQcmp(oldname->s, newname.s) != 0);
62 	    cleanup_ignore(&newname);
63 	    xfree(oldname->s);
64 	    *oldname = newname; /* shove it back. */
65 	    cleanup_until(&guess);
66 	    return retval;
67 	}
68 	guess.len = 0;		/* start at beginning of buf */
69 	Strbuf_append(&guess, newname.s); /* add current dir if any */
70 	ws = guess.len;
71 	for (; *old != '/' && *old != '\0'; old++)/* add current file name */
72 	    Strbuf_append1(&guess, *old);
73 	Strbuf_terminate(&guess);
74 
75 	/*
76 	 * Don't tell t_search we're looking for cmd if no '/' in the name so
77 	 * far but there are later - or it will look for *all* commands
78 	 */
79 	/* (*should* say "looking for directory" whenever '/' is next...) */
80 	retval = t_search(&guess, SPELL,
81 			  looking == TW_COMMAND && (foundslash || *old != '/') ?
82 			  TW_COMMAND : looking, 1, pat, suf);
83 	if (retval >= 4 || retval < 0) {
84 	    cleanup_until(&guess);
85 	    return -1;		/* hopeless */
86 	}
87 	Strbuf_append(&newname, guess.s + ws);
88     }
89 /*NOTREACHED*/
90 #ifdef notdef
91     return (0);			/* lint on the vax under mtXinu complains! */
92 #endif
93 }
94 
95 #define EQ(s,t)	(StrQcmp(s,t) == 0)
96 
97 /*
98  * spdist() is taken from Kernighan & Pike,
99  *  _The_UNIX_Programming_Environment_
100  * and adapted somewhat to correspond better to psychological reality.
101  * (Note the changes to the return values)
102  *
103  * According to Pollock and Zamora, CACM April 1984 (V. 27, No. 4),
104  * page 363, the correct order for this is:
105  * OMISSION = TRANSPOSITION > INSERTION > SUBSTITUTION
106  * thus, it was exactly backwards in the old version. -- PWP
107  */
108 
109 int
110 spdist(const Char *s, const Char *t)
111 {
112     for (; (*s & TRIM) == (*t & TRIM); t++, s++)
113 	if (*t == '\0')
114 	    return 0;		/* exact match */
115     if (*s) {
116 	if (*t) {
117 	    if (s[1] && t[1] && (*s & TRIM) == (t[1] & TRIM) &&
118 		(*t & TRIM) == (s[1] & TRIM) && EQ(s + 2, t + 2))
119 		return 1;	/* transposition */
120 	    if (EQ(s + 1, t + 1))
121 		return 3;	/* 1 char mismatch */
122 	}
123 	if (EQ(s + 1, t))
124 	    return 2;		/* extra character */
125     }
126     if (*t && EQ(s, t + 1))
127 	return 1;		/* missing character */
128     return 4;
129 }
130 
131 int
132 spdir(struct Strbuf *extended_name, const Char *tilded_dir, const Char *item,
133       Char *name)
134 {
135     Char *path, *s, oldch;
136     char *p;
137 
138     if (ISDOT(item) || ISDOTDOT(item))
139 	return 0;
140 
141     for (s = name; *s != 0 && (*s & TRIM) == (*item & TRIM); s++, item++)
142 	continue;
143     if (*s == 0 || s[1] == 0 || *item != 0)
144 	return 0;
145 
146     path = xmalloc((Strlen(tilded_dir) + Strlen(name) + 1) * sizeof (*path));
147     (void) Strcpy(path, tilded_dir);
148     oldch = *s;
149     *s = '/';
150     Strcat(path, name);
151     p = short2str(path);
152     xfree(path);
153     if (access(p, F_OK) == 0) {
154 	extended_name->len = 0;
155 	Strbuf_append(extended_name, name);
156 	Strbuf_terminate(extended_name);
157 	/* FIXME: *s = oldch? */
158 	return 1;
159     }
160     *s = oldch;
161     return 0;
162 }
163