3 #include <sys/select.h>
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);}
22 /* TODO: Convert file list to file array. */
23 typedef struct File File
;
30 int pos
; /* Position on the output buffer. */
34 typedef struct Config Config
;
41 int output_to_x_root_window
;
48 .output_to_x_root_window
= 0,
52 file_print_one(File
*f
)
76 file_print_all(File
*head
)
78 for (File
*f
= head
; f
; f
= f
->next
) {
84 config_print(Config
*c
)
100 file_print_all(c
->files
);
107 if (!isdigit(*(s
++)))
119 "Usage: %s [OPTION ...] SPEC [SPEC ...]\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"
127 " SEPARATOR = string\n"
128 " INTERVAL = int (* (positive) number of seconds *)\n"
134 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
140 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
143 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
146 char *param
= argv
[i
++];
148 if (is_pos_num(param
)) {
149 cfg
->interval
= atoi(param
);
150 opts_parse_any(cfg
, argc
, argv
, i
);
152 usage("Option -i parameter is invalid: \"%s\"\n", param
);
155 usage("Option -i parameter is missing.\n");
160 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
163 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
164 strcpy(cfg
->separator
, argv
[i
]);
165 opts_parse_any(cfg
, argc
, argv
, ++i
);
167 usage("Option -s parameter is missing.\n");
172 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
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 */
178 cfg
->output_to_x_root_window
= 1;
179 opts_parse_any(cfg
, argc
, argv
, ++i
);
182 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
187 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
190 usage("[spec] Parameter(s) missing for file \"%s\".\n", argv
[i
]);
197 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
199 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
200 File
*f
= calloc(1, sizeof(struct File
));
207 f
->pos
= cfg
->total_width
;
208 f
->next
= cfg
->files
;
211 cfg
->total_width
+= f
->width
;
214 fatal("[memory] Allocation failure.");
216 opts_parse_any(cfg
, argc
, argv
, i
);
220 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
223 switch (argv
[i
][0]) {
224 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
225 default : parse_opts_spec(cfg
, argc
, argv
, i
);
231 opts_parse(Config
*cfg
, int argc
, char *argv
[])
233 opts_parse_any(cfg
, argc
, argv
, 1);
235 File
*last
= cfg
->files
;
237 for (File
*f
= last
; f
; ) {
238 File
*next
= f
->next
;
239 f
->next
= cfg
->files
;
246 read_one(File
*f
, char *buf
)
257 memset(b
, ' ', f
->width
);
258 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
261 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
262 /* TODO Record timestamp read */
268 read_all(Config
*cfg
, char *buf
)
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
);
285 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
287 /* TODO: Consider backing off retries for failed files. */
288 fatal("Failed to open \"%s\"\n", f
->name
);
293 debug("selecting...\n");
294 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
295 debug("ready: %d\n", ready
);
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
);
308 main(int argc
, char *argv
[])
315 Config
*cfg
= &defaults
;
316 Display
*display
= NULL
;
320 opts_parse(cfg
, argc
, argv
);
321 debug("argv0 = %s\n", argv0
);
323 if (cfg
->files
== NULL
)
324 usage("No file specs were given!\n");
326 width
= cfg
->total_width
;
327 seplen
= strlen(cfg
->separator
);
329 /* 1st pass to make space for separators */
330 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
335 width
+= (seplen
* (nfiles
- 1));
336 buf
= calloc(1, width
+ 1);
338 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
339 memset(buf
, ' ', width
);
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
);
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 */
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.
359 if (cfg
->output_to_x_root_window
) {
360 if (XStoreName(display
, DefaultRootWindow(display
), buf
) < 0)
361 fatal("XStoreName failed.\n");
This page took 0.101451 seconds and 4 git commands to generate.