a41351a9b72842d7c9376216cf3c989441b962c3
3 #include <sys/select.h>
12 #define debug(args...) {fprintf(stderr, "[debug] " args);}
13 #define info( args...) {fprintf(stderr, "[info] " args);}
14 #define error(args...) {fprintf(stderr, "[error] " args);}
15 #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);}
16 #define usage(args...) {print_usage(); fatal("[usage] " args);}
20 typedef struct File File
;
27 int pos
; /* Position on the output buffer. */
31 typedef struct Config Config
;
47 file_print_one(File
*f
)
71 file_print_all(File
*head
)
73 for (File
*f
= head
; f
; f
= f
->next
) {
79 config_print(Config
*c
)
95 file_print_all(c
->files
);
102 if (!isdigit(*(s
++)))
114 "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
116 " SPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
117 " FILE_PATH = string\n"
118 " DATA_WIDTH = int (* (positive) number of characters *)\n"
119 " DATA_TTL = int (* (positive) number of seconds *)\n"
120 " OPTION = -i INTERVAL\n"
122 " SEPARATOR = string\n"
123 " INTERVAL = int (* (positive) number of seconds *)\n"
129 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
135 void opts_parse_any(Config
*, int, char *[], int); /* For mutually-recursive calls. */
138 parse_opts_opt_i(Config
*cfg
, int argc
, char *argv
[], int i
)
141 char *param
= argv
[i
++];
143 if (is_pos_num(param
)) {
144 cfg
->interval
= atoi(param
);
145 opts_parse_any(cfg
, argc
, argv
, i
);
147 usage("Option -i parameter is invalid: \"%s\"\n", param
);
150 usage("Option -i parameter is missing.\n");
155 parse_opts_opt_s(Config
*cfg
, int argc
, char *argv
[], int i
)
158 cfg
->separator
= calloc((strlen(argv
[i
]) + 1), sizeof(char));
159 strcpy(cfg
->separator
, argv
[i
]);
160 opts_parse_any(cfg
, argc
, argv
, ++i
);
162 usage("Option -s parameter is missing.\n");
167 parse_opts_opt(Config
*cfg
, int argc
, char *argv
[], int i
)
169 switch (argv
[i
][1]) {
170 case 'i': parse_opts_opt_i(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_int */
171 case 's': parse_opts_opt_s(cfg
, argc
, argv
, ++i
); break; /* TODO: Generic set_str */
172 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
177 parse_opts_spec(Config
*cfg
, int argc
, char *argv
[], int i
)
180 usage("[spec] Parameter(s) missing for file \"%s\".\n", argv
[i
]);
187 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
189 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
190 File
*f
= calloc(1, sizeof(struct File
));
197 f
->pos
= cfg
->total_width
;
198 f
->next
= cfg
->files
;
201 cfg
->total_width
+= f
->width
;
204 fatal("[memory] Allocation failure.");
206 opts_parse_any(cfg
, argc
, argv
, i
);
210 opts_parse_any(Config
*cfg
, int argc
, char *argv
[], int i
)
213 switch (argv
[i
][0]) {
214 case '-': parse_opts_opt(cfg
, argc
, argv
, i
); break;
215 default : parse_opts_spec(cfg
, argc
, argv
, i
);
221 opts_parse(Config
*cfg
, int argc
, char *argv
[], int i
)
223 opts_parse_any(cfg
, argc
, argv
, 1);
225 File
*last
= cfg
->files
;
227 for (File
*f
= last
; f
; ) {
228 File
*next
= f
->next
;
229 f
->next
= cfg
->files
;
236 read_one(File
*f
, char *buf
)
242 memset(b
, ' ', f
->width
);
243 while ((n
= read(f
->fd
, b
, f
->width
)) > 0) {
245 debug("read %zd from %s\n", n
, f
->name
);
249 if (*(b
- 1) == '\n')
253 "Failed to read: \"%s\". Error: %s\n",
264 read_all(Config
*cfg
, char *buf
)
272 /* TODO: stat then check TTL */
273 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
274 debug("opening: %s\n", f
->name
);
276 f
->fd
= open(f
->name
, O_RDONLY
| O_NONBLOCK
);
278 /* TODO: Consider backing off retries for failed files. */
279 fatal("Failed to open \"%s\"\n", f
->name
);
284 debug("selecting...\n");
285 ready
= select(maxfd
+ 1, &fds
, NULL
, NULL
, NULL
);
286 debug("ready: %d\n", ready
);
289 fatal("%s", strerror(errno
));
290 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
291 if (FD_ISSET(f
->fd
, &fds
)) {
292 debug("reading: %s\n", f
->name
);
299 main(int argc
, char *argv
[])
306 Config
*cfg
= &defaults
;
310 opts_parse(cfg
, argc
, argv
, 1);
311 debug("argv0 = %s\n", argv0
);
313 if (cfg
->files
== NULL
)
314 usage("No file specs were given!\n");
316 width
= cfg
->total_width
;
317 seplen
= strlen(cfg
->separator
);
319 /* 1st pass to make space for separators */
320 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
325 width
+= (seplen
* (nfiles
- 1));
326 buf
= calloc(1, width
+ 1);
328 fatal("[memory] Failed to allocate buffer of %d bytes", width
);
329 memset(buf
, ' ', width
);
331 /* 2nd pass to set the separators */
332 for (File
*f
= cfg
->files
; f
; f
= f
->next
) {
333 if (f
->pos
) { /* Skip the first, left-most */
334 /* Copying only seplen ensures we omit the '\0' byte. */
335 strncpy(buf
+ (f
->pos
- seplen
), cfg
->separator
, seplen
);
340 /* TODO: nanosleep and nano time diff */
This page took 0.157946 seconds and 4 git commands to generate.