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
;
67 buf_create(Config
*cfg
)
73 buf
= calloc(1, cfg
->buf_width
+ 1);
76 "[memory] Failed to allocate buffer of %d bytes",
79 memset(buf
, ' ', cfg
->buf_width
);
80 buf
[cfg
->buf_width
] = '\0';
81 seplen
= strlen(cfg
->separator
);
82 /* Set the separators */
83 for (s
= cfg
->slots
; s
; s
= s
->next
) {
84 /* Skip the first, left-most */
86 /* Copying only seplen ensures we omit the '\0' byte. */
88 buf
+ (s
->out_pos_lo
- seplen
),
120 " in_last_read = {tv_sec = %ld, tv_nsec = %ld}"
121 " out_ttl = {tv_sec = %ld, tv_nsec = %ld},"
130 s
->in_last_read
.tv_sec
,
131 s
->in_last_read
.tv_nsec
,
142 slots_log(Slot
*head
)
146 for (; s
; s
= s
->next
) {
152 slots_assert_fifos_exist(Slot
*s
)
157 for (; s
; s
= s
->next
) {
158 if (lstat(s
->in_fifo
, &st
) < 0) {
160 "Cannot stat \"%s\". Error: %s\n",
167 if (!(st
.st_mode
& S_IFIFO
)) {
168 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
175 "Encountered errors with given file paths. See log.\n"
184 s
->out_pos_cur
= s
->out_pos_lo
;
190 for (; s
; s
= s
->next
)
197 slot_expire(Slot
*s
, struct timespec t
, char *buf
)
201 timespecsub(&t
, &(s
->in_last_read
), &td
);
202 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
203 /* TODO: Maybe configurable expiry character. */
204 memset(buf
+ s
->out_pos_lo
, '_', s
->out_width
);
205 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
210 slot_set_error(Slot
*s
, char *buf
)
216 b
= buf
+ s
->out_pos_lo
;
217 /* Copy as much of the error message as possible.
218 * EXCLUDING the terminating \0. */
219 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
221 /* Any remaining positions: */
222 memset(b
+ i
, '_', s
->out_width
- i
);
226 slot_read(Slot
*s
, char *buf
)
228 char c
; /* Character read. */
229 int r
; /* Remaining unused positions in buffer range. */
232 switch (read(s
->in_fd
, &c
, 1)) {
235 "Failed to read: \"%s\". errno: %d, msg: %s\n",
248 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
249 s
->out_pos_cur
= s
->out_pos_lo
;
252 /* TODO: Consider making msg term char a CLI option */
253 if (c
== '\n' || c
== '\0') {
254 r
= (s
->out_pos_hi
- s
->out_pos_cur
) + 1;
256 memset(buf
+ s
->out_pos_cur
, ' ', r
);
257 return END_OF_MESSAGE
;
259 if (s
->out_pos_cur
<= s
->out_pos_hi
)
260 buf
[s
->out_pos_cur
++] = c
;
263 * Force EOM beyond available range.
264 * To ensure that a rogue large message
265 * doesn't trap us here needlessly
268 return END_OF_MESSAGE
;
278 slots_read(Config
*cfg
, struct timespec
*ti
, char *buf
)
288 for (s
= cfg
->slots
; s
; s
= s
->next
) {
289 /* TODO: Create the FIFO if it doesn't already exist. */
290 if (lstat(s
->in_fifo
, &st
) < 0) {
292 "Cannot stat \"%s\". Error: %s\n",
296 slot_set_error(s
, buf
);
299 if (!(st
.st_mode
& S_IFIFO
)) {
300 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
301 slot_set_error(s
, buf
);
306 "%s: closed. opening. in_fd: %d\n",
310 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
313 "%s: already openned. in_fd: %d\n",
318 if (s
->in_fd
== -1) {
319 /* TODO Consider backing off retries for failed slots */
320 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
321 slot_set_error(s
, buf
);
324 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
325 if (s
->in_fd
> maxfd
)
327 FD_SET(s
->in_fd
, &fds
);
329 khlib_debug("selecting...\n");
330 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
331 khlib_debug("ready: %d\n", ready
);
332 clock_gettime(CLOCK_MONOTONIC
, &t
);
337 "pselect interrupted: %d, errno: %d, msg: %s\n",
342 /* TODO: Reconsider what to do here. */
346 "pselect failed: %d, errno: %d, msg: %s\n",
353 /* At-least-once ensures that expiries are still checked on timeouts. */
355 for (s
= cfg
->slots
; s
; s
= s
->next
) {
358 if (FD_ISSET(s
->in_fd
, &fds
)) {
359 khlib_debug("reading: %s\n", s
->in_fifo
);
360 switch (slot_read(s
, buf
)) {
362 * ### MESSAGE LOSS ###
363 * is introduced by closing at EOM in addition
364 * to EOF, since there may be unread messages
365 * remaining in the pipe. However,
367 * ### INTER-MESSAGE PUSHBACK ###
368 * is also gained, since pipes block at the
371 * This is an acceptable trade-off because we
372 * are a stateless reporter of a _most-recent_
373 * status, not a stateful accumulator.
375 * ### LOSSLESS ALTERNATIVES ###
376 * - Read each pipe until EOF before reading
378 * PROBLEM: a fast writer can trap us in the
381 * - Read each pipe until EOM, but close only
383 * PROBLEM: a fast writer can fill the pipe
384 * faster than we can read it and we end-up
385 * displaying stale data.
401 slot_expire(s
, t
, buf
);
409 config_log(Config
*cfg
)
425 slots_log(cfg
->slots
);
429 config_stretch_for_separators(Config
*cfg
)
431 int seplen
= strlen(cfg
->separator
);
434 Slot
*s
= cfg
->slots
;
437 s
->out_pos_lo
+= prefix
;
438 s
->out_pos_hi
+= prefix
;
439 s
->out_pos_cur
= s
->out_pos_lo
;
444 cfg
->buf_width
+= (seplen
* (nslots
- 1));
448 is_pos_num(char *str
)
451 if (!isdigit(*(str
++)))
457 is_decimal(char *str
)
462 while ((c
= *(str
++)) != '\0')
464 if (c
== '.' && !seen
++)
479 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
481 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
482 " FILE_PATH = string\n"
483 " DATA_WIDTH = int (* (positive) number of characters *)\n"
484 " DATA_TTL = float (* (positive) number of seconds *)\n"
485 " OPTION = -i INTERVAL\n"
487 " | -x (* Output to X root window *)\n"
489 " SEPARATOR = string\n"
490 " INTERVAL = float (* (positive) number of seconds *)\n"
491 " LOG_LEVEL = int (* %d through %d *)\n"
499 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
505 /* For mutually-recursive calls. */
506 void opts_parse_any(Config
*, int, char *[], int);
509 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
514 usage("Option -i parameter is missing.\n");
516 if (!is_decimal(param
))
517 usage("Option -i parameter is invalid: \"%s\"\n", param
);
518 cfg
->interval
= atof(param
);
519 opts_parse_any(cfg
, argc
, argv
, i
);
523 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
526 usage("Option -s parameter is missing.\n");
527 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
528 strcpy(cfg
->separator
, argv
[i
]);
529 opts_parse_any(cfg
, argc
, argv
, ++i
);
533 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
539 usage("Option -l parameter is missing.\n");
541 if (!is_pos_num(param
))
542 usage("Option -l parameter is invalid: \"%s\"\n", param
);
543 log_level
= atoi(param
);
544 if (log_level
> Debug
)
546 "Option -l value (%d) exceeds maximum (%d)\n",
550 _khlib_log_level
= log_level
;
551 opts_parse_any(cfg
, argc
, argv
, i
);
555 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
557 switch (argv
[i
][1]) {
559 /* TODO: Generic set_int */
560 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
563 /* TODO: Generic set_str */
564 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
568 opts_parse_any(cfg
, argc
, argv
, ++i
);
571 /* TODO: Generic set_int */
572 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
575 usage("Option \"%s\" is invalid\n", argv
[i
]);
580 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
585 struct timespec in_last_read
;
590 "[spec] Parameter(s) missing for fifo \"%s\".\n",
599 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
601 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
603 in_last_read
.tv_sec
= 0;
604 in_last_read
.tv_nsec
= 0;
605 s
= calloc(1, sizeof(struct Slot
));
610 s
->out_width
= atoi(w
);
611 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
612 s
->in_last_read
= in_last_read
;
613 s
->out_pos_lo
= cfg
->buf_width
;
614 s
->out_pos_cur
= s
->out_pos_lo
;
615 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
616 s
->next
= cfg
->slots
;
619 cfg
->buf_width
+= s
->out_width
;
622 khlib_fatal("[memory] Allocation failure.");
624 opts_parse_any(cfg
, argc
, argv
, i
);
628 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
631 switch (argv
[i
][0]) {
633 parse_opts_opt(cfg
, argc
, argv
, i
);
636 parse_opts_spec(cfg
, argc
, argv
, i
);
642 opts_parse(Config
*cfg
, int argc
, char *argv
[])
644 opts_parse_any(cfg
, argc
, argv
, 1);
645 cfg
->slots
= slots_rev(cfg
->slots
);
647 if (cfg
->slots
== NULL
)
648 usage("No slot specs were given!\n");
652 loop(Config
*cfg
, char *buf
, Display
*d
)
655 t0
, /* time stamp. before reading slots */
656 t1
, /* time stamp. after reading slots */
657 ti
, /* time interval desired (t1 - t0) */
658 td
, /* time interval measured (t1 - t0) */
659 tc
; /* time interval correction (ti - td) when td < ti */
661 ti
= khlib_timespec_of_float(cfg
->interval
);
663 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
664 slots_read(cfg
, &ti
, buf
);
665 if (cfg
->to_x_root
) {
666 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
667 khlib_fatal("XStoreName failed.\n");
673 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
674 timespecsub(&t1
, &t0
, &td
);
676 "td {tv_sec = %ld, tv_nsec = %ld}\n",
680 if (timespeccmp(&td
, &ti
, <)) {
682 * Pushback on data producers by refusing to read the
683 * pipe more frequently than the interval.
685 timespecsub(&ti
, &td
, &tc
);
694 khlib_debug("terminating due to signal %d\n", s
);
699 main(int argc
, char *argv
[])
715 memset(&sa
, 0, sizeof(sa
));
716 sa
.sa_handler
= terminate
;
717 sigaction(SIGTERM
, &sa
, NULL
);
718 sigaction(SIGINT
, &sa
, NULL
);
720 opts_parse(&cfg
, argc
, argv
);
721 slots_assert_fifos_exist(cfg
.slots
);
722 config_stretch_for_separators(&cfg
);
723 buf
= buf_create(&cfg
);
724 if (cfg
.to_x_root
&& !(d
= XOpenDisplay(NULL
)))
725 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
727 slots_close(cfg
.slots
);
This page took 0.132705 seconds and 4 git commands to generate.