-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex.c
435 lines (405 loc) · 8.97 KB
/
ex.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* Copyright (c) 1979 Regents of the University of California */
#include "ex.h"
#include "ex_argv.h"
#include "ex_temp.h"
#include "ex_tty.h"
#ifdef TRACE
char tttrace[] = { '/','d','e','v','/','t','t','y','x','x',0 };
#endif
/*
* The code for ex is divided as follows:
*
* ex.c Entry point and routines handling interrupt, hangup
* signals; initialization code.
*
* ex_addr.c Address parsing routines for command mode decoding.
* Routines to set and check address ranges on commands.
*
* ex_cmds.c Command mode command decoding.
*
* ex_cmds2.c Subroutines for command decoding and processing of
* file names in the argument list. Routines to print
* messages and reset state when errors occur.
*
* ex_cmdsub.c Subroutines which implement command mode functions
* such as append, delete, join.
*
* ex_data.c Initialization of options.
*
* ex_get.c Command mode input routines.
*
* ex_io.c General input/output processing: file i/o, unix
* escapes, filtering, source commands, preserving
* and recovering.
*
* ex_put.c Terminal driving and optimizing routines for low-level
* output (cursor-positioning); output line formatting
* routines.
*
* ex_re.c Global commands, substitute, regular expression
* compilation and execution.
*
* ex_set.c The set command.
*
* ex_subr.c Loads of miscellaneous subroutines.
*
* ex_temp.c Editor buffer routines for main buffer and also
* for named buffers (Q registers if you will.)
*
* ex_tty.c Terminal dependent initializations from termcap
* data base, grabbing of tty modes (at beginning
* and after escapes).
*
* ex_v*.c Visual/open mode routines... see ex_v.c for a
* guide to the overall organization.
*/
/*
* Main procedure. Process arguments and then
* transfer control to the main command processing loop
* in the routine commands. We are entered as either "ex", "edit" or "vi"
* and the distinction is made here. Actually, we are "vi" if
* there is a 'v' in our name, and "edit" if there is a 'd' in our
* name. For edit we just diddle options; for vi we actually
* force an early visual command, setting the external initev so
* the q command in visual doesn't give command mode.
*/
int
main(int ac, char **av)
{
#if 0
char *erpath = EXSTRINGS;
#endif
register char *cp;
register int c;
bool recov = 0;
bool ivis = any('v', av[0]);
bool itag = 0;
bool fast = 0;
#ifdef TRACE
register char *tracef;
#endif
/*
* Immediately grab the tty modes so that we wont
* get messed up if an interrupt comes in quickly.
*/
gTTY(1);
normf = tty;
#if 0
/*
* For debugging take files out of . if name is a.out.
* If a 'd' in our name, then set options for edit.
*/
if (av[0][0] == 'a')
erpath += 9;
#endif
if (ivis) {
options[MAGIC].odefault = value(MAGIC) = 0;
options[BEAUTIFY].odefault = value(BEAUTIFY) = 1;
} else if (any('d', av[0])) {
value(OPEN) = 0;
value(REPORT) = 1;
value(MAGIC) = 0;
}
/*
* Open the error message file.
*/
draino();
#if 0
erfile = open(erpath, 0);
if (erfile < 0) {
flush();
exit(1);
}
#endif
pstop();
/*
* Initialize interrupt handling.
*/
oldhup = signal(SIGHUP, SIG_IGN);
if (oldhup == SIG_DFL)
signal(SIGHUP, onhup);
oldquit = signal(SIGQUIT, SIG_IGN);
ruptible = signal(SIGINT, SIG_IGN) == SIG_DFL;
if (signal(SIGTERM, SIG_IGN) == SIG_DFL)
signal(SIGTERM, onhup);
/*
* Initialize end of core pointers.
* Normally we avoid breaking back to fendcore after each
* file since this can be expensive (much core-core copying).
* If your system can scatter load processes you could do
* this as ed does, saving a little core, but it will probably
* not often make much difference.
*/
#ifdef UNIX_SBRK
fendcore = (line *) sbrk(0);
endcore = fendcore - 2;
#else
# define LINELIMIT 0x8000
fendcore = malloc(LINELIMIT * sizeof(line *));
endcore = fendcore + LINELIMIT - 1;
#endif
/*
* Process flag arguments.
*/
ac--, av++;
while (ac && av[0][0] == '-') {
c = av[0][1];
if (c == 0) {
hush = 1;
value(AUTOPRINT) = 0;
fast++;
} else switch (c) {
#ifdef TRACE
case 'T':
if (av[0][2] == 0)
tracef = "trace";
else {
tracef = tttrace;
tracef[8] = av[0][2];
if (tracef[8])
tracef[9] = av[0][3];
else
tracef[9] = 0;
}
trace = fopen(tracef, "w");
if (trace == NULL)
ex_printf("Trace create error\n");
setbuf(trace, tracbuf);
break;
#endif
#ifdef LISP
case 'l':
value(LISP) = 1;
value(SHOWMATCH) = 1;
break;
#endif
case 'r':
recov++;
break;
case 't':
if (ac > 1 && av[1][0] != '-') {
ac--, av++;
itag = 1;
/* BUG: should check for too long tag. */
CP(lasttag, av[0]);
}
break;
case 'v':
globp = "";
ivis = 1;
break;
default:
smerror("Unknown option %s\n", av[0]);
break;
}
ac--, av++;
}
if (ac && av[0][0] == '+') {
firstln = getn(av[0] + 1);
if (firstln == 0)
firstln = 20000;
ac--, av++;
}
/*
* If we are doing a recover and no filename
* was given, then execute an exrecover command with
* the -r option to type out the list of saved file names.
* Otherwise set the remembered file name to the first argument
* file name so the "recover" initial command will find it.
*/
if (recov) {
if (ac == 0) {
die++;
setrupt();
execl(EXRECOVER, "exrecover", "-r", NULL);
filioerr(EXRECOVER);
exit(1);
}
CP(savedfile, *av);
av++, ac--;
}
/*
* Initialize the argument list.
*/
argv0 = av;
argc0 = ac;
args0 = av[0];
erewind();
/*
* Initialize a temporary file (buffer) and
* set up terminal environment. Read user startup commands.
*/
init();
if (setexit() == 0) {
setrupt();
intty = isatty(0);
if (fast || !intty)
setterm("dumb");
else {
gettmode();
if ((cp = getenv("TERM")) != 0)
setterm(cp);
if ((cp = getenv("HOME")) != 0)
source(strcat(strcpy(genbuf, cp), "/.exrc"), 1);
}
}
/*
* Initial processing. Handle tag, recover, and file argument
* implied next commands. If going in as 'vi', then don't do
* anything, just set initev so we will do it later (from within
* visual).
*/
if (setexit() == 0) {
if (recov)
globp = "recover";
else if (itag)
globp = ivis ? "tag" : "tag|p";
else if (argc)
globp = "next";
if (ivis)
initev = globp;
else if (globp) {
inglobal = 1;
commands(1, 1);
inglobal = 0;
}
}
/*
* Vi command... go into visual.
* Strange... everything in vi usually happens
* before we ever "start".
*/
if (ivis) {
/*
* Don't have to be upward compatible with stupidity
* of starting editing at line $.
*/
if (dol > zero)
dot = one;
globp = "visual";
if (setexit() == 0)
commands(1, 1);
}
/*
* Clear out trash in state accumulated by startup,
* and then do the main command loop for a normal edit.
* If you quit out of a 'vi' command by doing Q or ^\,
* you also fall through to here.
*/
ungetchar(0);
globp = 0;
initev = 0;
setlastchar('\n');
setexit();
commands(0, 0);
cleanup(1);
return 0;
}
/*
* Initialization, before editing a new file.
* Main thing here is to get a new buffer (in fileinit),
* rest is peripheral state resetting.
*/
void
init(void)
{
register int i;
fileinit();
dot = zero = truedol = unddol = dol = fendcore;
one = zero+1;
undkind = UNDNONE;
chng = 0;
edited = 0;
for (i = 0; i <= 'z'-'a'+1; i++)
names[i] = 1;
anymarks = 0;
}
/*
* When a hangup occurs our actions are similar to a preserve
* command. If the buffer has not been [Modified], then we do
* nothing but remove the temporary files and exit.
* Otherwise, we sync the temp file and then attempt a preserve.
* If the preserve succeeds, we unlink our temp files.
* If the preserve fails, we leave the temp files as they are
* as they are a backup even without preservation if they
* are not removed.
*/
void
onhup(int i)
{
(void)i;
if (chng == 0) {
cleanup(1);
exit(0);
}
if (setexit() == 0) {
if (preserve()) {
cleanup(1);
exit(0);
}
}
exit(1);
}
/*
* An interrupt occurred. Drain any output which
* is still in the output buffering pipeline.
* Catch interrupts again. Unless we are in visual
* reset the output state (out of -nl mode, e.g).
* Then like a normal error (with the \n before Interrupt
* suppressed in visual mode).
*/
void
onintr(int i)
{
char *s = "\nInterrupt";
(void)i;
#ifdef V6
signal(SIGINT, onintr);
#else
signal(SIGINT, inopen ? vintr : onintr);
#endif
draino();
if (!inopen) {
pstop();
setlastchar('\n');
#ifndef V6
}
#else
} else
vraw();
#endif
error(inopen ? s + 1 : s);
}
/*
* If we are interruptible, enable interrupts again.
* In some critical sections we turn interrupts off,
* but not very often.
*/
void
setrupt(void)
{
if (ruptible)
#ifdef V6
signal(SIGINT, onintr);
#else
signal(SIGINT, inopen ? vintr : onintr);
#endif
}
int
preserve(void)
{
synctmp();
pid = fork();
if (pid < 0)
return (0);
if (pid == 0) {
close(0);
dup(tfile);
execl(EXPRESERVE, "expreserve", (char *) 0);
exit(1);
}
waitfor();
if (rpid == pid && status == 0)
return (1);
return (0);
}