#!/bin/perl -w # Java Macro Compiler V2: 3 may 2000: provides cpp directives to Java. # # Compiles a given set of files. Provides macros and line number directives # according to the cpp standard. The given files are first converted to # regular Java, and then compiled. The line numbers / file names of the error # messages from the Java compiler are converted to line numbers / filenames as # specified by the line number directives generated after running the files # through cpp. # # Usage: javamc [ [...] ] # # (v1, 19 apr 2000) # # (v1.1) # # 3 may 2000 - the compiler now compiles all files at once by supplying # multiple parameters to javac. # # read all files, run through cpp, collect and delete line directives, convert # to Java. #path and switches to use for cpp $CPP="cpp"; #same for javac $JAVAC="javac"; $FILE_EXT=".javam"; @javafiles=(); # run files through cpp, process line directives, generate java files $i=0; while (defined $ARGV[$i]) { if (not ($ARGV[$i] =~ /^[_a-zA-Z0-9]*$/)) { die "Filename \"$ARGV[$i]\" contains illegal characters"; } open(TEST,"<$ARGV[$i]$FILE_EXT") or die "Cannot open file \"$ARGV[$i]$FILE_EXT\" ($!)."; close(TEST); open(JAVALD,"cat $ARGV[$i]$FILE_EXT | $CPP |") or die "Cannot process file \"$ARGV[$i]$FILE_EXT\" ($!)."; open(JAVARAW,">$ARGV[$i].java") or die "Cannot write file \"$ARGV[$i].java\" ($!)."; $line_nr=1; $file="no_file"; $line=0; while () { if (/^#\s+([0-9]*)\s+"([^"]*)"/) { if ($2 eq "") { $file=$ARGV[$i]; } else { $file=$2; } $line=$1; } else { print JAVARAW $_; #print "\$file_$ARGV[$i]\[$line_nr\] = \"$file:$line\""; eval "\$file_$ARGV[$i]\[$line_nr\] = \"$file$FILE_EXT:$line\""; $line_nr++; $line++; } } push @javafiles, "$ARGV[$i]"; close(JAVALD); close(JAVARAW); $i++; } # compile java files and rewrite error headings open(JAVAERR, "$JAVAC ". join (".java ", @javafiles).".java 2>&1 |"); #open(JAVAERR,"$JAVAC $file.java 2>&1 |"); while () { # The program assumes there's a `./' prefix in front of any compiled # file that is not in the list. #s/^\.\///; if (/^([_a-zA-Z0-9]*)\.java\:([0-9]*)\:(.*)$/) { #if (defined $fileswitherror{$1}) { # print "####Not displaying $1.java because already disp.\n"; #} # only display error if not already displayed eval "print \"Java error in $1.java:$2, originates from \$file_$1\[$2\]:\n$3\n\""; } else { print $_; } }