title: How can I flush the input buffer in an expect script?
body: <p>I'm writing an Expect script and am having trouble dealing with the shell prompt (on Linux). My Expect script spawns <code>rlogin</code> and the remote system is using <code>ksh</code>. The prompt on the remote system contains the current directory followed by <code>" &gt; "</code> (space greater-than space). A script snippet might be:</p>
  
  <pre><code>send "some command here\r"
  expect " &gt; "
  </code></pre>
  
  <p>This works for simple commands, but things start to go wrong when the command I'm sending exceeds the width of the terminal (or more precisely, what <code>ksh</code> thinks is the width of the terminal). In that case, <code>ksh</code> does some weird horizontal scrolling of the interactive command line, which seems to rewrite the prompt and stick an extra " > " in the output. Naturally this causes the Expect script to get confused and out of sync when there appears to be more than one prompt in the output after executing a command (my script contains several <code>send</code>/<code>expect</code> pairs).</p>
  
  <p>I've tried changing <code>PS1</code> on the remote system to something more distinctive like "prompt> " but a similar problem arises which indicates to me that's not the right way to solve this.</p>
  
  <p>What I'm thinking might help is the ability for the script to tell Expect that "I know I'm properly synchronised with the remote system at this point, so flush the input buffer now." The <code>expect</code> statement has the <code>-notransfer</code> flag which <em>doesn't</em> discard the input buffer even if the pattern does match, so I think I need the opposite of that.</p>
  
  <p>Are there any other useful techniques that I can use to make the remote shell behave more predictably? I understand that Expect goes through a lot of work to make sure that the spawned session <em>appears</em> to be interactive to the remote system, but I'd rather that some of the more annoying interactive features (such as the horizontal scrolling of <code>ksh</code>) be turned off.</p>