1 #include <sys/select.h>
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);}
21 #define ERRMSG "ERROR"
23 static const char errmsg
[] = ERRMSG
;
24 static const int errlen
= sizeof(ERRMSG
) - 1;
28 /* TODO: Convert fifo list to fifo array. */
29 typedef struct Fifo Fifo
;
36 int pos
; /* Position on the output buffer. */
40 typedef struct Config Config
;
47 int output_to_x_root_window
;
54 .output_to_x_root_window
= 0,
58 fifo_print_one(Fifo
*f
)
82 fifo_print_all(Fifo
*head
)
84 for (Fifo
*f
= head
; f
; f
= f
->next
) {
90 config_print(Config
*c
)
106 fifo_print_all(c
->fifos
);
113 if (!isdigit(*(s
++)))
125 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
127 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
128 " FILE_PATH = string\n"
129 " DATA_WIDTH = int (* (positive) number of characters *)\n"
130 " DATA_TTL = int (* (positive) number of seconds *)\n"
131 " OPTION = -i INTERVAL\n"
133 " SEPARATOR = string\n"
134 " INTERVAL = int (* (positive) number of seconds *)\n"
140 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
146 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
149 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
152 char *param
= argv
[i
++];
154 if (is_pos_num(param
)) {
155 cfg
->interval
= atoi(param
);
156 opts_parse_any(cfg
, argc
, argv
, i
);
158 usage("Option -i parameter is invalid: \"%s\"\n", param
);
161 usage("Option -i parameter is missing.\n");
166 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
169 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
170 strcpy(cfg
->separator
, argv
[i
]);
171 opts_parse_any(cfg
, argc
, argv
, ++i
);
173 usage("Option -s parameter is missing.\n");
178 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
180 switch (argv
[i
][1]) {
182 /* TODO: Generic set_int */
183 parse_opts_opt_i(cfg
, argc
, argv
, ++i
);
186 /* TODO: Generic set_str */
187 parse_opts_opt_s(cfg
, argc
, argv
, ++i
);
190 cfg
->output_to_x_root_window
= 1;
191 opts_parse_any(cfg
, argc
, argv
, ++i
);
194 usage("Option \"%s\" is invalid\n", argv
[i
]);
199 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
202 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
209 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
211 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
212 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
219 f
->pos
= cfg
->total_width
;
220 f
->next
= cfg
->fifos
;
223 cfg
->total_width
+= f
->width
;
226 fatal("[memory] Allocation failure.");
228 opts_parse_any(cfg
, argc
, argv
, i
);
232 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
235 switch (argv
[i
][0]) {
237 parse_opts_opt(cfg
, argc
, argv
, i
);
240 parse_opts_spec(cfg
, argc
, argv
, i
);
246 opts_parse(Config
*cfg
, int argc
, char *argv
[])
248 opts_parse_any(cfg
, argc
, argv
, 1);
250 Fifo
*last
= cfg
->fifos
;
252 for (Fifo
*f
= last
; f
; ) {
253 Fifo
*next
= f
->next
;
254 f
->next
= cfg
->fifos
;
261 fifo_read_error(Fifo
*f
, char *buf
)
267 /* Copy as much of the error message as possible.
268 * EXCLUDING the reminating \0. */
269 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
271 /* Any remaining slots: */
272 for (; i
< f
->width
; i
++)
277 fifo_read_one(Fifo
*f
, char *buf
)
288 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
291 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
292 fifo_read_error(f
, buf
);
294 while (total
++ < f
->width
)
296 /* TODO Record timestamp read */
302 fifo_read_all(Config
*cfg
, char *buf
)
310 /* TODO: Check TTL */
311 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
312 /* TODO: Create the FIFO if it doesn't already exist. */
313 if (lstat(f
->name
, &st
) < 0) {
314 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
315 fifo_read_error(f
, buf
);
318 if (!(st
.st_mode
& S_IFIFO
)) {
319 error("\"%s\" is not a FIFO\n", f
->name
);
320 fifo_read_error(f
, buf
);
323 debug("opening: %s\n", f
->name
);
325 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
327 /* TODO: Consider backing off retries for failed fifos. */
328 error("Failed to open \"%s\"\n", f
->name
);
329 fifo_read_error(f
, buf
);
336 debug("selecting...\n");
337 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
338 debug("ready: %d\n", ready
);
341 fatal("%s", strerror(errno
));
342 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
343 if (FD_ISSET(f
->fd
, &fds
)) {
344 debug("reading: %s\n", f
->name
);
345 fifo_read_one(f
, buf
);
351 main(int argc
, char *argv
[])
359 Config cfg0
= defaults
;
361 Display
*display
= NULL
;
366 opts_parse(cfg
, argc
, argv
);
367 debug("argv0 = %s\n", argv0
);
369 if (cfg
->fifos
== NULL
)
370 usage("No fifo specs were given!\n");
372 /* 1st pass to check file existence and type */
373 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
374 if (lstat(f
->name
, &st
) < 0) {
375 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
379 if (!(st
.st_mode
& S_IFIFO
)) {
380 error("\"%s\" is not a FIFO\n", f
->name
);
386 fatal("Encountered errors with the given file paths. See log.\n");
388 width
= cfg
->total_width
;
389 seplen
= strlen(cfg
->separator
);
391 /* 2nd pass to make space for separators */
392 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
397 width
+= (seplen
* (nfifos
- 1));
398 buf
= calloc(1, width
+ 1);
400 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
401 memset(buf
, ' ', width
);
403 /* 3rd pass to set the separators */
404 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
405 if (f
->pos
) { /* Skip the first, left-most */
406 /* Copying only seplen ensures we omit the '\0' byte. */
407 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
411 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
412 fatal("XOpenDisplay failed with: %p\n", display
);
413 /* TODO: nanosleep and nano time diff */
414 /* TODO: Handle signals */
416 /* TODO: Check TTL and maybe blank-out */
417 /* TODO: How to trigger TTL check? On select? Alarm signal? */
418 /* TODO: Set timeout on fifo_read_all based on diff of last time of
419 * fifo_read_all and desired time of next TTL check.
421 fifo_read_all(cfg
, buf
);
422 if (cfg
->output_to_x_root_window
) {
423 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
424 fatal("XStoreName failed.\n");
This page took 0.086159 seconds and 4 git commands to generate.