]> git.refcnt.org Git - colorize.git/commitdiff
parse_conf(): minor tweaks
authorSteven Schubiger <stsc@refcnt.org>
Tue, 17 Sep 2019 21:14:18 +0000 (23:14 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Tue, 17 Sep 2019 21:14:18 +0000 (23:14 +0200)
- Parse lines with CRLF endings
- Separate the '#' sign from the option name

colorize.c
t/conf/endings.t [new file with mode: 0755]
t/conf/parse/fail.t

index 147e684a3c30aef2c3c9ca3d19975837116bc2ce..753f7490ca0712a0770c0251a6e7ee426184bb62 100644 (file)
@@ -681,9 +681,11 @@ parse_conf (const char *conf_file, struct conf *config)
         char *p;
 
         cnt++;
+        if ((p = strchr (line, '\r')) && *(p + 1) != '\n')
+          vfprintf_fail ("%s: CR ending of line %u is not supported, switch to CRLF/LF instead", conf_file, cnt);
         if (strlen (line) > (sizeof (line) - 2))
           vfprintf_fail ("%s: line %u exceeds maximum of %u characters", conf_file, cnt, (unsigned int)(sizeof (line) - 2));
-        if ((p = strrchr (line, '\n')))
+        if ((p = strpbrk (line, "\n\r")))
           *p = '\0';
 /* NAME PARSING (start) */
         p = line;
@@ -696,9 +698,9 @@ parse_conf (const char *conf_file, struct conf *config)
         opt = p;
         if (!(assign = strchr (opt, '='))) /* check for = */
           {
-            char *space;
-            if ((space = strchr (opt, ' ')))
-              *space = '\0';
+            char *s;
+            if ((s = strpbrk (opt, "# ")))
+              *s = '\0';
             vfprintf_fail (formats[FMT_CONF], conf_file, opt, "not followed by =");
           }
         p = assign;
diff --git a/t/conf/endings.t b/t/conf/endings.t
new file mode 100755 (executable)
index 0000000..35abe20
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use lib qw(lib);
+
+use Colorize::Common qw(:defaults $write_to_tmpfile);
+use File::Temp qw(tmpnam);
+use Test::More;
+
+my $tests = 1;
+
+my $conf = <<'EOT';
+attr=reverse
+color=red
+EOT
+
+plan tests => $tests;
+
+SKIP: {
+    my $program = tmpnam();
+    my $conf_file = tmpnam();
+
+    skip 'compiling failed (endings)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0;
+
+    my $infile = $write_to_tmpfile->('foo');
+
+    open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n";
+    print {$fh} join "\015\012", split /\n/, $conf;
+    close($fh);
+
+    is(qx($program $infile), "\e[7;31mfoo\e[0m", 'CRLF line endings');
+
+    unlink $program;
+    unlink $conf_file;
+}
index ea267f82fd3164bc5849d7c59bded808a117c2cf..d2988e496f4be49161780292cd1295c525ec6c5a 100755 (executable)
@@ -12,7 +12,7 @@ use IPC::Open3 qw(open3);
 use Symbol qw(gensym);
 use Test::More;
 
-my $tests = 8;
+my $tests = 9;
 
 my $run_program_fail = sub
 {
@@ -47,6 +47,7 @@ SKIP: {
         [ 'exclude-random1=black', 'option \'exclude-random1\' not recognized'                 ],
         [ 'omit-color-empty1=yes', 'option \'omit-color-empty1\' not recognized'               ],
         [ 'attr',                  'option \'attr\' not followed by ='                         ],
+        [ 'attr#',                 'option \'attr\' not followed by ='                         ],
         [ 'attr bold',             'option \'attr\' not followed by ='                         ],
         [ "color=$chars_exceed",   'line 1 exceeds maximum of'                                 ],
     );