-
Notifications
You must be signed in to change notification settings - Fork 1
/
te_rdcmd.c
executable file
·287 lines (254 loc) · 9.09 KB
/
te_rdcmd.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* TECO for Ultrix Copyright 1986 Matt Fichtenbaum */
/* This program and its components belong to GenRad Inc, Concord MA 01742 */
/* They may be copied if this copyright notice is included */
/* te_rdcmd.c read in the command string 6/4/87 */
/* version for multiple buffers 04/13/89 10.22 */
#include "te_defs.h"
int ccount; /* count of chars read in */
int read_cmdstr()
{
char c; /* temporary character */
int i; /* temporary */
goto prompt;
restart: /* prompt again: new line */
if (!eisw && !inp_noterm) crlf(); /* if input not from a file */
prompt: /* issue prompt */
if (!eisw && !inp_noterm)
{
#ifndef _POSIX_SOURCE
reset_ctlo(); /* reset any ^O */
#endif /* _POSIX_SOURCE */
type_char('*');
}
ccount = 0;
lastc = ' ';
reline: /* continue reading */
for (;;) /* loop to read command string chars */
{
if (!eisw && !inp_noterm) /* if terminal input */
{
if ((c = gettty()) == DEL) /* process rubout */
{
if (!ccount) goto restart; /* if at beginning, ignore */
--ccount; /* decrement char count */
backc(&cmdstr); /* back up the command-string pointer */
/* look at the character just deleted */
if (((c = cmdstr.p->ch[cmdstr.c]) < ' ') && (c != ESC)) /* control chars: set c to char erased */
{
if (c == LF) vt(VT_LINEUP); /* line up */
else if ((c == CR) || (c == TAB))
{
i = find_lasteol(); /* back up to previous line */
type_char(CR); /* erase current line */
vt(VT_EEOL);
if (i == ccount) type_char('*'); /* if this was line with prompt, retype the prompt */
for (; (t_qp.p != cmdstr.p) || (t_qp.c != cmdstr.c); fwdc(&t_qp))
type_char(t_qp.p->ch[t_qp.c]); /* retype line: stop before deleted position */
}
else
{
vt(VT_BS2); /* erase ordinary ctrl chars */
char_count -= 2;
}
}
else
{
vt(VT_BS1); /* erase printing chars */
char_count--;
}
lastc = ' '; /* disable dangerous last chars */
continue;
} /* end 'rubout' processing */
else if (c == CTL('U')) /* process "erase current line" */
{
type_char(CR); /* erase line */
vt(VT_EEOL);
if ((ccount -= find_lasteol()) <= 0) goto prompt; /* back up to last eol: if beginning, restart */
cmdstr.p = t_qp.p; /* put command pointer back to this point */
cmdstr.c = t_qp.c;
lastc = ' ';
continue; /* and read it again */
}
else /* not a rubout or ^U */
{
if (!ccount) /* if at beginning of line */
{
if (c == '*') /* save old command string */
{
type_char('*'); /* echo character */
type_char(c = gettty()); /* read reg spec and echo */
i = getqspec(0, c);
free_blist(qreg[i].f); /* return its previous contents */
qreg[i].f = cbuf.f; /* put the old command string in its place */
if (qreg[i].f) qreg[i].f->b = (struct buffcell *) &qreg[i];
qreg[i].z = cbuf.z;
cbuf.f = (struct buffcell *) (cbuf.z = 0); /* no old command string */
err = 0; /* no previous error */
goto restart;
}
else if ((c == '?') && (err)) /* echo previous command string up to error */
{
type_char('?'); /* echo ? */
for (aa.p = cptr.p; aa.p->b->b != NULL; aa.p = aa.p->b); /* find beginning */
for (aa.c = 0; (aa.p != cptr.p) || (aa.c < cptr.c); fwdc(&aa)) type_char(aa.p->ch[aa.c]);
type_char('?'); /* a final ? */
err = 0; /* reset error switch */
goto restart;
}
else if ((c == LF) || (c == CTL('H'))) /* line feed, backspace */
{
pbuff->dot += lines( (c == LF) ? 1 : -1); /* pointer up or down one line */
window(WIN_LINE); /* display one line */
goto restart;
}
else if ((c == CTL('W')) && (WN_scroll != 0)) /* immediate window redisplay */
{
window(WIN_REDRAW); /* redraw full window */
window(WIN_REFR);
goto restart;
}
else if ((c == ESC) && (ed_val & 32))
{
esp = &estack[0];
c = gettty();
if (c < ' ') /* control character: type 0 */
{
esp->val2 = 0;
esp->val1 = c;
}
else
{
if (c == '[' || c == 'O') c = gettty();
if (isupper(c))
{
esp->val2 = 1;
esp->val1 = c - 'A' + 1;
}
else if (islower(c))
{
esp->val2 = 2;
esp->val1 = c - 'a' + 1;
}
else if (isdigit(c))
{
esp->val2 = 3;
esp->val1 = c - '0';
while ((c = gettty()) != '~') esp->val1 *= 10, esp->val1 += c - '0';
}
else goto restart;
}
esp->flag2 = esp->flag1 = 1;
exec_qreg(&qreg[getqspec(0, 'a')]);
window(WIN_REFR);
goto reline;
}
else /* first real command on a line */
{
make_buffer(&cbuf); /* start a command string if need be */
cmdstr.p = cbuf.f; /* set cmdstr to point to start of command string */
cmdstr.c = 0;
cbuf.z = 0; /* no chars in command string now */
err = 0; /* clear last error flag */
}
} /* end of "if first char on line" */
/* check ^G-something */
if (lastc == CTL('G'))
{
if (c == CTL('G'))
{
cbuf.z = ccount; /* save count for possible "save in q-reg" */
goto restart;
}
if ((c == '*') || (c == ' '))
{
backc(&cmdstr); /* remove the previous ^G from buffer */
--ccount;
crlf();
retype_cmdstr(c); /* retype appropriate part of command string */
lastc = ' ';
continue;
} /* end of ^G* and ^G<sp> processing */
} /* end of "last char was ^G" */
} /* end of "not rubout or ^U */
} /* end of "if !eisw" */
else /* if input from indirect file or redirected std input */
{
if (!ccount) /* first command? */
{
if (!cbuf.f) /* start a command string if need be */
{
cbuf.f = get_bcell();
cbuf.f->b = (struct buffcell *) &cbuf;
}
cmdstr.p = cbuf.f; /* point cmdstr to start of command string */
cbuf.z = cmdstr.c = 0;
}
c = (eisw) ? getc(eisw) : gettty() ; /* get char */
if (eisw && (c == EOF)) /* if this is end of the indirect command file */
{
fclose(eisw); /* close the input file */
eisw = 0; /* reset the switch */
lastc = ' ';
continue; /* and go read more chars */
}
else
{
if ((c == LF) && (lastc != CR) && !(ez_val & EZ_CRLF)) /* LF: store implied CR first */
{
cmdstr.p->ch[cmdstr.c] = CR;
++ccount;
fwdcx(&cmdstr);
}
}
} /* end of "if redirected std in or eisw" */
/* store character in command string */
cmdstr.p->ch[cmdstr.c] = c; /* store the character */
++ccount; /* keep count of chars */
if (!eisw && !inp_noterm) type_char(c); /* echo the character */
fwdcx(&cmdstr); /* next char pos'n; extend command string if nec */
if ((c == ESC) && (lastc == ESC)) break; /* stop on 2nd ESC */
if ((c == CTL('C')) && (lastc == CTL('C'))) return(-1); /* immediate exit */
lastc = c; /* keep track of last char */
} /* end of read-char loop */
cbuf.z = ccount; /* indicate number of chars in command string */
if (!eisw && !inp_noterm) crlf(); /* final new-line */
return(0);
} /* end of read_cmdstr() */
/* back up to find most recent CR or LF in entered command string */
/* return number of chars backed up */
int find_lasteol()
{
int i;
for (i = 0, t_qp.p = cmdstr.p, t_qp.c = cmdstr.c; (backc(&t_qp)) ; i++) /* look for beg. of line */
{
if ((t_qp.p->ch[t_qp.c] == CR) || (t_qp.p->ch[t_qp.c] == LF))
{
fwdc(&t_qp); /* stop short of previous eol */
break;
}
}
char_count = 0; /* reset tab count */
return(i);
}
/* retype command string: entirely (arg = '*') or most recent line (arg = ' ') */
retype_cmdstr(c)
char c;
{
int i;
if (!inp_noterm) /* if input is really from terminal */
{
if (c == ' ') /* look for beginning of this line */
i = ccount - find_lasteol(); /* to last eol, and count char's backed up */
else
{
t_qp.p = cbuf.f; /* retype whole command string */
i = t_qp.c = 0;
}
if (!i) type_char('*'); /* if from beginning, retype prompt */
for (; i < ccount; i++) /* type command string from starting point */
{
type_char(t_qp.p->ch[t_qp.c]);
fwdc(&t_qp);
}
}
}