<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>confuse: ftpconf.c</title> <link rel="stylesheet" href="tutorial.css" type="text/css"> </head> <body> <div class="main"> <!-- doxygen-header.html ends here --> <!-- Generated by Doxygen 1.5.8 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>ftpconf.c</h1><div class="fragment"><pre class="fragment"><span class="comment">/*</span> <span class="comment"> * Parses and prints the configuration options for a fictous ftp client</span> <span class="comment"> */</span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <string.h></span> <span class="preprocessor">#include <errno.h></span> <span class="preprocessor">#include <<a class="code" href="confuse_8h.html" title="A configuration file parser library.">confuse.h</a>></span> <span class="comment">/* valid values for the auto-create-bookmark option */</span> <span class="preprocessor">#define ACB_YES 1</span> <span class="preprocessor"></span><span class="preprocessor">#define ACB_NO 2</span> <span class="preprocessor"></span><span class="preprocessor">#define ACB_ASK 3</span> <span class="preprocessor"></span> <span class="comment">/* called on alias() functions in the config file */</span> <span class="keywordtype">int</span> conf_alias(<a name="_a0"></a><a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg, <a name="_a1"></a><a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> *opt, <span class="keywordtype">int</span> argc, <span class="keyword">const</span> <span class="keywordtype">char</span> **argv) { <span class="keywordflow">if</span>(argc < 2) { <a name="a2"></a>cfg_error(cfg, <span class="stringliteral">"function '%s' requires 2 arguments"</span>, <a name="a3"></a>cfg_opt_name(opt)); <span class="keywordflow">return</span> -1; } printf(<span class="stringliteral">"got alias '%s' = '%s'\n"</span>, argv[0], argv[1]); <span class="keywordflow">return</span> 0; } <span class="comment">/* parse values for the auto-create-bookmark option */</span> <span class="keywordtype">int</span> conf_parse_acb(<a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg, <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> *opt, <span class="keyword">const</span> <span class="keywordtype">char</span> *value, <span class="keywordtype">void</span> *result) { <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"yes"</span>) == 0) *(<span class="keywordtype">int</span> *)result = ACB_YES; <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"no"</span>) == 0) *(<span class="keywordtype">int</span> *)result = ACB_NO; <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"ask"</span>) == 0) *(<span class="keywordtype">int</span> *)result = ACB_ASK; <span class="keywordflow">else</span> { cfg_error(cfg, <span class="stringliteral">"invalid value for option '%s': %s"</span>, cfg_opt_name(opt), value); <span class="keywordflow">return</span> -1; } <span class="keywordflow">return</span> 0; } <span class="comment">/* validates a port option (must be positive) */</span> <span class="keywordtype">int</span> conf_validate_port(<a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg, <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> *opt) { <span class="keywordtype">int</span> value = <a name="a4"></a>cfg_opt_getnint(opt, 0); <span class="keywordflow">if</span>(value <= 0) { cfg_error(cfg, <span class="stringliteral">"invalid port %d in section '%s'"</span>, value, <a name="a5"></a>cfg_name(cfg)); <span class="keywordflow">return</span> -1; } <span class="keywordflow">return</span> 0; } <span class="comment">/* validates a bookmark section (host option required) */</span> <span class="keywordtype">int</span> conf_validate_bookmark(<a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg, <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> *opt) { <a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *bookmark = <a name="a6"></a>cfg_opt_getnsec(opt, <a name="a7"></a>cfg_opt_size(opt) - 1); <span class="keywordflow">if</span>(<a name="a8"></a>cfg_size(bookmark, <span class="stringliteral">"host"</span>) == 0) { cfg_error(cfg, <span class="stringliteral">"missing required option 'host' in bookmark"</span>); <span class="keywordflow">return</span> -1; } <span class="keywordflow">return</span> 0; } <a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *parse_conf(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename) { <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> bookmark_opts[] = { <a name="a9"></a><a class="code" href="confuse_8h.html#963e2ee16396dcf36f6d1937faa00f0c" title="Initialize a string option.">CFG_STR</a>(<span class="stringliteral">"host"</span>, 0, <a name="a10"></a><a class="code" href="confuse_8h.html#9020ea37bfdb4f3e88a18d0a0d00cdd9" title="option has no default value">CFGF_NODEFAULT</a>), <a name="a11"></a><a class="code" href="confuse_8h.html#c2b17561e944264c594ea0802e56d308" title="Initialize an integer option.">CFG_INT</a>(<span class="stringliteral">"port"</span>, 21, <a name="a12"></a><a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a class="code" href="confuse_8h.html#963e2ee16396dcf36f6d1937faa00f0c" title="Initialize a string option.">CFG_STR</a>(<span class="stringliteral">"login"</span>, <span class="stringliteral">"anonymous"</span>, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a class="code" href="confuse_8h.html#963e2ee16396dcf36f6d1937faa00f0c" title="Initialize a string option.">CFG_STR</a>(<span class="stringliteral">"password"</span>, <span class="stringliteral">"anonymous@"</span>, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a class="code" href="confuse_8h.html#963e2ee16396dcf36f6d1937faa00f0c" title="Initialize a string option.">CFG_STR</a>(<span class="stringliteral">"directory"</span>, 0, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a name="a13"></a><a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>() }; <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> opts[] = { <a name="a14"></a><a class="code" href="confuse_8h.html#043d5c79624269e5f60f59a698e461a6" title="Initialize a section.">CFG_SEC</a>(<span class="stringliteral">"bookmark"</span>, bookmark_opts, <a name="a15"></a><a class="code" href="confuse_8h.html#86b522a5daef5c07066b28f1585d2d77" title="option may be specified multiple times (only applies to sections)">CFGF_MULTI</a> | <a name="a16"></a><a class="code" href="confuse_8h.html#65d20dbd5b778dd1bf3b3895206b4497" title="option has a title (only applies to sections)">CFGF_TITLE</a>), <a name="a17"></a><a class="code" href="confuse_8h.html#5a49fffc6735970c739e34d51a39c95d" title="Initialize a boolean option.">CFG_BOOL</a>(<span class="stringliteral">"passive-mode"</span>, cfg_false, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a class="code" href="confuse_8h.html#5a49fffc6735970c739e34d51a39c95d" title="Initialize a boolean option.">CFG_BOOL</a>(<span class="stringliteral">"remote-completion"</span>, cfg_true, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a name="a18"></a><a class="code" href="confuse_8h.html#b1ff85d6346cdefeb2ec2b696e50fde9" title="Initialize a function.">CFG_FUNC</a>(<span class="stringliteral">"alias"</span>, conf_alias), <a name="a19"></a><a class="code" href="confuse_8h.html#db9b1c168e72a3e065d2a06f2ed46c9f" title="Initialize a string list option.">CFG_STR_LIST</a>(<span class="stringliteral">"xterm-terminals"</span>, <span class="stringliteral">"{xterm, rxvt}"</span>, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>), <a name="a20"></a><a class="code" href="confuse_8h.html#6f80d4ee3191ce3fb2f9e3b8fba50d49" title="Initialize an integer option with a value parsing callback.">CFG_INT_CB</a>(<span class="stringliteral">"auto-create-bookmark"</span>, ACB_YES, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>, conf_parse_acb), <a class="code" href="confuse_8h.html#b1ff85d6346cdefeb2ec2b696e50fde9" title="Initialize a function.">CFG_FUNC</a>(<span class="stringliteral">"include-file"</span>, <a name="a21"></a>cfg_include), <a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>() }; <a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg = <a name="a22"></a>cfg_init(opts, <a class="code" href="confuse_8h.html#65df72d236bcc3e7f22c5b2b5c24bded" title="Flags.">CFGF_NONE</a>); <a name="a23"></a>cfg_set_validate_func(cfg, <span class="stringliteral">"bookmark|port"</span>, conf_validate_port); cfg_set_validate_func(cfg, <span class="stringliteral">"bookmark"</span>, conf_validate_bookmark); <span class="keywordflow">switch</span>(<a name="a24"></a>cfg_parse(cfg, filename)) { <span class="keywordflow">case</span> CFG_FILE_ERROR: printf(<span class="stringliteral">"warning: configuration file '%s' could not be read: %s\n"</span>, filename, strerror(errno)); printf(<span class="stringliteral">"continuing with default values...\n\n"</span>); <span class="keywordflow">case</span> <a name="a25"></a><a class="code" href="confuse_8h.html#66a1002180229809620b9bf2d3b888a6" title="Return codes from cfg_parse().">CFG_SUCCESS</a>: <span class="keywordflow">break</span>; <span class="keywordflow">case</span> CFG_PARSE_ERROR: <span class="keywordflow">return</span> 0; } <span class="keywordflow">return</span> cfg; } <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) { <span class="keywordtype">int</span> i; <a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg = parse_conf(argc > 1 ? argv[1] : <span class="stringliteral">"ftp.conf"</span>); <span class="comment">/* print the parsed configuration options */</span> <span class="keywordflow">if</span>(cfg) { printf(<span class="stringliteral">"passive-mode = %s\n"</span>, <a name="a26"></a>cfg_getbool(cfg, <span class="stringliteral">"passive-mode"</span>) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>); printf(<span class="stringliteral">"remote-completion = %s\n"</span>, cfg_getbool(cfg, <span class="stringliteral">"remote-completion"</span>) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>); printf(<span class="stringliteral">"number of bookmarks: %d\n"</span>, cfg_size(cfg, <span class="stringliteral">"bookmark"</span>)); <span class="keywordflow">for</span>(i = 0; i < cfg_size(cfg, <span class="stringliteral">"bookmark"</span>); i++) { <a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *bookmark = <a name="a27"></a>cfg_getnsec(cfg, <span class="stringliteral">"bookmark"</span>, i); printf(<span class="stringliteral">" bookmark #%d: %s:%s@%s:%ld%s\n"</span>, i+1, <a name="a28"></a>cfg_getstr(bookmark, <span class="stringliteral">"login"</span>), cfg_getstr(bookmark, <span class="stringliteral">"password"</span>), cfg_getstr(bookmark, <span class="stringliteral">"host"</span>), <a name="a29"></a>cfg_getint(bookmark, <span class="stringliteral">"port"</span>), cfg_getstr(bookmark, <span class="stringliteral">"directory"</span>)); } <span class="keywordflow">for</span>(i = 0; i < cfg_size(cfg, <span class="stringliteral">"xterm-terminals"</span>); i++) { printf(<span class="stringliteral">"xterm-terminal #%d: %s\n"</span>, i+1, <a name="a30"></a>cfg_getnstr(cfg, <span class="stringliteral">"xterm-terminals"</span>, i)); } printf(<span class="stringliteral">"auto-create-bookmark = %ld\n"</span>, cfg_getint(cfg, <span class="stringliteral">"auto-create-bookmark"</span>)); <a name="a31"></a>cfg_free(cfg); } <span class="keywordflow">return</span> 0; } </pre></div> </div> <!-- doxygen-footer.html starts here --> </div> </body> </html>