]> git.sur5r.net Git - cc65/blob - src/ca65html/ca65html
Patch for ca65html by Greg King:
[cc65] / src / ca65html / ca65html
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 #                                  ca65html                                   #
5 #                                                                             #
6 #                      Convert a ca65 source into HTML                        #
7 #                                                                             #
8 #                                                                             #
9 #                                                                             #
10 #  (C) 2000-2007 Ullrich von Bassewitz                                        #
11 #                Roemerstrasse 52                                             #
12 #                D-70794 Filderstadt                                          #
13 #  EMail:        uz@cc65.org                                                  #
14 #                                                                             #
15 #                                                                             #
16 #  This software is provided 'as-is', without any expressed or implied        #
17 #  warranty.  In no event will the authors be held liable for any damages     #
18 #  arising from the use of this software.                                     #
19 #                                                                             #
20 #  Permission is granted to anyone to use this software for any purpose,      #
21 #  including commercial applications, and to alter it and redistribute it     #
22 #  freely, subject to the following restrictions:                             #
23 #                                                                             #
24 #  1. The origin of this software must not be misrepresented; you must not    #
25 #     claim that you wrote the original software. If you use this software    #
26 #     in a product, an acknowledgment in the product documentation would be   #
27 #     appreciated but is not required.                                        #
28 #  2. Altered source versions must be plainly marked as such, and must not    #
29 #     be misrepresented as being the original software.                       #
30 #  3. This notice may not be removed or altered from any source               #
31 #     distribution.                                                           #
32 #                                                                             #
33 ###############################################################################
34
35
36
37 # Things currently missing:
38 #
39 #   - Scoping with .proc/.endproc, .scope/.endscope, .enum/.endenum,
40 #     .struct/.endstruct, .union/endunion, .repeat/.endrep, .local
41 #   - .global is ignored
42 #   - .case is ignored, labels are always case-sensitive
43 #   - .include handling (difficult)
44 #   - The global namespace operator ::
45 #
46
47
48
49 use strict 'vars';
50 use warnings;
51
52 # Modules
53 use Getopt::Long;
54
55
56
57 #-----------------------------------------------------------------------------#
58 #                                  Variables                                  #
59 # ----------------------------------------------------------------------------#
60
61
62
63 # Global variables
64 my %Files           = ();           # List of all files.
65 my $FileCount       = 0;            # Number of input files
66 my %Exports         = ();           # List of exported symbols.
67 my %Imports         = ();           # List of imported symbols.
68 my %Labels          = ();           # List of all labels
69 my $LabelNum        = 0;            # Counter to generate unique labels
70
71 # Command line options
72 my $BGColor         = "#FFFFFF";    # Background color
73 my $Colorize        = 0;            # Colorize the output
74 my $CommentColor    = "#B22222";    # Color for comments
75 my $CRefs           = 0;            # Add references to the C file
76 my $CtrlColor       = "#228B22";    # Color for control directives
77 my $CvtTabs         = 0;            # Convert tabs to spaces
78 my $Debug           = 0;            # No debugging
79 my $Help            = 0;            # Help flag
80 my $HTMLDir         = "";           # Directory in which to create the files
81 my $IndexCols       = 6;            # Columns in the file listing
82 my $IndexTitle      = "Index";      # Title of index page
83 my $IndexName       = "index.html"; # Name of index page
84 my $IndexPage       = 0;            # Create an index page
85 my $KeywordColor    = "#A020F0";    # Color for keywords
86 my $LineLabels      = 0;            # Add a HTML label to each line
87 my $LineNumbers     = 0;            # Add line numbers to the output
88 my $LinkStyle       = 0;            # Default link style
89 my $ReplaceExt      = 0;            # Replace extension instead of appending
90 my $StringColor     = "#6169C1";    # Color for strings
91 my $TextColor       = "#000000";    # Text color
92 my $Verbose         = 0;            # Be quiet
93
94 # Table used to convert the label number into names
95 my @NameTab         = ('A' .. 'Z', '0' .. '9');
96
97
98
99 #-----------------------------------------------------------------------------#
100 #                              Helper functions                               #
101 # ----------------------------------------------------------------------------#
102
103
104
105 # Terminate with an error
106 sub Abort {
107     print STDERR "ca65html: @_\n";
108     exit 1;
109 }
110
111 # Print a message if verbose is true
112 sub Gabble {
113     if ($Verbose) {
114         print "ca65html: @_\n";
115     }
116 }
117
118 # Generate a label and return it
119 sub GenLabel {
120
121     my $I;
122     my $L = "";;
123     my $Num = $LabelNum++;
124
125     # Generate the label
126     for ($I = 0; $I < 4; $I++) {
127         $L = $NameTab[$Num % 36] . $L;
128         $Num /= 36;
129     }
130     return $L;
131 }
132
133 # Make an output file name from an input file name
134 sub GetOutName {
135
136     # Input name is parameter
137     my $InName = $_[0];
138
139     # Create the output file name from the input file name
140     if ($ReplaceExt && $InName =~ /^(.+)\.([^\.\/]*)$/) {
141         return "$1.html";
142     } else {
143         return "$InName.html";
144     }
145 }
146
147 # Translate some HTML characters into harmless names.
148 sub Cleanup {
149     my $S = shift (@_);
150     $S =~ s/&/&amp;/g;
151     $S =~ s/</&lt;/g;
152     $S =~ s/>/&gt;/g;
153     $S =~ s/\"/&quot;/g;
154     return $S;
155 }
156
157 # Strip a path from a filename and return just the name
158 sub StripPath {
159
160     # Filename is argument
161     my $FileName = $_[0];
162
163     # Remove a path name if we have one
164     $FileName =~ /^(.*?)([^\/]*)$/;
165     return $2;
166 }
167
168
169
170 #-----------------------------------------------------------------------------#
171 #                         Document header and footer                          #
172 # ----------------------------------------------------------------------------#
173
174
175
176 # Print the document header
177 sub DocHeader {
178     my $OUT = shift (@_);
179     my $Asm = shift (@_);
180     print $OUT "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
181     print $OUT <<"EOF";
182 <html>
183 <head>
184 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
185 <meta name="GENERATOR" content="ca65html">
186 <title>$Asm</title>
187 <style type=\"text/css\">
188 body {
189     background-color: $BGColor;
190     color: $TextColor;
191 }
192 h1 {
193     text-align: center;
194 }
195 #top {
196     margin: 2em 0 3em 0;
197     border-bottom: 1px solid grey;
198 }
199 #bottom {
200     margin: 3em 0 1em 0;
201     padding-top: 1em;
202     border-top: 1px solid grey;
203 }
204 img {
205     border: 0;
206     margin: 0;
207     float: right;
208 }
209 .ctrl {
210     color: $CtrlColor;
211 }
212 .keyword {
213     color: $KeywordColor;
214 }
215 .string {
216     color: $StringColor;
217 }
218 .comment {
219     color: $CommentColor;
220 }
221 a:link {
222     color: #0000d0;
223 }
224 a:visited {
225     color: #000060;
226 }
227 a:active {
228     color: #00d0d0;
229 }
230 </style>
231 </head>
232 <body>
233 <div id=\"top\"><h1>$Asm</h1></div>
234 EOF
235 }
236
237 # Print the document footer
238 sub DocFooter {
239     my $OUT  = shift (@_);
240     my $Name = shift (@_);
241
242     # Get the current date and time
243     my $Today = localtime;
244
245     # Print
246     print $OUT "<div id=\"bottom\"><address>\n";
247     print $OUT "<a href=\"http://validator.w3.org/check?uri=referer\">\n";
248     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";
249     print $OUT "$Name; generated on $Today by ca65html<br>\n";
250     print $OUT "<a href=\"mailto:uz&#64;cc65.org\">uz&#64;cc65.org</a>\n";
251     print $OUT "</address></div>\n";
252     print $OUT "</body></html>\n";
253 }
254
255
256
257 #-----------------------------------------------------------------------------#
258 #                                Colorization                                 #
259 #-----------------------------------------------------------------------------#
260
261
262
263 sub ColorizeComment {
264     if ($Colorize && $_[0] ne "") {
265         return "<span class=\"comment\">$_[0]</span>";
266     } else {
267         return $_[0];
268     }
269 }
270
271
272
273 sub ColorizeCtrl {
274     if ($Colorize) {
275         return "<span class=\"ctrl\">$_[0]</span>";
276     } else {
277         return $_[0];
278     }
279 }
280
281
282
283 sub ColorizeKeyword {
284     if ($Colorize) {
285         return "<span class=\"keyword\">$_[0]</span>";
286     } else {
287         return $_[0];
288     }
289 }
290
291
292
293 sub ColorizeString {
294     if ($Colorize) {
295         return "<span class=\"string\">$_[0]</span>";
296     } else {
297         return $_[0];
298     }
299 }
300
301
302
303 #-----------------------------------------------------------------------------#
304 #                            File list management                             #
305 #-----------------------------------------------------------------------------#
306
307
308
309 sub AddFile {
310
311     # Argument is file to add
312     my $FileName = $_[0];
313
314     # Get just the name (remove a path if there is one)
315     my $Name = StripPath ($FileName);
316
317     # Check if we have the file already
318     if (exists ($Files{$Name})) {
319         Gabble ("File \"$FileName\" already known");
320         return;
321     }
322
323     # Check with the full pathname. If we don't find it, search in the current
324     # directory
325     if (-f $FileName && -r _) {
326         $Files{$Name} = $FileName;
327         $FileCount++;
328     } elsif (-f $Name && -r _) {
329         $Files{$Name} = $Name;
330         $FileCount++;
331     } else {
332         Abort ("$FileName not found or not readable");
333     }
334 }
335
336
337
338 #-----------------------------------------------------------------------------#
339 #                       Referencing and defining labels                       #
340 #-----------------------------------------------------------------------------#
341
342
343
344 # Get a label reference
345 sub RefLabel {
346
347     # Arguments are: Filename, identifier, item that should be tagged
348     my $FileName = $_[0];
349     my $Id       = $_[1];
350     my $Item     = $_[2];
351
352     # Search for the identifier in the list of labels
353     if (exists ($Labels{$FileName}{$Id})) {
354         # It is a label (in this file)
355         return sprintf ("<a href=\"#%s\">%s</a>", $Labels{$FileName}{$Id}, $Item);
356     } elsif (exists ($Imports{$FileName}{$Id})) {
357         # It is an import. If LinkStyle is 1, or if the file exporting the
358         # identifier is not visible, we link to the .import statement in the
359         # current file. Otherwise we link directly to the referenced symbol
360         # in the file that exports it.
361         if ($LinkStyle == 1 or not exists ($Exports{$Id})) {
362             return sprintf ("<a href=\"#%s\">%s</a>", $Imports{$FileName}{$Id}, $Item);
363         } else {
364             # Get the filename from the export
365             my $Label;
366             ($FileName, $Label) = split (/#/, $Exports{$Id});
367             if (not defined ($Labels{$FileName}{$Id})) {
368                 # This may currently happen because we don't see .include
369                 # statements, so we may have an export but no definition.
370                 # Link to the .export statement instead
371                 $Label = $Exports{$Id};
372             } else {
373                 # Link to the definition in the file
374                 $Label = sprintf ("%s#%s", $FileName, $Labels{$FileName}{$Id});
375             }
376             return sprintf ("<a href=\"%s\">%s</a>", $Label, $Item);
377         }
378     } else {
379         # The symbol is unknown, return as is
380         return $Item;
381     }
382 }
383
384
385
386 #-----------------------------------------------------------------------------#
387 #                                   Pass 1                                    #
388 # ----------------------------------------------------------------------------#
389
390
391
392 # Process1: Read one file for the first time.
393 sub Process1 {
394
395     # Variables
396     my $Line;
397     my $Id;
398
399     # Filename is parameter
400     my $InName = shift(@_);
401
402     # Create the output file name from the input file name
403     my $OutName = GetOutName ($InName);
404
405     # Current cheap local label prefix is empty
406     my $CheapPrefix = "";
407
408     # Open a the input file
409     my $FileName = $Files{$InName};     # Includes path if needed
410     open (INPUT, "<$FileName") or Abort ("Cannot open $FileName: $!");
411
412     # Keep the user happy
413     Gabble ("$FileName => $OutName");
414
415     # Read and process all lines from the file
416     while ($Line = <INPUT>) {
417
418         # Remove the newline
419         chomp ($Line);
420
421         # Check for a label
422         if ($Line =~ /^\s*(([\@?]?)[_a-zA-Z]\w*)\s*(?::=?|=)/) {
423
424             # Is this a local label?
425             if ($2 ne "") {
426                 # Use the prefix
427                 $Id = "$CheapPrefix$1";
428             } else {
429                 # Use as is
430                 $Id = $1;
431                 # Remember the id as new cheap local prefix
432                 $CheapPrefix = $Id;
433             }
434
435             # Remember the label
436             $Labels{$OutName}{$Id} = GenLabel();
437
438         # Check for an import statement
439         } elsif ($Line =~ /^\s*\.(?:(?:force)?import|importzp)\s+(.*?)\s*(?:;.*)?$/i) {
440
441             # Split into a list of identifiers
442             my @Ids = split (/\s*(?::\s*[A-Za-z]+\s*)?,\s*/, $1);
443
444             # Remove an address-size specifier, from the last identifier,
445             # if there is one.
446             $Ids[$#Ids] =~ s/\s*:\s*[A-Za-z]+//;
447
448             for $Id (@Ids) {
449                 $Imports{$OutName}{$Id} = GenLabel();
450             }
451
452         # Check for an export statement
453         } elsif ($Line =~ /^\s*\.export(?:zp)?\s+(.*?)\s*(?:;.*)?$/i) {
454
455             # Split into a list of identifiers
456             my @Ids = split (/\s*(?::\s*[A-Za-z]+\s*)?,\s*/, $1);
457
458             # Remove an address-size specifier, from the last identifier,
459             # if there is one.
460             $Ids[$#Ids] =~ s/\s*:\s*[A-Za-z]+//;
461
462             for $Id (@Ids) {
463                 $Exports{$Id} = sprintf ("%s#%s", $OutName, GenLabel());
464             }
465
466         # Check for an actor statement.
467         } elsif ($Line =~ /^\s*\.(?:(?:(?:con|de)struc|interrup)tor|condes)\s+([_a-z]\w*)/i) {
468             $Exports{$1} = sprintf ("%s#%s", $OutName, GenLabel());
469
470         # Check for a .proc statement
471         } elsif ($Line =~ /^\s*\.proc\s+([_a-z]\w*)/i) {
472
473             # Remember the ID as the new cheap-local prefix.
474             $CheapPrefix = $1;
475             $Labels{$OutName}{$1} = GenLabel();
476         }
477     }
478
479     # Close the input file
480     close (INPUT);
481 }
482
483
484
485 # Pass1: Read all files for the first time.
486 sub Pass1 () {
487
488     # Keep the user happy
489     Gabble ("Pass 1");
490
491     # Walk over the files
492     for my $InName (keys (%Files)) {
493         # Process one file
494         Process1 ($InName);
495     }
496 }
497
498
499
500 #-----------------------------------------------------------------------------#
501 #                                   Pass 2                                    #
502 # ----------------------------------------------------------------------------#
503
504
505
506 # Process2: Read one file the second time.
507 sub Process2 {
508
509     # Variables
510     my $Base;
511     my $Ext;
512     my $Line;
513     my $OutLine;
514     my $Id;
515     my $Label;
516     my $Comment;
517     my $Trailer;
518
519     # Input file is parameter
520     my $InName = shift(@_);
521
522     # Create the output file name from the input file name
523     my $OutName = GetOutName ($InName);
524
525     # Current cheap local label prefix is empty
526     my $CheapPrefix = "";
527
528     # Open a the input file
529     my $FileName = $Files{$InName};     # Includes path if needed
530     open (INPUT, "<$FileName") or Abort ("Cannot open $FileName: $!");
531
532     # Open the output file and print the HTML header
533     open (OUTPUT, ">$HTMLDir$OutName") or Abort ("Cannot open $OutName: $!");
534     DocHeader (OUTPUT, $InName);
535     print OUTPUT "<pre>\n";
536
537     # Keep the user happy
538     Gabble ("$FileName => $OutName");
539
540     # The instructions that will have hyperlinks if a label is used.
541     # And, they will be highlighted when color is used.
542     my $LabelIns = "adc|add|and|asl|bb[rs][0-7]|b[cv][cs]|beq|bge|bit|blt|".
543                  "bmi|bne|bpl|br[akl]|bsr|cmp|cop|cp[axy]|dec|eor|inc|jml|".
544                  "jmp|jsl|jsr|ld[axy]|lsr|mvn|mvp|ora|pe[air]|rep|".
545                  "[rs]mb[0-7]|rol|ror|sbc|sep|st[012axyz]|sub|tai|tam|tdd|".
546                  "ti[ain]|tma|trb|tsb|tst";
547
548     # Instructions that have only the implied-addressing mode -- therefore,
549     # no hyperlinking.  They will be highlighted only, when color is used.
550     my $OtherIns = "cl[acdivxy]|csh|csl|de[axy]|in[axy]|nop|ph[abdkpxy]|".
551                  "pl[abdpxy]|rt[ils]|sax|say|se[cdit]|stp|swa|sxy|ta[dsxy]|".
552                  "tam[0-7]|tcd|tcs|tda|tdc|tma[0-7]|ts[acx]|tx[asy]|tya|tyx|".
553                  "wai|xba|xce";
554
555     # Read the input file, replacing references with hyperlinks; and, mark
556     # labels as link targets.
557     my $LineNo = 0;
558     LINE: while ($Line = <INPUT>) {
559
560         # Count input lines
561         $LineNo++;
562
563         # Remove the newline
564         chomp ($Line);
565
566         # If requested, convert tabs to spaces
567         if ($CvtTabs) {
568             # Don't ask me - this is from the perl manual page
569             1 while ($Line =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e) ;
570         }
571
572         # Clear the output line
573         $OutLine = "";
574
575         # If requested, add a html label to each line with a name "linexxx",
576         # so it can be referenced from the outside (this is the same convention
577         # that is used by c2html). If we have line numbers enabled, add them.
578         if ($LineLabels && $LineNumbers) {
579             $OutLine .= sprintf ("<a name=\"line%d\">%6d</a>:  ", $LineNo, $LineNo);
580         } elsif ($LineLabels) {
581             $OutLine .= sprintf ("<a name=\"line%d\"></a>", $LineNo);
582         } elsif ($LineNumbers) {
583             $OutLine .= sprintf ("%6d:  ", $LineNo);
584         }
585
586         # Cut off a comment from the input line. Beware: We have to check for
587         # strings, since these may contain a semicolon that is no comment
588         # start.
589         ($Line, $Comment) = $Line =~ /^((?:[^"';]+|".*?"|'.*?')*)(.*)$/;
590         if ($Comment =~ /^["']/) {
591             # Line with invalid syntax - there's a string start but
592             # no string end.
593             Abort (sprintf ("Invalid input at %s(%d)", $FileName, $LineNo));
594         }
595
596         # Remove trailing whitespace and move it together with the comment
597         # into the $Trailer variable.
598         $Line =~ s/\s*$//;
599         $Trailer = $& . ColorizeComment (Cleanup ($Comment));
600
601         # Check for a label at the start of the line. If we have one, process
602         # it, and remove it from the line.
603         if ($Line =~ s/^\s*?(([\@?]?)[_a-zA-Z]\w*)(\s*(?::=?|=))//) {
604
605             # Is this a local label?
606             if ($2 ne "") {
607                 # Use the prefix
608                 $Id = "$CheapPrefix$1";
609             } else {
610                 # Use as is
611                 $Id = $1;
612                 # Remember the id as new cheap local prefix
613                 $CheapPrefix = $Id;
614             }
615
616             # Get the label for the id
617             $Label = $Labels{$OutName}{$Id};
618
619             # Print the label with a tag
620             $OutLine .= "<a name=\"$Label\">$1</a>$3";
621
622             # Is the name explicitly assigned a value?
623             if ($3 =~ /=$/) {
624                 # Print all identifiers if there are any.
625                 while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
626                     # Add the non-label stuff.
627                     $OutLine .= Cleanup ($1);
628
629                     # Use the prefix if the label is local.
630                     # Get the reference to that label if we find it.
631                     $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
632                 }
633
634                 # Add a remainder if there is one.
635                 $OutLine .= Cleanup ($Line);
636
637                 # The line is complete; print it.
638                 next LINE;
639             }
640         }
641
642         # Print any leading whitespace and remove it, so we don't have to
643         # care about whitespace below.
644         if ($Line =~ s/^\s+//) {
645             $OutLine .= $&;
646         }
647
648         # Handle the import statements
649         if ($Line =~ s/^\.(?:(?:force)?import|importzp)\s+//i) {
650
651             # Print any fixed stuff from the line and remove it
652             $OutLine .= $&;
653
654             # Print all identifiers if there are any
655             while ($Line =~ s/^[_a-zA-Z]\w*//) {
656
657                 # Remember the identifier
658                 my $Id = $&;
659
660                 # Variable to assemble HTML representation
661                 my $Contents = "";
662
663                 # Make this import a link target
664                 if (exists ($Imports{$OutName}{$Id})) {
665                     $Label = $Imports{$OutName}{$Id};
666                     $Contents .= sprintf (" name=\"%s\"", $Label);
667                 }
668
669                 # If we have an export for this import, add a link to this
670                 # export definition
671                 if (exists ($Exports{$Id})) {
672                     $Label = $Exports{$Id};
673                     $Contents .= sprintf (" href=\"%s\"", $Label);
674                 }
675
676                 # Add the HTML stuff to the output line
677                 if ($Contents ne "") {
678                     $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
679                 } else {
680                     $OutLine .= $Id;
681                 }
682
683                 # Check if another identifier follows
684                 if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
685                     $OutLine .= $&;
686                 } else {
687                     last;
688                 }
689             }
690
691             # Add an remainder if there is one
692             $OutLine .= Cleanup ($Line);
693
694         # Handle export statements
695         } elsif ($Line =~ s/^\.export(?:zp)?\s+//i) {
696
697             # Print the command and the whitespace.
698             $OutLine .= $&;
699
700             # Print all identifiers if there are any
701             while ($Line =~ s/^[_a-zA-Z]\w*//) {
702
703                 # Remember the identifier
704                 my $Id = $&;
705
706                 # Variable to assemble HTML representation
707                 my $Contents = "";
708
709                 # If we have a definition for this export in this file, add
710                 # a link to the definition.
711                 if (exists ($Labels{$OutName}{$Id})) {
712                     $Label = $Labels{$OutName}{$Id};
713                     $Contents = sprintf (" href=\"#%s\"", $Label);
714                 }
715
716                 # If we have this identifier in the list of exports, add a
717                 # jump target for the export.
718                 if (exists ($Exports{$Id})) {
719                     $Label = $Exports{$Id};
720                     # Be sure to use only the label part
721                     $Label =~ s/^.*#//;
722                     $Contents .= sprintf (" name=\"%s\"", $Label);
723                 }
724
725                 # Add the HTML stuff to the output line
726                 if ($Contents ne "") {
727                     $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
728                 } else {
729                     $OutLine .= $Id;
730                 }
731
732                 # Check if another identifier follows
733                 if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
734                     $OutLine .= $&;
735                 } else {
736                     last;
737                 }
738             }
739
740             # Add an remainder if there is one
741             $OutLine .= Cleanup ($Line);
742
743         # Handle actor statements.
744         } elsif ($Line =~ s/^(\.(?:(?:(?:con|de)struc|interrup)tor|condes)\s+)([_a-z]\w*)//i) {
745
746             # Print the command and the whitespace.
747             $OutLine .= $1;
748
749             # Remember the identifier.
750             $Id = $2;
751
752             # Variable to assemble HTML representation
753             my $Contents = "";
754
755             # If we have a definition for this actor, in this file,
756             # then add a link to that definition.
757             if (exists ($Labels{$OutName}{$Id})) {
758                 $Contents = sprintf (" href=\"#%s\"", $Labels{$OutName}{$Id});
759             }
760
761             # Get the target, for linking from imports in other files.
762             $Label = $Exports{$Id};
763             # Be sure to use only the label part.
764             $Label =~ s/^.*#//;
765
766             # Add the HTML stuff and the remainder of the actor
767             # to the output line.
768             $OutLine .= sprintf ("<a name=\"%s\"%s>%s</a>%s", $Label,
769                                  $Contents, $Id, Cleanup ($Line));
770
771         # Check for .faraddr, .addr, .dword, .word, .dbyt, .byt, .byte, .res,
772         # .elseif, .if, .align, and .org.
773         } elsif ($Line =~ s/^\.(?:(?:far)?addr|d?word|d?byte?|res|(?:else)?if|align|org)\s+//i) {
774
775             # Print the command and the white space
776             $OutLine .= $&;
777
778             # Print all identifiers if there are any
779             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
780                 # Add the non label stuff
781                 $OutLine .= Cleanup ($1);
782
783                 # Use the prefix if the label is local.
784                 # Get the reference to that label if we find it.
785                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
786             }
787
788             # Add an remainder if there is one
789             $OutLine .= Cleanup ($Line);
790
791         # Handle .proc
792         } elsif ($Line =~ /^(\.proc)(\s+)([_a-z]\w*)?(.*)$/i) {
793
794             # Do we have an identifier?
795             if ($3 ne "") {
796                 # Remember the ID as the new cheap-local prefix.
797                 $CheapPrefix = $3;
798
799                 # Get the label for the id
800                 $Label = $Labels{$OutName}{$3};
801
802                 # Print the label with a tag
803                 $OutLine .= "$1$2<a name=\"$Label\">$3</a>";
804
805             } else {
806
807                 # Print a line that has invalid syntax (its operand isn't
808                 # a correctly formed name).
809                 $OutLine .= "$1$2";
810             }
811
812             # Add the remainder
813             $OutLine .= Cleanup ($4);
814
815         # Handle .include
816         } elsif ($Line =~ /^(\.include)(\s*)\"((?:[^\"]+?|\\\")+)(\".*)$/i) {
817
818             # Add the fixed stuff to the output line
819             $OutLine .= "$1$2&quot;";
820
821             # Get the filename into a named variable
822             my $FileName = Cleanup ($3);
823
824             # Get the name without a path
825             my $Name = StripPath ($3);
826
827             # If the include file is among the list of our files, add a link,
828             # otherwise just add the name as is.
829             if (exists ($Files{$Name})) {
830                 $OutLine .= sprintf ("<a href=\"%s\">%s</a>", GetOutName ($Name), $FileName);
831             } else {
832                 $OutLine .= $FileName;
833             }
834
835             # Add the remainder
836             $OutLine .= Cleanup ($4);
837
838         # Handle .dbg line
839         } elsif ($CRefs && $Line =~ s/^\.dbg\s+//) {
840
841             # Add the fixed stuff to the output line
842             $OutLine .= $&;
843
844             # Check for the type of the .dbg directive
845             if ($Line =~ /^(line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*)$/) {
846
847                 # Add the fixed stuff to the output line
848                 $OutLine .= "$1&quot;";
849
850                 # Get the filename and line number into named variables
851                 my $DbgFile = $2;
852                 my $DbgLine = $4;
853
854                 # Remember the remainder
855                 $Line = "\"$3$4$5";
856
857                 # Get the name without a path
858                 my $Name = StripPath ($DbgFile);
859
860                 # We don't need FileName any longer as is, so clean it up
861                 $DbgFile = Cleanup ($DbgFile);
862
863                 # Add a link to the source file
864                 $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $DbgLine, $DbgFile);
865
866                 # Add the remainder
867                 $OutLine .= Cleanup ($Line);
868
869             } elsif ($Line =~ /^(file,\s*)\"((?:[^\"]+?|\\\")+)\"(.*)$/) { #pf FIXME: doesn't handle \" correctly!
870
871                 # Get the filename into a named variables
872                 my $DbgFile = Cleanup ($2);
873
874                 # Get the name without a path
875                 my $Name = Cleanup (StripPath ($2));
876
877                 # Add the fixed stuff to the output line
878                 $OutLine .= sprintf ("%s\"<a href=\"%s.html\">%s</a>\"%s",
879                                      $1, $Name, $DbgFile, $3);
880
881             } else {
882
883                 # Add the remainder
884                 $OutLine .= Cleanup ($Line);
885
886             }
887
888         } elsif ($CRefs && $Line =~ /^(\.dbg)(\s+line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*$)/) {
889
890             # Add the fixed stuff to the output line
891             $OutLine .= "$1$2&quot;";
892
893             # Get the filename and line number into named variables
894             my $FileName = $3;
895             my $LineNo   = $5;
896
897             # Remember the remainder
898             $Line = "\"$4$5$6";
899
900             # Get the name without a path
901             my $Name = StripPath ($FileName);
902
903             # We don't need FileName any longer as is, so clean it up
904             $FileName = Cleanup ($FileName);
905
906             # Add a link to the source file
907             $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $LineNo, $FileName);
908
909             # Add the remainder
910             $OutLine .= Cleanup ($Line);
911
912         # Check for .ifdef, .ifndef, .ifref, and .ifnref.
913         } elsif ($Line =~ s/^(\.ifn?[dr]ef\s+)(([\@?]?)[_a-z]\w*)?//i) {
914
915             # Print the command and the whitespace.
916             $OutLine .= $1;
917
918             if ($2 ne "") {
919                 # Use the prefix if the label is local.
920                 # Get the reference to that label if we find it.
921                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
922             }
923
924             # Add a remainder if there is one.
925             $OutLine .= Cleanup ($Line);
926
927         # Check for assertions.
928         } elsif ($Line =~ s/^(\.assert\s+)(.+?)(,\s*(?:error|warning)\s*(?:,.*)?)$/$2/i) {
929
930             # Print the command and the whitespace.
931             $OutLine .= $1;
932
933             $Comment = $3;
934
935             # Print all identifiers if there are any.
936             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
937                 # Add the non-label stuff.
938                 $OutLine .= Cleanup ($1);
939
940                 # Use the prefix if the label is local.
941                 # Get the reference to that label if we find it.
942                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
943             }
944
945             # Add a remainder if there is one.
946             $OutLine .= Cleanup ($Line . $Comment);
947
948         # Check for instructions with labels
949         } elsif ($Line =~ s/^($LabelIns)\b(\s*)//io) {
950
951             # Print the instruction and white space
952             $OutLine .= ColorizeKeyword ($1) . $2;
953
954             # Print all identifiers if there are any.
955             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
956
957                 # Add the non-label stuff.
958                 $OutLine .= Cleanup ($1);
959
960                 # Is this a local label?
961                 if ($3 ne "") {
962                     # Use the prefix
963                     $Id = "$CheapPrefix$2";
964                 } else {
965                     # Use as is
966                     $Id = $2;
967                 }
968
969                 # Get the reference to this label if we find it
970                 $OutLine .= RefLabel ($OutName, $Id, $2);
971             }
972
973             # Reassemble and print the line
974             $OutLine .= Cleanup ($Line);
975
976         # Check for all other instructions
977         } elsif ($Line =~ /^($OtherIns)\b(.*)$/io) {
978
979             # Colorize and print
980             $OutLine .= ColorizeKeyword ($1) . Cleanup ($2);
981
982         } else {
983
984             # Nothing known - print the line
985             $OutLine .= Cleanup ($Line);
986
987         }
988
989     } continue {
990         # Colorize all keywords
991         $OutLine =~ s/(?<![\w;])\.[_a-zA-Z]\w*/ColorizeCtrl ($&)/ge;
992
993         # Print the result with the trailer.
994         print OUTPUT "$OutLine$Trailer\n";
995     }
996
997     # Print the HTML footer
998     print OUTPUT "</pre>\n";
999     DocFooter (OUTPUT, $OutName);
1000
1001     # Close the files
1002     close (INPUT);
1003     close (OUTPUT);
1004 }
1005
1006
1007
1008 # Pass2: Read all files the second time.
1009 sub Pass2 () {
1010
1011     # Keep the user happy
1012     Gabble ("Pass 2");
1013
1014     # Walk over the files
1015     for my $InName (keys (%Files)) {
1016         # Process one file
1017         Process2 ($InName);
1018     }
1019 }
1020
1021
1022
1023 #-----------------------------------------------------------------------------#
1024 #                            Create an index page                             #
1025 # ----------------------------------------------------------------------------#
1026
1027
1028
1029 # Print a list of all files
1030 sub FileIndex {
1031
1032     # File is argument
1033     my $INDEX = $_[0];
1034
1035     # Print the file list in a table
1036     print $INDEX "<h2>Files</h2><p>\n";
1037     print $INDEX "<table border=\"0\" width=\"100%\">\n";
1038     my $Count = 0;
1039     for my $File (sort (keys (%Files))) {
1040
1041         #
1042         if (($Count % $IndexCols) == 0) {
1043             print $INDEX "<tr>\n";
1044         }
1045         printf $INDEX "<td><a href=\"%s\">%s</a></td>\n", GetOutName ($File), $File;
1046         if (($Count % $IndexCols) == $IndexCols-1) {
1047             print $INDEX "</tr>\n";
1048         }
1049         $Count++;
1050     }
1051     if (($Count % $IndexCols) != 0) {
1052         print $INDEX "</tr>\n";
1053     }
1054     print $INDEX "</table><p><br><p>\n";
1055 }
1056
1057
1058
1059 # Print a list of all exports
1060 sub ExportIndex {
1061
1062     # File is argument
1063     my $INDEX = $_[0];
1064
1065     # Print the file list in a table
1066     print $INDEX "<h2>Exports</h2><p>\n";
1067     print $INDEX "<table border=\"0\" width=\"100%\">\n";
1068     my $Count = 0;
1069     for my $Export (sort (keys (%Exports))) {
1070
1071         # Get the export
1072         my $File;
1073         my $Label;
1074         ($File, $Label) = split (/#/, $Exports{$Export});
1075
1076         # The label is the label of the export statement. If we can find the
1077         # actual label, use this instead.
1078         if (exists ($Labels{$File}{$Export})) {
1079             $Label = $Labels{$File}{$Export};
1080         }
1081
1082         #
1083         if (($Count % $IndexCols) == 0) {
1084             print $INDEX "<tr>\n";
1085         }
1086         printf $INDEX "<td><a href=\"%s#%s\">%s</a></td>\n", $File, $Label, $Export;
1087         if (($Count % $IndexCols) == $IndexCols-1) {
1088             print $INDEX "</tr>\n";
1089         }
1090         $Count++;
1091     }
1092     if (($Count % $IndexCols) != 0) {
1093         print $INDEX "</tr>\n";
1094     }
1095     print $INDEX "</table><p><br><p>\n";
1096 }
1097
1098
1099
1100 sub CreateIndex {
1101
1102     # Open the index page file
1103     open (INDEX, ">$HTMLDir$IndexName") or Abort ("Cannot open $IndexName: $!");
1104
1105     # Print the header
1106     DocHeader (INDEX, $IndexTitle, 0);
1107
1108     # Print the file list in a table
1109     FileIndex (INDEX);
1110     ExportIndex (INDEX);
1111
1112     # Print the document footer
1113     DocFooter (INDEX, $IndexName);
1114
1115     # Close the index file
1116     close (INDEX);
1117 }
1118
1119
1120
1121 #-----------------------------------------------------------------------------#
1122 #                           Print usage information                           #
1123 # ----------------------------------------------------------------------------#
1124
1125
1126
1127 sub Usage {
1128     print "Usage: ca65html [options] file ...\n";
1129     print "Options:\n";
1130     print "  --bgcolor c        Use background color c instead of $BGColor\n";
1131     print "  --colorize         Add color highlights to the output\n";
1132     print "  --commentcolor c   Use color c for comments instead of $CommentColor\n";
1133     print "  --crefs            Generate references to the C source file(s)\n";
1134     print "  --ctrlcolor c      Use color c for directives instead of $CtrlColor\n";
1135     print "  --cvttabs          Convert tabs to spaces in the output\n";
1136     print "  --help             This text\n";
1137     print "  --htmldir dir      Specify directory for HTML files\n";
1138     print "  --indexcols n      Use n columns on index page (default $IndexCols)\n";
1139     print "  --indexname file   Use file for the index file instead of $IndexName\n";
1140     print "  --indexpage        Create an index page\n";
1141     print "  --indextitle title Use title as the index title instead of $IndexTitle\n";
1142     print "  --keywordcolor c   Use color c for keywords instead of $KeywordColor\n";
1143     print "  --linelabels       Generate a linexxx HTML label for each line\n";
1144     print "  --linenumbers      Add line numbers to the output\n";
1145     print "  --linkstyle style  Use the given link style\n";
1146     print "  --replaceext       Replace source extension instead of appending .html\n";
1147     print "  --textcolor c      Use text color c instead of $TextColor\n";
1148     print "  --verbose          Be more verbose\n";
1149 }
1150
1151
1152
1153 #-----------------------------------------------------------------------------#
1154 #                                    Main                                     #
1155 # ----------------------------------------------------------------------------#
1156
1157
1158
1159 # Get program options
1160 GetOptions ("bgcolor=s"         => \$BGColor,
1161             "colorize"          => \$Colorize,
1162             "commentcolor=s"    => \$CommentColor,
1163             "crefs"             => \$CRefs,
1164             "ctrlcolor=s"       => \$CtrlColor,
1165             "cvttabs"           => \$CvtTabs,
1166             "debug!"            => \$Debug,
1167             "help"              => \$Help,
1168             "htmldir=s"         => \$HTMLDir,
1169             "indexcols=i"       => \$IndexCols,
1170             "indexname=s"       => \$IndexName,
1171             "indexpage"         => \$IndexPage,
1172             "indextitle=s"      => \$IndexTitle,
1173             "keywordcolor=s"    => \$KeywordColor,
1174             "linelabels"        => \$LineLabels,
1175             "linenumbers"       => \$LineNumbers,
1176             "linkstyle=i"       => \$LinkStyle,
1177             "replaceext"        => \$ReplaceExt,
1178             "textcolor=s"       => \$TextColor,
1179             "verbose!"          => \$Verbose,
1180             "<>"                => \&AddFile);
1181
1182 # Check some arguments
1183 if ($IndexCols <= 0 || $IndexCols >= 20) {
1184     Abort ("Invalid value for --indexcols option");
1185 }
1186 if ($HTMLDir ne "" && $HTMLDir =~ /[^\/]$/) {
1187     # Add a trailing path separator
1188     $HTMLDir .= "/";
1189 }
1190
1191
1192
1193 # Print help if requested
1194 if ($Help) {
1195     Usage ();
1196 }
1197
1198 # Check if we have input files given
1199 if ($FileCount == 0) {
1200     Abort ("No input files");
1201 }
1202
1203 # Convert the documents
1204 Pass1 ();
1205 Pass2 ();
1206
1207 # Generate an index page if requested
1208 if ($IndexPage) {
1209     CreateIndex ();
1210 }
1211
1212 # Done
1213 exit 0;