Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;
3
;    TERMINAL
4
;
5
;    Compile with FASM for Menuet
6
;
7
 
8
use32
9
 
10
                org     0x0
11
 
12
                db      'MENUET00'              ; 8 byte id
13
                dd      38                      ; required os
14
                dd      START                   ; program start
15
                dd      I_END                   ; program image size
16
                dd      0x100000                ; required amount of memory
17
                                                ; esp = 0x7FFF0
18
                dd      0x00000000              ; reserved=no extended header
19
 
20
 
21
include 'lang.inc'
22
include 'macros.inc'
23
 
24
START:                          ; start of execution
25
 
26
    ; Clear the screen memory
27
    mov     eax, '    '
28
    mov     edi,text
29
    mov     ecx,80*30 /4
30
    cld
31
    rep     stosd
32
 
33
 
34
    call draw_window
35
 
36
 
37
still:
38
    ; check connection status
39
    mov  eax,53
40
    mov  ebx,6
41
    mov  ecx,[socket]
42
    int  0x40
43
 
44
    mov     ebx, [socket_status]
45
    mov     [socket_status], eax
46
 
47
    cmp     eax, ebx
48
    je      waitev
49
 
50
    call    draw_window
51
 
52
waitev:
53
    mov  eax,23                 ; wait here for event
54
    mov  ebx,20
55
    int  0x40
56
 
57
    cmp  eax,1                  ; redraw request ?
58
    je   red
59
    cmp  eax,2                  ; key in buffer ?
60
    je   key
61
    cmp  eax,3                  ; button in buffer ?
62
    je   button
63
 
64
    ; any data from the socket?
65
 
66
    mov     eax, 53
67
    mov     ebx, 2
68
    mov     ecx, [socket]
69
    int     0x40
70
    cmp     eax, 0
71
    jne      read_input
72
 
73
    jmp  still
74
 
75
 
76
read_input:
77
 
78
    push ecx
79
    mov     eax, 53
80
    mov     ebx, 3
81
    mov     ecx, [socket]
82
    int     0x40
83
    pop  ecx
84
 
85
    call    handle_data
86
 
87
    push    ecx
88
    mov     eax, 53
89
    mov     ebx, 2
90
    mov     ecx, [socket]
91
    int     0x40
92
    pop     ecx
93
    cmp     eax, 0
94
 
95
 
96
    jne   read_input
97
    call draw_text
98
    jmp  still
99
 
100
 
101
 
102
handle_data:
103
    ; Telnet servers will want to negotiate options about our terminal window
104
    ; just reject them all.
105
    ; Telnet options start with the byte 0xff and are 3 bytes long.
106
 
107
    mov     al, [telnetstate]
108
    cmp     al, 0
109
    je      state0
110
    cmp     al, 1
111
    je      state1
112
    cmp     al, 2
113
    je      state2
114
    jmp     hd001
115
 
116
state0:
117
    cmp     bl, 255
118
    jne     hd001
119
    mov     al, 1
120
    mov     [telnetstate], al
121
    ret
122
 
123
state1:
124
    mov     al, 2
125
    mov     [telnetstate], al
126
    ret
127
 
128
state2:
129
    mov     al, 0
130
    mov     [telnetstate], al
131
    mov     [telnetrep+2], bl
132
 
133
    mov     edx, 3
134
    mov     eax,53
135
    mov     ebx,7
136
    mov     ecx,[socket]
137
    mov     esi, telnetrep
138
    int     0x40
139
    ret
140
 
141
hd001:
142
    cmp  bl,13                          ; BEGINNING OF LINE
143
    jne  nobol
144
    mov  ecx,[pos]
145
    add  ecx,1
146
  boll1:
147
    sub  ecx,1
148
    mov  eax,ecx
149
    xor  edx,edx
150
    mov  ebx,80
151
    div  ebx
152
    cmp  edx,0
153
    jne  boll1
154
    mov  [pos],ecx
155
    jmp  newdata
156
  nobol:
157
 
158
    cmp  bl,10                            ; LINE DOWN
159
    jne  nolf
160
   addx1:
161
    add  [pos],dword 1
162
    mov  eax,[pos]
163
    xor  edx,edx
164
    mov  ecx,80
165
    div  ecx
166
    cmp  edx,0
167
    jnz  addx1
168
    mov  eax,[pos]
169
    jmp  cm1
170
  nolf:
171
 
172
    cmp  bl,8                            ; BACKSPACE
173
    jne  nobasp
174
    mov  eax,[pos]
175
    dec  eax
176
    mov  [pos],eax
177
    mov  [eax+text],byte 32
178
    mov  [eax+text+60*80],byte 0
179
    jmp  newdata
180
   nobasp:
181
 
182
    cmp  bl,15                           ; CHARACTER
183
    jbe  newdata
184
    mov  eax,[pos]
185
    mov  [eax+text],bl
186
    mov  eax,[pos]
187
    add  eax,1
188
  cm1:
189
    mov  ebx,[scroll+4]
190
    imul ebx,80
191
    cmp  eax,ebx
192
    jb   noeaxz
193
    mov  esi,text+80
194
    mov  edi,text
195
    mov  ecx,ebx
196
    cld
197
    rep  movsb
198
    mov  eax,ebx
199
    sub  eax,80
200
  noeaxz:
201
    mov  [pos],eax
202
  newdata:
203
    ret
204
 
205
 
206
  red:                          ; REDRAW WINDOW
207
    call draw_window
208
    jmp  still
209
 
210
  key:                          ; KEY
211
    mov  eax,2                  ; send to modem
212
    int  0x40
213
 
214
    mov     ebx, [socket_status]
215
    cmp     ebx, 4              ; connection open?
216
    jne     still               ; no, so ignore key
217
 
218
    shr  eax,8
219
    cmp  eax,178                ; ARROW KEYS
220
    jne  noaup
221
    mov  al,'A'
222
    call arrow
223
    jmp  still
224
  noaup:
225
    cmp  eax,177
226
    jne  noadown
227
    mov  al,'B'
228
    call arrow
229
    jmp  still
230
  noadown:
231
    cmp  eax,179
232
    jne  noaright
233
    mov  al,'C'
234
    call arrow
235
    jmp  still
236
  noaright:
237
    cmp  eax,176
238
    jne  noaleft
239
    mov  al,'D'
240
    call arrow
241
    jmp  still
242
  noaleft:
243
  modem_out:
244
 
245
    call    to_modem
246
 
247
    jmp  still
248
 
249
  button:                       ; BUTTON
250
    mov  eax,17
251
    int  0x40
252
    cmp  ah,1                   ; CLOSE PROGRAM
253
    jne  noclose
254
 
255
    mov  eax,53
256
    mov  ebx,8
257
    mov  ecx,[socket]
258
    int  0x40
259
 
260
     mov  eax,-1
261
     int  0x40
262
  noclose:
263
    cmp     ah, 2               ; Set IP
264
    jne     notip
265
 
266
    mov  [string_x], dword 78
267
    mov  [string_y], dword 276
268
    mov  [string_length], dword 15
269
    call read_string
270
    mov   esi,string-1
271
    mov   edi,ip_address
272
    xor   eax,eax
273
   ip1:
274
    inc   esi
275
    cmp   [esi],byte '0'
276
    jb    ip2
277
    cmp   [esi],byte '9'
278
    jg    ip2
279
    imul  eax,10
280
    movzx ebx,byte [esi]
281
    sub   ebx,48
282
    add   eax,ebx
283
    jmp   ip1
284
   ip2:
285
    mov   [edi],al
286
    xor   eax,eax
287
    inc   edi
288
    cmp   edi,ip_address+3
289
    jbe   ip1
290
    call draw_window
291
 
292
 
293
    jmp     still
294
 
295
notip:
296
    cmp     ah, 3               ; set port
297
    jne     notport
298
 
299
    mov  [string_x], dword 215
300
    mov  [string_y], dword 276
301
    mov  [string_length], dword 4
302
    call read_string
303
    mov   esi,string-1
304
    mov   edi,port
305
    xor   eax,eax
306
   ip11:
307
    inc   esi
308
    cmp   [esi],byte '0'
309
    jb    ip21
310
    cmp   [esi],byte '9'
311
    jg    ip21
312
    imul  eax,10
313
    movzx ebx,byte [esi]
314
    sub   ebx,48
315
    add   eax,ebx
316
    jmp   ip11
317
   ip21:
318
    mov   [edi],al
319
    inc   edi
320
    mov   [edi],ah
321
    call draw_window
322
 
323
 
324
    jmp     still
325
 
326
notport:
327
    cmp     ah, 4               ; connect
328
    jne     notcon
329
 
330
    mov     eax, [socket_status]
331
    cmp     eax, 4
332
    je     still
333
    call    connect
334
 
335
    jmp     still
336
 
337
notcon:
338
    cmp     ah,5                ; disconnect
339
    jne     notdiscon
340
 
341
    call    disconnect
342
    jmp  still
343
 
344
notdiscon:                      ; Echo Toggle
345
    cmp     ah, 6
346
    jne     still
347
 
348
    mov     al, [echo]
349
    not     al
350
    mov     [echo], al
351
 
352
    call    draw_window
353
    jmp     still
354
 
355
arrow:
356
 
357
    push eax
358
    mov  al,27
359
    call to_modem
360
    mov  al,'['
361
    call to_modem
362
    pop  eax
363
    call to_modem
364
 
365
    ret
366
 
367
 
368
to_modem:
369
    pusha
370
    push    ax
371
    mov     [tx_buff], al
372
    mov     edx, 1
373
    cmp     al, 13
374
    jne     tm_000
375
    mov     edx, 2
376
tm_000:
377
    mov     eax,53
378
    mov     ebx,7
379
    mov     ecx,[socket]
380
    mov     esi, tx_buff
381
    int  0x40
382
    pop     bx
383
    mov     al, [echo]
384
    cmp     al, 0
385
    je      tm_001
386
 
387
    push    bx
388
    call    handle_data
389
    pop     bx
390
 
391
    cmp     bl, 13
392
    jne     tm_002
393
 
394
    mov     bl, 10
395
    call    handle_data
396
 
397
tm_002:
398
    call    draw_text
399
 
400
tm_001:
401
    popa
402
    ret
403
 
404
 
405
 
406
disconnect:
407
    mov  eax,53
408
    mov  ebx,8
409
    mov  ecx,[socket]
410
    int  0x40
411
    ret
412
 
413
 
414
 
415
connect:
416
    pusha
417
 
418
 mov     ecx, 1000  ; local port starting at 1000
419
 
420
getlp:
421
 inc     ecx
422
 push ecx
423
 mov     eax, 53
424
 mov     ebx, 9
425
 int     0x40
426
 pop     ecx
427
 cmp     eax, 0   ; is this local port in use?
428
 jz  getlp      ; yes - so try next
429
 
430
    mov     eax,53
431
    mov     ebx,5
432
    mov     dl, [ip_address + 3]
433
    shl     edx, 8
434
    mov     dl, [ip_address + 2]
435
    shl     edx, 8
436
    mov     dl, [ip_address + 1]
437
    shl     edx, 8
438
    mov     dl, [ip_address]
439
    mov     esi, edx
440
    movzx   edx, word [port]      ; telnet port id
441
    mov     edi,1      ; active open
442
    int     0x40
443
    mov     [socket], eax
444
 
445
    popa
446
 
447
    ret
448
 
449
 
450
 
451
;   *********************************************
452
;   *******  WINDOW DEFINITIONS AND DRAW ********
453
;   *********************************************
454
 
455
 
456
draw_window:
457
 
458
    pusha
459
 
460
    mov  eax,12
461
    mov  ebx,1
462
    int  0x40
463
 
464
    mov  eax,0                     ; DRAW WINDOW
465
    mov  ebx,100*65536+491 + 8 +15
466
    mov  ecx,100*65536+270 + 20     ; 20 for status bar
467
    mov  edx,[wcolor]
468
    add  edx,0x02000000
469
    mov  esi,0x80557799
470
    mov  edi,0x00557799
471
    int  0x40
472
 
473
    mov  eax,4                     ; WINDOW LABEL
474
    mov  ebx,8*65536+8
475
    mov  ecx,0x00ffffff
476
    mov  edx,labelt
477
    mov  esi,labellen-labelt
478
    int  0x40
479
 
480
 
481
    mov  eax,8                     ; CLOSE BUTTON
482
     mov  ebx,(491 + 20 -19)*65536+12
483
 
484
    mov  ecx,5*65536+12
485
    mov  edx,1
486
    mov  esi,0x557799
487
    int  0x40
488
 
489
    ; draw status bar
490
    mov     eax, 13
491
    mov     ebx, 4*65536+484 + 8 +15
492
    mov     ecx, 270*65536 + 3
493
    mov     edx, 0x00557799
494
    int     0x40
495
 
496
    mov  eax,8                     ; BUTTON 2: SET IP
497
    mov  ebx,4*65536+70
498
    mov  ecx,273*65536+12
499
    mov     esi, 0x00557799
500
    mov  edx,2
501
    int  0x40
502
 
503
    mov  eax,4                     ; Button text
504
    mov  ebx,6*65536+276
505
    mov  ecx,0x00ffffff
506
    mov  edx,setipt
507
    mov  esi,setiplen-setipt
508
    int  0x40
509
 
510
 
511
    mov  edi,ip_address             ; display IP address
512
    mov  edx,78*65536+276
513
    mov  esi,0x00ffffff
514
    mov  ebx,3*65536
515
  ipdisplay:
516
    mov  eax,47
517
    movzx ecx,byte [edi]
518
    int  0x40
519
    add  edx,6*4*65536
520
    inc  edi
521
    cmp  edi,ip_address+4
522
    jb   ipdisplay
523
 
524
    mov  eax,8                     ; BUTTON 3: SET PORT
525
    mov  ebx,173*65536+38
526
    mov  ecx,273*65536+12
527
    mov  edx,3
528
    mov     esi, 0x00557799
529
    int  0x40
530
 
531
    mov  eax,4                     ; Button text
532
    mov  ebx,178*65536+276
533
    mov  ecx,0x00ffffff
534
    mov  edx,setportt
535
    mov  esi,setportlen-setportt
536
    int  0x40
537
 
538
 
539
    mov  edx,216*65536+276           ; display port
540
    mov  esi,0x00ffffff
541
    mov  ebx,4*65536
542
    mov  eax,47
543
    movzx  ecx,word [port]
544
    int  0x40
545
 
546
    mov  eax,8                     ; BUTTON 4: Connect
547
    mov  ebx,250*65536+50
548
    mov  ecx,273*65536+12
549
    mov     esi, 0x00557799
550
    mov  edx,4
551
    int     0x40
552
 
553
    mov  eax,4                     ; Button text
554
    mov  ebx,255*65536+276
555
    mov  ecx,0x00ffffff
556
    mov  edx,cont
557
    mov  esi,conlen-cont
558
    int  0x40
559
 
560
 
561
    mov  eax,8                     ; BUTTON 5: disconnect
562
    mov  ebx,303*65536+70
563
    mov  ecx,273*65536+12
564
    mov  edx,5
565
    mov     esi, 0x00557799
566
    int     0x40
567
 
568
 
569
    mov  eax,4                     ; Button text
570
    mov  ebx,307*65536+276
571
    mov  ecx,0x00ffffff
572
    mov  edx,dist
573
    mov  esi,dislen-dist
574
    int  0x40
575
 
576
 
577
    mov  esi,contlen-contt          ; display connected status
578
    mov     edx, contt
579
    mov     eax, [socket_status]
580
    cmp     eax, 4                  ; 4 is connected
581
    je      pcon
582
    mov     esi,discontlen-discontt
583
    mov     edx, discontt
584
pcon:
585
 
586
    mov  eax,4                     ; status text
587
    mov  ebx,380*65536+276
588
    mov  ecx,0x00ffffff
589
    int  0x40
590
 
591
 
592
    mov  eax,8                     ; BUTTON 6: echo
593
    mov  ebx,460*65536+50
594
    mov  ecx,273*65536+12
595
    mov  edx,6
596
    mov     esi, 0x00557799
597
    int     0x40
598
 
599
    mov  edx,echot
600
    mov  esi,echolen-echot
601
    mov     al, [echo]
602
    cmp     al, 0
603
    jne     peo
604
    mov  edx,echoot
605
    mov  esi,echoolen-echoot
606
 
607
peo:
608
    mov  eax,4                     ; Button text
609
    mov  ebx,463*65536+276
610
    mov  ecx,0x00ffffff
611
    int  0x40
612
 
613
 
614
    xor  eax,eax
615
    mov  edi,text+80*30
616
    mov  ecx,80*30 /4
617
    cld
618
    rep  stosd
619
 
620
    call draw_text
621
 
622
    mov  eax,12
623
    mov  ebx,2
624
    int  0x40
625
 
626
    popa
627
 
628
    ret
629
 
630
 
631
draw_text:
632
 
633
    pusha
634
 
635
    mov  esi,text
636
    mov  eax,0
637
    mov  ebx,0
638
  newletter:
639
    mov  cl,[esi]
640
    cmp  cl,[esi+30*80]
641
    jne  yesletter
642
    jmp  noletter
643
  yesletter:
644
    mov  [esi+30*80],cl
645
 
646
    ; erase character
647
 
648
    pusha
649
    mov     edx, 0                  ; bg colour
650
    mov     ecx, ebx
651
    add     ecx, 26
652
    shl     ecx, 16
653
    mov     cx, 9
654
    mov     ebx, eax
655
    add     ebx, 6
656
    shl     ebx, 16
657
    mov     bx, 6
658
    mov     eax, 13
659
    int     0x40
660
    popa
661
 
662
    ; draw character
663
 
664
    pusha
665
    mov     ecx, 0x00ffffff
666
    push bx
667
    mov  ebx,eax
668
    add  ebx,6
669
    shl  ebx,16
670
    pop  bx
671
    add  bx,26
672
    mov  eax,4
673
    mov  edx,esi
674
    mov  esi,1
675
    int  0x40
676
    popa
677
 
678
  noletter:
679
 
680
    add  esi,1
681
    add  eax,6
682
    cmp  eax,80*6
683
    jb   newletter
684
    mov  eax,0
685
    add  ebx,10
686
    cmp  ebx,24*10
687
    jb   newletter
688
 
689
    popa
690
    ret
691
 
692
 
693
read_string:
694
 
695
    mov  edi,string
696
    mov  eax,'_'
697
    mov  ecx,[string_length]
698
    inc     ecx
699
    cld
700
    rep  stosb
701
    call print_text
702
 
703
    mov  edi,string
704
  f11:
705
    mov  eax,10
706
    int  0x40
707
    cmp  eax,2
708
    jne  read_done
709
    mov  eax,2
710
    int  0x40
711
    shr  eax,8
712
    cmp  eax,13
713
    je   read_done
714
    cmp  eax,8
715
    jnz  nobsl
716
    cmp  edi,string
717
    jz   f11
718
    sub  edi,1
719
    mov  [edi],byte '_'
720
    call print_text
721
    jmp  f11
722
  nobsl:
723
    cmp  eax,dword 31
724
    jbe  f11
725
    cmp  eax,dword 95
726
    jb   keyok
727
    sub  eax,32
728
  keyok:
729
    mov  [edi],al
730
    call print_text
731
 
732
    inc  edi
733
    mov  esi,string
734
    add  esi,[string_length]
735
    cmp  esi,edi
736
    jnz  f11
737
 
738
  read_done:
739
 
740
    call print_text
741
 
742
    ret
743
 
744
 
745
print_text:
746
 
747
    pusha
748
 
749
    mov  eax,13
750
    mov  ebx,[string_x]
751
    shl  ebx,16
752
    add  ebx,[string_length]
753
    imul bx,6
754
    mov  ecx,[string_y]
755
    shl  ecx,16
756
    mov  cx,8
757
    mov  edx,0x00000000
758
    int  0x40
759
 
760
    mov  eax,4
761
    mov  ebx,[string_x]
762
    shl  ebx,16
763
    add  ebx,[string_y]
764
    mov  ecx,0x00ffffff
765
    mov  edx,string
766
    mov  esi,[string_length]
767
    int  0x40
768
 
769
    popa
770
    ret
771
 
772
 
773
 
774
 
775
; DATA AREA
776
 
777
telnetrep       db 0xff,0xfc,0x00
778
telnetstate     db 0
779
 
780
string_length  dd    16
781
string_x       dd    200
782
string_y       dd    60
783
 
784
string         db    '________________'
785
 
786
tx_buff         db  0, 10
787
ip_address      db  001,002,003,004
788
port            db  0,0
789
echo            db  0
790
socket          dd  0x0
791
socket_status   dd  0x0
792
pos             dd  80 * 1
793
scroll          dd  1
794
                dd  24
795
wcolor          dd  0x000000
796
labelt          db  'Telnet v0.1'
797
labellen:
798
setipt          db  'IP Address:    .   .   .'
799
setiplen:
800
setportt        db  'Port:'
801
setportlen:
802
cont            db  'Connect'
803
conlen:
804
dist            db  'Disconnect'
805
dislen:
806
contt           db  'Connected'
807
contlen:
808
discontt        db  'Disconnected'
809
discontlen:
810
echot        db  'Echo On'
811
echolen:
812
echoot        db  'Echo Off'
813
echoolen:
814
 
815
 
816
 
817
text:
818
I_END: