]> git.netwichtig.de Git - user/henk/code/snooze.git/commitdiff
duration parsing
authorChristian Neukirchen <chneukirchen@gmail.com>
Wed, 4 Nov 2015 16:20:30 +0000 (17:20 +0100)
committerChristian Neukirchen <chneukirchen@gmail.com>
Wed, 4 Nov 2015 16:20:30 +0000 (17:20 +0100)
README.md
snooze.c

index 7b1bd20f3ab81ffeafda5372f9aef2a615f9891d..d5e86d1ad6961052e25ad77647eef559252db9a0 100644 (file)
--- a/README.md
+++ b/README.md
@@ -61,6 +61,9 @@ Over uschedule:
 * `-R`: add between 0 and RANDDELAY seconds to the scheduled time.
 * `-s`: commands are executed even if they are SLACK (default: 60) seconds late.
 
+The durations RANDDELAY and SLACK and TIMEWAIT are parsed as seconds,
+unless a postfix of `m` for minutes, `h` for hours, or `d` for days is used.
+
 The remaining arguments are patterns for the time fields:
 
 * `-d`: day of month
@@ -99,7 +102,7 @@ of TIMEFILE plus TIMEWAIT seconds.
 
 When `-T` is *not* passed, snooze will start finding the first matching time
 starting from the mtime of TIMEFILE, and taking SLACK into account.
-(E.g. `-H0 -s$((24*60*60)) -t timefile` will start an instant
+(E.g. `-H0 -s 1d -t timefile` will start an instant
 execution when timefile has not been touched today, whereas without `-t`
 this would always wait until next midnight.)
 
@@ -136,15 +139,15 @@ Run a job like cron, every day at 7am and 7pm:
 
 Run a job daily, never twice a day:
 
-       exec snooze -H0 -s $((24*60*60)) -t timefile \
+       exec snooze -H0 -s 1d -t timefile \
                sh -c 'run-parts /etc/cron.daily; touch timefile'
 
 Use snooze inline, run a mirror script every hour at 30 minutes past,
 but ensure there are at least 20 minutes in between.
 
        set -e
-       snooze -H'*' -M30 -t timefile -T $((20*60))
-       touch timefile  # remove this if instantly retrying on failure is ok
+       snooze -H'*' -M30 -t timefile -T 20m
+       touch timefile  # remove this if instantly retrying on failure were ok
        mirrorallthethings
        touch timefile
 
index a5df96767c541106a85deec9494c062ca0dd691a..d7dd67e0ccd221c1788b80cded3a4a70a7beeff0 100644 (file)
--- a/snooze.c
+++ b/snooze.c
@@ -64,6 +64,34 @@ parse_int(char **s, size_t minn, size_t maxn)
        return n;
 }
 
+static long
+parse_dur(char *s)
+{
+       long n;
+       char *end;
+
+       errno = 0;
+       n = strtol(s, &end, 10);
+       if (errno) {
+               perror("strtol");
+               exit(1);
+       }
+       if (n < 0) {
+               fprintf(stderr, "negative duration\n");
+               exit(1);
+       }
+       switch (*end) {
+       case 'm': n *= 60; break;
+       case 'h': n *= 60*60; break;
+       case 'd': n *= 24*60*60; break;
+       case 0: break;
+       default:
+               fprintf(stderr, "junk after duration: %s\n", end);
+               exit(1);
+       }
+       return n;
+}
+
 static int
 parse(char *expr, char *buf, long bufsiz, int offset)
 {
@@ -229,10 +257,10 @@ int main(int argc, char *argv[])
                        break;
                case 'n': nflag++; break;
                case 'v': vflag++; break;
-               case 's': slack = atoi(optarg); break;
-               case 'T': timewait = atoi(optarg); break;
+               case 's': slack = parse_dur(optarg); break;
+               case 'T': timewait = parse_dur(optarg); break;
                case 't': timefile = optarg; break;
-               case 'R': randdelay = atoi(optarg); break;
+               case 'R': randdelay = parse_dur(optarg); break;
                default:
                         fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
                            "  [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"