head	1.7;
access;
symbols
	perseant-exfatfs-base-20250801:1.7
	perseant-exfatfs-base-20240630:1.7
	perseant-exfatfs:1.7.0.36
	perseant-exfatfs-base:1.7
	cjep_sun2x:1.7.0.34
	cjep_sun2x-base:1.7
	cjep_staticlib_x-base1:1.7
	cjep_staticlib_x:1.7.0.32
	cjep_staticlib_x-base:1.7
	phil-wifi-20200421:1.7
	phil-wifi-20200411:1.7
	phil-wifi-20200406:1.7
	pgoyette-compat-merge-20190127:1.7
	pgoyette-compat-20190127:1.7
	pgoyette-compat-20190118:1.7
	pgoyette-compat-1226:1.7
	pgoyette-compat-1126:1.7
	pgoyette-compat-1020:1.7
	pgoyette-compat-0930:1.7
	pgoyette-compat-0906:1.7
	pgoyette-compat-0728:1.7
	pgoyette-compat-0625:1.7
	pgoyette-compat-0521:1.7
	pgoyette-compat-0502:1.7
	pgoyette-compat-0422:1.7
	pgoyette-compat-0415:1.7
	pgoyette-compat-0407:1.7
	pgoyette-compat-0330:1.7
	pgoyette-compat-0322:1.7
	pgoyette-compat-0315:1.7
	pgoyette-compat:1.7.0.30
	pgoyette-compat-base:1.7
	perseant-stdc-iso10646:1.7.0.28
	perseant-stdc-iso10646-base:1.7
	prg-localcount2-base3:1.7
	prg-localcount2-base2:1.7
	prg-localcount2-base1:1.7
	prg-localcount2:1.7.0.26
	prg-localcount2-base:1.7
	pgoyette-localcount-20170426:1.7
	bouyer-socketcan-base1:1.7
	pgoyette-localcount-20170320:1.7
	bouyer-socketcan:1.7.0.24
	bouyer-socketcan-base:1.7
	pgoyette-localcount-20170107:1.7
	pgoyette-localcount-20161104:1.7
	localcount-20160914:1.7
	pgoyette-localcount-20160806:1.7
	pgoyette-localcount-20160726:1.7
	pgoyette-localcount:1.7.0.22
	pgoyette-localcount-base:1.7
	netbsd-5-2-3-RELEASE:1.7
	netbsd-5-1-5-RELEASE:1.7
	yamt-pagecache-base9:1.7
	yamt-pagecache-tag8:1.7
	tls-earlyentropy:1.7.0.18
	tls-earlyentropy-base:1.7
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.7
	riastradh-drm2-base3:1.7
	netbsd-5-2-2-RELEASE:1.7
	netbsd-5-1-4-RELEASE:1.7
	netbsd-5-2-1-RELEASE:1.7
	netbsd-5-1-3-RELEASE:1.7
	agc-symver:1.7.0.20
	agc-symver-base:1.7
	tls-maxphys-base:1.7
	yamt-pagecache-base8:1.7
	netbsd-5-2:1.7.0.16
	yamt-pagecache-base7:1.7
	netbsd-5-2-RELEASE:1.7
	netbsd-5-2-RC1:1.7
	yamt-pagecache-base6:1.7
	yamt-pagecache-base5:1.7
	yamt-pagecache-base4:1.7
	netbsd-5-1-2-RELEASE:1.7
	netbsd-5-1-1-RELEASE:1.7
	yamt-pagecache-base3:1.7
	yamt-pagecache-base2:1.7
	yamt-pagecache:1.7.0.14
	yamt-pagecache-base:1.7
	bouyer-quota2-nbase:1.7
	bouyer-quota2:1.7.0.12
	bouyer-quota2-base:1.7
	matt-nb5-pq3:1.7.0.10
	matt-nb5-pq3-base:1.7
	netbsd-5-1:1.7.0.8
	netbsd-5-1-RELEASE:1.7
	netbsd-5-1-RC4:1.7
	netbsd-5-1-RC3:1.7
	netbsd-5-1-RC2:1.7
	netbsd-5-1-RC1:1.7
	netbsd-5-0-2-RELEASE:1.7
	netbsd-5-0-1-RELEASE:1.7
	jym-xensuspend-nbase:1.7
	netbsd-5-0:1.7.0.6
	netbsd-5-0-RELEASE:1.7
	netbsd-5-0-RC4:1.7
	netbsd-5-0-RC3:1.7
	netbsd-5-0-RC2:1.7
	jym-xensuspend:1.7.0.4
	jym-xensuspend-base:1.7
	netbsd-5-0-RC1:1.7
	netbsd-5:1.7.0.2
	netbsd-5-base:1.7
	matt-mips64-base2:1.6
	matt-mips64:1.6.0.40
	mjf-devfs2:1.6.0.38
	mjf-devfs2-base:1.6
	netbsd-4-0-1-RELEASE:1.6
	wrstuden-revivesa-base-3:1.6
	wrstuden-revivesa-base-2:1.6
	wrstuden-fixsa-newbase:1.6
	wrstuden-revivesa-base-1:1.6
	yamt-pf42-base4:1.6
	yamt-pf42-base3:1.6
	hpcarm-cleanup-nbase:1.6
	yamt-pf42-baseX:1.6
	yamt-pf42-base2:1.6
	wrstuden-revivesa:1.6.0.36
	wrstuden-revivesa-base:1.6
	yamt-pf42:1.6.0.34
	yamt-pf42-base:1.6
	keiichi-mipv6-nbase:1.6
	keiichi-mipv6:1.6.0.32
	keiichi-mipv6-base:1.6
	matt-armv6-nbase:1.6
	matt-armv6-prevmlocking:1.6
	wrstuden-fixsa-base-1:1.6
	netbsd-4-0:1.6.0.30
	netbsd-4-0-RELEASE:1.6
	cube-autoconf:1.6.0.28
	cube-autoconf-base:1.6
	netbsd-4-0-RC5:1.6
	netbsd-4-0-RC4:1.6
	netbsd-4-0-RC3:1.6
	netbsd-4-0-RC2:1.6
	netbsd-4-0-RC1:1.6
	matt-armv6:1.6.0.26
	matt-armv6-base:1.6
	matt-mips64-base:1.6
	hpcarm-cleanup:1.6.0.24
	hpcarm-cleanup-base:1.6
	netbsd-3-1-1-RELEASE:1.6
	netbsd-3-0-3-RELEASE:1.6
	wrstuden-fixsa:1.6.0.22
	wrstuden-fixsa-base:1.6
	abandoned-netbsd-4-base:1.6
	abandoned-netbsd-4:1.6.0.16
	netbsd-3-1:1.6.0.18
	netbsd-3-1-RELEASE:1.6
	netbsd-3-0-2-RELEASE:1.6
	netbsd-3-1-RC4:1.6
	netbsd-3-1-RC3:1.6
	netbsd-3-1-RC2:1.6
	netbsd-3-1-RC1:1.6
	netbsd-4:1.6.0.20
	netbsd-4-base:1.6
	chap-midi-nbase:1.6
	netbsd-3-0-1-RELEASE:1.6
	chap-midi:1.6.0.14
	chap-midi-base:1.6
	netbsd-3-0:1.6.0.12
	netbsd-3-0-RELEASE:1.6
	netbsd-3-0-RC6:1.6
	netbsd-3-0-RC5:1.6
	netbsd-3-0-RC4:1.6
	netbsd-3-0-RC3:1.6
	netbsd-3-0-RC2:1.6
	netbsd-3-0-RC1:1.6
	netbsd-2-0-3-RELEASE:1.6
	netbsd-2-1:1.6.0.10
	netbsd-2-1-RELEASE:1.6
	netbsd-2-1-RC6:1.6
	netbsd-2-1-RC5:1.6
	netbsd-2-1-RC4:1.6
	netbsd-2-1-RC3:1.6
	netbsd-2-1-RC2:1.6
	netbsd-2-1-RC1:1.6
	netbsd-2-0-2-RELEASE:1.6
	netbsd-3:1.6.0.8
	netbsd-3-base:1.6
	netbsd-2-0-1-RELEASE:1.6
	netbsd-2:1.6.0.6
	netbsd-2-base:1.6
	netbsd-2-0-RELEASE:1.6
	netbsd-2-0-RC5:1.6
	netbsd-2-0-RC4:1.6
	netbsd-2-0-RC3:1.6
	netbsd-2-0-RC2:1.6
	netbsd-2-0-RC1:1.6
	netbsd-2-0:1.6.0.4
	netbsd-2-0-base:1.6
	netbsd-1-6-PATCH002-RELEASE:1.6
	netbsd-1-6-PATCH002:1.6
	netbsd-1-6-PATCH002-RC4:1.6
	netbsd-1-6-PATCH002-RC3:1.6
	netbsd-1-6-PATCH002-RC2:1.6
	netbsd-1-6-PATCH002-RC1:1.6
	netbsd-1-6-PATCH001:1.6
	netbsd-1-6-PATCH001-RELEASE:1.6
	netbsd-1-6-PATCH001-RC3:1.6
	netbsd-1-6-PATCH001-RC2:1.6
	netbsd-1-6-PATCH001-RC1:1.6
	fvdl_fs64_base:1.6
	netbsd-1-6-RELEASE:1.6
	netbsd-1-6-RC3:1.6
	netbsd-1-6-RC2:1.6
	netbsd-1-6-RC1:1.6
	netbsd-1-6:1.6.0.2
	netbsd-1-6-base:1.6
	netbsd-1-5-PATCH003:1.3
	netbsd-1-5-PATCH002:1.3
	netbsd-1-5-PATCH001:1.3
	nvi_1_79:1.1.1.2
	netbsd-1-5-RELEASE:1.3
	netbsd-1-5-BETA2:1.3
	netbsd-1-5-BETA:1.3
	netbsd-1-4-PATCH003:1.3
	netbsd-1-5-ALPHA2:1.3
	netbsd-1-5:1.3.0.10
	netbsd-1-5-base:1.3
	minoura-xpg4dl-base:1.3
	minoura-xpg4dl:1.3.0.8
	netbsd-1-4-PATCH002:1.3
	wrstuden-devbsize-19991221:1.3
	wrstuden-devbsize:1.3.0.6
	wrstuden-devbsize-base:1.3
	comdex-fall-1999:1.3.0.4
	comdex-fall-1999-base:1.3
	netbsd-1-4-PATCH001:1.3
	netbsd-1-4-RELEASE:1.3
	netbsd-1-4:1.3.0.2
	netbsd-1-4-base:1.3
	netbsd-1-3-PATCH003:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.1
	netbsd-1-3-PATCH002:1.1.1.1
	netbsd-1-3-PATCH001:1.1.1.1
	netbsd-1-3-RELEASE:1.1.1.1
	netbsd-1-3-BETA:1.1.1.1
	netbsd-1-3:1.1.1.1.0.2
	netbsd-1-3-base:1.1.1.1
	netbsd-1-2-PATCH001:1.1.1.1
	netbsd-1-2-RELEASE:1.1.1.1
	netbsd-1-2-BETA:1.1.1.1
	netbsd-1-2:1.1.1.1.0.4
	netbsd-1-2-base:1.1.1.1
	nvi_1_66:1.1.1.1
	BOSTIC:1.1.1;
locks; strict;
comment	@ * @;


1.7
date	2008.10.29.16.49.34;	author christos;	state dead;
branches;
next	1.6;

1.6
date	2002.04.09.01.47.30;	author thorpej;	state Exp;
branches;
next	1.5;

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

1.4
date	2001.03.31.11.37.45;	author aymeric;	state Exp;
branches;
next	1.3;

1.3
date	99.02.19.21.25.02;	author abs;	state Exp;
branches;
next	1.2;

1.2
date	98.01.09.08.06.21;	author perry;	state Exp;
branches;
next	1.1;

1.1
date	96.05.20.01.58.19;	author mrg;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	96.05.20.01.58.19;	author mrg;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	2001.03.31.11.29.51;	author aymeric;	state Exp;
branches;
next	;


desc
@@


1.7
log
@bye old vi!
@
text
@/*	$NetBSD: cl_main.c,v 1.6 2002/04/09 01:47:30 thorpej Exp $	*/

/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 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
#if 0
static const char sccsid[] = "@@(#)cl_main.c	10.36 (Berkeley) 10/14/96";
#else
__RCSID("$NetBSD: cl_main.c,v 1.6 2002/04/09 01:47:30 thorpej Exp $");
#endif
#endif /* not lint */

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

#include <bitstring.h>
#include <curses.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "../common/common.h"
#ifdef RUNNING_IP
#include "../ip/ip.h"
#endif
#include "cl.h"
#include "pathnames.h"

GS *__global_list;				/* GLOBAL: List of screens. */
sigset_t __sigblockset;				/* GLOBAL: Blocked signals. */

int		   main __P((int, char **));
static void	   cl_func_std __P((GS *));
static CL_PRIVATE *cl_init __P((GS *));
static GS	  *gs_init __P((char *));
static void	   perr __P((char *, char *));
static int	   setsig __P((int, struct sigaction *, void (*)(int)));
static void	   sig_end __P((GS *));
static void	   term_init __P((char *, char *));
static void	   h_hup __P((int));
static void	   h_int __P((int));
static void	   h_term __P((int));
static void	   h_winch __P((int));

/*
 * main --
 *	This is the main loop for the standalone curses editor.
 */
int
main(argc, argv)
	int argc;
	char *argv[];
{
	static int reenter;
	CL_PRIVATE *clp;
	GS *gp;
	size_t rows, cols;
	int rval;
	char *ip_arg, *ttype;
#ifdef RUNNING_IP
	char **p_av, **t_av;
#endif
	/* If loaded at 0 and jumping through a NULL pointer, stop. */
	if (reenter++)
		abort();

	/* Create and initialize the global structure. */
	__global_list = gp = gs_init(argv[0]);

	/*
	 * Strip out any arguments that vi isn't going to understand.  There's
	 * no way to portably call getopt twice, so arguments parsed here must
	 * be removed from the argument list.
	 */
#ifdef RUNNING_IP
	ip_arg = NULL;
	for (p_av = t_av = argv;;) {
		if (*t_av == NULL) {
			*p_av = NULL;
			break;
		}
		if (!strcmp(*t_av, "--")) {
			while ((*p_av++ = *t_av++) != NULL);
			break;
		}
		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
			if (t_av[0][2] != '\0') {
				ip_arg = t_av[0] + 2;
				++t_av;
				--argc;
				continue;
			}
			if (t_av[1] != NULL) {
				ip_arg = t_av[1];
				t_av += 2;
				argc -= 2;
				continue;
			}
		}
		*p_av++ = *t_av++;
	}

	/*
	 * If we're being called as an editor library, we're done here, we
	 * get loaded with the curses screen, we don't share much code.
	 */
	if (ip_arg != NULL)
		exit (ip_main(argc, argv, gp, ip_arg));
#else
	ip_arg = argv[0];
#endif /* RUNNING_IP */
		
	/* Create and initialize the CL_PRIVATE structure. */
	clp = cl_init(gp);

	/*
	 * Initialize the terminal information.
	 *
	 * We have to know what terminal it is from the start, since we may
	 * have to use termcap/terminfo to find out how big the screen is.
	 */
	if ((ttype = getenv("TERM")) == NULL) {
		fprintf(stderr,"WARNING: TERM environment variable not set.\n");
		ttype = "unknown";
	}
	term_init(gp->progname, ttype);

	/* Add the terminal type to the global structure. */
	if ((OG_D_STR(gp, GO_TERM) =
	    OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
		perr(gp->progname, NULL);

	/* Figure out how big the screen is. */
	if (cl_ssize(NULL, 0, &rows, &cols, NULL))
		exit (1);

	/* Add the rows and columns to the global structure. */
	OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
	OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;

	/* Ex wants stdout to be buffered. */
	(void)setvbuf(stdout, NULL, _IOFBF, 0);

	/* Start catching signals. */
	if (sig_init(gp, NULL))
		exit (1);

	/* Run ex/vi. */
	rval = editor(gp, argc, argv);

	/* Clean up signals. */
	sig_end(gp);

	/* Clean up the terminal. */
	(void)cl_quit(gp);

	/*
	 * XXX
	 * Reset the O_MESG option.
	 */
	if (clp->tgw != TGW_UNKNOWN)
		(void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);

	/*
	 * XXX
	 * Reset the X11 xterm icon/window name.
	 */
	if (F_ISSET(clp, CL_RENAME)) {
		(void)printf(XTERM_RENAME, ttype);
		(void)fflush(stdout);
	}

	/* If a killer signal arrived, pretend we just got it. */
	if (clp->killersig) {
		(void)signal(clp->killersig, SIG_DFL);
		(void)kill(getpid(), clp->killersig);
		/* NOTREACHED */
	}

	/* Free the global and CL private areas. */
#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
	free(clp);
	free(gp);
#endif

	exit (rval);
}

/*
 * gs_init --
 *	Create and partially initialize the GS structure.
 */
static GS *
gs_init(name)
	char *name;
{
	GS *gp;
	char *p;

	/* Figure out what our name is. */
	if ((p = strrchr(name, '/')) != NULL)
		name = p + 1;

	/* Allocate the global structure. */
	CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
	if (gp == NULL)
		perr(name, NULL);


	gp->progname = name;
	return (gp);
}

/*
 * cl_init --
 *	Create and partially initialize the CL structure.
 */
static CL_PRIVATE *
cl_init(gp)
	GS *gp;
{
	CL_PRIVATE *clp;
	int fd;

	/* Allocate the CL private structure. */
	CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
	if (clp == NULL)
		perr(gp->progname, NULL);
	gp->cl_private = clp;

	/*
	 * Set the CL_STDIN_TTY flag.  It's purpose is to avoid setting
	 * and resetting the tty if the input isn't from there.  We also
	 * use the same test to determine if we're running a script or
	 * not.
	 */
	if (isatty(STDIN_FILENO))
		F_SET(clp, CL_STDIN_TTY);
	else
		F_SET(gp, G_SCRIPTED);

	/*
	 * We expect that if we've lost our controlling terminal that the
	 * open() (but not the tcgetattr()) will fail.
	 */
	if (F_ISSET(clp, CL_STDIN_TTY)) {
		if (tcgetattr(STDIN_FILENO, &clp->orig) == -1)
			goto tcfail;
	} else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
		if (tcgetattr(fd, &clp->orig) == -1) {
tcfail:			perr(gp->progname, "tcgetattr");
			exit (1);
		}
		(void)close(fd);
	}

	/* Initialize the list of curses functions. */
	cl_func_std(gp);

	return (clp);
}

/*
 * term_init --
 *	Initialize terminal information.
 */
static void
term_init(name, ttype)
	char *name, *ttype;
{
	int err;

	/* Set up the terminal database information. */
	setupterm(ttype, STDOUT_FILENO, &err);
	switch (err) {
	case -1:
		(void)fprintf(stderr,
		    "%s: No terminal database found\n", name);
		exit (1);
	case 0:
		(void)fprintf(stderr,
		    "%s: %s: unknown terminal type\n", name, ttype);
		exit (1);
	}
}

#define	GLOBAL_CLP \
	CL_PRIVATE *clp = GCLP(__global_list);
static void
h_hup(signo)
	int signo;
{
	GLOBAL_CLP;

	F_SET(clp, CL_SIGHUP);
	clp->killersig = SIGHUP;
}

static void
h_int(signo)
	int signo;
{
	GLOBAL_CLP;

	F_SET(clp, CL_SIGINT);
}

static void
h_term(signo)
	int signo;
{
	GLOBAL_CLP;

	F_SET(clp, CL_SIGTERM);
	clp->killersig = SIGTERM;
}

static void
h_winch(signo)
	int signo;
{
	GLOBAL_CLP;

	F_SET(clp, CL_SIGWINCH);
}
#undef	GLOBAL_CLP

/*
 * sig_init --
 *	Initialize signals.
 *
 * PUBLIC: int sig_init __P((GS *, SCR *));
 */
int
sig_init(gp, sp)
	GS *gp;
	SCR *sp;
{
	CL_PRIVATE *clp;

	clp = GCLP(gp);

	if (sp == NULL) {
		(void)sigemptyset(&__sigblockset);
		if (sigaddset(&__sigblockset, SIGHUP) ||
		    setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
		    sigaddset(&__sigblockset, SIGINT) ||
		    setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
		    sigaddset(&__sigblockset, SIGTERM) ||
		    setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
#ifdef SIGWINCH
		    ||
		    sigaddset(&__sigblockset, SIGWINCH) ||
		    setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
#endif
		    ) {
			perr(gp->progname, NULL);
			return (1);
		}
	} else
		if (setsig(SIGHUP, NULL, h_hup) ||
		    setsig(SIGINT, NULL, h_int) ||
		    setsig(SIGTERM, NULL, h_term)
#ifdef SIGWINCH
		    ||
		    setsig(SIGWINCH, NULL, h_winch)
#endif
		    ) {
			msgq(sp, M_SYSERR, "signal-reset");
		}
	return (0);
}

/*
 * setsig --
 *	Set a signal handler.
 */
static int
setsig(signo, oactp, handler)
	int signo;
	struct sigaction *oactp;
	void (*handler) __P((int));
{
	struct sigaction act;

	/*
	 * Use sigaction(2), not signal(3), since we don't always want to
	 * restart system calls.  The example is when waiting for a command
	 * mode keystroke and SIGWINCH arrives.  Besides, you can't portably
	 * restart system calls (thanks, POSIX!).  On the other hand, you
	 * can't portably NOT restart system calls (thanks, Sun!).  SunOS
	 * used SA_INTERRUPT as their extension to NOT restart read calls.
	 * We sure hope nobody else used it for anything else.  Mom told me
	 * there'd be days like this.  She just never told me that there'd
	 * be so many.
	 */
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);

#ifdef SA_INTERRUPT
	act.sa_flags = SA_INTERRUPT;
#else
	act.sa_flags = 0;
#endif
	return (sigaction(signo, &act, oactp));
}

/*
 * sig_end --
 *	End signal setup.
 */
static void
sig_end(gp)
	GS *gp;
{
	CL_PRIVATE *clp;

	clp = GCLP(gp);
	(void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
	(void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
	(void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
#ifdef SIGWINCH
	(void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
#endif
}

/*
 * cl_func_std --
 *	Initialize the standard curses functions.
 */
static void
cl_func_std(gp)
	GS *gp;
{
	gp->scr_addstr = cl_addstr;
	gp->scr_attr = cl_attr;
	gp->scr_baud = cl_baud;
	gp->scr_bell = cl_bell;
	gp->scr_busy = NULL;
	gp->scr_clrtoeol = cl_clrtoeol;
	gp->scr_cursor = cl_cursor;
	gp->scr_deleteln = cl_deleteln;
	gp->scr_event = cl_event;
	gp->scr_ex_adjust = cl_ex_adjust;
	gp->scr_fmap = cl_fmap;
	gp->scr_insertln = cl_insertln;
	gp->scr_keyval = cl_keyval;
	gp->scr_move = cl_move;
	gp->scr_msg = NULL;
	gp->scr_optchange = cl_optchange;
	gp->scr_refresh = cl_refresh;
	gp->scr_rename = cl_rename;
	gp->scr_screen = cl_screen;
	gp->scr_suspend = cl_suspend;
	gp->scr_usage = cl_usage;
}

/*
 * perr --
 *	Print system error.
 */
static void
perr(name, msg)
	char *name, *msg;
{
	(void)fprintf(stderr, "%s:", name);
	if (msg != NULL)
		(void)fprintf(stderr, "%s:", msg);
	(void)fprintf(stderr, "%s\n", strerror(errno));
	exit(1);
}
@


1.6
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: cl_main.c,v 1.5 2001/05/01 16:46:11 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.5
log
@shutup most (non-intrusive) gcc warnings
@
text
@d1 1
a1 1
/*	$NetBSD: cl_main.c,v 1.4 2001/03/31 11:37:45 aymeric Exp $	*/
d14 1
d16 1
d18 3
@


1.4
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: cl_main.c,v 1.3 1999/02/19 21:25:02 abs Exp $	*/
d42 1
d50 4
d69 4
a72 2
	char *ip_arg, **p_av, **t_av, *ttype;

d121 1
a121 1
#endif
a206 1
	CL_PRIVATE *clp;
@


1.3
log
@Warn if TERM environment variable not set (but still continue as normal)
@
text
@d1 1
a1 1
/*	$NetBSD: cl_main.c,v 1.2 1998/01/09 08:06:21 perry Exp $	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)cl_main.c	10.27 (Berkeley) 4/28/96";
d33 3
d42 7
a48 5
static GS	*gs_init __P((char *));
static void	 nomem __P((char *));
static int	 setsig __P((int, struct sigaction *, void (*)(int)));
static void	 sig_end __P((GS *));
static void	 term_init __P((char *, char *));
d64 1
a64 1
	char **p_av, **t_av, *ttype;
a71 1
	clp = GCLP(gp);
a72 1
#ifdef NOT_NEEDED_FOR_CURSES
d78 2
d85 18
d105 9
d115 3
d134 1
a134 1
		nomem(gp->progname);
d167 9
a201 1
	int fd;
d210 18
d230 3
a232 4
	if (gp != NULL)
		CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
	if (gp == NULL || clp == NULL)
		nomem(name);
a234 23
	/* Initialize the list of curses functions. */
	gp->scr_addstr = cl_addstr;
	gp->scr_attr = cl_attr;
	gp->scr_baud = cl_baud;
	gp->scr_bell = cl_bell;
	gp->scr_busy = NULL;
	gp->scr_clrtoeol = cl_clrtoeol;
	gp->scr_cursor = cl_cursor;
	gp->scr_deleteln = cl_deleteln;
	gp->scr_event = cl_event;
	gp->scr_ex_adjust = cl_ex_adjust;
	gp->scr_fmap = cl_fmap;
	gp->scr_insertln = cl_insertln;
	gp->scr_keyval = cl_keyval;
	gp->scr_move = cl_move;
	gp->scr_msg = NULL;
	gp->scr_optchange = cl_optchange;
	gp->scr_refresh = cl_refresh;
	gp->scr_rename = cl_rename;
	gp->scr_screen = cl_screen;
	gp->scr_suspend = cl_suspend;
	gp->scr_usage = cl_usage;

d236 4
a239 2
	 * Set the G_STDIN_TTY flag.  It's purpose is to avoid setting and
	 * resetting the tty if the input isn't from there.
d242 3
a244 1
		F_SET(gp, G_STDIN_TTY);
d250 1
a250 1
	if (F_ISSET(gp, G_STDIN_TTY)) {
d255 1
a255 2
tcfail:			(void)fprintf(stderr,
			    "%s: tcgetattr: %s\n", name, strerror(errno));
d261 4
a264 2
	gp->progname = name;
	return (gp);
d354 3
a356 1
		    setsig(SIGTERM, &clp->oact[INDX_TERM], h_term) ||
d358 4
a361 3
		    setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)) {
			(void)fprintf(stderr,
			    "%s: %s\n", gp->progname, strerror(errno));
d367 6
a372 2
		    setsig(SIGTERM, NULL, h_term) ||
		    setsig(SIGWINCH, NULL, h_winch)) {
d426 1
d428 1
d432 2
a433 2
 * nomem --
 *	No memory error.
d436 33
a468 2
nomem(name)
	char *name;
d470 4
a473 1
	(void)fprintf(stderr, "%s: %s\n", name, strerror(errno));
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d90 2
a91 1
	if ((ttype = getenv("TERM")) == NULL)
d93 1
@


1.1
log
@Initial revision
@
text
@d1 2
@


1.1.1.1
log
@import of nvi 1.66
@
text
@@


1.1.1.2
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)cl_main.c	10.36 (Berkeley) 10/14/96";
a30 3
#ifdef RUNNING_IP
#include "../ip/ip.h"
#endif
d37 5
a41 7
static void	   cl_func_std __P((GS *));
static CL_PRIVATE *cl_init __P((GS *));
static GS	  *gs_init __P((char *));
static void	   perr __P((char *, char *));
static int	   setsig __P((int, struct sigaction *, void (*)(int)));
static void	   sig_end __P((GS *));
static void	   term_init __P((char *, char *));
d57 1
a57 1
	char *ip_arg, **p_av, **t_av, *ttype;
d65 1
d67 1
a72 2
#ifdef RUNNING_IP
	ip_arg = NULL;
a77 18
		if (!strcmp(*t_av, "--")) {
			while ((*p_av++ = *t_av++) != NULL);
			break;
		}
		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
			if (t_av[0][2] != '\0') {
				ip_arg = t_av[0] + 2;
				++t_av;
				--argc;
				continue;
			}
			if (t_av[1] != NULL) {
				ip_arg = t_av[1];
				t_av += 2;
				argc -= 2;
				continue;
			}
		}
a79 9

	/*
	 * If we're being called as an editor library, we're done here, we
	 * get loaded with the curses screen, we don't share much code.
	 */
	if (ip_arg != NULL)
		exit (ip_main(argc, argv, gp, ip_arg));
#else
	ip_arg = argv[0];
a80 3
		
	/* Create and initialize the CL_PRIVATE structure. */
	clp = cl_init(gp);
d95 1
a95 1
		perr(gp->progname, NULL);
a127 9
	/*
	 * XXX
	 * Reset the X11 xterm icon/window name.
	 */
	if (F_ISSET(clp, CL_RENAME)) {
		(void)printf(XTERM_RENAME, ttype);
		(void)fflush(stdout);
	}

d154 1
a162 18
	if (gp == NULL)
		perr(name, NULL);


	gp->progname = name;
	return (gp);
}

/*
 * cl_init --
 *	Create and partially initialize the CL structure.
 */
static CL_PRIVATE *
cl_init(gp)
	GS *gp;
{
	CL_PRIVATE *clp;
	int fd;
d165 4
a168 3
	CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
	if (clp == NULL)
		perr(gp->progname, NULL);
d171 23
d195 2
a196 4
	 * Set the CL_STDIN_TTY flag.  It's purpose is to avoid setting
	 * and resetting the tty if the input isn't from there.  We also
	 * use the same test to determine if we're running a script or
	 * not.
d199 1
a199 3
		F_SET(clp, CL_STDIN_TTY);
	else
		F_SET(gp, G_SCRIPTED);
d205 1
a205 1
	if (F_ISSET(clp, CL_STDIN_TTY)) {
d210 2
a211 1
tcfail:			perr(gp->progname, "tcgetattr");
d217 2
a218 4
	/* Initialize the list of curses functions. */
	cl_func_std(gp);

	return (clp);
d308 1
a308 3
		    setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
#ifdef SIGWINCH
		    ||
d310 3
a312 4
		    setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
#endif
		    ) {
			perr(gp->progname, NULL);
d318 2
a319 6
		    setsig(SIGTERM, NULL, h_term)
#ifdef SIGWINCH
		    ||
		    setsig(SIGWINCH, NULL, h_winch)
#endif
		    ) {
a372 1
#ifdef SIGWINCH
a373 1
#endif
d377 2
a378 2
 * cl_func_std --
 *	Initialize the standard curses functions.
d381 2
a382 33
cl_func_std(gp)
	GS *gp;
{
	gp->scr_addstr = cl_addstr;
	gp->scr_attr = cl_attr;
	gp->scr_baud = cl_baud;
	gp->scr_bell = cl_bell;
	gp->scr_busy = NULL;
	gp->scr_clrtoeol = cl_clrtoeol;
	gp->scr_cursor = cl_cursor;
	gp->scr_deleteln = cl_deleteln;
	gp->scr_event = cl_event;
	gp->scr_ex_adjust = cl_ex_adjust;
	gp->scr_fmap = cl_fmap;
	gp->scr_insertln = cl_insertln;
	gp->scr_keyval = cl_keyval;
	gp->scr_move = cl_move;
	gp->scr_msg = NULL;
	gp->scr_optchange = cl_optchange;
	gp->scr_refresh = cl_refresh;
	gp->scr_rename = cl_rename;
	gp->scr_screen = cl_screen;
	gp->scr_suspend = cl_suspend;
	gp->scr_usage = cl_usage;
}

/*
 * perr --
 *	Print system error.
 */
static void
perr(name, msg)
	char *name, *msg;
d384 1
a384 4
	(void)fprintf(stderr, "%s:", name);
	if (msg != NULL)
		(void)fprintf(stderr, "%s:", msg);
	(void)fprintf(stderr, "%s\n", strerror(errno));
@

