]>
cygwin.com Git - cygwin-apps/cygutils.git/blob - src/col/col.c
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Michael Rendell of the Memorial University of Newfoundland.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* 2002-05-22 David A. Willis: modified license to the 'BSD no advert'
34 * license, as required by the Director of the Office of
35 * Technology Licensing of the University of California on
37 * See http://www.opensource.org/licenses/bsd-license.html
38 * 2002-05-22 David A. Willis: "Ported" code for compilation under
39 * ix86-pc-cygwin (just added the #if HAVE_CONFIG_H block,
44 static char copyright
[] =
45 "@(#) Copyright (c) 1990, 1993, 1994\n\
46 The Regents of the University of California. All rights reserved.\n";
50 static char sccsid
[] = "@(#)col.c 8.3 (Berkeley) 4/2/94";
61 /* These headers are included via common.h, except for <err.h> */
62 /* err.h is located in CWD, so include using " " instead of < > */
73 #endif /* HAVE_UNISTD_H */
77 #define BS '\b' /* backspace */
78 #define TAB '\t' /* tab */
79 #define SPACE ' ' /* space */
80 #define NL '\n' /* newline */
81 #define CR '\r' /* carriage return */
82 #define ESC '\033' /* escape */
83 #define SI '\017' /* shift in to normal character set */
84 #define SO '\016' /* shift out to alternate character set */
85 #define VT '\013' /* vertical tab (aka reverse line feed) */
86 #define RLF '7' /* ESC-7 reverse line feed */
87 #define RHLF '8' /* ESC-8 reverse half-line feed */
88 #define FHLF '9' /* ESC-9 forward half-line feed */
90 /* build up at least this many lines before flushing them out */
91 #define BUFFER_MARGIN 32
95 typedef struct char_str
{
97 #define CS_ALTERNATE 2
98 short c_column
; /* column character is in */
99 CSET c_set
; /* character set (currently only 2) */
100 char c_char
; /* character in question */
103 typedef struct line_str LINE
;
105 CHARACTER
*l_line
; /* characters on the line */
106 LINE
*l_prev
; /* previous line */
107 LINE
*l_next
; /* next line */
108 int l_lsize
; /* allocated sizeof l_line */
109 int l_line_len
; /* strlen(l_line) */
110 int l_needs_sort
; /* set if chars went in out of order */
111 int l_max_col
; /* max column in the line */
114 LINE
*alloc_line
__P((void));
115 void dowarn
__P((int));
116 void flush_line
__P((LINE
*));
117 void flush_lines
__P((int));
118 void flush_blanks
__P((void));
119 void free_line
__P((LINE
*));
120 void usage
__P((void));
121 void wrerr
__P((void));
122 void *xmalloc
__P((void *, size_t));
124 CSET last_set
; /* char_set of last char printed */
126 int compress_spaces
; /* if doing space -> tab conversion */
127 int fine
; /* if `fine' resolution (half lines) */
128 int max_bufd_lines
; /* max # lines to keep in memory */
129 int nblank_lines
; /* # blanks after last flushed line */
130 int no_backspaces
; /* if not to output any backspaces */
133 if (putchar(ch) == EOF) \
143 CSET cur_set
; /* current character set */
144 LINE
*l
; /* current line */
145 int extra_lines
; /* # of lines above first line */
146 int cur_col
; /* current column */
147 int cur_line
; /* line number of current position */
148 int max_line
; /* max value of cur_line */
149 int this_line
; /* line l points to */
150 int nflushd_lines
; /* number of lines that were flushed */
151 int adjust
, opt
, warned
;
153 err_setprogname(argv
[0]);
154 setlocale(LC_ALL
, "");
156 max_bufd_lines
= 128;
157 compress_spaces
= 1; /* compress spaces into tabs */
158 while ((opt
= getopt(argc
, argv
, "bfhl:x")) != EOF
)
160 case 'b': /* do not output backspaces */
163 case 'f': /* allow half forward line feeds */
166 case 'h': /* compress spaces into tabs */
169 case 'l': /* buffered line count */
170 if ((max_bufd_lines
= atoi(optarg
)) <= 0) {
171 (void)fprintf(stderr
,
172 "col: bad -l argument %s.\n", optarg
);
176 case 'x': /* do not compress spaces into tabs */
187 /* this value is in half lines */
190 adjust
= cur_col
= extra_lines
= warned
= 0;
191 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
192 cur_set
= last_set
= CS_NORMAL
;
193 lines
= l
= alloc_line();
195 while ((ch
= getchar()) != EOF
) {
198 case BS
: /* can't go back further */
206 case ESC
: /* just ignore EOF */
216 if (cur_line
> max_line
)
222 if (cur_line
> max_line
)
233 cur_set
= CS_ALTERNATE
;
235 case TAB
: /* adjust column */
246 /* Must stuff ch in a line - are we at the right one? */
247 if (cur_line
!= this_line
- adjust
) {
252 nmove
= cur_line
- this_line
;
254 /* round up to next line */
261 for (; nmove
< 0 && l
->l_prev
; nmove
++)
264 if (nflushd_lines
== 0) {
266 * Allow backup past first
267 * line if nothing has been
270 for (; nmove
< 0; nmove
++) {
284 /* may need to allocate here */
285 for (; nmove
> 0 && l
->l_next
; nmove
--)
287 for (; nmove
> 0; nmove
--) {
294 this_line
= cur_line
+ adjust
;
295 nmove
= this_line
- nflushd_lines
;
296 if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
297 nflushd_lines
+= nmove
- max_bufd_lines
;
298 flush_lines(nmove
- max_bufd_lines
);
301 /* grow line's buffer? */
302 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
305 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
306 l
->l_line
= (CHARACTER
*)xmalloc((void *) l
->l_line
,
307 (unsigned) need
* sizeof(CHARACTER
));
310 c
= &l
->l_line
[l
->l_line_len
++];
313 c
->c_column
= cur_col
;
315 * If things are put in out of order, they will need sorting
316 * when it is flushed.
318 if (cur_col
< l
->l_max_col
)
321 l
->l_max_col
= cur_col
;
324 /* goto the last line that had a character on it */
325 for (; l
->l_next
; l
= l
->l_next
)
327 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
329 /* make sure we leave things in a sane state */
330 if (last_set
!= CS_NORMAL
)
333 /* flush out the last few blank lines */
334 nblank_lines
= max_line
- this_line
;
337 else if (!nblank_lines
)
338 /* missing a \n on the last line? */
350 while (--nflush
>= 0) {
359 (void)free((void *)l
->l_line
);
363 lines
->l_prev
= NULL
;
367 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
368 * is the number of half line feeds, otherwise it is the number of whole line
385 for (i
= nb
; --i
>= 0;)
397 * Write a line to stdout taking care of space to tab conversion (-h flag)
398 * and character set shifts.
405 int nchars
, last_col
, this_col
;
408 nchars
= l
->l_line_len
;
410 if (l
->l_needs_sort
) {
411 static CHARACTER
*sorted
;
412 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
415 * Do an O(n) sort on l->l_line by column being careful to
416 * preserve the order of characters in the same column.
418 if (l
->l_lsize
> sorted_size
) {
419 sorted_size
= l
->l_lsize
;
420 sorted
= (CHARACTER
*)xmalloc((void *)sorted
,
421 (unsigned)sizeof(CHARACTER
) * sorted_size
);
423 if (l
->l_max_col
>= count_size
) {
424 count_size
= l
->l_max_col
+ 1;
425 count
= (int *)xmalloc((void *)count
,
426 (unsigned)sizeof(int) * count_size
);
428 memset((char *)count
, 0, sizeof(int) * l
->l_max_col
+ 1);
429 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
430 count
[c
->c_column
]++;
433 * calculate running total (shifted down by 1) to use as
434 * indices into new line.
436 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
442 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
443 sorted
[count
[c
->c_column
]++] = *c
;
448 this_col
= c
->c_column
;
452 } while (--nchars
> 0 && this_col
== endc
->c_column
);
454 /* if -b only print last character */
458 if (this_col
> last_col
) {
459 int nspace
= this_col
- last_col
;
461 if (compress_spaces
&& nspace
> 1) {
464 ntabs
= this_col
/ 8 - last_col
/ 8;
469 while (--nspace
>= 0)
476 if (c
->c_set
!= last_set
) {
496 static LINE
*line_freelist
;
504 if (!line_freelist
) {
505 l
= (LINE
*)xmalloc((void *)NULL
, sizeof(LINE
) * NALLOC
);
507 for (i
= 1; i
< NALLOC
; i
++, l
++)
512 line_freelist
= l
->l_next
;
514 memset(l
, 0, sizeof(LINE
));
523 l
->l_next
= line_freelist
;
533 if (!(p
= (void *)realloc(p
, size
)))
542 (void)fprintf(stderr
, "usage: col [-bfx] [-l nline]\n");
550 (void)fprintf(stderr
, "col: write error.\n");
559 warnx("warning: can't back up %s",
560 line
< 0 ? "past first line" : "-- line already flushed");
This page took 0.061045 seconds and 5 git commands to generate.