f8e7fe252c4574e822cd073688dadfc7fc780072
[platform/upstream/coreutils.git] / src / cut.c
1 /* cut - remove parts of lines of files
2    Copyright (C) 1984, 1997, 1998, 1999, 2000, 2001 by David M. Ihnat
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 David Ihnat.  */
19
20 /* POSIX changes, bug fixes, long-named options, and cleanup
21    by David MacKenzie <djm@gnu.ai.mit.edu>.
22
23    Rewrite cut_fields and cut_bytes -- Jim Meyering.  */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <assert.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31 #include "system.h"
32 #include "getstr.h"
33 #include "closeout.h"
34 #include "error.h"
35
36 /* The official name of this program (e.g., no `g' prefix).  */
37 #define PROGRAM_NAME "cut"
38
39 #define AUTHORS N_ ("David Ihnat, David MacKenzie, and Jim Meyering")
40
41 #define FATAL_ERROR(Message)                                            \
42   do                                                                    \
43     {                                                                   \
44       error (0, 0, (Message));                                          \
45       usage (2);                                                        \
46     }                                                                   \
47   while (0)
48
49 /* Append LOW, HIGH to the list RP of range pairs, allocating additional
50    space if necessary.  Update local variable N_RP.  When allocating,
51    update global variable N_RP_ALLOCATED.  */
52
53 #define ADD_RANGE_PAIR(rp, low, high)                                   \
54   do                                                                    \
55     {                                                                   \
56       if (n_rp >= n_rp_allocated)                                       \
57         {                                                               \
58           n_rp_allocated *= 2;                                          \
59           (rp) = (struct range_pair *) xrealloc ((char *) (rp),         \
60                                    n_rp_allocated * sizeof (*(rp)));    \
61         }                                                               \
62       rp[n_rp].lo = (low);                                              \
63       rp[n_rp].hi = (high);                                             \
64       ++n_rp;                                                           \
65     }                                                                   \
66   while (0)
67
68 struct range_pair
69   {
70     unsigned int lo;
71     unsigned int hi;
72   };
73
74 /* This buffer is used to support the semantics of the -s option
75    (or lack of same) when the specified field list includes (does
76    not include) the first field.  In both of those cases, the entire
77    first field must be read into this buffer to determine whether it
78    is followed by a delimiter or a newline before any of it may be
79    output.  Otherwise, cut_fields can do the job without using this
80    buffer.  */
81 static char *field_1_buffer;
82
83 /* The number of bytes allocated for FIELD_1_BUFFER.  */
84 static size_t field_1_bufsize;
85
86 /* The largest field or byte index used as an endpoint of a closed
87    or degenerate range specification;  this doesn't include the starting
88    index of right-open-ended ranges.  For example, with either range spec
89    `2-5,9-', `2-3,5,9-' this variable would be set to 5.  */
90 static unsigned int max_range_endpoint;
91
92 /* If nonzero, this is the index of the first field in a range that goes
93    to end of line. */
94 static unsigned int eol_range_start;
95
96 /* In byte mode, which bytes to output.
97    In field mode, which DELIM-separated fields to output.
98    Both bytes and fields are numbered starting with 1,
99    so the zeroth element of this array is unused.
100    A field or byte K has been selected if
101    (K <= MAX_RANGE_ENDPOINT and PRINTABLE_FIELD[K])
102     || (EOL_RANGE_START > 0 && K >= EOL_RANGE_START).  */
103 static int *printable_field;
104
105 enum operating_mode
106   {
107     undefined_mode,
108
109     /* Output characters that are in the given bytes. */
110     byte_mode,
111
112     /* Output the given delimeter-separated fields. */
113     field_mode
114   };
115
116 /* The name this program was run with. */
117 char *program_name;
118
119 static enum operating_mode operating_mode;
120
121 /* If nonzero do not output lines containing no delimeter characters.
122    Otherwise, all such lines are printed.  This option is valid only
123    with field mode.  */
124 static int suppress_non_delimited;
125
126 /* The delimeter character for field mode. */
127 static int delim;
128
129 /* The length of output_delimiter_string.  */
130 static size_t output_delimiter_length;
131
132 /* The output field separator string.  Defaults to the 1-character
133    string consisting of the input delimiter.  */
134 static char *output_delimiter_string;
135
136 /* Nonzero if we have ever read standard input. */
137 static int have_read_stdin;
138
139 /* For long options that have no equivalent short option, use a
140    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
141 enum
142 {
143   OUTPUT_DELIMITER_OPTION = CHAR_MAX + 1
144 };
145
146 static struct option const longopts[] =
147 {
148   {"bytes", required_argument, 0, 'b'},
149   {"characters", required_argument, 0, 'c'},
150   {"fields", required_argument, 0, 'f'},
151   {"delimiter", required_argument, 0, 'd'},
152   {"only-delimited", no_argument, 0, 's'},
153   {"output-delimiter", required_argument, 0, OUTPUT_DELIMITER_OPTION},
154   {GETOPT_HELP_OPTION_DECL},
155   {GETOPT_VERSION_OPTION_DECL},
156   {0, 0, 0, 0}
157 };
158
159 void
160 usage (int status)
161 {
162   if (status != 0)
163     fprintf (stderr, _("Try `%s --help' for more information.\n"),
164              program_name);
165   else
166     {
167       printf (_("\
168 Usage: %s [OPTION]... [FILE]...\n\
169 "),
170               program_name);
171       fputs (_("\
172 Print selected parts of lines from each FILE to standard output.\n\
173 \n\
174 Mandatory arguments to long options are mandatory for short options too.\n\
175   -b, --bytes=LIST        output only these bytes\n\
176   -c, --characters=LIST   output only these characters\n\
177   -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter\n\
178 "), stdout);
179       fputs (_("\
180   -f, --fields=LIST       output only these fields;  also print any line\n\
181                             that contains no delimiter character, unless\n\
182                             the -s option is specified\n\
183   -n                      (ignored)\n\
184 "), stdout);
185       fputs (_("\
186   -s, --only-delimited    do not print lines not containing delimiters\n\
187       --output-delimiter=STRING  use STRING as the output delimiter\n\
188                             the default is to use the input delimiter\n\
189       --help              display this help and exit\n\
190       --version           output version information and exit\n\
191 \n\
192 "), stdout);
193       fputs (_("\
194 Use one, and only one of -b, -c or -f.  Each LIST is made up of one\n\
195 range, or many ranges separated by commas.  Each range is one of:\n\
196 \n\
197   N     N'th byte, character or field, counted from 1\n\
198   N-    from N'th byte, character or field, to end of line\n\
199   N-M   from N'th to M'th (included) byte, character or field\n\
200   -M    from first to M'th (included) byte, character or field\n\
201 \n\
202 With no FILE, or when FILE is -, read standard input.\n\
203 "), stdout);
204       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
205     }
206   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
207 }
208
209 static int
210 print_kth (unsigned int k)
211 {
212   return ((0 < eol_range_start && eol_range_start <= k)
213           || (k <= max_range_endpoint && printable_field[k]));
214 }
215
216 /* Given the list of field or byte range specifications FIELDSTR, set
217    MAX_RANGE_ENDPOINT and allocate and initialize the PRINTABLE_FIELD
218    array.  If there is a right-open-ended range, set EOL_RANGE_START
219    to its starting index.  FIELDSTR should be composed of one or more
220    numbers or ranges of numbers, separated by blanks or commas.
221    Incomplete ranges may be given: `-m' means `1-m'; `n-' means `n'
222    through end of line.  Return nonzero if FIELDSTR contains at least
223    one field specification, zero otherwise.  */
224
225 /* FIXME-someday:  What if the user wants to cut out the 1,000,000-th field
226    of some huge input file?  This function shouldn't have to alloate a table
227    of a million ints just so we can test every field < 10^6 with an array
228    dereference.  Instead, consider using a dynamic hash table.  It would be
229    simpler and nearly as good a solution to use a 32K x 4-byte table with
230    one bit per field index instead of a whole `int' per index.  */
231
232 static int
233 set_fields (const char *fieldstr)
234 {
235   unsigned int initial = 1;     /* Value of first number in a range.  */
236   unsigned int value = 0;       /* If nonzero, a number being accumulated.  */
237   int dash_found = 0;           /* Nonzero if a '-' is found in this field.  */
238   int field_found = 0;          /* Non-zero if at least one field spec
239                                    has been processed.  */
240
241   struct range_pair *rp;
242   unsigned int n_rp;
243   unsigned int n_rp_allocated;
244   unsigned int i;
245
246   n_rp = 0;
247   n_rp_allocated = 16;
248   rp = (struct range_pair *) xmalloc (n_rp_allocated * sizeof (*rp));
249
250   /* Collect and store in RP the range end points.
251      It also sets EOL_RANGE_START if appropriate.  */
252
253   for (;;)
254     {
255       if (*fieldstr == '-')
256         {
257           /* Starting a range. */
258           if (dash_found)
259             FATAL_ERROR (_("invalid byte or field list"));
260           dash_found++;
261           fieldstr++;
262
263           if (value)
264             {
265               initial = value;
266               value = 0;
267             }
268           else
269             initial = 1;
270         }
271       else if (*fieldstr == ',' || ISBLANK (*fieldstr) || *fieldstr == '\0')
272         {
273           /* Ending the string, or this field/byte sublist. */
274           if (dash_found)
275             {
276               dash_found = 0;
277
278               /* A range.  Possibilites: -n, m-n, n-.
279                  In any case, `initial' contains the start of the range. */
280               if (value == 0)
281                 {
282                   /* `n-'.  From `initial' to end of line. */
283                   eol_range_start = initial;
284                   field_found = 1;
285                 }
286               else
287                 {
288                   /* `m-n' or `-n' (1-n). */
289                   if (value < initial)
290                     FATAL_ERROR (_("invalid byte or field list"));
291
292                   /* Is there already a range going to end of line? */
293                   if (eol_range_start != 0)
294                     {
295                       /* Yes.  Is the new sequence already contained
296                          in the old one?  If so, no processing is
297                          necessary. */
298                       if (initial < eol_range_start)
299                         {
300                           /* No, the new sequence starts before the
301                              old.  Does the old range going to end of line
302                              extend into the new range?  */
303                           if (value + 1 >= eol_range_start)
304                             {
305                               /* Yes.  Simply move the end of line marker. */
306                               eol_range_start = initial;
307                             }
308                           else
309                             {
310                               /* No.  A simple range, before and disjoint from
311                                  the range going to end of line.  Fill it. */
312                               ADD_RANGE_PAIR (rp, initial, value);
313                             }
314
315                           /* In any case, some fields were selected. */
316                           field_found = 1;
317                         }
318                     }
319                   else
320                     {
321                       /* There is no range going to end of line. */
322                       ADD_RANGE_PAIR (rp, initial, value);
323                       field_found = 1;
324                     }
325                   value = 0;
326                 }
327             }
328           else if (value != 0)
329             {
330               /* A simple field number, not a range. */
331               ADD_RANGE_PAIR (rp, value, value);
332               value = 0;
333               field_found = 1;
334             }
335
336           if (*fieldstr == '\0')
337             {
338               break;
339             }
340
341           fieldstr++;
342         }
343       else if (ISDIGIT (*fieldstr))
344         {
345           /* FIXME: detect overflow?  */
346           value = 10 * value + *fieldstr - '0';
347           fieldstr++;
348         }
349       else
350         FATAL_ERROR (_("invalid byte or field list"));
351     }
352
353   max_range_endpoint = 0;
354   for (i = 0; i < n_rp; i++)
355     {
356       if (rp[i].hi > max_range_endpoint)
357         max_range_endpoint = rp[i].hi;
358     }
359
360   /* Allocate an array large enough so that it may be indexed by
361      the field numbers corresponding to all finite ranges
362      (i.e. `2-6' or `-4', but not `5-') in FIELDSTR.  */
363
364   printable_field = (int *) xmalloc ((max_range_endpoint + 1) * sizeof (int));
365   memset (printable_field, 0, (max_range_endpoint + 1) * sizeof (int));
366
367   /* Set the array entries corresponding to integers in the ranges of RP.  */
368   for (i = 0; i < n_rp; i++)
369     {
370       unsigned int j;
371       for (j = rp[i].lo; j <= rp[i].hi; j++)
372         {
373           printable_field[j] = 1;
374         }
375     }
376
377   free (rp);
378
379   return field_found;
380 }
381
382 /* Read from stream STREAM, printing to standard output any selected bytes.  */
383
384 static void
385 cut_bytes (FILE *stream)
386 {
387   unsigned int byte_idx;        /* Number of chars in the line so far. */
388
389   byte_idx = 0;
390   while (1)
391     {
392       register int c;           /* Each character from the file. */
393
394       c = getc (stream);
395
396       if (c == '\n')
397         {
398           putchar ('\n');
399           byte_idx = 0;
400         }
401       else if (c == EOF)
402         {
403           if (byte_idx > 0)
404             putchar ('\n');
405           break;
406         }
407       else
408         {
409           ++byte_idx;
410           if (print_kth (byte_idx))
411             {
412               putchar (c);
413             }
414         }
415     }
416 }
417
418 /* Read from stream STREAM, printing to standard output any selected fields.  */
419
420 static void
421 cut_fields (FILE *stream)
422 {
423   int c;
424   unsigned int field_idx;
425   int found_any_selected_field;
426   int buffer_first_field;
427   int empty_input;
428
429   found_any_selected_field = 0;
430   field_idx = 1;
431
432   c = getc (stream);
433   empty_input = (c == EOF);
434   if (c != EOF)
435     ungetc (c, stream);
436
437   /* To support the semantics of the -s flag, we may have to buffer
438      all of the first field to determine whether it is `delimited.'
439      But that is unnecessary if all non-delimited lines must be printed
440      and the first field has been selected, or if non-delimited lines
441      must be suppressed and the first field has *not* been selected.
442      That is because a non-delimited line has exactly one field.  */
443   buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
444
445   while (1)
446     {
447       if (field_idx == 1 && buffer_first_field)
448         {
449           int len;
450
451           len = getstr (&field_1_buffer, &field_1_bufsize, stream,
452                         delim, '\n', 0);
453           if (len < 0)
454             {
455               if (ferror (stream) || feof (stream))
456                 break;
457               xalloc_die ();
458             }
459
460           assert (len != 0);
461
462           /* If the first field extends to the end of line (it is not
463              delimited) and we are printing all non-delimited lines,
464              print this one.  */
465           if ((unsigned char) field_1_buffer[len - 1] != delim)
466             {
467               if (suppress_non_delimited)
468                 {
469                   /* Empty.  */
470                 }
471               else
472                 {
473                   fwrite (field_1_buffer, sizeof (char), len, stdout);
474                   /* Make sure the output line is newline terminated.  */
475                   if (field_1_buffer[len - 1] != '\n')
476                     putchar ('\n');
477                 }
478               continue;
479             }
480           if (print_kth (1))
481             {
482               /* Print the field, but not the trailing delimiter.  */
483               fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
484               found_any_selected_field = 1;
485             }
486           ++field_idx;
487         }
488
489       if (c != EOF)
490         {
491           if (print_kth (field_idx))
492             {
493               if (found_any_selected_field)
494                 {
495                   fwrite (output_delimiter_string, sizeof (char),
496                           output_delimiter_length, stdout);
497                 }
498               found_any_selected_field = 1;
499
500               while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
501                 {
502                   putchar (c);
503                 }
504             }
505           else
506             {
507               while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
508                 {
509                   /* Empty.  */
510                 }
511             }
512         }
513
514       if (c == '\n')
515         {
516           c = getc (stream);
517           if (c != EOF)
518             {
519               ungetc (c, stream);
520               c = '\n';
521             }
522         }
523
524       if (c == delim)
525         ++field_idx;
526       else if (c == '\n' || c == EOF)
527         {
528           if (found_any_selected_field
529               || (!empty_input && !(suppress_non_delimited && field_idx == 1)))
530             putchar ('\n');
531           if (c == EOF)
532             break;
533           field_idx = 1;
534           found_any_selected_field = 0;
535         }
536     }
537 }
538
539 static void
540 cut_stream (FILE *stream)
541 {
542   if (operating_mode == byte_mode)
543     cut_bytes (stream);
544   else
545     cut_fields (stream);
546 }
547
548 /* Process file FILE to standard output.
549    Return 0 if successful, 1 if not. */
550
551 static int
552 cut_file (char *file)
553 {
554   FILE *stream;
555
556   if (STREQ (file, "-"))
557     {
558       have_read_stdin = 1;
559       stream = stdin;
560     }
561   else
562     {
563       stream = fopen (file, "r");
564       if (stream == NULL)
565         {
566           error (0, errno, "%s", file);
567           return 1;
568         }
569     }
570
571   cut_stream (stream);
572
573   if (ferror (stream))
574     {
575       error (0, errno, "%s", file);
576       return 1;
577     }
578   if (STREQ (file, "-"))
579     clearerr (stream);          /* Also clear EOF. */
580   else if (fclose (stream) == EOF)
581     {
582       error (0, errno, "%s", file);
583       return 1;
584     }
585   return 0;
586 }
587
588 int
589 main (int argc, char **argv)
590 {
591   int optc, exit_status = 0;
592   int delim_specified = 0;
593
594   program_name = argv[0];
595   setlocale (LC_ALL, "");
596   bindtextdomain (PACKAGE, LOCALEDIR);
597   textdomain (PACKAGE);
598
599   atexit (close_stdout);
600
601   operating_mode = undefined_mode;
602
603   /* By default, all non-delimited lines are printed.  */
604   suppress_non_delimited = 0;
605
606   delim = '\0';
607   have_read_stdin = 0;
608
609   while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
610     {
611       switch (optc)
612         {
613         case 0:
614           break;
615
616         case 'b':
617         case 'c':
618           /* Build the byte list. */
619           if (operating_mode != undefined_mode)
620             FATAL_ERROR (_("only one type of list may be specified"));
621           operating_mode = byte_mode;
622           if (set_fields (optarg) == 0)
623             FATAL_ERROR (_("missing list of positions"));
624           break;
625
626         case 'f':
627           /* Build the field list. */
628           if (operating_mode != undefined_mode)
629             FATAL_ERROR (_("only one type of list may be specified"));
630           operating_mode = field_mode;
631           if (set_fields (optarg) == 0)
632             FATAL_ERROR (_("missing list of fields"));
633           break;
634
635         case 'd':
636           /* New delimiter. */
637           /* Interpret -d '' to mean `use the NUL byte as the delimiter.'  */
638           if (optarg[0] != '\0' && optarg[1] != '\0')
639             FATAL_ERROR (_("the delimiter must be a single character"));
640           delim = (unsigned char) optarg[0];
641           delim_specified = 1;
642           break;
643
644         case OUTPUT_DELIMITER_OPTION:
645           /* Interpret --output-delimiter='' to mean
646              `use the NUL byte as the delimiter.'  */
647           output_delimiter_length = (optarg[0] == '\0'
648                                      ? 1 : strlen (optarg));
649           output_delimiter_string = xstrdup (optarg);
650           break;
651
652         case 'n':
653           break;
654
655         case 's':
656           suppress_non_delimited = 1;
657           break;
658
659         case_GETOPT_HELP_CHAR;
660
661         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
662
663         default:
664           usage (2);
665         }
666     }
667
668   if (operating_mode == undefined_mode)
669     FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
670
671   if (delim != '\0' && operating_mode != field_mode)
672     FATAL_ERROR (_("a delimiter may be specified only when operating on fields"));
673
674   if (suppress_non_delimited && operating_mode != field_mode)
675     FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\
676 \tonly when operating on fields"));
677
678   if (!delim_specified)
679     delim = '\t';
680
681   if (output_delimiter_string == NULL)
682     {
683       static char dummy[2];
684       dummy[0] = delim;
685       dummy[1] = '\0';
686       output_delimiter_string = dummy;
687       output_delimiter_length = 1;
688     }
689
690   if (optind == argc)
691     exit_status |= cut_file ("-");
692   else
693     for (; optind < argc; optind++)
694       exit_status |= cut_file (argv[optind]);
695
696   if (have_read_stdin && fclose (stdin) == EOF)
697     {
698       error (0, errno, "-");
699       exit_status = 1;
700     }
701
702   exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
703 }