File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / ftpconf.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 3 months ago) by misho
Branches: confuse, MAIN
CVS tags: v3_3, HEAD
confuse 3.3

    1: /*
    2:  * Parses and prints the configuration options for a fictous ftp client
    3:  */
    4: 
    5: #include <stdio.h>
    6: #include <string.h>
    7: #include <errno.h>
    8: #include <locale.h>
    9: #include <confuse.h>
   10: 
   11: /* valid values for the auto-create-bookmark option */
   12: #define ACB_YES 1
   13: #define ACB_NO 2
   14: #define ACB_ASK 3
   15: 
   16: /* called on alias() functions in the config file */
   17: int conf_alias(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
   18: {
   19: 	if (argc < 2) {
   20: 		cfg_error(cfg, "function '%s' requires 2 arguments", cfg_opt_name(opt));
   21: 		return -1;
   22: 	}
   23: 	printf("got alias '%s' = '%s'\n", argv[0], argv[1]);
   24: 	return 0;
   25: }
   26: 
   27: /* parse values for the auto-create-bookmark option */
   28: int conf_parse_acb(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
   29: {
   30: 	if (strcmp(value, "yes") == 0)
   31: 		*(int *)result = ACB_YES;
   32: 	else if (strcmp(value, "no") == 0)
   33: 		*(int *)result = ACB_NO;
   34: 	else if (strcmp(value, "ask") == 0)
   35: 		*(int *)result = ACB_ASK;
   36: 	else {
   37: 		cfg_error(cfg, "invalid value for option '%s': %s", cfg_opt_name(opt), value);
   38: 		return -1;
   39: 	}
   40: 	return 0;
   41: }
   42: 
   43: /* validates a port option (must be positive) */
   44: int conf_validate_port(cfg_t *cfg, cfg_opt_t *opt)
   45: {
   46: 	int value = cfg_opt_getnint(opt, 0);
   47: 
   48: 	if (value <= 0) {
   49: 		cfg_error(cfg, "invalid port %d in section '%s'", value, cfg_name(cfg));
   50: 		return -1;
   51: 	}
   52: 	return 0;
   53: }
   54: 
   55: /* validates a bookmark section (host option required) */
   56: int conf_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt)
   57: {
   58: 	cfg_t *bookmark = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
   59: 
   60: 	if (cfg_size(bookmark, "host") == 0) {
   61: 		cfg_error(cfg, "missing required option 'host' in bookmark");
   62: 		return -1;
   63: 	}
   64: 	return 0;
   65: }
   66: 
   67: cfg_t *parse_conf(const char *filename)
   68: {
   69: 	static cfg_opt_t bookmark_opts[] = {
   70: 		CFG_STR("host", 0, CFGF_NODEFAULT),
   71: 		CFG_INT("port", 21, CFGF_NONE),
   72: 		CFG_STR("login", "anonymous", CFGF_NONE),
   73: 		CFG_STR("password", "anonymous@", CFGF_NONE),
   74: 		CFG_STR("directory", 0, CFGF_NONE),
   75: 		CFG_END()
   76: 	};
   77: 
   78: 	cfg_opt_t opts[] = {
   79: 		CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE),
   80: 		CFG_BOOL("passive-mode", cfg_false, CFGF_NONE),
   81: 		CFG_BOOL("remote-completion", cfg_true, CFGF_NONE),
   82: 		CFG_FUNC("alias", conf_alias),
   83: 		CFG_STR_LIST("xterm-terminals", "{xterm, rxvt}", CFGF_NONE),
   84: 		CFG_INT_CB("auto-create-bookmark", ACB_YES, CFGF_NONE, conf_parse_acb),
   85: 		CFG_FUNC("include-file", cfg_include),
   86: 		CFG_END()
   87: 	};
   88: 
   89: 	cfg_t *cfg = cfg_init(opts, CFGF_NONE);
   90: 
   91: 	cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port);
   92: 	cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark);
   93: 
   94: 	switch (cfg_parse(cfg, filename)) {
   95: 	case CFG_FILE_ERROR:
   96: 		printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno));
   97: 		printf("continuing with default values...\n\n");
   98: 	case CFG_SUCCESS:
   99: 		break;
  100: 	case CFG_PARSE_ERROR:
  101: 		return 0;
  102: 	}
  103: 
  104: 	return cfg;
  105: }
  106: 
  107: /* Parse the file ftp.conf and print the parsed configuration options */
  108: int main(int argc, char **argv)
  109: {
  110: 	cfg_t *cfg;
  111: 
  112: 	/* Localize messages & types according to environment, since v2.9 */
  113: #ifdef LC_MESSAGES
  114: 	setlocale(LC_MESSAGES, "");
  115: 	setlocale(LC_CTYPE, "");
  116: #endif
  117: 
  118: 	cfg = parse_conf(argc > 1 ? argv[1] : "ftp.conf");
  119: 	if (cfg) {
  120: 		unsigned int i;
  121: 
  122: 		printf("passive-mode = %s\n", cfg_getbool(cfg, "passive-mode") ? "true" : "false");
  123: 		printf("remote-completion = %s\n", cfg_getbool(cfg, "remote-completion") ? "true" : "false");
  124: 
  125: 		printf("number of bookmarks: %d\n", cfg_size(cfg, "bookmark"));
  126: 		for (i = 0; i < cfg_size(cfg, "bookmark"); i++) {
  127: 			cfg_t *bookmark = cfg_getnsec(cfg, "bookmark", i);
  128: 
  129: 			printf("  bookmark #%d: %s:%s@%s:%ld%s\n", i + 1,
  130: 			       cfg_getstr(bookmark, "login"),
  131: 			       cfg_getstr(bookmark, "password"),
  132: 			       cfg_getstr(bookmark, "host"), cfg_getint(bookmark, "port"), cfg_getstr(bookmark, "directory"));
  133: 		}
  134: 
  135: 		for (i = 0; i < cfg_size(cfg, "xterm-terminals"); i++) {
  136: 			printf("xterm-terminal #%d: %s\n", i + 1, cfg_getnstr(cfg, "xterm-terminals", i));
  137: 		}
  138: 
  139: 		printf("auto-create-bookmark = %ld\n", cfg_getint(cfg, "auto-create-bookmark"));
  140: 		cfg_free(cfg);
  141: 	}
  142: 
  143: 	return 0;
  144: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>