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 | { | |
3efcce25 SK |
240 | ssize_t current; |
241 | ssize_t total; | |
77c76070 | 242 | char *b; |
3efcce25 | 243 | char c; |
77c76070 | 244 | |
3efcce25 SK |
245 | current = 0; |
246 | total = 0; | |
247 | c = '\0'; | |
77c76070 SK |
248 | b = buf + f->pos; |
249 | memset(b, ' ', f->width); | |
3efcce25 SK |
250 | while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width) |
251 | *b++ = c; | |
252 | if (current == -1) | |
253 | error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno)); | |
77c76070 SK |
254 | close(f->fd); |
255 | f->fd = -1; | |
256 | } | |
257 | ||
4d66492f SK |
258 | void |
259 | read_all(Config *cfg, char *buf) | |
260 | { | |
77c76070 SK |
261 | fd_set fds; |
262 | int maxfd; | |
263 | int ready; | |
bec93767 | 264 | struct stat st; |
77c76070 SK |
265 | |
266 | FD_ZERO(&fds); | |
267 | ||
bec93767 | 268 | /* TODO: Check TTL */ |
4d66492f | 269 | for (File *f = cfg->files; f; f = f->next) { |
bec93767 SK |
270 | /* TODO: Create the FIFO if it doesn't already exist. */ |
271 | if (lstat(f->name, &st) < 0) | |
272 | fatal("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno)); | |
273 | if (!(st.st_mode & S_IFIFO)) | |
274 | fatal("\"%s\" is not a FIFO\n", f->name); | |
77c76070 | 275 | debug("opening: %s\n", f->name); |
4d66492f | 276 | if (f->fd < 0) |
77c76070 SK |
277 | f->fd = open(f->name, O_RDONLY | O_NONBLOCK); |
278 | if (f->fd == -1) | |
4d66492f SK |
279 | /* TODO: Consider backing off retries for failed files. */ |
280 | fatal("Failed to open \"%s\"\n", f->name); | |
77c76070 SK |
281 | if (f->fd > maxfd) |
282 | maxfd = f->fd; | |
283 | FD_SET(f->fd, &fds); | |
284 | } | |
285 | debug("selecting...\n"); | |
286 | ready = select(maxfd + 1, &fds, NULL, NULL, NULL); | |
287 | debug("ready: %d\n", ready); | |
288 | assert(ready != 0); | |
289 | if (ready < 0) | |
290 | fatal("%s", strerror(errno)); | |
291 | for (File *f = cfg->files; f; f = f->next) { | |
292 | if (FD_ISSET(f->fd, &fds)) { | |
293 | debug("reading: %s\n", f->name); | |
294 | read_one(f, buf); | |
9b5ebc12 SK |
295 | } |
296 | } | |
297 | } | |
298 | ||
299 | int | |
4d66492f | 300 | main(int argc, char *argv[]) |
9b5ebc12 | 301 | { |
4d66492f SK |
302 | int width; |
303 | int nfiles = 0; | |
304 | int seplen; | |
305 | int prefix = 0; | |
306 | char *buf; | |
307 | Config *cfg = &defaults; | |
308 | ||
309 | argv0 = argv[0]; | |
310 | ||
311 | opts_parse(cfg, argc, argv, 1); | |
312 | debug("argv0 = %s\n", argv0); | |
77c76070 | 313 | config_print(cfg); |
4d66492f | 314 | if (cfg->files == NULL) |
9b5ebc12 | 315 | usage("No file specs were given!\n"); |
4d66492f SK |
316 | |
317 | width = cfg->total_width; | |
318 | seplen = strlen(cfg->separator); | |
319 | ||
320 | /* 1st pass to make space for separators */ | |
321 | for (File *f = cfg->files; f; f = f->next) { | |
322 | f->pos += prefix; | |
323 | prefix += seplen; | |
324 | nfiles++; | |
325 | } | |
326 | width += (seplen * (nfiles - 1)); | |
3c836bfd | 327 | buf = calloc(1, width + 1); |
4d66492f SK |
328 | if (buf == NULL) |
329 | fatal("[memory] Failed to allocate buffer of %d bytes", width); | |
330 | memset(buf, ' ', width); | |
331 | buf[width] = '\0'; | |
332 | /* 2nd pass to set the separators */ | |
333 | for (File *f = cfg->files; f; f = f->next) { | |
334 | if (f->pos) { /* Skip the first, left-most */ | |
77c76070 SK |
335 | /* Copying only seplen ensures we omit the '\0' byte. */ |
336 | strncpy(buf + (f->pos - seplen), cfg->separator, seplen); | |
4d66492f SK |
337 | } |
338 | } | |
339 | ||
77c76070 | 340 | printf("%s\n", buf); |
4d66492f SK |
341 | /* TODO: nanosleep and nano time diff */ |
342 | for (;;) { | |
343 | read_all(cfg, buf); | |
344 | printf("%s\n", buf); | |
4d66492f | 345 | } |
9b5ebc12 | 346 | } |