From bdcbf8589716c047a732db3cc349ee6114ccc25f Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 7 May 2023 12:39:26 +0200 Subject: iterate on previous commit which adds $SFEED_NEW_MAX_SECS Separate the common pattern to get the time to compare new items in format tools to the new util function: getcomparetime(). Some changes and notes: - Change it so it is OK to set this value to 0 or negative (in the future). - sfeed_curses: truncating newmaxsecs to an int would limit the value too much. - Just use strtotime() and parse the value to time_t. This is a signed long (32-bit, until 2038) or signed long long (64-bit) on most platforms. - sfeed_curses gets the comparison time on reload aswell and it needs errno = 0, because it uses die(). time() is not guaranteed to set an errno if it fails. - Rename environment variable to $SFEED_NEW_AGE. --- README | 6 +++--- sfeed_curses.1 | 2 +- sfeed_curses.c | 13 +++---------- sfeed_frames.1 | 2 +- sfeed_frames.c | 17 +++-------------- sfeed_gopher.1 | 2 +- sfeed_gopher.c | 17 +++-------------- sfeed_html.1 | 2 +- sfeed_html.c | 17 +++-------------- sfeed_plain.1 | 2 +- sfeed_plain.c | 18 +++--------------- util.c | 18 ++++++++++++++++++ util.h | 1 + 13 files changed, 42 insertions(+), 75 deletions(-) diff --git a/README b/README index 313ab4c..427435d 100644 --- a/README +++ b/README @@ -290,9 +290,9 @@ Just like the other format programs included in sfeed you can run it like this: sfeed_curses < ~/.sfeed/feeds/xkcd By default sfeed_curses marks the items of the last day as new/bold. This limit -might be overridden by setting the environment variable $SFEED_NEW_MAX_SECS to -the desired maximum in seconds. To manage read/unread items in a different way -a plain-text file with a list of the read URLs can be used. To enable this +might be overridden by setting the environment variable $SFEED_NEW_AGE to the +desired maximum in seconds. To manage read/unread items in a different way a +plain-text file with a list of the read URLs can be used. To enable this behaviour the path to this file can be specified by setting the environment variable $SFEED_URL_FILE to the URL file: diff --git a/sfeed_curses.1 b/sfeed_curses.1 index b3cccf2..56b2a92 100644 --- a/sfeed_curses.1 +++ b/sfeed_curses.1 @@ -208,7 +208,7 @@ SIGWINCH. Read and process a sequence of keys as input commands from this environment variable first, afterwards it reads from the tty as usual. This can be useful to automate certain actions at the start. -.It Ev SFEED_NEW_MAX_SECS +.It Ev SFEED_NEW_AGE Overwrite the maximum age in seconds to mark feeds as new. By default this is 86400, which equals one day. .It Ev SFEED_PIPER diff --git a/sfeed_curses.c b/sfeed_curses.c index cce608e..b6f995f 100644 --- a/sfeed_curses.c +++ b/sfeed_curses.c @@ -191,7 +191,6 @@ static int plumberia = 0; /* env variable: $SFEED_PLUMBER_INTERACTIVE */ static int piperia = 1; /* env variable: $SFEED_PIPER_INTERACTIVE */ static int yankeria = 0; /* env variable: $SFEED_YANKER_INTERACTIVE */ static int lazyload = 0; /* env variable: $SFEED_LAZYLOAD */ -static int newmaxsecs = 86400; /* env variable: $SFEED_NEW_MAX_SECS */ int ttywritef(const char *fmt, ...) @@ -1321,9 +1320,8 @@ feeds_load(struct feed *feeds, size_t nfeeds) size_t i; errno = 0; - if ((comparetime = time(NULL)) == (time_t)-1) - die("time"); - comparetime -= newmaxsecs; + if ((comparetime = getcomparetime()) == (time_t)-1) + die("getcomparetime"); for (i = 0; i < nfeeds; i++) { f = &feeds[i]; @@ -1967,7 +1965,7 @@ main(int argc, char *argv[]) struct pane *p; struct feed *f; struct row *row; - char *name, *tmp, *endptr; + char *name, *tmp; char *search = NULL; /* search text */ int button, ch, fd, i, keymask, release, x, y; off_t pos; @@ -1997,11 +1995,6 @@ main(int argc, char *argv[]) markunreadcmd = tmp; if ((tmp = getenv("SFEED_LAZYLOAD"))) lazyload = !strcmp(tmp, "1"); - if ((tmp = getenv("SFEED_NEW_MAX_SECS"))) { - newmaxsecs = (int) strtol(tmp, &endptr, 10); - if (*tmp == '\0' || *endptr != '\0' || newmaxsecs <= 0) - err(1, "cannot parse $SFEED_NEW_MAX_SECS"); - } urlfile = getenv("SFEED_URL_FILE"); /* can be NULL */ cmdenv = getenv("SFEED_AUTOCMD"); /* can be NULL */ diff --git a/sfeed_frames.1 b/sfeed_frames.1 index 976d5ed..455c1db 100644 --- a/sfeed_frames.1 +++ b/sfeed_frames.1 @@ -40,7 +40,7 @@ The HTML file of the menu frame which contains navigation "anchor" links (like .El .Sh ENVIRONMENT VARIABLES .Bl -tag -width Ds -.It Ev SFEED_NEW_MAX_SECS +.It Ev SFEED_NEW_AGE Overwrite the maximum age in seconds to mark feeds as new. By default this is 86400, which equals one day. .El diff --git a/sfeed_frames.c b/sfeed_frames.c index e52abd1..b2f75cf 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -77,9 +77,8 @@ int main(int argc, char *argv[]) { FILE *fpindex, *fpitems, *fpmenu = NULL, *fp; - char *name, *tmp, *endptr; + char *name; int i, showsidebar = (argc > 1); - long l; struct feed *f; if (pledge("stdio rpath wpath cpath", NULL) == -1) @@ -88,18 +87,8 @@ main(int argc, char *argv[]) if (!(feeds = calloc(argc, sizeof(struct feed)))) err(1, "calloc"); - if ((comparetime = time(NULL)) == (time_t)-1) - errx(1, "time"); - - if ((tmp = getenv("SFEED_NEW_MAX_SECS"))) { - l = strtol(tmp, &endptr, 10); - if (*tmp == '\0' || *endptr != '\0' || l <= 0) - err(1, "cannot parse $SFEED_NEW_MAX_SECS"); - comparetime -= l; - } else { - /* 1 day is old news */ - comparetime -= 86400; - } + if ((comparetime = getcomparetime()) == (time_t)-1) + errx(1, "getcomparetime"); /* write main index page */ if (!(fpindex = fopen("index.html", "wb"))) diff --git a/sfeed_gopher.1 b/sfeed_gopher.1 index f602d2c..bc45121 100644 --- a/sfeed_gopher.1 +++ b/sfeed_gopher.1 @@ -46,7 +46,7 @@ The default is "127.0.0.1". .It Ev SFEED_GOPHER_PORT This environment variable can be used as the Gopher Port field. The default is "70". -.It Ev SFEED_NEW_MAX_SECS +.It Ev SFEED_NEW_AGE Overwrite the maximum age in seconds to mark feeds as new. By default this is 86400, which equals one day. .El diff --git a/sfeed_gopher.c b/sfeed_gopher.c index 007a244..78e1f12 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -122,9 +122,8 @@ int main(int argc, char *argv[]) { FILE *fpitems, *fpindex, *fp; - char *name, *p, *tmp, *endptr; + char *name, *p; int i; - long l; if (argc == 1) { if (pledge("stdio", NULL) == -1) @@ -138,18 +137,8 @@ main(int argc, char *argv[]) err(1, "pledge"); } - if ((comparetime = time(NULL)) == (time_t)-1) - errx(1, "time"); - - if ((tmp = getenv("SFEED_NEW_MAX_SECS"))) { - l = strtol(tmp, &endptr, 10); - if (*tmp == '\0' || *endptr != '\0' || l <= 0) - err(1, "cannot parse $SFEED_NEW_MAX_SECS"); - comparetime -= l; - } else { - /* 1 day is old news */ - comparetime -= 86400; - } + if ((comparetime = getcomparetime()) == (time_t)-1) + errx(1, "getcomparetime"); if ((p = getenv("SFEED_GOPHER_HOST"))) host = p; diff --git a/sfeed_html.1 b/sfeed_html.1 index 98784c5..e517a63 100644 --- a/sfeed_html.1 +++ b/sfeed_html.1 @@ -32,7 +32,7 @@ Items are marked as new using a bold markup. There is an example style.css stylesheet file included in the distribution. .Sh ENVIRONMENT VARIABLES .Bl -tag -width Ds -.It Ev SFEED_NEW_MAX_SECS +.It Ev SFEED_NEW_AGE Overwrite the maximum age in seconds to mark feeds as new. By default this is 86400, which equals one day. .El diff --git a/sfeed_html.c b/sfeed_html.c index 89506be..9269413 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -78,28 +78,17 @@ int main(int argc, char *argv[]) { struct feed *f; - char *name, *tmp, *endptr; + char *name; FILE *fp; int i; - long l; if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) err(1, "pledge"); if (!(feeds = calloc(argc, sizeof(struct feed)))) err(1, "calloc"); - if ((comparetime = time(NULL)) == (time_t)-1) - errx(1, "time"); - - if ((tmp = getenv("SFEED_NEW_MAX_SECS"))) { - l = strtol(tmp, &endptr, 10); - if (*tmp == '\0' || *endptr != '\0' || l <= 0) - err(1, "cannot parse $SFEED_NEW_MAX_SECS"); - comparetime -= l; - } else { - /* 1 day is old news */ - comparetime -= 86400; - } + if ((comparetime = getcomparetime()) == (time_t)-1) + errx(1, "getcomparetime"); fputs("\n" "\n" diff --git a/sfeed_plain.1 b/sfeed_plain.1 index 93ed41d..2e8e6ad 100644 --- a/sfeed_plain.1 +++ b/sfeed_plain.1 @@ -42,7 +42,7 @@ and .Xr wcwidth 3 . .Sh ENVIRONMENT VARIABLES .Bl -tag -width Ds -.It Ev SFEED_NEW_MAX_SECS +.It Ev SFEED_NEW_AGE Overwrite the maximum age in seconds to mark feeds as new. By default this is 86400, which equals one day. .El diff --git a/sfeed_plain.c b/sfeed_plain.c index c790ec4..f8ce7ec 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -53,9 +52,8 @@ int main(int argc, char *argv[]) { FILE *fp; - char *name, *tmp, *endptr; + char *name; int i; - long l; if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); @@ -65,18 +63,8 @@ main(int argc, char *argv[]) if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) err(1, "pledge"); - if ((comparetime = time(NULL)) == (time_t)-1) - errx(1, "time"); - - if ((tmp = getenv("SFEED_NEW_MAX_SECS"))) { - l = strtol(tmp, &endptr, 10); - if (*tmp == '\0' || *endptr != '\0' || l <= 0) - err(1, "cannot parse $SFEED_NEW_MAX_SECS"); - comparetime -= l; - } else { - /* 1 day is old news */ - comparetime -= 86400; - } + if ((comparetime = getcomparetime()) == (time_t)-1) + errx(1, "getcomparetime"); if (argc == 1) { printfeed(stdin, ""); diff --git a/util.c b/util.c index 0b7da06..e5aec3b 100644 --- a/util.c +++ b/util.c @@ -318,6 +318,24 @@ strtotime(const char *s, time_t *t) return 0; } +time_t +getcomparetime(void) +{ + time_t now, t; + char *p; + + if ((now = time(NULL)) == (time_t)-1) + return (time_t)-1; + + if ((p = getenv("SFEED_NEW_AGE"))) { + if (strtotime(p, &t) == -1) + return (time_t)-1; + return now - t; + } + + return now - 86400; /* 1 day is old news */ +} + /* Escape characters below as HTML 2.0 / XML 1.0. */ void xmlencode(const char *s, FILE *fp) diff --git a/util.h b/util.h index 9138de7..3ce5e41 100644 --- a/util.h +++ b/util.h @@ -71,6 +71,7 @@ int uri_makeabs(struct uri *, struct uri *, struct uri *); int uri_parse(const char *, struct uri *); void checkfileerror(FILE *, const char *, int); +time_t getcomparetime(void); void parseline(char *, char *[FieldLast]); void printutf8pad(FILE *, const char *, size_t, int); int strtotime(const char *, time_t *); -- cgit v1.2.3