******************************************************************                                                               **  Scrappy v 1.1                                                **  Copyright 1997 Kelvin Sherlock                               **  ALL RIGHTS RESERVED.                                         **                                                               **  For ORCA/M assembler                                         **                                                               *                                         **                                                               **                                                               **  Nov 27th, 1997:                                              **	Finalized copy routines                                **                                                               **  August 27th, 1997                                            **	Squashed an output bug                                 **                                                               **  May 20th, 1997:                                              **	Initial version.                                       **                                                               ******************************************************************	mcopy scrappy.mac	case onscrappy	start	ScrappyMyID	equ 0CmdPtr	equ 2Scrap	equ 6Length	equ 10	phk	plb	sta MyID       	sty CmdPtr	stx CmdPtr+2	ldy #8loop	anop	iny	lda [CmdPtr],y	and #$00ff	beq NoFlag	cmp #'-'	beq GotFlag	bra loopNoFlag	anop			;;just paste it	brl DoHelpGotFlag	anop			;;determine the flag	iny	lda [CmdPtr],y	and #$00ff	beq DoHelp	cmp #'V'	beq DoHelp	cmp #'h'	beq DoHelp	cmp #'v'	beq DoPaste	cmp #'p'	beq DoPaste	cmp #'z'	beq DoZero	cmp #'c'	bne DoHelp	brl DoCopy;; DoHelp;;;; Writes Help info to Stderr and exits;;DoHelp	anop           	~ErrWriteCString #HelpTxt	brl Quit	;;a=0 if ok;; DoZero;;;; Zeros the scrap and exits;;DoZero	anop	_ZeroScrap		brl Quit	;;a=0 if ok;; DoPaste;;;; Pastes the clipboard to stdout and exits;;DoPaste	anop	pha	pha	pea $0000		;text scrap	_GetScrapHandle	plx	stx Scrap	plx	stx Scrap+2	bcc no_err		;still set ( or not )	cmp #$1610		;BadScrapType - No scrap available	bne st_err	~ErrWriteCString #No_Txt	lda #1		;error!	brl Quitst_err	anop			;generic error (not likely)	~ErrWriteCString #Err_Txt	lda #1		;error	brl Quitno_err	anop	pha		;;space	pha	~GetHandleSize <Scrap	;;get the scarp size	pla	sta WriteDCB_requestCount	;;store it for outpit	pla	sta WriteDCB_requestCount+2		ldy #2	;;deref the scrap handle	lda [<Scrap],y	sta WriteDCB_dataBuffer+2	lda [<Scrap]	sta WriteDCB_dataBuffer	lda #2	;;stdout	sta WriteDCB_refNum	_WriteGS WriteDCB	pha		;;store the error	pea $0D	;add an extra return	_WriteChar	pla		;;error (from WriteGS)	brl Quit;; DoCopy;;;; read stdin & copy to clipboard;;DoCopy	anop	lda #1		;;stdin	sta WriteDCB_refNum;;check if stdin is a redirected file ( scrappy -c < file )	_GetEOFGS EOFDCB	bcs notfile	ldax EOF_eof	stax WriteDCB_requestCount	pha		;;space	pha	phxa		;;length	pei MyID	;;ID	pea $8008	;;attr	ph4 #0	;;loc	_NewHandle	plax <Scrap	bcc n1	~ErrWriteCString #Err_Mem	lda #1	brl Quit	;;a=0 if okn1	anop	ldy #2	lda [<Scrap],y	sta WriteDCB_dataBuffer+2	lda [<Scrap]	sta WriteDCB_dataBuffer	_ReadGS WriteDCB	brl copy2;;;; Stdin is either a pipe or a terminal :-(;;notfile	anop	stz Length	stz Length+2	lda #1024	;presumed size of pipe	sta WriteDCB_requestCount	stz WriteDCB_requestCount+2	pha		;;space	pha	phxa		;;length	pei MyID	;;ID	pea $8008	;;attr	ph4 #0	;;loc	_NewHandle	plax <Scrap	bcc n2	~ErrWriteCString #Err_Mem	lda #1	brl Quitn2	anop	ldy #2	lda [<Scrap],y	sta WriteDCB_dataBuffer+2	lda [<Scrap]	sta WriteDCB_dataBufferrloop	anop	_ReadGS WriteDCB	cmp #$4c	;;eof?	bne get_more	clc	lda WriteDCB_transferCount	adc Length	sta Length	lda #0	adc Length+2	sta Length+2	lda WriteDCB_transferCount	;if =1024	cmp #1024	beq nres	;don't resize (not likely, though)	ph4 <Scrap	_HUnlock	ph4 <Length	;;new size	ph4 <Scrap	_SetHandleSize	;;since the new size is less than the old one, this can't fail!	ph4 <Scrap	_HLocknres	anop	pea $0D	;clean up the console...	_WriteChar	brl copy2get_more	anop		;get some more input	clc	lda WriteDCB_transferCount	adc <Length	sta <Length	lda #0	adc <Length+2	sta <Length+2	ph4 <Scrap	_HUnlock	lda WriteDCB_transferCount	cmp #1024	beq foo	; if 1024 chars read, don't resize	ph4 <Length	ph4 <Scrap	_SetHandleSize	;;set the handle to the filled size;;The handle is being shrunk, so there should't be a memory error...foo	clc	lda #1024	adc <Length	tax		;save	lda #0	adc <Length+2	pha	phx	ph4 <Scrap	_SetHandleSize	;;+1024 to the handle	bcc n3	~ErrWriteCString #Err_Mem	lda #1	brl Quitn3	anop	ph4 <Scrap	_HLock	ldy #2	lda [<Scrap],y	sta WriteDCB_dataBuffer+2	lda [<Scrap]	sta WriteDCB_dataBuffer	clc	lda <Length	adc WriteDCB_dataBuffer	sta WriteDCB_dataBuffer	lda <Length+2	adc WriteDCB_dataBuffer+2	sta WriteDCB_dataBuffer+2                      	brl rloopcopy2	anop	_ZeroScrap	;clear the old scrap	pha	pha	~GetHandleSize <Scrap	;length	pea 0		;type = text	ldy #2	lda [<Scrap],y	;src ptr	pha	lda [<Scrap]	pha	_PutScrap	pha	ph4 <Scrap	_DisposeHandle	plaQuit	anop	rtlNo_Txt	dc H'0D'	dc c'Scrappy: No text scrap available.',H'0d 00'Err_Txt	dc H'0D'	dc c'Scrappy: Something Bad happened, and I don''t know why!',H'0d 00'Err_Mem	dc H'0D'	dc c'Scrappy: Memory allocation error.',H'0d 00'HelpTxt	dc i1'$0D'	dc c'Scrappy v1.1 by Kelvin Sherlock',H'0d'	dc c'flags',H'0d'	dc c'  -V show version information',H'0d'	dc c'  -h show usage information',H'0d';	dc c'  -v paste clipboard to stdout',H'0d'	dc c'  -p paste clipboard to stdout',H'0d'	dc c'  -c copy stdin to clipboard',H'0d'	dc c'  -z zero (empty) the clipboard',H'0d 00'WriteDCB	anopWriteDCB_pcount	dc i2'5'WriteDCB_refNum	dc i2'2'	;;stdoutWriteDCB_dataBuffer	ds 4WriteDCB_requestCount	ds 4WriteDCB_transferCount	ds 4WriteDCB_cachePriority	dc i4'0'EOFDCB	dc i2'2'	;;pcount	dc i2'1'	;;stdinEOF_eof	ds 4		;;eofscrappy	endStack	data ~_STACK	kind $12	ds 512Stack	end
