Line-Oriented-Interaction.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on December 10, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 2.6.4.1 Line Oriented Interaction</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 2.6.4.1 Line Oriented Interaction">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 2.6.4.1 Line Oriented Interaction">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Line-Oriented-Interaction"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Character-Oriented-Interaction.html#Character-Oriented-Interaction" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Line-Oriented-Interaction-1"></a>
  58. <h4 class="subsubsection">2.6.4.1 Line Oriented Interaction</h4>
  59. <p>Within this general pattern, more specific styles of interaction are
  60. possible. In the simplest one to explain first, the result returned by
  61. the function is always a data structure of the form
  62. <a name="index-command-line-4"></a>
  63. <code>(<var>state</var>,(<var>command lines</var>,<var>prompts</var>))</code>, wherein the
  64. fields have these interpretations.
  65. </p>
  66. <dl compact="compact">
  67. <dt> <var>state</var></dt>
  68. <dd><p>is a tree incorporating any data in any format that the application
  69. needs to remember from one invocation to the next.
  70. </p></dd>
  71. <dt> <var>command lines</var></dt>
  72. <dd><p>is a list of character strings that are piped to the standard input
  73. stream of a separately spawned process. The process may persist from one
  74. invocation of the function to the next, or may be spawned each time.
  75. </p></dd>
  76. <dt> <var>prompts</var></dt>
  77. <dd><p>is a non-empty list of character strings containing a suffix of the text
  78. expected from the standard output stream of the process as a result of
  79. sending the command lines to it.
  80. </p></dd>
  81. </dl>
  82. <p>On each iteration, <code>avram</code> sends the command line character strings
  83. <a name="index-spawning-processes-1"></a>
  84. to a separately spawned process, with line breaks between them if there
  85. are more than one command. If a process remains from the previous iteration that
  86. has not terminated itself, the list of command lines is sent to the same
  87. process. If no such process already exists, the first string in the list
  88. of command lines is treated as a shell command and used to spawn the
  89. <a name="index-exp_005fpopen-1"></a>
  90. process (using the <code>exp_popen</code> library function), and the remaining
  91. strings are sent to the newly spawned process.
  92. </p>
  93. <p>Normally processes spawned with commands that invoke interactive command
  94. line interpreters of their own, such as <code>bash</code>, <code>ftp</code> or
  95. <code>bc</code>, will persist indefinitely unless the command causing them
  96. to exit is issued or some other event kills them. Processes spawned with
  97. non-interactive commands, such as <code>ls</code> or <code>pwd</code>, will
  98. terminate when the last of their initial output has been received.
  99. </p>
  100. <p>In the case of processes that persist after being spawned, <code>avram</code>
  101. needs some way of knowing when to stop waiting for more output from them
  102. so that it doesn&rsquo;t get stuck waiting forever. This purpose is served by
  103. the <var>prompts</var> field. This field could contain a single string
  104. holding the last thing the process will send before becoming quiescent,
  105. such as the strings <code>bash$ </code> or <code>ftp&gt; </code> in the above
  106. examples. Alternatively, a sequence of more than one prompt string can
  107. be used to indicate that the corresponding sequence of lines must be
  108. detected. An empty string followed by <code>ftp&gt; </code> would indicate that
  109. the <code>ftp&gt; </code> prompt is expected to be immediately preceded by a line
  110. <a name="index-prompts"></a>
  111. break. There is also the option of using prompt strings to indicate a
  112. pattern that does not necessarily imply quiescence, but is a more
  113. convenient point at which to stop reading the output from the process.
  114. </p>
  115. <p>For processes spawned with commands that do not start their own
  116. interactive command line interpreters, such as <code>ls</code> or
  117. <code>pwd</code>, it may be preferable to read all the output from them
  118. until they terminate. To achieve this effect, the list of prompt strings
  119. should contain only the single string containing only the single
  120. <code>EOF</code> character (usually code 4) or any other character that is
  121. certain not to occur in the output of the process. This technique is
  122. based on the assumption that the process was spawned originally with the
  123. command in question, not that such a command is sent to an existing
  124. shell process.
  125. </p>
  126. <p>In any case, when enough output has been received from the process, it
  127. is collected into a list of received strings including the prompt
  128. strings at the end (if they were received), and the function is applied
  129. to the pair <code>(<var>state</var>,<var>received strings</var>)</code>. If the cycle is
  130. to continue, the result returned by the function will include a new
  131. state, a new list of command lines, and a new list of prompt strings. A
  132. result of <code>nil</code> will cause the computation to terminate.
  133. </p>
  134. <p>There are some unusual situations that could occur in the course of line
  135. oriented interaction, and are summarized as follows.
  136. </p>
  137. <ul>
  138. <li> If the process terminates before any pattern matching the prompt
  139. strings is received from it, all of the output from the process up to
  140. the point where it terminated is collected into the <var>received
  141. strings</var> list and passed to the function. This situation includes cases
  142. where the process terminates immediately upon being spawned, but not
  143. abnormal completion of the <code>exp_popen</code> library function, which is
  144. a fatal error. This feature of the interface is what allows <code>EOF</code>
  145. to be used for collecting all the output at once from a non-interactive
  146. command.
  147. </li><li> If the list of <var>command lines</var> is empty, and no process
  148. currently exists due to a previous iteration, the effect is the same as
  149. if the process terminates unexpectedly before outputting anything. I.e.,
  150. the function is applied to a pair containing an empty list of received
  151. strings. There is no good reason for an application to get into this
  152. situation.
  153. </li><li> If the list of <var>command lines</var> is empty but a process persists
  154. from a previous iteration, no output is sent to it, but receiving from
  155. it proceeds normally. This feature of the interface could be used
  156. effectively by applications intended to process the received data in
  157. <a name="index-deadlock"></a>
  158. parts, but will cause deadlock if the process is already quiescent.
  159. </li><li> All character strings have to consist of lists of valid
  160. representations of non-null characters according to <a href="Character-Table.html#Character-Table">Character Table</a>, or else there will be some fatal error messages.
  161. </li><li> If the list of <var>prompt strings</var> contains only the empty
  162. string, <code>avram</code> will not wait to receive anything from the process,
  163. but will proceed with the next iteration immediately. If this effect is
  164. intended, care must be taken not to confuse the empty list
  165. of <var>prompt strings</var> with the list containing the empty string. The former
  166. indicates character oriented interaction, which is explained next.
  167. </li></ul>
  168. <hr size="1">
  169. <table cellpadding="1" cellspacing="1" border="0">
  170. <tr><td valign="middle" align="left">[<a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Previous section in reading order"> &lt; </a>]</td>
  171. <td valign="middle" align="left">[<a href="Character-Oriented-Interaction.html#Character-Oriented-Interaction" title="Next section in reading order"> &gt; </a>]</td>
  172. <td valign="middle" align="left"> &nbsp; </td>
  173. <td valign="middle" align="left">[<a href="Virtual-Machine-Specification.html#Virtual-Machine-Specification" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  174. <td valign="middle" align="left">[<a href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Up section"> Up </a>]</td>
  175. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Next chapter"> &gt;&gt; </a>]</td>
  176. <td valign="middle" align="left"> &nbsp; </td>
  177. <td valign="middle" align="left"> &nbsp; </td>
  178. <td valign="middle" align="left"> &nbsp; </td>
  179. <td valign="middle" align="left"> &nbsp; </td>
  180. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  181. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  182. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  183. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  184. </tr></table>
  185. <p>
  186. <font size="-1">
  187. This document was generated on <i>December 10, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  188. </font>
  189. <br>
  190. </p>
  191. </body>
  192. </html>