home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / awklib / eg / prog / alarm.awk next >
Text File  |  1997-03-15  |  2KB  |  82 lines

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