tq.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; tq.el --- utility to maintain a transaction queue
  2. ;; Copyright (C) 1985-1987, 1992, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Scott Draves <spot@cs.cmu.edu>
  4. ;; Maintainer: FSF
  5. ;; Adapted-By: ESR
  6. ;; Keywords: extensions
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file manages receiving a stream asynchronously, parsing it
  20. ;; into transactions, and then calling the associated handler function
  21. ;; upon the completion of each transaction.
  22. ;; Our basic structure is the queue/process/buffer triple. Each entry
  23. ;; of the queue part is a list of question, regexp, closure, and
  24. ;; function that is consed to the last element.
  25. ;; A transaction queue may be created by calling `tq-create'.
  26. ;; A request may be added to the queue by calling `tq-enqueue'. If
  27. ;; the `delay-question' argument is non-nil, we will wait to send the
  28. ;; question to the process until it has finished sending other input.
  29. ;; Otherwise, once a request is enqueued, we send the given question
  30. ;; immediately to the process.
  31. ;; We then buffer bytes from the process until we see the regexp that
  32. ;; was provided in the call to `tq-enqueue'. Then we call the
  33. ;; provided function with the closure and the collected bytes. If we
  34. ;; have indicated that the question from the next transaction was not
  35. ;; sent immediately, send it at this point, awaiting the response.
  36. ;;; Code:
  37. ;;; Accessors
  38. ;; This part looks like (queue . (process . buffer))
  39. (defun tq-queue (tq) (car tq))
  40. (defun tq-process (tq) (car (cdr tq)))
  41. (defun tq-buffer (tq) (cdr (cdr tq)))
  42. ;; The structure of `queue' is as follows
  43. ;; ((question regexp closure . fn)
  44. ;; <other queue entries>)
  45. ;; question: string to send to the process
  46. (defun tq-queue-head-question (tq) (car (car (tq-queue tq))))
  47. ;; regexp: regular expression that matches the end of a response from
  48. ;; the process
  49. (defun tq-queue-head-regexp (tq) (car (cdr (car (tq-queue tq)))))
  50. ;; closure: additional data to pass to the function
  51. (defun tq-queue-head-closure (tq) (car (cdr (cdr (car (tq-queue tq))))))
  52. ;; fn: function to call upon receiving a complete response from the
  53. ;; process
  54. (defun tq-queue-head-fn (tq) (cdr (cdr (cdr (car (tq-queue tq))))))
  55. ;; Determine whether queue is empty
  56. (defun tq-queue-empty (tq) (not (tq-queue tq)))
  57. ;;; Core functionality
  58. ;;;###autoload
  59. (defun tq-create (process)
  60. "Create and return a transaction queue communicating with PROCESS.
  61. PROCESS should be a subprocess capable of sending and receiving
  62. streams of bytes. It may be a local process, or it may be connected
  63. to a tcp server on another machine."
  64. (let ((tq (cons nil (cons process
  65. (generate-new-buffer
  66. (concat " tq-temp-"
  67. (process-name process)))))))
  68. (buffer-disable-undo (tq-buffer tq))
  69. (set-process-filter process
  70. `(lambda (proc string)
  71. (tq-filter ',tq string)))
  72. tq))
  73. (defun tq-queue-add (tq question re closure fn)
  74. (setcar tq (nconc (tq-queue tq)
  75. (cons (cons question (cons re (cons closure fn))) nil)))
  76. 'ok)
  77. (defun tq-queue-pop (tq)
  78. (setcar tq (cdr (car tq)))
  79. (let ((question (tq-queue-head-question tq)))
  80. (condition-case nil
  81. (process-send-string (tq-process tq) question)
  82. (error nil)))
  83. (null (car tq)))
  84. (defun tq-enqueue (tq question regexp closure fn &optional delay-question)
  85. "Add a transaction to transaction queue TQ.
  86. This sends the string QUESTION to the process that TQ communicates with.
  87. When the corresponding answer comes back, we call FN with two
  88. arguments: CLOSURE, which may contain additional data that FN
  89. needs, and the answer to the question.
  90. REGEXP is a regular expression to match the entire answer;
  91. that's how we tell where the answer ends.
  92. If DELAY-QUESTION is non-nil, delay sending this question until
  93. the process has finished replying to any previous questions.
  94. This produces more reliable results with some processes."
  95. (let ((sendp (or (not delay-question)
  96. (not (tq-queue tq)))))
  97. (tq-queue-add tq (unless sendp question) regexp closure fn)
  98. (when sendp
  99. (process-send-string (tq-process tq) question))))
  100. (defun tq-close (tq)
  101. "Shut down transaction queue TQ, terminating the process."
  102. (delete-process (tq-process tq))
  103. (kill-buffer (tq-buffer tq)))
  104. (defun tq-filter (tq string)
  105. "Append STRING to the TQ's buffer; then process the new data."
  106. (let ((buffer (tq-buffer tq)))
  107. (when (buffer-live-p buffer)
  108. (with-current-buffer buffer
  109. (goto-char (point-max))
  110. (insert string)
  111. (tq-process-buffer tq)))))
  112. (defun tq-process-buffer (tq)
  113. "Check TQ's buffer for the regexp at the head of the queue."
  114. (let ((buffer (tq-buffer tq)))
  115. (when (buffer-live-p buffer)
  116. (set-buffer buffer)
  117. (if (= 0 (buffer-size)) ()
  118. (if (tq-queue-empty tq)
  119. (let ((buf (generate-new-buffer "*spurious*")))
  120. (copy-to-buffer buf (point-min) (point-max))
  121. (delete-region (point-min) (point))
  122. (pop-to-buffer buf nil)
  123. (error "Spurious communication from process %s, see buffer %s"
  124. (process-name (tq-process tq))
  125. (buffer-name buf)))
  126. (goto-char (point-min))
  127. (if (re-search-forward (tq-queue-head-regexp tq) nil t)
  128. (let ((answer (buffer-substring (point-min) (point))))
  129. (delete-region (point-min) (point))
  130. (unwind-protect
  131. (condition-case nil
  132. (funcall (tq-queue-head-fn tq)
  133. (tq-queue-head-closure tq)
  134. answer)
  135. (error nil))
  136. (tq-queue-pop tq))
  137. (tq-process-buffer tq))))))))
  138. (provide 'tq)
  139. ;;; tq.el ends here