| 1 | ;;; -*- Log: hemlock.log; Package: Spell -*-
|
|---|
| 2 | ;;;
|
|---|
| 3 | ;;; **********************************************************************
|
|---|
| 4 | ;;; This code was written as part of the CMU Common Lisp project at
|
|---|
| 5 | ;;; Carnegie Mellon University, and has been placed in the public domain.
|
|---|
| 6 | ;;;
|
|---|
| 7 | #+CMU (ext:file-comment
|
|---|
| 8 | "$Header$")
|
|---|
| 9 | ;;;
|
|---|
| 10 | ;;; **********************************************************************
|
|---|
| 11 | ;;;
|
|---|
| 12 | ;;; Written by Bill Chiles
|
|---|
| 13 | ;;;
|
|---|
| 14 | ;;; This file contains system dependent primitives for the spelling checking/
|
|---|
| 15 | ;;; correcting code in Spell-Correct.Lisp, Spell-Augment.Lisp, and
|
|---|
| 16 | ;;; Spell-Build.Lisp.
|
|---|
| 17 |
|
|---|
| 18 | (defpackage "SPELL"
|
|---|
| 19 | (:use "LISP" "EXTENSIONS" "SYSTEM")
|
|---|
| 20 | (:export spell-try-word spell-root-word spell-collect-close-words
|
|---|
| 21 | maybe-read-spell-dictionary correct-spelling max-entry-length
|
|---|
| 22 | spell-read-dictionary spell-add-entry spell-root-flags
|
|---|
| 23 | spell-remove-entry))
|
|---|
| 24 |
|
|---|
| 25 | (in-package "SPELL")
|
|---|
| 26 |
|
|---|
| 27 | |
|---|
| 28 |
|
|---|
| 29 | ;;;; System Area Referencing and Setting
|
|---|
| 30 |
|
|---|
| 31 | (eval-when (:compile-toplevel :execute)
|
|---|
| 32 |
|
|---|
| 33 | ;;; MAKE-SAP returns pointers that *dictionary*, *descriptors*, and
|
|---|
| 34 | ;;; *string-table* are bound to. Address is in the system area.
|
|---|
| 35 | ;;;
|
|---|
| 36 | (defmacro make-sap (address)
|
|---|
| 37 | `(system:int-sap ,address))
|
|---|
| 38 |
|
|---|
| 39 | (defmacro system-address (sap)
|
|---|
| 40 | `(system:sap-int ,sap))
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | (defmacro allocate-bytes (count)
|
|---|
| 44 | `(system:allocate-system-memory ,count))
|
|---|
| 45 |
|
|---|
| 46 | (defmacro deallocate-bytes (address byte-count)
|
|---|
| 47 | `(system:deallocate-system-memory (int-sap ,address) ,byte-count))
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | (defmacro sapref (sap offset)
|
|---|
| 51 | `(system:sap-ref-16 ,sap (* ,offset 2)))
|
|---|
| 52 |
|
|---|
| 53 | (defsetf sapref (sap offset) (value)
|
|---|
| 54 | `(setf (system:sap-ref-16 ,sap (* ,offset 2)) ,value))
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | (defmacro sap-replace (dst-string src-string src-start dst-start dst-end)
|
|---|
| 58 | `(%primitive byte-blt ,src-string ,src-start ,dst-string ,dst-start ,dst-end))
|
|---|
| 59 |
|
|---|
| 60 | (defmacro string-sapref (sap index)
|
|---|
| 61 | `(system:sap-ref-8 ,sap ,index))
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | |
|---|
| 65 |
|
|---|
| 66 | ;;;; Primitive String Hashing
|
|---|
| 67 |
|
|---|
| 68 | ;;; STRING-HASH employs the instruction SXHASH-SIMPLE-SUBSTRING which takes
|
|---|
| 69 | ;;; an end argument, so we do not have to use SXHASH. SXHASH would mean
|
|---|
| 70 | ;;; doing a SUBSEQ of entry.
|
|---|
| 71 | ;;;
|
|---|
| 72 | (defmacro string-hash (string length)
|
|---|
| 73 | `(ext:truly-the lisp::index
|
|---|
| 74 | (%primitive sxhash-simple-substring
|
|---|
| 75 | ,string
|
|---|
| 76 | (the fixnum ,length))))
|
|---|
| 77 |
|
|---|
| 78 | ) ;eval-when
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | |
|---|
| 82 |
|
|---|
| 83 | ;;;; Binary Dictionary File I/O
|
|---|
| 84 |
|
|---|
| 85 | (defun open-dictionary (f)
|
|---|
| 86 | (let* ((filename (ext:unix-namestring f))
|
|---|
| 87 | (kind (unix:unix-file-kind filename)))
|
|---|
| 88 | (unless kind (error "Cannot find dictionary -- ~S." filename))
|
|---|
| 89 | (multiple-value-bind (fd err)
|
|---|
| 90 | (unix:unix-open filename unix:o_rdonly 0)
|
|---|
| 91 | (unless fd
|
|---|
| 92 | (error "Opening ~S failed: ~A." filename err))
|
|---|
| 93 | (multiple-value-bind (winp dev-or-err) (unix:unix-fstat fd)
|
|---|
| 94 | (unless winp (error "Opening ~S failed: ~A." filename dev-or-err))
|
|---|
| 95 | fd))))
|
|---|
| 96 |
|
|---|
| 97 | (defun close-dictionary (fd)
|
|---|
| 98 | (unix:unix-close fd))
|
|---|
| 99 |
|
|---|
| 100 | (defun read-dictionary-structure (fd bytes)
|
|---|
| 101 | (let* ((structure (allocate-bytes bytes)))
|
|---|
| 102 | (multiple-value-bind (read-bytes err)
|
|---|
| 103 | (unix:unix-read fd structure bytes)
|
|---|
| 104 | (when (or (null read-bytes) (not (= bytes read-bytes)))
|
|---|
| 105 | (deallocate-bytes (system-address structure) bytes)
|
|---|
| 106 | (error "Reading dictionary structure failed: ~A." err))
|
|---|
| 107 | structure)))
|
|---|