Check input device is a terminal: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring, marked p2js incompatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13:
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>.
<
with Interfaces.C_Streams; use Interfaces.C_Streams;
Line 23:
Put_Line(Standard_Error, "stdin is a tty.");
end if;
end Test_tty;</
{{out}}
Line 35:
=={{header|BaCon}}==
<
PRINT terminal</
{{out}}
Line 51:
=={{header|C}}==
Use <code>isatty()</code> on file descriptor to determine if it's a TTY. To get the file descriptor from a <code>FILE*</code> pointer, use <code>fileno</code>:
<
#include <stdio.h> //for fileno()
Line 60:
: "stdin is not tty");
return 0;
}</
{{out}}
<pre>
Line 74:
Works with GnuCOBOL.
<
*> istty, check id fd 0 is a tty
*> Tectonics: cobc -xj istty.cob
Line 99:
goback.
end program istty.</
DISPLAY for fd 1 is directed to SYSERR to get some output during the various trials.
Line 124:
=={{header|Common Lisp}}==
{{Works with|SBCL}}
<
(format T "stdin is~:[ not~;~] a terminal~%"
(interactive-stream-p s)))</
{{Out}}
Line 137:
=={{header|Crystal}}==
<
File.new("/dev/tty").tty? #=> true
STDIN.tty? #=> true</
=={{header|D}}==
<
extern(C) int isatty(int);
Line 151:
else
writeln("Input doesn't come from tty.");
}</
{{out}}
<pre>C:\test
Line 160:
=={{header|FreeBASIC}}==
<
Open Cons For Input As #1
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar
Line 172:
Close #1
Sleep
</syntaxhighlight>
=={{header|Go}}==
{{libheader|Go sub-repositories}}
<
import (
Line 191:
fmt.Println("Who are you? You're not a terminal.")
}
}</
{{out}}
<pre>
Line 204:
Example uses [https://hackage.haskell.org/package/unix <tt>unix</tt>] package:
<
import System.Posix.IO (stdInput)
Line 214:
putStrLn $ if isTTY
then "stdin is TTY"
else "stdin is not TTY"</
=={{header|Jsish}}==
<
;Interp.conf().subOpts.istty;
Line 224:
Interp.conf().subOpts.istty ==> false
=!EXPECTEND!=
*/</
{{out}}
Line 236:
=={{header|Julia}}==
<
if isa(STDIN, Base.TTY)
println("This program sees STDIN as a TTY.")
Line 242:
println("This program does not see STDIN as a TTY.")
end
</syntaxhighlight>
{{out}}
Line 251:
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<
import platform.posix.*
Line 261:
println("stdin is not a terminal")
}
</syntaxhighlight>
{{out}}
Line 270:
=={{header|Nemerle}}==
There is no explicit way (ie <tt>isatty()</tt>)to do this; however, if we ''assume'' that standard input ''is'' a terminal, we can check if the input stream has been redirected (presumably to something other than a terminal).
<
=={{header|Nim}}==
Using function "isatty" of standard module "terminal" which accepts a File as argument.
<
echo if stdin.isatty: "stdin is a terminal" else: "stdin is not a terminal"</
{{out}}
Line 288:
=={{header|OCaml}}==
<
print_endline (
if Unix.isatty Unix.stdin
then "Input comes from tty."
else "Input doesn't come from tty."
)</
Testing in interpreted mode:
Line 304:
=={{header|Ol}}==
<
(define (isatty? fd) (syscall 16 fd 19))
(print (if (isatty? stdin)
"Input comes from tty."
"Input doesn't come from tty."))
</syntaxhighlight>
=={{header|Perl}}==
<
use warnings;
use 5.010;
Line 320:
else {
say "Input doesn't come from tty.";
}</
$ perl istty.pl
Line 328:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no input redirection in a browser!)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"stdin:%t, stdout:%t, stderr:%t\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">isatty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 341:
=={{header|Pike}}==
<
{
if(Stdio.Terminfo.is_tty())
Line 347:
else
write("Input doesn't come from tty.\n");
}</
{{out}}
Line 356:
=={{header|Python}}==
<
if stdin.isatty():
print("Input comes from tty.")
else:
print("Input doesn't come from tty.")</
$ python istty.py
Line 371:
{{trans|Python}}
<
to_stack( 1 if stdin.isatty() else 0)|
python ] is ttyin ( --> b )
Line 378:
[ say "Looks like a teletype." ]
else
[ say "Not a teletype." ]</
{{out}}
Line 385:
=={{header|Racket}}==
<
(terminal-port? (current-input-port))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang=raku
$ raku istty.raku
Line 400:
=={{header|REXX}}==
<
if queued() then say 'input comes from the terminal.'
Line 406:
/*stick a fork in it, we're done.*/
</syntaxhighlight>
=={{header|Ring}}==
<
# Project : Check input device is a terminal
Line 426:
see SystemCmd("mycmd.bat")
ok
</syntaxhighlight>
Output:
<pre>
Line 434:
=={{header|Ruby}}==
Example from the docs.
<
File.new("/dev/tty").isatty #=> true</
=={{header|Rust}}==
<
extern crate libc;
Line 449:
println!("stdout is not tty");
}
}</
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<
object IsATty extends App {
Line 473:
println("tty " + apply(true))
}</
=={{header|Standard ML}}==
<
=={{header|Tcl}}==
Tcl automatically detects whether <tt>stdin</tt> is coming from a terminal (or a socket) and sets up the channel to have the correct type. One of the configuration options of a terminal channel is <tt>-mode</tt> (used to configure baud rates on a real serial terminal) so we simply detect whether the option is present.
<
puts "Input doesn't come from tty."
} else {
puts "Input comes from tty."
}</
Demonstrating:
<pre>
Line 494:
=={{header|UNIX Shell}}==
<
if [ -t 0 ]
Line 501:
else
echo "Input is NOT a terminal"
fi</
=={{header|Wren}}==
<
System.print("Input device is a terminal? %(Stdin.isTerminal ? "Yes" : "No")")</
{{out}}
Line 515:
=={{header|zkl}}==
On Unix, check to see if stdin's st_mode is a character device.
<
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() }
S_ISCHR(File.stdin).println();</
{{out}}
<pre>
|