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