| 1 | |
| 2 | #include <fcntl.h> |
| 3 | #include <unistd.h> |
| 4 | #include <sys/select.h> |
| 5 | #include <sys/stat.h> |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <ctype.h> |
| 9 | #include <errno.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #define debug(args...) {fprintf(stderr, "[debug] " args);} |
| 15 | #define info( args...) {fprintf(stderr, "[info] " args);} |
| 16 | #define error(args...) {fprintf(stderr, "[error] " args);} |
| 17 | #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);} |
| 18 | #define usage(args...) {print_usage(); fatal("[usage] " args);} |
| 19 | |
| 20 | char *argv0; |
| 21 | |
| 22 | /* TODO: Convert file list to file array. */ |
| 23 | typedef struct File File; |
| 24 | struct File { |
| 25 | char *name; |
| 26 | int fd; |
| 27 | int width; |
| 28 | int last_read; |
| 29 | int ttl; |
| 30 | int pos; /* Position on the output buffer. */ |
| 31 | File *next; |
| 32 | }; |
| 33 | |
| 34 | typedef struct Config Config; |
| 35 | struct Config { |
| 36 | int interval; |
| 37 | char * separator; |
| 38 | File * files; |
| 39 | int file_count; |
| 40 | int total_width; |
| 41 | } defaults = { |
| 42 | .interval = 1, |
| 43 | .separator = "|", |
| 44 | .files = NULL, |
| 45 | .file_count = 0, |
| 46 | .total_width = 0, |
| 47 | }; |
| 48 | |
| 49 | void |
| 50 | file_print_one(File *f) |
| 51 | { |
| 52 | debug( |
| 53 | "File " |
| 54 | "{" |
| 55 | " name = %s," |
| 56 | " fd = %d," |
| 57 | " width = %d," |
| 58 | " last_read = %d," |
| 59 | " ttl = %d," |
| 60 | " pos = %d," |
| 61 | " next = %p," |
| 62 | " }\n", |
| 63 | f->name, |
| 64 | f->fd, |
| 65 | f->width, |
| 66 | f->last_read, |
| 67 | f->ttl, |
| 68 | f->pos, |
| 69 | f->next |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | file_print_all(File *head) |
| 75 | { |
| 76 | for (File *f = head; f; f = f->next) { |
| 77 | file_print_one(f); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | config_print(Config *c) |
| 83 | { |
| 84 | debug( |
| 85 | "Config " |
| 86 | "{" |
| 87 | " interval = %d," |
| 88 | " separator = %s," |
| 89 | " file_count = %d," |
| 90 | " total_width = %d," |
| 91 | " files = ..." |
| 92 | " }\n", |
| 93 | c->interval, |
| 94 | c->separator, |
| 95 | c->file_count, |
| 96 | c->total_width |
| 97 | ); |
| 98 | file_print_all(c->files); |
| 99 | } |
| 100 | |
| 101 | int |
| 102 | is_pos_num(char *s) |
| 103 | { |
| 104 | while (*s != '\0') |
| 105 | if (!isdigit(*(s++))) |
| 106 | return 0; |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | print_usage() |
| 112 | { |
| 113 | assert(argv0); |
| 114 | fprintf( |
| 115 | stderr, |
| 116 | "\n" |
| 117 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" |
| 118 | "\n" |
| 119 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" |
| 120 | " FILE_PATH = string\n" |
| 121 | " DATA_WIDTH = int (* (positive) number of characters *)\n" |
| 122 | " DATA_TTL = int (* (positive) number of seconds *)\n" |
| 123 | " OPTION = -i INTERVAL\n" |
| 124 | " | -s SEPARATOR\n" |
| 125 | " SEPARATOR = string\n" |
| 126 | " INTERVAL = int (* (positive) number of seconds *)\n" |
| 127 | "\n", |
| 128 | argv0 |
| 129 | ); |
| 130 | fprintf( |
| 131 | stderr, |
| 132 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" |
| 133 | "\n", |
| 134 | argv0 |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
| 139 | |
| 140 | void |
| 141 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
| 142 | { |
| 143 | if (i < argc) { |
| 144 | char *param = argv[i++]; |
| 145 | |
| 146 | if (is_pos_num(param)) { |
| 147 | cfg->interval = atoi(param); |
| 148 | opts_parse_any(cfg, argc, argv, i); |
| 149 | } else { |
| 150 | usage("Option -i parameter is invalid: \"%s\"\n", param); |
| 151 | } |
| 152 | } else { |
| 153 | usage("Option -i parameter is missing.\n"); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
| 159 | { |
| 160 | if (i < argc) { |
| 161 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
| 162 | strcpy(cfg->separator, argv[i]); |
| 163 | opts_parse_any(cfg, argc, argv, ++i); |
| 164 | } else { |
| 165 | usage("Option -s parameter is missing.\n"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) |
| 171 | { |
| 172 | switch (argv[i][1]) { |
| 173 | case 'i': parse_opts_opt_i(cfg, argc, argv, ++i); break; /* TODO: Generic set_int */ |
| 174 | case 's': parse_opts_opt_s(cfg, argc, argv, ++i); break; /* TODO: Generic set_str */ |
| 175 | default : usage("Option \"%s\" is invalid\n", argv[i]); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
| 181 | { |
| 182 | if ((i + 3) > argc) |
| 183 | usage("[spec] Parameter(s) missing for file \"%s\".\n", argv[i]); |
| 184 | |
| 185 | char *n = argv[i++]; |
| 186 | char *w = argv[i++]; |
| 187 | char *t = argv[i++]; |
| 188 | |
| 189 | if (!is_pos_num(w)) |
| 190 | usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w, n); |
| 191 | if (!is_pos_num(t)) |
| 192 | usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t, n); |
| 193 | File *f = calloc(1, sizeof(struct File)); |
| 194 | if (f) { |
| 195 | f->name = n; |
| 196 | f->fd = -1; |
| 197 | f->width = atoi(w); |
| 198 | f->ttl = atoi(t); |
| 199 | f->last_read = 0; |
| 200 | f->pos = cfg->total_width; |
| 201 | f->next = cfg->files; |
| 202 | |
| 203 | cfg->files = f; |
| 204 | cfg->total_width += f->width; |
| 205 | cfg->file_count++; |
| 206 | } else { |
| 207 | fatal("[memory] Allocation failure."); |
| 208 | } |
| 209 | opts_parse_any(cfg, argc, argv, i); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
| 214 | { |
| 215 | if (i < argc) { |
| 216 | switch (argv[i][0]) { |
| 217 | case '-': parse_opts_opt(cfg, argc, argv, i); break; |
| 218 | default : parse_opts_spec(cfg, argc, argv, i); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | opts_parse(Config *cfg, int argc, char *argv[], int i) |
| 225 | { |
| 226 | opts_parse_any(cfg, argc, argv, 1); |
| 227 | |
| 228 | File *last = cfg->files; |
| 229 | cfg->files = NULL; |
| 230 | for (File *f = last; f; ) { |
| 231 | File *next = f->next; |
| 232 | f->next = cfg->files; |
| 233 | cfg->files = f; |
| 234 | f = next; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | read_one(File *f, char *buf) |
| 240 | { |
| 241 | ssize_t current; |
| 242 | ssize_t total; |
| 243 | char *b; |
| 244 | char c; |
| 245 | |
| 246 | current = 0; |
| 247 | total = 0; |
| 248 | c = '\0'; |
| 249 | b = buf + f->pos; |
| 250 | memset(b, ' ', f->width); |
| 251 | while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width) |
| 252 | *b++ = c; |
| 253 | if (current == -1) |
| 254 | error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); |
| 255 | /* TODO Record timestamp read */ |
| 256 | close(f->fd); |
| 257 | f->fd = -1; |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | read_all(Config *cfg, char *buf) |
| 262 | { |
| 263 | fd_set fds; |
| 264 | int maxfd; |
| 265 | int ready; |
| 266 | struct stat st; |
| 267 | |
| 268 | FD_ZERO(&fds); |
| 269 | |
| 270 | /* TODO: Check TTL */ |
| 271 | for (File *f = cfg->files; f; f = f->next) { |
| 272 | /* TODO: Create the FIFO if it doesn't already exist. */ |
| 273 | if (lstat(f->name, &st) < 0) |
| 274 | fatal("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); |
| 275 | if (!(st.st_mode & S_IFIFO)) |
| 276 | fatal("\"%s\" is not a FIFO\n", f->name); |
| 277 | debug("opening: %s\n", f->name); |
| 278 | if (f->fd < 0) |
| 279 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
| 280 | if (f->fd == -1) |
| 281 | /* TODO: Consider backing off retries for failed files. */ |
| 282 | fatal("Failed to open \"%s\"\n", f->name); |
| 283 | if (f->fd > maxfd) |
| 284 | maxfd = f->fd; |
| 285 | FD_SET(f->fd, &fds); |
| 286 | } |
| 287 | debug("selecting...\n"); |
| 288 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); |
| 289 | debug("ready: %d\n", ready); |
| 290 | assert(ready != 0); |
| 291 | if (ready < 0) |
| 292 | fatal("%s", strerror(errno)); |
| 293 | for (File *f = cfg->files; f; f = f->next) { |
| 294 | if (FD_ISSET(f->fd, &fds)) { |
| 295 | debug("reading: %s\n", f->name); |
| 296 | read_one(f, buf); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | int |
| 302 | main(int argc, char *argv[]) |
| 303 | { |
| 304 | int width; |
| 305 | int nfiles = 0; |
| 306 | int seplen; |
| 307 | int prefix = 0; |
| 308 | char *buf; |
| 309 | Config *cfg = &defaults; |
| 310 | |
| 311 | argv0 = argv[0]; |
| 312 | |
| 313 | opts_parse(cfg, argc, argv, 1); |
| 314 | debug("argv0 = %s\n", argv0); |
| 315 | config_print(cfg); |
| 316 | if (cfg->files == NULL) |
| 317 | usage("No file specs were given!\n"); |
| 318 | |
| 319 | width = cfg->total_width; |
| 320 | seplen = strlen(cfg->separator); |
| 321 | |
| 322 | /* 1st pass to make space for separators */ |
| 323 | for (File *f = cfg->files; f; f = f->next) { |
| 324 | f->pos += prefix; |
| 325 | prefix += seplen; |
| 326 | nfiles++; |
| 327 | } |
| 328 | width += (seplen * (nfiles - 1)); |
| 329 | buf = calloc(1, width + 1); |
| 330 | if (buf == NULL) |
| 331 | fatal("[memory] Failed to allocate buffer of %d bytes", width); |
| 332 | memset(buf, ' ', width); |
| 333 | buf[width] = '\0'; |
| 334 | /* 2nd pass to set the separators */ |
| 335 | for (File *f = cfg->files; f; f = f->next) { |
| 336 | if (f->pos) { /* Skip the first, left-most */ |
| 337 | /* Copying only seplen ensures we omit the '\0' byte. */ |
| 338 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | printf("%s\n", buf); |
| 343 | /* TODO: nanosleep and nano time diff */ |
| 344 | for (;;) { |
| 345 | /* TODO: Check TTL and maybe blank-out */ |
| 346 | /* TODO: How to trigger TTL check? On select? Alarm signal? */ |
| 347 | /* TODO: Option to set X root window title or print */ |
| 348 | read_all(cfg, buf); |
| 349 | printf("%s\n", buf); |
| 350 | } |
| 351 | } |