-
Notifications
You must be signed in to change notification settings - Fork 13
/
stdin_pipe.patch
179 lines (173 loc) · 4.43 KB
/
stdin_pipe.patch
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
diff --git a/ex.c b/ex.c
index ca49605..478333a 100644
--- a/ex.c
+++ b/ex.c
@@ -391,7 +391,9 @@ int ex_edit(const char *path, int len)
static int ec_edit(char *loc, char *cmd, char *arg)
{
char msg[128];
- int fd, len, rd = 0, cd = 0;
+ int fd = 0, len, rd = 0, cd = 0;
+ if (!cmd)
+ goto ret;
if (arg[0] == '.' && arg[1] == '/')
cd = 2;
len = strlen(arg+cd);
@@ -411,6 +413,9 @@ static int ec_edit(char *loc, char *cmd, char *arg)
ex_bufpostfix(ex_buf, arg[0]);
syn_setft(ex_ft);
}
+ if (!loc)
+ return fd < 0 || rd;
+ ret:
snprintf(msg, sizeof(msg), "\"%s\" %dL [%c]",
*ex_path ? ex_path : "unnamed", lbuf_len(xb),
fd < 0 || rd ? 'f' : 'r');
@@ -1124,15 +1129,26 @@ void ex(void)
void ex_init(char **files, int n)
{
- xbufsalloc = MAX(n, xbufsalloc);
+ xbufsalloc = MAX(n + !!stdin_fd, xbufsalloc);
ec_setbufsmax(NULL, NULL, "");
char *s = files[0] ? files[0] : "";
+ int i = n;
do {
- ec_edit("", "e", s);
+ ec_edit(!n && stdin_fd ? NULL : "", "e", s);
s = *(++files);
} while (--n > 0);
+ if (stdin_fd) {
+ if (i)
+ ec_edit(NULL, "", "");
+ i = lbuf_rd(xb, STDIN_FILENO, 0, lbuf_len(xb));
+ term_done();
+ term_init();
+ lbuf_saved(xb, 1);
+ ec_edit("", NULL, ""); /* shebang patch compat */
+ }
xmpt = 0;
xvis &= ~8;
+ signal(SIGINT, SIG_DFL); /* got past init? ok remove ^c */
if ((s = getenv("EXINIT")))
ex_command(s)
}
diff --git a/term.c b/term.c
index 83bdc97..dbabd87 100644
--- a/term.c
+++ b/term.c
@@ -1,6 +1,8 @@
sbuf *term_sbuf;
int term_record;
int xrows, xcols;
+int stdin_fd;
+static int isig;
static struct termios termios;
void term_init(void)
@@ -10,21 +12,25 @@ void term_init(void)
struct winsize win;
struct termios newtermios;
sbufn_make(term_sbuf, 2048)
- tcgetattr(0, &termios);
+ tcgetattr(stdin_fd, &termios);
newtermios = termios;
- newtermios.c_lflag &= ~(ICANON | ISIG | ECHO);
- tcsetattr(0, TCSAFLUSH, &newtermios);
+ if (!isig && stdin_fd)
+ newtermios.c_lflag &= ~(ICANON);
+ else
+ newtermios.c_lflag &= ~(ICANON | ISIG | ECHO);
+ tcsetattr(stdin_fd, TCSAFLUSH, &newtermios);
if (getenv("LINES"))
xrows = atoi(getenv("LINES"));
if (getenv("COLUMNS"))
xcols = atoi(getenv("COLUMNS"));
- if (!ioctl(0, TIOCGWINSZ, &win)) {
+ if (!ioctl(stdin_fd, TIOCGWINSZ, &win)) {
xcols = win.ws_col;
xrows = win.ws_row;
}
xcols = xcols ? xcols : 80;
xrows = xrows ? xrows : 25;
term_out("\33[m");
+ isig = 1;
}
void term_done(void)
@@ -33,7 +39,7 @@ void term_done(void)
return;
term_commit();
sbuf_free(term_sbuf)
- tcsetattr(0, 0, &termios);
+ tcsetattr(stdin_fd, 0, &termios);
}
void term_clean(void)
@@ -130,13 +136,13 @@ int term_read(void)
struct pollfd ufds[1];
int n;
if (ibuf_pos >= ibuf_cnt) {
- ufds[0].fd = STDIN_FILENO;
+ ufds[0].fd = stdin_fd;
ufds[0].events = POLLIN;
if (poll(ufds, 1, -1) <= 0)
return -1;
/* read a single input character */
- if ((n = read(STDIN_FILENO, ibuf, 1)) <= 0) {
- xquit = !isatty(STDIN_FILENO);
+ if ((n = read(stdin_fd, ibuf, 1)) <= 0) {
+ xquit = !isatty(stdin_fd);
return -1;
}
ibuf_cnt = n;
@@ -312,7 +318,7 @@ char *cmd_pipe(char *cmd, char *ibuf, int oproc)
close(ifd);
waitpid(pid, NULL, 0);
signal(SIGTTOU, SIG_IGN);
- tcsetpgrp(STDIN_FILENO, getpgrp());
+ tcsetpgrp(stdin_fd, getpgrp());
signal(SIGTTOU, SIG_DFL);
if (!ibuf) {
term_init();
diff --git a/vi.c b/vi.c
index c65d82a..1ec3ba6 100644
--- a/vi.c
+++ b/vi.c
@@ -2021,6 +2021,8 @@ void vi(int init)
static void sighandler(int signo)
{
+ if (signo == SIGINT)
+ return;
vi_back(TK_CTL('l'));
}
@@ -2028,8 +2030,9 @@ static int setup_signals(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sighandler;
- if (sigaction(SIGCONT, &sa, NULL) ||
- sigaction(SIGWINCH, &sa, NULL))
+ if (sigaction(SIGCONT, &sa, NULL)
+ || sigaction(SIGWINCH, &sa, NULL)
+ || sigaction(SIGINT, &sa, NULL))
return 0;
return 1;
}
@@ -2047,7 +2050,8 @@ int main(int argc, char *argv[])
if (argv[i][1] == '-' && !argv[i][2]) {
i++;
break;
- }
+ } else if (!argv[i][1])
+ stdin_fd = MAX(0, open(ctermid(NULL), O_RDONLY));
for (j = 1; argv[i][j]; j++) {
if (argv[i][j] == 's')
xvis |= 2|4;
diff --git a/vi.h b/vi.h
index b358bf1..fac5a7b 100644
--- a/vi.h
+++ b/vi.h
@@ -416,6 +416,7 @@ void vi_regput(int c, const char *s, int ln);
/* file system */
void dir_calc(char *path);
/* global variables */
+extern int stdin_fd;
extern int xrow;
extern int xoff;
extern int xtop;