Annotation of embedaddon/confuse/doc/html/ftpconf_8c-example.html, revision 1.1
1.1 ! misho 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
! 2: "http://www.w3.org/TR/html4/strict.dtd">
! 3: <html>
! 4: <head>
! 5: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
! 6: <title>confuse: ftpconf.c</title>
! 7: <link rel="stylesheet" href="tutorial.css" type="text/css">
! 8: </head>
! 9: <body>
! 10:
! 11: <div class="main">
! 12:
! 13: <!-- doxygen-header.html ends here -->
! 14: <!-- Generated by Doxygen 1.5.8 -->
! 15: <div class="navigation" id="top">
! 16: <div class="tabs">
! 17: <ul>
! 18: <li><a href="index.html"><span>Main Page</span></a></li>
! 19: <li><a href="annotated.html"><span>Data Structures</span></a></li>
! 20: <li><a href="files.html"><span>Files</span></a></li>
! 21: <li><a href="examples.html"><span>Examples</span></a></li>
! 22: </ul>
! 23: </div>
! 24: </div>
! 25: <div class="contents">
! 26: <h1>ftpconf.c</h1><div class="fragment"><pre class="fragment"><span class="comment">/*</span>
! 27: <span class="comment"> * Parses and prints the configuration options for a fictous ftp client</span>
! 28: <span class="comment"> */</span>
! 29:
! 30: <span class="preprocessor">#include <stdio.h></span>
! 31: <span class="preprocessor">#include <string.h></span>
! 32: <span class="preprocessor">#include <errno.h></span>
! 33: <span class="preprocessor">#include <<a class="code" href="confuse_8h.html" title="A configuration file parser library.">confuse.h</a>></span>
! 34:
! 35: <span class="comment">/* valid values for the auto-create-bookmark option */</span>
! 36: <span class="preprocessor">#define ACB_YES 1</span>
! 37: <span class="preprocessor"></span><span class="preprocessor">#define ACB_NO 2</span>
! 38: <span class="preprocessor"></span><span class="preprocessor">#define ACB_ASK 3</span>
! 39: <span class="preprocessor"></span>
! 40: <span class="comment">/* called on alias() functions in the config file */</span>
! 41: <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)
! 42: {
! 43: <span class="keywordflow">if</span>(argc < 2)
! 44: {
! 45: <a name="a2"></a>cfg_error(cfg, <span class="stringliteral">"function '%s' requires 2 arguments"</span>, <a name="a3"></a>cfg_opt_name(opt));
! 46: <span class="keywordflow">return</span> -1;
! 47: }
! 48: printf(<span class="stringliteral">"got alias '%s' = '%s'\n"</span>, argv[0], argv[1]);
! 49: <span class="keywordflow">return</span> 0;
! 50: }
! 51:
! 52: <span class="comment">/* parse values for the auto-create-bookmark option */</span>
! 53: <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)
! 54: {
! 55: <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"yes"</span>) == 0)
! 56: *(<span class="keywordtype">int</span> *)result = ACB_YES;
! 57: <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"no"</span>) == 0)
! 58: *(<span class="keywordtype">int</span> *)result = ACB_NO;
! 59: <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(value, <span class="stringliteral">"ask"</span>) == 0)
! 60: *(<span class="keywordtype">int</span> *)result = ACB_ASK;
! 61: <span class="keywordflow">else</span>
! 62: {
! 63: cfg_error(cfg, <span class="stringliteral">"invalid value for option '%s': %s"</span>,
! 64: cfg_opt_name(opt), value);
! 65: <span class="keywordflow">return</span> -1;
! 66: }
! 67: <span class="keywordflow">return</span> 0;
! 68: }
! 69:
! 70: <span class="comment">/* validates a port option (must be positive) */</span>
! 71: <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)
! 72: {
! 73: <span class="keywordtype">int</span> value = <a name="a4"></a>cfg_opt_getnint(opt, 0);
! 74: <span class="keywordflow">if</span>(value <= 0)
! 75: {
! 76: cfg_error(cfg, <span class="stringliteral">"invalid port %d in section '%s'"</span>, value, <a name="a5"></a>cfg_name(cfg));
! 77: <span class="keywordflow">return</span> -1;
! 78: }
! 79: <span class="keywordflow">return</span> 0;
! 80: }
! 81:
! 82: <span class="comment">/* validates a bookmark section (host option required) */</span>
! 83: <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)
! 84: {
! 85: <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);
! 86: <span class="keywordflow">if</span>(<a name="a8"></a>cfg_size(bookmark, <span class="stringliteral">"host"</span>) == 0)
! 87: {
! 88: cfg_error(cfg, <span class="stringliteral">"missing required option 'host' in bookmark"</span>);
! 89: <span class="keywordflow">return</span> -1;
! 90: }
! 91: <span class="keywordflow">return</span> 0;
! 92: }
! 93:
! 94: <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)
! 95: {
! 96: <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> bookmark_opts[] = {
! 97: <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>),
! 98: <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>),
! 99: <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>),
! 100: <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>),
! 101: <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>),
! 102: <a name="a13"></a><a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>()
! 103: };
! 104:
! 105: <a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> opts[] = {
! 106: <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>),
! 107: <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>),
! 108: <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>),
! 109: <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),
! 110: <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>),
! 111: <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),
! 112: <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),
! 113: <a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>()
! 114: };
! 115:
! 116: <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>);
! 117: <a name="a23"></a>cfg_set_validate_func(cfg, <span class="stringliteral">"bookmark|port"</span>, conf_validate_port);
! 118: cfg_set_validate_func(cfg, <span class="stringliteral">"bookmark"</span>, conf_validate_bookmark);
! 119:
! 120: <span class="keywordflow">switch</span>(<a name="a24"></a>cfg_parse(cfg, filename))
! 121: {
! 122: <span class="keywordflow">case</span> CFG_FILE_ERROR:
! 123: printf(<span class="stringliteral">"warning: configuration file '%s' could not be read: %s\n"</span>,
! 124: filename, strerror(errno));
! 125: printf(<span class="stringliteral">"continuing with default values...\n\n"</span>);
! 126: <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>:
! 127: <span class="keywordflow">break</span>;
! 128: <span class="keywordflow">case</span> CFG_PARSE_ERROR:
! 129: <span class="keywordflow">return</span> 0;
! 130: }
! 131:
! 132: <span class="keywordflow">return</span> cfg;
! 133: }
! 134:
! 135: <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
! 136: {
! 137: <span class="keywordtype">int</span> i;
! 138: <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>);
! 139:
! 140: <span class="comment">/* print the parsed configuration options */</span>
! 141: <span class="keywordflow">if</span>(cfg)
! 142: {
! 143: printf(<span class="stringliteral">"passive-mode = %s\n"</span>,
! 144: <a name="a26"></a>cfg_getbool(cfg, <span class="stringliteral">"passive-mode"</span>) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>);
! 145: printf(<span class="stringliteral">"remote-completion = %s\n"</span>,
! 146: cfg_getbool(cfg, <span class="stringliteral">"remote-completion"</span>) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>);
! 147:
! 148: printf(<span class="stringliteral">"number of bookmarks: %d\n"</span>, cfg_size(cfg, <span class="stringliteral">"bookmark"</span>));
! 149: <span class="keywordflow">for</span>(i = 0; i < cfg_size(cfg, <span class="stringliteral">"bookmark"</span>); i++)
! 150: {
! 151: <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);
! 152: printf(<span class="stringliteral">" bookmark #%d: %s:%s@%s:%ld%s\n"</span>, i+1,
! 153: <a name="a28"></a>cfg_getstr(bookmark, <span class="stringliteral">"login"</span>),
! 154: cfg_getstr(bookmark, <span class="stringliteral">"password"</span>),
! 155: cfg_getstr(bookmark, <span class="stringliteral">"host"</span>),
! 156: <a name="a29"></a>cfg_getint(bookmark, <span class="stringliteral">"port"</span>),
! 157: cfg_getstr(bookmark, <span class="stringliteral">"directory"</span>));
! 158: }
! 159:
! 160: <span class="keywordflow">for</span>(i = 0; i < cfg_size(cfg, <span class="stringliteral">"xterm-terminals"</span>); i++)
! 161: {
! 162: printf(<span class="stringliteral">"xterm-terminal #%d: %s\n"</span>,
! 163: i+1, <a name="a30"></a>cfg_getnstr(cfg, <span class="stringliteral">"xterm-terminals"</span>, i));
! 164: }
! 165:
! 166: printf(<span class="stringliteral">"auto-create-bookmark = %ld\n"</span>,
! 167: cfg_getint(cfg, <span class="stringliteral">"auto-create-bookmark"</span>));
! 168: <a name="a31"></a>cfg_free(cfg);
! 169: }
! 170:
! 171: <span class="keywordflow">return</span> 0;
! 172: }
! 173:
! 174: </pre></div> </div>
! 175: <!-- doxygen-footer.html starts here -->
! 176: </div>
! 177: </body>
! 178: </html>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>