-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete.c
More file actions
124 lines (107 loc) · 2.18 KB
/
complete.c
File metadata and controls
124 lines (107 loc) · 2.18 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
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "complete.h"
void free_list(struct list *list)
{
if (list == NULL)
return;
struct node *it = list->head;
while (it) {
struct node *next = it->next;
free(it->name);
free(it);
it = next;
}
free(list);
}
static const char *replace_cmd(const char *pattern, const char *placeholder,
const char *str)
{
/* TODO improve */
static char cmd[1024];
size_t len = 0, placeholder_len = strlen(placeholder);
const char *tok = pattern;
cmd[0] = '\0';
while (tok && *tok) {
const char *end = strstr(tok, placeholder);
if (end == NULL) {
snprintf(cmd + len, sizeof(cmd) - len, "%s", tok);
break;
}
len += snprintf(cmd + len, sizeof(cmd) - len, "%.*s%s",
(int) (end - tok), tok, str);
tok = end + placeholder_len;
}
return cmd;
}
struct list *complete(const char *cmd, const char *input)
{
/* resouces that must be released */
char *line = NULL;
struct list *list = NULL;
FILE *fh = NULL;
int fds[2];
if (pipe(fds))
return NULL;
pid_t pid = fork();
if (pid < -1)
goto error;
/* child */
if (pid == 0) {
dup2(fds[1], 1);
close(fds[0]);
execl("/bin/sh", "sh", "-c", replace_cmd(cmd, "%s", input),
NULL);
exit(1);
}
/* parent */
close(fds[1]);
list = malloc(sizeof(struct list));
if (list == NULL)
goto error;
list->head = list->tail = list->cur = NULL;
fh = fdopen(fds[0], "r");
if (fh == NULL)
goto error;
ssize_t len;
size_t size = 0;
while ((len = getline(&line, &size, fh)) > 0) {
struct node *node = malloc(sizeof(struct node));
if (node == NULL)
goto error;
if (isspace(line[len - 1]))
line[len - 1] = 0;
node->name = strdup(line);
node->next = NULL;
if (list->head == NULL) {
list->head = list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
}
int status = 0;
waitpid(pid, &status, 0);
if (WEXITSTATUS(status)) {
free_list(list);
list = NULL;
}
exit:
if (fh)
fclose(fh);
close(fds[0]);
free(line);
return list;
error:
printf("Error\n");
free_list(list);
list = NULL;
goto exit;
}