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 SK |
16 | #include "bsdtimespec.h" |
17 | ||
b6316e94 SK |
18 | #define debug(args...) if (cfg->log_level >= Debug) {fprintf(stderr, "[debug] " args); fflush(stderr);} |
19 | #define info( args...) if (cfg->log_level >= Info ) {fprintf(stderr, "[info] " args); fflush(stderr);} | |
20 | #define error(args...) if (cfg->log_level >= Error) {fprintf(stderr, "[error] " args); fflush(stderr);} | |
4d66492f SK |
21 | #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);} |
22 | #define usage(args...) {print_usage(); fatal("[usage] " args);} | |
9b5ebc12 | 23 | |
e6c523cd SK |
24 | #define ERRMSG "ERROR" |
25 | ||
26 | static const char errmsg[] = ERRMSG; | |
27 | static const int errlen = sizeof(ERRMSG) - 1; | |
28 | ||
4d66492f | 29 | char *argv0; |
9b5ebc12 | 30 | |
b6316e94 SK |
31 | typedef enum LogLevel { |
32 | Nothing, | |
33 | Error, | |
34 | Info, | |
35 | Debug | |
36 | } LogLevel; | |
37 | ||
1872c5c1 SK |
38 | /* TODO: Convert fifo list to fifo array. */ |
39 | typedef struct Fifo Fifo; | |
40 | struct Fifo { | |
4d66492f SK |
41 | char *name; |
42 | int fd; | |
9b5ebc12 | 43 | int width; |
4d66492f | 44 | int last_read; |
9b5ebc12 | 45 | int ttl; |
4d66492f | 46 | int pos; /* Position on the output buffer. */ |
1872c5c1 | 47 | Fifo *next; |
9b5ebc12 SK |
48 | }; |
49 | ||
4d66492f SK |
50 | typedef struct Config Config; |
51 | struct Config { | |
9b5ebc12 | 52 | int interval; |
4d66492f | 53 | char * separator; |
1872c5c1 SK |
54 | Fifo * fifos; |
55 | int fifo_count; | |
4d66492f | 56 | int total_width; |
fabb8771 | 57 | int output_to_x_root_window; |
b6316e94 | 58 | LogLevel log_level; |
4d66492f SK |
59 | } defaults = { |
60 | .interval = 1, | |
61 | .separator = "|", | |
1872c5c1 SK |
62 | .fifos = NULL, |
63 | .fifo_count = 0, | |
4d66492f | 64 | .total_width = 0, |
fabb8771 | 65 | .output_to_x_root_window = 0, |
b6316e94 | 66 | .log_level = Info, |
9b5ebc12 SK |
67 | }; |
68 | ||
77c76070 | 69 | void |
b6316e94 | 70 | fifo_print_one(Fifo *f, Config *cfg) |
77c76070 | 71 | { |
a6b13fa2 SK |
72 | info("Fifo " |
73 | "{" | |
74 | " name = %s," | |
75 | " fd = %d," | |
76 | " width = %d," | |
77 | " last_read = %d," | |
78 | " ttl = %d," | |
79 | " pos = %d," | |
80 | " next = %p," | |
81 | " }\n", | |
82 | f->name, | |
83 | f->fd, | |
84 | f->width, | |
85 | f->last_read, | |
86 | f->ttl, | |
87 | f->pos, | |
88 | f->next | |
77c76070 SK |
89 | ); |
90 | } | |
91 | ||
92 | void | |
b6316e94 | 93 | fifo_print_all(Fifo *head, Config *cfg) |
77c76070 | 94 | { |
1872c5c1 | 95 | for (Fifo *f = head; f; f = f->next) { |
b6316e94 | 96 | fifo_print_one(f, cfg); |
77c76070 SK |
97 | } |
98 | } | |
99 | ||
100 | void | |
b6316e94 | 101 | config_print(Config *cfg) |
77c76070 | 102 | { |
b6316e94 | 103 | info( |
a6b13fa2 SK |
104 | "Config " |
105 | "{" | |
106 | " interval = %d," | |
107 | " separator = %s," | |
108 | " fifo_count = %d," | |
109 | " total_width = %d," | |
110 | " log_level = %d," | |
111 | " fifos = ..." | |
112 | " }\n", | |
113 | cfg->interval, | |
114 | cfg->separator, | |
115 | cfg->fifo_count, | |
116 | cfg->total_width, | |
117 | cfg->log_level | |
77c76070 | 118 | ); |
b6316e94 | 119 | fifo_print_all(cfg->fifos, cfg); |
77c76070 | 120 | } |
9b5ebc12 SK |
121 | |
122 | int | |
123 | is_pos_num(char *s) | |
124 | { | |
125 | while (*s != '\0') | |
126 | if (!isdigit(*(s++))) | |
127 | return 0; | |
128 | return 1; | |
129 | } | |
130 | ||
131 | void | |
132 | print_usage() | |
133 | { | |
4d66492f | 134 | assert(argv0); |
9b5ebc12 | 135 | fprintf( |
a6b13fa2 SK |
136 | stderr, |
137 | "\n" | |
138 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" | |
139 | "\n" | |
140 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" | |
141 | " FILE_PATH = string\n" | |
142 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
143 | " DATA_TTL = int (* (positive) number of seconds *)\n" | |
144 | " OPTION = -i INTERVAL\n" | |
145 | " | -s SEPARATOR\n" | |
146 | " | -x (* Output to X root window *)\n" | |
147 | " | -l LOG_LEVEL\n" | |
148 | " SEPARATOR = string\n" | |
149 | " INTERVAL = int (* (positive) number of seconds *)\n" | |
150 | " LOG_LEVEL = int (* %d through %d *)\n" | |
151 | "\n", | |
152 | argv0, | |
153 | Nothing, | |
154 | Debug | |
9b5ebc12 SK |
155 | ); |
156 | fprintf( | |
a6b13fa2 SK |
157 | stderr, |
158 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
159 | "\n", | |
160 | argv0 | |
9b5ebc12 SK |
161 | ); |
162 | } | |
163 | ||
4d66492f | 164 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
9b5ebc12 SK |
165 | |
166 | void | |
4d66492f | 167 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 | 168 | { |
4c438cef SK |
169 | char *param; |
170 | ||
171 | if (i >= argc) | |
9b5ebc12 | 172 | usage("Option -i parameter is missing.\n"); |
4c438cef SK |
173 | param = argv[i++]; |
174 | if (!is_pos_num(param)) | |
175 | usage("Option -i parameter is invalid: \"%s\"\n", param); | |
176 | cfg->interval = atoi(param); | |
177 | opts_parse_any(cfg, argc, argv, i); | |
9b5ebc12 SK |
178 | } |
179 | ||
180 | void | |
4d66492f SK |
181 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
182 | { | |
4c438cef | 183 | if (i >= argc) |
4d66492f | 184 | usage("Option -s parameter is missing.\n"); |
4c438cef SK |
185 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
186 | strcpy(cfg->separator, argv[i]); | |
187 | opts_parse_any(cfg, argc, argv, ++i); | |
4d66492f SK |
188 | } |
189 | ||
b6316e94 SK |
190 | void |
191 | parse_opts_opt_l(Config *cfg, int argc, char *argv[], int i) | |
192 | { | |
4c438cef | 193 | char *param; |
b6316e94 SK |
194 | int log_level; |
195 | ||
4c438cef | 196 | if (i >= argc) |
b6316e94 | 197 | usage("Option -l parameter is missing.\n"); |
4c438cef SK |
198 | param = argv[i++]; |
199 | if (!is_pos_num(param)) | |
200 | usage("Option -l parameter is invalid: \"%s\"\n", param); | |
201 | log_level = atoi(param); | |
202 | if (log_level > Debug) | |
203 | usage("Option -l value (%d) exceeds maximum (%d)\n", log_level, Debug); | |
204 | cfg->log_level = log_level; | |
205 | opts_parse_any(cfg, argc, argv, i); | |
b6316e94 SK |
206 | } |
207 | ||
4d66492f SK |
208 | void |
209 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
210 | { |
211 | switch (argv[i][1]) { | |
ce552549 SK |
212 | case 'i': |
213 | /* TODO: Generic set_int */ | |
214 | parse_opts_opt_i(cfg, argc, argv, ++i); | |
215 | break; | |
216 | case 's': | |
217 | /* TODO: Generic set_str */ | |
218 | parse_opts_opt_s(cfg, argc, argv, ++i); | |
219 | break; | |
220 | case 'x': | |
221 | cfg->output_to_x_root_window = 1; | |
222 | opts_parse_any(cfg, argc, argv, ++i); | |
223 | break; | |
224 | case 'l': | |
225 | /* TODO: Generic set_int */ | |
226 | parse_opts_opt_l(cfg, argc, argv, ++i); | |
227 | break; | |
228 | default : | |
229 | usage("Option \"%s\" is invalid\n", argv[i]); | |
9b5ebc12 SK |
230 | } |
231 | } | |
232 | ||
233 | void | |
4d66492f | 234 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
235 | { |
236 | if ((i + 3) > argc) | |
1872c5c1 | 237 | usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv[i]); |
9b5ebc12 SK |
238 | |
239 | char *n = argv[i++]; | |
240 | char *w = argv[i++]; | |
241 | char *t = argv[i++]; | |
242 | ||
243 | if (!is_pos_num(w)) | |
1872c5c1 | 244 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
9b5ebc12 | 245 | if (!is_pos_num(t)) |
1872c5c1 SK |
246 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
247 | Fifo *f = calloc(1, sizeof(struct Fifo)); | |
9b5ebc12 | 248 | if (f) { |
4d66492f SK |
249 | f->name = n; |
250 | f->fd = -1; | |
251 | f->width = atoi(w); | |
252 | f->ttl = atoi(t); | |
253 | f->last_read = 0; | |
254 | f->pos = cfg->total_width; | |
1872c5c1 | 255 | f->next = cfg->fifos; |
4d66492f | 256 | |
1872c5c1 | 257 | cfg->fifos = f; |
4d66492f | 258 | cfg->total_width += f->width; |
1872c5c1 | 259 | cfg->fifo_count++; |
9b5ebc12 | 260 | } else { |
4d66492f | 261 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 262 | } |
4d66492f | 263 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
264 | } |
265 | ||
266 | void | |
4d66492f | 267 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
268 | { |
269 | if (i < argc) { | |
270 | switch (argv[i][0]) { | |
ce552549 SK |
271 | case '-': |
272 | parse_opts_opt(cfg, argc, argv, i); | |
273 | break; | |
274 | default : | |
275 | parse_opts_spec(cfg, argc, argv, i); | |
4d66492f SK |
276 | } |
277 | } | |
278 | } | |
279 | ||
280 | void | |
5400b86f | 281 | opts_parse(Config *cfg, int argc, char *argv[]) |
4d66492f SK |
282 | { |
283 | opts_parse_any(cfg, argc, argv, 1); | |
284 | ||
1872c5c1 SK |
285 | Fifo *last = cfg->fifos; |
286 | cfg->fifos = NULL; | |
287 | for (Fifo *f = last; f; ) { | |
288 | Fifo *next = f->next; | |
289 | f->next = cfg->fifos; | |
290 | cfg->fifos = f; | |
4d66492f SK |
291 | f = next; |
292 | } | |
293 | } | |
294 | ||
e6c523cd | 295 | void |
69b75a40 | 296 | fifo_read_error(Fifo *f, char *buf) |
e6c523cd SK |
297 | { |
298 | char *b; | |
299 | int i; | |
300 | ||
301 | b = buf + f->pos; | |
302 | /* Copy as much of the error message as possible. | |
303 | * EXCLUDING the reminating \0. */ | |
304 | for (i = 0; i < errlen && i < f->width; i++) | |
305 | b[i] = errmsg[i]; | |
306 | /* Any remaining slots: */ | |
307 | for (; i < f->width; i++) | |
308 | b[i] = '_'; | |
309 | } | |
310 | ||
77c76070 | 311 | void |
b6316e94 | 312 | fifo_read_one(Fifo *f, char *buf, Config *cfg) |
77c76070 | 313 | { |
3efcce25 SK |
314 | ssize_t current; |
315 | ssize_t total; | |
77c76070 | 316 | char *b; |
3efcce25 | 317 | char c; |
77c76070 | 318 | |
3efcce25 SK |
319 | current = 0; |
320 | total = 0; | |
321 | c = '\0'; | |
77c76070 | 322 | b = buf + f->pos; |
3efcce25 SK |
323 | while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width) |
324 | *b++ = c; | |
e6c523cd | 325 | if (current == -1) { |
3efcce25 | 326 | error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); |
69b75a40 | 327 | fifo_read_error(f, buf); |
7fb02106 SK |
328 | } else |
329 | while (total++ < f->width) | |
330 | *b++ = ' '; | |
6c29392d | 331 | /* TODO Record timestamp read */ |
77c76070 SK |
332 | close(f->fd); |
333 | f->fd = -1; | |
334 | } | |
335 | ||
4d66492f | 336 | void |
69b75a40 | 337 | fifo_read_all(Config *cfg, char *buf) |
4d66492f | 338 | { |
77c76070 | 339 | fd_set fds; |
bfab34f8 | 340 | int maxfd = -1; |
77c76070 | 341 | int ready; |
bec93767 | 342 | struct stat st; |
77c76070 SK |
343 | |
344 | FD_ZERO(&fds); | |
1872c5c1 | 345 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
bec93767 | 346 | /* TODO: Create the FIFO if it doesn't already exist. */ |
e6c523cd SK |
347 | if (lstat(f->name, &st) < 0) { |
348 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
69b75a40 | 349 | fifo_read_error(f, buf); |
e6c523cd SK |
350 | continue; |
351 | } | |
352 | if (!(st.st_mode & S_IFIFO)) { | |
353 | error("\"%s\" is not a FIFO\n", f->name); | |
69b75a40 | 354 | fifo_read_error(f, buf); |
e6c523cd SK |
355 | continue; |
356 | } | |
77c76070 | 357 | debug("opening: %s\n", f->name); |
4d66492f | 358 | if (f->fd < 0) |
77c76070 | 359 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
e6c523cd | 360 | if (f->fd == -1) { |
1872c5c1 | 361 | /* TODO: Consider backing off retries for failed fifos. */ |
e6c523cd | 362 | error("Failed to open \"%s\"\n", f->name); |
69b75a40 | 363 | fifo_read_error(f, buf); |
e6c523cd SK |
364 | continue; |
365 | } | |
77c76070 SK |
366 | if (f->fd > maxfd) |
367 | maxfd = f->fd; | |
368 | FD_SET(f->fd, &fds); | |
369 | } | |
370 | debug("selecting...\n"); | |
371 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); | |
372 | debug("ready: %d\n", ready); | |
373 | assert(ready != 0); | |
374 | if (ready < 0) | |
375 | fatal("%s", strerror(errno)); | |
1872c5c1 | 376 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
77c76070 SK |
377 | if (FD_ISSET(f->fd, &fds)) { |
378 | debug("reading: %s\n", f->name); | |
b6316e94 | 379 | fifo_read_one(f, buf, cfg); |
9b5ebc12 SK |
380 | } |
381 | } | |
382 | } | |
383 | ||
b7487ec5 | 384 | void |
b6316e94 | 385 | snooze(struct timespec *t, Config *cfg) |
b7487ec5 SK |
386 | { |
387 | struct timespec remainder; | |
388 | int result; | |
389 | ||
390 | result = nanosleep(t, &remainder); | |
391 | ||
392 | if (result < 0) { | |
393 | if (errno == EINTR) { | |
394 | info( | |
a6b13fa2 SK |
395 | "nanosleep interrupted. Remainder: " |
396 | "{ tv_sec = %ld, tv_nsec = %ld }", | |
397 | remainder.tv_sec, remainder.tv_nsec); | |
b7487ec5 SK |
398 | /* No big deal if we occasionally sleep less, |
399 | * so not attempting to correct after an interruption. | |
400 | */ | |
401 | } else { | |
402 | fatal("nanosleep: %s\n", strerror(errno)); | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
9b5ebc12 | 407 | int |
4d66492f | 408 | main(int argc, char *argv[]) |
9b5ebc12 | 409 | { |
f277f405 | 410 | int width = 0; |
1872c5c1 | 411 | int nfifos = 0; |
f277f405 | 412 | int seplen = 0; |
4d66492f | 413 | int prefix = 0; |
e6c523cd | 414 | int errors = 0; |
4d66492f | 415 | char *buf; |
a6d4c9c3 SK |
416 | Config cfg0 = defaults; |
417 | Config *cfg = &cfg0; | |
bfab34f8 | 418 | Display *display = NULL; |
e6c523cd | 419 | struct stat st; |
b7487ec5 SK |
420 | struct timespec |
421 | t0, /* time stamp. before reading fifos */ | |
422 | t1, /* time stamp. after reading fifos */ | |
423 | ti, /* time interval desired (t1 - t0) */ | |
424 | td, /* time interval measured (t1 - t0) */ | |
425 | tc; /* time interval correction (ti - td) when td < ti */ | |
4d66492f SK |
426 | |
427 | argv0 = argv[0]; | |
428 | ||
5400b86f | 429 | opts_parse(cfg, argc, argv); |
4d66492f | 430 | debug("argv0 = %s\n", argv0); |
77c76070 | 431 | config_print(cfg); |
b7487ec5 SK |
432 | |
433 | /* TODO: Support interval < 1. i.e. implement timespec_of_float */ | |
434 | ti.tv_sec = cfg->interval; | |
435 | ti.tv_nsec = 0; | |
436 | ||
1872c5c1 SK |
437 | if (cfg->fifos == NULL) |
438 | usage("No fifo specs were given!\n"); | |
4d66492f | 439 | |
e6c523cd SK |
440 | /* 1st pass to check file existence and type */ |
441 | for (Fifo *f = cfg->fifos; f; f = f->next) { | |
442 | if (lstat(f->name, &st) < 0) { | |
443 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
444 | errors++; | |
445 | continue; | |
446 | } | |
447 | if (!(st.st_mode & S_IFIFO)) { | |
448 | error("\"%s\" is not a FIFO\n", f->name); | |
449 | errors++; | |
450 | continue; | |
451 | } | |
452 | } | |
453 | if (errors) | |
454 | fatal("Encountered errors with the given file paths. See log.\n"); | |
455 | ||
4d66492f SK |
456 | width = cfg->total_width; |
457 | seplen = strlen(cfg->separator); | |
458 | ||
e6c523cd | 459 | /* 2nd pass to make space for separators */ |
1872c5c1 | 460 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
4d66492f SK |
461 | f->pos += prefix; |
462 | prefix += seplen; | |
1872c5c1 | 463 | nfifos++; |
4d66492f | 464 | } |
1872c5c1 | 465 | width += (seplen * (nfifos - 1)); |
3c836bfd | 466 | buf = calloc(1, width + 1); |
4d66492f SK |
467 | if (buf == NULL) |
468 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
469 | memset(buf, ' ', width); | |
470 | buf[width] = '\0'; | |
e6c523cd | 471 | /* 3rd pass to set the separators */ |
1872c5c1 | 472 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
4d66492f | 473 | if (f->pos) { /* Skip the first, left-most */ |
77c76070 SK |
474 | /* Copying only seplen ensures we omit the '\0' byte. */ |
475 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); | |
4d66492f SK |
476 | } |
477 | } | |
478 | ||
fabb8771 SK |
479 | if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL))) |
480 | fatal("XOpenDisplay failed with: %p\n", display); | |
805f0d22 | 481 | /* TODO: Handle signals */ |
4d66492f | 482 | for (;;) { |
b7487ec5 SK |
483 | clock_gettime(CLOCK_MONOTONIC, &t0); // FIXME: check errors |
484 | /* TODO: Cache expiration. i.e. use the TTL */ | |
6c29392d | 485 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ |
69b75a40 SK |
486 | /* TODO: Set timeout on fifo_read_all based on diff of last time of |
487 | * fifo_read_all and desired time of next TTL check. | |
b7487ec5 SK |
488 | */ |
489 | /* TODO: How long to wait on IO? Max TTL? */ | |
69b75a40 | 490 | fifo_read_all(cfg, buf); |
fabb8771 SK |
491 | if (cfg->output_to_x_root_window) { |
492 | if (XStoreName(display, DefaultRootWindow(display), buf) < 0) | |
493 | fatal("XStoreName failed.\n"); | |
494 | XFlush(display); | |
495 | } else { | |
496 | puts(buf); | |
497 | fflush(stdout); | |
498 | } | |
b7487ec5 SK |
499 | clock_gettime(CLOCK_MONOTONIC, &t1); // FIXME: check errors |
500 | timespecsub(&t1, &t0, &td); | |
501 | debug("td {tv_sec = %ld, tv_nsec = %ld}\n", td.tv_sec, td.tv_nsec); | |
502 | if (timespeccmp(&td, &ti, <)) { | |
503 | /* Pushback on data producers by refusing to read the | |
504 | * pipe more frequently than the interval. | |
505 | */ | |
506 | timespecsub(&ti, &td, &tc); | |
507 | debug("snooze YES\n"); | |
b6316e94 | 508 | snooze(&tc, cfg); |
b7487ec5 SK |
509 | } else |
510 | debug("snooze NO\n"); | |
4d66492f | 511 | } |
3d7e82a8 SK |
512 | |
513 | return EXIT_SUCCESS; | |
9b5ebc12 | 514 | } |