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