Debian Codegraph @

sw.sh

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`.
2 # sw.sh
3 # A command-line Stopwatch
4
5 # Author: Pádraig Brady
6 # http://www.pixelbeat.org/scripts/sw
7 # (Minor reformatting by ABS Guide author.)
8 # Used in ABS Guide with script author's permission.
9 # Notes:
10 # This script starts a few processes per lap, in addition to
11 # the shell loop processing, so the assumption is made that
12 # this takes an insignificant amount of time compared to
13 # the response time of humans (~.1s) (or the keyboard
14 # interrupt rate (~.05s)).
15 # '?' for splits must be entered twice if characters
16 # (erroneously) entered before it (on the same line).
17 # '?' since not generating a signal may be slightly delayed
18 # on heavily loaded systems.
19 # Lap timings on ubuntu may be slightly delayed due to:
20 # https://bugs.launchpad.net/bugs/62511
21 # Changes:
22 # V1.0, 23 Aug 2005, Initial release
23 # V1.1, 26 Jul 2007, Allow both splits and laps from single invocation.
24 # Only start timer after a key is pressed.
25 # Indicate lap number
26 # Cache programs at startup so there is less error
27 # due to startup delays.
28 # V1.2, 01 Aug 2007, Work around `date` commands that don't have
29 # nanoseconds.
30 # Use stty to change interrupt keys to space for
31 # laps etc.
32 # Ignore other input as it causes problems.
33 # V1.3, 01 Aug 2007, Testing release.
34 # V1.4, 02 Aug 2007, Various tweaks to get working under ubuntu
35 # and Mac OS X.
36 # V1.5, 27 Jun 2008, set LANG=C as got vague bug report about it.
37
38 export LANG=C
39
40 ulimit -c 0 # No coredumps from SIGQUIT.
41 trap '' TSTP # Ignore Ctrl-Z just in case.
42 save_tty=`sttyscip-shell system . . stty. -g` && trap "stty $save_tty" EXIT # Restore tty on exit.
43 sttyscip-shell system . . stty. quit ' ' # Space for laps rather than Ctrl-\.
44 sttyscip-shell system . . stty. eof '?' # ? for splits rather than Ctrl-D.
45 sttyscip-shell system . . stty. -echo # Don't echo input.
46
47 cache_progsscip-shell shell abs-guide 10-4 cache_progs().() {
48 sttyscip-shell system . . stty. > /dev/nullscip-shell filesystem . . `/dev/null`.
49 datescip-shell system . . date. > /dev/nullscip-shell filesystem . . `/dev/null`.
50 grepscip-shell system . . grep. . < /dev/nullscip-shell filesystem . . `/dev/null`.
51 (echoscip-shell system . . echo. "import time" | python) 2> /dev/nullscip-shell filesystem . . `/dev/null`.
52 bc < /dev/nullscip-shell filesystem . . `/dev/null`.
53 sedscip-shell system . . sed. '' < /dev/nullscip-shell filesystem . . `/dev/null`.
54 printfscip-shell system . . printf. '1' > /dev/nullscip-shell filesystem . . `/dev/null`.
55 /usr/bin/timescip-shell filesystem . . `/usr/bin/time`. false 2> /dev/nullscip-shell filesystem . . `/dev/null`.
56 catscip-shell system . . cat. < /dev/nullscip-shell filesystem . . `/dev/null`.
57 }
58 cache_progsscip-shell shell abs-guide 10-4 cache_progs(). # To minimise startup delay.
59
60 datescip-shell system . . date. +%s.%N | grepscip-shell system . . grep. -qF 'N' && use_python=1 # If `date` lacks nanoseconds.
61 nowscip-shell shell abs-guide 10-4 now().() {
62 if [scip-shell system . . `[`. "$use_python" ]; then
63 echoscip-shell system . . echo. "import time; print time.time()" 2>/dev/nullscip-shell filesystem . . `/dev/null`. | python
64 else
65 printfscip-shell system . . printf. "%.2f" `datescip-shell system . . date. +%s.%N`
66 fi
67 }
68
69 fmt_secondsscip-shell shell abs-guide 10-4 fmt_seconds().() {
70 seconds=$1
71 mins=`echoscip-shell system . . echo. $seconds/60 | bc`
72 if [scip-shell system . . `[`. "$mins" != "0" ]; then
73 seconds=`echoscip-shell system . . echo. "$seconds - ($mins*60)" | bc`
74 echoscip-shell system . . echo. "$mins:$seconds"
75 else
76 echoscip-shell system . . echo. "$seconds"
77 fi
78 }
79
80 totalscip-shell shell abs-guide 10-4 total().() {
81 end=`nowscip-shell shell abs-guide 10-4 now().`
82 total=`echoscip-shell system . . echo. "$end - $start" | bc`
83 fmt_secondsscip-shell shell abs-guide 10-4 fmt_seconds(). $total
84 }
85
86 stopscip-shell shell abs-guide 10-4 stop().() {
87 [scip-shell system . . `[`. "$lapped" ] && lapscip-shell shell abs-guide 10-4 lap(). "$laptime" "display"
88 totalscip-shell shell abs-guide 10-4 total().
89 exit
90 }
91
92 lapscip-shell shell abs-guide 10-4 lap().() {
93 laptime=`echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. -n 's/.*real[^0-9.]*\(.*\)/\1/p'`
94 [scip-shell system . . `[`. ! "$laptime" -o "$laptime" = "0.00" ] && return
95 # Signals too frequent.
96 laptotal=`echoscip-shell system . . echo. $laptime+0$laptotal | bc`
97 if [scip-shell system . . `[`. "$2" = "display" ]; then
98 lapcount=`echoscip-shell system . . echo. 0$lapcount+1 | bc`
99 laptime=`fmt_secondsscip-shell shell abs-guide 10-4 fmt_seconds(). $laptotal`
100 echoscip-shell system . . echo. $laptime "($lapcount)"
101 lapped="true"
102 laptotal="0"
103 fi
104 }
105
106 echoscip-shell system . . echo. -n "Space for lap | ? for split | Ctrl-C to stop | Space to start...">&2
107
108 while truescip-shell system . . true.; do
109 trap true INT QUIT # Set signal handlers.
110 laptime=`/usr/bin/timescip-shell filesystem . . `/usr/bin/time`. -p 2>&1 cat >/dev/nullscip-shell filesystem . . `/dev/null`.`
111 ret=$?
112 trap '' INT QUIT # Ignore signals within this script.
113 if [scip-shell system . . `[`. $ret -eq 1 -o $ret -eq 2 -o $ret -eq 130 ]; then # SIGINT = stop
114 [scip-shell system . . `[`. ! "$start" ] && { echoscip-shell system . . echo. >&2; exit; }
115 stopscip-shell shell abs-guide 10-4 stop().
116 elif [scip-shell system . . `[`. $ret -eq 3 -o $ret -eq 131 ]; then # SIGQUIT = lap
117 if [scip-shell system . . `[`. ! "$start" ]; then
118 start=`nowscip-shell shell abs-guide 10-4 now().` || exit 1
119 echoscip-shell system . . echo. >&2
120 continue
121 fi
122 lapscip-shell shell abs-guide 10-4 lap(). "$laptime" "display"
123 else # eof = split
124 [scip-shell system . . `[`. ! "$start" ] && continue
125 totalscip-shell shell abs-guide 10-4 total().
126 lapscip-shell shell abs-guide 10-4 lap(). "$laptime" # Update laptotal.
127 fi
128 done
129
130 exit $?