c9b54c6d5ad37e364fde124815ba5981eb80a162
[oweals/gnunet.git] / src / monkey / seaspider / org / gnunet / seaspider / Seaspider.java
1 package org.gnunet.seaspider;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FilenameFilter;
7
8 import org.gnunet.seaspider.parser.CParser;
9 import org.gnunet.seaspider.parser.ParseException;
10 import org.gnunet.seaspider.parser.TokenMgrError;
11 import org.gnunet.seaspider.parser.nodes.Node;
12
13 public class Seaspider {
14         
15         private static final boolean DEBUG = true;
16         private static CParser parser = null;
17         private static boolean isFirstFile = true;
18         private static int successCount = 0;
19         private static int failureCount = 0;
20         private static FilenameFilter filter = new FilenameFilter() {
21                 public boolean accept(File dir, String fileName) {
22                         File file = new File(dir.getPath() + "/" + fileName);
23                         if ((file.isDirectory() && !fileName.startsWith(".")) || (fileName.endsWith(".c") && !fileName.startsWith("test_")))
24                                 /* directories like .svn are of no interest */
25                                 return true;
26                         return false;
27                 }
28         };
29         
30         
31         private static void doParseFile(String filePath)
32         {
33                 try {
34                  if (isFirstFile) {
35                          parser = new CParser(new FileInputStream(filePath));
36                          isFirstFile = false;
37                  }
38                  else
39                          parser.ReInit(new FileInputStream(filePath));
40                  }
41                  catch (FileNotFoundException e) {
42                          /* This should never happen */
43                          System.err.println("File not found!");
44                          e.printStackTrace();
45                          System.exit(1);
46                  }
47                  try {
48                          System.out.println("Parsing file: " + filePath);
49                  Node root = parser.TranslationUnit();
50                  root.accept(new ExpressionExtractorVisitor(filePath));
51                  System.out.println("File " + filePath + " parsed successfully.");
52                  successCount++;
53              }
54              catch (ParseException e) {
55                  System.out.println("Encountered errors during parsing file " + filePath);
56                  failureCount++;
57                  if (DEBUG)
58                          e.printStackTrace();
59              } catch (TokenMgrError e) {
60                         System.err.println("Encountered errors during parsing file " + filePath + ":" + e.getMessage());
61                         failureCount++;
62                         if (DEBUG)
63                                 e.printStackTrace();                     
64                 }
65         }
66         
67         
68         private static void parseRecursively(String path)
69         {
70                 File file = new File(path);
71                 
72                 if (!file.isDirectory()) {
73                         if (path.endsWith(".db"))
74                                 return;
75                         /* A source file */
76                         doParseFile(path);
77                         return;
78                 }
79                 
80                 /* A source directory */
81                 System.out.println("Reading from: " + path + " source directory...");
82                 String[] dirContents = file.list(filter);/* Only directories and .c files */
83                 for (int i = 0; i < dirContents.length; i++) {
84                         String fullPath = path + "/" + dirContents[i];
85                         parseRecursively(fullPath);
86                 }
87         }
88         
89    public static void main(String args[])
90    {
91      String dbFilePath = null;
92      
93      if (args.length < 2)
94      {
95          System.err.println("Invoke seaspider with database filename and source path!");
96          System.exit(1);
97      }    
98      System.out.println("Seaspider 0.0\n");
99      
100      
101      for (int i = 0; i < args.length; i++) {
102          if (args[i].endsWith(".db"))
103                  dbFilePath = args[i];
104          else {
105                  /* Should be a valid path for a file or a directory */
106                  File file = new File(args[i]);
107                  if (!file.exists()) {
108                          System.err.println("\"" + args[i] + "\" is an invalid file or directory location");
109                          System.exit(1);
110                  } else if (!file.isDirectory() && !args[i].endsWith(".c")) {
111                          System.err.println("\"" + args[i] + "\" only source files can be parsed");
112                  }
113          }
114      }
115      if (null == dbFilePath) {
116          System.err.println("Missing database file path");
117          System.exit(1);
118      }
119      
120      /* Create the Expressions Database */
121      ExpressionDatabaseHandler.createExpressionDatabase(dbFilePath);
122      
123      for (int i = 0; i < args.length; i++)
124          parseRecursively(args[i]);
125      
126      /* We're done with the Expression Database, close it */
127      ExpressionDatabaseHandler.closeDatabase();
128      
129      System.out.println(successCount + " parsed successfully.");
130      System.out.println("Failed to parse " + failureCount + " files.");
131   }
132 }