1 /* $NetBSD: ex_write.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
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.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
32 #include "../common/common.h"
34 enum which
{WN
, WQ
, WRITE
, XIT
};
35 static int exwr
__P((SCR
*, EXCMD
*, enum which
));
38 * ex_wn -- :wn[!] [>>] [file]
39 * Write to a file and switch to the next one.
41 * PUBLIC: int ex_wn __P((SCR *, EXCMD *));
44 ex_wn(SCR
*sp
, EXCMD
*cmdp
)
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 *));
64 ex_wq(SCR
*sp
, EXCMD
*cmdp
)
68 if (exwr(sp
, cmdp
, WQ
))
73 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
75 if (ex_ncheck(sp
, force
))
78 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
83 * ex_write -- :write[!] [>>] [file]
87 * PUBLIC: int ex_write __P((SCR *, EXCMD *));
90 ex_write(SCR
*sp
, EXCMD
*cmdp
)
92 return (exwr(sp
, cmdp
, WRITE
));
97 * ex_xit -- :x[it]! [file]
98 * Write out any modifications and quit.
100 * PUBLIC: int ex_xit __P((SCR *, EXCMD *));
103 ex_xit(SCR
*sp
, EXCMD
*cmdp
)
109 if (F_ISSET(sp
->ep
, F_MODIFIED
) && exwr(sp
, cmdp
, XIT
))
114 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
116 if (ex_ncheck(sp
, force
))
119 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
125 * The guts of the ex write commands.
128 exwr(SCR
*sp
, EXCMD
*cmdp
, enum which cmd
)
141 /* All write commands can have an associated '!'. */
142 LF_INIT(FS_POSSIBLE
);
143 if (FL_ISSET(cmdp
->iflags
, E_C_FORCE
))
146 /* Skip any leading whitespace. */
148 for (p
= cmdp
->argv
[0]->bp
; *p
!= '\0' && isblank(*p
); ++p
);
150 /* If "write !" it's a pipe to a utility. */
151 if (cmdp
->argc
!= 0 && cmd
== WRITE
&& *p
== '!') {
152 /* Secure means no shell access. */
153 if (O_ISSET(sp
, O_SECURE
)) {
154 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
158 /* Expand the argument. */
159 for (++p
; *p
&& isblank(*p
); ++p
);
161 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
164 if (argv_exp1(sp
, cmdp
, p
, STRLEN(p
), 1))
167 /* Set the last bang command */
169 free(exp
->lastbcomm
);
170 exp
->lastbcomm
= v_wstrdup(sp
, cmdp
->argv
[1]->bp
,
174 * Historically, vi waited after a write filter even if there
175 * wasn't any output from the command. People complained when
176 * nvi waited only if there was output, wanting the visual cue
177 * that the program hadn't written anything.
179 F_SET(sp
, SC_EX_WAIT_YES
);
183 * Ignore the return cursor position, the cursor doesn't
186 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
187 &cmdp
->addr2
, &rm
, cmdp
->argv
[1]->bp
, FILTER_WRITE
))
190 /* Ex terminates with a bang, even if the command fails. */
191 if (!F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_EX_SILENT
))
192 (void)ex_puts(sp
, "!\n");
197 /* Set the FS_ALL flag if we're writing the entire file. */
198 if (cmdp
->addr1
.lno
<= 1 && !db_exist(sp
, cmdp
->addr2
.lno
+ 1))
201 /* If "write >>" it's an append to a file. */
202 if (cmdp
->argc
!= 0 && cmd
!= XIT
&& p
[0] == '>' && p
[1] == '>') {
205 /* Skip ">>" and whitespace. */
206 for (p
+= 2; *p
&& isblank(*p
); ++p
);
209 /* If no other arguments, just write the file back. */
210 if (cmdp
->argc
== 0 || *p
== '\0')
211 return (file_write(sp
,
212 &cmdp
->addr1
, &cmdp
->addr2
, NULL
, flags
));
214 /* Build an argv so we get an argument count and file expansion. */
215 if (argv_exp2(sp
, cmdp
, p
, STRLEN(p
)))
219 * 0 args: impossible.
220 * 1 args: impossible (I hope).
222 * >2 args: object, too many args.
224 * The 1 args case depends on the argv_sexp() function refusing
225 * to return success without at least one non-blank character.
227 switch (cmdp
->argc
) {
233 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+1,
235 name
= v_strdup(sp
, n
, nlen
- 1);
239 * Historically, the read and write commands renamed
240 * "unnamed" files, or, if the file had a name, set
241 * the alternate file name.
243 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
244 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
246 if ((q
= v_strdup(sp
, name
, nlen
- 1)) != NULL
) {
251 * The file has a real name, it's no longer a
252 * temporary, clear the temporary file flags.
255 * If we're writing the whole file, FR_NAMECHANGE
256 * will be cleared by the write routine -- this is
259 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
260 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
262 /* Notify the screen. */
263 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
265 set_alt_name(sp
, name
);
268 INT2CHAR(sp
, p
, STRLEN(p
) + 1, n
, nlen
);
269 ex_emsg(sp
, n
, EXM_FILECOUNT
);
273 rc
= file_write(sp
, &cmdp
->addr1
, &cmdp
->addr2
, name
, flags
);
282 * Write a range of lines to a FILE *.
284 * PUBLIC: int ex_writefp __P((SCR *,
285 * PUBLIC: const char *, FILE *, MARK *, MARK *, u_long *, u_long *, int));
288 ex_writefp(SCR
*sp
, const char *name
, FILE *fp
, MARK
*fm
, MARK
*tm
, u_long
*nlno
, u_long
*nch
, int silent
)
292 u_long ccnt
; /* XXX: can't print off_t portably. */
293 db_recno_t fline
, tline
, lcnt
;
311 * The vi filter code has multiple processes running simultaneously,
312 * and one of them calls ex_writefp(). The "unsafe" function calls
313 * in this code are to db_get() and msgq(). Db_get() is safe, see
314 * the comment in ex_filter.c:ex_filter() for details. We don't call
315 * msgq if the multiple process bit in the EXF is set.
318 * Historic vi permitted files of 0 length to be written. However,
319 * since the way vi got around dealing with "empty" files was to
320 * always have a line in the file no matter what, it wrote them as
321 * files of a single, empty line. We write empty files.
323 * "Alex, I'll take vi trivia for $1000."
327 msg
= "253|Writing...";
329 for (; fline
<= tline
; ++fline
, ++lcnt
) {
330 /* Caller has to provide any interrupt message. */
331 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
335 gp
->scr_busy(sp
, msg
, msg
== NULL
?
336 BUSY_UPDATE
: BUSY_ON
);
340 if (db_get(sp
, fline
, DBG_FATAL
, &p
, &len
))
342 INT2FILE(sp
, p
, len
, f
, flen
);
343 if (fwrite(f
, 1, flen
, fp
) != flen
)
346 if (putc('\n', fp
) != '\n')
355 * I don't trust NFS -- check to make sure that we're talking to
356 * a regular file and sync so that NFS is forced to flush.
358 if (!fstat(fileno(fp
), &sb
) &&
359 S_ISREG(sb
.st_mode
) && fsync(fileno(fp
)))
367 err
: if (!F_ISSET(sp
->ep
, F_MULTILOCK
))
368 msgq_str(sp
, M_SYSERR
, name
, "%s");
374 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
376 /* Report the possibly partial transfer. */