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
;
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 _khatus_lib_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
= 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 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 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)) {
361 error("Failed to read: \"%s\". errno: %d, msg: %s\n",
362 f
->name
, errno
, strerror(errno
));
371 debug("%s: End of FILE\n", f
->name
);
372 f
->pos_curr
= f
->pos_init
;
375 /* TODO: Consider making msg term char a CLI option */
376 if (c
== '\n' || c
== '\0') {
377 r
= f
->pos_final
- f
->pos_curr
;
379 memset(buf
+ f
->pos_curr
, ' ', r
);
380 f
->pos_curr
= f
->pos_init
;
382 return END_OF_MESSAGE
;
384 if (f
->pos_curr
<= f
->pos_final
)
385 buf
[f
->pos_curr
++] = c
;
386 /* Drop beyond available range. */
388 * TODO Define max after which we stop reading.
389 * To ensure that a rogue large message
390 * doesn't trap us here.
401 fifo_read_all(Config
*cfg
, struct timespec
*ti
, char *buf
)
410 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
411 /* TODO: Create the FIFO if it doesn't already exist. */
412 if (lstat(f
->name
, &st
) < 0) {
414 "Cannot stat \"%s\". Error: %s\n",
418 fifo_read_error(f
, buf
);
421 if (!(st
.st_mode
& S_IFIFO
)) {
422 error("\"%s\" is not a FIFO\n", f
->name
);
423 fifo_read_error(f
, buf
);
427 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
428 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
430 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
433 /* TODO Consider backing off retries for failed fifos */
434 error("Failed to open \"%s\"\n", f
->name
);
435 fifo_read_error(f
, buf
);
438 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
443 debug("selecting...\n");
444 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
445 debug("ready: %d\n", ready
);
446 clock_gettime(CLOCK_MONOTONIC
, &t
);
450 error("pselect temp failure: %d, errno: %d, msg: %s\n",
451 ready
, errno
, strerror(errno
));
452 /* TODO: Reconsider what to do here. */
455 fatal("pselect failed: %d, errno: %d, msg: %s\n",
456 ready
, errno
, strerror(errno
));
459 /* At-least-once ensures that expiries are still checked on timeouts. */
461 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
462 if (FD_ISSET(f
->fd
, &fds
)) {
463 debug("reading: %s\n", f
->name
);
464 switch (fifo_read_one(f
, t
, buf
)) {
466 * ### MESSAGE LOSS ###
467 * is introduced by closing at EOM in addition
468 * to EOF, since there may be unread messages
469 * remaining in the pipe. However,
471 * ### INTER-MESSAGE PUSHBACK ###
472 * is also gained, since pipes block at the
475 * This is an acceptable trade-off because we
476 * are a stateless reporter of a _most-recent_
477 * status, not a stateful accumulator.
492 fifo_expire(f
, t
, buf
);
500 main(int argc
, char *argv
[])
508 .output_to_x_root_window
= 0,
520 t0
, /* time stamp. before reading fifos */
521 t1
, /* time stamp. after reading fifos */
522 ti
, /* time interval desired (t1 - t0) */
523 td
, /* time interval measured (t1 - t0) */
524 tc
; /* time interval correction (ti - td) when td < ti */
528 opts_parse(&cfg
, argc
, argv
);
529 debug("argv0 = %s\n", argv0
);
532 ti
= timespec_of_float(cfg
.interval
);
534 if (cfg
.fifos
== NULL
)
535 usage("No fifo specs were given!\n");
537 /* 1st pass to check file existence and type */
538 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
539 if (lstat(f
->name
, &st
) < 0) {
541 "Cannot stat \"%s\". Error: %s\n",
548 if (!(st
.st_mode
& S_IFIFO
)) {
549 error("\"%s\" is not a FIFO\n", f
->name
);
555 fatal("Encountered errors with given file paths. See log.\n");
557 width
= cfg
.total_width
;
558 seplen
= strlen(cfg
.separator
);
560 /* 2nd pass to make space for separators */
561 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
562 f
->pos_init
+= prefix
;
563 f
->pos_final
+= prefix
;
564 f
->pos_curr
= f
->pos_init
;
568 width
+= (seplen
* (nfifos
- 1));
569 buf
= calloc(1, width
+ 1);
571 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
572 memset(buf
, ' ', width
);
574 /* 3rd pass to set the separators */
575 for (Fifo
*f
= cfg
.fifos
; f
; f
= f
->next
) {
576 if (f
->pos_init
) { /* Skip the first, left-most */
577 /* Copying only seplen ensures we omit the '\0' byte. */
579 buf
+ (f
->pos_init
- seplen
),
586 if (cfg
.output_to_x_root_window
&& !(d
= XOpenDisplay(NULL
)))
587 fatal("XOpenDisplay failed with: %p\n", d
);
588 /* TODO: Handle signals */
590 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
591 fifo_read_all(&cfg
, &ti
, buf
);
592 if (cfg
.output_to_x_root_window
) {
593 if (XStoreName(d
, DefaultRootWindow(d
), buf
) < 0)
594 fatal("XStoreName failed.\n");
600 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
601 timespecsub(&t1
, &t0
, &td
);
603 "td {tv_sec = %ld, tv_nsec = %ld}\n",
607 if (timespeccmp(&td
, &ti
, <)) {
608 /* Pushback on data producers by refusing to read the
609 * pipe more frequently than the interval.
611 timespecsub(&ti
, &td
, &tc
);
612 debug("snooze YES\n");
615 debug("snooze NO\n");
This page took 0.149876 seconds and 5 git commands to generate.