7 #define MAX_TOT_WIDTH 1024
9 #define debug( args...) {fprintf(stderr, "[debug] " args);}
10 #define error( args...) {fprintf(stderr, "[error] " args); errors++;}
11 #define fatal(n, args...) {fprintf(stderr, "[fatal] " args); exit(n);}
12 #define usage( args...) {print_usage(); fatal(1, "[usage] " args);}
15 typedef struct file
* File
;
34 typedef struct options
* Options
;
36 Options opts
= &options
;
56 "Usage: %s [OPTIONS ...] SPEC [SPEC ...]\n"
58 "\tSPEC = FILE_PATH DATA_WIDTH DATA_TTL\n"
59 "\tFILE_PATH = string\n"
60 "\tDATA_WIDTH = int (* (positive) number of characters *)\n"
61 "\tDATA_TTL = int (* (positive) number of seconds *)\n"
62 "\tOPTION = -i INTERVAL\n"
63 "\tINTERVAL = int (* (positive) number of seconds *)\n"
69 "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
75 void parse_opts(int, char *[], int); /* For mutually-recursive calls. */
78 parse_opts_opt_i(int argc
, char *argv
[], int i
)
81 char *param
= argv
[i
++];
83 if (is_pos_num(param
)) {
84 opts
->interval
= atoi(param
);
85 parse_opts(argc
, argv
, i
);
87 usage("Option -i parameter is invalid: \"%s\"\n", param
);
90 usage("Option -i parameter is missing.\n");
95 parse_opts_opt(int argc
, char *argv
[], int i
)
98 case 'i': parse_opts_opt_i(argc
, argv
, ++i
); break; /* TODO: Generic set_int */
99 default : usage("Option \"%s\" is invalid\n", argv
[i
]);
104 parse_opts_spec(int argc
, char *argv
[], int i
)
107 usage("[spec] Parameter(s) missing for file \"%s\".\n", argv
[i
]);
114 usage("[spec] Invalid width: \"%s\", for file \"%s\"\n", w
, n
);
116 usage("[spec] Invalid TTL: \"%s\", for file \"%s\"\n", t
, n
);
117 File f
= malloc(sizeof(struct file
));
122 f
->next
= opts
->files
;
125 fatal(2, "[memory] Allocation failure.");
127 parse_opts(argc
, argv
, i
);
131 parse_opts(int argc
, char *argv
[], int i
)
134 switch (argv
[i
][0]) {
135 case '-': parse_opts_opt(argc
, argv
, i
); break;
136 default : parse_opts_spec(argc
, argv
, i
);
142 main(int argc
, char **argv
)
144 opts
->argv0
= argv
[0];
145 parse_opts(argc
, argv
, 1);
147 debug("[options] argv0 = %s\n", opts
->argv0
);
148 debug("[options] interval = %d\n", opts
->interval
);
149 if (opts
->files
== NULL
)
150 usage("No file specs were given!\n");
151 for (File f
= opts
->files
; f
; f
= f
->next
) {
153 "[options] file = { name = %s, width = %d, ttl = %d }\n",
This page took 0.05522 seconds and 4 git commands to generate.