Check input device is a terminal: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 5 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>.
 
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C_Streams; use Interfaces.C_Streams;
 
Line 34 ⟶ 36:
</pre>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang=freebasic>terminal = isatty(0)
<syntaxhighlight lang="freebasic">terminal = isatty(0)
PRINT terminal</syntaxhighlight>
 
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>:
<syntaxhighlight lang="c">#include <unistd.h> //for isatty()
#include <stdio.h> //for fileno()
 
Line 70 ⟶ 89:
stdin is not tty
</pre>
 
=={{header|COBOL}}==
Works with GnuCOBOL.
 
<syntaxhighlight lang="cobol"> *>
*> istty, check id fd 0 is a tty
*> Tectonics: cobc -xj istty.cob
Line 121 ⟶ 139:
fd 0 tty: +0000000000
fd 2 tty: +0000000000</pre>
 
=={{header|Common Lisp}}==
{{Works with|SBCL}}
<syntaxhighlight lang="lisp">(with-open-stream (s *standard-input*)
(format T "stdin is~:[ not~;~] a terminal~%"
(interactive-stream-p s)))</syntaxhighlight>
Line 135 ⟶ 152:
$ echo "" | sbcl --script rc.lisp
stdin is not a terminal</pre>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">File.new("testfile").tty? #=> false
File.new("/dev/tty").tty? #=> true
STDIN.tty? #=> true</syntaxhighlight>
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio;
 
extern(C) int isatty(int);
Line 159 ⟶ 174:
 
 
=={{header|FreeBASICForth}}==
{{works with|gforth|0.7.3}}
<syntaxhighlight lang=freebasic>
In Gforth, the word "source-id" is used to determine the program source.
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 you got a program file "source.f":
If Err Then
<syntaxhighlight lang="Forth">
Print "Input doesn't come from tt."
: ?tty source-id if ." not " then ." from terminal" ; ?tty bye
Else
Print "Input comes from tty."
End If
Close #1
Sleep
</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}}
<syntaxhighlight lang="go">package main
 
import (
Line 199 ⟶ 237:
Who are you? You're not a terminal.
</pre>
 
 
=={{header|Haskell}}==
Line 204 ⟶ 243:
Example uses [https://hackage.haskell.org/package/unix <tt>unix</tt>] package:
 
<syntaxhighlight lang="haskell">module Main (main) where
import System.Posix.IO (stdInput)
Line 215 ⟶ 254:
then "stdin is TTY"
else "stdin is not TTY"</syntaxhighlight>
 
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">/* Check input device is a terminal, in Jsish */
;Interp.conf().subOpts.istty;
 
Line 234 ⟶ 272:
prompt$ jsish --U checkInputDevice.jsi
Interp.conf().subOpts.istty ==> false</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang=Julia"julia">
if isa(STDIN, Base.TTY)
println("This program sees STDIN as a TTY.")
Line 248 ⟶ 285:
This program sees STDIN as a TTY.
</pre>
 
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">// Kotlin Native version 0.5
 
import platform.posix.*
Line 267 ⟶ 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).
<syntaxhighlight lang=Nemerle"nemerle">def isTerm = System.Console.IsInputRedirected;</syntaxhighlight>
 
=={{header|Nim}}==
Using function "isatty" of standard module "terminal" which accepts a File as argument.
 
<syntaxhighlight lang=Nim"nim">import terminal
 
echo if stdin.isatty: "stdin is a terminal" else: "stdin is not a terminal"</syntaxhighlight>
Line 285 ⟶ 319:
<pre>Command: ./check_input_dev <somefile
Result: stdin is not a terminal</pre>
 
=={{header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let () =
print_endline (
if Unix.isatty Unix.stdin
Line 302 ⟶ 335:
Input doesn't come from tty.
</pre>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(define (isatty? fd) (syscall 16 fd 19))
(print (if (isatty? stdin)
Line 310 ⟶ 342:
"Input doesn't come from tty."))
</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use strict;
use warnings;
use 5.010;
Line 326 ⟶ 357:
$ true | perl istty.pl
Input doesn't come from tty.
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(notonline)-->
<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>
Line 339 ⟶ 369:
stdin:false, stdout:true, stderr:true
</pre>
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">void main()
{
if(Stdio.Terminfo.is_tty())
Line 354 ⟶ 383:
$ echo | ./istty.pike
Input doesn't come from tty.</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from sys import stdin
if stdin.isatty():
print("Input comes from tty.")
Line 366 ⟶ 394:
$ true | python istty.py
Input doesn't come from tty.
 
=={{header|Quackery}}==
 
{{trans|Python}}
 
<syntaxhighlight lang=Quackery"quackery"> [ $ |from sys import stdin
to_stack( 1 if stdin.isatty() else 0)|
python ] is ttyin ( --> b )
Line 383 ⟶ 410:
 
<pre>Looks like a teletype.</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
(terminal-port? (current-input-port))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" line>say $*IN.t ?? "Input comes from tty." !! "Input doesn't come from tty.";</syntaxhighlight>
 
$ raku istty.raku
Line 398 ⟶ 423:
$ true | raku istty.raku
Input doesn't come from tty.
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program determines if input comes from terminal or standard input*/
 
if queued() then say 'input comes from the terminal.'
Line 407 ⟶ 431:
/*stick a fork in it, we're done.*/
</syntaxhighlight>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Check input device is a terminal
Line 431 ⟶ 454:
input redirected
</pre>
 
=={{header|Ruby}}==
Example from the docs.
<syntaxhighlight lang="ruby">File.new("testfile").isatty #=> false
File.new("/dev/tty").isatty #=> true</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">/* Uses C library interface */
 
extern crate libc;
Line 450 ⟶ 471:
}
}</syntaxhighlight>
 
=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">import org.fusesource.jansi.internal.CLibrary._
 
object IsATty extends App {
Line 474 ⟶ 494:
println("tty " + apply(true))
}</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="tcl">if {[catch {fconfigure stdin -mode}]} {
puts "Input doesn't come from tty."
} else {
Line 492 ⟶ 510:
Input doesn't come from tty.
</pre>
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh">#!/bin/sh
 
if [ -t 0 ]
then echo "Input is a terminal"
then
else echo "Input is NOT a terminal"
else
echo "Input is NOT a terminal"
fi</syntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang=ecmascript"wren">import "io" for Stdin
 
System.print("Input device is a terminal? %(Stdin.isTerminal ? "Yes" : "No")")</syntaxhighlight>
Line 515 ⟶ 530:
=={{header|zkl}}==
On Unix, check to see if stdin's st_mode is a character device.
<syntaxhighlight lang="zkl">const S_IFCHR=0x2000;
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() }
S_ISCHR(File.stdin).println();</syntaxhighlight>
Line 527 ⟶ 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