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 typedef struct File File
;
29 int pos
; /* Position on the output buffer. */
33 typedef struct Config Config
;
49 file_print_one(File
*f
)
73 file_print_all(File
*head
)
75 for (File
*f
= head
; f
; f
= f
->next
) {
81 config_print(Config
*c
)
97 file_print_all(c
->files
);
104 if (!isdigit(*(s
++)))
116 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
118 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
119 " FILE_PATH = string\n"
120 " DATA_WIDTH = int (* (positive) number of characters *)\n"
121 " DATA_TTL = int (* (positive) number of seconds *)\n"
122 " OPTION = -i INTERVAL\n"
124 " SEPARATOR = string\n"
125 " INTERVAL = int (* (positive) number of seconds *)\n"
131 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
137 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
140 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
143 char *param
= argv
[i
++];
145 if (is_pos_num(param
)) {
146 cfg
->interval
= atoi(param
);
147 opts_parse_any(cfg
, argc
, argv
, i
);
149 usage("Option -i parameter is invalid: \"%s\"\n", param
);
152 usage("Option -i parameter is missing.\n");
157 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
160 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
161 strcpy(cfg
->separator
, argv
[i
]);
162 opts_parse_any(cfg
, argc
, argv
, ++i
);
164 usage("Option -s parameter is missing.\n");
169 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
171 switch (argv
[i
][1]) {
172 case 'i': parse_opts_opt_i(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_int */
173 case 's': parse_opts_opt_s(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_str */
174 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
179 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
182 usage("[spec] Parameter(s) missing for file \"%s\".\n", argv
[i
]);
189 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
191 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
192 File
*f
= calloc(1, sizeof(struct File
));
199 f
->pos
= cfg
->total_width
;
200 f
->next
= cfg
->files
;
203 cfg
->total_width
+= f
->width
;
206 fatal("[memory] Allocation failure.");
208 opts_parse_any(cfg
, argc
, argv
, i
);
212 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
215 switch (argv
[i
][0]) {
216 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
217 default : parse_opts_spec(cfg
, argc
, argv
, i
);
223 opts_parse(Config
*cfg
, int argc
, char *argv
[], int i
)
225 opts_parse_any(cfg
, argc
, argv
, 1);
227 File
*last
= cfg
->files
;
229 for (File
*f
= last
; f
; ) {
230 File
*next
= f
->next
;
231 f
->next
= cfg
->files
;
238 read_one(File
*f
, char *buf
)
244 memset(b
, ' ', f
->width
);
245 /* TODO: Read upto \n or width */
246 while ((n
= read(f
->fd
, b
, f
->width
)) > 0) {
248 debug("read %zd from %s\n", n
, f
->name
);
252 if (*(b
- 1) == '\n')
256 "Failed to read: \"%s\". Error: %s\n",
267 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
;
319 opts_parse(cfg
, argc
, argv
, 1);
320 debug("argv0 = %s\n", argv0
);
322 if (cfg
->files
== NULL
)
323 usage("No file specs were given!\n");
325 width
= cfg
->total_width
;
326 seplen
= strlen(cfg
->separator
);
328 /* 1st pass to make space for separators */
329 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
334 width
+= (seplen
* (nfiles
- 1));
335 buf
= calloc(1, width
+ 1);
337 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
338 memset(buf
, ' ', width
);
340 /* 2nd pass to set the separators */
341 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
342 if (f
->pos
) { /* Skip the first, left-most */
343 /* Copying only seplen ensures we omit the '\0' byte. */
344 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
349 /* TODO: nanosleep and nano time diff */
This page took 0.137025 seconds and 4 git commands to generate.