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