liboptcfg

annotate src/optcfg.c @ 1:8fd2858c6a29

example (test) program
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Nov 2015 14:12:30 +0200
parents 10b89befcaa9
children 9c73004c7af3
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@0 162 memcpy(buf, line, len);
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@0 191 char *optcfg_next_value(struct optcfg *oc)
nuclear@0 192 {
nuclear@0 193 if(oc->argidx) { /* we're in the middle of parsing arguments, so get the next one */
nuclear@0 194 return oc->argv[++oc->argidx];
nuclear@0 195 }
nuclear@0 196 if(oc->cfg_value) {
nuclear@0 197 char *val = oc->cfg_value;
nuclear@0 198 oc->cfg_value = 0;
nuclear@0 199 return val;
nuclear@0 200 }
nuclear@0 201 return 0;
nuclear@0 202 }
nuclear@0 203
nuclear@0 204 void optcfg_print_options(struct optcfg *oc)
nuclear@0 205 {
nuclear@0 206 int i;
nuclear@0 207 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@0 208 struct optcfg_option *opt = oc->optlist + i;
nuclear@0 209
nuclear@0 210 if(opt->c) {
nuclear@0 211 printf(" -%c", opt->c);
nuclear@0 212 } else {
nuclear@0 213 printf(" ");
nuclear@0 214 }
nuclear@0 215 if(opt->s) {
nuclear@0 216 printf("%c-%s: ", opt->c ? ',' : ' ', opt->s);
nuclear@0 217 } else {
nuclear@0 218 printf(": ");
nuclear@0 219 }
nuclear@0 220 printf("%s\n", opt->desc ? opt->desc : "undocumented");
nuclear@0 221 }
nuclear@0 222 }
nuclear@0 223
nuclear@0 224 int optcfg_bool_value(char *s, int *valret)
nuclear@0 225 {
nuclear@0 226 if(strcasecmp(s, "yes") == 0 || strcasecmp(s, "true") == 0 || strcmp(s, "1") == 0) {
nuclear@0 227 *valret = 1;
nuclear@0 228 return 0;
nuclear@0 229 }
nuclear@0 230 if(strcasecmp(s, "no") == 0 || strcasecmp(s, "false") == 0 || strcmp(s, "0") == 0) {
nuclear@0 231 *valret = 0;
nuclear@0 232 return 0;
nuclear@0 233 }
nuclear@0 234 return -1;
nuclear@0 235 }
nuclear@0 236
nuclear@0 237 int optcfg_int_value(char *str, int *valret)
nuclear@0 238 {
nuclear@0 239 char *endp;
nuclear@0 240 *valret = strtol(str, &endp, 0);
nuclear@0 241 if(endp == str) {
nuclear@0 242 return -1;
nuclear@0 243 }
nuclear@0 244 return 0;
nuclear@0 245 }
nuclear@0 246
nuclear@0 247 int optcfg_float_value(char *str, float *valret)
nuclear@0 248 {
nuclear@0 249 char *endp;
nuclear@0 250 *valret = strtod(str, &endp);
nuclear@0 251 if(endp == str) {
nuclear@0 252 return -1;
nuclear@0 253 }
nuclear@0 254 return 0;
nuclear@0 255 }
nuclear@0 256
nuclear@0 257
nuclear@0 258
nuclear@0 259 static int get_opt(struct optcfg *oc, const char *arg)
nuclear@0 260 {
nuclear@1 261 int i, ndashes = 0;
nuclear@0 262
nuclear@1 263 while(*arg && *arg == '-') {
nuclear@1 264 ndashes++;
nuclear@1 265 arg++;
nuclear@0 266 }
nuclear@0 267
nuclear@1 268 if(ndashes > 2) {
nuclear@1 269 arg -= ndashes;
nuclear@1 270 }
nuclear@1 271
nuclear@1 272 if(arg[1]) { /* match long options */
nuclear@0 273 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@1 274 if(strcmp(arg, oc->optlist[i].s) == 0) {
nuclear@0 275 return i;
nuclear@0 276 }
nuclear@0 277 }
nuclear@0 278 } else {
nuclear@0 279 for(i=0; oc->optlist[i].opt != -1; i++) {
nuclear@1 280 if(arg[0] == oc->optlist[i].c) {
nuclear@0 281 return i;
nuclear@0 282 }
nuclear@0 283 }
nuclear@0 284 }
nuclear@0 285 return -1;
nuclear@0 286 }
nuclear@0 287
nuclear@0 288 static char *skip_spaces(char *s)
nuclear@0 289 {
nuclear@0 290 while(*s && isspace(*s)) ++s;
nuclear@0 291 return s;
nuclear@0 292 }
nuclear@0 293
nuclear@0 294 static void strip_comments(char *s)
nuclear@0 295 {
nuclear@0 296 while(*s && *s != '#') ++s;
nuclear@0 297 if(*s == '#') *s = 0;
nuclear@0 298 }
nuclear@0 299
nuclear@0 300 static void strip_trailing_spaces(char *s)
nuclear@0 301 {
nuclear@0 302 char *end = s + strlen(s) - 1;
nuclear@0 303 while(end >= s && isspace(*end)) {
nuclear@0 304 *end-- = 0;
nuclear@0 305 }
nuclear@0 306 }
nuclear@0 307
nuclear@0 308 static char *parse_keyval(char *line)
nuclear@0 309 {
nuclear@0 310 char *val;
nuclear@0 311 char *eq = strchr(line, '=');
nuclear@0 312 if(!eq) return 0;
nuclear@0 313
nuclear@0 314 *eq = 0;
nuclear@0 315 strip_trailing_spaces(line);
nuclear@0 316 val = skip_spaces(eq + 1);
nuclear@0 317
nuclear@0 318 if(!*line || !*val) {
nuclear@0 319 return 0;
nuclear@0 320 }
nuclear@0 321 return val;
nuclear@0 322 }