9263df7630537182334e52185461a5df64619be4
[platform/upstream/coreutils.git] / src / tac-pipe.c
1 /* FIXME */
2 #include <assert.h>
3
4 /* FIXME: this is small for testing */
5 #define BUFFER_SIZE (8)
6
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)
9
10 #define ONE_PAST_END(X, I) ((X)->p[(I)].one_past_end)
11
12 struct Line_ptr
13 {
14   size_t i;
15   char *ptr;
16 };
17 typedef struct Line_ptr Line_ptr;
18
19 struct B_pair
20 {
21   char *start;
22   char *one_past_end;
23 };
24
25 struct Buf
26 {
27   size_t n_bufs;
28   struct obstack obs;
29   struct B_pair *p;
30 };
31 typedef struct Buf Buf;
32
33 static bool
34 buf_init_from_stdin (Buf *x, char eol_byte)
35 {
36   bool last_byte_is_eol_byte = true;
37   bool ok = true;
38
39 #define OBS (&(x->obs))
40   obstack_init (OBS);
41
42   while (1)
43     {
44       char *buf = (char *) malloc (BUFFER_SIZE);
45       size_t bytes_read;
46
47       if (buf == NULL)
48         {
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           ok = false;
53           break;
54         }
55       bytes_read = full_read (STDIN_FILENO, buf, BUFFER_SIZE);
56       if (bytes_read != buffer_size && errno != 0)
57         error (EXIT_FAILURE, errno, _("read error"));
58
59       {
60         struct B_pair bp;
61         bp.start = buf;
62         bp.one_past_end = buf + bytes_read;
63         obstack_grow (OBS, &bp, sizeof (bp));
64       }
65
66       if (bytes_read != 0)
67         last_byte_is_eol_byte = (buf[bytes_read - 1] == eol_byte);
68
69       if (bytes_read < BUFFER_SIZE)
70         break;
71     }
72
73   if (ok)
74     {
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)
78         {
79           char *buf = malloc (1);
80           if (buf == NULL)
81             {
82               /* FIXME: just like above */
83               ok = false;
84             }
85           else
86             {
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));
92             }
93         }
94     }
95
96   x->n_bufs = obstack_object_size (OBS) / sizeof (x->p[0]);
97   x->p = (struct B_pair *) obstack_finish (OBS);
98
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);
104
105   return ok;
106 }
107
108 static void
109 buf_free (Buf *x)
110 {
111   size_t i;
112   for (i = 0; i < x->n_bufs; i++)
113     free (x->p[i].start);
114   obstack_free (OBS, NULL);
115 }
116
117 Line_ptr
118 line_ptr_decrement (const Buf *x, const Line_ptr *lp)
119 {
120   Line_ptr lp_new;
121
122   if (lp->ptr > x->p[lp->i].start)
123     {
124       lp_new.i = lp->i;
125       lp_new.ptr = lp->ptr - 1;
126     }
127   else
128     {
129       assert (lp->i > 0);
130       lp_new.i = lp->i - 1;
131       lp_new.ptr = ONE_PAST_END (x, lp->i - 1) - 1;
132     }
133   return lp_new;
134 }
135
136 Line_ptr
137 line_ptr_increment (const Buf *x, const Line_ptr *lp)
138 {
139   Line_ptr lp_new;
140
141   assert (lp->ptr <= ONE_PAST_END (x, lp->i) - 1);
142   if (lp->ptr < ONE_PAST_END (x, lp->i) - 1)
143     {
144       lp_new.i = lp->i;
145       lp_new.ptr = lp->ptr + 1;
146     }
147   else
148     {
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;
152     }
153   return lp_new;
154 }
155
156 static bool
157 find_bol (const Buf *x,
158           const Line_ptr *last_bol, Line_ptr *new_bol, char eol_byte)
159 {
160   size_t i;
161   Line_ptr tmp;
162   char *last_bol_ptr;
163
164   if (last_bol->ptr == x->p[0].start)
165     return false;
166
167   tmp = line_ptr_decrement (x, last_bol);
168   last_bol_ptr = tmp.ptr;
169   i = tmp.i;
170   while (1)
171     {
172       char *nl = memrchr (x->p[i].start, last_bol_ptr, eol_byte);
173       if (nl)
174         {
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 true;
180         }
181
182       if (i == 0)
183         break;
184
185       --i;
186       last_bol_ptr = ONE_PAST_END (x, i);
187     }
188
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)
193     {
194       new_bol->i = 0;
195       new_bol->ptr = x->p[0].start;
196       return true;
197     }
198
199   return false;
200 }
201
202 static void
203 print_line (FILE *out_stream, const Buf *x,
204             const Line_ptr *bol, const Line_ptr *bol_next)
205 {
206   size_t i;
207   for (i = bol->i; i <= bol_next->i; i++)
208     {
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);
212     }
213 }
214
215 static bool
216 tac_mem ()
217 {
218   Buf x;
219   Line_ptr bol;
220   char eol_byte = '\n';
221
222   if (! buf_init_from_stdin (&x, eol_byte))
223     {
224       buf_free (&x);
225       return false;
226     }
227
228   /* Special case the empty file.  */
229   if (EMPTY (&x))
230     return true;
231
232   /* Initially, point at one past the last byte of the file.  */
233   bol.i = x.n_bufs - 1;
234   bol.ptr = ONE_PAST_END (&x, bol.i);
235
236   while (1)
237     {
238       Line_ptr new_bol;
239       if (! find_bol (&x, &bol, &new_bol, eol_byte))
240         break;
241       print_line (stdout, &x, &new_bol, &bol);
242       bol = new_bol;
243     }
244   return true;
245 }