Subversion Repositories Kolibri OS

Rev

Rev 109 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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