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]) {
181 case 'i': parse_opts_opt_i(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_int */
182 case 's': parse_opts_opt_s(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_str */
184 cfg
->output_to_x_root_window
= 1;
185 opts_parse_any(cfg
, argc
, argv
, ++i
);
188 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
193 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
196 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
203 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
205 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
206 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
213 f
->pos
= cfg
->total_width
;
214 f
->next
= cfg
->fifos
;
217 cfg
->total_width
+= f
->width
;
220 fatal("[memory] Allocation failure.");
222 opts_parse_any(cfg
, argc
, argv
, i
);
226 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
229 switch (argv
[i
][0]) {
230 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
231 default : parse_opts_spec(cfg
, argc
, argv
, i
);
237 opts_parse(Config
*cfg
, int argc
, char *argv
[])
239 opts_parse_any(cfg
, argc
, argv
, 1);
241 Fifo
*last
= cfg
->fifos
;
243 for (Fifo
*f
= last
; f
; ) {
244 Fifo
*next
= f
->next
;
245 f
->next
= cfg
->fifos
;
252 read_error(Fifo
*f
, char *buf
)
258 /* Copy as much of the error message as possible.
259 * EXCLUDING the reminating \0. */
260 for (i
= 0; i
< errlen
&& i
< f
->width
; i
++)
262 /* Any remaining slots: */
263 for (; i
< f
->width
; i
++)
268 read_one(Fifo
*f
, char *buf
)
279 /* TODO: Could optimize here and only blank-out the remaining slots. */
280 memset(b
, ' ', f
->width
);
281 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
284 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
287 /* TODO Record timestamp read */
293 read_all(Config
*cfg
, char *buf
)
301 /* TODO: Check TTL */
302 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
303 /* TODO: Create the FIFO if it doesn't already exist. */
304 if (lstat(f
->name
, &st
) < 0) {
305 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
309 if (!(st
.st_mode
& S_IFIFO
)) {
310 error("\"%s\" is not a FIFO\n", f
->name
);
314 debug("opening: %s\n", f
->name
);
316 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
318 /* TODO: Consider backing off retries for failed fifos. */
319 error("Failed to open \"%s\"\n", f
->name
);
327 debug("selecting...\n");
328 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
329 debug("ready: %d\n", ready
);
332 fatal("%s", strerror(errno
));
333 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
334 if (FD_ISSET(f
->fd
, &fds
)) {
335 debug("reading: %s\n", f
->name
);
342 main(int argc
, char *argv
[])
350 Config cfg0
= defaults
;
352 Display
*display
= NULL
;
357 opts_parse(cfg
, argc
, argv
);
358 debug("argv0 = %s\n", argv0
);
360 if (cfg
->fifos
== NULL
)
361 usage("No fifo specs were given!\n");
363 /* 1st pass to check file existence and type */
364 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
365 if (lstat(f
->name
, &st
) < 0) {
366 error("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
370 if (!(st
.st_mode
& S_IFIFO
)) {
371 error("\"%s\" is not a FIFO\n", f
->name
);
377 fatal("Encountered errors with the given file paths. See log.\n");
379 width
= cfg
->total_width
;
380 seplen
= strlen(cfg
->separator
);
382 /* 2nd pass to make space for separators */
383 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
388 width
+= (seplen
* (nfifos
- 1));
389 buf
= calloc(1, width
+ 1);
391 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
392 memset(buf
, ' ', width
);
394 /* 3rd pass to set the separators */
395 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
396 if (f
->pos
) { /* Skip the first, left-most */
397 /* Copying only seplen ensures we omit the '\0' byte. */
398 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
402 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
403 fatal("XOpenDisplay failed with: %p\n", display
);
404 /* TODO: nanosleep and nano time diff */
405 /* TODO: Handle signals */
407 /* TODO: Check TTL and maybe blank-out */
408 /* TODO: How to trigger TTL check? On select? Alarm signal? */
409 /* TODO: Set timeout on read_all based on diff of last time of
410 * read_all and desired time of next TTL check.
413 if (cfg
->output_to_x_root_window
) {
414 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
415 fatal("XStoreName failed.\n");
This page took 0.086368 seconds and 4 git commands to generate.