(xalloc_die): Declare to be `extern', not `static'
[platform/upstream/coreutils.git] / src / csplit.c
1 /* csplit - split a file into sections determined by context lines
2    Copyright (C) 91, 1995-2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Stuart Kemp, cpsrk@groper.jcu.edu.au.
19    Modified by David MacKenzie, djm@gnu.ai.mit.edu. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <getopt.h>
25 #include <sys/types.h>
26 #include <signal.h>
27
28 #include "system.h"
29
30 #include <regex.h>
31
32 #include "error.h"
33 #include "inttostr.h"
34 #include "safe-read.h"
35 #include "quote.h"
36 #include "xstrtol.h"
37
38 #ifndef SA_NOCLDSTOP
39 # define sigprocmask(How, Set, Oset) /* empty */
40 # define sigset_t int
41 #endif
42
43 /* The official name of this program (e.g., no `g' prefix).  */
44 #define PROGRAM_NAME "csplit"
45
46 #define AUTHORS "Stuart Kemp", "David MacKenzie"
47
48 /* Increment size of area for control records. */
49 #define ALLOC_SIZE 20
50
51 /* The default prefix for output file names. */
52 #define DEFAULT_PREFIX  "xx"
53
54 /* A compiled pattern arg. */
55 struct control
56 {
57   char *regexpr;                /* Non-compiled regular expression. */
58   struct re_pattern_buffer re_compiled; /* Compiled regular expression. */
59   intmax_t offset;              /* Offset from regexp to split at. */
60   uintmax_t lines_required;     /* Number of lines required. */
61   uintmax_t repeat;             /* Repeat count. */
62   int argnum;                   /* ARGV index. */
63   bool repeat_forever;          /* True if `*' used as a repeat count. */
64   bool ignore;                  /* If true, produce no output (for regexp). */
65 };
66
67 /* Initial size of data area in buffers. */
68 #define START_SIZE      8191
69
70 /* Increment size for data area. */
71 #define INCR_SIZE       2048
72
73 /* Number of lines kept in each node in line list. */
74 #define CTRL_SIZE       80
75
76 #ifdef DEBUG
77 /* Some small values to test the algorithms. */
78 # define START_SIZE     200
79 # define INCR_SIZE      10
80 # define CTRL_SIZE      1
81 #endif
82
83 /* A string with a length count. */
84 struct cstring
85 {
86   size_t len;
87   char *str;
88 };
89
90 /* Pointers to the beginnings of lines in the buffer area.
91    These structures are linked together if needed. */
92 struct line
93 {
94   size_t used;                  /* Number of offsets used in this struct. */
95   size_t insert_index;          /* Next offset to use when inserting line. */
96   size_t retrieve_index;        /* Next index to use when retrieving line. */
97   struct cstring starts[CTRL_SIZE]; /* Lines in the data area. */
98   struct line *next;            /* Next in linked list. */
99 };
100
101 /* The structure to hold the input lines.
102    Contains a pointer to the data area and a list containing
103    pointers to the individual lines. */
104 struct buffer_record
105 {
106   size_t bytes_alloc;           /* Size of the buffer area. */
107   size_t bytes_used;            /* Bytes used in the buffer area. */
108   uintmax_t start_line;         /* First line number in this buffer. */
109   uintmax_t first_available;    /* First line that can be retrieved. */
110   size_t num_lines;             /* Number of complete lines in this buffer. */
111   char *buffer;                 /* Data area. */
112   struct line *line_start;      /* Head of list of pointers to lines. */
113   struct line *curr_line;       /* The line start record currently in use. */
114   struct buffer_record *next;
115 };
116
117 static void close_output_file (void);
118 static void create_output_file (void);
119 static void delete_all_files (void);
120 static void save_line_to_file (const struct cstring *line);
121 void usage (int status);
122
123 /* The name this program was run with. */
124 char *program_name;
125
126 /* Input file descriptor. */
127 static int input_desc = 0;
128
129 /* Start of buffer list. */
130 static struct buffer_record *head = NULL;
131
132 /* Partially read line. */
133 static char *hold_area = NULL;
134
135 /* Number of bytes in `hold_area'. */
136 static size_t hold_count = 0;
137
138 /* Number of the last line in the buffers. */
139 static uintmax_t last_line_number = 0;
140
141 /* Number of the line currently being examined. */
142 static uintmax_t current_line = 0;
143
144 /* If true, we have read EOF. */
145 static bool have_read_eof = false;
146
147 /* Name of output files. */
148 static char * volatile filename_space = NULL;
149
150 /* Prefix part of output file names. */
151 static char * volatile prefix = NULL;
152
153 /* Suffix part of output file names. */
154 static char * volatile suffix = NULL;
155
156 /* Number of digits to use in output file names. */
157 static int volatile digits = 2;
158
159 /* Number of files created so far. */
160 static unsigned int volatile files_created = 0;
161
162 /* Number of bytes written to current file. */
163 static uintmax_t bytes_written;
164
165 /* Output file pointer. */
166 static FILE *output_stream = NULL;
167
168 /* Output file name. */
169 static char *output_filename = NULL;
170
171 /* Perhaps it would be cleaner to pass arg values instead of indexes. */
172 static char **global_argv;
173
174 /* If true, do not print the count of bytes in each output file. */
175 static bool suppress_count;
176
177 /* If true, remove output files on error. */
178 static bool volatile remove_files;
179
180 /* If true, remove all output files which have a zero length. */
181 static bool elide_empty_files;
182
183 /* The compiled pattern arguments, which determine how to split
184    the input file. */
185 static struct control *controls;
186
187 /* Number of elements in `controls'. */
188 static size_t control_used;
189
190 /* The set of signals that are caught.  */
191 static sigset_t caught_signals;
192
193 static struct option const longopts[] =
194 {
195   {"digits", required_argument, NULL, 'n'},
196   {"quiet", no_argument, NULL, 'q'},
197   {"silent", no_argument, NULL, 's'},
198   {"keep-files", no_argument, NULL, 'k'},
199   {"elide-empty-files", no_argument, NULL, 'z'},
200   {"prefix", required_argument, NULL, 'f'},
201   {"suffix-format", required_argument, NULL, 'b'},
202   {GETOPT_HELP_OPTION_DECL},
203   {GETOPT_VERSION_OPTION_DECL},
204   {NULL, 0, NULL, 0}
205 };
206
207 /* Optionally remove files created so far; then exit.
208    Called when an error detected. */
209
210 static void
211 cleanup (void)
212 {
213   sigset_t oldset;
214
215   close_output_file ();
216
217   sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
218   delete_all_files ();
219   sigprocmask (SIG_SETMASK, &oldset, NULL);
220 }
221
222 static void cleanup_fatal (void) ATTRIBUTE_NORETURN;
223 static void
224 cleanup_fatal (void)
225 {
226   cleanup ();
227   exit (EXIT_FAILURE);
228 }
229
230 extern void
231 xalloc_die (void)
232 {
233   error (0, 0, "%s", _("memory exhausted"));
234   cleanup_fatal ();
235 }
236
237 static void
238 interrupt_handler (int sig)
239 {
240 #ifndef SA_NOCLDSTOP
241   signal (sig, SIG_IGN);
242 #endif
243
244   delete_all_files ();
245
246   signal (sig, SIG_DFL);
247   raise (sig);
248 }
249
250 /* Keep track of NUM bytes of a partial line in buffer START.
251    These bytes will be retrieved later when another large buffer is read.
252    It is not necessary to create a new buffer for these bytes; instead,
253    we keep a pointer to the existing buffer.  This buffer *is* on the
254    free list, and when the next buffer is obtained from this list
255    (even if it is this one), these bytes will be placed at the
256    start of the new buffer. */
257
258 static void
259 save_to_hold_area (char *start, size_t num)
260 {
261   hold_area = start;
262   hold_count = num;
263 }
264
265 /* Read up to MAX_N_BYTES bytes from the input stream into DEST.
266    Return the number of bytes read. */
267
268 static size_t
269 read_input (char *dest, size_t max_n_bytes)
270 {
271   size_t bytes_read;
272
273   if (max_n_bytes == 0)
274     return 0;
275
276   bytes_read = safe_read (input_desc, dest, max_n_bytes);
277
278   if (bytes_read == 0)
279     have_read_eof = true;
280
281   if (bytes_read == SAFE_READ_ERROR)
282     {
283       error (0, errno, _("read error"));
284       cleanup_fatal ();
285     }
286
287   return bytes_read;
288 }
289
290 /* Initialize existing line record P. */
291
292 static void
293 clear_line_control (struct line *p)
294 {
295   p->used = 0;
296   p->insert_index = 0;
297   p->retrieve_index = 0;
298 }
299
300 /* Return a new, initialized line record. */
301
302 static struct line *
303 new_line_control (void)
304 {
305   struct line *p = xmalloc (sizeof *p);
306
307   p->next = NULL;
308   clear_line_control (p);
309
310   return p;
311 }
312
313 /* Record LINE_START, which is the address of the start of a line
314    of length LINE_LEN in the large buffer, in the lines buffer of B. */
315
316 static void
317 keep_new_line (struct buffer_record *b, char *line_start, size_t line_len)
318 {
319   struct line *l;
320
321   /* If there is no existing area to keep line info, get some. */
322   if (b->line_start == NULL)
323     b->line_start = b->curr_line = new_line_control ();
324
325   /* If existing area for lines is full, get more. */
326   if (b->curr_line->used == CTRL_SIZE)
327     {
328       b->curr_line->next = new_line_control ();
329       b->curr_line = b->curr_line->next;
330     }
331
332   l = b->curr_line;
333
334   /* Record the start of the line, and update counters. */
335   l->starts[l->insert_index].str = line_start;
336   l->starts[l->insert_index].len = line_len;
337   l->used++;
338   l->insert_index++;
339 }
340
341 /* Scan the buffer in B for newline characters
342    and record the line start locations and lengths in B.
343    Return the number of lines found in this buffer.
344
345    There may be an incomplete line at the end of the buffer;
346    a pointer is kept to this area, which will be used when
347    the next buffer is filled. */
348
349 static size_t
350 record_line_starts (struct buffer_record *b)
351 {
352   char *line_start;             /* Start of current line. */
353   char *line_end;               /* End of each line found. */
354   size_t bytes_left;            /* Length of incomplete last line. */
355   size_t lines;                 /* Number of lines found. */
356   size_t line_length;           /* Length of each line found. */
357
358   if (b->bytes_used == 0)
359     return 0;
360
361   lines = 0;
362   line_start = b->buffer;
363   bytes_left = b->bytes_used;
364
365   for (;;)
366     {
367       line_end = memchr (line_start, '\n', bytes_left);
368       if (line_end == NULL)
369         break;
370       line_length = line_end - line_start + 1;
371       keep_new_line (b, line_start, line_length);
372       bytes_left -= line_length;
373       line_start = line_end + 1;
374       lines++;
375     }
376
377   /* Check for an incomplete last line. */
378   if (bytes_left)
379     {
380       if (have_read_eof)
381         {
382           keep_new_line (b, line_start, bytes_left);
383           lines++;
384         }
385       else
386         save_to_hold_area (line_start, bytes_left);
387     }
388
389   b->num_lines = lines;
390   b->first_available = b->start_line = last_line_number + 1;
391   last_line_number += lines;
392
393   return lines;
394 }
395
396 /* Return a new buffer with room to store SIZE bytes, plus
397    an extra byte for safety. */
398
399 static struct buffer_record *
400 create_new_buffer (size_t size)
401 {
402   struct buffer_record *new_buffer = xmalloc (sizeof *new_buffer);
403
404   new_buffer->buffer = xmalloc (size + 1);
405
406   new_buffer->bytes_alloc = size;
407   new_buffer->line_start = new_buffer->curr_line = NULL;
408
409   return new_buffer;
410 }
411
412 /* Return a new buffer of at least MINSIZE bytes.  If a buffer of at
413    least that size is currently free, use it, otherwise create a new one. */
414
415 static struct buffer_record *
416 get_new_buffer (size_t min_size)
417 {
418   struct buffer_record *new_buffer; /* Buffer to return. */
419   size_t alloc_size;    /* Actual size that will be requested. */
420
421   alloc_size = START_SIZE;
422   if (alloc_size < min_size)
423     {
424       size_t s = min_size - alloc_size + INCR_SIZE - 1;
425       alloc_size += s - s % INCR_SIZE;
426     }
427
428   new_buffer = create_new_buffer (alloc_size);
429
430   new_buffer->num_lines = 0;
431   new_buffer->bytes_used = 0;
432   new_buffer->start_line = new_buffer->first_available = last_line_number + 1;
433   new_buffer->next = NULL;
434
435   return new_buffer;
436 }
437
438 static void
439 free_buffer (struct buffer_record *buf)
440 {
441   free (buf->buffer);
442 }
443
444 /* Append buffer BUF to the linked list of buffers that contain
445    some data yet to be processed. */
446
447 static void
448 save_buffer (struct buffer_record *buf)
449 {
450   struct buffer_record *p;
451
452   buf->next = NULL;
453   buf->curr_line = buf->line_start;
454
455   if (head == NULL)
456     head = buf;
457   else
458     {
459       for (p = head; p->next; p = p->next)
460         /* Do nothing. */ ;
461       p->next = buf;
462     }
463 }
464
465 /* Fill a buffer of input.
466
467    Set the initial size of the buffer to a default.
468    Fill the buffer (from the hold area and input stream)
469    and find the individual lines.
470    If no lines are found (the buffer is too small to hold the next line),
471    release the current buffer (whose contents would have been put in the
472    hold area) and repeat the process with another large buffer until at least
473    one entire line has been read.
474
475    Return true if a new buffer was obtained, otherwise false
476    (in which case end-of-file must have been encountered). */
477
478 static bool
479 load_buffer (void)
480 {
481   struct buffer_record *b;
482   size_t bytes_wanted = START_SIZE; /* Minimum buffer size. */
483   size_t bytes_avail;           /* Size of new buffer created. */
484   size_t lines_found;           /* Number of lines in this new buffer. */
485   char *p;                      /* Place to load into buffer. */
486
487   if (have_read_eof)
488     return false;
489
490   /* We must make the buffer at least as large as the amount of data
491      in the partial line left over from the last call. */
492   if (bytes_wanted < hold_count)
493     bytes_wanted = hold_count;
494
495   do
496     {
497       b = get_new_buffer (bytes_wanted);
498       bytes_avail = b->bytes_alloc; /* Size of buffer returned. */
499       p = b->buffer;
500
501       /* First check the `holding' area for a partial line. */
502       if (hold_count)
503         {
504           if (p != hold_area)
505             memcpy (p, hold_area, hold_count);
506           p += hold_count;
507           b->bytes_used += hold_count;
508           bytes_avail -= hold_count;
509           hold_count = 0;
510         }
511
512       b->bytes_used += read_input (p, bytes_avail);
513
514       lines_found = record_line_starts (b);
515       bytes_wanted = b->bytes_alloc * 2;
516       if (!lines_found)
517         free_buffer (b);
518     }
519   while (!lines_found && !have_read_eof);
520
521   if (lines_found)
522     save_buffer (b);
523
524   return lines_found != 0;
525 }
526
527 /* Return the line number of the first line that has not yet been retrieved. */
528
529 static uintmax_t
530 get_first_line_in_buffer (void)
531 {
532   if (head == NULL && !load_buffer ())
533     error (EXIT_FAILURE, errno, _("input disappeared"));
534
535   return head->first_available;
536 }
537
538 /* Return a pointer to the logical first line in the buffer and make the
539    next line the logical first line.
540    Return NULL if there is no more input. */
541
542 static struct cstring *
543 remove_line (void)
544 {
545   /* If non-NULL, this is the buffer for which the previous call
546      returned the final line.  So now, presuming that line has been
547      processed, we can free the buffer and reset this pointer.  */
548   static struct buffer_record *prev_buf = NULL;
549
550   struct cstring *line;         /* Return value. */
551   struct line *l;               /* For convenience. */
552
553   if (prev_buf)
554     {
555       free_buffer (prev_buf);
556       prev_buf = NULL;
557     }
558
559   if (head == NULL && !load_buffer ())
560     return NULL;
561
562   if (current_line < head->first_available)
563     current_line = head->first_available;
564
565   ++(head->first_available);
566
567   l = head->curr_line;
568
569   line = &l->starts[l->retrieve_index];
570
571   /* Advance index to next line. */
572   if (++l->retrieve_index == l->used)
573     {
574       /* Go on to the next line record. */
575       head->curr_line = l->next;
576       if (head->curr_line == NULL || head->curr_line->used == 0)
577         {
578           /* Go on to the next data block.
579              but first record the current one so we can free it
580              once the line we're returning has been processed.  */
581           prev_buf = head;
582           head = head->next;
583         }
584     }
585
586   return line;
587 }
588
589 /* Search the buffers for line LINENUM, reading more input if necessary.
590    Return a pointer to the line, or NULL if it is not found in the file. */
591
592 static struct cstring *
593 find_line (uintmax_t linenum)
594 {
595   struct buffer_record *b;
596
597   if (head == NULL && !load_buffer ())
598     return NULL;
599
600   if (linenum < head->start_line)
601     return NULL;
602
603   for (b = head;;)
604     {
605       if (linenum < b->start_line + b->num_lines)
606         {
607           /* The line is in this buffer. */
608           struct line *l;
609           size_t offset;        /* How far into the buffer the line is. */
610
611           l = b->line_start;
612           offset = linenum - b->start_line;
613           /* Find the control record. */
614           while (offset >= CTRL_SIZE)
615             {
616               l = l->next;
617               offset -= CTRL_SIZE;
618             }
619           return &l->starts[offset];
620         }
621       if (b->next == NULL && !load_buffer ())
622         return NULL;
623       b = b->next;              /* Try the next data block. */
624     }
625 }
626
627 /* Return true if at least one more line is available for input. */
628
629 static bool
630 no_more_lines (void)
631 {
632   return find_line (current_line + 1) == NULL;
633 }
634
635 /* Set the name of the input file to NAME and open it. */
636
637 static void
638 set_input_file (const char *name)
639 {
640   if (STREQ (name, "-"))
641     input_desc = 0;
642   else
643     {
644       input_desc = open (name, O_RDONLY);
645       if (input_desc < 0)
646         error (EXIT_FAILURE, errno, "%s", name);
647     }
648 }
649
650 /* Write all lines from the beginning of the buffer up to, but
651    not including, line LAST_LINE, to the current output file.
652    If IGNORE is true, do not output lines selected here.
653    ARGNUM is the index in ARGV of the current pattern. */
654
655 static void
656 write_to_file (uintmax_t last_line, bool ignore, int argnum)
657 {
658   struct cstring *line;
659   uintmax_t first_line;         /* First available input line. */
660   uintmax_t lines;              /* Number of lines to output. */
661   uintmax_t i;
662
663   first_line = get_first_line_in_buffer ();
664
665   if (first_line > last_line)
666     {
667       error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
668       cleanup_fatal ();
669     }
670
671   lines = last_line - first_line;
672
673   for (i = 0; i < lines; i++)
674     {
675       line = remove_line ();
676       if (line == NULL)
677         {
678           error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
679           cleanup_fatal ();
680         }
681       if (!ignore)
682         save_line_to_file (line);
683     }
684 }
685
686 /* Output any lines left after all regexps have been processed. */
687
688 static void
689 dump_rest_of_file (void)
690 {
691   struct cstring *line;
692
693   while ((line = remove_line ()) != NULL)
694     save_line_to_file (line);
695 }
696
697 /* Handle an attempt to read beyond EOF under the control of record P,
698    on iteration REPETITION if nonzero. */
699
700 static void handle_line_error (const struct control *, uintmax_t)
701      ATTRIBUTE_NORETURN;
702 static void
703 handle_line_error (const struct control *p, uintmax_t repetition)
704 {
705   char buf[INT_BUFSIZE_BOUND (uintmax_t)];
706
707   fprintf (stderr, _("%s: `%s': line number out of range"),
708            program_name, umaxtostr (p->lines_required, buf));
709   if (repetition)
710     fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
711   else
712     fprintf (stderr, "\n");
713
714   cleanup_fatal ();
715 }
716
717 /* Determine the line number that marks the end of this file,
718    then get those lines and save them to the output file.
719    P is the control record.
720    REPETITION is the repetition number. */
721
722 static void
723 process_line_count (const struct control *p, uintmax_t repetition)
724 {
725   uintmax_t linenum;
726   uintmax_t last_line_to_save = p->lines_required * (repetition + 1);
727   struct cstring *line;
728
729   create_output_file ();
730
731   linenum = get_first_line_in_buffer ();
732
733   while (linenum++ < last_line_to_save)
734     {
735       line = remove_line ();
736       if (line == NULL)
737         handle_line_error (p, repetition);
738       save_line_to_file (line);
739     }
740
741   close_output_file ();
742
743   /* Ensure that the line number specified is not 1 greater than
744      the number of lines in the file. */
745   if (no_more_lines ())
746     handle_line_error (p, repetition);
747 }
748
749 static void regexp_error (struct control *, uintmax_t, bool) ATTRIBUTE_NORETURN;
750 static void
751 regexp_error (struct control *p, uintmax_t repetition, bool ignore)
752 {
753   fprintf (stderr, _("%s: `%s': match not found"),
754            program_name, global_argv[p->argnum]);
755
756   if (repetition)
757     {
758       char buf[INT_BUFSIZE_BOUND (uintmax_t)];
759       fprintf (stderr, _(" on repetition %s\n"), umaxtostr (repetition, buf));
760     }
761   else
762     fprintf (stderr, "\n");
763
764   if (!ignore)
765     {
766       dump_rest_of_file ();
767       close_output_file ();
768     }
769   cleanup_fatal ();
770 }
771
772 /* Read the input until a line matches the regexp in P, outputting
773    it unless P->IGNORE is true.
774    REPETITION is this repeat-count; 0 means the first time. */
775
776 static void
777 process_regexp (struct control *p, uintmax_t repetition)
778 {
779   struct cstring *line;         /* From input file. */
780   size_t line_len;              /* To make "$" in regexps work. */
781   uintmax_t break_line;         /* First line number of next file. */
782   bool ignore = p->ignore;      /* If true, skip this section. */
783   int ret;
784
785   if (!ignore)
786     create_output_file ();
787
788   /* If there is no offset for the regular expression, or
789      it is positive, then it is not necessary to buffer the lines. */
790
791   if (p->offset >= 0)
792     {
793       for (;;)
794         {
795           line = find_line (++current_line);
796           if (line == NULL)
797             {
798               if (p->repeat_forever)
799                 {
800                   if (!ignore)
801                     {
802                       dump_rest_of_file ();
803                       close_output_file ();
804                     }
805                   exit (EXIT_SUCCESS);
806                 }
807               else
808                 regexp_error (p, repetition, ignore);
809             }
810           line_len = line->len;
811           if (line->str[line_len - 1] == '\n')
812             line_len--;
813           ret = re_search (&p->re_compiled, line->str, line_len,
814                            0, line_len, NULL);
815           if (ret == -2)
816             {
817               error (0, 0, _("error in regular expression search"));
818               cleanup_fatal ();
819             }
820           if (ret == -1)
821             {
822               line = remove_line ();
823               if (!ignore)
824                 save_line_to_file (line);
825             }
826           else
827             break;
828         }
829     }
830   else
831     {
832       /* Buffer the lines. */
833       for (;;)
834         {
835           line = find_line (++current_line);
836           if (line == NULL)
837             {
838               if (p->repeat_forever)
839                 {
840                   if (!ignore)
841                     {
842                       dump_rest_of_file ();
843                       close_output_file ();
844                     }
845                   exit (EXIT_SUCCESS);
846                 }
847               else
848                 regexp_error (p, repetition, ignore);
849             }
850           line_len = line->len;
851           if (line->str[line_len - 1] == '\n')
852             line_len--;
853           ret = re_search (&p->re_compiled, line->str, line_len,
854                            0, line_len, NULL);
855           if (ret == -2)
856             {
857               error (0, 0, _("error in regular expression search"));
858               cleanup_fatal ();
859             }
860           if (ret >= 0)
861             break;
862         }
863     }
864
865   /* Account for any offset from this regexp. */
866   break_line = current_line + p->offset;
867
868   write_to_file (break_line, ignore, p->argnum);
869
870   if (!ignore)
871     close_output_file ();
872
873   if (p->offset > 0)
874     current_line = break_line;
875 }
876
877 /* Split the input file according to the control records we have built. */
878
879 static void
880 split_file (void)
881 {
882   size_t i;
883
884   for (i = 0; i < control_used; i++)
885     {
886       uintmax_t j;
887       if (controls[i].regexpr)
888         {
889           for (j = 0; (controls[i].repeat_forever
890                        || j <= controls[i].repeat); j++)
891             process_regexp (&controls[i], j);
892         }
893       else
894         {
895           for (j = 0; (controls[i].repeat_forever
896                        || j <= controls[i].repeat); j++)
897             process_line_count (&controls[i], j);
898         }
899     }
900
901   create_output_file ();
902   dump_rest_of_file ();
903   close_output_file ();
904 }
905
906 /* Return the name of output file number NUM. */
907
908 static char *
909 make_filename (unsigned int num)
910 {
911   strcpy (filename_space, prefix);
912   if (suffix)
913     sprintf (filename_space+strlen(prefix), suffix, num);
914   else
915     sprintf (filename_space+strlen(prefix), "%0*u", digits, num);
916   return filename_space;
917 }
918
919 /* Create the next output file. */
920
921 static void
922 create_output_file (void)
923 {
924   sigset_t oldset;
925   bool fopen_ok;
926   int fopen_errno;
927
928   output_filename = make_filename (files_created);
929
930   /* Create the output file in a critical section, to avoid races.  */
931   sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
932   output_stream = fopen (output_filename, "w");
933   fopen_ok = (output_stream != NULL);
934   fopen_errno = errno;
935   files_created += fopen_ok;
936   sigprocmask (SIG_SETMASK, &oldset, NULL);
937
938   if (! fopen_ok)
939     {
940       error (0, fopen_errno, "%s", output_filename);
941       cleanup_fatal ();
942     }
943   bytes_written = 0;
944 }
945
946 /* If requested, delete all the files we have created.  This function
947    must be called only from critical sections.  */
948
949 static void
950 delete_all_files (void)
951 {
952   unsigned int i;
953
954   if (! remove_files)
955     return;
956
957   for (i = 0; i < files_created; i++)
958     {
959       const char *name = make_filename (i);
960       if (unlink (name))
961         error (0, errno, "%s", name);
962     }
963
964   files_created = 0;
965 }
966
967 /* Close the current output file and print the count
968    of characters in this file. */
969
970 static void
971 close_output_file (void)
972 {
973   if (output_stream)
974     {
975       if (ferror (output_stream))
976         {
977           error (0, 0, _("write error for `%s'"), output_filename);
978           output_stream = NULL;
979           cleanup_fatal ();
980         }
981       if (fclose (output_stream) != 0)
982         {
983           error (0, errno, "%s", output_filename);
984           output_stream = NULL;
985           cleanup_fatal ();
986         }
987       if (bytes_written == 0 && elide_empty_files)
988         {
989           sigset_t oldset;
990           bool unlink_ok;
991           int unlink_errno;
992
993           /* Remove the output file in a critical section, to avoid races.  */
994           sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
995           unlink_ok = (unlink (output_filename) == 0);
996           unlink_errno = errno;
997           files_created -= unlink_ok;
998           sigprocmask (SIG_SETMASK, &oldset, NULL);
999
1000           if (! unlink_ok)
1001             error (0, unlink_errno, "%s", output_filename);
1002         }
1003       else
1004         {
1005           if (!suppress_count)
1006             {
1007               char buf[INT_BUFSIZE_BOUND (uintmax_t)];
1008               fprintf (stdout, "%s\n", umaxtostr (bytes_written, buf));
1009             }
1010         }
1011       output_stream = NULL;
1012     }
1013 }
1014
1015 /* Save line LINE to the output file and
1016    increment the character count for the current file. */
1017
1018 static void
1019 save_line_to_file (const struct cstring *line)
1020 {
1021   fwrite (line->str, sizeof (char), line->len, output_stream);
1022   bytes_written += line->len;
1023 }
1024
1025 /* Return a new, initialized control record. */
1026
1027 static struct control *
1028 new_control_record (void)
1029 {
1030   static size_t control_allocated = 0; /* Total space allocated. */
1031   struct control *p;
1032
1033   if (control_used == control_allocated)
1034     controls = x2nrealloc (controls, &control_allocated, sizeof *controls);
1035   p = &controls[control_used++];
1036   p->regexpr = NULL;
1037   p->repeat = 0;
1038   p->repeat_forever = false;
1039   p->lines_required = 0;
1040   p->offset = 0;
1041   return p;
1042 }
1043
1044 /* Check if there is a numeric offset after a regular expression.
1045    STR is the entire command line argument.
1046    P is the control record for this regular expression.
1047    NUM is the numeric part of STR. */
1048
1049 static void
1050 check_for_offset (struct control *p, const char *str, const char *num)
1051 {
1052   if (xstrtoimax (num, NULL, 10, &p->offset, "") != LONGINT_OK)
1053     error (EXIT_FAILURE, 0, _("%s: integer expected after delimiter"), str);
1054 }
1055
1056 /* Given that the first character of command line arg STR is '{',
1057    make sure that the rest of the string is a valid repeat count
1058    and store its value in P.
1059    ARGNUM is the ARGV index of STR. */
1060
1061 static void
1062 parse_repeat_count (int argnum, struct control *p, char *str)
1063 {
1064   uintmax_t val;
1065   char *end;
1066
1067   end = str + strlen (str) - 1;
1068   if (*end != '}')
1069     error (EXIT_FAILURE, 0, _("%s: `}' is required in repeat count"), str);
1070   *end = '\0';
1071
1072   if (str+1 == end-1 && *(str+1) == '*')
1073     p->repeat_forever = true;
1074   else
1075     {
1076       if (xstrtoumax (str + 1, NULL, 10, &val, "") != LONGINT_OK)
1077         {
1078           error (EXIT_FAILURE, 0,
1079                  _("%s}: integer required between `{' and `}'"),
1080                  global_argv[argnum]);
1081         }
1082       p->repeat = val;
1083     }
1084
1085   *end = '}';
1086 }
1087
1088 /* Extract the regular expression from STR and check for a numeric offset.
1089    STR should start with the regexp delimiter character.
1090    Return a new control record for the regular expression.
1091    ARGNUM is the ARGV index of STR.
1092    Unless IGNORE is true, mark these lines for output. */
1093
1094 static struct control *
1095 extract_regexp (int argnum, bool ignore, char *str)
1096 {
1097   size_t len;                   /* Number of bytes in this regexp. */
1098   char delim = *str;
1099   char *closing_delim;
1100   struct control *p;
1101   const char *err;
1102
1103   closing_delim = strrchr (str + 1, delim);
1104   if (closing_delim == NULL)
1105     error (EXIT_FAILURE, 0,
1106            _("%s: closing delimiter `%c' missing"), str, delim);
1107
1108   len = closing_delim - str - 1;
1109   p = new_control_record ();
1110   p->argnum = argnum;
1111   p->ignore = ignore;
1112
1113   p->regexpr = xmalloc (len + 1);
1114   strncpy (p->regexpr, str + 1, len);
1115   p->re_compiled.allocated = len * 2;
1116   p->re_compiled.buffer = xmalloc (p->re_compiled.allocated);
1117   p->re_compiled.fastmap = xmalloc (1 << CHAR_BIT);
1118   p->re_compiled.translate = 0;
1119   err = re_compile_pattern (p->regexpr, len, &p->re_compiled);
1120   if (err)
1121     {
1122       error (0, 0, _("%s: invalid regular expression: %s"), str, err);
1123       cleanup_fatal ();
1124     }
1125
1126   if (closing_delim[1])
1127     check_for_offset (p, str, closing_delim + 1);
1128
1129   return p;
1130 }
1131
1132 /* Extract the break patterns from args START through ARGC - 1 of ARGV.
1133    After each pattern, check if the next argument is a repeat count. */
1134
1135 static void
1136 parse_patterns (int argc, int start, char **argv)
1137 {
1138   int i;                        /* Index into ARGV. */
1139   struct control *p;            /* New control record created. */
1140   uintmax_t val;
1141   static uintmax_t last_val = 0;
1142
1143   for (i = start; i < argc; i++)
1144     {
1145       if (*argv[i] == '/' || *argv[i] == '%')
1146         {
1147           p = extract_regexp (i, *argv[i] == '%', argv[i]);
1148         }
1149       else
1150         {
1151           p = new_control_record ();
1152           p->argnum = i;
1153
1154           if (xstrtoumax (argv[i], NULL, 10, &val, "") != LONGINT_OK)
1155             error (EXIT_FAILURE, 0, _("%s: invalid pattern"), argv[i]);
1156           if (val == 0)
1157             error (EXIT_FAILURE, 0,
1158                    _("%s: line number must be greater than zero"),
1159                    argv[i]);
1160           if (val < last_val)
1161             {
1162               char buf[INT_BUFSIZE_BOUND (uintmax_t)];
1163               error (EXIT_FAILURE, 0,
1164                _("line number `%s' is smaller than preceding line number, %s"),
1165                      argv[i], umaxtostr (last_val, buf));
1166             }
1167
1168           if (val == last_val)
1169             error (0, 0,
1170            _("warning: line number `%s' is the same as preceding line number"),
1171                    argv[i]);
1172
1173           last_val = val;
1174
1175           p->lines_required = val;
1176         }
1177
1178       if (i + 1 < argc && *argv[i + 1] == '{')
1179         {
1180           /* We have a repeat count. */
1181           i++;
1182           parse_repeat_count (i, p, argv[i]);
1183         }
1184     }
1185 }
1186
1187 static unsigned int
1188 get_format_flags (char **format_ptr)
1189 {
1190   unsigned int count = 0;
1191
1192   for (; **format_ptr; (*format_ptr)++)
1193     {
1194       switch (**format_ptr)
1195         {
1196         case '-':
1197           break;
1198
1199         case '+':
1200         case ' ':
1201           count |= 1;
1202           break;
1203
1204         case '#':
1205           count |= 2;   /* Allow for 0x prefix preceding an `x' conversion.  */
1206           break;
1207
1208         default:
1209           return count;
1210         }
1211     }
1212   return count;
1213 }
1214
1215 static size_t
1216 get_format_width (char **format_ptr)
1217 {
1218   unsigned long int val = 0;
1219
1220   if (ISDIGIT (**format_ptr)
1221       && (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
1222           || SIZE_MAX < val))
1223     error (EXIT_FAILURE, 0, _("invalid format width"));
1224
1225   /* Allow for enough octal digits to represent the value of UINT_MAX,
1226      even if the field width is less than that.  */
1227   return MAX (val, (sizeof (unsigned int) * CHAR_BIT + 2) / 3);
1228 }
1229
1230 static size_t
1231 get_format_prec (char **format_ptr)
1232 {
1233   if (**format_ptr != '.')
1234     return 0;
1235   (*format_ptr)++;
1236
1237   if (! ISDIGIT (**format_ptr))
1238     return 0;
1239   else
1240     {
1241       unsigned long int val;
1242       if (xstrtoul (*format_ptr, format_ptr, 10, &val, NULL) != LONGINT_OK
1243           || SIZE_MAX < val)
1244         error (EXIT_FAILURE, 0, _("invalid format precision"));
1245       return val;
1246     }
1247 }
1248
1249 static void
1250 get_format_conv_type (char **format_ptr)
1251 {
1252   unsigned char ch = *(*format_ptr)++;
1253
1254   switch (ch)
1255     {
1256     case 'd':
1257     case 'i':
1258     case 'o':
1259     case 'u':
1260     case 'x':
1261     case 'X':
1262       break;
1263
1264     case 0:
1265       error (EXIT_FAILURE, 0, _("missing conversion specifier in suffix"));
1266       break;
1267
1268     default:
1269       if (ISPRINT (ch))
1270         error (EXIT_FAILURE, 0,
1271                _("invalid conversion specifier in suffix: %c"), ch);
1272       else
1273         error (EXIT_FAILURE, 0,
1274                _("invalid conversion specifier in suffix: \\%.3o"), ch);
1275     }
1276 }
1277
1278 static size_t
1279 max_out (char *format)
1280 {
1281   size_t out_count = 0;
1282   bool percent = false;
1283
1284   while (*format)
1285     {
1286       if (*format++ != '%')
1287         out_count++;
1288       else if (*format == '%')
1289         {
1290           format++;
1291           out_count++;
1292         }
1293       else
1294         {
1295           if (percent)
1296             error (EXIT_FAILURE, 0,
1297                    _("too many %% conversion specifications in suffix"));
1298           percent = true;
1299           out_count += get_format_flags (&format);
1300           {
1301             size_t width = get_format_width (&format);
1302             size_t prec = get_format_prec (&format);
1303
1304             out_count += MAX (width, prec);
1305           }
1306           get_format_conv_type (&format);
1307         }
1308     }
1309
1310   if (! percent)
1311     error (EXIT_FAILURE, 0,
1312            _("missing %% conversion specification in suffix"));
1313
1314   return out_count;
1315 }
1316
1317 int
1318 main (int argc, char **argv)
1319 {
1320   int optc;
1321   unsigned long int val;
1322
1323   initialize_main (&argc, &argv);
1324   program_name = argv[0];
1325   setlocale (LC_ALL, "");
1326   bindtextdomain (PACKAGE, LOCALEDIR);
1327   textdomain (PACKAGE);
1328
1329   atexit (close_stdout);
1330
1331   global_argv = argv;
1332   controls = NULL;
1333   control_used = 0;
1334   suppress_count = false;
1335   remove_files = true;
1336   prefix = DEFAULT_PREFIX;
1337
1338   while ((optc = getopt_long (argc, argv, "f:b:kn:sqz", longopts, NULL)) != -1)
1339     switch (optc)
1340       {
1341       case 'f':
1342         prefix = optarg;
1343         break;
1344
1345       case 'b':
1346         suffix = optarg;
1347         break;
1348
1349       case 'k':
1350         remove_files = false;
1351         break;
1352
1353       case 'n':
1354         if (xstrtoul (optarg, NULL, 10, &val, "") != LONGINT_OK
1355             || val > INT_MAX)
1356           error (EXIT_FAILURE, 0, _("%s: invalid number"), optarg);
1357         digits = val;
1358         break;
1359
1360       case 's':
1361       case 'q':
1362         suppress_count = true;
1363         break;
1364
1365       case 'z':
1366         elide_empty_files = true;
1367         break;
1368
1369       case_GETOPT_HELP_CHAR;
1370
1371       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1372
1373       default:
1374         usage (EXIT_FAILURE);
1375       }
1376
1377   if (argc - optind < 2)
1378     {
1379       if (argc <= optind)
1380         error (0, 0, _("missing operand"));
1381       else
1382         error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
1383       usage (EXIT_FAILURE);
1384     }
1385
1386   if (suffix)
1387     filename_space = xmalloc (strlen (prefix) + max_out (suffix) + 2);
1388   else
1389     filename_space = xmalloc (strlen (prefix) + digits + 2);
1390
1391   set_input_file (argv[optind++]);
1392
1393   parse_patterns (argc, optind, argv);
1394
1395   {
1396     int i;
1397     static int const sig[] = { SIGHUP, SIGINT, SIGQUIT, SIGTERM };
1398     enum { nsigs = sizeof sig / sizeof sig[0] };
1399
1400 #ifdef SA_NOCLDSTOP
1401     struct sigaction act;
1402
1403     sigemptyset (&caught_signals);
1404     for (i = 0; i < nsigs; i++)
1405       {
1406         sigaction (sig[i], NULL, &act);
1407         if (act.sa_handler != SIG_IGN)
1408           sigaddset (&caught_signals, sig[i]);
1409       }
1410
1411     act.sa_handler = interrupt_handler;
1412     act.sa_mask = caught_signals;
1413     act.sa_flags = 0;
1414
1415     for (i = 0; i < nsigs; i++)
1416       if (sigismember (&caught_signals, sig[i]))
1417         sigaction (sig[i], &act, NULL);
1418 #else
1419     for (i = 0; i < nsigs; i++)
1420       if (signal (sig[i], SIG_IGN) != SIG_IGN)
1421         signal (sig[i], interrupt_handler);
1422 #endif
1423   }
1424
1425   split_file ();
1426
1427   if (close (input_desc) < 0)
1428     {
1429       error (0, errno, _("read error"));
1430       cleanup_fatal ();
1431     }
1432
1433   exit (EXIT_SUCCESS);
1434 }
1435
1436 void
1437 usage (int status)
1438 {
1439   if (status != EXIT_SUCCESS)
1440     fprintf (stderr, _("Try `%s --help' for more information.\n"),
1441              program_name);
1442   else
1443     {
1444       printf (_("\
1445 Usage: %s [OPTION]... FILE PATTERN...\n\
1446 "),
1447               program_name);
1448       fputs (_("\
1449 Output pieces of FILE separated by PATTERN(s) to files `xx01', `xx02', ...,\n\
1450 and output byte counts of each piece to standard output.\n\
1451 \n\
1452 "), stdout);
1453       fputs (_("\
1454 Mandatory arguments to long options are mandatory for short options too.\n\
1455 "), stdout);
1456       fputs (_("\
1457   -b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d\n\
1458   -f, --prefix=PREFIX        use PREFIX instead of `xx'\n\
1459   -k, --keep-files           do not remove output files on errors\n\
1460 "), stdout);
1461       fputs (_("\
1462   -n, --digits=DIGITS        use specified number of digits instead of 2\n\
1463   -s, --quiet, --silent      do not print counts of output file sizes\n\
1464   -z, --elide-empty-files    remove empty output files\n\
1465 "), stdout);
1466       fputs (HELP_OPTION_DESCRIPTION, stdout);
1467       fputs (VERSION_OPTION_DESCRIPTION, stdout);
1468       fputs (_("\
1469 \n\
1470 Read standard input if FILE is -.  Each PATTERN may be:\n\
1471 "), stdout);
1472       fputs (_("\
1473 \n\
1474   INTEGER            copy up to but not including specified line number\n\
1475   /REGEXP/[OFFSET]   copy up to but not including a matching line\n\
1476   %REGEXP%[OFFSET]   skip to, but not including a matching line\n\
1477   {INTEGER}          repeat the previous pattern specified number of times\n\
1478   {*}                repeat the previous pattern as many times as possible\n\
1479 \n\
1480 A line OFFSET is a required `+' or `-' followed by a positive integer.\n\
1481 "), stdout);
1482       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1483     }
1484   exit (status);
1485 }