File: terminal.fs

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (255 lines) | stat: -rw-r--r-- 4,667 bytes parent folder | download | duplicates (4)
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
\ tag: terminal emulation
\ 
\ this code implements IEEE 1275-1994 ANNEX B
\ 
\ Copyright (C) 2003 Stefan Reinauer
\ 
\ See the file "COPYING" for further information about
\ the copyright and warranty status of this work.
\ 

0 value (escseq)
10 buffer: (sequence)

: (match-number) ( x y [1|2] [1|2] -- x [z] )
  2dup = if \ 1 1 | 2 2
    drop exit
  then
  2dup > if
    2drop drop 1 exit
  then
  2drop 0
  ;

: (esc-number) ( maxchar -- ?? ?? num )
  >r depth >r  ( R: depth maxchar )
  0 (sequence) 2+ (escseq) 2- ( 0 seq+2 seqlen-2 )
  \ if numerical, scan until non-numerical
  0 ?do
    ( 0 seq+2 )
    dup i + c@ a
    digit if
      ( 0 ptr  n )
      rot a * + ( ptr val )
      swap 
    else
      ( 0 ptr asc )
      ascii ; = if
        0 swap
      else
        drop leave
      then
    then
    
  loop
  depth r> - r>
  0 to (escseq)
  (match-number) 
  ;
  
: (match-seq)
  (escseq) 1- (sequence) + c@  \ get last character in sequence
  \ dup draw-character
  case
    ascii A of \ CUU - cursor up
      1 (esc-number) 
      0> if 
        1 max
      else 
        1
      then
      negate line# + 
      0 max to line#
    endof
    ascii B of \ CUD - cursor down
      1 (esc-number) 
      0> if 
        1 max
	line# + 
        #lines 1- min to line#
      then
    endof
    ascii C of \ CUF - cursor forward
      1 (esc-number) 
      0> if 
        1 max
	column# + 
        #columns 1- min to column#
      then
    endof
    ascii D of \ CUB - cursor backward
      1 (esc-number) 
      0> if 
        1 max
	negate column# + 
	0 max to column#
      then
    endof
    ascii E of \ Cursor next line (CNL) 
      \ FIXME - check agains ANSI3.64
      1 (esc-number) 
      0> if 
        1 max
	line# + 
        #lines 1- min to line#
      then
      0 to column#
    endof
    ascii f of
      2 (esc-number) 
      2 = if
        #columns 1- min to column#
        #lines 1- min to line#
      then
    endof
    ascii H of
      2 (esc-number) 
      2 = if
        #columns 1- min to column#
        #lines 1- min to line#
      then
    endof
    ascii J of
      0 to (escseq)
      #columns column# - delete-characters
      #lines line# - delete-lines
    endof
    ascii K of
      0 to (escseq)
      #columns column# - delete-characters
    endof
    ascii L of
      1 (esc-number) 
      0> if
        1 max
        insert-lines
      then
    endof
    ascii M of
      1 (esc-number) 
      1 = if
        1 max
        delete-lines
      then
    endof
    ascii @ of
      1 (esc-number) 
      1 = if
        1 max
        insert-characters 
      then
    endof
    ascii P of
      1 (esc-number) 
      1 = if
	1 max
        delete-characters
      then
    endof
    ascii m of
      1 (esc-number)
      1 = if
        7 = if 
          true to inverse?
        else
          false to inverse?
        then
      then
    endof
    ascii p of \ normal text colors
      0 to (escseq)
      inverse-screen? if
        false to inverse-screen?
	inverse? 0= to inverse?
	invert-screen
      then
    endof
    ascii q of \ inverse text colors
      0 to (escseq)
      inverse-screen? not if
        true to inverse-screen?
	inverse? 0= to inverse?
	invert-screen
      then
    endof
    ascii s of
      \ Resets the display device associated with the terminal emulator.
      0 to (escseq)
      reset-screen
    endof
  endcase
  ;

: (term-emit) ( char -- )
  toggle-cursor
  
  (escseq) 0> if
    (escseq) 10 = if
      0 to (escseq)
      ." overflow in esc" cr
      drop
    then
    (escseq) 1 = if 
      dup ascii [ = if    \ not a [
        (sequence) 1+ c!
	2 to (escseq)
      else
        0 to (escseq)      \ break out of ESC sequence
	." out of ESC" cr
	drop               \ don't print breakout character
      then
      toggle-cursor exit
    else
      (sequence) (escseq) + c! 
      (escseq) 1+ to (escseq)
      (match-seq)
      toggle-cursor exit
    then  
  then
  
  case
  7 of \ BEL
    blink-screen
    s" /screen" s" ring-bell" 
    execute-device-method
  endof
  8 of \ BS
    column# 0<> if
      column# 1- dup 
      to column#
      20 draw-character
      to column#
    then
  endof
  9 of \ TAB
    column# dup #columns = if 
      drop
    else
      8 + -8 and ff and to column#
    then
  endof
  a of \ LF
    line# 1+ to line# 0 to column# 
  endof
  b of \ VT
    line# 0<> if
      line# 1- to line#
    then
  endof
  c of \ FF
    0 to column# 0 to line#
    erase-screen
  endof
  d of \ CR
    0 to column#
  endof
  1b of \ ESC
    1b (sequence) c!
    1 to (escseq)
  endof
  dup draw-character
  endcase
  toggle-cursor
  ;

['] (term-emit) to fb-emit