1 /* $NetBSD: ex_write.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
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.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: ex_write.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
36 #include "../common/common.h"
38 enum which
{WN
, WQ
, WRITE
, XIT
};
39 static int exwr
__P((SCR
*, EXCMD
*, enum which
));
42 * ex_wn -- :wn[!] [>>] [file]
43 * Write to a file and switch to the next one.
45 * PUBLIC: int ex_wn __P((SCR *, EXCMD *));
48 ex_wn(SCR
*sp
, EXCMD
*cmdp
)
50 if (exwr(sp
, cmdp
, WN
))
55 /* The file name isn't a new file to edit. */
58 return (ex_next(sp
, cmdp
));
62 * ex_wq -- :wq[!] [>>] [file]
63 * Write to a file and quit.
65 * PUBLIC: int ex_wq __P((SCR *, EXCMD *));
68 ex_wq(SCR
*sp
, EXCMD
*cmdp
)
72 if (exwr(sp
, cmdp
, WQ
))
77 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
79 if (ex_ncheck(sp
, force
))
82 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
87 * ex_write -- :write[!] [>>] [file]
91 * PUBLIC: int ex_write __P((SCR *, EXCMD *));
94 ex_write(SCR
*sp
, EXCMD
*cmdp
)
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 *));
107 ex_xit(SCR
*sp
, EXCMD
*cmdp
)
113 if (F_ISSET(sp
->ep
, F_MODIFIED
) && exwr(sp
, cmdp
, XIT
))
118 force
= FL_ISSET(cmdp
->iflags
, E_C_FORCE
);
120 if (ex_ncheck(sp
, force
))
123 F_SET(sp
, force
? SC_EXIT_FORCE
: SC_EXIT
);
129 * The guts of the ex write commands.
132 exwr(SCR
*sp
, EXCMD
*cmdp
, enum which cmd
)
145 /* All write commands can have an associated '!'. */
146 LF_INIT(FS_POSSIBLE
);
147 if (FL_ISSET(cmdp
->iflags
, E_C_FORCE
))
150 /* Skip any leading whitespace. */
152 for (p
= cmdp
->argv
[0]->bp
; *p
!= '\0' && ISBLANK((UCHAR_T
)*p
); ++p
);
154 /* If "write !" it's a pipe to a utility. */
155 if (cmdp
->argc
!= 0 && cmd
== WRITE
&& *p
== '!') {
156 /* Secure means no shell access. */
157 if (O_ISSET(sp
, O_SECURE
)) {
158 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
162 /* Expand the argument. */
163 for (++p
; *p
&& ISBLANK((UCHAR_T
)*p
); ++p
);
165 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
168 if (argv_exp1(sp
, cmdp
, p
, STRLEN(p
), 1))
171 /* Set the last bang command */
173 free(exp
->lastbcomm
);
174 exp
->lastbcomm
= v_wstrdup(sp
, cmdp
->argv
[1]->bp
,
178 * Historically, vi waited after a write filter even if there
179 * wasn't any output from the command. People complained when
180 * nvi waited only if there was output, wanting the visual cue
181 * that the program hadn't written anything.
183 F_SET(sp
, SC_EX_WAIT_YES
);
187 * Ignore the return cursor position, the cursor doesn't
190 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
191 &cmdp
->addr2
, &rm
, cmdp
->argv
[1]->bp
, FILTER_WRITE
))
194 /* Ex terminates with a bang, even if the command fails. */
195 if (!F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_EX_SILENT
))
196 (void)ex_puts(sp
, "!\n");
201 /* Set the FS_ALL flag if we're writing the entire file. */
202 if (cmdp
->addr1
.lno
<= 1 && !db_exist(sp
, cmdp
->addr2
.lno
+ 1))
205 /* If "write >>" it's an append to a file. */
206 if (cmdp
->argc
!= 0 && cmd
!= XIT
&& p
[0] == '>' && p
[1] == '>') {
209 /* Skip ">>" and whitespace. */
210 for (p
+= 2; *p
&& ISBLANK((UCHAR_T
)*p
); ++p
);
213 /* If no other arguments, just write the file back. */
214 if (cmdp
->argc
== 0 || *p
== '\0')
215 return (file_write(sp
,
216 &cmdp
->addr1
, &cmdp
->addr2
, NULL
, flags
));
218 /* Build an argv so we get an argument count and file expansion. */
219 if (argv_exp2(sp
, cmdp
, p
, STRLEN(p
)))
223 * 0 args: impossible.
224 * 1 args: impossible (I hope).
226 * >2 args: object, too many args.
228 * The 1 args case depends on the argv_sexp() function refusing
229 * to return success without at least one non-blank character.
231 switch (cmdp
->argc
) {
237 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+1,
239 name
= v_strdup(sp
, n
, nlen
- 1);
243 * Historically, the read and write commands renamed
244 * "unnamed" files, or, if the file had a name, set
245 * the alternate file name.
247 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
248 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
250 if ((q
= v_strdup(sp
, name
, nlen
- 1)) != NULL
) {
255 * The file has a real name, it's no longer a
256 * temporary, clear the temporary file flags.
259 * If we're writing the whole file, FR_NAMECHANGE
260 * will be cleared by the write routine -- this is
263 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
264 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
266 /* Notify the screen. */
267 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
269 set_alt_name(sp
, name
);
272 INT2CHAR(sp
, p
, STRLEN(p
) + 1, n
, nlen
);
273 ex_emsg(sp
, n
, EXM_FILECOUNT
);
277 rc
= file_write(sp
, &cmdp
->addr1
, &cmdp
->addr2
, name
, flags
);
286 * Write a range of lines to a FILE *.
288 * PUBLIC: int ex_writefp __P((SCR *,
289 * PUBLIC: const char *, FILE *, MARK *, MARK *, u_long *, u_long *, int));
292 ex_writefp(SCR
*sp
, const char *name
, FILE *fp
, MARK
*fm
, MARK
*tm
, u_long
*nlno
, u_long
*nch
, int silent
)
296 u_long ccnt
; /* XXX: can't print off_t portably. */
297 db_recno_t fline
, tline
, lcnt
;
315 * The vi filter code has multiple processes running simultaneously,
316 * and one of them calls ex_writefp(). The "unsafe" function calls
317 * in this code are to db_get() and msgq(). Db_get() is safe, see
318 * the comment in ex_filter.c:ex_filter() for details. We don't call
319 * msgq if the multiple process bit in the EXF is set.
322 * Historic vi permitted files of 0 length to be written. However,
323 * since the way vi got around dealing with "empty" files was to
324 * always have a line in the file no matter what, it wrote them as
325 * files of a single, empty line. We write empty files.
327 * "Alex, I'll take vi trivia for $1000."
331 msg
= "253|Writing...";
333 for (; fline
<= tline
; ++fline
, ++lcnt
) {
334 /* Caller has to provide any interrupt message. */
335 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
339 gp
->scr_busy(sp
, msg
, msg
== NULL
?
340 BUSY_UPDATE
: BUSY_ON
);
344 if (db_get(sp
, fline
, DBG_FATAL
, &p
, &len
))
346 INT2FILE(sp
, p
, len
, f
, flen
);
347 if (fwrite(f
, 1, flen
, fp
) != flen
)
350 if (putc('\n', fp
) != '\n')
359 * I don't trust NFS -- check to make sure that we're talking to
360 * a regular file and sync so that NFS is forced to flush.
362 if (!fstat(fileno(fp
), &sb
) &&
363 S_ISREG(sb
.st_mode
) && fsync(fileno(fp
)))
371 err
: if (!F_ISSET(sp
->ep
, F_MULTILOCK
))
372 msgq_str(sp
, M_SYSERR
, name
, "%s");
378 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);
380 /* Report the possibly partial transfer. */