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 file list to file array. */
24 typedef struct File File
;
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 file_print_one(File
*f
)
77 file_print_all(File
*head
)
79 for (File
*f
= head
; f
; f
= f
->next
) {
85 config_print(Config
*c
)
101 file_print_all(c
->files
);
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 file \"%s\".\n", argv
[i
]);
198 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
200 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
201 File
*f
= calloc(1, sizeof(struct File
));
208 f
->pos
= cfg
->total_width
;
209 f
->next
= cfg
->files
;
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 File
*last
= cfg
->files
;
238 for (File
*f
= last
; f
; ) {
239 File
*next
= f
->next
;
240 f
->next
= cfg
->files
;
247 read_one(File
*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 (File
*f
= cfg
->files
; 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 files. */
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 (File
*f
= cfg
->files
; 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
*cfg
= &defaults
;
317 Display
*display
= NULL
;
321 opts_parse(cfg
, argc
, argv
);
322 debug("argv0 = %s\n", argv0
);
324 if (cfg
->files
== NULL
)
325 usage("No file specs were given!\n");
327 width
= cfg
->total_width
;
328 seplen
= strlen(cfg
->separator
);
330 /* 1st pass to make space for separators */
331 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
336 width
+= (seplen
* (nfiles
- 1));
337 buf
= calloc(1, width
+ 1);
339 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
340 memset(buf
, ' ', width
);
342 /* 2nd pass to set the separators */
343 for (File
*f
= cfg
->files
; 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
);
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 */
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.
360 if (cfg
->output_to_x_root_window
) {
361 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
362 fatal("XStoreName failed.\n");
This page took 0.09006 seconds and 4 git commands to generate.