-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex_get.c
258 lines (236 loc) · 3.88 KB
/
ex_get.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
#include <unistd.h>
#include "ex.h"
#include "ex_tty.h"
#include "ex_vis.h"
/*
* Ex - a text editor
* Bill Joy UCB June 1977
*
* Check linebuf overflow more carefully.
* Think about when ai allowed more.
*/
static int smunch(int, char *);
static void spruce(char *);
static void checkjunk(int);
static int junk(int);
static int junkbs;
int peekc;
int lastc = '\n';
int
ex_getchar(void)
{
int c;
c = peekc;
if (c != 0) {
peekc = 0;
return (c);
}
if (globp) {
if (*globp)
return (*globp++);
globp = 0;
lastc = EOF;
return (EOF);
}
again:
if (read(0, &lastc, 1) == 1) {
lastc &= 0177;
if (junk(lastc)) {
checkjunk(lastc);
goto again;
}
if (lastc == '\n')
noteinp();
return (lastc);
}
lastc = EOF;
return (EOF);
}
int
peekchar(void)
{
if (peekc == 0)
peekc = ex_getchar();
return (peekc);
}
STATIC int lastin;
extern char aiflag;
int
gettty(void)
{
int c;
register char *lp, *gp;
char hadup, hadnl;
int offset;
lp = linebuf;
hadnl = 0;
offset = Pline == numbline ? 8 : 0;
if (intty && !inglobal) {
if (offset)
ex_printf("\240\240%4d", dot - zero + 1);
if (value(AUTOINDENT) ^ (aiflag != 0)) {
setcol(lastin + offset);
while ((c = read(0, &genbuf, 512)) == 0) {
if (lastin == 0 && !isatty(0))
return (EOF);
lastin = backtab(lastin);
setcol(lastin + offset);
}
hadup = 0;
if ((genbuf[0] == '^' || genbuf[0] == '0') && c == 1) {
if (genbuf[0] == '0')
lastin = 0;
if (!OS) {
notech(1);
ex_putchar('\b');
ex_putchar(' ' | QUOTE);
ex_putchar('\b');
}
setcol(offset);
hadup = 1;
genbuf[0] = 0;
c = 0;
}
if (c > 0) {
c--;
if (genbuf[c] == '\n') {
if (c == 1 && genbuf[0] == '.') {
noteinp();
return(EOF);
}
hadnl++;
} else
c++;
genbuf[c] = 0;
spruce(genbuf);
if (hadup == 0 && genbuf[0]) {
lastin = smunch(lastin, genbuf);
for (c = lastin; c >= 7; c -= 8)
*lp++ = '\t';
for (; c > 0; c--)
*lp++ = ' ';
}
gp = genbuf;
while ((*lp++ = *gp++))
continue;
lp--;
}
} else if (offset) {
ex_printf(" ");
flush();
}
while (hadnl == 0) {
/*
if (lp > linebuf) {
partinp();
spruce(linebuf);
dographic();
lp = linebuf;
while (*lp)
lp++;
flush();
}
*/
c = read(0, lp, linebuf + LBSIZE - lp);
if (c <= 0 && lp == linebuf)
return (EOF);
lp += c;
*lp = 0;
if (lp >= &linebuf[LBSIZE - 2])
goto toolong;
if (lp[-1] == '\n') {
*--lp = 0;
hadnl = 1;
}
}
noteinp();
} else {
while ((c = ex_getchar()) != '\n') {
if (c == EOF) {
if (inglobal)
ungetchar(EOF);
return (EOF);
}
c &= 0177;
*lp++ = c;
if (lp >= &linebuf[LBSIZE-2])
toolong:
error("Input line too long@- limit 510 characters");
}
}
*lp++ = 0;
spruce(linebuf);
if (linebuf[0] == '.' && linebuf[1] == 0)
return (EOF);
return (0);
}
int
backtab(int i)
{
register int j;
j = i % value(SHIFTWIDTH);
if (j == 0)
j = value(SHIFTWIDTH);
i -= j;
if (i < 0)
i = 0;
return (i);
}
static int
smunch(int col, char *ocp)
{
register char *cp;
cp = ocp;
for (;;)
switch (*cp++) {
case ' ':
col++;
continue;
case '\t':
col = (col + 8) &~ 7;
continue;
default:
cp--;
CP(ocp, cp);
return (col);
}
}
static void
spruce(char *ocp)
{
register char *cp;
int c;
cp = ocp;
while ((c = *ocp++)) {
c &= 0177;
if (junk(c)) {
checkjunk(c);
continue;
}
*cp++ = c;
}
*cp++ = 0;
}
char cntrlhm[] = "CTRL(H) discarded\n";
static void
checkjunk(int c)
{
if (junkbs == 0 && c == '\b') {
write(2, cntrlhm, sizeof cntrlhm);
junkbs = 1;
}
}
int *
setin(int *addr)
{
if (addr == zero)
lastin = 0;
else
lastin = smunch(0, ex_getline(*addr));
return (addr);
}
static int
junk(int c)
{
return (c == 0 || (value(BEAUTIFY) && c < ' ' && c != '\t' && c != '\n' && c != '\f'));
}