1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/usr.bin/colrm/colrm.c 216370 2010-12-11 08:32:16Z joel $ 30 * 31 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 32 * @(#)colrm.c 8.2 (Berkeley) 5/4/95 33 */ 34 35 #include <sys/types.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <limits.h> 39 #include <locale.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <wchar.h> 45 46 #define TAB 8 47 48 void check(FILE *); 49 static void usage(void); 50 51 int 52 main(int argc, char *argv[]) 53 { 54 u_long column, start, stop; 55 int ch, width; 56 char *p; 57 58 setlocale(LC_ALL, ""); 59 60 while ((ch = getopt(argc, argv, "")) != -1) 61 switch(ch) { 62 case '?': 63 default: 64 usage(); 65 } 66 argc -= optind; 67 argv += optind; 68 69 start = stop = 0; 70 switch(argc) { 71 case 2: 72 stop = strtol(argv[1], &p, 10); 73 if (stop <= 0 || *p) 74 errx(1, "illegal column -- %s", argv[1]); 75 /* FALLTHROUGH */ 76 case 1: 77 start = strtol(argv[0], &p, 10); 78 if (start <= 0 || *p) 79 errx(1, "illegal column -- %s", argv[0]); 80 break; 81 case 0: 82 break; 83 default: 84 usage(); 85 } 86 87 if (stop && start > stop) 88 errx(1, "illegal start and stop columns"); 89 90 for (column = 0;;) { 91 switch (ch = getwchar()) { 92 case WEOF: 93 check(stdin); 94 break; 95 case '\b': 96 if (column) 97 --column; 98 break; 99 case '\n': 100 column = 0; 101 break; 102 case '\t': 103 column = (column + TAB) & ~(TAB - 1); 104 break; 105 default: 106 if ((width = wcwidth(ch)) > 0) 107 column += width; 108 break; 109 } 110 111 if ((!start || column < start || (stop && column > stop)) && 112 putwchar(ch) == WEOF) 113 check(stdout); 114 } 115 } 116 117 void 118 check(FILE *stream) 119 { 120 if (feof(stream)) 121 exit(0); 122 if (ferror(stream)) 123 err(1, "%s", stream == stdin ? "stdin" : "stdout"); 124 } 125 126 void 127 usage(void) 128 { 129 (void)fprintf(stderr, "usage: colrm [start [stop]]\n"); 130 exit(1); 131 } 132 133