argp.c.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  3. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <title>argp.c</title>
  7. <meta name="generator" content="emacs 26.3; htmlfontify 0.21" />
  8. <style type="text/css"><!--
  9. body { font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; color: #dcdccc; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  10. span.default { font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; color: #dcdccc; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  11. span.default a { font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; color: #dcdccc; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  12. span.function-name { color: #93e0e3; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  13. span.function-name a { color: #93e0e3; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  14. span.comment { color: #7f9f7f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  15. span.comment a { color: #7f9f7f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  16. span.comment-delimiter { color: #5f7f5f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  17. span.comment-delimiter a { color: #5f7f5f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  18. span.variable-name { color: #dfaf8f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  19. span.variable-name a { color: #dfaf8f; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  20. span.type { color: #7cb8bb; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  21. span.type a { color: #7cb8bb; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  22. span.keyword { color: #f0dfaf; font-weight: 700; font-family: Hack; font-stretch: normal; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  23. span.keyword a { color: #f0dfaf; font-weight: 700; font-family: Hack; font-stretch: normal; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  24. span.string { color: #cc9393; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  25. span.string a { color: #cc9393; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  26. span.preprocessor { color: #94bff3; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: none; }
  27. span.preprocessor a { color: #94bff3; font-family: Hack; font-stretch: normal; font-weight: 500; font-style: normal; background: #3f3f3f; font-size: 11pt; text-decoration: underline; }
  28. --></style>
  29. <script type="text/javascript"><!--
  30. // this function is needed to work around
  31. // a bug in IE related to element attributes
  32. function hasClass(obj)
  33. {
  34. var result = false;
  35. if (obj.getAttributeNode("class") != null)
  36. {
  37. result = obj.getAttributeNode("class").value;
  38. }
  39. return result;
  40. }
  41. function stripe(id)
  42. {
  43. // the flag we'll use to keep track of
  44. // whether the current row is odd or even
  45. var even = false;
  46. // if arguments are provided to specify the colors
  47. // of the even & odd rows, then use them;
  48. // otherwise use the following defaults:
  49. var evenColor = arguments[1] ? arguments[1] : "#fff";
  50. var oddColor = arguments[2] ? arguments[2] : "#ddd";
  51. // obtain a reference to the desired table
  52. // if no such table exists, abort
  53. var table = document.getElementById(id);
  54. if (! table) { return; }
  55. // by definition, tables can have more than one tbody
  56. // element, so we'll have to get the list of child
  57. // &lt;tbody&gt;s
  58. var tbodies = table.getElementsByTagName("tbody");
  59. // and iterate through them...
  60. for (var h = 0; h < tbodies.length; h++)
  61. {
  62. // find all the &lt;tr&gt; elements...
  63. var trs = tbodies[h].getElementsByTagName("tr");
  64. // ... and iterate through them
  65. for (var i = 0; i < trs.length; i++)
  66. {
  67. // avoid rows that have a class attribute
  68. // or backgroundColor style
  69. if (! hasClass(trs[i]) &&
  70. ! trs[i].style.backgroundColor)
  71. {
  72. // get all the cells in this row...
  73. var tds = trs[i].getElementsByTagName("td");
  74. // and iterate through them...
  75. for (var j = 0; j < tds.length; j++)
  76. {
  77. var mytd = tds[j];
  78. // avoid cells that have a class attribute
  79. // or backgroundColor style
  80. if (! hasClass(mytd) &&
  81. ! mytd.style.backgroundColor)
  82. {
  83. mytd.style.backgroundColor =
  84. even ? evenColor : oddColor;
  85. }
  86. }
  87. }
  88. // flip from odd to even, or vice-versa
  89. even = ! even;
  90. }
  91. }
  92. }
  93. function toggle_invis( name )
  94. {
  95. var filter =
  96. { acceptNode:
  97. function( node )
  98. { var classname = node.id;
  99. if( classname )
  100. { var classbase = classname.substr( 0, name.length );
  101. if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
  102. return NodeFilter.FILTER_SKIP; } };
  103. var walker = document.createTreeWalker( document.body ,
  104. NodeFilter.SHOW_ELEMENT ,
  105. filter ,
  106. false );
  107. while( walker.nextNode() )
  108. {
  109. var e = walker.currentNode;
  110. if( e.style.display == "none" ) { e.style.display = "inline"; }
  111. else { e.style.display = "none"; }
  112. }
  113. }
  114. --> </script>
  115. </head>
  116. <body onload="stripe('index'); return true;">
  117. <pre><span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string">&lt;</span><span class="string">stdio.h</span><span class="string">&gt;</span>
  118. <span class="preprocessor">#</span><span class="preprocessor">include</span> <span class="string">&lt;</span><span class="string">argp.h</span><span class="string">&gt;</span>
  119. <span class="keyword">e</span><span class="keyword">nu</span><span class="keyword">m</span> <span class="type">boolean</span> { <span class="variable-name">NO</span>, <span class="variable-name">YES</span> };
  120. <span class="keyword">enum</span> <span class="type">boolea</span><span class="type">n</span> <span class="variable-name">bool</span> = NO;
  121. <span class="keyword">const</span> <span class="type">cha</span><span class="type">r</span> * <span class="variable-name">argp_program_version</span> = <span class="string">&quot;0.1&quot;</span>;
  122. <span class="comment-delimiter">/</span><span class="comment-delimiter">* </span><span class="comment">mail bug reports to </span><span class="comment-delimiter">*/</span>
  123. <span class="keyword">c</span><span class="keyword">onst</span> <span class="type">cha</span><span class="type">r</span> * <span class="variable-name">argp_program_bug_address</span> = <span class="string">&quot;jbranso@fastmail.com&quot;</span>;
  124. <span class="comment-delimiter">/</span><span class="comment-delimiter">/</span><span class="comment">define an argp option called joke aka &quot;--joke&quot; or &quot;-j&quot;
  125. </span><span class="comment-delimiter">/* </span><span class="comment">struct argp_option argp_options_joke; </span><span class="comment-delimiter">*/</span>
  126. <span class="comment-delimiter">/* </span><span class="comment">argp_options_joke.name = &quot;joke&quot;; </span><span class="comment-delimiter">*/</span>
  127. <span class="comment-delimiter">/* </span><span class="comment">argp_options_joke.key = 'j'; </span><span class="comment-delimiter">*/</span>
  128. <span class="comment-delimiter">/* </span><span class="comment">/\* This is the value that you see when you print: </span><span class="comment-delimiter">*/</span>
  129. <span class="comment-delimiter">/* </span><span class="comment">** ./argp --usage. </span><span class="comment-delimiter">*/</span>
  130. <span class="comment-delimiter">/* </span><span class="comment">*\/ </span><span class="comment-delimiter">*/</span>
  131. <span class="comment-delimiter">/* </span><span class="comment">argp_options_joke.arg = &quot;ARG&quot;; //must be provided. </span><span class="comment-delimiter">*/</span>
  132. <span class="comment-delimiter">/</span><span class="comment-delimiter">* </span><span class="comment">argp_options_joke.doc = &quot;Print a funny joke.&quot;; </span><span class="comment-delimiter">*/</span>
  133. <span class="comment-delimiter">/* </span><span class="comment">argp_options_joke.flags = OPTION_ARG_OPTIONAL; // this option does not need an argument. </span><span class="comment-delimiter">*/</span>
  134. <span class="keyword">s</span><span class="keyword">tatic</span> <span class="keyword">const</span> <span class="keyword">struct</span> <span class="type">argp_optio</span><span class="type">n</span> <span class="variable-name">options</span> [] =
  135. {
  136. {<span class="string">&quot;joke&quot;</span>, <span class="string">'j'</span>, <span class="string">&quot;ARG&quot;</span>, OPTION_ARG_OPTIONAL, <span class="string">&quot;Print a funny joke&quot;</span> },
  137. { 0 }
  138. };
  139. <span class="comment-delimiter">/</span><span class="comment-delimiter">/ </span><span class="comment">the above can be specified much simplier. I could actually get rid of the argp_options_joke variable.
  140. </span><span class="comment-delimiter">//</span><span class="comment">struct argp_option options [2] = { &quot;joke&quot;, &quot;j&quot; &quot;STRING&quot;, 0, &quot;Print a funny joke&quot;, { 0 } };
  141. </span>
  142. <span class="comment-delimiter">/</span><span class="comment-delimiter">/</span><span class="comment">define an argp parse function
  143. </span><span class="type">e</span><span class="type">rror_</span><span class="type">t</span> <span class="function-name">argp_parser</span> (<span class="type">int</span> <span class="variable-name">opt</span>, <span class="type">char</span> *<span class="variable-name">arg</span>, <span class="keyword">struct</span> <span class="type">argp_state</span> *<span class="variable-name">state</span>) {
  144. <span class="keyword">extern</span> <span class="keyword">enum</span> <span class="type">boolea</span><span class="type">n</span> <span class="variable-name">bool</span>;
  145. <span class="keyword">switch</span> (opt)
  146. {
  147. <span class="comment-delimiter">// </span><span class="comment">if this parser function is called on an option that it doesn</span><span class="comment">'</span><span class="comment">t recognize, then don</span><span class="comment">'</span><span class="comment">t do anything.
  148. </span> <span class="keyword">default</span>:
  149. <span class="keyword">return</span> ARGP_ERR_UNKNOWN;
  150. <span class="keyword">case</span> <span class="string">'j'</span>:
  151. {
  152. bool = YES;
  153. <span class="keyword">break</span>;
  154. }
  155. }
  156. <span class="keyword">return</span> 0;
  157. }
  158. <span class="comment-delimiter">/</span><span class="comment-delimiter">* </span><span class="comment">struct argp argp; </span><span class="comment-delimiter">*/</span>
  159. <span class="keyword">s</span><span class="keyword">truct</span> <span class="type">arg</span><span class="type">p</span> <span class="variable-name">argp</span> =
  160. { options, argp_parser, 0, <span class="string">&quot;Just a simple test argp program&quot;</span> };
  161. <span class="comment-delimiter">/</span><span class="comment-delimiter">* </span><span class="comment">a string containing the basic usage of this program. </span><span class="comment-delimiter">*/</span>
  162. <span class="comment-delimiter">/* </span><span class="comment">If I try to set the argp values manually, I get a segmentation fault. </span><span class="comment-delimiter">*/</span>
  163. <span class="comment-delimiter">/* </span><span class="comment">argp.args_doc = &quot;argp [options]&quot;; </span><span class="comment-delimiter">*/</span>
  164. <span class="comment-delimiter">/* </span><span class="comment">argp.doc = &quot;Just a simple test argp program!&quot;; </span><span class="comment-delimiter">*/</span>
  165. <span class="comment-delimiter">/* </span><span class="comment">argp.options = options; </span><span class="comment-delimiter">*/</span>
  166. <span class="comment-delimiter">/* </span><span class="comment">argp.parser = argp_parser; </span><span class="comment-delimiter">*/</span>
  167. <span class="type">i</span><span class="type">n</span><span class="type">t</span> <span class="function-name">main</span> (<span class="type">int</span> <span class="variable-name">argc</span>, <span class="type">char</span> **<span class="variable-name">argv</span>) {
  168. <span class="comment-delimiter">/</span><span class="comment-delimiter">/ </span><span class="comment">I still have a bit more to learn with argp, but that</span><span class="comment">'</span><span class="comment">s ok!
  169. </span> argp_parse (&amp;argp, argc, argv, 0, 0, 0);
  170. <span class="keyword">if</span> (bool == YES) {
  171. printf (<span class="string">&quot;What do you call a box full of ducks?\n&quot;</span>);
  172. printf (<span class="string">&quot;A bunch of quackers.\n&quot;</span>);
  173. }
  174. <span class="keyword">return</span> 0;
  175. }
  176. </pre>
  177. </body>
  178. </html>