better separation of modules
This commit is contained in:
parent
591f58d70a
commit
db13c085c8
4
status/.gitignore
vendored
Normal file
4
status/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
*.o
|
||||||
|
order.c
|
||||||
|
settings.c
|
||||||
|
status
|
@ -1,6 +1,6 @@
|
|||||||
__ ___ __ ___ _ _ __
|
__ ___ __ ___ _ _ __
|
||||||
/ _// // // // // // _/ STATUS
|
/ _// // // // // // _/ STATUS
|
||||||
_'. / // _ // / / // /_'. how's your day going?
|
_'. / // _ // / / // /_'. how's your day going?
|
||||||
/__/ /_//_//_//_/ /__/ /__/ DTB 2025. Public Domain.
|
/__/ /_//_//_//_/ /__/ /__/ 2025 DTB. Public Domain.
|
||||||
|
|
||||||
build: $ make
|
build: $ make
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* 2025 DTB. Public Domain. */
|
||||||
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
||||||
#include <assert.h> /* assert(3) */
|
#include <assert.h> /* assert(3) */
|
||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
|
/* 2025 DTB. Public Domain. */
|
||||||
void drop(void); /* Set the buffer to NULL. */
|
void drop(void); /* Set the buffer to NULL. */
|
||||||
void out(char *s); /* Print the buffer if not NULL and replace it with s. */
|
void out(char *s); /* Print the buffer if not NULL and replace it with s. */
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
||||||
#include <assert.h> /* assert(3) */
|
#include <assert.h> /* assert(3) */
|
||||||
|
#include <stdlib.h> /* atoi(3) */
|
||||||
#include <string.h> /* strcmp(3) */
|
#include <string.h> /* strcmp(3) */
|
||||||
#include "keyval.h"
|
#include "keyval.h"
|
||||||
|
|
||||||
@ -27,6 +28,9 @@ del(char *key) {
|
|||||||
char *
|
char *
|
||||||
get(char *key) { return state.settings[idx(key)].val; }
|
get(char *key) { return state.settings[idx(key)].val; }
|
||||||
|
|
||||||
|
int
|
||||||
|
getd(char *key) { return atoi(get(key)); }
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
idx(char *key) {
|
idx(char *key) {
|
||||||
size_t r;
|
size_t r;
|
||||||
|
@ -13,6 +13,8 @@ struct State {
|
|||||||
void cpy(char *key); /* Duplicate the setting key. */
|
void cpy(char *key); /* Duplicate the setting key. */
|
||||||
void del(char *key); /* Delete the first instance of key in settings. */
|
void del(char *key); /* Delete the first instance of key in settings. */
|
||||||
char * get(char *key); /* Get the corresponding val to 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. */
|
void keyval_init(void); /* Sanitize the settings store. */
|
||||||
size_t idx(char *key); /* Returns the index of key if in settings, or
|
size_t idx(char *key); /* Returns the index of key if in settings, or
|
||||||
* state.setting_last. */
|
* state.setting_last. */
|
||||||
|
5
status/mod_static.c
Normal file
5
status/mod_static.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// #include "io.h" /* out(3) */
|
||||||
|
// #include "keyval.h" /* get(3) */
|
||||||
|
|
||||||
|
void
|
||||||
|
mod_static(char *s) { out(s); out(get("separator")); }
|
35
status/mod_time.c
Normal file
35
status/mod_time.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* 2025 DTB. Public Domain. */
|
||||||
|
// #undef NDEBUG /* LOAD BEARING ASSERTS */
|
||||||
|
// #include <assert.h> /* assert(3) */
|
||||||
|
// #include <stdlib.h> /* setenv(3), unsetenv(3), NULL */
|
||||||
|
// #include <time.h> /* 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"));
|
||||||
|
}
|
@ -21,11 +21,11 @@
|
|||||||
* out(s) holds s and prints what it was previously holding, if it wasn't NULL
|
* out(s) holds s and prints what it was previously holding, if it wasn't NULL
|
||||||
* drop() sets out()'s held value to NULL */
|
* drop() sets out()'s held value to NULL */
|
||||||
|
|
||||||
/* Status
|
/* Modules
|
||||||
* status_static(s) outputs s and then the separator val, unless
|
* mod_static(s) outputs s and then the separator val, unless mod_static() is
|
||||||
* status_static() is called as the last function in order
|
* called as the last function in order
|
||||||
* status_time(fmt) outputs the time for the current time zone in the
|
* mod_time(fmt) outputs the time for the current time zone in the strftime(3)
|
||||||
* strftime(3) fmt */
|
* fmt */
|
||||||
void
|
void
|
||||||
order() {
|
order() {
|
||||||
/* there are a couple good ways to print without the separator. the
|
/* 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
|
/* but another way is to use status_static to write a message and
|
||||||
* separator, then use drop() to recall the last thing written (always
|
* separator, then use drop() to recall the last thing written (always
|
||||||
* the separator in status_* functions) */
|
* the separator in status_* functions) */
|
||||||
status_static(" "); drop(); /* => " " */
|
mod_static(" "); drop(); /* => " " */
|
||||||
set_timezone("");
|
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 */
|
/* and another way is to use the weird key-val system */
|
||||||
cpy("separator"); /* duplicate setting */
|
cpy("separator"); /* duplicate setting */
|
||||||
set("separator", " "); /* overwrite first setting */
|
set("separator", " "); /* overwrite first setting */
|
||||||
set_timezone(NULL);
|
set_timezone(NULL);
|
||||||
status_static("local"); /* use first setting */
|
mod_static("local"); /* use first setting */
|
||||||
del("separator"); /* delete the first, restoring the second */
|
del("separator"); /* delete the first, restoring the second */
|
||||||
status_time("%Y-%m-%dT%H:%M");
|
mod_time("%Y-%m-%dT%H:%M");
|
||||||
drop();
|
drop();
|
||||||
/* => "local 2025-03-26T18:00" */
|
/* => "local 2025-03-26T18:00" */
|
||||||
|
|
||||||
|
@ -1,59 +1,32 @@
|
|||||||
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
#undef NDEBUG /* LOAD-BEARING ASSERTS! */
|
||||||
#include <assert.h> /* assert(3) */
|
#include <assert.h> /* "mod_time.c" */
|
||||||
#include <limits.h> /* UCHAR_MAX */
|
#include <limits.h> /* UCHAR_MAX */
|
||||||
#include <stdio.h> /* fputs(3), stdout, NULL */
|
#include <stdlib.h> /* "mod_time.c" */
|
||||||
#include <stdlib.h> /* atoi(3) */
|
#include <time.h> /* "mod_time.c" */
|
||||||
#include <time.h> /* localtime(3), strftime(3), time(2), time_t, struct tm */
|
|
||||||
#include <unistd.h> /* sleep(3) */
|
#include <unistd.h> /* sleep(3) */
|
||||||
#include "io.h" /* drop(3), out(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) */
|
#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;
|
typedef unsigned char tick_t;
|
||||||
#define TICK_MAX UCHAR_MAX
|
#define TICK_MAX UCHAR_MAX
|
||||||
|
|
||||||
unsigned int slept = 0;
|
|
||||||
tick_t ticker = 0;
|
tick_t ticker = 0;
|
||||||
|
|
||||||
void
|
#include "mod_static.c"
|
||||||
set_timezone(char *zone) {
|
#include "mod_time.c"
|
||||||
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 "order.c"
|
#include "order.c"
|
||||||
|
|
||||||
int main(int argc, char *argv) {
|
int main(int argc, char *argv) {
|
||||||
keyval_init();
|
keyval_init();
|
||||||
|
|
||||||
while(1) {
|
for ever {
|
||||||
order();
|
order(); drop(); out("\n");
|
||||||
drop();
|
slept = sleep(getd("interval_seconds"));
|
||||||
out("\n");
|
|
||||||
slept = sleep(atoi(get("interval_seconds")));
|
|
||||||
ticker = ++ticker % TICK_MAX;
|
ticker = ++ticker % TICK_MAX;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user