; CLIPalette 1.0 - © 1993 F.Delacroix
; Usage: CLIPalette R0G0B0 R1G1B1 R2G2B2 R3G3B3

	incdir	Include:
	include	exec/exec.i
	include	exec/exec_lib.i
	include	exec/execbase.i
	include	libraries/dos.i
	include	libraries/dos_lib.i
	include	libraries/dosextens.i
	include	intuition/intuition.i
	include	graphics/graphics_lib.i

DosBase=0
OutHandle=4
GfxBase=8
MyProcess=12
Color0R=16
Color0G=17
Color0B=18
Color1R=19
Color1G=20
Color1B=21
Color2R=22
Color2G=23
Color2B=24
Color3R=25
Color3G=26
Color3B=27
CLIWindow=28
ArgC=32
ArgV=36
VARSIZE=436


	sub.l	#VARSIZE,sp
	move.l	sp,a4

	lea	ArgV(a4),a1
	bsr	Parse
	move.l	d0,ArgC(a4)

	lea	DosName(pc),a1
	moveq.l	#0,d0
	move.l	4.w,a6
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,DosBase(a4)
	beq	FreeVars

	move.l	d0,a6
	jsr	_LVOOutput(a6)
	move.l	d0,OutHandle(a4)

	move.l	d0,d1
	lea	Welcome.MSG(pc),a2
	move.l	a2,d2
	move.l	#Welcome.LGT,d3
	jsr	_LVOWrite(a6)

	move.l	4.w,a6
	lea	GfxName(pc),a1
	moveq.l	#0,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,GfxBase(a4)
	bne.s	GfxOpened
	move.l	DosBase(a4),a6
	move.l	OutHandle(a4),d1
	lea	NoGraphics.MSG(pc),a2
	move.l	a2,d2
	move.l	#NoGraphics.LGT,d3
	jsr	_LVOWrite(a6)
	bra	CloseDos

GfxOpened
	move.l	ThisTask(a6),MyProcess(a4)

	cmp.l	#4,ArgC(a4)
	bne	DisplayInfo

	moveq.l	#3,d6	; d6=Color Count
	lea	ArgV(a4),a5	; a5=Current Word
	lea	Color0R(a4),a3	; a3=Component Storage
NextColor
	move.l	(a5)+,a2
	moveq.l	#2,d5	; d5=Component Count
NextComponent
	move.b	(a2)+,d0
	cmp.b	#'0',d0
	bmi	DisplayInfo
	cmp.b	#'9'+1,d0
	bpl.s	NoDigit
	sub.b	#'0',d0
	bra.s	CompFound
NoDigit	cmp.b	#'A',d0
	bmi	DisplayInfo
	cmp.b	#'F'+1,d0
	bpl.s	NoUpper
	sub.b	#'A'-$A,d0
	bra.s	CompFound
NoUpper	cmp.b	#'a',d0
	bmi	DisplayInfo
	cmp.b	#'f'+1,d0
	bpl	DisplayInfo
	sub.b	#'a'-$A,d0
CompFound
	move.b	d0,(a3)+
	dbra	d5,NextComponent
	dbra	d6,NextColor

	move.l	MyProcess(a4),a0
	bsr	GetCLIWindow
	move.l	d0,CLIWindow(a4)
	bne.s	WindowFound
	move.l	OutHandle(a4),d1
	lea	NoWindow.MSG(pc),a2
	move.l	a2,d2
	move.l	#NoWindow.LGT,d3
	move.l	DosBase(a4),a6
	jsr	_LVOWrite(a6)
	bra.s	CloseGfx
WindowFound
	move.l	d0,a0
	move.l	wd_WScreen(a0),a0
	lea	sc_ViewPort(a0),a3
	lea	Color0R(a4),a5
	moveq.l	#0,d6
	move.l	GfxBase(a4),a6
SetColor
	move.l	d6,d0
	move.l	a3,a0
	move.b	(a5)+,d1
	move.b	(a5)+,d2
	move.b	(a5)+,d3
	jsr	_LVOSetRGB4(a6)
	addq.l	#1,d6
	cmp.l	#4,d6
	bne.s	SetColor

	move.l	OutHandle(a4),d1
	lea	ColorsSet.MSG(pc),a2
	move.l	a2,d2
	move.l	#ColorsSet.LGT,d3
	move.l	DosBase(a4),a6
	jsr	_LVOWrite(a6)

CloseGfx
	move.l	GfxBase(a4),a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)
CloseDos
	move.l	DosBase(a4),a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)
FreeVars
	add.l	#VARSIZE,sp
	rts

DisplayInfo
	move.l	OutHandle(a4),d1
	lea	Info.MSG(pc),a2
	move.l	a2,d2
	move.l	#Info.LGT,d3
	move.l	DosBase(a4),a6
	jsr	_LVOWrite(a6)
	bra.s	CloseGfx

Parse:	movem.l	a2-a3,-(sp)
	move.l	a1,a3
	move.l	a3,-(sp)
	clr.b	-1(a0,d0)
	move.l	a0,a2
	lea	0(a0,d0.l),a1
.SkipEndSpaces
	cmp.b	#" ",-(a1)
	dbhi	d0,.SkipEndSpaces
	clr.b	1(a1)
.NextWord
	move.b	(a0)+,d1	; Next Character into D1
	beq.s	.EndOfString
	cmp.b	#" ",d1		; SPACE ?
	beq.s	.NextWord
	cmp.b	#9,d1		; TAB ?
	beq.s	.NextWord
	move.l	a2,(a3)+	; Store pointer to word
	cmp.b	#'"',d1		; " ?
	beq.s	.Quote
	move.b	d1,(a2)+	; Store character
.NextLetter
	move.b	(a0)+,d1
	beq.s	.EndOfString
	cmp.b	#" ",d1		; SPACE ?
	beq.s	.Unquote
	move.b	d1,(a2)+	; Store character
	bra.s	.NextLetter
.Unquote
	clr.b	(a2)+
	bra.s	.NextWord
.Quote	move.b	(a0)+,d1	; quoted string:ignore spaces
	beq.s	.EndOfString
	cmp.b	#'"',d1		; " ?
	beq.s	.Unquote
	cmp.b	#"*",d1		; * ?
	bne.s	.NextQuotedChar
	move.b	(a0)+,d1
	cmp.b	#"n",d1		; n ?
	beq.s	.NewLine
	cmp.b	#"N",d1		; N ?
	bne.s	.NoNewLine
.NewLine
	moveq	#10,d1		; LineFeed
	bra.s	.NextQuotedChar
.NoNewLine
	cmp.b	#"e",d1		; e ?
	beq.s	.Escape
	cmp.b	#"E",d1		; E ?
	bne.s	.NextQuotedChar
.Escape	moveq	#$1B,d1		; ESC
.NextQuotedChar
	move.b	d1,(a2)+
	bra.s	.Quote
.EndOfString
	clr.b	(a2)
	clr.l	(a3)
	move.l	(sp)+,d0
	sub.l	d0,a3
	move.l	a3,d0
	lsr.l	#2,d0
	movem.l	(sp)+,a2-a3
	rts

GetCLIWindow:
	movem.l	a3-a6,-(sp)
	movea.l	4.w,a6
	move.l	a0,a5			; a5=Process
	tst.l	pr_ConsoleTask(a5)
	bne.s	.ProcessOK
	clr.l	-(sp)
	bra	.Retourne
.ProcessOK
	moveq.l	#sp_SIZEOF,d0
	move.l	#MEMF_CLEAR!MEMF_PUBLIC,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,a4			; a4=StandardPacket
	bne.s	.PacketOK
	clr.l	-(sp)
	bra.s	.Retourne
.PacketOK
	moveq.l	#id_SIZEOF,d0
	move.l	#MEMF_CLEAR!MEMF_PUBLIC,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,a3			; a3=InfoData
	bne.s	.InfoDataOK
	clr.l	-(sp)
	bra.s	.FreePacket
.InfoDataOK
	lea	sp_Pkt(a4),a0
	move.l	a0,sp_Msg+LN_NAME(a4)
	move.l	a4,sp_Pkt+dp_Link(a4)
	lea	pr_MsgPort(a5),a0
	move.l	a0,sp_Pkt+dp_Port(a4)
	move.l	#ACTION_DISK_INFO,sp_Pkt+dp_Type(a4)
	move.l	a3,d0
	lsr.l	#2,d0
	move.l	d0,sp_Pkt+dp_Arg1(a4)

	move.l	pr_ConsoleTask(a5),a0
	move.l	a4,a1
	jsr	_LVOPutMsg(a6)
	lea	pr_MsgPort(a5),a0
	jsr	_LVOWaitPort(a6)
	lea	pr_MsgPort(a5),a0
	jsr	_LVOGetMsg(a6)

	tst.l	sp_Pkt+dp_Res1(a4)
	bne.s	.ResultOK
	clr.l	-(sp)
	bra.s	.FreeInfoData
.ResultOK
	move.l	id_VolumeNode(a3),-(sp)

.FreeInfoData
	move.l	a3,a1
	moveq.l	#id_SIZEOF,d0
	jsr	_LVOFreeMem(a6)
.FreePacket
	move.l	a4,a1
	moveq.l	#sp_SIZEOF,d0
	jsr	_LVOFreeMem(a6)
.Retourne
	move.l	(sp)+,d0
	movem.l	(sp)+,a3-a6
	rts

DosName	dc.b	'dos.library',0

Welcome.MSG
	dc.b	'CLIPalette 1.0 by F.Delacroix.',10,0
Welcome.LGT=*-Welcome.MSG

NoGraphics.MSG
	dc.b	'Could not open '
GfxName	dc.b	'graphics.library',0
	dc.b	' !',10
NoGraphics.LGT=*-NoGraphics.MSG

NoWindow.MSG
	dc.b	'Couldn''t find the CLI''s window !',10
NoWindow.LGT=*-NoWindow.MSG

ColorsSet.MSG
	dc.b	'...Colors set.',10
ColorsSet.LGT=*-ColorsSet.MSG

Info.MSG
	dc.b	'Usage: CLIPalette R0G0B0 R1G1B1 R2G2B2 R3G3B3',10
	dc.b	'where: with x=0,1,2 or 3, RxGxBx being the color number x',10
	dc.b	' of the screen of the CLI screen,in RGB format (3-digit',10
	dc.b	' hexadecimal value.',10
Info.LGT=*-Info.MSG

