-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.lisp
365 lines (273 loc) · 9.31 KB
/
response.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
;;;; -*- Mode: Lisp -*-
(in-package "CL3270")
#|
;; The GO library uses 'int'. By using the more precise type, I
;; sometims get an error from the 3270 (wx3270) sending a position that
;; encodes a bad position. This also happens with the GO library.
;; Not being precise gives some leeway.
;; I guess this happens because the actual implementation of the 3720
;; protocol is a bit (a lot) shaky.
(deftype row-index ()
'(integer 0 24) ; 24 hardcoded for simple 3720
)
(deftype col-index ()
'(integer 0 80) ; 80 hardcoded for simple 3720
)
|#
(deftype row-index ()
'(integer 0 #.most-positive-fixnum)
)
(deftype col-index ()
'(integer 0 #.most-positive-fixnum)
)
(deftype aid ()
'(unsigned-byte 8) ; Hopefully right.
)
;;; Should use my own DEFENUM for the following.
(defvar *aid-symbols* (make-hash-table))
(eval-when (:load-toplevel :compile-toplevel :execute)
(defmacro def-aid-code (const-name code &optional (name nil))
`(progn
(declaim (type aid ,const-name))
(when ',name
(setf (gethash ,code *aid-symbols*) ',name))
(defconstant ,const-name ,code))))
(def-aid-code +aid-none+ #x60 none)
(def-aid-code +aid-enter+ #x7D enter)
(def-aid-code +aid-pf1+ #xF1 pf1)
(def-aid-code +aid-pf2+ #xF2 pf2)
(def-aid-code +aid-pf3+ #xF3 pf3)
(def-aid-code +aid-pf4+ #xF4 pf4)
(def-aid-code +aid-pf5+ #xF5 pf5)
(def-aid-code +aid-pf6+ #xF6 pf6)
(def-aid-code +aid-pf7+ #xF7 pf7)
(def-aid-code +aid-pf8+ #xF8 pf8)
(def-aid-code +aid-pf9+ #xF9 pf9)
(def-aid-code +aid-pf10+ #x7A pf10)
(def-aid-code +aid-pf11+ #x7B pf11)
(def-aid-code +aid-pf12+ #x7C pf12)
(def-aid-code +aid-pf13+ #xC1 pf13)
(def-aid-code +aid-pf14+ #xC2 pf14)
(def-aid-code +aid-pf15+ #xC3 pf15)
(def-aid-code +aid-pf16+ #xC4 pf16)
(def-aid-code +aid-pf17+ #xC5 pf17)
(def-aid-code +aid-pf18+ #xC6 pf18)
(def-aid-code +aid-pf19+ #xC7 pf19)
(def-aid-code +aid-pf20+ #xC8 pf20)
(def-aid-code +aid-pf21+ #xC9 pf21)
(def-aid-code +aid-pf22+ #x4A pf22)
(def-aid-code +aid-pf23+ #x4B pf23)
(def-aid-code +aid-pf24+ #x4C pf24)
(def-aid-code +aid-pa1+ #x6C pa1)
(def-aid-code +aid-pa2+ #x6E pa2)
(def-aid-code +aid-pa3+ #x6B pa3)
(def-aid-code +aid-clear+ #x6D clear)
(defun aid-to-string (aid-key)
(string (gethash aid-key *aid-symbols* "[unknown]")))
(declaim (inline is-aid-none))
(defun is-aid-none (b)
(= b +aid-none+))
(declaim (inline is-attention-key))
(defun is-attention-key (b)
(<= #x6B ; +aid-pa3+
b
#x6E ; +aid-pa2+
))
(declaim (inline is-clear-key))
(defun is-clear-key (b)
(= +aid-clear+ b))
(declaim (inline is-enter-key))
(defun is-enter-key (b)
(= +aid-enter+ b))
(declaim (inline is-pf-key))
(defun is-pf-key (b)
(or (<= +aid-pf1+ b +aid-pf9+)
(<= +aid-pf10+ b +aid-pf12+)
(<= +aid-pf13+ b +aid-pf21+)
(<= +aid-pf22+ b +aid-pf24+))
)
(defstruct response
aid
(row 0 :type row-index)
(col 0 :type col-index)
values
)
(defmethod print-object ((r response) stream)
(print-unreadable-object (r stream :identity t)
(format stream "3720: ~A ~D ~D ~S"
(response-aid r)
(response-row r)
(response-col r)
(response-values r))))
(defun read-aid (c)
;; C is a "connection", i.e., a stream tied to "telnet".
(dbgmsg ">>> Read aid~%")
(loop (multiple-value-bind (b valid _ err)
(telnet-read c nil)
(declare (ignore _))
(when (and (not valid) err)
(return-from read-aid (values +aid-none+ err)))
(when (or (is-aid-none b)
(is-enter-key b)
(is-attention-key b)
(is-pf-key b))
;; debug
(return-from read-aid (values b nil)))
;; debug
)))
(deftype field-map ()
'hash-table)
(defun read-response (c field-map)
;; C is a "connection", i.e., a stream tied to "telnet"; a USOCKET:USOCKET.
;; FIELD-MAP is a ... hash-table.
(declare (type field-map field-map))
(let ((r (make-response)))
;; Read the AID key.
(multiple-value-bind (aid err)
(read-aid c)
(when err
(format *error-output* "!!! READ-AID error ~S~%" err)
(return-from read-response (values r err)))
(dbgmsg ">>> READ-AID ~S ~S~%" aid (telnet-code-name aid))
(setf (response-aid r) aid)
;; If the use pressed clear, or a PA key we should return now
;; TODO: actually, we should consume the 0xffef, but that will
;; currently get taken care of in our next AID search.
(when (or (is-clear-key aid) (is-attention-key aid))
(return-from read-response (values r nil)))
)
;; Read the row and col (i.e., the position).
(multiple-value-bind (row col _ err)
(read-position c)
(declare (ignore _))
(when err
(format *error-output* "!!! READ-POSITION error ~S~%" err)
(return-from read-response (values r err)))
(setf (response-col r) col
(response-row r) row)
)
;; Read the field values.
(multiple-value-bind (field-values err)
(read-fields c field-map)
(when err
(return-from read-response (values r err)))
(setf (response-values r) field-values))
(values r nil)
))
(defun read-position (c
&aux
(raw (make-array 2
:element-type '(unsigned-byte 8)))
)
(dotimes (i 2)
(multiple-value-bind (b _ __ err)
(telnet-read c nil)
(declare (ignore _ __))
(when err
(return-from read-position (values 0 0 0 err)))
(dbgmsg ">>> READ-POSITION ~D ~2,'0X ~S~%"
i
b
b)
(setf (aref raw i) b)))
(let* ((addr (decode-buf-addr raw))
(row (mod addr 80))
(col (/ (- addr row) 80))
)
(dbgmsg ">>> Got position bytes ~2X ~:*~D ~2X ~:*~D, decoded to ~X, row ~d col ~d~%"
(aref raw 0)
(aref raw 1)
addr
row
col)
(values row col addr nil)
))
(defun read-fields (c fm)
;; C is a (telnet) connection
;; FM is a 'field map
(declare (type field-map fm))
(let ((infield nil)
(fieldval (make-buffer))
(fieldpos 0)
(vals (make-hash-table :test #'equal))
)
;; Consume bytes until we get #xFFEF
(loop (multiple-value-bind (b _ eor err)
(telnet-read c t)
(declare (ignore _))
(when err
(return-from read-fields (values nil err)))
(cond (eor ; Check for end of data stream (#xFFEF)
;; Finish current field.
(when infield
(dbgmsg ">>> Field ~D: ~S~%"
fieldpos
(to-ascii fieldval))
(handle-field fieldpos
fieldval
fm
vals))
(return-from read-fields (values vals nil)))
((= b #x11) ; No? Check for start-of-field-
;; Finish previous field if necessary.
(when infield
(dbgmsg ">>> Field ~D: ~S~%"
fieldpos
(to-ascii fieldval))
(handle-field fieldpos
fieldval
fm
vals))
;; Start a new field.
(setf infield t
fieldval (make-buffer)
fieldpos 0)
(multiple-value-bind (_ __ fps err)
(read-position c)
(declare (ignore _ __))
(when err
(return-from read-fields (values nil err)))
(setf fieldpos fps))
;; Next iteration.
)
((not infield) ; Consume all other bytes as field contents
; if we're in a field.
(dbgmsg ">>> Got unexpected byte while processing fields: ~2,'0x~%" b)
)
(t
(write-buffer fieldval b))
))
)))
(defun handle-field (addr val fm vals)
(declare (type fixnum addr)
(type (vector octet) val)
(type field-map fm)
;; (type map vals)
)
(multiple-value-bind (name ok)
(gethash addr fm)
;; Ok. Looks like it is a HASH-TABLE
(unless ok
(return-from handle-field nil))
(let ((v (if (plusp (length val))
(chars-to-string (code-chars (to-ascii val)))
""))
)
(setf (gethash name vals) v)
t)))
(defun decode-buf-addr (raw)
(declare (type vector raw))
(let ((d-raw-0 (aref *decodes* (aref raw 0)))
(d-raw-1 (aref *decodes* (aref raw 1)))
)
(when (or (> d-raw-0 254) (> d-raw-1 254))
(format *error-output*
"!!! UNEXPECTED VALUE: decodeBufAddr got raw value of ~2x ~2x~%"
(aref raw 0)
(aref raw 1)))
(let ((hi (ash d-raw-0 6))
(lo d-raw-1)
)
(logior hi lo)))
)
;;;; end of file -- response.lisp