cut.c
时间:2006-07-31 来源:anima
/* cut - remove parts of lines of files
Copyright (C) 1984, 1997, 1998, 1999, 2000, 2001, 2002 by David M. Ihnat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Written by David Ihnat. */
/* POSIX changes, bug fixes, long-named options, and cleanup
by David MacKenzie <[email protected]>.
Rewrite cut_fields and cut_bytes -- Jim Meyering. */
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <sys/types.h>
/* Get mbstate_t, mbrtowc(). */
#if HAVE_WCHAR_H
# include <wchar.h>
#endif
#include "system.h"
#include "getstr.h"
#include "closeout.h"
#include "error.h"
/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
installation; work around this configuration error. */
#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
# undef MB_LEN_MAX
# define MB_LEN_MAX 16
#endif
/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
#if HAVE_MBRTOWC && defined mbstate_t
# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
#endif
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "cut"
#define AUTHORS N_ ("David Ihnat, David MacKenzie, and Jim Meyering")
#define FATAL_ERROR(Message) \
do \
{ \
error (0, 0, (Message)); \
usage (2); \
} \
while (0)
/* Append LOW, HIGH to the list RP of range pairs, allocating additional
space if necessary. Update local variable N_RP. When allocating,
update global variable N_RP_ALLOCATED. */
#define ADD_RANGE_PAIR(rp, low, high) \
do \
{ \
if (n_rp >= n_rp_allocated) \
{ \
n_rp_allocated *= 2; \
(rp) = (struct range_pair *) xrealloc ((char *) (rp), \
n_rp_allocated * sizeof (*(rp))); \
} \
rp[n_rp].lo = (low); \
rp[n_rp].hi = (high); \
++n_rp; \
} \
while (0)
/* Refill the buffer BUF to get a multibyte character. */
#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM) \
do \
{ \
if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM)) \
{ \
memmove (BUF, BUFPOS, BUFLEN); \
BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
BUFPOS = BUF; \
} \
} \
while (0)
/* Get wide character on BUFPOS. BUFPOS is not included after that.
If byte sequence is not valid as a character, CONVFAIL is 1. Otherwise 0. */
#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
do \
{ \
mbstate_t state_bak; \
\
if (BUFLEN < 1) \
{ \
WC = WEOF; \
break; \
} \
\
/* Get a wide character. */ \
CONVFAIL = 0; \
state_bak = STATE; \
MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE); \
\
switch (MBLENGTH) \
{ \
case (size_t)-1: \
case (size_t)-2: \
CONVFAIL++; \
STATE = state_bak; \
/* Fall througn. */ \
\
case 0: \
MBLENGTH = 1; \
break; \
} \
} \
while (0)
struct range_pair
{
unsigned int lo;
unsigned int hi;
};
/* This buffer is used to support the semantics of the -s option
(or lack of same) when the specified field list includes (does
not include) the first field. In both of those cases, the entire
first field must be read into this buffer to determine whether it
is followed by a delimiter or a newline before any of it may be
output. Otherwise, cut_fields can do the job without using this
buffer. */
static char *field_1_buffer;
/* The number of bytes allocated for FIELD_1_BUFFER. */
static size_t field_1_bufsize;
/* The largest byte, character or field index used as an endpoint of a closed
or degenerate range specification; this doesn't include the starting
index of right-open-ended ranges. For example, with either range spec
`2-5,9-', `2-3,5,9-' this variable would be set to 5. */
static unsigned int max_range_endpoint;
/* If nonzero, this is the index of the first field in a range that goes
to end of line. */
static unsigned int eol_range_start;
/* In byte mode, which bytes to output.
In character mode, which characters to output.
In field mode, which DELIM-separated fields to output.
Bytes, characters and fields are numbered starting with 1,
so the zeroth element of this array is unused.
A byte, character or field K has been selected if
(K <= MAX_RANGE_ENDPOINT and PRINTABLE_FIELD[K])
|| (EOL_RANGE_START > 0 && K >= EOL_RANGE_START). */
static int *printable_field;
enum operating_mode
{
undefined_mode,
/* Output bytes that are at the given positions. */
byte_mode,
/* Output characters that are at the given positions. */
character_mode,
/* Output the given delimeter-separated fields. */
field_mode
};
/* The name this program was run with. */
char *program_name;
static enum operating_mode operating_mode;
/* If nonzero, when in byte mode, don't split multibyte characters. */
static int byte_mode_character_aware;
/* If nonzero, the function for single byte locale is work
if this program runs on multibyte locale. */
static int force_singlebyte_mode;
/* If nonzero do not output lines containing no delimeter characters.
Otherwise, all such lines are printed. This option is valid only
with field mode. */
static int suppress_non_delimited;
/* The delimeter character for field mode. */
static int delim;
#if HAVE_WCHAR_H
static wchar_t wcdelim;
#endif
/* The length of output_delimiter_string. */
static size_t output_delimiter_length;
/* The output field separator string. Defaults to the 1-character
string consisting of the input delimiter. */
static char *output_delimiter_string;
/* Nonzero if we have ever read standard input. */
static int have_read_stdin;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
OUTPUT_DELIMITER_OPTION = CHAR_MAX + 1
};
static struct option const longopts[] =
{
{"bytes", required_argument, 0, 'b'},
{"characters", required_argument, 0, 'c'},
{"fields", required_argument, 0, 'f'},
{"delimiter", required_argument, 0, 'd'},
{"only-delimited", no_argument, 0, 's'},
{"output-delimiter", required_argument, 0, OUTPUT_DELIMITER_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{0, 0, 0, 0}
};
void
usage (int status)
{
if (status != 0)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
Print selected parts of lines from each FILE to standard output.\n\
\n\
"), stdout);
fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
fputs (_("\
-b, --bytes=LIST output only these bytes\n\
-c, --characters=LIST output only these characters\n\
-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter\n\
"), stdout);
fputs (_("\
-f, --fields=LIST output only these fields; also print any line\n\
that contains no delimiter character, unless\n\
the -s option is specified\n\
-n with -b: don't split multibyte characters\n\
"), stdout);
fputs (_("\
-s, --only-delimited do not print lines not containing delimiters\n\
--output-delimiter=STRING use STRING as the output delimiter\n\
the default is to use the input delimiter\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
Use one, and only one of -b, -c or -f. Each LIST is made up of one\n\
range, or many ranges separated by commas. Each range is one of:\n\
\n\
N N'th byte, character or field, counted from 1\n\
N- from N'th byte, character or field, to end of line\n\
N-M from N'th to M'th (included) byte, character or field\n\
-M from first to M'th (included) byte, character or field\n\
\n\
With no FILE, or when FILE is -, read standard input.\n\
"), stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
static int
print_kth (unsigned int k)
{
return ((0 < eol_range_start && eol_range_start <= k)
|| (k <= max_range_endpoint && printable_field[k]));
}
/* Given the list of field or byte range specifications FIELDSTR, set
MAX_RANGE_ENDPOINT and allocate and initialize the PRINTABLE_FIELD
array. If there is a right-open-ended range, set EOL_RANGE_START
to its starting index. FIELDSTR should be composed of one or more
numbers or ranges of numbers, separated by blanks or commas.
Incomplete ranges may be given: `-m' means `1-m'; `n-' means `n'
through end of line. Return nonzero if FIELDSTR contains at least
one field specification, zero otherwise. */
/* FIXME-someday: What if the user wants to cut out the 1,000,000-th field
of some huge input file? This function shouldn't have to alloate a table
of a million ints just so we can test every field < 10^6 with an array
dereference. Instead, consider using a dynamic hash table. It would be
simpler and nearly as good a solution to use a 32K x 4-byte table with
one bit per field index instead of a whole `int' per index. */
static int
set_fields (const char *fieldstr)
{
unsigned int initial = 1; /* Value of first number in a range. */
unsigned int value = 0; /* If nonzero, a number being accumulated. */
int dash_found = 0; /* Nonzero if a '-' is found in this field. */
int field_found = 0; /* Non-zero if at least one field spec
has been processed. */
struct range_pair *rp;
unsigned int n_rp;
unsigned int n_rp_allocated;
unsigned int i;
n_rp = 0;
n_rp_allocated = 16;
rp = (struct range_pair *) xmalloc (n_rp_allocated * sizeof (*rp));
/* Collect and store in RP the range end points.
It also sets EOL_RANGE_START if appropriate. */
for (;;)
{
if (*fieldstr == '-')
{
/* Starting a range. */
if (dash_found)
FATAL_ERROR (_("invalid byte, character or field list"));
dash_found++;
fieldstr++;
if (value)
{
initial = value;
value = 0;
}
else
initial = 1;
}
else if (*fieldstr == ',' || ISBLANK (*fieldstr) || *fieldstr == '\0')
{
/* Ending the string, or this field/byte sublist. */
if (dash_found)
{
dash_found = 0;
/* A range. Possibilites: -n, m-n, n-.
In any case, `initial' contains the start of the range. */
if (value == 0)
{
/* `n-'. From `initial' to end of line. */
if(eol_range_start == 0 ||
(eol_range_start != 0 && eol_range_start > initial))
eol_range_start = initial;
field_found = 1;
}
else
{
/* `m-n' or `-n' (1-n). */
if (value < initial)
FATAL_ERROR (_("invalid byte, character or field list"));
/* Is there already a range going to end of line? */
if (eol_range_start != 0)
{
/* Yes. Is the new sequence already contained
in the old one? If so, no processing is
necessary. */
if (initial < eol_range_start)
{
/* No, the new sequence starts before the
old. Does the old range going to end of line
extend into the new range? */
if (value + 1 >= eol_range_start)
{
/* Yes. Simply move the end of line marker. */
eol_range_start = initial;
}
else
{
/* No. A simple range, before and disjoint from
the range going to end of line. Fill it. */
ADD_RANGE_PAIR (rp, initial, value);
}
/* In any case, some fields were selected. */
field_found = 1;
}
}
else
{
/* There is no range going to end of line. */
ADD_RANGE_PAIR (rp, initial, value);
field_found = 1;
}
value = 0;
}
}
else if (value != 0)
{
/* A simple field number, not a range. */
ADD_RANGE_PAIR (rp, value, value);
value = 0;
field_found = 1;
}
if (*fieldstr == '\0')
{
break;
}
fieldstr++;
}
else if (ISDIGIT (*fieldstr))
{
/* detect overflow. */
{
double check_overflow;
check_overflow = value + (*fieldstr - '0') / 10.0;
if(check_overflow > UINT_MAX / 10.0)
FATAL_ERROR (_("too large number"));
}
value = 10 * value + *fieldstr - '0';
fieldstr++;
}
else
FATAL_ERROR (_("invalid byte, character or field list"));
}
max_range_endpoint = 0;
for (i = 0; i < n_rp; i++)
{
if (rp[i].hi > max_range_endpoint)
max_range_endpoint = rp[i].hi;
}
/* Allocate an array large enough so that it may be indexed by
the field numbers corresponding to all finite ranges
(i.e. `2-6' or `-4', but not `5-') in FIELDSTR. */
printable_field = (int *) xmalloc ((max_range_endpoint + 1) * sizeof (int));
memset (printable_field, 0, (max_range_endpoint + 1) * sizeof (int));
/* Set the array entries corresponding to integers in the ranges of RP. */
for (i = 0; i < n_rp; i++)
{
unsigned int j;
for (j = rp[i].lo; j <= rp[i].hi; j++)
{
printable_field[j] = 1;
}
}
free (rp);
return field_found;
}
/* Read from stream STREAM, printing to standard output any selected bytes. */
static void
cut_bytes (FILE *stream)
{
int byte_idx; /* number of bytes in the line so far. */
byte_idx = 0;
while (1)
{
int c; /* each byte from the file. */
c = getc (stream);
if (c == '\n')
{
putchar ('\n');
byte_idx = 0;
}
else if (c == EOF)
{
if (byte_idx > 0)
putchar ('\n');
break;
}
else
{
++byte_idx;
if (print_kth (byte_idx))
{
putchar (c);
}
}
}
}
#if HAVE_MBRTOWC
/* This function is in use for the following case.
1. Read from the stream STREAM, printing to standard output any selected
characters.
2. Read from stream STREAM, printing to standard output any selected bytes,
without splitting multibyte characters. */
static void
cut_characters_or_cut_bytes_no_split (FILE *stream)
{
int idx; /* number of bytes or characters in the line so far. */
char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
char *bufpos; /* Next read position of BUF. */
size_t buflen; /* The length of the byte sequence in buf. */
wint_t wc; /* A gotten wide character. */
size_t mblength; /* The byte size of a multibyte character which shows
as same character as WC. */
mbstate_t state; /* State of the stream. */
int convfail; /* 1, when conversion is failed. Otherwise 0. */
idx = 0;
buflen = 0;
bufpos = buf;
memset (&state, '\0', sizeof(mbstate_t));
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
{
if (idx > 0)
putchar ('\n');
break;
}
else if (wc == L'\n')
{
putchar ('\n');
idx = 0;
}
else
{
idx += (operating_mode == byte_mode) ? mblength : 1;
if (print_kth (idx))
fwrite (bufpos, mblength, sizeof(char), stdout);
}
buflen -= mblength;
bufpos += mblength;
}
}
#endif
/* Read from stream STREAM, printing to standard output any selected fields. */
static void
cut_fields (FILE *stream)
{
int c;
unsigned int field_idx;
int found_any_selected_field;
int buffer_first_field;
int empty_input;
found_any_selected_field = 0;
field_idx = 1;
c = getc (stream);
empty_input = (c == EOF);
if (c != EOF)
ungetc (c, stream);
/* To support the semantics of the -s flag, we may have to buffer
all of the first field to determine whether it is `delimited.'
But that is unnecessary if all non-delimited lines must be printed
and the first field has been selected, or if non-delimited lines
must be suppressed and the first field has *not* been selected.
That is because a non-delimited line has exactly one field. */
buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
while (1)
{
if (field_idx == 1 && buffer_first_field)
{
int len;
size_t n_bytes;
len = getstr (&field_1_buffer, &field_1_bufsize, stream,
delim, '\n', 0);
if (len < 0)
{
if (ferror (stream) || feof (stream))
break;
xalloc_die ();
}
n_bytes = len;
assert (n_bytes != 0);
/* If the first field extends to the end of line (it is not
delimited) and we are printing all non-delimited lines,
print this one. */
if ((unsigned char) field_1_buffer[n_bytes - 1] != delim)
{
if (suppress_non_delimited)
{
/* Empty. */
}
else
{
fwrite (field_1_buffer, sizeof (char), n_bytes, stdout);
/* Make sure the output line is newline terminated. */
if (field_1_buffer[n_bytes - 1] != '\n')
putchar ('\n');
}
continue;
}
if (print_kth (1))
{
/* Print the field, but not the trailing delimiter. */
fwrite (field_1_buffer, sizeof (char), n_bytes - 1, stdout);
found_any_selected_field = 1;
}
++field_idx;
}
if (c != EOF)
{
if (print_kth (field_idx))
{
if (found_any_selected_field)
{
fwrite (output_delimiter_string, sizeof (char),
output_delimiter_length, stdout);
}
found_any_selected_field = 1;
while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
{
putchar (c);
}
}
else
{
while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
{
/* Empty. */
}
}
}
if (c == '\n')
{
c = getc (stream);
if (c != EOF)
{
ungetc (c, stream);
c = '\n';
}
}
if (c == delim)
++field_idx;
else if (c == '\n' || c == EOF)
{
if (found_any_selected_field
|| (!empty_input && !(suppress_non_delimited && field_idx == 1)))
putchar ('\n');
if (c == EOF)
break;
field_idx = 1;
found_any_selected_field = 0;
}
}
}
#if HAVE_MBRTOWC
static void
cut_fields_mb (FILE *stream)
{
int c;
unsigned int field_idx;
int found_any_selected_field;
int buffer_first_field;
int empty_input;
char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
char *bufpos; /* Next read position of BUF. */
size_t buflen; /* The length of the byte sequence in buf. */
wint_t wc; /* A gotten wide character. */
size_t mblength; /* The byte size of a multibyte character which shows
as same character as WC. */
mbstate_t state; /* State of the stream. */
int convfail; /* 1, when conversion is failed. Otherwise 0. */
found_any_selected_field = 0;
field_idx = 1;
bufpos = buf;
buflen = 0;
memset (&state, '\0', sizeof(mbstate_t));
c = getc (stream);
empty_input = (c == EOF);
if (c != EOF)
ungetc (c, stream);
else
wc = WEOF;
/* To support the semantics of the -s flag, we may have to buffer
all of the first field to determine whether it is `delimited.'
But that is unnecessary if all non-delimited lines must be printed
and the first field has been selected, or if non-delimited lines
must be suppressed and the first field has *not* been selected.
That is because a non-delimited line has exactly one field. */
buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
while (1)
{
if (field_idx == 1 && buffer_first_field)
{
int len = 0;
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER
(wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
break;
field_1_buffer = xrealloc (field_1_buffer, len + mblength);
memcpy (field_1_buffer + len, bufpos, mblength);
len += mblength;
buflen -= mblength;
bufpos += mblength;
if (!convfail && (wc == L'\n' || wc == wcdelim))
break;
}
if (wc == WEOF)
break;
/* If the first field extends to the end of line (it is not
delimited) and we are printing all non-delimited lines,
print this one. */
if (convfail || (!convfail && wc != wcdelim))
{
if (suppress_non_delimited)
{
/* Empty. */
}
else
{
fwrite (field_1_buffer, sizeof (char), len, stdout);
/* Make sure the output line is newline terminated. */
if (convfail || (!convfail && wc != L'\n'))
putchar ('\n');
}
continue;
}
if (print_kth (1))
{
/* Print the field, but not the trailing delimiter. */
fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
found_any_selected_field = 1;
}
++field_idx;
}
if (wc != WEOF)
{
if (print_kth (field_idx))
{
if (found_any_selected_field)
{
fwrite (output_delimiter_string, sizeof (char),
output_delimiter_length, stdout);
}
found_any_selected_field = 1;
}
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER
(wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
break;
else if (!convfail && (wc == wcdelim || wc == L'\n'))
{
buflen -= mblength;
bufpos += mblength;
break;
}
if (print_kth (field_idx))
fwrite (bufpos, mblength, sizeof(char), stdout);
buflen -= mblength;
bufpos += mblength;
}
}
if ((!convfail || wc == L'\n') && buflen < 1)
wc = WEOF;
if (!convfail && wc == wcdelim)
++field_idx;
else if (wc == WEOF || (!convfail && wc == L'\n'))
{
if (found_any_selected_field
|| (!empty_input && !(suppress_non_delimited && field_idx == 1)))
putchar ('\n');
if (wc == WEOF)
break;
field_idx = 1;
found_any_selected_field = 0;
}
}
}
#endif
static void
cut_stream (FILE *stream)
{
#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
{
switch (operating_mode)
{
case byte_mode:
if (byte_mode_character_aware)
cut_characters_or_cut_bytes_no_split (stream);
else
cut_bytes (stream);
break;
case character_mode:
cut_characters_or_cut_bytes_no_split (stream);
break;
case field_mode:
cut_fields_mb (stream);
break;
default:
abort ();
}
}
else
#endif
{
if (operating_mode == field_mode)
cut_fields (stream);
else
cut_bytes (stream);
}
}
/* Process file FILE to standard output.
Return 0 if successful, 1 if not. */
static int
cut_file (char *file)
{
FILE *stream;
if (STREQ (file, "-"))
{
have_read_stdin = 1;
stream = stdin;
}
else
{
stream = fopen (file, "r");
if (stream == NULL)
{
error (0, errno, "%s", file);
return 1;
}
}
cut_stream (stream);
if (ferror (stream))
{
error (0, errno, "%s", file);
return 1;
}
if (STREQ (file, "-"))
clearerr (stream); /* Also clear EOF. */
else if (fclose (stream) == EOF)
{
error (0, errno, "%s", file);
return 1;
}
return 0;
}
int
main (int argc, char **argv)
{
int optc, exit_status = 0;
int delim_specified = 0;
char mbdelim[MB_LEN_MAX + 1];
size_t delimlen = 0;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
operating_mode = undefined_mode;
/* By default, all non-delimited lines are printed. */
suppress_non_delimited = 0;
delim = '\0';
have_read_stdin = 0;
while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
{
switch (optc)
{
case 0:
break;
case 'b':
/* Build the byte list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = byte_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of positions"));
break;
case 'c':
/* Build the character list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = character_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of positions"));
break;
case 'f':
/* Build the field list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = field_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of fields"));
break;
case 'd':
/* New delimiter. */
/* Interpret -d '' to mean `use the NUL byte as the delimiter.' */
#if HAVE_MBRTOWC
{
if(MB_CUR_MAX > 1)
{
mbstate_t state;
memset (&state, '\0', sizeof(mbstate_t));
delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
++force_singlebyte_mode;
else
{
delimlen = (delimlen < 1) ? 1 : delimlen;
if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
FATAL_ERROR (_("the delimiter must be a single character"));
memcpy (mbdelim, optarg, delimlen);
}
}
if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
#endif
{
if (optarg[0] != '\0' && optarg[1] != '\0')
FATAL_ERROR (_("the delimiter must be a single character"));
delim = (unsigned char) optarg[0];
}
delim_specified = 1;
}
break;
case OUTPUT_DELIMITER_OPTION:
/* Interpret --output-delimiter='' to mean
`use the NUL byte as the delimiter.' */
output_delimiter_length = (optarg[0] == '\0'
? 1 : strlen (optarg));
output_delimiter_string = xstrdup (optarg);
break;
case 'n':
byte_mode_character_aware = 1;
break;
case 's':
suppress_non_delimited = 1;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (2);
}
}
if (operating_mode == undefined_mode)
FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
if (delim_specified && operating_mode != field_mode)
FATAL_ERROR (_("a delimiter may be specified only when operating on fields"));
if (suppress_non_delimited && operating_mode != field_mode)
FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\
\tonly when operating on fields"));
if (!delim_specified)
{
delim = '\t';
#ifdef HAVE_MBRTOWC
wcdelim = L'\t';
mbdelim[0] = '\t';
mbdelim[1] = '\0';
delimlen = 1;
#endif
}
if (output_delimiter_string == NULL)
{
#ifdef HAVE_MBRTOWC
if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
{
output_delimiter_string = xstrdup(mbdelim);
output_delimiter_length = delimlen;
}
if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
#endif
{
static char dummy[2];
dummy[0] = delim;
dummy[1] = '\0';
output_delimiter_string = dummy;
output_delimiter_length = 1;
}
}
if (optind == argc)
exit_status |= cut_file ("-");
else
for (; optind < argc; optind++)
exit_status |= cut_file (argv[optind]);
if (have_read_stdin && fclose (stdin) == EOF)
{
error (0, errno, "-");
exit_status = 1;
}
exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Copyright (C) 1984, 1997, 1998, 1999, 2000, 2001, 2002 by David M. Ihnat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Written by David Ihnat. */
/* POSIX changes, bug fixes, long-named options, and cleanup
by David MacKenzie <[email protected]>.
Rewrite cut_fields and cut_bytes -- Jim Meyering. */
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <sys/types.h>
/* Get mbstate_t, mbrtowc(). */
#if HAVE_WCHAR_H
# include <wchar.h>
#endif
#include "system.h"
#include "getstr.h"
#include "closeout.h"
#include "error.h"
/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
installation; work around this configuration error. */
#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
# undef MB_LEN_MAX
# define MB_LEN_MAX 16
#endif
/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
#if HAVE_MBRTOWC && defined mbstate_t
# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
#endif
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "cut"
#define AUTHORS N_ ("David Ihnat, David MacKenzie, and Jim Meyering")
#define FATAL_ERROR(Message) \
do \
{ \
error (0, 0, (Message)); \
usage (2); \
} \
while (0)
/* Append LOW, HIGH to the list RP of range pairs, allocating additional
space if necessary. Update local variable N_RP. When allocating,
update global variable N_RP_ALLOCATED. */
#define ADD_RANGE_PAIR(rp, low, high) \
do \
{ \
if (n_rp >= n_rp_allocated) \
{ \
n_rp_allocated *= 2; \
(rp) = (struct range_pair *) xrealloc ((char *) (rp), \
n_rp_allocated * sizeof (*(rp))); \
} \
rp[n_rp].lo = (low); \
rp[n_rp].hi = (high); \
++n_rp; \
} \
while (0)
/* Refill the buffer BUF to get a multibyte character. */
#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM) \
do \
{ \
if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM)) \
{ \
memmove (BUF, BUFPOS, BUFLEN); \
BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
BUFPOS = BUF; \
} \
} \
while (0)
/* Get wide character on BUFPOS. BUFPOS is not included after that.
If byte sequence is not valid as a character, CONVFAIL is 1. Otherwise 0. */
#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
do \
{ \
mbstate_t state_bak; \
\
if (BUFLEN < 1) \
{ \
WC = WEOF; \
break; \
} \
\
/* Get a wide character. */ \
CONVFAIL = 0; \
state_bak = STATE; \
MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE); \
\
switch (MBLENGTH) \
{ \
case (size_t)-1: \
case (size_t)-2: \
CONVFAIL++; \
STATE = state_bak; \
/* Fall througn. */ \
\
case 0: \
MBLENGTH = 1; \
break; \
} \
} \
while (0)
struct range_pair
{
unsigned int lo;
unsigned int hi;
};
/* This buffer is used to support the semantics of the -s option
(or lack of same) when the specified field list includes (does
not include) the first field. In both of those cases, the entire
first field must be read into this buffer to determine whether it
is followed by a delimiter or a newline before any of it may be
output. Otherwise, cut_fields can do the job without using this
buffer. */
static char *field_1_buffer;
/* The number of bytes allocated for FIELD_1_BUFFER. */
static size_t field_1_bufsize;
/* The largest byte, character or field index used as an endpoint of a closed
or degenerate range specification; this doesn't include the starting
index of right-open-ended ranges. For example, with either range spec
`2-5,9-', `2-3,5,9-' this variable would be set to 5. */
static unsigned int max_range_endpoint;
/* If nonzero, this is the index of the first field in a range that goes
to end of line. */
static unsigned int eol_range_start;
/* In byte mode, which bytes to output.
In character mode, which characters to output.
In field mode, which DELIM-separated fields to output.
Bytes, characters and fields are numbered starting with 1,
so the zeroth element of this array is unused.
A byte, character or field K has been selected if
(K <= MAX_RANGE_ENDPOINT and PRINTABLE_FIELD[K])
|| (EOL_RANGE_START > 0 && K >= EOL_RANGE_START). */
static int *printable_field;
enum operating_mode
{
undefined_mode,
/* Output bytes that are at the given positions. */
byte_mode,
/* Output characters that are at the given positions. */
character_mode,
/* Output the given delimeter-separated fields. */
field_mode
};
/* The name this program was run with. */
char *program_name;
static enum operating_mode operating_mode;
/* If nonzero, when in byte mode, don't split multibyte characters. */
static int byte_mode_character_aware;
/* If nonzero, the function for single byte locale is work
if this program runs on multibyte locale. */
static int force_singlebyte_mode;
/* If nonzero do not output lines containing no delimeter characters.
Otherwise, all such lines are printed. This option is valid only
with field mode. */
static int suppress_non_delimited;
/* The delimeter character for field mode. */
static int delim;
#if HAVE_WCHAR_H
static wchar_t wcdelim;
#endif
/* The length of output_delimiter_string. */
static size_t output_delimiter_length;
/* The output field separator string. Defaults to the 1-character
string consisting of the input delimiter. */
static char *output_delimiter_string;
/* Nonzero if we have ever read standard input. */
static int have_read_stdin;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
OUTPUT_DELIMITER_OPTION = CHAR_MAX + 1
};
static struct option const longopts[] =
{
{"bytes", required_argument, 0, 'b'},
{"characters", required_argument, 0, 'c'},
{"fields", required_argument, 0, 'f'},
{"delimiter", required_argument, 0, 'd'},
{"only-delimited", no_argument, 0, 's'},
{"output-delimiter", required_argument, 0, OUTPUT_DELIMITER_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{0, 0, 0, 0}
};
void
usage (int status)
{
if (status != 0)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
Print selected parts of lines from each FILE to standard output.\n\
\n\
"), stdout);
fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
fputs (_("\
-b, --bytes=LIST output only these bytes\n\
-c, --characters=LIST output only these characters\n\
-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter\n\
"), stdout);
fputs (_("\
-f, --fields=LIST output only these fields; also print any line\n\
that contains no delimiter character, unless\n\
the -s option is specified\n\
-n with -b: don't split multibyte characters\n\
"), stdout);
fputs (_("\
-s, --only-delimited do not print lines not containing delimiters\n\
--output-delimiter=STRING use STRING as the output delimiter\n\
the default is to use the input delimiter\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
Use one, and only one of -b, -c or -f. Each LIST is made up of one\n\
range, or many ranges separated by commas. Each range is one of:\n\
\n\
N N'th byte, character or field, counted from 1\n\
N- from N'th byte, character or field, to end of line\n\
N-M from N'th to M'th (included) byte, character or field\n\
-M from first to M'th (included) byte, character or field\n\
\n\
With no FILE, or when FILE is -, read standard input.\n\
"), stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
static int
print_kth (unsigned int k)
{
return ((0 < eol_range_start && eol_range_start <= k)
|| (k <= max_range_endpoint && printable_field[k]));
}
/* Given the list of field or byte range specifications FIELDSTR, set
MAX_RANGE_ENDPOINT and allocate and initialize the PRINTABLE_FIELD
array. If there is a right-open-ended range, set EOL_RANGE_START
to its starting index. FIELDSTR should be composed of one or more
numbers or ranges of numbers, separated by blanks or commas.
Incomplete ranges may be given: `-m' means `1-m'; `n-' means `n'
through end of line. Return nonzero if FIELDSTR contains at least
one field specification, zero otherwise. */
/* FIXME-someday: What if the user wants to cut out the 1,000,000-th field
of some huge input file? This function shouldn't have to alloate a table
of a million ints just so we can test every field < 10^6 with an array
dereference. Instead, consider using a dynamic hash table. It would be
simpler and nearly as good a solution to use a 32K x 4-byte table with
one bit per field index instead of a whole `int' per index. */
static int
set_fields (const char *fieldstr)
{
unsigned int initial = 1; /* Value of first number in a range. */
unsigned int value = 0; /* If nonzero, a number being accumulated. */
int dash_found = 0; /* Nonzero if a '-' is found in this field. */
int field_found = 0; /* Non-zero if at least one field spec
has been processed. */
struct range_pair *rp;
unsigned int n_rp;
unsigned int n_rp_allocated;
unsigned int i;
n_rp = 0;
n_rp_allocated = 16;
rp = (struct range_pair *) xmalloc (n_rp_allocated * sizeof (*rp));
/* Collect and store in RP the range end points.
It also sets EOL_RANGE_START if appropriate. */
for (;;)
{
if (*fieldstr == '-')
{
/* Starting a range. */
if (dash_found)
FATAL_ERROR (_("invalid byte, character or field list"));
dash_found++;
fieldstr++;
if (value)
{
initial = value;
value = 0;
}
else
initial = 1;
}
else if (*fieldstr == ',' || ISBLANK (*fieldstr) || *fieldstr == '\0')
{
/* Ending the string, or this field/byte sublist. */
if (dash_found)
{
dash_found = 0;
/* A range. Possibilites: -n, m-n, n-.
In any case, `initial' contains the start of the range. */
if (value == 0)
{
/* `n-'. From `initial' to end of line. */
if(eol_range_start == 0 ||
(eol_range_start != 0 && eol_range_start > initial))
eol_range_start = initial;
field_found = 1;
}
else
{
/* `m-n' or `-n' (1-n). */
if (value < initial)
FATAL_ERROR (_("invalid byte, character or field list"));
/* Is there already a range going to end of line? */
if (eol_range_start != 0)
{
/* Yes. Is the new sequence already contained
in the old one? If so, no processing is
necessary. */
if (initial < eol_range_start)
{
/* No, the new sequence starts before the
old. Does the old range going to end of line
extend into the new range? */
if (value + 1 >= eol_range_start)
{
/* Yes. Simply move the end of line marker. */
eol_range_start = initial;
}
else
{
/* No. A simple range, before and disjoint from
the range going to end of line. Fill it. */
ADD_RANGE_PAIR (rp, initial, value);
}
/* In any case, some fields were selected. */
field_found = 1;
}
}
else
{
/* There is no range going to end of line. */
ADD_RANGE_PAIR (rp, initial, value);
field_found = 1;
}
value = 0;
}
}
else if (value != 0)
{
/* A simple field number, not a range. */
ADD_RANGE_PAIR (rp, value, value);
value = 0;
field_found = 1;
}
if (*fieldstr == '\0')
{
break;
}
fieldstr++;
}
else if (ISDIGIT (*fieldstr))
{
/* detect overflow. */
{
double check_overflow;
check_overflow = value + (*fieldstr - '0') / 10.0;
if(check_overflow > UINT_MAX / 10.0)
FATAL_ERROR (_("too large number"));
}
value = 10 * value + *fieldstr - '0';
fieldstr++;
}
else
FATAL_ERROR (_("invalid byte, character or field list"));
}
max_range_endpoint = 0;
for (i = 0; i < n_rp; i++)
{
if (rp[i].hi > max_range_endpoint)
max_range_endpoint = rp[i].hi;
}
/* Allocate an array large enough so that it may be indexed by
the field numbers corresponding to all finite ranges
(i.e. `2-6' or `-4', but not `5-') in FIELDSTR. */
printable_field = (int *) xmalloc ((max_range_endpoint + 1) * sizeof (int));
memset (printable_field, 0, (max_range_endpoint + 1) * sizeof (int));
/* Set the array entries corresponding to integers in the ranges of RP. */
for (i = 0; i < n_rp; i++)
{
unsigned int j;
for (j = rp[i].lo; j <= rp[i].hi; j++)
{
printable_field[j] = 1;
}
}
free (rp);
return field_found;
}
/* Read from stream STREAM, printing to standard output any selected bytes. */
static void
cut_bytes (FILE *stream)
{
int byte_idx; /* number of bytes in the line so far. */
byte_idx = 0;
while (1)
{
int c; /* each byte from the file. */
c = getc (stream);
if (c == '\n')
{
putchar ('\n');
byte_idx = 0;
}
else if (c == EOF)
{
if (byte_idx > 0)
putchar ('\n');
break;
}
else
{
++byte_idx;
if (print_kth (byte_idx))
{
putchar (c);
}
}
}
}
#if HAVE_MBRTOWC
/* This function is in use for the following case.
1. Read from the stream STREAM, printing to standard output any selected
characters.
2. Read from stream STREAM, printing to standard output any selected bytes,
without splitting multibyte characters. */
static void
cut_characters_or_cut_bytes_no_split (FILE *stream)
{
int idx; /* number of bytes or characters in the line so far. */
char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
char *bufpos; /* Next read position of BUF. */
size_t buflen; /* The length of the byte sequence in buf. */
wint_t wc; /* A gotten wide character. */
size_t mblength; /* The byte size of a multibyte character which shows
as same character as WC. */
mbstate_t state; /* State of the stream. */
int convfail; /* 1, when conversion is failed. Otherwise 0. */
idx = 0;
buflen = 0;
bufpos = buf;
memset (&state, '\0', sizeof(mbstate_t));
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
{
if (idx > 0)
putchar ('\n');
break;
}
else if (wc == L'\n')
{
putchar ('\n');
idx = 0;
}
else
{
idx += (operating_mode == byte_mode) ? mblength : 1;
if (print_kth (idx))
fwrite (bufpos, mblength, sizeof(char), stdout);
}
buflen -= mblength;
bufpos += mblength;
}
}
#endif
/* Read from stream STREAM, printing to standard output any selected fields. */
static void
cut_fields (FILE *stream)
{
int c;
unsigned int field_idx;
int found_any_selected_field;
int buffer_first_field;
int empty_input;
found_any_selected_field = 0;
field_idx = 1;
c = getc (stream);
empty_input = (c == EOF);
if (c != EOF)
ungetc (c, stream);
/* To support the semantics of the -s flag, we may have to buffer
all of the first field to determine whether it is `delimited.'
But that is unnecessary if all non-delimited lines must be printed
and the first field has been selected, or if non-delimited lines
must be suppressed and the first field has *not* been selected.
That is because a non-delimited line has exactly one field. */
buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
while (1)
{
if (field_idx == 1 && buffer_first_field)
{
int len;
size_t n_bytes;
len = getstr (&field_1_buffer, &field_1_bufsize, stream,
delim, '\n', 0);
if (len < 0)
{
if (ferror (stream) || feof (stream))
break;
xalloc_die ();
}
n_bytes = len;
assert (n_bytes != 0);
/* If the first field extends to the end of line (it is not
delimited) and we are printing all non-delimited lines,
print this one. */
if ((unsigned char) field_1_buffer[n_bytes - 1] != delim)
{
if (suppress_non_delimited)
{
/* Empty. */
}
else
{
fwrite (field_1_buffer, sizeof (char), n_bytes, stdout);
/* Make sure the output line is newline terminated. */
if (field_1_buffer[n_bytes - 1] != '\n')
putchar ('\n');
}
continue;
}
if (print_kth (1))
{
/* Print the field, but not the trailing delimiter. */
fwrite (field_1_buffer, sizeof (char), n_bytes - 1, stdout);
found_any_selected_field = 1;
}
++field_idx;
}
if (c != EOF)
{
if (print_kth (field_idx))
{
if (found_any_selected_field)
{
fwrite (output_delimiter_string, sizeof (char),
output_delimiter_length, stdout);
}
found_any_selected_field = 1;
while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
{
putchar (c);
}
}
else
{
while ((c = getc (stream)) != delim && c != '\n' && c != EOF)
{
/* Empty. */
}
}
}
if (c == '\n')
{
c = getc (stream);
if (c != EOF)
{
ungetc (c, stream);
c = '\n';
}
}
if (c == delim)
++field_idx;
else if (c == '\n' || c == EOF)
{
if (found_any_selected_field
|| (!empty_input && !(suppress_non_delimited && field_idx == 1)))
putchar ('\n');
if (c == EOF)
break;
field_idx = 1;
found_any_selected_field = 0;
}
}
}
#if HAVE_MBRTOWC
static void
cut_fields_mb (FILE *stream)
{
int c;
unsigned int field_idx;
int found_any_selected_field;
int buffer_first_field;
int empty_input;
char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
char *bufpos; /* Next read position of BUF. */
size_t buflen; /* The length of the byte sequence in buf. */
wint_t wc; /* A gotten wide character. */
size_t mblength; /* The byte size of a multibyte character which shows
as same character as WC. */
mbstate_t state; /* State of the stream. */
int convfail; /* 1, when conversion is failed. Otherwise 0. */
found_any_selected_field = 0;
field_idx = 1;
bufpos = buf;
buflen = 0;
memset (&state, '\0', sizeof(mbstate_t));
c = getc (stream);
empty_input = (c == EOF);
if (c != EOF)
ungetc (c, stream);
else
wc = WEOF;
/* To support the semantics of the -s flag, we may have to buffer
all of the first field to determine whether it is `delimited.'
But that is unnecessary if all non-delimited lines must be printed
and the first field has been selected, or if non-delimited lines
must be suppressed and the first field has *not* been selected.
That is because a non-delimited line has exactly one field. */
buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
while (1)
{
if (field_idx == 1 && buffer_first_field)
{
int len = 0;
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER
(wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
break;
field_1_buffer = xrealloc (field_1_buffer, len + mblength);
memcpy (field_1_buffer + len, bufpos, mblength);
len += mblength;
buflen -= mblength;
bufpos += mblength;
if (!convfail && (wc == L'\n' || wc == wcdelim))
break;
}
if (wc == WEOF)
break;
/* If the first field extends to the end of line (it is not
delimited) and we are printing all non-delimited lines,
print this one. */
if (convfail || (!convfail && wc != wcdelim))
{
if (suppress_non_delimited)
{
/* Empty. */
}
else
{
fwrite (field_1_buffer, sizeof (char), len, stdout);
/* Make sure the output line is newline terminated. */
if (convfail || (!convfail && wc != L'\n'))
putchar ('\n');
}
continue;
}
if (print_kth (1))
{
/* Print the field, but not the trailing delimiter. */
fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
found_any_selected_field = 1;
}
++field_idx;
}
if (wc != WEOF)
{
if (print_kth (field_idx))
{
if (found_any_selected_field)
{
fwrite (output_delimiter_string, sizeof (char),
output_delimiter_length, stdout);
}
found_any_selected_field = 1;
}
while (1)
{
REFILL_BUFFER (buf, bufpos, buflen, stream);
GET_NEXT_WC_FROM_BUFFER
(wc, bufpos, buflen, mblength, state, convfail);
if (wc == WEOF)
break;
else if (!convfail && (wc == wcdelim || wc == L'\n'))
{
buflen -= mblength;
bufpos += mblength;
break;
}
if (print_kth (field_idx))
fwrite (bufpos, mblength, sizeof(char), stdout);
buflen -= mblength;
bufpos += mblength;
}
}
if ((!convfail || wc == L'\n') && buflen < 1)
wc = WEOF;
if (!convfail && wc == wcdelim)
++field_idx;
else if (wc == WEOF || (!convfail && wc == L'\n'))
{
if (found_any_selected_field
|| (!empty_input && !(suppress_non_delimited && field_idx == 1)))
putchar ('\n');
if (wc == WEOF)
break;
field_idx = 1;
found_any_selected_field = 0;
}
}
}
#endif
static void
cut_stream (FILE *stream)
{
#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
{
switch (operating_mode)
{
case byte_mode:
if (byte_mode_character_aware)
cut_characters_or_cut_bytes_no_split (stream);
else
cut_bytes (stream);
break;
case character_mode:
cut_characters_or_cut_bytes_no_split (stream);
break;
case field_mode:
cut_fields_mb (stream);
break;
default:
abort ();
}
}
else
#endif
{
if (operating_mode == field_mode)
cut_fields (stream);
else
cut_bytes (stream);
}
}
/* Process file FILE to standard output.
Return 0 if successful, 1 if not. */
static int
cut_file (char *file)
{
FILE *stream;
if (STREQ (file, "-"))
{
have_read_stdin = 1;
stream = stdin;
}
else
{
stream = fopen (file, "r");
if (stream == NULL)
{
error (0, errno, "%s", file);
return 1;
}
}
cut_stream (stream);
if (ferror (stream))
{
error (0, errno, "%s", file);
return 1;
}
if (STREQ (file, "-"))
clearerr (stream); /* Also clear EOF. */
else if (fclose (stream) == EOF)
{
error (0, errno, "%s", file);
return 1;
}
return 0;
}
int
main (int argc, char **argv)
{
int optc, exit_status = 0;
int delim_specified = 0;
char mbdelim[MB_LEN_MAX + 1];
size_t delimlen = 0;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
operating_mode = undefined_mode;
/* By default, all non-delimited lines are printed. */
suppress_non_delimited = 0;
delim = '\0';
have_read_stdin = 0;
while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
{
switch (optc)
{
case 0:
break;
case 'b':
/* Build the byte list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = byte_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of positions"));
break;
case 'c':
/* Build the character list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = character_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of positions"));
break;
case 'f':
/* Build the field list. */
if (operating_mode != undefined_mode)
FATAL_ERROR (_("only one type of list may be specified"));
operating_mode = field_mode;
if (set_fields (optarg) == 0)
FATAL_ERROR (_("missing list of fields"));
break;
case 'd':
/* New delimiter. */
/* Interpret -d '' to mean `use the NUL byte as the delimiter.' */
#if HAVE_MBRTOWC
{
if(MB_CUR_MAX > 1)
{
mbstate_t state;
memset (&state, '\0', sizeof(mbstate_t));
delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
++force_singlebyte_mode;
else
{
delimlen = (delimlen < 1) ? 1 : delimlen;
if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
FATAL_ERROR (_("the delimiter must be a single character"));
memcpy (mbdelim, optarg, delimlen);
}
}
if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
#endif
{
if (optarg[0] != '\0' && optarg[1] != '\0')
FATAL_ERROR (_("the delimiter must be a single character"));
delim = (unsigned char) optarg[0];
}
delim_specified = 1;
}
break;
case OUTPUT_DELIMITER_OPTION:
/* Interpret --output-delimiter='' to mean
`use the NUL byte as the delimiter.' */
output_delimiter_length = (optarg[0] == '\0'
? 1 : strlen (optarg));
output_delimiter_string = xstrdup (optarg);
break;
case 'n':
byte_mode_character_aware = 1;
break;
case 's':
suppress_non_delimited = 1;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (2);
}
}
if (operating_mode == undefined_mode)
FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
if (delim_specified && operating_mode != field_mode)
FATAL_ERROR (_("a delimiter may be specified only when operating on fields"));
if (suppress_non_delimited && operating_mode != field_mode)
FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\
\tonly when operating on fields"));
if (!delim_specified)
{
delim = '\t';
#ifdef HAVE_MBRTOWC
wcdelim = L'\t';
mbdelim[0] = '\t';
mbdelim[1] = '\0';
delimlen = 1;
#endif
}
if (output_delimiter_string == NULL)
{
#ifdef HAVE_MBRTOWC
if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
{
output_delimiter_string = xstrdup(mbdelim);
output_delimiter_length = delimlen;
}
if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
#endif
{
static char dummy[2];
dummy[0] = delim;
dummy[1] = '\0';
output_delimiter_string = dummy;
output_delimiter_length = 1;
}
}
if (optind == argc)
exit_status |= cut_file ("-");
else
for (; optind < argc; optind++)
exit_status |= cut_file (argv[optind]);
if (have_read_stdin && fclose (stdin) == EOF)
{
error (0, errno, "-");
exit_status = 1;
}
exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
相关阅读 更多 +