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