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