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.
13 static const char sccsid
[] = "$Id: ex_write.c,v 10.36 2001/06/09 18:26:30 skimo Exp $ (Berkeley) $Date: 2001/06/09 18:26:30 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.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 *));
46 if (exwr(sp
, cmdp
, WN
))
51 /* The file name isn't a new file to edit. */
54 return (ex_next(sp
, cmdp
));
58 * ex_wq -- :wq[!] [>>] [file]
59 * Write to a file and quit.
61 * PUBLIC: int ex_wq __P((SCR *, EXCMD *));
70 if (exwr(sp
, cmdp
, WQ
))
75 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
77 if (ex_ncheck(sp
, force
))
80 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
85 * ex_write -- :write[!] [>>] [file]
89 * PUBLIC: int ex_write __P((SCR *, EXCMD *));
96 return (exwr(sp
, cmdp
, WRITE
));
101 * ex_xit -- :x[it]! [file]
102 * Write out any modifications and quit.
104 * PUBLIC: int ex_xit __P((SCR *, EXCMD *));
115 if (F_ISSET(sp
->ep
, F_MODIFIED
) && exwr(sp
, cmdp
, XIT
))
120 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
122 if (ex_ncheck(sp
, force
))
125 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
131 * The guts of the ex write commands.
149 /* All write commands can have an associated '!'. */
150 LF_INIT(FS_POSSIBLE
);
151 if (FL_ISSET(cmdp
->iflags
, E_C_FORCE
))
154 /* Skip any leading whitespace. */
156 for (p
= cmdp
->argv
[0]->bp
; *p
!= '\0' && isblank(*p
); ++p
);
158 /* If "write !" it's a pipe to a utility. */
159 if (cmdp
->argc
!= 0 && cmd
== WRITE
&& *p
== '!') {
160 /* Secure means no shell access. */
161 if (O_ISSET(sp
, O_SECURE
)) {
162 ex_emsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
166 /* Expand the argument. */
167 for (++p
; *p
&& isblank(*p
); ++p
);
169 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
172 if (argv_exp1(sp
, cmdp
, p
, STRLEN(p
), 1))
176 * Historically, vi waited after a write filter even if there
177 * wasn't any output from the command. People complained when
178 * nvi waited only if there was output, wanting the visual cue
179 * that the program hadn't written anything.
181 F_SET(sp
, SC_EX_WAIT_YES
);
185 * Ignore the return cursor position, the cursor doesn't
188 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
189 &cmdp
->addr2
, &rm
, cmdp
->argv
[1]->bp
, FILTER_WRITE
))
192 /* Ex terminates with a bang, even if the command fails. */
193 if (!F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_EX_SILENT
))
194 (void)ex_puts(sp
, "!\n");
199 /* Set the FS_ALL flag if we're writing the entire file. */
200 if (cmdp
->addr1
.lno
<= 1 && !db_exist(sp
, cmdp
->addr2
.lno
+ 1))
203 /* If "write >>" it's an append to a file. */
204 if (cmdp
->argc
!= 0 && cmd
!= XIT
&& p
[0] == '>' && p
[1] == '>') {
207 /* Skip ">>" and whitespace. */
208 for (p
+= 2; *p
&& isblank(*p
); ++p
);
211 /* If no other arguments, just write the file back. */
212 if (cmdp
->argc
== 0 || *p
== '\0')
213 return (file_write(sp
,
214 &cmdp
->addr1
, &cmdp
->addr2
, NULL
, flags
));
216 /* Build an argv so we get an argument count and file expansion. */
217 if (argv_exp2(sp
, cmdp
, p
, STRLEN(p
)))
221 * 0 args: impossible.
222 * 1 args: impossible (I hope).
224 * >2 args: object, too many args.
226 * The 1 args case depends on the argv_sexp() function refusing
227 * to return success without at least one non-blank character.
229 switch (cmdp
->argc
) {
235 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+1,
237 name
= v_strdup(sp
, n
, nlen
- 1);
241 * Historically, the read and write commands renamed
242 * "unnamed" files, or, if the file had a name, set
243 * the alternate file name.
245 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
246 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
247 if ((n
= v_strdup(sp
, name
, nlen
- 1)) != NULL
) {
252 * The file has a real name, it's no longer a
253 * temporary, clear the temporary file flags.
256 * If we're writing the whole file, FR_NAMECHANGE
257 * will be cleared by the write routine -- this is
260 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
261 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
263 /* Notify the screen. */
264 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
266 set_alt_name(sp
, name
);
269 INT2CHAR(sp
, p
, STRLEN(p
) + 1, n
, nlen
);
270 ex_emsg(sp
, n
, EXM_FILECOUNT
);
274 rc
= file_write(sp
, &cmdp
->addr1
, &cmdp
->addr2
, name
, flags
);
283 * Write a range of lines to a FILE *.
285 * PUBLIC: int ex_writefp __P((SCR *,
286 * PUBLIC: char *, FILE *, MARK *, MARK *, u_long *, u_long *, int));
289 ex_writefp(sp
, name
, fp
, fm
, tm
, nlno
, nch
, silent
)
299 u_long ccnt
; /* XXX: can't print off_t portably. */
300 db_recno_t fline
, tline
, lcnt
;
318 * The vi filter code has multiple processes running simultaneously,
319 * and one of them calls ex_writefp(). The "unsafe" function calls
320 * in this code are to db_get() and msgq(). Db_get() is safe, see
321 * the comment in ex_filter.c:ex_filter() for details. We don't call
322 * msgq if the multiple process bit in the EXF is set.
325 * Historic vi permitted files of 0 length to be written. However,
326 * since the way vi got around dealing with "empty" files was to
327 * always have a line in the file no matter what, it wrote them as
328 * files of a single, empty line. We write empty files.
330 * "Alex, I'll take vi trivia for $1000."
334 msg
= "253|Writing...";
336 for (; fline
<= tline
; ++fline
, ++lcnt
) {
337 /* Caller has to provide any interrupt message. */
338 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
342 gp
->scr_busy(sp
, msg
, msg
== NULL
?
343 BUSY_UPDATE
: BUSY_ON
);
347 if (db_get(sp
, fline
, DBG_FATAL
, &p
, &len
))
349 INT2FILE(sp
, p
, len
, f
, flen
);
350 if (fwrite(f
, 1, flen
, fp
) != flen
)
353 if (putc('\n', fp
) != '\n')
362 * I don't trust NFS -- check to make sure that we're talking to
363 * a regular file and sync so that NFS is forced to flush.
365 if (!fstat(fileno(fp
), &sb
) &&
366 S_ISREG(sb
.st_mode
) && fsync(fileno(fp
)))
374 err
: if (!F_ISSET(sp
->ep
, F_MULTILOCK
))
375 msgq_str(sp
, M_SYSERR
, name
, "%s");
381 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
383 /* Report the possibly partial transfer. */