head	1.9;
access;
symbols
	perseant-exfatfs-base-20250801:1.9
	perseant-exfatfs-base-20240630:1.9
	perseant-exfatfs:1.9.0.36
	perseant-exfatfs-base:1.9
	cjep_sun2x:1.9.0.34
	cjep_sun2x-base:1.9
	cjep_staticlib_x-base1:1.9
	cjep_staticlib_x:1.9.0.32
	cjep_staticlib_x-base:1.9
	phil-wifi-20200421:1.9
	phil-wifi-20200411:1.9
	phil-wifi-20200406:1.9
	pgoyette-compat-merge-20190127:1.9
	pgoyette-compat-20190127:1.9
	pgoyette-compat-20190118:1.9
	pgoyette-compat-1226:1.9
	pgoyette-compat-1126:1.9
	pgoyette-compat-1020:1.9
	pgoyette-compat-0930:1.9
	pgoyette-compat-0906:1.9
	pgoyette-compat-0728:1.9
	pgoyette-compat-0625:1.9
	pgoyette-compat-0521:1.9
	pgoyette-compat-0502:1.9
	pgoyette-compat-0422:1.9
	pgoyette-compat-0415:1.9
	pgoyette-compat-0407:1.9
	pgoyette-compat-0330:1.9
	pgoyette-compat-0322:1.9
	pgoyette-compat-0315:1.9
	pgoyette-compat:1.9.0.30
	pgoyette-compat-base:1.9
	perseant-stdc-iso10646:1.9.0.28
	perseant-stdc-iso10646-base:1.9
	prg-localcount2-base3:1.9
	prg-localcount2-base2:1.9
	prg-localcount2-base1:1.9
	prg-localcount2:1.9.0.26
	prg-localcount2-base:1.9
	pgoyette-localcount-20170426:1.9
	bouyer-socketcan-base1:1.9
	pgoyette-localcount-20170320:1.9
	bouyer-socketcan:1.9.0.24
	bouyer-socketcan-base:1.9
	pgoyette-localcount-20170107:1.9
	pgoyette-localcount-20161104:1.9
	localcount-20160914:1.9
	pgoyette-localcount-20160806:1.9
	pgoyette-localcount-20160726:1.9
	pgoyette-localcount:1.9.0.22
	pgoyette-localcount-base:1.9
	netbsd-5-2-3-RELEASE:1.9
	netbsd-5-1-5-RELEASE:1.9
	yamt-pagecache-base9:1.9
	yamt-pagecache-tag8:1.9
	tls-earlyentropy:1.9.0.18
	tls-earlyentropy-base:1.9
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.9
	riastradh-drm2-base3:1.9
	netbsd-5-2-2-RELEASE:1.9
	netbsd-5-1-4-RELEASE:1.9
	netbsd-5-2-1-RELEASE:1.9
	netbsd-5-1-3-RELEASE:1.9
	agc-symver:1.9.0.20
	agc-symver-base:1.9
	tls-maxphys-base:1.9
	yamt-pagecache-base8:1.9
	netbsd-5-2:1.9.0.16
	yamt-pagecache-base7:1.9
	netbsd-5-2-RELEASE:1.9
	netbsd-5-2-RC1:1.9
	yamt-pagecache-base6:1.9
	yamt-pagecache-base5:1.9
	yamt-pagecache-base4:1.9
	netbsd-5-1-2-RELEASE:1.9
	netbsd-5-1-1-RELEASE:1.9
	yamt-pagecache-base3:1.9
	yamt-pagecache-base2:1.9
	yamt-pagecache:1.9.0.14
	yamt-pagecache-base:1.9
	bouyer-quota2-nbase:1.9
	bouyer-quota2:1.9.0.12
	bouyer-quota2-base:1.9
	matt-nb5-pq3:1.9.0.10
	matt-nb5-pq3-base:1.9
	netbsd-5-1:1.9.0.8
	netbsd-5-1-RELEASE:1.9
	netbsd-5-1-RC4:1.9
	netbsd-5-1-RC3:1.9
	netbsd-5-1-RC2:1.9
	netbsd-5-1-RC1:1.9
	netbsd-5-0-2-RELEASE:1.9
	netbsd-5-0-1-RELEASE:1.9
	jym-xensuspend-nbase:1.9
	netbsd-5-0:1.9.0.6
	netbsd-5-0-RELEASE:1.9
	netbsd-5-0-RC4:1.9
	netbsd-5-0-RC3:1.9
	netbsd-5-0-RC2:1.9
	jym-xensuspend:1.9.0.4
	jym-xensuspend-base:1.9
	netbsd-5-0-RC1:1.9
	netbsd-5:1.9.0.2
	netbsd-5-base:1.9
	matt-mips64-base2:1.8
	matt-mips64:1.7.0.38
	netbsd-4-0-1-RELEASE:1.7
	wrstuden-revivesa-base-3:1.8
	wrstuden-revivesa-base-2:1.8
	wrstuden-fixsa-newbase:1.7
	wrstuden-revivesa-base-1:1.7
	yamt-pf42-base4:1.7
	yamt-pf42-base3:1.7
	hpcarm-cleanup-nbase:1.7
	yamt-pf42-baseX:1.7
	yamt-pf42-base2:1.7
	wrstuden-revivesa:1.7.0.36
	wrstuden-revivesa-base:1.7
	yamt-pf42:1.7.0.34
	yamt-pf42-base:1.7
	keiichi-mipv6-nbase:1.7
	keiichi-mipv6:1.7.0.32
	keiichi-mipv6-base:1.7
	matt-armv6-nbase:1.7
	matt-armv6-prevmlocking:1.7
	wrstuden-fixsa-base-1:1.7
	netbsd-4-0:1.7.0.30
	netbsd-4-0-RELEASE:1.7
	cube-autoconf:1.7.0.28
	cube-autoconf-base:1.7
	netbsd-4-0-RC5:1.7
	netbsd-4-0-RC4:1.7
	netbsd-4-0-RC3:1.7
	netbsd-4-0-RC2:1.7
	netbsd-4-0-RC1:1.7
	matt-armv6:1.7.0.26
	matt-armv6-base:1.7
	matt-mips64-base:1.7
	hpcarm-cleanup:1.7.0.24
	hpcarm-cleanup-base:1.7
	netbsd-3-1-1-RELEASE:1.7
	netbsd-3-0-3-RELEASE:1.7
	wrstuden-fixsa:1.7.0.22
	wrstuden-fixsa-base:1.7
	abandoned-netbsd-4-base:1.7
	abandoned-netbsd-4:1.7.0.16
	netbsd-3-1:1.7.0.18
	netbsd-3-1-RELEASE:1.7
	netbsd-3-0-2-RELEASE:1.7
	netbsd-3-1-RC4:1.7
	netbsd-3-1-RC3:1.7
	netbsd-3-1-RC2:1.7
	netbsd-3-1-RC1:1.7
	netbsd-4:1.7.0.20
	netbsd-4-base:1.7
	chap-midi-nbase:1.7
	netbsd-3-0-1-RELEASE:1.7
	chap-midi:1.7.0.14
	chap-midi-base:1.7
	netbsd-3-0:1.7.0.12
	netbsd-3-0-RELEASE:1.7
	netbsd-3-0-RC6:1.7
	netbsd-3-0-RC5:1.7
	netbsd-3-0-RC4:1.7
	netbsd-3-0-RC3:1.7
	netbsd-3-0-RC2:1.7
	netbsd-3-0-RC1:1.7
	netbsd-2-0-3-RELEASE:1.7
	netbsd-2-1:1.7.0.10
	netbsd-2-1-RELEASE:1.7
	netbsd-2-1-RC6:1.7
	netbsd-2-1-RC5:1.7
	netbsd-2-1-RC4:1.7
	netbsd-2-1-RC3:1.7
	netbsd-2-1-RC2:1.7
	netbsd-2-1-RC1:1.7
	netbsd-2-0-2-RELEASE:1.7
	netbsd-3:1.7.0.8
	netbsd-3-base:1.7
	netbsd-2-0-1-RELEASE:1.7
	netbsd-2:1.7.0.6
	netbsd-2-base:1.7
	netbsd-2-0-RELEASE:1.7
	netbsd-2-0-RC5:1.7
	netbsd-2-0-RC4:1.7
	netbsd-2-0-RC3:1.7
	netbsd-2-0-RC2:1.7
	netbsd-2-0-RC1:1.7
	netbsd-2-0:1.7.0.4
	netbsd-2-0-base:1.7
	netbsd-1-6-PATCH002-RELEASE:1.7
	netbsd-1-6-PATCH002:1.7
	netbsd-1-6-PATCH002-RC4:1.7
	netbsd-1-6-PATCH002-RC3:1.7
	netbsd-1-6-PATCH002-RC2:1.7
	netbsd-1-6-PATCH002-RC1:1.7
	netbsd-1-6-PATCH001:1.7
	netbsd-1-6-PATCH001-RELEASE:1.7
	netbsd-1-6-PATCH001-RC3:1.7
	netbsd-1-6-PATCH001-RC2:1.7
	netbsd-1-6-PATCH001-RC1:1.7
	fvdl_fs64_base:1.7
	netbsd-1-6-RELEASE:1.7
	netbsd-1-6-RC3:1.7
	netbsd-1-6-RC2:1.7
	netbsd-1-6-RC1:1.7
	netbsd-1-6:1.7.0.2
	netbsd-1-6-base:1.7
	netbsd-1-5-PATCH003:1.4
	netbsd-1-5-PATCH002:1.4
	netbsd-1-5-PATCH001:1.4
	nvi_1_79:1.1.1.4
	netbsd-1-5-RELEASE:1.4
	netbsd-1-5-BETA2:1.4
	netbsd-1-5-BETA:1.4
	netbsd-1-4-PATCH003:1.4
	netbsd-1-5-ALPHA2:1.4
	netbsd-1-5:1.4.0.10
	netbsd-1-5-base:1.4
	minoura-xpg4dl-base:1.4
	minoura-xpg4dl:1.4.0.8
	netbsd-1-4-PATCH002:1.4
	wrstuden-devbsize-19991221:1.4
	wrstuden-devbsize:1.4.0.6
	wrstuden-devbsize-base:1.4
	comdex-fall-1999:1.4.0.4
	comdex-fall-1999-base:1.4
	netbsd-1-4-PATCH001:1.4
	netbsd-1-4-RELEASE:1.4
	netbsd-1-4:1.4.0.2
	netbsd-1-4-base:1.4
	netbsd-1-3-PATCH003:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.3
	netbsd-1-3-PATCH002:1.1.1.3
	netbsd-1-3-PATCH001:1.1.1.3
	netbsd-1-3-RELEASE:1.1.1.3
	netbsd-1-3-BETA:1.1.1.3
	netbsd-1-3:1.1.1.3.0.2
	netbsd-1-3-base:1.1.1.3
	netbsd-1-2-PATCH001:1.1.1.3
	netbsd-1-2-RELEASE:1.1.1.3
	netbsd-1-2-BETA:1.1.1.3
	netbsd-1-2:1.1.1.3.0.4
	netbsd-1-2-base:1.1.1.3
	nvi_1_66:1.1.1.3
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.1.1.2
	netbsd-1-1-RELEASE:1.1.1.2
	netbsd-1-1:1.1.1.2.0.4
	netbsd-1-1-base:1.1.1.2
	netbsd-1-0-PATCH06:1.1.1.2
	netbsd-1-0-PATCH05:1.1.1.2
	netbsd-1-0-PATCH04:1.1.1.2
	netbsd-1-0-PATCH03:1.1.1.2
	netbsd-1-0-PATCH02:1.1.1.2
	netbsd-1-0-PATCH1:1.1.1.2
	netbsd-1-0-PATCH0:1.1.1.2
	netbsd-1-0-RELEASE:1.1.1.2
	netbsd-1-0:1.1.1.2.0.2
	nvi-1-34b:1.1.1.2
	nvi-1-33b:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.9
date	2008.10.29.16.49.35;	author christos;	state dead;
branches;
next	1.8;

1.8
date	2008.07.21.14.19.27;	author lukem;	state Exp;
branches;
next	1.7;

1.7
date	2002.04.09.01.47.31;	author thorpej;	state Exp;
branches
	1.7.36.1;
next	1.6;

1.6
date	2001.05.01.16.46.11;	author aymeric;	state Exp;
branches;
next	1.5;

1.5
date	2001.03.31.11.37.46;	author aymeric;	state Exp;
branches;
next	1.4;

1.4
date	99.01.08.06.16.54;	author abs;	state Exp;
branches;
next	1.3;

1.3
date	98.01.09.08.06.47;	author perry;	state Exp;
branches;
next	1.2;

1.2
date	97.11.01.06.51.48;	author lukem;	state Exp;
branches;
next	1.1;

1.1
date	94.08.17.16.19.04;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.7.36.1
date	2008.09.18.04.29.26;	author wrstuden;	state Exp;
branches;
next	;

1.1.1.1
date	94.08.17.16.19.05;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.08.17.19.25.09;	author cgd;	state Exp;
branches
	1.1.1.2.2.1;
next	1.1.1.3;

1.1.1.3
date	96.05.20.02.04.29;	author mrg;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2001.03.31.11.30.01;	author aymeric;	state Exp;
branches;
next	;

1.1.1.2.2.1
date	94.08.17.19.25.09;	author cgd;	state dead;
branches;
next	1.1.1.2.2.2;

1.1.1.2.2.2
date	94.08.17.19.25.10;	author cgd;	state Exp;
branches;
next	;


desc
@@


1.9
log
@bye old vi!
@
text
@/*	$NetBSD: main.c,v 1.8 2008/07/21 14:19:27 lukem Exp $	*/

/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@@(#) Copyright (c) 1992, 1993, 1994\
 The Regents of the University of California.  All rights reserved.\
  Copyright (c) 1992, 1993, 1994, 1995, 1996\
 Keith Bostic.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static const char sccsid[] = "@@(#)main.c	10.48 (Berkeley) 10/11/96";
#else
__RCSID("$NetBSD: main.c,v 1.8 2008/07/21 14:19:27 lukem Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"
#include "../vi/vi.h"
#include "pathnames.h"

#ifdef DEBUG
static void	 attach __P((GS *));
#endif
static void	 v_estr __P((char *, int, char *));
static int	 v_obsolete __P((char *, char *[]));

/*
 * editor --
 *	Main editor routine.
 *
 * PUBLIC: int editor __P((GS *, int, char *[]));
 */
int
editor(gp, argc, argv)
	GS *gp;
	int argc;
	char *argv[];
{
	extern int optind;
	extern char *optarg;
	const char *p;
	EVENT ev;
	FREF *frp;
	SCR *sp;
	size_t len;
	u_int flags;
	int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
#ifdef GTAGS
	int gtags = 0;
#endif
	char *tag_f, *wsizearg, path[256];

	/* Initialize the busy routine, if not defined by the screen. */
	if (gp->scr_busy == NULL)
		gp->scr_busy = vs_busy;
	/* Initialize the message routine, if not defined by the screen. */
	if (gp->scr_msg == NULL)
		gp->scr_msg = vs_msg;

	/* Common global structure initialization. */
	CIRCLEQ_INIT(&gp->dq);
	CIRCLEQ_INIT(&gp->hq);
	LIST_INIT(&gp->ecq);
	LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q);
	gp->noprint = DEFAULT_NOPRINT;

	/* Structures shared by screens so stored in the GS structure. */
	CIRCLEQ_INIT(&gp->frefq);
	CIRCLEQ_INIT(&gp->dcb_store.textq);
	LIST_INIT(&gp->cutq);
	LIST_INIT(&gp->seqq);

	/* Set initial screen type and mode based on the program name. */
	readonly = 0;
	if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
		LF_INIT(SC_EX);
	else {
		/* Nview, view are readonly. */
		if (!strcmp(gp->progname, "nview") ||
		    !strcmp(gp->progname, "view"))
			readonly = 1;
		
		/* Vi is the default. */
		LF_INIT(SC_VI);
	}

	/* Convert old-style arguments into new-style ones. */
	if (v_obsolete(gp->progname, argv))
		return (1);

	/* Parse the arguments. */
	flagchk = '\0';
	tag_f = wsizearg = NULL;
	lflag = secure = silent = 0;
	startup = 1;

	/* Set the file snapshot flag. */
	F_SET(gp, G_SNAPSHOT);

#ifdef GTAGS
#ifdef DEBUG
	while ((ch = getopt(argc, argv, "c:D:eFGlRrSsT:t:vw:")) != -1)
#else
	while ((ch = getopt(argc, argv, "c:eFGlRrSst:vw:")) != -1)
#endif
#else
#ifdef DEBUG
	while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != -1)
#else
	while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != -1)
#endif
#endif
		switch (ch) {
		case 'c':		/* Run the command. */
			/*
			 * XXX
			 * We should support multiple -c options.
			 */
			if (gp->c_option != NULL) {
				v_estr(gp->progname, 0,
				    "only one -c command may be specified.");
				return (1);
			}
			gp->c_option = optarg;
			break;
#ifdef DEBUG
		case 'D':
			switch (optarg[0]) {
			case 's':
				startup = 0;
				break;
			case 'w':
				attach(gp);
				break;
			default:
				v_estr(gp->progname, 0,
				    "usage: -D requires s or w argument.");
				return (1);
			}
			break;
#endif
		case 'e':		/* Ex mode. */
			LF_CLR(SC_VI);
			LF_SET(SC_EX);
			break;
		case 'F':		/* No snapshot. */
			F_CLR(gp, G_SNAPSHOT);
			break;
#ifdef GTAGS
		case 'G':
			gtags = 1;
			break;
#endif
		case 'l':		/* Set lisp, showmatch options. */
			lflag = 1;
			break;
		case 'R':		/* Readonly. */
			readonly = 1;
			break;
		case 'r':		/* Recover. */
			if (flagchk == 't') {
				v_estr(gp->progname, 0,
				    "only one of -r and -t may be specified.");
				return (1);
			}
			flagchk = 'r';
			break;
		case 'S':
			secure = 1;
			break;
		case 's':
			silent = 1;
			break;
#ifdef DEBUG
		case 'T':		/* Trace. */
			if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
				v_estr(gp->progname, errno, optarg);
				goto err;
			}
			(void)fprintf(gp->tracefp,
			    "\n===\ntrace: open %s\n", optarg);
			break;
#endif
		case 't':		/* Tag. */
			if (flagchk == 'r') {
				v_estr(gp->progname, 0,
				    "only one of -r and -t may be specified.");
				return (1);
			}
			if (flagchk == 't') {
				v_estr(gp->progname, 0,
				    "only one tag file may be specified.");
				return (1);
			}
			flagchk = 't';
			tag_f = optarg;
			break;
		case 'v':		/* Vi mode. */
			LF_CLR(SC_EX);
			LF_SET(SC_VI);
			break;
		case 'w':
			wsizearg = optarg;
			break;
		case '?':
		default:
			(void)gp->scr_usage();
			return (1);
		}
	argc -= optind;
	argv += optind;

	/*
	 * -s option is only meaningful to ex.
	 *
	 * If not reading from a terminal, it's like -s was specified.
	 */
	if (silent && !LF_ISSET(SC_EX)) {
		v_estr(gp->progname, 0, "-s option is only applicable to ex.");
		goto err;
	}
	if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
		silent = 1;

	/*
	 * Build and initialize the first/current screen.  This is a bit
	 * tricky.  If an error is returned, we may or may not have a
	 * screen structure.  If we have a screen structure, put it on a
	 * display queue so that the error messages get displayed.
	 *
	 * !!!
	 * Everything we do until we go interactive is done in ex mode.
	 */
	if (screen_init(gp, NULL, &sp)) {
		if (sp != NULL)
			CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
		goto err;
	}
	F_SET(sp, SC_EX);
	CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);

	if (v_key_init(sp))		/* Special key initialization. */
		goto err;

	{ int oargs[5], *oargp = oargs;
					/* Command-line options. */
#ifdef GTAGS
	if (gtags)
		*oargp++ = O_GTAGSMODE;
#endif
	if (lflag) {
		*oargp++ = O_LISP;
		*oargp++ = O_SHOWMATCH;
	}
	if (readonly)
		*oargp++ = O_READONLY;
	if (secure)
		*oargp++ = O_SECURE;
	*oargp = -1;			/* Options initialization. */
	if (opts_init(sp, oargs))
		goto err;
	}
	if (wsizearg != NULL) {
		ARGS *av[2], a, b;
		(void)snprintf(path, sizeof(path), "window=%s", wsizearg);
		a.bp = (CHAR_T *)path;
		a.len = strlen(path);
		b.bp = NULL;
		b.len = 0;
		av[0] = &a;
		av[1] = &b;
		(void)opts_set(sp, av, NULL);
	}
	if (silent) {			/* Ex batch mode option values. */
		O_CLR(sp, O_AUTOPRINT);
		O_CLR(sp, O_PROMPT);
		O_CLR(sp, O_VERBOSE);
		O_CLR(sp, O_WARN);
		F_SET(sp, SC_EX_SILENT);
	}

	sp->rows = O_VAL(sp, O_LINES);	/* Make ex formatting work. */
	sp->cols = O_VAL(sp, O_COLUMNS);

	if (!silent && startup) {	/* Read EXINIT, exrc files. */
		if (ex_exrc(sp))
			goto err;
		if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
			if (screen_end(sp))
				goto err;
			goto done;
		}
	}

	/*
	 * List recovery files if -r specified without file arguments.
	 * Note, options must be initialized and startup information
	 * read before doing this.
	 */
	if (flagchk == 'r' && argv[0] == NULL) {
		if (rcv_list(sp))
			goto err;
		if (screen_end(sp))
			goto err;
		goto done;
	}

	/*
	 * !!!
	 * Initialize the default ^D, ^U scrolling value here, after the
	 * user has had every opportunity to set the window option.
	 *
	 * It's historic practice that changing the value of the window
	 * option did not alter the default scrolling value, only giving
	 * a count to ^D/^U did that.
	 */
	sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;

	/*
	 * If we don't have a command-line option, switch into the right
	 * editor now, so that we position default files correctly, and
	 * so that any tags file file-already-locked messages are in the
	 * vi screen, not the ex screen.
	 *
	 * XXX
	 * If we have a command-line option, the error message can end
	 * up in the wrong place, but I think that the combination is
	 * unlikely.
	 */
	if (gp->c_option == NULL) {
		F_CLR(sp, SC_EX | SC_VI);
		F_SET(sp, LF_ISSET(SC_EX | SC_VI));
	}

	/* Open a tag file if specified. */
	if (tag_f != NULL && ex_tag_first(sp, tag_f))
		goto err;

	/*
	 * Append any remaining arguments as file names.  Files are recovery
	 * files if -r specified.  If the tag option or ex startup commands
	 * loaded a file, then any file arguments are going to come after it.
	 */
	if (*argv != NULL) {
		if (sp->frp != NULL) {
			/* Cheat -- we know we have an extra argv slot. */
			MALLOC_NOMSG(sp,
			    *--argv, char *, strlen(sp->frp->name) + 1);
			if (*argv == NULL) {
				v_estr(gp->progname, errno, NULL);
				goto err;
			}
			(void)strcpy(*argv, sp->frp->name);
		}
		sp->argv = sp->cargv = argv;
		F_SET(sp, SC_ARGNOFREE);
		if (flagchk == 'r')
			F_SET(sp, SC_ARGRECOVER);
	}

	/*
	 * If the ex startup commands and or/the tag option haven't already
	 * created a file, create one.  If no command-line files were given,
	 * use a temporary file.
	 */
	if (sp->frp == NULL) {
		if (sp->argv == NULL) {
			if ((frp = file_add(sp, NULL)) == NULL)
				goto err;
		} else  {
			if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
				goto err;
			if (F_ISSET(sp, SC_ARGRECOVER))
				F_SET(frp, FR_RECOVER);
		}

		if (file_init(sp, frp, NULL, 0))
			goto err;
		if (EXCMD_RUNNING(gp)) {
			(void)ex_cmd(sp);
			if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
				if (screen_end(sp))
					goto err;
				goto done;
			}
		}
	}

	/*
	 * Check to see if we need to wait for ex.  If SC_SCR_EX is set, ex
	 * was forced to initialize the screen during startup.  We'd like to
	 * wait for a single character from the user, but we can't because
	 * we're not in raw mode.  We can't switch to raw mode because the
	 * vi initialization will switch to xterm's alternate screen, causing
	 * us to lose the messages we're pausing to make sure the user read.
	 * So, wait for a complete line.  
	 */
	if (F_ISSET(sp, SC_SCR_EX)) {
		p = msg_cmsg(sp, CMSG_CONT_R, &len);
		(void)write(STDOUT_FILENO, p, len);
		for (;;) {
			if (v_event_get(sp, &ev, 0, 0))
				goto err;
			if (ev.e_event == E_INTERRUPT ||
			    ev.e_event == E_CHARACTER &&
			    (ev.e_value == K_CR || ev.e_value == K_NL))
				break;
			(void)gp->scr_bell(sp);
		}
	}

	/* Switch into the right editor, regardless. */
	F_CLR(sp, SC_EX | SC_VI);
	F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);

	/*
	 * Main edit loop.  Vi handles split screens itself, we only return
	 * here when switching editor modes or restarting the screen.
	 */
	while (sp != NULL)
		if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
			goto err;

done:	rval = 0;
	if (0)
err:		rval = 1;

	/* Clean out the global structure. */
	v_end(gp);

	return (rval);
}

/*
 * v_end --
 *	End the program, discarding screens and most of the global area.
 *
 * PUBLIC: void v_end __P((GS *));
 */
void
v_end(gp)
	GS *gp;
{
	MSGS *mp;
	SCR *sp;

	/* If there are any remaining screens, kill them off. */
	if (gp->ccl_sp != NULL) {
		(void)file_end(gp->ccl_sp, NULL, 1);
		(void)screen_end(gp->ccl_sp);
	}
	while ((sp = gp->dq.cqh_first) != (void *)&gp->dq)
		(void)screen_end(sp);
	while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
		(void)screen_end(sp);

#ifdef HAVE_PERL_INTERP
	perl_end(gp);
#endif

#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
	{ FREF *frp;
		/* Free FREF's. */
		while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
			CIRCLEQ_REMOVE(&gp->frefq, frp, q);
			if (frp->name != NULL)
				free(frp->name);
			if (frp->tname != NULL)
				free(frp->tname);
			free(frp);
		}
	}

	/* Free key input queue. */
	if (gp->i_event != NULL)
		free(gp->i_event);

	/* Free cut buffers. */
	cut_close(gp);

	/* Free map sequences. */
	seq_close(gp);

	/* Free default buffer storage. */
	(void)text_lfree(&gp->dcb_store.textq);

	/* Close message catalogs. */
	msg_close(gp);
#endif

	/* Ring the bell if scheduled. */
	if (F_ISSET(gp, G_BELLSCHED))
		(void)fprintf(stderr, "\07");		/* \a */

	/*
	 * Flush any remaining messages.  If a message is here, it's almost
	 * certainly the message about the event that killed us (although
	 * it's possible that the user is sourcing a file that exits from the
	 * editor).
	 */
	while ((mp = gp->msgq.lh_first) != NULL) {
		(void)fprintf(stderr, "%s%.*s",
		    mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
		LIST_REMOVE(mp, q);
#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
		free(mp->buf);
		free(mp);
#endif
	}

#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
	/* Free any temporary space. */
	if (gp->tmp_bp != NULL)
		free(gp->tmp_bp);

#if defined(DEBUG)
	/* Close debugging file descriptor. */
	if (gp->tracefp != NULL)
		(void)fclose(gp->tracefp);
#endif
#endif
}

/*
 * v_obsolete --
 *	Convert historic arguments into something getopt(3) will like.
 */
static int
v_obsolete(name, argv)
	char *name, *argv[];
{
	size_t len;
	char *p;

	/*
	 * Translate old style arguments into something getopt will like.
	 * Make sure it's not text space memory, because ex modifies the
	 * strings.
	 *	Change "+" into "-c$".
	 *	Change "+<anything else>" into "-c<anything else>".
	 *	Change "-" into "-s"
	 *	The c, T, t and w options take arguments so they can't be
	 *	    special arguments.
	 *
	 * Stop if we find "--" as an argument, the user may want to edit
	 * a file named "+foo".
	 */
	while (*++argv && strcmp(argv[0], "--"))
		if (argv[0][0] == '+') {
			if (argv[0][1] == '\0') {
				MALLOC_NOMSG(NULL, argv[0], char *, 4);
				if (argv[0] == NULL)
					goto nomem;
				(void)strcpy(argv[0], "-c$");
			} else  {
				p = argv[0];
				len = strlen(argv[0]);
				MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
				if (argv[0] == NULL)
					goto nomem;
				argv[0][0] = '-';
				argv[0][1] = 'c';
				(void)strcpy(argv[0] + 2, p + 1);
			}
		} else if (argv[0][0] == '-')
			if (argv[0][1] == '\0') {
				MALLOC_NOMSG(NULL, argv[0], char *, 3);
				if (argv[0] == NULL) {
nomem:					v_estr(name, errno, NULL);
					return (1);
				}
				(void)strcpy(argv[0], "-s");
			} else
				if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
				    argv[0][1] == 't' || argv[0][1] == 'w') &&
				    argv[0][2] == '\0')
					++argv;
	return (0);
}

#ifdef DEBUG
static void
attach(gp)
	GS *gp;
{
	int fd;
	char ch;

	if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
		v_estr(gp->progname, errno, _PATH_TTY);
		return;
	}

	(void)printf("process %lu waiting, enter <CR> to continue: ",
	    (u_long)getpid());
	(void)fflush(stdout);

	do {
		if (read(fd, &ch, 1) != 1) {
			(void)close(fd);
			return;
		}
	} while (ch != '\n' && ch != '\r');
	(void)close(fd);
}
#endif

static void
v_estr(name, eno, msg)
	char *name, *msg;
	int eno;
{
	(void)fprintf(stderr, "%s", name);
	if (msg != NULL)
		(void)fprintf(stderr, ": %s", msg);
	if (eno)
		(void)fprintf(stderr, ": %s", strerror(errno));
	(void)fprintf(stderr, "\n");
}
@


1.8
log
@Remove the \n and tabs from the __COPYRIGHT() strings.
Tweak to use a consistent format.
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $	*/
d26 1
a26 1
__RCSID("$NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $");
@


1.7
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.6 2001/05/01 16:46:11 aymeric Exp $	*/
d16 4
a19 5
__COPYRIGHT(
"@@(#) Copyright (c) 1992, 1993, 1994\n\
	The Regents of the University of California.  All rights reserved.\n\
@@(#) Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
	Keith Bostic.  All rights reserved.\n");
d26 1
a26 1
__RCSID("$NetBSD$");
@


1.7.36.1
log
@Sync with wrstuden-revivesa-base-2.
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $	*/
d16 5
a20 4
__COPYRIGHT("@@(#) Copyright (c) 1992, 1993, 1994\
 The Regents of the University of California.  All rights reserved.\
  Copyright (c) 1992, 1993, 1994, 1995, 1996\
 Keith Bostic.  All rights reserved.");
d27 1
a27 1
__RCSID("$NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $");
@


1.6
log
@shutup most (non-intrusive) gcc warnings
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.5 2001/03/31 11:37:46 aymeric Exp $	*/
d14 1
d16 1
a16 1
static const char copyright[] =
d20 1
a20 1
	Keith Bostic.  All rights reserved.\n";
d24 1
d26 3
@


1.5
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.4 1999/01/08 06:16:54 abs Exp $	*/
d44 1
d46 1
@


1.4
log
@Add Gtags support from shigio@@wafu.netgate.net (PR 5978). Gtags are produced
by global (http://wafu.netgate.net/tama/unix/global.html)
@
text
@d1 1
a1 1
/*	$NetBSD: main.c,v 1.3 1998/01/09 08:06:47 perry Exp $	*/
d23 1
a23 1
static const char sccsid[] = "@@(#)main.c	10.43 (Berkeley) 5/15/96";
d44 1
d68 1
a68 1
	int ch, fd, flagchk, lflag, startup, readonly, rval, silent;
d72 1
a72 2
	char *tag_f, *wsizearg;
	char path[256];
d115 1
a115 1
	lflag = silent = 0;
d123 1
a123 1
	while ((ch = getopt(argc, argv, "c:D:eFGlRrsT:t:vw:")) != -1)
d125 1
a125 1
	while ((ch = getopt(argc, argv, "c:eFGlRrst:vw:")) != -1)
d129 1
a129 1
	while ((ch = getopt(argc, argv, "c:D:eFlRrsT:t:vw:")) != -1)
d131 1
a131 1
	while ((ch = getopt(argc, argv, "c:eFlRrst:vw:")) != -1)
a133 1

d154 1
a154 14
				if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
					v_estr(gp->progname, errno, _PATH_TTY);
					return (1);
				}
		(void)printf("process %lu waiting, enter <CR> to continue: ",
				    (u_long)getpid());
				(void)fflush(stdout);
				do {
					if (read(fd, &ch, 1) != 1) {
						(void)close(fd);
						return (0);
					}
				} while (ch != '\n' && ch != '\r');
				(void)close(fd);
d171 1
a171 1
		case 'G':               /* gtags mode. */
d189 3
d243 1
a243 1
	if (LF_ISSET(SC_EX) && !F_ISSET(gp, G_STDIN_TTY))
d266 2
a267 3
	{ int oargs[4], *oargp = oargs;
	if (readonly)			/* Command-line options. */
		*oargp++ = O_READONLY;
d276 4
d435 1
a435 1
	F_SET(sp, LF_ISSET(SC_EX | SC_VI));
d537 1
d542 1
d601 27
@


1.3
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d68 3
d121 7
d133 2
d184 5
d280 4
@


1.2
log
@getopt returns -1 not EOF
@
text
@d1 2
@


1.1
log
@Initial revision
@
text
@d4 2
d7 1
a7 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d10 2
d15 3
a17 1
	The Regents of the University of California.  All rights reserved.\n";
d21 1
a21 1
static const char sccsid[] = "@@(#)main.c	8.105 (Berkeley) 8/17/94";
d24 1
a24 1
#include <sys/param.h>
a29 2
#include <ctype.h>
#include <err.h>
a32 1
#include <signal.h>
a35 1
#include <termios.h>
d38 3
a40 5
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
d42 2
a43 18
#include "compat.h"
#include <db.h>
#include <regex.h>
#include <pathnames.h>

#include "vi.h"
#include "excmd.h"
#include "../ex/tag.h"

enum rc { NOEXIST, NOPERM, OK };

static enum rc	 exrc_isok __P((SCR *, struct stat *, char *, int, int));
static void	 gs_end __P((GS *));
static GS	*gs_init __P((void));
static void	 obsolete __P((char *[]));
static void	 usage __P((int));

GS *__global_list;			/* GLOBAL: List of screens. */
d45 6
d52 2
a53 1
main(argc, argv)
d59 2
a60 3
	static int reenter;		/* STATIC: Re-entrancy check. */
	struct stat hsb, lsb;
	GS *gp;
d63 25
a87 13
	u_int flags, saved_vi_mode;
	int ch, eval, flagchk, readonly, silent, snapshot;
	char *excmdarg, *myname, *p, *tag_f, *trace_f, *wsizearg;
	char path[MAXPATHLEN];

	/* Stop if indirecting through a NULL pointer. */
	if (reenter++)
		abort();

#ifdef GDBATTACH
	(void)printf("%u waiting...\n", getpid());
	(void)read(0, &eval, 1);
#endif
d89 1
a89 1
	/* Set screen type and mode based on the program name. */
d91 2
a92 6
	if ((myname = strrchr(*argv, '/')) == NULL)
		myname = *argv;
	else
		++myname;
	if (!strcmp(myname, "ex") || !strcmp(myname, "nex"))
		LF_INIT(S_EX);
d94 3
a96 2
		/* View is readonly. */
		if (!strcmp(myname, "view"))
d98 3
a100 1
		LF_INIT(S_VI_CURSES);
a101 1
	saved_vi_mode = S_VI_CURSES;
d104 2
a105 1
	obsolete(argv);
d109 12
a120 4
	excmdarg = tag_f = trace_f = wsizearg = NULL;
	silent = 0;
	snapshot = 1;
	while ((ch = getopt(argc, argv, "c:eFRrsT:t:vw:X:")) != EOF)
d123 38
a160 1
			excmdarg = optarg;
d162 1
d164 2
a165 2
			LF_CLR(S_SCREENS);
			LF_SET(S_EX);
d168 4
a171 1
			snapshot = 0;
d177 2
a178 2
			if (flagchk == 't')
				errx(1,
d180 2
d187 1
d189 6
a194 1
			trace_f = optarg;
d196 1
d198 2
a199 2
			if (flagchk == 'r')
				errx(1,
d201 4
a204 2
			if (flagchk == 't')
				errx(1,
d206 2
d212 2
a213 2
			LF_CLR(S_SCREENS);
			LF_SET(S_VI_CURSES);
a217 8
		case 'X':
			if (!strcmp(optarg, "aw")) {
				LF_CLR(S_SCREENS);
				LF_SET(S_VI_XAW);
				saved_vi_mode = S_VI_XAW;
				break;
			}
			/* FALLTHROUGH */
d220 2
a221 1
			usage(LF_ISSET(S_EX));
a225 7
	/* Silent is only applicable to ex. */
	if (silent && !LF_ISSET(S_EX))
		errx(1, "-s only applicable to ex.");

	/* Build and initialize the GS structure. */
	__global_list = gp = gs_init();

d227 2
a229 2
	 * Vi always reads from the terminal, so fail if it's not a
	 * terminal.
d231 5
a235 1
	if (!F_ISSET(gp, G_STDIN_TTY)) {
a236 6
		if (!LF_ISSET(S_EX)) {
			msgq(NULL, M_ERR,
			    "Vi's standard input must be a terminal");
			goto err;
		}
	}
d245 1
a245 1
	 * Signals not on, no need to block them for queue manipulation.
d247 1
a247 1
	if (screen_init(NULL, &sp, flags)) {
d249 1
a249 1
			CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);
d252 5
a256 2
	sp->saved_vi_mode = saved_vi_mode;
	CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);
d258 6
a263 8
	if (trace_f != NULL) {
#ifdef DEBUG
		if ((gp->tracefp = fopen(trace_f, "w")) == NULL)
			err(1, "%s", trace_f);
		(void)fprintf(gp->tracefp, "\n===\ntrace: open %s\n", trace_f);
#else
		msgq(sp, M_ERR, "-T support not compiled into this version");
#endif
d265 2
a266 2

	if (opts_init(sp))		/* Options initialization. */
a267 8
	if (readonly)			/* Global read-only bit. */
		O_SET(sp, O_READONLY);
	if (silent) {			/* Ex batch mode. */
		O_CLR(sp, O_AUTOPRINT);
		O_CLR(sp, O_PROMPT);
		O_CLR(sp, O_VERBOSE);
		O_CLR(sp, O_WARN);
		F_SET(sp, S_EXSILENT);
a270 3
		errno = 0;
		if (strtol(wsizearg, &p, 10) < 0 || errno || *p)
			errx(1, "illegal window size -- %s.", wsizearg);
d278 8
a285 3
		if (opts_set(sp, NULL, av))
			 msgq(sp, M_ERR,
			     "Unable to set command line window size option");
d288 12
a299 3
	/* Keymaps, special keys, must follow option initializations. */
	if (term_init(sp))
		goto err;
d301 12
a312 4
#ifdef	DIGRAPHS
	if (digraph_init(sp))		/* Digraph initialization. */
		goto err;
#endif
a314 6
	 * Source the system, environment, $HOME and local .exrc values.
	 * Vi historically didn't check $HOME/.exrc if the environment
	 * variable EXINIT was set.  This is all done before the file is
	 * read in, because things in the .exrc information can set, for
	 * example, the recovery directory.
	 *
d316 2
a317 5
	 * While nvi can handle any of the options settings of historic vi,
	 * the converse is not true.  Since users are going to have to have
	 * files and environmental variables that work with both, we use nvi
	 * versions of both the $HOME and local startup files if they exist,
	 * otherwise the historic ones.
d319 11
a329 3
	 * !!!
	 * For a discussion of permissions and when what .exrc files are
	 * read, see the the comment above the exrc_isok() function below.
d331 8
a338 63
	 * !!!
	 * If the user started the historic of vi in $HOME, vi read the user's
	 * .exrc file twice, as $HOME/.exrc and as ./.exrc.  We avoid this, as
	 * it's going to make some commands behave oddly, and I can't imagine
	 * anyone depending on it.
	 */
	if (!silent) {
		switch (exrc_isok(sp, &hsb, _PATH_SYSEXRC, 1, 0)) {
		case NOEXIST:
		case NOPERM:
			break;
		case OK:
			(void)ex_cfile(sp, NULL, _PATH_SYSEXRC, 0);
			break;
		}

		if ((p = getenv("NEXINIT")) != NULL ||
		    (p = getenv("EXINIT")) != NULL)
			if ((p = strdup(p)) == NULL) {
				msgq(sp, M_SYSERR, NULL);
				goto err;
			} else {
				F_SET(sp, S_VLITONLY);
				(void)ex_icmd(sp, NULL, p, strlen(p), 0);
				F_CLR(sp, S_VLITONLY);
				free(p);
			}
		else if ((p = getenv("HOME")) != NULL && *p) {
			(void)snprintf(path,
			    sizeof(path), "%s/%s", p, _PATH_NEXRC);
			switch (exrc_isok(sp, &hsb, path, 0, 1)) {
			case NOEXIST:
				(void)snprintf(path,
				    sizeof(path), "%s/%s", p, _PATH_EXRC);
				if (exrc_isok(sp, &hsb, path, 0, 1) == OK)
					(void)ex_cfile(sp, NULL, path, 0);
				break;
			case NOPERM:
				break;
			case OK:
				(void)ex_cfile(sp, NULL, path, 0);
				break;
			}
		}

		if (O_ISSET(sp, O_EXRC))
			switch (exrc_isok(sp, &lsb, _PATH_NEXRC, 0, 0)) {
			case NOEXIST:
				if (exrc_isok(sp,
				    &lsb, _PATH_EXRC, 0, 0) == OK &&
				    (lsb.st_dev != hsb.st_dev ||
				    lsb.st_ino != hsb.st_ino))
					(void)ex_cfile(sp, NULL, _PATH_EXRC, 0);
				break;
			case NOPERM:
				break;
			case OK:
				if (lsb.st_dev != hsb.st_dev ||
				    lsb.st_ino != hsb.st_ino)
					(void)ex_cfile(sp,
					    NULL, _PATH_NEXRC, 0);
				break;
			}
d341 2
a342 10
	/* List recovery files if -r specified without file arguments. */
	if (flagchk == 'r' && argv[0] == NULL)
		exit(rcv_list(sp));

	/* Set the file snapshot flag. */
	if (snapshot)
		F_SET(gp, G_SNAPSHOT);

	/* Use a tag file if specified. */
	if (tag_f != NULL && ex_tagfirst(sp, tag_f))
d346 3
a348 2
	 * Append any remaining arguments as file names.  Files are
	 * recovery files if -r specified.
d351 10
d362 1
a362 1
		F_SET(sp, S_ARGNOFREE);
d364 1
a364 1
			F_SET(sp, S_ARGRECOVER);
d368 3
a370 2
	 * If the tag option hasn't already created a file, create one.
	 * If no files as arguments, use a temporary file.
d372 11
a382 6
	if (tag_f == NULL) {
		if ((frp = file_add(sp,
		    sp->argv == NULL ? NULL : (CHAR_T *)(sp->argv[0]))) == NULL)
			goto err;
		if (F_ISSET(sp, S_ARGRECOVER))
			F_SET(frp, FR_RECOVER);
d385 8
d396 13
a408 17
	 * If there's an initial command, push it on the command stack.
	 * Historically, it was always an ex command, not vi in vi mode
	 * or ex in ex mode.  So, make it look like an ex command to vi.
	 *
	 * !!!
	 * Historically, all such commands were executed with the last
	 * line of the file as the current line, and not the first, so
	 * set up vi to be at the end of the file.
	 */
	if (excmdarg != NULL)
		if (IN_EX_MODE(sp)) {
			if (term_push(sp, "\n", 1, 0))
				goto err;
			if (term_push(sp, excmdarg, strlen(excmdarg), 0))
				goto err;
		} else if (IN_VI_MODE(sp)) {
			if (term_push(sp, "\n", 1, 0))
d410 3
a412 30
			if (term_push(sp, excmdarg, strlen(excmdarg), 0))
				goto err;
			if (term_push(sp, ":", 1, 0))
				goto err;
			if (file_lline(sp, sp->ep, &sp->frp->lno))
				goto err;
			F_SET(sp->frp, FR_CURSORSET);
		}

	/* Set up signals. */
	if (sig_init(sp))
		goto err;

	for (;;) {
		/* Ignore errors -- other screens may succeed. */
		(void)sp->s_edit(sp, sp->ep);

		/*
		 * Edit the next screen on the display queue, or, move
		 * a screen from the hidden queue to the display queue.
		 */
		if ((sp = __global_list->dq.cqh_first) ==
		    (void *)&__global_list->dq)
			if ((sp = __global_list->hq.cqh_first) !=
			    (void *)&__global_list->hq) {
				SIGBLOCK(__global_list);
				CIRCLEQ_REMOVE(&sp->gp->hq, sp, q);
				CIRCLEQ_INSERT_TAIL(&sp->gp->dq, sp, q);
				SIGUNBLOCK(__global_list);
			} else
d414 1
a414 20

		/*
		 * The screen type may have changed -- reinitialize the
		 * functions in case it has.
		 */
		switch (F_ISSET(sp, S_SCREENS)) {
		case S_EX:
			if (sex_screen_init(sp))
				goto err;
			break;
		case S_VI_CURSES:
			if (svi_screen_init(sp))
				goto err;
			break;
		case S_VI_XAW:
			if (xaw_screen_init(sp))
				goto err;
			break;
		default:
			abort();
d418 3
a420 3
	eval = 0;
	if (0)
err:		eval = 1;
d423 2
a424 2
	 * NOTE: sp may be GONE when the screen returns, so only
	 * the gp can be trusted.
d426 3
a428 1
	gs_end(gp);
d430 8
a437 1
	exit(eval);
d441 4
a444 2
 * gs_init --
 *	Build and initialize the GS structure.
d446 3
a448 2
static GS *
gs_init()
d450 2
a451 2
	GS *gp;
	int fd;
d453 9
a461 3
	CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
	if (gp == NULL)
		err(1, NULL);
d463 3
a465 7
	/*
	 * !!!
	 * Signals not on, no need to block them for queue manipulation.
	 */
	CIRCLEQ_INIT(&gp->dq);
	CIRCLEQ_INIT(&gp->hq);
	LIST_INIT(&gp->msgq);
d467 12
a478 8
	/* Structures shared by screens so stored in the GS structure. */
	CALLOC_NOMSG(NULL, gp->tty, IBUF *, 1, sizeof(IBUF));
	if (gp->tty == NULL)
		err(1, NULL);

	CIRCLEQ_INIT(&gp->dcb_store.textq);
	LIST_INIT(&gp->cutq);
	LIST_INIT(&gp->seqq);
d480 3
a482 3
	/* Set a flag if we're reading from the tty. */
	if (isatty(STDIN_FILENO))
		F_SET(gp, G_STDIN_TTY);
d484 2
a485 21
	/*
	 * Set the G_STDIN_TTY flag.  It's purpose is to avoid setting and
	 * resetting the tty if the input isn't from there.
	 *
	 * Set the G_TERMIOS_SET flag.  It's purpose is to avoid using the
	 * original_termios information (mostly special character values)
	 * if it's not valid.  We expect that if we've lost our controlling
	 * terminal that the open() (but not the tcgetattr()) will fail.
	 */
	if (F_ISSET(gp, G_STDIN_TTY)) {
		if (tcgetattr(STDIN_FILENO, &gp->original_termios) == -1)
			err(1, "tcgetattr");
		F_SET(gp, G_TERMIOS_SET);
	} else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
		if (tcgetattr(fd, &gp->original_termios) == -1)
			err(1, "tcgetattr");
		F_SET(gp, G_TERMIOS_SET);
		(void)close(fd);
	}
	return (gp);
}
d487 2
d490 1
a490 13
/*
 * gs_end --
 *	End the GS structure.
 */
static void
gs_end(gp)
	GS *gp;
{
	MSG *mp;
	SCR *sp;
	char *tty;

	/* Default buffer storage. */
d493 3
a495 6
	/* Reset anything that needs resetting. */
	if (gp->flags & G_SETMODE)			/* O_MESG */
		if ((tty = ttyname(STDERR_FILENO)) == NULL)
			warn("ttyname");
		else if (chmod(tty, gp->origmode) < 0)
			warn("%s", tty);
d501 15
a515 17
	/* If there are any remaining screens, flush their messages. */
	for (sp = __global_list->dq.cqh_first;
	    sp != (void *)&__global_list->dq; sp = sp->q.cqe_next)
		for (mp = sp->msgq.lh_first;
		    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
			(void)fprintf(stderr,
			    "%.*s.\n", (int)mp->len, mp->mbuf);
	for (sp = __global_list->hq.cqh_first;
	    sp != (void *)&__global_list->hq; sp = sp->q.cqe_next)
		for (mp = sp->msgq.lh_first;
		    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
			(void)fprintf(stderr,
			    "%.*s.\n", (int)mp->len, mp->mbuf);
	/* Flush messages on the global queue. */
	for (mp = gp->msgq.lh_first;
	    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
		(void)fprintf(stderr, "%.*s.\n", (int)mp->len, mp->mbuf);
d517 9
a525 4
	/*
	 * DON'T FREE THE GLOBAL STRUCTURE -- WE DIDN'T TURN
	 * OFF SIGNALS/TIMERS, SO IT MAY STILL BE REFERENCED.
	 */
d529 2
a530 34
 * exrc_isok --
 *	Check a .exrc file for source-ability.
 *
 * !!!
 * Historically, vi read the $HOME and local .exrc files if they were owned
 * by the user's real ID, or the "sourceany" option was set, regardless of
 * any other considerations.  We no longer support the sourceany option as
 * it's a security problem of mammoth proportions.  We require the system
 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
 * user's effective ID (or that the user's effective ID be root) and the
 * local .exrc files to be owned by the user's effective ID.  In all cases,
 * the file cannot be writeable by anyone other than its owner.
 * 
 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
 * it notes that System V release 3.2 and later has an option "[no]exrc".
 * The behavior is that local .exrc files are read only if the exrc option
 * is set.  The default for the exrc option was off, so, by default, local
 * .exrc files were not read.  The problem this was intended to solve was
 * that System V permitted users to give away files, so there's no possible
 * ownership or writeability test to ensure that the file is safe.
 * 
 * POSIX 1003.2-1992 standardized exrc as an option.  It required the exrc
 * option to be off by default, thus local .exrc files are not to be read
 * by default.  The Rationale noted (incorrectly) that this was a change
 * to historic practice, but correctly noted that a default of off improves
 * system security.  POSIX also required that vi check the effective user
 * ID instead of the real user ID, which is why we've switched from historic
 * practice.
 * 
 * We initialize the exrc variable to off.  If it's turned on by the system
 * or $HOME .exrc files, and the local .exrc file passes the ownership and
 * writeability tests, then we read it.  This breaks historic 4BSD practice,
 * but it gives us a measure of security on systems where users can give away
 * files.
d532 3
a534 41
static enum rc
exrc_isok(sp, sbp, path, rootown, rootid)
	SCR *sp;
	struct stat *sbp;
	char *path;
	int rootown, rootid;
{
	uid_t euid;
	char *emsg, buf[MAXPATHLEN];

	/* Check for the file's existence. */
	if (stat(path, sbp))
		return (NOEXIST);

	/* Check ownership permissions. */
	euid = geteuid();
	if (!(rootown && sbp->st_uid == 0) && 
	    !(rootid && euid == 0) && sbp->st_uid != euid) {
		emsg = rootown ?
		    "not owned by you or root" : "not owned by you";
		goto denied;
	}

	/* Check writeability. */
	if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
		emsg = "writeable by a user other than the owner";
denied:		if (strchr(path, '/') == NULL &&
		    getcwd(buf, sizeof(buf)) != NULL)
			msgq(sp, M_ERR,
			    "%s/%s: not sourced: %s", buf, path, emsg);
		else
			msgq(sp, M_ERR,
			    "%s: not sourced: %s", path, emsg);
		return (NOPERM);
	}
	return (OK);
}

static void
obsolete(argv)
	char *argv[];
d541 1
a541 1
	 * Make sure it's not text space memory, because ex changes the
d546 5
d552 1
a552 1
	while (*++argv)
d557 1
a557 1
					err(1, NULL);
d564 1
a564 1
					err(1, NULL);
d569 14
a582 6
		} else if (argv[0][0] == '-' && argv[0][1] == '\0') {
			MALLOC_NOMSG(NULL, argv[0], char *, 3);
			if (argv[0] == NULL)
				err(1, NULL);
			(void)strcpy(argv[0], "-s");
		}
d586 3
a588 2
usage(is_ex)
	int is_ex;
d590 6
a595 7
#define	EX_USAGE \
    "ex [-eFRrsv] [-c command] [-t tag] [-w size] [files ...]"
#define	VI_USAGE \
    "vi [-eFRrv] [-c command] [-t tag] [-w size] [files ...]"

	(void)fprintf(stderr, "usage: %s\n", is_ex ? EX_USAGE : VI_USAGE);
	exit(1);
@


1.1.1.1
log
@new public version of nvi
@
text
@@


1.1.1.2
log
@new public version of nvi
@
text
@d35 1
a35 1
static char copyright[] =
d41 1
a41 1
static char sccsid[] = "@@(#)main.c	8.106 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file main.c was added on branch netbsd-1-0 on 1994-08-17 19:25:10 +0000
@
text
@d1 720
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 720
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char copyright[] =
"@@(#) Copyright (c) 1992, 1993, 1994\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@@(#)main.c	8.106 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include "compat.h"
#include <db.h>
#include <regex.h>
#include <pathnames.h>

#include "vi.h"
#include "excmd.h"
#include "../ex/tag.h"

enum rc { NOEXIST, NOPERM, OK };

static enum rc	 exrc_isok __P((SCR *, struct stat *, char *, int, int));
static void	 gs_end __P((GS *));
static GS	*gs_init __P((void));
static void	 obsolete __P((char *[]));
static void	 usage __P((int));

GS *__global_list;			/* GLOBAL: List of screens. */

int
main(argc, argv)
	int argc;
	char *argv[];
{
	extern int optind;
	extern char *optarg;
	static int reenter;		/* STATIC: Re-entrancy check. */
	struct stat hsb, lsb;
	GS *gp;
	FREF *frp;
	SCR *sp;
	u_int flags, saved_vi_mode;
	int ch, eval, flagchk, readonly, silent, snapshot;
	char *excmdarg, *myname, *p, *tag_f, *trace_f, *wsizearg;
	char path[MAXPATHLEN];

	/* Stop if indirecting through a NULL pointer. */
	if (reenter++)
		abort();

#ifdef GDBATTACH
	(void)printf("%u waiting...\n", getpid());
	(void)read(0, &eval, 1);
#endif

	/* Set screen type and mode based on the program name. */
	readonly = 0;
	if ((myname = strrchr(*argv, '/')) == NULL)
		myname = *argv;
	else
		++myname;
	if (!strcmp(myname, "ex") || !strcmp(myname, "nex"))
		LF_INIT(S_EX);
	else {
		/* View is readonly. */
		if (!strcmp(myname, "view"))
			readonly = 1;
		LF_INIT(S_VI_CURSES);
	}
	saved_vi_mode = S_VI_CURSES;

	/* Convert old-style arguments into new-style ones. */
	obsolete(argv);

	/* Parse the arguments. */
	flagchk = '\0';
	excmdarg = tag_f = trace_f = wsizearg = NULL;
	silent = 0;
	snapshot = 1;
	while ((ch = getopt(argc, argv, "c:eFRrsT:t:vw:X:")) != EOF)
		switch (ch) {
		case 'c':		/* Run the command. */
			excmdarg = optarg;
			break;
		case 'e':		/* Ex mode. */
			LF_CLR(S_SCREENS);
			LF_SET(S_EX);
			break;
		case 'F':		/* No snapshot. */
			snapshot = 0;
			break;
		case 'R':		/* Readonly. */
			readonly = 1;
			break;
		case 'r':		/* Recover. */
			if (flagchk == 't')
				errx(1,
				    "only one of -r and -t may be specified.");
			flagchk = 'r';
			break;
		case 's':
			silent = 1;
			break;
		case 'T':		/* Trace. */
			trace_f = optarg;
			break;
		case 't':		/* Tag. */
			if (flagchk == 'r')
				errx(1,
				    "only one of -r and -t may be specified.");
			if (flagchk == 't')
				errx(1,
				    "only one tag file may be specified.");
			flagchk = 't';
			tag_f = optarg;
			break;
		case 'v':		/* Vi mode. */
			LF_CLR(S_SCREENS);
			LF_SET(S_VI_CURSES);
			break;
		case 'w':
			wsizearg = optarg;
			break;
		case 'X':
			if (!strcmp(optarg, "aw")) {
				LF_CLR(S_SCREENS);
				LF_SET(S_VI_XAW);
				saved_vi_mode = S_VI_XAW;
				break;
			}
			/* FALLTHROUGH */
		case '?':
		default:
			usage(LF_ISSET(S_EX));
		}
	argc -= optind;
	argv += optind;

	/* Silent is only applicable to ex. */
	if (silent && !LF_ISSET(S_EX))
		errx(1, "-s only applicable to ex.");

	/* Build and initialize the GS structure. */
	__global_list = gp = gs_init();

	/*
	 * If not reading from a terminal, it's like -s was specified.
	 * Vi always reads from the terminal, so fail if it's not a
	 * terminal.
	 */
	if (!F_ISSET(gp, G_STDIN_TTY)) {
		silent = 1;
		if (!LF_ISSET(S_EX)) {
			msgq(NULL, M_ERR,
			    "Vi's standard input must be a terminal");
			goto err;
		}
	}

	/*
	 * Build and initialize the first/current screen.  This is a bit
	 * tricky.  If an error is returned, we may or may not have a
	 * screen structure.  If we have a screen structure, put it on a
	 * display queue so that the error messages get displayed.
	 *
	 * !!!
	 * Signals not on, no need to block them for queue manipulation.
	 */
	if (screen_init(NULL, &sp, flags)) {
		if (sp != NULL)
			CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);
		goto err;
	}
	sp->saved_vi_mode = saved_vi_mode;
	CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);

	if (trace_f != NULL) {
#ifdef DEBUG
		if ((gp->tracefp = fopen(trace_f, "w")) == NULL)
			err(1, "%s", trace_f);
		(void)fprintf(gp->tracefp, "\n===\ntrace: open %s\n", trace_f);
#else
		msgq(sp, M_ERR, "-T support not compiled into this version");
#endif
	}

	if (opts_init(sp))		/* Options initialization. */
		goto err;
	if (readonly)			/* Global read-only bit. */
		O_SET(sp, O_READONLY);
	if (silent) {			/* Ex batch mode. */
		O_CLR(sp, O_AUTOPRINT);
		O_CLR(sp, O_PROMPT);
		O_CLR(sp, O_VERBOSE);
		O_CLR(sp, O_WARN);
		F_SET(sp, S_EXSILENT);
	}
	if (wsizearg != NULL) {
		ARGS *av[2], a, b;
		errno = 0;
		if (strtol(wsizearg, &p, 10) < 0 || errno || *p)
			errx(1, "illegal window size -- %s.", wsizearg);
		(void)snprintf(path, sizeof(path), "window=%s", wsizearg);
		a.bp = (CHAR_T *)path;
		a.len = strlen(path);
		b.bp = NULL;
		b.len = 0;
		av[0] = &a;
		av[1] = &b;
		if (opts_set(sp, NULL, av))
			 msgq(sp, M_ERR,
			     "Unable to set command line window size option");
	}

	/* Keymaps, special keys, must follow option initializations. */
	if (term_init(sp))
		goto err;

#ifdef	DIGRAPHS
	if (digraph_init(sp))		/* Digraph initialization. */
		goto err;
#endif

	/*
	 * Source the system, environment, $HOME and local .exrc values.
	 * Vi historically didn't check $HOME/.exrc if the environment
	 * variable EXINIT was set.  This is all done before the file is
	 * read in, because things in the .exrc information can set, for
	 * example, the recovery directory.
	 *
	 * !!!
	 * While nvi can handle any of the options settings of historic vi,
	 * the converse is not true.  Since users are going to have to have
	 * files and environmental variables that work with both, we use nvi
	 * versions of both the $HOME and local startup files if they exist,
	 * otherwise the historic ones.
	 *
	 * !!!
	 * For a discussion of permissions and when what .exrc files are
	 * read, see the the comment above the exrc_isok() function below.
	 *
	 * !!!
	 * If the user started the historic of vi in $HOME, vi read the user's
	 * .exrc file twice, as $HOME/.exrc and as ./.exrc.  We avoid this, as
	 * it's going to make some commands behave oddly, and I can't imagine
	 * anyone depending on it.
	 */
	if (!silent) {
		switch (exrc_isok(sp, &hsb, _PATH_SYSEXRC, 1, 0)) {
		case NOEXIST:
		case NOPERM:
			break;
		case OK:
			(void)ex_cfile(sp, NULL, _PATH_SYSEXRC, 0);
			break;
		}

		if ((p = getenv("NEXINIT")) != NULL ||
		    (p = getenv("EXINIT")) != NULL)
			if ((p = strdup(p)) == NULL) {
				msgq(sp, M_SYSERR, NULL);
				goto err;
			} else {
				F_SET(sp, S_VLITONLY);
				(void)ex_icmd(sp, NULL, p, strlen(p), 0);
				F_CLR(sp, S_VLITONLY);
				free(p);
			}
		else if ((p = getenv("HOME")) != NULL && *p) {
			(void)snprintf(path,
			    sizeof(path), "%s/%s", p, _PATH_NEXRC);
			switch (exrc_isok(sp, &hsb, path, 0, 1)) {
			case NOEXIST:
				(void)snprintf(path,
				    sizeof(path), "%s/%s", p, _PATH_EXRC);
				if (exrc_isok(sp, &hsb, path, 0, 1) == OK)
					(void)ex_cfile(sp, NULL, path, 0);
				break;
			case NOPERM:
				break;
			case OK:
				(void)ex_cfile(sp, NULL, path, 0);
				break;
			}
		}

		if (O_ISSET(sp, O_EXRC))
			switch (exrc_isok(sp, &lsb, _PATH_NEXRC, 0, 0)) {
			case NOEXIST:
				if (exrc_isok(sp,
				    &lsb, _PATH_EXRC, 0, 0) == OK &&
				    (lsb.st_dev != hsb.st_dev ||
				    lsb.st_ino != hsb.st_ino))
					(void)ex_cfile(sp, NULL, _PATH_EXRC, 0);
				break;
			case NOPERM:
				break;
			case OK:
				if (lsb.st_dev != hsb.st_dev ||
				    lsb.st_ino != hsb.st_ino)
					(void)ex_cfile(sp,
					    NULL, _PATH_NEXRC, 0);
				break;
			}
	}

	/* List recovery files if -r specified without file arguments. */
	if (flagchk == 'r' && argv[0] == NULL)
		exit(rcv_list(sp));

	/* Set the file snapshot flag. */
	if (snapshot)
		F_SET(gp, G_SNAPSHOT);

	/* Use a tag file if specified. */
	if (tag_f != NULL && ex_tagfirst(sp, tag_f))
		goto err;

	/*
	 * Append any remaining arguments as file names.  Files are
	 * recovery files if -r specified.
	 */
	if (*argv != NULL) {
		sp->argv = sp->cargv = argv;
		F_SET(sp, S_ARGNOFREE);
		if (flagchk == 'r')
			F_SET(sp, S_ARGRECOVER);
	}

	/*
	 * If the tag option hasn't already created a file, create one.
	 * If no files as arguments, use a temporary file.
	 */
	if (tag_f == NULL) {
		if ((frp = file_add(sp,
		    sp->argv == NULL ? NULL : (CHAR_T *)(sp->argv[0]))) == NULL)
			goto err;
		if (F_ISSET(sp, S_ARGRECOVER))
			F_SET(frp, FR_RECOVER);
		if (file_init(sp, frp, NULL, 0))
			goto err;
	}

	/*
	 * If there's an initial command, push it on the command stack.
	 * Historically, it was always an ex command, not vi in vi mode
	 * or ex in ex mode.  So, make it look like an ex command to vi.
	 *
	 * !!!
	 * Historically, all such commands were executed with the last
	 * line of the file as the current line, and not the first, so
	 * set up vi to be at the end of the file.
	 */
	if (excmdarg != NULL)
		if (IN_EX_MODE(sp)) {
			if (term_push(sp, "\n", 1, 0))
				goto err;
			if (term_push(sp, excmdarg, strlen(excmdarg), 0))
				goto err;
		} else if (IN_VI_MODE(sp)) {
			if (term_push(sp, "\n", 1, 0))
				goto err;
			if (term_push(sp, excmdarg, strlen(excmdarg), 0))
				goto err;
			if (term_push(sp, ":", 1, 0))
				goto err;
			if (file_lline(sp, sp->ep, &sp->frp->lno))
				goto err;
			F_SET(sp->frp, FR_CURSORSET);
		}

	/* Set up signals. */
	if (sig_init(sp))
		goto err;

	for (;;) {
		/* Ignore errors -- other screens may succeed. */
		(void)sp->s_edit(sp, sp->ep);

		/*
		 * Edit the next screen on the display queue, or, move
		 * a screen from the hidden queue to the display queue.
		 */
		if ((sp = __global_list->dq.cqh_first) ==
		    (void *)&__global_list->dq)
			if ((sp = __global_list->hq.cqh_first) !=
			    (void *)&__global_list->hq) {
				SIGBLOCK(__global_list);
				CIRCLEQ_REMOVE(&sp->gp->hq, sp, q);
				CIRCLEQ_INSERT_TAIL(&sp->gp->dq, sp, q);
				SIGUNBLOCK(__global_list);
			} else
				break;

		/*
		 * The screen type may have changed -- reinitialize the
		 * functions in case it has.
		 */
		switch (F_ISSET(sp, S_SCREENS)) {
		case S_EX:
			if (sex_screen_init(sp))
				goto err;
			break;
		case S_VI_CURSES:
			if (svi_screen_init(sp))
				goto err;
			break;
		case S_VI_XAW:
			if (xaw_screen_init(sp))
				goto err;
			break;
		default:
			abort();
		}
	}

	eval = 0;
	if (0)
err:		eval = 1;

	/*
	 * NOTE: sp may be GONE when the screen returns, so only
	 * the gp can be trusted.
	 */
	gs_end(gp);

	exit(eval);
}

/*
 * gs_init --
 *	Build and initialize the GS structure.
 */
static GS *
gs_init()
{
	GS *gp;
	int fd;

	CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
	if (gp == NULL)
		err(1, NULL);

	/*
	 * !!!
	 * Signals not on, no need to block them for queue manipulation.
	 */
	CIRCLEQ_INIT(&gp->dq);
	CIRCLEQ_INIT(&gp->hq);
	LIST_INIT(&gp->msgq);

	/* Structures shared by screens so stored in the GS structure. */
	CALLOC_NOMSG(NULL, gp->tty, IBUF *, 1, sizeof(IBUF));
	if (gp->tty == NULL)
		err(1, NULL);

	CIRCLEQ_INIT(&gp->dcb_store.textq);
	LIST_INIT(&gp->cutq);
	LIST_INIT(&gp->seqq);

	/* Set a flag if we're reading from the tty. */
	if (isatty(STDIN_FILENO))
		F_SET(gp, G_STDIN_TTY);

	/*
	 * Set the G_STDIN_TTY flag.  It's purpose is to avoid setting and
	 * resetting the tty if the input isn't from there.
	 *
	 * Set the G_TERMIOS_SET flag.  It's purpose is to avoid using the
	 * original_termios information (mostly special character values)
	 * if it's not valid.  We expect that if we've lost our controlling
	 * terminal that the open() (but not the tcgetattr()) will fail.
	 */
	if (F_ISSET(gp, G_STDIN_TTY)) {
		if (tcgetattr(STDIN_FILENO, &gp->original_termios) == -1)
			err(1, "tcgetattr");
		F_SET(gp, G_TERMIOS_SET);
	} else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
		if (tcgetattr(fd, &gp->original_termios) == -1)
			err(1, "tcgetattr");
		F_SET(gp, G_TERMIOS_SET);
		(void)close(fd);
	}
	return (gp);
}


/*
 * gs_end --
 *	End the GS structure.
 */
static void
gs_end(gp)
	GS *gp;
{
	MSG *mp;
	SCR *sp;
	char *tty;

	/* Default buffer storage. */
	(void)text_lfree(&gp->dcb_store.textq);

	/* Reset anything that needs resetting. */
	if (gp->flags & G_SETMODE)			/* O_MESG */
		if ((tty = ttyname(STDERR_FILENO)) == NULL)
			warn("ttyname");
		else if (chmod(tty, gp->origmode) < 0)
			warn("%s", tty);

	/* Ring the bell if scheduled. */
	if (F_ISSET(gp, G_BELLSCHED))
		(void)fprintf(stderr, "\07");		/* \a */

	/* If there are any remaining screens, flush their messages. */
	for (sp = __global_list->dq.cqh_first;
	    sp != (void *)&__global_list->dq; sp = sp->q.cqe_next)
		for (mp = sp->msgq.lh_first;
		    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
			(void)fprintf(stderr,
			    "%.*s.\n", (int)mp->len, mp->mbuf);
	for (sp = __global_list->hq.cqh_first;
	    sp != (void *)&__global_list->hq; sp = sp->q.cqe_next)
		for (mp = sp->msgq.lh_first;
		    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
			(void)fprintf(stderr,
			    "%.*s.\n", (int)mp->len, mp->mbuf);
	/* Flush messages on the global queue. */
	for (mp = gp->msgq.lh_first;
	    mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
		(void)fprintf(stderr, "%.*s.\n", (int)mp->len, mp->mbuf);

	/*
	 * DON'T FREE THE GLOBAL STRUCTURE -- WE DIDN'T TURN
	 * OFF SIGNALS/TIMERS, SO IT MAY STILL BE REFERENCED.
	 */
}

/*
 * exrc_isok --
 *	Check a .exrc file for source-ability.
 *
 * !!!
 * Historically, vi read the $HOME and local .exrc files if they were owned
 * by the user's real ID, or the "sourceany" option was set, regardless of
 * any other considerations.  We no longer support the sourceany option as
 * it's a security problem of mammoth proportions.  We require the system
 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
 * user's effective ID (or that the user's effective ID be root) and the
 * local .exrc files to be owned by the user's effective ID.  In all cases,
 * the file cannot be writeable by anyone other than its owner.
 * 
 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
 * it notes that System V release 3.2 and later has an option "[no]exrc".
 * The behavior is that local .exrc files are read only if the exrc option
 * is set.  The default for the exrc option was off, so, by default, local
 * .exrc files were not read.  The problem this was intended to solve was
 * that System V permitted users to give away files, so there's no possible
 * ownership or writeability test to ensure that the file is safe.
 * 
 * POSIX 1003.2-1992 standardized exrc as an option.  It required the exrc
 * option to be off by default, thus local .exrc files are not to be read
 * by default.  The Rationale noted (incorrectly) that this was a change
 * to historic practice, but correctly noted that a default of off improves
 * system security.  POSIX also required that vi check the effective user
 * ID instead of the real user ID, which is why we've switched from historic
 * practice.
 * 
 * We initialize the exrc variable to off.  If it's turned on by the system
 * or $HOME .exrc files, and the local .exrc file passes the ownership and
 * writeability tests, then we read it.  This breaks historic 4BSD practice,
 * but it gives us a measure of security on systems where users can give away
 * files.
 */
static enum rc
exrc_isok(sp, sbp, path, rootown, rootid)
	SCR *sp;
	struct stat *sbp;
	char *path;
	int rootown, rootid;
{
	uid_t euid;
	char *emsg, buf[MAXPATHLEN];

	/* Check for the file's existence. */
	if (stat(path, sbp))
		return (NOEXIST);

	/* Check ownership permissions. */
	euid = geteuid();
	if (!(rootown && sbp->st_uid == 0) && 
	    !(rootid && euid == 0) && sbp->st_uid != euid) {
		emsg = rootown ?
		    "not owned by you or root" : "not owned by you";
		goto denied;
	}

	/* Check writeability. */
	if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
		emsg = "writeable by a user other than the owner";
denied:		if (strchr(path, '/') == NULL &&
		    getcwd(buf, sizeof(buf)) != NULL)
			msgq(sp, M_ERR,
			    "%s/%s: not sourced: %s", buf, path, emsg);
		else
			msgq(sp, M_ERR,
			    "%s: not sourced: %s", path, emsg);
		return (NOPERM);
	}
	return (OK);
}

static void
obsolete(argv)
	char *argv[];
{
	size_t len;
	char *p;

	/*
	 * Translate old style arguments into something getopt will like.
	 * Make sure it's not text space memory, because ex changes the
	 * strings.
	 *	Change "+" into "-c$".
	 *	Change "+<anything else>" into "-c<anything else>".
	 *	Change "-" into "-s"
	 */
	while (*++argv)
		if (argv[0][0] == '+') {
			if (argv[0][1] == '\0') {
				MALLOC_NOMSG(NULL, argv[0], char *, 4);
				if (argv[0] == NULL)
					err(1, NULL);
				(void)strcpy(argv[0], "-c$");
			} else  {
				p = argv[0];
				len = strlen(argv[0]);
				MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
				if (argv[0] == NULL)
					err(1, NULL);
				argv[0][0] = '-';
				argv[0][1] = 'c';
				(void)strcpy(argv[0] + 2, p + 1);
			}
		} else if (argv[0][0] == '-' && argv[0][1] == '\0') {
			MALLOC_NOMSG(NULL, argv[0], char *, 3);
			if (argv[0] == NULL)
				err(1, NULL);
			(void)strcpy(argv[0], "-s");
		}
}

static void
usage(is_ex)
	int is_ex;
{
#define	EX_USAGE \
    "ex [-eFRrsv] [-c command] [-t tag] [-w size] [files ...]"
#define	VI_USAGE \
    "vi [-eFRrv] [-c command] [-t tag] [-w size] [files ...]"

	(void)fprintf(stderr, "usage: %s\n", is_ex ? EX_USAGE : VI_USAGE);
	exit(1);
}
@


1.1.1.3
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char copyright[] =
d37 1
a37 3
	The Regents of the University of California.  All rights reserved.\n\
@@(#) Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
	Keith Bostic.  All rights reserved.\n";
d41 1
a41 1
static const char sccsid[] = "@@(#)main.c	10.43 (Berkeley) 5/15/96";
d44 1
a44 1
#include <sys/types.h>
d50 2
d55 1
d59 1
d62 22
a83 3
#include "common.h"
#include "../vi/vi.h"
#include "pathnames.h"
d85 1
a85 2
static void	 v_estr __P((char *, int, char *));
static int	 v_obsolete __P((char *, char *[]));
a86 6
/*
 * editor --
 *	Main editor routine.
 *
 * PUBLIC: int editor __P((GS *, int, char *[]));
 */
d88 1
a88 2
editor(gp, argc, argv)
	GS *gp;
d94 3
a96 2
	const char *p;
	EVENT ev;
d99 13
a111 12
	size_t len;
	u_int flags;
	int ch, fd, flagchk, lflag, startup, readonly, rval, silent;
	char *tag_f, *wsizearg;
	char path[256];

	/* Initialize the busy routine, if not defined by the screen. */
	if (gp->scr_busy == NULL)
		gp->scr_busy = vs_busy;
	/* Initialize the message routine, if not defined by the screen. */
	if (gp->scr_msg == NULL)
		gp->scr_msg = vs_msg;
d113 1
a113 14
	/* Common global structure initialization. */
	CIRCLEQ_INIT(&gp->dq);
	CIRCLEQ_INIT(&gp->hq);
	LIST_INIT(&gp->ecq);
	LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q);
	gp->noprint = DEFAULT_NOPRINT;

	/* Structures shared by screens so stored in the GS structure. */
	CIRCLEQ_INIT(&gp->frefq);
	CIRCLEQ_INIT(&gp->dcb_store.textq);
	LIST_INIT(&gp->cutq);
	LIST_INIT(&gp->seqq);

	/* Set initial screen type and mode based on the program name. */
d115 6
a120 2
	if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
		LF_INIT(SC_EX);
d122 2
a123 3
		/* Nview, view are readonly. */
		if (!strcmp(gp->progname, "nview") ||
		    !strcmp(gp->progname, "view"))
d125 1
a125 3
		
		/* Vi is the default. */
		LF_INIT(SC_VI);
d127 1
d130 1
a130 2
	if (v_obsolete(gp->progname, argv))
		return (1);
d134 4
a137 12
	tag_f = wsizearg = NULL;
	lflag = silent = 0;
	startup = 1;

	/* Set the file snapshot flag. */
	F_SET(gp, G_SNAPSHOT);

#ifdef DEBUG
	while ((ch = getopt(argc, argv, "c:D:eFlRrsT:t:vw:")) != EOF)
#else
	while ((ch = getopt(argc, argv, "c:eFlRrst:vw:")) != EOF)
#endif
d140 1
a140 38
			/*
			 * XXX
			 * We should support multiple -c options.
			 */
			if (gp->c_option != NULL) {
				v_estr(gp->progname, 0,
				    "only one -c command may be specified.");
				return (1);
			}
			gp->c_option = optarg;
			break;
#ifdef DEBUG
		case 'D':
			switch (optarg[0]) {
			case 's':
				startup = 0;
				break;
			case 'w':
				if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
					v_estr(gp->progname, errno, _PATH_TTY);
					return (1);
				}
		(void)printf("process %lu waiting, enter <CR> to continue: ",
				    (u_long)getpid());
				(void)fflush(stdout);
				do {
					if (read(fd, &ch, 1) != 1) {
						(void)close(fd);
						return (0);
					}
				} while (ch != '\n' && ch != '\r');
				(void)close(fd);
				break;
			default:
				v_estr(gp->progname, 0,
				    "usage: -D requires s or w argument.");
				return (1);
			}
a141 1
#endif
d143 2
a144 2
			LF_CLR(SC_VI);
			LF_SET(SC_EX);
d147 1
a147 4
			F_CLR(gp, G_SNAPSHOT);
			break;
		case 'l':		/* Set lisp, showmatch options. */
			lflag = 1;
d153 2
a154 2
			if (flagchk == 't') {
				v_estr(gp->progname, 0,
a155 2
				return (1);
			}
a160 1
#ifdef DEBUG
d162 1
a162 6
			if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
				v_estr(gp->progname, errno, optarg);
				goto err;
			}
			(void)fprintf(gp->tracefp,
			    "\n===\ntrace: open %s\n", optarg);
a163 1
#endif
d165 2
a166 2
			if (flagchk == 'r') {
				v_estr(gp->progname, 0,
d168 2
a169 4
				return (1);
			}
			if (flagchk == 't') {
				v_estr(gp->progname, 0,
a170 2
				return (1);
			}
d175 2
a176 2
			LF_CLR(SC_EX);
			LF_SET(SC_VI);
d181 8
d191 1
a191 2
			(void)gp->scr_usage();
			return (1);
d196 7
a203 2
	 * -s option is only meaningful to ex.
	 *
d205 2
d208 7
a214 3
	if (silent && !LF_ISSET(SC_EX)) {
		v_estr(gp->progname, 0, "-s option is only applicable to ex.");
		goto err;
a215 2
	if (LF_ISSET(SC_EX) && !F_ISSET(gp, G_STDIN_TTY))
		silent = 1;
d224 1
a224 1
	 * Everything we do until we go interactive is done in ex mode.
d226 1
a226 1
	if (screen_init(gp, NULL, &sp)) {
d228 1
a228 1
			CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
d231 2
a232 2
	F_SET(sp, SC_EX);
	CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
d234 9
a242 2
	if (v_key_init(sp))		/* Special key initialization. */
		goto err;
d244 1
a244 9
	{ int oargs[4], *oargp = oargs;
	if (readonly)			/* Command-line options. */
		*oargp++ = O_READONLY;
	if (lflag) {
		*oargp++ = O_LISP;
		*oargp++ = O_SHOWMATCH;
	}
	*oargp = -1;			/* Options initialization. */
	if (opts_init(sp, oargs))
d246 8
d257 3
d267 3
a269 8
		(void)opts_set(sp, av, NULL);
	}
	if (silent) {			/* Ex batch mode option values. */
		O_CLR(sp, O_AUTOPRINT);
		O_CLR(sp, O_PROMPT);
		O_CLR(sp, O_VERBOSE);
		O_CLR(sp, O_WARN);
		F_SET(sp, SC_EX_SILENT);
d272 8
a279 2
	sp->rows = O_VAL(sp, O_LINES);	/* Make ex formatting work. */
	sp->cols = O_VAL(sp, O_COLUMNS);
d281 38
a318 5
	if (!silent && startup) {	/* Read EXINIT, exrc files. */
		if (ex_exrc(sp))
			goto err;
		if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
			if (screen_end(sp))
d320 22
a341 1
			goto done;
a342 1
	}
d344 18
a361 11
	/*
	 * List recovery files if -r specified without file arguments.
	 * Note, options must be initialized and startup information
	 * read before doing this.
	 */
	if (flagchk == 'r' && argv[0] == NULL) {
		if (rcv_list(sp))
			goto err;
		if (screen_end(sp))
			goto err;
		goto done;
d364 3
a366 10
	/*
	 * !!!
	 * Initialize the default ^D, ^U scrolling value here, after the
	 * user has had every opportunity to set the window option.
	 *
	 * It's historic practice that changing the value of the window
	 * option did not alter the default scrolling value, only giving
	 * a count to ^D/^U did that.
	 */
	sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
d368 3
a370 15
	/*
	 * If we don't have a command-line option, switch into the right
	 * editor now, so that we position default files correctly, and
	 * so that any tags file file-already-locked messages are in the
	 * vi screen, not the ex screen.
	 *
	 * XXX
	 * If we have a command-line option, the error message can end
	 * up in the wrong place, but I think that the combination is
	 * unlikely.
	 */
	if (gp->c_option == NULL) {
		F_CLR(sp, SC_EX | SC_VI);
		F_SET(sp, LF_ISSET(SC_EX | SC_VI));
	}
d372 2
a373 2
	/* Open a tag file if specified. */
	if (tag_f != NULL && ex_tag_first(sp, tag_f))
d377 2
a378 3
	 * Append any remaining arguments as file names.  Files are recovery
	 * files if -r specified.  If the tag option or ex startup commands
	 * loaded a file, then any file arguments are going to come after it.
a380 10
		if (sp->frp != NULL) {
			/* Cheat -- we know we have an extra argv slot. */
			MALLOC_NOMSG(sp,
			    *--argv, char *, strlen(sp->frp->name) + 1);
			if (*argv == NULL) {
				v_estr(gp->progname, errno, NULL);
				goto err;
			}
			(void)strcpy(*argv, sp->frp->name);
		}
d382 1
a382 1
		F_SET(sp, SC_ARGNOFREE);
d384 1
a384 1
			F_SET(sp, SC_ARGRECOVER);
d388 2
a389 3
	 * If the ex startup commands and or/the tag option haven't already
	 * created a file, create one.  If no command-line files were given,
	 * use a temporary file.
d391 6
a396 11
	if (sp->frp == NULL) {
		if (sp->argv == NULL) {
			if ((frp = file_add(sp, NULL)) == NULL)
				goto err;
		} else  {
			if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
				goto err;
			if (F_ISSET(sp, SC_ARGRECOVER))
				F_SET(frp, FR_RECOVER);
		}

a398 8
		if (EXCMD_RUNNING(gp)) {
			(void)ex_cmd(sp);
			if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
				if (screen_end(sp))
					goto err;
				goto done;
			}
		}
d402 17
a418 13
	 * Check to see if we need to wait for ex.  If SC_SCR_EX is set, ex
	 * was forced to initialize the screen during startup.  We'd like to
	 * wait for a single character from the user, but we can't because
	 * we're not in raw mode.  We can't switch to raw mode because the
	 * vi initialization will switch to xterm's alternate screen, causing
	 * us to lose the messages we're pausing to make sure the user read.
	 * So, wait for a complete line.  
	 */
	if (F_ISSET(sp, SC_SCR_EX)) {
		p = msg_cmsg(sp, CMSG_CONT_R, &len);
		(void)write(STDOUT_FILENO, p, len);
		for (;;) {
			if (v_event_get(sp, &ev, 0, 0))
d420 30
a449 3
			if (ev.e_event == E_INTERRUPT ||
			    ev.e_event == E_CHARACTER &&
			    (ev.e_value == K_CR || ev.e_value == K_NL))
d451 20
a470 1
			(void)gp->scr_bell(sp);
d474 3
a476 3
	/* Switch into the right editor, regardless. */
	F_CLR(sp, SC_EX | SC_VI);
	F_SET(sp, LF_ISSET(SC_EX | SC_VI));
d479 2
a480 2
	 * Main edit loop.  Vi handles split screens itself, we only return
	 * here when switching editor modes or restarting the screen.
d482 1
a482 3
	while (sp != NULL)
		if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
			goto err;
d484 1
a484 8
done:	rval = 0;
	if (0)
err:		rval = 1;

	/* Clean out the global structure. */
	v_end(gp);

	return (rval);
d488 2
a489 4
 * v_end --
 *	End the program, discarding screens and most of the global area.
 *
 * PUBLIC: void v_end __P((GS *));
d491 3
a493 2
void
v_end(gp)
d495 18
a512 3
{
	MSGS *mp;
	SCR *sp;
d514 3
a516 9
	/* If there are any remaining screens, kill them off. */
	if (gp->ccl_sp != NULL) {
		(void)file_end(gp->ccl_sp, NULL, 1);
		(void)screen_end(gp->ccl_sp);
	}
	while ((sp = gp->dq.cqh_first) != (void *)&gp->dq)
		(void)screen_end(sp);
	while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
		(void)screen_end(sp);
d518 3
a520 3
#ifdef HAVE_PERL_INTERP
	perl_end(gp);
#endif
d522 18
a539 11
#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
	{ FREF *frp;
		/* Free FREF's. */
		while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
			CIRCLEQ_REMOVE(&gp->frefq, frp, q);
			if (frp->name != NULL)
				free(frp->name);
			if (frp->tname != NULL)
				free(frp->tname);
			free(frp);
		}
d541 2
a543 3
	/* Free key input queue. */
	if (gp->i_event != NULL)
		free(gp->i_event);
d545 11
a555 5
	/* Free cut buffers. */
	cut_close(gp);

	/* Free map sequences. */
	seq_close(gp);
d557 1
a557 1
	/* Free default buffer storage. */
d560 6
a565 3
	/* Close message catalogs. */
	msg_close(gp);
#endif
d571 18
d590 3
a592 24
	 * Flush any remaining messages.  If a message is here, it's almost
	 * certainly the message about the event that killed us (although
	 * it's possible that the user is sourcing a file that exits from the
	 * editor).
	 */
	while ((mp = gp->msgq.lh_first) != NULL) {
		(void)fprintf(stderr, "%s%.*s",
		    mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
		LIST_REMOVE(mp, q);
#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
		free(mp->buf);
		free(mp);
#endif
	}

#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
	/* Free any temporary space. */
	if (gp->tmp_bp != NULL)
		free(gp->tmp_bp);

	/* Close debugging file descriptor. */
	if (gp->tracefp != NULL)
		(void)fclose(gp->tracefp);
#endif
d596 34
a629 2
 * v_obsolete --
 *	Convert historic arguments into something getopt(3) will like.
d631 41
a671 3
static int
v_obsolete(name, argv)
	char *name, *argv[];
d678 1
a678 1
	 * Make sure it's not text space memory, because ex modifies the
a682 5
	 *	The c, T, t and w options take arguments so they can't be
	 *	    special arguments.
	 *
	 * Stop if we find "--" as an argument, the user may want to edit
	 * a file named "+foo".
d684 1
a684 1
	while (*++argv && strcmp(argv[0], "--"))
d689 1
a689 1
					goto nomem;
d696 1
a696 1
					goto nomem;
d701 6
a706 14
		} else if (argv[0][0] == '-')
			if (argv[0][1] == '\0') {
				MALLOC_NOMSG(NULL, argv[0], char *, 3);
				if (argv[0] == NULL) {
nomem:					v_estr(name, errno, NULL);
					return (1);
				}
				(void)strcpy(argv[0], "-s");
			} else
				if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
				    argv[0][1] == 't' || argv[0][1] == 'w') &&
				    argv[0][2] == '\0')
					++argv;
	return (0);
d710 2
a711 3
v_estr(name, eno, msg)
	char *name, *msg;
	int eno;
d713 7
a719 6
	(void)fprintf(stderr, "%s", name);
	if (msg != NULL)
		(void)fprintf(stderr, ": %s", msg);
	if (eno)
		(void)fprintf(stderr, ": %s", strerror(errno));
	(void)fprintf(stderr, "\n");
@


1.1.1.4
log
@import of nvi 1.79
@
text
@d21 1
a21 1
static const char sccsid[] = "@@(#)main.c	10.48 (Berkeley) 10/11/96";
a41 1
static void	 attach __P((GS *));
d65 3
a67 2
	int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
	char *tag_f, *wsizearg, path[256];
d110 1
a110 1
	lflag = secure = silent = 0;
d117 1
a117 1
	while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
d119 1
a119 1
	while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
d141 14
a154 1
				attach(gp);
a183 3
		case 'S':
			secure = 1;
			break;
d235 1
a235 1
	if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
d258 4
a261 2
	{ int oargs[5], *oargp = oargs;
	if (lflag) {			/* Command-line options. */
a264 4
	if (readonly)
		*oargp++ = O_READONLY;
	if (secure)
		*oargp++ = O_SECURE;
d420 1
a420 1
	F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
a521 1
#if defined(DEBUG)
a525 1
#endif
a583 27

#ifdef DEBUG
static void
attach(gp)
	GS *gp;
{
	int fd;
	char ch;

	if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
		v_estr(gp->progname, errno, _PATH_TTY);
		return;
	}

	(void)printf("process %lu waiting, enter <CR> to continue: ",
	    (u_long)getpid());
	(void)fflush(stdout);

	do {
		if (read(fd, &ch, 1) != 1) {
			(void)close(fd);
			return;
		}
	} while (ch != '\n' && ch != '\r');
	(void)close(fd);
}
#endif
@
