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