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