From f71e04ab70ae8fc7eeca70e90828bfb1e4fbaa01 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Thu, 22 Aug 2019 21:46:18 +0200 Subject: Document config file usage --- colorize.1 | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'colorize.1') diff --git a/colorize.1 b/colorize.1 index f3341ed..b065b64 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2019-02-23" "colorize v0.64" "User Commands" +.TH COLORIZE 1 "2019-08-22" "colorize v0.64" "User Commands" .SH NAME colorize \- colorize text on terminal with ANSI escape sequences .SH SYNOPSIS @@ -51,6 +51,57 @@ show help screen and exit .TP .BR \-V ", " \-\-version display version data and exit +.SH FILES +.TP +.B ~/.colorize.conf +user configuration file +.PP +.RS +If the aforementioned file exists, it is read, parsed and processed +prior to handling the command-line options. Command-line options +override configuration values, but are currently not capable of +unsetting them. If unsetting is desired, comment them out or remove +them. +.RE +.PP +.RS +.B Sample configuration file +.RS +.nf +# ~/.colorize.conf +attr = bold,underscore +color = magenta # favorite one +exclude-random = black +omit-color-empty = yes +.fi +.RE +.RE +.PP +.RS +.B Configurable options and values +.RS +.nf +attr (values same as command-line option) +color (value same as command-line colors) +exclude-random (value same as command-line option) +omit-color-empty (yes/no) +.fi +.RE +.RE +.PP +.RS +.B Syntax +.RS +Each line ought to start with a name of an option, followed by = and +an optional value. Leaving the value blank, unsets the option. +.PP +Whitespace is allowed before the option name, after the option name, +before the option value and after the option value. +.PP +Comments may be placed before or after the option is set. Everything +following the '#' sign is treated as being commented out. +.RE +.RE .SH NOTES The list of color escape sequence codes being emitted and omitted is as follows: -- cgit v1.2.3 From 0cece912fc4a5d40bceb42ba9478539cc1e102b8 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Tue, 27 Aug 2019 22:44:54 +0200 Subject: Introduce --config --- colorize.1 | 5 ++- colorize.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 96 insertions(+), 24 deletions(-) (limited to 'colorize.1') diff --git a/colorize.1 b/colorize.1 index b065b64..45a3ef0 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2019-08-22" "colorize v0.64" "User Commands" +.TH COLORIZE 1 "2019-08-27" "colorize v0.64" "User Commands" .SH NAME colorize \- colorize text on terminal with ANSI escape sequences .SH SYNOPSIS @@ -40,6 +40,9 @@ clean text from color escape sequences emitted by colorize .BR \-\-clean\-all clean text from all valid color escape sequences .TP +.BR \-\-config=\fIPATH\fR +alternate configuration file location +.TP .BR \-\-exclude\-random=\fICOLOR\fR text color to be excluded when selecting a random foreground color .TP diff --git a/colorize.c b/colorize.c index 854581b..dbeed25 100644 --- a/colorize.c +++ b/colorize.c @@ -37,6 +37,7 @@ #include #include #include +#include #ifndef DEBUG # define DEBUG 0 @@ -216,10 +217,22 @@ static const struct { { bg_colors, COUNT_OF (bg_colors, struct color), "background" }, }; +static unsigned int opts_set; +enum { + OPT_ATTR_SET = 0x01, + OPT_EXCLUDE_RANDOM_SET = 0x02, + OPT_OMIT_COLOR_EMPTY_SET = 0x04 +}; +static struct { + char *attr; + char *exclude_random; +} opts_arg = { NULL, NULL }; + enum { OPT_ATTR = 1, OPT_CLEAN, OPT_CLEAN_ALL, + OPT_CONFIG, OPT_EXCLUDE_RANDOM, OPT_OMIT_COLOR_EMPTY, OPT_HELP, @@ -230,6 +243,7 @@ static const struct option long_opts[] = { { "attr", required_argument, &opt_type, OPT_ATTR }, { "clean", no_argument, &opt_type, OPT_CLEAN }, { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL }, + { "config", required_argument, &opt_type, OPT_CONFIG }, { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM }, { "omit-color-empty", no_argument, &opt_type, OPT_OMIT_COLOR_EMPTY }, { "help", no_argument, &opt_type, OPT_HELP }, @@ -270,13 +284,14 @@ static const char *program_name; #if DEBUG static void print_tstamp (FILE *); #endif -static void process_opts (int, char **); +static void process_opts (int, char **, char **); static void process_opt_attr (const char *, const bool); static void write_attr (const struct attr *, unsigned int *, const bool); static void process_opt_exclude_random (const char *, const bool); static void parse_conf (const char *, struct conf *); static void assign_conf (const char *, struct conf *, const char *, char *); static void init_conf_vars (const struct conf *); +static void init_opts_vars (void); static void print_hint (void); static void print_help (void); static void print_version (void); @@ -319,6 +334,7 @@ static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int); static void free_wrap (void **); static char *strdup_wrap (const char *, const char *, unsigned int); static char *str_concat_wrap (const char *, const char *, const char *, unsigned int); +static char *expand_string (const char *); static bool get_bytes_size (unsigned long, struct bytes_size *); static char *get_file_type (mode_t); static bool has_color_name (const char *, const char *); @@ -342,11 +358,7 @@ main (int argc, char **argv) const char *file = NULL; - char *conf_file; - uid_t uid; - struct passwd *passwd; - size_t size; - + char *conf_file = NULL; struct conf config = { NULL, NULL, NULL, NULL }; program_name = argv[0]; @@ -361,22 +373,43 @@ main (int argc, char **argv) attr[0] = '\0'; + process_opts (argc, argv, &conf_file); + #ifdef CONF_FILE_TEST conf_file = to_str (CONF_FILE_TEST); #elif !defined(TEST) - uid = getuid (); - errno = 0; - if ((passwd = getpwuid (uid)) == NULL) + if (conf_file == NULL) { - if (errno == 0) - vfprintf_diag ("password file entry for uid %lu not found", (unsigned long)uid); - else - perror ("getpwuid"); - exit (EXIT_FAILURE); + uid_t uid; + struct passwd *passwd; + size_t size; + + uid = getuid (); + errno = 0; + if ((passwd = getpwuid (uid)) == NULL) + { + if (errno == 0) + vfprintf_diag ("password file entry for uid %lu not found", (unsigned long)uid); + else + perror ("getpwuid"); + exit (EXIT_FAILURE); + } + size = strlen (passwd->pw_dir) + 1 + strlen (CONF_FILE) + 1; + conf_file = xmalloc (size); + snprintf (conf_file, size, "%s/%s", passwd->pw_dir, CONF_FILE); + } + else + { + char *s; + if ((s = expand_string (conf_file))) + { + free (conf_file); + conf_file = s; + } + errno = 0; + if (access (conf_file, F_OK) == -1) + vfprintf_fail (formats[FMT_FILE], conf_file, strerror (errno)); } - size = strlen (passwd->pw_dir) + 1 + strlen (CONF_FILE) + 1; - conf_file = xmalloc (size); - snprintf (conf_file, size, "%s/%s", passwd->pw_dir, CONF_FILE); #endif #if defined(CONF_FILE_TEST) || !defined(TEST) if (access (conf_file, F_OK) != -1) @@ -387,7 +420,7 @@ main (int argc, char **argv) #endif init_conf_vars (&config); - process_opts (argc, argv); + init_opts_vars (); arg_cnt = argc - optind; @@ -466,7 +499,7 @@ print_tstamp (FILE *log) extern char *optarg; static void -process_opts (int argc, char **argv) +process_opts (int argc, char **argv, char **conf_file) { int opt; while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1) @@ -477,8 +510,8 @@ process_opts (int argc, char **argv) switch (opt_type) { case OPT_ATTR: - attr[0] = '\0'; /* Clear attr string to discard values from the config file. */ - process_opt_attr (optarg, true); + opts_set |= OPT_ATTR_SET; + opts_arg.attr = xstrdup (optarg); break; case OPT_CLEAN: clean = true; @@ -486,11 +519,15 @@ process_opts (int argc, char **argv) case OPT_CLEAN_ALL: clean_all = true; break; + case OPT_CONFIG: + *conf_file = xstrdup (optarg); + break; case OPT_EXCLUDE_RANDOM: - process_opt_exclude_random (optarg, true); + opts_set |= OPT_EXCLUDE_RANDOM_SET; + opts_arg.exclude_random = xstrdup (optarg); break; case OPT_OMIT_COLOR_EMPTY: - omit_color_empty = true; + opts_set |= OPT_OMIT_COLOR_EMPTY_SET; break; case OPT_HELP: PRINT_HELP_EXIT (); @@ -604,6 +641,23 @@ process_opt_exclude_random (const char *s, const bool is_opt) is_opt ? "--exlude-random switch" : "exclude-random conf option"); } +static void +init_opts_vars (void) +{ + if (opts_set & OPT_ATTR_SET) + { + attr[0] = '\0'; /* Clear attr string to discard values from the config file. */ + process_opt_attr (opts_arg.attr, true); + } + if (opts_set & OPT_EXCLUDE_RANDOM_SET) + process_opt_exclude_random (opts_arg.exclude_random, true); + if (opts_set & OPT_OMIT_COLOR_EMPTY_SET) + omit_color_empty = true; + + free (opts_arg.attr); + free (opts_arg.exclude_random); +} + #define IS_SPACE(c) ((c) == ' ' || (c) == '\t') static void @@ -743,6 +797,7 @@ print_help (void) }; const struct opt_data opts_data[] = { { "attr", NULL, "=ATTR1,ATTR2,..." }, + { "config", NULL, "=PATH" }, { "exclude-random", NULL, "=COLOR" }, { "help", "h", NULL }, { "version", "V", NULL }, @@ -1664,6 +1719,20 @@ str_concat_wrap (const char *str1, const char *str2, const char *file, unsigned return str; } +static char * +expand_string (const char *str) +{ + char *s = NULL; + wordexp_t p; + + wordexp (str, &p, 0); + if (p.we_wordc >= 1) + s = xstrdup (p.we_wordv[0]); + wordfree (&p); + + return s; +} + static bool get_bytes_size (unsigned long bytes, struct bytes_size *bytes_size) { -- cgit v1.2.3 From d04e14ae7bef2af9121bc4880e36128acd3dd8ed Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 1 Sep 2019 15:51:59 +0200 Subject: Add config short option -c --- colorize.1 | 4 ++-- colorize.c | 20 ++++++++++++++------ t/conf/param.t | 18 +++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) (limited to 'colorize.1') diff --git a/colorize.1 b/colorize.1 index 45a3ef0..02ffe2e 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2019-08-27" "colorize v0.64" "User Commands" +.TH COLORIZE 1 "2019-09-01" "colorize v0.64" "User Commands" .SH NAME colorize \- colorize text on terminal with ANSI escape sequences .SH SYNOPSIS @@ -40,7 +40,7 @@ clean text from color escape sequences emitted by colorize .BR \-\-clean\-all clean text from all valid color escape sequences .TP -.BR \-\-config=\fIPATH\fR +.BR \-c ", " \-\-config=\fIPATH\fR alternate configuration file location .TP .BR \-\-exclude\-random=\fICOLOR\fR diff --git a/colorize.c b/colorize.c index bf7e3d6..147e684 100644 --- a/colorize.c +++ b/colorize.c @@ -488,6 +488,10 @@ print_tstamp (FILE *log) } #endif +#define DUP_CONFIG() \ + *conf_file = xstrdup (optarg); \ + break; + #define PRINT_HELP_EXIT() \ print_help (); \ exit (EXIT_SUCCESS); @@ -502,7 +506,7 @@ static void process_opts (int argc, char **argv, char **conf_file) { int opt; - while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1) + while ((opt = getopt_long (argc, argv, "c:hV", long_opts, NULL)) != -1) { switch (opt) { @@ -520,8 +524,7 @@ process_opts (int argc, char **argv, char **conf_file) clean_all = true; break; case OPT_CONFIG: - *conf_file = xstrdup (optarg); - break; + DUP_CONFIG (); case OPT_EXCLUDE_RANDOM: opts_set |= OPT_EXCLUDE_RANDOM_SET; opts_arg.exclude_random = xstrdup (optarg); @@ -537,6 +540,8 @@ process_opts (int argc, char **argv, char **conf_file) ABORT_TRACE (); } break; + case 'c': + DUP_CONFIG (); case 'h': PRINT_HELP_EXIT (); case 'V': @@ -799,7 +804,7 @@ print_help (void) }; const struct opt_data opts_data[] = { { "attr", NULL, "=ATTR1,ATTR2,..." }, - { "config", NULL, "=PATH" }, + { "config", "c", "=PATH" }, { "exclude-random", NULL, "=COLOR" }, { "help", "h", NULL }, { "version", "V", NULL }, @@ -839,9 +844,12 @@ print_help (void) if (opt_data) { if (opt_data->short_opt) - printf ("\t\t-%s, --%s\n", opt_data->short_opt, opt->name); + printf ("\t\t-%s, --%s", opt_data->short_opt, opt->name); else - printf ("\t\t --%s%s\n", opt->name, opt_data->arg); + printf ("\t\t --%s", opt->name); + if (opt_data->arg) + printf ("%s", opt_data->arg); + printf ("\n"); } else printf ("\t\t --%s\n", opt->name); diff --git a/t/conf/param.t b/t/conf/param.t index 3fe5436..520df2b 100755 --- a/t/conf/param.t +++ b/t/conf/param.t @@ -8,7 +8,7 @@ use Colorize::Common qw(:defaults $write_to_tmpfile); use File::Temp qw(tmpnam); use Test::More; -my $tests = 1; +my $tests = 2; my $conf = <<'EOT'; attr=bold @@ -16,6 +16,14 @@ color=blue omit-color-empty=yes EOT +my $expected = <<"EOT"; +\e[1;34mfoo\e[0m + +\e[1;34mbar\e[0m + +\e[1;34mbaz\e[0m +EOT + plan tests => $tests; SKIP: { @@ -35,13 +43,9 @@ EOT print {$fh} $conf; close($fh); - is(qx($program --config=$conf_file $infile), <<"EOT", 'config param'); -\e[1;34mfoo\e[0m - -\e[1;34mbar\e[0m + is(qx($program -c $conf_file $infile), $expected, 'short option'); + is(qx($program --config=$conf_file $infile), $expected, 'long option'); -\e[1;34mbaz\e[0m -EOT unlink $program; unlink $conf_file; } -- cgit v1.2.3