-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
272 lines (254 loc) · 7.92 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
/*
* This program invokes a subprocess and wraps its stderr/stdout and stdin asynchronously.
* stdout lines from the subprocess are line-buffered.
*/
const char const * const usage[] = {
"consoline version 0.3",
"",
"Usage:",
" consoline [consoline_options] command [command_args]",
"",
"Options:",
" -c",
" Leave the Ctrl+C behavior alone as a SIGINT signal. The default is",
" for Ctrl+C to behave like it does in bash.",
"",
" --no-completion",
" Turn off completion. The default is to complete from a database of",
" all words seen so far in the stdout and stdin.",
"",
" --hide-entered-lines",
" After lines are typed, make them disappear instead of staying on",
" stdout.",
"",
" --prompt=[PROMPT]",
" use prompt PROMPT. Default is \"\".",
"",
"Examples:",
" consoline bash -c \"sleep 3; echo hello; bash\"",
"",
"To enable cycle-through completion behavior, add this to you ~/.inputrc:",
" $if consoline",
" \"\\t\": menu-complete",
" $endif",
"",
};
#include "consoline.h"
#include "HistoryDatabase.h"
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <wait.h>
static int child_pid;
static int child_stdin_fd;
static int child_stdout_fd;
static fd_set child_stdout_fd_set;
static char use_completion = 1;
static HistoryDatabase * history_database;
static char handle_ctrl_c = 1;
static char is_seperator_char[256];
static void init_seperator_chars()
{
char * seperator_chars = consoline_get_completion_separators();
int i;
for (i = 0; seperator_chars[i] != '\0'; i++)
is_seperator_char[(int)seperator_chars[i]] = 1;
free(seperator_chars);
}
static char ** split(char * string)
{
int result_cap = 0x10;
char ** result = (char **)malloc(result_cap * sizeof(char *));
int result_size = 0;
int word_start = 0;
int i;
for (i = 0; ; i++) {
if (is_seperator_char[(int)string[i]] || string[i] == '\0') {
// end of a word
int word_len = i - word_start;
if (word_len != 0) {
result[result_size++] = strndup(&string[word_start], word_len);
if (result_size == result_cap) {
result_cap *= 2;
result = (char **)realloc(result, result_cap * sizeof(char *));
}
}
word_start = i + 1;
}
if (string[i] == '\0')
break;
}
result[result_size] = NULL;
return result;
}
static void register_words(char * line)
{
if (!use_completion)
return;
char ** words = split(line);
int i;
for (i = 0; words[i] != NULL; i++) {
HistoryDatabase_add(history_database, words[i]);
free(words[i]);
}
free(words);
}
static char ** completion_handler(char * line, int start, int end, const char * text)
{
return HistoryDatabase_prefix_matches(history_database, (char *)text);
}
static void eof_handler()
{
close(child_stdin_fd);
}
static void line_handler(char * line)
{
write(child_stdin_fd, line, strlen(line));
static char newline_char = '\n';
write(child_stdin_fd, &newline_char, 1);
register_words(line);
}
static void poll_subprocess()
{
static int line_buffer_capacity = 0x100;
static char * line_buffer = NULL;
static int line_buffer_cursor = 0;
struct timeval no_time;
memset(&no_time, 0, sizeof(no_time));
for (;;) {
FD_SET(child_stdout_fd, &child_stdout_fd_set);
int ready_count = select(FD_SETSIZE, &child_stdout_fd_set, NULL, NULL, &no_time);
if (ready_count < 0) {
if (errno == EINTR)
continue;
exit(1);
} else if (ready_count == 0) {
return;
}
// read a character
char c;
int read_count = read(child_stdout_fd, &c, 1);
if (read_count == 0) {
// stdout has been closed. wait and terminate with child's exit code.
int status;
waitpid(child_pid, &status, 0);
exit(WEXITSTATUS(status));
}
// expand buffer if needed
if (line_buffer == NULL || !(line_buffer_cursor + 1 < line_buffer_capacity)) {
line_buffer_capacity *= 2;
line_buffer = (char *)realloc(line_buffer, line_buffer_capacity * sizeof(char));
}
if (c != '\n') {
// buffer the char
line_buffer[line_buffer_cursor++] = c;
} else {
// flush line buffer. don't include newline.
line_buffer[line_buffer_cursor] = '\0';
consoline_println(line_buffer);
register_words(line_buffer);
line_buffer_cursor = 0;
}
}
}
static void launch_child_process(char ** child_argv)
{
int child_stdin_pipe[2];
if (pipe(child_stdin_pipe) == -1)
exit(1);
int child_stdout_pipe[2];
if (pipe(child_stdout_pipe) == -1)
exit(1);
child_pid = fork();
if (child_pid < 0) {
exit(1);
} else if (child_pid == 0) {
// child
dup2(child_stdin_pipe[0], STDIN_FILENO);
dup2(child_stdout_pipe[1], STDOUT_FILENO);
dup2(child_stdout_pipe[1], STDERR_FILENO);
// child doesn't need any of these
close(child_stdin_pipe[0]);
close(child_stdin_pipe[1]);
close(child_stdout_pipe[0]);
close(child_stdout_pipe[1]);
if (handle_ctrl_c)
consoline_ignore_ctrl_c();
// exec
execvp(child_argv[0], child_argv);
// failure
fprintf(stderr, "ERROR: Unable to start process: %s\n", child_argv[0]);
exit(1);
}
// parent
close(child_stdin_pipe[0]);
child_stdin_fd = child_stdin_pipe[1];
child_stdout_fd = child_stdout_pipe[0];
close(child_stdout_pipe[1]);
FD_ZERO(&child_stdout_fd_set);
}
static void print_usage_and_exit()
{
int i;
for (i = 0; i < sizeof(usage) / sizeof(char *); i++)
puts(usage[i]);
exit(1);
}
int main(int argc, char ** argv)
{
// process argv
char leave_stdin = 1;
const char * prompt = "";
int i;
for (i = 1; i < argc; i++) {
char * arg = argv[i];
if (arg[0] != '-')
break;
if (strcmp(arg, "-c") == 0)
handle_ctrl_c = 0;
else if (strcmp(arg, "--no-completion") == 0)
use_completion = 0;
else if (strcmp(arg, "--hide-entered-lines") == 0)
leave_stdin = 0;
else if (strncmp(arg, "--prompt=", strlen("--prompt=")) == 0)
prompt = arg + strlen("--prompt=");
else {
fprintf(stderr, "unrecognized option: %s\n\n", arg);
print_usage_and_exit();
}
}
if (use_completion) {
history_database = HistoryDatabase_create(0);
init_seperator_chars();
}
int child_argv_start = i;
int child_argv_size = argc - child_argv_start + 1;
if (child_argv_size <= 1)
print_usage_and_exit();
// prepare the child's command
char** child_argv = (char**)malloc(child_argv_size * sizeof(char *));
for (i = 0; i < child_argv_size - 1; i++)
child_argv[i] = argv[i + child_argv_start];
child_argv[i] = NULL;
consoline_init("consoline", prompt);
atexit(consoline_deinit);
consoline_set_eof_handler(eof_handler);
consoline_set_line_handler(line_handler);
consoline_set_ctrl_c_handled(handle_ctrl_c);
if (use_completion)
consoline_set_completion_handler(completion_handler);
consoline_set_leave_entered_lines_on_stdout(leave_stdin);
launch_child_process(child_argv);
for (;;) {
consoline_poll();
poll_subprocess();
// poll input at 60 Hz or whatever
struct timespec requested;
memset(&requested, 0, sizeof(requested));
requested.tv_nsec = 1000000000 / 60;
nanosleep(&requested, NULL);
}
}