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"
27 static const char errmsg
[] = ERRMSG
;
28 static const int errlen
= sizeof(ERRMSG
) - 1;
32 /* TODO: Convert slot list to slot array. */
33 typedef struct Slot Slot
;
37 struct timespec in_last_read
;
38 struct timespec out_ttl
;
40 int out_pos_lo
; /* Lowest position on the output buffer. */
41 int out_pos_cur
; /* Current position on the output buffer. */
42 int out_pos_hi
; /* Highest position on the output buffer. */
46 typedef struct Config Config
;
53 int output_to_x_root_window
;
64 slot_print_one(Slot
*s
)
71 " in_last_read = {tv_sec = %ld, tv_nsec = %ld}"
72 " out_ttl = {tv_sec = %ld, tv_nsec = %ld},"
81 s
->in_last_read
.tv_sec
,
82 s
->in_last_read
.tv_nsec
,
93 slot_print_all(Slot
*head
)
95 for (Slot
*s
= head
; s
; s
= s
->next
) {
101 config_print(Config
*cfg
)
117 slot_print_all(cfg
->slots
);
121 is_pos_num(char *str
)
124 if (!isdigit(*(str
++)))
130 is_decimal(char *str
)
135 while ((c
= *(str
++)) != '\0')
137 if (c
== '.' && !seen
++)
152 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
154 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
155 " FILE_PATH = string\n"
156 " DATA_WIDTH = int (* (positive) number of characters *)\n"
157 " DATA_TTL = float (* (positive) number of seconds *)\n"
158 " OPTION = -i INTERVAL\n"
160 " | -x (* Output to X root window *)\n"
162 " SEPARATOR = string\n"
163 " INTERVAL = float (* (positive) number of seconds *)\n"
164 " LOG_LEVEL = int (* %d through %d *)\n"
172 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
178 /* For mutually-recursive calls. */
179 void opts_parse_any(Config
*, int, char *[], int);
182 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
187 usage("Option -i parameter is missing.\n");
189 if (!is_decimal(param
))
190 usage("Option -i parameter is invalid: \"%s\"\n", param
);
191 cfg
->interval
= atof(param
);
192 opts_parse_any(cfg
, argc
, argv
, i
);
196 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
199 usage("Option -s parameter is missing.\n");
200 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
201 strcpy(cfg
->separator
, argv
[i
]);
202 opts_parse_any(cfg
, argc
, argv
, ++i
);
206 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
212 usage("Option -l parameter is missing.\n");
214 if (!is_pos_num(param
))
215 usage("Option -l parameter is invalid: \"%s\"\n", param
);
216 log_level
= atoi(param
);
217 if (log_level
> Debug
)
219 "Option -l value (%d) exceeds maximum (%d)\n",
223 _khlib_log_level
= log_level
;
224 opts_parse_any(cfg
, argc
, argv
, i
);
228 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
230 switch (argv
[i
][1]) {
232 /* TODO: Generic set_int */
233 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
236 /* TODO: Generic set_str */
237 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
240 cfg
->output_to_x_root_window
= 1;
241 opts_parse_any(cfg
, argc
, argv
, ++i
);
244 /* TODO: Generic set_int */
245 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
248 usage("Option \"%s\" is invalid\n", argv
[i
]);
253 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
257 "[spec] Parameter(s) missing for fifo \"%s\".\n",
265 struct timespec in_last_read
;
268 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
270 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
271 in_last_read
.tv_sec
= 0;
272 in_last_read
.tv_nsec
= 0;
273 Slot
*s
= calloc(1, sizeof(struct Slot
));
277 s
->out_width
= atoi(w
);
278 s
->out_ttl
= khlib_timespec_of_float(atof(t
));
279 s
->in_last_read
= in_last_read
;
280 s
->out_pos_lo
= cfg
->total_width
;
281 s
->out_pos_cur
= s
->out_pos_lo
;
282 s
->out_pos_hi
= s
->out_pos_lo
+ s
->out_width
- 1;
283 s
->next
= cfg
->slots
;
286 cfg
->total_width
+= s
->out_width
;
289 khlib_fatal("[memory] Allocation failure.");
291 opts_parse_any(cfg
, argc
, argv
, i
);
295 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
298 switch (argv
[i
][0]) {
300 parse_opts_opt(cfg
, argc
, argv
, i
);
303 parse_opts_spec(cfg
, argc
, argv
, i
);
309 opts_parse(Config
*cfg
, int argc
, char *argv
[])
311 opts_parse_any(cfg
, argc
, argv
, 1);
313 Slot
*last
= cfg
->slots
;
315 for (Slot
*s
= last
; s
; ) {
316 Slot
*next
= s
->next
;
317 s
->next
= cfg
->slots
;
324 slot_expire(Slot
*s
, struct timespec t
, char *buf
)
328 timespecsub(&t
, &(s
->in_last_read
), &td
);
329 if (timespeccmp(&td
, &(s
->out_ttl
), >=)) {
330 /* TODO: Maybe configurable expiry character. */
331 memset(buf
+ s
->out_pos_lo
, '_', s
->out_pos_hi
- s
->out_pos_lo
);
332 khlib_warn("Slot expired: \"%s\"\n", s
->in_fifo
);
337 slot_read_error(Slot
*s
, char *buf
)
342 b
= buf
+ s
->out_pos_lo
;
343 /* Copy as much of the error message as possible.
344 * EXCLUDING the terminating \0. */
345 for (i
= 0; i
< errlen
&& i
< s
->out_width
; i
++)
347 /* Any remaining slots: */
348 for (; i
< s
->out_width
; i
++)
353 slot_read_one(Slot
*s
, struct timespec t
, char *buf
)
355 char c
; /* Character read. */
356 int r
; /* Remaining unused slots in buffer range. */
359 switch (read(s
->in_fd
, &c
, 1)) {
362 "Failed to read: \"%s\". errno: %d, msg: %s\n",
375 khlib_debug("%s: End of FILE\n", s
->in_fifo
);
376 s
->out_pos_cur
= s
->out_pos_lo
;
379 /* TODO: Consider making msg term char a CLI option */
380 if (c
== '\n' || c
== '\0') {
381 r
= s
->out_pos_hi
- s
->out_pos_cur
;
383 memset(buf
+ s
->out_pos_cur
, ' ', r
);
384 s
->out_pos_cur
= s
->out_pos_lo
;
386 return END_OF_MESSAGE
;
388 if (s
->out_pos_cur
<= s
->out_pos_hi
)
389 buf
[s
->out_pos_cur
++] = c
;
390 /* Drop beyond available range. */
392 * TODO Define max after which we stop reading.
393 * To ensure that a rogue large message
394 * doesn't trap us here.
405 slot_read_all(Config
*cfg
, struct timespec
*ti
, char *buf
)
414 for (Slot
*s
= cfg
->slots
; s
; s
= s
->next
) {
415 /* TODO: Create the FIFO if it doesn't already exist. */
416 if (lstat(s
->in_fifo
, &st
) < 0) {
418 "Cannot stat \"%s\". Error: %s\n",
422 slot_read_error(s
, buf
);
425 if (!(st
.st_mode
& S_IFIFO
)) {
426 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
427 slot_read_error(s
, buf
);
432 "%s: closed. opening. in_fd: %d\n",
436 s
->in_fd
= open(s
->in_fifo
, O_RDONLY
| O_NONBLOCK
);
439 "%s: already openned. in_fd: %d\n",
444 if (s
->in_fd
== -1) {
445 /* TODO Consider backing off retries for failed slots */
446 khlib_error("Failed to open \"%s\"\n", s
->in_fifo
);
447 slot_read_error(s
, buf
);
450 khlib_debug("%s: open. in_fd: %d\n", s
->in_fifo
, s
->in_fd
);
451 if (s
->in_fd
> maxfd
)
453 FD_SET(s
->in_fd
, &fds
);
455 khlib_debug("selecting...\n");
456 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
457 khlib_debug("ready: %d\n", ready
);
458 clock_gettime(CLOCK_MONOTONIC
, &t
);
463 "pselect temp failure: %d, errno: %d, msg: %s\n",
468 /* TODO: Reconsider what to do here. */
472 "pselect failed: %d, errno: %d, msg: %s\n",
479 /* At-least-once ensures that expiries are still checked on timeouts. */
481 for (Slot
*s
= cfg
->slots
; s
; s
= s
->next
) {
482 if (FD_ISSET(s
->in_fd
, &fds
)) {
483 khlib_debug("reading: %s\n", s
->in_fifo
);
484 switch (slot_read_one(s
, t
, buf
)) {
486 * ### MESSAGE LOSS ###
487 * is introduced by closing at EOM in addition
488 * to EOF, since there may be unread messages
489 * remaining in the pipe. However,
491 * ### INTER-MESSAGE PUSHBACK ###
492 * is also gained, since pipes block at the
495 * This is an acceptable trade-off because we
496 * are a stateless reporter of a _most-recent_
497 * status, not a stateful accumulator.
512 slot_expire(s
, t
, buf
);
520 main(int argc
, char *argv
[])
528 .output_to_x_root_window
= 0,
540 t0
, /* time stamp. before reading slots */
541 t1
, /* time stamp. after reading slots */
542 ti
, /* time interval desired (t1 - t0) */
543 td
, /* time interval measured (t1 - t0) */
544 tc
; /* time interval correction (ti - td) when td < ti */
548 opts_parse(&cfg
, argc
, argv
);
549 khlib_debug("argv0 = %s\n", argv0
);
552 ti
= khlib_timespec_of_float(cfg
.interval
);
554 if (cfg
.slots
== NULL
)
555 usage("No slot specs were given!\n");
557 /* 1st pass to check file existence and type */
558 for (Slot
*s
= cfg
.slots
; s
; s
= s
->next
) {
559 if (lstat(s
->in_fifo
, &st
) < 0) {
561 "Cannot stat \"%s\". Error: %s\n",
568 if (!(st
.st_mode
& S_IFIFO
)) {
569 khlib_error("\"%s\" is not a FIFO\n", s
->in_fifo
);
576 "Encountered errors with given file paths. See log.\n"
579 width
= cfg
.total_width
;
580 seplen
= strlen(cfg
.separator
);
582 /* 2nd pass to make space for separators */
583 for (Slot
*s
= cfg
.slots
; s
; s
= s
->next
) {
584 s
->out_pos_lo
+= prefix
;
585 s
->out_pos_hi
+= prefix
;
586 s
->out_pos_cur
= s
->out_pos_lo
;
590 width
+= (seplen
* (nslots
- 1));
591 buf
= calloc(1, width
+ 1);
594 "[memory] Failed to allocate buffer of %d bytes",
597 memset(buf
, ' ', width
);
599 /* 3rd pass to set the separators */
600 for (Slot
*s
= cfg
.slots
; s
; s
= s
->next
) {
601 if (s
->out_pos_lo
) { /* Skip the first, left-most */
602 /* Copying only seplen ensures we omit the '\0' byte. */
604 buf
+ (s
->out_pos_lo
- seplen
),
611 if (cfg
.output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
612 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
613 /* TODO: Handle signals */
615 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
616 slot_read_all(&cfg
, &ti
, buf
);
617 if (cfg
.output_to_x_root_window
) {
618 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
619 khlib_fatal("XStoreName failed.\n");
625 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
626 timespecsub(&t1
, &t0
, &td
);
628 "td {tv_sec = %ld, tv_nsec = %ld}\n",
632 if (timespeccmp(&td
, &ti
, <)) {
633 /* Pushback on data producers by refusing to read the
634 * pipe more frequently than the interval.
636 timespecsub(&ti
, &td
, &tc
);
637 khlib_debug("khlib_sleep YES\n");
640 khlib_debug("khlib_sleep NO\n");
This page took 0.197453 seconds and 4 git commands to generate.