17c478bd9Sstevel@tonic-gate#!/usr/bin/perl -w 27c478bd9Sstevel@tonic-gate# 37c478bd9Sstevel@tonic-gate# CDDL HEADER START 47c478bd9Sstevel@tonic-gate# 57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the 6*627351e3Scy152378# Common Development and Distribution License (the "License"). 7*627351e3Scy152378# You may not use this file except in compliance with the License. 87c478bd9Sstevel@tonic-gate# 97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate# and limitations under the License. 137c478bd9Sstevel@tonic-gate# 147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate# 207c478bd9Sstevel@tonic-gate# CDDL HEADER END 217c478bd9Sstevel@tonic-gate# 227c478bd9Sstevel@tonic-gate# ident "%Z%%M% %I% %E% SMI" 237c478bd9Sstevel@tonic-gate# 24*627351e3Scy152378# Copyright 2008 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate# Use is subject to license terms. 267c478bd9Sstevel@tonic-gate# 277c478bd9Sstevel@tonic-gate# 287c478bd9Sstevel@tonic-gate# dictck -- Sanity check a .dict file and optionally the corresponding .po file 297c478bd9Sstevel@tonic-gate# 307c478bd9Sstevel@tonic-gate# example: dickck FMD.dict FMD.po 317c478bd9Sstevel@tonic-gate# 327c478bd9Sstevel@tonic-gate# usage: dickck [-vp] [ -b buildcode ] dictfile [ pofile ] 337c478bd9Sstevel@tonic-gate# 347c478bd9Sstevel@tonic-gate# -b specify location of "buildcode" command 357c478bd9Sstevel@tonic-gate# 367c478bd9Sstevel@tonic-gate# -p print a .po file template to stdout, based on dictfile given 377c478bd9Sstevel@tonic-gate# 387c478bd9Sstevel@tonic-gate# -v verbose, show how code is assembled 397c478bd9Sstevel@tonic-gate# 407c478bd9Sstevel@tonic-gate# Note: this program requires the "buildcode" program in your search path. 417c478bd9Sstevel@tonic-gate# 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gateuse strict; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gateuse Getopt::Std; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gateuse vars qw($opt_b $opt_p $opt_v); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gatemy $Myname = $0; # save our name for error messages 507c478bd9Sstevel@tonic-gate$Myname =~ s,.*/,,; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate$SIG{HUP} = $SIG{INT} = $SIG{TERM} = $SIG{__DIE__} = sub { 537c478bd9Sstevel@tonic-gate # although fatal, we prepend "WARNING:" to make sure the 547c478bd9Sstevel@tonic-gate # commonly-used "nightly" script flags this as lint on the .dict file 557c478bd9Sstevel@tonic-gate die "$Myname: WARNING: @_"; 567c478bd9Sstevel@tonic-gate}; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate# 597c478bd9Sstevel@tonic-gate# usage -- print a usage message and exit 607c478bd9Sstevel@tonic-gate# 617c478bd9Sstevel@tonic-gatesub usage { 627c478bd9Sstevel@tonic-gate my $msg = shift; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate warn "$Myname: $msg\n" if defined($msg); 657c478bd9Sstevel@tonic-gate warn "usage: $Myname [-pv] [ -b buildcode ] dictfile [ pofile ]\n"; 667c478bd9Sstevel@tonic-gate exit 1; 677c478bd9Sstevel@tonic-gate} 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gatemy %keys2val; 707c478bd9Sstevel@tonic-gatemy %val2keys; 717c478bd9Sstevel@tonic-gatemy %code2val; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gatemy $buildcode = 'buildcode'; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate# 767c478bd9Sstevel@tonic-gate# the "main" for this script... 777c478bd9Sstevel@tonic-gate# 787c478bd9Sstevel@tonic-gategetopts('b:pv') or usage; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gatemy $dictfile = shift; 817c478bd9Sstevel@tonic-gatemy $pofile = shift; 827c478bd9Sstevel@tonic-gateusage unless defined($dictfile); 837c478bd9Sstevel@tonic-gateusage if @ARGV; 847c478bd9Sstevel@tonic-gate$buildcode = $opt_b if defined($opt_b); 857c478bd9Sstevel@tonic-gatedodict($dictfile); 867c478bd9Sstevel@tonic-gatedopo($pofile) if defined($pofile); 877c478bd9Sstevel@tonic-gateexit 0; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate# 907c478bd9Sstevel@tonic-gate# dodict -- load up a .dict file, sanity checking it as we go 917c478bd9Sstevel@tonic-gate# 927c478bd9Sstevel@tonic-gatesub dodict { 937c478bd9Sstevel@tonic-gate my $name = shift; 947c478bd9Sstevel@tonic-gate my $dname; 957c478bd9Sstevel@tonic-gate my $line = 0; 967c478bd9Sstevel@tonic-gate my $lhs; 977c478bd9Sstevel@tonic-gate my $rhs; 987c478bd9Sstevel@tonic-gate my %props; 997c478bd9Sstevel@tonic-gate my $maxkey = 1; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if ($name =~ m,([^/]+)\.dict$,) { 1027c478bd9Sstevel@tonic-gate $dname = $1; 1037c478bd9Sstevel@tonic-gate } else { 1047c478bd9Sstevel@tonic-gate die "dictname \"$name\" not something.dict as expected\n"; 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate open(F, $name) or die "$name: $!\n"; 1087c478bd9Sstevel@tonic-gate print "parsing \"$name\"\n" if $opt_v; 1097c478bd9Sstevel@tonic-gate while (<F>) { 1107c478bd9Sstevel@tonic-gate $line++; 1117c478bd9Sstevel@tonic-gate next if /^\s*#/; 1127c478bd9Sstevel@tonic-gate chomp; 1137c478bd9Sstevel@tonic-gate next if /^\s*$/; 1147c478bd9Sstevel@tonic-gate die "$name:$line: first non-comment line must be FMDICT line\n" 1157c478bd9Sstevel@tonic-gate unless /^FMDICT:/; 1167c478bd9Sstevel@tonic-gate print "FMDICT keyword found on line $line\n" if $opt_v; 1177c478bd9Sstevel@tonic-gate s/FMDICT:\s*//; 1187c478bd9Sstevel@tonic-gate my $s = $_; 1197c478bd9Sstevel@tonic-gate while ($s =~ /^\s*([^=\s]+)(.*)$/) { 1207c478bd9Sstevel@tonic-gate $lhs = $1; 1217c478bd9Sstevel@tonic-gate $rhs = ""; 1227c478bd9Sstevel@tonic-gate $s = $+; 1237c478bd9Sstevel@tonic-gate if ($s =~ /^\s*=\s*(.*)$/) { 1247c478bd9Sstevel@tonic-gate $s = $+; 1257c478bd9Sstevel@tonic-gate die "$name:$line: property \"$lhs\" incomplete\n" 1267c478bd9Sstevel@tonic-gate unless $s ne ""; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate if ($s =~ /^"((?:[^"]|\\")*)"(.*)$/) { 1297c478bd9Sstevel@tonic-gate $s = $+; 1307c478bd9Sstevel@tonic-gate $rhs = $1; 1317c478bd9Sstevel@tonic-gate } else { 1327c478bd9Sstevel@tonic-gate $s =~ /^([^\s]*)(.*)$/; 1337c478bd9Sstevel@tonic-gate $s = $+; 1347c478bd9Sstevel@tonic-gate $rhs = $1; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate $rhs =~ s/\\(.)/dobs($1)/ge; 1377c478bd9Sstevel@tonic-gate $props{$lhs} = $rhs; 1387c478bd9Sstevel@tonic-gate print "property \"$lhs\" value \"$rhs\"\n" if $opt_v; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate last; 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate # check for required headers 1437c478bd9Sstevel@tonic-gate die "$name: no version property in header\n" 1447c478bd9Sstevel@tonic-gate unless defined($props{'version'}); 1457c478bd9Sstevel@tonic-gate die "$name: no name property in header\n" 1467c478bd9Sstevel@tonic-gate unless defined($props{'name'}); 1477c478bd9Sstevel@tonic-gate die "$name: no maxkey property in header\n" 1487c478bd9Sstevel@tonic-gate unless defined($props{'maxkey'}); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate # check version 1517c478bd9Sstevel@tonic-gate die "$name:$line: unexpected version: \"$props{'version'}\"\n" 1527c478bd9Sstevel@tonic-gate unless $props{'version'} eq "1"; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate # check name 1557c478bd9Sstevel@tonic-gate die "$name:$line: name \"$props{'name'}\" doesn't match \"$dname\" from filename\n" 1567c478bd9Sstevel@tonic-gate unless $props{'name'} eq $dname; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate # check format of maxkey (value checked later) 1597c478bd9Sstevel@tonic-gate die "$name:$line: maxkey property must be a number\n" 1607c478bd9Sstevel@tonic-gate unless $props{'maxkey'} =~ /^\d+$/; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate # check for old bits property 1637c478bd9Sstevel@tonic-gate die "$name: obsolete \"bits\" property found in header\n" 1647c478bd9Sstevel@tonic-gate if defined($props{'bits'}); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate # parse entries 1677c478bd9Sstevel@tonic-gate while (<F>) { 1687c478bd9Sstevel@tonic-gate $line++; 1697c478bd9Sstevel@tonic-gate chomp; 1707c478bd9Sstevel@tonic-gate s/#.*//; 1717c478bd9Sstevel@tonic-gate next if /^\s*$/; 1727c478bd9Sstevel@tonic-gate die "$name:$line: malformed entry\n" 1737c478bd9Sstevel@tonic-gate unless /^([^=]+)=(\d+)$/; 1747c478bd9Sstevel@tonic-gate $lhs = $1; 1757c478bd9Sstevel@tonic-gate $rhs = $2; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate # make sure keys are sorted 1787c478bd9Sstevel@tonic-gate my $elhs = join(' ', sort split(/\s/, $lhs)); 1797c478bd9Sstevel@tonic-gate die "$name:$line: keys not in expected format of:\n" . 1807c478bd9Sstevel@tonic-gate " \"$elhs\"\n" 1817c478bd9Sstevel@tonic-gate unless $elhs eq $lhs; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate # check for duplicate or unexpected keys 1847c478bd9Sstevel@tonic-gate my %keys; 1857c478bd9Sstevel@tonic-gate foreach my $e (split(/\s/, $lhs)) { 1867c478bd9Sstevel@tonic-gate die "$name:$line: unknown event type \"$e\"\n" 1877c478bd9Sstevel@tonic-gate unless $e =~ 188*627351e3Scy152378 /^(fault|defect|upset|ereport|list)\..*[^.]$/; 1897c478bd9Sstevel@tonic-gate die "$name:$line: key repeated: \"$e\"\n" 1907c478bd9Sstevel@tonic-gate if defined($keys{$e}); 1917c478bd9Sstevel@tonic-gate $keys{$e} = 1; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate $maxkey = keys(%keys) if $maxkey < keys(%keys); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate die "$name:$line: duplicate entry for keys\n" 1967c478bd9Sstevel@tonic-gate if defined($keys2val{$lhs}); 1977c478bd9Sstevel@tonic-gate die "$name:$line: duplicate entry for value $rhs\n" 1987c478bd9Sstevel@tonic-gate if defined($val2keys{$rhs}); 1997c478bd9Sstevel@tonic-gate $keys2val{$lhs} = $rhs; 2007c478bd9Sstevel@tonic-gate $val2keys{$rhs} = $lhs; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate open(B, "$buildcode $dname $rhs|") or 2037c478bd9Sstevel@tonic-gate die "can't run buildcode: $!\n"; 2047c478bd9Sstevel@tonic-gate my $code = <B>; 2057c478bd9Sstevel@tonic-gate chomp $code; 2067c478bd9Sstevel@tonic-gate close(B); 2077c478bd9Sstevel@tonic-gate print "code: $code keys: $lhs\n" if $opt_v; 2087c478bd9Sstevel@tonic-gate $code2val{$code} = $rhs; 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if ($opt_p) { 2117c478bd9Sstevel@tonic-gate print <<EOF; 2127c478bd9Sstevel@tonic-gate# 2137c478bd9Sstevel@tonic-gate# code: $code 2147c478bd9Sstevel@tonic-gate# keys: $lhs 2157c478bd9Sstevel@tonic-gate# 2167c478bd9Sstevel@tonic-gatemsgid "$code.type" 2177c478bd9Sstevel@tonic-gatemsgstr "XXX" 2187c478bd9Sstevel@tonic-gatemsgid "$code.severity" 2197c478bd9Sstevel@tonic-gatemsgstr "XXX" 2207c478bd9Sstevel@tonic-gatemsgid "$code.description" 2217c478bd9Sstevel@tonic-gatemsgstr "XXX" 2227c478bd9Sstevel@tonic-gatemsgid "$code.response" 2237c478bd9Sstevel@tonic-gatemsgstr "XXX" 2247c478bd9Sstevel@tonic-gatemsgid "$code.impact" 2257c478bd9Sstevel@tonic-gatemsgstr "XXX" 2267c478bd9Sstevel@tonic-gatemsgid "$code.action" 2277c478bd9Sstevel@tonic-gatemsgstr "XXX" 2287c478bd9Sstevel@tonic-gateEOF 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate print "computed maxkey: $maxkey\n" if $opt_v; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate # check maxkey 2357c478bd9Sstevel@tonic-gate die "$name: maxkey too low, should be $maxkey\n" 2367c478bd9Sstevel@tonic-gate if $props{'maxkey'} < $maxkey; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate close(F); 2397c478bd9Sstevel@tonic-gate} 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate# 2427c478bd9Sstevel@tonic-gate# dobs -- handle backslashed sequences 2437c478bd9Sstevel@tonic-gate# 2447c478bd9Sstevel@tonic-gatesub dobs { 2457c478bd9Sstevel@tonic-gate my $s = shift; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate return "\n" if $s eq 'n'; 2487c478bd9Sstevel@tonic-gate return "\r" if $s eq 'r'; 2497c478bd9Sstevel@tonic-gate return "\t" if $s eq 't'; 2507c478bd9Sstevel@tonic-gate return $s; 2517c478bd9Sstevel@tonic-gate} 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate# 2547c478bd9Sstevel@tonic-gate# dopo -- sanity check a po file 2557c478bd9Sstevel@tonic-gate# 2567c478bd9Sstevel@tonic-gatesub dopo { 2577c478bd9Sstevel@tonic-gate my $name = shift; 2587c478bd9Sstevel@tonic-gate my $line = 0; 2597c478bd9Sstevel@tonic-gate my $id; 2607c478bd9Sstevel@tonic-gate my $code; 2617c478bd9Sstevel@tonic-gate my $suffix; 2627c478bd9Sstevel@tonic-gate my %ids; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate open(F, $name) or die "$name: $!\n"; 2657c478bd9Sstevel@tonic-gate print "parsing \"$name\"\n" if $opt_v; 2667c478bd9Sstevel@tonic-gate while (<F>) { 2677c478bd9Sstevel@tonic-gate $line++; 2687c478bd9Sstevel@tonic-gate next if /^\s*#/; 2697c478bd9Sstevel@tonic-gate chomp; 2707c478bd9Sstevel@tonic-gate next if /^\s*$/; 2717c478bd9Sstevel@tonic-gate next unless /^msgid\s*"([^"]+)"$/; 2727c478bd9Sstevel@tonic-gate $id = $1; 2737c478bd9Sstevel@tonic-gate next unless $id =~ 2747c478bd9Sstevel@tonic-gate /^(.*)\.(type|severity|description|response|impact|action)$/; 2757c478bd9Sstevel@tonic-gate $code = $1; 2767c478bd9Sstevel@tonic-gate $suffix = $2; 2777c478bd9Sstevel@tonic-gate die "$name:$line: no dict entry for code \"$code\"\n" 2787c478bd9Sstevel@tonic-gate unless defined($code2val{$code}); 2797c478bd9Sstevel@tonic-gate $ids{$id} = $line; 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate close(F); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate # above checks while reading in file ensured that node code was 2847c478bd9Sstevel@tonic-gate # mentioned in .po file that didn't exist in .dict file. now 2857c478bd9Sstevel@tonic-gate # check the other direction: make sure the full set of entries 2867c478bd9Sstevel@tonic-gate # exist for each code in the .dict file 2877c478bd9Sstevel@tonic-gate foreach $code (sort keys %code2val) { 2887c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.type\"\n" 2897c478bd9Sstevel@tonic-gate unless defined($ids{"$code.type"}); 2907c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.severity\"\n" 2917c478bd9Sstevel@tonic-gate unless defined($ids{"$code.severity"}); 2927c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.description\"\n" 2937c478bd9Sstevel@tonic-gate unless defined($ids{"$code.description"}); 2947c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.response\"\n" 2957c478bd9Sstevel@tonic-gate unless defined($ids{"$code.response"}); 2967c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.impact\"\n" 2977c478bd9Sstevel@tonic-gate unless defined($ids{"$code.impact"}); 2987c478bd9Sstevel@tonic-gate die "$name: missing entry for \"$code.action\"\n" 2997c478bd9Sstevel@tonic-gate unless defined($ids{"$code.action"}); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate} 302