;
; hour_util.s
;
; Fiddle with hourglass from command line
;
;  1994-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's core utilities (coreutils).
;
; Coreutils is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Coreutils is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Coreutils.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- External dependencies ------------------------------------------------

		IMPORT	version

;----- Main code ------------------------------------------------------------

		GBLA	count

		AREA	|!!!Util$$Code|,CODE,READONLY

main		ROUT

		STMFD	R13!,{R14}		;Save the return address
		ADR	R0,hour__cmdDef		;Point to command definition
		MOV	R2,R12			;Point to a workspace buffer
		MOV	R3,#256			;Size of the buffer
		SWI	XOS_ReadArgs		;Try to interpret the args
		BVS	%90main			;If it failed, return

		; --- Handle the arguments ---

		LDMIA	R12,{R0-R4}		;Load values from block

		CMP	R0,#0			;Does he want some help?
		BNE	hour__help		;Yes -- display help then

		; --- Make sure at most one is set ---

		MOV	R14,#0			;Clear argument count
		MOV	R0,#1			;Default is to turn on

count		SETA	1
		WHILE	count<5
R$count		RN	$count
		CMP	R$count,#0		;Is the argument on?
		ADDNE	R14,R14,#1		;Yes -- bump the counter
		MOVNE	R0,#count-1		;And remember the argument
count		SETA	count+1
		WEND

		CMP	R14,#2			;Is the count valid?
		ADRCS	R0,hour__badArgs	;No -- point to error
		BCS	%90main			;And return to caller

		; --- Now do the necessaries ---
		;
		; The following should be two words long each -- this enables
		; most operations to be done inline with just a SWI and a
		; return.

		MOV	R14,PC			;Set up return address
		ADD	PC,PC,R0,LSL #3		;Do what we must do
		LDMFD	R13!,{PC}		;Return to caller

		; --- Disable the hourglass ---

		SWI	XHourglass_Off
		MOV	PC,R14

		; --- Enable the hourglass ---

		SWI	XHourglass_On
		MOV	PC,R14

		; --- Trash hourglass totally ---

		SWI	XHourglass_Smash
		MOV	PC,R14

		; --- Turn on the percentage indicator ---
		;
		; To save space, this should be the last option.

		LDRB	R0,[R4],#1		;Load the expression type
		CMP	R0,#0			;Is it numeric?
		ADRNE	R0,hour__badType	;No -- must be a bad type
		ORRNES	PC,R14,#V_flag		;So return the error
		AND	R2,R4,#3		;Get non-wordalignedness
		BIC	R4,R4,#3		;Word align the address
		LDMIA	R4,{R0,R1}		;Load two overlapping words
		MOV	R2,R2,LSL #3		;Convert bytes to bits
		RSB	R3,R2,#32		;And get reverse shift size
		MOV	R0,R0,LSR R2		;Get the bottom end right
		ORR	R0,R0,R1,LSL R3		;And move in the top end
		SWI	XHourglass_Percentage	;Set the percentage as reqd
		MOV	PC,R14			;And return to caller

90main		LDMFD	R13!,{R14}		;Load the return address
		ORRS	PC,R14,#V_flag		;And return an error

hour__cmdDef	DCB	"help/S,"		;Offset 0  (R0)
		DCB	"off/S,"		;Offset 8  (R1)
		DCB	"on/S,"			;Offset 4  (R2)
		DCB	"smash/S,"		;Offset 12 (R3)
		DCB	"percent/K/E,"		;Offset 16 (R4)
		DCB	0

hour__badArgs	DCD	1
		DCB	"Syntax: hour [<command>]",0

hour__badType	DCD	1
		DCB	"Percentage must be a number",0

hour__help	ADR	R0,hour__helpText	;Point to the help text
		MOV	R1,#0			;No dictionary please
		LDR	R2,=version		;Find the version string
		ADR	R14,main		;Find my base address
		ADD	R2,R14,R2		;Find the actual message
		SWI	XOS_PrettyPrint		;Print the message
		LDMFD	R13!,{PC}^		;And return to caller

		LTORG

hour__helpText	DCB	"hour ",27,0,13
		DCB	13
		DCB	"Syntax: hour [<command>]",13
		DCB	13
		DCB	"Performs an Hourglass operation as specified by "
		DCB	"<command>.  If no command is specified, the "
		DCB	"Hourglass is turned on.",13
		DCB	13
		DCB	"Commands allowed are:",13
		DCB	13
		DCB	"-help",9,9,"Displays this help text",13
		DCB	"-on",9,9,"Turns the Hourglass on",13
		DCB	"-off",9,9,"Turns the Hourglass off",13
		DCB	"-smash",9,9,"Forces the Hourglass off",13
		DCB	"-percent",9,"Displays the given percentage",13
		DCB	0

;----- That's all, folks ----------------------------------------------------

		END
