1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1991, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* 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
|
1994-08-18 00:12:00 +04:00
|
|
|
static char sccsid[] = "@(#)ex_args.c 8.29 (Berkeley) 8/17/94";
|
1994-01-24 08:52:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/time.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <bitstring.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <errno.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include <db.h>
|
|
|
|
#include <regex.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
#include "vi.h"
|
|
|
|
#include "excmd.h"
|
|
|
|
|
|
|
|
/*
|
1994-08-17 20:35:35 +04:00
|
|
|
* ex_next -- :next [+cmd] [files]
|
1994-01-24 08:52:58 +03:00
|
|
|
* Edit the next file, optionally setting the list of files.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* The :next command behaved differently from the :rewind command in
|
|
|
|
* historic vi. See nvi/docs/autowrite for details, but the basic
|
|
|
|
* idea was that it ignored the force flag if the autowrite flag was
|
|
|
|
* set. This implementation handles them all identically.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ex_next(sp, ep, cmdp)
|
|
|
|
SCR *sp;
|
|
|
|
EXF *ep;
|
|
|
|
EXCMDARG *cmdp;
|
|
|
|
{
|
1994-08-17 20:35:35 +04:00
|
|
|
ARGS **argv, **pc;
|
1994-01-24 08:52:58 +03:00
|
|
|
FREF *frp;
|
1994-08-17 20:35:35 +04:00
|
|
|
int noargs;
|
|
|
|
char **ap;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (file_m1(sp, ep, F_ISSET(cmdp, E_FORCE), FS_ALL | FS_POSSIBLE))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the first argument is a plus sign, '+', it's an initial
|
|
|
|
* ex command.
|
|
|
|
*/
|
|
|
|
argv = cmdp->argv;
|
|
|
|
if (cmdp->argc && argv[0]->bp[0] == '+') {
|
|
|
|
--cmdp->argc;
|
|
|
|
pc = argv++;
|
|
|
|
} else
|
|
|
|
pc = NULL;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/* Any other arguments are a replacement file list. */
|
1994-01-24 08:52:58 +03:00
|
|
|
if (cmdp->argc) {
|
1994-08-17 20:35:35 +04:00
|
|
|
/* Free the current list. */
|
|
|
|
if (!F_ISSET(sp, S_ARGNOFREE) && sp->argv != NULL) {
|
|
|
|
for (ap = sp->argv; *ap != NULL; ++ap)
|
|
|
|
free(*ap);
|
|
|
|
free(sp->argv);
|
|
|
|
}
|
|
|
|
F_CLR(sp, S_ARGNOFREE | S_ARGRECOVER);
|
|
|
|
sp->cargv = NULL;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/* Create a new list. */
|
|
|
|
CALLOC_RET(sp,
|
|
|
|
sp->argv, char **, cmdp->argc + 1, sizeof(char *));
|
|
|
|
for (ap = sp->argv,
|
|
|
|
argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv)
|
|
|
|
if ((*ap =
|
|
|
|
v_strdup(sp, argv[0]->bp, argv[0]->len)) == NULL)
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
*ap = NULL;
|
1994-03-28 08:28:20 +04:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/* Switch to the first one. */
|
|
|
|
sp->cargv = sp->argv;
|
|
|
|
if ((frp = file_add(sp, *sp->cargv)) == NULL)
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
noargs = 0;
|
|
|
|
} else {
|
|
|
|
if (sp->cargv == NULL || sp->cargv[1] == NULL) {
|
|
|
|
msgq(sp, M_ERR, "No more files to edit");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if ((frp = file_add(sp, sp->cargv[1])) == NULL)
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
if (F_ISSET(sp, S_ARGRECOVER))
|
|
|
|
F_SET(frp, FR_RECOVER);
|
|
|
|
noargs = 1;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1994-08-17 20:35:35 +04:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
|
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
if (noargs)
|
|
|
|
++sp->cargv;
|
|
|
|
|
|
|
|
/* Push the initial command onto the stack. */
|
|
|
|
if (pc != NULL)
|
|
|
|
if (IN_EX_MODE(sp))
|
|
|
|
(void)term_push(sp, pc[0]->bp, pc[0]->len, 0);
|
|
|
|
else if (IN_VI_MODE(sp)) {
|
|
|
|
(void)term_push(sp, "\n", 1, 0);
|
|
|
|
(void)term_push(sp, pc[0]->bp, pc[0]->len, 0);
|
|
|
|
(void)term_push(sp, ":", 1, 0);
|
|
|
|
(void)file_lline(sp, sp->ep, &sp->frp->lno);
|
|
|
|
F_SET(sp->frp, FR_CURSORSET);
|
|
|
|
}
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
F_SET(sp, S_FSWITCH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_prev -- :prev
|
|
|
|
* Edit the previous file.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ex_prev(sp, ep, cmdp)
|
|
|
|
SCR *sp;
|
|
|
|
EXF *ep;
|
|
|
|
EXCMDARG *cmdp;
|
|
|
|
{
|
|
|
|
FREF *frp;
|
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (file_m1(sp, ep, F_ISSET(cmdp, E_FORCE), FS_ALL | FS_POSSIBLE))
|
|
|
|
return (1);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->cargv == sp->argv) {
|
|
|
|
msgq(sp, M_ERR, "No previous files to edit");
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1994-08-17 20:35:35 +04:00
|
|
|
if ((frp = file_add(sp, sp->cargv[-1])) == NULL)
|
|
|
|
return (1);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
|
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
|
|
|
|
--sp->cargv;
|
1994-01-24 08:52:58 +03:00
|
|
|
F_SET(sp, S_FSWITCH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_rew -- :rew
|
|
|
|
* Re-edit the list of files.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ex_rew(sp, ep, cmdp)
|
|
|
|
SCR *sp;
|
|
|
|
EXF *ep;
|
|
|
|
EXCMDARG *cmdp;
|
|
|
|
{
|
1994-08-17 20:35:35 +04:00
|
|
|
FREF *frp;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Historic practice -- you can rewind to the current file.
|
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->argv == NULL) {
|
|
|
|
msgq(sp, M_ERR, "No previous files to rewind");
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (file_m1(sp, ep, F_ISSET(cmdp, E_FORCE), FS_ALL | FS_POSSIBLE))
|
|
|
|
return (1);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
1994-08-17 20:35:35 +04:00
|
|
|
* Historic practice, start at the beginning of the file.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
1994-08-17 20:35:35 +04:00
|
|
|
for (frp = sp->frefq.cqh_first;
|
|
|
|
frp != (FREF *)&sp->frefq; frp = frp->q.cqe_next)
|
|
|
|
F_CLR(frp, FR_CURSORSET | FR_FNONBLANK);
|
|
|
|
|
|
|
|
/* Switch to the first one. */
|
|
|
|
sp->cargv = sp->argv;
|
|
|
|
if ((frp = file_add(sp, *sp->cargv)) == NULL)
|
|
|
|
return (1);
|
1994-01-24 08:52:58 +03:00
|
|
|
if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
|
|
|
|
return (1);
|
1994-08-17 20:35:35 +04:00
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
F_SET(sp, S_FSWITCH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_args -- :args
|
|
|
|
* Display the list of files.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ex_args(sp, ep, cmdp)
|
|
|
|
SCR *sp;
|
|
|
|
EXF *ep;
|
|
|
|
EXCMDARG *cmdp;
|
|
|
|
{
|
1994-08-17 20:35:35 +04:00
|
|
|
int cnt, col, len, sep;
|
|
|
|
char **ap;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (sp->argv == NULL) {
|
|
|
|
(void)ex_printf(EXCOOKIE, "No file list to display.\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
col = len = sep = 0;
|
1994-08-17 20:35:35 +04:00
|
|
|
for (cnt = 1, ap = sp->argv; *ap != NULL; ++ap) {
|
|
|
|
col += len = strlen(*ap) + sep + (ap == sp->cargv ? 2 : 0);
|
1994-01-24 08:52:58 +03:00
|
|
|
if (col >= sp->cols - 1) {
|
|
|
|
col = len;
|
|
|
|
sep = 0;
|
|
|
|
(void)ex_printf(EXCOOKIE, "\n");
|
|
|
|
} else if (cnt != 1) {
|
|
|
|
sep = 1;
|
|
|
|
(void)ex_printf(EXCOOKIE, " ");
|
|
|
|
}
|
|
|
|
++cnt;
|
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
if (ap == sp->cargv)
|
|
|
|
(void)ex_printf(EXCOOKIE, "[%s]", *ap);
|
|
|
|
else
|
|
|
|
(void)ex_printf(EXCOOKIE, "%s", *ap);
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1994-08-17 20:35:35 +04:00
|
|
|
(void)ex_printf(EXCOOKIE, "\n");
|
1994-01-24 08:52:58 +03:00
|
|
|
return (0);
|
|
|
|
}
|