dotcloud/docker — pull request #6933 by ztombol (2014-07-09 16:53:10+00:00)
Title: Fix expect script in mkimage-arch

# Overview

The previously removed expect snippet hung after an arbitrary number of matches, because the input ended up being sent sometimes between `pacstrap` finishing output and waiting for input (wrong), and sometimes after waiting for input (right). The problem is `pacstap` seem to discard keystrokes sent before it waits for input. This patch adds a 0.1 second delay before starting every input. This should be sufficient to make expect succeed on today's computers.

# Details

The arbitrary hangs are due to a common **timing issue**. From `autoexpect(1)`:

> A surprisingly large number of programs (rn, ksh, zsh, telnet, etc.) and devices (e.g., modems) ignore keystrokes that arrive "too quickly" after prompts. If you find your new script hanging up at one spot, try adding a short sleep just before the previous send.

This seems to be the case with `pacstrap` too.

Usual tactics don't work:
- `set send_slow {1 1}` alone does not change the situation, as it only adds delay *between* characters instead of *before* the whole input, thus protecting from buffer over-runs and not early sends.
- Matching until the end of the buffer, e.g. `-re "Install anyway. .Y/n. $"`, does not help either as the input may still arrive between `pacstrap` outputting the matched string and accepting input.

The **solution** is to add a short delay before sending input. This patch defines a new `send` procedure that adds a 0.1 second delay before sending input. This implementation was taken from `autoexpect`, but explicitely adding `sleep .1` before every send would work just as well.