spec
Software for Diffraction
2.4.2.4. - Keyboard Input
input()
-
Reads a line of input from the keyboard.
Leading
white space and the trailing newline are removed and the string
is returned.
Returns the null string
""
if only white space was entered.
Example:
1.FOURC> def change_it '{
2.quot> local it
3.quot> printf("Change it? ");
4.quot> if ((it=input()) != "")
5.quot> change_mac(it)
6.quot> }'
7.FOURC>
input(prompt)
-
As above, but prompts with the string
prompt
.
Examples:
1.FOURC> input("Hit return when ready ... ")
Hit return when ready ... <return>
2.FOURC>
input(n)
-
This function behaves differently depending on whether the
input source is the keyboard or a pipe from another program (where
spec is invoked with the
-p fd pid
option, with
nonzero
fd
.)
In the usual case,
if
n
is less than or equal to zero,
the tty state is set to
"cbreak"
mode and
input echo is turned off.
Then
input()
checks to see if the user has typed a character and
immediately returns a null string if nothing has been typed.
Otherwise, it returns a string
containing the single (or first) character the user typed.
If
n
is less than zero, the cbreak, no-echo mode remains in effect when
input()
returns.
If
n
is greater than zero,
the normal tty state is restored (as it is also
if there is
an error, if the user types
^C
or if the user enters the
exit
command).
Also,
no characters are read and the null string is returned.
The normal state is also restored
before the next main prompt is issued, whether due
to an error, a
^C
,
or through the normal flow of the program.
On the other hand, when spec is invoked with the
-p fd pid
option, with
nonzero
fd
,
input()
reads nothing but does return the number of characters available to be
read.
If
n
is nonzero,
input()
simply reads and returns a line of text, as if it had been invoked
with no argument.
yesno(val)
-
Reads a line of input from the keyboard.
The function returns 1 if the user answers with
a string beginning with
Y
, y
or
1
. The value of
val
is returned if the user simply enters return.
Otherwise the function returns zero.
yesno(prompt, val)
-
As above, but prompts the user with the string
prompt
.
The characters
" (YES)? "
are appended to the prompt string if
val
is nonzero.
Otherwise the characters
" (NO)? "
are added.
getval(val)
-
Reads a line of input from the keyboard.
If the user enters a value, that value is returned.
The value of
val
is returned if the user simply enters return.
The function works with both number and string values.
getval(prompt, val)
-
As above, but prompts the user with the string
prompt
.
The string is printed followed by
the current value of
val
in parenthesis, a question mark and a space.
For example,
1.FOURC> DATAFILE = getval("Data file", DATAFILE)
Data file (pt100.133)? <return>
2.FOURC>
getsval(prompt, val)
-
Like
getval()
above, prompts the user with
the string
prompt
,
if present, then waits for a user response.
The
value of
val
is returned if the user simply enters return.
If the
prompt string
prompt
is present, the string is printed followed
by the current value of
val
in parenthesis, a question mark and a
space.
Unlike
getval()
, this function does not convert hexadecimal
or octal input (number strings that begin with 0, 0x or 0X) to the
corresponding decimal value.
Rather, the
getsval()
function returns
the literal string as entered.