| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | # $Id: gen_commands.pl,v 2.3 2007-11-13 21:54:18 pajas Exp $ | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use XML::LibXML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | if ($ARGV[0]=~'^(-h|--help)?$') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 9 | 0 | print <<EOF; | |||||||||
| 10 | Generates XSH commands specifications RecDescentXML source. | ||||||||||
| 11 | |||||||||||
| 12 | Usage: $0 <source.xml> | ||||||||||
| 13 | |||||||||||
| 14 | EOF | ||||||||||
| 15 | 0 | exit; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 1 | my $parser=XML::LibXML->new(); | |||||||||
| 19 | 1 | $parser->load_ext_dtd(1); | |||||||||
| 20 | 1 | $parser->validation(1); | |||||||||
| 21 | 1 | $parser->complete_attributes(1); | |||||||||
| 22 | 1 | my $doc=$parser->parse_file($ARGV[0]); | |||||||||
| 23 | |||||||||||
| 24 | 1 | my $dom=$doc->getDocumentElement(); | |||||||||
| 25 | 1 | my ($rules)=$dom->findnodes('./rules'); | |||||||||
| 26 | |||||||||||
| 27 | 1 | print "# This file was automatically generated from $ARGV[0] on \n# ",scalar(localtime),"\n"; | |||||||||
| 28 | |||||||||||
| 29 | 1 | print "package XML::XSH2::Functions;\n\n"; | |||||||||
| 30 | 1 | print "%COMMANDS = (\n"; | |||||||||
| 31 | 1 | foreach my $r ($rules->findnodes('./rule[command]')) { | |||||||||
| 32 | 102 | print_command($r); | |||||||||
| 33 | } | ||||||||||
| 34 | 1 | print ");\n\n1;\n"; | |||||||||
| 35 | |||||||||||
| 36 | 1 | exit; | |||||||||
| 37 | |||||||||||
| 38 | ## ================================================ | ||||||||||
| 39 | |||||||||||
| 40 | sub print_command { | ||||||||||
| 41 | 102 | ▶ | my ($rule) = @_; | ||||||||
|
Subroutine
print_command: called CC=9 SCAR=22.0
| |||||||||||
| 42 | 102 | my $name = $rule->getAttribute('name'); | |||||||||
| 43 | 102 | foreach ($rule->findnodes(q{aliases/alias/@name})) { | |||||||||
| 44 | 64 | print "'",$_->value(),"' => '$name',\n"; | |||||||||
| 45 | } | ||||||||||
| 46 | 102 | my ($cmd) = $rule->findnodes(q{command}); | |||||||||
| 47 | 102 | print "'$name' => ["; | |||||||||
| 48 | # print "\\\&",$cmd->getAttribute('func'); | ||||||||||
| 49 | 102 | print "'",$cmd->getAttribute('func'),"'"; | |||||||||
| 50 | 102 | ▶ | print ", ",($cmd->getAttribute('minargs') ne "" ? $cmd->getAttribute('minargs') : 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 102 | ▶ | print ", ",($cmd->getAttribute('maxargs') ne "" ? $cmd->getAttribute('maxargs') : "undef"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 102 | my @params = $cmd->findnodes(q{param}); | |||||||||
| 53 | 102 | ▶ | if (@params) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 32 | print ",\n\t{\n\t"; | |||||||||
| 55 | 32 | foreach my $param (@params) { | |||||||||
| 56 | 115 | ▶ | if ($param->getAttribute('short') ne "") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 114 | print " '".$param->getAttribute('short')."' => '", | |||||||||
| 58 | $param->getAttribute('name') | ||||||||||
| 59 | ,"',\n\t"; | ||||||||||
| 60 | 114 | ▶ | print " '".$param->getAttribute('name')."' => ", | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | ($param->getAttribute('argument') ne "" ? "'".$param->getAttribute('type')."'" | ||||||||||
| 62 | : "''"),",\n\t"; | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | 32 | print "}" | |||||||||
| 66 | } else { | ||||||||||
| 67 | 70 | print ",undef"; | |||||||||
| 68 | } | ||||||||||
| 69 | 102 | ▶ | if ($cmd->getAttribute('extraargs') ne "") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 15 | print ", ".$cmd->getAttribute('extraargs'); | |||||||||
| 71 | } | ||||||||||
| 72 | 102 | print "],\n"; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||