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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
dtach - A simple program that emulates the detach feature of screen.
Copyright (C) 2004-2016 Ned T. Crigler
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "dtach.h"
/*
** dtach is a quick hack, since I wanted the detach feature of screen without
** all the other crud. It'll work best with full-screen applications, as it
** does not keep track of the screen or anything like that.
*/
/* Make sure the binary has a copyright. */
const char copyright[] = "dtach - version " PACKAGE_VERSION "(C)Copyright 2004-2016 Ned T. Crigler";
/* argv[0] from the program */
char *progname;
/* The name of the passed in socket. */
char *sockname;
/* The character used for detaching. Defaults to '^\' */
int detach_char = '\\' - 64;
/* 1 if we should not interpret the suspend character. */
int no_suspend;
/* The default redraw method. Initially set to unspecified. */
int redraw_method = REDRAW_UNSPEC;
/*
** The original terminal settings. Shared between the master and attach
** processes. The master uses it to initialize the pty, and the attacher uses
** it to restore the original settings.
*/
struct termios orig_term;
int dont_have_tty;
static void
usage()
{
printf(
"dtach - version %s\n"
"Usage: dtach -a <socket> <options>\n"
" dtach -A <socket> <options> <command...>\n"
" dtach -c <socket> <options> <command...>\n"
" dtach -n <socket> <options> <command...>\n"
" dtach -N <socket> <options> <command...>\n"
" dtach -p <socket>\n"
"Modes:\n"
" -a\t\tAttach to the specified socket.\n"
" -A\t\tAttach to the specified socket, or create it if it\n"
"\t\t does not exist, running the specified command.\n"
" -c\t\tCreate a new socket and run the specified command.\n"
" -n\t\tCreate a new socket and run the specified command "
"detached.\n"
" -N\t\tCreate a new socket and run the specified command "
"detached,\n"
"\t\t and have dtach run in the foreground.\n"
" -p\t\tCopy the contents of standard input to the specified\n"
"\t\t socket.\n"
"Options:\n"
" -e <char>\tSet the detach character to <char>, defaults "
"to ^\\.\n"
" -E\t\tDisable the detach character.\n"
" -r <method>\tSet the redraw method to <method>. The "
"valid methods are:\n"
"\t\t none: Don't redraw at all.\n"
"\t\t ctrl_l: Send a Ctrl L character to the program.\n"
"\t\t winch: Send a WINCH signal to the program.\n"
" -z\t\tDisable processing of the suspend key.\n"
"\nReport any bugs to <" PACKAGE_BUGREPORT ">.\n",
PACKAGE_VERSION);
exit(0);
}
int
main(int argc, char **argv)
{
int mode = 0;
/* Save the program name */
progname = argv[0];
++argv; --argc;
/* Parse the arguments */
if (argc >= 1 && **argv == '-')
{
if (strncmp(*argv, "--help", strlen(*argv)) == 0)
usage();
else if (strncmp(*argv, "--version", strlen(*argv)) == 0)
{
printf("dtach - version %s\n",
PACKAGE_VERSION);
return 0;
}
mode = argv[0][1];
if (mode == '?')
usage();
else if (mode != 'a' && mode != 'c' && mode != 'n' &&
mode != 'A' && mode != 'N' && mode != 'p')
{
printf("%s: Invalid mode '-%c'\n", progname, mode);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
}
if (!mode)
{
printf("%s: No mode was specified.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
++argv; --argc;
if (argc < 1)
{
printf("%s: No socket was specified.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
sockname = *argv;
++argv; --argc;
if (mode == 'p')
{
if (argc > 0)
{
printf("%s: Invalid number of arguments.\n",
progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
return push_main();
}
while (argc >= 1 && **argv == '-')
{
char *p;
for (p = argv[0] + 1; *p; ++p)
{
if (*p == 'E')
detach_char = -1;
else if (*p == 'z')
no_suspend = 1;
else if (*p == 'e')
{
++argv; --argc;
if (argc < 1)
{
printf("%s: No escape character "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if (argv[0][0] == '^' && argv[0][1])
{
if (argv[0][1] == '?')
detach_char = '\177';
else
detach_char = argv[0][1] & 037;
}
else
detach_char = argv[0][0];
break;
}
else if (*p == 'r')
{
++argv; --argc;
if (argc < 1)
{
printf("%s: No redraw method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
if (strcmp(argv[0], "none") == 0)
redraw_method = REDRAW_NONE;
else if (strcmp(argv[0], "ctrl_l") == 0)
redraw_method = REDRAW_CTRL_L;
else if (strcmp(argv[0], "winch") == 0)
redraw_method = REDRAW_WINCH;
else
{
printf("%s: Invalid redraw method "
"specified.\n", progname);
printf("Try '%s --help' for more "
"information.\n", progname);
return 1;
}
break;
}
else
{
printf("%s: Invalid option '-%c'\n",
progname, *p);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
}
++argv; --argc;
}
if (mode != 'a' && argc < 1)
{
printf("%s: No command was specified.\n", progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
/* Save the original terminal settings. */
if (tcgetattr(0, &orig_term) < 0)
{
memset(&orig_term, 0, sizeof(struct termios));
dont_have_tty = 1;
}
if (dont_have_tty && mode != 'n' && mode != 'N')
{
printf("%s: Attaching to a session requires a terminal.\n",
progname);
return 1;
}
if (mode == 'a')
{
if (argc > 0)
{
printf("%s: Invalid number of arguments.\n",
progname);
printf("Try '%s --help' for more information.\n",
progname);
return 1;
}
return attach_main(0);
}
else if (mode == 'n')
return master_main(argv, 0, 0);
else if (mode == 'N')
return master_main(argv, 0, 1);
else if (mode == 'c')
{
if (master_main(argv, 1, 0) != 0)
return 1;
return attach_main(0);
}
else if (mode == 'A')
{
/* Try to attach first. If that doesn't work, create a new
** socket. */
if (attach_main(1) != 0)
{
if (errno == ECONNREFUSED || errno == ENOENT)
{
if (errno == ECONNREFUSED)
unlink(sockname);
if (master_main(argv, 1, 0) != 0)
return 1;
}
return attach_main(0);
}
}
return 0;
}
|