-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.c
285 lines (241 loc) · 6.88 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
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
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <setjmp.h>
#include <getopt.h>
#include "common.h"
#include "executor.h"
#include "completion.h"
#include "slot.h"
const char *HSS_VERSION = "1.9";
char history_file[512];
volatile bool sigint_interrupt_enabled = false;
sigjmp_buf sigint_interrupt_jmp;
struct slot *slots = NULL;
struct hss_config *pconfig = NULL;
int stdout_isatty = -1;
/* Forward declarations. */
static void
sigint_handler(int sig) {
SAVE_ERRNO;
/* if we are waiting for input, longjmp out of it */
if (sigint_interrupt_enabled) {
sigint_interrupt_enabled = true;
siglongjmp(sigint_interrupt_jmp, 1);
}
/* just in case the write changed it */
RESTORE_ERRNO;
}
static const char *
get_prompt() {
if (stdout_isatty) {
return "$ " ANSI_COLOR_BOLD;
} else {
return "$ ";
}
}
static void
reset_prompt_color() {
if (stdout_isatty) {
printf(ANSI_COLOR_RESET);
}
}
static void
update_completion_function() {
rl_attempted_completion_function = remote_filepath_completion_func;
}
/*
* Gets a line of interactive input by GNU readline.
* The result is a malloc'd string.
* Caller *must* have set up sigint_interrupt_jmp before calling.
*/
static char *
gets_interactive() {
char *line;
rl_reset_screen_size();
sigint_interrupt_enabled = true;
line = readline(get_prompt());
reset_prompt_color();
sigint_interrupt_enabled = false;
return line;
}
static void
add_host(const char *args) {
struct slot *pslot;
pslot = new_slot(args);
if (!pslot) {
return;
}
slot_append(slots, pslot);
}
bool
isspace_string(char *str) {
char c;
while ((c = *str++) != '\0') {
if (!isspace(c)) {
return false;
}
}
return true;
}
static int
add_hostfile(const char *fname) {
char line[1024];
char *p;
FILE *f = fopen(fname, "r");
if (!f) {
eprintf("can not open file %s (%s)\n", fname, strerror(errno));
exit(1);
}
while (fgets(line, sizeof(line), f)) {
p = line;
while (isspace(*p)) p++;
if (*p == '\0') continue;
add_host(p);
}
fclose(f);
return 0;
}
void usage(const char *msg) {
if (!msg) {
eprintf("An interactive parallel ssh client.\n"
"\n"
"Usage: hss [-f hostfile] [-o file] [-u username] [command]...\n\n"
"Options:\n"
" -f file file with the list of hosts\n"
" -H host specifies a host option, support the same options as the ssh command\n"
" -l limit number of multiple ssh to perform at a time (default: unlimited)\n"
" -u user the default user name to use when connecting to the remote server\n"
" -c opts specify the common ssh options (i.e. '-p 22 -i identity_file')\n"
" -o file write remote command output to a file\n"
" -i force use a vi-style line editing interface\n"
" -v be more verbose\n"
" -V show program version\n"
" -h display this message\n"
"\n"
"For more information, see https://github.com/six-ddc/hss"
"\n"
);
exit(0);
} else {
eprintf("%s\n", msg);
exit(1);
}
}
void print_version() {
printf("hss - %s\n", HSS_VERSION);
printf("libreadline - %s\n", rl_library_version);
exit(0);
}
void
parse_opts(int argc, char **argv) {
int ret;
int opt;
const char *short_opts = "hif:H:c:u:o:l:vV";
pconfig = calloc(1, sizeof(struct hss_config));
while ((opt = getopt(argc, argv, short_opts)) != -1) {
switch (opt) {
case 'h':
usage(NULL);
break;
case 'i':
rl_editing_mode = 0;
break;
case 'f':
add_hostfile(optarg);
break;
case 'H':
add_host(optarg);
break;
case 'l':
pconfig->concurrency = atoi(optarg);
break;
case 'c':
ret = parse_argv_string(optarg, &pconfig->common_options_argc, &pconfig->common_options_argv);
if (ret != 0) {
eprintf("unable parse options \"%s\"\n", optarg);
exit(1);
}
break;
case 'u':
pconfig->user = optarg;
break;
case 'o':
pconfig->output_file = new_string(optarg);
break;
case 'v':
pconfig->verbose = true;
break;
case 'V':
print_version();
break;
default:
usage(NULL);
}
}
argc -= optind;
argv += optind;
if (slots->next == NULL) {
eprintf("ssh slots is empty\n");
}
if (argc == 0) {
if (!stdout_isatty && isatty(STDIN_FILENO)) {
usage("missing command parameter");
}
return;
}
exec_remote_cmd_args(slots, argc, argv, pconfig->concurrency);
exit(0);
}
int
startup_hook() {
reset_prompt_color();
return 0;
}
int
main(int argc, char **argv) {
int ret = 0;
char *line;
slots = calloc(1, sizeof(struct slot));
stdout_isatty = isatty(STDOUT_FILENO);
/* Set the default locale values according to environment variables. */
setlocale(LC_ALL, "");
signal(SIGCHLD, reap_child_handler);
parse_opts(argc, argv);
signal(SIGINT, sigint_handler);
/* initialize readline */
rl_startup_hook = startup_hook;
rl_readline_name = "hss";
rl_initialize();
using_history();
sprintf(history_file, "%s/%s", getenv("HOME"), ".hss_history");
read_history(history_file);
update_completion_function();
while (1) {
/* Establish longjmp destination for exiting from wait-for-input. We
must re-do this each time through the loop for safety, since the
jmpbuf might get changed during command execution. */
if (sigsetjmp(sigint_interrupt_jmp, 1) != 0) {
/* got here with longjmp */
putc('\n', stdout);
}
fflush(stdout);
line = gets_interactive();
if (line == NULL || strcmp(line, "exit") == 0) {
if (line == NULL)
putc('\n', stdout);
printf("exit\n");
break;
}
if (*line) {
add_history(line);
ret = write_history(history_file);
if (ret != 0) {
eprintf("failed to write history: %s\n", strerror(errno));
}
exec_remote_cmd(slots, line, pconfig->concurrency);
}
rl_free(line);
}
return 0;
}