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
));
348 debug("%s: End of FILE\n", f
->name
);
349 f
->pos_curr
= f
->pos_init
;
352 /* TODO: Consider making msg term char a CLI option */
353 if (c
== '\n' || c
== '\0') {
354 r
= f
->pos_final
- f
->pos_curr
;
356 memset(buf
+ f
->pos_curr
, ' ', r
);
357 f
->pos_curr
= f
->pos_init
;
358 return END_OF_MESSAGE
;
360 if (f
->pos_curr
<= f
->pos_final
)
361 buf
[f
->pos_curr
++] = c
;
362 /* Drop beyond available range. */
369 /* TODO Record timestamp read */
373 fifo_read_all(Config
*cfg
, char *buf
)
381 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
382 /* TODO: Create the FIFO if it doesn't already exist. */
383 if (lstat(f
->name
, &st
) < 0) {
384 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
385 fifo_read_error(f
, buf
);
388 if (!(st
.st_mode
& S_IFIFO
)) {
389 error("\"%s\" is not a FIFO\n", f
->name
);
390 fifo_read_error(f
, buf
);
394 debug("%s: closed. opening. fd: %d\n", f
->name
, f
->fd
);
395 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
397 debug("%s: already openned. fd: %d\n", f
->name
, f
->fd
);
400 /* TODO: Consider backing off retries for failed fifos. */
401 error("Failed to open \"%s\"\n", f
->name
);
402 fifo_read_error(f
, buf
);
405 debug("%s: open. fd: %d\n", f
->name
, f
->fd
);
410 debug("selecting...\n");
411 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
412 debug("ready: %d\n", ready
);
415 fatal("%s", strerror(errno
));
416 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
417 if (FD_ISSET(f
->fd
, &fds
)) {
418 debug("reading: %s\n", f
->name
);
419 switch (fifo_read_one(f
, buf
)) {
436 main(int argc
, char *argv
[])
444 Config cfg0
= defaults
;
446 Display
*display
= NULL
;
449 t0
, /* time stamp. before reading fifos */
450 t1
, /* time stamp. after reading fifos */
451 ti
, /* time interval desired (t1 - t0) */
452 td
, /* time interval measured (t1 - t0) */
453 tc
; /* time interval correction (ti - td) when td < ti */
457 opts_parse(cfg
, argc
, argv
);
458 debug("argv0 = %s\n", argv0
);
461 ti
= timespec_of_float(cfg
->interval
);
463 if (cfg
->fifos
== NULL
)
464 usage("No fifo specs were given!\n");
466 /* 1st pass to check file existence and type */
467 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
468 if (lstat(f
->name
, &st
) < 0) {
469 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
473 if (!(st
.st_mode
& S_IFIFO
)) {
474 error("\"%s\" is not a FIFO\n", f
->name
);
480 fatal("Encountered errors with the given file paths. See log.\n");
482 width
= cfg
->total_width
;
483 seplen
= strlen(cfg
->separator
);
485 /* 2nd pass to make space for separators */
486 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
487 f
->pos_init
+= prefix
;
488 f
->pos_final
+= prefix
;
489 f
->pos_curr
= f
->pos_init
;
493 width
+= (seplen
* (nfifos
- 1));
494 buf
= calloc(1, width
+ 1);
496 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
497 memset(buf
, ' ', width
);
499 /* 3rd pass to set the separators */
500 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
501 if (f
->pos_init
) { /* Skip the first, left-most */
502 /* Copying only seplen ensures we omit the '\0' byte. */
503 strncpy(buf
+ (f
->pos_init
- seplen
), cfg
->separator
, seplen
);
507 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
508 fatal("XOpenDisplay failed with: %p\n", display
);
509 /* TODO: Handle signals */
511 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
512 /* TODO: Cache expiration. i.e. use the TTL */
513 /* TODO: How to trigger TTL check? On select? Alarm signal? */
514 /* TODO: Set timeout on fifo_read_all based on diff of last time of
515 * fifo_read_all and desired time of next TTL check.
517 /* TODO: How long to wait on IO? Max TTL? */
518 fifo_read_all(cfg
, buf
);
519 if (cfg
->output_to_x_root_window
) {
520 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
521 fatal("XStoreName failed.\n");
527 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
528 timespecsub(&t1
, &t0
, &td
);
529 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
530 if (timespeccmp(&td
, &ti
, <)) {
531 /* Pushback on data producers by refusing to read the
532 * pipe more frequently than the interval.
534 timespecsub(&ti
, &td
, &tc
);
535 debug("snooze YES\n");
538 debug("snooze NO\n");
This page took 0.127435 seconds and 4 git commands to generate.