schedule

(1)

// 例1
+0:00:05 echo hello.
+0:00:08 echo abc
// 頭に+をつけるとscheduleが起動した時刻を起点にした相対指定になる.
// 例2
09:00:00 echo 9:00
12:00:00 echo 12:00
// こちらは絶対指定.

(2) ver.1.00 [2024.09.15] (105行)

#include <acl1Tiny.c>
#include <AMemMan.c>
#include <AMemFile.c>
#include <aOsFunc.c>

AInlineStatic int hms2t(int h, int m, int s) { return ((h * 60 + m) * 60) + s; } // 00:00:00からの秒数.

char *t2str(char *st, int t)
{
    int s = t % 60; t /= 60;
    int m = t % 60; t /= 60;
    sprintf(st, "%02d:%02d:%02d", t, m, s);
    return st;
}

int getTimNow()
{
    time_t t0 = time(0);
    struct tm tm0;
    localtime_s(&tm0, &t0);
    return hms2t(tm0.tm_hour, tm0.tm_min, tm0.tm_sec);
}

char *skpSpc(const char *p)
{
    while (*p == ' ' || *p == '\t') p++;
    return (char *) p;
}

AInt delLastSpcCrlf(char *s)
{
    AInt l = strlen(s);
    while (l > 0 && s[l - 1] <= ' ') s[--l] = 0;
    return l; // == strlen(s)
}

char *skpLf(const char *s)
// 次の行の先頭を得る.
{
    while (*s != 0 && *s != '\n') s++;
    if (*s == '\n') s++;
    return (char *) s;
}

AClass(Schedule) {
    int t;
    char *cmdlin;
};

int main(int argc, const char **argv)
{
    Schedule *sc = 0;
    char *p = (char *) "schedule.txt", *p1, s[16]; // 引数なしで起動すると "scadule.txt"を開く.
    if (argc >= 2) p = (char *) argv[1];
    AMemFile *mp = AMemFile_open(AMemFile_Text, am0, p);
    int n = 0, i, d, hh, mm, ss, t0 = getTimNow(), t;
    for (p = mp->p0;; p = p1) {
        p1 = skpLf(p);
        if (p >= p1) break;
        p = skpSpc(p);
        if (*p == '/') continue;
        hh = mm = ss = -1;
        t = 0;
        if (*p == '+') {
            p = skpSpc(p + 1);
            t = t0;
        }
        sscanf(p, "%d:%d:%d", &hh, &mm, &ss);
        t = (t + hms2t(hh, mm, ss)) % (24 * 60 * 60);
        while (*p > ' ') p++;
        p = skpSpc(p);
        i = (int) (p1 - p);
        char *q = AM_alc(am0, 1, i + 1);
        memcpy(q, p, i);
        q[i] = 0;
        i = (int) delLastSpcCrlf(q);
        if (ss < 0 || i <= 1) continue;
        sc = AM_rlc(am0, 0, sc, n * sizeof (Schedule), (n + 1) * sizeof (Schedule));
        sc[n].t = t;
        sc[n++].cmdlin = q;
    }
    AMemFile_dst(mp);
    for (i = 0; i < n; i++)
        printf("[%s] %s\n", t2str(s, sc[i].t), sc[i].cmdlin);
    printf("\n");

    for (i = 0; i < n; i++) {
        for (;;) {
            t0 = getTimNow();
            printf("    (%s) \r", t2str(s, t0));
            d = (sc[i].t - t0 + 24 * 60 * 60) % (24 * 60 * 60);
            if (d == 0) break;
            if (d > 3)
                aSleepMs(1000);
            else
                aSleepMs(20);
        }
        printf("[%s] %s         \n", t2str(s, sc[i].t), sc[i].cmdlin);
        d = system(sc[i].cmdlin);
        if (d != 0) { printf("error-code=%d\n", d); return d; }
    }
    AM_fre(am0, sc, n * sizeof (Schedule));
    AM_dst(am0);
    return 0;
}

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS