csplit.c revision 99024
1171568Sscottl/*-
2171568Sscottl * Copyright (c) 2002 Tim J. Robbins.
3171568Sscottl * All rights reserved.
4171568Sscottl *
5171568Sscottl * Redistribution and use in source and binary forms, with or without
6171568Sscottl * modification, are permitted provided that the following conditions
7171568Sscottl * are met:
8171568Sscottl * 1. Redistributions of source code must retain the above copyright
9171568Sscottl *    notice, this list of conditions and the following disclaimer.
10171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11171568Sscottl *    notice, this list of conditions and the following disclaimer in the
12171568Sscottl *    documentation and/or other materials provided with the distribution.
13171568Sscottl *
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: head/usr.bin/csplit/csplit.c 99024 2002-06-29 05:31:26Z tjr $");
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
63void	 cleanup(void);
64void	 do_lineno(const char *);
65void	 do_rexp(const char *);
66char	*getline(void);
67void	 handlesig(int);
68FILE	*newfile(void);
69void	 toomuch(FILE *, long);
70void	 usage(void);
71
72/*
73 * Command line options
74 */
75const char *prefix;		/* File name prefix */
76long	 sufflen;		/* Number of decimal digits for suffix */
77int	 sflag;			/* Suppress output of file names */
78int	 kflag;			/* Keep output if error occurs */
79
80/*
81 * Other miscellaneous globals (XXX too many)
82 */
83long	 lineno;		/* Current line number in input file */
84long	 reps;			/* Number of repetitions for this pattern */
85long	 nfiles;		/* Number of files output so far */
86long	 maxfiles;		/* Maximum number of files we can create */
87char	 currfile[PATH_MAX];	/* Current output file */
88const char *infn;		/* Name of the input file */
89FILE	*infile;		/* Input file handle */
90FILE	*overfile;		/* Overflow file for toomuch() */
91off_t	 truncofs;		/* Offset this file should be truncated at */
92int	 doclean;		/* Should cleanup() remove output? */
93
94int
95main(int argc, char *argv[])
96{
97	long i;
98	int ch;
99	const char *expr;
100	char *ep, *p;
101	FILE *ofp;
102
103	setlocale(LC_ALL, "");
104
105	kflag = sflag = 0;
106	prefix = "xx";
107	sufflen = 2;
108	while ((ch = getopt(argc, argv, "ksf:n:")) > 0) {
109		switch (ch) {
110		case 'f':
111			prefix = optarg;
112			break;
113		case 'k':
114			kflag = 1;
115			break;
116		case 'n':
117			errno = 0;
118			sufflen = strtol(optarg, &ep, 10);
119			if (sufflen <= 0 || *ep != '\0' || errno != 0)
120				errx(1, "%s: bad suffix length", optarg);
121			break;
122		case 's':
123			sflag = 1;
124			break;
125		default:
126			usage();
127			/*NOTREACHED*/
128		}
129	}
130
131	if (sufflen + strlen(prefix) >= PATH_MAX)
132		errx(1, "name too long");
133
134	argc -= optind;
135	argv += optind;
136
137	if ((infn = *argv++) == NULL)
138		usage();
139	if (strcmp(infn, "-") == 0) {
140		infile = stdin;
141		infn = "stdin";
142	} else if ((infile = fopen(infn, "r")) == NULL)
143		err(1, "%s", infn);
144
145	if (!kflag) {
146		doclean = 1;
147		atexit(cleanup);
148		signal(SIGHUP, handlesig);
149		signal(SIGINT, handlesig);
150		signal(SIGTERM, handlesig);
151	}
152
153	lineno = 0;
154	nfiles = 0;
155	truncofs = 0;
156	overfile = NULL;
157
158	/* Ensure 10^sufflen < LONG_MAX. */
159	for (maxfiles = 1, i = 0; i < sufflen; i++) {
160		if (maxfiles > LONG_MAX / 10)
161			errx(1, "%ld: suffix too long (limit %ld)",
162			    sufflen, i);
163		maxfiles *= 10;
164	}
165
166	/* Create files based on supplied patterns. */
167	while (nfiles < maxfiles - 1 && (expr = *argv++) != NULL) {
168		/* Look ahead & see if this pattern has any repetitions. */
169		if (*argv != NULL && **argv == '{') {
170			errno = 0;
171			reps = strtol(*argv + 1, &ep, 10);
172			if (reps < 0 || *ep != '}' || errno != 0)
173				errx(1, "%s: bad repetition count", *argv + 1);
174			argv++;
175		} else
176			reps = 0;
177
178		if (*expr == '/' || *expr == '%') {
179			do
180				do_rexp(expr);
181			while (reps-- != 0 && nfiles < maxfiles - 1);
182		} else if (isdigit((unsigned char)*expr))
183			do_lineno(expr);
184		else
185			errx(1, "%s: unrecognised pattern", expr);
186	}
187
188	/* Copy the rest into a new file. */
189	if (!feof(infile)) {
190		ofp = newfile();
191		while ((p = getline()) != NULL && fputs(p, ofp) == 0)
192			;
193		if (!sflag)
194			printf("%jd\n", (intmax_t)ftello(ofp));
195		if (fclose(ofp) != 0)
196			err(1, "%s", currfile);
197	}
198
199	toomuch(NULL, 0);
200	doclean = 0;
201
202	return (0);
203}
204
205void
206usage(void)
207{
208
209	fprintf(stderr,
210"usage: csplit [-ks] [-f prefix] [-n number] file args ...\n");
211	exit(1);
212}
213
214void
215handlesig(int sig __unused)
216{
217	const char msg[] = "csplit: caught signal, cleaning up\n";
218
219	write(STDERR_FILENO, msg, sizeof(msg) - 1);
220	cleanup();
221	_exit(2);
222}
223
224/* Create a new output file. */
225FILE *
226newfile(void)
227{
228	FILE *fp;
229
230	if (snprintf(currfile, sizeof(currfile), "%s%0*ld", prefix,
231	    (int)sufflen, nfiles) >= sizeof(currfile)) {
232		errno = ENAMETOOLONG;
233		err(1, NULL);
234	}
235	if ((fp = fopen(currfile, "w+")) == NULL)
236		err(1, "%s", currfile);
237	nfiles++;
238
239	return (fp);
240}
241
242/* Remove partial output, called before exiting. */
243void
244cleanup(void)
245{
246	char fnbuf[PATH_MAX];
247	long i;
248
249	if (!doclean)
250		return;
251
252	/*
253	 * NOTE: One cannot portably assume to be able to call snprintf()
254	 * from inside a signal handler. It does, however, appear to be safe
255	 * to do on FreeBSD. The solution to this problem is worse than the
256	 * problem itself.
257	 */
258
259	for (i = 0; i < nfiles; i++) {
260		snprintf(fnbuf, sizeof(fnbuf), "%s%0*ld", prefix,
261		    (int)sufflen, i);
262		unlink(fnbuf);
263	}
264}
265
266/* Read a line from the input into a static buffer. */
267char *
268getline(void)
269{
270	static char lbuf[LINE_MAX];
271	FILE *src;
272
273	src = overfile != NULL ? overfile : infile;
274
275again: if (fgets(lbuf, sizeof(lbuf), src) == NULL) {
276		if (src == overfile) {
277			src = infile;
278			goto again;
279		}
280		return (NULL);
281	}
282	if (ferror(src))
283		err(1, "%s", infn);
284	lineno++;
285
286	return (lbuf);
287}
288
289/* Conceptually rewind the input (as obtained by getline()) back `n' lines. */
290void
291toomuch(FILE *ofp, long n)
292{
293	char buf[BUFSIZ];
294	size_t i, nread;
295
296	if (overfile != NULL) {
297		/*
298		 * Truncate the previous file we overflowed into back to
299		 * the correct length, close it.
300		 */
301		if (fflush(overfile) != 0)
302			err(1, "overflow");
303		if (ftruncate(fileno(overfile), truncofs) != 0)
304			err(1, "overflow");
305		if (fclose(overfile) != 0)
306			err(1, "overflow");
307		overfile = NULL;
308	}
309
310	if (n == 0)
311		/* Just tidying up */
312		return;
313
314	lineno -= n;
315
316	/*
317	 * Wind the overflow file backwards to `n' lines before the
318	 * current one.
319	 */
320	do {
321		if (ftello(ofp) < (off_t)sizeof(buf))
322			rewind(ofp);
323		else
324			fseek(ofp, -(long)sizeof(buf), SEEK_CUR);
325		if (ferror(ofp))
326			errx(1, "%s: can't seek", currfile);
327		if ((nread = fread(buf, 1, sizeof(buf), ofp)) == 0)
328			errx(1, "can't read overflowed output");
329		if (fseek(ofp, -(long)nread, SEEK_CUR) != 0)
330			err(1, "%s", currfile);
331		for (i = 1; i <= nread; i++)
332			if (buf[nread - i] == '\n' && n-- == 0)
333				break;
334		if (ftello(ofp) == 0)
335			break;
336	} while (n > 0);
337	if (fseek(ofp, nread - i + 1, SEEK_CUR) != 0)
338		err(1, "%s", currfile);
339
340	/*
341	 * getline() will read from here. Next call will truncate to
342	 * truncofs in this file.
343	 */
344	overfile = ofp;
345	truncofs = ftello(overfile);
346}
347
348/* Handle splits for /regexp/ and %regexp% patterns. */
349void
350do_rexp(const char *expr)
351{
352	regex_t cre;
353	intmax_t nwritten;
354	long ofs;
355	int first;
356	char *ecopy, *ep, *p, *pofs, *re;
357	FILE *ofp;
358
359	if ((ecopy = strdup(expr)) == NULL)
360		err(1, "strdup");
361
362	re = ecopy + 1;
363	if ((pofs = strrchr(ecopy, *expr)) == NULL || pofs[-1] == '\\')
364		errx(1, "%s: missing trailing %c", expr, *expr);
365	*pofs++ = '\0';
366
367	if (*pofs != '\0') {
368		errno = 0;
369		ofs = strtol(pofs, &ep, 10);
370		if (*ep != '\0' || errno != 0)
371			errx(1, "%s: bad offset", pofs);
372	} else
373		ofs = 0;
374
375	if (regcomp(&cre, re, REG_BASIC|REG_NOSUB) != 0)
376		errx(1, "%s: bad regular expression", re);
377
378	if (*expr == '/')
379		/* /regexp/: Save results to a file. */
380		ofp = newfile();
381	else {
382		/* %regexp%: Make a temporary file for overflow. */
383		if ((ofp = tmpfile()) == NULL)
384			err(1, "tmpfile");
385	}
386
387	/* Read and output lines until we get a match. */
388	first = 1;
389	while ((p = getline()) != NULL) {
390		if (fputs(p, ofp) != 0)
391			break;
392		if (!first && regexec(&cre, p, 0, NULL, 0) == 0)
393			break;
394		first = 0;
395	}
396
397	if (p == NULL)
398		errx(1, "%s: no match", re);
399
400	if (ofs <= 0) {
401		/*
402		 * Negative (or zero) offset: throw back any lines we should
403		 * not have read yet.
404		  */
405		if (p != NULL) {
406			toomuch(ofp, -ofs + 1);
407			nwritten = (intmax_t)truncofs;
408		} else
409			nwritten = (intmax_t)ftello(ofp);
410	} else {
411		/*
412		 * Positive offset: copy the requested number of lines
413		 * after the match.
414		 */
415		while (--ofs > 0 && (p = getline()) != NULL)
416			fputs(p, ofp);
417		toomuch(NULL, 0);
418		nwritten = (intmax_t)ftello(ofp);
419		if (fclose(ofp) != 0)
420			err(1, "%s", currfile);
421	}
422
423	if (!sflag && *expr == '/')
424		printf("%jd\n", nwritten);
425
426	regfree(&cre);
427	free(ecopy);
428}
429
430/* Handle splits based on line number. */
431void
432do_lineno(const char *expr)
433{
434	long lastline, tgtline;
435	char *ep, *p;
436	FILE *ofp;
437
438	errno = 0;
439	tgtline = strtol(expr, &ep, 10);
440	if (tgtline <= 0 || errno != 0 || *ep != '\0')
441		errx(1, "%s: bad line number", expr);
442	lastline = tgtline;
443	if (lastline <= lineno)
444		errx(1, "%s: can't go backwards", expr);
445
446	while (nfiles < maxfiles - 1) {
447		ofp = newfile();
448		while (lineno + 1 != lastline) {
449			if ((p = getline()) == NULL)
450				errx(1, "%ld: out of range", lastline);
451			if (fputs(p, ofp) != 0)
452				break;
453		}
454		if (!sflag)
455			printf("%jd\n", (intmax_t)ftello(ofp));
456		if (fclose(ofp) != 0)
457			err(1, "%s", currfile);
458		if (reps-- == 0)
459			break;
460		lastline += tgtline;
461	}
462}
463