summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/ContextTool/org/tianocore/context/ParseParameter.java
blob: 4d9c6de3a07c1f014f451f325e288a7b2e150793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/** @file
  File is ParseParameter class which is used to parse the validity of user's input args
  and standardize them. 
 
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
 
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
package org.tianocore.context;

public class ParseParameter {

    
    /** 
     * check the validity of user's input args
     * @param args -- user's input
     * @return true or false
     **/
    public static boolean checkParameter(String[] args) {
        
        if(args.length == 0){
            TargetFile.readFile();
            outputCurSetting();
            return false;
        } else {
            if( (args[0].compareToIgnoreCase("-h") == 0) || (args[0].compareToIgnoreCase("/h") == 0) || 
                (args[0].compareToIgnoreCase("--help") == 0) || (args[0].compareToIgnoreCase("/help") == 0) ){
                HelpInfo.outputUsageInfo();
                return false;
            }
            if( args[0].charAt(0) != '-' ){
                System.out.printf("%s\n", "Error Parameters! Please type \"ContextTool -h\" for helpinfo.");
                return false;
            }
            for(int i=0; i<args.length; i++){
                if( (args[i].startsWith("-") && 
                    ((args[i].compareTo("-a") != 0) && (args[i].compareTo("-c") != 0) && 
                    (args[i].compareTo("-n") != 0) && (args[i].compareTo("-p") != 0) && 
                    (args[i].compareTo("-t") != 0) && (args[i].compareTo("-m") != 0)))){
                    System.out.printf("%s\n", "Error Parameters! Please type \"ContextTool -h\" for helpinfo.");                                                                                                                                                            
                    return false;
                }
            }
        }
        
        return true; 
    }
    
    /** 
     * standardize user's input args
     * @param args -- user's input
     * @return no return value
     **/
    public static int standardizeParameter(String[] args) {
        
        
        StringBuffer InputData = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            InputData.append(args[i]);
            InputData.append(" ");
        }

        int i = 0;
        while (i < InputData.length()) {
            int j = InputData.indexOf("-", i + 1);
            if (j == -1)
                j = InputData.length();

            String argstr = InputData.substring(i, j);
            i = j;
            if (argstr.charAt(1) == 'p') {
                //
                // argstr is "-p ", display current setting
                //
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curpstr != null) {
                        System.out.printf("%s\n", curpstr);
                    } else {
                        System.out.printf("No ACTIVE_PLATFORM defined \n");
                    }
                    return 1;
                }
                //
                //argstr is "-p ? ", display possible setting
                //
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    String workspacePath = System.getenv("WORKSPACE");
                    System.out.printf( "%s\n", "Assign a platform FPD file with relative path to " + workspacePath);
                    return 2;
                }
                //
                //argstr is "-p 0 ", clean current setting
                //
                if(argstr.length() < 6 && argstr.charAt(3) == '0'){
                    curpstr = pstr;
                    npflag = true;
                    continue;
                }
                String[] S = argstr.split(" ");
                if(S.length > 2){
                    System.out.printf( "%s\n", "There should be none or only one ACTIVE_PLATFORM. Please check the number of value which follow \"-p\".");
                    return 3;
                }
                curpstr = pstr.concat(argstr.substring(2));
                npflag = true;
            } else if (argstr.charAt(1) == 't') {
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curtstr != null) {
                        System.out.printf("%s\n", curtstr);
                    } else {
                        System.out.printf("No TARGET defined\n");
                    }
                    return 1;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    System.out.printf( "%s\n", "What kind of the version is the binary target, such as DEBUG, RELEASE." );
                    return 2;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '0'){
                    curtstr = tstr;
                    ntflag = true;
                    continue;
                }
                curtstr = tstr.concat(argstr.substring(2));
                ntflag = true;
            } else if (argstr.charAt(1) == 'a') {
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curastr != null) {
                        System.out.printf("%s\n", curastr);
                    } else {
                        System.out.printf("No TARGET_ARCH defined\n");
                    }
                    return 1;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    System.out.printf( "%s\n", "What kind of architechure is the binary target, such as IA32, IA64, X64, EBC, or ARM." );
                    return 2;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '0'){
                    curastr = astr;
                    naflag = true;
                    continue;
                }
                curastr = astr.concat(argstr.substring(2));
                naflag = true;
            } else if (argstr.charAt(1) == 'c') {
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curcstr != null) {
                        System.out.printf("%s\n", curcstr);
                    } else {
                        System.out.printf("No TOOL_CHAIN_CONF defined\n");
                    }
                    return 1;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    String workspacePath = System.getenv("WORKSPACE");
                    System.out.printf( "%s\n", "Assign a txt file with relative path to " + workspacePath + ", which specify the tools to use for the build and must be located in the path:" + workspacePath + "\\Tools\\Conf" );
                    return 2;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '0'){
                    curcstr = cstr;
                    ncflag = true;
                    continue;
                }
                String[] S = argstr.split(" ");
                if(S.length > 2){
                    System.out.printf( "%s\n", "There should be one and only one TOOL_CHAIN_CONF. Please check the number of value which follow \"-c\".");
                    return 3;
                }
                curcstr = cstr.concat(argstr.substring(2));
                ncflag = true;
            } else if (argstr.charAt(1) == 'n') {
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curnstr != null) {
                        System.out.printf("%s\n", curnstr);
                    } else {
                        System.out.printf("No TOOL_CHAIN_TAG defined\n");
                    }
                    return 1;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    System.out.printf( "%s\n", "Specify the TagName, such as GCC, MSFT." );
                    return 2;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '0'){
                    curnstr = nstr;
                    nnflag = true;
                    continue;
                }
                curnstr = nstr.concat(argstr.substring(2));
                nnflag = true;
            } else if (argstr.charAt(1) == 'm') {
                if(argstr.length() < 4 && argstr.charAt(2) == ' '){
                    if (curmstr != null) {
                        System.out.printf("%s\n", curmstr);
                    } else {
                        System.out.printf("No MAX_CONCURRENT_THREAD_NUMBER defined\n");
                    }
                    return 1;
                }
                if(argstr.length() < 6 && argstr.charAt(3) == '?'){
                    System.out.printf( "%s\n", "The number of concurrent threads. Default is 2. Recommend to set this value to one more than the number of your compurter cores or CPUs." );
                    return 2;
                }
                String[] S = argstr.split(" ");
                if(S.length > 2){
                    System.out.printf( "%s\n", "There should be one and only one integer, which doesn't include space.");
                    return 3;
                }
                mstr += argstr.substring(2);
                curmstr = mstr;
                nmflag = true;
                if (argstr.charAt(3) == '0'){
                    mestr += " Disable";
                } else {
                    mestr += " Enable";
                }
                curmestr = mestr;
                nmeflag = true;
            }
            
        }
        return 0;
    }

    
    public static boolean outputCurSetting(){
        
        System.out.printf( "%s\n", "The current setting is:" );
        String[] A = { pstr, tstr, astr, cstr, nstr, mstr, mestr };
        String[] B = { curpstr, curtstr, curastr, curcstr, curnstr, curmstr, curmestr };
        
        for(int i=0; i<A.length; i++){
            if(B[i] != null){
                System.out.printf( "%s\n", B[i] );
            }
            else{
                System.out.printf( "%s\n", A[i] );
            }
            
        }
        
        return true;
    }

    
    public static String pstr = new String("ACTIVE_PLATFORM                     = ");
    public static String tstr = new String("TARGET                              = ");
    public static String astr = new String("TARGET_ARCH                         = ");
    public static String cstr = new String("TOOL_CHAIN_CONF                     = ");
    public static String nstr = new String("TOOL_CHAIN_TAG                      = ");
    public static String mstr = new String("MAX_CONCURRENT_THREAD_NUMBER        = ");
    public static String mestr = new String("MULTIPLE_THREAD                     = ");
    
    public static String curpstr = null;
    public static String curtstr = null;
    public static String curastr = null;
    public static String curcstr = null;
    public static String curnstr = null;
    public static String curmstr = null;
    public static String curmestr = null;
    
    public static boolean npflag = false;
    public static boolean ntflag = false;
    public static boolean naflag = false;
    public static boolean ncflag = false;
    public static boolean nnflag = false;
    public static boolean nmflag = false;
    public static boolean nmeflag = false;

}