Upload Tizen:Base source
[framework/base/util-linux-ng.git] / text-utils / col.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Michael Rendell of the Memorial University of Newfoundland.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * Wed Jun 22 22:15:41 1994, faith@cs.unc.edu: Added internationalization
37  *                           patches from Andries.Brouwer@cwi.nl
38  * Wed Sep 14 22:31:17 1994: patches from Carl Christofferson
39  *                           (cchris@connected.com)
40  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
41  *      added Native Language Support
42  * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
43  *      modified to work correctly in multi-byte locales
44  *
45  */
46
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include "nls.h"
54
55 #include "widechar.h"
56
57 #define BS      '\b'            /* backspace */
58 #define TAB     '\t'            /* tab */
59 #define SPACE   ' '             /* space */
60 #define NL      '\n'            /* newline */
61 #define CR      '\r'            /* carriage return */
62 #define ESC     '\033'          /* escape */
63 #define SI      '\017'          /* shift in to normal character set */
64 #define SO      '\016'          /* shift out to alternate character set */
65 #define VT      '\013'          /* vertical tab (aka reverse line feed) */
66 #define RLF     '\007'          /* ESC-07 reverse line feed */
67 #define RHLF    '\010'          /* ESC-010 reverse half-line feed */
68 #define FHLF    '\011'          /* ESC-011 forward half-line feed */
69
70 /* build up at least this many lines before flushing them out */
71 #define BUFFER_MARGIN           32
72
73 typedef char CSET;
74
75 typedef struct char_str {
76 #define CS_NORMAL       1
77 #define CS_ALTERNATE    2
78         short           c_column;       /* column character is in */
79         CSET            c_set;          /* character set (currently only 2) */
80         wchar_t         c_char;         /* character in question */
81         int             c_width;        /* character width */
82 } CHAR;
83
84 typedef struct line_str LINE;
85 struct line_str {
86         CHAR    *l_line;                /* characters on the line */
87         LINE    *l_prev;                /* previous line */
88         LINE    *l_next;                /* next line */
89         int     l_lsize;                /* allocated sizeof l_line */
90         int     l_line_len;             /* strlen(l_line) */
91         int     l_needs_sort;           /* set if chars went in out of order */
92         int     l_max_col;              /* max column in the line */
93 };
94
95 void usage(void);
96 void wrerr(void);
97 void warn(int);
98 void free_line(LINE *l);
99 void flush_line(LINE *l);
100 void flush_lines(int);
101 void flush_blanks(void);
102 void *xmalloc(void *p, size_t size);
103 LINE *alloc_line(void);
104
105 CSET last_set;                  /* char_set of last char printed */
106 LINE *lines;
107 int compress_spaces;            /* if doing space -> tab conversion */
108 int fine;                       /* if `fine' resolution (half lines) */
109 int max_bufd_lines;             /* max # lines to keep in memory */
110 int nblank_lines;               /* # blanks after last flushed line */
111 int no_backspaces;              /* if not to output any backspaces */
112 int pass_unknown_seqs;          /* whether to pass unknown control sequences */
113
114 #define PUTC(ch) \
115         if (putwchar(ch) == WEOF) \
116                 wrerr();
117
118 int main(int argc, char **argv)
119 {
120         register wint_t ch;
121         CHAR *c;
122         CSET cur_set;                   /* current character set */
123         LINE *l;                        /* current line */
124         int extra_lines;                /* # of lines above first line */
125         int cur_col;                    /* current column */
126         int cur_line;                   /* line number of current position */
127         int max_line;                   /* max value of cur_line */
128         int this_line;                  /* line l points to */
129         int nflushd_lines;              /* number of lines that were flushed */
130         int adjust, opt, warned;
131         int ret = 0;
132
133         setlocale(LC_ALL, "");
134         bindtextdomain(PACKAGE, LOCALEDIR);
135         textdomain(PACKAGE);
136         
137         max_bufd_lines = 128;
138         compress_spaces = 1;            /* compress spaces into tabs */
139         pass_unknown_seqs = 0;          /* remove unknown escape sequences */
140         while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
141                 switch (opt) {
142                 case 'b':               /* do not output backspaces */
143                         no_backspaces = 1;
144                         break;
145                 case 'f':               /* allow half forward line feeds */
146                         fine = 1;
147                         break;
148                 case 'h':               /* compress spaces into tabs */
149                         compress_spaces = 1;
150                         break;
151                 case 'l':               /* buffered line count */
152                         if ((max_bufd_lines = atoi(optarg)) <= 0) {
153                                 (void)fprintf(stderr,
154                                     _("col: bad -l argument %s.\n"), optarg);
155                                 exit(1);
156                         }
157                         break;
158                 case 'p':
159                         pass_unknown_seqs = 1;
160                         break;
161                 case 'x':               /* do not compress spaces into tabs */
162                         compress_spaces = 0;
163                         break;
164                 case '?':
165                 default:
166                         usage();
167                 }
168
169         if (optind != argc)
170                 usage();
171
172         /* this value is in half lines */
173         max_bufd_lines *= 2;
174
175         adjust = cur_col = extra_lines = warned = 0;
176         cur_line = max_line = nflushd_lines = this_line = 0;
177         cur_set = last_set = CS_NORMAL;
178         lines = l = alloc_line();
179         
180         while (feof(stdin)==0) {
181                 errno = 0;
182                 if ((ch = getwchar()) == WEOF) {
183                         if (errno==EILSEQ) {
184                                 perror("col");
185                                 ret = 1;
186                         }
187                         break;
188                 }       
189                 if (!iswgraph(ch)) {
190                         switch (ch) {
191                         case BS:                /* can't go back further */
192                                 if (cur_col == 0)
193                                         continue;
194                                 --cur_col;
195                                 continue;
196                         case CR:
197                                 cur_col = 0;
198                                 continue;
199                         case ESC:               /* just ignore EOF */
200                                 switch(getwchar()) {
201                                 case RLF:
202                                         cur_line -= 2;
203                                         break;
204                                 case RHLF:
205                                         cur_line--;
206                                         break;
207                                 case FHLF:
208                                         cur_line++;
209                                         if (cur_line > max_line)
210                                                 max_line = cur_line;
211                                 }
212                                 continue;
213                         case NL:
214                                 cur_line += 2;
215                                 if (cur_line > max_line)
216                                         max_line = cur_line;
217                                 cur_col = 0;
218                                 continue;
219                         case SPACE:
220                                 ++cur_col;
221                                 continue;
222                         case SI:
223                                 cur_set = CS_NORMAL;
224                                 continue;
225                         case SO:
226                                 cur_set = CS_ALTERNATE;
227                                 continue;
228                         case TAB:               /* adjust column */
229                                 cur_col |= 7;
230                                 ++cur_col;
231                                 continue;
232                         case VT:
233                                 cur_line -= 2;
234                                 continue;
235                         }
236                         if (iswspace(ch)) {
237                                 if (wcwidth(ch) > 0)
238                                         cur_col += wcwidth(ch);
239                                 continue;
240                         }
241                         if (!pass_unknown_seqs)
242                                 continue;
243                 }
244
245                 /* Must stuff ch in a line - are we at the right one? */
246                 if (cur_line != this_line - adjust) {
247                         LINE *lnew;
248                         int nmove;
249
250                         adjust = 0;
251                         nmove = cur_line - this_line;
252                         if (!fine) {
253                                 /* round up to next line */
254                                 if (cur_line & 1) {
255                                         adjust = 1;
256                                         nmove++;
257                                 }
258                         }
259                         if (nmove < 0) {
260                                 for (; nmove < 0 && l->l_prev; nmove++)
261                                         l = l->l_prev;
262                                 if (nmove) {
263                                         if (nflushd_lines == 0) {
264                                                 /*
265                                                  * Allow backup past first
266                                                  * line if nothing has been
267                                                  * flushed yet.
268                                                  */
269                                                 for (; nmove < 0; nmove++) {
270                                                         lnew = alloc_line();
271                                                         l->l_prev = lnew;
272                                                         lnew->l_next = l;
273                                                         l = lines = lnew;
274                                                         extra_lines++;
275                                                 }
276                                         } else {
277                                                 if (!warned++)
278                                                         warn(cur_line);
279                                                 cur_line -= nmove;
280                                         }
281                                 }
282                         } else {
283                                 /* may need to allocate here */
284                                 for (; nmove > 0 && l->l_next; nmove--)
285                                         l = l->l_next;
286                                 for (; nmove > 0; nmove--) {
287                                         lnew = alloc_line();
288                                         lnew->l_prev = l;
289                                         l->l_next = lnew;
290                                         l = lnew;
291                                 }
292                         }
293                         this_line = cur_line + adjust;
294                         nmove = this_line - nflushd_lines;
295                         if (nmove >= max_bufd_lines + BUFFER_MARGIN) {
296                                 nflushd_lines += nmove - max_bufd_lines;
297                                 flush_lines(nmove - max_bufd_lines);
298                         }
299                 }
300                 /* grow line's buffer? */
301                 if (l->l_line_len + 1 >= l->l_lsize) {
302                         int need;
303
304                         need = l->l_lsize ? l->l_lsize * 2 : 90;
305                         l->l_line = (CHAR *)xmalloc((void *) l->l_line,
306                             (unsigned) need * sizeof(CHAR));
307                         l->l_lsize = need;
308                 }
309                 c = &l->l_line[l->l_line_len++];
310                 c->c_char = ch;
311                 c->c_set = cur_set;
312                 c->c_column = cur_col;
313                 c->c_width = wcwidth(ch);
314                 /*
315                  * If things are put in out of order, they will need sorting
316                  * when it is flushed.
317                  */
318                 if (cur_col < l->l_max_col)
319                         l->l_needs_sort = 1;
320                 else
321                         l->l_max_col = cur_col;
322                 if (c->c_width > 0)
323                         cur_col += c->c_width;
324         }
325         /* goto the last line that had a character on it */
326         for (; l->l_next; l = l->l_next)
327                 this_line++;
328         flush_lines(this_line - nflushd_lines + extra_lines + 1);
329
330         /* make sure we leave things in a sane state */
331         if (last_set != CS_NORMAL)
332                 PUTC('\017');
333
334         /* flush out the last few blank lines */
335         nblank_lines = max_line - this_line;
336         if (max_line & 1)
337                 nblank_lines++;
338         else if (!nblank_lines)
339                 /* missing a \n on the last line? */
340                 nblank_lines = 2;
341         flush_blanks();
342         if (ferror(stdout) || fclose(stdout))
343                 return 1;
344         return ret;
345 }
346
347 void flush_lines(int nflush)
348 {
349         LINE *l;
350
351         while (--nflush >= 0) {
352                 l = lines;
353                 lines = l->l_next;
354                 if (l->l_line) {
355                         flush_blanks();
356                         flush_line(l);
357                 }
358                 nblank_lines++;
359                 if (l->l_line)
360                         (void)free((void *)l->l_line);
361                 free_line(l);
362         }
363         if (lines)
364                 lines->l_prev = NULL;
365 }
366
367 /*
368  * Print a number of newline/half newlines.  If fine flag is set, nblank_lines
369  * is the number of half line feeds, otherwise it is the number of whole line
370  * feeds.
371  */
372 void flush_blanks()
373 {
374         int half, i, nb;
375
376         half = 0;
377         nb = nblank_lines;
378         if (nb & 1) {
379                 if (fine)
380                         half = 1;
381                 else
382                         nb++;
383         }
384         nb /= 2;
385         for (i = nb; --i >= 0;)
386                 PUTC('\n');
387         if (half) {
388                 PUTC('\033');
389                 PUTC('9');
390                 if (!nb)
391                         PUTC('\r');
392         }
393         nblank_lines = 0;
394 }
395
396 /*
397  * Write a line to stdout taking care of space to tab conversion (-h flag)
398  * and character set shifts.
399  */
400 void flush_line(LINE *l)
401 {
402         CHAR *c, *endc;
403         int nchars, last_col, this_col;
404
405         last_col = 0;
406         nchars = l->l_line_len;
407
408         if (l->l_needs_sort) {
409                 static CHAR *sorted;
410                 static int count_size, *count, i, save, sorted_size, tot;
411
412                 /*
413                  * Do an O(n) sort on l->l_line by column being careful to
414                  * preserve the order of characters in the same column.
415                  */
416                 if (l->l_lsize > sorted_size) {
417                         sorted_size = l->l_lsize;
418                         sorted = (CHAR *)xmalloc((void *)sorted,
419                             (unsigned)sizeof(CHAR) * sorted_size);
420                 }
421                 if (l->l_max_col >= count_size) {
422                         count_size = l->l_max_col + 1;
423                         count = (int *)xmalloc((void *)count,
424                             (unsigned)sizeof(int) * count_size);
425                 }
426                 memset(count, 0, sizeof(int) * l->l_max_col + 1);
427                 for (i = nchars, c = l->l_line; --i >= 0; c++)
428                         count[c->c_column]++;
429
430                 /*
431                  * calculate running total (shifted down by 1) to use as
432                  * indices into new line.
433                  */
434                 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
435                         save = count[i];
436                         count[i] = tot;
437                         tot += save;
438                 }
439
440                 for (i = nchars, c = l->l_line; --i >= 0; c++)
441                         sorted[count[c->c_column]++] = *c;
442                 c = sorted;
443         } else
444                 c = l->l_line;
445         while (nchars > 0) {
446                 this_col = c->c_column;
447                 endc = c;
448                 do {
449                         ++endc;
450                 } while (--nchars > 0 && this_col == endc->c_column);
451
452                 /* if -b only print last character */
453                 if (no_backspaces) {
454                         c = endc - 1;
455                         if (nchars > 0 &&
456                             this_col + c->c_width > endc->c_column)
457                                 continue;
458                 }
459
460                 if (this_col > last_col) {
461                         int nspace = this_col - last_col;
462
463                         if (compress_spaces && nspace > 1) {
464                                 int ntabs;
465
466                                 ntabs = this_col / 8 - last_col / 8;
467                                 if (ntabs > 0) {
468                                         nspace = this_col & 7;
469                                         while (--ntabs >= 0)
470                                                 PUTC('\t');
471                                 }
472                         }
473                         while (--nspace >= 0)
474                                 PUTC(' ');
475                         last_col = this_col;
476                 }
477
478                 for (;;) {
479                         if (c->c_set != last_set) {
480                                 switch (c->c_set) {
481                                 case CS_NORMAL:
482                                         PUTC('\017');
483                                         break;
484                                 case CS_ALTERNATE:
485                                         PUTC('\016');
486                                 }
487                                 last_set = c->c_set;
488                         }
489                         PUTC(c->c_char);
490                         if ((c+1) < endc) {
491                                 int i;
492                                 for (i=0; i < c->c_width; i++)
493                                         PUTC('\b');
494                         }
495                         if (++c >= endc)
496                                 break;
497                 }
498                 last_col += (c-1)->c_width;
499         }
500 }
501
502 #define NALLOC 64
503
504 static LINE *line_freelist;
505
506 LINE *
507 alloc_line()
508 {
509         LINE *l;
510         int i;
511
512         if (!line_freelist) {
513                 l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC);
514                 line_freelist = l;
515                 for (i = 1; i < NALLOC; i++, l++)
516                         l->l_next = l + 1;
517                 l->l_next = NULL;
518         }
519         l = line_freelist;
520         line_freelist = l->l_next;
521
522         memset(l, 0, sizeof(LINE));
523         return(l);
524 }
525
526 void free_line(LINE *l)
527 {
528         l->l_next = line_freelist;
529         line_freelist = l;
530 }
531
532 void *
533 xmalloc(void *p, size_t size)
534 {
535         if (!(p = (void *)realloc(p, size))) {
536                 (void)fprintf(stderr, "col: %s.\n", strerror(ENOMEM));
537                 exit(1);
538         }
539         return(p);
540 }
541
542 void usage()
543 {
544         (void)fprintf(stderr, _("usage: col [-bfpx] [-l nline]\n"));
545         exit(1);
546 }
547
548 void wrerr()
549 {
550         (void)fprintf(stderr, _("col: write error.\n"));
551         exit(1);
552 }
553
554 void warn(int line)
555 {
556         (void)fprintf(stderr,
557             _("col: warning: can't back up %s.\n"), line < 0 ?
558             _("past first line") : _("-- line already flushed"));
559 }