-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
63 lines (51 loc) · 1.51 KB
/
main.rkt
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
#lang racket
(require "telnet.rkt")
(require "logger.rkt")
(require "gui.rkt")
(define host (make-parameter "bat.org"))
(define port (make-parameter 23))
(define (read-line-avail in)
(let loop ([buf (bytes)])
(if (and (> (bytes-length buf) 0)
(not (byte-ready? in)))
buf
(begin
(let ([b (read-byte in)])
(cond
[(eof-object? b)
(if (zero? (bytes-length buf))
eof
buf)]
[(= b (char->integer #\newline))
(bytes-append buf #"\n")]
[else
(loop (bytes-append buf (bytes b)))]))))))
(define (main)
(define-values (in out) (telnet-connect (host) (port)))
(define gui (new gui%))
(send gui set-on-send (lambda (cmd)
(displayln cmd out)
(flush-output out)))
(send gui run)
(define (handle-line raw-line)
(define line (string-replace raw-line "\r\n" "\n"))
(log-info line)
(send gui send-to-window "general" line))
(define (loop)
(sync (handle-evt
in
(lambda (_)
(define line (bytes->string/utf-8 (read-line-avail in)))
(unless (eof-object? line)
(handle-line line)
(loop))))
(handle-evt
(current-input-port)
(lambda (_)
(displayln (read-line) out)
(flush-output out)
(loop)))))
(thread
(lambda () (loop))))
(module+ main
(main))