e0b71cdc84e42f95c4a70630f705018bf3f67b25
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
++)))
124 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
126 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
127 " FILE_PATH = string\n"
128 " DATA_WIDTH = int (* (positive) number of characters *)\n"
129 " DATA_TTL = int (* (positive) number of seconds *)\n"
130 " OPTION = -i INTERVAL\n"
132 " | -x (* Output to X root window *)\n"
134 " SEPARATOR = string\n"
135 " INTERVAL = int (* (positive) number of seconds *)\n"
136 " LOG_LEVEL = int (* %d through %d *)\n"
144 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
150 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
153 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
158 usage("Option -i parameter is missing.\n");
160 if (!is_pos_num(param
))
161 usage("Option -i parameter is invalid: \"%s\"\n", param
);
162 cfg
->interval
= atoi(param
);
163 opts_parse_any(cfg
, argc
, argv
, i
);
167 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
170 usage("Option -s parameter is missing.\n");
171 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
172 strcpy(cfg
->separator
, argv
[i
]);
173 opts_parse_any(cfg
, argc
, argv
, ++i
);
177 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
183 usage("Option -l parameter is missing.\n");
185 if (!is_pos_num(param
))
186 usage("Option -l parameter is invalid: \"%s\"\n", param
);
187 log_level
= atoi(param
);
188 if (log_level
> Debug
)
189 usage("Option -l value (%d) exceeds maximum (%d)\n", log_level
, Debug
);
190 _khatus_lib_log_level
= log_level
;
191 opts_parse_any(cfg
, argc
, argv
, i
);
195 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
197 switch (argv
[i
][1]) {
199 /* TODO: Generic set_int */
200 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
203 /* TODO: Generic set_str */
204 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
207 cfg
->output_to_x_root_window
= 1;
208 opts_parse_any(cfg
, argc
, argv
, ++i
);
211 /* TODO: Generic set_int */
212 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
215 usage("Option \"%s\" is invalid\n", argv
[i
]);
220 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
223 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
230 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
232 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
233 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
240 f
->pos
= cfg
->total_width
;
241 f
->next
= cfg
->fifos
;
244 cfg
->total_width
+= f
->width
;
247 fatal("[memory] Allocation failure.");
249 opts_parse_any(cfg
, argc
, argv
, i
);
253 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
256 switch (argv
[i
][0]) {
258 parse_opts_opt(cfg
, argc
, argv
, i
);
261 parse_opts_spec(cfg
, argc
, argv
, i
);
267 opts_parse(Config
*cfg
, int argc
, char *argv
[])
269 opts_parse_any(cfg
, argc
, argv
, 1);
271 Fifo
*last
= cfg
->fifos
;
273 for (Fifo
*f
= last
; f
; ) {
274 Fifo
*next
= f
->next
;
275 f
->next
= cfg
->fifos
;
282 fifo_read_error(Fifo
*f
, char *buf
)
288 /* Copy as much of the error message as possible.
289 * EXCLUDING the reminating \0. */
290 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
292 /* Any remaining slots: */
293 for (; i
< f
->width
; i
++)
298 fifo_read_one(Fifo
*f
, char *buf
)
309 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
312 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
313 fifo_read_error(f
, buf
);
315 while (total
++ < f
->width
)
318 /* TODO Record timestamp read */
324 fifo_read_all(Config
*cfg
, char *buf
)
332 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
333 /* TODO: Create the FIFO if it doesn't already exist. */
334 if (lstat(f
->name
, &st
) < 0) {
335 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
336 fifo_read_error(f
, buf
);
339 if (!(st
.st_mode
& S_IFIFO
)) {
340 error("\"%s\" is not a FIFO\n", f
->name
);
341 fifo_read_error(f
, buf
);
344 debug("opening: %s\n", f
->name
);
346 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
348 /* TODO: Consider backing off retries for failed fifos. */
349 error("Failed to open \"%s\"\n", f
->name
);
350 fifo_read_error(f
, buf
);
357 debug("selecting...\n");
358 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
359 debug("ready: %d\n", ready
);
362 fatal("%s", strerror(errno
));
363 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
364 if (FD_ISSET(f
->fd
, &fds
)) {
365 debug("reading: %s\n", f
->name
);
366 fifo_read_one(f
, buf
);
372 main(int argc
, char *argv
[])
380 Config cfg0
= defaults
;
382 Display
*display
= NULL
;
385 t0
, /* time stamp. before reading fifos */
386 t1
, /* time stamp. after reading fifos */
387 ti
, /* time interval desired (t1 - t0) */
388 td
, /* time interval measured (t1 - t0) */
389 tc
; /* time interval correction (ti - td) when td < ti */
393 opts_parse(cfg
, argc
, argv
);
394 debug("argv0 = %s\n", argv0
);
397 /* TODO: Support interval < 1. i.e. implement timespec_of_float */
398 ti
.tv_sec
= cfg
->interval
;
401 if (cfg
->fifos
== NULL
)
402 usage("No fifo specs were given!\n");
404 /* 1st pass to check file existence and type */
405 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
406 if (lstat(f
->name
, &st
) < 0) {
407 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
411 if (!(st
.st_mode
& S_IFIFO
)) {
412 error("\"%s\" is not a FIFO\n", f
->name
);
418 fatal("Encountered errors with the given file paths. See log.\n");
420 width
= cfg
->total_width
;
421 seplen
= strlen(cfg
->separator
);
423 /* 2nd pass to make space for separators */
424 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
429 width
+= (seplen
* (nfifos
- 1));
430 buf
= calloc(1, width
+ 1);
432 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
433 memset(buf
, ' ', width
);
435 /* 3rd pass to set the separators */
436 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
437 if (f
->pos
) { /* Skip the first, left-most */
438 /* Copying only seplen ensures we omit the '\0' byte. */
439 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
443 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
444 fatal("XOpenDisplay failed with: %p\n", display
);
445 /* TODO: Handle signals */
447 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
448 /* TODO: Cache expiration. i.e. use the TTL */
449 /* TODO: How to trigger TTL check? On select? Alarm signal? */
450 /* TODO: Set timeout on fifo_read_all based on diff of last time of
451 * fifo_read_all and desired time of next TTL check.
453 /* TODO: How long to wait on IO? Max TTL? */
454 fifo_read_all(cfg
, buf
);
455 if (cfg
->output_to_x_root_window
) {
456 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
457 fatal("XStoreName failed.\n");
463 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
464 timespecsub(&t1
, &t0
, &td
);
465 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
466 if (timespeccmp(&td
, &ti
, <)) {
467 /* Pushback on data producers by refusing to read the
468 * pipe more frequently than the interval.
470 timespecsub(&ti
, &td
, &tc
);
471 debug("snooze YES\n");
474 debug("snooze NO\n");
This page took 0.134245 seconds and 3 git commands to generate.