maint: update copyright
[gnulib.git] / tests / test-pipe-filter-ii1.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 size;
37   size_t nwritten;
38   size_t nread;
39   char buf[19];
40 };
41
42 static const void *
43 prepare_write (size_t *num_bytes_p, void *private_data)
44 {
45   struct locals *l = (struct locals *) private_data;
46   if (l->nwritten < l->size)
47     {
48       *num_bytes_p = l->size - l->nwritten;
49       return l->input + l->nwritten;
50     }
51   else
52     return NULL;
53 }
54
55 static void
56 done_write (void *data_written, size_t num_bytes_written, void *private_data)
57 {
58   struct locals *l = (struct locals *) private_data;
59   l->nwritten += num_bytes_written;
60 }
61
62 static void *
63 prepare_read (size_t *num_bytes_p, void *private_data)
64 {
65   struct locals *l = (struct locals *) private_data;
66   *num_bytes_p = sizeof (l->buf);
67   return l->buf;
68 }
69
70 static void
71 done_read (void *data_read, size_t num_bytes_read, void *private_data)
72 {
73   struct locals *l = (struct locals *) private_data;
74   const char *p = l->input + l->nread;
75   const char *q = (const char *) data_read;
76   size_t i;
77
78   for (i = 0; i < num_bytes_read; i++, q++)
79     {
80       /* Handle conversion NL -> CRLF possibly done by the child process.  */
81       if (!(O_BINARY && *q == '\r'))
82         {
83           char orig = *p;
84           char expected = c_toupper (orig);
85           ASSERT (*q == expected);
86           p++;
87         }
88     }
89   l->nread = p - l->input;
90 }
91
92 int
93 main (int argc, char *argv[])
94 {
95   const char *tr_program;
96   const char *input_filename;
97   size_t input_size;
98   char *input;
99
100   set_program_name (argv[0]);
101
102   ASSERT (argc == 3);
103
104   tr_program = argv[1];
105
106   /* Read some text from a file.  */
107   input_filename = argv[2];
108   input = read_binary_file (input_filename, &input_size);
109   ASSERT (input != NULL);
110
111   /* Convert it to uppercase, line by line.  */
112   {
113     const char *argv[4];
114     struct locals l;
115     int result;
116
117     l.input = input;
118     l.size = input_size;
119     l.nwritten = 0;
120     l.nread = 0;
121
122     argv[0] = tr_program;
123     argv[1] = "abcdefghijklmnopqrstuvwxyz";
124     argv[2] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
125     argv[3] = NULL;
126
127     result = pipe_filter_ii_execute ("tr", tr_program, argv, false, true,
128                                      prepare_write, done_write,
129                                      prepare_read, done_read,
130                                      &l);
131     ASSERT (result == 0);
132     ASSERT (l.nwritten == input_size);
133     ASSERT (l.nread == input_size);
134   }
135
136   return 0;
137 }