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> | |
c5d1af8c SK |
11 | #include <unistd.h> |
12 | ||
13 | #include <X11/Xlib.h> | |
9b5ebc12 | 14 | |
4d66492f SK |
15 | #define debug(args...) {fprintf(stderr, "[debug] " args);} |
16 | #define info( args...) {fprintf(stderr, "[info] " args);} | |
17 | #define error(args...) {fprintf(stderr, "[error] " args);} | |
18 | #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);} | |
19 | #define usage(args...) {print_usage(); fatal("[usage] " args);} | |
9b5ebc12 | 20 | |
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; |
4d66492f | 36 | int pos; /* Position on the output buffer. */ |
1872c5c1 | 37 | Fifo *next; |
9b5ebc12 SK |
38 | }; |
39 | ||
4d66492f SK |
40 | typedef struct Config Config; |
41 | struct Config { | |
9b5ebc12 | 42 | int interval; |
4d66492f | 43 | char * separator; |
1872c5c1 SK |
44 | Fifo * fifos; |
45 | int fifo_count; | |
4d66492f | 46 | int total_width; |
fabb8771 | 47 | int output_to_x_root_window; |
4d66492f SK |
48 | } defaults = { |
49 | .interval = 1, | |
50 | .separator = "|", | |
1872c5c1 SK |
51 | .fifos = NULL, |
52 | .fifo_count = 0, | |
4d66492f | 53 | .total_width = 0, |
fabb8771 | 54 | .output_to_x_root_window = 0, |
9b5ebc12 SK |
55 | }; |
56 | ||
77c76070 | 57 | void |
1872c5c1 | 58 | fifo_print_one(Fifo *f) |
77c76070 SK |
59 | { |
60 | debug( | |
1872c5c1 | 61 | "Fifo " |
77c76070 SK |
62 | "{" |
63 | " name = %s," | |
64 | " fd = %d," | |
65 | " width = %d," | |
66 | " last_read = %d," | |
67 | " ttl = %d," | |
68 | " pos = %d," | |
69 | " next = %p," | |
70 | " }\n", | |
71 | f->name, | |
72 | f->fd, | |
73 | f->width, | |
74 | f->last_read, | |
75 | f->ttl, | |
76 | f->pos, | |
77 | f->next | |
78 | ); | |
79 | } | |
80 | ||
81 | void | |
1872c5c1 | 82 | fifo_print_all(Fifo *head) |
77c76070 | 83 | { |
1872c5c1 SK |
84 | for (Fifo *f = head; f; f = f->next) { |
85 | fifo_print_one(f); | |
77c76070 SK |
86 | } |
87 | } | |
88 | ||
89 | void | |
90 | config_print(Config *c) | |
91 | { | |
92 | debug( | |
93 | "Config " | |
94 | "{" | |
95 | " interval = %d," | |
96 | " separator = %s," | |
1872c5c1 | 97 | " fifo_count = %d," |
77c76070 | 98 | " total_width = %d," |
1872c5c1 | 99 | " fifos = ..." |
77c76070 SK |
100 | " }\n", |
101 | c->interval, | |
102 | c->separator, | |
1872c5c1 | 103 | c->fifo_count, |
77c76070 SK |
104 | c->total_width |
105 | ); | |
1872c5c1 | 106 | fifo_print_all(c->fifos); |
77c76070 | 107 | } |
9b5ebc12 SK |
108 | |
109 | int | |
110 | is_pos_num(char *s) | |
111 | { | |
112 | while (*s != '\0') | |
113 | if (!isdigit(*(s++))) | |
114 | return 0; | |
115 | return 1; | |
116 | } | |
117 | ||
118 | void | |
119 | print_usage() | |
120 | { | |
4d66492f | 121 | assert(argv0); |
9b5ebc12 SK |
122 | fprintf( |
123 | stderr, | |
124 | "\n" | |
4d66492f | 125 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" |
9b5ebc12 | 126 | "\n" |
4d66492f SK |
127 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" |
128 | " FILE_PATH = string\n" | |
129 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
130 | " DATA_TTL = int (* (positive) number of seconds *)\n" | |
131 | " OPTION = -i INTERVAL\n" | |
132 | " | -s SEPARATOR\n" | |
133 | " SEPARATOR = string\n" | |
134 | " INTERVAL = int (* (positive) number of seconds *)\n" | |
9b5ebc12 | 135 | "\n", |
4d66492f | 136 | argv0 |
9b5ebc12 SK |
137 | ); |
138 | fprintf( | |
139 | stderr, | |
140 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
141 | "\n", | |
4d66492f | 142 | argv0 |
9b5ebc12 SK |
143 | ); |
144 | } | |
145 | ||
4d66492f | 146 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
9b5ebc12 SK |
147 | |
148 | void | |
4d66492f | 149 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
150 | { |
151 | if (i < argc) { | |
152 | char *param = argv[i++]; | |
153 | ||
154 | if (is_pos_num(param)) { | |
4d66492f SK |
155 | cfg->interval = atoi(param); |
156 | opts_parse_any(cfg, argc, argv, i); | |
9b5ebc12 SK |
157 | } else { |
158 | usage("Option -i parameter is invalid: \"%s\"\n", param); | |
159 | } | |
160 | } else { | |
161 | usage("Option -i parameter is missing.\n"); | |
162 | } | |
163 | } | |
164 | ||
165 | void | |
4d66492f SK |
166 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
167 | { | |
168 | if (i < argc) { | |
3c836bfd | 169 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
4d66492f SK |
170 | strcpy(cfg->separator, argv[i]); |
171 | opts_parse_any(cfg, argc, argv, ++i); | |
172 | } else { | |
173 | usage("Option -s parameter is missing.\n"); | |
174 | } | |
175 | } | |
176 | ||
4d66492f SK |
177 | void |
178 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
179 | { |
180 | switch (argv[i][1]) { | |
fb8745ba SK |
181 | case 'i': |
182 | /* TODO: Generic set_int */ | |
183 | parse_opts_opt_i(cfg, argc, argv, ++i); | |
184 | break; | |
185 | case 's': | |
186 | /* TODO: Generic set_str */ | |
187 | parse_opts_opt_s(cfg, argc, argv, ++i); | |
188 | break; | |
189 | case 'x': | |
fabb8771 SK |
190 | cfg->output_to_x_root_window = 1; |
191 | opts_parse_any(cfg, argc, argv, ++i); | |
192 | break; | |
fb8745ba SK |
193 | default : |
194 | usage("Option \"%s\" is invalid\n", argv[i]); | |
9b5ebc12 SK |
195 | } |
196 | } | |
197 | ||
198 | void | |
4d66492f | 199 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
200 | { |
201 | if ((i + 3) > argc) | |
1872c5c1 | 202 | usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv[i]); |
9b5ebc12 SK |
203 | |
204 | char *n = argv[i++]; | |
205 | char *w = argv[i++]; | |
206 | char *t = argv[i++]; | |
207 | ||
208 | if (!is_pos_num(w)) | |
1872c5c1 | 209 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
9b5ebc12 | 210 | if (!is_pos_num(t)) |
1872c5c1 SK |
211 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
212 | Fifo *f = calloc(1, sizeof(struct Fifo)); | |
9b5ebc12 | 213 | if (f) { |
4d66492f SK |
214 | f->name = n; |
215 | f->fd = -1; | |
216 | f->width = atoi(w); | |
217 | f->ttl = atoi(t); | |
218 | f->last_read = 0; | |
219 | f->pos = cfg->total_width; | |
1872c5c1 | 220 | f->next = cfg->fifos; |
4d66492f | 221 | |
1872c5c1 | 222 | cfg->fifos = f; |
4d66492f | 223 | cfg->total_width += f->width; |
1872c5c1 | 224 | cfg->fifo_count++; |
9b5ebc12 | 225 | } else { |
4d66492f | 226 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 227 | } |
4d66492f | 228 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
229 | } |
230 | ||
231 | void | |
4d66492f | 232 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
233 | { |
234 | if (i < argc) { | |
235 | switch (argv[i][0]) { | |
fb8745ba SK |
236 | case '-': |
237 | parse_opts_opt(cfg, argc, argv, i); | |
238 | break; | |
239 | default : | |
240 | parse_opts_spec(cfg, argc, argv, i); | |
4d66492f SK |
241 | } |
242 | } | |
243 | } | |
244 | ||
245 | void | |
5400b86f | 246 | opts_parse(Config *cfg, int argc, char *argv[]) |
4d66492f SK |
247 | { |
248 | opts_parse_any(cfg, argc, argv, 1); | |
249 | ||
1872c5c1 SK |
250 | Fifo *last = cfg->fifos; |
251 | cfg->fifos = NULL; | |
252 | for (Fifo *f = last; f; ) { | |
253 | Fifo *next = f->next; | |
254 | f->next = cfg->fifos; | |
255 | cfg->fifos = f; | |
4d66492f SK |
256 | f = next; |
257 | } | |
258 | } | |
259 | ||
e6c523cd | 260 | void |
69b75a40 | 261 | fifo_read_error(Fifo *f, char *buf) |
e6c523cd SK |
262 | { |
263 | char *b; | |
264 | int i; | |
265 | ||
266 | b = buf + f->pos; | |
267 | /* Copy as much of the error message as possible. | |
268 | * EXCLUDING the reminating \0. */ | |
269 | for (i = 0; i < errlen && i < f->width; i++) | |
270 | b[i] = errmsg[i]; | |
271 | /* Any remaining slots: */ | |
272 | for (; i < f->width; i++) | |
273 | b[i] = '_'; | |
274 | } | |
275 | ||
77c76070 | 276 | void |
69b75a40 | 277 | fifo_read_one(Fifo *f, char *buf) |
77c76070 | 278 | { |
3efcce25 SK |
279 | ssize_t current; |
280 | ssize_t total; | |
77c76070 | 281 | char *b; |
3efcce25 | 282 | char c; |
77c76070 | 283 | |
3efcce25 SK |
284 | current = 0; |
285 | total = 0; | |
286 | c = '\0'; | |
77c76070 | 287 | b = buf + f->pos; |
3efcce25 SK |
288 | while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width) |
289 | *b++ = c; | |
e6c523cd | 290 | if (current == -1) { |
3efcce25 | 291 | error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); |
69b75a40 | 292 | fifo_read_error(f, buf); |
7fb02106 SK |
293 | } else |
294 | while (total++ < f->width) | |
295 | *b++ = ' '; | |
6c29392d | 296 | /* TODO Record timestamp read */ |
77c76070 SK |
297 | close(f->fd); |
298 | f->fd = -1; | |
299 | } | |
300 | ||
4d66492f | 301 | void |
69b75a40 | 302 | fifo_read_all(Config *cfg, char *buf) |
4d66492f | 303 | { |
77c76070 | 304 | fd_set fds; |
bfab34f8 | 305 | int maxfd = -1; |
77c76070 | 306 | int ready; |
bec93767 | 307 | struct stat st; |
77c76070 SK |
308 | |
309 | FD_ZERO(&fds); | |
bec93767 | 310 | /* TODO: Check TTL */ |
1872c5c1 | 311 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
bec93767 | 312 | /* TODO: Create the FIFO if it doesn't already exist. */ |
e6c523cd SK |
313 | if (lstat(f->name, &st) < 0) { |
314 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
69b75a40 | 315 | fifo_read_error(f, buf); |
e6c523cd SK |
316 | continue; |
317 | } | |
318 | if (!(st.st_mode & S_IFIFO)) { | |
319 | error("\"%s\" is not a FIFO\n", f->name); | |
69b75a40 | 320 | fifo_read_error(f, buf); |
e6c523cd SK |
321 | continue; |
322 | } | |
77c76070 | 323 | debug("opening: %s\n", f->name); |
4d66492f | 324 | if (f->fd < 0) |
77c76070 | 325 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
e6c523cd | 326 | if (f->fd == -1) { |
1872c5c1 | 327 | /* TODO: Consider backing off retries for failed fifos. */ |
e6c523cd | 328 | error("Failed to open \"%s\"\n", f->name); |
69b75a40 | 329 | fifo_read_error(f, buf); |
e6c523cd SK |
330 | continue; |
331 | } | |
77c76070 SK |
332 | if (f->fd > maxfd) |
333 | maxfd = f->fd; | |
334 | FD_SET(f->fd, &fds); | |
335 | } | |
336 | debug("selecting...\n"); | |
337 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); | |
338 | debug("ready: %d\n", ready); | |
339 | assert(ready != 0); | |
340 | if (ready < 0) | |
341 | fatal("%s", strerror(errno)); | |
1872c5c1 | 342 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
77c76070 SK |
343 | if (FD_ISSET(f->fd, &fds)) { |
344 | debug("reading: %s\n", f->name); | |
69b75a40 | 345 | fifo_read_one(f, buf); |
9b5ebc12 SK |
346 | } |
347 | } | |
348 | } | |
349 | ||
350 | int | |
4d66492f | 351 | main(int argc, char *argv[]) |
9b5ebc12 | 352 | { |
f277f405 | 353 | int width = 0; |
1872c5c1 | 354 | int nfifos = 0; |
f277f405 | 355 | int seplen = 0; |
4d66492f | 356 | int prefix = 0; |
e6c523cd | 357 | int errors = 0; |
4d66492f | 358 | char *buf; |
a6d4c9c3 SK |
359 | Config cfg0 = defaults; |
360 | Config *cfg = &cfg0; | |
bfab34f8 | 361 | Display *display = NULL; |
e6c523cd | 362 | struct stat st; |
4d66492f SK |
363 | |
364 | argv0 = argv[0]; | |
365 | ||
5400b86f | 366 | opts_parse(cfg, argc, argv); |
4d66492f | 367 | debug("argv0 = %s\n", argv0); |
77c76070 | 368 | config_print(cfg); |
1872c5c1 SK |
369 | if (cfg->fifos == NULL) |
370 | usage("No fifo specs were given!\n"); | |
4d66492f | 371 | |
e6c523cd SK |
372 | /* 1st pass to check file existence and type */ |
373 | for (Fifo *f = cfg->fifos; f; f = f->next) { | |
374 | if (lstat(f->name, &st) < 0) { | |
375 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
376 | errors++; | |
377 | continue; | |
378 | } | |
379 | if (!(st.st_mode & S_IFIFO)) { | |
380 | error("\"%s\" is not a FIFO\n", f->name); | |
381 | errors++; | |
382 | continue; | |
383 | } | |
384 | } | |
385 | if (errors) | |
386 | fatal("Encountered errors with the given file paths. See log.\n"); | |
387 | ||
4d66492f SK |
388 | width = cfg->total_width; |
389 | seplen = strlen(cfg->separator); | |
390 | ||
e6c523cd | 391 | /* 2nd pass to make space for separators */ |
1872c5c1 | 392 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
4d66492f SK |
393 | f->pos += prefix; |
394 | prefix += seplen; | |
1872c5c1 | 395 | nfifos++; |
4d66492f | 396 | } |
1872c5c1 | 397 | width += (seplen * (nfifos - 1)); |
3c836bfd | 398 | buf = calloc(1, width + 1); |
4d66492f SK |
399 | if (buf == NULL) |
400 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
401 | memset(buf, ' ', width); | |
402 | buf[width] = '\0'; | |
e6c523cd | 403 | /* 3rd pass to set the separators */ |
1872c5c1 | 404 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
4d66492f | 405 | if (f->pos) { /* Skip the first, left-most */ |
77c76070 SK |
406 | /* Copying only seplen ensures we omit the '\0' byte. */ |
407 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); | |
4d66492f SK |
408 | } |
409 | } | |
410 | ||
fabb8771 SK |
411 | if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL))) |
412 | fatal("XOpenDisplay failed with: %p\n", display); | |
4d66492f | 413 | /* TODO: nanosleep and nano time diff */ |
805f0d22 | 414 | /* TODO: Handle signals */ |
4d66492f | 415 | for (;;) { |
6c29392d SK |
416 | /* TODO: Check TTL and maybe blank-out */ |
417 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ | |
69b75a40 SK |
418 | /* TODO: Set timeout on fifo_read_all based on diff of last time of |
419 | * fifo_read_all and desired time of next TTL check. | |
fabb8771 | 420 | * */ |
69b75a40 | 421 | fifo_read_all(cfg, buf); |
fabb8771 SK |
422 | if (cfg->output_to_x_root_window) { |
423 | if (XStoreName(display, DefaultRootWindow(display), buf) < 0) | |
424 | fatal("XStoreName failed.\n"); | |
425 | XFlush(display); | |
426 | } else { | |
427 | puts(buf); | |
428 | fflush(stdout); | |
429 | } | |
4d66492f | 430 | } |
3d7e82a8 SK |
431 | |
432 | return EXIT_SUCCESS; | |
9b5ebc12 | 433 | } |