Debian Codegraph @

ext/termios/test.scm

raw
1 ;;
2 ;; testing termios
3 ;;
4
5 (use gauche.test)
6 (use gauche.uvector)
7
8 (test-start "termios")
9 (use gauche.termios)
10 (use scheme.list)
11 (use srfi.13)
12 (test-module 'gauche.termios)
13
14 (cond-expand
15 [(not gauche.os.windows)
16 ;;
17 ;; POSIX version
18 ;;
19 (define (list-if-bound . cans)
20 (let loop ([cans cans]
21 [syms '()]
22 [str ""])
23 (if (null? cans)
24 (map cons
25 syms
26 (let1 p (open-input-string (string-append "(list " str ")"))
27 (eval (read p) (current-module))))
28 (if (module-binds? (current-module) (car cans))
29 (loop (cdr cans)
30 (cons (car cans) syms)
31 (string-append str " " (symbol->string (car cans))))
32 (loop (cdr cans) syms str)))))
33
34 (define speeds
35 (list-if-bound 'B0 'B50 'B75 'B110 'B134 'B150 'B200 'B300 'B600 'B1200
36 'B1800 'B2400 'B4800 'B9600 'B19200 'B38400 'B57600
37 'B115200 'B230400))
38
39 (define iflags
40 (list-if-bound 'IGNBRK 'BRKINT 'IGNPAR 'PARMRK 'INPCK 'ISTRIP 'INLCR
41 'IGNCR 'ICRNL 'IXON 'IXOFF 'IXANY 'IUCLC 'IMAXBEL))
42
43 (define oflags
44 (list-if-bound 'OPOST 'OLCUC 'ONLCR 'OCRNL 'ONOCR 'ONLRET 'OFILL 'OFDEL
45 'NLDLY 'NL0 'NL1 'CRDLY 'CR0 'CR1 'CR2 'CR3 'BSDLY 'BS0
46 'BS1 'VTDLY 'VT0 'VT1 'FFDLY 'FF0 'FF1))
47
48 (define cflags
49 (list-if-bound 'CLOCAL 'CREAD 'CSIZE 'CS5 'CS6 'CS7 'CS8 'CSTOPB 'HUPCL
50 'PARENB 'PARODD 'CIBAUD 'CRTSCTS))
51
52 (define lflags
53 (list-if-bound 'ECHO 'ECHOE 'ECHOK 'ECHONL 'ICANON 'ISIG 'NOFLSH 'TOSTOP
54 'IEXTEN 'XCASE 'ECHOCTL 'ECHOPRT 'ECHOKE 'FLUSH0
55 'PENDIN))
56
57 (define ccs
58 (list-if-bound 'VEOF 'VEOL 'VERASE 'VINTR 'VKILL 'VMIN 'VQUIT 'VSTART
59 'VSTOP 'VSUSP 'VTIME 'VDISCARD 'VDSUSP 'VEOL2 'VLNEXT
60 'VREPRINT 'VSTATUS 'VWERASE 'VSWTCH 'VSWTC))
61
62 (define iterm #f)
63 (define oterm #f)
64
65 (define iport #f)
66 (define oport #f)
67
68 ;; If tests are run by a daemon, /dev/tty may not be available.
69 ;; We try /dev/tty first, then using pty for fallback.
70 (guard (e [(<system-error> e)
71 (cond-expand
72 [gauche.sys.openpty
73 (receive (master slave) (sys-openpty)
74 (set! iport (open-input-fd-port slave))
75 (set! oport (open-output-fd-port slave)))]
76 [else
77 ;; we can't continue the tests.
78 (test-end)
79 (exit 0)])])
80 (let1 term (sys-ctermid)
81 (set! iport (open-input-file term))
82 (set! oport (open-output-file term))))
83
84 (test "termios-tcgetattr" #t
85 (^[]
86 (set! iterm (sys-tcgetattr iport))
87 (set! oterm (sys-tcgetattr oport))
88 #t))
89
90 ;; NB: on cygwin (as of 1.5.25) tcdrain and tcflow does not seem to work.
91 (unless (string-contains (gauche-architecture) "-cygwin")
92 (test "termios-tcdrain" #t
93 (^[]
94 (sys-tcdrain oport)
95 #t))
96
97 (test "termios-tcflow" (make-list 4 (if #f #f))
98 (^[] (map (cut sys-tcflow oport <>)
99 (list TCOOFF TCOON TCIOFF TCION))))
100 ) ;!cygwin
101
102 (test "termios-tcflush" #t
103 (^[]
104 (sys-tcflush iport TCIFLUSH)
105 (sys-tcflush oport TCOFLUSH)
106 #t))
107
108 (test "termios-tcsetattr" (make-list 3 (undefined))
109 (^[] (map (cut sys-tcsetattr iport <> iterm)
110 (list TCSANOW TCSADRAIN TCSAFLUSH))))
111
112 ;; exclude B0 from this test, since it doesn't really set the baudrate
113 ;; (and some architecture such as Solaris does not set the value to
114 ;; termios structure).
115 (let ([slist (remove zero? (map cdr speeds))]
116 [orig-ispeed (sys-cfgetispeed iterm)]
117 [orig-ospeed (sys-cfgetospeed oterm)])
118 (test* "termios-set-n-get-speed" (map (^x (cons x x)) slist)
119 (with-error-handler
120 (^e
121 (sys-cfsetispeed iterm orig-ispeed)
122 (sys-cfsetospeed oterm orig-ospeed)
123 (raise e))
124 (^[] (map (^[speed]
125 (sys-cfsetispeed iterm speed)
126 (sys-cfsetospeed oterm speed)
127 (cons (sys-cfgetispeed iterm)
128 (sys-cfgetospeed oterm)))
129 slist)))))
130
131 (test "termios-cc" (make-list (length ccs) #t)
132 (^[]
133 (define test-char #x01)
134 (map
135 (^[ss-pair]
136 (let* ([ss (cdr ss-pair)]
137 [orig-cc (slot-ref iterm 'cc)]
138 [cc (u8vector-copy orig-cc)])
139 (dynamic-wind
140 (^[] (slot-set! iterm 'cc cc))
141 (^[]
142 (u8vector-set! cc ss test-char)
143 (slot-set! iterm 'cc cc)
144 (sys-tcsetattr iport TCSANOW iterm)
145 (let1 char (u8vector-ref
146 (slot-ref (sys-tcgetattr iport) 'cc)
147 ss)
148 (if (eqv? char (u8vector-ref cc ss))
149 #t
150 (format "~a of cc cannot change" (car ss-pair)))))
151 (^[]
152 (slot-set! iterm 'cc orig-cc)
153 (sys-tcsetattr iport TCSANOW iterm))
154 )))
155 ccs)))
156
157 ;; Check openpty basics
158 ;; sys-openpty isn't used in the tests above if we have console, so we check
159 ;; it here.
160 (cond-expand
161 [gauche.sys.openpty
162 (test* "openpty w/o term spec" '(#t #t #t)
163 (receive (m s) (sys-openpty)
164 (begin0 (list (integer? m)
165 (integer? s)
166 (sys-isatty s))
167 (sys-close m)
168 (sys-close s))))
169 ]
170 [else])
171 ]
172 [else
173 ;;
174 ;; Windows Version
175 ;;
176 ])
177
178 (test-end)