home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Elysian Archive
/
AmigaElysianArchive.iso
/
printer
/
deskjet_.lha
/
RemAnsi.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1992-10-03
|
7KB
|
175 lines
/* remove any (Amiga / ANSI-)control codes from the textfile */
/* ^^^^^ should remove a superset of these, hopefully ... */
/* remove 00..07,0b,0d..1f,7f..9f,ff */
/* optionally replace 0c by 0a (formfeed by linefeed) */
/* optionally remove 08 and one(!) of its neighbours */
/* to remove 'typewriterlike-doublestrike/underlining */
/* PS: NEVER USE A HP PC8 TEXT as INPUT -> some chars (UMLAUTS !) are removed ! */
/*
Peter Jakobi, Munich, Oct. 2, 1992
jakobi@informatik.tu-muenchen.de
lazarus@salyko.doit.cubenet.org
*/
parse upper arg infile outfile options /* open the files ... */
IF infile='?' THEN DO
say 'REMANSI.rexx <infile> <outfile> <options>'
say 'Remansi removes any control codes or ESC/CSI sequences from the inputfile.'
say 'Options: noff remove format feeds'
say ' nobs remove any BS from the text (double striked titles, ...)'
say 'You may want to filter the output with stripbin ... '
EXIT 1
END
/* GLOBAL VAR: VAL CHAR */
/* options */
IF index(' 'options' ',' NOFF ')>0 THEN noff=1
ELSE noff=0
IF index(' 'options' ',' NOBS ')>0 THEN nobs=1
ELSE nobs=0
IF open(infh,infile,'R')=0 THEN EXIT 5
IF open(outfh,outfile,'W')=0 THEN EXIT 5
ESC='1b'x /* remove */
CSI='9b'x /* remove */
BS ='08'x /* remove ? */
FF ='0c'x /* remove ? */
DEL='ff'x /* remove */
match = '1B9B 0001 0203 0405 0607 0b0d 0e0f 1011 1213 1415 1617 1819 1a1c 1d1e 1fff'
match = match||'7f80 8182 8384 8586 8788 898a 8b8c 8d8e 8f90 9192 9394 9596 9798 999a 9c9d 9e9f'
match=x2c(match)
IF nobs THEN match=match||BS
IF noff THEN match=match||FF
inputsize=1000
i=1 /* inputbuffer pointer */
out='' /* output buffer */
IF readblock()=0 THEN SIGNAL done
DO FOREVER
IF i=0 THEN SIGNAL done
IF inputsize=0 THEN SIGNAL done
rest=substr(line,i)
j=verify(rest,match,'M')
IF j=0 THEN DO
out=out||rest
IF readblock()=0 THEN SIGNAL done
END
ELSE DO
out=out||substr(line,i,j-1)
i=j+i-1
call getchar()
SELECT /* to what CLASS belongs the character ? */
WHEN char = ESC THEN DO
call getchar() /* next char */
SELECT /* CSI: 'superset' von ESC[ */
WHEN char='[' THEN DO
IF getchar()='P' THEN DO
/* test for P<n>"r and skip the raw bytes */
IF getzahl()='"' THEN IF getchar()='r' THEN DO; DO j=1 TO val ; call getchar() ; END; ITERATE; END
IF index(' #"(',char)>0 THEN call getchar() /* two char footer ? */
ITERATE
END
ELSE DO ; /* skip the number(s) and the 1 or 2 char footer of the sequence */
IF index(' #"(',getzahl())>0 THEN call getchar()
ITERATE
END
END
WHEN index(' #"(',char)>0 THEN DO
/* remove 3 letter codes */
call getchar()
ITERATE
END
OTHERWISE ITERATE /* 2 letter codes */
END
END
WHEN char = CSI THEN DO
/* CSI ist ein 'superset' von ESC[ */
IF getchar()='P' THEN DO
/* test for P<n>"r and skip the raw bytes */
/* getzahl degeniert u.u. zu NIL (nicht einmal getchar !) :-) */
IF getzahl()='"' THEN IF getchar()='r' THEN DO; DO j=1 TO val ; call getchar() ; END; ITERATE; END
IF index(' #"(',char)>0 THEN call getchar() /* two char footer ? */
ITERATE
END
ELSE DO ; /* skip the number(s) and the 1 or 2 char footer of the sequence */
IF index(' #"(',getzahl())>0 THEN call getchar()
ITERATE
END
/* (it's footer), before which there may be a sequence */
END
WHEN char=DEL THEN DO ; call getchar(); ITERATE ;END /* (3) delete removes the next char */
WHEN char=FF THEN DO
IF noff THEN char=LF
out=out||char
END
WHEN char=BS THEN DO
IF nobs THEN DO
IF getchar()='_' THEN ITERATE /* backspaces: xBS_ -> x ; _BSx|xBSx|yBSx -> x */
IF out~='' THEN out=substr(out,1,length(out)-1)
END
out=out||char
END
OTHERWISE nop /* remove the character ! */
END /* END SELECT */
/* avoid common code here ! since i'm iterating in advance ! */
END /* ELSE */
END
done:
call close(infh)
call close(outfh)
EXIT 0
/****************************************************************/ /* subroutines */
/* You might wish to call these routines Rexx Level II File IO :-) */
readblock: PROCEDURE EXPOSE infh line i inputsize out outfh oldchar /* read the next chunk */
oldchar=right(line,1)
line = readch(infh,inputsize)
inputsize=length(line) /* adjust the length, if zero: eof */
i=1
IF line=='' THEN i=0
k=length(out)-1
if k<0 then k=0
call writech(outfh,substr(out,1,k))
out=right(out,1) /* out and oldchar: a kind of unputc and ungetc :-) */
RETURN i
getchar: PROCEDURE EXPOSE infh line i char inputsize out outfh oldchar
/* ATN return the current char (line[i]) or blank and advance */
IF i=0 THEN a=''
ELSE DO
IF i>inputsize THEN call readblock /* next buffer */
IF inputsize=0 THEN a=''
ELSE a= substr(line,i,1)
END
i=i+1
oldchar=char
char = a
RETURN a
getzahl: PROCEDURE EXPOSE infh line i char val inputsize out outfh oldchar
val=''
DO FOREVER ; /* read a number */
IF index('0123456789P',char)=0 THEN LEAVE
IF char~='P' THEN val=val||char
call getchar()
END
IF char=';' THEN DO ; call getchar() ; call getzahl() ; END /* further numbers, introduced by ';' */
IF datatype(val)~='NUM' then val=0
oldchar='' /* disable test for removal of BS - undefined */
RETURN char