BSD col(1) for mandoc(1) on Linux

1vuio0pswjnm71 pts1 comments

/* $NetBSD: col.c,v 1.20 2021/09/10 21:52:17 rillig Exp $ */

/*-<br>* SPDX-License-Identifier: BSD-3-Clause<br>* Copyright (c) 1990, 1993, 1994<br>* The Regents of the University of California. All rights reserved.<br>* This code is derived from software contributed to Berkeley by<br>* Michael Rendell of the Memorial University of Newfoundland.<br>* Redistribution and use in source and binary forms, with or without<br>* modification, are permitted provided that the following conditions<br>* are met:<br>* 1. Redistributions of source code must retain the above copyright<br>* notice, this list of conditions and the following disclaimer.<br>* 2. Redistributions in binary form must reproduce the above copyright<br>* notice, this list of conditions and the following disclaimer in the<br>* documentation and/or other materials provided with the distribution.<br>* 3. Neither the name of the University nor the names of its contributors<br>* may be used to endorse or promote products derived from this software<br>* without specific prior written permission.<br>* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND<br>* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE<br>* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL<br>* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT<br>* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY<br>* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>* SUCH DAMAGE.<br>*/

#include<br>#ifndef lint<br>__COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\<br>The Regents of the University of California. All rights reserved.");<br>#endif /* not lint */

#ifndef lint<br>#if 0<br>static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";<br>__FBSDID("$FreeBSD: head/usr.bin/col/col.c 366577 2020-10-09 15:27:37Z markj $")

#endif<br>__RCSID("$NetBSD: col.c,v 1.20 2021/09/10 21:52:17 rillig Exp $");<br>#endif /* not lint */

#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include

#define BS '\b' /* backspace */<br>#define TAB '\t' /* tab */<br>#define SPACE ' ' /* space */<br>#define NL '\n' /* newline */<br>#define CR '\r' /* carriage return */<br>#define ESC '\033' /* escape */<br>#define SI '\017' /* shift in to normal character set */<br>#define SO '\016' /* shift out to alternate character set */<br>#define VT '\013' /* vertical tab (aka reverse line feed) */<br>#define RLF '7' /* ESC-7 reverse line feed */<br>#define RHLF '8' /* ESC-8 reverse half-line feed */<br>#define FHLF '9' /* ESC-9 forward half-line feed */

/* build up at least this many lines before flushing them out */<br>#define BUFFER_MARGIN 32

typedef char CSET;

typedef struct char_str {<br>#define CS_NORMAL 1<br>#define CS_ALTERNATE 2<br>int c_column; /* column character is in */<br>CSET c_set; /* character set (currently only 2) */<br>wchar_t c_char; /* character in question */<br>int c_width; /* character width */<br>} CHAR;

typedef struct line_str LINE;<br>struct line_str {<br>CHAR *l_line; /* characters on the line */<br>LINE *l_prev; /* previous line */<br>LINE *l_next; /* next line */<br>int l_lsize; /* allocated sizeof l_line */<br>int l_line_len; /* strlen(l_line) */<br>int l_needs_sort; /* set if chars went in out of order */<br>int l_max_col; /* max column in the line */<br>};

static void addto_lineno(int *, int);<br>static LINE *alloc_line(void);<br>static void dowarn(int);<br>static void flush_line(LINE *);<br>static void flush_lines(int);<br>static void flush_blanks(void);<br>static void free_line(LINE *);<br>__dead static void usage(void);

static CSET last_set; /* char_set of last char printed */<br>static LINE *lines;<br>static int compress_spaces; /* if doing space -> tab conversion */<br>static int fine; /* if `fine' resolution (half lines) */<br>static int max_bufd_lines; /* max # of half lines to keep in memory */<br>static int nblank_lines; /* # blanks after last flushed line */<br>static int no_backspaces; /* if not to output any backspaces */<br>static int pass_unknown_seqs; /* pass unknown control sequences */

#define PUTC(ch) \<br>do { \<br>if (putwchar(ch) == WEOF) \<br>errx(EXIT_FAILURE, "write error"); \<br>} while (0)

int<br>main(int argc, char **argv)<br>wint_t ch;<br>CHAR *c;<br>CSET cur_set; /* current character set */<br>LINE *l; /* current line */<br>int extra_lines; /* # of lines above first line */<br>int cur_col; /* current column */<br>int cur_line; /* line number of current position */<br>int max_line; /* max value of cur_line */<br>int this_line; /* line l points to */<br>int nflushd_lines; /* number of lines that were flushed */<br>int adjust, opt, warned, width;<br>int e;

(void)setlocale(LC_CTYPE, "");

max_bufd_lines = 256;<br>compress_spaces = 1; /* compress spaces into tabs */<br>while ((opt = getopt(argc, argv, "bfhl:px")) != -1)<br>switch (opt) {<br>case 'b': /* do not output...

line static define include void char

Related Articles