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 /* Initialize all to an impossible value: */
332 ssize_t n
= -5; /* Number of bytes read. */
333 char c
= -1; /* Character read. */
334 int r
= -1; /* Remaining unused slots in buffer range. */
337 n
= read(f
->fd
, &c
, 1);
338 assert(n
>= -1 && n
<= 1);
341 error("Failed to read: \"%s\". errno: %d, msg: %s\n",
342 f
->name
, errno
, strerror(errno
));
351 debug("%s: End of FILE\n", f
->name
);
352 f
->pos_curr
= f
->pos_init
;
355 /* TODO: Consider making msg term char a CLI option */
356 if (c
== '\n' || c
== '\0') {
357 r
= f
->pos_final
- f
->pos_curr
;
359 memset(buf
+ f
->pos_curr
, ' ', r
);
360 f
->pos_curr
= f
->pos_init
;
361 return END_OF_MESSAGE
;
363 if (f
->pos_curr
<= f
->pos_final
)
364 buf
[f
->pos_curr
++] = c
;
365 /* Drop beyond available range. */
372 /* TODO Record timestamp read */
376 fifo_read_all(Config
*cfg
, char *buf
)
384 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
385 /* TODO: Create the FIFO if it doesn't already exist. */
386 if (lstat(f
->name
, &st
) < 0) {
387 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
388 fifo_read_error(f
, buf
);
391 if (!(st
.st_mode
& S_IFIFO
)) {
392 error("\"%s\" is not a FIFO\n", f
->name
);
393 fifo_read_error(f
, buf
);
397 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
398 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
400 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
403 /* TODO: Consider backing off retries for failed fifos. */
404 error("Failed to open \"%s\"\n", f
->name
);
405 fifo_read_error(f
, buf
);
408 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
413 debug("selecting...\n");
414 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
415 debug("ready: %d\n", ready
);
418 fatal("%s", strerror(errno
));
419 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
420 if (FD_ISSET(f
->fd
, &fds
)) {
421 debug("reading: %s\n", f
->name
);
422 switch (fifo_read_one(f
, buf
)) {
439 main(int argc
, char *argv
[])
447 Config cfg0
= defaults
;
449 Display
*display
= NULL
;
452 t0
, /* time stamp. before reading fifos */
453 t1
, /* time stamp. after reading fifos */
454 ti
, /* time interval desired (t1 - t0) */
455 td
, /* time interval measured (t1 - t0) */
456 tc
; /* time interval correction (ti - td) when td < ti */
460 opts_parse(cfg
, argc
, argv
);
461 debug("argv0 = %s\n", argv0
);
464 ti
= timespec_of_float(cfg
->interval
);
466 if (cfg
->fifos
== NULL
)
467 usage("No fifo specs were given!\n");
469 /* 1st pass to check file existence and type */
470 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
471 if (lstat(f
->name
, &st
) < 0) {
472 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
476 if (!(st
.st_mode
& S_IFIFO
)) {
477 error("\"%s\" is not a FIFO\n", f
->name
);
483 fatal("Encountered errors with the given file paths. See log.\n");
485 width
= cfg
->total_width
;
486 seplen
= strlen(cfg
->separator
);
488 /* 2nd pass to make space for separators */
489 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
490 f
->pos_init
+= prefix
;
491 f
->pos_final
+= prefix
;
492 f
->pos_curr
= f
->pos_init
;
496 width
+= (seplen
* (nfifos
- 1));
497 buf
= calloc(1, width
+ 1);
499 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
500 memset(buf
, ' ', width
);
502 /* 3rd pass to set the separators */
503 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
504 if (f
->pos_init
) { /* Skip the first, left-most */
505 /* Copying only seplen ensures we omit the '\0' byte. */
506 strncpy(buf
+ (f
->pos_init
- seplen
), cfg
->separator
, seplen
);
510 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
511 fatal("XOpenDisplay failed with: %p\n", display
);
512 /* TODO: Handle signals */
514 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
515 /* TODO: Cache expiration. i.e. use the TTL */
516 /* TODO: How to trigger TTL check? On select? Alarm signal? */
517 /* TODO: Set timeout on fifo_read_all based on diff of last time of
518 * fifo_read_all and desired time of next TTL check.
520 /* TODO: How long to wait on IO? Max TTL? */
521 fifo_read_all(cfg
, buf
);
522 if (cfg
->output_to_x_root_window
) {
523 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
524 fatal("XStoreName failed.\n");
530 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
531 timespecsub(&t1
, &t0
, &td
);
532 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
533 if (timespeccmp(&td
, &ti
, <)) {
534 /* Pushback on data producers by refusing to read the
535 * pipe more frequently than the interval.
537 timespecsub(&ti
, &td
, &tc
);
538 debug("snooze YES\n");
541 debug("snooze NO\n");
This page took 0.140041 seconds and 4 git commands to generate.