/sys/boot/forth/delay.4th

https://bitbucket.org/iorivur/freebsd-bhyve-with-suspend-resume · Forth · 112 lines · 98 code · 14 blank · 0 comment · 10 complexity · 2ad4365c9010afaf82a61813bb051c7d MD5 · raw file

  1. \ Copyright (c) 2008-2011 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. \ $FreeBSD: head/sys/boot/forth/delay.4th 238431 2012-07-14 01:45:35Z dteske $
  26. marker task-delay.4th
  27. 2 constant delay_default \ Default delay (in seconds)
  28. 3 constant etx_key \ End-of-Text character produced by Ctrl+C
  29. 13 constant enter_key \ Carriage-Return character produce by ENTER
  30. 27 constant esc_key \ Escape character produced by ESC or Ctrl+[
  31. variable delay_tstart \ state variable used for delay timing
  32. variable delay_delay \ determined configurable delay duration
  33. variable delay_cancelled \ state variable for user cancellation
  34. variable delay_showdots \ whether continually print dots while waiting
  35. : delay_execute ( -- )
  36. \ make sure that we have a command to execute
  37. s" delay_command" getenv dup -1 = if
  38. drop exit
  39. then
  40. \ read custom time-duration (if set)
  41. s" loader_delay" getenv dup -1 = if
  42. drop \ no custom duration (remove dup'd bunk -1)
  43. delay_default \ use default setting (replacing bunk -1)
  44. else
  45. \ make sure custom duration is a number
  46. ?number 0= if
  47. delay_default \ use default if otherwise
  48. then
  49. then
  50. \ initialize state variables
  51. delay_delay ! \ stored value is on the stack from above
  52. seconds delay_tstart ! \ store the time we started
  53. 0 delay_cancelled ! \ boolean flag indicating user-cancelled event
  54. false delay_showdots ! \ reset to zero and read from environment
  55. s" delay_showdots" getenv dup -1 <> if
  56. 2drop \ don't need the value, just existance
  57. true delay_showdots !
  58. else
  59. drop
  60. then
  61. \ Loop until we have exceeded the desired time duration
  62. begin
  63. 25 ms \ sleep for 25 milliseconds (40 iterations/sec)
  64. \ throw some dots up on the screen if desired
  65. delay_showdots @ if
  66. ." ." \ dots visually aid in the perception of time
  67. then
  68. \ was a key depressed?
  69. key? if
  70. key \ obtain ASCII value for keystroke
  71. dup enter_key = if
  72. -1 delay_delay ! \ break loop
  73. then
  74. dup etx_key = swap esc_key = OR if
  75. -1 delay_delay ! \ break loop
  76. -1 delay_cancelled ! \ set cancelled flag
  77. then
  78. then
  79. \ if the time duration is set to zero, loop forever
  80. \ waiting for either ENTER or Ctrl-C/Escape to be pressed
  81. delay_delay @ 0> if
  82. \ calculate elapsed time
  83. seconds delay_tstart @ - delay_delay @ >
  84. else
  85. -1 \ break loop
  86. then
  87. until
  88. \ if we were throwing up dots, throw up a line-break
  89. delay_showdots @ if
  90. cr
  91. then
  92. \ did the user press either Ctrl-C or Escape?
  93. delay_cancelled @ if
  94. 2drop \ we don't need the command string anymore
  95. else
  96. evaluate \ evaluate/execute the command string
  97. then
  98. ;