tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / ex / ex_edit.c
blob934ca758b9e0526f15c4de59cd502824405b538d
1 /* $NetBSD: ex_edit.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
29 #include "../vi/vi.h"
31 static int ex_N_edit __P((SCR *, EXCMD *, FREF *, int));
34 * ex_edit -- :e[dit][!] [+cmd] [file]
35 * :ex[!] [+cmd] [file]
36 * :vi[sual][!] [+cmd] [file]
38 * Edit a file; if none specified, re-edit the current file. The third
39 * form of the command can only be executed while in vi mode. See the
40 * hack in ex.c:ex_cmd().
42 * !!!
43 * Historic vi didn't permit the '+' command form without specifying
44 * a file name as well. This seems unreasonable, so we support it
45 * regardless.
47 * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
49 int
50 ex_edit(SCR *sp, EXCMD *cmdp)
52 FREF *frp;
53 int attach, setalt;
54 const char *np;
55 size_t nlen;
57 switch (cmdp->argc) {
58 case 0:
60 * If the name has been changed, we edit that file, not the
61 * original name. If the user was editing a temporary file
62 * (or wasn't editing any file), create another one. The
63 * reason for not reusing temporary files is that there is
64 * special exit processing of them, and reuse is tricky.
66 frp = sp->frp;
67 if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
68 if ((frp = file_add(sp, NULL)) == NULL)
69 return (1);
70 attach = 0;
71 } else
72 attach = 1;
73 setalt = 0;
74 break;
75 case 1:
76 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
77 np, nlen);
78 if ((frp = file_add(sp, np)) == NULL)
79 return (1);
80 attach = 0;
81 setalt = 1;
82 set_alt_name(sp, np);
83 break;
84 default:
85 abort();
88 if (F_ISSET(cmdp, E_NEWSCREEN) || cmdp->cmd == &cmds[C_VSPLIT])
89 return (ex_N_edit(sp, cmdp, frp, attach));
92 * Check for modifications.
94 * !!!
95 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
97 if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
98 return (1);
100 /* Switch files. */
101 if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
102 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
103 return (1);
105 F_SET(sp, SC_FSWITCH);
106 return (0);
110 * ex_N_edit --
111 * New screen version of ex_edit.
113 static int
114 ex_N_edit(SCR *sp, EXCMD *cmdp, FREF *frp, int attach)
116 SCR *new;
118 /* Get a new screen. */
119 if (screen_init(sp->gp, sp, &new))
120 return (1);
121 if ((cmdp->cmd == &cmds[C_VSPLIT] && vs_vsplit(sp, new)) ||
122 (cmdp->cmd != &cmds[C_VSPLIT] && vs_split(sp, new, 0))) {
123 (void)screen_end(new);
124 return (1);
127 /* Get a backing file. */
128 if (attach) {
129 /* Copy file state, keep the screen and cursor the same. */
130 new->ep = sp->ep;
131 ++new->ep->refcnt;
132 TAILQ_INSERT_HEAD(&new->ep->scrq, new, eq);
134 new->frp = frp;
135 new->frp->flags = sp->frp->flags;
137 new->lno = sp->lno;
138 new->cno = sp->cno;
139 } else if (file_init(new, frp, NULL,
140 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
141 (void)vs_discard(new, NULL);
142 (void)screen_end(new);
143 return (1);
146 /* Create the argument list. */
147 new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
149 /* Set up the switch. */
150 sp->nextdisp = new;
151 F_SET(sp, SC_SSWITCH);
153 return (0);