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