ea63fa10e46571cb8014d541a9b78c849c0b958f
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 " SEPARATOR = string\n"
137 " INTERVAL = int (* (positive) number of seconds *)\n"
143 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
149 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
152 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
155 char *param
= argv
[i
++];
157 if (is_pos_num(param
)) {
158 cfg
->interval
= atoi(param
);
159 opts_parse_any(cfg
, argc
, argv
, i
);
161 usage("Option -i parameter is invalid: \"%s\"\n", param
);
164 usage("Option -i parameter is missing.\n");
169 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
172 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
173 strcpy(cfg
->separator
, argv
[i
]);
174 opts_parse_any(cfg
, argc
, argv
, ++i
);
176 usage("Option -s parameter is missing.\n");
181 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
183 switch (argv
[i
][1]) {
185 /* TODO: Generic set_int */
186 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
189 /* TODO: Generic set_str */
190 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
193 cfg
->output_to_x_root_window
= 1;
194 opts_parse_any(cfg
, argc
, argv
, ++i
);
197 usage("Option \"%s\" is invalid\n", argv
[i
]);
202 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
205 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
212 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
214 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
215 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
222 f
->pos
= cfg
->total_width
;
223 f
->next
= cfg
->fifos
;
226 cfg
->total_width
+= f
->width
;
229 fatal("[memory] Allocation failure.");
231 opts_parse_any(cfg
, argc
, argv
, i
);
235 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
238 switch (argv
[i
][0]) {
240 parse_opts_opt(cfg
, argc
, argv
, i
);
243 parse_opts_spec(cfg
, argc
, argv
, i
);
249 opts_parse(Config
*cfg
, int argc
, char *argv
[])
251 opts_parse_any(cfg
, argc
, argv
, 1);
253 Fifo
*last
= cfg
->fifos
;
255 for (Fifo
*f
= last
; f
; ) {
256 Fifo
*next
= f
->next
;
257 f
->next
= cfg
->fifos
;
264 fifo_read_error(Fifo
*f
, char *buf
)
270 /* Copy as much of the error message as possible.
271 * EXCLUDING the reminating \0. */
272 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
274 /* Any remaining slots: */
275 for (; i
< f
->width
; i
++)
280 fifo_read_one(Fifo
*f
, char *buf
)
291 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
294 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
295 fifo_read_error(f
, buf
);
297 while (total
++ < f
->width
)
299 /* TODO Record timestamp read */
305 fifo_read_all(Config
*cfg
, char *buf
)
313 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
314 /* TODO: Create the FIFO if it doesn't already exist. */
315 if (lstat(f
->name
, &st
) < 0) {
316 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
317 fifo_read_error(f
, buf
);
320 if (!(st
.st_mode
& S_IFIFO
)) {
321 error("\"%s\" is not a FIFO\n", f
->name
);
322 fifo_read_error(f
, buf
);
325 debug("opening: %s\n", f
->name
);
327 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
329 /* TODO: Consider backing off retries for failed fifos. */
330 error("Failed to open \"%s\"\n", f
->name
);
331 fifo_read_error(f
, buf
);
338 debug("selecting...\n");
339 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
340 debug("ready: %d\n", ready
);
343 fatal("%s", strerror(errno
));
344 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
345 if (FD_ISSET(f
->fd
, &fds
)) {
346 debug("reading: %s\n", f
->name
);
347 fifo_read_one(f
, buf
);
353 snooze(struct timespec
*t
)
355 struct timespec remainder
;
358 result
= nanosleep(t
, &remainder
);
361 if (errno
== EINTR
) {
363 "nanosleep interrupted. Remainder: "
364 "{ tv_sec = %ld, tv_nsec = %ld }",
365 remainder
.tv_sec
, remainder
.tv_nsec
);
366 /* No big deal if we occasionally sleep less,
367 * so not attempting to correct after an interruption.
370 fatal("nanosleep: %s\n", strerror(errno
));
376 main(int argc
, char *argv
[])
384 Config cfg0
= defaults
;
386 Display
*display
= NULL
;
389 t0
, /* time stamp. before reading fifos */
390 t1
, /* time stamp. after reading fifos */
391 ti
, /* time interval desired (t1 - t0) */
392 td
, /* time interval measured (t1 - t0) */
393 tc
; /* time interval correction (ti - td) when td < ti */
397 opts_parse(cfg
, argc
, argv
);
398 debug("argv0 = %s\n", argv0
);
401 /* TODO: Support interval < 1. i.e. implement timespec_of_float */
402 ti
.tv_sec
= cfg
->interval
;
405 if (cfg
->fifos
== NULL
)
406 usage("No fifo specs were given!\n");
408 /* 1st pass to check file existence and type */
409 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
410 if (lstat(f
->name
, &st
) < 0) {
411 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
415 if (!(st
.st_mode
& S_IFIFO
)) {
416 error("\"%s\" is not a FIFO\n", f
->name
);
422 fatal("Encountered errors with the given file paths. See log.\n");
424 width
= cfg
->total_width
;
425 seplen
= strlen(cfg
->separator
);
427 /* 2nd pass to make space for separators */
428 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
433 width
+= (seplen
* (nfifos
- 1));
434 buf
= calloc(1, width
+ 1);
436 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
437 memset(buf
, ' ', width
);
439 /* 3rd pass to set the separators */
440 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
441 if (f
->pos
) { /* Skip the first, left-most */
442 /* Copying only seplen ensures we omit the '\0' byte. */
443 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
447 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
448 fatal("XOpenDisplay failed with: %p\n", display
);
449 /* TODO: Handle signals */
451 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
452 /* TODO: Cache expiration. i.e. use the TTL */
453 /* TODO: How to trigger TTL check? On select? Alarm signal? */
454 /* TODO: Set timeout on fifo_read_all based on diff of last time of
455 * fifo_read_all and desired time of next TTL check.
457 /* TODO: How long to wait on IO? Max TTL? */
458 fifo_read_all(cfg
, buf
);
459 if (cfg
->output_to_x_root_window
) {
460 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
461 fatal("XStoreName failed.\n");
467 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
468 timespecsub(&t1
, &t0
, &td
);
469 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
470 if (timespeccmp(&td
, &ti
, <)) {
471 /* Pushback on data producers by refusing to read the
472 * pipe more frequently than the interval.
474 timespecsub(&ti
, &td
, &tc
);
475 debug("snooze YES\n");
478 debug("snooze NO\n");
This page took 0.139359 seconds and 3 git commands to generate.