docs/cut

annotate code/cut.c__4.3bsd-reno.1990-06-25 @ 36:c584c7d907c5

Seiten- und Zeilenumbrueche optimiert
author markus schnalke <meillo@marmaro.de>
date Fri, 02 Oct 2015 07:30:45 +0200
parents
children
rev   line source
meillo@14 1 /*
meillo@14 2 * Copyright (c) 1989 The Regents of the University of California.
meillo@14 3 * All rights reserved.
meillo@14 4 *
meillo@14 5 * This code is derived from software contributed to Berkeley by
meillo@14 6 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
meillo@14 7 *
meillo@14 8 * Redistribution and use in source and binary forms are permitted provided
meillo@14 9 * that: (1) source distributions retain this entire copyright notice and
meillo@14 10 * comment, and (2) distributions including binaries display the following
meillo@14 11 * acknowledgement: ``This product includes software developed by the
meillo@14 12 * University of California, Berkeley and its contributors'' in the
meillo@14 13 * documentation or other materials provided with the distribution and in
meillo@14 14 * all advertising materials mentioning features or use of this software.
meillo@14 15 * Neither the name of the University nor the names of its contributors may
meillo@14 16 * be used to endorse or promote products derived from this software without
meillo@14 17 * specific prior written permission.
meillo@14 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
meillo@14 19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
meillo@14 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
meillo@14 21 */
meillo@14 22
meillo@14 23 #ifndef lint
meillo@14 24 char copyright[] =
meillo@14 25 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
meillo@14 26 All rights reserved.\n";
meillo@14 27 #endif /* not lint */
meillo@14 28
meillo@14 29 #ifndef lint
meillo@14 30 static char sccsid[] = "@(#)cut.c 5.3 (Berkeley) 6/24/90";
meillo@14 31 #endif /* not lint */
meillo@14 32
meillo@14 33 #include <limits.h>
meillo@14 34 #include <stdio.h>
meillo@14 35 #include <ctype.h>
meillo@14 36
meillo@14 37 int cflag;
meillo@14 38 char dchar;
meillo@14 39 int dflag;
meillo@14 40 int fflag;
meillo@14 41 int sflag;
meillo@14 42
meillo@14 43 main(argc, argv)
meillo@14 44 int argc;
meillo@14 45 char **argv;
meillo@14 46 {
meillo@14 47 extern char *optarg;
meillo@14 48 extern int errno, optind;
meillo@14 49 FILE *fp;
meillo@14 50 int ch, (*fcn)(), c_cut(), f_cut();
meillo@14 51 char *strerror();
meillo@14 52
meillo@14 53 dchar = '\t'; /* default delimiter is \t */
meillo@14 54
meillo@14 55 while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF)
meillo@14 56 switch(ch) {
meillo@14 57 case 'c':
meillo@14 58 fcn = c_cut;
meillo@14 59 get_list(optarg);
meillo@14 60 cflag = 1;
meillo@14 61 break;
meillo@14 62 case 'd':
meillo@14 63 dchar = *optarg;
meillo@14 64 dflag = 1;
meillo@14 65 break;
meillo@14 66 case 'f':
meillo@14 67 get_list(optarg);
meillo@14 68 fcn = f_cut;
meillo@14 69 fflag = 1;
meillo@14 70 break;
meillo@14 71 case 's':
meillo@14 72 sflag = 1;
meillo@14 73 break;
meillo@14 74 case '?':
meillo@14 75 default:
meillo@14 76 usage();
meillo@14 77 }
meillo@14 78 argc -= optind;
meillo@14 79 argv += optind;
meillo@14 80
meillo@14 81 if (fflag) {
meillo@14 82 if (cflag)
meillo@14 83 usage();
meillo@14 84 } else if (!cflag || dflag || sflag)
meillo@14 85 usage();
meillo@14 86
meillo@14 87 if (*argv)
meillo@14 88 for (; *argv; ++argv) {
meillo@14 89 if (!(fp = fopen(*argv, "r"))) {
meillo@14 90 (void)fprintf(stderr,
meillo@14 91 "cut: %s: %s\n", *argv, strerror(errno));
meillo@14 92 exit(1);
meillo@14 93 }
meillo@14 94 fcn(fp, *argv);
meillo@14 95 }
meillo@14 96 else
meillo@14 97 fcn(stdin, "stdin");
meillo@14 98 exit(0);
meillo@14 99 }
meillo@14 100
meillo@14 101 int autostart, autostop, maxval;
meillo@14 102
meillo@14 103 char positions[_BSD_LINE_MAX + 1];
meillo@14 104
meillo@14 105 get_list(list)
meillo@14 106 char *list;
meillo@14 107 {
meillo@14 108 register char *pos;
meillo@14 109 register int setautostart, start, stop;
meillo@14 110 char *p, *strtok();
meillo@14 111
meillo@14 112 /*
meillo@14 113 * set a byte in the positions array to indicate if a field or
meillo@14 114 * column is to be selected; use +1, it's 1-based, not 0-based.
meillo@14 115 * This parser is less restrictive than the Draft 9 POSIX spec.
meillo@14 116 * POSIX doesn't allow lists that aren't in increasing order or
meillo@14 117 * overlapping lists. We also handle "-3-5" although there's no
meillo@14 118 * real reason too.
meillo@14 119 */
meillo@14 120 for (; p = strtok(list, ", \t"); list = NULL) {
meillo@14 121 setautostart = start = stop = 0;
meillo@14 122 if (*p == '-') {
meillo@14 123 ++p;
meillo@14 124 setautostart = 1;
meillo@14 125 }
meillo@14 126 if (isdigit(*p)) {
meillo@14 127 start = stop = strtol(p, &p, 10);
meillo@14 128 if (setautostart && start > autostart)
meillo@14 129 autostart = start;
meillo@14 130 }
meillo@14 131 if (*p == '-') {
meillo@14 132 if (isdigit(p[1]))
meillo@14 133 stop = strtol(p + 1, &p, 10);
meillo@14 134 if (*p == '-') {
meillo@14 135 ++p;
meillo@14 136 if (!autostop || autostop > stop)
meillo@14 137 autostop = stop;
meillo@14 138 }
meillo@14 139 }
meillo@14 140 if (*p)
meillo@14 141 badlist("illegal list value");
meillo@14 142 if (!stop || !start)
meillo@14 143 badlist("values may not include zero");
meillo@14 144 if (stop > _BSD_LINE_MAX) {
meillo@14 145 /* positions used rather than allocate a new buffer */
meillo@14 146 (void)sprintf(positions, "%d too large (max %d)",
meillo@14 147 stop, _BSD_LINE_MAX);
meillo@14 148 badlist(positions);
meillo@14 149 }
meillo@14 150 if (maxval < stop)
meillo@14 151 maxval = stop;
meillo@14 152 for (pos = positions + start; start++ <= stop; *pos++ = 1);
meillo@14 153 }
meillo@14 154
meillo@14 155 /* overlapping ranges */
meillo@14 156 if (autostop && maxval > autostop)
meillo@14 157 maxval = autostop;
meillo@14 158
meillo@14 159 /* set autostart */
meillo@14 160 if (autostart)
meillo@14 161 memset(positions + 1, '1', autostart);
meillo@14 162 }
meillo@14 163
meillo@14 164 /* ARGSUSED */
meillo@14 165 c_cut(fp, fname)
meillo@14 166 FILE *fp;
meillo@14 167 char *fname;
meillo@14 168 {
meillo@14 169 register int ch, col;
meillo@14 170 register char *pos;
meillo@14 171
meillo@14 172 for (;;) {
meillo@14 173 pos = positions + 1;
meillo@14 174 for (col = maxval; col; --col) {
meillo@14 175 if ((ch = getc(fp)) == EOF)
meillo@14 176 return;
meillo@14 177 if (ch == '\n')
meillo@14 178 break;
meillo@14 179 if (*pos++)
meillo@14 180 putchar(ch);
meillo@14 181 }
meillo@14 182 if (ch != '\n')
meillo@14 183 if (autostop)
meillo@14 184 while ((ch = getc(fp)) != EOF && ch != '\n')
meillo@14 185 putchar(ch);
meillo@14 186 else
meillo@14 187 while ((ch = getc(fp)) != EOF && ch != '\n');
meillo@14 188 putchar('\n');
meillo@14 189 }
meillo@14 190 }
meillo@14 191
meillo@14 192 f_cut(fp, fname)
meillo@14 193 FILE *fp;
meillo@14 194 char *fname;
meillo@14 195 {
meillo@14 196 register int ch, field, isdelim;
meillo@14 197 register char *pos, *p, sep;
meillo@14 198 int output;
meillo@14 199 char lbuf[_BSD_LINE_MAX + 1];
meillo@14 200
meillo@14 201 for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp);) {
meillo@14 202 for (isdelim = 0, p = lbuf;; ++p) {
meillo@14 203 if (!(ch = *p)) {
meillo@14 204 (void)fprintf(stderr,
meillo@14 205 "cut: %s: line too long.\n", fname);
meillo@14 206 exit(1);
meillo@14 207 }
meillo@14 208 /* this should work if newline is delimiter */
meillo@14 209 if (ch == sep)
meillo@14 210 isdelim = 1;
meillo@14 211 if (ch == '\n') {
meillo@14 212 if (!isdelim && !sflag)
meillo@14 213 (void)printf("%s", lbuf);
meillo@14 214 break;
meillo@14 215 }
meillo@14 216 }
meillo@14 217 if (!isdelim)
meillo@14 218 continue;
meillo@14 219
meillo@14 220 pos = positions + 1;
meillo@14 221 for (field = maxval, p = lbuf; field; --field, ++pos) {
meillo@14 222 if (*pos) {
meillo@14 223 if (output++)
meillo@14 224 putchar(sep);
meillo@14 225 while ((ch = *p++) != '\n' && ch != sep)
meillo@14 226 putchar(ch);
meillo@14 227 } else
meillo@14 228 while ((ch = *p++) != '\n' && ch != sep);
meillo@14 229 if (ch == '\n')
meillo@14 230 break;
meillo@14 231 }
meillo@14 232 if (ch != '\n')
meillo@14 233 if (autostop) {
meillo@14 234 if (output)
meillo@14 235 putchar(sep);
meillo@14 236 for (; (ch = *p) != '\n'; ++p)
meillo@14 237 putchar(ch);
meillo@14 238 } else
meillo@14 239 for (; (ch = *p) != '\n'; ++p);
meillo@14 240 putchar('\n');
meillo@14 241 }
meillo@14 242 }
meillo@14 243
meillo@14 244 badlist(msg)
meillo@14 245 char *msg;
meillo@14 246 {
meillo@14 247 (void)fprintf(stderr, "cut: [-cf] list: %s.\n", msg);
meillo@14 248 exit(1);
meillo@14 249 }
meillo@14 250
meillo@14 251 usage()
meillo@14 252 {
meillo@14 253 (void)fprintf(stderr,
meillo@14 254 "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
meillo@14 255 exit(1);
meillo@14 256 }