/opensource.apple.com/source/tcsh/tcsh-44/tcsh/tw.spell.c
C | 189 lines | 139 code | 12 blank | 38 comment | 26 complexity | 89a9622092e72174b037cb4c68b720cf MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <title>tw.spell.c</title>
- <style type="text/css">
- .enscript-comment { font-style: italic; color: rgb(178,34,34); }
- .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
- .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
- .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
- .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
- .enscript-string { font-weight: bold; color: rgb(188,143,143); }
- .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
- .enscript-type { font-weight: bold; color: rgb(34,139,34); }
- .enscript-highlight { text-decoration: underline; color: 0; }
- </style>
- </head>
- <body id="top">
- <h1 style="margin:8px;" id="f1">tw.spell.c <span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
- <hr/>
- <div></div>
- <pre>
- <span class="enscript-comment">/* $Header: /cvs/root/tcsh/tcsh/tw.spell.c,v 1.1.1.3 2003/01/17 03:41:30 nicolai Exp $ */</span>
- <span class="enscript-comment">/*
- * tw.spell.c: Spell check words
- */</span>
- <span class="enscript-comment">/*-
- * Copyright (c) 1980, 1991 The Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */</span>
- #<span class="enscript-reference">include</span> <span class="enscript-string">"sh.h"</span>
- <span class="enscript-function-name">RCSID</span>(<span class="enscript-string">"$Id: tw.spell.c,v 1.1.1.3 2003/01/17 03:41:30 nicolai Exp $"</span>)
- #<span class="enscript-reference">include</span> <span class="enscript-string">"tw.h"</span>
- <span class="enscript-comment">/* spell_me : return corrrectly spelled filename. From K&P spname */</span>
- <span class="enscript-type">int</span>
- <span class="enscript-function-name">spell_me</span>(oldname, oldsize, looking, pat, suf)
- Char *oldname;
- <span class="enscript-type">int</span> oldsize, looking;
- Char *pat;
- <span class="enscript-type">int</span> suf;
- {
- <span class="enscript-comment">/* The +1 is to fool hp's optimizer */</span>
- Char guess[FILSIZ + 1], newname[FILSIZ + 1];
- <span class="enscript-type">register</span> Char *new = newname, *old = oldname;
- <span class="enscript-type">register</span> Char *p, *cp, *ws;
- bool foundslash = 0;
- <span class="enscript-type">int</span> retval;
- <span class="enscript-keyword">for</span> (;;) {
- <span class="enscript-keyword">while</span> (*old == <span class="enscript-string">'/'</span>) { <span class="enscript-comment">/* skip '/' */</span>
- *new++ = *old++;
- foundslash = 1;
- }
- <span class="enscript-comment">/* do not try to correct spelling of single letter words */</span>
- <span class="enscript-keyword">if</span> (*old != <span class="enscript-string">'\0'</span> && old[1] == <span class="enscript-string">'\0'</span>)
- *new++ = *old++;
- *new = <span class="enscript-string">'\0'</span>;
- <span class="enscript-keyword">if</span> (*old == <span class="enscript-string">'\0'</span>) {
- retval = (StrQcmp(oldname, newname) != 0);
- copyn(oldname, newname, oldsize); <span class="enscript-comment">/* shove it back. */</span>
- <span class="enscript-keyword">return</span> retval;
- }
- p = guess; <span class="enscript-comment">/* start at beginning of buf */</span>
- <span class="enscript-keyword">if</span> (newname[0]) <span class="enscript-comment">/* add current dir if any */</span>
- <span class="enscript-keyword">for</span> (cp = newname; *cp; cp++)
- <span class="enscript-keyword">if</span> (p < guess + FILSIZ)
- *p++ = *cp;
- ws = p;
- <span class="enscript-keyword">for</span> (; *old != <span class="enscript-string">'/'</span> && *old != <span class="enscript-string">'\0'</span>; old++)<span class="enscript-comment">/* add current file name */</span>
- <span class="enscript-keyword">if</span> (p < guess + FILSIZ)
- *p++ = *old;
- *p = <span class="enscript-string">'\0'</span>; <span class="enscript-comment">/* terminate it */</span>
- <span class="enscript-comment">/*
- * Don't tell t_search we're looking for cmd if no '/' in the name so
- * far but there are later - or it will look for *all* commands
- */</span>
- <span class="enscript-comment">/* (*should* say "looking for directory" whenever '/' is next...) */</span>
- retval = t_search(guess, p, SPELL, FILSIZ,
- looking == TW_COMMAND && (foundslash || *old != <span class="enscript-string">'/'</span>) ?
- TW_COMMAND : looking, 1, pat, suf);
- <span class="enscript-keyword">if</span> (retval >= 4 || retval < 0)
- <span class="enscript-keyword">return</span> -1; <span class="enscript-comment">/* hopeless */</span>
- <span class="enscript-keyword">for</span> (p = ws; (*new = *p++) != <span class="enscript-string">'\0'</span>; new++)
- <span class="enscript-keyword">continue</span>;
- }
- <span class="enscript-comment">/*NOTREACHED*/</span>
- #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">notdef</span>
- <span class="enscript-keyword">return</span> (0); <span class="enscript-comment">/* lint on the vax under mtXinu complains! */</span>
- #<span class="enscript-reference">endif</span>
- }
- #<span class="enscript-reference">define</span> <span class="enscript-function-name">EQ</span>(s,t) (StrQcmp(s,t) == 0)
- <span class="enscript-comment">/*
- * spdist() is taken from Kernighan & Pike,
- * _The_UNIX_Programming_Environment_
- * and adapted somewhat to correspond better to psychological reality.
- * (Note the changes to the return values)
- *
- * According to Pollock and Zamora, CACM April 1984 (V. 27, No. 4),
- * page 363, the correct order for this is:
- * OMISSION = TRANSPOSITION > INSERTION > SUBSTITUTION
- * thus, it was exactly backwards in the old version. -- PWP
- */</span>
- <span class="enscript-type">int</span>
- <span class="enscript-function-name">spdist</span>(s, t)
- <span class="enscript-type">register</span> Char *s, *t;
- {
- <span class="enscript-keyword">for</span> (; (*s & TRIM) == (*t & TRIM); t++, s++)
- <span class="enscript-keyword">if</span> (*t == <span class="enscript-string">'\0'</span>)
- <span class="enscript-keyword">return</span> 0; <span class="enscript-comment">/* exact match */</span>
- <span class="enscript-keyword">if</span> (*s) {
- <span class="enscript-keyword">if</span> (*t) {
- <span class="enscript-keyword">if</span> (s[1] && t[1] && (*s & TRIM) == (t[1] & TRIM) &&
- (*t & TRIM) == (s[1] & TRIM) && EQ(s + 2, t + 2))
- <span class="enscript-keyword">return</span> 1; <span class="enscript-comment">/* transposition */</span>
- <span class="enscript-keyword">if</span> (EQ(s + 1, t + 1))
- <span class="enscript-keyword">return</span> 3; <span class="enscript-comment">/* 1 char mismatch */</span>
- }
- <span class="enscript-keyword">if</span> (EQ(s + 1, t))
- <span class="enscript-keyword">return</span> 2; <span class="enscript-comment">/* extra character */</span>
- }
- <span class="enscript-keyword">if</span> (*t && EQ(s, t + 1))
- <span class="enscript-keyword">return</span> 1; <span class="enscript-comment">/* missing character */</span>
- <span class="enscript-keyword">return</span> 4;
- }
- <span class="enscript-type">int</span>
- <span class="enscript-function-name">spdir</span>(extended_name, tilded_dir, item, name)
- Char *extended_name;
- Char *tilded_dir;
- Char *item;
- Char *name;
- {
- Char path[MAXPATHLEN + 1];
- Char *s;
- Char oldch;
- <span class="enscript-keyword">if</span> (ISDOT(item) || ISDOTDOT(item))
- <span class="enscript-keyword">return</span> 0;
- <span class="enscript-keyword">for</span> (s = name; *s != 0 && (*s & TRIM) == (*item & TRIM); s++, item++)
- <span class="enscript-keyword">continue</span>;
- <span class="enscript-keyword">if</span> (*s == 0 || s[1] == 0 || *item != 0)
- <span class="enscript-keyword">return</span> 0;
- (<span class="enscript-type">void</span>) Strcpy(path, tilded_dir);
- oldch = *s;
- *s = <span class="enscript-string">'/'</span>;
- catn(path, name, (<span class="enscript-type">int</span>) (<span class="enscript-keyword">sizeof</span>(path) / <span class="enscript-keyword">sizeof</span>(Char)));
- <span class="enscript-keyword">if</span> (access(short2str(path), F_OK) == 0) {
- (<span class="enscript-type">void</span>) Strcpy(extended_name, name);
- <span class="enscript-keyword">return</span> 1;
- }
- *s = oldch;
- <span class="enscript-keyword">return</span> 0;
- }
- </pre>
- <hr />
- </body></html>