meillo@14: /* cut - remove parts of lines of files
meillo@14: Copyright (C) 1997-2015 Free Software Foundation, Inc.
meillo@14: Copyright (C) 1984 David M. Ihnat
meillo@14:
meillo@14: This program is free software: you can redistribute it and/or modify
meillo@14: it under the terms of the GNU General Public License as published by
meillo@14: the Free Software Foundation, either version 3 of the License, or
meillo@14: (at your option) any later version.
meillo@14:
meillo@14: This program is distributed in the hope that it will be useful,
meillo@14: but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@14: GNU General Public License for more details.
meillo@14:
meillo@14: You should have received a copy of the GNU General Public License
meillo@14: along with this program. If not, see . */
meillo@14:
meillo@14: /* Written by David Ihnat. */
meillo@14:
meillo@14: /* POSIX changes, bug fixes, long-named options, and cleanup
meillo@14: by David MacKenzie .
meillo@14:
meillo@14: Rewrite cut_fields and cut_bytes -- Jim Meyering. */
meillo@14:
meillo@14: #include
meillo@14:
meillo@14: #include
meillo@14: #include
meillo@14: #include
meillo@14: #include
meillo@14: #include "system.h"
meillo@14:
meillo@14: #include "error.h"
meillo@14: #include "fadvise.h"
meillo@14: #include "getndelim2.h"
meillo@14: #include "hash.h"
meillo@14: #include "quote.h"
meillo@14: #include "xstrndup.h"
meillo@14:
meillo@14: /* The official name of this program (e.g., no 'g' prefix). */
meillo@14: #define PROGRAM_NAME "cut"
meillo@14:
meillo@14: #define AUTHORS \
meillo@14: proper_name ("David M. Ihnat"), \
meillo@14: proper_name ("David MacKenzie"), \
meillo@14: proper_name ("Jim Meyering")
meillo@14:
meillo@14: #define FATAL_ERROR(Message) \
meillo@14: do \
meillo@14: { \
meillo@14: error (0, 0, (Message)); \
meillo@14: usage (EXIT_FAILURE); \
meillo@14: } \
meillo@14: while (0)
meillo@14:
meillo@14:
meillo@14: struct range_pair
meillo@14: {
meillo@14: size_t lo;
meillo@14: size_t hi;
meillo@14: };
meillo@14:
meillo@14: /* Array of `struct range_pair' holding all the finite ranges. */
meillo@14: static struct range_pair *rp;
meillo@14:
meillo@14: /* Pointer inside RP. When checking if a byte or field is selected
meillo@14: by a finite range, we check if it is between CURRENT_RP.LO
meillo@14: and CURRENT_RP.HI. If the byte or field index is greater than
meillo@14: CURRENT_RP.HI then we make CURRENT_RP to point to the next range pair. */
meillo@14: static struct range_pair *current_rp;
meillo@14:
meillo@14: /* Number of finite ranges specified by the user. */
meillo@14: static size_t n_rp;
meillo@14:
meillo@14: /* Number of `struct range_pair's allocated. */
meillo@14: static size_t n_rp_allocated;
meillo@14:
meillo@14:
meillo@14: /* Append LOW, HIGH to the list RP of range pairs, allocating additional
meillo@14: space if necessary. Update global variable N_RP. When allocating,
meillo@14: update global variable N_RP_ALLOCATED. */
meillo@14:
meillo@14: static void
meillo@14: add_range_pair (size_t lo, size_t hi)
meillo@14: {
meillo@14: if (n_rp == n_rp_allocated)
meillo@14: rp = X2NREALLOC (rp, &n_rp_allocated);
meillo@14: rp[n_rp].lo = lo;
meillo@14: rp[n_rp].hi = hi;
meillo@14: ++n_rp;
meillo@14: }
meillo@14:
meillo@14: /* This buffer is used to support the semantics of the -s option
meillo@14: (or lack of same) when the specified field list includes (does
meillo@14: not include) the first field. In both of those cases, the entire
meillo@14: first field must be read into this buffer to determine whether it
meillo@14: is followed by a delimiter or a newline before any of it may be
meillo@14: output. Otherwise, cut_fields can do the job without using this
meillo@14: buffer. */
meillo@14: static char *field_1_buffer;
meillo@14:
meillo@14: /* The number of bytes allocated for FIELD_1_BUFFER. */
meillo@14: static size_t field_1_bufsize;
meillo@14:
meillo@14: enum operating_mode
meillo@14: {
meillo@14: undefined_mode,
meillo@14:
meillo@14: /* Output characters that are in the given bytes. */
meillo@14: byte_mode,
meillo@14:
meillo@14: /* Output the given delimiter-separated fields. */
meillo@14: field_mode
meillo@14: };
meillo@14:
meillo@14: static enum operating_mode operating_mode;
meillo@14:
meillo@14: /* If true do not output lines containing no delimiter characters.
meillo@14: Otherwise, all such lines are printed. This option is valid only
meillo@14: with field mode. */
meillo@14: static bool suppress_non_delimited;
meillo@14:
meillo@14: /* If true, print all bytes, characters, or fields _except_
meillo@14: those that were specified. */
meillo@14: static bool complement;
meillo@14:
meillo@14: /* The delimiter character for field mode. */
meillo@14: static unsigned char delim;
meillo@14:
meillo@14: /* True if the --output-delimiter=STRING option was specified. */
meillo@14: static bool output_delimiter_specified;
meillo@14:
meillo@14: /* The length of output_delimiter_string. */
meillo@14: static size_t output_delimiter_length;
meillo@14:
meillo@14: /* The output field separator string. Defaults to the 1-character
meillo@14: string consisting of the input delimiter. */
meillo@14: static char *output_delimiter_string;
meillo@14:
meillo@14: /* True if we have ever read standard input. */
meillo@14: static bool have_read_stdin;
meillo@14:
meillo@14: /* For long options that have no equivalent short option, use a
meillo@14: non-character as a pseudo short option, starting with CHAR_MAX + 1. */
meillo@14: enum
meillo@14: {
meillo@14: OUTPUT_DELIMITER_OPTION = CHAR_MAX + 1,
meillo@14: COMPLEMENT_OPTION
meillo@14: };
meillo@14:
meillo@14: static struct option const longopts[] =
meillo@14: {
meillo@14: {"bytes", required_argument, NULL, 'b'},
meillo@14: {"characters", required_argument, NULL, 'c'},
meillo@14: {"fields", required_argument, NULL, 'f'},
meillo@14: {"delimiter", required_argument, NULL, 'd'},
meillo@14: {"only-delimited", no_argument, NULL, 's'},
meillo@14: {"output-delimiter", required_argument, NULL, OUTPUT_DELIMITER_OPTION},
meillo@14: {"complement", no_argument, NULL, COMPLEMENT_OPTION},
meillo@14: {GETOPT_HELP_OPTION_DECL},
meillo@14: {GETOPT_VERSION_OPTION_DECL},
meillo@14: {NULL, 0, NULL, 0}
meillo@14: };
meillo@14:
meillo@14: void
meillo@14: usage (int status)
meillo@14: {
meillo@14: if (status != EXIT_SUCCESS)
meillo@14: emit_try_help ();
meillo@14: else
meillo@14: {
meillo@14: printf (_("\
meillo@14: Usage: %s OPTION... [FILE]...\n\
meillo@14: "),
meillo@14: program_name);
meillo@14: fputs (_("\
meillo@14: Print selected parts of lines from each FILE to standard output.\n\
meillo@14: "), stdout);
meillo@14:
meillo@14: emit_stdin_note ();
meillo@14: emit_mandatory_arg_note ();
meillo@14:
meillo@14: fputs (_("\
meillo@14: -b, --bytes=LIST select only these bytes\n\
meillo@14: -c, --characters=LIST select only these characters\n\
meillo@14: -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter\n\
meillo@14: "), stdout);
meillo@14: fputs (_("\
meillo@14: -f, --fields=LIST select only these fields; also print any line\n\
meillo@14: that contains no delimiter character, unless\n\
meillo@14: the -s option is specified\n\
meillo@14: -n (ignored)\n\
meillo@14: "), stdout);
meillo@14: fputs (_("\
meillo@14: --complement complement the set of selected bytes, characters\n\
meillo@14: or fields\n\
meillo@14: "), stdout);
meillo@14: fputs (_("\
meillo@14: -s, --only-delimited do not print lines not containing delimiters\n\
meillo@14: --output-delimiter=STRING use STRING as the output delimiter\n\
meillo@14: the default is to use the input delimiter\n\
meillo@14: "), stdout);
meillo@14: fputs (HELP_OPTION_DESCRIPTION, stdout);
meillo@14: fputs (VERSION_OPTION_DESCRIPTION, stdout);
meillo@14: fputs (_("\
meillo@14: \n\
meillo@14: Use one, and only one of -b, -c or -f. Each LIST is made up of one\n\
meillo@14: range, or many ranges separated by commas. Selected input is written\n\
meillo@14: in the same order that it is read, and is written exactly once.\n\
meillo@14: "), stdout);
meillo@14: fputs (_("\
meillo@14: Each range is one of:\n\
meillo@14: \n\
meillo@14: N N'th byte, character or field, counted from 1\n\
meillo@14: N- from N'th byte, character or field, to end of line\n\
meillo@14: N-M from N'th to M'th (included) byte, character or field\n\
meillo@14: -M from first to M'th (included) byte, character or field\n\
meillo@14: "), stdout);
meillo@14: emit_ancillary_info (PROGRAM_NAME);
meillo@14: }
meillo@14: exit (status);
meillo@14: }
meillo@14:
meillo@14: /* Comparison function for qsort to order the list of
meillo@14: struct range_pairs. */
meillo@14: static int
meillo@14: compare_ranges (const void *a, const void *b)
meillo@14: {
meillo@14: int a_start = ((const struct range_pair *) a)->lo;
meillo@14: int b_start = ((const struct range_pair *) b)->lo;
meillo@14: return a_start < b_start ? -1 : a_start > b_start;
meillo@14: }
meillo@14:
meillo@14: /* Reallocate Range Pair entries, with corresponding
meillo@14: entries outside the range of each specified entry. */
meillo@14:
meillo@14: static void
meillo@14: complement_rp (void)
meillo@14: {
meillo@14: if (complement)
meillo@14: {
meillo@14: struct range_pair *c = rp;
meillo@14: size_t n = n_rp;
meillo@14: size_t i;
meillo@14:
meillo@14: rp = NULL;
meillo@14: n_rp = 0;
meillo@14: n_rp_allocated = 0;
meillo@14:
meillo@14: if (c[0].lo > 1)
meillo@14: add_range_pair (1, c[0].lo - 1);
meillo@14:
meillo@14: for (i = 1; i < n; ++i)
meillo@14: {
meillo@14: if (c[i-1].hi + 1 == c[i].lo)
meillo@14: continue;
meillo@14:
meillo@14: add_range_pair (c[i-1].hi + 1, c[i].lo - 1);
meillo@14: }
meillo@14:
meillo@14: if (c[n-1].hi < SIZE_MAX)
meillo@14: add_range_pair (c[n-1].hi + 1, SIZE_MAX);
meillo@14:
meillo@14: free (c);
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: /* Given the list of field or byte range specifications FIELDSTR,
meillo@14: allocate and initialize the RP array. FIELDSTR should
meillo@14: be composed of one or more numbers or ranges of numbers, separated
meillo@14: by blanks or commas. Incomplete ranges may be given: '-m' means '1-m';
meillo@14: 'n-' means 'n' through end of line.
meillo@14: Return true if FIELDSTR contains at least one field specification,
meillo@14: false otherwise. */
meillo@14:
meillo@14: static bool
meillo@14: set_fields (const char *fieldstr)
meillo@14: {
meillo@14: size_t initial = 1; /* Value of first number in a range. */
meillo@14: size_t value = 0; /* If nonzero, a number being accumulated. */
meillo@14: bool lhs_specified = false;
meillo@14: bool rhs_specified = false;
meillo@14: bool dash_found = false; /* True if a '-' is found in this field. */
meillo@14: bool field_found = false; /* True if at least one field spec
meillo@14: has been processed. */
meillo@14:
meillo@14: size_t i;
meillo@14: bool in_digits = false;
meillo@14:
meillo@14: /* Collect and store in RP the range end points. */
meillo@14:
meillo@14: while (true)
meillo@14: {
meillo@14: if (*fieldstr == '-')
meillo@14: {
meillo@14: in_digits = false;
meillo@14: /* Starting a range. */
meillo@14: if (dash_found)
meillo@14: FATAL_ERROR (_("invalid byte, character or field list"));
meillo@14: dash_found = true;
meillo@14: fieldstr++;
meillo@14:
meillo@14: if (lhs_specified && !value)
meillo@14: FATAL_ERROR (_("fields and positions are numbered from 1"));
meillo@14:
meillo@14: initial = (lhs_specified ? value : 1);
meillo@14: value = 0;
meillo@14: }
meillo@14: else if (*fieldstr == ','
meillo@14: || isblank (to_uchar (*fieldstr)) || *fieldstr == '\0')
meillo@14: {
meillo@14: in_digits = false;
meillo@14: /* Ending the string, or this field/byte sublist. */
meillo@14: if (dash_found)
meillo@14: {
meillo@14: dash_found = false;
meillo@14:
meillo@14: if (!lhs_specified && !rhs_specified)
meillo@14: FATAL_ERROR (_("invalid range with no endpoint: -"));
meillo@14:
meillo@14: /* A range. Possibilities: -n, m-n, n-.
meillo@14: In any case, 'initial' contains the start of the range. */
meillo@14: if (!rhs_specified)
meillo@14: {
meillo@14: /* 'n-'. From 'initial' to end of line. */
meillo@14: add_range_pair (initial, SIZE_MAX);
meillo@14: field_found = true;
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: /* 'm-n' or '-n' (1-n). */
meillo@14: if (value < initial)
meillo@14: FATAL_ERROR (_("invalid decreasing range"));
meillo@14:
meillo@14: add_range_pair (initial, value);
meillo@14: field_found = true;
meillo@14: }
meillo@14: value = 0;
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: /* A simple field number, not a range. */
meillo@14: if (value == 0)
meillo@14: FATAL_ERROR (_("fields and positions are numbered from 1"));
meillo@14: add_range_pair (value, value);
meillo@14: value = 0;
meillo@14: field_found = true;
meillo@14: }
meillo@14:
meillo@14: if (*fieldstr == '\0')
meillo@14: break;
meillo@14:
meillo@14: fieldstr++;
meillo@14: lhs_specified = false;
meillo@14: rhs_specified = false;
meillo@14: }
meillo@14: else if (ISDIGIT (*fieldstr))
meillo@14: {
meillo@14: /* Record beginning of digit string, in case we have to
meillo@14: complain about it. */
meillo@14: static char const *num_start;
meillo@14: if (!in_digits || !num_start)
meillo@14: num_start = fieldstr;
meillo@14: in_digits = true;
meillo@14:
meillo@14: if (dash_found)
meillo@14: rhs_specified = 1;
meillo@14: else
meillo@14: lhs_specified = 1;
meillo@14:
meillo@14: /* Detect overflow. */
meillo@14: if (!DECIMAL_DIGIT_ACCUMULATE (value, *fieldstr - '0', size_t)
meillo@14: || value == SIZE_MAX)
meillo@14: {
meillo@14: /* In case the user specified -c$(echo 2^64|bc),22,
meillo@14: complain only about the first number. */
meillo@14: /* Determine the length of the offending number. */
meillo@14: size_t len = strspn (num_start, "0123456789");
meillo@14: char *bad_num = xstrndup (num_start, len);
meillo@14: if (operating_mode == byte_mode)
meillo@14: error (0, 0,
meillo@14: _("byte offset %s is too large"), quote (bad_num));
meillo@14: else
meillo@14: error (0, 0,
meillo@14: _("field number %s is too large"), quote (bad_num));
meillo@14: free (bad_num);
meillo@14: exit (EXIT_FAILURE);
meillo@14: }
meillo@14:
meillo@14: fieldstr++;
meillo@14: }
meillo@14: else
meillo@14: FATAL_ERROR (_("invalid byte, character or field list"));
meillo@14: }
meillo@14:
meillo@14: qsort (rp, n_rp, sizeof (rp[0]), compare_ranges);
meillo@14:
meillo@14: /* Merge range pairs (e.g. `2-5,3-4' becomes `2-5'). */
meillo@14: for (i = 0; i < n_rp; ++i)
meillo@14: {
meillo@14: for (size_t j = i + 1; j < n_rp; ++j)
meillo@14: {
meillo@14: if (rp[j].lo <= rp[i].hi)
meillo@14: {
meillo@14: rp[i].hi = MAX (rp[j].hi, rp[i].hi);
meillo@14: memmove (rp + j, rp + j + 1, (n_rp - j - 1) * sizeof *rp);
meillo@14: n_rp--;
meillo@14: j--;
meillo@14: }
meillo@14: else
meillo@14: break;
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: complement_rp ();
meillo@14:
meillo@14: /* After merging, reallocate RP so we release memory to the system.
meillo@14: Also add a sentinel at the end of RP, to avoid out of bounds access
meillo@14: and for performance reasons. */
meillo@14: ++n_rp;
meillo@14: rp = xrealloc (rp, n_rp * sizeof (struct range_pair));
meillo@14: rp[n_rp - 1].lo = rp[n_rp - 1].hi = SIZE_MAX;
meillo@14:
meillo@14: return field_found;
meillo@14: }
meillo@14:
meillo@14: /* Increment *ITEM_IDX (i.e., a field or byte index),
meillo@14: and if required CURRENT_RP. */
meillo@14:
meillo@14: static inline void
meillo@14: next_item (size_t *item_idx)
meillo@14: {
meillo@14: (*item_idx)++;
meillo@14: if ((*item_idx) > current_rp->hi)
meillo@14: current_rp++;
meillo@14: }
meillo@14:
meillo@14: /* Return nonzero if the K'th field or byte is printable. */
meillo@14:
meillo@14: static inline bool
meillo@14: print_kth (size_t k)
meillo@14: {
meillo@14: return current_rp->lo <= k;
meillo@14: }
meillo@14:
meillo@14: /* Return nonzero if K'th byte is the beginning of a range. */
meillo@14:
meillo@14: static inline bool
meillo@14: is_range_start_index (size_t k)
meillo@14: {
meillo@14: return k == current_rp->lo;
meillo@14: }
meillo@14:
meillo@14: /* Read from stream STREAM, printing to standard output any selected bytes. */
meillo@14:
meillo@14: static void
meillo@14: cut_bytes (FILE *stream)
meillo@14: {
meillo@14: size_t byte_idx; /* Number of bytes in the line so far. */
meillo@14: /* Whether to begin printing delimiters between ranges for the current line.
meillo@14: Set after we've begun printing data corresponding to the first range. */
meillo@14: bool print_delimiter;
meillo@14:
meillo@14: byte_idx = 0;
meillo@14: print_delimiter = false;
meillo@14: current_rp = rp;
meillo@14: while (true)
meillo@14: {
meillo@14: int c; /* Each character from the file. */
meillo@14:
meillo@14: c = getc (stream);
meillo@14:
meillo@14: if (c == '\n')
meillo@14: {
meillo@14: putchar ('\n');
meillo@14: byte_idx = 0;
meillo@14: print_delimiter = false;
meillo@14: current_rp = rp;
meillo@14: }
meillo@14: else if (c == EOF)
meillo@14: {
meillo@14: if (byte_idx > 0)
meillo@14: putchar ('\n');
meillo@14: break;
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: next_item (&byte_idx);
meillo@14: if (print_kth (byte_idx))
meillo@14: {
meillo@14: if (output_delimiter_specified)
meillo@14: {
meillo@14: if (print_delimiter && is_range_start_index (byte_idx))
meillo@14: {
meillo@14: fwrite (output_delimiter_string, sizeof (char),
meillo@14: output_delimiter_length, stdout);
meillo@14: }
meillo@14: print_delimiter = true;
meillo@14: }
meillo@14:
meillo@14: putchar (c);
meillo@14: }
meillo@14: }
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: /* Read from stream STREAM, printing to standard output any selected fields. */
meillo@14:
meillo@14: static void
meillo@14: cut_fields (FILE *stream)
meillo@14: {
meillo@14: int c;
meillo@14: size_t field_idx = 1;
meillo@14: bool found_any_selected_field = false;
meillo@14: bool buffer_first_field;
meillo@14:
meillo@14: current_rp = rp;
meillo@14:
meillo@14: c = getc (stream);
meillo@14: if (c == EOF)
meillo@14: return;
meillo@14:
meillo@14: ungetc (c, stream);
meillo@14: c = 0;
meillo@14:
meillo@14: /* To support the semantics of the -s flag, we may have to buffer
meillo@14: all of the first field to determine whether it is 'delimited.'
meillo@14: But that is unnecessary if all non-delimited lines must be printed
meillo@14: and the first field has been selected, or if non-delimited lines
meillo@14: must be suppressed and the first field has *not* been selected.
meillo@14: That is because a non-delimited line has exactly one field. */
meillo@14: buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
meillo@14:
meillo@14: while (1)
meillo@14: {
meillo@14: if (field_idx == 1 && buffer_first_field)
meillo@14: {
meillo@14: ssize_t len;
meillo@14: size_t n_bytes;
meillo@14:
meillo@14: len = getndelim2 (&field_1_buffer, &field_1_bufsize, 0,
meillo@14: GETNLINE_NO_LIMIT, delim, '\n', stream);
meillo@14: if (len < 0)
meillo@14: {
meillo@14: free (field_1_buffer);
meillo@14: field_1_buffer = NULL;
meillo@14: if (ferror (stream) || feof (stream))
meillo@14: break;
meillo@14: xalloc_die ();
meillo@14: }
meillo@14:
meillo@14: n_bytes = len;
meillo@14: assert (n_bytes != 0);
meillo@14:
meillo@14: c = 0;
meillo@14:
meillo@14: /* If the first field extends to the end of line (it is not
meillo@14: delimited) and we are printing all non-delimited lines,
meillo@14: print this one. */
meillo@14: if (to_uchar (field_1_buffer[n_bytes - 1]) != delim)
meillo@14: {
meillo@14: if (suppress_non_delimited)
meillo@14: {
meillo@14: /* Empty. */
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: fwrite (field_1_buffer, sizeof (char), n_bytes, stdout);
meillo@14: /* Make sure the output line is newline terminated. */
meillo@14: if (field_1_buffer[n_bytes - 1] != '\n')
meillo@14: putchar ('\n');
meillo@14: c = '\n';
meillo@14: }
meillo@14: continue;
meillo@14: }
meillo@14: if (print_kth (1))
meillo@14: {
meillo@14: /* Print the field, but not the trailing delimiter. */
meillo@14: fwrite (field_1_buffer, sizeof (char), n_bytes - 1, stdout);
meillo@14:
meillo@14: /* With -d$'\n' don't treat the last '\n' as a delimiter. */
meillo@14: if (delim == '\n')
meillo@14: {
meillo@14: int last_c = getc (stream);
meillo@14: if (last_c != EOF)
meillo@14: {
meillo@14: ungetc (last_c, stream);
meillo@14: found_any_selected_field = true;
meillo@14: }
meillo@14: }
meillo@14: else
meillo@14: found_any_selected_field = true;
meillo@14: }
meillo@14: next_item (&field_idx);
meillo@14: }
meillo@14:
meillo@14: int prev_c = c;
meillo@14:
meillo@14: if (print_kth (field_idx))
meillo@14: {
meillo@14: if (found_any_selected_field)
meillo@14: {
meillo@14: fwrite (output_delimiter_string, sizeof (char),
meillo@14: output_delimiter_length, stdout);
meillo@14: }
meillo@14: found_any_selected_field = true;
meillo@14:
meillo@14: while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
meillo@14: {
meillo@14: putchar (c);
meillo@14: prev_c = c;
meillo@14: }
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
meillo@14: {
meillo@14: prev_c = c;
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: /* With -d$'\n' don't treat the last '\n' as a delimiter. */
meillo@14: if (delim == '\n' && c == delim)
meillo@14: {
meillo@14: int last_c = getc (stream);
meillo@14: if (last_c != EOF)
meillo@14: ungetc (last_c, stream);
meillo@14: else
meillo@14: c = last_c;
meillo@14: }
meillo@14:
meillo@14: if (c == delim)
meillo@14: next_item (&field_idx);
meillo@14: else if (c == '\n' || c == EOF)
meillo@14: {
meillo@14: if (found_any_selected_field
meillo@14: || !(suppress_non_delimited && field_idx == 1))
meillo@14: {
meillo@14: if (c == '\n' || prev_c != '\n' || delim == '\n')
meillo@14: putchar ('\n');
meillo@14: }
meillo@14: if (c == EOF)
meillo@14: break;
meillo@14: field_idx = 1;
meillo@14: current_rp = rp;
meillo@14: found_any_selected_field = false;
meillo@14: }
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: static void
meillo@14: cut_stream (FILE *stream)
meillo@14: {
meillo@14: if (operating_mode == byte_mode)
meillo@14: cut_bytes (stream);
meillo@14: else
meillo@14: cut_fields (stream);
meillo@14: }
meillo@14:
meillo@14: /* Process file FILE to standard output.
meillo@14: Return true if successful. */
meillo@14:
meillo@14: static bool
meillo@14: cut_file (char const *file)
meillo@14: {
meillo@14: FILE *stream;
meillo@14:
meillo@14: if (STREQ (file, "-"))
meillo@14: {
meillo@14: have_read_stdin = true;
meillo@14: stream = stdin;
meillo@14: }
meillo@14: else
meillo@14: {
meillo@14: stream = fopen (file, "r");
meillo@14: if (stream == NULL)
meillo@14: {
meillo@14: error (0, errno, "%s", file);
meillo@14: return false;
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: fadvise (stream, FADVISE_SEQUENTIAL);
meillo@14:
meillo@14: cut_stream (stream);
meillo@14:
meillo@14: if (ferror (stream))
meillo@14: {
meillo@14: error (0, errno, "%s", file);
meillo@14: return false;
meillo@14: }
meillo@14: if (STREQ (file, "-"))
meillo@14: clearerr (stream); /* Also clear EOF. */
meillo@14: else if (fclose (stream) == EOF)
meillo@14: {
meillo@14: error (0, errno, "%s", file);
meillo@14: return false;
meillo@14: }
meillo@14: return true;
meillo@14: }
meillo@14:
meillo@14: int
meillo@14: main (int argc, char **argv)
meillo@14: {
meillo@14: int optc;
meillo@14: bool ok;
meillo@14: bool delim_specified = false;
meillo@14: char *spec_list_string IF_LINT ( = NULL);
meillo@14:
meillo@14: initialize_main (&argc, &argv);
meillo@14: set_program_name (argv[0]);
meillo@14: setlocale (LC_ALL, "");
meillo@14: bindtextdomain (PACKAGE, LOCALEDIR);
meillo@14: textdomain (PACKAGE);
meillo@14:
meillo@14: atexit (close_stdout);
meillo@14:
meillo@14: operating_mode = undefined_mode;
meillo@14:
meillo@14: /* By default, all non-delimited lines are printed. */
meillo@14: suppress_non_delimited = false;
meillo@14:
meillo@14: delim = '\0';
meillo@14: have_read_stdin = false;
meillo@14:
meillo@14: while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
meillo@14: {
meillo@14: switch (optc)
meillo@14: {
meillo@14: case 'b':
meillo@14: case 'c':
meillo@14: /* Build the byte list. */
meillo@14: if (operating_mode != undefined_mode)
meillo@14: FATAL_ERROR (_("only one type of list may be specified"));
meillo@14: operating_mode = byte_mode;
meillo@14: spec_list_string = optarg;
meillo@14: break;
meillo@14:
meillo@14: case 'f':
meillo@14: /* Build the field list. */
meillo@14: if (operating_mode != undefined_mode)
meillo@14: FATAL_ERROR (_("only one type of list may be specified"));
meillo@14: operating_mode = field_mode;
meillo@14: spec_list_string = optarg;
meillo@14: break;
meillo@14:
meillo@14: case 'd':
meillo@14: /* New delimiter. */
meillo@14: /* Interpret -d '' to mean 'use the NUL byte as the delimiter.' */
meillo@14: if (optarg[0] != '\0' && optarg[1] != '\0')
meillo@14: FATAL_ERROR (_("the delimiter must be a single character"));
meillo@14: delim = optarg[0];
meillo@14: delim_specified = true;
meillo@14: break;
meillo@14:
meillo@14: case OUTPUT_DELIMITER_OPTION:
meillo@14: output_delimiter_specified = true;
meillo@14: /* Interpret --output-delimiter='' to mean
meillo@14: 'use the NUL byte as the delimiter.' */
meillo@14: output_delimiter_length = (optarg[0] == '\0'
meillo@14: ? 1 : strlen (optarg));
meillo@14: output_delimiter_string = xstrdup (optarg);
meillo@14: break;
meillo@14:
meillo@14: case 'n':
meillo@14: break;
meillo@14:
meillo@14: case 's':
meillo@14: suppress_non_delimited = true;
meillo@14: break;
meillo@14:
meillo@14: case COMPLEMENT_OPTION:
meillo@14: complement = true;
meillo@14: break;
meillo@14:
meillo@14: case_GETOPT_HELP_CHAR;
meillo@14:
meillo@14: case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
meillo@14:
meillo@14: default:
meillo@14: usage (EXIT_FAILURE);
meillo@14: }
meillo@14: }
meillo@14:
meillo@14: if (operating_mode == undefined_mode)
meillo@14: FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
meillo@14:
meillo@14: if (delim_specified && operating_mode != field_mode)
meillo@14: FATAL_ERROR (_("an input delimiter may be specified only\
meillo@14: when operating on fields"));
meillo@14:
meillo@14: if (suppress_non_delimited && operating_mode != field_mode)
meillo@14: FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\
meillo@14: \tonly when operating on fields"));
meillo@14:
meillo@14: if (! set_fields (spec_list_string))
meillo@14: {
meillo@14: if (operating_mode == field_mode)
meillo@14: FATAL_ERROR (_("missing list of fields"));
meillo@14: else
meillo@14: FATAL_ERROR (_("missing list of positions"));
meillo@14: }
meillo@14:
meillo@14: if (!delim_specified)
meillo@14: delim = '\t';
meillo@14:
meillo@14: if (output_delimiter_string == NULL)
meillo@14: {
meillo@14: static char dummy[2];
meillo@14: dummy[0] = delim;
meillo@14: dummy[1] = '\0';
meillo@14: output_delimiter_string = dummy;
meillo@14: output_delimiter_length = 1;
meillo@14: }
meillo@14:
meillo@14: if (optind == argc)
meillo@14: ok = cut_file ("-");
meillo@14: else
meillo@14: for (ok = true; optind < argc; optind++)
meillo@14: ok &= cut_file (argv[optind]);
meillo@14:
meillo@14:
meillo@14: if (have_read_stdin && fclose (stdin) == EOF)
meillo@14: {
meillo@14: error (0, errno, "-");
meillo@14: ok = false;
meillo@14: }
meillo@14:
meillo@14: return ok ? EXIT_SUCCESS : EXIT_FAILURE;
meillo@14: }