From: Martin Q. <mqu...@us...> - 2006-07-13 18:58:27
|
Update of /cvsroot/flexml/flexml In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv18441 Modified Files: flexml.pl Log Message: Switch to Getopt::Long; allow to override the scanner, header and application names Index: flexml.pl =================================================================== RCS file: /cvsroot/flexml/flexml/flexml.pl,v retrieving revision 1.50 retrieving revision 1.51 diff -u -d -r1.50 -r1.51 --- flexml.pl 31 May 2006 20:02:54 -0000 1.50 +++ flexml.pl 13 Jul 2006 18:58:24 -0000 1.51 @@ -27,7 +27,7 @@ # IMPORTS. -use Getopt::Std; +use Getopt::Long; use LWP::UserAgent; use Carp qw/cluck confess/; @@ -44,7 +44,6 @@ # OPTIONS (and other globals). my $Use; # usage string -my %opt = (); # options my $debug; # -d option flag my $verbose; # -v option flag @@ -53,9 +52,17 @@ my $quiet_parser; # -q option flag my $uri; # -u option uri my $pubid; # -p option string -my $stacksize; # -b option flag -my $tagprefix; # -P option flag -my $actbin; # -T option content +my $stacksize=100000; # -b option flag +my $tagprefix=""; # -P option flag +my $actbin="./flexml-act"; # -T option content + +my $header; # -H option flag/content +my $dummy; # -D option flag/content +my $standalone; # -A option flag/content +my $scanner; # -S option flag/content +my $actions; # -a option content + +my $dryrun; # -n option flag my $dtd; # DTD file name (or URI) my $dtdrevision; # DTD version pruned from file @@ -490,46 +497,63 @@ $Use = "Usage: flexml [-ASHDvdqnLXV] [-s skel] [-T actbin] [-p pubid] [-u uri]\n" . " [-b stack_size] [-r roottags] [-a actions] [-P prefix] name[.dtd]"; -getopts('ASHDvdnLXVqp:b:P:s:T:u:r:a:', \%opt); - -# Version! -print "FleXML version $Id.\n" if $opt{V} or $opt{v}; -exit 0 if $opt{V}; - -# Debugging? -$debug = $opt{d}; -$verbose = $opt{v}; - -# Line numbers? -$lineno = $opt{L}; +sub show_version { -# Quiet parser? -$quiet_parser = $opt{q}; + exit 0; +} -# Exit without fail message? -$nofail = $opt{X}; +Getopt::Long::Configure ("bundling"); +GetOptions( + # Debugging and verbosity + "debug|d" => \$debug, + "verbose|v" => \$verbose, + "quiet|q" => \$quiet_parser, + + # Version! + "version|V" => sub { print "FleXML version $Id.\n"; exit 0; }, -# Specific root tags? -if ($opt{r}) { - for (split ',',$opt{r}) { $roottags{$_} = 'true'; } -} + # dry-run ? + "dry-run|n" => \$dryrun, -# Specific stack size? -$stacksize = ($opt{'b'} ? $opt{'b'} : 100000); + # Line numbers? + "lineno|L" => \$lineno, + + # Exit without fail message? + "nofail|X" => \$nofail, -# Specific tagprefix? -$tagprefix = ($opt{'P'} ? $opt{'P'}."_" : ""); + # Specific root tags? + "root-tags|r=s" => sub { + for (split ',',$_[1]) { $roottags{$_} = 'true'; } + }, -# Specific actbin? -$actbin = ($opt{'T'} ? $opt{'T'} : "./flexml-act"); + # Specific stack size? + "stack-size|b=s" => \$stacksize, -# Set skeleton scanner file name and check it is readable (if needed). -$SKELETON = ($opt{'s'} ? $opt{'s'} : './skel'); -die "$0: No skeleton file $SKELETON.\n" if not -r $SKELETON and $opt{S}; + # Specific tagprefix? + "tag-prefix|P=s" => sub { $tagprefix = $_[1]."_" }, + + # Specific actbin? (internal use) + "act-bin|T=s" => \$actbin, + + # Set skeleton scanner file name and check it is readable (if needed). + "skel|s=s" => sub { + $SKELETON = $_[1]; + die "$0: No skeleton file $SKELETON.\n" if not -r $SKELETON and $_[1]; + }, + + # Set document type URI and PUBID. + "uri|u=s" => \$uri, + "pubid|p=s" => \$pubid, -# Set document type URI and PUBID. -$uri = $opt{u} if $opt{u}; -$pubid = $opt{p} if $opt{p}; + # What to generate + "header|H:s" => sub { $header = $_[1] || 'true' }, + "dummy|D:s" => sub { $dummy = $_[1] || 'true' }, + "stand-alone|A:s" => sub { $standalone = $_[1] || 'true' }, + "scanner|S:s" => sub { $scanner = $_[1] || 'true' }, + "actions|a=s" => \$actions + ); + +print "FleXML version $Id.\n" if $verbose; # Set DTD file name...and extract prefix for later my $prefix = $ARGV[0]; @@ -546,30 +570,30 @@ # Selection options: If none of -SHDA specified then default to -SH. # Furthermore -a implies -D. -$opt{S} = $opt{H} = 'true' unless ($opt{S} or $opt{H} or $opt{D} or $opt{A}); -$opt{D} ||= $opt{a} unless $opt{A}; +$scanner = $header = 'true' unless ($scanner or $header or $dummy or $standalone); +$dummy ||= $actions unless $standalone; # Set default (DTD-based) output file names. -$SCANNER = "$prefix.l"; -$HEADER = "$prefix.h"; -$APPLICATION = "$prefix-dummy.c"; +$SCANNER = (!defined($scanner)) || $scanner eq 'true' ? "$prefix.l" : $scanner; +$HEADER = (!defined($header)) || $header eq 'true' ? "$prefix.h" : $header; +$APPLICATION = (!defined($dummy)) || $dummy eq 'true' ? "$prefix-dummy.c" : $dummy; # Set actions=based output file names, if any. -if ($ACTIONS = $opt{a}) { - $opt{a} =~ s/\.[a-z]+$//; - $APPLICATION = "$opt{a}.c"; +if ($ACTIONS = $actions) { + $actions =~ s/\.[a-z]+$//; + $APPLICATION = "$actions.c"; } # Stand-alone applications... -if ($opt{A}) { - die "$0: -A conflicts with -SHD.\n" if ($opt{S} or $opt{H} or $opt{D}); +if ($standalone) { + die "$0: -A conflicts with -SHD.\n" if ($scanner or $header or $dummy); $SCANNER = $APPLICATION; $SCANNER =~ s/\.c$/.l/; } # Dry-run? -if ($opt{n}) { - $opt{A} = $opt{S} = $opt{H} = $opt{D} = undef; +if ($dryrun) { + $standalone = $scanner = $header = $dummy = undef; } @@ -986,7 +1010,7 @@ } # Handling prefix -if($opt{P}) { +if(length($tagprefix)) { my($h,$k); my(@hashlist) = (\%states,\%emptytrans,\%instates,\%startstate, \%endstates,\%exittrans); @@ -1022,8 +1046,32 @@ return $out; } - print '%opt = (' . printhash(\%opt) . ")\n"; + # display the options we got + print "debug=$debug\n"; + print "verbose=$verbose\n"; + print "quiet=$quiet_parser\n"; + + print "dry-run=$dryrun\n"; + print "lineno=$lineno\n"; + + print "nofail=$nofail\n"; + + print "stack-size=$stacksize\n"; + print "tag-prefix=$tagprefix\n"; + + print "act-bin=$actbin\n"; + + print "skel=$SKELETON\n"; + + print "uri=$uri\n"; + print "pubid=$pubid\n\n"; + print "header=".($header||'undef')."\n"; + print "dummy=".($dummy||'undef')."\n"; + print "stand-alone=".($standalone||'undef')."\n"; + print "scanner=".($scanner||'undef')."\n"; + print "actions=".($actions||'undef')."\n\n"; + print '%source = (' . printhash(\%source) . ")\n"; print "\n"; @@ -1071,7 +1119,7 @@ # WRITE API HEADER (if requested). -if ($opt{H}) { +if ($header) { print STDOUT "Generating XML processor header in `$HEADER'.\n" if $verbose; @@ -1118,10 +1166,10 @@ # WRITE XML PROCESSOR (if requested). -if ($opt{S} or $opt{A}) { +if ($scanner or $standalone) { print STDOUT "Writing XML processor" - . ($opt{a} || $opt{A} ? " and application" : "") + . ($actions || $standalone ? " and application" : "") . " onto `$SCANNER'.\n" if $verbose; open SCANNER, "+>$SCANNER"|| die "$0: cannot write $SCANNER: $!\n"; @@ -1164,7 +1212,7 @@ print "#define FLEXML_BUFFERSTACKSIZE $stacksize\n"; print "\n"; - if ($opt{A}) { + if ($standalone) { api_functions('static ',';'); print "\n"; api_types(); @@ -1174,7 +1222,7 @@ else { print "/* XML processor api. */\n"; print "#include \"$HEADER\"\n\n" - if ($opt{H}); + if ($header); api_data(''); } @@ -1517,7 +1565,7 @@ } close SKELETON || die "$0: Cannot close $SKELETON: $!\n"; - unless ($opt{A}) { + unless ($standalone) { close SCANNER || die "$0: Cannot close $SCANNER: $!\n"; } } @@ -1527,10 +1575,10 @@ # WRITE APPLICATION. -if ($opt{D}) { +if ($dummy) { print STDOUT "Writing XML" - . ($opt{a} ? "" : " dummy") + . ($actions ? "" : " dummy") . " application onto `$APPLICATION'.\n" if $verbose; open APPLICATION, "+>$APPLICATION" || die "$0: Cannot write $APPLICATION: $!\n"; @@ -1552,7 +1600,7 @@ } -if ($opt{D} or $opt{A}) { +if ($dummy or $standalone) { # Get requested actions. if ($ACTIONS) { @@ -1629,6 +1677,7 @@ die "\"$ACTIONS\", line $lineno: Malformed annotation `$&' in <$tag> action.\n" if m|\{[^;\s]+\}|o; } + print STDERR "Action: $_" if $verbose; print $_; } @@ -1641,10 +1690,10 @@ } -if ($opt{D}) { +if ($dummy) { close APPLICATION || die "$0: Cannot close $APPLICATION: $!\n"; } -elsif ($opt{A}) { +elsif ($standalone) { close SCANNER || die "$0: Cannot close $SCANNER: $!\n"; } @@ -1706,21 +1755,21 @@ =over 4 -=item B<-A> +=item B<--stand-alone>, B<-A> Generate a I<stand-alone> scanner application. If combined with B<-a>I<actions> then the application will be named as I<actions> with the extension replaced by F<.l>, otherwise it will be in I<name>F<.l>. Conflicts with B<-S>, B<-H>, and B<-D>. -=item B<-a> I<actions> +=item B<--actions> I<actions>, B<-a> I<actions> Uses the I<actions> file to produce an XML application in the file with the same name as I<actions> after replacing the extension with F<.c>. If combined with B<-A> then instead the stand-alone application will include the action functions. -=item B<-D> +=item B<--dummy>, B<-D> Generate a dummy application I<name>F<-dummy.c> with just empty functions to be called by the XML processor. If combined with @@ -1729,79 +1778,79 @@ F<.c>. Conflicts with B<-A>; implied by B<-a> unless either of B<-SHD> is specified. -=item B<-d> +=item B<--dummy>, B<-d> Turns on debug mode in the flex scanner and also prints out the details of the DTD analysis performed by I<flexml>. -=item B<-H> +=item B<--header>, B<-H> Generate the header file I<name>F<.h>. Conflicts with B<-A>; on by default if none of B<-SHD> specified. -=item B<-L> +=item B<--lineno>, B<-L> Makes the XML processor (as produced by I<flex>(1)) count the lines in the input and keep it available to XML application actions in the integer C<yylineno>. (This is off by default as the performance overhead is significant.) -=item B<-q> +=item B<--quiet>, B<-q> Prevents the XML processor (as produced by I<flex>(1)) from reporting the error it runs into on stderr. Instead, users will have to pool for error messages with the parse_err_msg() function. By default, error messages are written on stderr. -=item B<-n> +=item B<--dry-run>, B<-n> "Dry-run": do not produce any of the output files. -=item B<-p> I<pubid> +=item B<--pubid> I<pubid>, B<-p> I<pubid> Sets the document type to be C<PUBLIC> with the identifier I<pubid> instead of C<SYSTEM>, the default. -=item B<-r> I<roottags> +=item B<--root-tags> I<roottags>, B<-r> I<roottags> Restricts the XML processor to validate only documents with one of the root elements listed in the comma-separated I<roottags>. -=item B<-S> +=item B<--scanner>, B<-S> Generate the scanner I<name>F<.l>. Conflicts with B<-A>; on by default if none of B<-SHD> specified. -=item B<-s> I<skel> +=item B<--skel> I<skel>, B<-s> I<skel> Use the skeleton scanner I<skel> instead of the default. -=item B<-T> I<flexml-act> +=item B<--act-bin> I<flexml-act>, B<-T> I<flexml-act> This is an internal option mainly used to test versions of flexml not installed yet. -=item B<-b> I<stack_size> +=item B<--stack-size> I<stack_size>, B<-b> I<stack_size> Sets the FLEXML_BUFFERSTACKSIZE to stack_size (100000 by default). Use this option when you get an error like "Assertion `next<limit' failed". -=item B<-p> I<STRING> +=item B<--tag-prefix> I<STRING>, B<-O> I<STRING> Use STRING to differentiate multiple versions of flexml in the same C -code. +code, just like the -P flex argument. -=item B<-u> I<uri> +=item B<--uri> I<uri>, B<-u> I<uri> Sets the URI of the DTD, used in the C<DOCTYPE> header, to the specified I<uri> (the default is the DTD name). -=item B<-v> +=item B<--verbose>, B<-v> Be verbose: echo each DTD declaration (after parameter expansion). -=item B<-V> +=item B<--version>, B<-V> Print the version of I<flexml> and exit. |