2002-04-09 05:47:30 +04:00
|
|
|
/* $NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $ */
|
1998-01-09 11:03:16 +03:00
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 06:01:36 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-17 20:17:19 +04:00
|
|
|
*
|
1996-05-20 06:01:36 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-04-09 05:47:30 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-08-17 20:17:19 +04:00
|
|
|
#ifndef lint
|
2002-04-09 05:47:30 +04:00
|
|
|
__COPYRIGHT(
|
1994-08-17 20:17:19 +04:00
|
|
|
"@(#) Copyright (c) 1992, 1993, 1994\n\
|
1996-05-20 06:01:36 +04:00
|
|
|
The Regents of the University of California. All rights reserved.\n\
|
|
|
|
@(#) Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
|
2002-04-09 05:47:30 +04:00
|
|
|
Keith Bostic. All rights reserved.\n");
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2002-04-09 05:47:30 +04:00
|
|
|
#if 0
|
2001-03-31 15:37:44 +04:00
|
|
|
static const char sccsid[] = "@(#)main.c 10.48 (Berkeley) 10/11/96";
|
2002-04-09 05:47:30 +04:00
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: main.c,v 1.7 2002/04/09 01:47:31 thorpej Exp $");
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#include <sys/types.h>
|
1994-08-17 20:17:19 +04:00
|
|
|
#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>
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#include "common.h"
|
|
|
|
#include "../vi/vi.h"
|
|
|
|
#include "pathnames.h"
|
1994-08-17 20:17:19 +04:00
|
|
|
|
2001-05-01 20:46:11 +04:00
|
|
|
#ifdef DEBUG
|
2001-03-31 15:37:44 +04:00
|
|
|
static void attach __P((GS *));
|
2001-05-01 20:46:11 +04:00
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
static void v_estr __P((char *, int, char *));
|
|
|
|
static int v_obsolete __P((char *, char *[]));
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* editor --
|
|
|
|
* Main editor routine.
|
|
|
|
*
|
|
|
|
* PUBLIC: int editor __P((GS *, int, char *[]));
|
|
|
|
*/
|
1994-08-17 20:17:19 +04:00
|
|
|
int
|
1996-05-20 06:01:36 +04:00
|
|
|
editor(gp, argc, argv)
|
|
|
|
GS *gp;
|
1994-08-17 20:17:19 +04:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
1996-05-20 06:01:36 +04:00
|
|
|
const char *p;
|
|
|
|
EVENT ev;
|
1994-08-17 20:17:19 +04:00
|
|
|
FREF *frp;
|
|
|
|
SCR *sp;
|
1996-05-20 06:01:36 +04:00
|
|
|
size_t len;
|
|
|
|
u_int flags;
|
2001-03-31 15:37:44 +04:00
|
|
|
int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
|
1999-01-08 09:16:54 +03:00
|
|
|
#ifdef GTAGS
|
|
|
|
int gtags = 0;
|
|
|
|
#endif
|
2001-03-31 15:37:44 +04:00
|
|
|
char *tag_f, *wsizearg, path[256];
|
1996-05-20 06:01:36 +04:00
|
|
|
|
|
|
|
/* 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);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Set initial screen type and mode based on the program name. */
|
1994-08-17 20:17:19 +04:00
|
|
|
readonly = 0;
|
1996-05-20 06:01:36 +04:00
|
|
|
if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
|
|
|
|
LF_INIT(SC_EX);
|
1994-08-17 20:17:19 +04:00
|
|
|
else {
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Nview, view are readonly. */
|
|
|
|
if (!strcmp(gp->progname, "nview") ||
|
|
|
|
!strcmp(gp->progname, "view"))
|
1994-08-17 20:17:19 +04:00
|
|
|
readonly = 1;
|
1996-05-20 06:01:36 +04:00
|
|
|
|
|
|
|
/* Vi is the default. */
|
|
|
|
LF_INIT(SC_VI);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert old-style arguments into new-style ones. */
|
1996-05-20 06:01:36 +04:00
|
|
|
if (v_obsolete(gp->progname, argv))
|
|
|
|
return (1);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/* Parse the arguments. */
|
|
|
|
flagchk = '\0';
|
1996-05-20 06:01:36 +04:00
|
|
|
tag_f = wsizearg = NULL;
|
2001-03-31 15:37:44 +04:00
|
|
|
lflag = secure = silent = 0;
|
1996-05-20 06:01:36 +04:00
|
|
|
startup = 1;
|
|
|
|
|
|
|
|
/* Set the file snapshot flag. */
|
|
|
|
F_SET(gp, G_SNAPSHOT);
|
|
|
|
|
1999-01-08 09:16:54 +03:00
|
|
|
#ifdef GTAGS
|
|
|
|
#ifdef DEBUG
|
2001-03-31 15:37:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, "c:D:eFGlRrSsT:t:vw:")) != -1)
|
1999-01-08 09:16:54 +03:00
|
|
|
#else
|
2001-03-31 15:37:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, "c:eFGlRrSst:vw:")) != -1)
|
1999-01-08 09:16:54 +03:00
|
|
|
#endif
|
|
|
|
#else
|
1996-05-20 06:01:36 +04:00
|
|
|
#ifdef DEBUG
|
2001-03-31 15:37:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != -1)
|
1996-05-20 06:01:36 +04:00
|
|
|
#else
|
2001-03-31 15:37:44 +04:00
|
|
|
while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != -1)
|
1996-05-20 06:01:36 +04:00
|
|
|
#endif
|
1999-01-08 09:16:54 +03:00
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'c': /* Run the command. */
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* 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':
|
2001-03-31 15:37:44 +04:00
|
|
|
attach(gp);
|
1996-05-20 06:01:36 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
v_estr(gp->progname, 0,
|
|
|
|
"usage: -D requires s or w argument.");
|
|
|
|
return (1);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
1996-05-20 06:01:36 +04:00
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
case 'e': /* Ex mode. */
|
1996-05-20 06:01:36 +04:00
|
|
|
LF_CLR(SC_VI);
|
|
|
|
LF_SET(SC_EX);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
case 'F': /* No snapshot. */
|
1996-05-20 06:01:36 +04:00
|
|
|
F_CLR(gp, G_SNAPSHOT);
|
|
|
|
break;
|
1999-01-08 09:16:54 +03:00
|
|
|
#ifdef GTAGS
|
2001-03-31 15:37:44 +04:00
|
|
|
case 'G':
|
1999-01-08 09:16:54 +03:00
|
|
|
gtags = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
case 'l': /* Set lisp, showmatch options. */
|
|
|
|
lflag = 1;
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
case 'R': /* Readonly. */
|
|
|
|
readonly = 1;
|
|
|
|
break;
|
|
|
|
case 'r': /* Recover. */
|
1996-05-20 06:01:36 +04:00
|
|
|
if (flagchk == 't') {
|
|
|
|
v_estr(gp->progname, 0,
|
1994-08-17 20:17:19 +04:00
|
|
|
"only one of -r and -t may be specified.");
|
1996-05-20 06:01:36 +04:00
|
|
|
return (1);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
flagchk = 'r';
|
|
|
|
break;
|
2001-03-31 15:37:44 +04:00
|
|
|
case 'S':
|
|
|
|
secure = 1;
|
|
|
|
break;
|
1994-08-17 20:17:19 +04:00
|
|
|
case 's':
|
|
|
|
silent = 1;
|
|
|
|
break;
|
1996-05-20 06:01:36 +04:00
|
|
|
#ifdef DEBUG
|
1994-08-17 20:17:19 +04:00
|
|
|
case 'T': /* Trace. */
|
1996-05-20 06:01:36 +04:00
|
|
|
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);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
1996-05-20 06:01:36 +04:00
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
case 't': /* Tag. */
|
1996-05-20 06:01:36 +04:00
|
|
|
if (flagchk == 'r') {
|
|
|
|
v_estr(gp->progname, 0,
|
1994-08-17 20:17:19 +04:00
|
|
|
"only one of -r and -t may be specified.");
|
1996-05-20 06:01:36 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (flagchk == 't') {
|
|
|
|
v_estr(gp->progname, 0,
|
1994-08-17 20:17:19 +04:00
|
|
|
"only one tag file may be specified.");
|
1996-05-20 06:01:36 +04:00
|
|
|
return (1);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
flagchk = 't';
|
|
|
|
tag_f = optarg;
|
|
|
|
break;
|
|
|
|
case 'v': /* Vi mode. */
|
1996-05-20 06:01:36 +04:00
|
|
|
LF_CLR(SC_EX);
|
|
|
|
LF_SET(SC_VI);
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wsizearg = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
1996-05-20 06:01:36 +04:00
|
|
|
(void)gp->scr_usage();
|
|
|
|
return (1);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* -s option is only meaningful to ex.
|
|
|
|
*
|
1994-08-17 20:17:19 +04:00
|
|
|
* If not reading from a terminal, it's like -s was specified.
|
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
if (silent && !LF_ISSET(SC_EX)) {
|
|
|
|
v_estr(gp->progname, 0, "-s option is only applicable to ex.");
|
|
|
|
goto err;
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
2001-03-31 15:37:44 +04:00
|
|
|
if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
|
1996-05-20 06:01:36 +04:00
|
|
|
silent = 1;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* !!!
|
1996-05-20 06:01:36 +04:00
|
|
|
* Everything we do until we go interactive is done in ex mode.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
if (screen_init(gp, NULL, &sp)) {
|
1994-08-17 20:17:19 +04:00
|
|
|
if (sp != NULL)
|
1996-05-20 06:01:36 +04:00
|
|
|
CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
|
1994-08-17 20:17:19 +04:00
|
|
|
goto err;
|
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
F_SET(sp, SC_EX);
|
|
|
|
CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
if (v_key_init(sp)) /* Special key initialization. */
|
|
|
|
goto err;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
{ int oargs[5], *oargp = oargs;
|
|
|
|
/* Command-line options. */
|
1999-01-08 09:16:54 +03:00
|
|
|
#ifdef GTAGS
|
|
|
|
if (gtags)
|
|
|
|
*oargp++ = O_GTAGSMODE;
|
|
|
|
#endif
|
1996-05-20 06:01:36 +04:00
|
|
|
if (lflag) {
|
|
|
|
*oargp++ = O_LISP;
|
|
|
|
*oargp++ = O_SHOWMATCH;
|
|
|
|
}
|
2001-03-31 15:37:44 +04:00
|
|
|
if (readonly)
|
|
|
|
*oargp++ = O_READONLY;
|
|
|
|
if (secure)
|
|
|
|
*oargp++ = O_SECURE;
|
1996-05-20 06:01:36 +04:00
|
|
|
*oargp = -1; /* Options initialization. */
|
|
|
|
if (opts_init(sp, oargs))
|
1994-08-17 20:17:19 +04:00
|
|
|
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;
|
1996-05-20 06:01:36 +04:00
|
|
|
(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);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
|
|
|
|
sp->cols = O_VAL(sp, O_COLUMNS);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
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;
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
1996-05-20 06:01:36 +04:00
|
|
|
* Initialize the default ^D, ^U scrolling value here, after the
|
|
|
|
* user has had every opportunity to set the window option.
|
1994-08-17 20:17:19 +04:00
|
|
|
*
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* 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));
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Open a tag file if specified. */
|
|
|
|
if (tag_f != NULL && ex_tag_first(sp, tag_f))
|
1994-08-17 20:17:19 +04:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
|
|
|
if (*argv != NULL) {
|
1996-05-20 06:01:36 +04:00
|
|
|
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);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
sp->argv = sp->cargv = argv;
|
1996-05-20 06:01:36 +04:00
|
|
|
F_SET(sp, SC_ARGNOFREE);
|
1994-08-17 20:17:19 +04:00
|
|
|
if (flagchk == 'r')
|
1996-05-20 06:01:36 +04:00
|
|
|
F_SET(sp, SC_ARGRECOVER);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
if (file_init(sp, frp, NULL, 0))
|
|
|
|
goto err;
|
1996-05-20 06:01:36 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
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))
|
1994-08-17 20:17:19 +04:00
|
|
|
goto err;
|
1996-05-20 06:01:36 +04:00
|
|
|
if (ev.e_event == E_INTERRUPT ||
|
|
|
|
ev.e_event == E_CHARACTER &&
|
|
|
|
(ev.e_value == K_CR || ev.e_value == K_NL))
|
1994-08-17 20:17:19 +04:00
|
|
|
break;
|
1996-05-20 06:01:36 +04:00
|
|
|
(void)gp->scr_bell(sp);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Switch into the right editor, regardless. */
|
|
|
|
F_CLR(sp, SC_EX | SC_VI);
|
2001-03-31 15:37:44 +04:00
|
|
|
F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* Main edit loop. Vi handles split screens itself, we only return
|
|
|
|
* here when switching editor modes or restarting the screen.
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
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);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
return (rval);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* v_end --
|
|
|
|
* End the program, discarding screens and most of the global area.
|
|
|
|
*
|
|
|
|
* PUBLIC: void v_end __P((GS *));
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
void
|
|
|
|
v_end(gp)
|
1994-08-17 20:17:19 +04:00
|
|
|
GS *gp;
|
1996-05-20 06:01:36 +04:00
|
|
|
{
|
|
|
|
MSGS *mp;
|
|
|
|
SCR *sp;
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* 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);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#ifdef HAVE_PERL_INTERP
|
|
|
|
perl_end(gp);
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#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);
|
|
|
|
}
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Free key input queue. */
|
|
|
|
if (gp->i_event != NULL)
|
|
|
|
free(gp->i_event);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Free cut buffers. */
|
|
|
|
cut_close(gp);
|
|
|
|
|
|
|
|
/* Free map sequences. */
|
|
|
|
seq_close(gp);
|
1994-08-17 20:17:19 +04:00
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Free default buffer storage. */
|
1994-08-17 20:17:19 +04:00
|
|
|
(void)text_lfree(&gp->dcb_store.textq);
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Close message catalogs. */
|
|
|
|
msg_close(gp);
|
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
|
|
|
|
/* Ring the bell if scheduled. */
|
|
|
|
if (F_ISSET(gp, G_BELLSCHED))
|
|
|
|
(void)fprintf(stderr, "\07"); /* \a */
|
|
|
|
|
|
|
|
/*
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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).
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
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
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
|
|
|
|
/* Free any temporary space. */
|
|
|
|
if (gp->tmp_bp != NULL)
|
|
|
|
free(gp->tmp_bp);
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
#if defined(DEBUG)
|
1996-05-20 06:01:36 +04:00
|
|
|
/* Close debugging file descriptor. */
|
|
|
|
if (gp->tracefp != NULL)
|
|
|
|
(void)fclose(gp->tracefp);
|
|
|
|
#endif
|
2001-03-31 15:37:44 +04:00
|
|
|
#endif
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
1996-05-20 06:01:36 +04:00
|
|
|
/*
|
|
|
|
* v_obsolete --
|
|
|
|
* Convert historic arguments into something getopt(3) will like.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
v_obsolete(name, argv)
|
|
|
|
char *name, *argv[];
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate old style arguments into something getopt will like.
|
1996-05-20 06:01:36 +04:00
|
|
|
* Make sure it's not text space memory, because ex modifies the
|
1994-08-17 20:17:19 +04:00
|
|
|
* strings.
|
|
|
|
* Change "+" into "-c$".
|
|
|
|
* Change "+<anything else>" into "-c<anything else>".
|
|
|
|
* Change "-" into "-s"
|
1996-05-20 06:01:36 +04:00
|
|
|
* 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".
|
1994-08-17 20:17:19 +04:00
|
|
|
*/
|
1996-05-20 06:01:36 +04:00
|
|
|
while (*++argv && strcmp(argv[0], "--"))
|
1994-08-17 20:17:19 +04:00
|
|
|
if (argv[0][0] == '+') {
|
|
|
|
if (argv[0][1] == '\0') {
|
|
|
|
MALLOC_NOMSG(NULL, argv[0], char *, 4);
|
|
|
|
if (argv[0] == NULL)
|
1996-05-20 06:01:36 +04:00
|
|
|
goto nomem;
|
1994-08-17 20:17:19 +04:00
|
|
|
(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)
|
1996-05-20 06:01:36 +04:00
|
|
|
goto nomem;
|
1994-08-17 20:17:19 +04:00
|
|
|
argv[0][0] = '-';
|
|
|
|
argv[0][1] = 'c';
|
|
|
|
(void)strcpy(argv[0] + 2, p + 1);
|
|
|
|
}
|
1996-05-20 06:01:36 +04:00
|
|
|
} 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);
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|
|
|
|
|
2001-03-31 15:37:44 +04:00
|
|
|
#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
|
|
|
|
|
1994-08-17 20:17:19 +04:00
|
|
|
static void
|
1996-05-20 06:01:36 +04:00
|
|
|
v_estr(name, eno, msg)
|
|
|
|
char *name, *msg;
|
|
|
|
int eno;
|
1994-08-17 20:17:19 +04:00
|
|
|
{
|
1996-05-20 06:01:36 +04:00
|
|
|
(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");
|
1994-08-17 20:17:19 +04:00
|
|
|
}
|