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 config_print(Config
*cfg
)
133 slots_print(cfg
->slots
);
137 is_pos_num(char *str
)
140 if (!isdigit(*(str
++)))
146 is_decimal(char *str
)
151 while ((c
= *(str
++)) != '\0')
153 if (c
== '.' && !seen
++)
168 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
170 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
171 " FILE_PATH = string\n"
172 " DATA_WIDTH = int (* (positive) number of characters *)\n"
173 " DATA_TTL = float (* (positive) number of seconds *)\n"
174 " OPTION = -i INTERVAL\n"
176 " | -x (* Output to X root window *)\n"
178 " SEPARATOR = string\n"
179 " INTERVAL = float (* (positive) number of seconds *)\n"
180 " LOG_LEVEL = int (* %d through %d *)\n"
188 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
194 /* For mutually-recursive calls. */
195 void opts_parse_any(Config
*, int, char *[], int);
198 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
203 usage("Option -i parameter is missing.\n");
205 if (!is_decimal(param
))
206 usage("Option -i parameter is invalid: \"%s\"\n", param
);
207 cfg
->interval
= atof(param
);
208 opts_parse_any(cfg
, argc
, argv
, i
);
212 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
215 usage("Option -s parameter is missing.\n");
216 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
217 strcpy(cfg
->separator
, argv
[i
]);
218 opts_parse_any(cfg
, argc
, argv
, ++i
);
222 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
228 usage("Option -l parameter is missing.\n");
230 if (!is_pos_num(param
))
231 usage("Option -l parameter is invalid: \"%s\"\n", param
);
232 log_level
= atoi(param
);
233 if (log_level
> Debug
)
235 "Option -l value (%d) exceeds maximum (%d)\n",
239 _khlib_log_level
= log_level
;
240 opts_parse_any(cfg
, argc
, argv
, i
);
244 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
246 switch (argv
[i
][1]) {
248 /* TODO: Generic set_int */
249 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
252 /* TODO: Generic set_str */
253 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
256 cfg
->output_to_x_root_window
= 1;
257 opts_parse_any(cfg
, argc
, argv
, ++i
);
260 /* TODO: Generic set_int */
261 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
264 usage("Option \"%s\" is invalid\n", argv
[i
]);
269 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
274 struct timespec in_last_read
;
279 "[spec] Parameter(s) missing for fifo \"%s\".\n",
288 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
290 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
292 in_last_read
.tv_sec
= 0;
293 in_last_read
.tv_nsec
= 0;
294 s
= calloc(1, sizeof(struct Slot
));
299 s
->out_width
= atoi(w
);
300 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
301 s
->in_last_read
= in_last_read
;
302 s
->out_pos_lo
= cfg
->total_width
;
303 s
->out_pos_cur
= s
->out_pos_lo
;
304 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
305 s
->next
= cfg
->slots
;
308 cfg
->total_width
+= s
->out_width
;
311 khlib_fatal("[memory] Allocation failure.");
313 opts_parse_any(cfg
, argc
, argv
, i
);
317 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
320 switch (argv
[i
][0]) {
322 parse_opts_opt(cfg
, argc
, argv
, i
);
325 parse_opts_spec(cfg
, argc
, argv
, i
);
331 opts_parse(Config
*cfg
, int argc
, char *argv
[])
333 opts_parse_any(cfg
, argc
, argv
, 1);
334 cfg
->slots
= slots_rev(cfg
->slots
);
338 slot_expire(Slot
*s
, struct timespec t
, char *buf
)
342 timespecsub(&t
, &(s
->in_last_read
), &td
);
343 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
344 /* TODO: Maybe configurable expiry character. */
345 memset(buf
+ s
->out_pos_lo
, '_', s
->out_pos_hi
- s
->out_pos_lo
);
346 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
351 slot_read_error(Slot
*s
, char *buf
)
356 b
= buf
+ s
->out_pos_lo
;
357 /* Copy as much of the error message as possible.
358 * EXCLUDING the terminating \0. */
359 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
361 /* Any remaining positions: */
362 for (; i
< s
->out_width
; i
++)
367 slot_read(Slot
*s
, struct timespec t
, char *buf
)
369 char c
; /* Character read. */
370 int r
; /* Remaining unused positions in buffer range. */
373 switch (read(s
->in_fd
, &c
, 1)) {
376 "Failed to read: \"%s\". errno: %d, msg: %s\n",
389 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
390 s
->out_pos_cur
= s
->out_pos_lo
;
393 /* TODO: Consider making msg term char a CLI option */
394 if (c
== '\n' || c
== '\0') {
395 r
= s
->out_pos_hi
- s
->out_pos_cur
;
397 memset(buf
+ s
->out_pos_cur
, ' ', r
);
398 s
->out_pos_cur
= s
->out_pos_lo
;
400 return END_OF_MESSAGE
;
402 if (s
->out_pos_cur
<= s
->out_pos_hi
)
403 buf
[s
->out_pos_cur
++] = c
;
404 /* Drop beyond available range. */
406 * TODO Define max after which we stop reading.
407 * To ensure that a rogue large message
408 * doesn't trap us here.
419 slots_read(Config
*cfg
, struct timespec
*ti
, char *buf
)
429 for (s
= cfg
->slots
; s
; s
= s
->next
) {
430 /* TODO: Create the FIFO if it doesn't already exist. */
431 if (lstat(s
->in_fifo
, &st
) < 0) {
433 "Cannot stat \"%s\". Error: %s\n",
437 slot_read_error(s
, buf
);
440 if (!(st
.st_mode
& S_IFIFO
)) {
441 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
442 slot_read_error(s
, buf
);
447 "%s: closed. opening. in_fd: %d\n",
451 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
454 "%s: already openned. in_fd: %d\n",
459 if (s
->in_fd
== -1) {
460 /* TODO Consider backing off retries for failed slots */
461 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
462 slot_read_error(s
, buf
);
465 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
466 if (s
->in_fd
> maxfd
)
468 FD_SET(s
->in_fd
, &fds
);
470 khlib_debug("selecting...\n");
471 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
472 khlib_debug("ready: %d\n", ready
);
473 clock_gettime(CLOCK_MONOTONIC
, &t
);
478 "pselect temp failure: %d, errno: %d, msg: %s\n",
483 /* TODO: Reconsider what to do here. */
487 "pselect failed: %d, errno: %d, msg: %s\n",
494 /* At-least-once ensures that expiries are still checked on timeouts. */
496 for (s
= cfg
->slots
; s
; s
= s
->next
) {
497 if (FD_ISSET(s
->in_fd
, &fds
)) {
498 khlib_debug("reading: %s\n", s
->in_fifo
);
499 switch (slot_read(s
, t
, buf
)) {
501 * ### MESSAGE LOSS ###
502 * is introduced by closing at EOM in addition
503 * to EOF, since there may be unread messages
504 * remaining in the pipe. However,
506 * ### INTER-MESSAGE PUSHBACK ###
507 * is also gained, since pipes block at the
510 * This is an acceptable trade-off because we
511 * are a stateless reporter of a _most-recent_
512 * status, not a stateful accumulator.
527 slot_expire(s
, t
, buf
);
535 main(int argc
, char *argv
[])
543 .output_to_x_root_window
= 0,
555 t0
, /* time stamp. before reading slots */
556 t1
, /* time stamp. after reading slots */
557 ti
, /* time interval desired (t1 - t0) */
558 td
, /* time interval measured (t1 - t0) */
559 tc
; /* time interval correction (ti - td) when td < ti */
564 opts_parse(&cfg
, argc
, argv
);
565 khlib_debug("argv0 = %s\n", argv0
);
568 ti
= khlib_timespec_of_float(cfg
.interval
);
570 if (cfg
.slots
== NULL
)
571 usage("No slot specs were given!\n");
573 /* 1st pass to check file existence and type */
574 for (s
= cfg
.slots
; s
; s
= s
->next
) {
575 if (lstat(s
->in_fifo
, &st
) < 0) {
577 "Cannot stat \"%s\". Error: %s\n",
584 if (!(st
.st_mode
& S_IFIFO
)) {
585 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
592 "Encountered errors with given file paths. See log.\n"
595 width
= cfg
.total_width
;
596 seplen
= strlen(cfg
.separator
);
598 /* 2nd pass to make space for separators */
599 for (s
= cfg
.slots
; s
; s
= s
->next
) {
600 s
->out_pos_lo
+= prefix
;
601 s
->out_pos_hi
+= prefix
;
602 s
->out_pos_cur
= s
->out_pos_lo
;
606 width
+= (seplen
* (nslots
- 1));
607 buf
= calloc(1, width
+ 1);
610 "[memory] Failed to allocate buffer of %d bytes",
613 memset(buf
, ' ', width
);
615 /* 3rd pass to set the separators */
616 for (s
= cfg
.slots
; s
; s
= s
->next
) {
617 if (s
->out_pos_lo
) { /* Skip the first, left-most */
618 /* Copying only seplen ensures we omit the '\0' byte. */
620 buf
+ (s
->out_pos_lo
- seplen
),
627 if (cfg
.output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
628 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
629 /* TODO: Handle signals */
631 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
632 slots_read(&cfg
, &ti
, buf
);
633 if (cfg
.output_to_x_root_window
) {
634 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
635 khlib_fatal("XStoreName failed.\n");
641 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
642 timespecsub(&t1
, &t0
, &td
);
644 "td {tv_sec = %ld, tv_nsec = %ld}\n",
648 if (timespeccmp(&td
, &ti
, <)) {
650 * Pushback on data producers by refusing to read the
651 * pipe more frequently than the interval.
653 timespecsub(&ti
, &td
, &tc
);
This page took 0.145172 seconds and 5 git commands to generate.