xref: /openbsd/usr.bin/vi/ex/ex_edit.c (revision 043fbe51)
1 /*	$OpenBSD: ex_edit.c,v 1.5 2009/10/27 23:59:47 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17 
18 #include <bitstring.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "../common/common.h"
26 #include "../vi/vi.h"
27 
28 static int ex_N_edit(SCR *, EXCMD *, FREF *, int);
29 
30 /*
31  * ex_edit --	:e[dit][!] [+cmd] [file]
32  *		:ex[!] [+cmd] [file]
33  *		:vi[sual][!] [+cmd] [file]
34  *
35  * Edit a file; if none specified, re-edit the current file.  The third
36  * form of the command can only be executed while in vi mode.  See the
37  * hack in ex.c:ex_cmd().
38  *
39  * !!!
40  * Historic vi didn't permit the '+' command form without specifying
41  * a file name as well.  This seems unreasonable, so we support it
42  * regardless.
43  *
44  * PUBLIC: int ex_edit(SCR *, EXCMD *);
45  */
46 int
47 ex_edit(sp, cmdp)
48 	SCR *sp;
49 	EXCMD *cmdp;
50 {
51 	FREF *frp;
52 	int attach, setalt;
53 
54 	switch (cmdp->argc) {
55 	case 0:
56 		/*
57 		 * If the name has been changed, we edit that file, not the
58 		 * original name.  If the user was editing a temporary file
59 		 * (or wasn't editing any file), create another one.  The
60 		 * reason for not reusing temporary files is that there is
61 		 * special exit processing of them, and reuse is tricky.
62 		 */
63 		frp = sp->frp;
64 		if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
65 			if ((frp = file_add(sp, NULL)) == NULL)
66 				return (1);
67 			attach = 0;
68 		} else
69 			attach = 1;
70 		setalt = 0;
71 		break;
72 	case 1:
73 		if ((frp = file_add(sp, cmdp->argv[0]->bp)) == NULL)
74 			return (1);
75 		attach = 0;
76 		setalt = 1;
77 		set_alt_name(sp, cmdp->argv[0]->bp);
78 		break;
79 	default:
80 		abort();
81 	}
82 
83 	if (F_ISSET(cmdp, E_NEWSCREEN))
84 		return (ex_N_edit(sp, cmdp, frp, attach));
85 
86 	/*
87 	 * Check for modifications.
88 	 *
89 	 * !!!
90 	 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
91 	 */
92 	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
93 		return (1);
94 
95 	/* Switch files. */
96 	if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
97 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
98 		return (1);
99 
100 	F_SET(sp, SC_FSWITCH);
101 	return (0);
102 }
103 
104 /*
105  * ex_N_edit --
106  *	New screen version of ex_edit.
107  */
108 static int
109 ex_N_edit(sp, cmdp, frp, attach)
110 	SCR *sp;
111 	EXCMD *cmdp;
112 	FREF *frp;
113 	int attach;
114 {
115 	SCR *new;
116 
117 	/* Get a new screen. */
118 	if (screen_init(sp->gp, sp, &new))
119 		return (1);
120 	if (vs_split(sp, new, 0)) {
121 		(void)screen_end(new);
122 		return (1);
123 	}
124 
125 	/* Get a backing file. */
126 	if (attach) {
127 		/* Copy file state, keep the screen and cursor the same. */
128 		new->ep = sp->ep;
129 		++new->ep->refcnt;
130 
131 		new->frp = frp;
132 		new->frp->flags = sp->frp->flags;
133 
134 		new->lno = sp->lno;
135 		new->cno = sp->cno;
136 	} else if (file_init(new, frp, NULL,
137 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
138 		(void)vs_discard(new, NULL);
139 		(void)screen_end(new);
140 		return (1);
141 	}
142 
143 	/* Create the argument list. */
144 	new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
145 
146 	/* Set up the switch. */
147 	sp->nextdisp = new;
148 	F_SET(sp, SC_SSWITCH);
149 
150 	return (0);
151 }
152