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