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