J2CPS.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**********************************************************************/
  2. /* Main class for j2cps */
  3. /* */
  4. /* (c) copyright QUT, John Gough 2000-2012, John Gough, 2012-2017 */
  5. /**********************************************************************/
  6. package j2cps;
  7. import java.io.IOException;
  8. import java.util.jar.JarFile;
  9. import j2cpsfiles.j2cpsfiles;
  10. public class j2cps {
  11. static String argString;
  12. /**
  13. * Main program. Takes a package name as a parameter, produces the
  14. * Component Pascal symbol file. The class-files of the package must
  15. * be nested in a directory with the package name.
  16. * In the case of the -jar option the last argument is the path from
  17. * the current working directory to the target jar file.
  18. */
  19. public static void main(String args[]) {
  20. int argLen = args.length;
  21. String argStr = null;
  22. boolean anonPack = false;
  23. boolean seenParg = false;
  24. MkArgString(args);
  25. TypeDesc.InitTypes();
  26. if (argLen == 0) {
  27. System.err.println("j2cps version 1.4.0.2 (March 2017)");
  28. System.err.println("Usage:");
  29. System.err.println("java [VM-opts] j2cps.j2cps [options] PackageNameOrJarFile");
  30. System.err.println("java [VM-opts] -jar j2cps.jar [options] PackageNameOrJarFile");
  31. System.err.println("J2cps options may be in any order.");
  32. System.err.println(" -d[st] dir => symbol file destination directory");
  33. System.err.println(" -p[kg] dir => package-root directory");
  34. System.err.println(" -jar => process the named jar file");
  35. System.err.println(" -s[ummary] => summary of progress");
  36. System.err.println(" -v[erbose] => verbose diagnostic messages");
  37. System.err.println(" -nocpsym => only use sym-files from destination,");
  38. System.err.println(" (overrides any CPSYM path setting)");
  39. System.exit(0);
  40. }
  41. else {
  42. int argIx = 0;
  43. argStr = args[argIx];
  44. while (argStr.startsWith("-")) {
  45. String optString = argStr.substring(1);
  46. /* parse options here */
  47. switch (argStr.charAt(1)) {
  48. case 'V':
  49. if ("VERBOSE".startsWith(optString)) {
  50. ClassDesc.VERBOSE = true;
  51. ClassDesc.verbose = true;
  52. ClassDesc.summary = true;
  53. } else
  54. BadOption(argStr);
  55. break;
  56. case 'v':
  57. if ("verbose".startsWith(optString)) {
  58. ClassDesc.verbose = true;
  59. ClassDesc.summary = true;
  60. j2cpsfiles.SetVerbose( true );
  61. j2cpsfiles.SetSummary( true );
  62. } else
  63. BadOption(argStr);
  64. break;
  65. case 's':
  66. if ("summary".startsWith(optString)) {
  67. ClassDesc.summary = true;
  68. j2cpsfiles.SetSummary( true );
  69. } else
  70. BadOption(argStr);
  71. break;
  72. case 'd':
  73. if (!"dst".startsWith(optString))
  74. BadOption(argStr);
  75. else if (argIx + 1 < argLen) {
  76. argStr = args[++argIx];
  77. j2cpsfiles.SetDstDir(argStr);
  78. } else
  79. System.err.println(
  80. "-d option is missing symfile destination directory name");
  81. break;
  82. case 'p':
  83. if (!"pkg".startsWith(optString))
  84. BadOption(argStr);
  85. else if (argIx + 1 < argLen) {
  86. seenParg = true;
  87. argStr = args[++argIx];
  88. j2cpsfiles.SetPackageRootDir(argStr);
  89. } else
  90. System.err.println(
  91. "-p option is missing package-root directory name");
  92. break;
  93. case 'j':
  94. if (optString.equalsIgnoreCase("jar")) {
  95. ClassDesc.useJar = true;
  96. } else
  97. BadOption(argStr);
  98. break;
  99. case 'n':
  100. if (optString.equalsIgnoreCase("nocpsym")) {
  101. ClassDesc.nocpsym = true;
  102. } else
  103. BadOption(argStr);
  104. break;
  105. default:
  106. BadOption(argStr);
  107. break;
  108. }
  109. if (argIx + 1 < argLen) {
  110. argStr = args[++argIx];
  111. } else {
  112. System.err.println("No package-name or jar filename given, terminating");
  113. System.exit(1);
  114. }
  115. }
  116. //
  117. // Consistency checks ...
  118. //
  119. if (ClassDesc.useJar && seenParg) {
  120. System.out.println("Cannot use both -jar and -p. Ignoring -p");
  121. }
  122. if (ClassDesc.summary)
  123. System.out.println(argString);
  124. j2cpsfiles.GetPaths(ClassDesc.nocpsym);
  125. }
  126. try {
  127. if (ClassDesc.useJar) {
  128. if (ClassDesc.useJar && !argStr.toLowerCase().endsWith(".jar")) {
  129. System.err.println("After -jar, filename must end \".jar\"");
  130. System.exit(1);
  131. }
  132. JarFile jf = new JarFile(argStr);
  133. JarHandler jh = new JarHandler();
  134. jh.ProcessJar(jf);
  135. PackageDesc.ProcessJarDependencies();
  136. } else {
  137. PackageDesc.MakeRootPackageDescriptor(argStr, anonPack);
  138. PackageDesc.ProcessPkgWorklist();
  139. }
  140. PackageDesc.WriteSymbolFiles();
  141. }
  142. catch (IOException e) {
  143. System.err.println("IOException occurs while reading input file.");
  144. System.err.println( e.getMessage() );
  145. System.err.println("Aborting...");
  146. e.printStackTrace(System.err);
  147. System.exit(1);
  148. }
  149. }
  150. static private void BadOption(String s) {
  151. System.err.println("Unknown option " + s);
  152. }
  153. static void MkArgString( String[] args ) {
  154. StringBuilder bldr = new StringBuilder( "J2cps args>");
  155. for (String arg : args) {
  156. bldr.append(' ');
  157. bldr.append(arg);
  158. }
  159. argString = bldr.toString();
  160. }
  161. }