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 | ||
27 | static const char errmsg[] = ERRMSG; | |
28 | static const int errlen = sizeof(ERRMSG) - 1; | |
29 | ||
4d66492f | 30 | char *argv0; |
9b5ebc12 | 31 | |
900b9a6b SK |
32 | /* TODO: Convert slot list to slot array. */ |
33 | typedef struct Slot Slot; | |
34 | struct Slot { | |
35 | char *in_fifo; | |
36 | int in_fd; | |
37 | struct timespec in_last_read; | |
38 | struct timespec out_ttl; | |
39 | int out_width; | |
40 | int out_pos_lo; /* Lowest position on the output buffer. */ | |
41 | int out_pos_cur; /* Current position on the output buffer. */ | |
42 | int out_pos_hi; /* Highest position on the output buffer. */ | |
43 | Slot *next; | |
9b5ebc12 SK |
44 | }; |
45 | ||
4d66492f SK |
46 | typedef struct Config Config; |
47 | struct Config { | |
348d5f36 | 48 | double interval; |
4d66492f | 49 | char * separator; |
900b9a6b SK |
50 | Slot * slots; |
51 | int slot_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 |
900b9a6b | 64 | slot_print_one(Slot *s) |
77c76070 | 65 | { |
900b9a6b | 66 | khlib_info("Slot " |
a6b13fa2 | 67 | "{" |
900b9a6b SK |
68 | " in_fifo = %s," |
69 | " in_fd = %d," | |
70 | " out_width = %d," | |
71 | " in_last_read = {tv_sec = %ld, tv_nsec = %ld}" | |
72 | " out_ttl = {tv_sec = %ld, tv_nsec = %ld}," | |
73 | " out_pos_lo = %d," | |
74 | " out_pos_cur = %d," | |
75 | " out_pos_hi = %d," | |
a6b13fa2 SK |
76 | " next = %p," |
77 | " }\n", | |
900b9a6b SK |
78 | s->in_fifo, |
79 | s->in_fd, | |
80 | s->out_width, | |
81 | s->in_last_read.tv_sec, | |
82 | s->in_last_read.tv_nsec, | |
83 | s->out_ttl.tv_sec, | |
84 | s->out_ttl.tv_nsec, | |
85 | s->out_pos_lo, | |
86 | s->out_pos_cur, | |
87 | s->out_pos_hi, | |
88 | s->next | |
77c76070 SK |
89 | ); |
90 | } | |
91 | ||
92 | void | |
900b9a6b | 93 | slot_print_all(Slot *head) |
77c76070 | 94 | { |
900b9a6b SK |
95 | for (Slot *s = head; s; s = s->next) { |
96 | slot_print_one(s); | |
77c76070 SK |
97 | } |
98 | } | |
99 | ||
100 | void | |
b6316e94 | 101 | config_print(Config *cfg) |
77c76070 | 102 | { |
1084633a | 103 | khlib_info( |
a6b13fa2 SK |
104 | "Config " |
105 | "{" | |
348d5f36 | 106 | " interval = %f," |
a6b13fa2 | 107 | " separator = %s," |
900b9a6b | 108 | " slot_count = %d," |
a6b13fa2 | 109 | " total_width = %d," |
900b9a6b | 110 | " slots = ..." |
a6b13fa2 SK |
111 | " }\n", |
112 | cfg->interval, | |
113 | cfg->separator, | |
900b9a6b | 114 | cfg->slot_count, |
17a27e48 | 115 | cfg->total_width |
77c76070 | 116 | ); |
900b9a6b | 117 | slot_print_all(cfg->slots); |
77c76070 | 118 | } |
9b5ebc12 SK |
119 | |
120 | int | |
900b9a6b | 121 | is_pos_num(char *str) |
9b5ebc12 | 122 | { |
900b9a6b SK |
123 | while (*str != '\0') |
124 | if (!isdigit(*(str++))) | |
9b5ebc12 SK |
125 | return 0; |
126 | return 1; | |
127 | } | |
128 | ||
348d5f36 | 129 | int |
900b9a6b | 130 | is_decimal(char *str) |
348d5f36 SK |
131 | { |
132 | char c; | |
133 | int seen = 0; | |
134 | ||
900b9a6b | 135 | while ((c = *(str++)) != '\0') |
348d5f36 SK |
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 | ); | |
1084633a | 223 | _khlib_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 | ||
900b9a6b | 265 | struct timespec in_last_read; |
0a01172a | 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); |
900b9a6b SK |
271 | in_last_read.tv_sec = 0; |
272 | in_last_read.tv_nsec = 0; | |
273 | Slot *s = calloc(1, sizeof(struct Slot)); | |
274 | if (s) { | |
275 | s->in_fifo = n; | |
276 | s->in_fd = -1; | |
277 | s->out_width = atoi(w); | |
278 | s->out_ttl = khlib_timespec_of_float(atof(t)); | |
279 | s->in_last_read = in_last_read; | |
280 | s->out_pos_lo = cfg->total_width; | |
281 | s->out_pos_cur = s->out_pos_lo; | |
282 | s->out_pos_hi = s->out_pos_lo + s->out_width - 1; | |
283 | s->next = cfg->slots; | |
284 | ||
285 | cfg->slots = s; | |
286 | cfg->total_width += s->out_width; | |
287 | cfg->slot_count++; | |
9b5ebc12 | 288 | } else { |
1084633a | 289 | khlib_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 | ||
900b9a6b SK |
313 | Slot *last = cfg->slots; |
314 | cfg->slots = NULL; | |
315 | for (Slot *s = last; s; ) { | |
316 | Slot *next = s->next; | |
317 | s->next = cfg->slots; | |
318 | cfg->slots = s; | |
319 | s = next; | |
4d66492f SK |
320 | } |
321 | } | |
322 | ||
0a01172a | 323 | void |
900b9a6b | 324 | slot_expire(Slot *s, struct timespec t, char *buf) |
0a01172a SK |
325 | { |
326 | struct timespec td; | |
327 | ||
900b9a6b SK |
328 | timespecsub(&t, &(s->in_last_read), &td); |
329 | if (timespeccmp(&td, &(s->out_ttl), >=)) { | |
0a01172a | 330 | /* TODO: Maybe configurable expiry character. */ |
900b9a6b SK |
331 | memset(buf + s->out_pos_lo, '_', s->out_pos_hi - s->out_pos_lo); |
332 | khlib_warn("Slot expired: \"%s\"\n", s->in_fifo); | |
0a01172a SK |
333 | } |
334 | } | |
335 | ||
e6c523cd | 336 | void |
900b9a6b | 337 | slot_read_error(Slot *s, char *buf) |
e6c523cd SK |
338 | { |
339 | char *b; | |
340 | int i; | |
341 | ||
900b9a6b | 342 | b = buf + s->out_pos_lo; |
e6c523cd | 343 | /* Copy as much of the error message as possible. |
17e4ecd5 | 344 | * EXCLUDING the terminating \0. */ |
900b9a6b | 345 | for (i = 0; i < errlen && i < s->out_width; i++) |
e6c523cd SK |
346 | b[i] = errmsg[i]; |
347 | /* Any remaining slots: */ | |
900b9a6b | 348 | for (; i < s->out_width; i++) |
e6c523cd SK |
349 | b[i] = '_'; |
350 | } | |
351 | ||
efa97b71 | 352 | enum read_status |
900b9a6b | 353 | slot_read_one(Slot *s, 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 (;;) { |
900b9a6b | 359 | switch (read(s->in_fd, &c, 1)) { |
efa97b71 | 360 | case -1: |
1084633a SK |
361 | khlib_error( |
362 | "Failed to read: \"%s\". errno: %d, msg: %s\n", | |
900b9a6b SK |
363 | s->in_fifo, |
364 | errno, | |
365 | strerror(errno) | |
1084633a | 366 | ); |
e6441710 | 367 | switch (errno) { |
8ce5d971 | 368 | case EINTR: |
e6441710 SK |
369 | case EAGAIN: |
370 | return RETRY; | |
371 | default: | |
372 | return FAILURE; | |
373 | } | |
efa97b71 | 374 | case 0: |
900b9a6b SK |
375 | khlib_debug("%s: End of FILE\n", s->in_fifo); |
376 | s->out_pos_cur = s->out_pos_lo; | |
efa97b71 SK |
377 | return END_OF_FILE; |
378 | case 1: | |
379 | /* TODO: Consider making msg term char a CLI option */ | |
380 | if (c == '\n' || c == '\0') { | |
900b9a6b | 381 | r = s->out_pos_hi - s->out_pos_cur; |
efa97b71 | 382 | if (r > 0) |
900b9a6b SK |
383 | memset(buf + s->out_pos_cur, ' ', r); |
384 | s->out_pos_cur = s->out_pos_lo; | |
385 | s->in_last_read = t; | |
efa97b71 SK |
386 | return END_OF_MESSAGE; |
387 | } else { | |
900b9a6b SK |
388 | if (s->out_pos_cur <= s->out_pos_hi) |
389 | buf[s->out_pos_cur++] = c; | |
efa97b71 | 390 | /* Drop beyond available range. */ |
9f40d4f2 SK |
391 | /* |
392 | * TODO Define max after which we stop reading. | |
393 | * To ensure that a rogue large message | |
394 | * doesn't trap us here. | |
395 | */ | |
efa97b71 SK |
396 | } |
397 | break; | |
398 | default: | |
399 | assert(0); | |
400 | } | |
574a4bff | 401 | } |
77c76070 SK |
402 | } |
403 | ||
a415999c | 404 | void |
900b9a6b | 405 | slot_read_all(Config *cfg, struct timespec *ti, char *buf) |
4d66492f | 406 | { |
77c76070 | 407 | fd_set fds; |
bfab34f8 | 408 | int maxfd = -1; |
a415999c | 409 | int ready = 0; |
bec93767 | 410 | struct stat st; |
0741fd04 | 411 | struct timespec t; |
77c76070 SK |
412 | |
413 | FD_ZERO(&fds); | |
900b9a6b | 414 | for (Slot *s = cfg->slots; s; s = s->next) { |
bec93767 | 415 | /* TODO: Create the FIFO if it doesn't already exist. */ |
900b9a6b | 416 | if (lstat(s->in_fifo, &st) < 0) { |
1084633a | 417 | khlib_error( |
544b0835 | 418 | "Cannot stat \"%s\". Error: %s\n", |
900b9a6b | 419 | s->in_fifo, |
544b0835 SK |
420 | strerror(errno) |
421 | ); | |
900b9a6b | 422 | slot_read_error(s, buf); |
e6c523cd SK |
423 | continue; |
424 | } | |
425 | if (!(st.st_mode & S_IFIFO)) { | |
900b9a6b SK |
426 | khlib_error("\"%s\" is not a FIFO\n", s->in_fifo); |
427 | slot_read_error(s, buf); | |
e6c523cd SK |
428 | continue; |
429 | } | |
900b9a6b | 430 | if (s->in_fd < 0) { |
1084633a | 431 | khlib_debug( |
900b9a6b SK |
432 | "%s: closed. opening. in_fd: %d\n", |
433 | s->in_fifo, | |
434 | s->in_fd | |
1084633a | 435 | ); |
900b9a6b | 436 | s->in_fd = open(s->in_fifo, O_RDONLY | O_NONBLOCK); |
efa97b71 | 437 | } else { |
1084633a | 438 | khlib_debug( |
900b9a6b SK |
439 | "%s: already openned. in_fd: %d\n", |
440 | s->in_fifo, | |
441 | s->in_fd | |
1084633a | 442 | ); |
efa97b71 | 443 | } |
900b9a6b SK |
444 | if (s->in_fd == -1) { |
445 | /* TODO Consider backing off retries for failed slots */ | |
446 | khlib_error("Failed to open \"%s\"\n", s->in_fifo); | |
447 | slot_read_error(s, buf); | |
e6c523cd SK |
448 | continue; |
449 | } | |
900b9a6b SK |
450 | khlib_debug("%s: open. in_fd: %d\n", s->in_fifo, s->in_fd); |
451 | if (s->in_fd > maxfd) | |
452 | maxfd = s->in_fd; | |
453 | FD_SET(s->in_fd, &fds); | |
77c76070 | 454 | } |
1084633a | 455 | khlib_debug("selecting...\n"); |
eb6dbe7a | 456 | ready = pselect(maxfd + 1, &fds, NULL, NULL, ti, NULL); |
1084633a | 457 | khlib_debug("ready: %d\n", ready); |
0741fd04 | 458 | clock_gettime(CLOCK_MONOTONIC, &t); |
0166c18d SK |
459 | if (ready == -1) { |
460 | switch (errno) { | |
461 | case EINTR: | |
1084633a SK |
462 | khlib_error( |
463 | "pselect temp failure: %d, errno: %d, msg: %s\n", | |
464 | ready, | |
465 | errno, | |
466 | strerror(errno) | |
467 | ); | |
0166c18d SK |
468 | /* TODO: Reconsider what to do here. */ |
469 | return; | |
470 | default: | |
1084633a SK |
471 | khlib_fatal( |
472 | "pselect failed: %d, errno: %d, msg: %s\n", | |
473 | ready, | |
474 | errno, | |
475 | strerror(errno) | |
476 | ); | |
0166c18d SK |
477 | } |
478 | } | |
716cc2b4 SK |
479 | /* At-least-once ensures that expiries are still checked on timeouts. */ |
480 | do { | |
900b9a6b SK |
481 | for (Slot *s = cfg->slots; s; s = s->next) { |
482 | if (FD_ISSET(s->in_fd, &fds)) { | |
483 | khlib_debug("reading: %s\n", s->in_fifo); | |
484 | switch (slot_read_one(s, t, buf)) { | |
a415999c SK |
485 | /* |
486 | * ### MESSAGE LOSS ### | |
487 | * is introduced by closing at EOM in addition | |
488 | * to EOF, since there may be unread messages | |
489 | * remaining in the pipe. However, | |
490 | * | |
491 | * ### INTER-MESSAGE PUSHBACK ### | |
492 | * is also gained, since pipes block at the | |
493 | * "open" call. | |
494 | * | |
495 | * This is an acceptable trade-off because we | |
496 | * are a stateless reporter of a _most-recent_ | |
497 | * status, not a stateful accumulator. | |
498 | */ | |
499 | case END_OF_MESSAGE: | |
500 | case END_OF_FILE: | |
501 | case FAILURE: | |
900b9a6b SK |
502 | close(s->in_fd); |
503 | s->in_fd = -1; | |
a415999c SK |
504 | ready--; |
505 | break; | |
506 | case RETRY: | |
507 | break; | |
508 | default: | |
509 | assert(0); | |
510 | } | |
716cc2b4 | 511 | } else { |
900b9a6b | 512 | slot_expire(s, t, buf); |
efa97b71 | 513 | } |
b7487ec5 | 514 | } |
716cc2b4 | 515 | } while (ready); |
a415999c | 516 | assert(ready == 0); |
b7487ec5 SK |
517 | } |
518 | ||
9b5ebc12 | 519 | int |
4d66492f | 520 | main(int argc, char *argv[]) |
9b5ebc12 | 521 | { |
16e0239d SK |
522 | Config cfg = { |
523 | .interval = 1.0, | |
524 | .separator = "|", | |
900b9a6b SK |
525 | .slots = NULL, |
526 | .slot_count = 0, | |
16e0239d SK |
527 | .total_width = 0, |
528 | .output_to_x_root_window = 0, | |
529 | }; | |
530 | ||
f277f405 | 531 | int width = 0; |
900b9a6b | 532 | int nslots = 0; |
f277f405 | 533 | int seplen = 0; |
4d66492f | 534 | int prefix = 0; |
e6c523cd | 535 | int errors = 0; |
4d66492f | 536 | char *buf; |
544b0835 | 537 | Display *d = NULL; |
e6c523cd | 538 | struct stat st; |
b7487ec5 | 539 | struct timespec |
900b9a6b SK |
540 | t0, /* time stamp. before reading slots */ |
541 | t1, /* time stamp. after reading slots */ | |
b7487ec5 SK |
542 | ti, /* time interval desired (t1 - t0) */ |
543 | td, /* time interval measured (t1 - t0) */ | |
544 | tc; /* time interval correction (ti - td) when td < ti */ | |
4d66492f SK |
545 | |
546 | argv0 = argv[0]; | |
547 | ||
16e0239d | 548 | opts_parse(&cfg, argc, argv); |
1084633a | 549 | khlib_debug("argv0 = %s\n", argv0); |
16e0239d | 550 | config_print(&cfg); |
b7487ec5 | 551 | |
1084633a | 552 | ti = khlib_timespec_of_float(cfg.interval); |
b7487ec5 | 553 | |
900b9a6b SK |
554 | if (cfg.slots == NULL) |
555 | usage("No slot specs were given!\n"); | |
4d66492f | 556 | |
e6c523cd | 557 | /* 1st pass to check file existence and type */ |
900b9a6b SK |
558 | for (Slot *s = cfg.slots; s; s = s->next) { |
559 | if (lstat(s->in_fifo, &st) < 0) { | |
1084633a | 560 | khlib_error( |
544b0835 | 561 | "Cannot stat \"%s\". Error: %s\n", |
900b9a6b | 562 | s->in_fifo, |
544b0835 SK |
563 | strerror(errno) |
564 | ); | |
e6c523cd SK |
565 | errors++; |
566 | continue; | |
567 | } | |
568 | if (!(st.st_mode & S_IFIFO)) { | |
900b9a6b | 569 | khlib_error("\"%s\" is not a FIFO\n", s->in_fifo); |
e6c523cd SK |
570 | errors++; |
571 | continue; | |
572 | } | |
573 | } | |
574 | if (errors) | |
1084633a SK |
575 | khlib_fatal( |
576 | "Encountered errors with given file paths. See log.\n" | |
577 | ); | |
e6c523cd | 578 | |
16e0239d SK |
579 | width = cfg.total_width; |
580 | seplen = strlen(cfg.separator); | |
4d66492f | 581 | |
e6c523cd | 582 | /* 2nd pass to make space for separators */ |
900b9a6b SK |
583 | for (Slot *s = cfg.slots; s; s = s->next) { |
584 | s->out_pos_lo += prefix; | |
585 | s->out_pos_hi += prefix; | |
586 | s->out_pos_cur = s->out_pos_lo; | |
4d66492f | 587 | prefix += seplen; |
900b9a6b | 588 | nslots++; |
4d66492f | 589 | } |
900b9a6b | 590 | width += (seplen * (nslots - 1)); |
3c836bfd | 591 | buf = calloc(1, width + 1); |
4d66492f | 592 | if (buf == NULL) |
1084633a SK |
593 | khlib_fatal( |
594 | "[memory] Failed to allocate buffer of %d bytes", | |
595 | width | |
596 | ); | |
4d66492f SK |
597 | memset(buf, ' ', width); |
598 | buf[width] = '\0'; | |
e6c523cd | 599 | /* 3rd pass to set the separators */ |
900b9a6b SK |
600 | for (Slot *s = cfg.slots; s; s = s->next) { |
601 | if (s->out_pos_lo) { /* Skip the first, left-most */ | |
77c76070 | 602 | /* Copying only seplen ensures we omit the '\0' byte. */ |
544b0835 | 603 | strncpy( |
900b9a6b | 604 | buf + (s->out_pos_lo - seplen), |
16e0239d | 605 | cfg.separator, |
544b0835 SK |
606 | seplen |
607 | ); | |
4d66492f SK |
608 | } |
609 | } | |
610 | ||
16e0239d | 611 | if (cfg.output_to_x_root_window && !(d = XOpenDisplay(NULL))) |
1084633a | 612 | khlib_fatal("XOpenDisplay failed with: %p\n", d); |
805f0d22 | 613 | /* TODO: Handle signals */ |
4d66492f | 614 | for (;;) { |
b7487ec5 | 615 | clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors |
900b9a6b | 616 | slot_read_all(&cfg, &ti, buf); |
16e0239d | 617 | if (cfg.output_to_x_root_window) { |
544b0835 | 618 | if (XStoreName(d, DefaultRootWindow(d), buf) < 0) |
1084633a | 619 | khlib_fatal("XStoreName failed.\n"); |
544b0835 | 620 | XFlush(d); |
fabb8771 SK |
621 | } else { |
622 | puts(buf); | |
623 | fflush(stdout); | |
624 | } | |
b7487ec5 SK |
625 | clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors |
626 | timespecsub(&t1, &t0, &td); | |
1084633a | 627 | khlib_debug( |
544b0835 SK |
628 | "td {tv_sec = %ld, tv_nsec = %ld}\n", |
629 | td.tv_sec, | |
630 | td.tv_nsec | |
631 | ); | |
a415999c | 632 | if (timespeccmp(&td, &ti, <)) { |
b7487ec5 SK |
633 | /* Pushback on data producers by refusing to read the |
634 | * pipe more frequently than the interval. | |
635 | */ | |
636 | timespecsub(&ti, &td, &tc); | |
1084633a SK |
637 | khlib_debug("khlib_sleep YES\n"); |
638 | khlib_sleep(&tc); | |
574a4bff | 639 | } else { |
1084633a | 640 | khlib_debug("khlib_sleep NO\n"); |
574a4bff | 641 | } |
4d66492f | 642 | } |
3d7e82a8 SK |
643 | |
644 | return EXIT_SUCCESS; | |
9b5ebc12 | 645 | } |