]> git.sur5r.net Git - cc65/blob - src/ca65html/ca65html
Be more tolerant with dos/windows line endings.
[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 at the end of line. Don't use chomp to be able to
564         # read dos/windows sources on unices.
565         $Line =~ s/[\r\n]*$//;
566
567         # If requested, convert tabs to spaces
568         if ($CvtTabs) {
569             # Don't ask me - this is from the perl manual page
570             1 while ($Line =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e) ;
571         }
572
573         # Clear the output line
574         $OutLine = "";
575
576         # If requested, add a html label to each line with a name "linexxx",
577         # so it can be referenced from the outside (this is the same convention
578         # that is used by c2html). If we have line numbers enabled, add them.
579         if ($LineLabels && $LineNumbers) {
580             $OutLine .= sprintf ("<a name=\"line%d\">%6d</a>:  ", $LineNo, $LineNo);
581         } elsif ($LineLabels) {
582             $OutLine .= sprintf ("<a name=\"line%d\"></a>", $LineNo);
583         } elsif ($LineNumbers) {
584             $OutLine .= sprintf ("%6d:  ", $LineNo);
585         }
586
587         # Cut off a comment from the input line. Beware: We have to check for
588         # strings, since these may contain a semicolon that is no comment
589         # start.
590         ($Line, $Comment) = $Line =~ /^((?:[^"';]+|".*?"|'.*?')*)(.*)$/;
591         if ($Comment =~ /^["']/) {
592             # Line with invalid syntax - there's a string start but
593             # no string end.
594             Abort (sprintf ("Invalid input at %s(%d)", $FileName, $LineNo));
595         }
596
597         # Remove trailing whitespace and move it together with the comment
598         # into the $Trailer variable.
599         $Line =~ s/\s*$//;
600         $Trailer = $& . ColorizeComment (Cleanup ($Comment));
601
602         # Check for a label at the start of the line. If we have one, process
603         # it, and remove it from the line.
604         if ($Line =~ s/^\s*?(([\@?]?)[_a-zA-Z]\w*)(\s*(?::=?|=))//) {
605
606             # Is this a local label?
607             if ($2 ne "") {
608                 # Use the prefix
609                 $Id = "$CheapPrefix$1";
610             } else {
611                 # Use as is
612                 $Id = $1;
613                 # Remember the id as new cheap local prefix
614                 $CheapPrefix = $Id;
615             }
616
617             # Get the label for the id
618             $Label = $Labels{$OutName}{$Id};
619
620             # Print the label with a tag
621             $OutLine .= "<a name=\"$Label\">$1</a>$3";
622
623             # Is the name explicitly assigned a value?
624             if ($3 =~ /=$/) {
625                 # Print all identifiers if there are any.
626                 while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
627                     # Add the non-label stuff.
628                     $OutLine .= Cleanup ($1);
629
630                     # Use the prefix if the label is local.
631                     # Get the reference to that label if we find it.
632                     $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
633                 }
634
635                 # Add a remainder if there is one.
636                 $OutLine .= Cleanup ($Line);
637
638                 # The line is complete; print it.
639                 next LINE;
640             }
641         }
642
643         # Print any leading whitespace and remove it, so we don't have to
644         # care about whitespace below.
645         if ($Line =~ s/^\s+//) {
646             $OutLine .= $&;
647         }
648
649         # Handle the import statements
650         if ($Line =~ s/^\.(?:(?:force)?import|importzp)\s+//i) {
651
652             # Print any fixed stuff from the line and remove it
653             $OutLine .= $&;
654
655             # Print all identifiers if there are any
656             while ($Line =~ s/^[_a-zA-Z]\w*//) {
657
658                 # Remember the identifier
659                 my $Id = $&;
660
661                 # Variable to assemble HTML representation
662                 my $Contents = "";
663
664                 # Make this import a link target
665                 if (exists ($Imports{$OutName}{$Id})) {
666                     $Label = $Imports{$OutName}{$Id};
667                     $Contents .= sprintf (" name=\"%s\"", $Label);
668                 }
669
670                 # If we have an export for this import, add a link to this
671                 # export definition
672                 if (exists ($Exports{$Id})) {
673                     $Label = $Exports{$Id};
674                     $Contents .= sprintf (" href=\"%s\"", $Label);
675                 }
676
677                 # Add the HTML stuff to the output line
678                 if ($Contents ne "") {
679                     $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
680                 } else {
681                     $OutLine .= $Id;
682                 }
683
684                 # Check if another identifier follows
685                 if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
686                     $OutLine .= $&;
687                 } else {
688                     last;
689                 }
690             }
691
692             # Add an remainder if there is one
693             $OutLine .= Cleanup ($Line);
694
695         # Handle export statements
696         } elsif ($Line =~ s/^\.export(?:zp)?\s+//i) {
697
698             # Print the command and the whitespace.
699             $OutLine .= $&;
700
701             # Print all identifiers if there are any
702             while ($Line =~ s/^[_a-zA-Z]\w*//) {
703
704                 # Remember the identifier
705                 my $Id = $&;
706
707                 # Variable to assemble HTML representation
708                 my $Contents = "";
709
710                 # If we have a definition for this export in this file, add
711                 # a link to the definition.
712                 if (exists ($Labels{$OutName}{$Id})) {
713                     $Label = $Labels{$OutName}{$Id};
714                     $Contents = sprintf (" href=\"#%s\"", $Label);
715                 }
716
717                 # If we have this identifier in the list of exports, add a
718                 # jump target for the export.
719                 if (exists ($Exports{$Id})) {
720                     $Label = $Exports{$Id};
721                     # Be sure to use only the label part
722                     $Label =~ s/^.*#//;
723                     $Contents .= sprintf (" name=\"%s\"", $Label);
724                 }
725
726                 # Add the HTML stuff to the output line
727                 if ($Contents ne "") {
728                     $OutLine .= sprintf ("<a%s>%s</a>", $Contents, $Id);
729                 } else {
730                     $OutLine .= $Id;
731                 }
732
733                 # Check if another identifier follows
734                 if ($Line =~ s/^\s*(?::\s*[A-Za-z]+\s*)?,\s*//) {
735                     $OutLine .= $&;
736                 } else {
737                     last;
738                 }
739             }
740
741             # Add an remainder if there is one
742             $OutLine .= Cleanup ($Line);
743
744         # Handle actor statements.
745         } elsif ($Line =~ s/^(\.(?:(?:(?:con|de)struc|interrup)tor|condes)\s+)([_a-z]\w*)//i) {
746
747             # Print the command and the whitespace.
748             $OutLine .= $1;
749
750             # Remember the identifier.
751             $Id = $2;
752
753             # Variable to assemble HTML representation
754             my $Contents = "";
755
756             # If we have a definition for this actor, in this file,
757             # then add a link to that definition.
758             if (exists ($Labels{$OutName}{$Id})) {
759                 $Contents = sprintf (" href=\"#%s\"", $Labels{$OutName}{$Id});
760             }
761
762             # Get the target, for linking from imports in other files.
763             $Label = $Exports{$Id};
764             # Be sure to use only the label part.
765             $Label =~ s/^.*#//;
766
767             # Add the HTML stuff and the remainder of the actor
768             # to the output line.
769             $OutLine .= sprintf ("<a name=\"%s\"%s>%s</a>%s", $Label,
770                                  $Contents, $Id, Cleanup ($Line));
771
772         # Check for .faraddr, .addr, .dword, .word, .dbyt, .byt, .byte, .res,
773         # .elseif, .if, .align, and .org.
774         } elsif ($Line =~ s/^\.(?:(?:far)?addr|d?word|d?byte?|res|(?:else)?if|align|org)\s+//i) {
775
776             # Print the command and the white space
777             $OutLine .= $&;
778
779             # Print all identifiers if there are any
780             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
781                 # Add the non label stuff
782                 $OutLine .= Cleanup ($1);
783
784                 # Use the prefix if the label is local.
785                 # Get the reference to that label if we find it.
786                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
787             }
788
789             # Add an remainder if there is one
790             $OutLine .= Cleanup ($Line);
791
792         # Handle .proc
793         } elsif ($Line =~ /^(\.proc)(\s+)([_a-z]\w*)?(.*)$/i) {
794
795             # Do we have an identifier?
796             if ($3 ne "") {
797                 # Remember the ID as the new cheap-local prefix.
798                 $CheapPrefix = $3;
799
800                 # Get the label for the id
801                 $Label = $Labels{$OutName}{$3};
802
803                 # Print the label with a tag
804                 $OutLine .= "$1$2<a name=\"$Label\">$3</a>";
805
806             } else {
807
808                 # Print a line that has invalid syntax (its operand isn't
809                 # a correctly formed name).
810                 $OutLine .= "$1$2";
811             }
812
813             # Add the remainder
814             $OutLine .= Cleanup ($4);
815
816         # Handle .include
817         } elsif ($Line =~ /^(\.include)(\s*)\"((?:[^\"]+?|\\\")+)(\".*)$/i) {
818
819             # Add the fixed stuff to the output line
820             $OutLine .= "$1$2&quot;";
821
822             # Get the filename into a named variable
823             my $FileName = Cleanup ($3);
824
825             # Get the name without a path
826             my $Name = StripPath ($3);
827
828             # If the include file is among the list of our files, add a link,
829             # otherwise just add the name as is.
830             if (exists ($Files{$Name})) {
831                 $OutLine .= sprintf ("<a href=\"%s\">%s</a>", GetOutName ($Name), $FileName);
832             } else {
833                 $OutLine .= $FileName;
834             }
835
836             # Add the remainder
837             $OutLine .= Cleanup ($4);
838
839         # Handle .dbg line
840         } elsif ($CRefs && $Line =~ s/^\.dbg\s+//) {
841
842             # Add the fixed stuff to the output line
843             $OutLine .= $&;
844
845             # Check for the type of the .dbg directive
846             if ($Line =~ /^(line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*)$/) {
847
848                 # Add the fixed stuff to the output line
849                 $OutLine .= "$1&quot;";
850
851                 # Get the filename and line number into named variables
852                 my $DbgFile = $2;
853                 my $DbgLine = $4;
854
855                 # Remember the remainder
856                 $Line = "\"$3$4$5";
857
858                 # Get the name without a path
859                 my $Name = StripPath ($DbgFile);
860
861                 # We don't need FileName any longer as is, so clean it up
862                 $DbgFile = Cleanup ($DbgFile);
863
864                 # Add a link to the source file
865                 $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $DbgLine, $DbgFile);
866
867                 # Add the remainder
868                 $OutLine .= Cleanup ($Line);
869
870             } elsif ($Line =~ /^(file,\s*)\"((?:[^\"]+?|\\\")+)\"(.*)$/) { #pf FIXME: doesn't handle \" correctly!
871
872                 # Get the filename into a named variables
873                 my $DbgFile = Cleanup ($2);
874
875                 # Get the name without a path
876                 my $Name = Cleanup (StripPath ($2));
877
878                 # Add the fixed stuff to the output line
879                 $OutLine .= sprintf ("%s\"<a href=\"%s.html\">%s</a>\"%s",
880                                      $1, $Name, $DbgFile, $3);
881
882             } else {
883
884                 # Add the remainder
885                 $OutLine .= Cleanup ($Line);
886
887             }
888
889         } elsif ($CRefs && $Line =~ /^(\.dbg)(\s+line,\s*)\"((?:[^\"]+?|\\\")+)\"(,\s*)(\d+)(.*$)/) {
890
891             # Add the fixed stuff to the output line
892             $OutLine .= "$1$2&quot;";
893
894             # Get the filename and line number into named variables
895             my $FileName = $3;
896             my $LineNo   = $5;
897
898             # Remember the remainder
899             $Line = "\"$4$5$6";
900
901             # Get the name without a path
902             my $Name = StripPath ($FileName);
903
904             # We don't need FileName any longer as is, so clean it up
905             $FileName = Cleanup ($FileName);
906
907             # Add a link to the source file
908             $OutLine .= sprintf ("<a href=\"%s.html#line%d\">%s</a>", $Name, $LineNo, $FileName);
909
910             # Add the remainder
911             $OutLine .= Cleanup ($Line);
912
913         # Check for .ifdef, .ifndef, .ifref, and .ifnref.
914         } elsif ($Line =~ s/^(\.ifn?[dr]ef\s+)(([\@?]?)[_a-z]\w*)?//i) {
915
916             # Print the command and the whitespace.
917             $OutLine .= $1;
918
919             if ($2 ne "") {
920                 # Use the prefix if the label is local.
921                 # Get the reference to that label if we find it.
922                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
923             }
924
925             # Add a remainder if there is one.
926             $OutLine .= Cleanup ($Line);
927
928         # Check for assertions.
929         } elsif ($Line =~ s/^(\.assert\s+)(.+?)(,\s*(?:error|warning)\s*(?:,.*)?)$/$2/i) {
930
931             # Print the command and the whitespace.
932             $OutLine .= $1;
933
934             $Comment = $3;
935
936             # Print all identifiers if there are any.
937             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
938                 # Add the non-label stuff.
939                 $OutLine .= Cleanup ($1);
940
941                 # Use the prefix if the label is local.
942                 # Get the reference to that label if we find it.
943                 $OutLine .= RefLabel ($OutName, ($3 ne "") ? "$CheapPrefix$2" : $2, $2);
944             }
945
946             # Add a remainder if there is one.
947             $OutLine .= Cleanup ($Line . $Comment);
948
949         # Check for instructions with labels
950         } elsif ($Line =~ s/^($LabelIns)\b(\s*)//io) {
951
952             # Print the instruction and white space
953             $OutLine .= ColorizeKeyword ($1) . $2;
954
955             # Print all identifiers if there are any.
956             while ($Line =~ s/^([^_a-zA-Z]*?)(([\@?]?)[_a-zA-Z]\w*)//) {
957
958                 # Add the non-label stuff.
959                 $OutLine .= Cleanup ($1);
960
961                 # Is this a local label?
962                 if ($3 ne "") {
963                     # Use the prefix
964                     $Id = "$CheapPrefix$2";
965                 } else {
966                     # Use as is
967                     $Id = $2;
968                 }
969
970                 # Get the reference to this label if we find it
971                 $OutLine .= RefLabel ($OutName, $Id, $2);
972             }
973
974             # Reassemble and print the line
975             $OutLine .= Cleanup ($Line);
976
977         # Check for all other instructions
978         } elsif ($Line =~ /^($OtherIns)\b(.*)$/io) {
979
980             # Colorize and print
981             $OutLine .= ColorizeKeyword ($1) . Cleanup ($2);
982
983         } else {
984
985             # Nothing known - print the line
986             $OutLine .= Cleanup ($Line);
987
988         }
989
990     } continue {
991         # Colorize all keywords
992         $OutLine =~ s/(?<![\w;])\.[_a-zA-Z]\w*/ColorizeCtrl ($&)/ge;
993
994         # Print the result with the trailer.
995         print OUTPUT "$OutLine$Trailer\n";
996     }
997
998     # Print the HTML footer
999     print OUTPUT "</pre>\n";
1000     DocFooter (OUTPUT, $OutName);
1001
1002     # Close the files
1003     close (INPUT);
1004     close (OUTPUT);
1005 }
1006
1007
1008
1009 # Pass2: Read all files the second time.
1010 sub Pass2 () {
1011
1012     # Keep the user happy
1013     Gabble ("Pass 2");
1014
1015     # Walk over the files
1016     for my $InName (keys (%Files)) {
1017         # Process one file
1018         Process2 ($InName);
1019     }
1020 }
1021
1022
1023
1024 #-----------------------------------------------------------------------------#
1025 #                            Create an index page                             #
1026 # ----------------------------------------------------------------------------#
1027
1028
1029
1030 # Print a list of all files
1031 sub FileIndex {
1032
1033     # File is argument
1034     my $INDEX = $_[0];
1035
1036     # Print the file list in a table
1037     print $INDEX "<h2>Files</h2><p>\n";
1038     print $INDEX "<table border=\"0\" width=\"100%\">\n";
1039     my $Count = 0;
1040     for my $File (sort (keys (%Files))) {
1041
1042         #
1043         if (($Count % $IndexCols) == 0) {
1044             print $INDEX "<tr>\n";
1045         }
1046         printf $INDEX "<td><a href=\"%s\">%s</a></td>\n", GetOutName ($File), $File;
1047         if (($Count % $IndexCols) == $IndexCols-1) {
1048             print $INDEX "</tr>\n";
1049         }
1050         $Count++;
1051     }
1052     if (($Count % $IndexCols) != 0) {
1053         print $INDEX "</tr>\n";
1054     }
1055     print $INDEX "</table><p><br><p>\n";
1056 }
1057
1058
1059
1060 # Print a list of all exports
1061 sub ExportIndex {
1062
1063     # File is argument
1064     my $INDEX = $_[0];
1065
1066     # Print the file list in a table
1067     print $INDEX "<h2>Exports</h2><p>\n";
1068     print $INDEX "<table border=\"0\" width=\"100%\">\n";
1069     my $Count = 0;
1070     for my $Export (sort (keys (%Exports))) {
1071
1072         # Get the export
1073         my $File;
1074         my $Label;
1075         ($File, $Label) = split (/#/, $Exports{$Export});
1076
1077         # The label is the label of the export statement. If we can find the
1078         # actual label, use this instead.
1079         if (exists ($Labels{$File}{$Export})) {
1080             $Label = $Labels{$File}{$Export};
1081         }
1082
1083         #
1084         if (($Count % $IndexCols) == 0) {
1085             print $INDEX "<tr>\n";
1086         }
1087         printf $INDEX "<td><a href=\"%s#%s\">%s</a></td>\n", $File, $Label, $Export;
1088         if (($Count % $IndexCols) == $IndexCols-1) {
1089             print $INDEX "</tr>\n";
1090         }
1091         $Count++;
1092     }
1093     if (($Count % $IndexCols) != 0) {
1094         print $INDEX "</tr>\n";
1095     }
1096     print $INDEX "</table><p><br><p>\n";
1097 }
1098
1099
1100
1101 sub CreateIndex {
1102
1103     # Open the index page file
1104     open (INDEX, ">$HTMLDir$IndexName") or Abort ("Cannot open $IndexName: $!");
1105
1106     # Print the header
1107     DocHeader (INDEX, $IndexTitle, 0);
1108
1109     # Print the file list in a table
1110     FileIndex (INDEX);
1111     ExportIndex (INDEX);
1112
1113     # Print the document footer
1114     DocFooter (INDEX, $IndexName);
1115
1116     # Close the index file
1117     close (INDEX);
1118 }
1119
1120
1121
1122 #-----------------------------------------------------------------------------#
1123 #                           Print usage information                           #
1124 # ----------------------------------------------------------------------------#
1125
1126
1127
1128 sub Usage {
1129     print "Usage: ca65html [options] file ...\n";
1130     print "Options:\n";
1131     print "  --bgcolor c        Use background color c instead of $BGColor\n";
1132     print "  --colorize         Add color highlights to the output\n";
1133     print "  --commentcolor c   Use color c for comments instead of $CommentColor\n";
1134     print "  --crefs            Generate references to the C source file(s)\n";
1135     print "  --ctrlcolor c      Use color c for directives instead of $CtrlColor\n";
1136     print "  --cvttabs          Convert tabs to spaces in the output\n";
1137     print "  --help             This text\n";
1138     print "  --htmldir dir      Specify directory for HTML files\n";
1139     print "  --indexcols n      Use n columns on index page (default $IndexCols)\n";
1140     print "  --indexname file   Use file for the index file instead of $IndexName\n";
1141     print "  --indexpage        Create an index page\n";
1142     print "  --indextitle title Use title as the index title instead of $IndexTitle\n";
1143     print "  --keywordcolor c   Use color c for keywords instead of $KeywordColor\n";
1144     print "  --linelabels       Generate a linexxx HTML label for each line\n";
1145     print "  --linenumbers      Add line numbers to the output\n";
1146     print "  --linkstyle style  Use the given link style\n";
1147     print "  --replaceext       Replace source extension instead of appending .html\n";
1148     print "  --textcolor c      Use text color c instead of $TextColor\n";
1149     print "  --verbose          Be more verbose\n";
1150 }
1151
1152
1153
1154 #-----------------------------------------------------------------------------#
1155 #                                    Main                                     #
1156 # ----------------------------------------------------------------------------#
1157
1158
1159
1160 # Get program options
1161 GetOptions ("bgcolor=s"         => \$BGColor,
1162             "colorize"          => \$Colorize,
1163             "commentcolor=s"    => \$CommentColor,
1164             "crefs"             => \$CRefs,
1165             "ctrlcolor=s"       => \$CtrlColor,
1166             "cvttabs"           => \$CvtTabs,
1167             "debug!"            => \$Debug,
1168             "help"              => \$Help,
1169             "htmldir=s"         => \$HTMLDir,
1170             "indexcols=i"       => \$IndexCols,
1171             "indexname=s"       => \$IndexName,
1172             "indexpage"         => \$IndexPage,
1173             "indextitle=s"      => \$IndexTitle,
1174             "keywordcolor=s"    => \$KeywordColor,
1175             "linelabels"        => \$LineLabels,
1176             "linenumbers"       => \$LineNumbers,
1177             "linkstyle=i"       => \$LinkStyle,
1178             "replaceext"        => \$ReplaceExt,
1179             "textcolor=s"       => \$TextColor,
1180             "verbose!"          => \$Verbose,
1181             "<>"                => \&AddFile);
1182
1183 # Check some arguments
1184 if ($IndexCols <= 0 || $IndexCols >= 20) {
1185     Abort ("Invalid value for --indexcols option");
1186 }
1187 if ($HTMLDir ne "" && $HTMLDir =~ /[^\/]$/) {
1188     # Add a trailing path separator
1189     $HTMLDir .= "/";
1190 }
1191
1192
1193
1194 # Print help if requested
1195 if ($Help) {
1196     Usage ();
1197 }
1198
1199 # Check if we have input files given
1200 if ($FileCount == 0) {
1201     Abort ("No input files");
1202 }
1203
1204 # Convert the documents
1205 Pass1 ();
1206 Pass2 ();
1207
1208 # Generate an index page if requested
1209 if ($IndexPage) {
1210     CreateIndex ();
1211 }
1212
1213 # Done
1214 exit 0;