From 3c9f3e036bf6a40bb1055cb41e94583189678e7d Mon Sep 17 00:00:00 2001 From: aland Date: Mon, 8 Aug 2005 20:22:14 +0000 Subject: [PATCH] Script to format dictionaries in a consistent manner, to avoid hand-formatting issues --- share/Makefile | 15 +++++ share/format.pl | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 share/Makefile create mode 100755 share/format.pl diff --git a/share/Makefile b/share/Makefile new file mode 100644 index 0000000..ab342b4 --- /dev/null +++ b/share/Makefile @@ -0,0 +1,15 @@ +# +# Scripts to format dictionary files. +# +# $Id$ +# +.PHONY: format + +# +# This should only be run by hand, and then sanity checked by hand! +# +format: dictionary* + @for x in dictionary* ; do \ + cat $$x | ./format.pl > tmp; \ + mv tmp $$x; \ + done diff --git a/share/format.pl b/share/format.pl new file mode 100755 index 0000000..be4caf8 --- /dev/null +++ b/share/format.pl @@ -0,0 +1,176 @@ +#!/usr/bin/env perl +# +# Format the dictionaries according to a standard scheme. +# +# Usage: cat dictionary | ./format.pl > new +# +# We don't over-write the dictionaries in place, so that the process +# can be double-checked by hand. +# +# This is a bit of a hack. +# +# FIXME: get lengths from variables, rather than hard-coding. +# +# $Id$ +# + +$begin_vendor = 0; +$blank = 0; + +while (<>) { + # + # Clear out trailing whitespace + # + s/[ \t]+$//; + + # + # Suppress multiple blank lines + # + if (/^\s+$/) { + next if ($blank == 1); + $blank = 1; + print "\n"; + next; + } + $blank = 0; + + # + # Remember the vendor + # + if (/^VENDOR\s+([\w-]+)\s+(\d+)/) { + $name=$1; + $len = length $name; + if ($len < 32) { + $lenx = 31 - $len; + $lenx += 7; # round up + $lenx /= 8; + $lenx = int $lenx; + $tabs = "\t" x $lenx; + } else { + $tabs = " "; + } + print "VENDOR\t$name$tabs$2\n"; + $vendor = $name; + next; + } + + # + # Remember if we did begin-vendor. + # + if (/^BEGIN-VENDOR\s+([\w-]+)/) { + $begin_vendor = 1; + print "BEGIN-VENDOR\t$vendor\n"; + next; + } + + # + # Get attribute. + # + if (/^ATTRIBUTE\s+([\w-]+)\s+(\d+)\s+(\w+)(.*)/) { + $name=$1; + $len = length $name; + if ($len < 40) { + $lenx = 40 - $len; + $lenx += 7; # round up + $lenx /= 8; + $lenx = int $lenx; + $tabs = "\t" x $lenx; + if ($tabs eq "") { + $tabs = " "; + } + } else { + $tabs = " "; + } + + $value = $2; + $type = $3; + $stuff = $4; + + # + # See if it's old format, with the vendor at the end of + # the line. If so, make it the new format. + # + if ($stuff =~ /$vendor/) { + if ($begin_vendor == 0) { + print "BEGIN-VENDOR\t$vendor\n\n"; + $begin_vendor = 1; + } + $stuff =~ s/$vendor//; + $stuff =~ s/\s+$//; + } + + print "ATTRIBUTE\t$name$tabs$value\t$type$stuff\n"; + next; + } + + # + # Values. + # + if (/^VALUE\s+([\w-]+)\s+([\w-]+)\s+(\d+)/) { + $attr=$1; + $len = length $attr; + if ($len < 32) { + $lenx = 32 - $len; + $lenx += 7; # round up + $lenx /= 8; + $lenx = int $lenx; + $tabsa = "\t" x $lenx; + if ($tabsa eq "") { + $tabsa = " "; + $len += 1; + } else { + $len -= $len % 8; + $len += 8 * length $tabsa; + } + } else { + $tabsa = " "; + $len += 1; + } + + # + # For the code below, we assume that the attribute lengths + # + if ($len < 32) { + $lena = 0; + } else { + $lena = $len - 32; + } + + $name=$2; + $len = length $name; + if ($len < 24) { + $lenx = 24 - $lena - $len; + $lenx += 7; # round up + $lenx /= 8; + $lenx = int $lenx; + $tabsn = "\t" x $lenx; + if ($tabsn eq "") { + $tabsn = " "; + } + } else { + $tabsn = " "; + } + + print "VALUE\t$attr$tabsa$name$tabsn$3\n"; + next; + } + + # + # Remember if we did this. + # + if (/^END-VENDOR/) { + $begin_vendor = 0; + } + + # + # Everything else gets dumped out as-is. + # + print; +} + +# +# If we changed the format, print the end vendor, too. +# +if ($begin_vendor) { + print "\nEND-VENDOR\t$vendor\n"; +} -- 2.1.4