-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.c
More file actions
374 lines (341 loc) · 8.29 KB
/
Copy pathterm.c
File metadata and controls
374 lines (341 loc) · 8.29 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* JFBTERM -
* Copyright (c) 2003 Fumitoshi UKAI <ukai@debian.or.jp>
* Copyright (C) 1999 Noritoshi MASUICHI (nmasu@ma3.justnet.ne.jp)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NORITOSHI MASUICHI ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL NORITOSHI MASUICHI BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <dlfcn.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pwd.h>
#include <pty.h>
#include <utmp.h>
#include <grp.h>
#include "term.h"
#include "vterm.h"
#include "fbcommon.h"
#include "font.h"
#include "message.h"
#include "main.h"
#include "util.h"
#include "config.h"
#if 1 // Hangul
#include "automata.h"
#include "comp.h"
#endif
int gChildProcessId = 0;
TTerm gTerm;
void tterm_wakeup_shell(TTerm* p, const char* tn);
void tterm_final(TTerm* p);
static void tterm_set_utmp(TTerm* p);
static void tterm_reset_utmp(TTerm* p);
void send_hangup(
int closure)
{
if (gChildProcessId) {
kill(gChildProcessId, SIGHUP);
}
}
void sigchld(sig) int sig; {
int st;
int ret;
ret = wait(&st);
if (ret == gChildProcessId || ret == ECHILD) {
tvterm_unregister_signal();
tterm_final(&gTerm);
exit(EXIT_SUCCESS);
}
signal(SIGCHLD, sigchld);
}
void tterm_init(TTerm* p, const char* en)
{
p->ptyfd = -1;
p->ttyfd = -1;
p->name[0] = '\0';
tcgetattr(0, &(p->ttysave));
tvterm_init(&(p->vterm), p,
gFramebuffer.width/gFontsWidth,
gFramebuffer.height/gFontsHeight,
&(gApp.gCaps), en);
}
void tterm_final(TTerm* p)
{
tterm_reset_utmp(p);
tvterm_final(&(p->vterm));
}
void application_final(void)
{
TTerm* p = &gTerm;
/*
write(1, "\x1B[?25h", 6);
*/
tcsetattr(0, TCSAFLUSH, &(p->ttysave));
tterm_final(p);
tfbm_close(&gFramebuffer);
tfont_ary_final();
}
int tterm_get_ptytty(TTerm* p)
{
if (openpty(&p->ptyfd, &p->ttyfd, p->name, NULL, NULL) < 0) {
print_strerror("openpty");
return 0;
}
return 1;
}
#if 1 // Hangul
int get_shift_state(void)
{
int na = 6, ns = 0;
if(ioctl(fileno(stdin), TIOCLINUX, &na) == 0) ns = na;
return ns;
}
#endif
#define BUF_SIZE 1024
void tterm_start(TTerm* p, const char* tn, const char* en)
{
struct termios ntio;
int ret;
struct timeval tv;
u_char buf[BUF_SIZE+1];
#if 1 // Hangul
extern int hangul_state;
extern int hangul_output_code;
extern int process_hangul_input;
u_char constr[BUF_SIZE+1];
int i, c, show_fig, hconstr = 0;
#endif
#ifdef JFB_ENABLE_DIMMER
u_int idle_time = 0;
u_int blank = 0;
int tfbm_set_blank(int, int);
# define DIMMER_TIMEOUT (3 * 60 * 10) /* 3 min */
#endif
tterm_init(p, en);
if (!tterm_get_ptytty(p)) {
die("Cannot get free pty-tty.\n");
}
ntio = p->ttysave;
ntio.c_lflag &= ~(ECHO|ISIG|ICANON|XCASE);
ntio.c_iflag = 0;
ntio.c_oflag &= ~OPOST;
ntio.c_cc[VMIN] = 1;
ntio.c_cc[VTIME] = 0;
ntio.c_cflag |= CS8;
ntio.c_line = 0;
tcsetattr(0, TCSAFLUSH, &ntio);
/*
write(1, "\x1B[?25l", 6);
*/
tvterm_start(&(p->vterm));
fflush(stdout);
gChildProcessId = fork();
if (gChildProcessId == 0) {
/* child */
tterm_wakeup_shell(p, tn);
exit(1);
} else if (gChildProcessId < 0) {
print_strerror("fork");
exit(1);
}
/* parent */
tterm_set_utmp(p);
signal(SIGCHLD, sigchld);
atexit(application_final);
/* not available
* VtInit();
* VtStart();
*/
for (;;) {
fd_set fds;
int max = 0;
tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 msec
FD_ZERO(&fds);
FD_SET(0,&fds);
FD_SET(p->ptyfd,&fds);
if (p->ptyfd > max) max = p->ptyfd;
ret = select(max+1, &fds, NULL, NULL, &tv);
if (ret == 0 || (ret < 0 && errno == EINTR)) {
#ifdef JFB_ENABLE_DIMMER
if (!blank && ++idle_time > DIMMER_TIMEOUT) {
// Goto blank
idle_time = 0;
if (tfbm_set_blank(gFramebuffer.fh, 1))
blank = 1;
}
#endif
continue;
}
if (ret < 0) {
print_strerror_and_exit("select");
}
if (FD_ISSET(0, &fds)) {
ret = read(0, buf, BUF_SIZE);
#ifdef JFB_ENABLE_DIMMER
idle_time = 0;
if (blank) {
// Wakeup console
blank = 0;
tfbm_set_blank(gFramebuffer.fh, 0);
}
#endif
if (ret > 0) {
#if 1 // Hangul
if(process_hangul_input) {
for(i = 0; i < ret; i++) {
c = buf[i];
if(c == 27 && hangul_state && ret == 1) {
show_fig = hangul_automata_toggle(buf);
if(buf[0] != 0) {
if(hangul_output_code == 0)
write(p->ptyfd, buf, 3);
else
write(p->ptyfd, buf, 2);
}
}
if(c == ' ' && get_shift_state()) {
show_fig = hangul_automata_toggle(buf);
}
else {
show_fig = hangul_automata(c, buf);
if(show_fig == 0) {
get_ime_status(constr);
if(constr[0] == 0) { // bkspc: clear composing
memset(constr, ' ', 2);
tvterm_emulate(&(p->vterm), constr, 2);
}
else if(hangul_output_code == 0)
tvterm_emulate(&(p->vterm), constr, 3);
else
tvterm_emulate(&(p->vterm), constr, 2);
p->vterm.pen.x -= 2;
tvterm_refresh(&(p->vterm));
}
}
if(show_fig > 1) {
if(hangul_output_code == 0)
write(p->ptyfd, buf, show_fig + 1);
else
write(p->ptyfd, buf, show_fig);
get_ime_status(constr);
if(constr[0] != 0) hconstr = 1;
}
else if(show_fig == 1) write(p->ptyfd, buf, show_fig);
}
}
else {
write(p->ptyfd, buf, ret);
}
#else
write(p->ptyfd, buf, ret);
#endif
}
} else if (FD_ISSET(p->ptyfd,&fds)) {
ret = read(p->ptyfd, buf, BUF_SIZE);
if (ret > 0) {
/* write(1, buf, ret); */
tvterm_emulate(&(p->vterm), buf, ret);
tvterm_refresh(&(p->vterm));
#if 1 // Hangul
if(process_hangul_input) {
if(hconstr) {
if(hangul_output_code == 0)
tvterm_emulate(&(p->vterm), constr, 3);
else
tvterm_emulate(&(p->vterm), constr, 2);
p->vterm.pen.x -= 2;
tvterm_refresh(&(p->vterm));
hconstr = 0;
}
}
#endif
}
}
}
}
void tterm_wakeup_shell(TTerm* p, const char* tn)
{
setenv("TERM", tn, 1);
close(p->ptyfd);
login_tty(p->ttyfd);
tcsetattr(0, TCSANOW, &(p->ttysave));
setgid(getgid());
setuid(getuid());
sleep(1); /* XXX: wait vt swtich completed? */
execvp(gApp.gExecShell, gApp.gExecShellArgv);
exit(1);
}
void tterm_set_utmp(TTerm* p)
{
struct utmp utmp;
struct passwd *pw;
char *tn;
pw = getpwuid(util_getuid());
tn = rindex(p->name, '/') + 1;
memset((char *)&utmp, 0, sizeof(utmp));
strncpy(utmp.ut_id, tn + 3, sizeof(utmp.ut_id));
utmp.ut_type = DEAD_PROCESS;
setutent();
getutid(&utmp);
utmp.ut_type = USER_PROCESS;
utmp.ut_pid = getpid();
if (strncmp("/dev/", p->name, 5) == 0)
tn = p->name + 5;
strncpy(utmp.ut_line, tn, sizeof(utmp.ut_line));
strncpy(utmp.ut_user, pw->pw_name, sizeof(utmp.ut_user));
time(&(utmp.ut_time));
pututline(&utmp);
endutent();
}
void tterm_reset_utmp(TTerm* p)
{
struct utmp utmp, *utp;
char *tn;
tn = rindex(p->name, '/') + 4;
memset((char *)&utmp, 0, sizeof(utmp));
strncpy(utmp.ut_id, tn, sizeof(utmp.ut_id));
utmp.ut_type = USER_PROCESS;
setutent();
utp = getutid(&utmp);
utp->ut_type = DEAD_PROCESS;
// https://bugs.launchpad.net/ubuntu/+source/jfbterm/+bug/253163/+activity
if (utp != NULL) {
memset(utp->ut_user, 0, sizeof(utmp.ut_user));
utp->ut_type = DEAD_PROCESS;
time(&(utp->ut_time));
pututline(utp);
}
endutent();
}