]> git.sur5r.net Git - cc65/blobdiff - src/ca65html/ca65html
Fixed a bug in the last change. For variables that have no initializer, no
[cc65] / src / ca65html / ca65html
index eaf6901063488f1534d3b77417a1bb0473b56493..3d82abaeb0c7da9286376d482ba4d9602fab28ac 100755 (executable)
@@ -1,16 +1,16 @@
 #!/usr/bin/perl
 ###############################################################################
 #                                                                             #
-#                                  main.c                                    #
+#                                  ca65html                                   #
 #                                                                             #
 #                     Convert a ca65 source into HTML                        #
 #                                                                             #
 #                                                                             #
 #                                                                             #
-#  (C) 2000      Ullrich von Bassewitz                                        #
-#                Wacholderweg 14                                              #
-#                D-70597 Stuttgart                                            #
-#  EMail:        uz@musoftware.de                                             #
+#  (C) 2000-2007 Ullrich von Bassewitz                                        #
+#                Roemerstrasse 52                                             #
+#                D-70794 Filderstadt                                          #
+#  EMail:        uz@cc65.org                                                  #
 #                                                                             #
 #                                                                             #
 #  This software is provided 'as-is', without any expressed or implied        #
 
 
 
+# Things currently missing:
+#
+#   - Scoping with .proc/.endproc, .scope/.endscope, .enum/.endenum,
+#     .struct/.endstruct, .union/endunion, .repeat/.endrep, .local
+#   - .global is ignored
+#   - .case is ignored, labels are always case-sensitive
+#   - .include handling (difficult)
+#   - The global namespace operator ::
+#
+
+
+
 use strict 'vars';
 use warnings;
 
@@ -49,26 +61,39 @@ use Getopt::Long;
 
 
 # Global variables
-my %Files      = ();           # List of all files.
-my $FileCount  = 0;            # Number of input files
-my %Exports    = ();           # List of exported symbols.
-my %Imports    = ();           # List of imported symbols.
-my %Labels     = ();           # List of all labels
-my $LabelNum   = 0;            # Counter to generate unique labels
+my %Files                  = ();           # List of all files.
+my $FileCount              = 0;            # Number of input files
+my %Exports                = ();           # List of exported symbols.
+my %Imports                = ();           # List of imported symbols.
+my %Labels                 = ();           # List of all labels
+my $LabelNum               = 0;            # Counter to generate unique labels
 
 # Command line options
-my $BGColor    = "#FFFFFF";    # Background color
-my $Debug      = 0;            # No debugging
-my $Help       = 0;            # Help flag
-my $HTMLDir    = "";           # Directory in which to create the files
-my $IndexCols          = 6;            # Columns in the file listing
-my $IndexTitle = "Index";      # Title of index page
-my $IndexName  = "index.html"; # Name of index page
-my $IndexPage  = 0;            # Create an index page
-my $LinkStyle          = 0;            # Default link style
-my $ReplaceExt = 0;            # Replace extension instead of appending
-my $TextColor  = "#000000";    # Text color
-my $Verbose            = 0;            # Be quiet
+my $BGColor                = "#FFFFFF";    # Background color
+my $Colorize        = 0;            # Colorize the output
+my $CommentColor    = "#B22222";    # Color for comments
+my $CRefs           = 0;            # Add references to the C file
+my $CtrlColor       = "#228B22";    # Color for control directives
+my $CvtTabs        = 0;            # Convert tabs to spaces
+my $TabSize         = 8;            # This is how god created them
+my $Debug                  = 0;            # No debugging
+my $Help                   = 0;            # Help flag
+my $HTMLDir                = "";           # Directory in which to create the files
+my $IndexCols              = 6;            # Columns in the file listing
+my $IndexTitle             = "Index";      # Title of index page
+my $IndexName              = "index.html"; # Name of index page
+my $IndexPage              = 0;            # Create an index page
+my $KeywordColor    = "#A020F0";    # Color for keywords
+my $LineLabels      = 0;            # Add a HTML label to each line
+my $LineNumbers     = 0;            # Add line numbers to the output
+my $LinkStyle              = 0;            # Default link style
+my $ReplaceExt             = 0;            # Replace extension instead of appending
+my $StringColor     = "#6169C1";    # Color for strings
+my $TextColor              = "#000000";    # Text color
+my $Verbose                = 0;            # Be quiet
+
+# Table used to convert the label number into names
+my @NameTab         = ('A' .. 'Z', '0' .. '9');
 
 
 
@@ -93,8 +118,17 @@ sub Gabble {
 
 # Generate a label and return it
 sub GenLabel {
+
+    my $I;
+    my $L = "";;
+    my $Num = $LabelNum++;
+
     # Generate the label
-    return sprintf ("L%06X", $LabelNum++);
+    for ($I = 0; $I < 4; $I++) {
+       $L = $NameTab[$Num % 36] . $L;
+       $Num /= 36;
+    }
+    return $L;
 }
 
 # Make an output file name from an input file name
@@ -111,7 +145,7 @@ sub GetOutName {
     }
 }
 
-# Remove illegal characters from a string
+# Translate some HTML characters into harmless names.
 sub Cleanup {
     my $S = shift (@_);
     $S =~ s/&/&amp;/g;
@@ -135,7 +169,7 @@ sub StripPath {
 
 
 #-----------------------------------------------------------------------------#
-#                        Document header and footer                          #
+#                        Document header and footer                          #
 # ----------------------------------------------------------------------------#
 
 
@@ -144,19 +178,60 @@ sub StripPath {
 sub DocHeader {
     my $OUT = shift (@_);
     my $Asm = shift (@_);
+    print $OUT "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
     print $OUT <<"EOF";
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html-40/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <meta name="GENERATOR" content="ca65html">
 <title>$Asm</title>
+<style type=\"text/css\">
+body {
+    background-color: $BGColor;
+    color: $TextColor;
+}
+h1 {
+    text-align: center;
+}
+#top {
+    margin: 2em 0 3em 0;
+    border-bottom: 1px solid grey;
+}
+#bottom {
+    margin: 3em 0 1em 0;
+    padding-top: 1em;
+    border-top: 1px solid grey;
+}
+img {
+    border: 0;
+    margin: 0;
+    float: right;
+}
+.ctrl {
+    color: $CtrlColor;
+}
+.keyword {
+    color: $KeywordColor;
+}
+.string {
+    color: $StringColor;
+}
+.comment {
+    color: $CommentColor;
+}
+a:link {
+    color: #0000d0;
+}
+a:visited {
+    color: #000060;
+}
+a:active {
+    color: #00d0d0;
+}
+</style>
 </head>
-<body bgcolor="$BGColor" text="$TextColor" link="#0000d0" vlink="#000060" alink="#00d0d0">
-<p><br><p>
-<center><h1>$Asm</h1></center>
-<hr><p><br><p>
-<pre>
+<body>
+<div id=\"top\"><h1>$Asm</h1></div>
 EOF
 }
 
@@ -168,27 +243,67 @@ sub DocFooter {
     # Get the current date and time
     my $Today = localtime;
 
-    print $OUT <<"EOF";
-</pre>
-<p><br><p>
-<hr size=1 noshade>
-<address>
-    <a href=\"http://validator.w3.org/check/referer\"><img border=0
-       src=\"http://validator.w3.org/images/vh40\"
-       alt=\"Valid HTML 4.0!\" height=31 width=88 align=right></a>
-    $Name; generated on $Today by ca65html<br>
-    <a href=\"mailto:uz\@cc65.org\">uz\@cc65.org</a>
-</address>
-</body>
-</html>
-EOF
+    # Print
+    print $OUT "<div id=\"bottom\"><address>\n";
+    print $OUT "<a href=\"http://validator.w3.org/check?uri=referer\">\n";
+    print $OUT "<img src=\"http://www.w3.org/Icons/valid-xhtml10-blue\" alt=\"Valid XHTML 1.0 Strict\" height=\"31\" width=\"88\" /></a><br>\n";
+    print $OUT "$Name; generated on $Today by ca65html<br>\n";
+    print $OUT "<a href=\"mailto:uz&#64;cc65.org\">uz&#64;cc65.org</a>\n";
+    print $OUT "</address></div>\n";
+    print $OUT "</body></html>\n";
 }
 
 
 
 #-----------------------------------------------------------------------------#
-#                           File list management                             #
-# ----------------------------------------------------------------------------#
+#                                Colorization                                 #
+#-----------------------------------------------------------------------------#
+
+
+
+sub ColorizeComment {
+    if ($Colorize && $_[0] ne "") {
+        return "<span class=\"comment\">$_[0]</span>";
+    } else {
+        return $_[0];
+    }
+}
+
+
+
+sub ColorizeCtrl {
+    if ($Colorize) {
+        return "<span class=\"ctrl\">$_[0]</span>";
+    } else {
+        return $_[0];
+    }
+}
+
+
+
+sub ColorizeKeyword {
+    if ($Colorize) {
+        return "<span class=\"keyword\">$_[0]</span>";
+    } else {
+        return $_[0];
+    }
+}
+
+
+
+sub ColorizeString {
+    if ($Colorize) {
+        return "<span class=\"string\">$_[0]</span>";
+    } else {
+        return $_[0];
+    }
+}
+
+
+
+#-----------------------------------------------------------------------------#
+#                           File list management                             #
+#-----------------------------------------------------------------------------#
 
 
 
@@ -208,10 +323,10 @@ sub AddFile {
 
     # Check with the full pathname. If we don't find it, search in the current
     # directory
-    if (-f $FileName && -r $FileName) {
+    if (-f $FileName && -r _) {
        $Files{$Name} = $FileName;
        $FileCount++;
-    } elsif (-f $Name && -r $Name) {
+    } elsif (-f $Name && -r _) {
        $Files{$Name} = $Name;
        $FileCount++;
     } else {
@@ -223,7 +338,7 @@ sub AddFile {
 
 #-----------------------------------------------------------------------------#
 #                      Referencing and defining labels                       #
-# ----------------------------------------------------------------------------#
+#-----------------------------------------------------------------------------#
 
 
 
@@ -270,7 +385,7 @@ sub RefLabel {
 
 
 #-----------------------------------------------------------------------------#
-#                                  Pass 1                                    #
+#                                  Pass 1                                    #
 # ----------------------------------------------------------------------------#
 
 
@@ -302,18 +417,18 @@ sub Process1 {
     while ($Line = <INPUT>) {
 
                # Remove the newline
-               chop ($Line);
+               chomp ($Line);
 
                # Check for a label
-               if ($Line =~ /^\s*(\@?)([_a-zA-Z][_\w]*)\s*(:|=)/) {
+        if ($Line =~ /^\s*(([\@?]?)[_a-zA-Z]\w*)\s*(?::=?|=)/) {
 
            # Is this a local label?
-           if ($1 eq "\@") {
+           if ($2 ne "") {
                # Use the prefix
-               $Id = "$CheapPrefix$1$2";
+               $Id = "$CheapPrefix$1";
            } else {
                # Use as is
-               $Id = $2;
+               $Id = $1;
                # Remember the id as new cheap local prefix
                $CheapPrefix = $Id;
            }
@@ -322,32 +437,43 @@ sub Process1 {
            $Labels{$OutName}{$Id} = GenLabel();
 
        # Check for an import statement
-       } elsif ($Line =~ /^\s*(\.import|\.importzp)\s+(.*?)(\s*)(;.*$|$)/) {
+       } elsif ($Line =~ /^\s*\.(?:(?:force)?import|importzp)\s+(.*?)\s*(?:;.*)?$/i) {
 
            # Split into a list of identifiers
-           my @Ids = split (/\s*,\s*/, $2);
+           my @Ids = split (/\s*(?::\s*[A-Za-z]+\s*)?,\s*/, $1);
+
+           # Remove an address-size specifier, from the last identifier,
+           # if there is one.
+           $Ids[$#Ids] =~ s/\s*:\s*[A-Za-z]+//;
+
            for $Id (@Ids) {
                $Imports{$OutName}{$Id} = GenLabel();
            }
 
        # Check for an export statement
-       } elsif ($Line =~ /^\s*(\.export|\.exportzp)\s+(.*?)(\s*)(;.*$|$)/) {
+       } elsif ($Line =~ /^\s*\.export(?:zp)?\s+(.*?)\s*(?:;.*)?$/i) {
 
            # Split into a list of identifiers
-           my @Ids = split (/\s*,\s*/, $2);
+           my @Ids = split (/\s*(?::\s*[A-Za-z]+\s*)?,\s*/, $1);
+
+           # Remove an address-size specifier, from the last identifier,
+           # if there is one.
+           $Ids[$#Ids] =~ s/\s*:\s*[A-Za-z]+//;
+
                    for $Id (@Ids) {
                $Exports{$Id} = sprintf ("%s#%s", $OutName, GenLabel());
            }
 
-       # Check for a .proc statement
-               } elsif ($Line =~ /^\s*\.proc\s+([_a-zA-Z][_\w]*)?.*$/) {
+       # Check for an actor statement.
+               } elsif ($Line =~ /^\s*\.(?:(?:(?:con|de)struc|interrup)tor|condes)\s+([_a-z]\w*)/i) {
+           $Exports{$1} = sprintf ("%s#%s", $OutName, GenLabel());
 
-           # Do we have an id?
-           $Id = $1;
-           if ($Id ne "") {
-               $Labels{$OutName}{$Id} = GenLabel();
-           }
+       # Check for a .proc statement
+               } elsif ($Line =~ /^\s*\.proc\s+([_a-z]\w*)/i) {
 
+           # Remember the ID as the new cheap-local prefix.
+           $CheapPrefix = $1;
+           $Labels{$OutName}{$1} = GenLabel();
        }
     }
 
@@ -388,8 +514,8 @@ sub Process2 {
     my $OutLine;
     my $Id;
     my $Label;
-    my $Operand;
     my $Comment;
+    my $Trailer;
 
     # Input file is parameter
     my $InName = shift(@_);
@@ -407,36 +533,84 @@ sub Process2 {
     # Open the output file and print the HTML header
     open (OUTPUT, ">$HTMLDir$OutName") or Abort ("Cannot open $OutName: $!");
     DocHeader (OUTPUT, $InName);
+    print OUTPUT "<pre>\n";
 
     # Keep the user happy
     Gabble ("$FileName => $OutName");
 
-    # The instructions that will have hyperlinks if a label is used
-    my $Ins = "adc|add|and|bcc|bcs|beq|bit|bmi|bne|bpl|bcv|bra|bvs|".
-             "cmp|cpx|cpy|dec|eor|inc|jmp|jsr|lda|ldx|ldy|ora|rol|".
-             "sbc|sta|stx|sty|sub|";
-
-    # Read the input file, replacing references by hyperlinks and mark
+    # The instructions that will have hyperlinks if a label is used.
+    # And, they will be highlighted when color is used.
+    my $LabelIns = "adc|add|and|asl|bb[rs][0-7]|b[cv][cs]|beq|bge|bit|blt|".
+                "bmi|bne|bpl|br[akl]|bsr|cmp|cop|cp[axy]|dec|eor|inc|jml|".
+                "jmp|jsl|jsr|ld[axy]|lsr|mvn|mvp|ora|pe[air]|rep|".
+                "[rs]mb[0-7]|rol|ror|sbc|sep|st[012axyz]|sub|tai|tam|tdd|".
+                "ti[ain]|tma|trb|tsb|tst";
+
+    # Instructions that have only the implied-addressing mode -- therefore,
+    # no hyperlinking.  They will be highlighted only, when color is used.
+    my $OtherIns = "cl[acdivxy]|csh|csl|de[axy]|in[axy]|nop|ph[abdkpxy]|".
+                "pl[abdpxy]|rt[ils]|sax|say|se[cdit]|stp|swa|sxy|ta[dsxy]|".
+                "tam[0-7]|tcd|tcs|tda|tdc|tma[0-7]|ts[acx]|tx[asy]|tya|tyx|".
+                "wai|xba|xce";
+
+    # Read the input file, replacing references with hyperlinks; and, mark
     # labels as link targets.
-    while ($Line = <INPUT>) {
+    my $LineNo = 0;
+    LINE: while ($Line = <INPUT>) {
+
+        # Count input lines
+        $LineNo++;
 
-       # Remove the newline
-       chop ($Line);
+       # Remove the newline at the end of line. Don't use chomp to be able to
+        # read dos/windows sources on unices.
+        $Line =~ s/[\r\n]*$//;
 
-       # Clear the output line
+        # If requested, convert tabs to spaces
+       if ($CvtTabs) {
+           # Don't ask me - this is from the perl manual page
+           1 while ($Line =~ s/\t+/' ' x (length($&) * $TabSize - length($`) % $TabSize)/e) ;
+       }
+
+               # Clear the output line
        $OutLine = "";
 
-       # Check for a label. If we have one, process it and remove it
-       # from the line
-       if ($Line =~ /^\s*?(\@?)([_a-zA-Z][_\w]*)(\s*)(:|=)(.*)$/) {
+        # If requested, add a html label to each line with a name "linexxx",
+        # so it can be referenced from the outside (this is the same convention
+        # that is used by c2html). If we have line numbers enabled, add them.
+        if ($LineLabels && $LineNumbers) {
+            $OutLine .= sprintf ("<a name=\"line%d\">%6d</a>:  ", $LineNo, $LineNo);
+        } elsif ($LineLabels) {
+            $OutLine .= sprintf ("<a name=\"line%d\"></a>", $LineNo);
+        } elsif ($LineNumbers) {
+            $OutLine .= sprintf ("%6d:  ", $LineNo);
+        }
+
+        # Cut off a comment from the input line. Beware: We have to check for
+        # strings, since these may contain a semicolon that is no comment
+        # start.
+        ($Line, $Comment) = $Line =~ /^((?:[^"';]+|".*?"|'.*?')*)(.*)$/;
+        if ($Comment =~ /^["']/) {
+            # Line with invalid syntax - there's a string start but
+            # no string end.
+            Abort (sprintf ("Invalid input at %s(%d)", $FileName, $LineNo));
+        }
+
+        # Remove trailing whitespace and move it together with the comment
+        # into the $Trailer variable.
+               $Line =~ s/\s*$//;
+       $Trailer = $& . ColorizeComment (Cleanup ($Comment));
+
+               # Check for a label at the start of the line. If we have one, process
+        # it, and remove it from the line.
+        if ($Line =~ s/^\s*?(([\@?]?)[_a-zA-Z]\w*)(\s*(?::=?|=))//) {
 
            # Is this a local label?
-                   if ("$1" eq "\@") {
+                   if ($2 ne "") {
                # Use the prefix
-               $Id = "$CheapPrefix$1$2";
+               $Id = "$CheapPrefix$1";
            } else {
                # Use as is
-               $Id = $2;
+               $Id = $1;
                # Remember the id as new cheap local prefix
                $CheapPrefix = $Id;
            }
@@ -445,57 +619,73 @@ sub Process2 {
            $Label = $Labels{$OutName}{$Id};
 
            # Print the label with a tag
-                   $OutLine .= sprintf ("<a name=\"%s\">%s%s</a>%s%s", $Label, $1, $2, $3, $4);
+           $OutLine .= "<a name=\"$Label\">$1</a>$3";
+
+           # Is the name explicitly assigned a value?
+           if ($3 =~ /=$/) {
+               # Print all identifiers if there are any.
+               while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
+                   # Add the non-label stuff.
+                   $OutLine .= Cleanup ($1);
+
+                   # Use the prefix if the label is local.
+                   # Get the reference to that label if we find it.
+                   $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
+               }
 
-           # Use the remainder for line
-           $Line = $5;
+               # Add a remainder if there is one.
+               $OutLine .= Cleanup ($Line);
+
+               # The line is complete; print it.
+               next LINE;
+           }
        }
 
        # Print any leading whitespace and remove it, so we don't have to
        # care about whitespace below.
-       if ($Line =~ /^(\s+)(.*)$/) {
-           $OutLine .= "$1";
-           $Line = $2;
+       if ($Line =~ s/^\s+//) {
+           $OutLine .= $&;
        }
 
        # Handle the import statements
-       if ($Line =~ /^(\.import|\.importzp)(\s+)(.*)$/) {
+       if ($Line =~ s/^\.(?:(?:force)?import|importzp)\s+//i) {
 
            # Print any fixed stuff from the line and remove it
-           $OutLine .= "$1$2";
-           $Line = $3;
+                   $OutLine .= $&;
 
            # Print all identifiers if there are any
-           while ($Line =~ /^([_a-zA-Z][_\w]*)(.*)$/) {
+           while ($Line =~ s/^[_a-zA-Z]\w*//) {
 
-               # Identifier is $1, remainder is $2
-               $Id = $1;
-               $Line = $2;
+                # Remember the identifier
+                       my $Id = $&;
 
                # Variable to assemble HTML representation
-               my $Item = $Id;
+                       my $Contents = "";
+
+               # Make this import a link target
+               if (exists ($Imports{$OutName}{$Id})) {
+                           $Label = $Imports{$OutName}{$Id};
+                           $Contents .= sprintf (" name=\"%s\"", $Label);
+               }
 
                # If we have an export for this import, add a link to this
                # export definition
                if (exists ($Exports{$Id})) {
                    $Label = $Exports{$Id};
-                   $Item = sprintf ("<a href=\"%s\">%s</a>", $Label, $Item);
+                   $Contents .= sprintf (" href=\"%s\"", $Label);
                }
 
-               # Make this import a link target
-               if (exists ($Imports{$OutName}{$Id})) {
-                           $Label = $Imports{$OutName}{$1};
-                   $Item = sprintf ("<a name=\"%s\">%s</a>", $Label, $Item);
+               # Add the HTML stuff to the output line
+               if ($Contents ne "") {
+                   $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
+               } else {
+                   $OutLine .= $Id;
                }
 
-               # Add the HTML stuff to the output line
-               $OutLine .= $Item;
-
                # Check if another identifier follows
-               if ($Line =~ /^(\s*),(\s*)(.*)$/) {
-                   $OutLine .= "$1,$2";
-                   $Line = $3;
-                       } else {
+               if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
+                   $OutLine .= $&;
+                       } else {
                    last;
                }
            }
@@ -504,27 +694,25 @@ sub Process2 {
            $OutLine .= Cleanup ($Line);
 
                # Handle export statements
-       } elsif ($Line =~ /^(\.export|\.exportzp)(\s+)(.*)$/) {
+       } elsif ($Line =~ s/^\.export(?:zp)?\s+//i) {
 
-           # Print the command the and white space
-           $OutLine .= "$1$2";
-           $Line = $3;
+           # Print the command and the whitespace.
+           $OutLine .= $&;
 
            # Print all identifiers if there are any
-           while ($Line =~ /^([_a-zA-Z][_\w]*)(.*)$/) {
+           while ($Line =~ s/^[_a-zA-Z]\w*//) {
 
-               # Identifier is $1, remainder is $2
-               $Id = $1;
-               $Line = $2;
+                # Remember the identifier
+                       my $Id = $&;
 
                # Variable to assemble HTML representation
-               my $Item = $Id;
+                       my $Contents = "";
 
                # If we have a definition for this export in this file, add
                # a link to the definition.
-               if (exists ($Labels{$OutName}{$1})) {
-                   $Label = $Labels{$OutName}{$1};
-                           $Item = sprintf ("<a href=\"#%s\">%s</a>", $Label, $Item);
+               if (exists ($Labels{$OutName}{$Id})) {
+                   $Label = $Labels{$OutName}{$Id};
+                           $Contents = sprintf (" href=\"#%s\"", $Label);
                }
 
                # If we have this identifier in the list of exports, add a
@@ -532,17 +720,20 @@ sub Process2 {
                if (exists ($Exports{$Id})) {
                    $Label = $Exports{$Id};
                            # Be sure to use only the label part
-                   $Label =~ s/^(.*#)(.*)$/$2/;
-                   $Item = sprintf ("<a name=\"%s\">%s</a>", $Label, $Item);
+                   $Label =~ s/^.*#//;
+                   $Contents .= sprintf (" name=\"%s\"", $Label);
                }
 
                # Add the HTML stuff to the output line
-               $OutLine .= $Item;
+               if ($Contents ne "") {
+                   $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
+               } else {
+                   $OutLine .= $Id;
+               }
 
                # Check if another identifier follows
-               if ($Line =~ /^(\s*),(\s*)(.*)$/) {
-                   $OutLine .= "$1,$2";
-                   $Line = $3;
+               if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
+                   $OutLine .= $&;
                } else {
                    last;
                }
@@ -551,62 +742,162 @@ sub Process2 {
            # Add an remainder if there is one
            $OutLine .= Cleanup ($Line);
 
-       # Check for .addr and .word
-       } elsif ($Line =~ /^(\.addr|\.word)(\s+)(.*)$/) {
+       # Handle actor statements.
+       } elsif ($Line =~ s/^(\.(?:(?:(?:con|de)struc|interrup)tor|condes)\s+)([_a-z]\w*)//i) {
+
+           # Print the command and the whitespace.
+           $OutLine .= $1;
+
+           # Remember the identifier.
+           $Id = $2;
 
-           # Print the command the and white space
-           $OutLine .= "$1$2";
-           $Line = $3;
+           # Variable to assemble HTML representation
+           my $Contents = "";
+
+           # If we have a definition for this actor, in this file,
+           # then add a link to that definition.
+           if (exists ($Labels{$OutName}{$Id})) {
+               $Contents = sprintf (" href=\"#%s\"", $Labels{$OutName}{$Id});
+           }
+
+           # Get the target, for linking from imports in other files.
+           $Label = $Exports{$Id};
+           # Be sure to use only the label part.
+           $Label =~ s/^.*#//;
+
+           # Add the HTML stuff and the remainder of the actor
+           # to the output line.
+           $OutLine .= sprintf ("<a name=\"%s\"%s>%s</a>%s", $Label,
+                                $Contents, $Id, Cleanup ($Line));
+
+       # Check for .faraddr, .addr, .dword, .word, .dbyt, .byt, .byte, .res,
+       # .elseif, .if, .align, and .org.
+       } elsif ($Line =~ s/^\.(?:(?:far)?addr|d?word|d?byte?|res|(?:else)?if|align|org)\s+//i) {
+
+           # Print the command and the white space
+                   $OutLine .= $&;
 
            # Print all identifiers if there are any
-           while ($Line =~ /^([_a-zA-Z][_\w]*)(.*)$/) {
-               if (exists ($Labels{$OutName}{$1})) {
-                   $Label = $Labels{$OutName}{$1};
-                           $OutLine .= sprintf ("<a href=\"#%s\">%s</a>", $Label, $1);
-               } else {
-                   $OutLine .= "$1";
-               }
-               $Line = $2;
-               if ($Line =~ /^(\s*),(\s*)(.*)$/) {
-                   $OutLine .= "$1,$2";
-                   $Line = $3;
-               } else {
-                   last;
-               }
+                   while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
+                # Add the non label stuff
+                $OutLine .= Cleanup ($1);
+
+               # Use the prefix if the label is local.
+               # Get the reference to that label if we find it.
+               $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
            }
 
            # Add an remainder if there is one
            $OutLine .= Cleanup ($Line);
 
        # Handle .proc
-               } elsif ($Line =~ /^(\.proc\s+)([_a-zA-Z][_\w]*)?(.*)$/) {
+               } elsif ($Line =~ /^(\.proc)(\s+)([_a-z]\w*)?(.*)$/i) {
 
            # Do we have an identifier?
-           if ($2 ne "") {
-               # Get the label for the id
-               $Label = $Labels{$OutName}{$2};
+           if ($3 ne "") {
+               # Remember the ID as the new cheap-local prefix.
+               $CheapPrefix = $3;
+
+               # Get the label for the id
+               $Label = $Labels{$OutName}{$3};
 
                # Print the label with a tag
-               $OutLine .= sprintf ("%s<a name=\"%s\">%s</a>", $1, $Label, $2);
+               $OutLine .= "$1$2<a name=\"$Label\">$3</a>";
 
-               # Use the remainder for line
-               $Line = $3;
-           }
+           } else {
 
-           # Cleanup the remainder and add it
-           $OutLine .= Cleanup ($Line);
+               # Print a line that has invalid syntax (its operand isn't
+               # a correctly formed name).
+               $OutLine .= "$1$2";
+            }
+
+            # Add the remainder
+            $OutLine .= Cleanup ($4);
 
        # Handle .include
-       } elsif ($Line =~ /^(\.include)(\s+)\"((?:[^\"]+?|\\\")+)\"(\s*)(;.*$|$)/) {
+       } elsif ($Line =~ /^(\.include)(\s*)\"((?:[^\"]+?|\\\")+)(\".*)$/i) {
 
            # Add the fixed stuff to the output line
-           $OutLine .= "$1$2\"";
+           $OutLine .= "$1$2&quot;";
 
            # Get the filename into a named variable
+           my $FileName = Cleanup ($3);
+
+           # Get the name without a path
+           my $Name = StripPath ($3);
+
+           # If the include file is among the list of our files, add a link,
+           # otherwise just add the name as is.
+           if (exists ($Files{$Name})) {
+               $OutLine .= sprintf ("<a href=\"%s\">%s</a>", GetOutName ($Name), $FileName);
+           } else {
+               $OutLine .= $FileName;
+           }
+
+           # Add the remainder
+           $OutLine .= Cleanup ($4);
+
+        # Handle .dbg line
+        } elsif ($CRefs && $Line =~ s/^\.dbg\s+//) {
+
+            # Add the fixed stuff to the output line
+            $OutLine .= $&;
+
+            # Check for the type of the .dbg directive
+            if ($Line =~ /^(line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*)$/) {
+
+                # Add the fixed stuff to the output line
+                $OutLine .= "$1&quot;";
+
+                # Get the filename and line number into named variables
+                my $DbgFile = $2;
+                my $DbgLine = $4;
+
+                # Remember the remainder
+                $Line = "\"$3$4$5";
+
+                # Get the name without a path
+                my $Name = StripPath ($DbgFile);
+
+                # We don't need FileName any longer as is, so clean it up
+                $DbgFile = Cleanup ($DbgFile);
+
+                # Add a link to the source file
+                $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $DbgLine, $DbgFile);
+
+                # Add the remainder
+                $OutLine .= Cleanup ($Line);
+
+            } elsif ($Line =~ /^(file,\s*)\"((?:[^\"]+?|\\\")+)\"(.*)$/) { #pf FIXME: doesn't handle \" correctly!
+
+                # Get the filename into a named variables
+                my $DbgFile = Cleanup ($2);
+
+                # Get the name without a path
+                my $Name = Cleanup (StripPath ($2));
+
+                # Add the fixed stuff to the output line
+                $OutLine .= sprintf ("%s\"<a href=\"%s.html\">%s</a>\"%s",
+                                     $1, $Name, $DbgFile, $3);
+
+            } else {
+
+                # Add the remainder
+                $OutLine .= Cleanup ($Line);
+
+            }
+
+        } elsif ($CRefs && $Line =~ /^(\.dbg)(\s+line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*$)/) {
+
+            # Add the fixed stuff to the output line
+            $OutLine .= "$1$2&quot;";
+
+           # Get the filename and line number into named variables
            my $FileName = $3;
+            my $LineNo   = $5;
 
            # Remember the remainder
-           $Line = "\"$4$5";
+           $Line = "\"$4$5$6";
 
            # Get the name without a path
            my $Name = StripPath ($FileName);
@@ -614,46 +905,81 @@ sub Process2 {
            # We don't need FileName any longer as is, so clean it up
            $FileName = Cleanup ($FileName);
 
-           # If the include file is among the list of our files, add a link,
-           # otherwise just add the name as is.
-           if (exists ($Files{$Name})) {
-               $OutLine .= sprintf ("<a href=\"%s\">%s</a>", GetOutName ($Name), $FileName);
-           } else {
-               $OutLine .= $FileName;
-           }
+                   # Add a link to the source file
+                   $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $LineNo, $FileName);
 
            # Add the remainder
            $OutLine .= Cleanup ($Line);
 
-       # Check for any legal instruction
-       } elsif ($Line =~ /^($Ins)(\s+)(.*?)(\s*)(;.*$|$)/) {
+       # Check for .ifdef, .ifndef, .ifref, and .ifnref.
+       } elsif ($Line =~ s/^(\.ifn?[dr]ef\s+)(([\@?]?)[_a-z]\w*)?//i) {
+
+           # Print the command and the whitespace.
+                   $OutLine .= $1;
+
+                   if ($2 ne "") {
+               # Use the prefix if the label is local.
+               # Get the reference to that label if we find it.
+               $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
+           }
+
+           # Add a remainder if there is one.
+           $OutLine .= Cleanup ($Line);
+
+       # Check for assertions.
+       } elsif ($Line =~ s/^(\.assert\s+)(.+?)(,\s*(?:error|warning)\s*(?:,.*)?)$/$2/i) {
+
+           # Print the command and the whitespace.
+           $OutLine .= $1;
+
+           $Comment = $3;
+
+           # Print all identifiers if there are any.
+           while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
+               # Add the non-label stuff.
+               $OutLine .= Cleanup ($1);
+
+               # Use the prefix if the label is local.
+               # Get the reference to that label if we find it.
+               $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
+           }
+
+           # Add a remainder if there is one.
+           $OutLine .= Cleanup ($Line . $Comment);
+
+       # Check for instructions with labels
+        } elsif ($Line =~ s/^($LabelIns)\b(\s*)//io) {
 
            # Print the instruction and white space
-           $OutLine .= "$1$2";
+            $OutLine .= ColorizeKeyword ($1) . $2;
 
-           # Remember the remaining parts
-           $Operand = $3;
-           $Comment = Cleanup ("$4$5");
+           # Print all identifiers if there are any.
+           while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
 
-           # Check for the first identifier in the operand and replace it
-           # by a hyperlink
-                   if ($Operand =~ /^([^_a-zA-Z]*?)(\@?)([_a-zA-Z][_\w]*)(.*)$/) {
+               # Add the non-label stuff.
+               $OutLine .= Cleanup ($1);
 
                # Is this a local label?
-               if ("$2" eq "\@") {
+               if ($3 ne "") {
                    # Use the prefix
-                   $Id = "$CheapPrefix$2$3";
-               } else {
-                   # Use as is
-                   $Id = $3;
-               }
+                   $Id = "$CheapPrefix$2";
+               } else {
+                   # Use as is
+                   $Id = $2;
+               }
 
-               # Get the reference to this label if we find it
-               $Operand = Cleanup($1) . RefLabel($OutName, $Id, $2 . $3) . Cleanup($4);
+               # Get the reference to this label if we find it
+               $OutLine .= RefLabel ($OutName, $Id, $2);
            }
 
            # Reassemble and print the line
-                   $OutLine .= "$Operand$Comment";
+           $OutLine .= Cleanup ($Line);
+
+       # Check for all other instructions
+        } elsif ($Line =~ /^($OtherIns)\b(.*)$/io) {
+
+           # Colorize and print
+            $OutLine .= ColorizeKeyword ($1) . Cleanup ($2);
 
        } else {
 
@@ -662,11 +988,16 @@ sub Process2 {
 
        }
 
-       # Print the result
-       print OUTPUT "$OutLine\n";
+    } continue {
+        # Colorize all keywords
+               $OutLine =~ s/(?<![\w;])\.[_a-zA-Z]\w*/ColorizeCtrl ($&)/ge;
+
+       # Print the result with the trailer.
+       print OUTPUT "$OutLine$Trailer\n";
     }
 
     # Print the HTML footer
+    print OUTPUT "</pre>\n";
     DocFooter (OUTPUT, $OutName);
 
     # Close the files
@@ -774,39 +1105,14 @@ sub CreateIndex {
     open (INDEX, ">$HTMLDir$IndexName") or Abort ("Cannot open $IndexName: $!");
 
     # Print the header
-    print INDEX <<"EOF";
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html-40/loose.dtd">
-<html>
-<head>
-<meta http-equiv="Content-Type" content=\"text/html; charset=iso-8859-1">
-<meta name="GENERATOR" content="ca65html">
-<title>$IndexTitle</title>
-</head>
-<body bgcolor="$BGColor" text="$TextColor" link="#0000d0" vlink="#000060" alink="#00d0d0">
-<p><br><p>
-<center><h1>$IndexTitle</h1></center>
-<hr><p><br><p>
-EOF
+    DocHeader (INDEX, $IndexTitle, 0);
 
     # Print the file list in a table
     FileIndex (INDEX);
     ExportIndex (INDEX);
 
     # Print the document footer
-    my $Today = localtime;
-    print INDEX <<"EOF";
-<p><br><p>
-<hr size=1 noshade>
-<address>
-    <a href="http://validator.w3.org/check/referer"><img border=0
-       src="http://validator.w3.org/images/vh40"
-       alt="Valid HTML 4.0!" height="31" width="88" align=right></a>
-    $IndexName; generated on $Today by ca65html<br>
-    <a href=\"mailto:uz\@cc65.org\">uz\@cc65.org</a>
-</address>
-</body>
-</html>
-EOF
+    DocFooter (INDEX, $IndexName);
 
     # Close the index file
     close (INDEX);
@@ -823,46 +1129,67 @@ EOF
 sub Usage {
     print "Usage: ca65html [options] file ...\n";
     print "Options:\n";
-    print "  --bgcolor color     Use background color c instead of $BGColor\n";
-    print "  --help              This text\n";
-    print "  --htmldir dir       Specify directory for HTML files\n";
-    print "  --indexcols n       Use n columns on index page (default $IndexCols)\n";
-    print "  --indexname file    Use file for the index file instead of $IndexName\n";
-    print "  --indexpage         Create an index page\n";
-    print "  --indextitle title  Use title as the index title instead of $IndexTitle\n";
-    print "  --linkstyle style   Use the given link style\n";
-    print "  --replaceext        Replace source extension instead of appending .html\n";
-    print "  --textcolor color   Use text color c instead of $TextColor\n";
-    print "  --verbose           Be more verbose\n";
+    print "  --bgcolor c        Use background color c instead of $BGColor\n";
+    print "  --colorize         Add color highlights to the output\n";
+    print "  --commentcolor c   Use color c for comments instead of $CommentColor\n";
+    print "  --crefs            Generate references to the C source file(s)\n";
+    print "  --ctrlcolor c      Use color c for directives instead of $CtrlColor\n";
+    print "  --cvttabs          Convert tabs to spaces in the output\n";
+    print "  --help             This text\n";
+    print "  --htmldir dir      Specify directory for HTML files\n";
+    print "  --indexcols n      Use n columns on index page (default $IndexCols)\n";
+    print "  --indexname file   Use file for the index file instead of $IndexName\n";
+    print "  --indexpage        Create an index page\n";
+    print "  --indextitle title Use title as the index title instead of $IndexTitle\n";
+    print "  --keywordcolor c   Use color c for keywords instead of $KeywordColor\n";
+    print "  --linelabels       Generate a linexxx HTML label for each line\n";
+    print "  --linenumbers      Add line numbers to the output\n";
+    print "  --linkstyle style  Use the given link style\n";
+    print "  --replaceext       Replace source extension instead of appending .html\n";
+    print "  --tabsize n        Use n spaces when replacing tabs (default $TabSize)\n";
+    print "  --textcolor c      Use text color c instead of $TextColor\n";
+    print "  --verbose          Be more verbose\n";
 }
 
 
 
 #-----------------------------------------------------------------------------#
-#                                   Main                                     #
+#                                   Main                                     #
 # ----------------------------------------------------------------------------#
 
 
 
 # Get program options
-GetOptions ("bgcolor=s"                => \$BGColor,
-           "debug!"            => \$Debug,
-           "help"              => \$Help,
-           "htmldir=s"         => \$HTMLDir,
+GetOptions ("bgcolor=s"                => \$BGColor,
+            "colorize"          => \$Colorize,
+            "commentcolor=s"    => \$CommentColor,
+            "crefs"             => \$CRefs,
+            "ctrlcolor=s"       => \$CtrlColor,
+           "cvttabs"           => \$CvtTabs,
+           "debug!"            => \$Debug,
+           "help"              => \$Help,
+           "htmldir=s"         => \$HTMLDir,
            "indexcols=i"       => \$IndexCols,
-           "indexname=s"       => \$IndexName,
-           "indexpage"         => \$IndexPage,
-           "indextitle=s"      => \$IndexTitle,
-           "linkstyle=i"       => \$LinkStyle,
-           "replaceext"        => \$ReplaceExt,
-           "textcolor=s"       => \$TextColor,
-                   "verbose!"          => \$Verbose,
-           "<>"                => \&AddFile);
+           "indexname=s"       => \$IndexName,
+           "indexpage"         => \$IndexPage,
+           "indextitle=s"      => \$IndexTitle,
+            "keywordcolor=s"    => \$KeywordColor,
+            "linelabels"        => \$LineLabels,
+            "linenumbers"       => \$LineNumbers,
+           "linkstyle=i"       => \$LinkStyle,
+           "replaceext"        => \$ReplaceExt,
+            "tabsize=i"         => \$TabSize,
+           "textcolor=s"       => \$TextColor,
+                   "verbose!"          => \$Verbose,
+           "<>"                => \&AddFile);
 
 # Check some arguments
 if ($IndexCols <= 0 || $IndexCols >= 20) {
     Abort ("Invalid value for --indexcols option");
 }
+if ($TabSize < 1 || $TabSize > 16) {
+    Abort ("Invalid value for --tabsize option");
+}
 if ($HTMLDir ne "" && $HTMLDir =~ /[^\/]$/) {
     # Add a trailing path separator
     $HTMLDir .= "/";
@@ -891,4 +1218,3 @@ if ($IndexPage) {
 
 # Done
 exit 0;
-