package c99.driver;

import java.io.FileInputStream;

import c99.DummyErrorReporter;
import c99.ISourceRange;
import c99.parser.SymTable;
import c99.parser.pp.PPLexer;
import c99.parser.pp.Prepr;

public class Preprocessor
{
public static void main ( String[] args )
{
  try
  {
    DummyErrorReporter reporter = new DummyErrorReporter();
    SymTable symTable = new SymTable();
    Prepr pp = new Prepr( reporter, args[0], new FileInputStream( args[0] ), symTable );

    String lastFile = "";
    int lastLine = -1;
    PPLexer.Token tok;
    while ((tok = pp.nextToken()).code() != PPLexer.Code.EOF)
    {
      if (!tok.getFileName().equals( lastFile ))
      {
        System.out.format(  "\n# %d %s\n", tok.getLine1(), tok.getFileName() );
        lastLine = tok.getLine1();
      }
      else if (tok.getLine1() != lastLine)
      {
        if (tok.getLine1() - lastLine <= 10)
        {
          do
            System.out.println();
          while (++lastLine < tok.getLine1());
        }
        else
          System.out.format( "\n# %d\n", tok.getLine1() );
        lastLine = tok.getLine1();
      }

      lastFile = tok.getFileName();

      tok.output( System.out );
      if (false)
        System.err.println( tok );
    }
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}
} // class

@@ -268,24 +268,919 @@
 %7B%0A  try%0A  %7B%0A
+    boolean cpp = true;%0A    boolean toks = false;%0A    String fileName = null;%0A%0A    for ( String arg : args )%0A    %7B%0A      if (%22--toks%22.equals( arg ))%0A      %7B%0A        toks = true;%0A        cpp = false;%0A      %7D%0A      else if (%22--no-toks%22.equals( arg ))%0A        toks = false;%0A      else if (%22--cpp%22.equals( arg ))%0A        cpp = true;%0A      else if (%22--no-cpp%22.equals( arg ))%0A        cpp = false;%0A      else if (arg.startsWith( %22-%22))%0A      %7B%0A        System.err.println( %22**fatal: unknown command line option '%22+arg +%22'%22 );%0A        System.exit(1);%0A      %7D%0A      else%0A      %7B%0A        if (fileName != null)%0A        %7B%0A          System.err.println( %22**fatal: More than one input filename specified%22 );%0A          System.exit(1);%0A        %7D%0A        fileName = arg;%0A      %7D%0A    %7D%0A%0A    if (fileName == null)%0A    %7B%0A      System.err.println( %22**fatal: No input filename specified%22 );%0A      System.exit(1);%0A    %7D%0A%0A
     DummyErr
@@ -1303,23 +1303,24 @@
 porter, 
-args%5B0%5D
+fileName
 , new Fi
@@ -1338,15 +1338,16 @@
 am( 
-args%5B0%5D
+fileName
  ), 
@@ -1555,32 +1555,41 @@
 %0A      %7B%0A       
+ if (cpp)
  System.out.form
@@ -1810,32 +1810,41 @@
   do%0A           
+ if (cpp)
  System.out.prin
@@ -1921,32 +1921,41 @@
   else%0A         
+ if (cpp)
  System.out.form
@@ -2070,24 +2070,33 @@
 me();%0A%0A     
+ if (cpp)
  tok.output(
@@ -2124,30 +2124,21 @@
 if (
-false)%0A       
+toks)
  System.
 err.
@@ -2125,35 +2125,35 @@
 f (toks) System.
-err
+out
 .println( tok );

add simple command line parameter handling