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