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