-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
113 lines (100 loc) · 2.33 KB
/
main.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
/*
** main.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:10:13 2000 Matthieu Lucotte
** Last update Mon Sep 1 23:18:58 2003
*/
#include "zssh.h"
int main(int argc, char *argv[])
{
init(&argc, &argv);
printf("Press ^%c (%s) to enter file transfer mode, then ? for help\n",
gl_escape, escape_help());
fflush(stdout);
if (!sfork(&gl_child_output))
dooutput();
if (!sfork(&gl_child_shell))
doshell(argc, argv, gl_shav); /* std{in,out} mapped to gl_slave in initslave() */
doinput();
return 0;
}
/* new one: test escape sequence
* NB: customizing Ctrl-Something escape sequences is easy ...
* Alt-Something is not.
*/
int escape_input(ssize_t *cc, unsigned char *ibuf)
{
if (*cc == 1 && ibuf[0] == gl_escape - '@') /* escape key code */
return 1;
return 0;
}
void read_input(ssize_t *cc, unsigned char *ibuf)
{
*cc = read(0, ibuf, BUFSIZ);
#ifdef DEBUG_CRAZY
{
int i;
for (i = 0; i < *cc; i++)
printf("%02i ", ibuf[i]);
printf("\n");
}
#endif
}
/* switch to local shell mode
*/
void rz_mode(void)
{
char **av;
int ac;
int i, k;
char *line;
gl_local_shell_mode = 1;
kill(gl_child_output, SIGSTOP); /* suspend output */
signal(SIGWINCH, SIG_DFL);
printf("\r");
printf("\n");
tcgetattr(gl_slave, &gl_tt2); /* save slave tty state */
/* TCSAFLUSH causes problems on some systems */
tcsetattr(0, TCSANOW, &gl_tt); /* was in raw mode */
i = 0;
while (i < 100) { /* action codes >= 100 exit */
line = zprompt();
if (zparse(&line, &av, &ac) < 0)
continue;
i = zrun(av);
free(line);
for (k = 0; k < ac; k++)
if (av[k])
free(av[k]);
free(av);
}
tcsetattr(0, TCSANOW, &gl_rtt); /* restore raw term */
sigwinch_handler(SIGWINCH); /* update window size */
kill(gl_child_output, SIGCONT); /* resume output */
gl_local_shell_mode = 0;
}
void fail(void)
{
done(1);
}
/* should be called only by main process */
void done(int ret)
{
if (getpid() != gl_main_pid)
error("done() should only be called by main process", "");
if (gl_child_rz)
kill(gl_child_rz, SIGTERM);
if (gl_child_shell)
kill(gl_child_shell, SIGTERM);
if (gl_child_read)
kill(gl_child_read, SIGTERM);
if (gl_child_output) {
usleep(500);
kill(gl_child_output, SIGTERM);
tcsetattr(0, TCSAFLUSH, &gl_tt);
}
exit(ret);
}