Check input device is a terminal: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
⚫ | |||
⚫ | |||
⚫ | |||
{{task}} |
{{task}} |
||
Line 8: | Line 11: | ||
* [[Check output device is a terminal]] |
* [[Check output device is a terminal]] |
||
<br><br> |
<br><br> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT}} |
{{works with|GNAT}} |
||
We use the interface to C library functions <code>isatty()</code> and <code>fileno()</code>. |
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; |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Interfaces.C_Streams; use Interfaces.C_Streams; |
with Interfaces.C_Streams; use Interfaces.C_Streams; |
||
Line 33: | Line 35: | ||
stdin is not a tty. |
stdin is not a tty. |
||
</pre> |
</pre> |
||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
<syntaxhighlight lang=freebasic>terminal = isatty(0) |
<syntaxhighlight lang="freebasic">terminal = isatty(0) |
||
PRINT terminal</syntaxhighlight> |
PRINT terminal</syntaxhighlight> |
||
Line 48: | Line 49: | ||
prompt$ ./istty <<<"testing" |
prompt$ ./istty <<<"testing" |
||
0</pre> |
0</pre> |
||
=={{header|C}}== |
=={{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>: |
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() |
<syntaxhighlight lang="c">#include <unistd.h> //for isatty() |
||
#include <stdio.h> //for fileno() |
#include <stdio.h> //for fileno() |
||
Line 70: | Line 70: | ||
stdin is not tty |
stdin is not tty |
||
</pre> |
</pre> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Works with GnuCOBOL. |
Works with GnuCOBOL. |
||
<syntaxhighlight lang=cobol> *> |
<syntaxhighlight lang="cobol"> *> |
||
*> istty, check id fd 0 is a tty |
*> istty, check id fd 0 is a tty |
||
*> Tectonics: cobc -xj istty.cob |
*> Tectonics: cobc -xj istty.cob |
||
Line 121: | Line 120: | ||
fd 0 tty: +0000000000 |
fd 0 tty: +0000000000 |
||
fd 2 tty: +0000000000</pre> |
fd 2 tty: +0000000000</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{Works with|SBCL}} |
{{Works with|SBCL}} |
||
<syntaxhighlight lang=lisp>(with-open-stream (s *standard-input*) |
<syntaxhighlight lang="lisp">(with-open-stream (s *standard-input*) |
||
(format T "stdin is~:[ not~;~] a terminal~%" |
(format T "stdin is~:[ not~;~] a terminal~%" |
||
(interactive-stream-p s)))</syntaxhighlight> |
(interactive-stream-p s)))</syntaxhighlight> |
||
Line 135: | Line 133: | ||
$ echo "" | sbcl --script rc.lisp |
$ echo "" | sbcl --script rc.lisp |
||
stdin is not a terminal</pre> |
stdin is not a terminal</pre> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
<syntaxhighlight lang=ruby>File.new("testfile").tty? #=> false |
<syntaxhighlight lang="ruby">File.new("testfile").tty? #=> false |
||
File.new("/dev/tty").tty? #=> true |
File.new("/dev/tty").tty? #=> true |
||
STDIN.tty? #=> true</syntaxhighlight> |
STDIN.tty? #=> true</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang=d>import std.stdio; |
<syntaxhighlight lang="d">import std.stdio; |
||
extern(C) int isatty(int); |
extern(C) int isatty(int); |
||
Line 157: | Line 153: | ||
C:\test < in.txt |
C:\test < in.txt |
||
Input doesn't come from tty.</pre> |
Input doesn't come from tty.</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
Open Cons For Input As #1 |
Open Cons For Input As #1 |
||
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar |
' Open Cons abre los flujos de entrada (stdin) o salida (stdout) estándar |
||
Line 173: | Line 167: | ||
Sleep |
Sleep |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{libheader|Go sub-repositories}} |
{{libheader|Go sub-repositories}} |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 199: | Line 191: | ||
Who are you? You're not a terminal. |
Who are you? You're not a terminal. |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Example uses [https://hackage.haskell.org/package/unix <tt>unix</tt>] package: |
Example uses [https://hackage.haskell.org/package/unix <tt>unix</tt>] package: |
||
<syntaxhighlight lang=haskell>module Main (main) where |
<syntaxhighlight lang="haskell">module Main (main) where |
||
import System.Posix.IO (stdInput) |
import System.Posix.IO (stdInput) |
||
Line 215: | Line 206: | ||
then "stdin is TTY" |
then "stdin is TTY" |
||
else "stdin is not TTY"</syntaxhighlight> |
else "stdin is not TTY"</syntaxhighlight> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
<syntaxhighlight lang=javascript>/* Check input device is a terminal, in Jsish */ |
<syntaxhighlight lang="javascript">/* Check input device is a terminal, in Jsish */ |
||
;Interp.conf().subOpts.istty; |
;Interp.conf().subOpts.istty; |
||
Line 234: | Line 224: | ||
prompt$ jsish --U checkInputDevice.jsi |
prompt$ jsish --U checkInputDevice.jsi |
||
Interp.conf().subOpts.istty ==> false</pre> |
Interp.conf().subOpts.istty ==> false</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="julia"> |
||
if isa(STDIN, Base.TTY) |
if isa(STDIN, Base.TTY) |
||
println("This program sees STDIN as a TTY.") |
println("This program sees STDIN as a TTY.") |
||
Line 248: | Line 237: | ||
This program sees STDIN as a TTY. |
This program sees STDIN as a TTY. |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{Works with|Ubuntu|14.04}} |
{{Works with|Ubuntu|14.04}} |
||
<syntaxhighlight lang=scala>// Kotlin Native version 0.5 |
<syntaxhighlight lang="scala">// Kotlin Native version 0.5 |
||
import platform.posix.* |
import platform.posix.* |
||
Line 267: | Line 255: | ||
stdin is a terminal |
stdin is a terminal |
||
</pre> |
</pre> |
||
=={{header|Nemerle}}== |
=={{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). |
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= |
<syntaxhighlight lang="nemerle">def isTerm = System.Console.IsInputRedirected;</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Using function "isatty" of standard module "terminal" which accepts a File as argument. |
Using function "isatty" of standard module "terminal" which accepts a File as argument. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import terminal |
||
echo if stdin.isatty: "stdin is a terminal" else: "stdin is not a terminal"</syntaxhighlight> |
echo if stdin.isatty: "stdin is a terminal" else: "stdin is not a terminal"</syntaxhighlight> |
||
Line 285: | Line 271: | ||
<pre>Command: ./check_input_dev <somefile |
<pre>Command: ./check_input_dev <somefile |
||
Result: stdin is not a terminal</pre> |
Result: stdin is not a terminal</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang=ocaml>let () = |
<syntaxhighlight lang="ocaml">let () = |
||
print_endline ( |
print_endline ( |
||
if Unix.isatty Unix.stdin |
if Unix.isatty Unix.stdin |
||
Line 302: | Line 287: | ||
Input doesn't come from tty. |
Input doesn't come from tty. |
||
</pre> |
</pre> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
(define (isatty? fd) (syscall 16 fd 19)) |
(define (isatty? fd) (syscall 16 fd 19)) |
||
(print (if (isatty? stdin) |
(print (if (isatty? stdin) |
||
Line 310: | Line 294: | ||
"Input doesn't come from tty.")) |
"Input doesn't come from tty.")) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>use strict; |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use 5.010; |
use 5.010; |
||
Line 326: | Line 309: | ||
$ true | perl istty.pl |
$ true | perl istty.pl |
||
Input doesn't come from tty. |
Input doesn't come from tty. |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="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: #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> |
<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: | Line 321: | ||
stdin:false, stdout:true, stderr:true |
stdin:false, stdout:true, stderr:true |
||
</pre> |
</pre> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
<syntaxhighlight lang=pike>void main() |
<syntaxhighlight lang="pike">void main() |
||
{ |
{ |
||
if(Stdio.Terminfo.is_tty()) |
if(Stdio.Terminfo.is_tty()) |
||
Line 354: | Line 335: | ||
$ echo | ./istty.pike |
$ echo | ./istty.pike |
||
Input doesn't come from tty.</pre> |
Input doesn't come from tty.</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang=python>from sys import stdin |
<syntaxhighlight lang="python">from sys import stdin |
||
if stdin.isatty(): |
if stdin.isatty(): |
||
print("Input comes from tty.") |
print("Input comes from tty.") |
||
Line 366: | Line 346: | ||
$ true | python istty.py |
$ true | python istty.py |
||
Input doesn't come from tty. |
Input doesn't come from tty. |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> [ $ |from sys import stdin |
||
to_stack( 1 if stdin.isatty() else 0)| |
to_stack( 1 if stdin.isatty() else 0)| |
||
python ] is ttyin ( --> b ) |
python ] is ttyin ( --> b ) |
||
Line 383: | Line 362: | ||
<pre>Looks like a teletype.</pre> |
<pre>Looks like a teletype.</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
(terminal-port? (current-input-port)) |
(terminal-port? (current-input-port)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2015.12}} |
{{works with|Rakudo|2015.12}} |
||
<syntaxhighlight lang=raku line>say $*IN.t ?? "Input comes from tty." !! "Input doesn't come from tty.";</syntaxhighlight> |
<syntaxhighlight lang="raku" line>say $*IN.t ?? "Input comes from tty." !! "Input doesn't come from tty.";</syntaxhighlight> |
||
$ raku istty.raku |
$ raku istty.raku |
||
Line 398: | Line 375: | ||
$ true | raku istty.raku |
$ true | raku istty.raku |
||
Input doesn't come from tty. |
Input doesn't come from tty. |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<syntaxhighlight lang=rexx>/*REXX program determines if input comes from terminal or standard input*/ |
<syntaxhighlight lang="rexx">/*REXX program determines if input comes from terminal or standard input*/ |
||
if queued() then say 'input comes from the terminal.' |
if queued() then say 'input comes from the terminal.' |
||
Line 407: | Line 383: | ||
/*stick a fork in it, we're done.*/ |
/*stick a fork in it, we're done.*/ |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
# Project : Check input device is a terminal |
# Project : Check input device is a terminal |
||
Line 431: | Line 406: | ||
input redirected |
input redirected |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Example from the docs. |
Example from the docs. |
||
<syntaxhighlight lang=ruby>File.new("testfile").isatty #=> false |
<syntaxhighlight lang="ruby">File.new("testfile").isatty #=> false |
||
File.new("/dev/tty").isatty #=> true</syntaxhighlight> |
File.new("/dev/tty").isatty #=> true</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>/* Uses C library interface */ |
<syntaxhighlight lang="rust">/* Uses C library interface */ |
||
extern crate libc; |
extern crate libc; |
||
Line 450: | Line 423: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{Works with|Ubuntu|14.04}} |
{{Works with|Ubuntu|14.04}} |
||
<syntaxhighlight lang=scala>import org.fusesource.jansi.internal.CLibrary._ |
<syntaxhighlight lang="scala">import org.fusesource.jansi.internal.CLibrary._ |
||
object IsATty extends App { |
object IsATty extends App { |
||
Line 474: | Line 446: | ||
println("tty " + apply(true)) |
println("tty " + apply(true)) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang=sml>val stdinRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdin</syntaxhighlight> |
<syntaxhighlight lang="sml">val stdinRefersToTerminal : bool = Posix.ProcEnv.isatty Posix.FileSys.stdin</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{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. |
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}]} { |
<syntaxhighlight lang="tcl">if {[catch {fconfigure stdin -mode}]} { |
||
puts "Input doesn't come from tty." |
puts "Input doesn't come from tty." |
||
} else { |
} else { |
||
Line 492: | Line 462: | ||
Input doesn't come from tty. |
Input doesn't come from tty. |
||
</pre> |
</pre> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
<syntaxhighlight lang=sh>#!/bin/sh |
<syntaxhighlight lang="sh">#!/bin/sh |
||
if [ -t 0 ] |
if [ -t 0 ] |
||
Line 502: | Line 471: | ||
echo "Input is NOT a terminal" |
echo "Input is NOT a terminal" |
||
fi</syntaxhighlight> |
fi</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
<syntaxhighlight lang=ecmascript>import "io" for Stdin |
<syntaxhighlight lang="ecmascript">import "io" for Stdin |
||
System.print("Input device is a terminal? %(Stdin.isTerminal ? "Yes" : "No")")</syntaxhighlight> |
System.print("Input device is a terminal? %(Stdin.isTerminal ? "Yes" : "No")")</syntaxhighlight> |
||
Line 512: | Line 480: | ||
Input device is a terminal? Yes |
Input device is a terminal? Yes |
||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
On Unix, check to see if stdin's st_mode is a character device. |
On Unix, check to see if stdin's st_mode is a character device. |
||
<syntaxhighlight lang=zkl>const S_IFCHR=0x2000; |
<syntaxhighlight lang="zkl">const S_IFCHR=0x2000; |
||
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() } |
fcn S_ISCHR(f){ f.info()[4].bitAnd(S_IFCHR).toBool() } |
||
S_ISCHR(File.stdin).println();</syntaxhighlight> |
S_ISCHR(File.stdin).println();</syntaxhighlight> |
||
Line 527: | Line 494: | ||
False |
False |
||
</pre> |
</pre> |
||
{{omit from|Clojure}} |
{{omit from|Clojure}} |
||
⚫ | |||
{{omit from|Java|See bug JDK-4099017}} |
{{omit from|Java|See bug JDK-4099017}} |
||
⚫ | |||
{{omit from|Processing}} |
{{omit from|Processing}} |
||
{{omit from|TI-83 BASIC|Input device is always either a terminal or created by the program}} |
{{omit from|TI-83 BASIC|Input device is always either a terminal or created by the program}} |
||
{{omit from|ZX Spectrum Basic}} |
{{omit from|ZX Spectrum Basic}} |
||
⚫ | |||
⚫ | |||
⚫ |