Line-Oriented-Interaction.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <html lang="en">
  2. <head>
  3. <title>Line Oriented Interaction - 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="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Output From Interactive Applications">
  9. <link rel="prev" href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications" title="Output From Interactive Applications">
  10. <link rel="next" href="Character-Oriented-Interaction.html#Character-Oriented-Interaction" title="Character Oriented Interaction">
  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="Line-Oriented-Interaction"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Character-Oriented-Interaction.html#Character-Oriented-Interaction">Character Oriented Interaction</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications">Output From Interactive Applications</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Output-From-Interactive-Applications.html#Output-From-Interactive-Applications">Output From Interactive Applications</a>
  32. <hr>
  33. </div>
  34. <h5 class="subsubsection">2.6.4.1 Line Oriented Interaction</h5>
  35. <p>Within this general pattern, more specific styles of interaction are
  36. possible. In the simplest one to explain first, the result returned by
  37. the function is always a data structure of the form
  38. <a name="index-command-line-231"></a><code>(</code><var>state</var><code>,(</code><var>command lines</var><code>,</code><var>prompts</var><code>))</code>, wherein the
  39. fields have these interpretations.
  40. <dl>
  41. <dt><var>state</var><dd>is a tree incorporating any data in any format that the application
  42. needs to remember from one invocation to the next.
  43. <br><dt><var>command lines</var><dd>is a list of character strings that are piped to the standard input
  44. stream of a separately spawned process. The process may persist from one
  45. invocation of the function to the next, or may be spawned each time.
  46. <br><dt><var>prompts</var><dd>is a non-empty list of character strings containing a suffix of the text
  47. expected from the standard output stream of the process as a result of
  48. sending the command lines to it.
  49. </dl>
  50. <p>On each iteration, <code>avram</code> sends the command line character strings
  51. <a name="index-spawning-processes-232"></a>to a separately spawned process, with line breaks between them if there
  52. are more than one command. If a process remains from the previous iteration that
  53. has not terminated itself, the list of command lines is sent to the same
  54. process. If no such process already exists, the first string in the list
  55. of command lines is treated as a shell command and used to spawn the
  56. <a name="index-g_t_0040code_007bexp_005fpopen_007d-233"></a>process (using the <code>exp_popen</code> library function), and the remaining
  57. strings are sent to the newly spawned process.
  58. <p>Normally processes spawned with commands that invoke interactive command
  59. line interpreters of their own, such as <samp><span class="command">bash</span></samp>, <samp><span class="command">ftp</span></samp> or
  60. <samp><span class="command">bc</span></samp>, will persist indefinitely unless the command causing them
  61. to exit is issued or some other event kills them. Processes spawned with
  62. non-interactive commands, such as <samp><span class="command">ls</span></samp> or <samp><span class="command">pwd</span></samp>, will
  63. terminate when the last of their initial output has been received.
  64. <p>In the case of processes that persist after being spawned, <code>avram</code>
  65. needs some way of knowing when to stop waiting for more output from them
  66. so that it doesn't get stuck waiting forever. This purpose is served by
  67. the <var>prompts</var> field. This field could contain a single string
  68. holding the last thing the process will send before becoming quiescent,
  69. such as the strings <code>bash$ </code> or <code>ftp&gt; </code> in the above
  70. examples. Alternatively, a sequence of more than one prompt string can
  71. be used to indicate that the corresponding sequence of lines must be
  72. detected. An empty string followed by <code>ftp&gt; </code> would indicate that
  73. the <code>ftp&gt; </code> prompt is expected to be immediately preceded by a line
  74. <a name="index-prompts-234"></a>break. There is also the option of using prompt strings to indicate a
  75. pattern that does not necessarily imply quiescence, but is a more
  76. convenient point at which to stop reading the output from the process.
  77. <p>For processes spawned with commands that do not start their own
  78. interactive command line interpreters, such as <samp><span class="command">ls</span></samp> or
  79. <samp><span class="command">pwd</span></samp>, it may be preferable to read all the output from them
  80. until they terminate. To achieve this effect, the list of prompt strings
  81. should contain only the single string containing only the single
  82. <code>EOF</code> character (usually code 4) or any other character that is
  83. certain not to occur in the output of the process. This technique is
  84. based on the assumption that the process was spawned originally with the
  85. command in question, not that such a command is sent to an existing
  86. shell process.
  87. <p>In any case, when enough output has been received from the process, it
  88. is collected into a list of received strings including the prompt
  89. strings at the end (if they were received), and the function is applied
  90. to the pair <code>(</code><var>state</var><code>,</code><var>received strings</var><code>)</code>. If the cycle is
  91. to continue, the result returned by the function will include a new
  92. state, a new list of command lines, and a new list of prompt strings. A
  93. result of <code>nil</code> will cause the computation to terminate.
  94. <p>There are some unusual situations that could occur in the course of line
  95. oriented interaction, and are summarized as follows.
  96. <ul>
  97. <li>If the process terminates before any pattern matching the prompt
  98. strings is received from it, all of the output from the process up to
  99. the point where it terminated is collected into the <var>received
  100. strings</var> list and passed to the function. This situation includes cases
  101. where the process terminates immediately upon being spawned, but not
  102. abnormal completion of the <code>exp_popen</code> library function, which is
  103. a fatal error. This feature of the interface is what allows <code>EOF</code>
  104. to be used for collecting all the output at once from a non-interactive
  105. command.
  106. <li>If the list of <var>command lines</var> is empty, and no process
  107. currently exists due to a previous iteration, the effect is the same as
  108. if the process terminates unexpectedly before outputting anything. I.e.,
  109. the function is applied to a pair containing an empty list of received
  110. strings. There is no good reason for an application to get into this
  111. situation.
  112. <li>If the list of <var>command lines</var> is empty but a process persists
  113. from a previous iteration, no output is sent to it, but receiving from
  114. it proceeds normally. This feature of the interface could be used
  115. effectively by applications intended to process the received data in
  116. <a name="index-deadlock-235"></a>parts, but will cause deadlock if the process is already quiescent.
  117. <li>All character strings have to consist of lists of valid
  118. 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.
  119. <li>If the list of <var>prompt strings</var> contains only the empty
  120. string, <code>avram</code> will not wait to receive anything from the process,
  121. but will proceed with the next iteration immediately. If this effect is
  122. intended, care must be taken not to confuse the empty list
  123. of <var>prompt strings</var> with the list containing the empty string. The former
  124. indicates character oriented interaction, which is explained next.
  125. </ul>
  126. </body></html>