src/java.base/share/classes/com/sun/java/util/jar/pack/Driver.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47216 71c04702a3d5
child 50603 95c0644a1c47
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    28
import java.io.BufferedInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    29
import java.io.BufferedOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    30
import java.io.File;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    31
import java.io.FileInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    32
import java.io.FileOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    33
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    34
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    35
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    36
import java.io.PrintStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.text.MessageFormat;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 9035
diff changeset
    38
import java.nio.file.Files;
13249
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
    39
import java.nio.file.Path;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    40
import java.util.ArrayList;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    41
import java.util.Arrays;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    42
import java.util.HashMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    43
import java.util.Iterator;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    44
import java.util.List;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    45
import java.util.ListIterator;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    46
import java.util.Map;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    47
import java.util.Properties;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    48
import java.util.ResourceBundle;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    49
import java.util.SortedMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    50
import java.util.TreeMap;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    51
import java.util.jar.JarFile;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    52
import java.util.jar.JarOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    53
import java.util.jar.Pack200;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    54
import java.util.zip.GZIPInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 6313
diff changeset
    55
import java.util.zip.GZIPOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/** Command line interface for Pack200.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
class Driver {
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    60
        private static final ResourceBundle RESOURCE =
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
    61
                ResourceBundle.getBundle("com.sun.java.util.jar.pack.DriverResource");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void main(String[] ava) throws IOException {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    64
        List<String> av = new ArrayList<>(Arrays.asList(ava));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        boolean doPack   = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        boolean doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        boolean doRepack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        boolean doZip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String logFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String verboseProp = Utils.DEBUG_VERBOSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // Non-standard, undocumented "--unpack" switch enables unpack mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            String arg0 = av.isEmpty() ? "" : av.get(0);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    76
            switch (arg0) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    77
                case "--pack":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                av.remove(0);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    79
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    80
                case "--unpack":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                doPack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                doUnpack = true;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    84
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Collect engine properties here:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    89
        Map<String,String> engProps = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        engProps.put(verboseProp, System.getProperty(verboseProp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String optionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String[] propTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (doPack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            optionMap = PACK200_OPTION_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            propTable = PACK200_PROPERTY_TO_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            optionMap = UNPACK200_OPTION_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            propTable = UNPACK200_PROPERTY_TO_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Collect argument properties here:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   103
        Map<String,String> avProps = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                String state = parseCommandOptions(av, optionMap, avProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                // Translate command line options to Pack200 properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            eachOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                for (Iterator<String> opti = avProps.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                     opti.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    String opt = opti.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    String prop = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    for (int i = 0; i < propTable.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        if (opt.equals(propTable[1+i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            prop = propTable[0+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        String val = avProps.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        opti.remove();  // remove opt from avProps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        if (!prop.endsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            // Normal string or boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            if (!(opt.equals("--verbose")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                  || opt.endsWith("="))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                // Normal boolean; convert to T/F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                boolean flag = (val != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                if (opt.startsWith("--no-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                    flag = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                val = flag? "true": "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                            engProps.put(prop, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        } else if (prop.contains(".attribute.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            for (String val1 : val.split("\0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                String[] val2 = val1.split("=", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                engProps.put(prop+val2[0], val2[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            // Collection property: pack.pass.file.cli.NNN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            int idx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            for (String val1 : val.split("\0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                String prop1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                    prop1 = prop+"cli."+(idx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                } while (engProps.containsKey(prop1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                engProps.put(prop1, val1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // See if there is any other action to take.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   153
                if ("--config-file=".equals(state)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    String propFile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    Properties fileProps = new Properties();
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   156
                    try (InputStream propIn = new FileInputStream(propFile)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   157
                        fileProps.load(propIn);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   158
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (engProps.get(verboseProp) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        fileProps.list(System.out);
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   161
                    for (Map.Entry<Object,Object> me : fileProps.entrySet()) {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   162
                        engProps.put((String) me.getKey(), (String) me.getValue());
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   163
                    }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   164
                } else if ("--version".equals(state)) {
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   165
                        System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.VERSION),
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   166
                                                                Driver.class.getName(), "1.31, 07/05/05"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    return;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   168
                } else if ("--help".equals(state)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    printUsage(doPack, true, System.out);
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   170
                    System.exit(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } catch (IllegalArgumentException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_ARGUMENT), ee));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Deal with remaining non-engine properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (String opt : avProps.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            String val = avProps.get(opt);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   186
            switch (opt) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   187
                case "--repack":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   188
                    doRepack = true;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   189
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   190
                case "--no-gzip":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   191
                    doZip = (val == null);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   192
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   193
                case "--log-file=":
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   194
                    logFile = val;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   195
                    break;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   196
                default:
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   197
                    throw new InternalError(MessageFormat.format(
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   198
                            RESOURCE.getString(DriverResource.BAD_OPTION),
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   199
                            opt, avProps.get(opt)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (logFile != null && !logFile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (logFile.equals("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                System.setErr(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                OutputStream log = new FileOutputStream(logFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                //log = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                System.setErr(new PrintStream(log));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        boolean verbose = (engProps.get(verboseProp) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        String packfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (!av.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            packfile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String jarfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (!av.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            jarfile = av.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String newfile = "";  // output JAR file if --repack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        String bakfile = "";  // temporary backup of input JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        String tmpfile = "";  // temporary file to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (doRepack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // The first argument is the target JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // (Note:  *.pac is nonstandard, but may be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // if a host OS truncates file extensions.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (packfile.toLowerCase().endsWith(".pack") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                packfile.toLowerCase().endsWith(".pac") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                packfile.toLowerCase().endsWith(".gz")) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   233
                System.err.println(MessageFormat.format(
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   234
                        RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT),
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   235
                        packfile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            newfile = packfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // The optional second argument is the source JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (jarfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // If only one file is given, it is the only JAR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // It serves as both input and output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                jarfile = newfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            tmpfile = createTempFile(newfile, ".pack").getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            packfile = tmpfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            doZip = false;  // no need to zip the temporary file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!av.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Accept jarfiles ending with .jar or .zip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // Accept jarfile of "-" (stdout), but only if unpacking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            || !(jarfile.toLowerCase().endsWith(".jar")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                 || jarfile.toLowerCase().endsWith(".zip")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                 || (jarfile.equals("-") && !doPack))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (doRepack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            doPack = doUnpack = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        else if (doPack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        Pack200.Packer jpack = Pack200.newPacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Pack200.Unpacker junpack = Pack200.newUnpacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        jpack.properties().putAll(engProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        junpack.properties().putAll(engProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (doRepack && newfile.equals(jarfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            String zipc = getZipComment(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (verbose && zipc.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.DETECTED_ZIP_COMMENT), zipc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (zipc.indexOf(Utils.PACK_ZIP_ARCHIVE_MARKER_COMMENT) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_REPACKED), jarfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        doPack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        doUnpack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        doRepack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (doPack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                // Mode = Pack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                JarFile in = new JarFile(new File(jarfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                // Packfile must be -, *.gz, *.pack, or *.pac.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (packfile.equals("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // Send warnings, etc., to stderr instead of stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    System.setOut(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } else if (doZip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    if (!packfile.endsWith(".gz")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACK_FILE), packfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    out = new FileOutputStream(packfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    out = new GZIPOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (!packfile.toLowerCase().endsWith(".pack") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                            !packfile.toLowerCase().endsWith(".pac")) {
20497
f36fb849fda7 8025342: NLS: unsupported translation format in jar/pack/DriverResource.java
ksrini
parents: 13249
diff changeset
   307
                        System.err.println(MessageFormat.format(RESOURCE.getString(DriverResource.WRITE_PACKGZ_FILE),packfile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        printUsage(doPack, false, System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    out = new FileOutputStream(packfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    out = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                jpack.pack(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                //in.close();  // p200 closes in but not out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (doRepack && newfile.equals(jarfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                // If the source and destination are the same,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // we will move the input JAR aside while regenerating it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // This allows us to restore it if something goes wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                File bakf = createTempFile(jarfile, ".bak");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // On Windows target must be deleted see 4017593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                bakf.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                boolean okBackup = new File(jarfile).renameTo(bakf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (!okBackup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED),bakfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    // Open jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    bakfile = bakf.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (doUnpack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // Mode = Unpack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (packfile.equals("-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    in = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    in = new FileInputStream(new File(packfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                BufferedInputStream inBuf = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                in = inBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    in = new GZIPInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                String outfile = newfile.equals("")? jarfile: newfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                OutputStream fileOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (outfile.equals("-"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    fileOut = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    fileOut = new FileOutputStream(outfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                fileOut = new BufferedOutputStream(fileOut);
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   354
                try (JarOutputStream out = new JarOutputStream(fileOut)) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   355
                    junpack.unpack(in, out);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   356
                    // p200 closes in but not out
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   357
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                // At this point, we have a good jarfile (or newfile, if -r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (!bakfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        // On success, abort jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        new File(bakfile).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        bakfile = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // Close jarfile recovery bracket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (!bakfile.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                File jarFile = new File(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                jarFile.delete(); // Win32 requires this, see above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                new File(bakfile).renameTo(jarFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // In all cases, delete temporary *.pack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (!tmpfile.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                new File(tmpfile).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   380
    private static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    File createTempFile(String basefile, String suffix) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        File base = new File(basefile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        String prefix = base.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (prefix.length() < 3)  prefix += "tmp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
13249
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   386
        File where = (base.getParentFile() == null && suffix.equals(".bak"))
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   387
                ? new File(".").getAbsoluteFile()
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   388
                : base.getParentFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
13249
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   390
        Path tmpfile = (where == null)
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   391
                ? Files.createTempFile(prefix, suffix)
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   392
                : Files.createTempFile(where.toPath(), prefix, suffix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
13249
9f4bd1ab7873 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path
ksrini
parents: 13041
diff changeset
   394
        return tmpfile.toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   397
    private static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    void printUsage(boolean doPack, boolean full, PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        String prog = doPack ? "pack200" : "unpack200";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        String[] packUsage = (String[])RESOURCE.getObject(DriverResource.PACK_HELP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        String[] unpackUsage = (String[])RESOURCE.getObject(DriverResource.UNPACK_HELP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        String[] usage = doPack? packUsage: unpackUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        for (int i = 0; i < usage.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            out.println(usage[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (!full) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            out.println(MessageFormat.format(RESOURCE.getString(DriverResource.MORE_INFO), prog));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   412
    private static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        String getZipComment(String jarfile) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        byte[] tail = new byte[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        long filelen = new File(jarfile).length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (filelen <= 0)  return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        long skiplen = Math.max(0, filelen - tail.length);
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 7795
diff changeset
   418
        try (InputStream in = new FileInputStream(new File(jarfile))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            in.skip(skiplen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            in.read(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            for (int i = tail.length-4; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (tail[i+0] == 'P' && tail[i+1] == 'K' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    tail[i+2] ==  5  && tail[i+3] ==  6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    // Skip sig4, disks4, entries4, clen4, coff4, cmt2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    i += 4+4+4+4+4+2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    if (i < tail.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        return new String(tail, i, tail.length-i, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static final String PACK200_OPTION_MAP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        (""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         +"--repack                 $ \n  -r +>- @--repack              $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         +"--no-gzip                $ \n  -g +>- @--no-gzip             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         +"--strip-debug            $ \n  -G +>- @--strip-debug         $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         +"--no-keep-file-order     $ \n  -O +>- @--no-keep-file-order  $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         +"--segment-limit=      *> = \n  -S +>  @--segment-limit=      = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         +"--effort=             *> = \n  -E +>  @--effort=             = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         +"--deflate-hint=       *> = \n  -H +>  @--deflate-hint=       = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         +"--modification-time=  *> = \n  -m +>  @--modification-time=  = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         +"--pass-file=        *> &\0 \n  -P +>  @--pass-file=        &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         +"--unknown-attribute=  *> = \n  -U +>  @--unknown-attribute=  = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         +"--class-attribute=  *> &\0 \n  -C +>  @--class-attribute=  &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         +"--field-attribute=  *> &\0 \n  -F +>  @--field-attribute=  &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         +"--method-attribute= *> &\0 \n  -M +>  @--method-attribute= &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         +"--code-attribute=   *> &\0 \n  -D +>  @--code-attribute=   &\0 \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         +"--config-file=      *>   . \n  -f +>  @--config-file=        . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         // Negative options as required by CLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         +"--no-strip-debug  !--strip-debug         \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         +"--gzip            !--no-gzip             \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         +"--keep-file-order !--no-keep-file-order  \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         // Non-Standard Options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         +"--verbose                $ \n  -v +>- @--verbose             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         +"--quiet        !--verbose  \n  -q +>- !--verbose               \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         +"--log-file=           *> = \n  -l +>  @--log-file=           = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         //+"--java-option=      *> = \n  -J +>  @--java-option=        = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         +"--version                . \n  -V +>  @--version             . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         +"--help               . \n  -? +> @--help . \n  -h +> @--help . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         // Termination:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         +"--           . \n"  // end option sequence here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         +"-   +?    >- . \n"  // report error if -XXX present; else use stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // Note: Collection options use "\0" as a delimiter between arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // For Java version of unpacker (used for testing only):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private static final String UNPACK200_OPTION_MAP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        (""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         +"--deflate-hint=       *> = \n  -H +>  @--deflate-hint=       = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         +"--verbose                $ \n  -v +>- @--verbose             $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         +"--quiet        !--verbose  \n  -q +>- !--verbose               \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         +"--remove-pack-file       $ \n  -r +>- @--remove-pack-file    $ \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         +"--log-file=           *> = \n  -l +>  @--log-file=           = \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         +"--config-file=        *> . \n  -f +>  @--config-file=        . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         // Termination:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         +"--           . \n"  // end option sequence here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         +"-   +?    >- . \n"  // report error if -XXX present; else use stdin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         +"--version                . \n  -V +>  @--version             . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         +"--help               . \n  -? +> @--help . \n  -h +> @--help . \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private static final String[] PACK200_PROPERTY_TO_OPTION = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        Pack200.Packer.SEGMENT_LIMIT, "--segment-limit=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        Pack200.Packer.KEEP_FILE_ORDER, "--no-keep-file-order",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        Pack200.Packer.EFFORT, "--effort=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        Pack200.Packer.DEFLATE_HINT, "--deflate-hint=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        Pack200.Packer.MODIFICATION_TIME, "--modification-time=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        Pack200.Packer.PASS_FILE_PFX, "--pass-file=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        Pack200.Packer.UNKNOWN_ATTRIBUTE, "--unknown-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        Pack200.Packer.CLASS_ATTRIBUTE_PFX, "--class-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        Pack200.Packer.FIELD_ATTRIBUTE_PFX, "--field-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        Pack200.Packer.METHOD_ATTRIBUTE_PFX, "--method-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        Pack200.Packer.CODE_ATTRIBUTE_PFX, "--code-attribute=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        //Pack200.Packer.PROGRESS, "--progress=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        Utils.DEBUG_VERBOSE, "--verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Utils.COM_PREFIX+"strip.debug", "--strip-debug",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static final String[] UNPACK200_PROPERTY_TO_OPTION = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        Pack200.Unpacker.DEFLATE_HINT, "--deflate-hint=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        //Pack200.Unpacker.PROGRESS, "--progress=",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        Utils.DEBUG_VERBOSE, "--verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        Utils.UNPACK_REMOVE_PACKFILE, "--remove-pack-file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /*-*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Remove a set of command-line options from args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * storing them in the map in a canonicalized form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * The options string is a newline-separated series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * option processing specifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    String parseCommandOptions(List<String> args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                               String options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                               Map<String,String> properties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        //System.out.println(args+" // "+properties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        String resultString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // Convert options string into optLines dictionary.
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   529
        TreeMap<String,String[]> optmap = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    loadOptmap:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        for (String optline : options.split("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            String[] words = optline.split("\\p{Space}+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (words.length == 0)    continue loadOptmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            String opt = words[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            words[0] = "";  // initial word is not a spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (opt.length() == 0 && words.length >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                opt = words[1];  // initial "word" is empty due to leading ' '
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                words[1] = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (opt.length() == 0)    continue loadOptmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            String[] prevWords = optmap.put(opt, words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (prevWords != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.DUPLICATE_OPTION), optline.trim()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // State machine for parsing a command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        ListIterator<String> argp = args.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        ListIterator<String> pbp = new ArrayList<String>().listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    doArgs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            // One trip through this loop per argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // Multiple trips per option only if several options per argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            String arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (pbp.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                arg = pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            } else if (argp.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                arg = argp.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // No more arguments at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        tryOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            for (int optlen = arg.length(); ; optlen--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                // One time through this loop for each matching arg prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                String opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                // Match some prefix of the argument to a key in optmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            findOpt:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    opt = arg.substring(0, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    if (optmap.containsKey(opt))  break findOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    if (optlen == 0)              break tryOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    // Decide on a smaller prefix to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    SortedMap<String,String[]> pfxmap = optmap.headMap(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    // pfxmap.lastKey is no shorter than any prefix in optmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    int len = pfxmap.isEmpty() ? 0 : pfxmap.lastKey().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    optlen = Math.min(len, optlen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    opt = arg.substring(0, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    // (Note:  We could cut opt down to its common prefix with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    // pfxmap.lastKey, but that wouldn't save many cycles.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                opt = opt.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                assert(arg.startsWith(opt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                assert(opt.length() == optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                String val = arg.substring(optlen);  // arg == opt+val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                // Execute the option processing specs for this opt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                // If no actions are taken, then look for a shorter prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                boolean didAction = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                boolean isError = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                int pbpMark = pbp.nextIndex();  // in case of backtracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                String[] specs = optmap.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            eachSpec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                for (String spec : specs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    if (spec.length() == 0)     continue eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    if (spec.startsWith("#"))   break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    int sidx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    char specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    // Deal with '+'/'*' prefixes (spec conditions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    switch (specop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        // + means we want an non-empty val suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        ok = (val.length() != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        // * means we accept empty or non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        specop = spec.charAt(sidx++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        // No condition prefix means we require an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        // match, as indicated by an empty val suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        ok = (val.length() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    if (!ok)  continue eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    String specarg = spec.substring(sidx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    switch (specop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    case '.':  // terminate the option sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        resultString = (specarg.length() != 0)? specarg.intern(): opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    case '?':  // abort the option sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        resultString = (specarg.length() != 0)? specarg.intern(): arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        isError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    case '@':  // change the effective opt name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        opt = specarg.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    case '>':  // shift remaining arg val to next arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        pbp.add(specarg + val);  // push a new argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        val = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    case '!':  // negation option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        String negopt = (specarg.length() != 0)? specarg.intern(): opt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        properties.remove(negopt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        properties.put(negopt, null);  // leave placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    case '$':  // normal "boolean" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        String boolval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        if (specarg.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            // If there is a given spec token, store it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            boolval = specarg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                            String old = properties.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                            if (old == null || old.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                boolval = "1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                // Increment any previous value as a numeral.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                boolval = ""+(1+Integer.parseInt(old));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        properties.put(opt, boolval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    case '=':  // "string" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    case '&':  // "collection" option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        // Read an option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                        boolean append = (specop == '&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        String strval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        if (pbp.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                            strval = pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        } else if (argp.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            strval = argp.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                            resultString = arg + " ?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            isError = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                            break eachSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        if (append) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                            String old = properties.get(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                // Append new val to old with embedded delim.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                String delim = specarg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                if (delim.length() == 0)  delim = " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                strval = old + specarg + strval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        properties.put(opt, strval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        didAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        throw new RuntimeException(MessageFormat.format(RESOURCE.getString(DriverResource.BAD_SPEC),opt, spec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                // Done processing specs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                if (didAction && !isError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    continue doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // The specs should have done something, but did not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                while (pbp.nextIndex() > pbpMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    // Remove anything pushed during these specs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    pbp.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    pbp.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (isError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    throw new IllegalArgumentException(resultString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                if (optlen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    // We cannot try a shorter matching option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    break tryOpt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // If we come here, there was no matching option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            // So, push back the argument, and return to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            pbp.add(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            break doArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // Report number of arguments consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        args.subList(0, argp.nextIndex()).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // Report any unconsumed partial argument.
6313
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   723
        while (pbp.hasPrevious()) {
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   724
            args.add(0, pbp.previous());
470912c9e214 6888127: java.util.jar.Pack200.Packer Memory Leak
ksrini
parents: 5506
diff changeset
   725
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        //System.out.println(args+" // "+properties+" -> "+resultString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return resultString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
}