1 ;;; egg-com.el --- Communication Routines in Egg Input
2 ;;; Method Architecture
4 ;; Copyright (C) 1997, 1998 Mule Project, Powered by Electrotechnical
6 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
8 ;; Author: Hisashi Miyashita <himi@bird.scphys.kyoto-u.ac.jp>
9 ;; NIIBE Yutaka <gniibe@mri.co.jp>
10 ;; KATAYAMA Yoshio <kate@pfu.co.jp> ; Korean, Chinese support.
11 ;; Maintainer: NIIBE Yutaka <gniibe@mri.co.jp>
13 ;; This file will be part of GNU Emacs (in future).
15 ;; EGG is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; EGG is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
34 (defvar egg-fixed-euc 'fixed-euc-jp)
35 (make-variable-buffer-local 'egg-fixed-euc)
37 (defvar egg-mb-euc 'euc-japan)
38 (make-variable-buffer-local 'egg-mb-euc)
41 (define-ccl-program ccl-decode-fixed-euc-jp
43 ((r2 = ,(charset-id 'japanese-jisx0208))
44 (r3 = ,(charset-id 'japanese-jisx0212))
45 (r4 = ,(charset-id 'katakana-jisx0201))
52 (write-read-repeat r0))
54 (write-read-repeat r0))
58 (write-read-repeat r0))
61 (write-read-repeat r0)))))))))
63 (define-ccl-program ccl-encode-fixed-euc
68 ; (write-read-repeat r0))
69 (if (r0 == ,(charset-id 'latin-jisx0201)) ; Unify
74 (write-read-repeat r0)))
75 (r6 = (r0 == ,(charset-id 'japanese-jisx0208)))
76 (r6 |= (r0 == ,(charset-id 'japanese-jisx0208-1978)))
77 (r6 |= (r0 == ,(charset-id 'chinese-gb2312)))
78 (r6 |= (r0 == ,(charset-id 'korean-ksc5601)))
83 (write-read-repeat r0)))
84 (r6 = (r0 == ,(charset-id 'katakana-jisx0201)))
85 (r6 |= (r0 == ,(charset-id 'chinese-sisheng)))
89 (write-read-repeat r0)))
90 (if (r0 == ,(charset-id 'japanese-jisx0212)) ;G3
95 (write-read-repeat r0)))
100 (make-coding-system 'fixed-euc-jp 4 ?W "Coding System for fixed EUC Japanese"
101 (cons ccl-decode-fixed-euc-jp ccl-encode-fixed-euc))
104 (define-ccl-program ccl-decode-fixed-euc-kr
106 ((r2 = ,(charset-id 'korean-ksc5601))
114 (write-read-repeat r0)))))
116 (define-ccl-program ccl-encode-fixed-euc-kr
121 ; (write-read-repeat r0))
124 (write-read-repeat r0)))
125 (if (r0 == ,(charset-id 'korean-ksc5601))
131 (write-read-repeat r0)))
136 (make-coding-system 'fixed-euc-kr 4 ?W "Coding System for fixed EUC Korean"
137 (cons ccl-decode-fixed-euc-kr ccl-encode-fixed-euc-kr))
139 (defsubst comm-format-u32c (uint32c)
140 (let ((h0 (car uint32c))
142 (let ((b0 (logand (lsh h0 -8) 255))
144 (b2 (logand (lsh h1 -8) 255))
145 (b3 (logand h1 255)))
149 (insert-char b3 1))))
151 (defsubst comm-format-u32 (uint32)
152 (let ((b0 (logand (lsh uint32 -24) 255))
153 (b1 (logand (lsh uint32 -16) 255))
154 (b2 (logand (lsh uint32 -8) 255))
155 (b3 (logand uint32 255)))
161 (defsubst comm-format-i32 (int32)
162 (let ((b0 (logand (ash int32 -24) 255))
163 (b1 (logand (ash int32 -16) 255))
164 (b2 (logand (ash int32 -8) 255))
165 (b3 (logand int32 255)))
171 (defsubst comm-format-u16 (uint16)
172 (let ((b0 (logand (lsh uint16 -8) 255))
173 (b1 (logand uint16 255)))
177 (defsubst comm-format-u8 (uint8)
178 (let ((b0 (logand uint8 255)))
181 ;;; XXX should support other code conversion
182 (defsubst comm-format-u16-string (s)
183 (let ((euc-string (encode-coding-string s egg-fixed-euc)))
188 ;;; XXX should support other code conversion
189 (defsubst comm-format-mb-string (s)
190 (let ((euc-string (encode-coding-string s egg-mb-euc)))
194 (defsubst comm-format-u8-string (s)
198 (defsubst comm-format-bytes (s)
202 (defmacro comm-format (format &rest args)
203 "Format a string out of a control-list and arguments into the buffer.
204 u means unsigned 32-bit in big endian.
205 i means unsigned 32-bit in big endian.
206 w means unsigned 16-bit in big endian.
207 b means unsigned 8-bit.
208 S means 16-bit(big endian) wide-character string (0x0000 terminated).
209 E means multibyte string (0x00 terminated).
210 s means 8-bit string (0x00 terminated)."
211 (let ((p args) result arg f)
214 (setq f (car format))
218 (cond ((eq f 'U) (list 'comm-format-u32c arg))
219 ((eq f 'u) (list 'comm-format-u32 arg))
220 ((eq f 'i) (list 'comm-format-i32 arg))
221 ((eq f 'w) (list 'comm-format-u16 arg))
222 ((eq f 'b) (list 'comm-format-u8 arg))
223 ((eq f 'S) (list 'comm-format-u16-string arg))
224 ((eq f 'E) (list 'comm-format-mb-string arg))
225 ((eq f 's) (list 'comm-format-u8-string arg))
226 ((eq f 'B) (list 'comm-format-bytes arg))))))
228 (setq format (cdr format)))
233 ;; Do not move the point, leave it where it was.
234 (defun comm-accept-process-output (proc)
236 (accept-process-output proc)
239 ;; Assume PROC is bound to the process of current buffer
240 (defsubst comm-following-char-or-wait (proc)
243 (while (= p (point-max))
244 (accept-process-output proc))
248 (defun comm-following+forward-char (proc)
250 (comm-following-char-or-wait proc)
253 (defsubst comm-unpack-u32c (proc uint32c)
257 (lsh (comm-following+forward-char proc) 8)
258 (comm-following+forward-char proc)))
261 (lsh (comm-following+forward-char proc) 8)
262 (comm-following+forward-char proc)))
263 (set uint32c (cons h0 h1))))
265 (defsubst comm-unpack-u32 (proc uint32)
268 (lsh (comm-following+forward-char proc) 24)
269 (lsh (comm-following+forward-char proc) 16)
270 (lsh (comm-following+forward-char proc) 8)
271 (comm-following+forward-char proc))))
273 (defsubst comm-unpack-u16 (proc uint16)
276 (lsh (comm-following+forward-char proc) 8)
277 (comm-following+forward-char proc))))
279 (defsubst comm-unpack-u8 (proc uint8)
281 (comm-following+forward-char proc)))
283 ;;; XXX should support other conversion (euc-kr, cns)
284 (defsubst comm-unpack-u16-string (proc s)
285 (let ((start (point)))
286 (while (not (search-forward "\0\0" nil t))
287 (comm-accept-process-output proc))
288 (set s (buffer-substring start
290 (decode-coding-region start (- (point) 2)
293 ;;; XXX should support other conversion (euc-kr, cns)
294 (defsubst comm-unpack-mb-string (proc s)
295 (let ((start (point)))
296 (while (not (search-forward "\0" nil t))
297 (comm-accept-process-output proc))
298 (set s (buffer-substring start
300 (decode-coding-region start (- (point) 1)
303 (defsubst comm-unpack-u8-string (proc s)
304 (let ((start (point)))
305 (while (not (search-forward "\0" nil 1))
306 (comm-accept-process-output proc))
307 (set s (buffer-substring start (1- (point))))))
309 (defsubst comm-unpack-bytes (proc s)
310 (let ((start (point)))
311 (while (not (search-forward "\377" nil 1))
312 (comm-accept-process-output proc))
313 (set s (buffer-substring start (1- (point))))))
315 (defmacro comm-unpack (format &rest args)
316 "Unpack a string out of a control-string and set arguments.
317 u means unsigned 32-bit in big endian.
318 w means unsigned 16-bit in big endian.
319 b means unsigned 8-bit.
320 S means 16-bit(big endian) string (0x0000 terminated).
321 E means multibyte string (0x00 terminated).
322 s means 8-bit string (0x00 terminated).
324 (let ((p args) result arg f)
327 (setq f (car format))
331 (cond ((eq f 'U) (list 'comm-unpack-u32c
332 'proc (list 'quote arg)))
333 ((eq f 'u) (list 'comm-unpack-u32
334 'proc (list 'quote arg)))
335 ((eq f 'w) (list 'comm-unpack-u16
336 'proc (list 'quote arg)))
337 ((eq f 'b) (list 'comm-unpack-u8
338 'proc (list 'quote arg)))
339 ((eq f 'S) (list 'comm-unpack-u16-string
340 'proc (list 'quote arg)))
341 ((eq f 'E) (list 'comm-unpack-mb-string
342 'proc (list 'quote arg)))
343 ((eq f 's) (list 'comm-unpack-u8-string
344 'proc (list 'quote arg)))
345 ((eq f 'B) (list 'comm-unpack-u8-string
346 'proc (list 'quote arg)))))))
348 (setq format (cdr format)))
353 (defmacro comm-call-with-proc (proc vlist send-expr &rest receive-exprs)
358 (set-buffer (process-buffer proc))
361 (process-send-region proc (point-min) (point-max))
362 (goto-char (prog1 (point) (accept-process-output proc))))
365 (defmacro comm-call-with-proc-1 (proc vlist send-expr &rest receive-exprs)
372 (process-send-region proc (point-min) (point-max))
373 (goto-char (prog1 (point) (accept-process-output proc))))
377 ;;; egg-com.el ends here.