0580430983777265a6613b436b574d8a056dfbfb
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. */
395 * TODO Define max after which we stop reading.
396 * To ensure that a rogue large message
397 * doesn't trap us here.
408 fifo_read_all(Config
*cfg
, struct timespec
*ti
, char *buf
)
417 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
418 /* TODO: Create the FIFO if it doesn't already exist. */
419 if (lstat(f
->name
, &st
) < 0) {
421 "Cannot stat \"%s\". Error: %s\n",
425 fifo_read_error(f
, buf
);
428 if (!(st
.st_mode
& S_IFIFO
)) {
429 error("\"%s\" is not a FIFO\n", f
->name
);
430 fifo_read_error(f
, buf
);
434 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
435 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
437 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
440 /* TODO Consider backing off retries for failed fifos */
441 error("Failed to open \"%s\"\n", f
->name
);
442 fifo_read_error(f
, buf
);
445 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
450 debug("selecting...\n");
451 ready
= pselect(maxfd
+ 1, &fds
, NULL
, NULL
, ti
, NULL
);
452 debug("ready: %d\n", ready
);
453 clock_gettime(CLOCK_MONOTONIC
, &t
);
457 error("pselect temp failure: %d, errno: %d, msg: %s\n",
458 ready
, errno
, strerror(errno
));
459 /* TODO: Reconsider what to do here. */
462 fatal("pselect failed: %d, errno: %d, msg: %s\n",
463 ready
, errno
, strerror(errno
));
466 /* At-least-once ensures that expiries are still checked on timeouts. */
468 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
469 if (FD_ISSET(f
->fd
, &fds
)) {
470 debug("reading: %s\n", f
->name
);
471 switch (fifo_read_one(f
, t
, buf
)) {
473 * ### MESSAGE LOSS ###
474 * is introduced by closing at EOM in addition
475 * to EOF, since there may be unread messages
476 * remaining in the pipe. However,
478 * ### INTER-MESSAGE PUSHBACK ###
479 * is also gained, since pipes block at the
482 * This is an acceptable trade-off because we
483 * are a stateless reporter of a _most-recent_
484 * status, not a stateful accumulator.
499 fifo_expire(f
, t
, buf
);
507 main(int argc
, char *argv
[])
515 Config cfg0
= defaults
;
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.174667 seconds and 3 git commands to generate.