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