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 fifo list to fifo array. */
33 typedef struct Fifo Fifo
;
38 struct timespec last_read
;
40 int pos_init
; /* Initial position on the output buffer. */
41 int pos_curr
; /* Current position on the output buffer. */
42 int pos_final
; /* Final position on the output buffer. */
46 typedef struct Config Config
;
53 int output_to_x_root_window
;
64 fifo_print_one(Fifo
*f
)
71 " last_read = {tv_sec = %ld, tv_nsec = %ld}"
72 " ttl = {tv_sec = %ld, tv_nsec = %ld},"
93 fifo_print_all(Fifo
*head
)
95 for (Fifo
*f
= head
; f
; f
= f
->next
) {
101 config_print(Config
*cfg
)
117 fifo_print_all(cfg
->fifos
);
124 if (!isdigit(*(s
++)))
135 while ((c
= *(s
++)) != '\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 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 last_read
.tv_sec
= 0;
272 last_read
.tv_nsec
= 0;
273 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
278 f
->ttl
= khlib_timespec_of_float(atof(t
));
279 f
->last_read
= last_read
;
280 f
->pos_init
= cfg
->total_width
;
281 f
->pos_curr
= f
->pos_init
;
282 f
->pos_final
= f
->pos_init
+ f
->width
- 1;
283 f
->next
= cfg
->fifos
;
286 cfg
->total_width
+= f
->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 Fifo
*last
= cfg
->fifos
;
315 for (Fifo
*f
= last
; f
; ) {
316 Fifo
*next
= f
->next
;
317 f
->next
= cfg
->fifos
;
324 fifo_expire(Fifo
*f
, struct timespec t
, char *buf
)
328 timespecsub(&t
, &(f
->last_read
), &td
);
329 if (timespeccmp(&td
, &(f
->ttl
), >=)) {
330 /* TODO: Maybe configurable expiry character. */
331 memset(buf
+ f
->pos_init
, '_', f
->pos_final
- f
->pos_init
);
332 khlib_warn("Data source expired: \"%s\"\n", f
->name
);
337 fifo_read_error(Fifo
*f
, char *buf
)
342 b
= buf
+ f
->pos_init
;
343 /* Copy as much of the error message as possible.
344 * EXCLUDING the terminating \0. */
345 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
347 /* Any remaining slots: */
348 for (; i
< f
->width
; i
++)
353 fifo_read_one(Fifo
*f
, struct timespec t
, char *buf
)
355 char c
; /* Character read. */
356 int r
; /* Remaining unused slots in buffer range. */
359 switch (read(f
->fd
, &c
, 1)) {
362 "Failed to read: \"%s\". errno: %d, msg: %s\n",
363 f
->name
, errno
, strerror(errno
)
373 khlib_debug("%s: End of FILE\n", f
->name
);
374 f
->pos_curr
= f
->pos_init
;
377 /* TODO: Consider making msg term char a CLI option */
378 if (c
== '\n' || c
== '\0') {
379 r
= f
->pos_final
- f
->pos_curr
;
381 memset(buf
+ f
->pos_curr
, ' ', r
);
382 f
->pos_curr
= f
->pos_init
;
384 return END_OF_MESSAGE
;
386 if (f
->pos_curr
<= f
->pos_final
)
387 buf
[f
->pos_curr
++] = c
;
388 /* Drop beyond available range. */
390 * TODO Define max after which we stop reading.
391 * To ensure that a rogue large message
392 * doesn't trap us here.
403 fifo_read_all(Config
*cfg
, struct timespec
*ti
, char *buf
)
412 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
413 /* TODO: Create the FIFO if it doesn't already exist. */
414 if (lstat(f
->name
, &st
) < 0) {
416 "Cannot stat \"%s\". Error: %s\n",
420 fifo_read_error(f
, buf
);
423 if (!(st
.st_mode
& S_IFIFO
)) {
424 khlib_error("\"%s\" is not a FIFO\n", f
->name
);
425 fifo_read_error(f
, buf
);
430 "%s: closed. opening. fd: %d\n",
434 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
437 "%s: already openned. fd: %d\n",
443 /* TODO Consider backing off retries for failed fifos */
444 khlib_error("Failed to open \"%s\"\n", f
->name
);
445 fifo_read_error(f
, buf
);
448 khlib_debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
453 khlib_debug("selecting...\n");
454 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
455 khlib_debug("ready: %d\n", ready
);
456 clock_gettime(CLOCK_MONOTONIC
, &t
);
461 "pselect temp failure: %d, errno: %d, msg: %s\n",
466 /* TODO: Reconsider what to do here. */
470 "pselect failed: %d, errno: %d, msg: %s\n",
477 /* At-least-once ensures that expiries are still checked on timeouts. */
479 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
480 if (FD_ISSET(f
->fd
, &fds
)) {
481 khlib_debug("reading: %s\n", f
->name
);
482 switch (fifo_read_one(f
, t
, buf
)) {
484 * ### MESSAGE LOSS ###
485 * is introduced by closing at EOM in addition
486 * to EOF, since there may be unread messages
487 * remaining in the pipe. However,
489 * ### INTER-MESSAGE PUSHBACK ###
490 * is also gained, since pipes block at the
493 * This is an acceptable trade-off because we
494 * are a stateless reporter of a _most-recent_
495 * status, not a stateful accumulator.
510 fifo_expire(f
, t
, buf
);
518 main(int argc
, char *argv
[])
526 .output_to_x_root_window
= 0,
538 t0
, /* time stamp. before reading fifos */
539 t1
, /* time stamp. after reading fifos */
540 ti
, /* time interval desired (t1 - t0) */
541 td
, /* time interval measured (t1 - t0) */
542 tc
; /* time interval correction (ti - td) when td < ti */
546 opts_parse(&cfg
, argc
, argv
);
547 khlib_debug("argv0 = %s\n", argv0
);
550 ti
= khlib_timespec_of_float(cfg
.interval
);
552 if (cfg
.fifos
== NULL
)
553 usage("No fifo specs were given!\n");
555 /* 1st pass to check file existence and type */
556 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
557 if (lstat(f
->name
, &st
) < 0) {
559 "Cannot stat \"%s\". Error: %s\n",
566 if (!(st
.st_mode
& S_IFIFO
)) {
567 khlib_error("\"%s\" is not a FIFO\n", f
->name
);
574 "Encountered errors with given file paths. See log.\n"
577 width
= cfg
.total_width
;
578 seplen
= strlen(cfg
.separator
);
580 /* 2nd pass to make space for separators */
581 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
582 f
->pos_init
+= prefix
;
583 f
->pos_final
+= prefix
;
584 f
->pos_curr
= f
->pos_init
;
588 width
+= (seplen
* (nfifos
- 1));
589 buf
= calloc(1, width
+ 1);
592 "[memory] Failed to allocate buffer of %d bytes",
595 memset(buf
, ' ', width
);
597 /* 3rd pass to set the separators */
598 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
599 if (f
->pos_init
) { /* Skip the first, left-most */
600 /* Copying only seplen ensures we omit the '\0' byte. */
602 buf
+ (f
->pos_init
- seplen
),
609 if (cfg
.output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
610 khlib_fatal("XOpenDisplay failed with: %p\n", d
);
611 /* TODO: Handle signals */
613 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
614 fifo_read_all(&cfg
, &ti
, buf
);
615 if (cfg
.output_to_x_root_window
) {
616 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
617 khlib_fatal("XStoreName failed.\n");
623 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
624 timespecsub(&t1
, &t0
, &td
);
626 "td {tv_sec = %ld, tv_nsec = %ld}\n",
630 if (timespeccmp(&td
, &ti
, <)) {
631 /* Pushback on data producers by refusing to read the
632 * pipe more frequently than the interval.
634 timespecsub(&ti
, &td
, &tc
);
635 khlib_debug("khlib_sleep YES\n");
638 khlib_debug("khlib_sleep NO\n");
This page took 0.188425 seconds and 5 git commands to generate.