1 #include <sys/select.h>
17 #include "bsdtimespec.h"
18 #include "khlib_log.h"
19 #include "khlib_time.h"
21 #define usage(...) { \
23 fprintf(stderr, "Error:\n " __VA_ARGS__); \
26 #define ERRMSG "ERROR"
29 static const char errmsg
[] = ERRMSG
;
30 static const int errlen
= sizeof(ERRMSG
) - 1;
35 /* TODO: Convert slot list to slot array. */
36 typedef struct Slot Slot
;
40 struct timespec in_last_read
;
41 struct timespec out_ttl
;
43 int out_pos_lo
; /* Lowest position on the output buffer. */
44 int out_pos_cur
; /* Current position on the output buffer. */
45 int out_pos_hi
; /* Highest position on the output buffer. */
49 typedef struct Config Config
;
53 char expiry_character
;
68 buf_create(Config
*cfg
)
74 buf
= calloc(1, cfg
->buf_width
+ 1);
77 "[memory] Failed to allocate buffer of %d bytes",
80 memset(buf
, ' ', cfg
->buf_width
);
81 buf
[cfg
->buf_width
] = '\0';
82 seplen
= strlen(cfg
->separator
);
83 /* Set the separators */
84 for (s
= cfg
->slots
; s
; s
= s
->next
) {
85 /* Skip the first, left-most */
87 /* Copying only seplen ensures we omit the '\0' byte. */
89 buf
+ (s
->out_pos_lo
- seplen
),
121 " in_last_read = {tv_sec = %ld, tv_nsec = %ld}"
122 " out_ttl = {tv_sec = %ld, tv_nsec = %ld},"
131 s
->in_last_read
.tv_sec
,
132 s
->in_last_read
.tv_nsec
,
143 slots_log(Slot
*head
)
147 for (; s
; s
= s
->next
) {
153 slots_assert_fifos_exist(Slot
*s
)
158 for (; s
; s
= s
->next
) {
159 if (lstat(s
->in_fifo
, &st
) < 0) {
161 "Cannot stat \"%s\". Error: %s\n",
168 if (!(st
.st_mode
& S_IFIFO
)) {
169 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
176 "Encountered errors with given file paths. See log.\n"
185 s
->out_pos_cur
= s
->out_pos_lo
;
191 for (; s
; s
= s
->next
)
198 slot_expire(Slot
*s
, struct timespec t
, char expiry_character
, char *buf
)
202 timespecsub(&t
, &(s
->in_last_read
), &td
);
203 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
209 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
214 slot_set_error(Slot
*s
, char *buf
)
220 b
= buf
+ s
->out_pos_lo
;
221 /* Copy as much of the error message as possible.
222 * EXCLUDING the terminating \0. */
223 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
225 /* Any remaining positions: */
226 memset(b
+ i
, '_', s
->out_width
- i
);
230 slot_read(Slot
*s
, char *buf
)
232 char c
; /* Character read. */
233 int r
; /* Remaining unused positions in buffer range. */
236 switch (read(s
->in_fd
, &c
, 1)) {
239 "Failed to read: \"%s\". errno: %d, msg: %s\n",
252 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
253 s
->out_pos_cur
= s
->out_pos_lo
;
256 /* TODO: Consider making msg term char a CLI option */
257 if (c
== '\n' || c
== '\0') {
258 r
= (s
->out_pos_hi
- s
->out_pos_cur
) + 1;
260 memset(buf
+ s
->out_pos_cur
, ' ', r
);
261 return END_OF_MESSAGE
;
263 if (s
->out_pos_cur
<= s
->out_pos_hi
)
264 buf
[s
->out_pos_cur
++] = c
;
267 * Force EOM beyond available range.
268 * To ensure that a rogue large message
269 * doesn't trap us here needlessly
272 return END_OF_MESSAGE
;
282 slots_read(Config
*cfg
, struct timespec
*ti
, char *buf
)
292 for (s
= cfg
->slots
; s
; s
= s
->next
) {
293 /* TODO: Create the FIFO if it doesn't already exist. */
294 if (lstat(s
->in_fifo
, &st
) < 0) {
296 "Cannot stat \"%s\". Error: %s\n",
300 slot_set_error(s
, buf
);
303 if (!(st
.st_mode
& S_IFIFO
)) {
304 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
305 slot_set_error(s
, buf
);
310 "%s: closed. opening. in_fd: %d\n",
314 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
317 "%s: already openned. in_fd: %d\n",
322 if (s
->in_fd
== -1) {
323 /* TODO Consider backing off retries for failed slots */
324 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
325 slot_set_error(s
, buf
);
328 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
329 if (s
->in_fd
> maxfd
)
331 FD_SET(s
->in_fd
, &fds
);
333 khlib_debug("selecting...\n");
334 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
335 khlib_debug("ready: %d\n", ready
);
336 clock_gettime(CLOCK_MONOTONIC
, &t
);
341 "pselect interrupted: %d, errno: %d, msg: %s\n",
346 /* TODO: Reconsider what to do here. */
350 "pselect failed: %d, errno: %d, msg: %s\n",
357 /* At-least-once ensures that expiries are still checked on timeouts. */
359 for (s
= cfg
->slots
; s
; s
= s
->next
) {
362 if (FD_ISSET(s
->in_fd
, &fds
)) {
363 khlib_debug("reading: %s\n", s
->in_fifo
);
364 switch (slot_read(s
, buf
)) {
366 * ### MESSAGE LOSS ###
367 * is introduced by closing at EOM in addition
368 * to EOF, since there may be unread messages
369 * remaining in the pipe. However,
371 * ### INTER-MESSAGE PUSHBACK ###
372 * is also gained, since pipes block at the
375 * This is an acceptable trade-off because we
376 * are a stateless reporter of a _most-recent_
377 * status, not a stateful accumulator.
379 * ### LOSSLESS ALTERNATIVES ###
380 * - Read each pipe until EOF before reading
382 * PROBLEM: a fast writer can trap us in the
385 * - Read each pipe until EOM, but close only
387 * PROBLEM: a fast writer can fill the pipe
388 * faster than we can read it and we end-up
389 * displaying stale data.
405 slot_expire(s
, t
, cfg
->expiry_character
, buf
);
413 config_log(Config
*cfg
)
429 slots_log(cfg
->slots
);
433 config_stretch_for_separators(Config
*cfg
)
435 int seplen
= strlen(cfg
->separator
);
438 Slot
*s
= cfg
->slots
;
441 s
->out_pos_lo
+= prefix
;
442 s
->out_pos_hi
+= prefix
;
443 s
->out_pos_cur
= s
->out_pos_lo
;
448 cfg
->buf_width
+= (seplen
* (nslots
- 1));
452 is_pos_num(char *str
)
455 if (!isdigit(*(str
++)))
461 is_decimal(char *str
)
466 while ((c
= *(str
++)) != '\0')
468 if (c
== '.' && !seen
++)
483 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
485 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
486 " FILE_PATH = string\n"
487 " DATA_WIDTH = int (* (positive) number of characters *)\n"
488 " DATA_TTL = float (* (positive) number of seconds *)\n"
489 " OPTION = -i INTERVAL\n"
491 " | -x (* Output to X root window *)\n"
493 " | -e EXPIRY_CHARACTER\n"
494 " SEPARATOR = string\n"
495 " INTERVAL = float (* (positive) number of seconds *)\n"
496 " LOG_LEVEL = int (* %d through %d *)\n"
497 " EXPIRY_CHARACTER = string "
498 "(* Character with which to fill the slot upon expiration. *)\n"
506 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
512 /* For mutually-recursive calls. */
513 void opts_parse_any(Config
*, int, char *[], int);
516 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
521 usage("Option -i parameter is missing.\n");
523 if (!is_decimal(param
))
524 usage("Option -i parameter is invalid: \"%s\"\n", param
);
525 cfg
->interval
= atof(param
);
526 opts_parse_any(cfg
, argc
, argv
, i
);
530 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
533 usage("Option -s parameter is missing.\n");
534 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
535 strcpy(cfg
->separator
, argv
[i
]);
536 opts_parse_any(cfg
, argc
, argv
, ++i
);
540 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
546 usage("Option -l parameter is missing.\n");
548 if (!is_pos_num(param
))
549 usage("Option -l parameter is invalid: \"%s\"\n", param
);
550 log_level
= atoi(param
);
551 if (log_level
> Debug
)
553 "Option -l value (%d) exceeds maximum (%d)\n",
557 _khlib_log_level
= log_level
;
558 opts_parse_any(cfg
, argc
, argv
, i
);
562 parse_opts_opt_e(Config
*cfg
, int argc
, char *argv
[], int i
)
565 usage("Option -e parameter is missing.\n");
566 cfg
->expiry_character
= argv
[i
++][0];
567 opts_parse_any(cfg
, argc
, argv
, i
);
571 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
573 switch (argv
[i
][1]) {
575 /* TODO: Generic set_int */
576 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
579 /* TODO: Generic set_str */
580 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
584 opts_parse_any(cfg
, argc
, argv
, ++i
);
587 /* TODO: Generic set_int */
588 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
591 /* TODO: Generic set_str */
592 parse_opts_opt_e(cfg
, argc
, argv
, ++i
);
595 usage("Option \"%s\" is invalid\n", argv
[i
]);
600 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
605 struct timespec in_last_read
;
610 "[spec] Parameter(s) missing for fifo \"%s\".\n",
619 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
621 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
623 in_last_read
.tv_sec
= 0;
624 in_last_read
.tv_nsec
= 0;
625 s
= calloc(1, sizeof(struct Slot
));
630 s
->out_width
= atoi(w
);
631 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
632 s
->in_last_read
= in_last_read
;
633 s
->out_pos_lo
= cfg
->buf_width
;
634 s
->out_pos_cur
= s
->out_pos_lo
;
635 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
636 s
->next
= cfg
->slots
;
639 cfg
->buf_width
+= s
->out_width
;
642 khlib_fatal("[memory] Allocation failure.");
644 opts_parse_any(cfg
, argc
, argv
, i
);
648 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
651 switch (argv
[i
][0]) {
653 parse_opts_opt(cfg
, argc
, argv
, i
);
656 parse_opts_spec(cfg
, argc
, argv
, i
);
662 opts_parse(Config
*cfg
, int argc
, char *argv
[])
664 opts_parse_any(cfg
, argc
, argv
, 1);
665 cfg
->slots
= slots_rev(cfg
->slots
);
667 if (cfg
->slots
== NULL
)
668 usage("No slot specs were given!\n");
672 loop(Config
*cfg
, char *buf
, Display
*d
)
675 t0
, /* time stamp. before reading slots */
676 t1
, /* time stamp. after reading slots */
677 ti
, /* time interval desired (t1 - t0) */
678 td
, /* time interval measured (t1 - t0) */
679 tc
; /* time interval correction (ti - td) when td < ti */
681 ti
= khlib_timespec_of_float(cfg
->interval
);
683 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
684 slots_read(cfg
, &ti
, buf
);
685 if (cfg
->to_x_root
) {
686 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
687 khlib_fatal("XStoreName failed.\n");
693 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
694 timespecsub(&t1
, &t0
, &td
);
696 "td {tv_sec = %ld, tv_nsec = %ld}\n",
700 if (timespeccmp(&td
, &ti
, <)) {
702 * Pushback on data producers by refusing to read the
703 * pipe more frequently than the interval.
705 timespecsub(&ti
, &td
, &tc
);
714 khlib_debug("terminating due to signal %d\n", s
);
719 main(int argc
, char *argv
[])
726 .expiry_character
= '_',
736 memset(&sa
, 0, sizeof(sa
));
737 sa
.sa_handler
= terminate
;
738 sigaction(SIGTERM
, &sa
, NULL
);
739 sigaction(SIGINT
, &sa
, NULL
);
741 opts_parse(&cfg
, argc
, argv
);
742 slots_assert_fifos_exist(cfg
.slots
);
743 config_stretch_for_separators(&cfg
);
744 buf
= buf_create(&cfg
);
745 if (cfg
.to_x_root
&& !(d
= XOpenDisplay(NULL
)))
746 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
748 slots_close(cfg
.slots
);
This page took 0.152545 seconds and 4 git commands to generate.