Characters-and-Strings.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <html lang="en">
  2. <head>
  3. <title>Characters and Strings - 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="Library-Reference.html#Library-Reference" title="Library Reference">
  9. <link rel="prev" href="Lists.html#Lists" title="Lists">
  10. <link rel="next" href="File-Manipulation.html#File-Manipulation" title="File Manipulation">
  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="Characters-and-Strings"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="File-Manipulation.html#File-Manipulation">File Manipulation</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Lists.html#Lists">Lists</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Library-Reference.html#Library-Reference">Library Reference</a>
  32. <hr>
  33. </div>
  34. <h3 class="section">3.2 Characters and Strings</h3>
  35. <p><a name="index-character-strings-506"></a>If a C program is to interact with a virtual code application by
  36. exchanging text, it uses the representation for characters described in
  37. <a href="Character-Table.html#Character-Table">Character Table</a>. This convention would be inconvenient without a
  38. suitable API, so the functions in this section address the need. These
  39. functions are declared in the header file <samp><span class="file">chrcodes.h</span></samp>.
  40. <p>Some of these functions have two forms, with one of them having the
  41. word <code>standard</code> as part of its name. The reason is to cope with
  42. multiple character encodings. Versions of <code>avram</code> prior to 0.1.0
  43. <a name="index-character-encodings-507"></a><a name="index-multiple-character-encodings-508"></a>used a different character encoding than the one documented in
  44. <a href="Character-Table.html#Character-Table">Character Table</a>. The functions described in <a href="Version-Management.html#Version-Management">Version Management</a> can be used to select backward compatible operation with
  45. the older character encoding. The normal forms of the functions in
  46. this section will use the older character set if a backward
  47. compatibility mode is indicated, whereas the standard forms will use
  48. the character encoding documented in <a href="Character-Table.html#Character-Table">Character Table</a> regardless.
  49. <p>Standard encodings should always be assumed for library and function
  50. <a name="index-standard-character-encoding-509"></a>names associated with the <code>library</code> combinator (<a href="Calling-existing-library-functions.html#Calling-existing-library-functions">Calling existing library functions</a>), and for values of lists defined by
  51. <code>avm_list_of_value</code> (<a href="Primitive-types.html#Primitive-types">Primitive types</a>), but version
  52. dependent encodings should be used for all other purposes such as
  53. error messages. Alternatively, the normal version dependent forms of
  54. the functions below can be used safely in any case if backward
  55. <a name="index-backward-compatability-510"></a>compatibility is not an issue. This distinction is viewed as a
  56. transitional feature of the API that will be discontinued eventually
  57. when support for the old character set is withdrawn and the
  58. <code>standard</code> forms are be removed.
  59. <div class="defun">
  60. &mdash; Function: list <b>avm_character_representation</b> (<var>int character</var>)<var><a name="index-avm_005fcharacter_005frepresentation-511"></a></var><br>
  61. </div>
  62. <div class="defun">
  63. &mdash; Function: list <b>avm_standard_character_representation</b> (<var>int character</var>)<var><a name="index-avm_005fstandard_005fcharacter_005frepresentation-512"></a></var><br>
  64. <blockquote><p>This function takes an integer character code and returns a copy of
  65. the list representing it, as per the table in <a href="Character-Table.html#Character-Table">Character Table</a>. Because the copy is shared, no memory is allocated by this
  66. function so there is no possibility of overflow. Nevertheless, it is
  67. the responsibility of the caller dispose of the list when it is no
  68. longer needed by <code>avm_dispose</code>, just as if the copy were not
  69. shared (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a>). For performance reasons, this
  70. function is implemented as a macro. If the argument is outside the
  71. range of zero to 255, it is masked into that range.
  72. </p></blockquote></div>
  73. <div class="defun">
  74. &mdash; Function: int <b>avm_character_code</b> (<var>list operand</var>)<var><a name="index-avm_005fcharacter_005fcode-513"></a></var><br>
  75. </div>
  76. <div class="defun">
  77. &mdash; Function: int <b>avm_standard_character_code</b> (<var>list operand</var>)<var><a name="index-avm_005fstandard_005fcharacter_005fcode-514"></a></var><br>
  78. <blockquote><p>This function takes a list as an argument and returns the corresponding
  79. character code, as per <a href="Character-Table.html#Character-Table">Character Table</a>. If the argument does not
  80. represent any character, a value of <code>-1</code> is returned.
  81. </p></blockquote></div>
  82. <div class="defun">
  83. &mdash; Function: list <b>avm_strung</b> (<var>char *string</var>)<var><a name="index-avm_005fstrung-515"></a></var><br>
  84. </div>
  85. <div class="defun">
  86. &mdash; Function: list <b>avm_standard_strung</b> (<var>char *string</var>)<var><a name="index-avm_005fstandard_005fstrung-516"></a></var><br>
  87. <blockquote><p>This function takes a pointer to a null terminated character string and
  88. returns the list obtained by translating each character into its list
  89. representation and enqueuing them together. Memory needs to be allocated
  90. for the result, and if there isn't enough available, an error message is
  91. written to standard error and the process is terminated. This function
  92. is useful to initialize lists from hard coded strings at the beginning
  93. of a run, as in this example.
  94. <pre class="example"> hello_string = avm_strung("hello");
  95. </pre>
  96. <p>This form initializes a single string, but to initialize a one line
  97. message suitable for writing to a file, it would have to be a list of
  98. strings, as in this example.
  99. <pre class="example"> hello_message = avm_join(avm_strung("hello"),NULL);
  100. </pre>
  101. <p>The latter form is used internally by the library for initializing
  102. most of the various error messages that can be returned by other functions.
  103. </p></blockquote></div>
  104. <div class="defun">
  105. &mdash; Function: list <b>avm_recoverable_strung</b> (<var>char *string, int *fault</var>)<var>;<a name="index-avm_005frecoverable_005fstrung-517"></a></var><br>
  106. </div>
  107. <div class="defun">
  108. &mdash; Function: list <b>avm_recoverable_standard_strung</b> (<var>char *string, int *fault</var>)<var>;<a name="index-avm_005frecoverable_005fstandard_005fstrung-518"></a></var><br>
  109. <blockquote><p>This function is like <code>avm_strung</code> except that if it runs out of memory
  110. it sets the integer referenced by <var>fault</var> to a non-zero value and returns
  111. instead of terminating the process.
  112. </p></blockquote></div>
  113. <div class="defun">
  114. &mdash; Function: char <b>*avm_unstrung</b> (<var>list string, list *message, int *fault</var>)<var><a name="index-g_t_002aavm_005funstrung-519"></a></var><br>
  115. </div>
  116. <div class="defun">
  117. &mdash; Function: char <b>*avm_standard_unstrung</b> (<var>list string, list *message, int *fault</var>)<var><a name="index-g_t_002aavm_005fstandard_005funstrung-520"></a></var><br>
  118. <blockquote><p>This function performs an inverse operation to
  119. <code>avm_recoverable_strung</code>, taking a list representing a character
  120. string to the character string in ASCII null terminated form as per
  121. the standard C representation. Memory is allocated for the result by
  122. this function which should be freed by the caller.
  123. <p>In the event of an exception, the integer referenced by <code>fault</code>
  124. is assigned a non-zero value and an error message represented as a
  125. list is assigned to the list referenced by <code>message</code>. The error
  126. message should be reclaimed by the caller with <code>avm_dispose</code>
  127. (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a> if it is non-empty. Possible error messages
  128. are <code>&lt;'memory overflow'&gt;</code>, <code>&lt;'counter overflow'&gt;</code>, and
  129. <code>&lt;'invalid text format'&gt;</code>.
  130. </p></blockquote></div>
  131. <div class="defun">
  132. &mdash; Function: list <b>avm_scanned_list</b> (<var>char *string</var>)<var><a name="index-avm_005fscanned_005flist-521"></a></var><br>
  133. <blockquote><p>An application that makes use of virtual code snippets or data that are
  134. known at compile time can use this function to initialize them. The
  135. argument is a string in the format described in <a href="Concrete-Syntax.html#Concrete-Syntax">Concrete Syntax</a>,
  136. and the result is the list representing it. For example, the program
  137. discussed in <a href="Example-Script.html#Example-Script">Example Script</a> could be hard coded into a C program
  138. by pasting the data from its virtual code file into an expression of
  139. this form.
  140. <pre class="example"> cat_program = avm_scanned_list("sKYQNTP\\");
  141. </pre>
  142. <p>Note that the backslash character in the original data has to be
  143. preceded by an extra backslash in the C source, because backslashes
  144. usually mean something in C character constants.
  145. <p>The <code>avm_scanned_list</code> function needs to allocate memory. If there
  146. isn't enough memory available, it writes a message to standard error and
  147. causes the process to exit.
  148. </p></blockquote></div>
  149. <div class="defun">
  150. &mdash; Function: list <b>avm_multiscanned</b> (<var>char **strings</var>)<var><a name="index-avm_005fmultiscanned-522"></a></var><br>
  151. <blockquote><p>Sometimes it may be useful to initialize very large lists from
  152. strings, but some C compilers impose limitations on the maximum length
  153. of a string constant, and the ISO standard for C requires only 512
  154. bytes. This function serves a similar purpose to
  155. <code>avm_scanned_list</code>, but allows the argument to be a pointer to a
  156. null terminated array of strings instead of one long string, thereby
  157. circumventing this limitation in the compiler.
  158. <pre class="example"> char *code[] = {"sKYQ","NTP\\",NULL};
  159. ...
  160. cat_program = avm_multiscanned(code);
  161. </pre>
  162. <p>If there is insufficient memory to allocate the list this function needs
  163. to create, it causes an error message to be written to standard error,
  164. and then kills the process.
  165. </p></blockquote></div>
  166. <div class="defun">
  167. &mdash; Function: char* <b>avm_prompt</b> (<var>list prompt_strings</var>)<var><a name="index-avm_005fprompt-523"></a></var><br>
  168. <blockquote><p>This function takes a list representing a list of character strings, and
  169. returns its translation to a character string with the sequence 13 10
  170. used as a separator. For example, given a tree of this form
  171. <pre class="example"> some_message = avm_join(
  172. avm_strung("hay"),
  173. avm_join(
  174. avm_strung("you"),
  175. NULL));
  176. </pre>
  177. <p>the result returned by <code>prompt_strings(some_message)</code> would be a
  178. pointer to a null terminated character string equivalent to the C constant
  179. <code>"hay\13\10you"</code>.
  180. <p>Error messages are printed and the process terminated in the event of
  181. either a memory overflow or an invalid character representation.
  182. <p>This function is used by <code>avram</code> in the evaluation of interactive
  183. <a name="index-interactive-applications-524"></a>virtual code applications, whose output has to be compared to the output
  184. from a shell command in this format. The separator is chosen to be
  185. compatible with the <code>expect</code> library.
  186. </p></blockquote></div>
  187. <div class="defun">
  188. &mdash; Function: char* <b>avm_recoverable_prompt</b> (<var>list prompt_strings, list *message, int *fault</var>)<var><a name="index-avm_005frecoverable_005fprompt-525"></a></var><br>
  189. <blockquote><p>This function performs the same operation as <code>avm_prompt</code> but
  190. allows the caller to handle exceptional conditions. If an exception
  191. such as a memory overflow occurs, the integer referenced by
  192. <code>fault</code> is assigned a non-zero value and a representation of the
  193. error message as a list of strings is assigned to the list referenced
  194. by <code>message</code>.
  195. <p>This function is used to by <code>avram</code> to evaluate the
  196. <code>interact</code> combinator (<a href="Interaction-combinator.html#Interaction-combinator">Interaction combinator</a>), when
  197. terminating in the event of an error would be inappropriate.
  198. </p></blockquote></div>
  199. <div class="defun">
  200. &mdash; Function: void <b>avm_initialize_chrcodes</b> ()<var><a name="index-avm_005finitialize_005fchrcodes-526"></a></var><br>
  201. <blockquote><p>This function has to be called before any of the other character
  202. conversion functions in this section, or else their results are
  203. undefined. It performs the initialization of various internal data
  204. structures.
  205. </p></blockquote></div>
  206. <div class="defun">
  207. &mdash; Function: void <b>avm_count_chrcodes</b> ()<var><a name="index-avm_005fcount_005fchrcodes-527"></a></var><br>
  208. <blockquote><p>This function can be called at the end of a run, after the last call to
  209. any of the other functions in this section, but before
  210. <code>avm_count_lists</code> if that function is also being used. The purpose
  211. of this function is to detect and report memory leaks. If any memory
  212. associated with any of these functions has not been reclaimed by the
  213. client program, a message giving the number of unreclaimed lists will be
  214. written to standard error.
  215. </p></blockquote></div>
  216. </body></html>