docs/cut

annotate code/cut.c__netbsd.1993-03-21 @ 21:bac481be86d7

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