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