delay.4th revision 222417
1222417Sjulian\ Copyright (c) 2008-2011 Devin Teske <devinteske@hotmail.com>
2222417Sjulian\ All rights reserved.
3222417Sjulian\ 
4222417Sjulian\ Redistribution and use in source and binary forms, with or without
5222417Sjulian\ modification, are permitted provided that the following conditions
6222417Sjulian\ are met:
7222417Sjulian\ 1. Redistributions of source code must retain the above copyright
8222417Sjulian\    notice, this list of conditions and the following disclaimer.
9222417Sjulian\ 2. Redistributions in binary form must reproduce the above copyright
10222417Sjulian\    notice, this list of conditions and the following disclaimer in the
11222417Sjulian\    documentation and/or other materials provided with the distribution.
12222417Sjulian\ 
13222417Sjulian\ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14222417Sjulian\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15222417Sjulian\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16222417Sjulian\ ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17222417Sjulian\ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18222417Sjulian\ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19222417Sjulian\ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20222417Sjulian\ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21222417Sjulian\ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22222417Sjulian\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23222417Sjulian\ SUCH DAMAGE.
24222417Sjulian\ 
25222417Sjulian\ $FreeBSD: head/sys/boot/forth/delay.4th 222417 2011-05-28 08:50:38Z julian $
26222417Sjulian
27222417Sjulianmarker task-delay.4th
28222417Sjulian
29222417Sjulian2  constant delay_default \ Default delay (in seconds)
30222417Sjulian3  constant etx_key       \ End-of-Text character produced by Ctrl+C
31222417Sjulian13 constant enter_key     \ Carriage-Return character produce by ENTER
32222417Sjulian27 constant esc_key       \ Escape character produced by ESC or Ctrl+[
33222417Sjulian
34222417Sjulianvariable delay_tstart     \ state variable used for delay timing
35222417Sjulianvariable delay_delay      \ determined configurable delay duration
36222417Sjulianvariable delay_cancelled  \ state variable for user cancellation
37222417Sjulianvariable delay_showdots   \ whether continually print dots while waiting
38222417Sjulian
39222417Sjulian: delay_execute ( -- )
40222417Sjulian
41222417Sjulian	\ make sure that we have a command to execute
42222417Sjulian	s" delay_command" getenv dup -1 = if
43222417Sjulian		drop exit 
44222417Sjulian	then
45222417Sjulian
46222417Sjulian	\ read custom time-duration (if set)
47222417Sjulian	s" loader_delay" getenv dup -1 = if
48222417Sjulian		drop          \ no custom duration (remove dup'd bunk -1)
49222417Sjulian		delay_default \ use default setting (replacing bunk -1)
50222417Sjulian	else
51222417Sjulian		\ make sure custom duration is a number
52222417Sjulian		?number 0= if
53222417Sjulian			delay_default \ use default if otherwise
54222417Sjulian		then
55222417Sjulian	then
56222417Sjulian
57222417Sjulian	\ initialize state variables
58222417Sjulian	delay_delay !          \ stored value is on the stack from above
59222417Sjulian	seconds delay_tstart ! \ store the time we started
60222417Sjulian	0 delay_cancelled !    \ boolean flag indicating user-cancelled event
61222417Sjulian
62222417Sjulian	false delay_showdots ! \ reset to zero and read from environment
63222417Sjulian	s" delay_showdots" getenv dup -1 <> if
64222417Sjulian		2drop \ don't need the value, just existance
65222417Sjulian		true delay_showdots !
66222417Sjulian	else
67222417Sjulian		drop
68222417Sjulian	then
69222417Sjulian
70222417Sjulian	\ Loop until we have exceeded the desired time duration
71222417Sjulian	begin
72222417Sjulian		25 ms \ sleep for 25 milliseconds (40 iterations/sec)
73222417Sjulian
74222417Sjulian		\ throw some dots up on the screen if desired
75222417Sjulian		delay_showdots @ if
76222417Sjulian			." ." \ dots visually aid in the perception of time
77222417Sjulian		then
78222417Sjulian
79222417Sjulian		\ was a key depressed?
80222417Sjulian		key? if
81222417Sjulian			key \ obtain ASCII value for keystroke
82222417Sjulian			dup enter_key = if
83222417Sjulian				-1 delay_delay ! \ break loop
84222417Sjulian			then
85222417Sjulian			dup etx_key = swap esc_key = OR if
86222417Sjulian				-1 delay_delay !     \ break loop
87222417Sjulian				-1 delay_cancelled ! \ set cancelled flag
88222417Sjulian			then
89222417Sjulian		then
90222417Sjulian
91222417Sjulian		\ if the time duration is set to zero, loop forever
92222417Sjulian		\ waiting for either ENTER or Ctrl-C/Escape to be pressed
93222417Sjulian		delay_delay @ 0> if
94222417Sjulian			\ calculate elapsed time
95222417Sjulian			seconds delay_tstart @ - delay_delay @ >
96222417Sjulian		else
97222417Sjulian			-1 \ break loop
98222417Sjulian		then
99222417Sjulian	until
100222417Sjulian
101222417Sjulian	\ if we were throwing up dots, throw up a line-break
102222417Sjulian	delay_showdots @ if
103222417Sjulian		cr
104222417Sjulian	then
105222417Sjulian
106222417Sjulian	\ did the user press either Ctrl-C or Escape?
107222417Sjulian	delay_cancelled @ if
108222417Sjulian		2drop \ we don't need the command string anymore
109222417Sjulian	else
110222417Sjulian		evaluate \ evaluate/execute the command string
111222417Sjulian 	then
112222417Sjulian;
113