annotate lisp/utils/tq.el @ 138:6608ceec7cf8 r20-2b3

Import from CVS: tag r20-2b3
author cvs
date Mon, 13 Aug 2007 09:31:46 +0200
parents ac2d302a0011
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; tq.el --- utility to maintain a transaction queue
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Scott Draves <spot@cs.cmu.edu>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Adapted-By: ESR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
9 ;; This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
12 ;; under the terms of the GNU General Public License as published by
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
19 ;; General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
24 ;; 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
26 ;;; Synched up with: FSF 19.34.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; manages receiving a stream asynchronously,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; parsing it into transactions, and then calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; handler functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; Our basic structure is the queue/process/buffer triple. Each entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; of the queue is a regexp/closure/function triple. We buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; bytes from the process until we see the regexp at the head of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; queue. Then we call the function with the closure and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; collected bytes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (defun tq-create (process)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 "Create and return a transaction queue communicating with PROCESS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 PROCESS should be a subprocess capable of sending and receiving
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 streams of bytes. It may be a local process, or it may be connected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 to a tcp server on another machine."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (let ((tq (cons nil (cons process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (generate-new-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (concat " tq-temp-"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (process-name process)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (set-process-filter process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (`(lambda (proc string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (tq-filter '(, tq) string))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;;; accessors
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (defun tq-queue (tq) (car tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (defun tq-process (tq) (car (cdr tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (defun tq-buffer (tq) (cdr (cdr tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (defun tq-queue-add (tq re closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (setcar tq (nconc (tq-queue tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (cons (cons re (cons closure fn)) nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 'ok)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (defun tq-queue-head-regexp (tq) (car (car (tq-queue tq))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (defun tq-queue-head-fn (tq) (cdr (cdr (car (tq-queue tq)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (defun tq-queue-head-closure (tq) (car (cdr (car (tq-queue tq)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (defun tq-queue-empty (tq) (not (tq-queue tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (defun tq-queue-pop (tq) (setcar tq (cdr (car tq))) (null (car tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;;; must add to queue before sending!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (defun tq-enqueue (tq question regexp closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 "Add a transaction to transaction queue TQ.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 This sends the string QUESTION to the process that TQ communicates with.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 When the corresponding answer comes back, we call FN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 with two arguments: CLOSURE, and the answer to the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 REGEXP is a regular expression to match the entire answer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 that's how we tell where the answer ends."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (tq-queue-add tq regexp closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (process-send-string (tq-process tq) question))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (defun tq-close (tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 "Shut down transaction queue TQ, terminating the process."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (delete-process (tq-process tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (kill-buffer (tq-buffer tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (defun tq-filter (tq string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 "Append STRING to the TQ's buffer; then process the new data."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (set-buffer (tq-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (insert string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (tq-process-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (defun tq-process-buffer (tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 "Check TQ's buffer for the regexp at the head of the queue."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (set-buffer (tq-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (if (= 0 (buffer-size)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (if (tq-queue-empty tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (let ((buf (generate-new-buffer "*spurious*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (copy-to-buffer buf (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (delete-region (point-min) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (pop-to-buffer buf nil)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
106 (error "Spurious communication from process %s, see buffer %s"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
107 (process-name (tq-process tq))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
108 (buffer-name buf)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (if (re-search-forward (tq-queue-head-regexp tq) nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (let ((answer (buffer-substring (point-min) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (delete-region (point-min) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (funcall (tq-queue-head-fn tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (tq-queue-head-closure tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 answer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (tq-queue-pop tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (tq-process-buffer tq))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (provide 'tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;;; tq.el ends here