src/jdk.jpackage/share/classes/jdk/jpackage/main/CommandLine.java
branchJDK-8200758-branch
changeset 57438 4a31db8d42bd
parent 57404 a477b26bf888
child 57447 3fbe47a62ed0
equal deleted inserted replaced
57421:0410ee319681 57438:4a31db8d42bd
    34 import java.util.Arrays;
    34 import java.util.Arrays;
    35 import java.util.List;
    35 import java.util.List;
    36 
    36 
    37 /**
    37 /**
    38  * This file is direct copy of CommandLine.java in com.sun.tools.javac.main.
    38  * This file is direct copy of CommandLine.java in com.sun.tools.javac.main.
    39  * It should not be modified other than the package declaration above,
       
    40  * the copyright year and this comment.
       
    41  * It should track changes made to that file.
    39  * It should track changes made to that file.
       
    40  * It is modified only to remove content not used by jpackage
    42  */
    41  */
    43 
    42 
    44 /**
    43 /**
    45  * Various utility methods for processing Java tool command line arguments.
    44  * Various utility methods for processing Java tool command line arguments.
    46  *
    45  *
    82                 newArgs.add(arg);
    81                 newArgs.add(arg);
    83             }
    82             }
    84         }
    83         }
    85     }
    84     }
    86 
    85 
    87     /**
       
    88      * Process the given environment variable and appends any Win32-style
       
    89      * command files for the specified command line arguments and return
       
    90      * the resulting arguments. A command file argument
       
    91      * is of the form '@file' where 'file' is the name of the file whose
       
    92      * contents are to be parsed for additional arguments. The contents of
       
    93      * the command file are parsed using StreamTokenizer and the original
       
    94      * '@file' argument replaced with the resulting tokens. Recursive command
       
    95      * files are not supported. The '@' character itself can be quoted with
       
    96      * the sequence '@@'.
       
    97      * @param envVariable the env variable to process
       
    98      * @param args the arguments that may contain @files
       
    99      * @return the arguments, with environment variable's content and expansion of @files
       
   100      * @throws IOException if there is a problem reading any of the @files
       
   101      * @throws UnmatchedQuote
       
   102      */
       
   103     public static List<String> parse(String envVariable, List<String> args)
       
   104             throws IOException, UnmatchedQuote {
       
   105 
       
   106         List<String> inArgs = new ArrayList<>();
       
   107         appendParsedEnvVariables(inArgs, envVariable);
       
   108         inArgs.addAll(args);
       
   109         List<String> newArgs = new ArrayList<>();
       
   110         appendParsedCommandArgs(newArgs, inArgs);
       
   111         return newArgs;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Process the given environment variable and appends any Win32-style
       
   116      * command files for the specified command line arguments and return
       
   117      * the resulting arguments. A command file argument
       
   118      * is of the form '@file' where 'file' is the name of the file whose
       
   119      * contents are to be parsed for additional arguments. The contents of
       
   120      * the command file are parsed using StreamTokenizer and the original
       
   121      * '@file' argument replaced with the resulting tokens. Recursive command
       
   122      * files are not supported. The '@' character itself can be quoted with
       
   123      * the sequence '@@'.
       
   124      * @param envVariable the env variable to process
       
   125      * @param args the arguments that may contain @files
       
   126      * @return the arguments, with environment variable's content and expansion of @files
       
   127      * @throws IOException if there is a problem reading any of the @files
       
   128      * @throws UnmatchedQuote
       
   129      */
       
   130     public static String[] parse(String envVariable, String[] args) throws IOException, UnmatchedQuote {
       
   131         List<String> out = parse(envVariable, Arrays.asList(args));
       
   132         return out.toArray(new String[out.size()]);
       
   133     }
       
   134 
       
   135     private static void loadCmdFile(String name, List<String> args) throws IOException {
    86     private static void loadCmdFile(String name, List<String> args) throws IOException {
   136         try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) {
    87         try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) {
   137             Tokenizer t = new Tokenizer(r);
    88             Tokenizer t = new Tokenizer(r);
   138             String s;
    89             String s;
   139             while ((s = t.nextToken()) != null) {
    90             while ((s = t.nextToken()) != null) {
   247 
   198 
   248                 ch = in.read();
   199                 ch = in.read();
   249             }
   200             }
   250         }
   201         }
   251     }
   202     }
   252 
       
   253     @SuppressWarnings("fallthrough")
       
   254     private static void appendParsedEnvVariables(List<String> newArgs, String envVariable)
       
   255             throws UnmatchedQuote {
       
   256 
       
   257         if (envVariable == null) {
       
   258             return;
       
   259         }
       
   260         String in = System.getenv(envVariable);
       
   261         if (in == null || in.trim().isEmpty()) {
       
   262             return;
       
   263         }
       
   264 
       
   265         final char NUL = (char)0;
       
   266         final int len = in.length();
       
   267 
       
   268         int pos = 0;
       
   269         StringBuilder sb = new StringBuilder();
       
   270         char quote = NUL;
       
   271         char ch;
       
   272 
       
   273         loop:
       
   274         while (pos < len) {
       
   275             ch = in.charAt(pos);
       
   276             switch (ch) {
       
   277                 case '\"': case '\'':
       
   278                     if (quote == NUL) {
       
   279                         quote = ch;
       
   280                     } else if (quote == ch) {
       
   281                         quote = NUL;
       
   282                     } else {
       
   283                         sb.append(ch);
       
   284                     }
       
   285                     pos++;
       
   286                     break;
       
   287                 case '\f': case '\n': case '\r': case '\t': case ' ':
       
   288                     if (quote == NUL) {
       
   289                         newArgs.add(sb.toString());
       
   290                         sb.setLength(0);
       
   291                         while (ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' || ch == ' ') {
       
   292                             pos++;
       
   293                             if (pos >= len) {
       
   294                                 break loop;
       
   295                             }
       
   296                             ch = in.charAt(pos);
       
   297                         }
       
   298                         break;
       
   299                     }
       
   300                     // fall through
       
   301                 default:
       
   302                     sb.append(ch);
       
   303                     pos++;
       
   304             }
       
   305         }
       
   306         if (sb.length() != 0) {
       
   307             newArgs.add(sb.toString());
       
   308         }
       
   309         if (quote != NUL) {
       
   310             throw new UnmatchedQuote(envVariable);
       
   311         }
       
   312     }
       
   313 
       
   314     public static class UnmatchedQuote extends Exception {
       
   315         private static final long serialVersionUID = 0;
       
   316 
       
   317         public final String variableName;
       
   318 
       
   319         UnmatchedQuote(String variable) {
       
   320             this.variableName = variable;
       
   321         }
       
   322     }
       
   323 }
   203 }