docs/cut

annotate code/cut.c__freebsd.1994-05-27 @ 24:7fd31331580a

Kleine Formulierungsanpassungen Und s/mein/der/ fuer freiesMagazin
author markus schnalke <meillo@marmaro.de>
date Sat, 13 Jun 2015 08:53:45 +0200
parents
children
rev   line source
meillo@14 1 /*
meillo@14 2 * Copyright (c) 1989, 1993
meillo@14 3 * The Regents of the University of California. 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 static char copyright[] =
meillo@14 39 "@(#) Copyright (c) 1989, 1993\n\
meillo@14 40 The Regents of the University of California. 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 8.1 (Berkeley) 6/6/93";
meillo@14 45 #endif /* not lint */
meillo@14 46
meillo@14 47 #include <ctype.h>
meillo@14 48 #include <errno.h>
meillo@14 49 #include <limits.h>
meillo@14 50 #include <stdio.h>
meillo@14 51 #include <stdlib.h>
meillo@14 52 #include <string.h>
meillo@14 53
meillo@14 54 int cflag;
meillo@14 55 char dchar;
meillo@14 56 int dflag;
meillo@14 57 int fflag;
meillo@14 58 int sflag;
meillo@14 59
meillo@14 60 void c_cut __P((FILE *, char *));
meillo@14 61 void err __P((const char *, ...));
meillo@14 62 void f_cut __P((FILE *, char *));
meillo@14 63 void get_list __P((char *));
meillo@14 64 void usage __P((void));
meillo@14 65
meillo@14 66 int
meillo@14 67 main(argc, argv)
meillo@14 68 int argc;
meillo@14 69 char *argv[];
meillo@14 70 {
meillo@14 71 FILE *fp;
meillo@14 72 void (*fcn) __P((FILE *, char *));
meillo@14 73 int ch;
meillo@14 74
meillo@14 75 dchar = '\t'; /* default delimiter is \t */
meillo@14 76
meillo@14 77 while ((ch = getopt(argc, argv, "c:d:f:s")) != EOF)
meillo@14 78 switch(ch) {
meillo@14 79 case 'c':
meillo@14 80 fcn = c_cut;
meillo@14 81 get_list(optarg);
meillo@14 82 cflag = 1;
meillo@14 83 break;
meillo@14 84 case 'd':
meillo@14 85 dchar = *optarg;
meillo@14 86 dflag = 1;
meillo@14 87 break;
meillo@14 88 case 'f':
meillo@14 89 get_list(optarg);
meillo@14 90 fcn = f_cut;
meillo@14 91 fflag = 1;
meillo@14 92 break;
meillo@14 93 case 's':
meillo@14 94 sflag = 1;
meillo@14 95 break;
meillo@14 96 case '?':
meillo@14 97 default:
meillo@14 98 usage();
meillo@14 99 }
meillo@14 100 argc -= optind;
meillo@14 101 argv += optind;
meillo@14 102
meillo@14 103 if (fflag) {
meillo@14 104 if (cflag)
meillo@14 105 usage();
meillo@14 106 } else if (!cflag || dflag || sflag)
meillo@14 107 usage();
meillo@14 108
meillo@14 109 if (*argv)
meillo@14 110 for (; *argv; ++argv) {
meillo@14 111 if (!(fp = fopen(*argv, "r")))
meillo@14 112 err("%s: %s\n", *argv, strerror(errno));
meillo@14 113 fcn(fp, *argv);
meillo@14 114 (void)fclose(fp);
meillo@14 115 }
meillo@14 116 else
meillo@14 117 fcn(stdin, "stdin");
meillo@14 118 exit(0);
meillo@14 119 }
meillo@14 120
meillo@14 121 int autostart, autostop, maxval;
meillo@14 122
meillo@14 123 char positions[_POSIX2_LINE_MAX + 1];
meillo@14 124
meillo@14 125 void
meillo@14 126 get_list(list)
meillo@14 127 char *list;
meillo@14 128 {
meillo@14 129 register int setautostart, start, stop;
meillo@14 130 register char *pos;
meillo@14 131 char *p;
meillo@14 132
meillo@14 133 /*
meillo@14 134 * set a byte in the positions array to indicate if a field or
meillo@14 135 * column is to be selected; use +1, it's 1-based, not 0-based.
meillo@14 136 * This parser is less restrictive than the Draft 9 POSIX spec.
meillo@14 137 * POSIX doesn't allow lists that aren't in increasing order or
meillo@14 138 * overlapping lists. We also handle "-3-5" although there's no
meillo@14 139 * real reason too.
meillo@14 140 */
meillo@14 141 for (; p = strtok(list, ", \t"); list = NULL) {
meillo@14 142 setautostart = start = stop = 0;
meillo@14 143 if (*p == '-') {
meillo@14 144 ++p;
meillo@14 145 setautostart = 1;
meillo@14 146 }
meillo@14 147 if (isdigit(*p)) {
meillo@14 148 start = stop = strtol(p, &p, 10);
meillo@14 149 if (setautostart && start > autostart)
meillo@14 150 autostart = start;
meillo@14 151 }
meillo@14 152 if (*p == '-') {
meillo@14 153 if (isdigit(p[1]))
meillo@14 154 stop = strtol(p + 1, &p, 10);
meillo@14 155 if (*p == '-') {
meillo@14 156 ++p;
meillo@14 157 if (!autostop || autostop > stop)
meillo@14 158 autostop = stop;
meillo@14 159 }
meillo@14 160 }
meillo@14 161 if (*p)
meillo@14 162 err("[-cf] list: illegal list value\n");
meillo@14 163 if (!stop || !start)
meillo@14 164 err("[-cf] list: values may not include zero\n");
meillo@14 165 if (stop > _POSIX2_LINE_MAX)
meillo@14 166 err("[-cf] list: %d too large (max %d)\n",
meillo@14 167 stop, _POSIX2_LINE_MAX);
meillo@14 168 if (maxval < stop)
meillo@14 169 maxval = stop;
meillo@14 170 for (pos = positions + start; start++ <= stop; *pos++ = 1);
meillo@14 171 }
meillo@14 172
meillo@14 173 /* overlapping ranges */
meillo@14 174 if (autostop && maxval > autostop)
meillo@14 175 maxval = autostop;
meillo@14 176
meillo@14 177 /* set autostart */
meillo@14 178 if (autostart)
meillo@14 179 memset(positions + 1, '1', autostart);
meillo@14 180 }
meillo@14 181
meillo@14 182 /* ARGSUSED */
meillo@14 183 void
meillo@14 184 c_cut(fp, fname)
meillo@14 185 FILE *fp;
meillo@14 186 char *fname;
meillo@14 187 {
meillo@14 188 register int ch, col;
meillo@14 189 register char *pos;
meillo@14 190
meillo@14 191 for (;;) {
meillo@14 192 pos = positions + 1;
meillo@14 193 for (col = maxval; col; --col) {
meillo@14 194 if ((ch = getc(fp)) == EOF)
meillo@14 195 return;
meillo@14 196 if (ch == '\n')
meillo@14 197 break;
meillo@14 198 if (*pos++)
meillo@14 199 (void)putchar(ch);
meillo@14 200 }
meillo@14 201 if (ch != '\n')
meillo@14 202 if (autostop)
meillo@14 203 while ((ch = getc(fp)) != EOF && ch != '\n')
meillo@14 204 (void)putchar(ch);
meillo@14 205 else
meillo@14 206 while ((ch = getc(fp)) != EOF && ch != '\n');
meillo@14 207 (void)putchar('\n');
meillo@14 208 }
meillo@14 209 }
meillo@14 210
meillo@14 211 void
meillo@14 212 f_cut(fp, fname)
meillo@14 213 FILE *fp;
meillo@14 214 char *fname;
meillo@14 215 {
meillo@14 216 register int ch, field, isdelim;
meillo@14 217 register char *pos, *p, sep;
meillo@14 218 int output;
meillo@14 219 char lbuf[_POSIX2_LINE_MAX + 1];
meillo@14 220
meillo@14 221 for (sep = dchar, output = 0; fgets(lbuf, sizeof(lbuf), fp);) {
meillo@14 222 for (isdelim = 0, p = lbuf;; ++p) {
meillo@14 223 if (!(ch = *p))
meillo@14 224 err("%s: line too long.\n", fname);
meillo@14 225 /* this should work if newline is delimiter */
meillo@14 226 if (ch == sep)
meillo@14 227 isdelim = 1;
meillo@14 228 if (ch == '\n') {
meillo@14 229 if (!isdelim && !sflag)
meillo@14 230 (void)printf("%s", lbuf);
meillo@14 231 break;
meillo@14 232 }
meillo@14 233 }
meillo@14 234 if (!isdelim)
meillo@14 235 continue;
meillo@14 236
meillo@14 237 pos = positions + 1;
meillo@14 238 for (field = maxval, p = lbuf; field; --field, ++pos) {
meillo@14 239 if (*pos) {
meillo@14 240 if (output++)
meillo@14 241 (void)putchar(sep);
meillo@14 242 while ((ch = *p++) != '\n' && ch != sep)
meillo@14 243 (void)putchar(ch);
meillo@14 244 } else
meillo@14 245 while ((ch = *p++) != '\n' && ch != sep);
meillo@14 246 if (ch == '\n')
meillo@14 247 break;
meillo@14 248 }
meillo@14 249 if (ch != '\n')
meillo@14 250 if (autostop) {
meillo@14 251 if (output)
meillo@14 252 (void)putchar(sep);
meillo@14 253 for (; (ch = *p) != '\n'; ++p)
meillo@14 254 (void)putchar(ch);
meillo@14 255 } else
meillo@14 256 for (; (ch = *p) != '\n'; ++p);
meillo@14 257 (void)putchar('\n');
meillo@14 258 }
meillo@14 259 }
meillo@14 260
meillo@14 261 void
meillo@14 262 usage()
meillo@14 263 {
meillo@14 264 (void)fprintf(stderr,
meillo@14 265 "usage:\tcut -c list [file1 ...]\n\tcut -f list [-s] [-d delim] [file ...]\n");
meillo@14 266 exit(1);
meillo@14 267 }
meillo@14 268
meillo@14 269 #if __STDC__
meillo@14 270 #include <stdarg.h>
meillo@14 271 #else
meillo@14 272 #include <varargs.h>
meillo@14 273 #endif
meillo@14 274
meillo@14 275 void
meillo@14 276 #if __STDC__
meillo@14 277 err(const char *fmt, ...)
meillo@14 278 #else
meillo@14 279 err(fmt, va_alist)
meillo@14 280 char *fmt;
meillo@14 281 va_dcl
meillo@14 282 #endif
meillo@14 283 {
meillo@14 284 va_list ap;
meillo@14 285 #if __STDC__
meillo@14 286 va_start(ap, fmt);
meillo@14 287 #else
meillo@14 288 va_start(ap);
meillo@14 289 #endif
meillo@14 290 (void)fprintf(stderr, "cut: ");
meillo@14 291 (void)vfprintf(stderr, fmt, ap);
meillo@14 292 va_end(ap);
meillo@14 293 (void)fprintf(stderr, "\n");
meillo@14 294 exit(1);
meillo@14 295 /* NOTREACHED */
meillo@14 296 }