xref: /freebsd/stand/forth/delay.4th (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1\ Copyright (c) 2008-2015 Devin Teske <dteske@FreeBSD.org>
2\ All rights reserved.
3\
4\ Redistribution and use in source and binary forms, with or without
5\ modification, are permitted provided that the following conditions
6\ are met:
7\ 1. Redistributions of source code must retain the above copyright
8\    notice, this list of conditions and the following disclaimer.
9\ 2. Redistributions in binary form must reproduce the above copyright
10\    notice, this list of conditions and the following disclaimer in the
11\    documentation and/or other materials provided with the distribution.
12\
13\ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16\ ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17\ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18\ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19\ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20\ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21\ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23\ SUCH DAMAGE.
24\
25
26marker task-delay.4th
27
28vocabulary delay-processing
29only forth also delay-processing definitions
30
312  constant delay_default \ Default delay (in seconds)
323  constant etx_key       \ End-of-Text character produced by Ctrl+C
3313 constant enter_key     \ Carriage-Return character produce by ENTER
3427 constant esc_key       \ Escape character produced by ESC or Ctrl+[
35
36variable delay_tstart     \ state variable used for delay timing
37variable delay_delay      \ determined configurable delay duration
38variable delay_cancelled  \ state variable for user cancellation
39variable delay_showdots   \ whether continually print dots while waiting
40
41only forth definitions also delay-processing
42
43: delay_execute ( -- )
44
45	\ make sure that we have a command to execute
46	s" delay_command" getenv dup -1 = if
47		drop exit
48	then
49
50	\ read custom time-duration (if set)
51	s" loader_delay" getenv dup -1 = if
52		drop          \ no custom duration (remove dup'd bunk -1)
53		delay_default \ use default setting (replacing bunk -1)
54	else
55		\ make sure custom duration is a number
56		?number 0= if
57			delay_default \ use default if otherwise
58		then
59	then
60
61	\ initialize state variables
62	delay_delay !          \ stored value is on the stack from above
63	seconds delay_tstart ! \ store the time we started
64	0 delay_cancelled !    \ boolean flag indicating user-cancelled event
65
66	false delay_showdots ! \ reset to zero and read from environment
67	s" delay_showdots" getenv dup -1 <> if
68		2drop \ don't need the value, just existence
69		true delay_showdots !
70	else
71		drop
72	then
73
74	\ Loop until we have exceeded the desired time duration
75	begin
76		25 ms \ sleep for 25 milliseconds (40 iterations/sec)
77
78		\ throw some dots up on the screen if desired
79		delay_showdots @ if
80			." ." \ dots visually aid in the perception of time
81		then
82
83		\ was a key depressed?
84		key? if
85			key \ obtain ASCII value for keystroke
86			dup enter_key = if
87				-1 delay_delay ! \ break loop
88			then
89			dup etx_key = swap esc_key = OR if
90				-1 delay_delay !     \ break loop
91				-1 delay_cancelled ! \ set cancelled flag
92			then
93		then
94
95		\ if the time duration is set to zero, loop forever
96		\ waiting for either ENTER or Ctrl-C/Escape to be pressed
97		delay_delay @ 0> if
98			\ calculate elapsed time
99			seconds delay_tstart @ - delay_delay @ >
100		else
101			-1 \ break loop
102		then
103	until
104
105	\ if we were throwing up dots, throw up a line-break
106	delay_showdots @ if
107		cr
108	then
109
110	\ did the user press either Ctrl-C or Escape?
111	delay_cancelled @ if
112		2drop \ we don't need the command string anymore
113	else
114		evaluate \ evaluate/execute the command string
115 	then
116;
117
118only forth definitions
119