#
# re_graph.pl -- Graph a regular expression
#
=pod
=head1 NAME
re_graph.pl - Graph regular expression
=head1 SYNOPSIS
B
[B<-d>]
[B<-o> I]
[B<-x> I]
[B<-y> I]
I
[I]
=head1 DESCRIPTION
The I program graphs regular expressions. The guts of the
regular expression engine is a simple state machine. The various states
and operations in the regular expression parser can be displayed using a
surprisingly simple diagram.
A few notes on what you are looking at:
The nodes B and B denote the beginning and end of the regular
expression.
The solid squares denote atoms. Lines indicate the next state.
When a line splits, the state machine will take the top line first.
If it's path is blocked it will backup and take the next lower line.
This is repeated until it finds a path to the end or all paths are exhausted.
Brown boxes indicate a grouping operation, i.e. ().
Green boxes indicate a zero with test. The state machine will perform the
test inside the box before moving ahead.
For more information, see the tutorial below.
=head1 OPTIONS
=over 4
=item B<-d>
Turn on debugging. The debugging output is printed on the console as regular
expressions are compiled.
=item B<-o> I
Specify the output file for the images. Default = I.
If only a regular expresion is specified, the output will be written
to the given file.
If the system is used in graph / execution mode, a series of files will
be written using the printf style file name specified.
=item B<-x> I
Specify the minimum size of the image in pixels in the X direction.
=item B<-y> I
Specify the minimum size of the image in pixels in the Y direction.
=back
=head1 TUTORIAL
This tutorial shows what happens when a set of sample regular expressions
are graphed. This set of regular expressions closely follows the
Chapter 4 of "Perl for C Programmers" by Steve Oualline.
The set of regular expressions used for this tutorial is:
test
^Test
^ *#
^[ \t]*#
^\s*#
([^#]*)(#.*)
a|b
^(([^#"]*|".*")*)(#.*)
^((?:[^#"]*|".*?")*)(#.*)
^((?:[^#"]*|".*?(?
=end html
=item B
This is a very simple expression. It matches "test" anywhere on the line.
If you look at the graph of this expression, it consists of three nodes "Start",
"EXACT " and "END".
The "Start" node indicates the start of the regular expression.
The "EXACT " node tells the engine that the text must match the
text "test" exactly.
The "END" node indicates the end of the regular expression. If you reach
the "END" node, a successful match was found.
Flow is a straight line from "Start", through the "EXACT" check, to end.
=begin html

=end html
=item B^Test/>
A new item was added with this expression, an anchor. It's named BOL
(Beginning of line) and shows up as an additional node.
=begin html

=end html
=item B^ *#/>
Now we start having fun. This expression matches anything that consists
of a start of line (^), a bunch of spaces ( *), and a sharp (#).
The way the state machine works it that it starts at "Start" and works
it's way through the nodes. You'll notice that between "BOL" and
"EXACT < >" there's a fork in the road.
The state machine will take the top branch if possible. So if the next
character is a space, the system will take the top branch and match the
"EXACT < >" node. If not, the bottom branch is taken and we wind up
at the "EXACT <#>" node.
If there's no path to the "END", then we don't have a match.
=begin html

=end html
=item B^[ \t]*#/>
This is the same as the previous example, except the space was replaced
by a character set. We call the set "space and tab". The system translates
this into "\11\40". It's the same thing, suitable obfuscated for computer
work.
=begin html

=end html
=item B^\s*#/>
Again, the middle as been replace by another token. In this case it's
the SPACE token which matches any whitespace.
=begin html

=end html
=item B([^#]*)(#.*)/>
This expression introduces us to the grouping operators. They show as the
big brown boxes.
The other change is that we use the expression [^#], which matches anything
except a hash mark. Perl changes this to a "ANYOF" clauses which matches
all characters except the single one we don't want.
Note: This ANYOF node overflows the size of the box. This is a know bug.
The graphing program can show how the regular expression excution
process. Let's see what happens when we run the command:
perl re_graph.pl -o tut_06_%d.png '([^#]*)(#.*)' 'text # Comment'
=begin html

=end html
The first image show a yellow arrow pointing to the first set of
(), indicating that the system is about to go into $1.
=begin html

=end html
The next yellow arrow points at the B operator indicating that
the regular expression engine is about to look at the C<[^#]> part
of the expression.
=begin html

=end html
In the next screen, the yellow arrow has moved to the box
representing the second set of (). That means that the first
part of matching process is done. The string C is highlighted
yellowing, indicating that that much of the string has be matched
so far.
=begin html

=end html
Next the yellow arrow points to the "#" node. The string is highlighted
up to the just before the "#". This tells us that engine is about to
match the "#" in the string against the "#" in the regular expression.
The next few images (not shown) show the engine matching the rest of
the string.
=begin html

=end html
=item B
Now we introduce the concept of a selection of two different atoms. Note that
the branch arrows are drawn smaller to make them stand out.
=begin html

=end html
=item B^(([^#"]*|".*")*)(#.*)/>
See the book for what this regular expression tries to match.
This expression adds nested grouping, and some additional stuff that we've seen
before.
=begin html

=end html
=item B^((?:[^#"]*|".*?")*)(#.*)/>
This is like the previous example, except what was the $2 grouping has been
replaced by the "Group no $" operator (?:...). Notice that the line around
the second group has disappeared and what was $3 is now $2.
(In future versions of this graphing tool we will graph the invisible group
operator. We just did figure out how to do it yet.)
Also notice the use of the "*?" operator. Remember when going through
the nodes, when a branch is encountered, the system will try and take
the top one first.
=begin html

=end html
=item B^((?:[^#"]*|".*?(?
The grand finale. One new type of node has been introduced: (? 60;
# Size of a node (Y Space)
use constant Y_NODE_SIZE => 40;
# Space text over this far
use constant X_TEXT_OFFSET => 3;
use constant Y_TEXT_OFFSET => 3;
# Space between nodes (X)
use constant X_MARGIN => 50;
# Vertical spacing
use constant Y_MARGIN => 10;
# Offset for line 2 of a 2 line text field
use constant X_LINE2_OFFSET => 10;
# Offset for line 2 of a 2 line text field
use constant Y_LINE2_OFFSET => 15;
# Padding for PLUS style nodes (left, right)
use constant PLUS_PAD => 10;
# Space between branches (x)
use constant X_BRANCH_MARGIN => 20;
# Space between branches (y)
use constant Y_BRANCH_MARGIN => 20;
# Thickness of the lines
use constant THICKNESS => 3;
# Margin around the graph
use constant MARGIN => 100;
# Label location
use constant LABEL_LOC_X => 50;
use constant LABEL_LOC_Y => 50;
# Location of progress msg
use constant PROGRESS_X => 50;
use constant PROGRESS_Y => 70;
# Length of the yellow arrow
use constant YELLOW_ARROW_SIZE => 25;
use constant YELLOW_ARROW_WIDTH => 5;
use Getopt::Std;
use vars qw/$opt_d $opt_o $opt_x $opt_y/;
STDOUT->autoflush(1);
# Configuration items
my $x_margin = 16; # Space between items
my $y_margin = 16; # Space between items
#
# Fields
# node -- Node number
# type -- Node type (from re debug)
# arg -- Argument (optional)
# next -- Next node
#
# Regular expression debugging information
my @re_debug;
#
# Fields
# x_size - Size of the node in X
# y_size - Size of the node in Y
# x_loc - X Location of the node
# y_loc - Y Location of the node
# node - Reference to the
# node in @re_debug
# child - Array of child
# nodes for this node
#
# Formatted version of the regular expression
my @format_re;
# Re we are displaying now
my $current_re;
my $re_to_add = ""; # Re we are adding
#
# Image variables
#
my $image; # The image
my $color_white; # White color
my $color_black; # The black color
my $color_green; # The green color
my $color_blue; # Blue color
my $color_light_green; # Light green color
# Forward declarations
sub draw_node_array($);
sub size_array(\@);
sub layout_array($$$\@);
################################################
# filled_rectangle -- Draw a filled rectangle at
# the given location
################################################
sub filled_rectangle($$$$$)
{
# Corners of the rectangle
my $x1 = shift;
my $y1 = shift;
my $x2 = shift;
my $y2 = shift;
my $color = shift; # Color for drawing
if ($opt_d) {
print
"Rectangle($x1, $y1, $x2, $y2, $color)\n";
}
$image->filledRectangle(
$x1, $y1, $x2, $y2,
$color);
$image->setThickness(1);
$image->rectangle(
$x1, $y1, $x2, $y2,
$color_black);
}
################################################
# arrow -- Draw an arrow from x1,y1 -> x2,y2
#
# All arrows are black
################################################
sub arrow($$$$) {
my $x1 = shift; # Start of arrow
my $y1 = shift;
my $x2 = shift; # End of arrow
my $y2 = shift;
if ($opt_d) {
print "Arrow($x1, $y1, $x2, $y2)\n";
}
# For some reason arrows
# tend to point backwards
my $arrow = GD::Arrow::Full->new(
-X1 => $x2,
-Y1 => $y2,
-X2 => $x1,
-Y2 => $y1,
-WIDTH => THICKNESS-1);
$image->setThickness(1);
$image->filledPolygon($arrow, $color_black);
}
############################################
# The "PLUS" node
#
#
# 0 1 2 1p 2p 3p (p = +size of child)
# v v v L3 v v v
# . --------- . .
# . /. . .\ . .
# ./ . . . \ .
# a2 < . . . > a1.
# .\ . . . /. .
# . \+-------+/ .
# L1--->| child |----->+ L2
# . +-------+ . .
#
# Arc start, end, centers
#
# a1 / 270 - 180 / (ap*2, y-a)
# a2 / 90 - 180 / (a0, y-2a), (a2, y-2a)
#
# L1 (a3, y+2a) (a3p, y+2a)
############################################
#------------------------------------------
# size_plus -- Compute the size of
# a plus/star type node
#------------------------------------------
sub size_plus($)
{
# Node we want layout information for
my $node = shift;
# Compute the size of the children
my ($x_size, $y_size) =
size_array(@{$node->{children}});
# Arc size is based on the
# Y dimension of the children
$node->{arc_size} =
int($y_size/4) + PLUS_PAD;
$node->{child_x} = $x_size - X_MARGIN;
$node->{x_size} =
$node->{child_x} +
$node->{arc_size} * 2 + X_MARGIN;
$node->{y_size} =
$y_size + $node->{arc_size} * 2;
}
#------------------------------------------
# Draw the plus type node
#------------------------------------------
sub draw_plus($)
{
# The node we are drawing
my $cur_node = shift;
layout_array(
$cur_node->{x_loc} +
$cur_node->{arc_size} * 1,
$cur_node->{y_loc},
$cur_node->{y_size},
@{$cur_node->{children}});
draw_node_array($cur_node->{children});
# The place we start drawing from (X)
my $from_x = $cur_node->{x_loc};
# The current middle of the item (Y)
my $y = $cur_node->{y_loc} +
int($cur_node->{y_size}/2);
# Size of an arc
my $arc_size = $cur_node->{arc_size};
# Size of the child
my $child_x = $cur_node->{child_x};
# Debugging
if (0) {
for (my $debug_x = 0;
$debug_x < 5;
$debug_x++) {
$image->line(
$from_x +
$arc_size * $debug_x,
$y - $arc_size*2,
$from_x +
$arc_size * $debug_x,
$y + $arc_size*2,
$color_black
);
}
for (my $debug_x = 3;
$debug_x < 7;
$debug_x++) {
$image->line(
$from_x + $child_x +
$arc_size * $debug_x,
$y - $arc_size*2,
$from_x + $child_x +
$arc_size * $debug_x,
$y + $arc_size*2,
$color_green
);
}
}
my $flip = 1; # Flipping factor
if ($cur_node->{min_flag}) {
$flip = -1;
}
$image->setThickness(THICKNESS);
# First arc (a1)
$image->arc(
$from_x + $child_x + $arc_size,
$y - $arc_size * $flip,
$arc_size *2, $arc_size *2,
270, 90,
$color_black);
$image->arc(
$from_x + $arc_size * 1,
$y - $arc_size * $flip,
$arc_size *2, $arc_size *2,
90, 270,
$color_black);
# Draw (L1)
arrow(
$from_x, $y,
$from_x + $arc_size * 1, $y
);
# Draw (L2)
arrow(
$from_x + $child_x + $arc_size * 1,
$y,
$from_x + $child_x + $arc_size * 2,
$y
);
# Draw (L3)
arrow(
$from_x + $child_x + $arc_size * 1,
$y - $arc_size * 2,
$from_x + $arc_size * 1,
$y - $arc_size * 2
);
# Text to display for the current node
my $text = $cur_node->{node}->{text_label};
if ($cur_node->{min_flag}) {
$text .= "?";
}
$image->string(
gdMediumBoldFont,
$from_x + $child_x + $arc_size * 2,
$y - $arc_size * 2,
$text,
$color_blue);
$cur_node->{left_x} = $from_x;
$cur_node->{left_y} = $y;
$cur_node->{right_x} =
$from_x + $cur_node->{child_x} +
$cur_node->{arc_size} * 2;
$cur_node->{right_y} = $y;
}
############################################
# The "STAR" node
#
#
# (p = +size of child)
# 0 1 2 3 p3 p4 p5
# v v v v L2 v v v
# . ----------------- . .
# . /. . . .\ . .
# ./ . . . . \ .
# a6 < . . . a5 . > .
# .\ . . . . /. .
# . \. . . +-------+/ .
# L3----------->| child |- . +
# . .\ . j +-------+ .a4/.
# . . \a1 . . . / .
# . . \ . . ./ .
# . . | . . | .
# . . .\ . . / .
# . . a2\ . ./a3 .
# . . . \---------
# ^ ^ L1
# 2 3
#
# Arc / swing / center
# a1 / 270 - 0 / (a1, y + a)
# a2 / 90 - 180 / (a3, y + a)
# a3 / 0 - 90 / (p3, y + a)
# a4 / 180 - 270 / (a4p, y)
#
# a5 / 270 - 90 / (p3, y-a)
# a6 / 90 - 270 / (a1, y-a)
#
# L1 (a3, y+2a) (a3p, y+2a)
############################################
#-----------------------------------------
# size_star -- Compute the size of
# a star type node
#-----------------------------------------
sub size_star($)
{
# Node we want layout information for
my $node = shift;
# Compute the size of the children
my ($x_size, $y_size) =
size_array(@{$node->{children}});
# Arc size is based on the
# Y dimension of the children
$node->{arc_size} =
int($y_size/4) + PLUS_PAD;
$node->{child_x} = $x_size - X_MARGIN;
$node->{x_size} = $node->{child_x} +
$node->{arc_size} * 5 + X_MARGIN;
$node->{y_size} = $y_size +
$node->{arc_size} * 2 + Y_MARGIN;
}
#-----------------------------------------
# Draw the star type node
#-----------------------------------------
sub draw_star($)
{
# The node we are drawing
my $cur_node = shift;
layout_array(
$cur_node->{x_loc} +
$cur_node->{arc_size} * 3,
$cur_node->{y_loc},
$cur_node->{y_size},
@{$cur_node->{children}});
# The place we start drawing from (X)
my $from_x = $cur_node->{x_loc};
# The current middle of the item (Y)
my $y = int($cur_node->{y_loc} +
$cur_node->{y_size}/2);
# Size of an arc
my $arc_size = $cur_node->{arc_size};
# Size of the child
my $child_x = $cur_node->{child_x};
# Debugging
if (0) {
for (my $debug_x = 0;
$debug_x < 5;
$debug_x++) {
$image->line(
$from_x +
$arc_size * $debug_x,
$y - $arc_size*2,
$from_x +
$arc_size * $debug_x,
$y + $arc_size*2,
$color_black
);
}
for (my $debug_x = 3;
$debug_x < 7;
$debug_x++) {
$image->line(
$from_x + $child_x +
$arc_size * $debug_x,
$y - $arc_size*2,
$from_x + $child_x +
$arc_size * $debug_x,
$y + $arc_size*2,
$color_green
);
}
}
my $flip = 1; # Flipping factor
if ($cur_node->{min_flag}) {
$flip = -1;
}
$image->setThickness(THICKNESS);
if ($flip == 1) {
# First arc (a1)
$image->arc(
$from_x + $arc_size,
$y + $arc_size,
$arc_size * 2, $arc_size * 2,
270, 0,
$color_black);
# Second arc (a2)
$image->arc(
$from_x + $arc_size * 3,
$y + $arc_size,
$arc_size * 2, $arc_size * 2,
90, 180,
$color_black);
} else {
# First arc (a1)
$image->arc(
$from_x + $arc_size,
$y - $arc_size,
$arc_size * 2, $arc_size * 2,
0, 90,
$color_black);
# Second arc (a2)
$image->arc(
$from_x + $arc_size * 3,
$y - $arc_size,
$arc_size * 2, $arc_size * 2,
180, 270,
$color_black);
}
if ($flip > 0) {
# Third arc (a3)
$image->arc(
$from_x + $child_x +
$arc_size * 3,
$y + $arc_size,
$arc_size * 2, $arc_size * 2,
0, 90,
$color_black);
# Fourth arc (a4)
$image->arc(
$from_x + $child_x +
$arc_size * 5,
$y + $arc_size,
$arc_size * 2, $arc_size * 2,
180, 270,
$color_black);
} else {
# Third arc (a3)
$image->arc(
$from_x + $child_x +
$arc_size * 3,
$y - $arc_size,
$arc_size * 2, $arc_size * 2,
270, 0,
$color_black);
# Fourth arc (a4)
$image->arc(
$from_x + $child_x +
$arc_size * 5,
$y - $arc_size,
$arc_size * 2, $arc_size * 2,
90, 180,
$color_black);
}
# Fifth arc (a5)
$image->arc(
$from_x + $child_x + $arc_size * 3,
$y - $arc_size * $flip,
$arc_size * 2, $arc_size * 2,
270, 90,
$color_black);
# Sixth arc (a6)
$image->arc(
$from_x + $arc_size,
$y - $arc_size * $flip,
$arc_size * 2, $arc_size * 2,
90, 270,
$color_black);
# L1
arrow(
$from_x + $arc_size * 3,
$y + $arc_size * 2 * $flip,
$from_x + $arc_size * 3 + $child_x,
$y + $arc_size * 2 * $flip);
# L2
arrow(
$from_x + $arc_size * 3 + $child_x,
$y - $arc_size * 2 * $flip,
$from_x + $arc_size * 1,
$y - $arc_size * 2 * $flip);
# Draw (L3)
arrow(
$from_x, $y,
$from_x + $arc_size * 3, $y);
$image->string(
gdMediumBoldFont,
$from_x + $child_x + $arc_size * 4,
$y - $arc_size * 2,
($cur_node->{min_flag}) ? "*?" : "*",
$color_black);
draw_node_array($cur_node->{children});
$cur_node->{left_x} = $from_x;
$cur_node->{left_y} = $y;
$cur_node->{right_x} =
$from_x + $cur_node->{child_x} +
$cur_node->{arc_size} * 5;
$cur_node->{right_y} = $y;
}
############################################
# Branch nodes
############################################
#-------------------------------------------
# layout a branch node
#-------------------------------------------
sub size_branch($)
{
# Node we want layout information for
my $node = shift;
my $x_size = 0; # Current X size
my $y_size = 0; # Current Y size
foreach my $cur_choice (
@{$node->{choices}}) {
# The size of the current choice
my ($x_choice, $y_choice) =
size_array(@{$cur_choice});
if ($x_size < $x_choice) {
$x_size = $x_choice;
}
if ($y_size != 0) {
$y_size += Y_BRANCH_MARGIN;
}
$cur_choice->[0]->{row_y_size} =
$y_choice;
$y_size += $y_choice;
}
$x_size += 2 * X_BRANCH_MARGIN + X_MARGIN;
$node->{x_size} = $x_size;
$node->{y_size} = $y_size;
}
#-------------------------------------------
# draw_branch -- Draw a branch structure
#-------------------------------------------
sub draw_branch($)
{
# Node we want layout information for
my $cur_node = shift;
# Location where we draw the branches
my $x_loc = $cur_node->{x_loc} +
X_BRANCH_MARGIN;
my $y_loc = $cur_node->{y_loc};
foreach my $cur_child (
@{$cur_node->{choices}}
) {
layout_array(
$x_loc + X_BRANCH_MARGIN,
$y_loc,
$cur_child->[0]->{row_y_size},
@{$cur_child});
$y_loc += $cur_child->[0]->{row_y_size} +
Y_BRANCH_MARGIN;
draw_node_array($cur_child);
}
# Largest right x of any node
my $max_x = 0;
foreach my $cur_child (
@{$cur_node->{choices}}) {
# Last node on the string of children
my $last_node =
$cur_child->[$#{$cur_child}];
if ($last_node->{right_x} > $max_x) {
$max_x = $last_node->{right_x};
}
}
foreach my $cur_child (
@{$cur_node->{choices}}
) {
# Last node on the
# string of children
my $last_node =
$cur_child->[$#{$cur_child}];
if ($last_node->{right_x} < $max_x) {
$image->line(
$last_node->{right_x},
$last_node->{right_y},
$max_x,
$last_node->{right_y},
$color_black);
}
}
my $left_x = $cur_node->{x_loc};
my $right_x = $cur_node->{x_loc} +
$cur_node->{x_size} - X_MARGIN;
my $y = $cur_node->{y_loc} +
($cur_node->{y_size} / 2);
foreach my $cur_child (
@{$cur_node->{choices}}
) {
# Create a branch line to the item
# in the list of nodes
$image->line(
$left_x, $y,
$cur_child->[0]->{left_x},
$cur_child->[0]->{left_y},
$color_black);
# The last node on the list
my $last_child =
$cur_child->[$#$cur_child];
# Line from the last node
# to the collection point
$image->line(
$max_x, $last_child->{right_y},
$right_x, $y,
$color_black);
}
$cur_node->{left_x} = $left_x;
$cur_node->{left_y} = $y;
$cur_node->{right_x} = $right_x;
$cur_node->{right_y} = $y;
}
############################################
# Functions to compute the size of various nodes
############################################
#-------------------------------------------
# layout the start node
#-------------------------------------------
sub size_start($)
{
# Node we want layout information for
my $node = shift;
$node->{x_size} = X_NODE_SIZE + X_MARGIN;
$node->{y_size} = Y_NODE_SIZE;
}
#-------------------------------------------
# layout the end node
#-------------------------------------------
sub size_end($)
{
# Node we want layout information for
my $node = shift;
$node->{x_size} = X_NODE_SIZE;
$node->{y_size} = Y_NODE_SIZE;
}
#-------------------------------------------
# layout the "EXACT" node (EXACT + text)
#-------------------------------------------
sub size_exact($)
{
# Node we want layout information for
my $node = shift;
$node->{x_size} = X_NODE_SIZE + X_MARGIN;
$node->{y_size} = Y_NODE_SIZE;
}
#-------------------------------------------
# layout the "ANYOF" node (ANYOF + text)
#-------------------------------------------
# Size of a character in X dimensions
use constant X_CHAR_SIZE => 7;
sub size_text($)
{
# Node we want layout information for
my $node = shift;
# Get the size of the string argument
my $length = length($node->{node}->{arg});
if ($length < 10) {
$length = 10;
}
$node->{x_size} =
$length * X_CHAR_SIZE + X_MARGIN;
$node->{y_size} = Y_NODE_SIZE;
}
#-------------------------------------------
# OPEN the open (
#-------------------------------------------
# Size of the box around a group
use constant BOX_MARGIN => 50;
# Height of the font used to label boxes
use constant BOX_FONT_SIZE => 15;
sub size_open($)
{
# The node we want to size
my $node = shift;
# Compute the size of the children
my ($x_size, $y_size) =
size_array(@{$node->{children}});
# We add X_MARGIN because we
# must for all nodes
#
# We subtract X_MARGIN because one too many
# is added in our children
#
# Result is nothing
$node->{x_size} = $x_size + BOX_MARGIN;
$node->{y_size} =
$y_size + BOX_MARGIN + BOX_FONT_SIZE;
}
# Functions used to compute the sizes
# of various elements
my %compute_size = (
"ANYOF" => \&size_text,
"BOL" => \&size_exact,
"SPACE" => \&size_exact,
"NSPACE" => \&size_exact,
"DIGIT" => \&size_exact,
"BRANCH"=> \&size_branch,
"END" => \&size_end,
"EOL" => \&size_exact,
"EXACT" => \&size_exact,
"IFMATCH" => \&size_open,
"OPEN" => \&size_open,
"PLUS" => \&size_plus,
"REF" => \&size_exact,
"REG_ANY" => \&size_exact,
"STAR" => \&size_star,
"Start" => \&size_start,
"UNLESSM" => \&size_open
);
############################################
# draw functions
############################################
sub draw_start_end($)
{
my $cur_node = shift;
my $node_number = $cur_node->{node}->{node};
filled_rectangle(
$cur_node->{x_loc},
$cur_node->{y_loc},
$cur_node->{x_loc} + X_NODE_SIZE,
$cur_node->{y_loc} + Y_NODE_SIZE,
$color_green);
$cur_node->{text} = $image->string(
gdSmallFont,
$cur_node->{x_loc} + X_TEXT_OFFSET,
$cur_node->{y_loc} + Y_TEXT_OFFSET,
$cur_node->{node}->{type},
$color_black);
$cur_node->{left_x} = $cur_node->{x_loc};
$cur_node->{left_y} =
$cur_node->{y_loc} + Y_NODE_SIZE / 2;
$cur_node->{right_x} =
$cur_node->{x_loc} + X_NODE_SIZE;
$cur_node->{right_y} =
$cur_node->{y_loc} + Y_NODE_SIZE / 2;
}
#-------------------------------------------
# draw_exact($node) -- Draw a "EXACT" re node
#-------------------------------------------
sub draw_exact($)
{
my $cur_node = shift; # The node
my $node_number = $cur_node->{node}->{node};
filled_rectangle(
$cur_node->{x_loc},
$cur_node->{y_loc},
$cur_node->{x_loc} +
$cur_node->{x_size} -
X_MARGIN,
$cur_node->{y_loc} + Y_NODE_SIZE,
$color_green);
$image->string(
gdSmallFont,
$cur_node->{x_loc} + X_TEXT_OFFSET,
$cur_node->{y_loc} + Y_TEXT_OFFSET,
"$cur_node->{node}->{type}",
$color_black);
$image->string(
gdSmallFont,
$cur_node->{x_loc} +
X_TEXT_OFFSET + X_LINE2_OFFSET,
$cur_node->{y_loc} +
Y_TEXT_OFFSET + Y_LINE2_OFFSET,
"$cur_node->{node}->{arg}",
$color_black);
$cur_node->{left_x} = $cur_node->{x_loc};
$cur_node->{left_y} =
$cur_node->{y_loc} + Y_NODE_SIZE / 2;
$cur_node->{right_x} =
$cur_node->{x_loc} + X_NODE_SIZE;
$cur_node->{right_y} =
$cur_node->{y_loc} + Y_NODE_SIZE / 2;
}
#-------------------------------------------
# draw_ref($node) -- Draw a "REF" re node
#-------------------------------------------
sub draw_ref($)
{
my $cur_node = shift; # The node
my $node_number = $cur_node->{node}->{node};
filled_rectangle(
$cur_node->{x_loc},
$cur_node->{y_loc},
$cur_node->{x_loc} + X_NODE_SIZE,
$cur_node->{y_loc} + Y_NODE_SIZE,
$color_light_green);
$cur_node->{text} = $image->String(
gdSmallFont,
$cur_node->{x_loc} + X_TEXT_OFFSET,
$cur_node->{y_loc} + Y_TEXT_OFFSET,
"Back Reference:\n".
" $cur_node->{node}->{ref}",
$color_black);
$cur_node->{left_x} = $cur_node->{x_loc};
$cur_node->{left_y} =
$cur_node->{y_loc} + Y_NODE_SIZE / 2;
$cur_node->{right_x} =
$cur_node->{x_loc} + X_NODE_SIZE;
$cur_node->{right_y} =
$cur_node->{y_loc} + Y_NODE_SIZE;
}
#-------------------------------------------
# draw the () stuff
#-------------------------------------------
sub draw_open($$)
{
my $cur_node = shift; # The node
$image->setStyle(
$color_black, $color_black,
$color_black, $color_black,
$color_black,
$color_white, $color_white,
$color_white, $color_white,
$color_white
);
$image->rectangle(
$cur_node->{x_loc},
$cur_node->{y_loc} +
BOX_FONT_SIZE,
$cur_node->{x_loc} +
$cur_node->{x_size} -
X_MARGIN,
$cur_node->{y_loc} +
$cur_node->{y_size},
gdStyled);
$image->string(
gdSmallFont,
$cur_node->{x_loc},
$cur_node->{y_loc},
$cur_node->{text},
$color_black);
layout_array(
$cur_node->{x_loc} +
BOX_MARGIN/2,
$cur_node->{y_loc} +
BOX_MARGIN/2 + BOX_FONT_SIZE,
$cur_node->{y_size} -
BOX_MARGIN - BOX_FONT_SIZE,
@{$cur_node->{children}});
draw_node_array($cur_node->{children});
$cur_node->{left_x} = $cur_node->{x_loc};
$cur_node->{left_y} = $cur_node->{y_loc} +
($cur_node->{y_size} + BOX_FONT_SIZE)/2;
$cur_node->{right_x} = $cur_node->{x_loc} +
$cur_node->{x_size} - X_MARGIN;
$cur_node->{right_y} = $cur_node->{left_y};
# Child we are drawing arrows to / from
my $child = $cur_node->{children}->[0];
$image->line(
$cur_node->{left_x},
$cur_node->{left_y},
$child->{left_x},
$child->{left_y},
$color_black
);
$child =
$cur_node->{children}->[
$#{$cur_node->{children}}
];
$image->line(
$child->{right_x},
$child->{right_y},
$cur_node->{right_x},
$cur_node->{right_y},
$color_black
);
}
my %draw_node = (
"ANYOF" => \&draw_exact,
"BOL" => \&draw_start_end,
"EOL" => \&draw_start_end,
"SPACE" => \&draw_start_end,
"NSPACE" => \&draw_start_end,
"DIGIT" => \&draw_start_end,
"BRANCH"=> \&draw_branch,
"END" => \&draw_start_end,
"EXACT" => \&draw_exact,
"IFMATCH" => \&draw_open,
"OPEN" => \&draw_open,
"PLUS" => \&draw_plus,
"REF" => \&draw_ref,
"REG_ANY" => \&draw_start_end,
"STAR" => \&draw_star,
"Start" => \&draw_start_end,
"UNLESSM" => \&draw_open
);
################################################
# usage -- Tell the user how to use us
################################################
sub usage()
{
print STDERR <] []
Options:
-d -- Debug
-o -- Output file (%d = sequence number)
-x -- Minimum size in X
-y -- Minimum size in Y
EOF
exit (8);
}
################################################
# parse_re -- Parse a regular expression
# and leave the results in the array @re
################################################
sub parse_re()
{
my $quote_re = $current_re;
$quote_re =~ s/\\/\\\\/g;
my $cmd = <&1 <&1 < 0,
type => "Start",
next => 1
});
foreach my $cur_line (@raw_debug) {
if ($cur_line =~ /^Compiling/) {
next;
}
if ($cur_line =~ /^\s*size/) {
next;
}
# +++---------------------------------- Spaces
# ||| +++------------------------------ Digits
# |||+|||+----------------------------- Group $1
# |||||||| (Node)
# ||||||||
# ||||||||+---------------------------- Colon
# |||||||||+++------------------------- Spaces
# ||||||||||||
# |||||||||||| +++--------------------- Word chars
# ||||||||||||+|||+-------------------- Group $2
# ||||||||||||||||| (Type)
# |||||||||||||||||
# |||||||||||||||||+++----------------- Spaces
# ||||||||||||||||||||
# |||||||||||||||||||| ++--------------- Any char str
# ||||||||||||||||||||+||+-------------- Group $3
# |||||||||||||||||||||||| (arg)
# ||||||||||||||||||||||||------------- Lit <>
# ||||||||||||||||||||||||
# ||||||||||||||||||||||||+++---------- Spaces
# |||||||||||||||||||||||||||
# ||||||||||||||||||||||||||| ++----- Any char str
# |||||||||||||||||||||||||||++ || ++-- Lit ()
# ||||||||||||||||||||||||||||| || || (next state)
# |||||||||||||||||||||||||||||+||+||-- Group $4
if ($cur_line =~ /\s*(\d+):\s*(\w+)\s*(.*)\s*\((.*)\)/) {
push(@re_debug, {
node => $1,
type => $2,
raw_type => $2,
arg => $3,
next => $4
});
next;
}
if ($cur_line =~ /^anchored/) {
next;
}
if ($cur_line =~ /^Freeing/) {
last;
}
}
}
################################################
# $new_index = parse_node($index,
# $array, $next, $close)
#
# -- Parse a single regular expression node
# -- Stop when next (or end) is found
# -- Or when a close ")" is found
################################################
sub parse_node($$$$);
sub parse_node($$$$)
{
# Index into the array
my $index = shift;
# Array to put things on
my $array = shift;
my $next = shift; # Next node
# Looking for a close?
my $close = shift;
my $min_flag = 0; # Minimize flag
while (1) {
if (not defined($re_debug[$index])) {
return ($index);
}
if (defined($next)) {
if ($next <=
$re_debug[$index]->{node}) {
return ($index);
}
}
if ($re_debug[$index]->{type} =~
/CLOSE(\d+)/) {
if (defined($close)) {
if ($1 == $close) {
return ($index + 1);
}
}
}
if ($re_debug[$index]->{type} eq
"MINMOD") {
$min_flag = 1;
$index++;
next;
}
#--------------------------------------------
if (($re_debug[$index]->{type} eq
"IFMATCH") ||
($re_debug[$index]->{type} eq
"UNLESSM")) {
if ($re_debug[$index]->{arg} !~
/\[(.*?)\]/) {
die("IFMATCH/UNLESSM funny ".
"argument ".
"$re_debug[$index]->{arg}");
}
# Ending text (= or !=)
my $equal = "!=";
if ($re_debug[$index]->{type} eq
"IFMATCH") {
$equal = "=";
}
# Flag indicating the next look ahead
my $flag = $1;
# Text to label this box
my $text;
if ($flag eq "-0") {
$text = "$equal ahead";
} elsif ($flag eq "-0") {
$text = "$equal behind";
} elsif ($flag eq "-1") {
$text = "$equal behind";
} else {
die("Unknown IFMATCH/UNLESSM ".
"flag text $flag");
exit;
}
push(@{$array}, {
node => $re_debug[$index],
text => $text,
children => []
});
$index = parse_node($index+1,
$$array[$#$array]->{children},
$re_debug[$index]->{next},
undef);
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} =~
/OPEN(\d+)/) {
my $paren_count = $1;
$re_debug[$index]->{type} = "OPEN";
push(@{$array}, {
node => $re_debug[$index],
paren_count => $paren_count,
text => "( ) => \$$paren_count",
children => []
});
$index = parse_node($index+1,
$$array[$#$array]->{children},
undef, $paren_count);
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} =~
/REF(\d+)/) {
my $ref_number = $1;
$re_debug[$index]->{type} = "REF";
push(@{$array}, {
node => $re_debug[$index],
ref => $ref_number,
children => []
});
++$index;
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} eq
"BRANCH") {
push(@{$array}, {
node => $re_debug[$index],
choices => []
});
my $choice_index = 0;
while (1) {
# Next node in this series
my $next =
$re_debug[$index]->{next};
$$array[$#$array]->
{choices}[$choice_index] = [];
$index = parse_node($index+1,
$$array[$#$array]->
{choices}[$choice_index],
$next, undef);
if (not defined(
$re_debug[$index])) {
last;
}
if ($re_debug[$index]->{type} ne
"BRANCH") {
last;
}
$choice_index++;
}
next;
}
#--------------------------------------------
if (($re_debug[$index]->{type} eq
"CURLYX") |
($re_debug[$index]->{type} eq
"CURLY")) {
# Min number of matches
my $min_number;
# Max number of matches
my $max_number;
if ($re_debug[$index]->{arg} =~
/{(\d+),(\d+)}/) {
$min_number = $1;
$max_number = $2;
} else {
die("Funny CURLYX args ".
"$re_debug[$index]->{arg}");
exit;
}
my $star_flag = ($min_number == 0);
my $text = "+";
if ($min_number == 0) {
$text = "*";
}
if (($max_number != 32767) ||
($min_number > 1)) {
$text =
"{$min_number, $max_number}";
if ($max_number == 32767) {
$text = "min($min_number)";
}
}
# Node that's enclosed
# inside this one
my $child = {
node => {
type =>
($star_flag) ?
"STAR" : "PLUS",
raw_type =>
$re_debug[$index]->{type},
arg =>
$re_debug[$index]->{arg},
next =>
$re_debug[$index]->{next},
text_label =>
$text
},
min_flag => $min_flag,
children => [],
};
push(@{$array}, $child);
$index = parse_node($index+1,
$child->{children},
$re_debug[$index]->{next},
undef);
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} eq
"CURLYM") {
my $paren_count; # () number
# Min number of matches
my $min_number;
# Max number of matches
my $max_number;
if ($re_debug[$index]->{arg} =~
/\[(\d+)\]\s*{(\d+),(\d+)}/) {
$paren_count = $1;
$min_number = $2;
$max_number = $3;
} else {
die("Funny CURLYM args ".
"$re_debug[$index]->{arg}");
exit;
}
# Are we doing a * or +
# (anything else is just too hard_
my $star_flag = ($min_number == 0);
# The text for labeling this node
my $text = "+";
if ($min_number == 0) {
$text = "*";
}
if (($max_number != 32767) ||
($min_number > 1)) {
$text =
"{$min_number, $max_number}";
if ($max_number == 32767) {
$text = "min($min_number)";
}
}
# Node that's enclosed
# inside this one
my $child = {
node => {
type =>
($star_flag) ?
"STAR" : "PLUS",
raw_type =>
$re_debug[$index]->{type},
arg =>
$re_debug[$index]->{arg},
next =>
$re_debug[$index]->{next},
text_label =>
$text
},
min_flag => $min_flag,
children => [],
};
$min_flag = 0;
# The text for labeling this node
$text = "( ) => \$$paren_count";
if ($paren_count == 0) {
$text = '( ) [no $x]';
}
push(@{$array},
{
node => {
type =>
"OPEN",
raw_type =>
$re_debug[$index]->{type},
arg =>
$re_debug[$index]->{arg},
next =>
$re_debug[$index]->{next}
},
paren_count => $paren_count,
text => $text,
children => [$child]
});
$index = parse_node($index+1,
$child->{children},
$re_debug[$index]->{next},
undef);
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} eq
"STAR") {
push(@{$array},
{
node => {
%{$re_debug[$index]},
-text_label => "+"
},
min_flag => $min_flag,
children => []
});
$min_flag = 0;
# Where we go for the next state
my $star_next;
if (defined($next)) {
$star_next = $next;
} else {
$star_next =
$re_debug[$index]->{next};
}
$index = parse_node($index+1,
$$array[$#$array]->{children},
$star_next, undef);
next;
}
#-----------------------------------------
if ($re_debug[$index]->{type} eq
"PLUS") {
push(@{$array},
{
node => {
%{$re_debug[$index]},
text_label => "+"
},
min_flag => $min_flag,
children => []
});
$min_flag = 0;
$index = parse_node($index+1,
$$array[$#$array]->{children},
$re_debug[$index]->{next},
undef);
next;
}
#-----------------------------------------
# Ignore a couple of nodes
if ($re_debug[$index]->{type} eq
"WHILEM") {
++$index;
next;
}
if ($re_debug[$index]->{type} eq
"SUCCEED") {
++$index;
next;
}
if ($re_debug[$index]->{type} eq
"NOTHING") {
++$index;
next;
}
if ($re_debug[$index]->{type} eq
"TAIL") {
++$index;
next;
}
push(@$array, {
node => $re_debug[$index]});
if ($re_debug[$index]->{type} eq "END") {
return ($index+1);
}
$index++;
}
}
################################################
# do_size($cur_node) --
# Compute the size of a given node
################################################
sub do_size($);
sub do_size($)
{
my $cur_node = shift;
if (not defined(
$compute_size{
$cur_node->{node}->{type}})) {
die("No compute function for ".
"$cur_node->{node}->{type}");
exit;
}
$compute_size{
$cur_node->{node}->{type}}($cur_node);
}
################################################
# size_array(\@array) -- Compute the size of
# an array of nodes
#
# Returns
# (x_size, y_size) -- Size of the elements
#
# x_size -- Size of all the elements in X
# (We assume they are
# laid out in a line)
# y_size -- Biggest Y size
# (side by side layout)
#################################################
sub size_array(\@)
{
# The array
my $re_array = shift;
# Size of the array in X
my $x_size = 0;
# Size of the elements in Y
my $y_size = 0;
foreach my $cur_node(@$re_array) {
do_size($cur_node);
$x_size += $cur_node->{x_size};
if ($y_size < $cur_node->{y_size}) {
$y_size = $cur_node->{y_size};
}
}
return ($x_size, $y_size);
}
################################################
# layout_array($x_start, $y_start,
# $y_max, \@array)
#
# Layout an array of nodes
################################################
sub layout_array($$$\@)
{
# Starting point in X
my $x_start = shift;
# Starting point in Y
my $y_start = shift;
# largest Y value
my $y_max = shift;
# The data
my $re_array = shift;
foreach my $cur_node(@$re_array) {
$cur_node->{x_loc} = $x_start;
$cur_node->{y_loc} = $y_start +
int(($y_max -
$cur_node->{y_size})/2);
$x_start += $cur_node->{x_size};
}
}
################################################
# convert_re -- Convert @re_debug -> @format_re
#
# The formatted re node contains layout
# information as
# well as information on nodes contained
# inside the current one.
################################################
sub convert_re()
{
# Clear out old data
@format_re = ();
parse_node(0, \@format_re, undef, undef);
#
# Compute sizes of each node
#
my ($x_size, $y_size) =
size_array(@format_re);
#
# Compute the location of each node
#
layout_array(MARGIN,
MARGIN, $y_size,
@format_re
);
return (MARGIN + $x_size, MARGIN + $y_size);
}
##############################################
# draw_node_array -- draw an array of nodes
##############################################
sub draw_node_array($)
{
my $array = shift;
#
# Draw Nodes
#
foreach my $cur_node (@$array) {
if (not defined(
$draw_node{
$cur_node->{node}->{type}})) {
die("No draw function for ".
"$cur_node->{node}->{type}");
}
$draw_node{
$cur_node->{node}->{type}}(
$cur_node
);
}
#
# Loop through all the things
# (except the last) and
# draw arrows between them
#
for (my $index = 0;
$index < $#$array;
++$index) {
my $from_x = $array->[$index]->{right_x};
my $from_y = $array->[$index]->{right_y};
my $to_x = $array->[$index+1]->{left_x};
my $to_y = $array->[$index+1]->{left_y};
arrow(
$from_x, $from_y,
$to_x, $to_y
);
}
}
##############################################
# draw_re -- Draw the image
##############################################
sub draw_re()
{
# Draw the top level array
# (Which recursively draws
# all the enclosed elements)
draw_node_array(\@format_re);
# Make all the canvas visible
}
##############################################
# find_node($state, $node_array) -- Find a node
# the parsed node tree
#
# Returns the location of the node
##############################################
sub find_node($$);
sub find_node($$)
{
# State (node number) to find
my $state = shift;
my $array = shift; # The array to search
foreach my $cur_node (@$array) {
if ($cur_node->{node}->{node} ==
$state) {
return ($cur_node->{x_loc},
$cur_node->{y_loc});
}
if (defined($cur_node->{children})) {
# Get the x,y to return from
# the children
my ($ret_x, $ret_y) =
find_node(
$state,
$cur_node->{children});
if (defined($ret_x)) {
return ($ret_x, $ret_y);
}
}
if (defined($cur_node->{choices})) {
my $choices = $cur_node->{choices};
foreach my $cur_choice (@$choices) {
# Get the x,y to return from the
# choice list
my ($ret_x, $ret_y) =
find_node(
$state, $cur_choice);
if (defined($ret_x)) {
return ($ret_x, $ret_y);
}
}
}
}
return (undef, undef);
}
##############################################
# draw_progress($cur_line, $page)
#
# Draw a progress page
#
# Returns true if the page was drawn
##############################################
sub draw_progress($$$)
{
my $value = shift; # Value to check
my $cur_line = shift;# Line we are processing
my $page = shift; # Page number
# Check to see if this
# is one of the progress lines
if (substr($cur_line, 26, 1) ne '|') {
return (0); # Not a good line
}
# Line containing the progress number
# from the debug output
my $progress_line = substr($cur_line, 0, 24);
# Location of the current state information
my $state_line = substr($cur_line, 27);
# Extract progress number
$progress_line =~ /^\s*(\d+)/;
my $progress = $1;
# Extract state number
$state_line =~ /^\s*(\d+)/;
my $state = $1;
# Find the location of this node
# on the graph
my ($x_location, $y_location) =
find_node($state, \@format_re);
if ($opt_d) {
if (defined($x_location)) {
print
"node $state ".
"($x_location, $y_location)\n";
} else {
print "node $state not found\n";
}
}
# If the node is not graphable,
# skip this step
if (not defined($x_location)) {
return (0);
}
# Create a new image with arrow
my $new_image =
GD::Image->newFromPngData(
$image->png(0));
# Create the arrow
my $arrow = GD::Arrow::Full->new(
-X1 => $x_location,
-Y1 => $y_location,
-X2 => $x_location - YELLOW_ARROW_SIZE,
-Y2 => $y_location - YELLOW_ARROW_SIZE,
-WIDTH => YELLOW_ARROW_WIDTH
);
$new_image->setThickness(1);
# Create some colors for
# the new image
my $new_color_yellow =
$new_image->colorAllocate(255, 255, 0);
my $new_color_black =
$new_image->colorAllocate(0,0,0);
# Make the arrow point
# to the current step
$new_image->filledPolygon(
$arrow, $new_color_yellow);
$new_image->polygon(
$arrow, $new_color_black);
# Get the size of the font we are using
my $char_width = gdGiantFont->width;
my $char_height = gdGiantFont->height;
$new_image->filledRectangle(
PROGRESS_X, PROGRESS_Y,
PROGRESS_X +
$progress * $char_width,
PROGRESS_Y + $char_height,
$new_color_yellow
);
$new_image->string(gdGiantFont,
PROGRESS_X, PROGRESS_Y,
$value, $new_color_black);
# Generate the output file name
my $out_file =
sprintf($opt_o, $page);
open OUT_FILE, ">$out_file" or
die("Could not open output".
"file: $out_file");
binmode OUT_FILE;
print OUT_FILE $new_image->png(0);
close OUT_FILE;
return (1);
}
##############################################
# chart_progress -- Chart the progress of the
# execution of the RE
##############################################
sub chart_progress()
{
my $value = $ARGV[0]; # Value to check
# Value with ' quoted
my $quote_value = $value;
$quote_value =~ s/'/\\'/g;
# Regular expression
my $quote_re = $current_re;
$quote_re =~ s/\\/\\\\/g;
my $cmd = <&1 < 0) and
($raw_debug[0] !~ /^Matching/)) {
shift(@raw_debug);
}
shift(@raw_debug);
my $page = 1; # Current output page
foreach my $cur_line (@raw_debug) {
# Skip other lines
if (length($cur_line) < 27) {
next;
}
if (draw_progress($value,
$cur_line, $page)) {
++$page;
}
}
}
# -d -- Print RE debug output and draw output
# -o file -- specify output file (template)
# -x
# -y
my $status = getopts("do:x:y:");
if ($status == 0)
{
usage();
}
if (not defined($opt_o)) {
$opt_o = "re_graph_%02d.png";
}
if ($#ARGV == -1) {
usage();
}
$current_re = shift(@ARGV);
parse_re();
my ($x_size, $y_size) = convert_re();
$x_size += MARGIN;
$y_size += MARGIN;
if (defined($opt_x)) {
if ($opt_x > $x_size) {
$x_size = $opt_x;
}
}
if (defined($opt_y)) {
if ($opt_y > $y_size) {
$y_size = $opt_y;
}
}
$image = GD::Image->new($x_size, $y_size);
# Background color
$color_white =
$image->colorAllocate(255,255,255);
$color_black = $image->colorAllocate(0,0,0);
$color_green = $image->colorAllocate(0, 255, 0);
$color_blue = $image->colorAllocate(0, 0, 255);
$color_light_green =
$image->colorAllocate(0, 128, 0);
draw_re();
$image->string(gdGiantFont,
LABEL_LOC_X, LABEL_LOC_Y,
"Regular Expression: /$current_re/",
$color_black);
my $out_file = sprintf($opt_o, 0);
open OUT_FILE, ">$out_file" or
die("Could not open output file: $out_file");
binmode OUT_FILE;
print OUT_FILE $image->png(0);
close OUT_FILE;
if ($#ARGV != -1) {
chart_progress();
}
__END__
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.