Fixed a usage spelling error
[mirror_edk2.git] / Tools / Java / Source / DbTools / src / org / tianocore / DbTools / DbCmds.java
CommitLineData
4de92764 1// @file\r
2// DbCmds command-line interface to the classes that \r
3// update the FrameworkDatabase.db file based on WORKSPACE Contents\r
4//\r
5// Copyright (c) 2006, Intel Corporation All rights reserved.\r
6//\r
7// This program and the accompanying materials are licensed and made\r
8// available under the terms and conditions of the BSD License which\r
9// accompanies this distribution. The full text of the license may \r
10// be found at http://opensource.org/licenses/bsd-license.php\r
11//\r
12// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14//\r
15// This program is the command line interface to the CombineMsa class, which\r
16// will take the following arguments:\r
17//\r
18// Input:\r
19// -t, --test Test the Workspace against the FrameworkDatabase.db file\r
20// Returns 0 if valid, returns 1 if failed\r
21//\r
22// -f, --fix Fix the FrameworkDatabase.db file, so that it matches the\r
23// the contents of the WORKSPACE\r
24// Returns 0 if valid, returns 1 if failed\r
25//\r
26// -v [-v] Verbose Flag - sum of these will be used to set different\r
27// levels of verbosity\r
28//\r
29// -i Interactive, when used with -f or --fix, will query the user\r
30// regarding adds and deletes of packages and platforms from\r
31// the database.\r
32//\r
33// -a, --add Add an SPD or FPD file to the FrameworkDatabase.db file. The\r
34// SPD/FPD file must exist, or the command will fail.\r
35// Returns 0 if valid, returns 1 if failed\r
36//\r
37// -r, --del Remove an SPD or FPD file from the FrameworkDatabase.db file.\r
38// If the SPD/FPD file exists, the user will be queried to\r
39// remove it from the directory tree. For SPD files, the user\r
40// will also be presented with a list of Modules in the SPD\r
41// file, with the query to remove the modules as well as the\r
42// SPD file.\r
43// Returns 0 if valid, returns 1 if failed\r
44//\r
45// -u Display the UiName for all Packages and Platforms currently in\r
46// the FrameworkDatabase.db file.\r
47// Returns 0 if valid, returns 1 if failed\r
48//\r
49// -c Display a CSV listing of Type (SPD|FPD) UiName and Filename of\r
50// every entry in the FrameworkDatabase.db file.\r
51// Returns 0 if valid, returns 1 if failed\r
52//\r
53// No Options Display a list of Type (Package|Platfrom) and Filename of every\r
54// entry in the FrameworkDatabase.db file.\r
55// Returns 0 if valid, returns 1 if failed\r
56//\r
57// -h, -?, --help Displays this usage and exits.\r
58// Returns 0 if valid, returns 1 if failed\r
59//\r
60//\r
61// Output:\r
62// Displayed information\r
63//\r
64// Modifies - OPTIONAL\r
65// FrameworkDatabase.db\r
66//\r
67\r
68package org.tianocore.DbTools;\r
69\r
70import java.io.*;\r
71\r
72public class DbCmds {\r
73\r
74 protected enum Cmd {\r
75 SHOW_CSV, SHOW_PLATFORMS, SHOW_PACKAGES, FIX_DB, TEST_DB, FIND_SOMETHING, SHOW_FAR, ADD_SOMETHING,\r
76 DELETE_SOMETHING\r
77 }\r
78\r
79 private int DEBUG = 0;\r
80\r
81 private static final String copyright = "Copyright (c) 2006, Intel Corporation All rights reserved.";\r
82\r
83 private static final String version = "Version 0.1";\r
84\r
85 private static final String Specification = "FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052";\r
86\r
87 private int VERBOSE = 0;\r
88\r
89 public boolean INTERACTIVE = false;\r
90\r
91 private String workspace = System.getenv("WORKSPACE");\r
92\r
93 private final String frameworkDatabase = workspace + File.separator + "Tools" + File.separator + "Conf"\r
94 + File.separator + "FrameworkDatabase.db";\r
95\r
96 private final int ESUCCESS = 0;\r
97\r
98 private final int EFAILURE = 1;\r
99\r
100 private final int MIN_VERBOSE = 0;\r
101\r
102 private final int MED_VERBOSE = 1;\r
103\r
104 private final int MAX_VERBOSE = 2;\r
105\r
106 private final static int FOUND = 1;\r
107\r
108 private final static int NOTFOUND = 0;\r
109\r
110 private boolean TEST = false;\r
111\r
112 private int result = ESUCCESS;\r
113\r
114 private Cmd commandToCall;\r
115\r
116 private boolean isPlatform = false;\r
117\r
118 private boolean isPackage = false;\r
119\r
120 private boolean isFar = false;\r
121\r
122 private boolean QUIET = false;\r
123\r
124 private String commandArgs = "";\r
125\r
126 private String whatToFind = "";\r
127\r
128 public int DbUpdateCmdLine(String[] args) {\r
129\r
130 if (testFile(frameworkDatabase) == NOTFOUND) {\r
131 System.out.println("ERROR: E000 Invalid Workspace!");\r
132 System.out.println("The environment variable, WORKSPACE, does not point to a valid workspace");\r
133 System.out.println("DbUpdate Aborted!");\r
134 System.err.flush();\r
135 System.exit(EFAILURE);\r
136 }\r
137 result = parseCmdLine(args);\r
138 if (result == ESUCCESS) {\r
139 if (DEBUG > 2)\r
140 System.out.println("Parse Succeeded!");\r
141 if (VERBOSE > MAX_VERBOSE)\r
142 System.out.println("WORKSPACE: " + workspace);\r
143 if (DEBUG > 1)\r
144 System.out.println("Command to call: " + commandToCall.toString());\r
145 result = processCmdLine(commandToCall);\r
146 } else {\r
147 if (QUIET == false) {\r
148 System.out.println("Invalid Arguments");\r
149 outputUsage();\r
150 result = EFAILURE;\r
151 }\r
152 }\r
153 if (DEBUG > 2)\r
154 System.out.println(" Result: " + result);\r
155\r
156 return result;\r
157 }\r
158\r
159 private int processCmdLine(Cmd cmdCode) {\r
160 UpdateDb dbUp = new UpdateDb();\r
161 int res = ESUCCESS;\r
162 switch (cmdCode) {\r
163 case SHOW_CSV:\r
164 // display current Database contents in CSV format\r
165 // Modifies: NOTHING\r
166 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
167 System.out.println("Display contents of the FrameworkDatabase.db file in CSV format.");\r
168 result = dbUp.getCsvEntries(frameworkDatabase, VERBOSE);\r
169 break;\r
170 case FIX_DB:\r
171 // Automatically make the database match the contents of the workspace\r
172 // Modifies: FrameworkDatabase.db\r
173 if ((VERBOSE > MIN_VERBOSE) && (QUIET == false)) {\r
174 if (TEST == false)\r
175 System.out.println("Adjusting the FrameworkDatabase to match the contents of the WORKSPACE: "\r
176 + workspace);\r
177 if (TEST)\r
178 System.out.println("Verify the FrameworkDatabase matches the contents of the WORKSPACE: "\r
179 + workspace);\r
180 }\r
181 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
182 System.out.println("Scan the Workspace and update the FrameworkDatabase.db file.");\r
183 result = dbUp.fixDatabase(workspace, VERBOSE, INTERACTIVE, TEST, QUIET);\r
184\r
185 break;\r
186 case ADD_SOMETHING:\r
187 // Add a Platform, package or FAR to the workspace.\r
188 // Modifies: FrameworkDatabase.db\r
189 if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPlatform))\r
190 System.out.println("Add Platform " + commandArgs + " to the FrameworkDatabase.db file");\r
191 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPackage))\r
192 System.out.println("Add Package " + commandArgs + " to the FrameworkDatabase.db file");\r
193 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isFar))\r
194 System.out.println("Add Framework Archive " + commandArgs + " to the FrameworkDatabase.db file");\r
195 result = dbUp.addItem(frameworkDatabase, commandArgs, VERBOSE, INTERACTIVE);\r
196 break;\r
197 case DELETE_SOMETHING:\r
198 // Remove a platform, package or FAR from the workspace\r
199 // Modifies: FrameworkDatabase.db, AND Filesystem\r
200 if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPlatform))\r
201 System.out.println("Removing Platform " + commandArgs + " from the FrameworkDatabase.db file");\r
202 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isPackage))\r
203 System.out.println("Removing Package " + commandArgs + " from the FrameworkDatabase.db file");\r
204 else if (((DEBUG > 3) || (VERBOSE > MED_VERBOSE)) && (isFar))\r
205 System.out.println("Removing Framework Archive " + commandArgs + " from the FrameworkDatabase.db file");\r
206 result = dbUp.addItem(workspace, commandArgs, VERBOSE, INTERACTIVE);\r
207 break;\r
208 case FIND_SOMETHING:\r
209 // Find something in the workspace\r
210 // Modifies: NOTHING\r
211 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
212 System.out.println("Finding " + whatToFind + " " + commandArgs + " in the Workspace");\r
213 result = dbUp.findItem(workspace, VERBOSE, whatToFind, commandArgs);\r
214 break;\r
215 case SHOW_FAR:\r
216 // Display FAR information for all SPDs in the workspace\r
217 // Modifies: NOTHING\r
218 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
219 System.out.println("Display Framework Archives in the Workspace");\r
220 result = dbUp.findFars(workspace, VERBOSE);\r
221 break;\r
222 case SHOW_PACKAGES:\r
223 // Display SPD information for all SPDs in the workspace\r
224 // Modifies: NOTHING\r
225 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
226 System.out.println("Display Packages in the Workspace");\r
227 result = dbUp.findSpds(workspace, VERBOSE);\r
228 break;\r
229 case SHOW_PLATFORMS:\r
230 // Display FPD information for all SPDs in the workspace\r
231 // Modifies: NOTHING\r
232 if ((DEBUG > 3) || (VERBOSE > MED_VERBOSE))\r
233 System.out.println("Display Platforms in the Workspace");\r
234 result = dbUp.findFpds(workspace, VERBOSE);\r
235 break;\r
236 default:\r
237 // ERROR IF WE GET HERE!\r
238 if ((DEBUG > 3) || (VERBOSE > MAX_VERBOSE))\r
239 System.out.println("We could not process the following: " + commandToCall.toString());\r
240 else if (QUIET == false)\r
241 outputUsage();\r
242 result = EFAILURE;\r
243 break;\r
244 }\r
245 return res;\r
246 }\r
247\r
248 private int parseCmdLine(String[] args) {\r
249\r
250 // Default is to fix the database.\r
251 commandToCall = Cmd.FIX_DB;\r
252 if (args.length == NOTFOUND) {\r
253 if ((DEBUG > 3) || (VERBOSE > MAX_VERBOSE))\r
254 System.out.println("NO ARGUMENTS! " + commandToCall.toString());\r
255 return (ESUCCESS);\r
256 }\r
257\r
258 for (int i = 0; i < args.length; i++)\r
259 if (args[i].toLowerCase().contentEquals("-q"))\r
260 QUIET = true;\r
261\r
262 for (int i = 0; i < args.length; i++) {\r
263 String arg = args[i].trim();\r
264 // This is the list of valid options\r
265 if (!((arg.toLowerCase().startsWith("-t")) || (arg.toLowerCase().startsWith("--t"))\r
266 || (arg.toLowerCase().startsWith("-q")) || (arg.toLowerCase().startsWith("-i"))\r
267 || (arg.toLowerCase().startsWith("-f")) || (arg.toLowerCase().startsWith("--f"))\r
268 || (arg.toLowerCase().startsWith("-c")) || (arg.toLowerCase().startsWith("--c"))\r
269 || (arg.toLowerCase().startsWith("-a")) || (arg.toLowerCase().startsWith("--a"))\r
270 || (arg.toLowerCase().startsWith("-r")) || (arg.toLowerCase().startsWith("--d"))\r
271 || (arg.toLowerCase().startsWith("-v")) || (arg.toLowerCase().startsWith("--c"))\r
272 || (arg.toLowerCase().startsWith("--p")) || (arg.toLowerCase().startsWith("-h"))\r
273 || (arg.toLowerCase().startsWith("--h")) || (arg.toLowerCase().startsWith("/h")) || (arg\r
274 .toLowerCase()\r
275 .startsWith("-?")))) {\r
276 // This is the fall through, we got something we did not know how to\r
277 // process!\r
278 if (args[i].startsWith("-")) {\r
279 System.out.println("ERROR: E1004 Unknown Option: " + arg);\r
280 System.out.println("Try running with -h or --help");\r
281 } else {\r
282 System.out.println("ERROR: E1005 Unknown Argument: " + arg);\r
283 System.out.println("Try running with -h or --help");\r
284 }\r
285 System.out.println("Program Aborted!");\r
286 System.err.flush();\r
287 System.exit(EFAILURE);\r
288 }\r
289 if ((arg.toLowerCase().contentEquals("-t")) || (arg.toLowerCase().contains("--test"))) {\r
290 // Test Workspace, do not fix.\r
291 TEST = true;\r
292 }\r
293 if (arg.toLowerCase().contentEquals("-q")) {\r
294 QUIET = true;\r
295 }\r
296 if (arg.toLowerCase().contentEquals("-i")) {\r
297 INTERACTIVE = true;\r
298 }\r
299 if ((arg.toLowerCase().contentEquals("-f")) || (arg.toLowerCase().contains("--fix"))) {\r
300 // Non-interactive fix of the database\r
301 commandToCall = Cmd.FIX_DB;\r
302 }\r
303 if ((arg.toLowerCase().contentEquals("-c")) || (arg.toLowerCase().contains("--csv"))) {\r
304 // Dump database in CSV format\r
305 commandToCall = Cmd.SHOW_CSV;\r
306 }\r
307 if ((arg.toLowerCase().trim().contentEquals("-a")) || (arg.toLowerCase().contains("--add"))) {\r
308 i++;\r
309 if (args[i].startsWith("-")) {\r
310 System.out.println("ERROR: E002 Missing Argument!");\r
311 System.out\r
312 .println("The add function requires an argument, either a package name or a platform name!");\r
313 System.out.println("DbUpdate Aborted!");\r
314 System.err.flush();\r
315 System.exit(EFAILURE);\r
316 }\r
317 commandToCall = Cmd.ADD_SOMETHING;\r
318 commandArgs = args[i];\r
319 setArgType(commandArgs);\r
320 }\r
321 if ((arg.toLowerCase().trim().contentEquals("-r")) || (arg.toLowerCase().contains("--del"))) {\r
322 i++;\r
323 if (args[i].startsWith("-")) {\r
324 System.out.println("ERROR: E002 Missing Argument!");\r
325 System.out\r
326 .println("The remove function requires an argument, either a package name or a platform name!");\r
327 System.out.println("DbUpdate Aborted!");\r
328 System.err.flush();\r
329 System.exit(EFAILURE);\r
330 }\r
331 commandToCall = Cmd.DELETE_SOMETHING;\r
332 commandArgs = args[i];\r
333 setArgType(commandArgs);\r
334 }\r
335 if (arg.toLowerCase().contains("--find")) {\r
336 commandToCall = Cmd.FIND_SOMETHING;\r
337 i++;\r
338 if (args[i].toLowerCase().contains("--lib"))\r
339 whatToFind = "LIBRARY";\r
340 else if (args[i].toLowerCase().contains("--guid"))\r
341 whatToFind = "GUID";\r
342 else if (args[i].toLowerCase().contains("--ppi"))\r
343 whatToFind = "PPI";\r
344 else if (args[i].toLowerCase().contains("--prot"))\r
345 whatToFind = "PROTOCOL";\r
346 else if (args[i].toLowerCase().contains("--pcd"))\r
347 whatToFind = "PCD";\r
348 else if (args[i].startsWith("-")) {\r
349 System.out.println("ERROR: E001 Invalid Argument");\r
350 System.out.println("The find function takes either a qualifier of --guid, --ppi,");\r
351 System.out.println(" --proto, --pcd, or the string to search for.");\r
352 System.err.flush();\r
353 System.exit(EFAILURE);\r
354 } else\r
355 commandArgs = args[i];\r
356\r
357 if (!whatToFind.contentEquals("")) {\r
358 i++;\r
359 if (args[i].startsWith("-")) {\r
360 System.out.println("ERROR: E001 Invalid Argument");\r
361 System.out.println("The find function qualifier (--guid, --ppi, --proto, --pcd)");\r
362 System.out.println(" must be followed by the string to search for.");\r
363 System.err.flush();\r
364 System.exit(EFAILURE);\r
365 } else\r
366 commandArgs = args[i];\r
367 }\r
368\r
369 }\r
370 if (arg.trim().contentEquals("-v")) {\r
371 VERBOSE++;\r
372 }\r
373 if (arg.toLowerCase().contains("--lib")) {\r
374 whatToFind = "LIBRARY";\r
375 }\r
376 if (arg.toLowerCase().contains("--guid")) {\r
377 whatToFind = "GUID";\r
378 }\r
379 if (arg.toLowerCase().contains("--ppi")) {\r
380 whatToFind = "PPI";\r
381 }\r
382 if (arg.toLowerCase().contains("--prot")) {\r
383 whatToFind = "PROTOCOL";\r
384 }\r
385 if (arg.toLowerCase().contains("--pcd")) {\r
386 whatToFind = "PCD";\r
387 }\r
388 if (arg.toLowerCase().contentEquals("-d") || arg.trim().toLowerCase().contains("--debug")) {\r
389 if ((i + 1 == args.length) || (args[i + 1].trim().startsWith("-"))) {\r
390 DEBUG = 1;\r
391 } else if (i + 1 < args.length) {\r
392 String pattern = "^\\d+";\r
393 if (args[i + 1].trim().matches(pattern)) {\r
394 i++;\r
395 DEBUG = Integer.parseInt(args[i]);\r
396 } else\r
397 DEBUG = 1;\r
398 }\r
399 }\r
400 if (arg.trim().contentEquals("-V")) {\r
401 System.out.println("DbTools, " + version);\r
402 System.out.println(copyright);\r
403 System.out.println(Specification);\r
404 System.err.flush();\r
405 System.exit(ESUCCESS);\r
406 }\r
407 if (arg.toLowerCase().trim().startsWith("--pack")) {\r
408 commandToCall = Cmd.SHOW_PACKAGES;\r
409 }\r
410 if (arg.toLowerCase().trim().startsWith("--far")) {\r
411 commandToCall = Cmd.SHOW_FAR;\r
412 }\r
413 if (arg.toLowerCase().trim().startsWith("--plat")) {\r
414 commandToCall = Cmd.SHOW_PLATFORMS;\r
415 }\r
416 if ((arg.toLowerCase().contentEquals("-h")) || (arg.toLowerCase().contentEquals("-?"))\r
417 || (arg.toLowerCase().startsWith("/h")) || (arg.toLowerCase().contentEquals("--help"))) {\r
418 outputUsage();\r
419 System.exit(EFAILURE);\r
420 }\r
421\r
422 }\r
423 return ESUCCESS;\r
424 }\r
425\r
426 private int testFile(String Filename) {\r
427 File tFile = new File(Filename);\r
428 if (DEBUG > 4)\r
429 System.out.println("File is located: " + tFile.getPath());\r
430 if (tFile.exists())\r
431 return FOUND;\r
432 else\r
433 return NOTFOUND;\r
434 }\r
435\r
436 private void setArgType(String argv) {\r
437 if (argv.trim().toLowerCase().contains(".spd"))\r
438 isPackage = true;\r
439 if (argv.trim().toLowerCase().contains(".fpd"))\r
440 isPlatform = true;\r
441 if (argv.trim().toLowerCase().contains(".far"))\r
442 isFar = true;\r
443 }\r
444\r
445 public String getArgType() {\r
446 String argt = "UNKNOWN";\r
447 if (isPackage)\r
448 argt = "SPD";\r
449 if (isFar)\r
450 argt = "FAR";\r
451 if (isPlatform)\r
452 argt = "FPD";\r
453 return argt;\r
454 }\r
455\r
456 private static void outputUsage() {\r
457\r
458 System.out.println("DbTool, " + version);\r
459 System.out.println(copyright);\r
460 System.out.println("Usage:");\r
461 System.out.println(" DbTool [-v] [-t] [-q] [-V] [--package] [--platform] [-h | -? | --help]");\r
462 System.out.println(" where:");\r
463 System.out.println(" -h | -? | --help OPTIONAL - This Help Text");\r
464 System.out\r
465 .println(" -t | --test OPTIONAL - Test the FrameworkDatabase Contents against the WORKSPACE");\r
466 System.out\r
467 .println(" -q OPTIONAL - Quiet mode - pass or fail only on return value, nothing is printed");\r
468 System.out\r
469 .println(" -f | --fix OPTIONAL - Automatically fix (non-interactive) the Database file to match the WORKSPACE.");\r
470 System.out\r
471 .println(" -v OPTIONAL - Verbose, print information messages. Adding more -v arguments increases verbosity.");\r
472 System.out.println(" --package OPTIONAL - Show all Packages installed in the WORKSPACE.");\r
473 System.out.println(" --platforms OPTIONAL - Show all Platforms installed in the WORKSPACE.");\r
94e17f11 474 System.out.println(" -V OPTIONAL - Display Version information and exit.");\r
4de92764 475 //\r
476 // TODO: Implement the following options.\r
477 //\r
478 /**\r
479 System.out.println("");\r
480 System.out.println(" =================== Options below this line have not been implemented in this release ===================");\r
481 System.out\r
482 .println(" -i OPTIONAL - Force interactive on commands that modify the WORKSPACE");\r
483 System.out\r
484 .println(" --far OPTIONAL - Show all Framework Archives installed in the WORKSPACE.");\r
485 System.out\r
486 .println(" --find [qualifier] value OPTIONAL - Search the WORKSPACE for value, with one and only one optional Qualifier.");\r
487 System.out.println(" qualifiers: --guid Find a GUID by Guid C Name");\r
488 System.out.println(" --prot Find a Protocol or ProtocolNotify by C Name");\r
489 System.out.println(" --ppi Find a PPI or PpiNotify by C Name");\r
490 System.out.println(" --pcd Find a PCD entry by C Name");\r
491 System.out.println(" --lib Find information about a Library Class");\r
492 System.out\r
493 .println(" -c OPTIONAL - Print a comma separated value listing of TYPE,UiName,Filename");\r
494 System.out\r
495 .println(" -a, --add value OPTIONAL - Add a value (package, platform or far) to the database");\r
496 System.out\r
497 .println(" -r, --del value OPTIONAL - Remove a value (package, platform or far) from the database");\r
498 System.out.println("");\r
499 */\r
500 }\r
501}\r