Command-Line-Parsing.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <html lang="en">
  2. <head>
  3. <title>Command Line Parsing - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Invocation.html#Invocation" title="Invocation">
  9. <link rel="prev" href="Invocation.html#Invocation" title="Invocation">
  10. <link rel="next" href="Execution-Modes.html#Execution-Modes" title="Execution Modes">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Command-Line-Parsing"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Execution-Modes.html#Execution-Modes">Execution Modes</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Invocation.html#Invocation">Invocation</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Invocation.html#Invocation">Invocation</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">3.4.1 Command Line Parsing</h4>
  35. <p><a name="index-command-line-584"></a>A couple of functions declared in <samp><span class="file">cmdline.h</span></samp> can be used to do all
  36. the necessary parsing of command lines and environment variables needed
  37. by virtual code applications.
  38. <div class="defun">
  39. &mdash; Function: list <b>avm_default_command_line</b> (<var>int argc, char *argv</var>[]<var>, int index, char *extension, char *paths, int default_to_stdin_mode, int force_text_input_mode, int *file_ordinal</var>)<var><a name="index-avm_005fdefault_005fcommand_005fline-585"></a></var><br>
  40. <blockquote>
  41. <p>The purpose of this function is to build most of the data structure used
  42. by parameter mode applications, as described in <a href="Input-Data-Structure.html#Input-Data-Structure">Input Data Structure</a>, by parsing the command line according to <a href="Command-Line-Syntax.html#Command-Line-Syntax">Command Line Syntax</a>. The parameters have these interpretations.
  43. <dl>
  44. <dt><var>argc</var><dd>is the number elements in the array referenced by <var>argv</var>
  45. <br><dt><var>argv</var><dd>is the address of an array of pointers to null terminated character strings
  46. holding command line arguments
  47. <br><dt><var>index</var><dd>is the position of the first element of <var>argv</var> to be
  48. considered. Those preceding it are ignored.
  49. <br><dt><var>extension</var><dd>is the address of a string that will be appended to input file names
  50. given in <var>argv</var> in an effort to find the associated files
  51. <br><dt><var>paths</var><dd>is the address of a null terminated character string containing a colon
  52. separated list of directory names that will be searched for input files
  53. <br><dt><var>default_to_stdin_mode</var><dd>is set to a non-zero value by the caller if the contents of standard
  54. input should be read in the absence of input files
  55. <br><dt><var>force_text_input_mode</var><dd>is set to a non-zero value by the caller to indicate that input files
  56. should be read as text, using <code>avm_load</code> (rather than
  57. <code>avm_preamble_and_contents</code>, which would allow them to be either
  58. text or data). The <var>preamble</var> field of the returned file
  59. specifications will always be empty when this flag is set.
  60. <br><dt><var>file_ordinal</var><dd>is set to a pointer to an integer by the caller if only one file is to
  61. be loaded during each call. The value of the integer indicates the which
  62. one it will be.
  63. </dl>
  64. <p>The result returned by this function is a list whose <code>head</code> is a list
  65. of file specifications and whose <code>tail</code> is a list of command line options
  66. intended for input to a virtual code application.
  67. <p>The list of file specifications returned in the <code>head</code> of the
  68. result follows the same conventions as the <var>data</var> parameter
  69. to the function <code>avm_output_as_directed</code>, except that the
  70. <code>head</code> of the <code>head</code> of each item is a list representing the
  71. time stamp of the file as given by <code>avm_date_representation</code>. If
  72. the file is standard input, then it holds the current system date and
  73. time.
  74. <p>If the <var>file_ordinal</var> parameter is <code>NULL</code>, then all
  75. files on the command line are loaded, but if it points to an integer
  76. <var>n</var>, then only the <var>n</var>th file is loaded, and <var>n</var> is
  77. incremented. If there is no <var>n</var>th file, a <code>NULL</code> value is
  78. returned as the entire result of the function. For a series of calls,
  79. the integer should be initialized to zero by the caller before the first
  80. call.
  81. <p>If standard input is indicated as one of the files on the command line
  82. (by a dash), then it is also loaded regardless of the
  83. <var>file_ordinal</var>, but a cached copy of it is used on subsequent
  84. calls after the first, so that the function does not actually attempt to
  85. reread it. If standard input is to be loaded, it must be finite for this
  86. function to work properly.
  87. <p>The search strategy for files is described in <a href="Environment.html#Environment">Environment</a>, and
  88. makes use of the <var>extension</var> and <var>paths</var> parameters.
  89. <p>In the list of command line options returned in the <code>tail</code> of the
  90. result, each item is a list with a non-empty <code>head</code> and
  91. <code>tail</code>, and is interpreted as follows.
  92. <ul>
  93. <li>The <code>head</code> of the <code>head</code> is a list representing a
  94. natural number, as given by <code>avm_natural</code>, indicating the position
  95. of the option on the command line relative to the initial value of the
  96. <var>index</var> parameter.
  97. <li>The <code>tail</code> of the <code>head</code> is a list which is <code>NULL</code>
  98. in the case of a &ldquo;short form&rdquo; option, written with a single
  99. dash on the command line, but is a list whose <code>head</code> and
  100. <code>tail</code> are <code>NULL</code> in the case of a &ldquo;long form&rdquo; option,
  101. written with two dashes.
  102. <li>The <code>head</code> of the <code>tail</code> is a list representing a
  103. character string for the keyword of an option, for example <kbd>foo</kbd> in
  104. the case of an option written <kbd>--foo=bar,baz</kbd>.
  105. <li>The <code>tail</code> of the <code>tail</code> is a list of lists representing
  106. character strings, with one item for each parameter associated with the
  107. option, for example, <kbd>bar</kbd> and <kbd>baz</kbd>.
  108. </ul>
  109. <p>If multiple calls to the function are made with differing values of
  110. <code>*</code><var>file_ordinal</var> but other parameters unchanged, the same list of
  111. options will be returned each time, except insofar as the position
  112. numbers in the <code>head</code> of the <code>head</code> of each item are adjusted
  113. as explained in <a href="Input-for-Mapped-Applications.html#Input-for-Mapped-Applications">Input for Mapped Applications</a>.
  114. <p>Any of the i/o errors or fatal errors associated with other file
  115. input operations are possible with this function as well. This non-fatal
  116. warning message is also possible.
  117. <a name="index-g_t_0040code_007bsearch-paths-not-supported_007d-586"></a>
  118. <pre class="display"> <var>program-name</var><code>: warning: search paths not supported</code>
  119. </pre>
  120. <p>This error occurs if the library has been built on a platform that
  121. <a name="index-g_t_0040file_007bargz_002eh_007d-587"></a>doesn't have the <samp><span class="file">argz.h</span></samp> header file and the <var>paths</var>
  122. parameter is non-<code>NULL</code>.
  123. </p></blockquote></div>
  124. <div class="defun">
  125. &mdash; Function: list <b>avm_environment</b> (<var>char *env</var>[])<var><a name="index-avm_005fenvironment-588"></a></var><br>
  126. <blockquote><p><a name="index-environment-589"></a>This function takes the address of a null terminated array of pointers
  127. to null terminated character strings of the form
  128. <code>"variable=value"</code>. The result returned is a list of lists, with
  129. one item for each element of the array. The <code>head</code> of each item is
  130. a representation of the left side of the corresponding string, and the
  131. <code>tail</code> is a representation of the right.
  132. <p>This function is therefore useful along with
  133. <code>avm_default_command_line</code> for building the remainder of the data
  134. structure described in <a href="Parameter-Mode-Interface.html#Parameter-Mode-Interface">Parameter Mode Interface</a>. For example, a
  135. virtual machine emulator for non-interactive parameter mode applications
  136. with no bells and whistles could have the following form.
  137. <pre class="example"> int
  138. main(argc,argv,env)
  139. ...
  140. {
  141. FILE *virtual_code_file;
  142. ...
  143. avm_initialize_lists();
  144. avm_initialize_apply();
  145. avm_initialize_rawio();
  146. avm_initialize_formout();
  147. avm_initialize_cmdline();
  148. virtual_code_file = fopen(argv[1],"rb");
  149. operator = avm_received_list(
  150. virtual_code_file,argv[1]);
  151. fclose(virtual_code_file);
  152. command = avm_default_command_line(argc,
  153. argv,2,NULL,NULL,0,0,NULL);
  154. environs = avm_environment(env);
  155. operand = avm_join(command,environs);
  156. result = avm_apply(operator,operand);
  157. avm_output_as_directed(result,0,0);
  158. avm_dispose(result);
  159. ...
  160. }
  161. </pre>
  162. <p>The <code>avm_environment</code> function could cause the program to abort due
  163. to a memory overflow. For security reasons, it will also abort with an
  164. <a name="index-security-590"></a>error message if any non-printing characters are detected in its
  165. argument. (See <a href="Other-Diagnostics-and-Warnings.html#Other-Diagnostics-and-Warnings">Other Diagnostics and Warnings</a>.)
  166. </p></blockquote></div>
  167. <div class="defun">
  168. &mdash; Function: void <b>avm_initialize_cmdline</b> ()<var><a name="index-avm_005finitialize_005fcmdline-591"></a></var><br>
  169. <blockquote><p>This function initializes some local variables and should be called
  170. before any of the other functions in this section is called, or else
  171. their results are unpredictable.
  172. </p></blockquote></div>
  173. <div class="defun">
  174. &mdash; Function: void <b>avm_count_cmdline</b> ()<var><a name="index-avm_005fcount_005fcmdline-592"></a></var><br>
  175. <blockquote><p>This function should be called after the last call to any of the other
  176. functions in this section, as it reclaims some locally allocated
  177. storage. If the <code>avm_count_lists</code> function is used, it should be
  178. called after this one.
  179. </p></blockquote></div>
  180. </body></html>