-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.awk
94 lines (81 loc) · 2.41 KB
/
alarm.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# alarm.awk --- set an alarm
#
# Requires getlocaltime() library function
#
# Arnold Robbins, arnold@skeeve.com, Public Domain
# May 1993
# Revised December 2010
# usage: alarm time [ "message" [ count [ delay ] ] ]
@include "gettime"
BEGIN {
# Initial argument sanity checking
usage1 = "usage: alarm time ['message' [count [delay]]]"
usage2 = sprintf("\t(%s) time ::= hh:mm", ARGV[1])
if (ARGC < 2) {
print usage1 > "/dev/stderr"
print usage2 > "/dev/stderr"
exit 1
}
switch (ARGC) {
case 5:
delay = ARGV[4] + 0
# fall through
case 4:
count = ARGV[3] + 0
# fall through
case 3:
message = ARGV[2]
# "break" from original code doesn't work
# fall through
default:
if (ARGV[1] !~ /^[[:digit:]]{1,2}:[[:digit:]]{2}$/) {
print usage1 > "/dev/stderr"
print usage2 > "/dev/stderr"
exit 1
}
break
}
# set defaults for once we reach the desired time
if (delay == 0)
delay = 180 # 3 minutes
if (count == 0)
count = 5
if (message == "")
message = sprintf("\aIt is now %s!\a", ARGV[1])
else if (index(message, "\a") == 0)
message = "\a" message "\a"
# split up alarm time
split(ARGV[1], atime, ":")
hour = atime[1] + 0 # force numeric
minute = atime[2] + 0 # force numeric
# get current broken down time
getlocaltime(now)
# if time given is 12-hour hours and it's after that
# hour, e.g., `alarm 5:30' at 9 a.m. means 5:30 p.m.,
# then add 12 to real hour
if (hour < 12 && now["hour"] > hour)
hour += 12
# set target time in seconds since midnight
target = (hour * 60 * 60) + (minute * 60)
# get current time in seconds since midnight
current = (now["hour"] * 60 * 60) + \
(now["minute"] * 60) + now["second"]
# how long to sleep for
naptime = target - current
if (naptime <= 0) {
print "alarm: time is in the past!" > "/dev/stderr"
exit 1
}
# zzzzzz..... go away if interrupted
if (system(sprintf("sleep %d && exit 1", naptime)) == 0)
exit 1
# time to notify!
command = sprintf("sleep %d && exit 1", delay)
for (i = 1; i <= count; i++) {
print message
# if sleep command interrupted, go away
if (system(command) == 0)
break
}
exit 0
}