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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
26 #include "../common/common.h"
28 enum which {WN, WQ, WRITE, XIT};
29 static int exwr(SCR *, EXCMD *, enum which);
32 * ex_wn -- :wn[!] [>>] [file]
33 * Write to a file and switch to the next one.
35 * PUBLIC: int ex_wn(SCR *, EXCMD *);
38 ex_wn(SCR *sp, EXCMD *cmdp)
40 if (exwr(sp, cmdp, WN))
45 /* The file name isn't a new file to edit. */
48 return (ex_next(sp, cmdp));
52 * ex_wq -- :wq[!] [>>] [file]
53 * Write to a file and quit.
55 * PUBLIC: int ex_wq(SCR *, EXCMD *);
58 ex_wq(SCR *sp, EXCMD *cmdp)
62 if (exwr(sp, cmdp, WQ))
67 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
69 if (ex_ncheck(sp, force))
72 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
77 * ex_write -- :write[!] [>>] [file]
81 * PUBLIC: int ex_write(SCR *, EXCMD *);
84 ex_write(SCR *sp, EXCMD *cmdp)
86 return (exwr(sp, cmdp, WRITE));
91 * ex_xit -- :x[it]! [file]
92 * Write out any modifications and quit.
94 * PUBLIC: int ex_xit(SCR *, EXCMD *);
97 ex_xit(SCR *sp, EXCMD *cmdp)
103 if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT))
108 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
110 if (ex_ncheck(sp, force))
113 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
119 * The guts of the ex write commands.
122 exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
135 /* All write commands can have an associated '!'. */
136 LF_INIT(FS_POSSIBLE);
137 if (FL_ISSET(cmdp->iflags, E_C_FORCE))
140 /* Skip any leading whitespace. */
142 for (p = cmdp->argv[0]->bp; *p != '\0' && cmdskip(*p); ++p);
144 /* If "write !" it's a pipe to a utility. */
145 if (cmdp->argc != 0 && cmd == WRITE && *p == '!') {
146 /* Secure means no shell access. */
147 if (O_ISSET(sp, O_SECURE)) {
148 ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F);
152 /* Expand the argument. */
153 for (++p; *p && cmdskip(*p); ++p);
155 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
158 if (argv_exp1(sp, cmdp, p, STRLEN(p), 1))
161 /* Set the last bang command */
163 free(exp->lastbcomm);
164 exp->lastbcomm = v_wstrdup(sp, cmdp->argv[1]->bp,
168 * Historically, vi waited after a write filter even if there
169 * wasn't any output from the command. People complained when
170 * nvi waited only if there was output, wanting the visual cue
171 * that the program hadn't written anything.
173 F_SET(sp, SC_EX_WAIT_YES);
177 * Ignore the return cursor position, the cursor doesn't
180 if (ex_filter(sp, cmdp, &cmdp->addr1,
181 &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE))
184 /* Ex terminates with a bang, even if the command fails. */
185 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
186 (void)ex_puts(sp, "!\n");
191 /* Set the FS_ALL flag if we're writing the entire file. */
192 if (cmdp->addr1.lno <= 1 && !db_exist(sp, cmdp->addr2.lno + 1))
195 /* If "write >>" it's an append to a file. */
196 if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') {
199 /* Skip ">>" and whitespace. */
200 for (p += 2; *p && cmdskip(*p); ++p);
203 /* If no other arguments, just write the file back. */
204 if (cmdp->argc == 0 || *p == '\0')
205 return (file_write(sp,
206 &cmdp->addr1, &cmdp->addr2, NULL, flags));
208 /* Build an argv so we get an argument count and file expansion. */
209 if (argv_exp2(sp, cmdp, p, STRLEN(p)))
213 * 0 args: impossible.
214 * 1 args: impossible (I hope).
216 * >2 args: object, too many args.
218 * The 1 args case depends on the argv_sexp() function refusing
219 * to return success without at least one non-blank character.
221 switch (cmdp->argc) {
227 INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1,
229 name = v_strdup(sp, n, nlen - 1);
233 * Historically, the read and write commands renamed
234 * "unnamed" files, or, if the file had a name, set
235 * the alternate file name.
237 if (F_ISSET(sp->frp, FR_TMPFILE) &&
238 !F_ISSET(sp->frp, FR_EXNAMED)) {
239 if ((n = v_strdup(sp, name, nlen - 1)) != NULL) {
244 * The file has a real name, it's no longer a
245 * temporary, clear the temporary file flags.
248 * If we're writing the whole file, FR_NAMECHANGE
249 * will be cleared by the write routine -- this is
252 F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
253 F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
255 /* Notify the screen. */
256 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
258 set_alt_name(sp, name);
261 INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen);
262 ex_emsg(sp, n, EXM_FILECOUNT);
266 rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags);
275 * Write a range of lines to a FILE *.
277 * PUBLIC: int ex_writefp(SCR *,
278 * PUBLIC: char *, FILE *, MARK *, MARK *, u_long *, u_long *, int);
281 ex_writefp(SCR *sp, char *name, FILE *fp, MARK *fm, MARK *tm, u_long *nlno, u_long *nch, int silent)
285 u_long ccnt; /* XXX: can't print off_t portably. */
286 recno_t fline, tline, lcnt;
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.
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."
317 msg = "253|Writing...";
319 for (; fline <= tline; ++fline, ++lcnt) {
320 /* Caller has to provide any interrupt message. */
321 if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
325 gp->scr_busy(sp, msg, msg == NULL ?
326 BUSY_UPDATE : BUSY_ON);
330 if (db_rget(sp, fline, &p, &len))
332 if (fwrite(p, 1, len, fp) != len)
335 if (putc('\n', fp) != '\n')
344 * I don't trust NFS -- check to make sure that we're talking to
345 * a regular file and sync so that NFS is forced to flush.
347 if (!fstat(fileno(fp), &sb) &&
348 S_ISREG(sb.st_mode) && fsync(fileno(fp)))
356 err: if (!F_ISSET(sp->ep, F_MULTILOCK))
357 msgq_str(sp, M_SYSERR, name, "%s");
363 gp->scr_busy(sp, NULL, BUSY_OFF);
365 /* Report the possibly partial transfer. */