29 lines
974 B
Forth
29 lines
974 B
Forth
variable sendbuffer-len 0 sendbuffer-len !
|
|
4096 constant SENDBUFFER_SIZE
|
|
create sendbuffer SENDBUFFER_SIZE allot
|
|
|
|
\ Calling C here is just optimization.
|
|
c-library sanitizelib
|
|
\c void csanitize(char *buffer, int buffersize) {
|
|
\c int lastIsNewline = buffer[buffersize-1] == '\n' ? 1 : 0;
|
|
\c for(int i = 0; i<buffersize; i++) {
|
|
\c if(buffer[i]<32 || buffer[i]>126) { buffer[i] = '?'; }
|
|
\c }
|
|
\c if(lastIsNewline) { buffer[buffersize-1] = '\n'; }
|
|
\c return;
|
|
\c }
|
|
c-function csanitize csanitize a n -- void
|
|
end-c-library
|
|
|
|
: sendbuffer-reset ( -- ) 0 sendbuffer-len ! ;
|
|
: (overflow?) ( n -- flag )
|
|
sendbuffer-len @ + SENDBUFFER_SIZE u> ;
|
|
: (append) ( str -- )
|
|
dup -rot sendbuffer sendbuffer-len @ + swap move
|
|
sendbuffer-len +! ;
|
|
: sendbuffer-append ( str -- )
|
|
dup (overflow?) abort" sendbuffer overflow" (append) ;
|
|
: sendbuffer-sanitize ( -- )
|
|
sendbuffer sendbuffer-len @ csanitize ;
|
|
: sendbuffer@ ( -- str ) sendbuffer sendbuffer-len @ ;
|