source: vendor/gawk/3.1.5/awklib/eg/prog/alarm.awk@ 3076

Last change on this file since 3076 was 3076, checked in by bird, 19 years ago

gawk 3.1.5

File size: 2.3 KB
Line 
1# alarm.awk --- set an alarm
2#
3# Requires gettimeofday library function
4#
5# Arnold Robbins, [email protected], Public Domain
6# May 1993
7
8# usage: alarm time [ "message" [ count [ delay ] ] ]
9
10BEGIN \
11{
12 # Initial argument sanity checking
13 usage1 = "usage: alarm time ['message' [count [delay]]]"
14 usage2 = sprintf("\t(%s) time ::= hh:mm", ARGV[1])
15
16 if (ARGC < 2) {
17 print usage1 > "/dev/stderr"
18 print usage2 > "/dev/stderr"
19 exit 1
20 } else if (ARGC == 5) {
21 delay = ARGV[4] + 0
22 count = ARGV[3] + 0
23 message = ARGV[2]
24 } else if (ARGC == 4) {
25 count = ARGV[3] + 0
26 message = ARGV[2]
27 } else if (ARGC == 3) {
28 message = ARGV[2]
29 } else if (ARGV[1] !~ /[0-9]?[0-9]:[0-9][0-9]/) {
30 print usage1 > "/dev/stderr"
31 print usage2 > "/dev/stderr"
32 exit 1
33 }
34
35 # set defaults for once we reach the desired time
36 if (delay == 0)
37 delay = 180 # 3 minutes
38 if (count == 0)
39 count = 5
40 if (message == "")
41 message = sprintf("\aIt is now %s!\a", ARGV[1])
42 else if (index(message, "\a") == 0)
43 message = "\a" message "\a"
44 # split up alarm time
45 split(ARGV[1], atime, ":")
46 hour = atime[1] + 0 # force numeric
47 minute = atime[2] + 0 # force numeric
48
49 # get current broken down time
50 gettimeofday(now)
51
52 # if time given is 12-hour hours and it's after that
53 # hour, e.g., `alarm 5:30' at 9 a.m. means 5:30 p.m.,
54 # then add 12 to real hour
55 if (hour < 12 && now["hour"] > hour)
56 hour += 12
57
58 # set target time in seconds since midnight
59 target = (hour * 60 * 60) + (minute * 60)
60
61 # get current time in seconds since midnight
62 current = (now["hour"] * 60 * 60) + \
63 (now["minute"] * 60) + now["second"]
64
65 # how long to sleep for
66 naptime = target - current
67 if (naptime <= 0) {
68 print "time is in the past!" > "/dev/stderr"
69 exit 1
70 }
71 # zzzzzz..... go away if interrupted
72 if (system(sprintf("sleep %d", naptime)) != 0)
73 exit 1
74
75 # time to notify!
76 command = sprintf("sleep %d", delay)
77 for (i = 1; i <= count; i++) {
78 print message
79 # if sleep command interrupted, go away
80 if (system(command) != 0)
81 break
82 }
83
84 exit 0
85}
Note: See TracBrowser for help on using the repository browser.