1 #include <sys/select.h>
16 #include "bsdtimespec.h"
17 #include "khatus_lib_log.h"
18 #include "khatus_lib_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
;
60 .output_to_x_root_window
= 0,
71 fifo_print_one(Fifo
*f
)
78 " last_read = {tv_sec = %ld, tv_nsec = %ld}"
79 " ttl = {tv_sec = %ld, tv_nsec = %ld},"
100 fifo_print_all(Fifo
*head
)
102 for (Fifo
*f
= head
; f
; f
= f
->next
) {
108 config_print(Config
*cfg
)
124 fifo_print_all(cfg
->fifos
);
131 if (!isdigit(*(s
++)))
142 while ((c
= *(s
++)) != '\0')
144 if (c
== '.' && !seen
++)
159 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
161 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
162 " FILE_PATH = string\n"
163 " DATA_WIDTH = int (* (positive) number of characters *)\n"
164 " DATA_TTL = float (* (positive) number of seconds *)\n"
165 " OPTION = -i INTERVAL\n"
167 " | -x (* Output to X root window *)\n"
169 " SEPARATOR = string\n"
170 " INTERVAL = float (* (positive) number of seconds *)\n"
171 " LOG_LEVEL = int (* %d through %d *)\n"
179 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
185 /* For mutually-recursive calls. */
186 void opts_parse_any(Config
*, int, char *[], int);
189 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
194 usage("Option -i parameter is missing.\n");
196 if (!is_decimal(param
))
197 usage("Option -i parameter is invalid: \"%s\"\n", param
);
198 cfg
->interval
= atof(param
);
199 opts_parse_any(cfg
, argc
, argv
, i
);
203 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
206 usage("Option -s parameter is missing.\n");
207 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
208 strcpy(cfg
->separator
, argv
[i
]);
209 opts_parse_any(cfg
, argc
, argv
, ++i
);
213 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
219 usage("Option -l parameter is missing.\n");
221 if (!is_pos_num(param
))
222 usage("Option -l parameter is invalid: \"%s\"\n", param
);
223 log_level
= atoi(param
);
224 if (log_level
> Debug
)
226 "Option -l value (%d) exceeds maximum (%d)\n",
230 _khatus_lib_log_level
= log_level
;
231 opts_parse_any(cfg
, argc
, argv
, i
);
235 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
237 switch (argv
[i
][1]) {
239 /* TODO: Generic set_int */
240 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
243 /* TODO: Generic set_str */
244 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
247 cfg
->output_to_x_root_window
= 1;
248 opts_parse_any(cfg
, argc
, argv
, ++i
);
251 /* TODO: Generic set_int */
252 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
255 usage("Option \"%s\" is invalid\n", argv
[i
]);
260 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
264 "[spec] Parameter(s) missing for fifo \"%s\".\n",
272 struct timespec last_read
;
275 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
277 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
278 last_read
.tv_sec
= 0;
279 last_read
.tv_nsec
= 0;
280 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
285 f
->ttl
= timespec_of_float(atof(t
));
286 f
->last_read
= last_read
;
287 f
->pos_init
= cfg
->total_width
;
288 f
->pos_curr
= f
->pos_init
;
289 f
->pos_final
= f
->pos_init
+ f
->width
- 1;
290 f
->next
= cfg
->fifos
;
293 cfg
->total_width
+= f
->width
;
296 fatal("[memory] Allocation failure.");
298 opts_parse_any(cfg
, argc
, argv
, i
);
302 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
305 switch (argv
[i
][0]) {
307 parse_opts_opt(cfg
, argc
, argv
, i
);
310 parse_opts_spec(cfg
, argc
, argv
, i
);
316 opts_parse(Config
*cfg
, int argc
, char *argv
[])
318 opts_parse_any(cfg
, argc
, argv
, 1);
320 Fifo
*last
= cfg
->fifos
;
322 for (Fifo
*f
= last
; f
; ) {
323 Fifo
*next
= f
->next
;
324 f
->next
= cfg
->fifos
;
331 fifo_expire(Fifo
*f
, struct timespec t
, char *buf
)
335 timespecsub(&t
, &(f
->last_read
), &td
);
336 if (timespeccmp(&td
, &(f
->ttl
), >=)) {
337 /* TODO: Maybe configurable expiry character. */
338 memset(buf
+ f
->pos_init
, '_', f
->pos_final
- f
->pos_init
);
339 warn("Data source expired: \"%s\"\n", f
->name
);
344 fifo_read_error(Fifo
*f
, char *buf
)
349 b
= buf
+ f
->pos_init
;
350 /* Copy as much of the error message as possible.
351 * EXCLUDING the terminating \0. */
352 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
354 /* Any remaining slots: */
355 for (; i
< f
->width
; i
++)
360 fifo_read_one(Fifo
*f
, struct timespec t
, char *buf
)
362 char c
; /* Character read. */
363 int r
; /* Remaining unused slots in buffer range. */
366 switch (read(f
->fd
, &c
, 1)) {
368 error("Failed to read: \"%s\". errno: %d, msg: %s\n",
369 f
->name
, errno
, strerror(errno
));
378 debug("%s: End of FILE\n", f
->name
);
379 f
->pos_curr
= f
->pos_init
;
382 /* TODO: Consider making msg term char a CLI option */
383 if (c
== '\n' || c
== '\0') {
384 r
= f
->pos_final
- f
->pos_curr
;
386 memset(buf
+ f
->pos_curr
, ' ', r
);
387 f
->pos_curr
= f
->pos_init
;
389 return END_OF_MESSAGE
;
391 if (f
->pos_curr
<= f
->pos_final
)
392 buf
[f
->pos_curr
++] = c
;
393 /* Drop beyond available range. */
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 error("\"%s\" is not a FIFO\n", f
->name
);
425 fifo_read_error(f
, buf
);
429 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
430 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
432 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
435 /* TODO Consider backing off retries for failed fifos */
436 error("Failed to open \"%s\"\n", f
->name
);
437 fifo_read_error(f
, buf
);
440 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
445 debug("selecting...\n");
446 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
447 debug("ready: %d\n", ready
);
448 clock_gettime(CLOCK_MONOTONIC
, &t
);
452 error("pselect temp failure: %d, errno: %d, msg: %s\n",
453 ready
, errno
, strerror(errno
));
454 /* TODO: Reconsider what to do here. */
457 fatal("pselect failed: %d, errno: %d, msg: %s\n",
458 ready
, errno
, strerror(errno
));
461 /* At-least-once ensures that expiries are still checked on timeouts. */
463 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
464 if (FD_ISSET(f
->fd
, &fds
)) {
465 debug("reading: %s\n", f
->name
);
466 switch (fifo_read_one(f
, t
, buf
)) {
468 * ### MESSAGE LOSS ###
469 * is introduced by closing at EOM in addition
470 * to EOF, since there may be unread messages
471 * remaining in the pipe. However,
473 * ### INTER-MESSAGE PUSHBACK ###
474 * is also gained, since pipes block at the
477 * This is an acceptable trade-off because we
478 * are a stateless reporter of a _most-recent_
479 * status, not a stateful accumulator.
494 fifo_expire(f
, t
, buf
);
502 main(int argc
, char *argv
[])
510 Config cfg0
= defaults
;
515 t0
, /* time stamp. before reading fifos */
516 t1
, /* time stamp. after reading fifos */
517 ti
, /* time interval desired (t1 - t0) */
518 td
, /* time interval measured (t1 - t0) */
519 tc
; /* time interval correction (ti - td) when td < ti */
523 opts_parse(cfg
, argc
, argv
);
524 debug("argv0 = %s\n", argv0
);
527 ti
= timespec_of_float(cfg
->interval
);
529 if (cfg
->fifos
== NULL
)
530 usage("No fifo specs were given!\n");
532 /* 1st pass to check file existence and type */
533 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
534 if (lstat(f
->name
, &st
) < 0) {
536 "Cannot stat \"%s\". Error: %s\n",
543 if (!(st
.st_mode
& S_IFIFO
)) {
544 error("\"%s\" is not a FIFO\n", f
->name
);
550 fatal("Encountered errors with given file paths. See log.\n");
552 width
= cfg
->total_width
;
553 seplen
= strlen(cfg
->separator
);
555 /* 2nd pass to make space for separators */
556 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
557 f
->pos_init
+= prefix
;
558 f
->pos_final
+= prefix
;
559 f
->pos_curr
= f
->pos_init
;
563 width
+= (seplen
* (nfifos
- 1));
564 buf
= calloc(1, width
+ 1);
566 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
567 memset(buf
, ' ', width
);
569 /* 3rd pass to set the separators */
570 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
571 if (f
->pos_init
) { /* Skip the first, left-most */
572 /* Copying only seplen ensures we omit the '\0' byte. */
574 buf
+ (f
->pos_init
- seplen
),
581 if (cfg
->output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
582 fatal("XOpenDisplay failed with: %p\n", d
);
583 /* TODO: Handle signals */
585 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
586 fifo_read_all(cfg
, &ti
, buf
);
587 if (cfg
->output_to_x_root_window
) {
588 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
589 fatal("XStoreName failed.\n");
595 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
596 timespecsub(&t1
, &t0
, &td
);
598 "td {tv_sec = %ld, tv_nsec = %ld}\n",
602 if (timespeccmp(&td
, &ti
, <)) {
603 /* Pushback on data producers by refusing to read the
604 * pipe more frequently than the interval.
606 timespecsub(&ti
, &td
, &tc
);
607 debug("snooze YES\n");
610 debug("snooze NO\n");
This page took 0.118218 seconds and 4 git commands to generate.