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);}
23 /* TODO: Convert fifo list to fifo array. */
24 typedef struct Fifo Fifo
;
31 int pos
; /* Position on the output buffer. */
35 typedef struct Config Config
;
42 int output_to_x_root_window
;
49 .output_to_x_root_window
= 0,
53 fifo_print_one(Fifo
*f
)
77 fifo_print_all(Fifo
*head
)
79 for (Fifo
*f
= head
; f
; f
= f
->next
) {
85 config_print(Config
*c
)
101 fifo_print_all(c
->fifos
);
108 if (!isdigit(*(s
++)))
120 "Usage: %s [OPTION ...] SPEC [SPEC ...]\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"
128 " SEPARATOR = string\n"
129 " INTERVAL = int (* (positive) number of seconds *)\n"
135 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
141 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
144 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
147 char *param
= argv
[i
++];
149 if (is_pos_num(param
)) {
150 cfg
->interval
= atoi(param
);
151 opts_parse_any(cfg
, argc
, argv
, i
);
153 usage("Option -i parameter is invalid: \"%s\"\n", param
);
156 usage("Option -i parameter is missing.\n");
161 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
164 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
165 strcpy(cfg
->separator
, argv
[i
]);
166 opts_parse_any(cfg
, argc
, argv
, ++i
);
168 usage("Option -s parameter is missing.\n");
173 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
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 */
179 cfg
->output_to_x_root_window
= 1;
180 opts_parse_any(cfg
, argc
, argv
, ++i
);
183 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
188 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
191 usage("[spec] Parameter(s) missing for fifo \"%s\".\n", argv
[i
]);
198 usage("[spec] Invalid width: \"%s\", for fifo \"%s\"\n", w
, n
);
200 usage("[spec] Invalid TTL: \"%s\", for fifo \"%s\"\n", t
, n
);
201 Fifo
*f
= calloc(1, sizeof(struct Fifo
));
208 f
->pos
= cfg
->total_width
;
209 f
->next
= cfg
->fifos
;
212 cfg
->total_width
+= f
->width
;
215 fatal("[memory] Allocation failure.");
217 opts_parse_any(cfg
, argc
, argv
, i
);
221 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
224 switch (argv
[i
][0]) {
225 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
226 default : parse_opts_spec(cfg
, argc
, argv
, i
);
232 opts_parse(Config
*cfg
, int argc
, char *argv
[])
234 opts_parse_any(cfg
, argc
, argv
, 1);
236 Fifo
*last
= cfg
->fifos
;
238 for (Fifo
*f
= last
; f
; ) {
239 Fifo
*next
= f
->next
;
240 f
->next
= cfg
->fifos
;
247 read_one(Fifo
*f
, char *buf
)
258 memset(b
, ' ', f
->width
);
259 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
262 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
263 /* TODO Record timestamp read */
269 read_all(Config
*cfg
, char *buf
)
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
);
286 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
288 /* TODO: Consider backing off retries for failed fifos. */
289 fatal("Failed to open \"%s\"\n", f
->name
);
294 debug("selecting...\n");
295 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
296 debug("ready: %d\n", ready
);
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
);
309 main(int argc
, char *argv
[])
316 Config cfg0
= defaults
;
318 Display
*display
= NULL
;
322 opts_parse(cfg
, argc
, argv
);
323 debug("argv0 = %s\n", argv0
);
325 if (cfg
->fifos
== NULL
)
326 usage("No fifo specs were given!\n");
328 width
= cfg
->total_width
;
329 seplen
= strlen(cfg
->separator
);
331 /* 1st pass to make space for separators */
332 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
337 width
+= (seplen
* (nfifos
- 1));
338 buf
= calloc(1, width
+ 1);
340 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
341 memset(buf
, ' ', width
);
343 /* 2nd pass to set the separators */
344 for (Fifo
*f
= cfg
->fifos
; f
; f
= f
->next
) {
345 if (f
->pos
) { /* Skip the first, left-most */
346 /* Copying only seplen ensures we omit the '\0' byte. */
347 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
351 if (cfg
->output_to_x_root_window
&& !(display
= XOpenDisplay(NULL
)))
352 fatal("XOpenDisplay failed with: %p\n", display
);
353 /* TODO: nanosleep and nano time diff */
354 /* TODO: Handle signals */
356 /* TODO: Check TTL and maybe blank-out */
357 /* TODO: How to trigger TTL check? On select? Alarm signal? */
358 /* TODO: Set timeout on read_all based on diff of last time of
359 * read_all and desired time of next TTL check.
362 if (cfg
->output_to_x_root_window
) {
363 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
364 fatal("XStoreName failed.\n");
This page took 0.099744 seconds and 4 git commands to generate.