xref: /freebsd/contrib/nvi/ex/ex_write.c (revision cd0d51baaa4509a1db83251a601d34404d20c990)
1 /*-
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_write.c,v 10.43 2015/04/03 15:18:45 zy Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "../common/common.h"
31 
32 enum which {WN, WQ, WRITE, XIT};
33 static int exwr(SCR *, EXCMD *, enum which);
34 
35 /*
36  * ex_wn --	:wn[!] [>>] [file]
37  *	Write to a file and switch to the next one.
38  *
39  * PUBLIC: int ex_wn(SCR *, EXCMD *);
40  */
41 int
42 ex_wn(SCR *sp, EXCMD *cmdp)
43 {
44 	if (exwr(sp, cmdp, WN))
45 		return (1);
46 	if (file_m3(sp, 0))
47 		return (1);
48 
49 	/* The file name isn't a new file to edit. */
50 	cmdp->argc = 0;
51 
52 	return (ex_next(sp, cmdp));
53 }
54 
55 /*
56  * ex_wq --	:wq[!] [>>] [file]
57  *	Write to a file and quit.
58  *
59  * PUBLIC: int ex_wq(SCR *, EXCMD *);
60  */
61 int
62 ex_wq(SCR *sp, EXCMD *cmdp)
63 {
64 	int force;
65 
66 	if (exwr(sp, cmdp, WQ))
67 		return (1);
68 	if (file_m3(sp, 0))
69 		return (1);
70 
71 	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
72 
73 	if (ex_ncheck(sp, force))
74 		return (1);
75 
76 	F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
77 	return (0);
78 }
79 
80 /*
81  * ex_write --	:write[!] [>>] [file]
82  *		:write [!] [cmd]
83  *	Write to a file.
84  *
85  * PUBLIC: int ex_write(SCR *, EXCMD *);
86  */
87 int
88 ex_write(SCR *sp, EXCMD *cmdp)
89 {
90 	return (exwr(sp, cmdp, WRITE));
91 }
92 
93 
94 /*
95  * ex_xit -- :x[it]! [file]
96  *	Write out any modifications and quit.
97  *
98  * PUBLIC: int ex_xit(SCR *, EXCMD *);
99  */
100 int
101 ex_xit(SCR *sp, EXCMD *cmdp)
102 {
103 	int force;
104 
105 	NEEDFILE(sp, cmdp);
106 
107 	if (F_ISSET(sp->ep, F_MODIFIED) && exwr(sp, cmdp, XIT))
108 		return (1);
109 	if (file_m3(sp, 0))
110 		return (1);
111 
112 	force = FL_ISSET(cmdp->iflags, E_C_FORCE);
113 
114 	if (ex_ncheck(sp, force))
115 		return (1);
116 
117 	F_SET(sp, force ? SC_EXIT_FORCE : SC_EXIT);
118 	return (0);
119 }
120 
121 /*
122  * exwr --
123  *	The guts of the ex write commands.
124  */
125 static int
126 exwr(SCR *sp, EXCMD *cmdp, enum which cmd)
127 {
128 	MARK rm;
129 	int flags;
130 	char *name;
131 	CHAR_T *p = NULL;
132 	size_t nlen;
133 	char *n;
134 	int rc;
135 	EX_PRIVATE *exp;
136 
137 	NEEDFILE(sp, cmdp);
138 
139 	/* All write commands can have an associated '!'. */
140 	LF_INIT(FS_POSSIBLE);
141 	if (FL_ISSET(cmdp->iflags, E_C_FORCE))
142 		LF_SET(FS_FORCE);
143 
144 	/* Skip any leading whitespace. */
145 	if (cmdp->argc != 0)
146 		for (p = cmdp->argv[0]->bp; *p != '\0' && cmdskip(*p); ++p);
147 
148 	/* If "write !" it's a pipe to a utility. */
149 	if (cmdp->argc != 0 && cmd == WRITE && *p == '!') {
150 		/* Secure means no shell access. */
151 		if (O_ISSET(sp, O_SECURE)) {
152 			ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F);
153 			return (1);
154 		}
155 
156 		/* Expand the argument. */
157 		for (++p; *p && cmdskip(*p); ++p);
158 		if (*p == '\0') {
159 			ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
160 			return (1);
161 		}
162 		if (argv_exp1(sp, cmdp, p, STRLEN(p), 1))
163 			return (1);
164 
165 		/* Set the last bang command */
166 		exp = EXP(sp);
167 		free(exp->lastbcomm);
168 		exp->lastbcomm = v_wstrdup(sp, cmdp->argv[1]->bp,
169 		    cmdp->argv[1]->len);
170 
171 		/*
172 		 * Historically, vi waited after a write filter even if there
173 		 * wasn't any output from the command.  People complained when
174 		 * nvi waited only if there was output, wanting the visual cue
175 		 * that the program hadn't written anything.
176 		 */
177 		F_SET(sp, SC_EX_WAIT_YES);
178 
179 		/*
180 		 * !!!
181 		 * Ignore the return cursor position, the cursor doesn't
182 		 * move.
183 		 */
184 		if (ex_filter(sp, cmdp, &cmdp->addr1,
185 		    &cmdp->addr2, &rm, cmdp->argv[1]->bp, FILTER_WRITE))
186 			return (1);
187 
188 		/* Ex terminates with a bang, even if the command fails. */
189 		if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
190 			(void)ex_puts(sp, "!\n");
191 
192 		return (0);
193 	}
194 
195 	/* Set the FS_ALL flag if we're writing the entire file. */
196 	if (cmdp->addr1.lno <= 1 && !db_exist(sp, cmdp->addr2.lno + 1))
197 		LF_SET(FS_ALL);
198 
199 	/* If "write >>" it's an append to a file. */
200 	if (cmdp->argc != 0 && cmd != XIT && p[0] == '>' && p[1] == '>') {
201 		LF_SET(FS_APPEND);
202 
203 		/* Skip ">>" and whitespace. */
204 		for (p += 2; *p && cmdskip(*p); ++p);
205 	}
206 
207 	/* If no other arguments, just write the file back. */
208 	if (cmdp->argc == 0 || *p == '\0')
209 		return (file_write(sp,
210 		    &cmdp->addr1, &cmdp->addr2, NULL, flags));
211 
212 	/* Build an argv so we get an argument count and file expansion. */
213 	if (argv_exp2(sp, cmdp, p, STRLEN(p)))
214 		return (1);
215 
216 	/*
217 	 *  0 args: impossible.
218 	 *  1 args: impossible (I hope).
219 	 *  2 args: read it.
220 	 * >2 args: object, too many args.
221 	 *
222 	 * The 1 args case depends on the argv_sexp() function refusing
223 	 * to return success without at least one non-blank character.
224 	 */
225 	switch (cmdp->argc) {
226 	case 0:
227 	case 1:
228 		abort();
229 		/* NOTREACHED */
230 	case 2:
231 		INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len+1,
232 			 n, nlen);
233 		name = v_strdup(sp, n, nlen - 1);
234 
235 		/*
236 		 * !!!
237 		 * Historically, the read and write commands renamed
238 		 * "unnamed" files, or, if the file had a name, set
239 		 * the alternate file name.
240 		 */
241 		if (F_ISSET(sp->frp, FR_TMPFILE) &&
242 		    !F_ISSET(sp->frp, FR_EXNAMED)) {
243 			if ((n = v_strdup(sp, name, nlen - 1)) != NULL) {
244 				free(sp->frp->name);
245 				sp->frp->name = n;
246 			}
247 			/*
248 			 * The file has a real name, it's no longer a
249 			 * temporary, clear the temporary file flags.
250 			 *
251 			 * !!!
252 			 * If we're writing the whole file, FR_NAMECHANGE
253 			 * will be cleared by the write routine -- this is
254 			 * historic practice.
255 			 */
256 			F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
257 			F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
258 
259 			/* Notify the screen. */
260 			(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
261 		} else
262 			set_alt_name(sp, name);
263 		break;
264 	default:
265 		INT2CHAR(sp, p, STRLEN(p) + 1, n, nlen);
266 		ex_emsg(sp, n, EXM_FILECOUNT);
267 		return (1);
268 	}
269 
270 	rc = file_write(sp, &cmdp->addr1, &cmdp->addr2, name, flags);
271 
272 	free(name);
273 
274 	return rc;
275 }
276 
277 /*
278  * ex_writefp --
279  *	Write a range of lines to a FILE *.
280  *
281  * PUBLIC: int ex_writefp(SCR *,
282  * PUBLIC:    char *, FILE *, MARK *, MARK *, u_long *, u_long *, int);
283  */
284 int
285 ex_writefp(SCR *sp, char *name, FILE *fp, MARK *fm, MARK *tm, u_long *nlno, u_long *nch, int silent)
286 {
287 	struct stat sb;
288 	GS *gp;
289 	u_long ccnt;			/* XXX: can't print off_t portably. */
290 	recno_t fline, tline, lcnt;
291 	size_t len;
292 	int rval;
293 	char *msg, *p;
294 
295 	gp = sp->gp;
296 	fline = fm->lno;
297 	tline = tm->lno;
298 
299 	if (nlno != NULL) {
300 		*nch = 0;
301 		*nlno = 0;
302 	}
303 
304 	/*
305 	 * The vi filter code has multiple processes running simultaneously,
306 	 * and one of them calls ex_writefp().  The "unsafe" function calls
307 	 * in this code are to db_get() and msgq().  Db_get() is safe, see
308 	 * the comment in ex_filter.c:ex_filter() for details.  We don't call
309 	 * msgq if the multiple process bit in the EXF is set.
310 	 *
311 	 * !!!
312 	 * Historic vi permitted files of 0 length to be written.  However,
313 	 * since the way vi got around dealing with "empty" files was to
314 	 * always have a line in the file no matter what, it wrote them as
315 	 * files of a single, empty line.  We write empty files.
316 	 *
317 	 * "Alex, I'll take vi trivia for $1000."
318 	 */
319 	ccnt = 0;
320 	lcnt = 0;
321 	msg = "253|Writing...";
322 	if (tline != 0)
323 		for (; fline <= tline; ++fline, ++lcnt) {
324 			/* Caller has to provide any interrupt message. */
325 			if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
326 				if (INTERRUPTED(sp))
327 					break;
328 				if (!silent) {
329 					gp->scr_busy(sp, msg, msg == NULL ?
330 					    BUSY_UPDATE : BUSY_ON);
331 					msg = NULL;
332 				}
333 			}
334 			if (db_rget(sp, fline, &p, &len))
335 				goto err;
336 			if (fwrite(p, 1, len, fp) != len)
337 				goto err;
338 			ccnt += len;
339 			if (putc('\n', fp) != '\n')
340 				break;
341 			++ccnt;
342 		}
343 
344 	if (fflush(fp))
345 		goto err;
346 	/*
347 	 * XXX
348 	 * I don't trust NFS -- check to make sure that we're talking to
349 	 * a regular file and sync so that NFS is forced to flush.
350 	 */
351 	if (!fstat(fileno(fp), &sb) &&
352 	    S_ISREG(sb.st_mode) && fsync(fileno(fp)))
353 		goto err;
354 
355 	if (fclose(fp))
356 		goto err;
357 
358 	rval = 0;
359 	if (0) {
360 err:		if (!F_ISSET(sp->ep, F_MULTILOCK))
361 			msgq_str(sp, M_SYSERR, name, "%s");
362 		(void)fclose(fp);
363 		rval = 1;
364 	}
365 
366 	if (!silent)
367 		gp->scr_busy(sp, NULL, BUSY_OFF);
368 
369 	/* Report the possibly partial transfer. */
370 	if (nlno != NULL) {
371 		*nch = ccnt;
372 		*nlno = lcnt;
373 	}
374 	return (rval);
375 }
376