From 3582f0672895201b3c460c4aeec9fa7b8ee7480f Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Fri, 24 Jul 2015 20:44:43 +0200 Subject: Move option processing code to a function --- colorize.c | 127 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 67 insertions(+), 60 deletions(-) (limited to 'colorize.c') diff --git a/colorize.c b/colorize.c index abeb665..498e566 100644 --- a/colorize.c +++ b/colorize.c @@ -201,6 +201,7 @@ static char *exclude; static const char *program_name; +static void process_opts (int, char **); static void print_hint (void); static void print_help (void); static void print_version (void); @@ -245,21 +246,81 @@ static void vfprintf_fail (const char *, ...); static void stack_var (void ***, unsigned int *, unsigned int, void *); static void release_var (void **, unsigned int, void **); +extern int optind; + +int +main (int argc, char **argv) +{ + unsigned int arg_cnt = 0; + + bool bold = false; + + const struct color *colors[2] = { + NULL, /* foreground */ + NULL, /* background */ + }; + + const char *file = NULL; + + program_name = argv[0]; + atexit (cleanup); + + setvbuf (stdout, NULL, _IOLBF, 0); + +#if DEBUG + log = open_file (DEBUG_FILE, "w"); +#endif + + process_opts (argc, argv); + + arg_cnt = argc - optind; + + if (clean || clean_all) + { + if (clean && clean_all) + vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive"); + if (arg_cnt > 1) + { + const char *format = "%s %s"; + const char *message = "switch cannot be used with more than one file"; + if (clean) + vfprintf_fail (format, "--clean", message); + else if (clean_all) + vfprintf_fail (format, "--clean-all", message); + } + } + else + { + if (arg_cnt == 0 || arg_cnt > 2) + { + vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt); + print_hint (); + exit (EXIT_FAILURE); + } + } + + if (clean || clean_all) + process_file_arg (argv[optind], &file, &stream); + else + process_args (arg_cnt, &argv[optind], &bold, colors, &file, &stream); + read_print_stream (bold, colors, file, stream); + + RELEASE_VAR (exclude); + + exit (EXIT_SUCCESS); +} + #define SET_OPT_TYPE(type) \ opt_type = type; \ opt = 0; \ goto PARSE_OPT; \ extern char *optarg; -extern int optind; - static int opt_type; -int -main (int argc, char **argv) +static void +process_opts (int argc, char **argv) { - unsigned int arg_cnt = 0; - enum { OPT_CLEAN = 1, OPT_CLEAN_ALL, @@ -278,24 +339,6 @@ main (int argc, char **argv) { NULL, 0, NULL, 0 }, }; - bool bold = false; - - const struct color *colors[2] = { - NULL, /* foreground */ - NULL, /* background */ - }; - - const char *file = NULL; - - program_name = argv[0]; - atexit (cleanup); - - setvbuf (stdout, NULL, _IOLBF, 0); - -#if DEBUG - log = open_file (DEBUG_FILE, "w"); -#endif - while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1) { PARSE_OPT: @@ -349,42 +392,6 @@ main (int argc, char **argv) ABORT_TRACE (); } } - - arg_cnt = argc - optind; - - if (clean || clean_all) - { - if (clean && clean_all) - vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive"); - if (arg_cnt > 1) - { - const char *format = "%s %s"; - const char *message = "switch cannot be used with more than one file"; - if (clean) - vfprintf_fail (format, "--clean", message); - else if (clean_all) - vfprintf_fail (format, "--clean-all", message); - } - } - else - { - if (arg_cnt == 0 || arg_cnt > 2) - { - vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt); - print_hint (); - exit (EXIT_FAILURE); - } - } - - if (clean || clean_all) - process_file_arg (argv[optind], &file, &stream); - else - process_args (arg_cnt, &argv[optind], &bold, colors, &file, &stream); - read_print_stream (bold, colors, file, stream); - - RELEASE_VAR (exclude); - - exit (EXIT_SUCCESS); } static void -- cgit v1.2.3