home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / tw.spell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  5.3 KB  |  167 lines

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