mirror of
git://git.sv.gnu.org/coreutils.git
synced 2026-04-18 01:40:06 +02:00
cut: simplify and remove an IF_LINT
* src/cut.c (enum operating_mode, operating_mode) (output_delimiter_specified, cut_stream): Remove; no longer needed. (output_delimiter_default): New static var. Code can now use ‘output_delimiter_string != output_delimiter_default’ instead of ‘output_delimiter_specified’. (cut_file): New arg CUT_STREAM. Caller changed. (main): Simplify. Coalesce duplicate code. Redo to avoid need for IF_LINT, or for the static var. No need to xstrdup optarg.
This commit is contained in:
83
src/cut.c
83
src/cut.c
@@ -72,19 +72,6 @@ static char *field_1_buffer;
|
||||
/* The number of bytes allocated for FIELD_1_BUFFER. */
|
||||
static size_t field_1_bufsize;
|
||||
|
||||
enum operating_mode
|
||||
{
|
||||
undefined_mode,
|
||||
|
||||
/* Output characters that are in the given bytes. */
|
||||
byte_mode,
|
||||
|
||||
/* Output the given delimiter-separated fields. */
|
||||
field_mode
|
||||
};
|
||||
|
||||
static enum operating_mode operating_mode;
|
||||
|
||||
/* If true do not output lines containing no delimiter characters.
|
||||
Otherwise, all such lines are printed. This option is valid only
|
||||
with field mode. */
|
||||
@@ -100,9 +87,6 @@ static unsigned char delim;
|
||||
/* The delimiter for each line/record. */
|
||||
static unsigned char line_delim = '\n';
|
||||
|
||||
/* True if the --output-delimiter=STRING option was specified. */
|
||||
static bool output_delimiter_specified;
|
||||
|
||||
/* The length of output_delimiter_string. */
|
||||
static size_t output_delimiter_length;
|
||||
|
||||
@@ -110,6 +94,9 @@ static size_t output_delimiter_length;
|
||||
string consisting of the input delimiter. */
|
||||
static char *output_delimiter_string;
|
||||
|
||||
/* The output delimiter string contents, if the default. */
|
||||
char output_delimiter_default[1];
|
||||
|
||||
/* True if we have ever read standard input. */
|
||||
static bool have_read_stdin;
|
||||
|
||||
@@ -263,7 +250,7 @@ cut_bytes (FILE *stream)
|
||||
next_item (&byte_idx);
|
||||
if (print_kth (byte_idx))
|
||||
{
|
||||
if (output_delimiter_specified)
|
||||
if (output_delimiter_string != output_delimiter_default)
|
||||
{
|
||||
if (print_delimiter && is_range_start_index (byte_idx))
|
||||
{
|
||||
@@ -424,20 +411,11 @@ cut_fields (FILE *stream)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
cut_stream (FILE *stream)
|
||||
{
|
||||
if (operating_mode == byte_mode)
|
||||
cut_bytes (stream);
|
||||
else
|
||||
cut_fields (stream);
|
||||
}
|
||||
|
||||
/* Process file FILE to standard output.
|
||||
/* Process file FILE to standard output, using CUT_STREAM.
|
||||
Return true if successful. */
|
||||
|
||||
static bool
|
||||
cut_file (char const *file)
|
||||
cut_file (char const *file, void (*cut_stream) (FILE *))
|
||||
{
|
||||
FILE *stream;
|
||||
|
||||
@@ -481,7 +459,8 @@ main (int argc, char **argv)
|
||||
int optc;
|
||||
bool ok;
|
||||
bool delim_specified = false;
|
||||
char *spec_list_string IF_LINT ( = NULL);
|
||||
bool byte_mode = false;
|
||||
char *spec_list_string = NULL;
|
||||
|
||||
initialize_main (&argc, &argv);
|
||||
set_program_name (argv[0]);
|
||||
@@ -491,8 +470,6 @@ main (int argc, char **argv)
|
||||
|
||||
atexit (close_stdout);
|
||||
|
||||
operating_mode = undefined_mode;
|
||||
|
||||
/* By default, all non-delimited lines are printed. */
|
||||
suppress_non_delimited = false;
|
||||
|
||||
@@ -506,17 +483,12 @@ main (int argc, char **argv)
|
||||
case 'b':
|
||||
case 'c':
|
||||
/* Build the byte list. */
|
||||
if (operating_mode != undefined_mode)
|
||||
FATAL_ERROR (_("only one type of list may be specified"));
|
||||
operating_mode = byte_mode;
|
||||
spec_list_string = optarg;
|
||||
break;
|
||||
|
||||
byte_mode = true;
|
||||
FALLTHROUGH;
|
||||
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 (spec_list_string)
|
||||
FATAL_ERROR (_("only one list may be specified"));
|
||||
spec_list_string = optarg;
|
||||
break;
|
||||
|
||||
@@ -530,12 +502,11 @@ main (int argc, char **argv)
|
||||
break;
|
||||
|
||||
case OUTPUT_DELIMITER_OPTION:
|
||||
output_delimiter_specified = true;
|
||||
/* 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);
|
||||
output_delimiter_string = optarg;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
@@ -562,38 +533,40 @@ main (int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (operating_mode == undefined_mode)
|
||||
if (!spec_list_string)
|
||||
FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
|
||||
|
||||
if (delim_specified && operating_mode != field_mode)
|
||||
FATAL_ERROR (_("an input delimiter may be specified only\
|
||||
if (byte_mode)
|
||||
{
|
||||
if (delim_specified)
|
||||
FATAL_ERROR (_("an input 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\
|
||||
if (suppress_non_delimited)
|
||||
FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\
|
||||
\tonly when operating on fields"));
|
||||
}
|
||||
|
||||
set_fields (spec_list_string,
|
||||
( (operating_mode == field_mode) ? 0 : SETFLD_ERRMSG_USE_POS)
|
||||
| (complement ? SETFLD_COMPLEMENT : 0) );
|
||||
((byte_mode ? SETFLD_ERRMSG_USE_POS : 0)
|
||||
| (complement ? SETFLD_COMPLEMENT : 0)));
|
||||
|
||||
if (!delim_specified)
|
||||
delim = '\t';
|
||||
|
||||
if (output_delimiter_string == NULL)
|
||||
{
|
||||
static char dummy[2];
|
||||
dummy[0] = delim;
|
||||
dummy[1] = '\0';
|
||||
output_delimiter_string = dummy;
|
||||
output_delimiter_default[0] = delim;
|
||||
output_delimiter_string = output_delimiter_default;
|
||||
output_delimiter_length = 1;
|
||||
}
|
||||
|
||||
void (*cut_stream) (FILE *) = byte_mode ? cut_bytes : cut_fields;
|
||||
if (optind == argc)
|
||||
ok = cut_file ("-");
|
||||
ok = cut_file ("-", cut_stream);
|
||||
else
|
||||
for (ok = true; optind < argc; optind++)
|
||||
ok &= cut_file (argv[optind]);
|
||||
ok &= cut_file (argv[optind], cut_stream);
|
||||
|
||||
|
||||
if (have_read_stdin && fclose (stdin) == EOF)
|
||||
|
||||
Reference in New Issue
Block a user