text input: fix problem with autoindenting and ^^D
[nvi.git] / ex / ex_write.c
blobb8e9e6ab6f7f6b6b008acc7a062f60ca7bc9baea
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_write.c,v 10.38 2001/06/25 15:19:22 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:22 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "../common/common.h"
32 enum which {WN, WQ, WRITE, XIT};
33 static int exwr __P((SCR *, EXCMD *, enum which));
36 * ex_wn -- :wn[!] [>>] [file]
37 * Write to a file and switch to the next one.
39 * PUBLIC: int ex_wn __P((SCR *, EXCMD *));
41 int
42 ex_wn(SCR *sp, EXCMD *cmdp)
44 if (exwr(sp, cmdp, WN))
45 return (1);
46 if (file_m3(sp, 0))
47 return (1);
49 /* The file name isn't a new file to edit. */
50 cmdp->argc = 0;
52 return (ex_next(sp, cmdp));
56 * ex_wq -- :wq[!] [>>] [file]
57 * Write to a file and quit.
59 * PUBLIC: int ex_wq __P((SCR *, EXCMD *));
61 int
62 ex_wq(SCR *sp, EXCMD *cmdp)
64 int force;
66 if (exwr(sp, cmdp, WQ))
67 return (1);
68 if (file_m3(sp, 0))
69 return (1);
71 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
73 if (ex_ncheck(sp, force))
74 return (1);
76 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
77 return (0);
81 * ex_write -- :write[!] [>>] [file]
82 * :write [!] [cmd]
83 * Write to a file.
85 * PUBLIC: int ex_write __P((SCR *, EXCMD *));
87 int
88 ex_write(SCR *sp, EXCMD *cmdp)
90 return (exwr(sp, cmdp, WRITE));
95 * ex_xit -- :x[it]! [file]
96 * Write out any modifications and quit.
98 * PUBLIC: int ex_xit __P((SCR *, EXCMD *));
101 ex_xit(SCR *sp, EXCMD *cmdp)
103 int force;
105 NEEDFILE(sp, cmdp);
107 if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT))
108 return (1);
109 if (file_m3(sp, 0))
110 return (1);
112 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
114 if (ex_ncheck(sp, force))
115 return (1);
117 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
118 return (0);
122 * exwr --
123 * The guts of the ex write commands.
125 static int
126 exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
128 MARK rm;
129 int flags;
130 char *name;
131 CHAR_T *p;
132 size_t nlen;
133 char *n;
134 int rc;
136 NEEDFILE(sp, cmdp);
138 /* All write commands can have an associated '!'. */
139 LF_INIT(FS_POSSIBLE);
140 if (FL_ISSET(cmdp->iflags, E_C_FORCE))
141 LF_SET(FS_FORCE);
143 /* Skip any leading whitespace. */
144 if (cmdp->argc != 0)
145 for (p = cmdp->argv[0]->bp; *p != '\0' && isblank(*p); ++p);
147 /* If "write !" it's a pipe to a utility. */
148 if (cmdp->argc != 0 && cmd == WRITE && *p == '!') {
149 /* Secure means no shell access. */
150 if (O_ISSET(sp, O_SECURE)) {
151 ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F);
152 return (1);
155 /* Expand the argument. */
156 for (++p; *p && isblank(*p); ++p);
157 if (*p == '\0') {
158 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
159 return (1);
161 if (argv_exp1(sp, cmdp, p, STRLEN(p), 1))
162 return (1);
165 * Historically, vi waited after a write filter even if there
166 * wasn't any output from the command. People complained when
167 * nvi waited only if there was output, wanting the visual cue
168 * that the program hadn't written anything.
170 F_SET(sp, SC_EX_WAIT_YES);
173 * !!!
174 * Ignore the return cursor position, the cursor doesn't
175 * move.
177 if (ex_filter(sp, cmdp, &cmdp->addr1,
178 &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE))
179 return (1);
181 /* Ex terminates with a bang, even if the command fails. */
182 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
183 (void)ex_puts(sp, "!\n");
185 return (0);
188 /* Set the FS_ALL flag if we're writing the entire file. */
189 if (cmdp->addr1.lno <= 1 && !db_exist(sp, cmdp->addr2.lno + 1))
190 LF_SET(FS_ALL);
192 /* If "write >>" it's an append to a file. */
193 if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') {
194 LF_SET(FS_APPEND);
196 /* Skip ">>" and whitespace. */
197 for (p += 2; *p && isblank(*p); ++p);
200 /* If no other arguments, just write the file back. */
201 if (cmdp->argc == 0 || *p == '\0')
202 return (file_write(sp,
203 &cmdp->addr1, &cmdp->addr2, NULL, flags));
205 /* Build an argv so we get an argument count and file expansion. */
206 if (argv_exp2(sp, cmdp, p, STRLEN(p)))
207 return (1);
210 * 0 args: impossible.
211 * 1 args: impossible (I hope).
212 * 2 args: read it.
213 * >2 args: object, too many args.
215 * The 1 args case depends on the argv_sexp() function refusing
216 * to return success without at least one non-blank character.
218 switch (cmdp->argc) {
219 case 0:
220 case 1:
221 abort();
222 /* NOTREACHED */
223 case 2:
224 INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1,
225 n, nlen);
226 name = v_strdup(sp, n, nlen - 1);
229 * !!!
230 * Historically, the read and write commands renamed
231 * "unnamed" files, or, if the file had a name, set
232 * the alternate file name.
234 if (F_ISSET(sp->frp, FR_TMPFILE) &&
235 !F_ISSET(sp->frp, FR_EXNAMED)) {
236 if ((n = v_strdup(sp, name, nlen - 1)) != NULL) {
237 free(sp->frp->name);
238 sp->frp->name = n;
241 * The file has a real name, it's no longer a
242 * temporary, clear the temporary file flags.
244 * !!!
245 * If we're writing the whole file, FR_NAMECHANGE
246 * will be cleared by the write routine -- this is
247 * historic practice.
249 F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
250 F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
252 /* Notify the screen. */
253 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
254 } else
255 set_alt_name(sp, name);
256 break;
257 default:
258 INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen);
259 ex_emsg(sp, n, EXM_FILECOUNT);
260 return (1);
263 rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags);
265 free(name);
267 return rc;
271 * ex_writefp --
272 * Write a range of lines to a FILE *.
274 * PUBLIC: int ex_writefp __P((SCR *,
275 * PUBLIC: char *, FILE *, MARK *, MARK *, u_long *, u_long *, int));
278 ex_writefp(SCR *sp, char *name, FILE *fp, MARK *fm, MARK *tm, u_long *nlno, u_long *nch, int silent)
280 struct stat sb;
281 GS *gp;
282 u_long ccnt; /* XXX: can't print off_t portably. */
283 db_recno_t fline, tline, lcnt;
284 size_t len;
285 int rval;
286 char *msg;
287 CHAR_T *p;
288 char *f;
289 size_t flen;
291 gp = sp->gp;
292 fline = fm->lno;
293 tline = tm->lno;
295 if (nlno != NULL) {
296 *nch = 0;
297 *nlno = 0;
301 * The vi filter code has multiple processes running simultaneously,
302 * and one of them calls ex_writefp(). The "unsafe" function calls
303 * in this code are to db_get() and msgq(). Db_get() is safe, see
304 * the comment in ex_filter.c:ex_filter() for details. We don't call
305 * msgq if the multiple process bit in the EXF is set.
307 * !!!
308 * Historic vi permitted files of 0 length to be written. However,
309 * since the way vi got around dealing with "empty" files was to
310 * always have a line in the file no matter what, it wrote them as
311 * files of a single, empty line. We write empty files.
313 * "Alex, I'll take vi trivia for $1000."
315 ccnt = 0;
316 lcnt = 0;
317 msg = "253|Writing...";
318 if (tline != 0)
319 for (; fline <= tline; ++fline, ++lcnt) {
320 /* Caller has to provide any interrupt message. */
321 if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
322 if (INTERRUPTED(sp))
323 break;
324 if (!silent) {
325 gp->scr_busy(sp, msg, msg == NULL ?
326 BUSY_UPDATE : BUSY_ON);
327 msg = NULL;
330 if (db_get(sp, fline, DBG_FATAL, &p, &len))
331 goto err;
332 INT2FILE(sp, p, len, f, flen);
333 if (fwrite(f, 1, flen, fp) != flen)
334 goto err;
335 ccnt += len;
336 if (putc('\n', fp) != '\n')
337 break;
338 ++ccnt;
341 if (fflush(fp))
342 goto err;
344 * XXX
345 * I don't trust NFS -- check to make sure that we're talking to
346 * a regular file and sync so that NFS is forced to flush.
348 if (!fstat(fileno(fp), &sb) &&
349 S_ISREG(sb.st_mode) && fsync(fileno(fp)))
350 goto err;
352 if (fclose(fp))
353 goto err;
355 rval = 0;
356 if (0) {
357 err: if (!F_ISSET(sp->ep, F_MULTILOCK))
358 msgq_str(sp, M_SYSERR, name, "%s");
359 (void)fclose(fp);
360 rval = 1;
363 if (!silent)
364 gp->scr_busy(sp, NULL, BUSY_OFF);
366 /* Report the possibly partial transfer. */
367 if (nlno != NULL) {
368 *nch = ccnt;
369 *nlno = lcnt;
371 return (rval);