From db13c085c8b48bb721df723ef93d90ab6e88ec24 Mon Sep 17 00:00:00 2001 From: DTB Date: Thu, 27 Mar 2025 02:34:39 -0600 Subject: [PATCH] better separation of modules --- status/.gitignore | 4 ++++ status/README | 2 +- status/io.c | 1 + status/io.h | 1 + status/keyval.c | 4 ++++ status/keyval.h | 2 ++ status/mod_static.c | 5 ++++ status/mod_time.c | 35 ++++++++++++++++++++++++++++ status/order.def.c | 18 +++++++------- status/status.c | 57 ++++++++++++--------------------------------- 10 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 status/.gitignore create mode 100644 status/mod_static.c create mode 100644 status/mod_time.c diff --git a/status/.gitignore b/status/.gitignore new file mode 100644 index 0000000..3ec8aea --- /dev/null +++ b/status/.gitignore @@ -0,0 +1,4 @@ +*.o +order.c +settings.c +status diff --git a/status/README b/status/README index 2881d41..7efbb8d 100644 --- a/status/README +++ b/status/README @@ -1,6 +1,6 @@ __ ___ __ ___ _ _ __ / _// // // // // // _/ STATUS _'. / // _ // / / // /_'. how's your day going? -/__/ /_//_//_//_/ /__/ /__/ DTB 2025. Public Domain. +/__/ /_//_//_//_/ /__/ /__/ 2025 DTB. Public Domain. build: $ make diff --git a/status/io.c b/status/io.c index d8151dd..791be96 100644 --- a/status/io.c +++ b/status/io.c @@ -1,3 +1,4 @@ +/* 2025 DTB. Public Domain. */ #undef NDEBUG /* LOAD-BEARING ASSERTS! */ #include /* assert(3) */ #include /* errno */ diff --git a/status/io.h b/status/io.h index 7554589..d744541 100644 --- a/status/io.h +++ b/status/io.h @@ -1,2 +1,3 @@ +/* 2025 DTB. Public Domain. */ void drop(void); /* Set the buffer to NULL. */ void out(char *s); /* Print the buffer if not NULL and replace it with s. */ diff --git a/status/keyval.c b/status/keyval.c index 4c5c6ef..b4e2097 100644 --- a/status/keyval.c +++ b/status/keyval.c @@ -2,6 +2,7 @@ #undef NDEBUG /* LOAD-BEARING ASSERTS! */ #include /* assert(3) */ +#include /* atoi(3) */ #include /* strcmp(3) */ #include "keyval.h" @@ -27,6 +28,9 @@ del(char *key) { char * get(char *key) { return state.settings[idx(key)].val; } +int +getd(char *key) { return atoi(get(key)); } + size_t idx(char *key) { size_t r; diff --git a/status/keyval.h b/status/keyval.h index fba2276..d73a78a 100644 --- a/status/keyval.h +++ b/status/keyval.h @@ -13,6 +13,8 @@ struct State { void cpy(char *key); /* Duplicate the setting key. */ void del(char *key); /* Delete the first instance of key in settings. */ char * get(char *key); /* Get the corresponding val to key in settings. */ +int getd(char *key); /* get() that returns a decimal number if key was a + * number, or 0. */ void keyval_init(void); /* Sanitize the settings store. */ size_t idx(char *key); /* Returns the index of key if in settings, or * state.setting_last. */ diff --git a/status/mod_static.c b/status/mod_static.c new file mode 100644 index 0000000..4b04de6 --- /dev/null +++ b/status/mod_static.c @@ -0,0 +1,5 @@ +// #include "io.h" /* out(3) */ +// #include "keyval.h" /* get(3) */ + +void +mod_static(char *s) { out(s); out(get("separator")); } diff --git a/status/mod_time.c b/status/mod_time.c new file mode 100644 index 0000000..925f8c1 --- /dev/null +++ b/status/mod_time.c @@ -0,0 +1,35 @@ +/* 2025 DTB. Public Domain. */ +// #undef NDEBUG /* LOAD BEARING ASSERTS */ +// #include /* assert(3) */ +// #include /* setenv(3), unsetenv(3), NULL */ +// #include /* localtime(3), strftime(3), time(2), tzset(3), time_t, +// * struct tm */ +// #include "io.h" /* out(3) */ +// #include "keyval.h" /* get(3) */ +#ifndef mod_time_bufsize +# define mod_time_bufsize 100 +#endif + +void +set_timezone(char *zone) { + if (zone == NULL) { assert(unsetenv("TZ") == 0); } + else { assert(setenv("TZ", zone, 1) == 0); } + tzset(); +} + +void +mod_time(char *fmt) { + static char buf[mod_time_bufsize]; + struct tm *tm; + static time_t t = 0; + static tick_t last_tick = 0; + + if (ticker == 0) { t = time(NULL); } + else if (ticker != last_tick) { t += slept; } + last_tick = ticker; + assert((tm = localtime(&t)) != NULL); + assert(strftime(buf, sizeof(buf) / sizeof(*buf), fmt, tm) != 0); + + out(buf); + out(get("separator")); +} diff --git a/status/order.def.c b/status/order.def.c index 8afdc68..f42fd92 100644 --- a/status/order.def.c +++ b/status/order.def.c @@ -21,11 +21,11 @@ * out(s) holds s and prints what it was previously holding, if it wasn't NULL * drop() sets out()'s held value to NULL */ -/* Status - * status_static(s) outputs s and then the separator val, unless - * status_static() is called as the last function in order - * status_time(fmt) outputs the time for the current time zone in the - * strftime(3) fmt */ +/* Modules + * mod_static(s) outputs s and then the separator val, unless mod_static() is + * called as the last function in order + * mod_time(fmt) outputs the time for the current time zone in the strftime(3) + * fmt */ void order() { /* there are a couple good ways to print without the separator. the @@ -34,17 +34,17 @@ order() { /* but another way is to use status_static to write a message and * separator, then use drop() to recall the last thing written (always * the separator in status_* functions) */ - status_static(" "); drop(); /* => " " */ + mod_static(" "); drop(); /* => " " */ set_timezone(""); - status_time("%Y-%m-%dT%H:%M"); /* => "2025-03-27T00:00 // " */ + mod_time("%Y-%m-%dT%H:%M"); /* => "2025-03-27T00:00 // " */ /* and another way is to use the weird key-val system */ cpy("separator"); /* duplicate setting */ set("separator", " "); /* overwrite first setting */ set_timezone(NULL); - status_static("local"); /* use first setting */ + mod_static("local"); /* use first setting */ del("separator"); /* delete the first, restoring the second */ - status_time("%Y-%m-%dT%H:%M"); + mod_time("%Y-%m-%dT%H:%M"); drop(); /* => "local 2025-03-26T18:00" */ diff --git a/status/status.c b/status/status.c index fe144af..3e0a2c5 100644 --- a/status/status.c +++ b/status/status.c @@ -1,59 +1,32 @@ #undef NDEBUG /* LOAD-BEARING ASSERTS! */ -#include /* assert(3) */ +#include /* "mod_time.c" */ #include /* UCHAR_MAX */ -#include /* fputs(3), stdout, NULL */ -#include /* atoi(3) */ -#include /* localtime(3), strftime(3), time(2), time_t, struct tm */ +#include /* "mod_time.c" */ +#include /* "mod_time.c" */ #include /* sleep(3) */ -#include "io.h" /* drop(3), out(3) */ -#include "keyval.h" /* cpy(3), del(3), get(3), keyval_init(3), set(3) */ +#include "io.h" /* drop(3), out(3), "mod_static.c" */ +#include "keyval.h" /* cpy(3), del(3), get(3), keyval_init(3), set(3), + * "mod_static.c" */ + +#define ever (;;) + +unsigned int slept = 0; typedef unsigned char tick_t; #define TICK_MAX UCHAR_MAX - -unsigned int slept = 0; tick_t ticker = 0; -void -set_timezone(char *zone) { - if (zone == NULL) { assert(unsetenv("TZ") == 0); } - else { assert(setenv("TZ", zone, 1) == 0); } - tzset(); -} - -void -status_static(char *s) { out(s); out(get("separator")); } - -#ifndef status_time_bufsize -# define status_time_bufsize 100 -#endif -void -status_time(char *fmt) { - static char buf[status_time_bufsize]; - struct tm *tm; - static time_t t = 0; - static tick_t last_tick = 0; - - if (ticker == 0) { t = time(NULL); } - else if (ticker != last_tick) { t += slept; } - last_tick = ticker; - assert((tm = localtime(&t)) != NULL); - assert(strftime(buf, sizeof(buf) / sizeof(*buf), fmt, tm) != 0); - - out(buf); - out(get("separator")); -} +#include "mod_static.c" +#include "mod_time.c" #include "order.c" int main(int argc, char *argv) { keyval_init(); - while(1) { - order(); - drop(); - out("\n"); - slept = sleep(atoi(get("interval_seconds"))); + for ever { + order(); drop(); out("\n"); + slept = sleep(getd("interval_seconds")); ticker = ++ticker % TICK_MAX; } }