* Copyright (c) 1992, 1993
* 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
* 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
static char copyright
[] =
"@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 6/9/93";
static void msgflush
__P((GS
*));
static void obsolete
__P((char *[]));
static void reset
__P((GS
*));
static void usage
__P((void));
GS
*__global_list
; /* GLOBAL: List of screens. */
static int reenter
; /* STATIC: Re-entrancy check. */
char *excmdarg
, *errf
, *myname
, *p
, *rfname
, *tag
;
/* Stop if indirecting through a NULL pointer. */
/* Figure out the program name. */
if ((myname
= strrchr(*argv
, '/')) == NULL
)
/* Build and initialize the GS structure. */
if ((gp
= malloc(sizeof(GS
))) == NULL
)
memset(gp
, 0, sizeof(GS
));
HDR_INIT(gp
->scrhdr
, next
, prev
);
HDR_INIT(gp
->exfhdr
, next
, prev
);
if (tcgetattr(STDIN_FILENO
, &gp
->original_termios
))
/* Build and initialize the first/current screen. */
if ((sp
= malloc(sizeof(SCR
))) == NULL
)
sp
->gp
= gp
; /* All screens point to the GS structure. */
HDR_INIT(sp
->seqhdr
, next
, prev
);
/* No need to block SIGALRM yet. */
HDR_APPEND(sp
, &gp
->scrhdr
, next
, prev
, SCR
);
if (set_window_size(sp
, 0)) /* Set the window size. */
if (opts_init(sp
)) /* Options initialization. */
* Must follow options and sequence map initializations.
if (digraph_init(sp
)) /* Digraph initialization. */
/* Set screen mode based on the program name. */
if (!strcmp(myname
, "ex") || !strcmp(myname
, "nex"))
else if (!strcmp(myname
, "view")) {
/* Convert old-style arguments into new-style ones. */
/* Parse the arguments. */
excmdarg
= errf
= rfname
= tag
= NULL
;
while ((ch
= getopt(argc
, argv
, "c:elmRr:sT:t:v")) != EOF
)
case 'c': /* Run the command. */
case 'm': /* Error list. */
case 'R': /* Readonly. */
case 's': /* No snapshot. */
if ((gp
->tracefp
= fopen(optarg
, "w")) == NULL
)
(void)fprintf(gp
->tracefp
,
"\n===\ntrace: open %s\n", optarg
);
/* Only permit one file specification. */
err(1, "only one of -r and -t is permitted.");
* Source the system, environment, ~user and local .exrc values.
* If the environment exists, vi historically doesn't check ~user.
(void)ex_cfile(sp
, NULL
, _PATH_SYSEXRC
, 0);
/* Source the EXINIT environment variable. */
if ((p
= getenv("EXINIT")) != NULL
)
if ((p
= strdup(p
)) == NULL
) {
msgq(sp
, M_ERR
, "Error: %s", strerror(errno
));
(void)ex_cstring(sp
, NULL
, p
, strlen(p
));
else if ((p
= getenv("HOME")) != NULL
&& *p
) {
(void)snprintf(path
, sizeof(path
), "%s/.exrc", p
);
(void)ex_cfile(sp
, NULL
, path
, 0);
(void)ex_cfile(sp
, NULL
, _PATH_EXRC
, 0);
/* Any remaining arguments are file names. */
file_set(sp
, argc
, argv
);
/* Use an error list file, recovery file or tag file if specified. */
SETCMDARG(cmd
, C_ERRLIST
, 0, OOBLNO
, 0, 0, errf
);
if (ex_errlist(sp
, NULL
, &cmd
))
if (ex_tagfirst(sp
, tag
))
} else if (rfname
!= NULL
) {
if ((sp
->ep
= rcv_read(sp
, rfname
)) == NULL
)
} else if ((sp
->ep
= file_start(sp
, file_first(sp
, 1), NULL
)) == NULL
)
/* Do commands from the command line. */
(void)ex_cstring(sp
, sp
->ep
, excmdarg
, strlen(excmdarg
));
* The commands we executed probably positioned the file. This
* isn't a wonderful test, but it's probably fairly safe.
if ((sp
->lno
!= 1 || sp
->cno
!= 0) && F_ISSET(sp
->ep
, F_NOSETPOS
))
F_CLR(sp
->ep
, F_NOSETPOS
);
switch(F_ISSET(sp
, S_MODE_EX
| S_MODE_VI
)) {
if (sex(sp
, sp
->ep
, &sp
))
if (svi(sp
, sp
->ep
, &sp
))
* Two error paths. The first means that something failed before
* we called a screen routine. Swap the message pointers between
* the SCR and the GS, so messages get displayed. The second is
* something failed in a screen. NOTE: sp may be GONE when the
* screen returns, so only the gp can be trusted.
err1
: gp
->msgp
= sp
->msgp
;
/* Turn off the recovery timer. */
/* Reset anything that needs resetting. */
/* Flush any left-over error messages. */
if (tcsetattr(STDIN_FILENO
, TCSADRAIN
, &gp
->original_termios
))
* Reset any changed global state.
if (gp
->flags
& G_SETMODE
) /* O_MESG */
if ((tty
= ttyname(STDERR_FILENO
)) == NULL
)
else if (chmod(tty
, gp
->origmode
) < 0)
* Flush any remaining messages.
if (F_ISSET(gp
, S_BELLSCHED
))
(void)fprintf(stderr
, "\07"); /* \a */
/* Display the messages. */
mp
!= NULL
&& !(F_ISSET(mp
, M_EMPTY
)); mp
= mp
->next
)
(void)fprintf(stderr
, "%.*s\n", (int)mp
->len
, mp
->mbuf
);
* Translate old style arguments into something getopt will like.
* Make sure it's not text space memory, because ex changes the
* Change "+/command" into "-ccommand".
* Change "+" and "+$" into "-c$".
* Change "+[0-9]*" into "-c[0-9]".
for (myname
= argv
[0]; *++argv
;)
if (argv
[0][1] == '\0' || argv
[0][1] == '$') {
if ((argv
[0] = malloc(4)) == NULL
)
memmove(argv
[0], "-c$", 4);
} else if (argv
[0][1] == '/') {
if ((argv
[0] = malloc(len
+ 3)) == NULL
)
(void)strcpy(argv
[0] + 2, p
+ 1);
} else if (isdigit(argv
[0][1])) {
if ((argv
[0] = malloc(len
+ 2)) == NULL
)
(void)strcpy(argv
[0] + 2, p
+ 1);
else if (argv
[0][0] == '-' &&
argv
[0][1] == 'r' && argv
[1] == NULL
)
"usage: vi [-eRrsv] [-c command] [-m file] [-t tag]\n");