1 #include <sys/select.h>
16 #include "bsdtimespec.h"
17 #include "khatus_lib_log.h"
18 #include "khatus_lib_time.h"
20 #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
21 #define ERRMSG "ERROR"
23 static const char errmsg
[] = ERRMSG
;
24 static const int errlen
= sizeof(ERRMSG
) - 1;
28 /* TODO: Convert fifo list to fifo array. */
29 typedef struct Fifo Fifo
;
34 struct timespec last_read
;
36 int pos_init
; /* Initial position on the output buffer. */
37 int pos_curr
; /* Current position on the output buffer. */
38 int pos_final
; /* Final position on the output buffer. */
42 typedef struct Config Config
;
49 int output_to_x_root_window
;
56 .output_to_x_root_window
= 0,
67 fifo_print_one(Fifo
*f
)
74 " last_read = {tv_sec = %ld, tv_nsec = %ld}"
75 " ttl = {tv_sec = %ld, tv_nsec = %ld},"
96 fifo_print_all(Fifo
*head
)
98 for (Fifo
*f
= head
; f
; f
= f
->next
) {
104 config_print(Config
*cfg
)
120 fifo_print_all(cfg
->fifos
);
127 if (!isdigit(*(s
++)))
138 while ((c
= *(s
++)) != '\0')
140 if (c
== '.' && !seen
++)
155 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
157 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
158 " FILE_PATH = string\n"
159 " DATA_WIDTH = int (* (positive) number of characters *)\n"
160 " DATA_TTL = float (* (positive) number of seconds *)\n"
161 " OPTION = -i INTERVAL\n"
163 " | -x (* Output to X root window *)\n"
165 " SEPARATOR = string\n"
166 " INTERVAL = float (* (positive) number of seconds *)\n"
167 " LOG_LEVEL = int (* %d through %d *)\n"
175 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
181 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
184 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
189 usage("Option -i parameter is missing.\n");
191 if (!is_decimal(param
))
192 usage("Option -i parameter is invalid: \"%s\"\n", param
);
193 cfg
->interval
= atof(param
);
194 opts_parse_any(cfg
, argc
, argv
, i
);
198 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
201 usage("Option -s parameter is missing.\n");
202 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
203 strcpy(cfg
->separator
, argv
[i
]);
204 opts_parse_any(cfg
, argc
, argv
, ++i
);
208 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
214 usage("Option -l parameter is missing.\n");
216 if (!is_pos_num(param
))
217 usage("Option -l parameter is invalid: \"%s\"\n", param
);
218 log_level
= atoi(param
);
219 if (log_level
> Debug
)
220 usage("Option -l value (%d) exceeds maximum (%d)\n", log_level
, Debug
);
221 _khatus_lib_log_level
= log_level
;
222 opts_parse_any(cfg
, argc
, argv
, i
);
226 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
228 switch (argv
[i
][1]) {
230 /* TODO: Generic set_int */
231 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
234 /* TODO: Generic set_str */
235 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
238 cfg
->output_to_x_root_window
= 1;
239 opts_parse_any(cfg
, argc
, argv
, ++i
);
242 /* TODO: Generic set_int */
243 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
246 usage("Option \"%s\" is invalid\n", argv
[i
]);
251 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
254 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
260 struct timespec last_read
;
263 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
265 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
266 last_read
.tv_sec
= 0;
267 last_read
.tv_nsec
= 0;
268 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
273 f
->ttl
= timespec_of_float(atof(t
));
274 f
->last_read
= last_read
;
275 f
->pos_init
= cfg
->total_width
;
276 f
->pos_curr
= f
->pos_init
;
277 f
->pos_final
= f
->pos_init
+ f
->width
- 1;
278 f
->next
= cfg
->fifos
;
281 cfg
->total_width
+= f
->width
;
284 fatal("[memory] Allocation failure.");
286 opts_parse_any(cfg
, argc
, argv
, i
);
290 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
293 switch (argv
[i
][0]) {
295 parse_opts_opt(cfg
, argc
, argv
, i
);
298 parse_opts_spec(cfg
, argc
, argv
, i
);
304 opts_parse(Config
*cfg
, int argc
, char *argv
[])
306 opts_parse_any(cfg
, argc
, argv
, 1);
308 Fifo
*last
= cfg
->fifos
;
310 for (Fifo
*f
= last
; f
; ) {
311 Fifo
*next
= f
->next
;
312 f
->next
= cfg
->fifos
;
319 fifo_expire(Fifo
*f
, struct timespec t
, char *buf
)
323 timespecsub(&t
, &(f
->last_read
), &td
);
324 if (timespeccmp(&td
, &(f
->ttl
), >=)) {
325 /* TODO: Maybe configurable expiry character. */
326 memset(buf
+ f
->pos_init
, '_', f
->pos_final
- f
->pos_init
);
327 warn("Data source expired: \"%s\"\n", f
->name
);
332 fifo_read_error(Fifo
*f
, char *buf
)
337 b
= buf
+ f
->pos_init
;
338 /* Copy as much of the error message as possible.
339 * EXCLUDING the terminating \0. */
340 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
342 /* Any remaining slots: */
343 for (; i
< f
->width
; i
++)
348 fifo_read_one(Fifo
*f
, struct timespec t
, char *buf
)
350 char c
; /* Character read. */
351 int r
; /* Remaining unused slots in buffer range. */
354 switch (read(f
->fd
, &c
, 1)) {
356 error("Failed to read: \"%s\". errno: %d, msg: %s\n",
357 f
->name
, errno
, strerror(errno
));
366 debug("%s: End of FILE\n", f
->name
);
367 f
->pos_curr
= f
->pos_init
;
370 /* TODO: Consider making msg term char a CLI option */
371 if (c
== '\n' || c
== '\0') {
372 r
= f
->pos_final
- f
->pos_curr
;
374 memset(buf
+ f
->pos_curr
, ' ', r
);
375 f
->pos_curr
= f
->pos_init
;
377 return END_OF_MESSAGE
;
379 if (f
->pos_curr
<= f
->pos_final
)
380 buf
[f
->pos_curr
++] = c
;
381 /* Drop beyond available range. */
391 fifo_read_all(Config
*cfg
, struct timespec
*ti
, char *buf
)
400 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
401 /* TODO: Create the FIFO if it doesn't already exist. */
402 if (lstat(f
->name
, &st
) < 0) {
403 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
404 fifo_read_error(f
, buf
);
407 if (!(st
.st_mode
& S_IFIFO
)) {
408 error("\"%s\" is not a FIFO\n", f
->name
);
409 fifo_read_error(f
, buf
);
413 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
414 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
416 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
419 /* TODO: Consider backing off retries for failed fifos. */
420 error("Failed to open \"%s\"\n", f
->name
);
421 fifo_read_error(f
, buf
);
424 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
429 debug("selecting...\n");
430 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
431 debug("ready: %d\n", ready
);
433 clock_gettime(CLOCK_MONOTONIC
, &t
);
434 /* At-least-once ensures that expiries are still checked on timeouts. */
436 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
437 if (FD_ISSET(f
->fd
, &fds
)) {
438 debug("reading: %s\n", f
->name
);
439 switch (fifo_read_one(f
, t
, buf
)) {
441 * ### MESSAGE LOSS ###
442 * is introduced by closing at EOM in addition
443 * to EOF, since there may be unread messages
444 * remaining in the pipe. However,
446 * ### INTER-MESSAGE PUSHBACK ###
447 * is also gained, since pipes block at the
450 * This is an acceptable trade-off because we
451 * are a stateless reporter of a _most-recent_
452 * status, not a stateful accumulator.
467 fifo_expire(f
, t
, buf
);
475 main(int argc
, char *argv
[])
483 Config cfg0
= defaults
;
485 Display
*display
= NULL
;
488 t0
, /* time stamp. before reading fifos */
489 t1
, /* time stamp. after reading fifos */
490 ti
, /* time interval desired (t1 - t0) */
491 td
, /* time interval measured (t1 - t0) */
492 tc
; /* time interval correction (ti - td) when td < ti */
496 opts_parse(cfg
, argc
, argv
);
497 debug("argv0 = %s\n", argv0
);
500 ti
= timespec_of_float(cfg
->interval
);
502 if (cfg
->fifos
== NULL
)
503 usage("No fifo specs were given!\n");
505 /* 1st pass to check file existence and type */
506 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
507 if (lstat(f
->name
, &st
) < 0) {
508 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
512 if (!(st
.st_mode
& S_IFIFO
)) {
513 error("\"%s\" is not a FIFO\n", f
->name
);
519 fatal("Encountered errors with the given file paths. See log.\n");
521 width
= cfg
->total_width
;
522 seplen
= strlen(cfg
->separator
);
524 /* 2nd pass to make space for separators */
525 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
526 f
->pos_init
+= prefix
;
527 f
->pos_final
+= prefix
;
528 f
->pos_curr
= f
->pos_init
;
532 width
+= (seplen
* (nfifos
- 1));
533 buf
= calloc(1, width
+ 1);
535 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
536 memset(buf
, ' ', width
);
538 /* 3rd pass to set the separators */
539 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
540 if (f
->pos_init
) { /* Skip the first, left-most */
541 /* Copying only seplen ensures we omit the '\0' byte. */
542 strncpy(buf
+ (f
->pos_init
- seplen
), cfg
->separator
, seplen
);
546 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
547 fatal("XOpenDisplay failed with: %p\n", display
);
548 /* TODO: Handle signals */
550 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
551 fifo_read_all(cfg
, &ti
, buf
);
552 if (cfg
->output_to_x_root_window
) {
553 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
554 fatal("XStoreName failed.\n");
560 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
561 timespecsub(&t1
, &t0
, &td
);
562 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
563 if (timespeccmp(&td
, &ti
, <)) {
564 /* Pushback on data producers by refusing to read the
565 * pipe more frequently than the interval.
567 timespecsub(&ti
, &td
, &tc
);
568 debug("snooze YES\n");
571 debug("snooze NO\n");
This page took 0.125983 seconds and 4 git commands to generate.