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