Check that file exists and that it is a FIFO
[khatus.git] / x5 / khatus.c
... / ...
CommitLineData
1
2#include <fcntl.h>
3#include <unistd.h>
4#include <sys/select.h>
5#include <sys/stat.h>
6
7#include <assert.h>
8#include <ctype.h>
9#include <errno.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
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);}
19
20char *argv0;
21
22typedef struct File File;
23struct File {
24 char *name;
25 int fd;
26 int width;
27 int last_read;
28 int ttl;
29 int pos; /* Position on the output buffer. */
30 File *next;
31};
32
33typedef struct Config Config;
34struct Config {
35 int interval;
36 char * separator;
37 File * files;
38 int file_count;
39 int total_width;
40} defaults = {
41 .interval = 1,
42 .separator = "|",
43 .files = NULL,
44 .file_count = 0,
45 .total_width = 0,
46};
47
48void
49file_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
72void
73file_print_all(File *head)
74{
75 for (File *f = head; f; f = f->next) {
76 file_print_one(f);
77 }
78}
79
80void
81config_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}
99
100int
101is_pos_num(char *s)
102{
103 while (*s != '\0')
104 if (!isdigit(*(s++)))
105 return 0;
106 return 1;
107}
108
109void
110print_usage()
111{
112 assert(argv0);
113 fprintf(
114 stderr,
115 "\n"
116 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
117 "\n"
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"
126 "\n",
127 argv0
128 );
129 fprintf(
130 stderr,
131 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
132 "\n",
133 argv0
134 );
135}
136
137void opts_parse_any(Config *, int, char *[], int); /* For mutually-recursive calls. */
138
139void
140parse_opts_opt_i(Config *cfg, int argc, char *argv[], int i)
141{
142 if (i < argc) {
143 char *param = argv[i++];
144
145 if (is_pos_num(param)) {
146 cfg->interval = atoi(param);
147 opts_parse_any(cfg, argc, argv, i);
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
156void
157parse_opts_opt_s(Config *cfg, int argc, char *argv[], int i)
158{
159 if (i < argc) {
160 cfg->separator = calloc((strlen(argv[i]) + 1), sizeof(char));
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
168void
169parse_opts_opt(Config *cfg, int argc, char *argv[], int i)
170{
171 switch (argv[i][1]) {
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 */
174 default : usage("Option \"%s\" is invalid\n", argv[i]);
175 }
176}
177
178void
179parse_opts_spec(Config *cfg, int argc, char *argv[], int i)
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);
192 File *f = calloc(1, sizeof(struct File));
193 if (f) {
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++;
205 } else {
206 fatal("[memory] Allocation failure.");
207 }
208 opts_parse_any(cfg, argc, argv, i);
209}
210
211void
212opts_parse_any(Config *cfg, int argc, char *argv[], int i)
213{
214 if (i < argc) {
215 switch (argv[i][0]) {
216 case '-': parse_opts_opt(cfg, argc, argv, i); break;
217 default : parse_opts_spec(cfg, argc, argv, i);
218 }
219 }
220}
221
222void
223opts_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
237void
238read_one(File *f, char *buf)
239{
240 ssize_t n;
241 char *b;
242
243 b = buf + f->pos;
244 memset(b, ' ', f->width);
245 /* TODO: Read upto \n or width */
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
266void
267read_all(Config *cfg, char *buf)
268{
269 fd_set fds;
270 int maxfd;
271 int ready;
272 struct stat st;
273
274 FD_ZERO(&fds);
275
276 /* TODO: Check TTL */
277 for (File *f = cfg->files; f; f = f->next) {
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);
283 debug("opening: %s\n", f->name);
284 if (f->fd < 0)
285 f->fd = open(f->name, O_RDONLY | O_NONBLOCK);
286 if (f->fd == -1)
287 /* TODO: Consider backing off retries for failed files. */
288 fatal("Failed to open \"%s\"\n", f->name);
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);
303 }
304 }
305}
306
307int
308main(int argc, char *argv[])
309{
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);
321 config_print(cfg);
322 if (cfg->files == NULL)
323 usage("No file specs were given!\n");
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));
335 buf = calloc(1, width + 1);
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 */
343 /* Copying only seplen ensures we omit the '\0' byte. */
344 strncpy(buf + (f->pos - seplen), cfg->separator, seplen);
345 }
346 }
347
348 printf("%s\n", buf);
349 /* TODO: nanosleep and nano time diff */
350 for (;;) {
351 read_all(cfg, buf);
352 printf("%s\n", buf);
353 }
354}
This page took 0.023351 seconds and 4 git commands to generate.