1 /*- 2 * Copyright (c) 2002 Tim J. Robbins. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * csplit -- split files based on context 29 * 30 * This utility splits its input into numbered output files by line number 31 * or by a regular expression. Regular expression matches have an optional 32 * offset with them, allowing the split to occur a specified number of 33 * lines before or after the match. 34 * 35 * To handle negative offsets, we stop reading when the match occurs and 36 * store the offset that the file should have been split at, then use 37 * this output file as input until all the "overflowed" lines have been read. 38 * The file is then closed and truncated to the correct length. 39 * 40 * We assume that the output files can be seeked upon (ie. they cannot be 41 * symlinks to named pipes or character devices), but make no such 42 * assumption about the input. 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/types.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <limits.h> 54 #include <locale.h> 55 #include <regex.h> 56 #include <signal.h> 57 #include <stdint.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 static void cleanup(void); 64 static void do_lineno(const char *); 65 static void do_rexp(const char *); 66 static char *get_line(void); 67 static void handlesig(int); 68 static FILE *newfile(void); 69 static void toomuch(FILE *, long); 70 static void usage(void); 71 72 /* 73 * Command line options 74 */ 75 static const char *prefix; /* File name prefix */ 76 static long sufflen; /* Number of decimal digits for suffix */ 77 static int sflag; /* Suppress output of file names */ 78 static int kflag; /* Keep output if error occurs */ 79 80 /* 81 * Other miscellaneous globals (XXX too many) 82 */ 83 static long lineno; /* Current line number in input file */ 84 static long reps; /* Number of repetitions for this pattern */ 85 static long nfiles; /* Number of files output so far */ 86 static long maxfiles; /* Maximum number of files we can create */ 87 static char currfile[PATH_MAX]; /* Current output file */ 88 static const char *infn; /* Name of the input file */ 89 static FILE *infile; /* Input file handle */ 90 static FILE *overfile; /* Overflow file for toomuch() */ 91 static off_t truncofs; /* Offset this file should be truncated at */ 92 static int doclean; /* Should cleanup() remove output? */ 93 94 int 95 main(int argc, char *argv[]) 96 { 97 struct sigaction sa; 98 long i; 99 int ch; 100 const char *expr; 101 char *ep, *p; 102 FILE *ofp; 103 104 setlocale(LC_ALL, ""); 105 106 kflag = sflag = 0; 107 prefix = "xx"; 108 sufflen = 2; 109 while ((ch = getopt(argc, argv, "ksf:n:")) > 0) { 110 switch (ch) { 111 case 'f': 112 prefix = optarg; 113 break; 114 case 'k': 115 kflag = 1; 116 break; 117 case 'n': 118 errno = 0; 119 sufflen = strtol(optarg, &ep, 10); 120 if (sufflen <= 0 || *ep != '\0' || errno != 0) 121 errx(1, "%s: bad suffix length", optarg); 122 break; 123 case 's': 124 sflag = 1; 125 break; 126 default: 127 usage(); 128 /*NOTREACHED*/ 129 } 130 } 131 132 if (sufflen + strlen(prefix) >= PATH_MAX) 133 errx(1, "name too long"); 134 135 argc -= optind; 136 argv += optind; 137 138 if ((infn = *argv++) == NULL) 139 usage(); 140 if (strcmp(infn, "-") == 0) { 141 infile = stdin; 142 infn = "stdin"; 143 } else if ((infile = fopen(infn, "r")) == NULL) 144 err(1, "%s", infn); 145 146 if (!kflag) { 147 doclean = 1; 148 atexit(cleanup); 149 sa.sa_flags = 0; 150 sa.sa_handler = handlesig; 151 sigemptyset(&sa.sa_mask); 152 sigaddset(&sa.sa_mask, SIGHUP); 153 sigaddset(&sa.sa_mask, SIGINT); 154 sigaddset(&sa.sa_mask, SIGTERM); 155 sigaction(SIGHUP, &sa, NULL); 156 sigaction(SIGINT, &sa, NULL); 157 sigaction(SIGTERM, &sa, NULL); 158 } 159 160 lineno = 0; 161 nfiles = 0; 162 truncofs = 0; 163 overfile = NULL; 164 165 /* Ensure 10^sufflen < LONG_MAX. */ 166 for (maxfiles = 1, i = 0; i < sufflen; i++) { 167 if (maxfiles > LONG_MAX / 10) 168 errx(1, "%ld: suffix too long (limit %ld)", 169 sufflen, i); 170 maxfiles *= 10; 171 } 172 173 /* Create files based on supplied patterns. */ 174 while (nfiles < maxfiles - 1 && (expr = *argv++) != NULL) { 175 /* Look ahead & see if this pattern has any repetitions. */ 176 if (*argv != NULL && **argv == '{') { 177 errno = 0; 178 reps = strtol(*argv + 1, &ep, 10); 179 if (reps < 0 || *ep != '}' || errno != 0) 180 errx(1, "%s: bad repetition count", *argv + 1); 181 argv++; 182 } else 183 reps = 0; 184 185 if (*expr == '/' || *expr == '%') { 186 do 187 do_rexp(expr); 188 while (reps-- != 0 && nfiles < maxfiles - 1); 189 } else if (isdigit((unsigned char)*expr)) 190 do_lineno(expr); 191 else 192 errx(1, "%s: unrecognised pattern", expr); 193 } 194 195 /* Copy the rest into a new file. */ 196 if (!feof(infile)) { 197 ofp = newfile(); 198 while ((p = get_line()) != NULL && fputs(p, ofp) != EOF) 199 ; 200 if (!sflag) 201 printf("%jd\n", (intmax_t)ftello(ofp)); 202 if (fclose(ofp) != 0) 203 err(1, "%s", currfile); 204 } 205 206 toomuch(NULL, 0); 207 doclean = 0; 208 209 return (0); 210 } 211 212 static void 213 usage(void) 214 { 215 216 fprintf(stderr, 217 "usage: csplit [-ks] [-f prefix] [-n number] file args ...\n"); 218 exit(1); 219 } 220 221 static void 222 handlesig(int sig __unused) 223 { 224 const char msg[] = "csplit: caught signal, cleaning up\n"; 225 226 write(STDERR_FILENO, msg, sizeof(msg) - 1); 227 cleanup(); 228 _exit(2); 229 } 230 231 /* Create a new output file. */ 232 static FILE * 233 newfile(void) 234 { 235 FILE *fp; 236 237 if ((size_t)snprintf(currfile, sizeof(currfile), "%s%0*ld", prefix, 238 (int)sufflen, nfiles) >= sizeof(currfile)) 239 errc(1, ENAMETOOLONG, NULL); 240 if ((fp = fopen(currfile, "w+")) == NULL) 241 err(1, "%s", currfile); 242 nfiles++; 243 244 return (fp); 245 } 246 247 /* Remove partial output, called before exiting. */ 248 static void 249 cleanup(void) 250 { 251 char fnbuf[PATH_MAX]; 252 long i; 253 254 if (!doclean) 255 return; 256 257 /* 258 * NOTE: One cannot portably assume to be able to call snprintf() 259 * from inside a signal handler. It does, however, appear to be safe 260 * to do on FreeBSD. The solution to this problem is worse than the 261 * problem itself. 262 */ 263 264 for (i = 0; i < nfiles; i++) { 265 snprintf(fnbuf, sizeof(fnbuf), "%s%0*ld", prefix, 266 (int)sufflen, i); 267 unlink(fnbuf); 268 } 269 } 270 271 /* Read a line from the input into a static buffer. */ 272 static char * 273 get_line(void) 274 { 275 static char lbuf[LINE_MAX]; 276 FILE *src; 277 278 src = overfile != NULL ? overfile : infile; 279 280 again: if (fgets(lbuf, sizeof(lbuf), src) == NULL) { 281 if (src == overfile) { 282 src = infile; 283 goto again; 284 } 285 return (NULL); 286 } 287 if (ferror(src)) 288 err(1, "%s", infn); 289 lineno++; 290 291 return (lbuf); 292 } 293 294 /* Conceptually rewind the input (as obtained by get_line()) back `n' lines. */ 295 static void 296 toomuch(FILE *ofp, long n) 297 { 298 char buf[BUFSIZ]; 299 size_t i, nread; 300 301 if (overfile != NULL) { 302 /* 303 * Truncate the previous file we overflowed into back to 304 * the correct length, close it. 305 */ 306 if (fflush(overfile) != 0) 307 err(1, "overflow"); 308 if (ftruncate(fileno(overfile), truncofs) != 0) 309 err(1, "overflow"); 310 if (fclose(overfile) != 0) 311 err(1, "overflow"); 312 overfile = NULL; 313 } 314 315 if (n == 0) 316 /* Just tidying up */ 317 return; 318 319 lineno -= n; 320 321 /* 322 * Wind the overflow file backwards to `n' lines before the 323 * current one. 324 */ 325 do { 326 if (ftello(ofp) < (off_t)sizeof(buf)) 327 rewind(ofp); 328 else 329 fseeko(ofp, -(off_t)sizeof(buf), SEEK_CUR); 330 if (ferror(ofp)) 331 errx(1, "%s: can't seek", currfile); 332 if ((nread = fread(buf, 1, sizeof(buf), ofp)) == 0) 333 errx(1, "can't read overflowed output"); 334 if (fseeko(ofp, -(off_t)nread, SEEK_CUR) != 0) 335 err(1, "%s", currfile); 336 for (i = 1; i <= nread; i++) 337 if (buf[nread - i] == '\n' && n-- == 0) 338 break; 339 if (ftello(ofp) == 0) 340 break; 341 } while (n > 0); 342 if (fseeko(ofp, nread - i + 1, SEEK_CUR) != 0) 343 err(1, "%s", currfile); 344 345 /* 346 * get_line() will read from here. Next call will truncate to 347 * truncofs in this file. 348 */ 349 overfile = ofp; 350 truncofs = ftello(overfile); 351 } 352 353 /* Handle splits for /regexp/ and %regexp% patterns. */ 354 static void 355 do_rexp(const char *expr) 356 { 357 regex_t cre; 358 intmax_t nwritten; 359 long ofs; 360 int first; 361 char *ecopy, *ep, *p, *pofs, *re; 362 FILE *ofp; 363 364 if ((ecopy = strdup(expr)) == NULL) 365 err(1, "strdup"); 366 367 re = ecopy + 1; 368 if ((pofs = strrchr(ecopy, *expr)) == NULL || pofs[-1] == '\\') 369 errx(1, "%s: missing trailing %c", expr, *expr); 370 *pofs++ = '\0'; 371 372 if (*pofs != '\0') { 373 errno = 0; 374 ofs = strtol(pofs, &ep, 10); 375 if (*ep != '\0' || errno != 0) 376 errx(1, "%s: bad offset", pofs); 377 } else 378 ofs = 0; 379 380 if (regcomp(&cre, re, REG_BASIC|REG_NOSUB) != 0) 381 errx(1, "%s: bad regular expression", re); 382 383 if (*expr == '/') 384 /* /regexp/: Save results to a file. */ 385 ofp = newfile(); 386 else { 387 /* %regexp%: Make a temporary file for overflow. */ 388 if ((ofp = tmpfile()) == NULL) 389 err(1, "tmpfile"); 390 } 391 392 /* Read and output lines until we get a match. */ 393 first = 1; 394 while ((p = get_line()) != NULL) { 395 if (fputs(p, ofp) == EOF) 396 break; 397 if (!first && regexec(&cre, p, 0, NULL, 0) == 0) 398 break; 399 first = 0; 400 } 401 402 if (p == NULL) { 403 toomuch(NULL, 0); 404 errx(1, "%s: no match", re); 405 } 406 407 if (ofs <= 0) { 408 /* 409 * Negative (or zero) offset: throw back any lines we should 410 * not have read yet. 411 */ 412 if (p != NULL) { 413 toomuch(ofp, -ofs + 1); 414 nwritten = (intmax_t)truncofs; 415 } else 416 nwritten = (intmax_t)ftello(ofp); 417 } else { 418 /* 419 * Positive offset: copy the requested number of lines 420 * after the match. 421 */ 422 while (--ofs > 0 && (p = get_line()) != NULL) 423 fputs(p, ofp); 424 toomuch(NULL, 0); 425 nwritten = (intmax_t)ftello(ofp); 426 if (fclose(ofp) != 0) 427 err(1, "%s", currfile); 428 } 429 430 if (!sflag && *expr == '/') 431 printf("%jd\n", nwritten); 432 433 regfree(&cre); 434 free(ecopy); 435 } 436 437 /* Handle splits based on line number. */ 438 static void 439 do_lineno(const char *expr) 440 { 441 long lastline, tgtline; 442 char *ep, *p; 443 FILE *ofp; 444 445 errno = 0; 446 tgtline = strtol(expr, &ep, 10); 447 if (tgtline <= 0 || errno != 0 || *ep != '\0') 448 errx(1, "%s: bad line number", expr); 449 lastline = tgtline; 450 if (lastline <= lineno) 451 errx(1, "%s: can't go backwards", expr); 452 453 while (nfiles < maxfiles - 1) { 454 ofp = newfile(); 455 while (lineno + 1 != lastline) { 456 if ((p = get_line()) == NULL) 457 errx(1, "%ld: out of range", lastline); 458 if (fputs(p, ofp) == EOF) 459 break; 460 } 461 if (!sflag) 462 printf("%jd\n", (intmax_t)ftello(ofp)); 463 if (fclose(ofp) != 0) 464 err(1, "%s", currfile); 465 if (reps-- == 0) 466 break; 467 lastline += tgtline; 468 } 469 } 470