-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcocot.c
221 lines (213 loc) · 4.71 KB
/
cocot.c
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
/*
* cocot - COde COnverter on Tty
*
* Copyright (c) 2002, 2004, 2005, 2008 IWAMURO Motonori
* All rights reserved.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_UTMP_H
# include <utmp.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#include <errno.h>
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#if HAVE_LIBUTIL_H
# include <libutil.h>
#endif
#if HAVE_UTIL_H
# include <util.h>
#endif
#if !defined(HAVE_LOGIN_TTY) && defined(HAVE_TERMIOS_H)
# include <termios.h>
#endif
#if HAVE_LIBUTIL_H
# include <libutil.h>
#endif
#if HAVE_UTIL_H
# include <util.h>
#endif
#if HAVE_SIGNAL_H
# include <signal.h>
#endif
#include <getopt.h>
#include "init.h"
#include "loop.h"
#include "suspend.h"
#if DEBUG
#define DEBUG_LOG "debug.log"
FILE *debug = NULL;
#endif
static void
show_version(void)
{
fprintf(stderr, "%s, version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit(1);
}
static void
usage(int argc, char *argv[])
{
fprintf(stderr,
"Usage: %s [OPTIONS] [--] COMMAND ARG1 ARG2 ...\n"
"\n"
"Options:\n"
" -o LOGFILE logging all output of command.\n"
" -a append log file.\n"
" -t TERM_CODE\n"
" -t TERM_INPUT_CODE,TERM_OUTPUT_CODE\n"
" character encoding(s) for terminal. (default is %s)\n"
" -p PROC_CODE\n"
" -p PROC_INPUT_CODE,PROC_OUTPUT_CODE\n"
" character encoding(s) in command process. (default is %s)\n"
" -i ignore ISO-2022-JP escape sequence.\n"
" -n no conversion. (like script(1))\n"
" -h, --help show this message.\n"
" -v, --version show version.\n",
argv[0],
DEFAULT_TERM_CODE,
DEFAULT_PROC_CODE);
exit(1);
}
int
main(int argc, char *argv[])
{
int i;
int c;
char *logfn = NULL;
char *logmd = "w";
FILE *logfp = NULL;
char *term_input_code = DEFAULT_TERM_CODE;
char *term_output_code = DEFAULT_TERM_CODE;
char *proc_input_code = DEFAULT_PROC_CODE;
char *proc_output_code = DEFAULT_PROC_CODE;
char *p;
int dec_jis = 1;
int mfd, sfd;
int status;
pid_t ppid, cpid, gcpid;
if (argc == 1)
usage(argc, argv);
for (i = 1; i < argc && argv[i][0] == '-'; i++) {
if (strcmp(argv[i], "--help") == 0)
argv[i] = "-h";
else if (strcmp(argv[i], "--version") == 0)
argv[i] = "-v";
}
while ((c = getopt(argc, argv, "ao:t:p:inhv")) != -1) {
switch (c) {
case 'a':
logmd = "a";
break;
case 'o':
logfn = optarg;
break;
case 't':
if ((p = strchr(optarg, ',')) != NULL) {
term_input_code = optarg;
*p++ = '\0';
term_output_code = p;
} else {
term_input_code = term_output_code = optarg;
}
break;
case 'p':
if ((p = strchr(optarg, ',')) != NULL) {
proc_input_code = optarg;
*p++ = '\0';
proc_output_code = p;
} else {
proc_input_code = proc_output_code = optarg;
}
break;
case 'i':
dec_jis = 0;
break;
case 'n':
term_input_code = term_output_code =
proc_input_code = proc_output_code = NULL;
break;
case 'h':
usage(argc, argv);
break;
case 'v':
show_version();
break;
}
}
if (logfn) {
if ((logfp = fopen(logfn, logmd)) == NULL)
fatal("Can't open file '%s' (%s).", logfn, strerror(errno));
setvbuf(logfp, NULL, _IONBF, 0);
}
init(&mfd, &sfd);
ppid = getpid();
if ((cpid = fork()) < 0) {
/* error */
fatal("Can't fork process");
} else if (cpid == 0) {
/* child */
close(mfd);
if (logfp)
fclose(logfp);
#ifdef HAVE_LOGIN_TTY
login_tty(sfd);
#else
setsid();
ioctl(sfd, TIOCSCTTY, 0);
dup2(sfd, 0);
dup2(sfd, 1);
dup2(sfd, 2);
if (sfd > 2)
close(sfd);
#endif
if ((gcpid = fork()) < 0) {
/* error */
fatal("Can't fork subprocess");
} else if (gcpid == 0) {
/* grandchild */
setfg();
execvp(argv[optind], argv + optind);
fatal("Can't exec process");
}
/* child */
do {
if (waitpid(gcpid, &status, WUNTRACED) < 0)
fatal("waitpid");
if (WIFSTOPPED(status))
kill(ppid, SIGTSTP);
kill(gcpid, SIGCONT);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(0);
}
/* parent */
close(sfd);
#ifdef DEBUG
if ((debug = fopen(DEBUG_LOG, "a")) == NULL)
fatal("Can't open file '%s' (%s).", DEBUG_LOG, strerror(errno));
setvbuf(debug, NULL, _IONBF, 0);
#endif
loop(mfd, logfp,
term_input_code, term_output_code,
proc_input_code, proc_output_code, dec_jis);
close(mfd);
if (logfp)
fclose(logfp);
wait(&status);
reset_tty();
return 0;
}