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 | |
4d66492f SK |
22 | typedef struct File File; |
23 | struct File { | |
24 | char *name; | |
25 | int fd; | |
9b5ebc12 | 26 | int width; |
4d66492f | 27 | int last_read; |
9b5ebc12 | 28 | int ttl; |
4d66492f SK |
29 | int pos; /* Position on the output buffer. */ |
30 | File *next; | |
9b5ebc12 SK |
31 | }; |
32 | ||
4d66492f SK |
33 | typedef struct Config Config; |
34 | struct Config { | |
9b5ebc12 | 35 | int interval; |
4d66492f | 36 | char * separator; |
4d66492f SK |
37 | File * files; |
38 | int file_count; | |
39 | int total_width; | |
40 | } defaults = { | |
41 | .interval = 1, | |
42 | .separator = "|", | |
4d66492f SK |
43 | .files = NULL, |
44 | .file_count = 0, | |
45 | .total_width = 0, | |
9b5ebc12 SK |
46 | }; |
47 | ||
77c76070 SK |
48 | void |
49 | file_print_one(File *f) | |
50 | { | |
51 | debug( | |
52 | "File " | |
53 | "{" | |
54 | " name = %s," | |
55 | " fd = %d," | |
56 | " width = %d," | |
57 | " last_read = %d," | |
58 | " ttl = %d," | |
59 | " pos = %d," | |
60 | " next = %p," | |
61 | " }\n", | |
62 | f->name, | |
63 | f->fd, | |
64 | f->width, | |
65 | f->last_read, | |
66 | f->ttl, | |
67 | f->pos, | |
68 | f->next | |
69 | ); | |
70 | } | |
71 | ||
72 | void | |
73 | file_print_all(File *head) | |
74 | { | |
75 | for (File *f = head; f; f = f->next) { | |
76 | file_print_one(f); | |
77 | } | |
78 | } | |
79 | ||
80 | void | |
81 | config_print(Config *c) | |
82 | { | |
83 | debug( | |
84 | "Config " | |
85 | "{" | |
86 | " interval = %d," | |
87 | " separator = %s," | |
88 | " file_count = %d," | |
89 | " total_width = %d," | |
90 | " files = ..." | |
91 | " }\n", | |
92 | c->interval, | |
93 | c->separator, | |
94 | c->file_count, | |
95 | c->total_width | |
96 | ); | |
97 | file_print_all(c->files); | |
98 | } | |
9b5ebc12 SK |
99 | |
100 | int | |
101 | is_pos_num(char *s) | |
102 | { | |
103 | while (*s != '\0') | |
104 | if (!isdigit(*(s++))) | |
105 | return 0; | |
106 | return 1; | |
107 | } | |
108 | ||
109 | void | |
110 | print_usage() | |
111 | { | |
4d66492f | 112 | assert(argv0); |
9b5ebc12 SK |
113 | fprintf( |
114 | stderr, | |
115 | "\n" | |
4d66492f | 116 | "Usage: %s [OPTION ...] SPEC [SPEC ...]\n" |
9b5ebc12 | 117 | "\n" |
4d66492f SK |
118 | " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n" |
119 | " FILE_PATH = string\n" | |
120 | " DATA_WIDTH = int (* (positive) number of characters *)\n" | |
121 | " DATA_TTL = int (* (positive) number of seconds *)\n" | |
122 | " OPTION = -i INTERVAL\n" | |
123 | " | -s SEPARATOR\n" | |
124 | " SEPARATOR = string\n" | |
125 | " INTERVAL = int (* (positive) number of seconds *)\n" | |
9b5ebc12 | 126 | "\n", |
4d66492f | 127 | argv0 |
9b5ebc12 SK |
128 | ); |
129 | fprintf( | |
130 | stderr, | |
131 | "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n" | |
132 | "\n", | |
4d66492f | 133 | argv0 |
9b5ebc12 SK |
134 | ); |
135 | } | |
136 | ||
4d66492f | 137 | void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */ |
9b5ebc12 SK |
138 | |
139 | void | |
4d66492f | 140 | parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
141 | { |
142 | if (i < argc) { | |
143 | char *param = argv[i++]; | |
144 | ||
145 | if (is_pos_num(param)) { | |
4d66492f SK |
146 | cfg->interval = atoi(param); |
147 | opts_parse_any(cfg, argc, argv, i); | |
9b5ebc12 SK |
148 | } else { |
149 | usage("Option -i parameter is invalid: \"%s\"\n", param); | |
150 | } | |
151 | } else { | |
152 | usage("Option -i parameter is missing.\n"); | |
153 | } | |
154 | } | |
155 | ||
156 | void | |
4d66492f SK |
157 | parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i) |
158 | { | |
159 | if (i < argc) { | |
3c836bfd | 160 | cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char)); |
4d66492f SK |
161 | strcpy(cfg->separator, argv[i]); |
162 | opts_parse_any(cfg, argc, argv, ++i); | |
163 | } else { | |
164 | usage("Option -s parameter is missing.\n"); | |
165 | } | |
166 | } | |
167 | ||
4d66492f SK |
168 | void |
169 | parse_opts_opt(Config *cfg, int argc, char *argv[], int i) | |
9b5ebc12 SK |
170 | { |
171 | switch (argv[i][1]) { | |
4d66492f SK |
172 | case 'i': parse_opts_opt_i(cfg, argc, argv, ++i); break; /* TODO: Generic set_int */ |
173 | case 's': parse_opts_opt_s(cfg, argc, argv, ++i); break; /* TODO: Generic set_str */ | |
9b5ebc12 SK |
174 | default : usage("Option \"%s\" is invalid\n", argv[i]); |
175 | } | |
176 | } | |
177 | ||
178 | void | |
4d66492f | 179 | parse_opts_spec(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
180 | { |
181 | if ((i + 3) > argc) | |
182 | usage("[spec] Parameter(s) missing for file \"%s\".\n", argv[i]); | |
183 | ||
184 | char *n = argv[i++]; | |
185 | char *w = argv[i++]; | |
186 | char *t = argv[i++]; | |
187 | ||
188 | if (!is_pos_num(w)) | |
189 | usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w, n); | |
190 | if (!is_pos_num(t)) | |
191 | usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t, n); | |
3c836bfd | 192 | File *f = calloc(1, sizeof(struct File)); |
9b5ebc12 | 193 | if (f) { |
4d66492f SK |
194 | f->name = n; |
195 | f->fd = -1; | |
196 | f->width = atoi(w); | |
197 | f->ttl = atoi(t); | |
198 | f->last_read = 0; | |
199 | f->pos = cfg->total_width; | |
200 | f->next = cfg->files; | |
201 | ||
202 | cfg->files = f; | |
203 | cfg->total_width += f->width; | |
204 | cfg->file_count++; | |
9b5ebc12 | 205 | } else { |
4d66492f | 206 | fatal("[memory] Allocation failure."); |
9b5ebc12 | 207 | } |
4d66492f | 208 | opts_parse_any(cfg, argc, argv, i); |
9b5ebc12 SK |
209 | } |
210 | ||
211 | void | |
4d66492f | 212 | opts_parse_any(Config *cfg, int argc, char *argv[], int i) |
9b5ebc12 SK |
213 | { |
214 | if (i < argc) { | |
215 | switch (argv[i][0]) { | |
4d66492f SK |
216 | case '-': parse_opts_opt(cfg, argc, argv, i); break; |
217 | default : parse_opts_spec(cfg, argc, argv, i); | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | void | |
223 | opts_parse(Config *cfg, int argc, char *argv[], int i) | |
224 | { | |
225 | opts_parse_any(cfg, argc, argv, 1); | |
226 | ||
227 | File *last = cfg->files; | |
228 | cfg->files = NULL; | |
229 | for (File *f = last; f; ) { | |
230 | File *next = f->next; | |
231 | f->next = cfg->files; | |
232 | cfg->files = f; | |
233 | f = next; | |
234 | } | |
235 | } | |
236 | ||
77c76070 SK |
237 | void |
238 | read_one(File *f, char *buf) | |
239 | { | |
240 | ssize_t n; | |
241 | char *b; | |
242 | ||
243 | b = buf + f->pos; | |
244 | memset(b, ' ', f->width); | |
bec93767 | 245 | /* TODO: Read upto \n or width */ |
77c76070 SK |
246 | while ((n = read(f->fd, b, f->width)) > 0) { |
247 | b += n; | |
248 | debug("read %zd from %s\n", n, f->name); | |
249 | } | |
250 | ||
251 | if (n > -1) { | |
252 | if (*(b - 1) == '\n') | |
253 | *(b - 1) = ' '; | |
254 | } else { | |
255 | error( | |
256 | "Failed to read: \"%s\". Error: %s\n", | |
257 | f->name, | |
258 | strerror(errno) | |
259 | ); | |
260 | } | |
261 | ||
262 | close(f->fd); | |
263 | f->fd = -1; | |
264 | } | |
265 | ||
4d66492f SK |
266 | void |
267 | read_all(Config *cfg, char *buf) | |
268 | { | |
77c76070 SK |
269 | fd_set fds; |
270 | int maxfd; | |
271 | int ready; | |
bec93767 | 272 | struct stat st; |
77c76070 SK |
273 | |
274 | FD_ZERO(&fds); | |
275 | ||
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; | |
316 | ||
317 | argv0 = argv[0]; | |
318 | ||
319 | opts_parse(cfg, argc, argv, 1); | |
320 | debug("argv0 = %s\n", argv0); | |
77c76070 | 321 | config_print(cfg); |
4d66492f | 322 | if (cfg->files == NULL) |
9b5ebc12 | 323 | usage("No file specs were given!\n"); |
4d66492f SK |
324 | |
325 | width = cfg->total_width; | |
326 | seplen = strlen(cfg->separator); | |
327 | ||
328 | /* 1st pass to make space for separators */ | |
329 | for (File *f = cfg->files; f; f = f->next) { | |
330 | f->pos += prefix; | |
331 | prefix += seplen; | |
332 | nfiles++; | |
333 | } | |
334 | width += (seplen * (nfiles - 1)); | |
3c836bfd | 335 | buf = calloc(1, width + 1); |
4d66492f SK |
336 | if (buf == NULL) |
337 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
338 | memset(buf, ' ', width); | |
339 | buf[width] = '\0'; | |
340 | /* 2nd pass to set the separators */ | |
341 | for (File *f = cfg->files; f; f = f->next) { | |
342 | if (f->pos) { /* Skip the first, left-most */ | |
77c76070 SK |
343 | /* Copying only seplen ensures we omit the '\0' byte. */ |
344 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); | |
4d66492f SK |
345 | } |
346 | } | |
347 | ||
77c76070 | 348 | printf("%s\n", buf); |
4d66492f SK |
349 | /* TODO: nanosleep and nano time diff */ |
350 | for (;;) { | |
351 | read_all(cfg, buf); | |
352 | printf("%s\n", buf); | |
4d66492f | 353 | } |
9b5ebc12 | 354 | } |