-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdtach.in
157 lines (148 loc) · 3.06 KB
/
dtach.in
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
usage()
{
cat << _EOF_
dtach - version @PACKAGE_VERSION@, compiled on @BUILD_DATE@.
Usage: dtach -a <socket> <options>
dtach -A <socket> <options> <command...>
dtach -c <socket> <options> <command...>
dtach -n <socket> <options> <command...>
dtach -N <socket> <options> <command...>
Modes:
-a Attach to the specified socket.
-A Attach to the specified socket, or create it if it
does not exist, running the specified command.
-c Create a new socket and run the specified command.
-n Create a new socket and run the specified command detached.
-N Create a new socket and run the specified command detached,
but without forking to the background.
Options:
-e <char> Set the detach character to <char>, defaults to ^\.
-E Disable the detach character.
-r <method> Set the redraw method to <method>. The valid methods are:
none: Don't redraw at all.
ctrl_l: Send a Ctrl-L character to the program.
winch: Send SIGWINCH to the program.
-z Disable processing of the suspend key.
Report any bugs to <@PACKAGE_BUGREPORT@>.
_EOF_
}
version()
{
cat << _EOF_
dtach - version @PACKAGE_VERSION@, compiled on @BUILD_DATE@.
(C) Copyright 2004-2014 Ned T. Crigler, modified by Devin J. Pohly
_EOF_
}
declare mode= sockname= rv=
declare -a dtmaster_opts=() dtattach_opts=()
case $1 in
--help|-\?)
usage
exit 0
;;
--version)
version
exit 0
;;
-[acnAN])
mode=${1:1:1}
;;
*)
echo "$0: No mode was specified."
echo "Try '$0 --help' for more information."
exit 1
;;
esac
sockname=$2
if [[ -z $sockname ]]; then
echo "$0: No socket was specified."
echo "Try '$0 --help' for more information."
exit 1
fi
shift 2
while [[ $# -gt 1 ]]; do
case $1 in
-E)
dtattach_opts+=(-e '')
;;
-z)
dtattach_opts+=(-z)
;;
-e)
shift
if [[ $# -lt 1 ]]; then
echo "$0: No escape character specified."
echo "Try '$0 --help' for more information."
exit 1
fi
dtattach_opts+=(-e "$1")
;;
-r)
shift
if [[ $# -lt 1 ]]; then
echo "$0: No redraw method specified."
echo "Try '$0 --help' for more information."
exit 1
fi
dtmaster_opts+=(-r "$1")
dtattach_opts+=(-r "$1")
;;
-*)
echo "$0: Invalid option '$1'"
echo "Try '$0 --help' for more information."
exit 1
;;
*)
break
;;
esac
shift
done
case $mode in
N)
dtmaster_opts+=(-n)
;&
n)
dtmaster "$sockname" "${dtmaster_opts[@]}" "$@"
;;
A)
clear
dtattach "$sockname" "${dtattach_opts[@]}"
rv=$?
case $rv in
0)
tput cup `tput lines` || tput ll
echo "[detached]"
exit 0
;;
3)
tput cup `tput lines` || tput ll
echo "[EOF - dtach terminating]"
exit 0
;;
2) rm "$sockname" || exit;;
esac
;&
c)
dtmaster "$sockname" "${dtmaster_opts[@]}" -w "$@" || exit
;&
a)
clear
dtattach "$sockname" "${dtattach_opts[@]}"
rv=$?
case $rv in
0)
tput cup `tput lines` || tput ll
echo "[detached]"
exit 0
;;
3)
tput cup `tput lines` || tput ll
echo "[EOF - dtach terminating]"
exit 0
;;
esac
exit 1
;;
esac