confuse: ftpconf.c

- Main Page
- Data Structures
- Files
- Examples

# ftpconf.c

    00001 /*
    00002  * Parses and prints the configuration options for a fictous ftp client
    00003  */
    00004
    00005 #include <stdio.h>
    00006 #include <string.h>
    00007 #include <errno.h>
    00008 #include <confuse.h>
    00009
    00010 /* valid values for the auto-create-bookmark option */
    00011 #define ACB_YES 1
    00012 #define ACB_NO 2
    00013 #define ACB_ASK 3
    00014
    00015 /* called on alias() functions in the config file */
    00016 int conf_alias(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
    00017 {
    00018     if(argc < 2)
    00019     {
    00020         cfg_error(cfg, "function '%s' requires 2 arguments", cfg_opt_name(opt));
    00021         return -1;
    00022     }
    00023     printf("got alias '%s' = '%s'\n", argv[0], argv[1]);
    00024     return 0;
    00025 }
    00026
    00027 /* parse values for the auto-create-bookmark option */
    00028 int conf_parse_acb(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
    00029 {
    00030     if(strcmp(value, "yes") == 0)
    00031         *(int *)result = ACB_YES;
    00032     else if(strcmp(value, "no") == 0)
    00033         *(int *)result = ACB_NO;
    00034     else if(strcmp(value, "ask") == 0)
    00035         *(int *)result = ACB_ASK;
    00036     else
    00037     {
    00038         cfg_error(cfg, "invalid value for option '%s': %s",
    00039         cfg_opt_name(opt), value);
    00040         return -1;
    00041     }
    00042     return 0;
    00043 }
    00044
    00045 /* validates a port option (must be positive) */
    00046 int conf_validate_port(cfg_t *cfg, cfg_opt_t *opt)
    00047 {
    00048     int value = cfg_opt_getnint(opt, 0);
    00049     if(value <= 0)
    00050     {
    00051         cfg_error(cfg, "invalid port %d in section '%s'", value, cfg_name(cfg));
    00052         return -1;
    00053     }
    00054     return 0;
    00055 }
    00056
    00057 /* validates a bookmark section (host option required) */
    00058 int conf_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt)
    00059 {
    00060     cfg_t *bookmark = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
    00061     if(cfg_size(bookmark, "host") == 0)
    00062     {
    00063         cfg_error(cfg, "missing required option 'host' in bookmark");
    00064         return -1;
    00065     }
    00066     return 0;
    00067 }
    00068
    00069 cfg_t *parse_conf(const char *filename)
    00070 {
    00071     cfg_opt_t bookmark_opts[] = {
    00072         CFG_STR("host", 0, CFGF_NODEFAULT),
    00073         CFG_INT("port", 21, CFGF_NONE),
    00074         CFG_STR("login", "anonymous", CFGF_NONE),
    00075         CFG_STR("password", "anonymous@", CFGF_NONE),
    00076         CFG_STR("directory", 0, CFGF_NONE),
    00077         CFG_END()
    00078     };
    00079
    00080     cfg_opt_t opts[] = {
    00081         CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE),
    00082         CFG_BOOL("passive-mode", cfg_false, CFGF_NONE),
    00083         CFG_BOOL("remote-completion", cfg_true, CFGF_NONE),
    00084         CFG_FUNC("alias", conf_alias),
    00085         CFG_STR_LIST("xterm-terminals", "{xterm, rxvt}", CFGF_NONE),
    00086         CFG_INT_CB("auto-create-bookmark", ACB_YES, CFGF_NONE, conf_parse_acb),
    00087         CFG_FUNC("include-file", cfg_include),
    00088         CFG_END()
    00089     };
    00090
    00091     cfg_t *cfg = cfg_init(opts, CFGF_NONE);
    00092     cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port);
    00093     cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark);
    00094
    00095     switch(cfg_parse(cfg, filename))
    00096     {
    00097         case CFG_FILE_ERROR:
    00098             printf("warning: configuration file '%s' could not be read: %s\n",
    00099                     filename, strerror(errno));
    00100             printf("continuing with default values...\n\n");
    00101         case CFG_SUCCESS:
    00102             break;
    00103         case CFG_PARSE_ERROR:
    00104             return 0;
    00105     }
    00106
    00107     return cfg;
    00108 }
    00109
    00110 int main(int argc, char **argv)
    00111 {
    00112     int i;
    00113     cfg_t *cfg = parse_conf(argc > 1 ? argv[1] : "ftp.conf");
    00114
    00115     /* print the parsed configuration options */
    00116     if(cfg)
    00117     {
    00118         printf("passive-mode = %s\n",
    00119         cfg_getbool(cfg, "passive-mode") ? "true" : "false");
    00120         printf("remote-completion = %s\n",
    00121         cfg_getbool(cfg, "remote-completion") ? "true" : "false");
    00122
    00123         printf("number of bookmarks: %d\n", cfg_size(cfg, "bookmark"));
    00124         for(i = 0; i < cfg_size(cfg, "bookmark"); i++)
    00125         {
    00126             cfg_t *bookmark = cfg_getnsec(cfg, "bookmark", i);
    00127             printf("  bookmark #%d: %s:%s@%s:%ld%s\n", i+1,
    00128                     cfg_getstr(bookmark, "login"),
    00129                     cfg_getstr(bookmark, "password"),
    00130                     cfg_getstr(bookmark, "host"),
    00131                     cfg_getint(bookmark, "port"),
    00132                     cfg_getstr(bookmark, "directory"));
    00133         }
    00134
    00135         for(i = 0; i < cfg_size(cfg, "xterm-terminals"); i++)
    00136     {
    00137             printf("xterm-terminal #%d: %s\n",
    00138         i+1, cfg_getnstr(cfg, "xterm-terminals", i));
    00139     }
    00140
    00141         printf("auto-create-bookmark = %ld\n",
    00142         cfg_getint(cfg, "auto-create-bookmark"));
    00143         cfg_free(cfg);
    00144     }
    00145
    00146     return 0;
    00147 }
    00148