ProgArgs.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Body of ProgArgs interface.
  3. // This file implements the code of the ProgArgs.cp file.
  4. // kjg December 1999.
  5. //
  6. // The reason that this module is implemented as a Java class is
  7. // that the name CPmain has special meaning to the compiler, so
  8. // it must be imported secretly in the implementation.
  9. //
  10. package CP.ProgArgs;
  11. import CP.CPmain.CPmain;
  12. import java.io.File;
  13. import java.nio.file.*;
  14. import java.util.ArrayList;
  15. public class ProgArgs
  16. {
  17. public static int ArgNumber()
  18. {
  19. if (CP.CPmain.CPmain.args == null)
  20. return 0;
  21. else
  22. return CP.CPmain.CPmain.args.length;
  23. }
  24. public static void GetArg(int num, char[] str)
  25. {
  26. int i;
  27. if (CP.CPmain.CPmain.args == null) {
  28. str[0] = '\0';
  29. } else {
  30. for (i = 0;
  31. i < str.length && i < CP.CPmain.CPmain.args[num].length();
  32. i++) {
  33. str[i] = CP.CPmain.CPmain.args[num].charAt(i);
  34. }
  35. if (i == str.length)
  36. i--;
  37. str[i] = '\0';
  38. }
  39. }
  40. public static void GetEnvVar(char[] ss, char[] ds)
  41. {
  42. String path = CP.CPJ.CPJ.MkStr(ss);
  43. //
  44. // getenv was deprecated between jave 1.1 and SE 5 (!)
  45. //
  46. String valu = System.getProperty(path);
  47. if (valu == null) // Try getenv instead
  48. valu = System.getenv(path);
  49. int i;
  50. for (i = 0;
  51. i < valu.length() && i < ds.length;
  52. i++) {
  53. ds[i] = valu.charAt(i);
  54. }
  55. if (i == ds.length)
  56. i--;
  57. ds[i] = '\0';
  58. }
  59. public static void ExpandWildcards(int argsToSkip) {
  60. //
  61. // The Java launcher expands wildcards, but only
  62. // for simple filenames in the current directory.
  63. //
  64. try {
  65. CP.CPmain.CPmain.args = ExpandArgs(CP.CPmain.CPmain.args, argsToSkip);
  66. } catch (Exception x) {
  67. System.err.println(x.toString());
  68. }
  69. }
  70. private static boolean needsExpansion(String arg) {
  71. return (arg.contains("*") || arg.contains("?"));
  72. }
  73. private static String[] ExpandArgs(String[] args, int first) throws Exception {
  74. ArrayList<String> list = new ArrayList<String>();
  75. for (int i = 0; i < args.length; i++) {
  76. String argS = args[i];
  77. if (i < first || argS.charAt(0) == '-' || !needsExpansion(argS)) {
  78. list.add(argS);
  79. } else {
  80. File argF = new File(args[i]);
  81. File parent = argF.getParentFile();
  82. String pattern = argF.getName();
  83. boolean implicitParent = (parent == null);
  84. if (implicitParent)
  85. parent = new File(".");
  86. try (DirectoryStream<Path> stream =
  87. Files.newDirectoryStream(parent.toPath(), pattern)) {
  88. for (Path entry: stream) {
  89. if (implicitParent)
  90. list.add(entry.toFile().getName());
  91. else
  92. list.add(entry.toString());
  93. }
  94. } catch (DirectoryIteratorException x) {
  95. throw x.getCause();
  96. }
  97. }
  98. }
  99. return list.toArray(new String[0]);
  100. }
  101. } // end of public class ProgArgs