1 /* Test of filtering of data through a subprocess.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2009.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "pipe-filter.h"
25 #include "binary-io.h"
27 #include "read-file.h"
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 /* Pipe a text file through 'tr a-z A-Z', which converts ASCII characters from
44 lower case to upper case. */
54 prepare_read (size_t *num_bytes_p, void *private_data)
56 struct locals *l = (struct locals *) private_data;
57 *num_bytes_p = sizeof (l->buf);
62 done_read (void *data_read, size_t num_bytes_read, void *private_data)
64 struct locals *l = (struct locals *) private_data;
65 const char *p = l->input + l->nread;
66 const char *q = (const char *) data_read;
69 for (i = 0; i < num_bytes_read; i++, q++)
71 /* Handle conversion NL -> CRLF possibly done by the child process. */
72 if (!(O_BINARY && *q == '\r'))
75 char expected = c_toupper (orig);
76 ASSERT (*q == expected);
80 l->nread = p - l->input;
84 main (int argc, char *argv[])
86 const char *tr_program;
87 const char *input_filename;
91 set_program_name (argv[0]);
97 /* Read some text from a file. */
98 input_filename = argv[2];
99 input = read_binary_file (input_filename, &input_size);
100 ASSERT (input != NULL);
102 /* Convert it to uppercase, line by line. */
106 struct pipe_filter_gi *f;
112 argv[0] = tr_program;
117 f = pipe_filter_gi_create ("tr", tr_program, argv, false, true,
118 prepare_read, done_read, &l);
120 result = pipe_filter_gi_write (f, input, input_size);
121 ASSERT (result == 0);
122 result = pipe_filter_gi_close (f);
123 ASSERT (result == 0);
124 ASSERT (l.nread == input_size);