liboptcfg

annotate src/optcfg.c @ 2:9c73004c7af3

works
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Nov 2015 17:05:39 +0200
parents 8fd2858c6a29
children a6f127f3408d
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <ctype.h>
nuclear@0 5 #ifdef _MSC_VER
nuclear@0 6 #include <malloc.h>
nuclear@0 7 #else
nuclear@0 8 #include <alloca.h>
nuclear@0 9 #endif
nuclear@0 10 #include "optcfg.h"
nuclear@0 11
nuclear@0 12 struct optcfg {
nuclear@0 13 struct optcfg_option *optlist;
nuclear@0 14
nuclear@0 15 optcfg_opt_callback opt_func;
nuclear@0 16 void *opt_cls;
nuclear@0 17 optcfg_arg_callback arg_func;
nuclear@0 18 void *arg_cls;
nuclear@0 19
nuclear@0 20 int err_abort;
nuclear@0 21
nuclear@0 22 /* argument parsing state */
nuclear@0 23 char **argv;
nuclear@0 24 int argidx;
nuclear@0 25
nuclear@0 26 /* config file parsing state */
nuclear@0 27 const char *cfg_fname;
nuclear@0 28 int cfg_nline;
nuclear@0 29 char *cfg_value;
nuclear@0 30 };
nuclear@0 31
nuclear@0 32 static int get_opt(struct optcfg *oc, const char *s);
nuclear@0 33 static char *skip_spaces(char *s);
nuclear@0 34 static void strip_comments(char *s);
nuclear@0 35 static void strip_trailing_spaces(char *s);
nuclear@0 36 static char *parse_keyval(char *line);
nuclear@0 37
nuclear@0 38
nuclear@0 39 struct optcfg *optcfg_init(struct optcfg_option *optv)
nuclear@0 40 {
nuclear@0 41 struct optcfg *oc;
nuclear@0 42
nuclear@0 43 if(!(oc = calloc(1, sizeof *oc))) {
nuclear@0 44 return 0;
nuclear@0 45 }
nuclear@0 46 oc->optlist = optv;
nuclear@0 47 oc->err_abort = 1;
nuclear@0 48 return oc;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void optcfg_destroy(struct optcfg *oc)
nuclear@0 52 {
nuclear@0 53 memset(oc, 0, sizeof *oc);
nuclear@0 54 free(oc);
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls)
nuclear@0 58 {
nuclear@0 59 oc->opt_func = func;
nuclear@0 60 oc->opt_cls = cls;
nuclear@0 61 }
nuclear@0 62
nuclear@0 63 void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls)
nuclear@0 64 {
nuclear@0 65 oc->arg_func = func;
nuclear@0 66 oc->arg_cls = cls;
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 void optcfg_set_error_action(struct optcfg *oc, int act)
nuclear@0 70 {
nuclear@0 71 if(act == OPTCFG_ERROR_FAIL) {
nuclear@0 72 oc->err_abort = 1;
nuclear@0 73 } else if(act == OPTCFG_ERROR_IGNORE) {
nuclear@0 74 oc->err_abort = 0;
nuclear@0 75 }
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 int optcfg_parse_args(struct optcfg *oc, int argc, char **argv)
nuclear@0 79 {
nuclear@0 80 int i;
nuclear@0 81
nuclear@0 82 oc->argv = argv;
nuclear@0 83
nuclear@0 84 for(i=1; i<argc; i++) {
nuclear@0 85 oc->argidx = i;
nuclear@0 86
nuclear@0 87 if(argv[i][0] == '-') {
nuclear@0 88 if(oc->opt_func) {
nuclear@0 89 int o = get_opt(oc, argv[i]);
nuclear@0 90 if(o == -1 || oc->opt_func(oc, o, oc->opt_cls) == -1) {
nuclear@0 91 if(oc->err_abort) {
nuclear@0 92 return -1;
nuclear@0 93 }
nuclear@0 94 }
nuclear@0 95 } else {
nuclear@0 96 fprintf(stderr, "unexpected option: %s\n", argv[i]);
nuclear@0 97 if(oc->err_abort) {
nuclear@0 98 return -1;
nuclear@0 99 }
nuclear@0 100 }
nuclear@0 101 } else {
nuclear@0 102 if(oc->arg_func) {
nuclear@0 103 if(oc->arg_func(oc, argv[i], oc->arg_cls) == -1) {
nuclear@0 104 if(oc->err_abort) {
nuclear@0 105 return -1;
nuclear@0 106 }
nuclear@0 107 }
nuclear@0 108 } else {
nuclear@0 109 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@0 110 if(oc->err_abort) {
nuclear@0 111 return -1;
nuclear@0 112 }
nuclear@0 113 }
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 i = oc->argidx;
nuclear@0 117 }
nuclear@0 118
nuclear@0 119 oc->argidx = 0; /* done parsing args */
nuclear@0 120 return 0;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 int optcfg_parse_config_file(struct optcfg *oc, const char *fname)
nuclear@0 124 {
nuclear@0 125 int res;
nuclear@0 126 FILE *fp = fopen(fname, "rb");
nuclear@0 127 if(!fp) {
nuclear@0 128 return -1;
nuclear@0 129 }
nuclear@0 130
nuclear@0 131 oc->cfg_fname = fname;
nuclear@0 132 res = optcfg_parse_config_stream(oc, fp);
nuclear@0 133 oc->cfg_fname = 0;
nuclear@0 134 fclose(fp);
nuclear@0 135 return res;
nuclear@0 136 }
nuclear@0 137
nuclear@0 138 int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp)
nuclear@0 139 {
nuclear@0 140 char buf[512];
nuclear@0 141
nuclear@0 142 oc->cfg_nline = 0;
nuclear@0 143 while(fgets(buf, sizeof buf, fp)) {
nuclear@0 144 ++oc->cfg_nline;
nuclear@0 145
nuclear@0 146 if(optcfg_parse_config_line(oc, buf) == -1) {
nuclear@0 147 if(oc->err_abort) {
nuclear@0 148 return -1;
nuclear@0 149 }
nuclear@0 150 }
nuclear@0 151 }
nuclear@0 152 return 0;
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 int optcfg_parse_config_line(struct optcfg *oc, const char *line)
nuclear@0 156 {
nuclear@0 157 int opt, len;
nuclear@0 158 char *start, *val, *buf;
nuclear@0 159
nuclear@0 160 len = strlen(line);
nuclear@0 161 buf = alloca(len + 1);
nuclear@2 162 memcpy(buf, line, len + 1);
nuclear@0 163
nuclear@0 164 start = skip_spaces(buf);
nuclear@0 165 strip_comments(start);
nuclear@0 166 strip_trailing_spaces(start);
nuclear@0 167 if(!*start) {
nuclear@0 168 return 0;
nuclear@0 169 }
nuclear@0 170
nuclear@0 171 if(!(val = parse_keyval(start))) {
nuclear@0 172 fprintf(stderr, "error parsing %s line %d: invalid syntax\n", oc->cfg_fname ? oc->cfg_fname : "", oc->cfg_nline);
nuclear@0 173 return -1;
nuclear@0 174 }
nuclear@0 175 oc->cfg_value = val;
nuclear@0 176 if((opt = get_opt(oc, start)) == -1) {
nuclear@0 177 fprintf(stderr, "error parsing %s line %d: unknown option: %s\n", oc->cfg_fname ? oc->cfg_fname : "",
nuclear@0 178 oc->cfg_nline, start);
nuclear@0 179 return -1;
nuclear@0 180 }
nuclear@0 181
nuclear@0 182 if(oc->opt_func) {
nuclear@0 183 if(oc->opt_func(oc, opt, oc->opt_cls) == -1) {
nuclear@0 184 return -1;
nuclear@0 185 }
nuclear@0 186 }
nuclear@0 187 oc->cfg_value = 0;
nuclear@0 188 return 0;
nuclear@0 189 }
nuclear@0 190
nuclear@2 191 int optcfg_enabled_value(struct optcfg *oc, int *enabledp)
nuclear@2 192 {
nuclear@2 193 if(oc->argidx) {
nuclear@2 194 *enabledp = 1; /* TODO take -no- prefix into account */
nuclear@2 195 } else {
nuclear@2 196 char *val = optcfg_next_value(oc);
nuclear@2 197 if(optcfg_bool_value(val, enabledp) == -1) {
nuclear@2 198 return -1;
nuclear@2 199 }
nuclear@2 200 }
nuclear@2 201 return 0;
nuclear@2 202 }
nuclear@2 203
nuclear@2 204
nuclear@0 205 char *optcfg_next_value(struct optcfg *oc)
nuclear@0 206 {
nuclear@0 207 if(oc->argidx) { /* we're in the middle of parsing arguments, so get the next one */
nuclear@0 208 return oc->argv[++oc->argidx];
nuclear@0 209 }
nuclear@0 210 if(oc->cfg_value) {
nuclear@0 211 char *val = oc->cfg_value;
nuclear@0 212 oc->cfg_value = 0;
nuclear@0 213 return val;
nuclear@0 214 }
nuclear@0 215 return 0;
nuclear@0 216 }
nuclear@0 217
nuclear@0 218 void optcfg_print_options(struct optcfg *oc)
nuclear@0 219 {
nuclear@0 220 int i;
nuclear@0 221 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@0 222 struct optcfg_option *opt = oc->optlist + i;
nuclear@0 223
nuclear@0 224 if(opt->c) {
nuclear@0 225 printf(" -%c", opt->c);
nuclear@0 226 } else {
nuclear@0 227 printf(" ");
nuclear@0 228 }
nuclear@0 229 if(opt->s) {
nuclear@0 230 printf("%c-%s: ", opt->c ? ',' : ' ', opt->s);
nuclear@0 231 } else {
nuclear@0 232 printf(": ");
nuclear@0 233 }
nuclear@0 234 printf("%s\n", opt->desc ? opt->desc : "undocumented");
nuclear@0 235 }
nuclear@0 236 }
nuclear@0 237
nuclear@0 238 int optcfg_bool_value(char *s, int *valret)
nuclear@0 239 {
nuclear@0 240 if(strcasecmp(s, "yes") == 0 || strcasecmp(s, "true") == 0 || strcmp(s, "1") == 0) {
nuclear@0 241 *valret = 1;
nuclear@0 242 return 0;
nuclear@0 243 }
nuclear@0 244 if(strcasecmp(s, "no") == 0 || strcasecmp(s, "false") == 0 || strcmp(s, "0") == 0) {
nuclear@0 245 *valret = 0;
nuclear@0 246 return 0;
nuclear@0 247 }
nuclear@0 248 return -1;
nuclear@0 249 }
nuclear@0 250
nuclear@0 251 int optcfg_int_value(char *str, int *valret)
nuclear@0 252 {
nuclear@0 253 char *endp;
nuclear@0 254 *valret = strtol(str, &endp, 0);
nuclear@0 255 if(endp == str) {
nuclear@0 256 return -1;
nuclear@0 257 }
nuclear@0 258 return 0;
nuclear@0 259 }
nuclear@0 260
nuclear@0 261 int optcfg_float_value(char *str, float *valret)
nuclear@0 262 {
nuclear@0 263 char *endp;
nuclear@0 264 *valret = strtod(str, &endp);
nuclear@0 265 if(endp == str) {
nuclear@0 266 return -1;
nuclear@0 267 }
nuclear@0 268 return 0;
nuclear@0 269 }
nuclear@0 270
nuclear@0 271
nuclear@0 272
nuclear@0 273 static int get_opt(struct optcfg *oc, const char *arg)
nuclear@0 274 {
nuclear@1 275 int i, ndashes = 0;
nuclear@0 276
nuclear@1 277 while(*arg && *arg == '-') {
nuclear@1 278 ndashes++;
nuclear@1 279 arg++;
nuclear@0 280 }
nuclear@0 281
nuclear@1 282 if(ndashes > 2) {
nuclear@1 283 arg -= ndashes;
nuclear@1 284 }
nuclear@1 285
nuclear@1 286 if(arg[1]) { /* match long options */
nuclear@0 287 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@1 288 if(strcmp(arg, oc->optlist[i].s) == 0) {
nuclear@0 289 return i;
nuclear@0 290 }
nuclear@0 291 }
nuclear@0 292 } else {
nuclear@0 293 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@1 294 if(arg[0] == oc->optlist[i].c) {
nuclear@0 295 return i;
nuclear@0 296 }
nuclear@0 297 }
nuclear@0 298 }
nuclear@0 299 return -1;
nuclear@0 300 }
nuclear@0 301
nuclear@0 302 static char *skip_spaces(char *s)
nuclear@0 303 {
nuclear@0 304 while(*s && isspace(*s)) ++s;
nuclear@0 305 return s;
nuclear@0 306 }
nuclear@0 307
nuclear@0 308 static void strip_comments(char *s)
nuclear@0 309 {
nuclear@0 310 while(*s && *s != '#') ++s;
nuclear@0 311 if(*s == '#') *s = 0;
nuclear@0 312 }
nuclear@0 313
nuclear@0 314 static void strip_trailing_spaces(char *s)
nuclear@0 315 {
nuclear@0 316 char *end = s + strlen(s) - 1;
nuclear@0 317 while(end >= s && isspace(*end)) {
nuclear@0 318 *end-- = 0;
nuclear@0 319 }
nuclear@0 320 }
nuclear@0 321
nuclear@0 322 static char *parse_keyval(char *line)
nuclear@0 323 {
nuclear@0 324 char *val;
nuclear@0 325 char *eq = strchr(line, '=');
nuclear@0 326 if(!eq) return 0;
nuclear@0 327
nuclear@0 328 *eq = 0;
nuclear@0 329 strip_trailing_spaces(line);
nuclear@0 330 val = skip_spaces(eq + 1);
nuclear@2 331 strip_trailing_spaces(val);
nuclear@0 332
nuclear@0 333 if(!*line || !*val) {
nuclear@0 334 return 0;
nuclear@0 335 }
nuclear@0 336 return val;
nuclear@0 337 }