Check input device is a terminal: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Promote to task, lots of examples, most of the concerns and controversy has been resolved)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 11 users not shown)
Line 1:
[[Category:Terminal control]]
[[Category:Hardware]]
[[Category:Initialization]]
{{task}}
 
Line 8 ⟶ 11:
*   [[Check output device is a terminal]]
<br><br>
 
=={{header|Ada}}==
{{works with|GNAT}}
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>.
 
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C_Streams; use Interfaces.C_Streams;
 
Line 23 ⟶ 25:
Put_Line(Standard_Error, "stdin is a tty.");
end if;
end Test_tty;</langsyntaxhighlight>
 
{{out}}
Line 34 ⟶ 36:
</pre>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<lang freebasic>terminal = isatty(0)
<syntaxhighlight lang="freebasic">terminal = isatty(0)
PRINT terminal</lang>
PRINT terminal</syntaxhighlight>
 
{{out}}
Line 48 ⟶ 51:
prompt$ ./istty <<<"testing"
0</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">
Open Cons For Input As #1
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar
' de la consola para leer o escribir.
 
If Err Then
Print "Input doesn't come from tt."
Else
Print "Input comes from tty."
End If
Close #1
Sleep
</syntaxhighlight>
 
 
=={{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>:
<langsyntaxhighlight lang="c">#include <unistd.h> //for isatty()
#include <stdio.h> //for fileno()
 
Line 60 ⟶ 79:
: "stdin is not tty");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 70 ⟶ 89:
stdin is not tty
</pre>
 
=={{header|COBOL}}==
Works with GnuCOBOL.
 
<langsyntaxhighlight lang="cobol"> *>
*> istty, check id fd 0 is a tty
*> Tectonics: cobc -xj istty.cob
Line 99 ⟶ 117:
 
goback.
end program istty.</langsyntaxhighlight>
 
DISPLAY for fd 1 is directed to SYSERR to get some output during the various trials.
Line 121 ⟶ 139:
fd 0 tty: +0000000000
fd 2 tty: +0000000000</pre>
 
=={{header|Common Lisp}}==
{{Works with|SBCL}}
<langsyntaxhighlight lang="lisp">(with-open-stream (s *standard-input*)
(format T "stdin is~:[ not~;~] a terminal~%"
(interactive-stream-p s)))</langsyntaxhighlight>
 
{{Out}}
Line 135 ⟶ 152:
$ echo "" | sbcl --script rc.lisp
stdin is not a terminal</pre>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">File.new("testfile").tty? #=> false
File.new("/dev/tty").tty? #=> true
STDIN.tty? #=> true</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
extern(C) int isatty(int);
Line 151 ⟶ 166:
else
writeln("Input doesn't come from tty.");
}</langsyntaxhighlight>
{{out}}
<pre>C:\test
Line 157 ⟶ 172:
C:\test < in.txt
Input doesn't come from tty.</pre>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
In Gforth, the word "source-id" is used to determine the program source.
 
If you got a program file "source.f":
<syntaxhighlight lang="Forth">
: ?tty source-id if ." not " then ." from terminal" ; ?tty bye
</syntaxhighlight>
Then,
<syntaxhighlight lang="bash">gforth source.f</syntaxhighlight> in the shell will display:
{{out}}<pre>not from terminal</pre>
Then,
<syntaxhighlight lang="bash">gforth -e ': ?tty source-id if ." not " then ." from terminal" ; ?tty bye'</syntaxhighlight> will display:
{{out}}<pre>not from terminal</pre>
 
At Gforth prompt,
<syntaxhighlight lang="bash">: ?tty source-id if ." not " then ." from terminal" ; ?tty bye</syntaxhighlight> will display:
{{out}}<pre>from terminal</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
BeginCCode
if (isatty(fileno(stdin)))
NSLog( @"stdin is connected to a terminal" );
else
NSLog( @"stdin is NOT connected to a terminal" );
EndC
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
stdin is NOT connected to a terminal
</pre>
 
 
=={{header|Go}}==
{{libheader|Go sub-repositories}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 174 ⟶ 229:
fmt.Println("Who are you? You're not a terminal.")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 182 ⟶ 237:
Who are you? You're not a terminal.
</pre>
 
 
=={{header|Haskell}}==
Line 187 ⟶ 243:
Example uses [https://hackage.haskell.org/package/unix <tt>unix</tt>] package:
 
<langsyntaxhighlight lang="haskell">module Main (main) where
import System.Posix.IO (stdInput)
Line 197 ⟶ 253:
putStrLn $ if isTTY
then "stdin is TTY"
else "stdin is not TTY"</langsyntaxhighlight>
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* Check input device is a terminal, in Jsish */
;Interp.conf().subOpts.istty;
 
Line 207 ⟶ 262:
Interp.conf().subOpts.istty ==> false
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 217 ⟶ 272:
prompt$ jsish --U checkInputDevice.jsi
Interp.conf().subOpts.istty ==> false</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
if isa(STDIN, Base.TTY)
println("This program sees STDIN as a TTY.")
Line 225 ⟶ 279:
println("This program does not see STDIN as a TTY.")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 231 ⟶ 285:
This program sees STDIN as a TTY.
</pre>
 
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<langsyntaxhighlight lang="scala">// Kotlin Native version 0.5
 
import platform.posix.*
Line 244 ⟶ 297:
println("stdin is not a terminal")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 250 ⟶ 303:
stdin is a terminal
</pre>
 
=={{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).
<langsyntaxhighlight Nemerlelang="nemerle">def isTerm = System.Console.IsInputRedirected;</langsyntaxhighlight>
=={{header|Nim}}==
Using function "isatty" of standard module "terminal" which accepts a File as argument.
 
<syntaxhighlight lang="nim">import terminal
 
echo if stdin.isatty: "stdin is a terminal" else: "stdin is not a terminal"</syntaxhighlight>
 
{{out}}
<pre>Command: ./check_input_dev
Result: stdin is a terminal</pre>
 
<pre>Command: ./check_input_dev <somefile
Result: stdin is not a terminal</pre>
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let () =
print_endline (
if Unix.isatty Unix.stdin
then "Input comes from tty."
else "Input doesn't come from tty."
)</langsyntaxhighlight>
 
Testing in interpreted mode:
Line 271 ⟶ 335:
Input doesn't come from tty.
</pre>
=={{header|Ol}}==
 
<syntaxhighlight lang="scheme">
(define (isatty? fd) (syscall 16 fd 19))
(print (if (isatty? stdin)
"Input comes from tty."
"Input doesn't come from tty."))
</syntaxhighlight>
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use 5.010;
Line 281 ⟶ 351:
else {
say "Input doesn't come from tty.";
}</langsyntaxhighlight>
 
$ perl istty.pl
Line 287 ⟶ 357:
$ true | perl istty.pl
Input doesn't come from tty.
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
Requires 0.8.2+
<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>
<lang Phix>printf(1,"stdin:%t, stdout:%t, stderr:%t\n",{isatty(0),isatty(1),isatty(2)})</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 298 ⟶ 369:
stdin:false, stdout:true, stderr:true
</pre>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">void main()
{
if(Stdio.Terminfo.is_tty())
Line 306 ⟶ 376:
else
write("Input doesn't come from tty.\n");
}</langsyntaxhighlight>
 
{{out}}
Line 313 ⟶ 383:
$ echo | ./istty.pike
Input doesn't come from tty.</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from sys import stdin
if stdin.isatty():
print("Input comes from tty.")
else:
print("Input doesn't come from tty.")</langsyntaxhighlight>
 
$ python istty.py
Line 325 ⟶ 394:
$ true | python istty.py
Input doesn't come from tty.
=={{header|Quackery}}==
 
{{trans|Python}}
 
<syntaxhighlight lang="quackery"> [ $ |from sys import stdin
to_stack( 1 if stdin.isatty() else 0)|
python ] is ttyin ( --> b )
 
ttyin if
[ say "Looks like a teletype." ]
else
[ say "Not a teletype." ]</syntaxhighlight>
 
{{out}}
 
<pre>Looks like a teletype.</pre>
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
(terminal-port? (current-input-port))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" perl6line>say $*IN.t ?? "Input comes from tty." !! "Input doesn't come from tty.";</langsyntaxhighlight>
 
$ raku istty.raku
Line 340 ⟶ 423:
$ true | raku istty.raku
Input doesn't come from tty.
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program determines if input comes from terminal or standard input*/
 
if queued() then say 'input comes from the terminal.'
Line 348 ⟶ 430:
 
/*stick a fork in it, we're done.*/
</syntaxhighlight>
</lang>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Check input device is a terminal
Line 368 ⟶ 449:
see SystemCmd("mycmd.bat")
ok
</syntaxhighlight>
</lang>
Output:
<pre>
input redirected
</pre>
 
=={{header|Ruby}}==
Example from the docs.
<langsyntaxhighlight lang="ruby">File.new("testfile").isatty #=> false
File.new("/dev/tty").isatty #=> true</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">/* Uses C library interface */
 
extern crate libc;
Line 391 ⟶ 470:
println!("stdout is not tty");
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<langsyntaxhighlight lang="scala">import org.fusesource.jansi.internal.CLibrary._
 
object IsATty extends App {
Line 415 ⟶ 493:
 
println("tty " + apply(true))
}</langsyntaxhighlight>
=={{header|Standard ML}}==
 
<syntaxhighlight lang="sml">val stdinRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdin</syntaxhighlight>
=={{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.
<langsyntaxhighlight lang="tcl">if {[catch {fconfigure stdin -mode}]} {
puts "Input doesn't come from tty."
} else {
puts "Input comes from tty."
}</langsyntaxhighlight>
Demonstrating:
<pre>
Line 431 ⟶ 510:
Input doesn't come from tty.
</pre>
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="sh">#!/bin/sh
 
if [ -t 0 ]
then echo "Input is a terminal"
then
else echo "Input is NOT a terminal"
fi</syntaxhighlight>
else
echo "Input is NOT a terminal"
fi</lang>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin
 
System.print("Input device is a terminal? %(Stdin.isTerminal ? "Yes" : "No")")</langsyntaxhighlight>
 
{{out}}
Line 454 ⟶ 530:
=={{header|zkl}}==
On Unix, check to see if stdin's st_mode is a character device.
<langsyntaxhighlight lang="zkl">const S_IFCHR=0x2000;
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() }
S_ISCHR(File.stdin).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 466 ⟶ 542:
False
</pre>
 
{{omit from|Clojure}}
{{omit from|GUISS}}
{{omit from|Java|See bug JDK-4099017}}
{{omit from|GUISS}}
{{omit from|Processing}}
{{omit from|TI-83 BASIC|Input device is always either a terminal or created by the program}}
{{omit from|ZX Spectrum Basic}}
[[Category:Terminal control]]
[[Category:Hardware]]
[[Category:Initialization]]
9,476

edits