J2CPS.java 7.2 KB

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