************************************************************************
*							*
*		     STRIPWF				*
*							*
*		    Version 1.1				*
*							*
************************************************************************

;Dr J. Szajman
;38 Ludbrook Ave
;South Caulfield
;Melbourne, Vic.  3162
;Australia

;Strip WordStar document:
;Carriage Return (CR)
;Characters below $20
;Clear the most significant bit
;Remove format information (delete a single Line Feed)
;Remove the "dot" commands


**	Permission is granted to purchasers of Macro68 to modify this source
**	file.  Permission is further granted to distribute the source code that
**	they have modified subject to the following conditions:
**	1.  That the original, unaltered, source is included with the
**	    distribution.
**	2.  That no charge is made for the distribution, other than the cost of
**	    media.                    (Distribution via Fred Fish is permitted)
**	3.  That my copyright notice remains as part of this and any other
**	    file adapted from this file.


;-----------------  Modifications  -------------------------------------

;Tuesday 13-Feb-90
;version 1.1
;bug fix: last few lines are not scanned

	exeobj
	errfile	'ram:assem.output'
	objfile	'ram:stripWS'
	strict


	ifnd	_AbsExecBase
_AbsExecBase	equ	4		;exec library base address

**********  Exec.lib offsets  **********

_LVOAlert	equ	-$6C
_LVOAllocMem	equ	-$C6
_LVOFreeMem	equ	-$D2
_LVOFindTask	equ	-$126
_LVOOldOpenLibrary equ	-$198
_LVOCloseLibrary	equ	-$19E

**********  Alerts  **********

AG_OpenLib	equ	$30000
AO_DOSLib	equ	$70008007

**********  DOS.lib offsets   **********

_LVOOpen	equ	-$1E
_LVOClose	equ	-$24
_LVORead	equ	-$2A
_LVOWrite	equ	-$30
_LVOInput	equ	-$36
_LVOOutput	equ	-$3C

**********  Definitions   **********

MODE_OLDFILE	equ	1005
MODE_NEWFILE	equ	1006

pr_CLI	equ	172		;CLI or WB program

	endc

buffer	equ	$50000

**********    ERROR definitions    ***************

ERROR_FAIL	equ	10

****************************************

	movea.l (_AbsExecBase),a6
	move.l	d0,d4
	movea.l	a0,a4
	suba.l	a1,a1
	jsr	(_LVOFindTask,a6)	;find own task
	movea.l	d0,a0
	tst.l	(pr_CLI,a0)	;from CLI?
	beq	exit0	;workbench
	lea	(doslib,pc),a1	;dos.library
	clr.l	d0		;any version
	jsr	(_LVOOldOpenLibrary,a6) ;open Dos Library
	movea.l	d0,a5	;DOSBase in a5
	tst.l	d0
	bne.b	DOS_OK
	move.l	#AG_OpenLib!AO_DOSLib,d7
	jsr	(_LVOAlert,a6)
	bra	exit0

DOS_OK	move.l	#ERROR_FAIL,d3
	move.l	#buffer,d0
	moveq	#0,d1
	jsr	(_LVOAllocMem,a6)
	tst.l	d0
	bne.b	mem_OK
	lea	(nomem.msg,pc),a0
	exg	a5,a6
	bsr	display
	exg	a5,a6
	bra	exit1

mem_OK	movea.l	d0,a2
	exg	a5,a6	;DosBase in a6
	subq.l	#1,d4	;skip LF & correct count
	lea	(usage.msg,pc),a0
	blt.b	error_exit
loop	cmpi.b	#' ',(a4)+
	dbne	d4,loop
	tst.l	d4
	ble.b	error_exit
	suba.l	#1,a4
	move.b	#0,(0,a4,d4.w)
	cmpi.w	#255-3,d4
	blt	check_option
error_name	lea	(name.error,pc),a0
error_exit	bsr	display
	bra	exit2

check_option	moveq	#1,d7	;flag
	cmpi.b	#'-',(a4)
	bne.b	copy_name
	move.b	(1,a4),d0
	bset	#6,d0
	moveq	#0,d7
	cmpi.b	#'a',d0
	beq.b	opt_OK
	cmpi.b	#'d',d0
	bne.b	error_exit
	moveq	#2,d7
opt_OK	subq.l	#2,d4
	adda.l	#2,a4	;skip option
1$	cmpi.b	#' ',(a4)+
	dbne	d4,1$
	tst.l	d4
	ble.b	error_exit
	suba.l	#1,a4
copy_name	subq.l	#1,d4	;do not shift terminator
	movea.l	a4,a0	;CLI command file name
	movea.l	a2,a1	;buffer
file_name_lp1	move.b	(a0)+,(a1)+
	dbf	d4,file_name_lp1	;file name
	move.b	#'.',(a1)+
	move.b	#'s',(a1)+
	clr.b	(a1)
	move.l	#MODE_OLDFILE,d2
	move.l	a4,d1
	jsr	(_LVOOpen,a6)
	move.l	d0,d5	;file handle
	bne.b	open_strip
	lea	(open_error,pc),a0
	bsr	display
	bra	exit2

open_strip	move.l	a2,d1
	move.l	#MODE_NEWFILE,d2
	jsr	(_LVOOpen,a6)
	move.l	d0,d6
	bne.b	loop3
	lea	(open_error,pc),a0
	bsr	display
	bra	exit3

loop3	move.l	d5,d1
	move.l	a2,d2
	move.l	#buffer,d3
	jsr	(_LVORead,a6)
	tst.l	d0
	ble	exit4
	moveq	#0,d4	;count
	movea.l	a2,a0
	movea.l	a2,a1
	move.b	(a0),d1
	cmpi.b	#'.',d1
	beq.b	dot_strip_lp1
loop1	subq.l	#1,d0
	blt.b	done
	move.b	(a0)+,d1
	bclr	#7,d1
	cmpi.b	#13,d1 
	beq.b	loop1
	cmp.b	#10,d1
	beq.b	loop2
	cmpi.b	#' ',d1
	blt.b	loop1
good_char	move.b	d1,(a1)+
	addq.l	#1,d4
	bra.b	loop1

loop2	cmpi.b	#13,(a0)
	bne.b	check_LF
	subq.l	#1,d0
	blt.b	done
	addq.l	#1,a0	;eleminate CR's
	bra.b	loop2

check_LF	tst.w	d7
	bne.b	dot_strip
	move.b	#' ',d1
	cmp.b	(a0),d1
	beq.b	loop1	;next char space
	cmp.b	(-1,a1),d1	;previous char SPACE?
	beq.b	loop1
check1_LF	cmpi.b	#10,(a0)
	bne.b	dot_strip
	subq.l	#1,d0
	blt.b	done
	move.b	(a0)+,d1	;next LF
	move.b	d1,(a1)+
	add.l	#1,d4
loop4	cmpi.b	#13,(a0)
	bne.b	check1_LF
	subq.l	#1,d0
	blt.b	done
	addq.l	#1,a0	;eleminate CR's
	bra.b	loop4

dot_strip	move.b	d1,(a1)+
	addq.l	#1,d4
dot_strip_lp2	btst	#1,d7
	bne.b	loop1
	cmpi.b	#'.',(a0)	;dot command?
	bne.b	loop1
dot_strip_lp1	subq.l	#1,d0
	blt.b	done
	cmpi.b	#10,(a0)+
	bne.b	dot_strip_lp1
	bra.b	dot_strip_lp2	;another dot command

done	move.l	d6,d1
	move.l	a2,d2
	move.l	d4,d3
	jsr	(_LVOWrite,a6)
	tst.l	d0
	bge	loop3
	lea	(writeerr.msg,pc),a0
	bsr.b	display
exit4	move.l	d6,d1
	jsr	(_LVOClose,a6)
exit3	move.l	d5,d1
	jsr	(_LVOClose,a6)
exit2	exg	a5,a6
	movea.l	a2,a1
	move.l	#buffer,d0
	jsr	(_LVOFreeMem,a6)
exit1	movea.l	a5,a1
	jsr	(_LVOCloseLibrary,a6)
	move.l	d3,d0	 ;return code
exit0	rts


;Display ASCII text
;entry - a0 - points to msg
;        a6 - DOSBase
;used  - NONE

display	movem.l d0-d3,-(sp)
	move.l  a0,d2
	jsr	(_LVOOutput,a6)	;get output
	movea.l	d2,a0
	move.l	d0,d1
display_loop1	tst.b  (a0)+		;look for NULL
	bne.b	display_loop1
	move.l	a0,d3
	sub.l	d2,d3
	jsr	(_LVOWrite,a6)
	movem.l (sp)+,d0-d3
	rts


doslib	dc.b	'dos.library',0

usage.msg	dc.b	10,$9B,$31,$3B,$33,$33,$3B,$34,$30,$6D	;bold, colour
	dc.b	'Word Star to ASCII reformatter (V1.1), written by J. Szajman'
	dc.b	$9B,$30,$3B,$33,$31,$3B,$34,$30,$6D,10,10
	dc.b	'Usage: [a|d] <file name>',10
	dc.b	'       a = strip all',10
	dc.b	'       d = CTL codes and CR',10
	dc.b	'    name = dot cmd, CTL codes and CR',10,10,0
nomem.msg	dc.b	10,'Error, out of memory',10,0
writeerr.msg	dc.b	10,'Error, while writting',10,0
name.error	dc.b	10,'Error, name/path too long',10,0
open_error	dc.b	10,'Error, cannot open file',10,0


	END
