Formatted-Output.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <html lang="en">
  2. <head>
  3. <title>Formatted Output - 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="File-Manipulation.html#File-Manipulation" title="File Manipulation">
  9. <link rel="prev" href="Formatted-Input.html#Formatted-Input" title="Formatted Input">
  10. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  11. <meta http-equiv="Content-Style-Type" content="text/css">
  12. <style type="text/css"><!--
  13. pre.display { font-family:inherit }
  14. pre.format { font-family:inherit }
  15. pre.smalldisplay { font-family:inherit; font-size:smaller }
  16. pre.smallformat { font-family:inherit; font-size:smaller }
  17. pre.smallexample { font-size:smaller }
  18. pre.smalllisp { font-size:smaller }
  19. span.sc { font-variant:small-caps }
  20. span.roman { font-family:serif; font-weight:normal; }
  21. span.sansserif { font-family:sans-serif; font-weight:normal; }
  22. --></style>
  23. </head>
  24. <body>
  25. <div class="node">
  26. <a name="Formatted-Output"></a>
  27. <p>
  28. Previous:&nbsp;<a rel="previous" accesskey="p" href="Formatted-Input.html#Formatted-Input">Formatted Input</a>,
  29. Up:&nbsp;<a rel="up" accesskey="u" href="File-Manipulation.html#File-Manipulation">File Manipulation</a>
  30. <hr>
  31. </div>
  32. <h4 class="subsection">3.3.4 Formatted Output</h4>
  33. <p>The following functions pertaining to the output of text files or data files with
  34. <a name="index-preamble-562"></a>preambles are declared in the header file <samp><span class="file">formout.h</span></samp>.
  35. <div class="defun">
  36. &mdash; Function: void <b>avm_output</b> (<var>FILE *repository, char *filename, list preamble, list contents, int trace_mode</var>)<var><a name="index-avm_005foutput-563"></a></var><br>
  37. <blockquote>
  38. <p>This function writes a either a text file or a data file in the format
  39. described in <a href="File-Format.html#File-Format">File Format</a>. The parameters have these
  40. interpretations.
  41. <dl>
  42. <dt><var>repository</var><dd>is the address of a file opened for writing by the caller, that will be
  43. written from its current position.
  44. <br><dt><var>filename</var><dd>is the address of a null terminated character string set
  45. by the caller to be the name of the file that will
  46. be reported to the user in the event of an i/o error.
  47. <br><dt><var>preamble</var><dd>is <code>NULL</code> in the case of a text file, but a list of character string
  48. representations as per <a href="Character-Table.html#Character-Table">Character Table</a>, in the case of a data
  49. file. If a data file has is to be written with an empty preamble, then
  50. this list should have a <code>NULL</code> <code>head</code> and a <code>NULL</code>
  51. <code>tail</code>.
  52. <br><dt><var>contents</var><dd>is either a list of character string representations in the case of a
  53. text file, or is an unconstrained list in the case of a data file.
  54. <br><dt><var>trace_mode</var><dd>may be set to a non-zero value by the caller to request that everything
  55. written to a text file should be echoed to standard output. It is
  56. ignored in the case of a data file.
  57. </dl>
  58. <p>The effect of calling this function is to write the preamble and
  59. contents to the file in the format indicated by the preamble. The file
  60. is left open when this function returns.
  61. <p>Line breaks are always written as character code 10, not as 13 10,
  62. <a name="index-line-breaks-564"></a>regardless of the convention on the host system, so that files written
  63. by this function can be reliably read by other functions in the library.
  64. <p>Leading hashes are automatically added to the beginning of the lines in
  65. the preamble, except where they are unnecessary due to a continuation
  66. character on the previous line. This action enforces consistency with the
  67. file format, ensuring that anything written as a data file can be read
  68. back as one. The hashes are stripped automatically when the file is
  69. read by <code>avm_preamble_and_contents</code>.
  70. <p>Another feature of this function is that it will mark any output file as
  71. executable if it is a data format file with a prelude whose first
  72. <a name="index-executable-files-565"></a>character in the first line is an exclamation point. This feature makes
  73. it easier for a compiler implemented in virtual code to generate
  74. executable shell scripts directly.
  75. <p>A fatal error is reported if any of the data required
  76. to be a character representation is not listed in the <a href="Character-Table.html#Character-Table">Character Table</a>. A fatal error can also be caused by a memory overflow. Possible
  77. error messages are the following.
  78. <a name="index-g_t_0040code_007binvalid-output-preamble-format_007d-566"></a><a name="index-g_t_0040code_007binvalid-text-format_007d-567"></a><a name="index-g_t_0040code_007bcan_0027t-write_007d-568"></a>
  79. <ul>
  80. <li><var>program-name</var><code>: invalid output preamble format</code>
  81. <li><var>program-name</var><code>: invalid text format</code>
  82. <li><var>program-name</var><code>: can't write to </code><var>filename</var>
  83. </ul>
  84. <p><a name="index-g_t_0040code_007bstrerror_007d-569"></a>In the last case, the error message will be followed by an explanation
  85. furnished by the standard <code>strerror</code> function if available.
  86. </p></blockquote></div>
  87. <div class="defun">
  88. &mdash; Function: void <b>avm_output_as_directed</b> (<var>list data, int ask_to_overwrite_mode, int verbose_mode</var>)<var><a name="index-avm_005foutput_005fas_005fdirected-570"></a></var><br>
  89. <blockquote><p>This function writes an ensemble of files at specified paths in either
  90. text or data format, optionally interacting with the user through
  91. standard input and output. The parameters
  92. have these interpretations.
  93. <dl>
  94. <dt><var>data</var><dd>is a list in which each item specifies a file to be
  95. written.
  96. <br><dt><var>ask_to_overwrite_mode</var><dd>may be set to a non-zero value by the calling program in order to
  97. have this function ask the user for permission to overwrite existing files.
  98. <br><dt><var>verbose_mode</var><dd>may be set to a non-zero value by the calling program to have this
  99. function print to standard output a list of the names of the files it
  100. writes.
  101. </dl>
  102. <p>A high level interface between virtual code applications and the file
  103. system is provided by this function. The <var>data</var> parameter
  104. format is compatible with the the data structure returned by an
  105. application complying with the conventions in <a href="Output-From-Non_002dinteractive-Applications.html#Output-From-Non_002dinteractive-Applications">Output From Non-interactive Applications</a>.
  106. <p>Each item in the <var>data</var> list should be a non-empty list whose
  107. <code>head</code> and <code>tail</code> are also non-empty. The fields in each item have
  108. the following relevance to the file it specifies.
  109. <ul>
  110. <li>The <code>head</code> of the <code>head</code> is <code>NULL</code> if the file is
  111. to be opened for appending, and non-<code>NULL</code> if it is to be
  112. overwritten.
  113. <li>The <code>tail</code> of the <code>head</code> represents a path as a list of
  114. character string representations, in a form suitable as an argument to
  115. <code>avm_path_name</code>.
  116. <li>The <code>head</code> of the <code>tail</code> represents the preamble of the
  117. file, as either <code>NULL</code> for a text file or a non-empty list of
  118. character string representations for a data file.
  119. <li>The <code>tail</code> of the <code>tail</code> represents the contents of the
  120. file, either as a list of character string representations for a text
  121. file or as a list in an unconstrained format for a data file.
  122. </ul>
  123. <p>For each item in the list, the function performs the following steps.
  124. <ol type=1 start=1>
  125. <li>It decides whether to open a file
  126. for overwriting or appending based on the <code>head</code> of the
  127. <code>head</code>.
  128. <li>It uses the <code>tail</code> of the <code>head</code> to find out the
  129. file name from <code>avm_path_name</code>, in order to open it.
  130. <li>If the
  131. <var>ask_to_overwrite_mode</var> flag is set and the file is found to
  132. exist already, the function will print one of the following messages to
  133. standard output, depending on whether the file is to be overwritten or
  134. appended.
  135. <ul>
  136. <li><var>program-name</var><code>: overwrite </code><var>filename</var><code>? (y/n)</code>
  137. <li><var>program-name</var><code>: append to </code><var>filename</var><code>? (y/n)</code>
  138. </ul>
  139. It will then insist on either <kbd>y</kbd> or <kbd>n</kbd> as an answer before continuing.
  140. <li>If the <var>ask_to_overwrite</var> flag has not been set, or the file did
  141. not previously exist, or the answer of <kbd>y</kbd> was given, the preamble
  142. and contents of the file are then written with <code>avm_output</code>.
  143. <li>If permission to write or append was denied, one of the following
  144. messages is reported to standard output, and the data that were to be
  145. written are lost.
  146. <a name="index-g_t_0040code_007bnot-writing_007d-file-name-571"></a><a name="index-g_t_0040code_007bwriting_007d-file-name-572"></a>
  147. <ul>
  148. <li><var>program-name</var><code>: not writing </code><var>filename</var>
  149. <li><var>program-name</var><code>: not appending </code><var>filename</var>
  150. </ul>
  151. <li>If permission was granted to write or append to the file or the <var>verbose_mode</var> flag is
  152. set, one of the messages
  153. <ul>
  154. <li><var>program-name</var><code>: writing </code><var>filename</var>
  155. <li><var>program-name</var><code>: appending </code><var>filename</var>
  156. </ul>
  157. <p>is sent to standard output.
  158. </ol>
  159. <p><a name="index-standard-output-573"></a>If any files are to be written to standard output, which would be
  160. indicated by a <code>NULL</code> path, they are not written until all other
  161. files in the list are written. This feature is in the interest of
  162. <a name="index-security-574"></a>security, as it makes it more difficult for malicious or inept virtual
  163. code to alter the appearance of the console through standard output until after
  164. the interactive dialogue has taken place. Permission is not solicited
  165. for writing to standard output, and it will not be closed.
  166. <p>Any of the fatal errors or i/o errors possible with <code>avm_output</code> or
  167. <code>avm_path_name</code> are also possible with this function, as well as
  168. the following additional ones.
  169. <a name="index-g_t_0040code_007binvalid-file-specification_007d-575"></a><a name="index-g_t_0040code_007bcan_0027t-close_007d-576"></a><a name="index-g_t_0040code_007bcan_0027t-write_007d-577"></a>
  170. <ul>
  171. <li><var>program-name</var><code>: invalid file specification</code>
  172. <li><var>program-name</var><code>: can't close </code><var>filename</var>
  173. <li><var>program-name</var><code>: can't write </code><var>filename</var>
  174. </ul>
  175. <p>The last two are non-fatal i/o errors that will be accompanied by an
  176. <a name="index-g_t_0040code_007bstrerror_007d-578"></a>explanation from the <code>strerror</code> function if the host supports
  177. it. The other message is the result of a badly formatted
  178. <var>data</var> parameter.
  179. </blockquote></div>
  180. <div class="defun">
  181. &mdash; Function: void <b>avm_put_bytes</b> (<var>list bytes</var>)<var><a name="index-avm_005fput_005fbytes-579"></a></var><br>
  182. <blockquote>
  183. <p>This function takes a list of character representations, converts them
  184. to characters, and sends them to standard output. There is no chance of
  185. a memory overflow, but the following other errors are possible.
  186. <a name="index-g_t_0040code_007binvalid-text-format_007d-580"></a><a name="index-g_t_0040code_007bcan_0027t-write_007d-581"></a>
  187. <ul>
  188. <li><var>program-name</var><code>: invalid text format (code </code><var>nn</var><code>)</code>
  189. <li><var>program-name</var><code>: can't write to standard output</code>
  190. </ul>
  191. <p>The latter is non-fatal, but the former causes the program to abort.
  192. It is caused when any member of the list <var>bytes</var> is not a
  193. character representation appearing in <a href="Character-Table.html#Character-Table">Character Table</a>.
  194. </p></blockquote></div>
  195. <div class="defun">
  196. &mdash; Function: void <b>avm_initialize_formout</b> ()<var><a name="index-avm_005finitialize_005fformout-582"></a></var><br>
  197. <blockquote><p>This function initializes some data structures used locally by the other
  198. functions in this section, and should be called at the beginning of a
  199. run before any of them is called.
  200. </p></blockquote></div>
  201. <div class="defun">
  202. &mdash; Function: void <b>avm_count_formout</b> ()<var><a name="index-avm_005fcount_005fformout-583"></a></var><br>
  203. <blockquote><p>This function doesn't do anything in the current version of the library,
  204. but should be called after the last call to any of the other functions
  205. in this section. Future versions of the library might use this function
  206. for cleaning up some internal data structures, and client programs that
  207. call it will maintain compatibility with them.
  208. </p></blockquote></div>
  209. </body></html>