parse.el 47.1 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
Linus Tolke's avatar
Linus Tolke committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
Linus Tolke's avatar
Linus Tolke committed
7
8
9
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
10
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke's avatar
Linus Tolke committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
26
27
28
29
30
31
32
33
34
35
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: parse.el
;;;;
;;;; This file contains functions which parse replies from the
;;;; server. 
;;;;


36
37
38
39
40
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))


Per Cederqvist's avatar
.    
Per Cederqvist committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; ================================================================
;;;        Errors that are handled of use while parsing


(put 'lyskom-parse-incomplete 'error-conditions
     '(error lyskom-error lyskom-parse-incomplete))
(put 'lyskom-parse-incomplete 'error-message
     "LysKOM internal error: Parse incomplete.")


;;; ================================================================
;;;                  Low-level parsing.


(defun lyskom-string-skip-whitespace (string)
  "Return STRING omitting any leading whitespace."
  (let ((start (string-match "[^ \t\n\r]" string)))
    (cond ((null start) "")
	  (t (substring string start)))))

David Byers's avatar
David Byers committed
61
62
63
(defun lyskom-parse-skip-rest-of-token ()
  "Skip to the next whitespace"
  (let ((c (lyskom-parse-char)))
64
    (while (not (or (= c ?\ ) (= c ?\n)))
David Byers's avatar
David Byers committed
65
      (setq c (lyskom-parse-char)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
66
67
68
69
70
71

(defun lyskom-parse-nonwhite-char ()
  "Get next character, skipping whitespace, from lyskom-unparsed-buffer and
increase lyskom-parse-pos. Signal lyskom-parse-incomplete if
the buffer lyskom-unparsed-buffer is exhausted."
  (let ((char (lyskom-parse-char)))
David Byers's avatar
David Byers committed
72
73
74
75
    (while (or (= char ?\ )
               (= char ?\n))
      (setq char (lyskom-parse-char)))
    char))
Per Cederqvist's avatar
.    
Per Cederqvist committed
76
77
78
79
80
81
82

  
(defun lyskom-parse-char ()
  "Get next character from lyskom-unparsed-buffer and increase 
lyskom-parse-pos. Signal lyskom-parse-incomplete if the buffer 
lyskom-unparsed-buffer is exhausted."
  (cond
David Byers's avatar
David Byers committed
83
84
85
86
   ((< lyskom-parse-pos (point-max))
    (prog1 (char-after lyskom-parse-pos)
      (++ lyskom-parse-pos)))
   (t (signal 'lyskom-parse-incomplete nil))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
87
88
89
90
91
92
93


(defun lyskom-expect-char (char)
  "Read past next non-white character, which must be equal to CHAR.
Return nil, or signal lyskom-protocol-error if the
first non-white character was not equal to CHAR."
  (if (/= char (lyskom-parse-nonwhite-char))
94
95
96
      (lyskom-protocol-error 'lyskom-expect-char
                             "Expecting %S but got %S"
                             char (char-after (1- lyskom-parse-pos)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
97
98
99
100
101
    nil))


(defun lyskom-char-p (char)
  "Check if next token is CHAR (a character)."
102
  (let* ((lyskom-parse-pos lyskom-parse-pos)
103
         (c (lyskom-parse-nonwhite-char)))
104
    (eq char c)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
105
106
107
108
109
110

(defun lyskom-string-to-parse ()
  "Return unparsed data as a string."
  (lyskom-string-skip-whitespace     
   (buffer-substring lyskom-parse-pos (point-max))))

111

Per Cederqvist's avatar
.    
Per Cederqvist committed
112
113
114
115
116
(defun lyskom-parse-num ()
  "Parse the next token as a number.
Signal lyskom-parse-incomplete if the number is not followed by whitespace.
Signal lyskom-protocol-error if the next token is not a number."
  (goto-char lyskom-parse-pos)
David Byers's avatar
David Byers committed
117

David Byers's avatar
David Byers committed
118
  (cond
David Byers's avatar
David Byers committed
119
120
121
   ((looking-at "[ \n]*[0-9]+")
    (if (char-after (match-end 0))
        (progn (setq lyskom-parse-pos (goto-char (match-end 0)))
122
               (lyskom-string-to-number (match-string 0)))
David Byers's avatar
David Byers committed
123
124
125
126
      (signal 'lyskom-parse-incomplete nil)))
   ((looking-at "[ \n]*\\'") 
    (goto-char (point-max))
    (signal 'lyskom-parse-incomplete nil))
127
128
129
130
131
   (t (lyskom-protocol-error 'lyskom-parse-num
                             "Expected number, got %S"
                             (lyskom-string-to-parse))))
)

132
133
134
135
136
(defun lyskom-parse-float ()
  "Parse the next token as a float.
Signal lyskom-parse-incomplete if the number is not followed by whitespace.
Signal lyskom-protocol-error if the next token is not a number."
  (goto-char lyskom-parse-pos)
137
  (cond ((looking-at "[ \n]*[-+]?[0-9]*\\(\\.\\([0-9]*\\([eE]\\([-+]?[0-9]*\\)?\\)?\\)?\\)?")
138
139
140
141
142
143
144
145
146
147
148
         (if (char-after (match-end 0))
             (progn (setq lyskom-parse-pos (goto-char (match-end 0)))
                    (string-to-number (match-string 0)))
           (signal 'lyskom-parse-incomplete nil)))
        ((looking-at "[ \n]*\\'")
         (goto-char (point-max))
         (signal 'lyskom-parse-incomplete nil))
        (t (lyskom-protocol-error 'lyskom-parse-num
                                  "Expected float, got %S"
                                  (lyskom-string-to-parse)))))

Per Cederqvist's avatar
.    
Per Cederqvist committed
149
150
151


(defun lyskom-parse-string ()
152
153
154
  "Parse the next token as a string in the server coding system.
Signal lyskom-parse-incomplete if the string is not complete.
Signal lyskom-protocol-error if the next token is not a string."
155
156
  (lyskom-decode-coding-string (lyskom-parse-raw-string)
                               lyskom-server-coding-system))
157
158
159

(defun lyskom-parse-raw-string ()
  "Parse next token as a raw string.
Per Cederqvist's avatar
.    
Per Cederqvist committed
160
161
162
163
164
165
Signal lyskom-parse-incomplete if the string is not complete.
Signal lyskom-protocol-error if the next token is not a string."
  ;; Kludge to deal with leading spaces.
  (lyskom-parse-nonwhite-char)
  (setq lyskom-parse-pos (1- lyskom-parse-pos))
  ;; End kludge.
166
167
168
169
170
171
172
173
174
175
176
  (goto-char lyskom-parse-pos)
  (cond
   ((looking-at "[0-9]*\\(\\|H\\)\\'")
    (signal 'lyskom-parse-incomplete nil))
   ((null (looking-at "[0-9]+H"))
    (lyskom-protocol-error 'lyskom-parse-string
                           "Expected hollerith, got %S" 
                           (lyskom-string-to-parse)))   ;Not a legal string.
   (t
    (let* ((num (match-string 0))
           (end (match-end 0))
177
           (len (lyskom-string-to-number num)))
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
      (setq lyskom-parse-pos end)
      (cond
       ((< (point-max) (+ lyskom-parse-pos len))
        (lyskom-setq-default lyskom-string-bytes-missing
                             (- (+ lyskom-parse-pos len)
                                (point-max)))
        (signal 'lyskom-parse-incomplete nil))
       (t
        (prog1 (buffer-substring lyskom-parse-pos 
                                 (+ lyskom-parse-pos len))
          (lyskom-setq-default lyskom-string-bytes-missing 0)
          (setq lyskom-parse-pos (+ lyskom-parse-pos len)))))))))


;;(defun lyskom-parse-raw-string ()
;;  "Parse next token as a raw string.
;;Signal lyskom-parse-incomplete if the string is not complete.
;;Signal lyskom-protocol-error if the next token is not a string."
;;  ;; Kludge to deal with leading spaces.
;;  (lyskom-parse-nonwhite-char)
;;  (setq lyskom-parse-pos (1- lyskom-parse-pos))
;;  ;; End kludge.
;;  (let ((to-parse (lyskom-string-to-parse)))
;;    (cond
;;     ((string-match "\\`[0-9]*\\(\\|H\\)\\'" to-parse)
;;      (signal 'lyskom-parse-incomplete nil))
;;     ((null (string-match "\\`[0-9]+H" to-parse))
;;      (lyskom-protocol-error 'lyskom-parse-string
;;                             "Expected hollerith, got %S" 
;;                             to-parse)) ;Not a legal string.
;;     (t
;;      (let ((end (match-end 0))
210
;;            (len (lyskom-string-to-number to-parse)))
211
212
213
214
215
216
217
218
219
220
221
222
;;        (setq lyskom-parse-pos (+ lyskom-parse-pos end))
;;        (cond
;;         ((< (point-max) (+ lyskom-parse-pos len))
;;          (lyskom-setq-default lyskom-string-bytes-missing
;;                (- (+ lyskom-parse-pos len)
;;                   (point-max)))
;;          (signal 'lyskom-parse-incomplete nil))
;;         (t
;;          (prog1 (buffer-substring lyskom-parse-pos 
;;                                   (+ lyskom-parse-pos len))
;;            (lyskom-setq-default lyskom-string-bytes-missing 0)
;;            (setq lyskom-parse-pos (+ lyskom-parse-pos len))))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
223

David Byers's avatar
David Byers committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(defun lyskom-parse-coding ()
  "Parse next token as a raw string.
Signal lyskom-parse-incomplete if the string is not complete.
Signal lyskom-protocol-error if the next token is not a string."
  ;; Kludge to deal with leading spaces.
  (lyskom-parse-nonwhite-char)
  (setq lyskom-parse-pos (1- lyskom-parse-pos))
  ;; End kludge.
  (let ((to-parse (lyskom-string-to-parse)))
    (cond
     ((string-match "\\`[0-9]*\\(\\|C\\)\\'" to-parse)
      (signal 'lyskom-parse-incomplete nil))
     ((null (string-match "\\`[0-9]+C" to-parse))
      (lyskom-protocol-error 'lyskom-parse-string
                             "Expected hollerith, got %S" 
                             to-parse)) ;Not a legal string.
     (t
      (let ((end (match-end 0))
242
	    (len (lyskom-string-to-number to-parse)))
David Byers's avatar
David Byers committed
243
244
245
246
247
248
249
250
251
252
253
254
255
	(setq lyskom-parse-pos (+ lyskom-parse-pos end))
	(cond
	 ((< (point-max) (+ lyskom-parse-pos len))
	  (lyskom-setq-default lyskom-string-bytes-missing
		(- (+ lyskom-parse-pos len)
		   (point-max)))
	  (signal 'lyskom-parse-incomplete nil))
	 (t
	  (prog1 (buffer-substring lyskom-parse-pos 
				   (+ lyskom-parse-pos len))
            (lyskom-setq-default lyskom-string-bytes-missing 0)
	    (setq lyskom-parse-pos (+ lyskom-parse-pos len))))))))))

David Byers's avatar
David Byers committed
256
;;UNUSED: lyskom-parse-coded-string
David Byers's avatar
David Byers committed
257
258
259
260
261
(defun lyskom-parse-coded-string ()
  "Parse a string with explicit coding."
  (let ((coding (intern (lyskom-parse-coding)))
        (data (lyskom-parse-raw-string)))
    (condition-case nil
262
263
        (progn (lyskom-check-coding-system coding)
               (lyskom-decode-coding-string data coding))
David Byers's avatar
David Byers committed
264
265
266
267
268
269
      (nil data))))





Per Cederqvist's avatar
.    
Per Cederqvist committed
270
271
272
273
274
275
(defun lyskom-parse-1-or-0 ()
  "Parse next nonwhite char and return t if it was 1, nil if it was 0.
Signal lyskom-protocol-error if it was neither 1 nor 0.
Signal lyskom-parse-incomplete if there is no nonwhite char to parse."
  (let ((char (lyskom-parse-nonwhite-char)))
    (cond
276
277
     ((= char ?0) nil)
     ((= char ?1) t)
278
279
     (t (lyskom-protocol-error 'lyskom-parse-1-or-0
                               "Expected boolean, got %S" char)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
280

David Byers's avatar
David Byers committed
281
282
283
284

(defun lyskom-parse-bitstring (default)
  "Parse a generic bit string"
  (let ((result nil)
David Kågedal's avatar
David Kågedal committed
285
286
287
        (char (lyskom-parse-nonwhite-char))
	(continue t))
    (while (and continue default)
David Byers's avatar
David Byers committed
288
      (cond ((eq char ?0) (setq result (cons nil result)
David Kågedal's avatar
David Kågedal committed
289
290
			       default (cdr default)
			       char (lyskom-parse-char)))
David Byers's avatar
David Byers committed
291

David Byers's avatar
David Byers committed
292
            ((eq char ?1) (setq result (cons t result)
David Kågedal's avatar
David Kågedal committed
293
294
			       default (cdr default)
			       char (lyskom-parse-char)))
David Byers's avatar
David Byers committed
295
            ((or (= char ?\ )
296
                 (= char ?\n))
David Kågedal's avatar
David Kågedal committed
297
298
299
	     ;; This occurs when the received string is shorter than
	     ;; expected.
             (setq continue nil))
David Byers's avatar
David Byers committed
300

301
302
303
            (t (lyskom-protocol-error 'lyskom-parse-bitstring
                                      "Expected bool or space, got %S"
                                      char))))
304
    (if (not (memq char '(?\  ?\n)))
David Kågedal's avatar
David Kågedal committed
305
306
	;; This occurs when the received string is longer than
	;; expected.
307
308
309
        (progn
          (lyskom-parse-skip-rest-of-token)
          (nreverse result))
David Kågedal's avatar
David Kågedal committed
310
      (nconc (nreverse result) (copy-sequence default)))))
311

Per Cederqvist's avatar
.    
Per Cederqvist committed
312
313
314

(defun lyskom-parse-time ()
  "Parse a time from server. Args: none."
315
  (lyskom-create-time-from-utc
Per Cederqvist's avatar
.    
Per Cederqvist committed
316
317
318
319
   (lyskom-parse-num)			;sec
   (lyskom-parse-num)			;min
   (lyskom-parse-num)			;hour
   (lyskom-parse-num)			;mday
320
321
   (1+ (lyskom-parse-num))		;mon
   (+ 1900 (lyskom-parse-num))		;year
Per Cederqvist's avatar
.    
Per Cederqvist committed
322
   (lyskom-parse-num)			;wday
323
   (1+ (lyskom-parse-num))		;yday
Per Cederqvist's avatar
.    
Per Cederqvist committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
   (lyskom-parse-num)))			;isdst


;;; ================================================================
;;; Skip tokens. (Used e g to skip unknown asynchronous messages.)


(defun lyskom-skip-tokens (to-skip)
  "Skip next TO-SKIP tokens"
  (while (not (zerop to-skip))
    (lyskom-skip-one-token)
    (-- to-skip)))


(defun lyskom-skip-one-token ()
  (let ((to-parse (lyskom-string-to-parse)))
    (cond
     ((string-match "\\`{" to-parse)	;Array/list?
342
        (lyskom-parse-nonwhite-char)
Per Cederqvist's avatar
.    
Per Cederqvist committed
343
344
345
346
347
348
349
350
351
        (lyskom-skip-array))
     ((string-match "\\`*" to-parse)	;Empty array/list?
        (lyskom-parse-nonwhite-char))	;Simply skip it.
     ((string-match "\\`[0-9]+H" to-parse) ;Hollerith string?
        (lyskom-parse-string))
     ((string-match "\\`[0-9]+[ \t\n\r]" to-parse) ;Number?
        (lyskom-parse-num))
     ((string-match "\\`[0-9]\\'" to-parse)	;Incomplete number?
        (signal 'lyskom-parse-incomplete nil))
352
353
     (t (lyskom-protocol-error 'lyskom-skip-one-token
                               "Unrecognized token")))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
354
355
356
357
358


(defun lyskom-skip-array ()
  (let ((to-parse (lyskom-string-to-parse)))
    (cond
359
360
     ((string-match "\\`}" to-parse)
      (lyskom-parse-nonwhite-char))
Per Cederqvist's avatar
.    
Per Cederqvist committed
361
362
363
364
365
366
367
368
369
370
371
372
373
     (t (lyskom-skip-one-token)
	(lyskom-skip-array)))))
  

;;; ================================================================
;;; Medium level parsing. Parse arrays, misc-info-lists
;;; and other complex LysKOM types.


(defun lyskom-parse-vector (len parser)
  "Parse a vector with LEN elements.
Each element is parsed by PARSER, a function that takes no arguments."
  (cond
David Byers's avatar
David Byers committed
374
375
376
377
   ((zerop len) (if (lyskom-char-p ?*)
                    (lyskom-expect-char ?*)
                  (lyskom-expect-char ?\{)
                  (lyskom-expect-char ?\})))
Per Cederqvist's avatar
.    
Per Cederqvist committed
378
379
380
   ((lyskom-char-p ?*) (lyskom-expect-char ?*))
   (t (lyskom-expect-char ?{)
      (prog1
David Byers's avatar
   
David Byers committed
381
382
383
384
385
          (let ((res nil))
            (while (> len 0)
              (setq res (cons (funcall parser) res) len (1- len)))
            (apply 'vector (nreverse res)))
;        (lyskom-fill-vector (make-vector len nil) parser)
Per Cederqvist's avatar
.    
Per Cederqvist committed
386
387
	(lyskom-expect-char ?})))))

David Byers's avatar
David Byers committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
(defun lyskom-parse-list (len parser)
  "Parse a vector with LEN elements and return it as a list.
Each element is parsed by PARSER, a function that takes no arguments."
  (cond
   ((zerop len) (if (lyskom-char-p ?*)
                    (lyskom-expect-char ?*)
                  (lyskom-expect-char ?\{)
                  (lyskom-expect-char ?\})) nil)
   ((lyskom-char-p ?*) (lyskom-expect-char ?*) nil)
   (t (lyskom-expect-char ?{)
      (let ((result nil))
        (while (> len 0)
          (setq result (cons (funcall parser) result))
          (setq len (1- len)))
402
403
404
        (prog1
            (nreverse result)
          (lyskom-expect-char ?}))))))
David Byers's avatar
David Byers committed
405

Per Cederqvist's avatar
.    
Per Cederqvist committed
406
407
408
409
410
411
412
413

(defun lyskom-fill-vector (vector parser)
  "Fill a vector. Args: VECTOR PARSER.
Fills in all elements in VECTOR. PARSER is called for each element and the
result is assigned to the element."
  (let ((index 0)
	(len (length vector)))
    (while (< index len)
David Byers's avatar
David Byers committed
414
      (aset vector index (funcall parser))
Per Cederqvist's avatar
.    
Per Cederqvist committed
415
416
417
418
419
420
      (setq index (1+ index))))
  vector)


(defun lyskom-parse-conf-type ()
  "Parse a conf-type. No args."
David Byers's avatar
David Byers committed
421
422
423
  (apply 'lyskom-create-conf-type (lyskom-parse-bitstring 
                                   '(nil nil nil nil t nil nil nil))))
          
Per Cederqvist's avatar
.    
Per Cederqvist committed
424
425
(defun lyskom-parse-privs ()
  "Parse privileges. No args."
David Byers's avatar
David Byers committed
426
427
428
  (apply 'lyskom-create-privs
         (lyskom-parse-bitstring
          '(nil nil nil t t t nil nil nil nil nil nil nil nil nil nil))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
429
430
431
432


(defun lyskom-parse-flags ()
  "Parse Personal_flags. No args."
David Byers's avatar
David Byers committed
433
434
435
  (apply 'lyskom-create-flags
         (lyskom-parse-bitstring
          '(nil nil nil nil nil nil nil nil))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
436
437
438
439
440
441
442

	
(defun lyskom-parse-misc-info-list ()
  "Parse a misc-info-list."
  (let ((n (lyskom-parse-num))		;Number of misc-items to parse.
	(char (lyskom-parse-nonwhite-char)))
    (cond
443
     ((= char ?*)			;Empty list.
Per Cederqvist's avatar
.    
Per Cederqvist committed
444
      nil)
445
     ((= char ?{)			;Start of list.
Per Cederqvist's avatar
.    
Per Cederqvist committed
446
447
448
      (prog1
	  (lyskom-parse-misc-info-list-sub n)
	(lyskom-expect-char ?})))
449
450
     (t (lyskom-protocol-error 'lyskom-parse-misc-info-list
                               "Expected * or {, got %S" char)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472


(defun lyskom-parse-misc-info-list-sub (n)
  "Parse a misc-info list with N items."
  (let* ((result (list 'dummy))
	 (last result)
	 (next-key (lyskom-parse-num))
	 (res))
    (while (> n 0)
      (cond
       ((eq next-key 0)			;recpt
	(setq res (lyskom-parse-misc-recipient 'RECPT last n)))
       ((eq next-key 1)			;cc-recpt
	(setq res (lyskom-parse-misc-recipient 'CC-RECPT last n)))
       ((eq next-key 2)			;comm-to
	(setq res (lyskom-parse-misc-comm-to last n)))
       ((eq next-key 3)			;comm-in
	(setq res (lyskom-parse-misc-comm-in last n)))
       ((eq next-key 4)			;footn-to
	(setq res (lyskom-parse-misc-footn-to last n)))
       ((eq next-key 5)			;footn-in
	(setq res (lyskom-parse-misc-footn-in last n)))
David Byers's avatar
David Byers committed
473
474
       ((eq next-key 15)                ;bcc-recpt
        (setq res (lyskom-parse-misc-recipient 'BCC-RECPT last n)))
475
476
       (t (lyskom-protocol-error 'lyskom-parse-misc-info-list-sub
                                 "Unknown misc-info type %S" next-key)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
477
478
479
480
481
482
483
484
      (setq n (car res))
      (setq next-key (cdr res))
      (setq last (cdr last)))
    (cdr result)))			;Don't return the dummy element.
      
	      
(defun lyskom-parse-misc-recipient (type last n)
  "Parse a recipient. Args: TYPE LAST N.
David Byers's avatar
David Byers committed
485
TYPE is either RECPT, CC-RECPT or BCC-RECPT.
Per Cederqvist's avatar
.    
Per Cederqvist committed
486
487
488
489
490
491
492
493
494
495
496
LAST is a pointer to the last element on a misc-info-list.
N is number of misc-items left to parse.
Returns (cons n next-key)."
  (setcdr last (cons (lyskom-create-empty-misc-info) nil))
  (let ((info (car (cdr last)))
	(next-key nil))
      (set-misc-info->type info type)
    (set-misc-info->recipient-no info (lyskom-parse-num))
    (setq n (1- n))
    ;; A loc-no should follow.
    (if (/= 6 (lyskom-next-num n nil))
497
498
        (lyskom-protocol-error 'lyskom-parse-misc-recipient
                               "Expected 6, got something else"))
Per Cederqvist's avatar
.    
Per Cederqvist committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
    (set-misc-info->local-no info (lyskom-parse-num))
    (setq n (1- n))
    ;; A rec-time might follow.
    (if (= 7 (setq next-key (lyskom-next-num n nil)))
	(progn
	  (set-misc-info->rec-time info (lyskom-parse-time))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; A sent-by might follow.
    (if (= 8 (setq next-key (lyskom-next-num n next-key)))
	(progn
	  (set-misc-info->sender info (lyskom-parse-num))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; A sent-at might follow.
    (if (= 9 (setq next-key (lyskom-next-num n next-key)))
	(progn
	  (set-misc-info->sent-at info (lyskom-parse-time))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; Return n and next-key.
    (cons n (lyskom-next-num n next-key))))


(defun lyskom-parse-misc-comm-to (last n)
  "Parse a comm-to. Args: LAST N.
LAST is a pointer to the last element on a misc-info-list.
N is number of misc-items left to parse.
Returns (cons n next-key)."
  (setcdr last (cons (lyskom-create-empty-misc-info) nil))
  (let ((info (car (cdr last)))
	(next-key nil))
    (set-misc-info->type info 'COMM-TO)
    (set-misc-info->comm-to info (lyskom-parse-num))
    (setq n (1- n))
    ;; A sent-by might follow.
    (if (= 8 (setq next-key (lyskom-next-num n nil)))
	(progn
	  (set-misc-info->sender info (lyskom-parse-num))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; A sent-at might follow.
    (if (= 9 (setq next-key (lyskom-next-num n next-key)))
	(progn
	  (set-misc-info->sent-at info (lyskom-parse-time))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; Return n and next-key.
    (cons n (lyskom-next-num n next-key))))


(defun lyskom-parse-misc-footn-to (last n)
  "Parse a footn-to. Args: LAST N.
LAST is a pointer to the last element on a misc-info-list.
N is number of misc-items left to parse.
Returns (cons n next-key)."
  (setcdr last (cons (lyskom-create-empty-misc-info) nil))
  (let ((info (car (cdr last)))
	(next-key nil))
    (set-misc-info->type info 'FOOTN-TO)
    (set-misc-info->footn-to info (lyskom-parse-num))
    (setq n (1- n))
    ;; A sent-at might follow.
    (if (= 9 (setq next-key (lyskom-next-num n nil)))
	(progn
	  (set-misc-info->sent-at info (lyskom-parse-time))
	  (setq n (1- n))
	  (setq next-key nil)))
    ;; Return n and next-key.
    (cons n (lyskom-next-num n next-key))))


(defun lyskom-parse-misc-comm-in (last n)
  "Parse a comm-in. Args: LAST N.
LAST is a pointer to the last element on a misc-info-list.
N is number of misc-items left to parse.
Returns (cons n next-key)."
  (setcdr last (cons (lyskom-create-empty-misc-info) nil))
  (let ((info (car (cdr last))))
    (set-misc-info->type info 'COMM-IN)
    (set-misc-info->comm-in info (lyskom-parse-num))
    (setq n (1- n))
    ;; Return n and next-key.
    (cons n (lyskom-next-num n nil))))


(defun lyskom-parse-misc-footn-in (last n)
  "Parse a footn-in. Args: LAST N.
LAST is a pointer to the last element on a misc-info-list.
N is number of misc-items left to parse.
Returns (cons n next-key)."
  (setcdr last (cons (lyskom-create-empty-misc-info) nil))
  (let ((info (car (cdr last))))
    (set-misc-info->type info 'FOOTN-IN)
    (set-misc-info->footn-in info (lyskom-parse-num))
    (setq n (1- n))
    ;; Return n and next-key.
    (cons n (lyskom-next-num n nil))))

    
(defun lyskom-next-num (items-to-parse pre-fetched)
  "Parse next number if PRE-FETCHED is nil and ITEMS-TO-PARSE is greater
than 0. Args: ITEMS-TO-PARSE PRE-FETCHED. Returns -1 if ITEMS-TO-PARSE is
0."
  (cond
   ((zerop items-to-parse) -1)
   (pre-fetched)
   (t (lyskom-parse-num))))


;;; ================================================================
;;;         High level parsing. Parsing of entire datatypes.


613
614
615
616
617
(defun lyskom-parse-number-array ()
  "Parse an array of integers."
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-num))


618
619
(defun lyskom-parse-membership-type ()
  "Parse a membership type"
David Byers's avatar
David Byers committed
620
621
622
623
624
625
626
627
  (let ((type
         (apply 'lyskom-create-membership-type
                (lyskom-parse-bitstring '(nil nil nil nil nil nil nil nil)))))
    (set-membership-type->message-flag
     type
     (eq (membership-type->passive type)
         (membership-type->message-flag type)))
    type))
628
629
630
631
632

(defun lyskom-parse-member-old ()
  "Parse an old-style member record."
  (lyskom-create-member (lyskom-parse-num) 
                        0
633
                        (lyskom-current-client-time)
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
                        (lyskom-create-membership-type
                         nil nil nil nil nil nil nil nil)))

(defun lyskom-parse-member ()
  "Parse a member record"
  (lyskom-create-member
   (lyskom-parse-num)                   ;conf-no
   (lyskom-parse-num)                   ;added-by
   (lyskom-parse-time)                  ;added-at
   (lyskom-parse-membership-type)       ;type
   ))

(defun lyskom-parse-member-list ()
  "Parse a list of members"
  (lyskom-create-member-list
   (lyskom-parse-vector (lyskom-parse-num)
                        'lyskom-parse-member)))

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
(defun lyskom-convert-read-ranges-to-map (read-ranges)
  (let ((result nil)
        a b)
    (while read-ranges
      (setq a (car (car read-ranges))
            b (cdr (car read-ranges))
            read-ranges (cdr read-ranges))
      (while (<= a b) (setq result (cons a result) a (1+ a))))
    (and result (apply 'vector (nreverse result)))))

(defun lyskom-parse-membership-11 ()
  "Parse a membership, converting read-ranges to the old style."
  (let ((read-ranges nil))
    (lyskom-create-membership
     (lyskom-parse-num)                 ;position
     (lyskom-parse-time)                ;last-time-read
     (lyskom-parse-num)			;conf-no
     (lyskom-parse-num)			;priority
     (progn (setq read-ranges 
                  (listify-vector
                   (lyskom-parse-vector
                    (lyskom-parse-num) 'lyskom-parse-read-range)))
            (if (eq (car (car read-ranges)) 1)
                (prog1 (cdr (elt read-ranges 0))
                       (setq read-ranges (cdr read-ranges)))
              0))
     (lyskom-convert-read-ranges-to-map read-ranges)
     (lyskom-parse-num)                 ;added-by
     (lyskom-parse-time)                ;added-at
     (lyskom-parse-membership-type))))

683
(defun lyskom-parse-membership-10 ()
Per Cederqvist's avatar
.    
Per Cederqvist committed
684
  "Parse a membership."
David Byers's avatar
David Byers committed
685
686
687
688
689
690
691
692
693
694
695
    (lyskom-create-membership
     (lyskom-parse-num)                 ;position
     (lyskom-parse-time)                ;last-time-read
     (lyskom-parse-num)			;conf-no
     (lyskom-parse-num)			;priority
     (lyskom-parse-num)			;last-text-read
     (lyskom-parse-vector               ;read-texts
      (lyskom-parse-num) 'lyskom-parse-num)
     (lyskom-parse-num)                 ;added-by
     (lyskom-parse-time)                ;added-at
     (lyskom-parse-membership-type)))
696
697
698
699

(defun lyskom-parse-membership-old ()
  "Parse a membership."
  (lyskom-create-membership
David Byers's avatar
David Byers committed
700
   nil
701
702
703
704
705
706
707
   (lyskom-parse-time)			;last-time-read
   (lyskom-parse-num)			;conf-no
   (lyskom-parse-num)			;priority
   (lyskom-parse-num)			;last-text-read
   (lyskom-parse-vector			;read-texts
    (lyskom-parse-num) 'lyskom-parse-num)
   0
708
   (lyskom-current-client-time)
709
   (lyskom-create-membership-type nil nil nil nil nil nil nil nil)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
710
711


712
713
714
715
716
717
718
(defun lyskom-parse-version-info ()
  "Parse info about the server and protocol."
  (lyskom-create-version-info
   (lyskom-parse-num)
   (lyskom-parse-string)
   (lyskom-parse-string)))

Per Cederqvist's avatar
.    
Per Cederqvist committed
719
(defun lyskom-parse-server-info ()
720
721
722
723
724
725
726
727
728
729
730
  "Parse info about the server."
  (lyskom-create-server-info
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-aux-item-list)))

(defun lyskom-parse-server-info-old ()
Per Cederqvist's avatar
.    
Per Cederqvist committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  "Parse info about the server."
  (lyskom-create-server-info
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)
   (lyskom-parse-num)))


(defun lyskom-parse-map ()
  "Parse a text-list (also known as map)."
  (lyskom-create-map
   (lyskom-parse-num)			;first-local
   (lyskom-parse-vector			;text-nos
    (lyskom-parse-num) 'lyskom-parse-num)))

(defun lyskom-parse-who-info ()
  "Parse a who-info."
  (lyskom-create-who-info
   (lyskom-parse-num)			;pers-no
   (lyskom-parse-num)			;working-conf
   (lyskom-parse-num)			;connection
   (lyskom-parse-string)		;doing-what
   (lyskom-parse-string)))		;userid@host

David Byers's avatar
David Byers committed
757
758
759
760
761
762
763
764
765
766
767
768
769
(defun lyskom-parse-who-info-ident ()
  "Parse a who-info-ident"
  (lyskom-create-who-info
   (lyskom-parse-num)			;pers-no
   (lyskom-parse-num)			;working-conf
   (lyskom-parse-num)			;connection
   (lyskom-parse-string)		;doing-what
   (lyskom-parse-string)		;userid@host
   (lyskom-parse-string)                ;hostname
   (lyskom-parse-string)                ;ident-user
   ))


Per Cederqvist's avatar
.    
Per Cederqvist committed
770
771
772
773
774
775
776
777
(defun lyskom-parse-session-info ()
  "Parse a session-info."
  (lyskom-create-session-info
   (lyskom-parse-num)			;pers-no
   (lyskom-parse-num)			;working-conf
   (lyskom-parse-num)			;connection
   (lyskom-parse-string)		;doing
   (lyskom-parse-string)		;userid@host
David Byers's avatar
David Byers committed
778
779
   nil                                  ;username
   nil                                  ;ident-user
Per Cederqvist's avatar
.    
Per Cederqvist committed
780
781
782
   (lyskom-parse-num)			;idletime
   (lyskom-parse-time)))		;connect-time

David Byers's avatar
David Byers committed
783
784
785
786
787
788
789
790
791
792
793
794
(defun lyskom-parse-session-info-ident ()
  "Parse a session-info."
  (lyskom-create-session-info
   (lyskom-parse-num)			;pers-no
   (lyskom-parse-num)			;working-conf
   (lyskom-parse-num)			;connection
   (lyskom-parse-string)		;doing
   (lyskom-parse-string)		;userid@host
   (lyskom-parse-string)                ;username
   (lyskom-parse-string)                ;ident-user
   (lyskom-parse-num)			;idletime
   (lyskom-parse-time)))                ;connect-time
Per Cederqvist's avatar
.    
Per Cederqvist committed
795

796
797
;; prot-A.txt says that this should allow more or less flags than
;; specified, but I can't figure out how. /davidk
David Byers's avatar
David Byers committed
798

799
800
(defun lyskom-parse-session-flags ()
  "Parse session-flags."
David Byers's avatar
David Byers committed
801
802
803
  (apply 'lyskom-create-session-flags
         (lyskom-parse-bitstring
          '(nil nil nil nil nil nil nil nil))))
804
805
806
807
808
809
810
811
812
813
814
815
816

	
(defun lyskom-parse-dynamic-session-info ()
  "Parse a dynamic-session-info."
  (lyskom-create-dynamic-session-info
   (lyskom-parse-num)			;session-no
   (lyskom-parse-num)			;pers-no
   (lyskom-parse-num)			;working-conf
   (lyskom-parse-num)			;idle-time
   (lyskom-parse-session-flags)		;session-flags
   (lyskom-parse-string)))		;doing


Per Cederqvist's avatar
.    
Per Cederqvist committed
817
818
819
820
821
822
823
824
;;; High level parsing. Parsing of complete replies.


(defun lyskom-parse-void ()
  "Parse result from functions that only return an OK/FAILURE."
  t)					;Needn't do anything.
			  

David Byers's avatar
David Byers committed
825
826
(defun lyskom-parse-conf-no-list ()
  "Parse result from functions that return conf-no-list"
David Byers's avatar
David Byers committed
827
828
829
  (lyskom-create-conf-no-list
   (lyskom-parse-vector (lyskom-parse-num)
                        'lyskom-parse-num)))
David Byers's avatar
David Byers committed
830

David Byers's avatar
David Byers committed
831

Per Cederqvist's avatar
.    
Per Cederqvist committed
832
833
834
835
836
837
838
839
(defun lyskom-parse-conf-list ()
  "Parse result from functions that return a conf-list."
  (let* ((list-len (lyskom-parse-num)))
    (lyskom-create-conf-list
     (lyskom-parse-vector list-len 'lyskom-parse-num)
     (lyskom-parse-vector list-len 'lyskom-parse-conf-type))))


840
(defun lyskom-parse-member-list-old ()
Per Cederqvist's avatar
.    
Per Cederqvist committed
841
  "Parse result from functions that return a conf-no-list."
842
843
  (lyskom-create-member-list
   (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-member-old)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
844
845
846
847
848
849
850
851
852
853
854
855


(defun lyskom-parse-mark-list ()
  "Parser result from functions that returns a mark-list."
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-mark))


(defun lyskom-parse-mark ()
  "Parse a marked text."
  (lyskom-create-mark
   (lyskom-parse-num)			;Text-no
   (lyskom-parse-num)))			;Mark-type
856
857
858
859
860
861
862
863

(defun lyskom-parse-aux-item-list ()
  "Parse an aux-item list"
  (listify-vector
   (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-aux-item)))

(defun lyskom-parse-aux-item ()
  "Parse an aux-item"
864
865
866
867
868
869
870
871
872
  (lyskom-aux-item-after-parse
   (lyskom-create-aux-item (lyskom-parse-num)    ; aux-no
                           (lyskom-parse-num)    ; tag
                           (lyskom-parse-num)    ; creator
                           (lyskom-parse-time)   ; sent-at
                           (lyskom-parse-aux-item-flags)
                           (lyskom-parse-num)    ; inherit-limit
                           (lyskom-parse-raw-string) ; data
                           )))
873
874
875
876
877
878

(defun lyskom-parse-aux-item-flags ()
  "Parse aux-item flags"
  (apply 'lyskom-create-aux-item-flags
         (lyskom-parse-bitstring
          '(nil nil nil nil nil nil nil nil))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
879
880
881
882
883
884
  

;;;================================================================
;;; Parsing of datatypes with cache


885
886
887
888
889
890
891
892
893
894
895
896
897
(defun lyskom-parse-static-session-info (session)
  "Parse a static-session-info and add it to the cache."
  (let ((info (lyskom-create-static-session-info
	       (lyskom-parse-string)	;username
	       (lyskom-parse-string)	;hostname
	       (lyskom-parse-string)	;ident-user
	       (lyskom-parse-time))))	;connection-time
    (lyskom-save-excursion
    	(set-buffer lyskom-buffer)
    	(cache-add-static-session-info session info))
    info))
    

898
(defun lyskom-parse-conf-stat-old (conf-no)
Per Cederqvist's avatar
.    
Per Cederqvist committed
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
  "Parse a conf-stat, add add it in the cache.
Retuns the conf-stat. Args: CONF-NO."
  (let
      ((conf-stat (lyskom-create-conf-stat
		   conf-no		;conf-no (supplied by
					; initiate-get-conf-stat)
		   (lyskom-parse-string) ;name
		   (lyskom-parse-conf-type) ;conf-type
		   (lyskom-parse-time)	;creation-time
		   (lyskom-parse-time)	;last-written
		   (lyskom-parse-num)	;creator
		   (lyskom-parse-num)	;presentation
		   (lyskom-parse-num)	;supervisor
		   (lyskom-parse-num)	;permitted-submitters
		   (lyskom-parse-num)	;super-conf
		   (lyskom-parse-num)	;msg-of-day
		   (lyskom-parse-num)	;garb-nice
David Byers's avatar
David Byers committed
916
                   77                   ;fake keep-commented
Per Cederqvist's avatar
.    
Per Cederqvist committed
917
918
919
920
921
922
923
924
925
		   (lyskom-parse-num)	;no-of-members
		   (lyskom-parse-num)	;first-local-no
		   (lyskom-parse-num)))) ;no-of-texts
    
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-conf-stat conf-stat))
    conf-stat))

926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
(defun lyskom-parse-conf-stat (conf-no)
  "Parse a conf-stat, add add it in the cache.
Retuns the conf-stat. Args: CONF-NO."
  (let
      ((conf-stat (lyskom-create-conf-stat
		   conf-no		;conf-no (supplied by
					; initiate-get-conf-stat)
		   (lyskom-parse-string) ;name
		   (lyskom-parse-conf-type) ;conf-type
		   (lyskom-parse-time)	;creation-time
		   (lyskom-parse-time)	;last-written
		   (lyskom-parse-num)	;creator
		   (lyskom-parse-num)	;presentation
		   (lyskom-parse-num)	;supervisor
		   (lyskom-parse-num)	;permitted-submitters
		   (lyskom-parse-num)	;super-conf
		   (lyskom-parse-num)	;msg-of-day
		   (lyskom-parse-num)	;garb-nice
David Byers's avatar
David Byers committed
944
                   (lyskom-parse-num)   ;keep-commented
945
946
947
948
949
950
951
952
953
954
955
956
		   (lyskom-parse-num)	;no-of-members
		   (lyskom-parse-num)	;first-local-no
		   (lyskom-parse-num)   ;no-of-texts
                   (lyskom-parse-num)   ;expire
                   (lyskom-parse-aux-item-list)
                   )))
    
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-conf-stat conf-stat))
    conf-stat))

Per Cederqvist's avatar
.    
Per Cederqvist committed
957

958
959
960
961
962
963
964
965
966
967
968
969
970
971
972

(defun lyskom-parse-uconf-stat (conf-no)
  "Parse a uconf-stat, and add it to the cache.
Returns the conf-stat. Args CONF-NO."
  (let ((conf-stat (lyskom-create-uconf-stat conf-no
					     (lyskom-parse-string)
					     (lyskom-parse-conf-type)
					     (lyskom-parse-num)
					     (lyskom-parse-num))))
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-uconf-stat conf-stat))
    conf-stat))


Per Cederqvist's avatar
.    
Per Cederqvist committed
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
(defun lyskom-parse-pers-stat (pers-no)
  "Parse a pers-stat, add add it in the cache.
Retuns the pers-stat. Args: PERS-NO."
  (let
      ((pers-stat
	(lyskom-create-pers-stat
	 pers-no			;pers-no
	 (lyskom-parse-string)		;username
	 (lyskom-parse-privs)		;privileges
	 (lyskom-parse-flags)		;flags
	 (lyskom-parse-time)		;last-login
	 (lyskom-parse-num)		;user-area
	 (lyskom-parse-num)		;total-time-present
	 (lyskom-parse-num)		;sessions
	 (lyskom-parse-num)		;created-lines
	 (lyskom-parse-num)		;created-bytes
	 (lyskom-parse-num)		;read-texts
	 (lyskom-parse-num)		;no-of-text-fetches
	 (lyskom-parse-num)		;created-persons
	 (lyskom-parse-num)		;created-confs
	 (lyskom-parse-num)		;first-created-text
	 (lyskom-parse-num)		;no-of-created-texts
	 (lyskom-parse-num)		;no-of-marks
	 (lyskom-parse-num))))		;no-of-confs
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-pers-stat pers-stat))
    pers-stat))


1003
(defun lyskom-parse-text-stat-old (text-no)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
  "Parse a text-stat and add it in the cache. 
Args: TEXT-NO. Value: text-stat."
  (let
      ((text-stat (lyskom-create-text-stat
		   text-no
		   (lyskom-parse-time)	;creation-time
		   (lyskom-parse-num)	;author
		   (lyskom-parse-num)	;no-of-lines
		   (lyskom-parse-num)	;no-of-chars
		   (lyskom-parse-num)	;no-of-marks
		   (lyskom-parse-misc-info-list)))) ;misc-info-list
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-text-stat text-stat))
    text-stat))

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
(defun lyskom-parse-text-stat (text-no)
  "Parse a text-stat and add it in the cache. 
Args: TEXT-NO. Value: text-stat."
  (let
      ((text-stat (lyskom-create-text-stat
		   text-no
		   (lyskom-parse-time)	;creation-time
		   (lyskom-parse-num)	;author
		   (lyskom-parse-num)	;no-of-lines
		   (lyskom-parse-num)	;no-of-chars
		   (lyskom-parse-num)	;no-of-marks
		   (lyskom-parse-misc-info-list)  ;misc-info-list
                   (lyskom-parse-aux-item-list)
                   )))
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-text-stat text-stat))
    text-stat))

Per Cederqvist's avatar
.    
Per Cederqvist committed
1039
1040
1041
1042
1043
1044

(defun lyskom-parse-text (text-no)
  "Parse a text and add it to the cache.
  Args: TEXT-NO. Result: text-stat."
  (let ((text (lyskom-create-text
	       text-no
1045
	       (lyskom-parse-raw-string))))		;The text.
Per Cederqvist's avatar
.    
Per Cederqvist committed
1046
1047
1048
1049
1050
1051
    (lyskom-save-excursion
     (set-buffer lyskom-buffer)
     (cache-add-text text))
    text))


1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
(defun lyskom-parse-conf-z-info-list ()
  "Parse result from functions that return a conf-z-info-list."
  (let* ((list-len (lyskom-parse-num)))
    (lyskom-create-conf-z-info-list
     (lyskom-parse-vector list-len 'lyskom-parse-conf-z-info))))

(defun lyskom-parse-conf-z-info ()
  "Parse a conf-z-info."
  (lyskom-create-conf-z-info
   (lyskom-parse-string)		;name
   (lyskom-parse-conf-type)		;conf-type
   (lyskom-parse-num)))			;conf-no


1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
(defun lyskom-parse-text-mapping (existing)
  "Parse a Text-Mapping"
  (let ((block-type nil))
    (lyskom-create-text-mapping
     (lyskom-parse-num)
     (lyskom-parse-num)
     existing
     (lyskom-parse-1-or-0)
     (let ((val (lyskom-parse-num)))
       (cond ((= val 0) (setq block-type 'sparse))
             ((= val 1) (setq block-type 'dense)))
       block-type)
     (lyskom-parse-local-to-global-block block-type))))

(defun lyskom-parse-local-to-global-block (block-type)
  "Parse a Local-To-Global-Block"
  (cond ((eq block-type 'sparse)
1083
         (let ((len (lyskom-parse-num)))
1084
1085
1086
1087
1088
1089
1090
           (lyskom-parse-list len 'lyskom-parse-text-number-pair)))
        ((eq block-type 'dense)
         (lyskom-parse-map))))

(defun lyskom-parse-text-number-pair ()
  "Parse a Text-Number-Pair"
  (lyskom-create-text-pair (lyskom-parse-num) (lyskom-parse-num)))
1091

1092
1093
1094
1095
(defun lyskom-parse-read-range ()
  "Parse a Read-Range"
  (lyskom-create-text-pair (lyskom-parse-num) (lyskom-parse-num)))

1096
1097
(defun lyskom-parse-stats-description ()
  "Parse a Stats-Description"
1098
1099
1100
1101
1102
1103
  (let ((tmp (lyskom-create-stats-description
              (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-string)
              (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-num))))
    (lyskom-save-excursion
      (set-buffer lyskom-buffer)
      (setq lyskom-stats-description tmp))))
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114

(defun lyskom-parse-stats ()
  "Parse a Stats"
  (lyskom-create-stats (lyskom-parse-float)
                       (lyskom-parse-float)
                       (lyskom-parse-float)))

(defun lyskom-parse-stats-array ()
  "Parse an array of Stats"
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-stats))

1115
1116
(defun lyskom-parse-static-server-info ()
  "Parse a static-server-info"
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  (let ((tmp (lyskom-create-static-server-info (lyskom-parse-time)
                                               (lyskom-parse-time)
                                               (lyskom-parse-string)
                                               (lyskom-parse-num)
                                               (lyskom-parse-num)
                                               (lyskom-parse-num)
                                               (lyskom-parse-num)
                                               (lyskom-parse-num))))
    (lyskom-save-excursion
      (set-buffer lyskom-buffer)
      (setq lyskom-static-server-info tmp))))
1128

David Byers's avatar
David Byers committed
1129
1130
1131
1132
1133
(defun lyskom-parse-scheduling-info ()
  "Parse a scheduling-info"
  (lyskom-create-scheduling-info (lyskom-parse-num)
                                 (lyskom-parse-num)))

1134

Per Cederqvist's avatar
.    
Per Cederqvist committed
1135
1136
1137
1138
;;; ================================================================
;;;          Parsing of complex datatypes without cache.


1139
1140
1141
1142
(defun lyskom-parse-membership-list-11 ()
  "Parse a membership-list. Returns a vector."
   (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-membership-11))

1143
(defun lyskom-parse-membership-list-10 ()
Per Cederqvist's avatar
.    
Per Cederqvist committed
1144
  "Parse a membership-list. Returns a vector."
1145
   (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-membership-10))
1146
1147
1148
1149

(defun lyskom-parse-membership-list-old ()
  "Parse a membership-list. Returns a vector."
   (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-membership-old))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1150
1151
1152
1153
1154
1155


(defun lyskom-parse-who-info-list ()
  "Parse a who-info-list. Returns a vector."
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-who-info))
  
David Byers's avatar
David Byers committed
1156
1157
1158
1159
(defun lyskom-parse-who-info-ident-list ()
  "Parse a who-info-ident-list. Returns a vector."
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-who-info-ident))
  
Per Cederqvist's avatar
.    
Per Cederqvist committed
1160

1161
1162
1163
1164
1165
(defun lyskom-parse-dynamic-session-info-list ()
  "Parse a who-info-list. Returns a vector."
  (lyskom-parse-vector (lyskom-parse-num) 'lyskom-parse-dynamic-session-info))
  

David Kågedal's avatar
David Kågedal committed
1166
(defun lyskom-init-parse (buffer)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1167
1168
  "Does all initialization of the parsing routines.
i.e creates the buffer, sets all markers and pointers."
1169
  (setq lyskom-is-parsing nil)
1170
  (setq lyskom-unparsed-buffer 
1171
	(lyskom-generate-new-buffer 
1172
1173
1174
	 (concat (if lyskom-debug-communications-to-buffer "" " ")
		 (buffer-name)
		 "-replies")))
1175
  (lyskom-set-buffer-parent lyskom-unparsed-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
1176
  (lyskom-update-inherited-variables lyskom-unparsed-buffer)
1177
  (save-excursion (set-buffer lyskom-unparsed-buffer)
1178
		  (lyskom-set-buffer-multibyte nil))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1179
1180
1181
1182
  (setq lyskom-unparsed-marker 
	(lyskom-save-excursion
	 (let ((proc lyskom-proc))
	   (set-buffer lyskom-unparsed-buffer)
David Kågedal's avatar
David Kågedal committed
1183
 	   (make-local-variable 'lyskom-proc)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1184
1185
	   (make-local-variable 'lyskom-string-bytes-missing)
	   (setq lyskom-proc proc)
David Kågedal's avatar
David Kågedal committed
1186
1187
	   (make-local-variable 'lyskom-buffer)
	   (setq lyskom-buffer buffer)
David Byers's avatar
David Byers committed
1188
           (lyskom-setq-default lyskom-string-bytes-missing 0)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
	   (goto-char (point-max))
	   (point-marker)))))


;;; ================================================================
;;;        Functions that call the other parsing functions. 


(defun lyskom-parse-success (ref-no buffer)
  "Parse the results of a successful call and call the handler."
  (lyskom-save-excursion
David Kågedal's avatar
David Kågedal committed
1200
   (set-buffer buffer)
David Kågedal's avatar
David Kågedal committed
1201
   (let* ((kom-queue (cdr (assq ref-no lyskom-pending-calls)))
David Kågedal's avatar
David Kågedal committed
1202
	  (call-info (lyskom-locate-ref-no kom-queue ref-no)))
David Kågedal's avatar
David Kågedal committed
1203
1204
     (set-buffer lyskom-unparsed-buffer)
     (if call-info
1205
	 (lyskom-apply-parser call-info))
David Kågedal's avatar
David Kågedal committed
1206
1207
1208
1209
1210
     (set-buffer buffer)
     (lyskom-decrease-pending-calls)
     (setq lyskom-pending-calls
	   (lyskom-assoc-dremove ref-no lyskom-pending-calls))
     (lyskom-check-call kom-queue))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241


(defun lyskom-locate-ref-no (kom-queue ref-no)
  (let ((pending (lyskom-queue->all-entries
		  (kom-queue->pending
		   (cdr (assoc kom-queue lyskom-call-data)))))
	(result nil))
    (while (and (null result)
		(not (null pending)))
      (cond
       ((and (eq (car (car pending)) 'CALL)
	     (eq (car (cdr (car pending))) ref-no))
	(setq result (car pending)))
       (t (setq pending (cdr pending)))))
    result))

     
(defun lyskom-assoc-dremove (elt list)
  "Args: ELT LIST. Return a copy of LIST, but exclude any elements whose
car is equal to ELT."
  (let* ((head (cons nil list))
	 (tail head))
    (while (not (null (cdr tail)))
      (cond
       ((equal elt (car (car (cdr tail))))
	(setcdr tail (cdr (cdr tail))))
       (t
	(setq tail (cdr tail)))))
    (cdr head)))


1242
(defun lyskom-apply-parser (call-info)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
  "Try to parse a reply from the server.
CALL-INFO looks like this: (See lyskom-call-data). 
	('CALL REF-NO PARSER PARSER-DATA HANDLER HANDLER-DATA)
PARSER is called, and if it succeeds
CALL-INFO is destructively changed to
	('PARSED RESULT HANDLER HANDLER-DATA)"
  (let ((result
	 (apply (car (cdr (cdr call-info)))	     ;Parser
		(car (cdr (cdr (cdr call-info))))))) ;Parser-data
    ;; If some part of the reply has not yet arrived
    ;; the parser will signal lyskom-parse-incomplete
    ;; and execution will not continue here.
    ;; The parse is complete. Change the call-info.
    (lyskom-tr-call-to-parsed call-info result)))


(defun lyskom-tr-call-to-parsed (call-info result)
  "Transform a CALL to a PARSED."
  (setcar call-info 'PARSED)
1262
1263
  (setcar (cdr (cdr call-info)) result)
  (setcdr (cdr (cdr call-info)) (cdr (cdr (cdr (cdr call-info))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1264
1265
1266
1267
1268
1269


(defun lyskom-parse-error (ref-no buffer)
  "Parse the result of an unsuccessful call and call the handler."
  (lyskom-save-excursion
    (set-buffer buffer)
David Kågedal's avatar
David Kågedal committed
1270
    (let* ((kom-queue (cdr (assq ref-no lyskom-pending-calls)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1271
	   (call-info (lyskom-locate-ref-no kom-queue ref-no))
David Byers's avatar
David Byers committed
1272
	   errno err-stat)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1273
1274
      (set-buffer lyskom-unparsed-buffer)
      (setq errno (lyskom-parse-num))
David Byers's avatar
David Byers committed
1275
      (setq err-stat (lyskom-parse-num))		;Skip ref_no.
Per Cederqvist's avatar
.    
Per Cederqvist committed
1276
1277
      (set-buffer buffer)
      (setq lyskom-errno errno)
David Byers's avatar
David Byers committed
1278
      (setq lyskom-err-stat err-stat)
Per Cederqvist's avatar
.    
Per Cederqvist committed
1279
1280
1281
      (setq lyskom-pending-calls
	    (lyskom-assoc-dremove ref-no lyskom-pending-calls))
      (lyskom-decrease-pending-calls)
1282
1283
      (when call-info
	  (lyskom-tr-call-to-parsed call-info nil))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
      (lyskom-check-call kom-queue))))


(defun lyskom-parse-unparsed ()
  "Parse all complete replies in lyskom-unparsed-buffer.
All parsing is to take place in this buffer but calling the functions:
lyskom-parse-success, lyskom-parse-error and lyskom-parse-async calls 
functions and variables that are connected with the lyskom-buffer."
  (let ((lyskom-buffer (current-buffer))
	(match-data (match-data)))
David Kågedal's avatar
David Kågedal committed
1294
1295
1296
    ;; Was the server saving?
    (if lyskom-is-saving
	(progn
David Kågedal's avatar
David Kågedal committed
1297
1298
1299
1300
1301
	  (setq mode-line-process (lyskom-get-string
				   (if lyskom-executing-command
				       'mode-line-working
				     'mode-line-waiting))
		lyskom-is-saving nil)
1302
1303
1304
1305
1306
	  ;; I guess the following two lines could be replaced by
	  ;; force-mode-line-update in a modern emacs.
	  (set-buffer-modified-p (buffer-modified-p))
	  (sit-for 0)

David Kågedal's avatar
David Kågedal committed
1307
1308
1309
	  ;; Removed check for kom-presence-messages
	  (if (and (not (lyskom-is-in-minibuffer)))
	      (message ""))))
David Byers's avatar
David Byers committed
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
    (if lyskom-parser-recovering
        (lyskom-save-excursion
          (set-buffer lyskom-unparsed-buffer)
          (goto-char (point-min))
          (if (re-search-forward "^[:%=]" nil t)
              (progn (delete-region (point-min) (match-beginning 0))
                     (lyskom-set-default 'lyskom-parser-recovering nil))
            (when (> (point-max) (point-min))
              (delete-region (point-min) (1- (point-max))))))
      (lyskom-save-excursion
        (set-buffer lyskom-unparsed-buffer)
        (while (not (zerop (1- (point-max)))) ;Parse while replies.
          (let* ((lyskom-parse-pos 1)
1323
1324
1325
1326
1327
                 (key (lyskom-parse-nonwhite-char))
                 (endpos nil))
            (unwind-protect
                (condition-case err
                    (let ((inhibit-quit t)) ; Used to be nil, but that can
1328
1329
					; cause hard-to-repair
					; problems
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
                      (cond
                       ((= key ?=)      ;The call succeeded.
                        (lyskom-parse-success (lyskom-parse-num) lyskom-buffer))
                       ((= key ?%)      ;The call was not successful.
                        (lyskom-parse-error (lyskom-parse-num) lyskom-buffer))
                       ((= key ?:)      ;An asynchronous message.
                        (lyskom-parse-async (lyskom-parse-num) lyskom-buffer))
                       (t
                        (lyskom-protocol-error 'lyskom-parse-unparsed
                                               "Expected =, %% or :, got %S"
                                               (lyskom-string-to-parse))))
                      (setq endpos lyskom-parse-pos))
                  ;; One reply is now parsed.
                  (lyskom-protocol-error
                   (setq endpos (min (point-max) (1+ lyskom-parse-pos)))
                   (signal 'lyskom-protocol-error err))
                  (lyskom-parse-incomplete (setq endpos (point-min))
                                           (signal (car err) (cdr err))))
              (delete-region (point-min) (or endpos lyskom-parse-pos)))
David Byers's avatar
David Byers committed
1349
1350
1351
1352
            (goto-char (point-min))
            (if (looking-at "[ \n]+")
                (delete-region (match-beginning 0) (match-end 0)))
            ))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1353
1354
    (store-match-data match-data)))

1355
1356
(defun lyskom-protocol-error (function format-string &rest args)
  (when lyskom-debug-communications-to-buffer
David Byers's avatar
David Byers committed
1357
    (setq lyskom-debug-communications-limit nil)
1358
1359
    (lyskom-debug-insert lyskom-proc
                         (format " Protocol error in %S: " function)
David Byers's avatar
David Byers committed
1360
                         (apply 'format format-string args))
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
    (lyskom-debug-insert lyskom-proc
                         " Backtrace:"
                         "")
    (let ((standard-output
           (get-buffer-create lyskom-debug-communications-to-buffer-buffer)))
      (backtrace))
    (lyskom-debug-insert lyskom-proc
                         (format " Current string: ")
                         (buffer-substring lyskom-parse-pos (point-max))))

1371
  (lyskom-save-backtrace (lyskom-string-to-parse))
David Byers's avatar
David Byers committed
1372
  (lyskom-parse-recover)
1373
1374
1375
  (signal 'lyskom-protocol-error
          (format "Protocol error in %S: %s"
                  function
David Byers's avatar
David Byers committed
1376
                  (apply 'format format-string args))))
1377

David Byers's avatar
David Byers committed
1378
1379
1380
1381
1382
1383
1384
(defun lyskom-parse-recover ()
  "Initiate parser recovery."
  (lyskom-save-excursion
    (set-buffer lyskom-buffer)
    (setq lyskom-parser-recovering t)
    (initiate-get-time 'main nil)))