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