Commit | Line | Data |
---|---|---|
bec93767 | 1 | |
77c76070 SK |
2 | #include <fcntl.h> |
3 | #include <unistd.h> | |
4 | #include <sys/select.h> | |
bec93767 | 5 | #include <sys/stat.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; | |
41 | } defaults = { | |
42 | .interval = 1, | |
43 | .separator = "|", | |
4d66492f SK |
44 | .files = NULL, |
45 | .file_count = 0, | |
46 | .total_width = 0, | |
9b5ebc12 SK |
47 | }; |
48 | ||
77c76070 SK |
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 | } | |
9b5ebc12 SK |
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 | { | |
4d66492f | 113 | assert(argv0); |
9b5ebc12 SK |
114 | fprintf( |
115 | stderr, | |
116 | "\n" | |
4d66492f | 117 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" |
9b5ebc12 | 118 | "\n" |
4d66492f SK |
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" | |
9b5ebc12 | 127 | "\n", |
4d66492f | 128 | argv0 |
9b5ebc12 SK |
129 | ); |
130 | fprintf( | |
131 | stderr, | |
132 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
133 | "\n", | |
4d66492f | 134 | argv0 |
9b5ebc12 SK |
135 | ); |
136 | } | |
137 | ||
4d66492f | 138 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
9b5ebc12 SK |
139 | |
140 | void | |
4d66492f | 141 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
142 | { |
143 | if (i < argc) { | |
144 | char *param = argv[i++]; | |
145 | ||
146 | if (is_pos_num(param)) { | |
4d66492f SK |
147 | cfg->interval = atoi(param); |
148 | opts_parse_any(cfg, argc, argv, i); | |
9b5ebc12 SK |
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 | |
4d66492f SK |
158 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
159 | { | |
160 | if (i < argc) { | |
3c836bfd | 161 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
4d66492f SK |
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 | ||
4d66492f SK |
169 | void |
170 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
171 | { |
172 | switch (argv[i][1]) { | |
4d66492f SK |
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 */ | |
9b5ebc12 SK |
175 | default : usage("Option \"%s\" is invalid\n", argv[i]); |
176 | } | |
177 | } | |
178 | ||
179 | void | |
4d66492f | 180 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
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); | |
3c836bfd | 193 | File *f = calloc(1, sizeof(struct File)); |
9b5ebc12 | 194 | if (f) { |
4d66492f SK |
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++; | |
9b5ebc12 | 206 | } else { |
4d66492f | 207 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 208 | } |
4d66492f | 209 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
210 | } |
211 | ||
212 | void | |
4d66492f | 213 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
214 | { |
215 | if (i < argc) { | |
216 | switch (argv[i][0]) { | |
4d66492f SK |
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 | ||
77c76070 SK |
238 | void |
239 | read_one(File *f, char *buf) | |
240 | { | |
3efcce25 SK |
241 | ssize_t current; |
242 | ssize_t total; | |
77c76070 | 243 | char *b; |
3efcce25 | 244 | char c; |
77c76070 | 245 | |
3efcce25 SK |
246 | current = 0; |
247 | total = 0; | |
248 | c = '\0'; | |
77c76070 SK |
249 | b = buf + f->pos; |
250 | memset(b, ' ', f->width); | |
3efcce25 SK |
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)); | |
6c29392d | 255 | /* TODO Record timestamp read */ |
77c76070 SK |
256 | close(f->fd); |
257 | f->fd = -1; | |
258 | } | |
259 | ||
4d66492f SK |
260 | void |
261 | read_all(Config *cfg, char *buf) | |
262 | { | |
77c76070 SK |
263 | fd_set fds; |
264 | int maxfd; | |
265 | int ready; | |
bec93767 | 266 | struct stat st; |
77c76070 SK |
267 | |
268 | FD_ZERO(&fds); | |
269 | ||
bec93767 | 270 | /* TODO: Check TTL */ |
4d66492f | 271 | for (File *f = cfg->files; f; f = f->next) { |
bec93767 SK |
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); | |
77c76070 | 277 | debug("opening: %s\n", f->name); |
4d66492f | 278 | if (f->fd < 0) |
77c76070 SK |
279 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
280 | if (f->fd == -1) | |
4d66492f SK |
281 | /* TODO: Consider backing off retries for failed files. */ |
282 | fatal("Failed to open \"%s\"\n", f->name); | |
77c76070 SK |
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); | |
9b5ebc12 SK |
297 | } |
298 | } | |
299 | } | |
300 | ||
301 | int | |
4d66492f | 302 | main(int argc, char *argv[]) |
9b5ebc12 | 303 | { |
4d66492f SK |
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); | |
77c76070 | 315 | config_print(cfg); |
4d66492f | 316 | if (cfg->files == NULL) |
9b5ebc12 | 317 | usage("No file specs were given!\n"); |
4d66492f SK |
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)); | |
3c836bfd | 329 | buf = calloc(1, width + 1); |
4d66492f SK |
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 */ | |
77c76070 SK |
337 | /* Copying only seplen ensures we omit the '\0' byte. */ |
338 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); | |
4d66492f SK |
339 | } |
340 | } | |
341 | ||
77c76070 | 342 | printf("%s\n", buf); |
4d66492f SK |
343 | /* TODO: nanosleep and nano time diff */ |
344 | for (;;) { | |
6c29392d SK |
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 */ | |
4d66492f SK |
348 | read_all(cfg, buf); |
349 | printf("%s\n", buf); | |
4d66492f | 350 | } |
9b5ebc12 | 351 | } |