File:  [gforth] / gforth / utf-8.fs
Revision 1.6: download - view: text, annotated - select for diffs
Tue Dec 28 17:27:24 2004 UTC (19 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed UTF-8 buffer overflow

    1: \ UTF-8 handling                                       12dec04py
    2: 
    3: \ Copyright (C) 2004 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: \ short: u8 means utf-8 encoded address
   22: 
   23: : u8len ( u8 -- n )
   24:     dup      $80 u< IF  drop 1  EXIT  THEN \ special case ASCII
   25:     $800  2 >r
   26:     BEGIN  2dup u>=  WHILE  5 lshift r> 1+ >r  REPEAT
   27:     2drop r> ;
   28: 
   29: : u8@+ ( u8addr -- u8addr' u )
   30:     count  dup $80 and 0= ?EXIT  \ special case ASCII
   31:     $7F and  $40 >r
   32:     BEGIN  dup r@ and  WHILE  r@ xor
   33: 	    6 lshift r> 5 lshift >r >r count
   34: \	    dup $C0 and $80 <> abort" malformed character"
   35: 	    $3F and r> or
   36:     REPEAT  rdrop ;
   37: 
   38: : u8!+ ( u u8addr -- u8addr' )
   39:     over $80 < IF  tuck c! 1+  EXIT  THEN \ special case ASCII
   40:     >r 0 swap  $3F
   41:     BEGIN  2dup u>  WHILE
   42: 	    2/ >r  dup $3F and $80 or swap 6 rshift r>
   43:     REPEAT  $7F xor 2* or  r>
   44:     BEGIN   over $80 u>= WHILE  tuck c! 1+  REPEAT  nip ;
   45: 
   46: \ scan to next/previous character
   47: 
   48: : u8>> ( u8addr -- u8addr' )
   49:     BEGIN  count $C0 and $80 <>  UNTIL ;
   50: : u8<< ( u8addr -- u8addr' )
   51:     BEGIN  1- dup c@ $C0 and $80 <>  UNTIL ;
   52: 
   53: \ utf key and emit
   54: 
   55: : u8key ( -- u )
   56:     defers key dup $80 and 0= ?EXIT  \ special case ASCII
   57:     $7F and  $40 >r
   58:     BEGIN  dup r@ and  WHILE  r@ xor
   59: 	    6 lshift r> 5 lshift >r >r defers key
   60: \	    dup $C0 and $80 <> abort" malformed character"
   61: 	    $3F and r> or
   62:     REPEAT  rdrop ;
   63: 
   64: : u8emit ( u -- )
   65:     dup $80 < IF  defers emit  EXIT  THEN \ special case ASCII
   66:     0 swap  $3F
   67:     BEGIN  2dup u>  WHILE
   68: 	    2/ >r  dup $3F and $80 or swap 6 rshift r>
   69:     REPEAT  $7F xor 2* or
   70:     BEGIN   dup $80 u>= WHILE  defers emit  REPEAT  drop ;
   71: 
   72: \ input editor
   73: 
   74: : save-cursor ( -- )  27 emit '7 emit ;
   75: : restore-cursor ( -- )  27 emit '8 emit ;
   76: : .rest ( addr pos1 -- addr pos1 )
   77:     restore-cursor 2dup type ;
   78: : .all ( span addr pos1 -- span addr pos1 )
   79:     restore-cursor >r 2dup swap type r> ;
   80: 
   81: : <u8ins>  ( max span addr pos1 u8char -- max span addr pos2 )
   82:     >r  2over r@ u8len + u< IF  rdrop bell  EXIT  THEN
   83:     >string over r@ u8len + swap move 2dup chars + r@ swap u8!+ drop
   84:     r> u8len >r  rot r@ chars + -rot r> chars + ;
   85: : (u8ins)  ( max span addr pos1 u8char -- max span addr pos2 )
   86:     <u8ins> .all .rest ;
   87: : u8back  ( max span addr pos1 -- max span addr pos2 f )
   88:     dup  IF  over + u8<< over -  0 max .all .rest
   89:     ELSE  bell  THEN 0 ;
   90: : u8forw  ( max span addr pos1 -- max span addr pos2 f )
   91:     2 pick over <> IF  over + u8@+ u8emit over -  ELSE  bell  THEN 0 ;
   92: : (u8del)  ( max span addr pos1 -- max span addr pos2 )
   93:     over + dup u8<< tuck - >r over -
   94:     >string over r@ + -rot move
   95:     rot r> - -rot ;
   96: : ?u8del ( max span addr pos1 -- max span addr pos2 0 )
   97:   dup  IF  (u8del) .all 2 spaces .rest  THEN  0 ;
   98: : <u8del> ( max span addr pos1 -- max span addr pos2 0 )
   99:   2 pick over <>
  100:     IF  u8forw drop (u8del) .all 2 spaces .rest
  101:     ELSE  bell  THEN  0 ;
  102: : u8eof  2 pick over or 0=  IF  bye  ELSE  <u8del>  THEN ;
  103: 
  104: : u8first-pos  ( max span addr pos1 -- max span addr 0 0 )
  105:   drop 0 .all .rest 0 ;
  106: : u8end-pos  ( max span addr pos1 -- max span addr span 0 )
  107:   drop over .all 0 ;
  108: 
  109: 
  110: : u8clear-line ( max span addr pos1 -- max addr )
  111:     drop restore-cursor swap spaces restore-cursor ;
  112: : u8clear-tib ( max span addr pos -- max 0 addr 0 false )
  113:     u8clear-line 0 tuck dup ;
  114: 
  115: : (u8enter)  ( max span addr pos1 -- max span addr pos2 true )
  116:     >r end^ 2@ hist-setpos
  117:     2dup swap history write-line drop ( throw ) \ don't worry about errors
  118:     hist-pos 2dup backward^ 2! end^ 2!
  119:     r> .all space true ;
  120: 
  121: : u8kill-expand ( max span addr pos1 -- max span addr pos2 )
  122:     prefix-found cell+ @ ?dup IF  >r
  123: 	r@ - >string over r@ + -rot move
  124: 	rot r@ - -rot .all r> spaces .rest THEN ;
  125: 
  126: : insert   ( string length buffer size -- )
  127:     rot over min >r  r@ - ( left over )
  128:     over dup r@ +  rot move   r> move  ;
  129: 
  130: : u8tab-expand ( max span addr pos1 -- max span addr pos2 0 )
  131:     key? IF  #tab (u8ins) 0  EXIT  THEN
  132:     u8kill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
  133:     search-prefix tib-full?
  134:     IF    7 emit  2drop  prefix-off
  135:     ELSE  dup >r
  136: 	2>r >string r@ + 2r> 2swap insert
  137: 	r@ + rot r> + -rot
  138:     THEN
  139:     prefix-found @ IF  bl (u8ins)  ELSE  .all .rest  THEN  0 ;
  140: 
  141: : utf-8-io ( -- )
  142:     ['] u8forw       ctrl F bindkey
  143:     ['] u8back       ctrl B bindkey
  144:     ['] ?u8del       ctrl H bindkey
  145:     ['] u8eof        ctrl D bindkey
  146:     ['] <u8del>      ctrl X bindkey
  147:     ['] u8clear-tib  ctrl K bindkey
  148:     ['] u8first-pos  ctrl A bindkey
  149:     ['] u8end-pos    ctrl E bindkey
  150:     ['] (u8enter)    #lf    bindkey
  151:     ['] (u8enter)    #cr    bindkey
  152:     ['] u8tab-expand #tab   bindkey
  153:     ['] (u8ins)      IS insert-char
  154:     ['] kill-prefix  IS everychar
  155:     ['] save-cursor  IS everyline
  156:     ['] u8key        IS key
  157:     ['] u8emit       IS emit ;
  158: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>