-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsul254.f90
356 lines (294 loc) · 10.7 KB
/
consul254.f90
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
!-------------------------------------------------------------------------------
! Эмулятор электрической пишущей машинки/терминала Consul-254.
!
! (c) 2023 Станислав Масловский <[email protected]>
!
! Исходный текст на ЯП GNU Fortran 2008.
! Для реализации многопоточности использована библиотека OpenMP.
!-------------------------------------------------------------------------------
module Consul254
implicit none
type :: CharSeq
character(:), allocatable :: seq
end type
interface CharSeq
module procedure charseq_cons
end interface
interface assignment (=)
module procedure c2cs
end interface
character, parameter :: TAB = achar(9), LF = achar(10), CR = achar(13), SO = achar(14), SI = achar(15), ESC = achar(27)
character(*), parameter :: data_rd_fmt = "(3i1,1a1,4i1)"
character(*), parameter :: data_wr_fmt = "(3i1,':',5i1)"
type(CharSeq) :: symbol(0:127) ! таблица печатных символов
integer :: code(0:255) ! таблица перекодировки
logical :: inv_input = .false. ! флаг инвертирования входных данных (печать)
logical :: inv_output = .false. ! флаг инвертирования выходных данных (клавиатура)
logical :: parity_bit = .false. ! флаг вывода бита четности
logical :: interactive = .true. ! флаг интерактивной сессии
contains
subroutine Consul_254_printer(iunit, stdout)
integer, intent(in) :: iunit, stdout
integer :: stat, ba(0:6)
character :: dummy
character(128) :: errmsg
stat = 0
l1: do while (stat == 0)
read (iunit, data_rd_fmt, iostat=stat, iomsg=errmsg) ba(0:2), dummy, ba(3:6)
if (stat /= 0) exit l1
if (inv_input) ba = inv(ba)
write (stdout, iostat=stat, iomsg=errmsg) symbol(ba2i(ba))%seq
flush (stdout)
enddo l1
write (0,*) "Printer: " // trim(errmsg)
end subroutine
subroutine Consul_254_keyboard(stdin, ounit)
integer, intent(in) :: stdin, ounit
integer :: i, c_cnt, stat, parser_state, ba(0:6)
integer, parameter :: FIRST_CHAR=0, ESCAPE=1, LESS_EQ=2, NOT_EQ=3, GREAT_EQ=4, POW_10=5
type(CharSeq) :: out_seq
character(128) :: errmsg
character(4) :: c
! Парсер многобайтных символов в кодировке UTF-8 и ESC-последовательностей
stat = 0
parser_state = FIRST_CHAR
l1: do while (stat == 0)
read (stdin, iostat=stat, iomsg=errmsg) c(1:1)
if (stat /= 0) exit l1
i = ichar(c(1:1))
select case (parser_state)
case (FIRST_CHAR)
c_cnt = 1
if (i < 128) then ! однобайтовый символ UTF-8
if (c(1:1) == ESC) then
parser_state = ESCAPE
cycle l1
elseif (code(i) == 128) then
cycle l1
endif
elseif (i < 224) then ! двухбайтовый
c_cnt = 2
elseif (i < 240) then ! трехбайтовый
c_cnt = 3
else ! четырехбайтовый
c_cnt = 4
endif
if (c_cnt > 1) read (stdin, iostat=stat, iomsg=errmsg) c(2:c_cnt)
if (stat /= 0) exit l1
out_seq = c(1:c_cnt)
case (ESCAPE)
select case(c(1:1))
case ("*")
out_seq = "×"
case (" ")
out_seq = "␣"
case ("=")
out_seq = "≡"
case ("7")
out_seq = "↴"
case ("}")
out_seq = "⊃"
case ("v")
out_seq = "∨"
case (":")
out_seq = "÷"
case ("^")
out_seq = "⋀"
case ("#")
out_seq = "◊"
case ("<")
parser_state = LESS_EQ; cycle l1
case ("/")
parser_state = NOT_EQ; cycle l1
case (">")
parser_state = GREAT_EQ; cycle l1
case ("1")
parser_state = POW_10; cycle l1
case default
parser_state = FIRST_CHAR; cycle l1
end select
case (LESS_EQ, NOT_EQ, GREAT_EQ)
if (c(1:1) == "=") then
select case (parser_state)
case (LESS_EQ)
out_seq = "≤"
case (NOT_EQ)
out_seq = "≠"
case (GREAT_EQ)
out_seq = "≥"
end select
elseif (c(1:1) == ESC) then
parser_state = ESCAPE; cycle l1
else
parser_state = FIRST_CHAR; cycle l1
endif
case (POW_10)
if (c(1:1) == "0") then
out_seq = "⏨"
elseif (c(1:1) == ESC) then
parser_state = ESCAPE; cycle l1
else
parser_state = FIRST_CHAR; cycle l1
endif
end select
ba = i2ba(code(hash(out_seq%seq)))
if (inv_output) ba = inv(ba)
if (parity_bit) then
write (ounit, data_wr_fmt, iostat=stat, iomsg=errmsg) ba, modulo(sum(ba)+1, 2)
else
write (ounit, data_wr_fmt, iostat=stat, iomsg=errmsg) ba
endif
flush (ounit)
parser_state = FIRST_CHAR
enddo l1
write (0,*) "Keyboard: " // trim(errmsg)
end subroutine
subroutine init_tables
! Равные по начертанию русские и латинские буквы
character(2), parameter :: rus(*) = [ "А", "В", "Е", "К", "М", "Н", "О", "Р", "С", "Т", "У", "Х" ] ! русские буквы
character(1), parameter :: lat(*) = [ "A", "B", "E", "K", "M", "H", "O", "P", "C", "T", "Y", "X" ] ! латинские буквы
integer :: i, k
symbol(:) = CharSeq()
! Таблица печатных символов в кодировке UTF-8
symbol(0:12) = [ ("0123456789+-/"(i:i), i=1,13) ]
symbol(16:17) = [ CR, LF ]
symbol(18) = ESC//"[1;31m"
symbol(19) = ESC//"[0m"
symbol(20:23) = [ SO, TAB, SI, " " ]
symbol(24) = ESC//"[9m"
symbol(25) = "*"
symbol(32:63) = [ ("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЬЭЮЯ"(i:i+1), i=1,63,2) ] ! двухбайтные символы
symbol(65:67) = [ "]", ",", "." ]
symbol(68) = "⏨"
symbol(69:71) = [ "^", "(", ")" ]
symbol(72) = "×"
symbol(73:74) = [ ";", "[" ]
symbol(75) = "␣"
symbol(76:77) = [ "=", ">" ]
symbol(88) = ESC//"[4m"
symbol(89) = "|"
symbol(96:105) = [ ("_%WGD:VZIJ"(i:i), i=1,10) ]
symbol(106) = "≤"
symbol(107) = "L"
symbol(108) = "≡"
symbol(109) = "N"
symbol(110) = "↴"
symbol(111) = "⊃"
symbol(112:113) = [ "R", "S" ]
symbol(114) = "∨"
symbol(115:117) = [ "U", "F", "<" ]
symbol(118) = "≠"
symbol(119) = "÷"
symbol(120:121) = [ "‘", "’" ]
symbol(122) = "!"
symbol(123:125) = [ "⋀", "≥", "◊" ]
symbol(126) = "Q"
! Xeш-таблица для перекодировки в символьные коды Сетунь-70
code(:) = 128
! блок печатных символов
do i = 0, 127
k = hash(symbol(i)%seq)
code(k) = i
enddo
! блок равнозначной латиницы
do i = 1, size(lat)
code(ichar(lat(i))) = code(hash(rus(i)))
enddo
code(97:122) = code(65:90)
! Кавычки
code(ichar("`")) = code(hash("‘"))
code(ichar("'")) = code(hash("’"))
end subroutine
function hash(s) result(h)
character(*) s
integer :: h, l, i, k
h = 0
l = len(s)
if (l > 0) then
if (s(1:1) /= ESC) then
do i = 1, l
k = ichar(s(i:i))
if (i == 2) then
h = ibclr(h, 0)
if (h == 80) then
k = modulo(k - 16, 32)
else
k = ishftc(k, 3, 6)
endif
endif
h = h + ishft(h, -3)
h = modulo(h + k, 128)
enddo
endif
endif
if (l /= 1) h = h + 128
end function
function inv(ba) result (res)
integer, intent(in) :: ba(0:6)
integer :: res(0:6)
res = 0
where (ba == 0) res = 1
end function
function ba2i(ba) result (n)
integer, intent(in) :: ba(0:6)
integer :: i, n
n = 0
do i = 0, 6
if (ba(i) /= 0) n = ibset(n,i)
enddo
end function
function i2ba(n) result (ba)
integer, intent(in) :: n
integer :: i, ba(0:6)
do i = 0, 6
ba(i) = ibits(n,i,1)
enddo
end function
elemental function charseq_cons(c) result (cs)
character(*), intent(in), optional :: c
type(CharSeq) :: cs
if (present(c)) then
cs%seq = c
else
cs%seq = ""
endif
end function
elemental subroutine c2cs(cs, c)
type(CharSeq), intent(out) :: cs
character(*), intent(in) :: c
cs = CharSeq(c)
end subroutine
end module
program Consul_254
use Consul254
implicit none
integer, parameter :: max_arg_len = 128
character(max_arg_len) :: input_fifo, output_fifo, conf_flags
integer :: stat, iunit, ounit, stdin, stdout, argc
! Разбор командной строки
argc = command_argument_count()
if ( argc < 2) stop "Usage: consul254 <input_fifo> <output_fifo> [<conf_flags>]"
call get_command_argument(1, input_fifo)
call get_command_argument(2, output_fifo)
if (argc >= 3) then
call get_command_argument(3, conf_flags)
read (conf_flags, *, iostat=stat) inv_input, inv_output, interactive
if (stat /= 0) write (0,*) "Warning: unrecognizable/incomplete conf_flags argument"
endif
if (interactive) write (*,"(a,/)") "Consul-254 TTY EMU. Copyright (c) 2023 Stanislav Maslovski"
! Бесформатный потоковый режим ввода/вывода на stdin/stdout (поддерживается на линукс и андроид)
open (newunit=stdin, file="/dev/stdin", status="old", access="stream", action="read", iostat=stat)
if (stat /= 0) stop "Error opening /dev/stdin"
open (newunit=stdout, file="/dev/stdout", status="old", access="stream", action="write", iostat=stat)
if (stat /= 0) stop "Error opening /dev/stdout"
call init_tables
!$omp parallel sections num_threads(2)
open (newunit=ounit, file=output_fifo, status="old", action="write", iostat=stat)
if (stat /= 0) stop "Error opening output fifo"
call Consul_254_keyboard(stdin, ounit)
!$omp section
open (newunit=iunit, file=input_fifo, status="old", action="read", iostat=stat)
if (stat /= 0) stop "Error opening input fifo"
call Consul_254_printer(iunit, stdout)
!$omp end parallel sections
end program