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
;
54 int output_to_x_root_window
;
87 " in_last_read = {tv_sec = %ld, tv_nsec = %ld}"
88 " out_ttl = {tv_sec = %ld, tv_nsec = %ld},"
97 s
->in_last_read
.tv_sec
,
98 s
->in_last_read
.tv_nsec
,
109 slots_print(Slot
*head
)
111 for (Slot
*s
= head
; s
; s
= s
->next
) {
117 slot_expire(Slot
*s
, struct timespec t
, char *buf
)
121 timespecsub(&t
, &(s
->in_last_read
), &td
);
122 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
123 /* TODO: Maybe configurable expiry character. */
124 memset(buf
+ s
->out_pos_lo
, '_', s
->out_width
);
125 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
130 slot_set_error(Slot
*s
, char *buf
)
136 b
= buf
+ s
->out_pos_lo
;
137 /* Copy as much of the error message as possible.
138 * EXCLUDING the terminating \0. */
139 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
141 /* Any remaining positions: */
142 memset(b
+ i
, '_', s
->out_width
- i
);
146 slot_read(Slot
*s
, struct timespec t
, char *buf
)
148 char c
; /* Character read. */
149 int r
; /* Remaining unused positions in buffer range. */
152 switch (read(s
->in_fd
, &c
, 1)) {
155 "Failed to read: \"%s\". errno: %d, msg: %s\n",
168 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
169 s
->out_pos_cur
= s
->out_pos_lo
;
172 /* TODO: Consider making msg term char a CLI option */
173 if (c
== '\n' || c
== '\0') {
174 r
= (s
->out_pos_hi
- s
->out_pos_cur
) + 1;
176 memset(buf
+ s
->out_pos_cur
, ' ', r
);
177 s
->out_pos_cur
= s
->out_pos_lo
;
179 return END_OF_MESSAGE
;
181 if (s
->out_pos_cur
<= s
->out_pos_hi
)
182 buf
[s
->out_pos_cur
++] = c
;
183 /* Drop beyond available range. */
185 * TODO Define max after which we stop reading.
186 * To ensure that a rogue large message
187 * doesn't trap us here.
198 slots_read(Config
*cfg
, struct timespec
*ti
, char *buf
)
208 for (s
= cfg
->slots
; s
; s
= s
->next
) {
209 /* TODO: Create the FIFO if it doesn't already exist. */
210 if (lstat(s
->in_fifo
, &st
) < 0) {
212 "Cannot stat \"%s\". Error: %s\n",
216 slot_set_error(s
, buf
);
219 if (!(st
.st_mode
& S_IFIFO
)) {
220 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
221 slot_set_error(s
, buf
);
226 "%s: closed. opening. in_fd: %d\n",
230 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
233 "%s: already openned. in_fd: %d\n",
238 if (s
->in_fd
== -1) {
239 /* TODO Consider backing off retries for failed slots */
240 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
241 slot_set_error(s
, buf
);
244 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
245 if (s
->in_fd
> maxfd
)
247 FD_SET(s
->in_fd
, &fds
);
249 khlib_debug("selecting...\n");
250 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
251 khlib_debug("ready: %d\n", ready
);
252 clock_gettime(CLOCK_MONOTONIC
, &t
);
257 "pselect temp failure: %d, errno: %d, msg: %s\n",
262 /* TODO: Reconsider what to do here. */
266 "pselect failed: %d, errno: %d, msg: %s\n",
273 /* At-least-once ensures that expiries are still checked on timeouts. */
275 for (s
= cfg
->slots
; s
; s
= s
->next
) {
278 if (FD_ISSET(s
->in_fd
, &fds
)) {
279 khlib_debug("reading: %s\n", s
->in_fifo
);
280 switch (slot_read(s
, t
, buf
)) {
282 * ### MESSAGE LOSS ###
283 * is introduced by closing at EOM in addition
284 * to EOF, since there may be unread messages
285 * remaining in the pipe. However,
287 * ### INTER-MESSAGE PUSHBACK ###
288 * is also gained, since pipes block at the
291 * This is an acceptable trade-off because we
292 * are a stateless reporter of a _most-recent_
293 * status, not a stateful accumulator.
295 * ### LOSSLESS ALTERNATIVES ###
296 * - Read each pipe until EOF before reading
298 * PROBLEM: a fast writer can trap us in the
301 * - Read each pipe until EOM, but close only
303 * PROBLEM: a fast writer can fill the pipe
304 * faster than we can read it and we end-up
305 * displaying stale data.
321 slot_expire(s
, t
, buf
);
329 config_print(Config
*cfg
)
345 slots_print(cfg
->slots
);
349 is_pos_num(char *str
)
352 if (!isdigit(*(str
++)))
358 is_decimal(char *str
)
363 while ((c
= *(str
++)) != '\0')
365 if (c
== '.' && !seen
++)
380 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
382 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
383 " FILE_PATH = string\n"
384 " DATA_WIDTH = int (* (positive) number of characters *)\n"
385 " DATA_TTL = float (* (positive) number of seconds *)\n"
386 " OPTION = -i INTERVAL\n"
388 " | -x (* Output to X root window *)\n"
390 " SEPARATOR = string\n"
391 " INTERVAL = float (* (positive) number of seconds *)\n"
392 " LOG_LEVEL = int (* %d through %d *)\n"
400 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
406 /* For mutually-recursive calls. */
407 void opts_parse_any(Config
*, int, char *[], int);
410 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
415 usage("Option -i parameter is missing.\n");
417 if (!is_decimal(param
))
418 usage("Option -i parameter is invalid: \"%s\"\n", param
);
419 cfg
->interval
= atof(param
);
420 opts_parse_any(cfg
, argc
, argv
, i
);
424 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
427 usage("Option -s parameter is missing.\n");
428 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
429 strcpy(cfg
->separator
, argv
[i
]);
430 opts_parse_any(cfg
, argc
, argv
, ++i
);
434 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
440 usage("Option -l parameter is missing.\n");
442 if (!is_pos_num(param
))
443 usage("Option -l parameter is invalid: \"%s\"\n", param
);
444 log_level
= atoi(param
);
445 if (log_level
> Debug
)
447 "Option -l value (%d) exceeds maximum (%d)\n",
451 _khlib_log_level
= log_level
;
452 opts_parse_any(cfg
, argc
, argv
, i
);
456 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
458 switch (argv
[i
][1]) {
460 /* TODO: Generic set_int */
461 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
464 /* TODO: Generic set_str */
465 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
468 cfg
->output_to_x_root_window
= 1;
469 opts_parse_any(cfg
, argc
, argv
, ++i
);
472 /* TODO: Generic set_int */
473 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
476 usage("Option \"%s\" is invalid\n", argv
[i
]);
481 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
486 struct timespec in_last_read
;
491 "[spec] Parameter(s) missing for fifo \"%s\".\n",
500 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
502 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
504 in_last_read
.tv_sec
= 0;
505 in_last_read
.tv_nsec
= 0;
506 s
= calloc(1, sizeof(struct Slot
));
511 s
->out_width
= atoi(w
);
512 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
513 s
->in_last_read
= in_last_read
;
514 s
->out_pos_lo
= cfg
->total_width
;
515 s
->out_pos_cur
= s
->out_pos_lo
;
516 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
517 s
->next
= cfg
->slots
;
520 cfg
->total_width
+= s
->out_width
;
523 khlib_fatal("[memory] Allocation failure.");
525 opts_parse_any(cfg
, argc
, argv
, i
);
529 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
532 switch (argv
[i
][0]) {
534 parse_opts_opt(cfg
, argc
, argv
, i
);
537 parse_opts_spec(cfg
, argc
, argv
, i
);
543 opts_parse(Config
*cfg
, int argc
, char *argv
[])
545 opts_parse_any(cfg
, argc
, argv
, 1);
546 cfg
->slots
= slots_rev(cfg
->slots
);
550 main(int argc
, char *argv
[])
558 .output_to_x_root_window
= 0,
570 t0
, /* time stamp. before reading slots */
571 t1
, /* time stamp. after reading slots */
572 ti
, /* time interval desired (t1 - t0) */
573 td
, /* time interval measured (t1 - t0) */
574 tc
; /* time interval correction (ti - td) when td < ti */
579 opts_parse(&cfg
, argc
, argv
);
580 khlib_debug("argv0 = %s\n", argv0
);
583 ti
= khlib_timespec_of_float(cfg
.interval
);
585 if (cfg
.slots
== NULL
)
586 usage("No slot specs were given!\n");
588 /* 1st pass to check file existence and type */
589 for (s
= cfg
.slots
; s
; s
= s
->next
) {
590 if (lstat(s
->in_fifo
, &st
) < 0) {
592 "Cannot stat \"%s\". Error: %s\n",
599 if (!(st
.st_mode
& S_IFIFO
)) {
600 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
607 "Encountered errors with given file paths. See log.\n"
610 width
= cfg
.total_width
;
611 seplen
= strlen(cfg
.separator
);
613 /* 2nd pass to make space for separators */
614 for (s
= cfg
.slots
; s
; s
= s
->next
) {
615 s
->out_pos_lo
+= prefix
;
616 s
->out_pos_hi
+= prefix
;
617 s
->out_pos_cur
= s
->out_pos_lo
;
621 width
+= (seplen
* (nslots
- 1));
622 buf
= calloc(1, width
+ 1);
625 "[memory] Failed to allocate buffer of %d bytes",
628 memset(buf
, ' ', width
);
630 /* 3rd pass to set the separators */
631 for (s
= cfg
.slots
; s
; s
= s
->next
) {
632 if (s
->out_pos_lo
) { /* Skip the first, left-most */
633 /* Copying only seplen ensures we omit the '\0' byte. */
635 buf
+ (s
->out_pos_lo
- seplen
),
642 if (cfg
.output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
643 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
644 /* TODO: Handle signals */
646 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
647 slots_read(&cfg
, &ti
, buf
);
648 if (cfg
.output_to_x_root_window
) {
649 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
650 khlib_fatal("XStoreName failed.\n");
656 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
657 timespecsub(&t1
, &t0
, &td
);
659 "td {tv_sec = %ld, tv_nsec = %ld}\n",
663 if (timespeccmp(&td
, &ti
, <)) {
665 * Pushback on data producers by refusing to read the
666 * pipe more frequently than the interval.
668 timespecsub(&ti
, &td
, &tc
);
This page took 0.102172 seconds and 4 git commands to generate.