Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_write.c
blob29fdee6bcf4dabcb3d22a4046db2ba187a9dcf06
1 /* $NetBSD: ex_write.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
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.
12 #include "config.h"
14 #ifndef lint
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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.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 *));
43 int
44 ex_wn(SCR *sp, EXCMD *cmdp)
46 if (exwr(sp, cmdp, WN))
47 return (1);
48 if (file_m3(sp, 0))
49 return (1);
51 /* The file name isn't a new file to edit. */
52 cmdp->argc = 0;
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 *));
63 int
64 ex_wq(SCR *sp, EXCMD *cmdp)
66 int force;
68 if (exwr(sp, cmdp, WQ))
69 return (1);
70 if (file_m3(sp, 0))
71 return (1);
73 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
75 if (ex_ncheck(sp, force))
76 return (1);
78 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
79 return (0);
83 * ex_write -- :write[!] [>>] [file]
84 * :write [!] [cmd]
85 * Write to a file.
87 * PUBLIC: int ex_write __P((SCR *, EXCMD *));
89 int
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)
105 int force;
107 NEEDFILE(sp, cmdp);
109 if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT))
110 return (1);
111 if (file_m3(sp, 0))
112 return (1);
114 force = FL_ISSET(cmdp->iflags, E_C_FORCE);
116 if (ex_ncheck(sp, force))
117 return (1);
119 F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
120 return (0);
124 * exwr --
125 * The guts of the ex write commands.
127 static int
128 exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
130 MARK rm;
131 int flags;
132 char *name;
133 CHAR_T *p = NULL;
134 size_t nlen;
135 const char *n;
136 int rc;
137 EX_PRIVATE *exp;
139 NEEDFILE(sp, cmdp);
141 /* All write commands can have an associated '!'. */
142 LF_INIT(FS_POSSIBLE);
143 if (FL_ISSET(cmdp->iflags, E_C_FORCE))
144 LF_SET(FS_FORCE);
146 /* Skip any leading whitespace. */
147 if (cmdp->argc != 0)
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);
155 return (1);
158 /* Expand the argument. */
159 for (++p; *p && isblank(*p); ++p);
160 if (*p == '\0') {
161 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
162 return (1);
164 if (argv_exp1(sp, cmdp, p, STRLEN(p), 1))
165 return (1);
167 /* Set the last bang command */
168 exp = EXP(sp);
169 free(exp->lastbcomm);
170 exp->lastbcomm = v_wstrdup(sp, cmdp->argv[1]->bp,
171 cmdp->argv[1]->len);
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);
182 * !!!
183 * Ignore the return cursor position, the cursor doesn't
184 * move.
186 if (ex_filter(sp, cmdp, &cmdp->addr1,
187 &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE))
188 return (1);
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");
194 return (0);
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))
199 LF_SET(FS_ALL);
201 /* If "write >>" it's an append to a file. */
202 if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') {
203 LF_SET(FS_APPEND);
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)))
216 return (1);
219 * 0 args: impossible.
220 * 1 args: impossible (I hope).
221 * 2 args: read it.
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) {
228 case 0:
229 case 1:
230 abort();
231 /* NOTREACHED */
232 case 2:
233 INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1,
234 n, nlen);
235 name = v_strdup(sp, n, nlen - 1);
238 * !!!
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)) {
245 char *q;
246 if ((q = v_strdup(sp, name, nlen - 1)) != NULL) {
247 free(sp->frp->name);
248 sp->frp->name = q;
251 * The file has a real name, it's no longer a
252 * temporary, clear the temporary file flags.
254 * !!!
255 * If we're writing the whole file, FR_NAMECHANGE
256 * will be cleared by the write routine -- this is
257 * historic practice.
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);
264 } else
265 set_alt_name(sp, name);
266 break;
267 default:
268 INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen);
269 ex_emsg(sp, n, EXM_FILECOUNT);
270 return (1);
273 rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags);
275 free(name);
277 return rc;
281 * ex_writefp --
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)
290 struct stat sb;
291 GS *gp;
292 u_long ccnt; /* XXX: can't print off_t portably. */
293 db_recno_t fline, tline, lcnt;
294 size_t len;
295 int rval;
296 const char *msg;
297 CHAR_T *p;
298 const char *f;
299 size_t flen;
301 gp = sp->gp;
302 fline = fm->lno;
303 tline = tm->lno;
305 if (nlno != NULL) {
306 *nch = 0;
307 *nlno = 0;
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.
317 * !!!
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."
325 ccnt = 0;
326 lcnt = 0;
327 msg = "253|Writing...";
328 if (tline != 0)
329 for (; fline <= tline; ++fline, ++lcnt) {
330 /* Caller has to provide any interrupt message. */
331 if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
332 if (INTERRUPTED(sp))
333 break;
334 if (!silent) {
335 gp->scr_busy(sp, msg, msg == NULL ?
336 BUSY_UPDATE : BUSY_ON);
337 msg = NULL;
340 if (db_get(sp, fline, DBG_FATAL, &p, &len))
341 goto err;
342 INT2FILE(sp, p, len, f, flen);
343 if (fwrite(f, 1, flen, fp) != flen)
344 goto err;
345 ccnt += len;
346 if (putc('\n', fp) != '\n')
347 break;
348 ++ccnt;
351 if (fflush(fp))
352 goto err;
354 * XXX
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)))
360 goto err;
362 if (fclose(fp))
363 goto err;
365 rval = 0;
366 if (0) {
367 err: if (!F_ISSET(sp->ep, F_MULTILOCK))
368 msgq_str(sp, M_SYSERR, name, "%s");
369 (void)fclose(fp);
370 rval = 1;
373 if (!silent)
374 gp->scr_busy(sp, NULL, BUSY_OFF);
376 /* Report the possibly partial transfer. */
377 if (nlno != NULL) {
378 *nch = ccnt;
379 *nlno = lcnt;
381 return (rval);