Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
99 7u83 1
/*
2
 * This code contains changes by
3
 *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4
 *
5
 * Conditions 1, 2, and 4 and the no-warranty notice below apply
6
 * to these changes.
7
 *
8
 *
9
 * Copyright (c) 1980, 1993
10
 * 	The Regents of the University of California.  All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in the
19
 *    documentation and/or other materials provided with the distribution.
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgement:
22
 * 	This product includes software developed by the University of
23
 * 	California, Berkeley and its contributors.
24
 * 4. Neither the name of the University nor the names of its contributors
25
 *    may be used to endorse or promote products derived from this software
26
 *    without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
 * SUCH DAMAGE.
39
 *
40
 *
41
 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
42
 *
43
 * Redistribution and use in source and binary forms, with or without
44
 * modification, are permitted provided that the following conditions
45
 * are met:
46
 *   Redistributions of source code and documentation must retain the
47
 *    above copyright notice, this list of conditions and the following
48
 *    disclaimer.
49
 *   Redistributions in binary form must reproduce the above copyright
50
 *    notice, this list of conditions and the following disclaimer in the
51
 *    documentation and/or other materials provided with the distribution.
52
 *   All advertising materials mentioning features or use of this software
53
 *    must display the following acknowledgement:
54
 *      This product includes software developed or owned by Caldera
55
 *      International, Inc.
56
 *   Neither the name of Caldera International, Inc. nor the names of
57
 *    other contributors may be used to endorse or promote products
58
 *    derived from this software without specific prior written permission.
59
 *
60
 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61
 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64
 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
65
 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
66
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
68
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
70
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
71
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72
 */
73
 
74
#ifndef	lint
75
#ifdef	DOSCCS
76
char *copyright =
77
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
78
 All rights reserved.\n";
79
 
80
static char sccsid[] = "@(#)ex.c	1.36 (gritter) 2/13/05";
81
#endif	/* DOSCCS */
82
#endif	/* !lint */
83
 
84
/* from ex.c	7.5.1.1 (Berkeley) 8/12/86 */
85
 
86
#include "ex.h"
87
#include "ex_argv.h"
88
#include "ex_temp.h"
89
#include "ex_tty.h"
90
 
91
#ifdef TRACE
92
char	tttrace[]	= { '/','d','e','v','/','t','t','y','x','x',0 };
93
#endif
94
 
95
/*
96
 * The code for ex is divided as follows:
97
 *
98
 * ex.c			Entry point and routines handling interrupt, hangup
99
 *			signals; initialization code.
100
 *
101
 * ex_addr.c		Address parsing routines for command mode decoding.
102
 *			Routines to set and check address ranges on commands.
103
 *
104
 * ex_cmds.c		Command mode command decoding.
105
 *
106
 * ex_cmds2.c		Subroutines for command decoding and processing of
107
 *			file names in the argument list.  Routines to print
108
 *			messages and reset state when errors occur.
109
 *
110
 * ex_cmdsub.c		Subroutines which implement command mode functions
111
 *			such as append, delete, join.
112
 *
113
 * ex_data.c		Initialization of options.
114
 *
115
 * ex_get.c		Command mode input routines.
116
 *
117
 * ex_io.c		General input/output processing: file i/o, unix
118
 *			escapes, filtering, source commands, preserving
119
 *			and recovering.
120
 *
121
 * ex_put.c		Terminal driving and optimizing routines for low-level
122
 *			output (cursor-positioning); output line formatting
123
 *			routines.
124
 *
125
 * ex_re.c		Global commands, substitute, regular expression
126
 *			compilation and execution.
127
 *
128
 * ex_set.c		The set command.
129
 *
130
 * ex_subr.c		Loads of miscellaneous subroutines.
131
 *
132
 * ex_temp.c		Editor buffer routines for main buffer and also
133
 *			for named buffers (Q registers if you will.)
134
 *
135
 * ex_tty.c		Terminal dependent initializations from termcap
136
 *			data base, grabbing of tty modes (at beginning
137
 *			and after escapes).
138
 *
139
 * ex_unix.c		Routines for the ! command and its variations.
140
 *
141
 * ex_v*.c		Visual/open mode routines... see ex_v.c for a
142
 *			guide to the overall organization.
143
 */
144
 
145
static char *progname;
146
 
147
void 
148
erropen(void)
149
{
150
	close(1);
151
	dup(2);
152
}
153
 
154
void 
155
usage(void)
156
{
157
	printf(catgets(catd, 1, 1, "\
158
Usage: %s [- | -s] [-l] [-L] [-R] [-r [file]] [-t tag]\n\
159
       [-v] [-V] [-w size] [+cmd | -c cmd] file...\n"),
160
		progname);
161
	flush();
162
	exitex(1);
163
}
164
 
165
void
166
needarg(int c)
167
{
168
	erropen();
169
	printf(catgets(catd, 1, 2,
170
		"%s: option requires an argument -- %c\n"), progname, c);
171
	usage();
172
}
173
 
174
void
175
invopt(int c)
176
{
177
	erropen();
178
	printf(catgets(catd, 1, 3, "%s: illegal option -- %c\n"), progname, c);
179
	usage();
180
}
181
 
182
/*
183
 * Return last component of unix path name p.
184
 */
185
char *
186
tailpath(register char *p)
187
{
188
	register char *r;
189
 
190
	for (r=p; *p; p++)
191
		if (*p == '/')
192
			r = p+1;
193
	return(r);
194
}
195
 
196
/*
197
 * Check ownership of file.  Return nonzero if it exists and is owned by the
198
 * user or the option sourceany is used
199
 */
200
int 
201
iownit(char *file)
202
{
203
	struct stat sb;
204
 
205
	if (*file == '.' && value(EXRC) == 0)
206
		return 0;
207
	if (stat(file, &sb))
208
		return 0;
209
	if (value(SOURCEANY))
210
		return 1;
211
	if (sb.st_uid != getuid())
212
		return 0;
213
	if (sb.st_mode & (S_IWOTH | S_IWGRP))
214
		return 0;
215
	return 1;
216
}
217
 
218
shand 
219
setsig(int signum, shand handler)
220
{
221
	struct sigaction nact, oact;
222
 
223
	nact.sa_handler = handler;
224
	sigemptyset(&nact.sa_mask);
225
	nact.sa_flags = 0;
226
	if (signum == SIGALRM) {
227
#ifdef	SA_INTERRUPT
228
		nact.sa_flags |= SA_INTERRUPT;
229
#endif
230
	/*EMPTY*/ ;
231
	} else {
232
#ifdef	SA_RESTART
233
		nact.sa_flags |= SA_RESTART;
234
#endif
235
	/*EMPTY*/ ;
236
	}
237
	if (sigaction(signum, &nact, &oact) != 0)
238
		return SIG_ERR;
239
	return oact.sa_handler;
240
}
241
 
242
/*
243
 * Initialization, before editing a new file.
244
 * Main thing here is to get a new buffer (in fileinit),
245
 * rest is peripheral state resetting.
246
 */
247
void 
248
init(void)
249
{
250
	register int i;
251
 
252
	fileinit();
253
	dot = zero = truedol = unddol = dol = fendcore;
254
	one = zero+1;
255
	undkind = UNDNONE;
256
	chng = 0;
257
	edited = 0;
258
	for (i = 0; i <= 'z'-'a'+1; i++)
259
		names[i] = 1;
260
	anymarks = 0;
261
}
262
 
263
/*
264
 * Main procedure.  Process arguments and then
265
 * transfer control to the main command processing loop
266
 * in the routine commands.  We are entered as either "ex", "edit", "vi"
267
 * or "view" and the distinction is made here.  Actually, we are "vi" if
268
 * there is a 'v' in our name, "view" is there is a 'w', and "edit" if
269
 * there is a 'd' in our name.  For edit we just diddle options;
270
 * for vi we actually force an early visual command.
271
 */
272
int 
273
main(register int ac, register char *av[])
274
{
275
#ifndef VMUNIX
276
	char *erpath = EXSTRINGS;
277
#endif
278
	char *cp = NULL;
279
	register int c;
280
	bool ivis;
281
	bool fast = 0;
282
#ifdef TRACE
283
	register char *tracef;
284
#endif
285
 
286
	CLOBBGRD(ivis);
287
	CLOBBGRD(fast);
288
	CLOBBGRD(cp);
289
 
290
	/*
291
	 * Initialize the built-in memory allocator.
292
	 */
293
#ifdef	VMUNIX
294
	poolsbrk(0);
295
#endif
296
 
297
	/*
298
	 * Immediately grab the tty modes so that we wont
299
	 * get messed up if an interrupt comes in quickly.
300
	 */
301
	gTTY(1);
302
	normf = tty;
303
	ppid = getpid();
304
	/*
305
	 * Defend against d's, v's, w's, and a's in directories of
306
	 * path leading to our true name.
307
	 */
308
	av[0] = tailpath(av[0]);
309
 
310
	/*
311
	 * Figure out how we were invoked: ex, edit, vi, view.
312
	 */
313
	ivis = any('v', av[0]);	/* "vi" */
314
	if (any('w', av[0]))	/* "view" */
315
		value(READONLY) = 1;
316
	if (any('d', av[0])) {	/* "edit" */
317
		value(SHOWMODE) = 1;
318
		/*
319
		 * I do not understand why novices should not
320
		 * switch to visual mode. So they can now. gritter
321
		 */
322
		/*value(OPEN) = 0;*/
323
		value(REPORT) = 1;
324
		value(MAGIC) = 0;
325
	}
326
 
327
#ifndef VMUNIX
328
	/*
329
	 * For debugging take files out of . if name is a.out.
330
	 */
331
	if (av[0][0] == 'a')
332
		erpath = tailpath(erpath);
333
#endif	/* !VMUNIX */
334
 
335
	progname = av[0];
336
	/*
337
	 * Open the error message file.
338
	 */
339
	draino();
340
#ifndef VMUNIX
341
	erfile = open(erpath+4, O_RDONLY);
342
	if (erfile < 0) {
343
		erfile = open(erpath, O_RDONLY);
344
	}
345
#endif	/* !VMUNIX */
346
	pstop();
347
 
348
	/*
349
	 * Initialize interrupt handling.
350
	 */
351
	oldhup = signal(SIGHUP, SIG_IGN);
352
	if (oldhup == SIG_DFL)
353
		signal(SIGHUP, onhup);
354
	oldquit = signal(SIGQUIT, SIG_IGN);
355
#ifdef	SIGXFSZ
356
	oldxfsz = signal(SIGXFSZ, SIG_IGN);
357
#endif
358
	ruptible = signal(SIGINT, SIG_IGN) == SIG_DFL;
359
	if (signal(SIGTERM, SIG_IGN) == SIG_DFL)
360
		signal(SIGTERM, onhup);
361
#ifdef	SIGEMT
362
	if (signal(SIGEMT, SIG_IGN) == SIG_DFL)
363
		signal(SIGEMT, onemt);
364
#endif
365
 
366
#ifdef	BIT8
367
#ifndef	ISO8859_1
368
	setlocale(LC_CTYPE, "");
369
#endif
370
#endif
371
 
372
#ifdef	MB_CUR_MAX
373
	mb_cur_max = MB_CUR_MAX;
374
#else
375
	mb_cur_max = 1;
376
#endif
377
 
378
#ifdef	MB
379
	TRIM = mb_cur_max > 1 ? 0x6fffffff : 0xff;
380
	QUOTE = mb_cur_max > 1 ? 0x10000000 : 0x100;
381
#endif
382
 
383
#ifdef	LANGMSG
384
	setlocale(LC_MESSAGES, "");
385
	catd = catopen(CATNAME, NL_CAT_LOCALE);
386
#endif
387
 
388
	/*
389
	 * Process flag arguments.
390
	 */
391
	ac--, av++;
392
	while (ac) {
393
		if (av[0][0] == '+') {
394
			firstpat = &av[0][1];
395
			if (*firstpat == '\0')
396
				needarg('+');
397
		} else if (av[0][0] == '-') {
398
arggroup:
399
		c = av[0][1];
400
		if (c == 0
401
			|| c == 's'
402
				) {
403
			hush = 1;
404
			value(AUTOPRINT) = 0;
405
			fast++;
406
		} else switch (c) {
407
 
408
		case '-':
409
			if (av[0][2])
410
				invopt('-');
411
			ac--, av++;
412
			goto argend;
413
 
414
		case 'R':
415
			value(READONLY) = 1;
416
			break;
417
 
418
#ifdef TRACE
419
		case 'T':
420
			if (av[0][2] == 0)
421
				tracef = "trace";
422
			else {
423
				tracef = tttrace;
424
				tracef[8] = av[0][2];
425
				if (tracef[8])
426
					tracef[9] = av[0][3];
427
				else
428
					tracef[9] = 0;
429
			}
430
			trace = fopen(tracef, "w");
431
#define tracbuf NULL
432
			if (trace == NULL)
433
				printf(catgets(catd, 1, 4,
434
						"Trace create error\n"));
435
			else
436
				setbuf(trace, tracbuf);
437
			break;
438
 
439
#endif	/* TRACE */
440
 
441
		case 'c':
442
			if (av[0][2] == '\0' && (av[1] == NULL
443
					|| *av[1] == '-' || *av[1] == '+'))
444
				needarg('c');
445
			if (av[0][2]) {
446
				firstpat = &av[0][2];
447
			} else {
448
				firstpat = av[1];
449
				ac--, av++;
450
			}
451
			break;
452
 
453
		case 'e':
454
			ivis = 0;
455
			break;
456
 
457
#ifdef LISPCODE
458
		case 'l':
459
			value(LISP) = 1;
460
			value(SHOWMATCH) = 1;
461
			break;
462
#endif
463
 
464
		case 'L':
465
		case 'r':
466
			recov++;
467
			break;
468
 
469
		case 't':
470
			if (av[0][2]) {
471
				tflag = 1;
472
				safecp(lasttag, av[0], sizeof lasttag,
473
						"argument to -t too long");
474
			} else if (ac > 1 && av[1][0] != '-' &&
475
					av[1][0] != '+') {
476
				ac--, av++;
477
				tflag = 1;
478
				safecp(lasttag, av[0], sizeof lasttag,
479
						"argument to -t too long");
480
			} else
481
				needarg('t');
482
			break;
483
 
484
		case 'v':
485
			ivis = 1;
486
			break;
487
 
488
		case 'V':
489
			verbose = 1;
490
			break;
491
 
492
		case 'w':
493
			if (av[0][2])
494
				cp = &av[0][2];
495
			else if (ac > 1 && av[1][0] != '-' && av[1][0] != '+') {
496
				cp = av[1];
497
				ac--, av++;
498
			} else
499
				needarg('w');
500
			defwind = atoi(cp);
501
			break;
502
 
503
		default:
504
			invopt(c);
505
		}
506
		if (c && c != 'c' && c != 't' && c != 'w' && av[0][2]) {
507
			av[0]++;
508
			goto arggroup;
509
		}
510
	    } else
511
		    break;
512
		ac--, av++;
513
	}
514
argend:
515
 
516
	cntrlhm = catgets(catd, 1, 70, "^H discarded\n");
517
	/*
518
	 * Initialize end of core pointers.
519
	 * Normally we avoid breaking back to fendcore after each
520
	 * file since this can be expensive (much core-core copying).
521
	 * If your system can scatter load processes you could do
522
	 * this as ed does, saving a little core, but it will probably
523
	 * not often make much difference.
524
	 */
525
	fendcore = (line *) sbrk(0);
526
	endcore = fendcore - 2;
527
 
528
#ifdef SIGTSTP
529
	if (!hush && signal(SIGTSTP, SIG_IGN) == SIG_DFL)
530
		signal(SIGTSTP, onsusp), dosusp++;
531
#endif	/* SIGTSTP */
532
 
533
	/*
534
	 * If we are doing a recover and no filename
535
	 * was given, then execute an exrecover command with
536
	 * the -r option to type out the list of saved file names.
537
	 * Otherwise set the remembered file name to the first argument
538
	 * file name so the "recover" initial command will find it.
539
	 */
540
	if (recov) {
541
		if (ac == 0) {
542
			ppid = 0;
543
			setrupt();
544
			execl(EXRECOVER, "exrecover", "-r", (char *)0);
545
			filioerr(EXRECOVER);
546
			exitex(1);
547
		}
548
		safecp(savedfile, *av++, sizeof savedfile, "Filename too long");
549
		ac--;
550
	}
551
 
552
	/*
553
	 * Initialize the argument list.
554
	 */
555
	argv0 = av;
556
	argc0 = ac;
557
	args0 = av[0];
558
	erewind();
559
 
560
	/*
561
	 * Initialize a temporary file (buffer) and
562
	 * set up terminal environment.  Read user startup commands.
563
	 */
564
	if (setexit() == 0) {
565
		setrupt();
566
		intty = isatty(0);
567
		value(PROMPT) = intty;
568
		if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
569
			safecp(shell, cp, sizeof shell, "$SHELL too long");
570
		if (fast || !intty)
571
			setterm("dumb");
572
		else {
573
			gettmode();
574
			if ((cp = getenv("TERM")) != 0 && *cp) {
575
				setterm(cp);
576
			}
577
		}
578
	}
579
	if (setexit() == 0) {
580
		/*
581
		 * This is necessary because 'if (setexit() == 0 && !fast)'
582
		 * is rejected on the Cray.
583
		 */
584
		if (fast)
585
			goto skip;
586
		if ((globp = getenv("EXINIT")) && *globp)
587
			commands(1,1);
588
		else {
589
			globp = 0;
590
			if ((cp = getenv("HOME")) != 0 && *cp) {
591
				safecat(safecp(genbuf, cp, sizeof genbuf,
592
							"$HOME too long"),
593
						"/.exrc", sizeof genbuf,
594
						"$HOME too long");
595
				if (iownit(genbuf))
596
					source(genbuf, 1);
597
			}
598
		}
599
		/*
600
		 * Allow local .exrc too.  This loses if . is $HOME,
601
		 * but nobody should notice unless they do stupid things
602
		 * like putting a version command in .exrc.  Besides,
603
		 * they should be using EXINIT, not .exrc, right?
604
		 *
605
		 * This may not be done anymore. GR
606
		 */
607
		/*
608
		 * The getcwd() function is not present on very
609
		 * old Unix systems. So if this fails, comment out
610
		 * the following three lines or supply code e.g. from
611
		 * the `pwd' utility.
612
		 */
613
		if (cp == NULL || *cp == '\0'
614
				|| getcwd(genbuf, MAXBSIZE) == NULL
615
				|| strcmp(cp, genbuf) != 0)
616
 
617
		if (iownit(".exrc"))
618
			source(".exrc", 1);
619
	}
620
skip:	init();	/* moved after prev 2 chunks to fix directory option */
621
 
622
	/*
623
	 * Initial processing.  Handle tag, recover, and file argument
624
	 * implied next commands.  If going in as 'vi', then don't do
625
	 * anything, just set initev so we will do it later (from within
626
	 * visual).
627
	 */
628
	if (setexit() == 0) {
629
		if (recov)
630
			globp = "recover";
631
		else if (tflag)
632
			globp = ivis ? "tag" : "tag|p";
633
		else if (argc)
634
			globp = "next";
635
		if (ivis)
636
			initev = globp;
637
		else if (globp) {
638
			inglobal = 1;
639
			commands(1, 1);
640
			inglobal = 0;
641
		}
642
	}
643
 
644
	/*
645
	 * Vi command... go into visual.
646
	 * Strange... everything in vi usually happens
647
	 * before we ever "start".
648
	 */
649
	if (ivis) {
650
		/*
651
		 * Don't have to be upward compatible with stupidity
652
		 * of starting editing at line $.
653
		 */
654
		if (dol > zero)
655
			dot = one;
656
		globp = "visual";
657
		if (setexit() == 0)
658
			commands(1, 1);
659
	}
660
 
661
	/*
662
	 * Clear out trash in state accumulated by startup,
663
	 * and then do the main command loop for a normal edit.
664
	 * If you quit out of a 'vi' command by doing Q or ^\,
665
	 * you also fall through to here.
666
	 */
667
	seenprompt = 1;
668
	ungetchar(0);
669
	globp = 0;
670
	initev = 0;
671
	setlastchar('\n');
672
	setexit();
673
	commands(0, 0);
674
	cleanup(1);
675
	exitex(0);
676
	/*NOTREACHED*/
677
	return 0;
678
}