doc: refactor and update expand and unexpand --help
[coreutils.git] / src / tac-pipe.c
blobd31df43e442fc541723d3fde3b5b6f1fd0d4ed04
1 /* tac from a pipe.
3 Copyright (C) 1997-2017 Free Software Foundation, Inc.
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/>. */
18 /* FIXME */
19 #include <assert.h>
21 #include "die.h"
23 /* FIXME: this is small for testing */
24 #define BUFFER_SIZE (8)
26 #define LEN(X, I) ((X)->p[(I)].one_past_end - (X)->p[(I)].start)
27 #define EMPTY(X) ((X)->n_bufs == 1 && LEN (X, 0) == 0)
29 #define ONE_PAST_END(X, I) ((X)->p[(I)].one_past_end)
31 struct Line_ptr
33 size_t i;
34 char *ptr;
36 typedef struct Line_ptr Line_ptr;
38 struct B_pair
40 char *start;
41 char *one_past_end;
44 struct Buf
46 size_t n_bufs;
47 struct obstack obs;
48 struct B_pair *p;
50 typedef struct Buf Buf;
52 static bool
53 buf_init_from_stdin (Buf *x, char eol_byte)
55 bool last_byte_is_eol_byte = true;
56 bool ok = true;
58 #define OBS (&(x->obs))
59 obstack_init (OBS);
61 while (1)
63 char *buf = (char *) malloc (BUFFER_SIZE);
64 size_t bytes_read;
66 if (buf == NULL)
68 /* Fall back on the code that relies on a temporary file.
69 Write all buffers to that file and free them. */
70 /* FIXME */
71 ok = false;
72 break;
74 bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE);
75 if (bytes_read != buffer_size && errno != 0)
76 die (EXIT_FAILURE, errno, _("read error"));
79 struct B_pair bp;
80 bp.start = buf;
81 bp.one_past_end = buf + bytes_read;
82 obstack_grow (OBS, &bp, sizeof (bp));
85 if (bytes_read != 0)
86 last_byte_is_eol_byte = (buf[bytes_read - 1] == eol_byte);
88 if (bytes_read < BUFFER_SIZE)
89 break;
92 if (ok)
94 /* If the file was non-empty and lacked an EOL_BYTE at its end,
95 then add a buffer containing just that one byte. */
96 if (!last_byte_is_eol_byte)
98 char *buf = malloc (1);
99 if (buf == NULL)
101 /* FIXME: just like above */
102 ok = false;
104 else
106 struct B_pair bp;
107 *buf = eol_byte;
108 bp.start = buf;
109 bp.one_past_end = buf + 1;
110 obstack_grow (OBS, &bp, sizeof (bp));
115 x->n_bufs = obstack_object_size (OBS) / sizeof (x->p[0]);
116 x->p = (struct B_pair *) obstack_finish (OBS);
118 /* If there are two or more buffers and the last buffer is empty,
119 then free the last one and decrement the buffer count. */
120 if (x->n_bufs >= 2
121 && x->p[x->n_bufs - 1].start == x->p[x->n_bufs - 1].one_past_end)
122 free (x->p[--(x->n_bufs)].start);
124 return ok;
127 static void
128 buf_free (Buf *x)
130 size_t i;
131 for (i = 0; i < x->n_bufs; i++)
132 free (x->p[i].start);
133 obstack_free (OBS, NULL);
136 Line_ptr
137 line_ptr_decrement (const Buf *x, const Line_ptr *lp)
139 Line_ptr lp_new;
141 if (lp->ptr > x->p[lp->i].start)
143 lp_new.i = lp->i;
144 lp_new.ptr = lp->ptr - 1;
146 else
148 assert (lp->i > 0);
149 lp_new.i = lp->i - 1;
150 lp_new.ptr = ONE_PAST_END (x, lp->i - 1) - 1;
152 return lp_new;
155 Line_ptr
156 line_ptr_increment (const Buf *x, const Line_ptr *lp)
158 Line_ptr lp_new;
160 assert (lp->ptr <= ONE_PAST_END (x, lp->i) - 1);
161 if (lp->ptr < ONE_PAST_END (x, lp->i) - 1)
163 lp_new.i = lp->i;
164 lp_new.ptr = lp->ptr + 1;
166 else
168 assert (lp->i < x->n_bufs - 1);
169 lp_new.i = lp->i + 1;
170 lp_new.ptr = x->p[lp->i + 1].start;
172 return lp_new;
175 static bool
176 find_bol (const Buf *x,
177 const Line_ptr *last_bol, Line_ptr *new_bol, char eol_byte)
179 size_t i;
180 Line_ptr tmp;
181 char *last_bol_ptr;
183 if (last_bol->ptr == x->p[0].start)
184 return false;
186 tmp = line_ptr_decrement (x, last_bol);
187 last_bol_ptr = tmp.ptr;
188 i = tmp.i;
189 while (1)
191 char *nl = memrchr (x->p[i].start, last_bol_ptr, eol_byte);
192 if (nl)
194 Line_ptr nl_pos;
195 nl_pos.i = i;
196 nl_pos.ptr = nl;
197 *new_bol = line_ptr_increment (x, &nl_pos);
198 return true;
201 if (i == 0)
202 break;
204 --i;
205 last_bol_ptr = ONE_PAST_END (x, i);
208 /* If last_bol->ptr didn't point at the first byte of X, then reaching
209 this point means that we're about to return the line that is at the
210 beginning of X. */
211 if (last_bol->ptr != x->p[0].start)
213 new_bol->i = 0;
214 new_bol->ptr = x->p[0].start;
215 return true;
218 return false;
221 static void
222 print_line (FILE *out_stream, const Buf *x,
223 const Line_ptr *bol, const Line_ptr *bol_next)
225 size_t i;
226 for (i = bol->i; i <= bol_next->i; i++)
228 char *a = (i == bol->i ? bol->ptr : x->p[i].start);
229 char *b = (i == bol_next->i ? bol_next->ptr : ONE_PAST_END (x, i));
230 fwrite (a, 1, b - a, out_stream);
234 static bool
235 tac_mem ()
237 Buf x;
238 Line_ptr bol;
239 char eol_byte = '\n';
241 if (! buf_init_from_stdin (&x, eol_byte))
243 buf_free (&x);
244 return false;
247 /* Special case the empty file. */
248 if (EMPTY (&x))
249 return true;
251 /* Initially, point at one past the last byte of the file. */
252 bol.i = x.n_bufs - 1;
253 bol.ptr = ONE_PAST_END (&x, bol.i);
255 while (1)
257 Line_ptr new_bol;
258 if (! find_bol (&x, &bol, &new_bol, eol_byte))
259 break;
260 print_line (stdout, &x, &new_bol, &bol);
261 bol = new_bol;
263 return true;