The "loop and a half"

Marko Rauhamaa marko at pacujo.net
Fri Oct 6 13:55:25 EDT 2017


bartc <bc at freeuk.com>:

> The internal utilities used within an operating system, primarily
> intended for file or redirected i/o with no live interaction, should be
> distinct from those designed to be used directly with a live user.
>
> Or is it against the rules of Unix to have two different versions of a
> program?
>
> Then you might have 'sort' for the non-interactive version, and 'isort'
> or whatever for the interactive.

You are right that interactive programs exist and operate very
differently from what are known as *tools*.

Interactive Linux programs include:

    LibreOffice
    NetworkManager
    Firefox
    Skype
    XBoard
    Emacs
    GIMP
    top
    gdb

etc.

I don't know if anybody has seen a market/need for an interactive sort
program, but there's nothing preventing you from writing one.

> So I can't see the point of using that same pattern of input usage
> (reading from stream, file, pipe whatever until interrupted with an
> EOF signal) for a true interactive program that is used in a sensible
> manner.

So go ahead and write an interactive sort program. It might employ a
mouse, joystick, iris scanner, touchscreen, sound effects or whatever
facilities you would think would be useful for the human user.

> A program like 'python' might fit the bill too. Here, you can give it
> the name of a file, OR say nothing, and it will take input from stdin.

Personally, I think stdin is a bit lame as a stimulus source for an
interactive program. That's not even what stdin is primarily meant for;
stdin is meant to be the input data for a job. Similarly, stdout is
meant to be the result of the computation. Stderr, then, is used to
deliver optional diagnostic messages, ie, metainfo about the
computation.

For interaction with a human, you should look into frameworks like Qt or
curses.

> Funnily enough, you now get a message, and a prompt. And when entering
> multiple lines of code, you get a prompt before each line. You can
> also terminate the sequence by entering a blank line.
>
> So it IS possible to do it properly after all!)

The Unix terminal can operate in two main modes: canonical and
non-canonical. The latter is typically used for interaction.

See:

   <URL: https://en.wikipedia.org/wiki/POSIX_terminal_interface#C
   anonical_mode_processing>


Marko



More information about the Python-list mailing list