Subversion Repositories Kolibri OS

Rev

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