-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
78 lines (60 loc) · 1.31 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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "sshconfig/parser.h"
int main() {
StringList p = expand_path("~/.ssh/config");
FILE *f = fopen(p.list[0], "rb");
Config c = parse(f);
int fd[2];
if (pipe(fd) != 0) {
puts("pipe error");
exit(1);
}
for (int i = 0; i < c.len; i++) {
Rule r = c.rules[i];
char* host = "-";
char* user = "-";
char* hostname = "-";
for (int j = 0; j < r.len; j++) {
Attribute a = r.attributes[j];
if (strcmp(a.key, "Host") == 0) {
host = a.value;
continue;
}
if (strcmp(a.key, "User") == 0) {
user = a.value;
continue;
}
if (strcmp(a.key, "HostName") == 0) {
hostname = a.value;
continue;
}
}
char *msg = malloc(sizeof(char) * (strlen(host) + strlen(user) + strlen(hostname) + 1));
memset(msg, 0, sizeof(char) * (strlen(host) + strlen(user) + strlen(hostname) + 1));
sprintf(msg, ": %-40s: %s@%s\n", host, user, hostname);
write(fd[1], msg, strlen(msg) + 1);
free(msg);
}
// cleanup memory
fclose(f);
free_config(c);
free(p.list[0]);
free(p.list);
// handover to fzf
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
char *args[] = {
"fzf",
"--delimiter",
":",
"--bind",
"enter:become(ssh {2})",
NULL
};
execvp(args[0], args);
puts("error opening fzf");
return 1;
}