1 /* $OpenBSD: cut.c,v 1.15 2010/01/23 00:32:12 schwarze Exp $ */ 2 /* $NetBSD: cut.c,v 1.9 1995/09/02 05:59:23 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <ctype.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <limits.h> 40 #include <locale.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 int cflag; 47 char dchar; 48 int dflag; 49 int fflag; 50 int sflag; 51 52 void c_cut(FILE *, char *); 53 void f_cut(FILE *, char *); 54 void get_list(char *); 55 void usage(void); 56 57 int 58 main(int argc, char *argv[]) 59 { 60 FILE *fp; 61 void (*fcn)(FILE *, char *); 62 int ch; 63 64 setlocale (LC_ALL, ""); 65 66 dchar = '\t'; /* default delimiter is \t */ 67 68 /* Since we don't support multi-byte characters, the -c and -b 69 options are equivalent, and the -n option is meaningless. */ 70 while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) 71 switch(ch) { 72 case 'b': 73 case 'c': 74 fcn = c_cut; 75 get_list(optarg); 76 cflag = 1; 77 break; 78 case 'd': 79 dchar = *optarg; 80 dflag = 1; 81 break; 82 case 'f': 83 get_list(optarg); 84 fcn = f_cut; 85 fflag = 1; 86 break; 87 case 's': 88 sflag = 1; 89 break; 90 case 'n': 91 break; 92 case '?': 93 default: 94 usage(); 95 } 96 argc -= optind; 97 argv += optind; 98 99 if (fflag) { 100 if (cflag) 101 usage(); 102 } else if (!cflag || dflag || sflag) 103 usage(); 104 105 if (*argv) 106 for (; *argv; ++argv) { 107 if (!(fp = fopen(*argv, "r"))) 108 err(1, "%s", *argv); 109 fcn(fp, *argv); 110 (void)fclose(fp); 111 } 112 else 113 fcn(stdin, "stdin"); 114 exit(0); 115 } 116 117 int autostart, autostop, maxval; 118 119 char positions[_POSIX2_LINE_MAX + 1]; 120 121 void 122 get_list(char *list) 123 { 124 int setautostart, start, stop; 125 char *pos; 126 char *p; 127 128 /* 129 * set a byte in the positions array to indicate if a field or 130 * column is to be selected; use +1, it's 1-based, not 0-based. 131 * This parser is less restrictive than the Draft 9 POSIX spec. 132 * POSIX doesn't allow lists that aren't in increasing order or 133 * overlapping lists. We also handle "-3-5" although there's no 134 * real reason too. 135 */ 136 while ((p = strsep(&list, ", \t"))) { 137 setautostart = start = stop = 0; 138 if (*p == '-') { 139 ++p; 140 setautostart = 1; 141 } 142 if (isdigit(*p)) { 143 start = stop = strtol(p, &p, 10); 144 if (setautostart && start > autostart) 145 autostart = start; 146 } 147 if (*p == '-') { 148 if (isdigit(p[1])) 149 stop = strtol(p + 1, &p, 10); 150 if (*p == '-') { 151 ++p; 152 if (!autostop || autostop > stop) 153 autostop = stop; 154 } 155 } 156 if (*p) 157 errx(1, "[-bcf] list: illegal list value"); 158 if (!stop || !start) 159 errx(1, "[-bcf] list: values may not include zero"); 160 if (stop > _POSIX2_LINE_MAX) 161 errx(1, "[-bcf] list: %d too large (max %d)", 162 stop, _POSIX2_LINE_MAX); 163 if (maxval < stop) 164 maxval = stop; 165 for (pos = positions + start; start++ <= stop; *pos++ = 1) 166 ; 167 } 168 169 /* overlapping ranges */ 170 if (autostop && maxval > autostop) 171 maxval = autostop; 172 173 /* set autostart */ 174 if (autostart) 175 memset(positions + 1, '1', autostart); 176 } 177 178 /* ARGSUSED */ 179 void 180 c_cut(FILE *fp, char *fname) 181 { 182 int ch, col; 183 char *pos; 184 185 for (;;) { 186 pos = positions + 1; 187 for (col = maxval; col; --col) { 188 if ((ch = getc(fp)) == EOF) 189 return; 190 if (ch == '\n') 191 break; 192 if (*pos++) 193 (void)putchar(ch); 194 } 195 if (ch != '\n') { 196 if (autostop) 197 while ((ch = getc(fp)) != EOF && ch != '\n') 198 (void)putchar(ch); 199 else 200 while ((ch = getc(fp)) != EOF && ch != '\n') 201 ; 202 } 203 (void)putchar('\n'); 204 } 205 } 206 207 void 208 f_cut(FILE *fp, char *fname) 209 { 210 int ch, field, isdelim; 211 char *pos, *p, sep; 212 int output; 213 size_t len; 214 char *lbuf, *tbuf; 215 216 for (sep = dchar, tbuf = NULL; (lbuf = fgetln(fp, &len));) { 217 output = 0; 218 if (lbuf[len - 1] != '\n') { 219 /* no newline at the end of the last line so add one */ 220 if ((tbuf = (char *)malloc(len + 1)) == NULL) 221 err(1, NULL); 222 memcpy(tbuf, lbuf, len); 223 tbuf[len] = '\n'; 224 lbuf = tbuf; 225 } 226 for (isdelim = 0, p = lbuf;; ++p) { 227 ch = *p; 228 /* this should work if newline is delimiter */ 229 if (ch == sep) 230 isdelim = 1; 231 if (ch == '\n') { 232 if (!isdelim && !sflag) 233 (void)fwrite(lbuf, len, 1, stdout); 234 break; 235 } 236 } 237 if (!isdelim) 238 continue; 239 240 pos = positions + 1; 241 for (field = maxval, p = lbuf; field; --field, ++pos) { 242 if (*pos) { 243 if (output++) 244 (void)putchar(sep); 245 while ((ch = *p++) != '\n' && ch != sep) 246 (void)putchar(ch); 247 } else 248 while ((ch = *p++) != '\n' && ch != sep) 249 ; 250 if (ch == '\n') 251 break; 252 } 253 if (ch != '\n') { 254 if (autostop) { 255 if (output) 256 (void)putchar(sep); 257 for (; (ch = *p) != '\n'; ++p) 258 (void)putchar(ch); 259 } else 260 for (; (ch = *p) != '\n'; ++p) 261 ; 262 } 263 (void)putchar('\n'); 264 } 265 if (tbuf) 266 free(tbuf); 267 } 268 269 void 270 usage(void) 271 { 272 (void)fprintf(stderr, 273 "usage: cut -b list [-n] [file ...]\n" 274 " cut -c list [file ...]\n" 275 " cut -f list [-s] [-d delim] [file ...]\n"); 276 exit(1); 277 } 278