11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1991, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3141568Sarchiestatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3487244Smarkm#endif
351590Srgrimes
3687628Sdwmalone#if 0
371590Srgrimes#ifndef lint
3887628Sdwmalonestatic char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
3987244Smarkm#endif
4087628Sdwmalone#endif
411590Srgrimes
4287628Sdwmalone#include <sys/cdefs.h>
4387628Sdwmalone__FBSDID("$FreeBSD$");
4487628Sdwmalone
451590Srgrimes#include <sys/types.h>
4626960Scharnier#include <err.h>
47200462Sdelphij#include <errno.h>
48200462Sdelphij#include <limits.h>
49132831Stjr#include <locale.h>
501590Srgrimes#include <stdio.h>
511590Srgrimes#include <stdlib.h>
52200462Sdelphij#include <string.h>
5323690Speter#include <unistd.h>
54132831Stjr#include <wchar.h>
551590Srgrimes
561590Srgrimes#define	TAB	8
571590Srgrimes
5892920Simpvoid check(FILE *);
5992920Simpstatic void usage(void);
601590Srgrimes
611590Srgrimesint
62100817Sdwmalonemain(int argc, char *argv[])
631590Srgrimes{
6487244Smarkm	u_long column, start, stop;
65132831Stjr	int ch, width;
661590Srgrimes	char *p;
671590Srgrimes
68132831Stjr	setlocale(LC_ALL, "");
69132831Stjr
7024360Simp	while ((ch = getopt(argc, argv, "")) != -1)
711590Srgrimes		switch(ch) {
721590Srgrimes		case '?':
731590Srgrimes		default:
741590Srgrimes			usage();
751590Srgrimes		}
761590Srgrimes	argc -= optind;
771590Srgrimes	argv += optind;
781590Srgrimes
791590Srgrimes	start = stop = 0;
801590Srgrimes	switch(argc) {
811590Srgrimes	case 2:
821590Srgrimes		stop = strtol(argv[1], &p, 10);
831590Srgrimes		if (stop <= 0 || *p)
8426960Scharnier			errx(1, "illegal column -- %s", argv[1]);
851590Srgrimes		/* FALLTHROUGH */
861590Srgrimes	case 1:
871590Srgrimes		start = strtol(argv[0], &p, 10);
881590Srgrimes		if (start <= 0 || *p)
8926960Scharnier			errx(1, "illegal column -- %s", argv[0]);
901590Srgrimes		break;
911590Srgrimes	case 0:
921590Srgrimes		break;
931590Srgrimes	default:
941590Srgrimes		usage();
951590Srgrimes	}
961590Srgrimes
971590Srgrimes	if (stop && start > stop)
9826960Scharnier		errx(1, "illegal start and stop columns");
991590Srgrimes
1001590Srgrimes	for (column = 0;;) {
101132831Stjr		switch (ch = getwchar()) {
102132831Stjr		case WEOF:
1031590Srgrimes			check(stdin);
1041590Srgrimes			break;
1051590Srgrimes		case '\b':
1061590Srgrimes			if (column)
1071590Srgrimes				--column;
1081590Srgrimes			break;
1091590Srgrimes		case '\n':
1101590Srgrimes			column = 0;
1111590Srgrimes			break;
1121590Srgrimes		case '\t':
1131590Srgrimes			column = (column + TAB) & ~(TAB - 1);
1141590Srgrimes			break;
1151590Srgrimes		default:
116132831Stjr			if ((width = wcwidth(ch)) > 0)
117132831Stjr				column += width;
1181590Srgrimes			break;
1191590Srgrimes		}
1201590Srgrimes
12132069Salex		if ((!start || column < start || (stop && column > stop)) &&
122132831Stjr		    putwchar(ch) == WEOF)
1231590Srgrimes			check(stdout);
1241590Srgrimes	}
1251590Srgrimes}
1261590Srgrimes
1271590Srgrimesvoid
128100817Sdwmalonecheck(FILE *stream)
1291590Srgrimes{
1301590Srgrimes	if (feof(stream))
1311590Srgrimes		exit(0);
1321590Srgrimes	if (ferror(stream))
13326960Scharnier		err(1, "%s", stream == stdin ? "stdin" : "stdout");
1341590Srgrimes}
1351590Srgrimes
1361590Srgrimesvoid
137100817Sdwmaloneusage(void)
1381590Srgrimes{
1391590Srgrimes	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
1401590Srgrimes	exit(1);
1411590Srgrimes}
1421590Srgrimes
143