b6f66f7a1134923d8733a18016bb7404d9f4ecd3
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
; /* Position on the output buffer. */
40 typedef struct Config Config
;
47 int output_to_x_root_window
;
54 .output_to_x_root_window
= 0,
58 fifo_print_one(Fifo
*f
)
81 fifo_print_all(Fifo
*head
)
83 for (Fifo
*f
= head
; f
; f
= f
->next
) {
89 config_print(Config
*cfg
)
105 fifo_print_all(cfg
->fifos
);
112 if (!isdigit(*(s
++)))
123 while ((c
= *(s
++)) != '\0')
125 if (c
== '.' && !seen
++)
140 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
142 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
143 " FILE_PATH = string\n"
144 " DATA_WIDTH = int (* (positive) number of characters *)\n"
145 " DATA_TTL = int (* (positive) number of seconds *)\n"
146 " OPTION = -i INTERVAL\n"
148 " | -x (* Output to X root window *)\n"
150 " SEPARATOR = string\n"
151 " INTERVAL = int (* (positive) number of seconds *)\n"
152 " LOG_LEVEL = int (* %d through %d *)\n"
160 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
166 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
169 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
174 usage("Option -i parameter is missing.\n");
176 if (!is_decimal(param
))
177 usage("Option -i parameter is invalid: \"%s\"\n", param
);
178 cfg
->interval
= atof(param
);
179 opts_parse_any(cfg
, argc
, argv
, i
);
183 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
186 usage("Option -s parameter is missing.\n");
187 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
188 strcpy(cfg
->separator
, argv
[i
]);
189 opts_parse_any(cfg
, argc
, argv
, ++i
);
193 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
199 usage("Option -l parameter is missing.\n");
201 if (!is_pos_num(param
))
202 usage("Option -l parameter is invalid: \"%s\"\n", param
);
203 log_level
= atoi(param
);
204 if (log_level
> Debug
)
205 usage("Option -l value (%d) exceeds maximum (%d)\n", log_level
, Debug
);
206 _khatus_lib_log_level
= log_level
;
207 opts_parse_any(cfg
, argc
, argv
, i
);
211 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
213 switch (argv
[i
][1]) {
215 /* TODO: Generic set_int */
216 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
219 /* TODO: Generic set_str */
220 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
223 cfg
->output_to_x_root_window
= 1;
224 opts_parse_any(cfg
, argc
, argv
, ++i
);
227 /* TODO: Generic set_int */
228 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
231 usage("Option \"%s\" is invalid\n", argv
[i
]);
236 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
239 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
246 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
248 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
249 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
256 f
->pos
= cfg
->total_width
;
257 f
->next
= cfg
->fifos
;
260 cfg
->total_width
+= f
->width
;
263 fatal("[memory] Allocation failure.");
265 opts_parse_any(cfg
, argc
, argv
, i
);
269 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
272 switch (argv
[i
][0]) {
274 parse_opts_opt(cfg
, argc
, argv
, i
);
277 parse_opts_spec(cfg
, argc
, argv
, i
);
283 opts_parse(Config
*cfg
, int argc
, char *argv
[])
285 opts_parse_any(cfg
, argc
, argv
, 1);
287 Fifo
*last
= cfg
->fifos
;
289 for (Fifo
*f
= last
; f
; ) {
290 Fifo
*next
= f
->next
;
291 f
->next
= cfg
->fifos
;
298 fifo_read_error(Fifo
*f
, char *buf
)
304 /* Copy as much of the error message as possible.
305 * EXCLUDING the reminating \0. */
306 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
308 /* Any remaining slots: */
309 for (; i
< f
->width
; i
++)
314 fifo_read_one(Fifo
*f
, char *buf
)
325 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
328 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
329 fifo_read_error(f
, buf
);
331 while (total
++ < f
->width
)
334 /* TODO Record timestamp read */
340 fifo_read_all(Config
*cfg
, char *buf
)
348 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
349 /* TODO: Create the FIFO if it doesn't already exist. */
350 if (lstat(f
->name
, &st
) < 0) {
351 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
352 fifo_read_error(f
, buf
);
355 if (!(st
.st_mode
& S_IFIFO
)) {
356 error("\"%s\" is not a FIFO\n", f
->name
);
357 fifo_read_error(f
, buf
);
360 debug("opening: %s\n", f
->name
);
362 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
364 /* TODO: Consider backing off retries for failed fifos. */
365 error("Failed to open \"%s\"\n", f
->name
);
366 fifo_read_error(f
, buf
);
373 debug("selecting...\n");
374 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
375 debug("ready: %d\n", ready
);
378 fatal("%s", strerror(errno
));
379 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
380 if (FD_ISSET(f
->fd
, &fds
)) {
381 debug("reading: %s\n", f
->name
);
382 fifo_read_one(f
, buf
);
388 main(int argc
, char *argv
[])
396 Config cfg0
= defaults
;
398 Display
*display
= NULL
;
401 t0
, /* time stamp. before reading fifos */
402 t1
, /* time stamp. after reading fifos */
403 ti
, /* time interval desired (t1 - t0) */
404 td
, /* time interval measured (t1 - t0) */
405 tc
; /* time interval correction (ti - td) when td < ti */
409 opts_parse(cfg
, argc
, argv
);
410 debug("argv0 = %s\n", argv0
);
413 ti
= timespec_of_float(cfg
->interval
);
415 if (cfg
->fifos
== NULL
)
416 usage("No fifo specs were given!\n");
418 /* 1st pass to check file existence and type */
419 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
420 if (lstat(f
->name
, &st
) < 0) {
421 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
425 if (!(st
.st_mode
& S_IFIFO
)) {
426 error("\"%s\" is not a FIFO\n", f
->name
);
432 fatal("Encountered errors with the given file paths. See log.\n");
434 width
= cfg
->total_width
;
435 seplen
= strlen(cfg
->separator
);
437 /* 2nd pass to make space for separators */
438 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
443 width
+= (seplen
* (nfifos
- 1));
444 buf
= calloc(1, width
+ 1);
446 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
447 memset(buf
, ' ', width
);
449 /* 3rd pass to set the separators */
450 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
451 if (f
->pos
) { /* Skip the first, left-most */
452 /* Copying only seplen ensures we omit the '\0' byte. */
453 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
457 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
458 fatal("XOpenDisplay failed with: %p\n", display
);
459 /* TODO: Handle signals */
461 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
462 /* TODO: Cache expiration. i.e. use the TTL */
463 /* TODO: How to trigger TTL check? On select? Alarm signal? */
464 /* TODO: Set timeout on fifo_read_all based on diff of last time of
465 * fifo_read_all and desired time of next TTL check.
467 /* TODO: How long to wait on IO? Max TTL? */
468 fifo_read_all(cfg
, buf
);
469 if (cfg
->output_to_x_root_window
) {
470 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
471 fatal("XStoreName failed.\n");
477 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
478 timespecsub(&t1
, &t0
, &td
);
479 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
480 if (timespeccmp(&td
, &ti
, <)) {
481 /* Pushback on data producers by refusing to read the
482 * pipe more frequently than the interval.
484 timespecsub(&ti
, &td
, &tc
);
485 debug("snooze YES\n");
488 debug("snooze NO\n");
This page took 0.153623 seconds and 3 git commands to generate.