-
Notifications
You must be signed in to change notification settings - Fork 1
/
zmodem.c
146 lines (131 loc) · 2.99 KB
/
zmodem.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
/*
** zmodem.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:11:24 2000 Matthieu Lucotte
** Last update Mon Sep 1 23:02:15 2003
*/
#include "zssh.h"
#include <readline/readline.h>
#include <readline/history.h>
/* prompt one line of input using readline */
char *zprompt(void)
{
static char *prompt = 0;
char *tmp;
char *line;
if (!prompt) {
prompt = "zssh > ";
if ((tmp = getenv("HOSTNAME"))) {
tmp = str_cat("zssh@", tmp);
prompt = str_cat(tmp, " > ");
free(tmp);
}
}
tcdrain(1);
tcdrain(0);
line = readline(prompt);
if (!line)
line = strdup("exit\n");
else
if (line[0]) /* line != "" */
add_history(line);
#ifdef DEBUG
printf("read: >%s<\n", line);
#endif
return line;
}
/* parse a line applying some shell expansions */
int zparse(char **str, char ***av, int *ac)
{
if (pc_test_escapes(*str) < 0)
return -1;
if (pc_tilde_expansion(str) < 0)
return -1;
pc_split_words(*str, ac, av);
if (*ac == 1)
return -1;
pc_quote_removal(*av, ac);
glob_args(ac, av);
#ifdef DEBUG
{
int i;
for (i = 0; (*av)[i]; i++)
printf("arg %i >%s<\n", i, (*av)[i]);
}
#endif
return 0;
}
int zrun(char **av)
{
int i, j;
gl_repeat = 0;
j = 1;
do {
i = zaction(av, gl_master, gl_slave);
if (i >= 100)
break; /* avoid repeating C_* >= 100 actions */
if (gl_repeat && j)
av++;
j = 0;
} while (gl_repeat);
return i;
}
t_act_tab cmdtab[] =
{
{"?", C_HELP, zact_help},
{"cd", C_CD, zact_cd},
{"disconnect", C_DISCONNECT, zact_disconnect},
{"escape", C_ESCAPE, zact_escape},
{"exit", C_EXIT, zact_exit},
{"help", C_HELP, zact_help},
{"hook", C_HOOK, zact_hook},
{"quit", C_EXIT, zact_exit},
{"repeat", C_REPEAT, zact_repeat},
{"rz", C_RZ, zact_hook_sub},
{"suspend", C_SUSPEND, zact_suspend},
{"sz", C_SZ, zact_hook_sub},
{"version", C_VERSION, zact_version},
{0, C_SHELL, zact_shell}
};
int zaction(char **av, int master, int slave)
{
t_act_tab *pt;
char c = 24; /* "^X" */
int i = 1;
for (pt = cmdtab; pt->name && strcmp(pt->name, av[0]); pt++)
;
gl_child_rz = 0;
pt->f(av, master);
while (gl_child_rz)
sigsuspend(&gl_sig_mask);
gl_child_rz = 0;
if (gl_copty) {
if (gl_child_read) {
kill(gl_child_read, SIGTERM);
}
while (gl_child_read)
sigsuspend(&gl_sig_mask);
gl_child_read = 0;
tcflush(gl_hook_master, TCIOFLUSH);
tcflush(gl_hook_slave, TCIOFLUSH);
}
if (gl_interrupt) {
printf("\nInterrupted !\n");
gl_interrupt = 0;
tcflush(master, TCIOFLUSH);
tcflush(slave, TCIOFLUSH);
for (i = 0; i < 10; i++) { /* double CAN*5 */
write(master, &c, 1);
tcdrain(master);
}
flush(master, 50);
flush(slave, 100);
flush(master, 50);
}
tcsetattr(gl_slave, TCSANOW, &gl_tt2);
tcsetattr(0, TCSANOW, &gl_tt);
return pt->n;
}