bb76507ef0394918ad9718ece16a0f81512febb8
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 warn( args...) if (cfg->log_level >= Warn ) {fprintf(stderr, "[warn] " args); fflush(stderr);}
21 #define error(args...) if (cfg->log_level >= Error) {fprintf(stderr, "[error] " args); fflush(stderr);}
22 #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);}
23 #define usage(args...) {print_usage(); fatal("[usage] " args);}
25 #define ERRMSG "ERROR"
27 static const char errmsg
[] = ERRMSG
;
28 static const int errlen
= sizeof(ERRMSG
) - 1;
32 typedef enum LogLevel
{
40 /* TODO: Convert fifo list to fifo array. */
41 typedef struct Fifo Fifo
;
48 int pos
; /* Position on the output buffer. */
52 typedef struct Config Config
;
59 int output_to_x_root_window
;
67 .output_to_x_root_window
= 0,
72 fifo_print_one(Fifo
*f
, Config
*cfg
)
95 fifo_print_all(Fifo
*head
, Config
*cfg
)
97 for (Fifo
*f
= head
; f
; f
= f
->next
) {
98 fifo_print_one(f
, cfg
);
103 config_print(Config
*cfg
)
121 fifo_print_all(cfg
->fifos
, cfg
);
128 if (!isdigit(*(s
++)))
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_pos_num(param
))
177 usage("Option -i parameter is invalid: \"%s\"\n", param
);
178 cfg
->interval
= atoi(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 cfg
->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
, Config
*cfg
)
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
, cfg
);
388 snooze(struct timespec
*t
, Config
*cfg
)
390 struct timespec remainder
;
393 result
= nanosleep(t
, &remainder
);
396 if (errno
== EINTR
) {
398 "nanosleep interrupted. Remainder: "
399 "{ tv_sec = %ld, tv_nsec = %ld }",
400 remainder
.tv_sec
, remainder
.tv_nsec
);
401 /* No big deal if we occasionally sleep less,
402 * so not attempting to correct after an interruption.
405 fatal("nanosleep: %s\n", strerror(errno
));
411 main(int argc
, char *argv
[])
419 Config cfg0
= defaults
;
421 Display
*display
= NULL
;
424 t0
, /* time stamp. before reading fifos */
425 t1
, /* time stamp. after reading fifos */
426 ti
, /* time interval desired (t1 - t0) */
427 td
, /* time interval measured (t1 - t0) */
428 tc
; /* time interval correction (ti - td) when td < ti */
432 opts_parse(cfg
, argc
, argv
);
433 debug("argv0 = %s\n", argv0
);
436 /* TODO: Support interval < 1. i.e. implement timespec_of_float */
437 ti
.tv_sec
= cfg
->interval
;
440 if (cfg
->fifos
== NULL
)
441 usage("No fifo specs were given!\n");
443 /* 1st pass to check file existence and type */
444 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
445 if (lstat(f
->name
, &st
) < 0) {
446 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
450 if (!(st
.st_mode
& S_IFIFO
)) {
451 error("\"%s\" is not a FIFO\n", f
->name
);
457 fatal("Encountered errors with the given file paths. See log.\n");
459 width
= cfg
->total_width
;
460 seplen
= strlen(cfg
->separator
);
462 /* 2nd pass to make space for separators */
463 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
468 width
+= (seplen
* (nfifos
- 1));
469 buf
= calloc(1, width
+ 1);
471 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
472 memset(buf
, ' ', width
);
474 /* 3rd pass to set the separators */
475 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
476 if (f
->pos
) { /* Skip the first, left-most */
477 /* Copying only seplen ensures we omit the '\0' byte. */
478 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
482 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
483 fatal("XOpenDisplay failed with: %p\n", display
);
484 /* TODO: Handle signals */
486 clock_gettime(CLOCK_MONOTONIC
, &t0
); // FIXME: check errors
487 /* TODO: Cache expiration. i.e. use the TTL */
488 /* TODO: How to trigger TTL check? On select? Alarm signal? */
489 /* TODO: Set timeout on fifo_read_all based on diff of last time of
490 * fifo_read_all and desired time of next TTL check.
492 /* TODO: How long to wait on IO? Max TTL? */
493 fifo_read_all(cfg
, buf
);
494 if (cfg
->output_to_x_root_window
) {
495 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
496 fatal("XStoreName failed.\n");
502 clock_gettime(CLOCK_MONOTONIC
, &t1
); // FIXME: check errors
503 timespecsub(&t1
, &t0
, &td
);
504 debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td
.tv_sec
, td
.tv_nsec
);
505 if (timespeccmp(&td
, &ti
, <)) {
506 /* Pushback on data producers by refusing to read the
507 * pipe more frequently than the interval.
509 timespecsub(&ti
, &td
, &tc
);
510 debug("snooze YES\n");
513 debug("snooze NO\n");
This page took 0.113672 seconds and 3 git commands to generate.