1 #include <sys/select.h>
16 #include "bsdtimespec.h"
18 #define debug(args...) if (cfg->log_level >= Debug) {fprintf(stderr, "[debug] " args); fflush(stderr);}
19 #define info( args...) if (cfg->log_level >= Info ) {fprintf(stderr, "[info] " args); fflush(stderr);}
20 #define error(args...) if (cfg->log_level >= Error) {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 typedef enum LogLevel
{
38 /* TODO: Convert fifo list to fifo array. */
39 typedef struct Fifo Fifo
;
46 int pos
; /* Position on the output buffer. */
50 typedef struct Config Config
;
57 int output_to_x_root_window
;
65 .output_to_x_root_window
= 0,
70 fifo_print_one(Fifo
*f
, Config
*cfg
)
94 fifo_print_all(Fifo
*head
, Config
*cfg
)
96 for (Fifo
*f
= head
; f
; f
= f
->next
) {
97 fifo_print_one(f
, cfg
);
102 config_print(Config
*cfg
)
120 fifo_print_all(cfg
->fifos
, cfg
);
127 if (!isdigit(*(s
++)))
139 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
141 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
142 " FILE_PATH = string\n"
143 " DATA_WIDTH = int (* (positive) number of characters *)\n"
144 " DATA_TTL = int (* (positive) number of seconds *)\n"
145 " OPTION = -i INTERVAL\n"
147 " | -x (* Output to X root window *)\n"
149 " SEPARATOR = string\n"
150 " INTERVAL = int (* (positive) number of seconds *)\n"
151 " LOG_LEVEL = int (* %d through %d *)\n"
159 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
165 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
168 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
171 char *param
= argv
[i
++];
173 if (is_pos_num(param
)) {
174 cfg
->interval
= atoi(param
);
175 opts_parse_any(cfg
, argc
, argv
, i
);
177 usage("Option -i parameter is invalid: \"%s\"\n", param
);
180 usage("Option -i parameter is missing.\n");
185 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
188 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
189 strcpy(cfg
->separator
, argv
[i
]);
190 opts_parse_any(cfg
, argc
, argv
, ++i
);
192 usage("Option -s parameter is missing.\n");
197 parse_opts_opt_l(Config
*cfg
, int argc
, char *argv
[], int i
)
202 char *param
= argv
[i
++];
204 if (is_pos_num(param
)) {
205 log_level
= atoi(param
);
206 if (log_level
<= Debug
) {
207 cfg
->log_level
= log_level
;
208 opts_parse_any(cfg
, argc
, argv
, i
);
210 usage("Option -l value (%d) exceeds maximum (%d)\n", log_level
, Debug
);
213 usage("Option -l parameter is invalid: \"%s\"\n", param
);
216 usage("Option -l parameter is missing.\n");
221 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
223 switch (argv
[i
][1]) {
225 /* TODO: Generic set_int */
226 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
229 /* TODO: Generic set_str */
230 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
233 cfg
->output_to_x_root_window
= 1;
234 opts_parse_any(cfg
, argc
, argv
, ++i
);
237 /* TODO: Generic set_int */
238 parse_opts_opt_l(cfg
, argc
, argv
, ++i
);
241 usage("Option \"%s\" is invalid\n", argv
[i
]);
246 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
249 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
256 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
258 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
259 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
266 f
->pos
= cfg
->total_width
;
267 f
->next
= cfg
->fifos
;
270 cfg
->total_width
+= f
->width
;
273 fatal("[memory] Allocation failure.");
275 opts_parse_any(cfg
, argc
, argv
, i
);
279 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
282 switch (argv
[i
][0]) {
284 parse_opts_opt(cfg
, argc
, argv
, i
);
287 parse_opts_spec(cfg
, argc
, argv
, i
);
293 opts_parse(Config
*cfg
, int argc
, char *argv
[])
295 opts_parse_any(cfg
, argc
, argv
, 1);
297 Fifo
*last
= cfg
->fifos
;
299 for (Fifo
*f
= last
; f
; ) {
300 Fifo
*next
= f
->next
;
301 f
->next
= cfg
->fifos
;
308 fifo_read_error(Fifo
*f
, char *buf
)
314 /* Copy as much of the error message as possible.
315 * EXCLUDING the reminating \0. */
316 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
318 /* Any remaining slots: */
319 for (; i
< f
->width
; i
++)
324 fifo_read_one(Fifo
*f
, char *buf
, Config
*cfg
)
335 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
338 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
339 fifo_read_error(f
, buf
);
341 while (total
++ < f
->width
)
343 /* TODO Record timestamp read */
349 fifo_read_all(Config
*cfg
, char *buf
)
357 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
358 /* TODO: Create the FIFO if it doesn't already exist. */
359 if (lstat(f
->name
, &st
) < 0) {
360 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
361 fifo_read_error(f
, buf
);
364 if (!(st
.st_mode
& S_IFIFO
)) {
365 error("\"%s\" is not a FIFO\n", f
->name
);
366 fifo_read_error(f
, buf
);
369 debug("opening: %s\n", f
->name
);
371 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
373 /* TODO: Consider backing off retries for failed fifos. */
374 error("Failed to open \"%s\"\n", f
->name
);
375 fifo_read_error(f
, buf
);
382 debug("selecting...\n");
383 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
384 debug("ready: %d\n", ready
);
387 fatal("%s", strerror(errno
));
388 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
389 if (FD_ISSET(f
->fd
, &fds
)) {
390 debug("reading: %s\n", f
->name
);
391 fifo_read_one(f
, buf
, cfg
);
397 snooze(struct timespec
*t
, Config
*cfg
)
399 struct timespec remainder
;
402 result
= nanosleep(t
, &remainder
);
405 if (errno
== EINTR
) {
407 "nanosleep interrupted. Remainder: "
408 "{ tv_sec = %ld, tv_nsec = %ld }",
409 remainder
.tv_sec
, remainder
.tv_nsec
);
410 /* No big deal if we occasionally sleep less,
411 * so not attempting to correct after an interruption.
414 fatal("nanosleep: %s\n", strerror(errno
));
420 main(int argc
, char *argv
[])
428 Config cfg0
= defaults
;
430 Display
*display
= NULL
;
433 t0
, /* time stamp. before reading fifos */
434 t1
, /* time stamp. after reading fifos */
435 ti
, /* time interval desired (t1 - t0) */
436 td
, /* time interval measured (t1 - t0) */
437 tc
; /* time interval correction (ti - td) when td < ti */
441 opts_parse(cfg
, argc
, argv
);
442 debug("argv0 = %s\n", argv0
);
445 /* TODO: Support interval < 1. i.e. implement timespec_of_float */
446 ti
.tv_sec
= cfg
->interval
;
449 if (cfg
->fifos
== NULL
)
450 usage("No fifo specs were given!\n");
452 /* 1st pass to check file existence and type */
453 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
454 if (lstat(f
->name
, &st
) < 0) {
455 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
459 if (!(st
.st_mode
& S_IFIFO
)) {
460 error("\"%s\" is not a FIFO\n", f
->name
);
466 fatal("Encountered errors with the given file paths. See log.\n");
468 width
= cfg
->total_width
;
469 seplen
= strlen(cfg
->separator
);
471 /* 2nd pass to make space for separators */
472 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
477 width
+= (seplen
* (nfifos
- 1));
478 buf
= calloc(1, width
+ 1);
480 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
481 memset(buf
, ' ', width
);
483 /* 3rd pass to set the separators */
484 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
485 if (f
->pos
) { /* Skip the first, left-most */
486 /* Copying only seplen ensures we omit the '\0' byte. */
487 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
491 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
492 fatal("XOpenDisplay failed with: %p\n", display
);
493 /* TODO: Handle signals */
495 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
496 /* TODO: Cache expiration. i.e. use the TTL */
497 /* TODO: How to trigger TTL check? On select? Alarm signal? */
498 /* TODO: Set timeout on fifo_read_all based on diff of last time of
499 * fifo_read_all and desired time of next TTL check.
501 /* TODO: How long to wait on IO? Max TTL? */
502 fifo_read_all(cfg
, buf
);
503 if (cfg
->output_to_x_root_window
) {
504 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
505 fatal("XStoreName failed.\n");
511 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
512 timespecsub(&t1
, &t0
, &td
);
513 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
514 if (timespeccmp(&td
, &ti
, <)) {
515 /* Pushback on data producers by refusing to read the
516 * pipe more frequently than the interval.
518 timespecsub(&ti
, &td
, &tc
);
519 debug("snooze YES\n");
522 debug("snooze NO\n");
This page took 0.152841 seconds and 5 git commands to generate.