4626729e35c34c92b49287968f5ac05d400329c2
1 #include <sys/select.h>
16 #include "bsdtimespec.h"
17 #include "khlib_log.h"
18 #include "khlib_time.h"
20 #define usage(...) { \
22 fprintf(stderr, "Error:\n " __VA_ARGS__); \
25 #define ERRMSG "ERROR"
28 static const char errmsg
[] = ERRMSG
;
29 static const int errlen
= sizeof(ERRMSG
) - 1;
33 /* TODO: Convert slot list to slot array. */
34 typedef struct Slot Slot
;
38 struct timespec in_last_read
;
39 struct timespec out_ttl
;
41 int out_pos_lo
; /* Lowest position on the output buffer. */
42 int out_pos_cur
; /* Current position on the output buffer. */
43 int out_pos_hi
; /* Highest position on the output buffer. */
47 typedef struct Config Config
;
65 buf_create(Config
*cfg
)
71 buf
= calloc(1, cfg
->buf_width
+ 1);
74 "[memory] Failed to allocate buffer of %d bytes",
77 memset(buf
, ' ', cfg
->buf_width
);
78 buf
[cfg
->buf_width
] = '\0';
79 seplen
= strlen(cfg
->separator
);
80 /* Set the separators */
81 for (s
= cfg
->slots
; s
; s
= s
->next
) {
82 /* Skip the first, left-most */
84 /* Copying only seplen ensures we omit the '\0' byte. */
86 buf
+ (s
->out_pos_lo
- seplen
),
118 " in_last_read = {tv_sec = %ld, tv_nsec = %ld}"
119 " out_ttl = {tv_sec = %ld, tv_nsec = %ld},"
128 s
->in_last_read
.tv_sec
,
129 s
->in_last_read
.tv_nsec
,
140 slots_log(Slot
*head
)
142 for (Slot
*s
= head
; s
; s
= s
->next
) {
148 slots_assert_fifos_exist(Slot
*s
)
153 for (; s
; s
= s
->next
) {
154 if (lstat(s
->in_fifo
, &st
) < 0) {
156 "Cannot stat \"%s\". Error: %s\n",
163 if (!(st
.st_mode
& S_IFIFO
)) {
164 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
171 "Encountered errors with given file paths. See log.\n"
176 slot_expire(Slot
*s
, struct timespec t
, char *buf
)
180 timespecsub(&t
, &(s
->in_last_read
), &td
);
181 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
182 /* TODO: Maybe configurable expiry character. */
183 memset(buf
+ s
->out_pos_lo
, '_', s
->out_width
);
184 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
189 slot_set_error(Slot
*s
, char *buf
)
195 b
= buf
+ s
->out_pos_lo
;
196 /* Copy as much of the error message as possible.
197 * EXCLUDING the terminating \0. */
198 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
200 /* Any remaining positions: */
201 memset(b
+ i
, '_', s
->out_width
- i
);
205 slot_read(Slot
*s
, char *buf
)
207 char c
; /* Character read. */
208 int r
; /* Remaining unused positions in buffer range. */
211 switch (read(s
->in_fd
, &c
, 1)) {
214 "Failed to read: \"%s\". errno: %d, msg: %s\n",
227 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
228 s
->out_pos_cur
= s
->out_pos_lo
;
231 /* TODO: Consider making msg term char a CLI option */
232 if (c
== '\n' || c
== '\0') {
233 r
= (s
->out_pos_hi
- s
->out_pos_cur
) + 1;
235 memset(buf
+ s
->out_pos_cur
, ' ', r
);
236 return END_OF_MESSAGE
;
238 if (s
->out_pos_cur
<= s
->out_pos_hi
)
239 buf
[s
->out_pos_cur
++] = c
;
242 * Force EOM beyond available range.
243 * To ensure that a rogue large message
244 * doesn't trap us here needlessly
247 return END_OF_MESSAGE
;
257 slots_read(Config
*cfg
, struct timespec
*ti
, char *buf
)
267 for (s
= cfg
->slots
; s
; s
= s
->next
) {
268 /* TODO: Create the FIFO if it doesn't already exist. */
269 if (lstat(s
->in_fifo
, &st
) < 0) {
271 "Cannot stat \"%s\". Error: %s\n",
275 slot_set_error(s
, buf
);
278 if (!(st
.st_mode
& S_IFIFO
)) {
279 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
280 slot_set_error(s
, buf
);
285 "%s: closed. opening. in_fd: %d\n",
289 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
292 "%s: already openned. in_fd: %d\n",
297 if (s
->in_fd
== -1) {
298 /* TODO Consider backing off retries for failed slots */
299 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
300 slot_set_error(s
, buf
);
303 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
304 if (s
->in_fd
> maxfd
)
306 FD_SET(s
->in_fd
, &fds
);
308 khlib_debug("selecting...\n");
309 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
310 khlib_debug("ready: %d\n", ready
);
311 clock_gettime(CLOCK_MONOTONIC
, &t
);
316 "pselect temp failure: %d, errno: %d, msg: %s\n",
321 /* TODO: Reconsider what to do here. */
325 "pselect failed: %d, errno: %d, msg: %s\n",
332 /* At-least-once ensures that expiries are still checked on timeouts. */
334 for (s
= cfg
->slots
; s
; s
= s
->next
) {
337 if (FD_ISSET(s
->in_fd
, &fds
)) {
338 khlib_debug("reading: %s\n", s
->in_fifo
);
339 switch (slot_read(s
, buf
)) {
341 * ### MESSAGE LOSS ###
342 * is introduced by closing at EOM in addition
343 * to EOF, since there may be unread messages
344 * remaining in the pipe. However,
346 * ### INTER-MESSAGE PUSHBACK ###
347 * is also gained, since pipes block at the
350 * This is an acceptable trade-off because we
351 * are a stateless reporter of a _most-recent_
352 * status, not a stateful accumulator.
354 * ### LOSSLESS ALTERNATIVES ###
355 * - Read each pipe until EOF before reading
357 * PROBLEM: a fast writer can trap us in the
360 * - Read each pipe until EOM, but close only
362 * PROBLEM: a fast writer can fill the pipe
363 * faster than we can read it and we end-up
364 * displaying stale data.
373 s
->out_pos_cur
= s
->out_pos_lo
;
382 slot_expire(s
, t
, buf
);
390 config_log(Config
*cfg
)
406 slots_log(cfg
->slots
);
410 config_stretch_for_separators(Config
*cfg
)
412 int seplen
= strlen(cfg
->separator
);
415 Slot
*s
= cfg
->slots
;
418 s
->out_pos_lo
+= prefix
;
419 s
->out_pos_hi
+= prefix
;
420 s
->out_pos_cur
= s
->out_pos_lo
;
425 cfg
->buf_width
+= (seplen
* (nslots
- 1));
429 is_pos_num(char *str
)
432 if (!isdigit(*(str
++)))
438 is_decimal(char *str
)
443 while ((c
= *(str
++)) != '\0')
445 if (c
== '.' && !seen
++)
460 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
462 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
463 " FILE_PATH = string\n"
464 " DATA_WIDTH = int (* (positive) number of characters *)\n"
465 " DATA_TTL = float (* (positive) number of seconds *)\n"
466 " OPTION = -i INTERVAL\n"
468 " | -x (* Output to X root window *)\n"
470 " SEPARATOR = string\n"
471 " INTERVAL = float (* (positive) number of seconds *)\n"
472 " LOG_LEVEL = int (* %d through %d *)\n"
480 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
486 /* For mutually-recursive calls. */
487 void opts_parse_any(Config
*, int, char *[], int);
490 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
495 usage("Option -i parameter is missing.\n");
497 if (!is_decimal(param
))
498 usage("Option -i parameter is invalid: \"%s\"\n", param
);
499 cfg
->interval
= atof(param
);
500 opts_parse_any(cfg
, argc
, argv
, i
);
504 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
507 usage("Option -s parameter is missing.\n");
508 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
509 strcpy(cfg
->separator
, argv
[i
]);
510 opts_parse_any(cfg
, argc
, argv
, ++i
);
514 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
520 usage("Option -l parameter is missing.\n");
522 if (!is_pos_num(param
))
523 usage("Option -l parameter is invalid: \"%s\"\n", param
);
524 log_level
= atoi(param
);
525 if (log_level
> Debug
)
527 "Option -l value (%d) exceeds maximum (%d)\n",
531 _khlib_log_level
= log_level
;
532 opts_parse_any(cfg
, argc
, argv
, i
);
536 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
538 switch (argv
[i
][1]) {
540 /* TODO: Generic set_int */
541 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
544 /* TODO: Generic set_str */
545 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
549 opts_parse_any(cfg
, argc
, argv
, ++i
);
552 /* TODO: Generic set_int */
553 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
556 usage("Option \"%s\" is invalid\n", argv
[i
]);
561 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
566 struct timespec in_last_read
;
571 "[spec] Parameter(s) missing for fifo \"%s\".\n",
580 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
582 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
584 in_last_read
.tv_sec
= 0;
585 in_last_read
.tv_nsec
= 0;
586 s
= calloc(1, sizeof(struct Slot
));
591 s
->out_width
= atoi(w
);
592 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
593 s
->in_last_read
= in_last_read
;
594 s
->out_pos_lo
= cfg
->buf_width
;
595 s
->out_pos_cur
= s
->out_pos_lo
;
596 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
597 s
->next
= cfg
->slots
;
600 cfg
->buf_width
+= s
->out_width
;
603 khlib_fatal("[memory] Allocation failure.");
605 opts_parse_any(cfg
, argc
, argv
, i
);
609 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
612 switch (argv
[i
][0]) {
614 parse_opts_opt(cfg
, argc
, argv
, i
);
617 parse_opts_spec(cfg
, argc
, argv
, i
);
623 opts_parse(Config
*cfg
, int argc
, char *argv
[])
625 opts_parse_any(cfg
, argc
, argv
, 1);
626 cfg
->slots
= slots_rev(cfg
->slots
);
628 if (cfg
->slots
== NULL
)
629 usage("No slot specs were given!\n");
633 loop(Config
*cfg
, char *buf
, Display
*d
)
636 t0
, /* time stamp. before reading slots */
637 t1
, /* time stamp. after reading slots */
638 ti
, /* time interval desired (t1 - t0) */
639 td
, /* time interval measured (t1 - t0) */
640 tc
; /* time interval correction (ti - td) when td < ti */
642 ti
= khlib_timespec_of_float(cfg
->interval
);
644 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
645 slots_read(cfg
, &ti
, buf
);
646 if (cfg
->to_x_root
) {
647 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
648 khlib_fatal("XStoreName failed.\n");
654 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
655 timespecsub(&t1
, &t0
, &td
);
657 "td {tv_sec = %ld, tv_nsec = %ld}\n",
661 if (timespeccmp(&td
, &ti
, <)) {
663 * Pushback on data producers by refusing to read the
664 * pipe more frequently than the interval.
666 timespecsub(&ti
, &td
, &tc
);
673 main(int argc
, char *argv
[])
686 /* TODO: Handle signals */
689 opts_parse(&cfg
, argc
, argv
);
690 slots_assert_fifos_exist(cfg
.slots
);
691 config_stretch_for_separators(&cfg
);
692 buf
= buf_create(&cfg
);
693 if (cfg
.to_x_root
&& !(d
= XOpenDisplay(NULL
)))
694 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
This page took 0.11603 seconds and 3 git commands to generate.