Commit | Line | Data |
---|---|---|
77c76070 | 1 | #include <sys/select.h> |
bec93767 | 2 | #include <sys/stat.h> |
4d66492f | 3 | |
9b5ebc12 SK |
4 | #include <assert.h> |
5 | #include <ctype.h> | |
77c76070 | 6 | #include <errno.h> |
c5d1af8c | 7 | #include <fcntl.h> |
9b5ebc12 SK |
8 | #include <stdio.h> |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
b7487ec5 | 11 | #include <time.h> |
c5d1af8c SK |
12 | #include <unistd.h> |
13 | ||
14 | #include <X11/Xlib.h> | |
9b5ebc12 | 15 | |
b7487ec5 | 16 | #include "bsdtimespec.h" |
17a27e48 SK |
17 | #include "khatus_lib_log.h" |
18 | #include "khatus_lib_time.h" | |
b7487ec5 | 19 | |
544b0835 SK |
20 | #define usage(...) { \ |
21 | print_usage(); \ | |
22 | fprintf(stderr, "Error:\n " __VA_ARGS__); \ | |
23 | exit(EXIT_FAILURE); \ | |
24 | } | |
e6c523cd SK |
25 | #define ERRMSG "ERROR" |
26 | ||
27 | static const char errmsg[] = ERRMSG; | |
28 | static const int errlen = sizeof(ERRMSG) - 1; | |
29 | ||
4d66492f | 30 | char *argv0; |
9b5ebc12 | 31 | |
1872c5c1 SK |
32 | /* TODO: Convert fifo list to fifo array. */ |
33 | typedef struct Fifo Fifo; | |
34 | struct Fifo { | |
4d66492f SK |
35 | char *name; |
36 | int fd; | |
9b5ebc12 | 37 | int width; |
0a01172a SK |
38 | struct timespec last_read; |
39 | struct timespec ttl; | |
efa97b71 SK |
40 | int pos_init; /* Initial position on the output buffer. */ |
41 | int pos_curr; /* Current position on the output buffer. */ | |
42 | int pos_final; /* Final position on the output buffer. */ | |
1872c5c1 | 43 | Fifo *next; |
9b5ebc12 SK |
44 | }; |
45 | ||
4d66492f SK |
46 | typedef struct Config Config; |
47 | struct Config { | |
348d5f36 | 48 | double interval; |
4d66492f | 49 | char * separator; |
1872c5c1 SK |
50 | Fifo * fifos; |
51 | int fifo_count; | |
4d66492f | 52 | int total_width; |
fabb8771 | 53 | int output_to_x_root_window; |
9b5ebc12 SK |
54 | }; |
55 | ||
efa97b71 | 56 | enum read_status { |
e6441710 SK |
57 | END_OF_FILE, |
58 | END_OF_MESSAGE, | |
59 | RETRY, | |
60 | FAILURE | |
efa97b71 SK |
61 | }; |
62 | ||
77c76070 | 63 | void |
17a27e48 | 64 | fifo_print_one(Fifo *f) |
77c76070 | 65 | { |
a6b13fa2 SK |
66 | info("Fifo " |
67 | "{" | |
68 | " name = %s," | |
69 | " fd = %d," | |
70 | " width = %d," | |
0a01172a SK |
71 | " last_read = {tv_sec = %ld, tv_nsec = %ld}" |
72 | " ttl = {tv_sec = %ld, tv_nsec = %ld}," | |
efa97b71 SK |
73 | " pos_init = %d," |
74 | " pos_curr = %d," | |
75 | " pos_final = %d," | |
a6b13fa2 SK |
76 | " next = %p," |
77 | " }\n", | |
78 | f->name, | |
79 | f->fd, | |
80 | f->width, | |
0a01172a SK |
81 | f->last_read.tv_sec, |
82 | f->last_read.tv_nsec, | |
83 | f->ttl.tv_sec, | |
84 | f->ttl.tv_nsec, | |
efa97b71 SK |
85 | f->pos_init, |
86 | f->pos_curr, | |
87 | f->pos_final, | |
a6b13fa2 | 88 | f->next |
77c76070 SK |
89 | ); |
90 | } | |
91 | ||
92 | void | |
17a27e48 | 93 | fifo_print_all(Fifo *head) |
77c76070 | 94 | { |
1872c5c1 | 95 | for (Fifo *f = head; f; f = f->next) { |
17a27e48 | 96 | fifo_print_one(f); |
77c76070 SK |
97 | } |
98 | } | |
99 | ||
100 | void | |
b6316e94 | 101 | config_print(Config *cfg) |
77c76070 | 102 | { |
b6316e94 | 103 | info( |
a6b13fa2 SK |
104 | "Config " |
105 | "{" | |
348d5f36 | 106 | " interval = %f," |
a6b13fa2 SK |
107 | " separator = %s," |
108 | " fifo_count = %d," | |
109 | " total_width = %d," | |
a6b13fa2 SK |
110 | " fifos = ..." |
111 | " }\n", | |
112 | cfg->interval, | |
113 | cfg->separator, | |
114 | cfg->fifo_count, | |
17a27e48 | 115 | cfg->total_width |
77c76070 | 116 | ); |
17a27e48 | 117 | fifo_print_all(cfg->fifos); |
77c76070 | 118 | } |
9b5ebc12 SK |
119 | |
120 | int | |
121 | is_pos_num(char *s) | |
122 | { | |
123 | while (*s != '\0') | |
124 | if (!isdigit(*(s++))) | |
125 | return 0; | |
126 | return 1; | |
127 | } | |
128 | ||
348d5f36 SK |
129 | int |
130 | is_decimal(char *s) | |
131 | { | |
132 | char c; | |
133 | int seen = 0; | |
134 | ||
135 | while ((c = *(s++)) != '\0') | |
136 | if (!isdigit(c)) { | |
137 | if (c == '.' && !seen++) | |
138 | continue; | |
139 | else | |
140 | return 0; | |
141 | } | |
142 | return 1; | |
143 | } | |
144 | ||
9b5ebc12 SK |
145 | void |
146 | print_usage() | |
147 | { | |
4d66492f | 148 | assert(argv0); |
9b5ebc12 | 149 | fprintf( |
a6b13fa2 SK |
150 | stderr, |
151 | "\n" | |
152 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" | |
153 | "\n" | |
154 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" | |
155 | " FILE_PATH = string\n" | |
156 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
0a01172a | 157 | " DATA_TTL = float (* (positive) number of seconds *)\n" |
a6b13fa2 SK |
158 | " OPTION = -i INTERVAL\n" |
159 | " | -s SEPARATOR\n" | |
160 | " | -x (* Output to X root window *)\n" | |
161 | " | -l LOG_LEVEL\n" | |
162 | " SEPARATOR = string\n" | |
0a01172a | 163 | " INTERVAL = float (* (positive) number of seconds *)\n" |
a6b13fa2 SK |
164 | " LOG_LEVEL = int (* %d through %d *)\n" |
165 | "\n", | |
166 | argv0, | |
167 | Nothing, | |
168 | Debug | |
9b5ebc12 SK |
169 | ); |
170 | fprintf( | |
a6b13fa2 SK |
171 | stderr, |
172 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
173 | "\n", | |
174 | argv0 | |
9b5ebc12 SK |
175 | ); |
176 | } | |
177 | ||
544b0835 SK |
178 | /* For mutually-recursive calls. */ |
179 | void opts_parse_any(Config *, int, char *[], int); | |
9b5ebc12 SK |
180 | |
181 | void | |
4d66492f | 182 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 | 183 | { |
4c438cef SK |
184 | char *param; |
185 | ||
186 | if (i >= argc) | |
9b5ebc12 | 187 | usage("Option -i parameter is missing.\n"); |
4c438cef | 188 | param = argv[i++]; |
348d5f36 | 189 | if (!is_decimal(param)) |
4c438cef | 190 | usage("Option -i parameter is invalid: \"%s\"\n", param); |
348d5f36 | 191 | cfg->interval = atof(param); |
4c438cef | 192 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
193 | } |
194 | ||
195 | void | |
4d66492f SK |
196 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
197 | { | |
4c438cef | 198 | if (i >= argc) |
4d66492f | 199 | usage("Option -s parameter is missing.\n"); |
4c438cef SK |
200 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
201 | strcpy(cfg->separator, argv[i]); | |
202 | opts_parse_any(cfg, argc, argv, ++i); | |
4d66492f SK |
203 | } |
204 | ||
b6316e94 SK |
205 | void |
206 | parse_opts_opt_l(Config *cfg, int argc, char *argv[], int i) | |
207 | { | |
4c438cef | 208 | char *param; |
b6316e94 SK |
209 | int log_level; |
210 | ||
4c438cef | 211 | if (i >= argc) |
b6316e94 | 212 | usage("Option -l parameter is missing.\n"); |
4c438cef SK |
213 | param = argv[i++]; |
214 | if (!is_pos_num(param)) | |
215 | usage("Option -l parameter is invalid: \"%s\"\n", param); | |
216 | log_level = atoi(param); | |
217 | if (log_level > Debug) | |
544b0835 SK |
218 | usage( |
219 | "Option -l value (%d) exceeds maximum (%d)\n", | |
220 | log_level, | |
221 | Debug | |
222 | ); | |
17a27e48 | 223 | _khatus_lib_log_level = log_level; |
4c438cef | 224 | opts_parse_any(cfg, argc, argv, i); |
b6316e94 SK |
225 | } |
226 | ||
4d66492f SK |
227 | void |
228 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
229 | { |
230 | switch (argv[i][1]) { | |
ce552549 SK |
231 | case 'i': |
232 | /* TODO: Generic set_int */ | |
233 | parse_opts_opt_i(cfg, argc, argv, ++i); | |
234 | break; | |
235 | case 's': | |
236 | /* TODO: Generic set_str */ | |
237 | parse_opts_opt_s(cfg, argc, argv, ++i); | |
238 | break; | |
239 | case 'x': | |
240 | cfg->output_to_x_root_window = 1; | |
241 | opts_parse_any(cfg, argc, argv, ++i); | |
242 | break; | |
243 | case 'l': | |
244 | /* TODO: Generic set_int */ | |
245 | parse_opts_opt_l(cfg, argc, argv, ++i); | |
246 | break; | |
247 | default : | |
248 | usage("Option \"%s\" is invalid\n", argv[i]); | |
9b5ebc12 SK |
249 | } |
250 | } | |
251 | ||
252 | void | |
4d66492f | 253 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
254 | { |
255 | if ((i + 3) > argc) | |
544b0835 SK |
256 | usage( |
257 | "[spec] Parameter(s) missing for fifo \"%s\".\n", | |
258 | argv[i] | |
259 | ); | |
9b5ebc12 SK |
260 | |
261 | char *n = argv[i++]; | |
262 | char *w = argv[i++]; | |
263 | char *t = argv[i++]; | |
264 | ||
0a01172a SK |
265 | struct timespec last_read; |
266 | ||
9b5ebc12 | 267 | if (!is_pos_num(w)) |
1872c5c1 | 268 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
0a01172a | 269 | if (!is_decimal(t)) |
1872c5c1 | 270 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
0a01172a SK |
271 | last_read.tv_sec = 0; |
272 | last_read.tv_nsec = 0; | |
1872c5c1 | 273 | Fifo *f = calloc(1, sizeof(struct Fifo)); |
9b5ebc12 | 274 | if (f) { |
4d66492f SK |
275 | f->name = n; |
276 | f->fd = -1; | |
277 | f->width = atoi(w); | |
0a01172a SK |
278 | f->ttl = timespec_of_float(atof(t)); |
279 | f->last_read = last_read; | |
efa97b71 SK |
280 | f->pos_init = cfg->total_width; |
281 | f->pos_curr = f->pos_init; | |
282 | f->pos_final = f->pos_init + f->width - 1; | |
1872c5c1 | 283 | f->next = cfg->fifos; |
4d66492f | 284 | |
1872c5c1 | 285 | cfg->fifos = f; |
4d66492f | 286 | cfg->total_width += f->width; |
1872c5c1 | 287 | cfg->fifo_count++; |
9b5ebc12 | 288 | } else { |
4d66492f | 289 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 290 | } |
4d66492f | 291 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
292 | } |
293 | ||
294 | void | |
4d66492f | 295 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
296 | { |
297 | if (i < argc) { | |
298 | switch (argv[i][0]) { | |
ce552549 SK |
299 | case '-': |
300 | parse_opts_opt(cfg, argc, argv, i); | |
301 | break; | |
302 | default : | |
303 | parse_opts_spec(cfg, argc, argv, i); | |
4d66492f SK |
304 | } |
305 | } | |
306 | } | |
307 | ||
308 | void | |
5400b86f | 309 | opts_parse(Config *cfg, int argc, char *argv[]) |
4d66492f SK |
310 | { |
311 | opts_parse_any(cfg, argc, argv, 1); | |
312 | ||
1872c5c1 SK |
313 | Fifo *last = cfg->fifos; |
314 | cfg->fifos = NULL; | |
315 | for (Fifo *f = last; f; ) { | |
316 | Fifo *next = f->next; | |
317 | f->next = cfg->fifos; | |
318 | cfg->fifos = f; | |
4d66492f SK |
319 | f = next; |
320 | } | |
321 | } | |
322 | ||
0a01172a | 323 | void |
716cc2b4 | 324 | fifo_expire(Fifo *f, struct timespec t, char *buf) |
0a01172a SK |
325 | { |
326 | struct timespec td; | |
327 | ||
328 | timespecsub(&t, &(f->last_read), &td); | |
329 | if (timespeccmp(&td, &(f->ttl), >=)) { | |
330 | /* TODO: Maybe configurable expiry character. */ | |
331 | memset(buf + f->pos_init, '_', f->pos_final - f->pos_init); | |
332 | warn("Data source expired: \"%s\"\n", f->name); | |
333 | } | |
334 | } | |
335 | ||
e6c523cd | 336 | void |
69b75a40 | 337 | fifo_read_error(Fifo *f, char *buf) |
e6c523cd SK |
338 | { |
339 | char *b; | |
340 | int i; | |
341 | ||
efa97b71 | 342 | b = buf + f->pos_init; |
e6c523cd | 343 | /* Copy as much of the error message as possible. |
17e4ecd5 | 344 | * EXCLUDING the terminating \0. */ |
e6c523cd SK |
345 | for (i = 0; i < errlen && i < f->width; i++) |
346 | b[i] = errmsg[i]; | |
347 | /* Any remaining slots: */ | |
348 | for (; i < f->width; i++) | |
349 | b[i] = '_'; | |
350 | } | |
351 | ||
efa97b71 | 352 | enum read_status |
0a01172a | 353 | fifo_read_one(Fifo *f, struct timespec t, char *buf) |
77c76070 | 354 | { |
03ed0008 SK |
355 | char c; /* Character read. */ |
356 | int r; /* Remaining unused slots in buffer range. */ | |
77c76070 | 357 | |
efa97b71 | 358 | for (;;) { |
03ed0008 | 359 | switch (read(f->fd, &c, 1)) { |
efa97b71 SK |
360 | case -1: |
361 | error("Failed to read: \"%s\". errno: %d, msg: %s\n", | |
362 | f->name, errno, strerror(errno)); | |
e6441710 | 363 | switch (errno) { |
8ce5d971 | 364 | case EINTR: |
e6441710 SK |
365 | case EAGAIN: |
366 | return RETRY; | |
367 | default: | |
368 | return FAILURE; | |
369 | } | |
efa97b71 SK |
370 | case 0: |
371 | debug("%s: End of FILE\n", f->name); | |
372 | f->pos_curr = f->pos_init; | |
373 | return END_OF_FILE; | |
374 | case 1: | |
375 | /* TODO: Consider making msg term char a CLI option */ | |
376 | if (c == '\n' || c == '\0') { | |
377 | r = f->pos_final - f->pos_curr; | |
378 | if (r > 0) | |
379 | memset(buf + f->pos_curr, ' ', r); | |
380 | f->pos_curr = f->pos_init; | |
0a01172a | 381 | f->last_read = t; |
efa97b71 SK |
382 | return END_OF_MESSAGE; |
383 | } else { | |
384 | if (f->pos_curr <= f->pos_final) | |
385 | buf[f->pos_curr++] = c; | |
386 | /* Drop beyond available range. */ | |
9f40d4f2 SK |
387 | /* |
388 | * TODO Define max after which we stop reading. | |
389 | * To ensure that a rogue large message | |
390 | * doesn't trap us here. | |
391 | */ | |
efa97b71 SK |
392 | } |
393 | break; | |
394 | default: | |
395 | assert(0); | |
396 | } | |
574a4bff | 397 | } |
77c76070 SK |
398 | } |
399 | ||
a415999c | 400 | void |
eb6dbe7a | 401 | fifo_read_all(Config *cfg, struct timespec *ti, char *buf) |
4d66492f | 402 | { |
77c76070 | 403 | fd_set fds; |
bfab34f8 | 404 | int maxfd = -1; |
a415999c | 405 | int ready = 0; |
bec93767 | 406 | struct stat st; |
0741fd04 | 407 | struct timespec t; |
77c76070 SK |
408 | |
409 | FD_ZERO(&fds); | |
1872c5c1 | 410 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
bec93767 | 411 | /* TODO: Create the FIFO if it doesn't already exist. */ |
e6c523cd | 412 | if (lstat(f->name, &st) < 0) { |
544b0835 SK |
413 | error( |
414 | "Cannot stat \"%s\". Error: %s\n", | |
415 | f->name, | |
416 | strerror(errno) | |
417 | ); | |
69b75a40 | 418 | fifo_read_error(f, buf); |
e6c523cd SK |
419 | continue; |
420 | } | |
421 | if (!(st.st_mode & S_IFIFO)) { | |
422 | error("\"%s\" is not a FIFO\n", f->name); | |
69b75a40 | 423 | fifo_read_error(f, buf); |
e6c523cd SK |
424 | continue; |
425 | } | |
efa97b71 SK |
426 | if (f->fd < 0) { |
427 | debug("%s: closed. opening. fd: %d\n", f->name, f->fd); | |
77c76070 | 428 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
efa97b71 SK |
429 | } else { |
430 | debug("%s: already openned. fd: %d\n", f->name, f->fd); | |
431 | } | |
e6c523cd | 432 | if (f->fd == -1) { |
544b0835 | 433 | /* TODO Consider backing off retries for failed fifos */ |
e6c523cd | 434 | error("Failed to open \"%s\"\n", f->name); |
69b75a40 | 435 | fifo_read_error(f, buf); |
e6c523cd SK |
436 | continue; |
437 | } | |
efa97b71 | 438 | debug("%s: open. fd: %d\n", f->name, f->fd); |
77c76070 SK |
439 | if (f->fd > maxfd) |
440 | maxfd = f->fd; | |
441 | FD_SET(f->fd, &fds); | |
442 | } | |
443 | debug("selecting...\n"); | |
eb6dbe7a | 444 | ready = pselect(maxfd + 1, &fds, NULL, NULL, ti, NULL); |
77c76070 | 445 | debug("ready: %d\n", ready); |
0741fd04 | 446 | clock_gettime(CLOCK_MONOTONIC, &t); |
0166c18d SK |
447 | if (ready == -1) { |
448 | switch (errno) { | |
449 | case EINTR: | |
450 | error("pselect temp failure: %d, errno: %d, msg: %s\n", | |
451 | ready, errno, strerror(errno)); | |
452 | /* TODO: Reconsider what to do here. */ | |
453 | return; | |
454 | default: | |
7ade73c9 | 455 | fatal("pselect failed: %d, errno: %d, msg: %s\n", |
0166c18d SK |
456 | ready, errno, strerror(errno)); |
457 | } | |
458 | } | |
716cc2b4 SK |
459 | /* At-least-once ensures that expiries are still checked on timeouts. */ |
460 | do { | |
a415999c SK |
461 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
462 | if (FD_ISSET(f->fd, &fds)) { | |
463 | debug("reading: %s\n", f->name); | |
0a01172a | 464 | switch (fifo_read_one(f, t, buf)) { |
a415999c SK |
465 | /* |
466 | * ### MESSAGE LOSS ### | |
467 | * is introduced by closing at EOM in addition | |
468 | * to EOF, since there may be unread messages | |
469 | * remaining in the pipe. However, | |
470 | * | |
471 | * ### INTER-MESSAGE PUSHBACK ### | |
472 | * is also gained, since pipes block at the | |
473 | * "open" call. | |
474 | * | |
475 | * This is an acceptable trade-off because we | |
476 | * are a stateless reporter of a _most-recent_ | |
477 | * status, not a stateful accumulator. | |
478 | */ | |
479 | case END_OF_MESSAGE: | |
480 | case END_OF_FILE: | |
481 | case FAILURE: | |
482 | close(f->fd); | |
483 | f->fd = -1; | |
484 | ready--; | |
485 | break; | |
486 | case RETRY: | |
487 | break; | |
488 | default: | |
489 | assert(0); | |
490 | } | |
716cc2b4 SK |
491 | } else { |
492 | fifo_expire(f, t, buf); | |
efa97b71 | 493 | } |
b7487ec5 | 494 | } |
716cc2b4 | 495 | } while (ready); |
a415999c | 496 | assert(ready == 0); |
b7487ec5 SK |
497 | } |
498 | ||
9b5ebc12 | 499 | int |
4d66492f | 500 | main(int argc, char *argv[]) |
9b5ebc12 | 501 | { |
16e0239d SK |
502 | Config cfg = { |
503 | .interval = 1.0, | |
504 | .separator = "|", | |
505 | .fifos = NULL, | |
506 | .fifo_count = 0, | |
507 | .total_width = 0, | |
508 | .output_to_x_root_window = 0, | |
509 | }; | |
510 | ||
f277f405 | 511 | int width = 0; |
1872c5c1 | 512 | int nfifos = 0; |
f277f405 | 513 | int seplen = 0; |
4d66492f | 514 | int prefix = 0; |
e6c523cd | 515 | int errors = 0; |
4d66492f | 516 | char *buf; |
544b0835 | 517 | Display *d = NULL; |
e6c523cd | 518 | struct stat st; |
b7487ec5 SK |
519 | struct timespec |
520 | t0, /* time stamp. before reading fifos */ | |
521 | t1, /* time stamp. after reading fifos */ | |
522 | ti, /* time interval desired (t1 - t0) */ | |
523 | td, /* time interval measured (t1 - t0) */ | |
524 | tc; /* time interval correction (ti - td) when td < ti */ | |
4d66492f SK |
525 | |
526 | argv0 = argv[0]; | |
527 | ||
16e0239d | 528 | opts_parse(&cfg, argc, argv); |
4d66492f | 529 | debug("argv0 = %s\n", argv0); |
16e0239d | 530 | config_print(&cfg); |
b7487ec5 | 531 | |
16e0239d | 532 | ti = timespec_of_float(cfg.interval); |
b7487ec5 | 533 | |
16e0239d | 534 | if (cfg.fifos == NULL) |
1872c5c1 | 535 | usage("No fifo specs were given!\n"); |
4d66492f | 536 | |
e6c523cd | 537 | /* 1st pass to check file existence and type */ |
16e0239d | 538 | for (Fifo *f = cfg.fifos; f; f = f->next) { |
e6c523cd | 539 | if (lstat(f->name, &st) < 0) { |
544b0835 SK |
540 | error( |
541 | "Cannot stat \"%s\". Error: %s\n", | |
542 | f->name, | |
543 | strerror(errno) | |
544 | ); | |
e6c523cd SK |
545 | errors++; |
546 | continue; | |
547 | } | |
548 | if (!(st.st_mode & S_IFIFO)) { | |
549 | error("\"%s\" is not a FIFO\n", f->name); | |
550 | errors++; | |
551 | continue; | |
552 | } | |
553 | } | |
554 | if (errors) | |
544b0835 | 555 | fatal("Encountered errors with given file paths. See log.\n"); |
e6c523cd | 556 | |
16e0239d SK |
557 | width = cfg.total_width; |
558 | seplen = strlen(cfg.separator); | |
4d66492f | 559 | |
e6c523cd | 560 | /* 2nd pass to make space for separators */ |
16e0239d | 561 | for (Fifo *f = cfg.fifos; f; f = f->next) { |
efa97b71 SK |
562 | f->pos_init += prefix; |
563 | f->pos_final += prefix; | |
564 | f->pos_curr = f->pos_init; | |
4d66492f | 565 | prefix += seplen; |
1872c5c1 | 566 | nfifos++; |
4d66492f | 567 | } |
1872c5c1 | 568 | width += (seplen * (nfifos - 1)); |
3c836bfd | 569 | buf = calloc(1, width + 1); |
4d66492f SK |
570 | if (buf == NULL) |
571 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
572 | memset(buf, ' ', width); | |
573 | buf[width] = '\0'; | |
e6c523cd | 574 | /* 3rd pass to set the separators */ |
16e0239d | 575 | for (Fifo *f = cfg.fifos; f; f = f->next) { |
efa97b71 | 576 | if (f->pos_init) { /* Skip the first, left-most */ |
77c76070 | 577 | /* Copying only seplen ensures we omit the '\0' byte. */ |
544b0835 SK |
578 | strncpy( |
579 | buf + (f->pos_init - seplen), | |
16e0239d | 580 | cfg.separator, |
544b0835 SK |
581 | seplen |
582 | ); | |
4d66492f SK |
583 | } |
584 | } | |
585 | ||
16e0239d | 586 | if (cfg.output_to_x_root_window && !(d = XOpenDisplay(NULL))) |
544b0835 | 587 | fatal("XOpenDisplay failed with: %p\n", d); |
805f0d22 | 588 | /* TODO: Handle signals */ |
4d66492f | 589 | for (;;) { |
b7487ec5 | 590 | clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors |
16e0239d SK |
591 | fifo_read_all(&cfg, &ti, buf); |
592 | if (cfg.output_to_x_root_window) { | |
544b0835 | 593 | if (XStoreName(d, DefaultRootWindow(d), buf) < 0) |
fabb8771 | 594 | fatal("XStoreName failed.\n"); |
544b0835 | 595 | XFlush(d); |
fabb8771 SK |
596 | } else { |
597 | puts(buf); | |
598 | fflush(stdout); | |
599 | } | |
b7487ec5 SK |
600 | clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors |
601 | timespecsub(&t1, &t0, &td); | |
544b0835 SK |
602 | debug( |
603 | "td {tv_sec = %ld, tv_nsec = %ld}\n", | |
604 | td.tv_sec, | |
605 | td.tv_nsec | |
606 | ); | |
a415999c | 607 | if (timespeccmp(&td, &ti, <)) { |
b7487ec5 SK |
608 | /* Pushback on data producers by refusing to read the |
609 | * pipe more frequently than the interval. | |
610 | */ | |
611 | timespecsub(&ti, &td, &tc); | |
612 | debug("snooze YES\n"); | |
17a27e48 | 613 | snooze(&tc); |
574a4bff | 614 | } else { |
b7487ec5 | 615 | debug("snooze NO\n"); |
574a4bff | 616 | } |
4d66492f | 617 | } |
3d7e82a8 SK |
618 | |
619 | return EXIT_SUCCESS; | |
9b5ebc12 | 620 | } |