/* 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. */
/* 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;
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;
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))
{
}
}
-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;
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]);
atexit (close_stdout);
- operating_mode = undefined_mode;
-
/* By default, all non-delimited lines are printed. */
suppress_non_delimited = false;
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;
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':
}
}
- 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)