1998-01-09 11:03:16 +03:00
|
|
|
/* $NetBSD: ex_edit.c,v 1.7 1998/01/09 08:07:47 perry Exp $ */
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-05-20 07:47:00 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
#ifndef lint
|
1996-05-20 07:47:00 +04:00
|
|
|
static const char sccsid[] = "@(#)ex_edit.c 10.10 (Berkeley) 4/27/96";
|
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 <stdio.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "../common/common.h"
|
|
|
|
#include "../vi/vi.h"
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
static int ex_N_edit __P((SCR *, EXCMD *, FREF *, int));
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_edit -- :e[dit][!] [+cmd] [file]
|
1996-05-20 07:47:00 +04:00
|
|
|
* :ex[!] [+cmd] [file]
|
1994-01-24 08:52:58 +03:00
|
|
|
* :vi[sual][!] [+cmd] [file]
|
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* Edit a file; if none specified, re-edit the current file. The third
|
1994-01-24 08:52:58 +03:00
|
|
|
* form of the command can only be executed while in vi mode. See the
|
|
|
|
* hack in ex.c:ex_cmd().
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historic vi didn't permit the '+' command form without specifying
|
1996-05-20 07:47:00 +04:00
|
|
|
* a file name as well. This seems unreasonable, so we support it
|
|
|
|
* regardless.
|
|
|
|
*
|
|
|
|
* PUBLIC: int ex_edit __P((SCR *, EXCMD *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_edit(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
|
|
|
FREF *frp;
|
1996-05-20 07:47:00 +04:00
|
|
|
int attach, setalt;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
switch (cmdp->argc) {
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* If the name has been changed, we edit that file, not the
|
1996-05-20 07:47:00 +04:00
|
|
|
* original name. If the user was editing a temporary file
|
|
|
|
* (or wasn't editing any file), create another one. The
|
|
|
|
* reason for not reusing temporary files is that there is
|
|
|
|
* special exit processing of them, and reuse is tricky.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
frp = sp->frp;
|
|
|
|
if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
|
1994-08-17 20:35:35 +04:00
|
|
|
if ((frp = file_add(sp, NULL)) == NULL)
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1996-05-20 07:47:00 +04:00
|
|
|
attach = 0;
|
|
|
|
} else
|
|
|
|
attach = 1;
|
|
|
|
setalt = 0;
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
1996-05-20 07:47:00 +04:00
|
|
|
if ((frp = file_add(sp, cmdp->argv[0]->bp)) == NULL)
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1996-05-20 07:47:00 +04:00
|
|
|
attach = 0;
|
|
|
|
setalt = 1;
|
|
|
|
set_alt_name(sp, cmdp->argv[0]->bp);
|
1994-01-24 08:52:58 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
if (F_ISSET(cmdp, E_NEWSCREEN))
|
|
|
|
return (ex_N_edit(sp, cmdp, frp, attach));
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
/*
|
|
|
|
* Check for modifications.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
|
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* Switch files. */
|
1996-05-20 07:47:00 +04:00
|
|
|
if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
|
|
|
|
(FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
F_SET(sp, SC_FSWITCH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_N_edit --
|
|
|
|
* New screen version of ex_edit.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ex_N_edit(sp, cmdp, frp, attach)
|
|
|
|
SCR *sp;
|
|
|
|
EXCMD *cmdp;
|
|
|
|
FREF *frp;
|
|
|
|
int attach;
|
|
|
|
{
|
|
|
|
SCR *new;
|
|
|
|
|
|
|
|
/* Get a new screen. */
|
|
|
|
if (screen_init(sp->gp, sp, &new))
|
|
|
|
return (1);
|
|
|
|
if (vs_split(sp, new, 0)) {
|
|
|
|
(void)screen_end(new);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
1996-05-20 07:47:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a backing file. */
|
|
|
|
if (attach) {
|
|
|
|
/* Copy file state, keep the screen and cursor the same. */
|
|
|
|
new->ep = sp->ep;
|
|
|
|
++new->ep->refcnt;
|
|
|
|
|
|
|
|
new->frp = frp;
|
|
|
|
new->frp->flags = sp->frp->flags;
|
|
|
|
|
|
|
|
new->lno = sp->lno;
|
|
|
|
new->cno = sp->cno;
|
|
|
|
} else if (file_init(new, frp, NULL,
|
|
|
|
(FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
|
|
|
|
(void)vs_discard(new, NULL);
|
|
|
|
(void)screen_end(new);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the argument list. */
|
|
|
|
new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
|
|
|
|
|
|
|
|
/* Set up the switch. */
|
|
|
|
sp->nextdisp = new;
|
|
|
|
F_SET(sp, SC_SSWITCH);
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
return (0);
|
|
|
|
}
|