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