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; |
4d66492f | 34 | int last_read; |
9b5ebc12 | 35 | int 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," | |
74 | " last_read = %d," | |
75 | " ttl = %d," | |
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, | |
84 | f->last_read, | |
85 | f->ttl, | |
efa97b71 SK |
86 | f->pos_init, |
87 | f->pos_curr, | |
88 | f->pos_final, | |
a6b13fa2 | 89 | f->next |
77c76070 SK |
90 | ); |
91 | } | |
92 | ||
93 | void | |
17a27e48 | 94 | fifo_print_all(Fifo *head) |
77c76070 | 95 | { |
1872c5c1 | 96 | for (Fifo *f = head; f; f = f->next) { |
17a27e48 | 97 | fifo_print_one(f); |
77c76070 SK |
98 | } |
99 | } | |
100 | ||
101 | void | |
b6316e94 | 102 | config_print(Config *cfg) |
77c76070 | 103 | { |
b6316e94 | 104 | info( |
a6b13fa2 SK |
105 | "Config " |
106 | "{" | |
348d5f36 | 107 | " interval = %f," |
a6b13fa2 SK |
108 | " separator = %s," |
109 | " fifo_count = %d," | |
110 | " total_width = %d," | |
a6b13fa2 SK |
111 | " fifos = ..." |
112 | " }\n", | |
113 | cfg->interval, | |
114 | cfg->separator, | |
115 | cfg->fifo_count, | |
17a27e48 | 116 | cfg->total_width |
77c76070 | 117 | ); |
17a27e48 | 118 | fifo_print_all(cfg->fifos); |
77c76070 | 119 | } |
9b5ebc12 SK |
120 | |
121 | int | |
122 | is_pos_num(char *s) | |
123 | { | |
124 | while (*s != '\0') | |
125 | if (!isdigit(*(s++))) | |
126 | return 0; | |
127 | return 1; | |
128 | } | |
129 | ||
348d5f36 SK |
130 | int |
131 | is_decimal(char *s) | |
132 | { | |
133 | char c; | |
134 | int seen = 0; | |
135 | ||
136 | while ((c = *(s++)) != '\0') | |
137 | if (!isdigit(c)) { | |
138 | if (c == '.' && !seen++) | |
139 | continue; | |
140 | else | |
141 | return 0; | |
142 | } | |
143 | return 1; | |
144 | } | |
145 | ||
9b5ebc12 SK |
146 | void |
147 | print_usage() | |
148 | { | |
4d66492f | 149 | assert(argv0); |
9b5ebc12 | 150 | fprintf( |
a6b13fa2 SK |
151 | stderr, |
152 | "\n" | |
153 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" | |
154 | "\n" | |
155 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" | |
156 | " FILE_PATH = string\n" | |
157 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
158 | " DATA_TTL = int (* (positive) number of seconds *)\n" | |
159 | " OPTION = -i INTERVAL\n" | |
160 | " | -s SEPARATOR\n" | |
161 | " | -x (* Output to X root window *)\n" | |
162 | " | -l LOG_LEVEL\n" | |
163 | " SEPARATOR = string\n" | |
164 | " INTERVAL = int (* (positive) number of seconds *)\n" | |
165 | " LOG_LEVEL = int (* %d through %d *)\n" | |
166 | "\n", | |
167 | argv0, | |
168 | Nothing, | |
169 | Debug | |
9b5ebc12 SK |
170 | ); |
171 | fprintf( | |
a6b13fa2 SK |
172 | stderr, |
173 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
174 | "\n", | |
175 | argv0 | |
9b5ebc12 SK |
176 | ); |
177 | } | |
178 | ||
4d66492f | 179 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
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) | |
218 | usage("Option -l value (%d) exceeds maximum (%d)\n", log_level, Debug); | |
17a27e48 | 219 | _khatus_lib_log_level = log_level; |
4c438cef | 220 | opts_parse_any(cfg, argc, argv, i); |
b6316e94 SK |
221 | } |
222 | ||
4d66492f SK |
223 | void |
224 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
225 | { |
226 | switch (argv[i][1]) { | |
ce552549 SK |
227 | case 'i': |
228 | /* TODO: Generic set_int */ | |
229 | parse_opts_opt_i(cfg, argc, argv, ++i); | |
230 | break; | |
231 | case 's': | |
232 | /* TODO: Generic set_str */ | |
233 | parse_opts_opt_s(cfg, argc, argv, ++i); | |
234 | break; | |
235 | case 'x': | |
236 | cfg->output_to_x_root_window = 1; | |
237 | opts_parse_any(cfg, argc, argv, ++i); | |
238 | break; | |
239 | case 'l': | |
240 | /* TODO: Generic set_int */ | |
241 | parse_opts_opt_l(cfg, argc, argv, ++i); | |
242 | break; | |
243 | default : | |
244 | usage("Option \"%s\" is invalid\n", argv[i]); | |
9b5ebc12 SK |
245 | } |
246 | } | |
247 | ||
248 | void | |
4d66492f | 249 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
250 | { |
251 | if ((i + 3) > argc) | |
1872c5c1 | 252 | usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv[i]); |
9b5ebc12 SK |
253 | |
254 | char *n = argv[i++]; | |
255 | char *w = argv[i++]; | |
256 | char *t = argv[i++]; | |
257 | ||
258 | if (!is_pos_num(w)) | |
1872c5c1 | 259 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
9b5ebc12 | 260 | if (!is_pos_num(t)) |
1872c5c1 SK |
261 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
262 | Fifo *f = calloc(1, sizeof(struct Fifo)); | |
9b5ebc12 | 263 | if (f) { |
4d66492f SK |
264 | f->name = n; |
265 | f->fd = -1; | |
266 | f->width = atoi(w); | |
267 | f->ttl = atoi(t); | |
268 | f->last_read = 0; | |
efa97b71 SK |
269 | f->pos_init = cfg->total_width; |
270 | f->pos_curr = f->pos_init; | |
271 | f->pos_final = f->pos_init + f->width - 1; | |
1872c5c1 | 272 | f->next = cfg->fifos; |
4d66492f | 273 | |
1872c5c1 | 274 | cfg->fifos = f; |
4d66492f | 275 | cfg->total_width += f->width; |
1872c5c1 | 276 | cfg->fifo_count++; |
9b5ebc12 | 277 | } else { |
4d66492f | 278 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 279 | } |
4d66492f | 280 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
281 | } |
282 | ||
283 | void | |
4d66492f | 284 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
285 | { |
286 | if (i < argc) { | |
287 | switch (argv[i][0]) { | |
ce552549 SK |
288 | case '-': |
289 | parse_opts_opt(cfg, argc, argv, i); | |
290 | break; | |
291 | default : | |
292 | parse_opts_spec(cfg, argc, argv, i); | |
4d66492f SK |
293 | } |
294 | } | |
295 | } | |
296 | ||
297 | void | |
5400b86f | 298 | opts_parse(Config *cfg, int argc, char *argv[]) |
4d66492f SK |
299 | { |
300 | opts_parse_any(cfg, argc, argv, 1); | |
301 | ||
1872c5c1 SK |
302 | Fifo *last = cfg->fifos; |
303 | cfg->fifos = NULL; | |
304 | for (Fifo *f = last; f; ) { | |
305 | Fifo *next = f->next; | |
306 | f->next = cfg->fifos; | |
307 | cfg->fifos = f; | |
4d66492f SK |
308 | f = next; |
309 | } | |
310 | } | |
311 | ||
e6c523cd | 312 | void |
69b75a40 | 313 | fifo_read_error(Fifo *f, char *buf) |
e6c523cd SK |
314 | { |
315 | char *b; | |
316 | int i; | |
317 | ||
efa97b71 | 318 | b = buf + f->pos_init; |
e6c523cd SK |
319 | /* Copy as much of the error message as possible. |
320 | * EXCLUDING the reminating \0. */ | |
321 | for (i = 0; i < errlen && i < f->width; i++) | |
322 | b[i] = errmsg[i]; | |
323 | /* Any remaining slots: */ | |
324 | for (; i < f->width; i++) | |
325 | b[i] = '_'; | |
326 | } | |
327 | ||
efa97b71 | 328 | enum read_status |
17a27e48 | 329 | fifo_read_one(Fifo *f, char *buf) |
77c76070 | 330 | { |
efa97b71 SK |
331 | /* Initialize all to an impossible value: */ |
332 | ssize_t n = -5; /* Number of bytes read. */ | |
333 | char c = -1; /* Character read. */ | |
334 | int r = -1; /* Remaining unused slots in buffer range. */ | |
77c76070 | 335 | |
efa97b71 SK |
336 | for (;;) { |
337 | n = read(f->fd, &c, 1); | |
338 | assert(n >= -1 && n <= 1); | |
339 | switch (n) { | |
340 | case -1: | |
341 | error("Failed to read: \"%s\". errno: %d, msg: %s\n", | |
342 | f->name, errno, strerror(errno)); | |
e6441710 | 343 | switch (errno) { |
8ce5d971 | 344 | case EINTR: |
e6441710 SK |
345 | case EAGAIN: |
346 | return RETRY; | |
347 | default: | |
348 | return FAILURE; | |
349 | } | |
efa97b71 SK |
350 | case 0: |
351 | debug("%s: End of FILE\n", f->name); | |
352 | f->pos_curr = f->pos_init; | |
353 | return END_OF_FILE; | |
354 | case 1: | |
355 | /* TODO: Consider making msg term char a CLI option */ | |
356 | if (c == '\n' || c == '\0') { | |
357 | r = f->pos_final - f->pos_curr; | |
358 | if (r > 0) | |
359 | memset(buf + f->pos_curr, ' ', r); | |
360 | f->pos_curr = f->pos_init; | |
361 | return END_OF_MESSAGE; | |
362 | } else { | |
363 | if (f->pos_curr <= f->pos_final) | |
364 | buf[f->pos_curr++] = c; | |
365 | /* Drop beyond available range. */ | |
366 | } | |
367 | break; | |
368 | default: | |
369 | assert(0); | |
370 | } | |
574a4bff | 371 | } |
6c29392d | 372 | /* TODO Record timestamp read */ |
77c76070 SK |
373 | } |
374 | ||
4d66492f | 375 | void |
69b75a40 | 376 | fifo_read_all(Config *cfg, char *buf) |
4d66492f | 377 | { |
77c76070 | 378 | fd_set fds; |
bfab34f8 | 379 | int maxfd = -1; |
77c76070 | 380 | int ready; |
bec93767 | 381 | struct stat st; |
77c76070 SK |
382 | |
383 | FD_ZERO(&fds); | |
1872c5c1 | 384 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
bec93767 | 385 | /* TODO: Create the FIFO if it doesn't already exist. */ |
e6c523cd SK |
386 | if (lstat(f->name, &st) < 0) { |
387 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
69b75a40 | 388 | fifo_read_error(f, buf); |
e6c523cd SK |
389 | continue; |
390 | } | |
391 | if (!(st.st_mode & S_IFIFO)) { | |
392 | error("\"%s\" is not a FIFO\n", f->name); | |
69b75a40 | 393 | fifo_read_error(f, buf); |
e6c523cd SK |
394 | continue; |
395 | } | |
efa97b71 SK |
396 | if (f->fd < 0) { |
397 | debug("%s: closed. opening. fd: %d\n", f->name, f->fd); | |
77c76070 | 398 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
efa97b71 SK |
399 | } else { |
400 | debug("%s: already openned. fd: %d\n", f->name, f->fd); | |
401 | } | |
e6c523cd | 402 | if (f->fd == -1) { |
1872c5c1 | 403 | /* TODO: Consider backing off retries for failed fifos. */ |
e6c523cd | 404 | error("Failed to open \"%s\"\n", f->name); |
69b75a40 | 405 | fifo_read_error(f, buf); |
e6c523cd SK |
406 | continue; |
407 | } | |
efa97b71 | 408 | debug("%s: open. fd: %d\n", f->name, f->fd); |
77c76070 SK |
409 | if (f->fd > maxfd) |
410 | maxfd = f->fd; | |
411 | FD_SET(f->fd, &fds); | |
412 | } | |
413 | debug("selecting...\n"); | |
414 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); | |
415 | debug("ready: %d\n", ready); | |
416 | assert(ready != 0); | |
417 | if (ready < 0) | |
418 | fatal("%s", strerror(errno)); | |
1872c5c1 | 419 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
77c76070 SK |
420 | if (FD_ISSET(f->fd, &fds)) { |
421 | debug("reading: %s\n", f->name); | |
efa97b71 SK |
422 | switch (fifo_read_one(f, buf)) { |
423 | case END_OF_FILE: | |
e6441710 | 424 | case FAILURE: |
efa97b71 SK |
425 | close(f->fd); |
426 | f->fd = -1; | |
427 | break; | |
428 | case END_OF_MESSAGE: | |
e6441710 | 429 | case RETRY: |
efa97b71 SK |
430 | break; |
431 | default: | |
432 | assert(0); | |
433 | } | |
b7487ec5 SK |
434 | } |
435 | } | |
436 | } | |
437 | ||
9b5ebc12 | 438 | int |
4d66492f | 439 | main(int argc, char *argv[]) |
9b5ebc12 | 440 | { |
f277f405 | 441 | int width = 0; |
1872c5c1 | 442 | int nfifos = 0; |
f277f405 | 443 | int seplen = 0; |
4d66492f | 444 | int prefix = 0; |
e6c523cd | 445 | int errors = 0; |
4d66492f | 446 | char *buf; |
a6d4c9c3 SK |
447 | Config cfg0 = defaults; |
448 | Config *cfg = &cfg0; | |
bfab34f8 | 449 | Display *display = NULL; |
e6c523cd | 450 | struct stat st; |
b7487ec5 SK |
451 | struct timespec |
452 | t0, /* time stamp. before reading fifos */ | |
453 | t1, /* time stamp. after reading fifos */ | |
454 | ti, /* time interval desired (t1 - t0) */ | |
455 | td, /* time interval measured (t1 - t0) */ | |
456 | tc; /* time interval correction (ti - td) when td < ti */ | |
4d66492f SK |
457 | |
458 | argv0 = argv[0]; | |
459 | ||
5400b86f | 460 | opts_parse(cfg, argc, argv); |
4d66492f | 461 | debug("argv0 = %s\n", argv0); |
77c76070 | 462 | config_print(cfg); |
b7487ec5 | 463 | |
348d5f36 | 464 | ti = timespec_of_float(cfg->interval); |
b7487ec5 | 465 | |
1872c5c1 SK |
466 | if (cfg->fifos == NULL) |
467 | usage("No fifo specs were given!\n"); | |
4d66492f | 468 | |
e6c523cd SK |
469 | /* 1st pass to check file existence and type */ |
470 | for (Fifo *f = cfg->fifos; f; f = f->next) { | |
471 | if (lstat(f->name, &st) < 0) { | |
472 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
473 | errors++; | |
474 | continue; | |
475 | } | |
476 | if (!(st.st_mode & S_IFIFO)) { | |
477 | error("\"%s\" is not a FIFO\n", f->name); | |
478 | errors++; | |
479 | continue; | |
480 | } | |
481 | } | |
482 | if (errors) | |
483 | fatal("Encountered errors with the given file paths. See log.\n"); | |
484 | ||
4d66492f SK |
485 | width = cfg->total_width; |
486 | seplen = strlen(cfg->separator); | |
487 | ||
e6c523cd | 488 | /* 2nd pass to make space for separators */ |
1872c5c1 | 489 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
efa97b71 SK |
490 | f->pos_init += prefix; |
491 | f->pos_final += prefix; | |
492 | f->pos_curr = f->pos_init; | |
4d66492f | 493 | prefix += seplen; |
1872c5c1 | 494 | nfifos++; |
4d66492f | 495 | } |
1872c5c1 | 496 | width += (seplen * (nfifos - 1)); |
3c836bfd | 497 | buf = calloc(1, width + 1); |
4d66492f SK |
498 | if (buf == NULL) |
499 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
500 | memset(buf, ' ', width); | |
501 | buf[width] = '\0'; | |
e6c523cd | 502 | /* 3rd pass to set the separators */ |
1872c5c1 | 503 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
efa97b71 | 504 | if (f->pos_init) { /* Skip the first, left-most */ |
77c76070 | 505 | /* Copying only seplen ensures we omit the '\0' byte. */ |
efa97b71 | 506 | strncpy(buf + (f->pos_init - seplen), cfg->separator, seplen); |
4d66492f SK |
507 | } |
508 | } | |
509 | ||
fabb8771 SK |
510 | if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL))) |
511 | fatal("XOpenDisplay failed with: %p\n", display); | |
805f0d22 | 512 | /* TODO: Handle signals */ |
4d66492f | 513 | for (;;) { |
b7487ec5 SK |
514 | clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors |
515 | /* TODO: Cache expiration. i.e. use the TTL */ | |
6c29392d | 516 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ |
69b75a40 SK |
517 | /* TODO: Set timeout on fifo_read_all based on diff of last time of |
518 | * fifo_read_all and desired time of next TTL check. | |
b7487ec5 SK |
519 | */ |
520 | /* TODO: How long to wait on IO? Max TTL? */ | |
69b75a40 | 521 | fifo_read_all(cfg, buf); |
fabb8771 SK |
522 | if (cfg->output_to_x_root_window) { |
523 | if (XStoreName(display, DefaultRootWindow(display), buf) < 0) | |
524 | fatal("XStoreName failed.\n"); | |
525 | XFlush(display); | |
526 | } else { | |
527 | puts(buf); | |
528 | fflush(stdout); | |
529 | } | |
b7487ec5 SK |
530 | clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors |
531 | timespecsub(&t1, &t0, &td); | |
532 | debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td.tv_sec, td.tv_nsec); | |
533 | if (timespeccmp(&td, &ti, <)) { | |
534 | /* Pushback on data producers by refusing to read the | |
535 | * pipe more frequently than the interval. | |
536 | */ | |
537 | timespecsub(&ti, &td, &tc); | |
538 | debug("snooze YES\n"); | |
17a27e48 | 539 | snooze(&tc); |
574a4bff | 540 | } else { |
b7487ec5 | 541 | debug("snooze NO\n"); |
574a4bff | 542 | } |
4d66492f | 543 | } |
3d7e82a8 SK |
544 | |
545 | return EXIT_SUCCESS; | |
9b5ebc12 | 546 | } |