1 | |
package org.codehaus.plexus.util.cli; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.codehaus.plexus.util.cli.shell.Shell; |
23 | |
import org.codehaus.plexus.util.cli.shell.CmdShell; |
24 | |
import org.codehaus.plexus.util.cli.shell.CommandShell; |
25 | |
|
26 | |
import java.io.File; |
27 | |
import java.io.IOException; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Arrays; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
import java.util.Properties; |
33 | |
import java.util.StringTokenizer; |
34 | |
import java.util.Vector; |
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 | |
public class Commandline |
65 | |
implements Cloneable |
66 | |
{ |
67 | |
protected static final String OS_NAME = "os.name"; |
68 | |
|
69 | |
protected static final String WINDOWS = "Windows"; |
70 | |
|
71 | 0 | protected String executable = null; |
72 | |
|
73 | 0 | protected Vector arguments = new Vector(); |
74 | |
|
75 | 0 | protected Vector envVars = new Vector(); |
76 | |
|
77 | 0 | private File workingDir = null; |
78 | |
|
79 | 0 | private long pid = -1; |
80 | |
|
81 | |
private Shell shell; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public Commandline( String toProcess ) |
90 | |
{ |
91 | 0 | super(); |
92 | 0 | setDefaultShell(); |
93 | 0 | String[] tmp = new String[0]; |
94 | |
try |
95 | |
{ |
96 | 0 | tmp = translateCommandline( toProcess ); |
97 | |
} |
98 | 0 | catch ( Exception e ) |
99 | |
{ |
100 | 0 | System.err.println( "Error translating Commandline." ); |
101 | 0 | } |
102 | 0 | if ( tmp != null && tmp.length > 0 ) |
103 | |
{ |
104 | 0 | setExecutable( tmp[0] ); |
105 | 0 | for ( int i = 1; i < tmp.length; i++ ) |
106 | |
{ |
107 | 0 | createArgument().setValue( tmp[i] ); |
108 | |
} |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public Commandline() |
117 | |
{ |
118 | 0 | super(); |
119 | 0 | setDefaultShell(); |
120 | 0 | } |
121 | |
|
122 | |
public long getPid() |
123 | |
{ |
124 | 0 | if ( pid == -1 ) |
125 | |
{ |
126 | 0 | pid = Long.parseLong( String.valueOf( System.currentTimeMillis() ) ); |
127 | |
} |
128 | |
|
129 | 0 | return pid; |
130 | |
} |
131 | |
|
132 | |
public void setPid( long pid ) |
133 | |
{ |
134 | 0 | this.pid = pid; |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 0 | public static class Argument |
141 | |
{ |
142 | |
|
143 | |
private String[] parts; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public void setValue( String value ) |
151 | |
{ |
152 | 0 | if ( value != null ) |
153 | |
{ |
154 | 0 | parts = new String[]{value}; |
155 | |
} |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public void setLine( String line ) |
164 | |
{ |
165 | 0 | if ( line == null ) |
166 | |
{ |
167 | 0 | return; |
168 | |
} |
169 | |
try |
170 | |
{ |
171 | 0 | parts = translateCommandline( line ); |
172 | |
} |
173 | 0 | catch ( Exception e ) |
174 | |
{ |
175 | 0 | System.err.println( "Error translating Commandline." ); |
176 | 0 | } |
177 | 0 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public void setFile( File value ) |
186 | |
{ |
187 | 0 | parts = new String[]{value.getAbsolutePath()}; |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public String[] getParts() |
194 | |
{ |
195 | 0 | return parts; |
196 | |
} |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public class Marker |
206 | |
{ |
207 | |
|
208 | |
private int position; |
209 | |
|
210 | 0 | private int realPos = -1; |
211 | |
|
212 | |
Marker( int position ) |
213 | 0 | { |
214 | 0 | this.position = position; |
215 | 0 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public int getPosition() |
224 | |
{ |
225 | 0 | if ( realPos == -1 ) |
226 | |
{ |
227 | 0 | realPos = ( executable == null ? 0 : 1 ); |
228 | 0 | for ( int i = 0; i < position; i++ ) |
229 | |
{ |
230 | 0 | Argument arg = (Argument) arguments.elementAt( i ); |
231 | 0 | realPos += arg.getParts().length; |
232 | |
} |
233 | |
} |
234 | 0 | return realPos; |
235 | |
} |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
private void setDefaultShell() |
244 | |
{ |
245 | 0 | String os = System.getProperty( OS_NAME ); |
246 | |
|
247 | |
|
248 | 0 | if ( os.indexOf( WINDOWS ) > -1 ) |
249 | |
{ |
250 | 0 | if ( os.indexOf( "95" ) > -1 || os.indexOf( "98" ) > -1 || os.indexOf( "Me" ) > -1 ) |
251 | |
{ |
252 | 0 | setShell( new CommandShell() ); |
253 | |
} |
254 | |
else |
255 | |
{ |
256 | 0 | setShell( new CmdShell() ); |
257 | |
} |
258 | |
} |
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public Argument createArgument() |
272 | |
{ |
273 | 0 | return this.createArgument( false ); |
274 | |
} |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
public Argument createArgument( boolean insertAtStart ) |
286 | |
{ |
287 | 0 | Argument argument = new Argument(); |
288 | 0 | if ( insertAtStart ) |
289 | |
{ |
290 | 0 | arguments.insertElementAt( argument, 0 ); |
291 | |
} |
292 | |
else |
293 | |
{ |
294 | 0 | arguments.addElement( argument ); |
295 | |
} |
296 | 0 | return argument; |
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
public void setExecutable( String executable ) |
303 | |
{ |
304 | 0 | if ( executable == null || executable.length() == 0 ) |
305 | |
{ |
306 | 0 | return; |
307 | |
} |
308 | 0 | this.executable = executable.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); |
309 | 0 | } |
310 | |
|
311 | |
public String getExecutable() |
312 | |
{ |
313 | 0 | return executable; |
314 | |
} |
315 | |
|
316 | |
public void addArguments( String[] line ) |
317 | |
{ |
318 | 0 | for ( int i = 0; i < line.length; i++ ) |
319 | |
{ |
320 | 0 | createArgument().setValue( line[i] ); |
321 | |
} |
322 | 0 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public void addEnvironment( String name, |
328 | |
String value ) |
329 | |
{ |
330 | 0 | envVars.add( name + "=" + value ); |
331 | 0 | } |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
public void addSystemEnvironment() |
337 | |
throws Exception |
338 | |
{ |
339 | 0 | Properties envVars = CommandLineUtils.getSystemEnvVars(); |
340 | |
|
341 | 0 | for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); ) |
342 | |
{ |
343 | 0 | String key = (String) i.next(); |
344 | |
|
345 | 0 | addEnvironment( key, envVars.getProperty( key ) ); |
346 | |
} |
347 | 0 | } |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public String[] getEnvironmentVariables() |
353 | |
throws CommandLineException |
354 | |
{ |
355 | |
try |
356 | |
{ |
357 | 0 | addSystemEnvironment(); |
358 | |
} |
359 | 0 | catch ( Exception e ) |
360 | |
{ |
361 | 0 | throw new CommandLineException( "Error setting up environmental variables", e ); |
362 | 0 | } |
363 | |
|
364 | 0 | return (String[]) envVars.toArray( new String[envVars.size()] ); |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
public String[] getCommandline() |
371 | |
{ |
372 | 0 | final String[] args = getArguments(); |
373 | 0 | if ( executable == null ) |
374 | |
{ |
375 | 0 | return args; |
376 | |
} |
377 | 0 | final String[] result = new String[args.length + 1]; |
378 | 0 | result[0] = executable; |
379 | 0 | System.arraycopy( args, 0, result, 1, args.length ); |
380 | 0 | return result; |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public String[] getShellCommandline() |
387 | |
{ |
388 | |
|
389 | 0 | if ( getShell() == null ) |
390 | |
{ |
391 | 0 | if ( executable != null ) |
392 | |
{ |
393 | 0 | List commandLine = new ArrayList(); |
394 | 0 | commandLine.add( executable ); |
395 | 0 | commandLine.addAll( Arrays.asList( getArguments() ) ); |
396 | 0 | return (String[]) commandLine.toArray( new String[0] ); |
397 | |
} |
398 | |
else |
399 | |
{ |
400 | 0 | return getArguments(); |
401 | |
} |
402 | |
|
403 | |
} |
404 | |
else |
405 | |
{ |
406 | 0 | return (String[]) getShell().getShellCommandLine( executable, getArguments() ).toArray( new String[0] ); |
407 | |
} |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
public String[] getArguments() |
415 | |
{ |
416 | 0 | Vector result = new Vector( arguments.size() * 2 ); |
417 | 0 | for ( int i = 0; i < arguments.size(); i++ ) |
418 | |
{ |
419 | 0 | Argument arg = (Argument) arguments.elementAt( i ); |
420 | 0 | String[] s = arg.getParts(); |
421 | 0 | if ( s != null ) |
422 | |
{ |
423 | 0 | for ( int j = 0; j < s.length; j++ ) |
424 | |
{ |
425 | 0 | result.addElement( s[j] ); |
426 | |
} |
427 | |
} |
428 | |
} |
429 | |
|
430 | 0 | String[] res = new String[result.size()]; |
431 | 0 | result.copyInto( res ); |
432 | 0 | return res; |
433 | |
} |
434 | |
|
435 | |
public String toString() |
436 | |
{ |
437 | 0 | return toString( getCommandline() ); |
438 | |
} |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public static String quoteArgument( String argument ) |
450 | |
throws CommandLineException |
451 | |
{ |
452 | 0 | if ( argument.indexOf( "\"" ) > -1 ) |
453 | |
{ |
454 | 0 | if ( argument.indexOf( "\'" ) > -1 ) |
455 | |
{ |
456 | 0 | throw new CommandLineException( "Can't handle single and double quotes in same argument" ); |
457 | |
} |
458 | |
else |
459 | |
{ |
460 | 0 | return '\'' + argument + '\''; |
461 | |
} |
462 | |
} |
463 | 0 | else if ( argument.indexOf( "\'" ) > -1 || argument.indexOf( " " ) > -1 ) |
464 | |
{ |
465 | 0 | return '\"' + argument + '\"'; |
466 | |
} |
467 | |
else |
468 | |
{ |
469 | 0 | return argument; |
470 | |
} |
471 | |
} |
472 | |
|
473 | |
public static String toString( String[] line ) |
474 | |
{ |
475 | |
|
476 | 0 | if ( line == null || line.length == 0 ) |
477 | |
{ |
478 | 0 | return ""; |
479 | |
} |
480 | |
|
481 | |
|
482 | 0 | final StringBuffer result = new StringBuffer(); |
483 | 0 | for ( int i = 0; i < line.length; i++ ) |
484 | |
{ |
485 | 0 | if ( i > 0 ) |
486 | |
{ |
487 | 0 | result.append( ' ' ); |
488 | |
} |
489 | |
try |
490 | |
{ |
491 | 0 | result.append( quoteArgument( line[i] ) ); |
492 | |
} |
493 | 0 | catch ( Exception e ) |
494 | |
{ |
495 | 0 | System.err.println( "Error quoting argument." ); |
496 | 0 | } |
497 | |
} |
498 | 0 | return result.toString(); |
499 | |
} |
500 | |
|
501 | |
public static String[] translateCommandline( String toProcess ) |
502 | |
throws Exception |
503 | |
{ |
504 | 0 | if ( toProcess == null || toProcess.length() == 0 ) |
505 | |
{ |
506 | 0 | return new String[0]; |
507 | |
} |
508 | |
|
509 | |
|
510 | |
|
511 | 0 | final int normal = 0; |
512 | 0 | final int inQuote = 1; |
513 | 0 | final int inDoubleQuote = 2; |
514 | 0 | int state = normal; |
515 | 0 | StringTokenizer tok = new StringTokenizer( toProcess, "\"\' ", true ); |
516 | 0 | Vector v = new Vector(); |
517 | 0 | StringBuffer current = new StringBuffer(); |
518 | |
|
519 | 0 | while ( tok.hasMoreTokens() ) |
520 | |
{ |
521 | 0 | String nextTok = tok.nextToken(); |
522 | 0 | switch ( state ) |
523 | |
{ |
524 | |
case inQuote: |
525 | 0 | if ( "\'".equals( nextTok ) ) |
526 | |
{ |
527 | 0 | state = normal; |
528 | |
} |
529 | |
else |
530 | |
{ |
531 | 0 | current.append( nextTok ); |
532 | |
} |
533 | 0 | break; |
534 | |
case inDoubleQuote: |
535 | 0 | if ( "\"".equals( nextTok ) ) |
536 | |
{ |
537 | 0 | state = normal; |
538 | |
} |
539 | |
else |
540 | |
{ |
541 | 0 | current.append( nextTok ); |
542 | |
} |
543 | 0 | break; |
544 | |
default: |
545 | 0 | if ( "\'".equals( nextTok ) ) |
546 | |
{ |
547 | 0 | state = inQuote; |
548 | |
} |
549 | 0 | else if ( "\"".equals( nextTok ) ) |
550 | |
{ |
551 | 0 | state = inDoubleQuote; |
552 | |
} |
553 | 0 | else if ( " ".equals( nextTok ) ) |
554 | |
{ |
555 | 0 | if ( current.length() != 0 ) |
556 | |
{ |
557 | 0 | v.addElement( current.toString() ); |
558 | 0 | current.setLength( 0 ); |
559 | |
} |
560 | |
} |
561 | |
else |
562 | |
{ |
563 | 0 | current.append( nextTok ); |
564 | |
} |
565 | 0 | break; |
566 | |
} |
567 | |
} |
568 | |
|
569 | 0 | if ( current.length() != 0 ) |
570 | |
{ |
571 | 0 | v.addElement( current.toString() ); |
572 | |
} |
573 | |
|
574 | 0 | if ( state == inQuote || state == inDoubleQuote ) |
575 | |
{ |
576 | 0 | throw new CommandLineException( "unbalanced quotes in " + toProcess ); |
577 | |
} |
578 | |
|
579 | 0 | String[] args = new String[v.size()]; |
580 | 0 | v.copyInto( args ); |
581 | 0 | return args; |
582 | |
} |
583 | |
|
584 | |
public int size() |
585 | |
{ |
586 | 0 | return getCommandline().length; |
587 | |
} |
588 | |
|
589 | |
public Object clone() |
590 | |
{ |
591 | 0 | Commandline c = new Commandline(); |
592 | 0 | c.setExecutable( executable ); |
593 | 0 | c.addArguments( getArguments() ); |
594 | 0 | return c; |
595 | |
} |
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
public void clear() |
601 | |
{ |
602 | 0 | executable = null; |
603 | 0 | arguments.removeAllElements(); |
604 | 0 | } |
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
public void clearArgs() |
610 | |
{ |
611 | 0 | arguments.removeAllElements(); |
612 | 0 | } |
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
public Marker createMarker() |
622 | |
{ |
623 | 0 | return new Marker( arguments.size() ); |
624 | |
} |
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
public void setWorkingDirectory( String path ) |
630 | |
{ |
631 | 0 | if ( path != null ) |
632 | |
{ |
633 | 0 | workingDir = new File( path ); |
634 | |
} |
635 | 0 | } |
636 | |
|
637 | |
public File getWorkingDirectory() |
638 | |
{ |
639 | 0 | return workingDir; |
640 | |
} |
641 | |
|
642 | |
|
643 | |
|
644 | |
|
645 | |
public Process execute() |
646 | |
throws CommandLineException |
647 | |
{ |
648 | |
Process process; |
649 | |
|
650 | |
|
651 | |
|
652 | 0 | String[] environment = getEnvironmentVariables(); |
653 | |
|
654 | |
try |
655 | |
{ |
656 | 0 | if ( workingDir == null ) |
657 | |
{ |
658 | 0 | process = Runtime.getRuntime().exec( getShellCommandline(), environment ); |
659 | |
} |
660 | |
else |
661 | |
{ |
662 | 0 | if ( !workingDir.exists() ) |
663 | |
{ |
664 | 0 | throw new CommandLineException( |
665 | |
"Working directory \"" + workingDir.getPath() + "\" does not exist!" ); |
666 | |
} |
667 | 0 | else if ( !workingDir.isDirectory() ) |
668 | |
{ |
669 | 0 | throw new CommandLineException( |
670 | |
"Path \"" + workingDir.getPath() + "\" does not specify a directory." ); |
671 | |
} |
672 | |
|
673 | 0 | process = Runtime.getRuntime().exec( getShellCommandline(), environment, workingDir ); |
674 | |
} |
675 | |
} |
676 | 0 | catch ( IOException ex ) |
677 | |
{ |
678 | 0 | throw new CommandLineException( "Error while executing process.", ex ); |
679 | 0 | } |
680 | |
|
681 | 0 | return process; |
682 | |
} |
683 | |
|
684 | |
public Properties getSystemEnvVars() |
685 | |
throws Exception |
686 | |
{ |
687 | 0 | return CommandLineUtils.getSystemEnvVars(); |
688 | |
} |
689 | |
|
690 | |
|
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
|
696 | |
public void setShell( Shell shell ) |
697 | |
{ |
698 | 0 | this.shell = shell; |
699 | 0 | } |
700 | |
|
701 | |
|
702 | |
|
703 | |
|
704 | |
|
705 | |
|
706 | |
public Shell getShell() |
707 | |
{ |
708 | 0 | return shell; |
709 | |
} |
710 | |
} |