62b3774fdb21c18604dfd4da7de1e11f8b1fe926
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
;
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
)
94 fifo_print_all(Fifo
*head
)
96 for (Fifo
*f
= head
; f
; f
= f
->next
) {
102 config_print(Config
*cfg
)
118 fifo_print_all(cfg
->fifos
);
125 if (!isdigit(*(s
++)))
136 while ((c
= *(s
++)) != '\0')
138 if (c
== '.' && !seen
++)
153 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
155 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
156 " FILE_PATH = string\n"
157 " DATA_WIDTH = int (* (positive) number of characters *)\n"
158 " DATA_TTL = int (* (positive) number of seconds *)\n"
159 " OPTION = -i INTERVAL\n"
161 " | -x (* Output to X root window *)\n"
163 " SEPARATOR = string\n"
164 " INTERVAL = int (* (positive) number of seconds *)\n"
165 " LOG_LEVEL = int (* %d through %d *)\n"
173 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
179 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
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
)
218 usage("Option -l value (%d) exceeds maximum (%d)\n", log_level
, Debug
);
219 _khatus_lib_log_level
= log_level
;
220 opts_parse_any(cfg
, argc
, argv
, i
);
224 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
226 switch (argv
[i
][1]) {
228 /* TODO: Generic set_int */
229 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
232 /* TODO: Generic set_str */
233 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
236 cfg
->output_to_x_root_window
= 1;
237 opts_parse_any(cfg
, argc
, argv
, ++i
);
240 /* TODO: Generic set_int */
241 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
244 usage("Option \"%s\" is invalid\n", argv
[i
]);
249 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
252 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
259 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
261 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
262 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
269 f
->pos_init
= cfg
->total_width
;
270 f
->pos_curr
= f
->pos_init
;
271 f
->pos_final
= f
->pos_init
+ f
->width
- 1;
272 f
->next
= cfg
->fifos
;
275 cfg
->total_width
+= f
->width
;
278 fatal("[memory] Allocation failure.");
280 opts_parse_any(cfg
, argc
, argv
, i
);
284 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
287 switch (argv
[i
][0]) {
289 parse_opts_opt(cfg
, argc
, argv
, i
);
292 parse_opts_spec(cfg
, argc
, argv
, i
);
298 opts_parse(Config
*cfg
, int argc
, char *argv
[])
300 opts_parse_any(cfg
, argc
, argv
, 1);
302 Fifo
*last
= cfg
->fifos
;
304 for (Fifo
*f
= last
; f
; ) {
305 Fifo
*next
= f
->next
;
306 f
->next
= cfg
->fifos
;
313 fifo_read_error(Fifo
*f
, char *buf
)
318 b
= buf
+ f
->pos_init
;
319 /* Copy as much of the error message as possible.
320 * EXCLUDING the reminating \0. */
321 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
323 /* Any remaining slots: */
324 for (; i
< f
->width
; i
++)
329 fifo_read_one(Fifo
*f
, char *buf
)
331 char c
; /* Character read. */
332 int r
; /* Remaining unused slots in buffer range. */
335 switch (read(f
->fd
, &c
, 1)) {
337 error("Failed to read: \"%s\". errno: %d, msg: %s\n",
338 f
->name
, errno
, strerror(errno
));
347 debug("%s: End of FILE\n", f
->name
);
348 f
->pos_curr
= f
->pos_init
;
351 /* TODO: Consider making msg term char a CLI option */
352 if (c
== '\n' || c
== '\0') {
353 r
= f
->pos_final
- f
->pos_curr
;
355 memset(buf
+ f
->pos_curr
, ' ', r
);
356 f
->pos_curr
= f
->pos_init
;
357 return END_OF_MESSAGE
;
359 if (f
->pos_curr
<= f
->pos_final
)
360 buf
[f
->pos_curr
++] = c
;
361 /* Drop beyond available range. */
368 /* TODO Record timestamp read */
372 fifo_read_all(Config
*cfg
, char *buf
)
380 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
381 /* TODO: Create the FIFO if it doesn't already exist. */
382 if (lstat(f
->name
, &st
) < 0) {
383 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
384 fifo_read_error(f
, buf
);
387 if (!(st
.st_mode
& S_IFIFO
)) {
388 error("\"%s\" is not a FIFO\n", f
->name
);
389 fifo_read_error(f
, buf
);
393 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
394 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
396 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
399 /* TODO: Consider backing off retries for failed fifos. */
400 error("Failed to open \"%s\"\n", f
->name
);
401 fifo_read_error(f
, buf
);
404 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
409 debug("selecting...\n");
410 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
411 debug("ready: %d\n", ready
);
414 /* TODO: Do we really want to fail here? */
415 fatal("%s", strerror(errno
));
417 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
418 if (FD_ISSET(f
->fd
, &fds
)) {
419 debug("reading: %s\n", f
->name
);
420 switch (fifo_read_one(f
, buf
)) {
422 * ### MESSAGE LOSS ###
423 * is introduced by closing at EOM in addition
424 * to EOF, since there may be unread messages
425 * remaining in the pipe. However,
427 * ### INTER-MESSAGE PUSHBACK ###
428 * is also gained, since pipes block at the
431 * This is an acceptable trade-off because we
432 * are a stateless reporter of a _most-recent_
433 * status, not a stateful accumulator.
454 main(int argc
, char *argv
[])
462 Config cfg0
= defaults
;
464 Display
*display
= NULL
;
467 t0
, /* time stamp. before reading fifos */
468 t1
, /* time stamp. after reading fifos */
469 ti
, /* time interval desired (t1 - t0) */
470 td
, /* time interval measured (t1 - t0) */
471 tc
; /* time interval correction (ti - td) when td < ti */
475 opts_parse(cfg
, argc
, argv
);
476 debug("argv0 = %s\n", argv0
);
479 ti
= timespec_of_float(cfg
->interval
);
481 if (cfg
->fifos
== NULL
)
482 usage("No fifo specs were given!\n");
484 /* 1st pass to check file existence and type */
485 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
486 if (lstat(f
->name
, &st
) < 0) {
487 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
491 if (!(st
.st_mode
& S_IFIFO
)) {
492 error("\"%s\" is not a FIFO\n", f
->name
);
498 fatal("Encountered errors with the given file paths. See log.\n");
500 width
= cfg
->total_width
;
501 seplen
= strlen(cfg
->separator
);
503 /* 2nd pass to make space for separators */
504 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
505 f
->pos_init
+= prefix
;
506 f
->pos_final
+= prefix
;
507 f
->pos_curr
= f
->pos_init
;
511 width
+= (seplen
* (nfifos
- 1));
512 buf
= calloc(1, width
+ 1);
514 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
515 memset(buf
, ' ', width
);
517 /* 3rd pass to set the separators */
518 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
519 if (f
->pos_init
) { /* Skip the first, left-most */
520 /* Copying only seplen ensures we omit the '\0' byte. */
521 strncpy(buf
+ (f
->pos_init
- seplen
), cfg
->separator
, seplen
);
525 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
526 fatal("XOpenDisplay failed with: %p\n", display
);
527 /* TODO: Handle signals */
529 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
530 /* TODO: Cache expiration. i.e. use the TTL */
531 /* TODO: How to trigger TTL check? On select? Alarm signal? */
532 /* TODO: Set timeout on fifo_read_all based on diff of last time of
533 * fifo_read_all and desired time of next TTL check.
535 /* TODO: How long to wait on IO? Max TTL? */
536 fifo_read_all(cfg
, buf
);
537 if (cfg
->output_to_x_root_window
) {
538 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
539 fatal("XStoreName failed.\n");
545 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
546 timespecsub(&t1
, &t0
, &td
);
547 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
548 if (timespeccmp(&td
, &ti
, <)) {
549 /* Pushback on data producers by refusing to read the
550 * pipe more frequently than the interval.
552 timespecsub(&ti
, &td
, &tc
);
553 debug("snooze YES\n");
556 debug("snooze NO\n");
This page took 0.139578 seconds and 3 git commands to generate.