colrm.c revision 132831
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 * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3541568Sarchiestatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1991, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3887244Smarkm#endif
391590Srgrimes
4087628Sdwmalone#if 0
411590Srgrimes#ifndef lint
4287628Sdwmalonestatic char sccsid[] = "@(#)colrm.c	8.2 (Berkeley) 5/4/95";
4387244Smarkm#endif
4487628Sdwmalone#endif
451590Srgrimes
4687628Sdwmalone#include <sys/cdefs.h>
4787628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/colrm/colrm.c 132831 2004-07-29 09:09:22Z tjr $");
4887628Sdwmalone
491590Srgrimes#include <sys/types.h>
5026960Scharnier#include <err.h>
511590Srgrimes#include <errno.h>
5287244Smarkm#include <limits.h>
53132831Stjr#include <locale.h>
541590Srgrimes#include <stdio.h>
551590Srgrimes#include <stdlib.h>
561590Srgrimes#include <string.h>
5723690Speter#include <unistd.h>
58132831Stjr#include <wchar.h>
591590Srgrimes
601590Srgrimes#define	TAB	8
611590Srgrimes
6292920Simpvoid check(FILE *);
6392920Simpstatic void usage(void);
641590Srgrimes
651590Srgrimesint
66100817Sdwmalonemain(int argc, char *argv[])
671590Srgrimes{
6887244Smarkm	u_long column, start, stop;
69132831Stjr	int ch, width;
701590Srgrimes	char *p;
711590Srgrimes
72132831Stjr	setlocale(LC_ALL, "");
73132831Stjr
7424360Simp	while ((ch = getopt(argc, argv, "")) != -1)
751590Srgrimes		switch(ch) {
761590Srgrimes		case '?':
771590Srgrimes		default:
781590Srgrimes			usage();
791590Srgrimes		}
801590Srgrimes	argc -= optind;
811590Srgrimes	argv += optind;
821590Srgrimes
831590Srgrimes	start = stop = 0;
841590Srgrimes	switch(argc) {
851590Srgrimes	case 2:
861590Srgrimes		stop = strtol(argv[1], &p, 10);
871590Srgrimes		if (stop <= 0 || *p)
8826960Scharnier			errx(1, "illegal column -- %s", argv[1]);
891590Srgrimes		/* FALLTHROUGH */
901590Srgrimes	case 1:
911590Srgrimes		start = strtol(argv[0], &p, 10);
921590Srgrimes		if (start <= 0 || *p)
9326960Scharnier			errx(1, "illegal column -- %s", argv[0]);
941590Srgrimes		break;
951590Srgrimes	case 0:
961590Srgrimes		break;
971590Srgrimes	default:
981590Srgrimes		usage();
991590Srgrimes	}
1001590Srgrimes
1011590Srgrimes	if (stop && start > stop)
10226960Scharnier		errx(1, "illegal start and stop columns");
1031590Srgrimes
1041590Srgrimes	for (column = 0;;) {
105132831Stjr		switch (ch = getwchar()) {
106132831Stjr		case WEOF:
1071590Srgrimes			check(stdin);
1081590Srgrimes			break;
1091590Srgrimes		case '\b':
1101590Srgrimes			if (column)
1111590Srgrimes				--column;
1121590Srgrimes			break;
1131590Srgrimes		case '\n':
1141590Srgrimes			column = 0;
1151590Srgrimes			break;
1161590Srgrimes		case '\t':
1171590Srgrimes			column = (column + TAB) & ~(TAB - 1);
1181590Srgrimes			break;
1191590Srgrimes		default:
120132831Stjr			if ((width = wcwidth(ch)) > 0)
121132831Stjr				column += width;
1221590Srgrimes			break;
1231590Srgrimes		}
1241590Srgrimes
12532069Salex		if ((!start || column < start || (stop && column > stop)) &&
126132831Stjr		    putwchar(ch) == WEOF)
1271590Srgrimes			check(stdout);
1281590Srgrimes	}
1291590Srgrimes}
1301590Srgrimes
1311590Srgrimesvoid
132100817Sdwmalonecheck(FILE *stream)
1331590Srgrimes{
1341590Srgrimes	if (feof(stream))
1351590Srgrimes		exit(0);
1361590Srgrimes	if (ferror(stream))
13726960Scharnier		err(1, "%s", stream == stdin ? "stdin" : "stdout");
1381590Srgrimes}
1391590Srgrimes
1401590Srgrimesvoid
141100817Sdwmaloneusage(void)
1421590Srgrimes{
1431590Srgrimes	(void)fprintf(stderr, "usage: colrm [start [stop]]\n");
1441590Srgrimes	exit(1);
1451590Srgrimes}
1461590Srgrimes
147