b1f94e114f4dd9c0905504ca5bd0ea3864c30389
[gnulib.git] / tests / test-pipe-filter-gi1.c
1 /* Test of filtering of data through a subprocess.
2    Copyright (C) 2009-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2009.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 #include "pipe-filter.h"
21
22 #include "binary-io.h"
23 #include "c-ctype.h"
24 #include "read-file.h"
25 #include "progname.h"
26 #include "macros.h"
27
28
29 /* Pipe a text file through 'tr "[a-z]" "[A-Z]"', which converts ASCII
30    characters from lower case to upper case.  */
31
32 struct locals
33 {
34   const char *input;
35   size_t nread;
36   char buf[19];
37 };
38
39 static void *
40 prepare_read (size_t *num_bytes_p, void *private_data)
41 {
42   struct locals *l = (struct locals *) private_data;
43   *num_bytes_p = sizeof (l->buf);
44   return l->buf;
45 }
46
47 static void
48 done_read (void *data_read, size_t num_bytes_read, void *private_data)
49 {
50   struct locals *l = (struct locals *) private_data;
51   const char *p = l->input + l->nread;
52   const char *q = (const char *) data_read;
53   size_t i;
54
55   for (i = 0; i < num_bytes_read; i++, q++)
56     {
57       /* Handle conversion NL -> CRLF possibly done by the child process.  */
58       if (!(O_BINARY && *q == '\r'))
59         {
60           char orig = *p;
61           char expected = c_toupper (orig);
62           ASSERT (*q == expected);
63           p++;
64         }
65     }
66   l->nread = p - l->input;
67 }
68
69 int
70 main (int argc, char *argv[])
71 {
72   const char *tr_program;
73   const char *input_filename;
74   size_t input_size;
75   char *input;
76
77   set_program_name (argv[0]);
78
79   ASSERT (argc == 3);
80
81   tr_program = argv[1];
82
83   /* Read some text from a file.  */
84   input_filename = argv[2];
85   input = read_binary_file (input_filename, &input_size);
86   ASSERT (input != NULL);
87
88   /* Convert it to uppercase, line by line.  */
89   {
90     const char *argv[4];
91     struct locals l;
92     struct pipe_filter_gi *f;
93     int result;
94
95     l.input = input;
96     l.nread = 0;
97
98     argv[0] = tr_program;
99     argv[1] = "[a-z]";
100     argv[2] = "[A-Z]";
101     argv[3] = NULL;
102
103     f = pipe_filter_gi_create ("tr", tr_program, argv, false, true,
104                                prepare_read, done_read, &l);
105     ASSERT (f != NULL);
106     result = pipe_filter_gi_write (f, input, input_size);
107     ASSERT (result == 0);
108     result = pipe_filter_gi_close (f);
109     ASSERT (result == 0);
110     ASSERT (l.nread == input_size);
111   }
112
113   return 0;
114 }