4 #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
;
50 file_print_one(File
*f
)
74 file_print_all(File
*head
)
76 for (File
*f
= head
; f
; f
= f
->next
) {
82 config_print(Config
*c
)
98 file_print_all(c
->files
);
105 if (!isdigit(*(s
++)))
117 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
119 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
120 " FILE_PATH = string\n"
121 " DATA_WIDTH = int (* (positive) number of characters *)\n"
122 " DATA_TTL = int (* (positive) number of seconds *)\n"
123 " OPTION = -i INTERVAL\n"
125 " SEPARATOR = string\n"
126 " INTERVAL = int (* (positive) number of seconds *)\n"
132 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
138 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
141 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
144 char *param
= argv
[i
++];
146 if (is_pos_num(param
)) {
147 cfg
->interval
= atoi(param
);
148 opts_parse_any(cfg
, argc
, argv
, i
);
150 usage("Option -i parameter is invalid: \"%s\"\n", param
);
153 usage("Option -i parameter is missing.\n");
158 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
161 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
162 strcpy(cfg
->separator
, argv
[i
]);
163 opts_parse_any(cfg
, argc
, argv
, ++i
);
165 usage("Option -s parameter is missing.\n");
170 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
172 switch (argv
[i
][1]) {
173 case 'i': parse_opts_opt_i(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_int */
174 case 's': parse_opts_opt_s(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_str */
175 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
180 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
183 usage("[spec] Parameter(s) missing for file \"%s\".\n", argv
[i
]);
190 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
192 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
193 File
*f
= calloc(1, sizeof(struct File
));
200 f
->pos
= cfg
->total_width
;
201 f
->next
= cfg
->files
;
204 cfg
->total_width
+= f
->width
;
207 fatal("[memory] Allocation failure.");
209 opts_parse_any(cfg
, argc
, argv
, i
);
213 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
216 switch (argv
[i
][0]) {
217 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
218 default : parse_opts_spec(cfg
, argc
, argv
, i
);
224 opts_parse(Config
*cfg
, int argc
, char *argv
[], int i
)
226 opts_parse_any(cfg
, argc
, argv
, 1);
228 File
*last
= cfg
->files
;
230 for (File
*f
= last
; f
; ) {
231 File
*next
= f
->next
;
232 f
->next
= cfg
->files
;
239 read_one(File
*f
, char *buf
)
250 memset(b
, ' ', f
->width
);
251 while ((current
= read(f
->fd
, &c
, 1)) && c
!= '\n' && c
!= '\0' && total
++ < f
->width
)
254 error("Failed to read: \"%s\". Error: %s\n", f
->name
, strerror(errno
));
255 /* TODO Record timestamp read */
261 read_all(Config
*cfg
, char *buf
)
270 /* TODO: Check TTL */
271 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
272 /* TODO: Create the FIFO if it doesn't already exist. */
273 if (lstat(f
->name
, &st
) < 0)
274 fatal("Cannot stat \"%s\". Error: %s\n", f
->name
, strerror(errno
));
275 if (!(st
.st_mode
& S_IFIFO
))
276 fatal("\"%s\" is not a FIFO\n", f
->name
);
277 debug("opening: %s\n", f
->name
);
279 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
281 /* TODO: Consider backing off retries for failed files. */
282 fatal("Failed to open \"%s\"\n", f
->name
);
287 debug("selecting...\n");
288 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
289 debug("ready: %d\n", ready
);
292 fatal("%s", strerror(errno
));
293 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
294 if (FD_ISSET(f
->fd
, &fds
)) {
295 debug("reading: %s\n", f
->name
);
302 main(int argc
, char *argv
[])
309 Config
*cfg
= &defaults
;
313 opts_parse(cfg
, argc
, argv
, 1);
314 debug("argv0 = %s\n", argv0
);
316 if (cfg
->files
== NULL
)
317 usage("No file specs were given!\n");
319 width
= cfg
->total_width
;
320 seplen
= strlen(cfg
->separator
);
322 /* 1st pass to make space for separators */
323 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
328 width
+= (seplen
* (nfiles
- 1));
329 buf
= calloc(1, width
+ 1);
331 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
332 memset(buf
, ' ', width
);
334 /* 2nd pass to set the separators */
335 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
336 if (f
->pos
) { /* Skip the first, left-most */
337 /* Copying only seplen ensures we omit the '\0' byte. */
338 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
343 /* TODO: nanosleep and nano time diff */
345 /* TODO: Check TTL and maybe blank-out */
346 /* TODO: How to trigger TTL check? On select? Alarm signal? */
347 /* TODO: Option to set X root window title or print */
This page took 0.14579 seconds and 4 git commands to generate.