*** empty log message ***
[coreutils.git] / src / tac-pipe.c
blob8e21d1cc3f4e6baec6ac6cadeeeccdac12983c8c
1 /* FIXME */
2 #include <assert.h>
4 /* FIXME: this is small for testing */
5 #define BUFFER_SIZE (8)
7 #define LEN(X, I) ((X)->p[(I)].one_past_end - (X)->p[(I)].start)
8 #define EMPTY(X) ((X)->n_bufs == 1 && LEN (X, 0) == 0)
10 #define ONE_PAST_END(X, I) ((X)->p[(I)].one_past_end)
12 struct Line_ptr
14 size_t i;
15 char *ptr;
17 typedef struct Line_ptr Line_ptr;
19 struct B_pair
21 char *start;
22 char *one_past_end;
25 struct Buf
27 size_t n_bufs;
28 struct obstack obs;
29 struct B_pair *p;
31 typedef struct Buf Buf;
33 static int
34 buf_init_from_stdin (Buf *x, int eol_byte)
36 int last_byte_is_eol_byte = 1;
37 int fail = 0;
39 #define OBS (&(x->obs))
40 obstack_init (OBS);
42 while (1)
44 char *buf = (char *) malloc (BUFFER_SIZE);
45 int bytes_read;
47 if (buf == NULL)
49 /* Fall back on the code that relies on a temporary file.
50 Write all buffers to that file and free them. */
51 /* FIXME */
52 fail = 1;
53 break;
55 bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE);
56 if (bytes_read < 0)
57 error (1, errno, _("read error"));
60 struct B_pair bp;
61 bp.start = buf;
62 bp.one_past_end = buf + bytes_read;
63 obstack_grow (OBS, &bp, sizeof (bp));
66 if (bytes_read > 0)
67 last_byte_is_eol_byte = (buf[bytes_read - 1] == eol_byte);
69 if (bytes_read < BUFFER_SIZE)
70 break;
73 if (!fail)
75 /* If the file was non-empty and lacked an EOL_BYTE at its end,
76 then add a buffer containing just that one byte. */
77 if (!last_byte_is_eol_byte)
79 char *buf = malloc (1);
80 if (buf == NULL)
82 /* FIXME: just like above */
83 fail = 1;
85 else
87 struct B_pair bp;
88 *buf = eol_byte;
89 bp.start = buf;
90 bp.one_past_end = buf + 1;
91 obstack_grow (OBS, &bp, sizeof (bp));
96 x->n_bufs = obstack_object_size (OBS) / sizeof (x->p[0]);
97 x->p = (struct B_pair *) obstack_finish (OBS);
99 /* If there are two or more buffers and the last buffer is empty,
100 then free the last one and decrement the buffer count. */
101 if (x->n_bufs >= 2
102 && x->p[x->n_bufs - 1].start == x->p[x->n_bufs - 1].one_past_end)
103 free (x->p[--(x->n_bufs)].start);
105 return fail;
108 static void
109 buf_free (Buf *x)
111 int i;
112 for (i = 0; i < x->n_bufs; i++)
113 free (x->p[i].start);
114 obstack_free (OBS, NULL);
117 Line_ptr
118 line_ptr_decrement (const Buf *x, const Line_ptr *lp)
120 Line_ptr lp_new;
122 if (lp->ptr > x->p[lp->i].start)
124 lp_new.i = lp->i;
125 lp_new.ptr = lp->ptr - 1;
127 else
129 assert (lp->i > 0);
130 lp_new.i = lp->i - 1;
131 lp_new.ptr = ONE_PAST_END (x, lp->i - 1) - 1;
133 return lp_new;
136 Line_ptr
137 line_ptr_increment (const Buf *x, const Line_ptr *lp)
139 Line_ptr lp_new;
141 assert (lp->ptr <= ONE_PAST_END (x, lp->i) - 1);
142 if (lp->ptr < ONE_PAST_END (x, lp->i) - 1)
144 lp_new.i = lp->i;
145 lp_new.ptr = lp->ptr + 1;
147 else
149 assert (lp->i < x->n_bufs - 1);
150 lp_new.i = lp->i + 1;
151 lp_new.ptr = x->p[lp->i + 1].start;
153 return lp_new;
156 static int
157 find_bol (const Buf *x,
158 const Line_ptr *last_bol, Line_ptr *new_bol, char eol_byte)
160 int i;
161 Line_ptr tmp;
162 char *last_bol_ptr;
164 if (last_bol->ptr == x->p[0].start)
165 return 0;
167 tmp = line_ptr_decrement (x, last_bol);
168 last_bol_ptr = tmp.ptr;
169 i = tmp.i;
170 while (1)
172 char *nl = memrchr (x->p[i].start, last_bol_ptr, eol_byte);
173 if (nl)
175 Line_ptr nl_pos;
176 nl_pos.i = i;
177 nl_pos.ptr = nl;
178 *new_bol = line_ptr_increment (x, &nl_pos);
179 return 1;
182 if (i <= 0)
183 break;
185 --i;
186 last_bol_ptr = ONE_PAST_END (x, i);
189 /* If last_bol->ptr didn't point at the first byte of X, then reaching
190 this point means that we're about to return the line that is at the
191 beginning of X. */
192 if (last_bol->ptr != x->p[0].start)
194 new_bol->i = 0;
195 new_bol->ptr = x->p[0].start;
196 return 1;
199 return 0;
202 static void
203 print_line (FILE *out_stream, const Buf *x,
204 const Line_ptr *bol, const Line_ptr *bol_next)
206 int i;
207 for (i = bol->i; i <= bol_next->i; i++)
209 char *a = (i == bol->i ? bol->ptr : x->p[i].start);
210 char *b = (i == bol_next->i ? bol_next->ptr : ONE_PAST_END (x, i));
211 fwrite (a, 1, b - a, out_stream);
215 static int
216 tac_mem ()
218 Buf x;
219 Line_ptr bol;
220 char eol_byte = '\n';
221 int fail;
223 fail = buf_init_from_stdin (&x, eol_byte);
224 if (fail)
226 buf_free (&x);
227 return 1;
230 /* Special case the empty file. */
231 if (EMPTY (&x))
232 return 0;
234 /* Initially, point at one past the last byte of the file. */
235 bol.i = x.n_bufs - 1;
236 bol.ptr = ONE_PAST_END (&x, bol.i);
238 while (1)
240 Line_ptr new_bol;
241 int found = find_bol (&x, &bol, &new_bol, eol_byte);
242 if (!found)
243 break;
244 print_line (stdout, &x, &new_bol, &bol);
245 bol = new_bol;
247 return 0;