File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / ftpconf.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Tue Jan 24 14:48:56 2017 UTC (7 years, 5 months ago) by misho
CVS tags: MAIN, HEAD
Initial revision

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

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