File: ttyoff

package info (click to toggle)
xdg-utils 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,652 kB
  • sloc: sh: 8,244; xml: 3,474; perl: 335; makefile: 188; awk: 41
file content (84 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl

# ttyoff - run command detached from terminal
#
# Copyright 2020, Nicholas Guriev <guriev-ns@ya.ru>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

=head1 NAME

ttyoff - run command detached from terminal

=head1 SYNOPSIS

B<ttyoff> [B<-a> I<NAME>] [B<-s>] [I<COMMAND>...]

=head1 DESCRIPTION

Simple script that detaches itself from the controlling terminal and executes
passed command. The script is primarily intended for running automated tests.

If no command is specified, current Shell is executed.

Options:

=over

=item B<-a>

Override the zeroth argument of the command.

=item B<-s>

Before proceeding, close standard streams (numbered 0, 1, and 2).

=back

=head1 AUTHOR

Written by Nicholas Guriev on New Year's Eve 2021.

=head1 SEE ALSO

tty(4)

=cut


use strict;
use warnings;

use Getopt::Std;
BEGIN { require "sys/ioctl.ph" }

$Getopt::Std::STANDARD_HELP_VERSION = 1;
our ($opt_a, $opt_s);
getopts("a:s") or exit 1;

if (open my $term, "+<", "/dev/tty") {
  ioctl $term, TIOCNOTTY, 0 or die $!;
}
if ($opt_s) {
  close $_ or die $! for (*STDIN, *STDOUT, *STDERR);
}
unless (@ARGV) {
  push @ARGV, $ENV{SHELL} || "/bin/sh";
}
exec { $opt_a or $ARGV[0] } @ARGV or die $!;