| 1 | #include <sys/select.h> |
| 2 | #include <sys/stat.h> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <ctype.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include <X11/Xlib.h> |
| 14 | |
| 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);} |
| 20 | |
| 21 | #define ERRMSG "ERROR" |
| 22 | |
| 23 | static const char errmsg[] = ERRMSG; |
| 24 | static const int errlen = sizeof(ERRMSG) - 1; |
| 25 | |
| 26 | char *argv0; |
| 27 | |
| 28 | /* TODO: Convert fifo list to fifo array. */ |
| 29 | typedef struct Fifo Fifo; |
| 30 | struct Fifo { |
| 31 | char *name; |
| 32 | int fd; |
| 33 | int width; |
| 34 | int last_read; |
| 35 | int ttl; |
| 36 | int pos; /* Position on the output buffer. */ |
| 37 | Fifo *next; |
| 38 | }; |
| 39 | |
| 40 | typedef struct Config Config; |
| 41 | struct Config { |
| 42 | int interval; |
| 43 | char * separator; |
| 44 | Fifo * fifos; |
| 45 | int fifo_count; |
| 46 | int total_width; |
| 47 | int output_to_x_root_window; |
| 48 | } defaults = { |
| 49 | .interval = 1, |
| 50 | .separator = "|", |
| 51 | .fifos = NULL, |
| 52 | .fifo_count = 0, |
| 53 | .total_width = 0, |
| 54 | .output_to_x_root_window = 0, |
| 55 | }; |
| 56 | |
| 57 | void |
| 58 | fifo_print_one(Fifo *f) |
| 59 | { |
| 60 | debug( |
| 61 | "Fifo " |
| 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 |
| 82 | fifo_print_all(Fifo *head) |
| 83 | { |
| 84 | for (Fifo *f = head; f; f = f->next) { |
| 85 | fifo_print_one(f); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | config_print(Config *c) |
| 91 | { |
| 92 | debug( |
| 93 | "Config " |
| 94 | "{" |
| 95 | " interval = %d," |
| 96 | " separator = %s," |
| 97 | " fifo_count = %d," |
| 98 | " total_width = %d," |
| 99 | " fifos = ..." |
| 100 | " }\n", |
| 101 | c->interval, |
| 102 | c->separator, |
| 103 | c->fifo_count, |
| 104 | c->total_width |
| 105 | ); |
| 106 | fifo_print_all(c->fifos); |
| 107 | } |
| 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 | { |
| 121 | assert(argv0); |
| 122 | fprintf( |
| 123 | stderr, |
| 124 | "\n" |
| 125 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" |
| 126 | "\n" |
| 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" |
| 135 | "\n", |
| 136 | argv0 |
| 137 | ); |
| 138 | fprintf( |
| 139 | stderr, |
| 140 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" |
| 141 | "\n", |
| 142 | argv0 |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
| 147 | |
| 148 | void |
| 149 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
| 150 | { |
| 151 | if (i < argc) { |
| 152 | char *param = argv[i++]; |
| 153 | |
| 154 | if (is_pos_num(param)) { |
| 155 | cfg->interval = atoi(param); |
| 156 | opts_parse_any(cfg, argc, argv, i); |
| 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 |
| 166 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
| 167 | { |
| 168 | if (i < argc) { |
| 169 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
| 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 | |
| 177 | void |
| 178 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) |
| 179 | { |
| 180 | switch (argv[i][1]) { |
| 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': |
| 190 | cfg->output_to_x_root_window = 1; |
| 191 | opts_parse_any(cfg, argc, argv, ++i); |
| 192 | break; |
| 193 | default : |
| 194 | usage("Option \"%s\" is invalid\n", argv[i]); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
| 200 | { |
| 201 | if ((i + 3) > argc) |
| 202 | usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv[i]); |
| 203 | |
| 204 | char *n = argv[i++]; |
| 205 | char *w = argv[i++]; |
| 206 | char *t = argv[i++]; |
| 207 | |
| 208 | if (!is_pos_num(w)) |
| 209 | usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w, n); |
| 210 | if (!is_pos_num(t)) |
| 211 | usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t, n); |
| 212 | Fifo *f = calloc(1, sizeof(struct Fifo)); |
| 213 | if (f) { |
| 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; |
| 220 | f->next = cfg->fifos; |
| 221 | |
| 222 | cfg->fifos = f; |
| 223 | cfg->total_width += f->width; |
| 224 | cfg->fifo_count++; |
| 225 | } else { |
| 226 | fatal("[memory] Allocation failure."); |
| 227 | } |
| 228 | opts_parse_any(cfg, argc, argv, i); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
| 233 | { |
| 234 | if (i < argc) { |
| 235 | switch (argv[i][0]) { |
| 236 | case '-': |
| 237 | parse_opts_opt(cfg, argc, argv, i); |
| 238 | break; |
| 239 | default : |
| 240 | parse_opts_spec(cfg, argc, argv, i); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | opts_parse(Config *cfg, int argc, char *argv[]) |
| 247 | { |
| 248 | opts_parse_any(cfg, argc, argv, 1); |
| 249 | |
| 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; |
| 256 | f = next; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | fifo_read_error(Fifo *f, char *buf) |
| 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 | |
| 276 | void |
| 277 | fifo_read_one(Fifo *f, char *buf) |
| 278 | { |
| 279 | ssize_t current; |
| 280 | ssize_t total; |
| 281 | char *b; |
| 282 | char c; |
| 283 | |
| 284 | current = 0; |
| 285 | total = 0; |
| 286 | c = '\0'; |
| 287 | b = buf + f->pos; |
| 288 | while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width) |
| 289 | *b++ = c; |
| 290 | if (current == -1) { |
| 291 | error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); |
| 292 | fifo_read_error(f, buf); |
| 293 | } else |
| 294 | while (total++ < f->width) |
| 295 | *b++ = ' '; |
| 296 | /* TODO Record timestamp read */ |
| 297 | close(f->fd); |
| 298 | f->fd = -1; |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | fifo_read_all(Config *cfg, char *buf) |
| 303 | { |
| 304 | fd_set fds; |
| 305 | int maxfd = -1; |
| 306 | int ready; |
| 307 | struct stat st; |
| 308 | |
| 309 | FD_ZERO(&fds); |
| 310 | /* TODO: Check TTL */ |
| 311 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
| 312 | /* TODO: Create the FIFO if it doesn't already exist. */ |
| 313 | if (lstat(f->name, &st) < 0) { |
| 314 | error("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); |
| 315 | fifo_read_error(f, buf); |
| 316 | continue; |
| 317 | } |
| 318 | if (!(st.st_mode & S_IFIFO)) { |
| 319 | error("\"%s\" is not a FIFO\n", f->name); |
| 320 | fifo_read_error(f, buf); |
| 321 | continue; |
| 322 | } |
| 323 | debug("opening: %s\n", f->name); |
| 324 | if (f->fd < 0) |
| 325 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
| 326 | if (f->fd == -1) { |
| 327 | /* TODO: Consider backing off retries for failed fifos. */ |
| 328 | error("Failed to open \"%s\"\n", f->name); |
| 329 | fifo_read_error(f, buf); |
| 330 | continue; |
| 331 | } |
| 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)); |
| 342 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
| 343 | if (FD_ISSET(f->fd, &fds)) { |
| 344 | debug("reading: %s\n", f->name); |
| 345 | fifo_read_one(f, buf); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | int |
| 351 | main(int argc, char *argv[]) |
| 352 | { |
| 353 | int width = 0; |
| 354 | int nfifos = 0; |
| 355 | int seplen = 0; |
| 356 | int prefix = 0; |
| 357 | int errors = 0; |
| 358 | char *buf; |
| 359 | Config cfg0 = defaults; |
| 360 | Config *cfg = &cfg0; |
| 361 | Display *display = NULL; |
| 362 | struct stat st; |
| 363 | |
| 364 | argv0 = argv[0]; |
| 365 | |
| 366 | opts_parse(cfg, argc, argv); |
| 367 | debug("argv0 = %s\n", argv0); |
| 368 | config_print(cfg); |
| 369 | if (cfg->fifos == NULL) |
| 370 | usage("No fifo specs were given!\n"); |
| 371 | |
| 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 | |
| 388 | width = cfg->total_width; |
| 389 | seplen = strlen(cfg->separator); |
| 390 | |
| 391 | /* 2nd pass to make space for separators */ |
| 392 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
| 393 | f->pos += prefix; |
| 394 | prefix += seplen; |
| 395 | nfifos++; |
| 396 | } |
| 397 | width += (seplen * (nfifos - 1)); |
| 398 | buf = calloc(1, width + 1); |
| 399 | if (buf == NULL) |
| 400 | fatal("[memory] Failed to allocate buffer of %d bytes", width); |
| 401 | memset(buf, ' ', width); |
| 402 | buf[width] = '\0'; |
| 403 | /* 3rd pass to set the separators */ |
| 404 | for (Fifo *f = cfg->fifos; f; f = f->next) { |
| 405 | if (f->pos) { /* Skip the first, left-most */ |
| 406 | /* Copying only seplen ensures we omit the '\0' byte. */ |
| 407 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL))) |
| 412 | fatal("XOpenDisplay failed with: %p\n", display); |
| 413 | /* TODO: nanosleep and nano time diff */ |
| 414 | /* TODO: Handle signals */ |
| 415 | for (;;) { |
| 416 | /* TODO: Check TTL and maybe blank-out */ |
| 417 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ |
| 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. |
| 420 | * */ |
| 421 | fifo_read_all(cfg, buf); |
| 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 | } |
| 430 | } |
| 431 | |
| 432 | return EXIT_SUCCESS; |
| 433 | } |