b835ad8fe2f756a8f4c2635c53e960e55e0d6ac3
1 #include <sys/select.h>
16 #include "bsdtimespec.h"
18 #define debug(args...) {fprintf(stderr, "[debug] " args); fflush(stderr);}
19 #define info( args...) {fprintf(stderr, "[info] " args); fflush(stderr);}
20 #define error(args...) {fprintf(stderr, "[error] " args); fflush(stderr);}
21 #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);}
22 #define usage(args...) {print_usage(); fatal("[usage] " args);}
24 #define ERRMSG "ERROR"
26 static const char errmsg
[] = ERRMSG
;
27 static const int errlen
= sizeof(ERRMSG
) - 1;
31 /* TODO: Convert fifo list to fifo array. */
32 typedef struct Fifo Fifo
;
39 int pos
; /* Position on the output buffer. */
43 typedef struct Config Config
;
50 int output_to_x_root_window
;
57 .output_to_x_root_window
= 0,
61 fifo_print_one(Fifo
*f
)
85 fifo_print_all(Fifo
*head
)
87 for (Fifo
*f
= head
; f
; f
= f
->next
) {
93 config_print(Config
*c
)
109 fifo_print_all(c
->fifos
);
116 if (!isdigit(*(s
++)))
128 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
130 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
131 " FILE_PATH = string\n"
132 " DATA_WIDTH = int (* (positive) number of characters *)\n"
133 " DATA_TTL = int (* (positive) number of seconds *)\n"
134 " OPTION = -i INTERVAL\n"
136 " | -x (* Output to X root window *)\n"
137 " SEPARATOR = string\n"
138 " INTERVAL = int (* (positive) number of seconds *)\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
)
156 char *param
= argv
[i
++];
158 if (is_pos_num(param
)) {
159 cfg
->interval
= atoi(param
);
160 opts_parse_any(cfg
, argc
, argv
, i
);
162 usage("Option -i parameter is invalid: \"%s\"\n", param
);
165 usage("Option -i parameter is missing.\n");
170 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
173 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
174 strcpy(cfg
->separator
, argv
[i
]);
175 opts_parse_any(cfg
, argc
, argv
, ++i
);
177 usage("Option -s parameter is missing.\n");
182 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
184 switch (argv
[i
][1]) {
186 /* TODO: Generic set_int */
187 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
190 /* TODO: Generic set_str */
191 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
194 cfg
->output_to_x_root_window
= 1;
195 opts_parse_any(cfg
, argc
, argv
, ++i
);
198 usage("Option \"%s\" is invalid\n", argv
[i
]);
203 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
206 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
213 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
215 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
216 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
223 f
->pos
= cfg
->total_width
;
224 f
->next
= cfg
->fifos
;
227 cfg
->total_width
+= f
->width
;
230 fatal("[memory] Allocation failure.");
232 opts_parse_any(cfg
, argc
, argv
, i
);
236 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
239 switch (argv
[i
][0]) {
241 parse_opts_opt(cfg
, argc
, argv
, i
);
244 parse_opts_spec(cfg
, argc
, argv
, i
);
250 opts_parse(Config
*cfg
, int argc
, char *argv
[])
252 opts_parse_any(cfg
, argc
, argv
, 1);
254 Fifo
*last
= cfg
->fifos
;
256 for (Fifo
*f
= last
; f
; ) {
257 Fifo
*next
= f
->next
;
258 f
->next
= cfg
->fifos
;
265 fifo_read_error(Fifo
*f
, char *buf
)
271 /* Copy as much of the error message as possible.
272 * EXCLUDING the reminating \0. */
273 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
275 /* Any remaining slots: */
276 for (; i
< f
->width
; i
++)
281 fifo_read_one(Fifo
*f
, char *buf
)
292 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
295 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
296 fifo_read_error(f
, buf
);
298 while (total
++ < f
->width
)
300 /* TODO Record timestamp read */
306 fifo_read_all(Config
*cfg
, char *buf
)
314 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
315 /* TODO: Create the FIFO if it doesn't already exist. */
316 if (lstat(f
->name
, &st
) < 0) {
317 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
318 fifo_read_error(f
, buf
);
321 if (!(st
.st_mode
& S_IFIFO
)) {
322 error("\"%s\" is not a FIFO\n", f
->name
);
323 fifo_read_error(f
, buf
);
326 debug("opening: %s\n", f
->name
);
328 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
330 /* TODO: Consider backing off retries for failed fifos. */
331 error("Failed to open \"%s\"\n", f
->name
);
332 fifo_read_error(f
, buf
);
339 debug("selecting...\n");
340 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
341 debug("ready: %d\n", ready
);
344 fatal("%s", strerror(errno
));
345 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
346 if (FD_ISSET(f
->fd
, &fds
)) {
347 debug("reading: %s\n", f
->name
);
348 fifo_read_one(f
, buf
);
354 snooze(struct timespec
*t
)
356 struct timespec remainder
;
359 result
= nanosleep(t
, &remainder
);
362 if (errno
== EINTR
) {
364 "nanosleep interrupted. Remainder: "
365 "{ tv_sec = %ld, tv_nsec = %ld }",
366 remainder
.tv_sec
, remainder
.tv_nsec
);
367 /* No big deal if we occasionally sleep less,
368 * so not attempting to correct after an interruption.
371 fatal("nanosleep: %s\n", strerror(errno
));
377 main(int argc
, char *argv
[])
385 Config cfg0
= defaults
;
387 Display
*display
= NULL
;
390 t0
, /* time stamp. before reading fifos */
391 t1
, /* time stamp. after reading fifos */
392 ti
, /* time interval desired (t1 - t0) */
393 td
, /* time interval measured (t1 - t0) */
394 tc
; /* time interval correction (ti - td) when td < ti */
398 opts_parse(cfg
, argc
, argv
);
399 debug("argv0 = %s\n", argv0
);
402 /* TODO: Support interval < 1. i.e. implement timespec_of_float */
403 ti
.tv_sec
= cfg
->interval
;
406 if (cfg
->fifos
== NULL
)
407 usage("No fifo specs were given!\n");
409 /* 1st pass to check file existence and type */
410 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
411 if (lstat(f
->name
, &st
) < 0) {
412 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
416 if (!(st
.st_mode
& S_IFIFO
)) {
417 error("\"%s\" is not a FIFO\n", f
->name
);
423 fatal("Encountered errors with the given file paths. See log.\n");
425 width
= cfg
->total_width
;
426 seplen
= strlen(cfg
->separator
);
428 /* 2nd pass to make space for separators */
429 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
434 width
+= (seplen
* (nfifos
- 1));
435 buf
= calloc(1, width
+ 1);
437 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
438 memset(buf
, ' ', width
);
440 /* 3rd pass to set the separators */
441 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
442 if (f
->pos
) { /* Skip the first, left-most */
443 /* Copying only seplen ensures we omit the '\0' byte. */
444 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
448 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
449 fatal("XOpenDisplay failed with: %p\n", display
);
450 /* TODO: Handle signals */
452 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
453 /* TODO: Cache expiration. i.e. use the TTL */
454 /* TODO: How to trigger TTL check? On select? Alarm signal? */
455 /* TODO: Set timeout on fifo_read_all based on diff of last time of
456 * fifo_read_all and desired time of next TTL check.
458 /* TODO: How long to wait on IO? Max TTL? */
459 fifo_read_all(cfg
, buf
);
460 if (cfg
->output_to_x_root_window
) {
461 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
462 fatal("XStoreName failed.\n");
468 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
469 timespecsub(&t1
, &t0
, &td
);
470 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
471 if (timespeccmp(&td
, &ti
, <)) {
472 /* Pushback on data producers by refusing to read the
473 * pipe more frequently than the interval.
475 timespecsub(&ti
, &td
, &tc
);
476 debug("snooze YES\n");
479 debug("snooze NO\n");
This page took 0.149161 seconds and 4 git commands to generate.