2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: ex_filter.c,v 10.44 2003/11/05 17:11:54 skimo Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
28 #include "../common/common.h"
30 static int filter_ldisplay(SCR
*, FILE *);
34 * Run a range of lines through a filter utility and optionally
35 * replace the original text with the stdout/stderr output of
38 * PUBLIC: int ex_filter(SCR *,
39 * PUBLIC: EXCMD *, MARK *, MARK *, MARK *, CHAR_T *, enum filtertype);
42 ex_filter(SCR
*sp
, EXCMD
*cmdp
, MARK
*fm
, MARK
*tm
, MARK
*rp
, CHAR_T
*cmd
, enum filtertype ftype
)
45 pid_t parent_writer_pid
, utility_pid
;
47 int input
[2], output
[2], rval
;
54 /* Set return cursor position, which is never less than line 1. */
59 /* We're going to need a shell. */
60 if (opts_empty(sp
, O_SHELL
, 0))
64 * There are three different processes running through this code.
65 * They are the utility, the parent-writer and the parent-reader.
66 * The parent-writer is the process that writes from the file to
67 * the utility, the parent reader is the process that reads from
70 * Input and output are named from the utility's point of view.
71 * The utility reads from input[0] and the parent(s) write to
72 * input[1]. The parent(s) read from output[0] and the utility
73 * writes to output[1].
76 * Historically, in the FILTER_READ case, the utility reads from
77 * the terminal (e.g. :r! cat works). Otherwise open up utility
81 input
[0] = input
[1] = output
[0] = output
[1] = -1;
82 if (ftype
!= FILTER_READ
&& pipe(input
) < 0) {
83 msgq(sp
, M_SYSERR
, "pipe");
87 /* Open up utility output pipe. */
88 if (pipe(output
) < 0) {
89 msgq(sp
, M_SYSERR
, "pipe");
92 if ((ofp
= fdopen(output
[0], "r")) == NULL
) {
93 msgq(sp
, M_SYSERR
, "fdopen");
97 /* Fork off the utility process. */
98 switch (utility_pid
= vfork()) {
100 msgq(sp
, M_SYSERR
, "vfork");
101 err
: if (input
[0] != -1)
102 (void)close(input
[0]);
104 (void)close(input
[1]);
107 else if (output
[0] != -1)
108 (void)close(output
[0]);
110 (void)close(output
[1]);
112 case 0: /* Utility. */
114 * Redirect stdin from the read end of the input pipe, and
115 * redirect stdout/stderr to the write end of the output pipe.
118 * Historically, ex only directed stdout into the input pipe,
119 * letting stderr come out on the terminal as usual. Vi did
120 * not, directing both stdout and stderr into the input pipe.
121 * We match that practice in both ex and vi for consistency.
124 (void)dup2(input
[0], STDIN_FILENO
);
125 (void)dup2(output
[1], STDOUT_FILENO
);
126 (void)dup2(output
[1], STDERR_FILENO
);
128 /* Close the utility's file descriptors. */
130 (void)close(input
[0]);
132 (void)close(input
[1]);
133 (void)close(output
[0]);
134 (void)close(output
[1]);
136 if ((name
= strrchr(O_STR(sp
, O_SHELL
), '/')) == NULL
)
137 name
= O_STR(sp
, O_SHELL
);
141 INT2CHAR(sp
, cmd
, STRLEN(cmd
)+1, np
, nlen
);
142 execl(O_STR(sp
, O_SHELL
), name
, "-c", np
, (char *)NULL
);
143 msgq_str(sp
, M_SYSERR
, O_STR(sp
, O_SHELL
), "execl: %s");
146 default: /* Parent-reader, parent-writer. */
147 /* Close the pipe ends neither parent will use. */
149 (void)close(input
[0]);
150 (void)close(output
[1]);
155 * FILTER_RBANG, FILTER_READ:
157 * Reading is the simple case -- we don't need a parent writer,
158 * so the parent reads the output from the read end of the output
159 * pipe until it finishes, then waits for the child. Ex_readfp
160 * appends to the MARK, and closes ofp.
162 * For FILTER_RBANG, there is nothing to write to the utility.
163 * Make sure it doesn't wait forever by closing its standard
167 * Set the return cursor to the last line read in for FILTER_READ.
168 * Historically, this behaves differently from ":r file" command,
169 * which leaves the cursor at the first line read in. Check to
170 * make sure that it's not past EOF because we were reading into an
173 if (ftype
== FILTER_RBANG
|| ftype
== FILTER_READ
) {
174 if (ftype
== FILTER_RBANG
)
175 (void)close(input
[1]);
177 if (ex_readfp(sp
, "filter", ofp
, fm
, &nread
, 1))
179 sp
->rptlines
[L_ADDED
] += nread
;
180 if (ftype
== FILTER_READ
)
189 * FILTER_BANG, FILTER_WRITE
191 * Here we need both a reader and a writer. Temporary files are
192 * expensive and we'd like to avoid disk I/O. Using pipes has the
193 * obvious starvation conditions. It's done as follows:
201 * read lines into the file
204 * read and display lines
208 * We get away without locking the underlying database because we know
209 * that none of the records that we're reading will be modified until
210 * after we've read them. This depends on the fact that the current
211 * B+tree implementation doesn't balance pages or similar things when
212 * it inserts new records. When the DB code has locking, we should
213 * treat vi as if it were multiple applications sharing a database, and
214 * do the required locking. If necessary a work-around would be to do
215 * explicit locking in the line.c:db_get() code, based on the flag set
218 F_SET(sp
->ep
, F_MULTILOCK
);
219 switch (parent_writer_pid
= fork()) {
220 case -1: /* Error. */
221 msgq(sp
, M_SYSERR
, "fork");
222 (void)close(input
[1]);
223 (void)close(output
[0]);
226 case 0: /* Parent-writer. */
228 * Write the selected lines to the write end of the input
229 * pipe. This instance of ifp is closed by ex_writefp.
231 (void)close(output
[0]);
232 if ((ifp
= fdopen(input
[1], "w")) == NULL
)
234 _exit(ex_writefp(sp
, "filter", ifp
, fm
, tm
, NULL
, NULL
, 1));
237 default: /* Parent-reader. */
238 (void)close(input
[1]);
239 if (ftype
== FILTER_WRITE
) {
241 * Read the output from the read end of the output
242 * pipe and display it. Filter_ldisplay closes ofp.
244 if (filter_ldisplay(sp
, ofp
))
248 * Read the output from the read end of the output
249 * pipe. Ex_readfp appends to the MARK and closes
252 if (ex_readfp(sp
, "filter", ofp
, tm
, &nread
, 1))
254 sp
->rptlines
[L_ADDED
] += nread
;
257 /* Wait for the parent-writer. */
259 (long)parent_writer_pid
, "parent-writer", 0, 1))
262 /* Delete any lines written to the utility. */
263 if (rval
== 0 && ftype
== FILTER_BANG
&&
264 (cut(sp
, NULL
, fm
, tm
, CUT_LINEMODE
) ||
265 del(sp
, fm
, tm
, 1))) {
271 * If the filter had no output, we may have just deleted
272 * the cursor. Don't do any real error correction, we'll
273 * try and recover later.
275 if (rp
->lno
> 1 && !db_exist(sp
, rp
->lno
))
279 F_CLR(sp
->ep
, F_MULTILOCK
);
283 * Ignore errors on vi file reads, to make reads prettier. It's
284 * completely inconsistent, and historic practice.
286 uwait
: INT2CHAR(sp
, cmd
, STRLEN(cmd
) + 1, np
, nlen
);
287 return (proc_wait(sp
, (long)utility_pid
, np
,
288 ftype
== FILTER_READ
&& F_ISSET(sp
, SC_VI
) ? 1 : 0, 0) || rval
);
293 * Display output from a utility.
296 * Historically, the characters were passed unmodified to the terminal.
297 * We use the ex print routines to make sure they're printable.
300 filter_ldisplay(SCR
*sp
, FILE *fp
)
308 for (exp
= EXP(sp
); !ex_getline(sp
, fp
, &len
) && !INTERRUPTED(sp
);) {
309 FILE2INT5(sp
, exp
->ibcw
, exp
->ibp
, len
, wp
, wlen
);
310 if (ex_ldisplay(sp
, wp
, wlen
, 0, 0))
314 msgq(sp
, M_SYSERR
, "filter read");