xref: /minix3/external/bsd/nvi/dist/ex/ex_edit.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_edit.c,v 1.7 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_edit.c,v 10.14 2001/08/28 13:29:15 skimo Exp  (Berkeley) Date: 2001/08/28 13:29:15 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_edit.c,v 1.7 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/time.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc #include "../common/common.h"
3484d9c625SLionel Sambuc #include "../vi/vi.h"
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc static int ex_N_edit __P((SCR *, EXCMD *, FREF *, int));
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc /*
3984d9c625SLionel Sambuc  * ex_edit --	:e[dit][!] [+cmd] [file]
4084d9c625SLionel Sambuc  *		:ex[!] [+cmd] [file]
4184d9c625SLionel Sambuc  *		:vi[sual][!] [+cmd] [file]
4284d9c625SLionel Sambuc  *
4384d9c625SLionel Sambuc  * Edit a file; if none specified, re-edit the current file.  The third
4484d9c625SLionel Sambuc  * form of the command can only be executed while in vi mode.  See the
4584d9c625SLionel Sambuc  * hack in ex.c:ex_cmd().
4684d9c625SLionel Sambuc  *
4784d9c625SLionel Sambuc  * !!!
4884d9c625SLionel Sambuc  * Historic vi didn't permit the '+' command form without specifying
4984d9c625SLionel Sambuc  * a file name as well.  This seems unreasonable, so we support it
5084d9c625SLionel Sambuc  * regardless.
5184d9c625SLionel Sambuc  *
5284d9c625SLionel Sambuc  * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
5384d9c625SLionel Sambuc  */
5484d9c625SLionel Sambuc int
ex_edit(SCR * sp,EXCMD * cmdp)5584d9c625SLionel Sambuc ex_edit(SCR *sp, EXCMD *cmdp)
5684d9c625SLionel Sambuc {
5784d9c625SLionel Sambuc 	FREF *frp;
5884d9c625SLionel Sambuc 	int attach, setalt;
5984d9c625SLionel Sambuc 	const char *np;
6084d9c625SLionel Sambuc 	size_t nlen;
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc 	switch (cmdp->argc) {
6384d9c625SLionel Sambuc 	case 0:
6484d9c625SLionel Sambuc 		/*
6584d9c625SLionel Sambuc 		 * If the name has been changed, we edit that file, not the
6684d9c625SLionel Sambuc 		 * original name.  If the user was editing a temporary file
6784d9c625SLionel Sambuc 		 * (or wasn't editing any file), create another one.  The
6884d9c625SLionel Sambuc 		 * reason for not reusing temporary files is that there is
6984d9c625SLionel Sambuc 		 * special exit processing of them, and reuse is tricky.
7084d9c625SLionel Sambuc 		 */
7184d9c625SLionel Sambuc 		frp = sp->frp;
7284d9c625SLionel Sambuc 		if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
7384d9c625SLionel Sambuc 			if ((frp = file_add(sp, NULL)) == NULL)
7484d9c625SLionel Sambuc 				return (1);
7584d9c625SLionel Sambuc 			attach = 0;
7684d9c625SLionel Sambuc 		} else
7784d9c625SLionel Sambuc 			attach = 1;
7884d9c625SLionel Sambuc 		setalt = 0;
7984d9c625SLionel Sambuc 		break;
8084d9c625SLionel Sambuc 	case 1:
8184d9c625SLionel Sambuc 		INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
8284d9c625SLionel Sambuc 			 np, nlen);
8384d9c625SLionel Sambuc 		if ((frp = file_add(sp, np)) == NULL)
8484d9c625SLionel Sambuc 			return (1);
8584d9c625SLionel Sambuc 		attach = 0;
8684d9c625SLionel Sambuc 		setalt = 1;
8784d9c625SLionel Sambuc 		set_alt_name(sp, np);
8884d9c625SLionel Sambuc 		break;
8984d9c625SLionel Sambuc 	default:
9084d9c625SLionel Sambuc 		abort();
9184d9c625SLionel Sambuc 	}
9284d9c625SLionel Sambuc 
9384d9c625SLionel Sambuc 	if (F_ISSET(cmdp, E_NEWSCREEN) || cmdp->cmd == &cmds[C_VSPLIT])
9484d9c625SLionel Sambuc 		return (ex_N_edit(sp, cmdp, frp, attach));
9584d9c625SLionel Sambuc 
9684d9c625SLionel Sambuc 	/*
9784d9c625SLionel Sambuc 	 * Check for modifications.
9884d9c625SLionel Sambuc 	 *
9984d9c625SLionel Sambuc 	 * !!!
10084d9c625SLionel Sambuc 	 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
10184d9c625SLionel Sambuc 	 */
10284d9c625SLionel Sambuc 	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
10384d9c625SLionel Sambuc 		return (1);
10484d9c625SLionel Sambuc 
10584d9c625SLionel Sambuc 	/* Switch files. */
10684d9c625SLionel Sambuc 	if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
10784d9c625SLionel Sambuc 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
10884d9c625SLionel Sambuc 		return (1);
10984d9c625SLionel Sambuc 
11084d9c625SLionel Sambuc 	F_SET(sp, SC_FSWITCH);
11184d9c625SLionel Sambuc 	return (0);
11284d9c625SLionel Sambuc }
11384d9c625SLionel Sambuc 
11484d9c625SLionel Sambuc /*
11584d9c625SLionel Sambuc  * ex_N_edit --
11684d9c625SLionel Sambuc  *	New screen version of ex_edit.
11784d9c625SLionel Sambuc  */
11884d9c625SLionel Sambuc static int
ex_N_edit(SCR * sp,EXCMD * cmdp,FREF * frp,int attach)11984d9c625SLionel Sambuc ex_N_edit(SCR *sp, EXCMD *cmdp, FREF *frp, int attach)
12084d9c625SLionel Sambuc {
12184d9c625SLionel Sambuc 	SCR *new;
12284d9c625SLionel Sambuc 
12384d9c625SLionel Sambuc 	/* Get a new screen. */
12484d9c625SLionel Sambuc 	if (screen_init(sp->gp, sp, &new))
12584d9c625SLionel Sambuc 		return (1);
12684d9c625SLionel Sambuc 	if ((cmdp->cmd == &cmds[C_VSPLIT] && vs_vsplit(sp, new)) ||
12784d9c625SLionel Sambuc 	    (cmdp->cmd != &cmds[C_VSPLIT] && vs_split(sp, new, 0))) {
128*0a6a1f1dSLionel Sambuc 		(void)screen_fini(new);
12984d9c625SLionel Sambuc 		return (1);
13084d9c625SLionel Sambuc 	}
13184d9c625SLionel Sambuc 
13284d9c625SLionel Sambuc 	/* Get a backing file. */
13384d9c625SLionel Sambuc 	if (attach) {
13484d9c625SLionel Sambuc 		/* Copy file state, keep the screen and cursor the same. */
13584d9c625SLionel Sambuc 		new->ep = sp->ep;
13684d9c625SLionel Sambuc 		++new->ep->refcnt;
13784d9c625SLionel Sambuc 		TAILQ_INSERT_HEAD(&new->ep->scrq, new, eq);
13884d9c625SLionel Sambuc 
13984d9c625SLionel Sambuc 		new->frp = frp;
14084d9c625SLionel Sambuc 		new->frp->flags = sp->frp->flags;
14184d9c625SLionel Sambuc 
14284d9c625SLionel Sambuc 		new->lno = sp->lno;
14384d9c625SLionel Sambuc 		new->cno = sp->cno;
14484d9c625SLionel Sambuc 	} else if (file_init(new, frp, NULL,
14584d9c625SLionel Sambuc 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
14684d9c625SLionel Sambuc 		(void)vs_discard(new, NULL);
14784d9c625SLionel Sambuc 		(void)screen_end(new);
14884d9c625SLionel Sambuc 		return (1);
14984d9c625SLionel Sambuc 	}
15084d9c625SLionel Sambuc 
15184d9c625SLionel Sambuc 	/* Create the argument list. */
15284d9c625SLionel Sambuc 	new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc 	/* Set up the switch. */
15584d9c625SLionel Sambuc 	sp->nextdisp = new;
15684d9c625SLionel Sambuc 	F_SET(sp, SC_SSWITCH);
15784d9c625SLionel Sambuc 
15884d9c625SLionel Sambuc 	return (0);
15984d9c625SLionel Sambuc }
160