PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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