coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] scripts: rewrite dcgen in shellscript


From: Christopher Bayliss
Subject: [PATCH] scripts: rewrite dcgen in shellscript
Date: Mon, 27 May 2024 07:09:48 +1000

* this makes dcgen work when building corutils on a system without perl

* I also added comments to help the next person who looks at this file

* there is a minor difference in the output between the original dcgen
  and this version, lines that start with a [:blank:] don't have that
  blank in the dircolors header. In practice this means that the line
  starting with '# numerical' doesn't have one space at the start, this
  obviously won't negatively affect dircolors.

* in this situation, the shellcheck warnings SC1003 and SC1143 are false
  positives, remove them to see for yourself. :)

Signed-off-by: Christopher Bayliss <cjbdev@icloud.com>
---
 src/dcgen    | 61 +++++++++++++++++++++-------------------------------
 src/local.mk |  3 +--
 2 files changed, 25 insertions(+), 39 deletions(-)

diff --git a/src/dcgen b/src/dcgen
index 957427370..3e9db50ee 100755
--- a/src/dcgen
+++ b/src/dcgen
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/bin/sh
 # dcgen -- convert dircolors.hin to dircolors.h.
 
 # Copyright (C) 1996-2024 Free Software Foundation, Inc.
@@ -16,40 +16,27 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-# written by Jim Meyering
+# written by Christopher Bayliss
+# based on the original perl script by Jim Meyering
 
-require 5.002;
-use strict;
-(my $ME = $0) =~ s|.*/||;
-
-# A global destructor to close standard output with error checking.
-sub END
-{
-  defined fileno STDOUT
-    or return;
-  close STDOUT
-    and return;
-  warn "$ME: closing standard output: $!\n";
-  $? ||= 1;
-}
-
-my @line;
-while (<>)
-  {
-    chomp;
-    s/[[:blank:]]+/ /g;
-    $_
-      and push @line, $_;
-  }
-
-my $indent = '  ';
-
-print "static char const G_line[] =\n{\n";
-foreach (@line)
-  {
-    s/./'$&',/g;
-    s/'\\'/'\\\\'/g;
-    s/'''/'\\''/g;
-    print "$indent${_}0,\n";
-  }
-print "};\n";
+echo "static char const G_line[] ="
+echo "{"
+# shellcheck disable=SC1003,SC1143
+while read -r line;
+  do echo "$line" |\
+    # match multiple blanks and compact them down to one space
+    sed 's/[[:blank:]]\+/ /g' |\
+    # match each character and wrap it like: '<char>',
+    sed "s/./'&',/g" |\
+    # delete empty lines
+    sed '/^$/d' |\
+    # escape backslashes like: \\
+    sed 's/''\\''/''\\\\''/g' |\
+    # escape single quotes inside single quotes like so: '\''
+    sed "s/'''/'\\\''/g" |\
+    # indent the lines with two spaces
+    sed 's/^/  /g' |\
+    # add a zero terminator for each line
+    sed 's/$/0,/'
+done < "$1"
+echo "};"
diff --git a/src/local.mk b/src/local.mk
index 8133925ac..e78ee0389 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -525,8 +525,7 @@ BUILT_SOURCES += src/dircolors.h
 src/dircolors.h: src/dcgen src/dircolors.hin
        $(AM_V_GEN)rm -f $@ $@-t
        $(AM_V_at)${MKDIR_P} src
-       $(AM_V_at)$(PERL) -w -- $(srcdir)/src/dcgen \
-                               $(srcdir)/src/dircolors.hin > $@-t
+       $(AM_V_at)$(srcdir)/src/dcgen $(srcdir)/src/dircolors.hin > $@-t
        $(AM_V_at)chmod a-w $@-t
        $(AM_V_at)mv $@-t $@
 
-- 
2.44.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]