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