1996-11-01 06:45:43 +00:00
|
|
|
/*-
|
|
|
|
* 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/types.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <bitstring.h>
|
2020-09-09 07:57:59 +00:00
|
|
|
#include <err.h>
|
1996-11-01 06:45:43 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#ifdef HAVE_TERM_H
|
|
|
|
#include <term.h>
|
|
|
|
#endif
|
1996-11-01 06:45:43 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../common/common.h"
|
|
|
|
#include "cl.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
GS *__global_list; /* GLOBAL: List of screens. */
|
|
|
|
sigset_t __sigblockset; /* GLOBAL: Blocked signals. */
|
|
|
|
|
2015-04-10 13:26:05 +00:00
|
|
|
static void cl_func_std(GS *);
|
|
|
|
static CL_PRIVATE *cl_init(GS *);
|
2020-09-09 07:57:59 +00:00
|
|
|
static GS *gs_init(void);
|
2015-04-10 13:26:05 +00:00
|
|
|
static int setsig(int, struct sigaction *, void (*)(int));
|
|
|
|
static void sig_end(GS *);
|
2020-09-09 07:57:59 +00:00
|
|
|
static void term_init(char *);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main --
|
|
|
|
* This is the main loop for the standalone curses editor.
|
|
|
|
*/
|
|
|
|
int
|
2015-04-10 13:26:05 +00:00
|
|
|
main(int argc, char *argv[])
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
static int reenter;
|
|
|
|
CL_PRIVATE *clp;
|
|
|
|
GS *gp;
|
|
|
|
size_t rows, cols;
|
|
|
|
int rval;
|
2013-08-11 09:44:58 +00:00
|
|
|
char **p_av, **t_av, *ttype;
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/* If loaded at 0 and jumping through a NULL pointer, stop. */
|
|
|
|
if (reenter++)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
/* Create and initialize the global structure. */
|
2020-09-09 07:57:59 +00:00
|
|
|
__global_list = gp = gs_init();
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
*p_av++ = *t_av++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
2015-04-10 13:26:05 +00:00
|
|
|
ttype = "ansi";
|
2020-09-09 07:57:59 +00:00
|
|
|
term_init(ttype);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/* Add the terminal type to the global structure. */
|
|
|
|
if ((OG_D_STR(gp, GO_TERM) =
|
|
|
|
OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
|
2020-09-09 07:57:59 +00:00
|
|
|
err(1, NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
if (F_ISSET(clp, CL_RENAME))
|
|
|
|
cl_setname(gp, clp->oname);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2015-04-10 13:26:05 +00:00
|
|
|
#if defined(DEBUG) || defined(PURIFY)
|
2020-09-09 07:57:59 +00:00
|
|
|
free(clp->oname);
|
1996-11-01 06:45:43 +00:00
|
|
|
free(clp);
|
2013-08-11 09:44:58 +00:00
|
|
|
free(OG_STR(gp, GO_TERM));
|
1996-11-01 06:45:43 +00:00
|
|
|
free(gp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gs_init --
|
|
|
|
* Create and partially initialize the GS structure.
|
|
|
|
*/
|
|
|
|
static GS *
|
2020-09-09 07:57:59 +00:00
|
|
|
gs_init(void)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GS *gp;
|
|
|
|
|
2020-09-09 07:57:59 +00:00
|
|
|
gp = calloc(1, sizeof(GS));
|
1996-11-01 06:45:43 +00:00
|
|
|
if (gp == NULL)
|
2020-09-09 07:57:59 +00:00
|
|
|
err(1, NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
return (gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cl_init --
|
|
|
|
* Create and partially initialize the CL structure.
|
|
|
|
*/
|
|
|
|
static CL_PRIVATE *
|
2013-08-11 09:44:58 +00:00
|
|
|
cl_init(GS *gp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
CL_PRIVATE *clp;
|
|
|
|
int fd;
|
|
|
|
|
2020-09-09 07:57:59 +00:00
|
|
|
clp = calloc(1, sizeof(CL_PRIVATE));
|
1996-11-01 06:45:43 +00:00
|
|
|
if (clp == NULL)
|
2020-09-09 07:57:59 +00:00
|
|
|
err(1, NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
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) {
|
2020-09-09 07:57:59 +00:00
|
|
|
tcfail: err(1, "tcgetattr");
|
1996-11-01 06:45:43 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
(void)close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the list of curses functions. */
|
|
|
|
cl_func_std(gp);
|
|
|
|
|
|
|
|
return (clp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* term_init --
|
|
|
|
* Initialize terminal information.
|
|
|
|
*/
|
|
|
|
static void
|
2020-09-09 07:57:59 +00:00
|
|
|
term_init(char *ttype)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Set up the terminal database information. */
|
|
|
|
setupterm(ttype, STDOUT_FILENO, &err);
|
|
|
|
switch (err) {
|
|
|
|
case -1:
|
2020-09-09 07:57:59 +00:00
|
|
|
errx(1, "No terminal database found");
|
1996-11-01 06:45:43 +00:00
|
|
|
case 0:
|
2020-09-09 07:57:59 +00:00
|
|
|
errx(1, "%s: unknown terminal type", ttype);
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GLOBAL_CLP \
|
|
|
|
CL_PRIVATE *clp = GCLP(__global_list);
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
h_hup(int signo)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GLOBAL_CLP;
|
|
|
|
|
|
|
|
F_SET(clp, CL_SIGHUP);
|
|
|
|
clp->killersig = SIGHUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
h_int(int signo)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GLOBAL_CLP;
|
|
|
|
|
|
|
|
F_SET(clp, CL_SIGINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
h_term(int signo)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GLOBAL_CLP;
|
|
|
|
|
|
|
|
F_SET(clp, CL_SIGTERM);
|
|
|
|
clp->killersig = SIGTERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
h_winch(int signo)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GLOBAL_CLP;
|
|
|
|
|
|
|
|
F_SET(clp, CL_SIGWINCH);
|
|
|
|
}
|
|
|
|
#undef GLOBAL_CLP
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sig_init --
|
|
|
|
* Initialize signals.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int sig_init(GS *, SCR *);
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
sig_init(GS *gp, SCR *sp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
) {
|
2020-09-09 07:57:59 +00:00
|
|
|
err(1, NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
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
|
2015-04-10 13:26:05 +00:00
|
|
|
setsig(int signo, struct sigaction *oactp, void (*handler)(int))
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
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
|
2015-04-10 13:26:05 +00:00
|
|
|
* restart system calls (thanks, POSIX!).
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
|
|
|
act.sa_handler = handler;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
|
|
|
|
act.sa_flags = 0;
|
|
|
|
return (sigaction(signo, &act, oactp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sig_end --
|
|
|
|
* End signal setup.
|
|
|
|
*/
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
sig_end(GS *gp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
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
|
2013-08-11 09:44:58 +00:00
|
|
|
cl_func_std(GS *gp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
gp->scr_addstr = cl_addstr;
|
2013-08-11 09:44:58 +00:00
|
|
|
gp->scr_waddstr = cl_waddstr;
|
1996-11-01 06:45:43 +00:00
|
|
|
gp->scr_attr = cl_attr;
|
|
|
|
gp->scr_baud = cl_baud;
|
|
|
|
gp->scr_bell = cl_bell;
|
|
|
|
gp->scr_busy = NULL;
|
2013-08-11 09:44:58 +00:00
|
|
|
gp->scr_child = NULL;
|
1996-11-01 06:45:43 +00:00
|
|
|
gp->scr_clrtoeol = cl_clrtoeol;
|
|
|
|
gp->scr_cursor = cl_cursor;
|
|
|
|
gp->scr_deleteln = cl_deleteln;
|
2013-08-11 09:44:58 +00:00
|
|
|
gp->scr_reply = NULL;
|
|
|
|
gp->scr_discard = cl_discard;
|
1996-11-01 06:45:43 +00:00
|
|
|
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;
|
2013-08-11 09:44:58 +00:00
|
|
|
gp->scr_split = cl_split;
|
1996-11-01 06:45:43 +00:00
|
|
|
gp->scr_suspend = cl_suspend;
|
|
|
|
gp->scr_usage = cl_usage;
|
|
|
|
}
|