#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Sat, 03 Mar 2018 10:00:39 +0100
changeset 22568 eb116af8c3a2
parent 22567 8c44c87bd949
child 22569 3f6ffe519140
#FEATURE by cg class: OSProcess class added: #commandStringForProgramName:arguments: #defaultShellPath #possiblyQuoted: #programName:arguments:initialEnvironment: comment/format in: #initialize
OSProcess.st
--- a/OSProcess.st	Fri Mar 02 19:11:59 2018 +0100
+++ b/OSProcess.st	Sat Mar 03 10:00:39 2018 +0100
@@ -187,6 +187,15 @@
     ^ self new command:aCommandString directory:aStringOrFilename.
 !
 
+commandStringForProgramName:executableFile arguments:arrayOfStrings
+    ^ String streamContents:[:s |
+        s nextPutAll:(self possiblyQuoted:executableFile asFilename pathName).
+        arrayOfStrings do:[:eachArg |
+            s nextPutAll:(self possiblyQuoted:eachArg).
+        ].
+    ]
+!
+
 new
     "return an initialized instance for a local process"
 
@@ -208,15 +217,52 @@
      (OSProcess new) command:'ls'
      (OSProcess onHost:'exeptn') command:'ls'
     "
+!
+
+possiblyQuoted:aString
+    "should we quote or escape?"
+
+    ^ String streamContents:[:s |
+        aString do:[:ch |
+            (ch isSeparator or:[ch = $\]) ifTrue:[
+                s nextPut:$\
+            ].
+            s nextPut:ch
+        ]
+    ]
+
+    "
+     self possiblyQuoted:'foo bar' 
+     self possiblyQuoted:'foo\bar'
+    "
+!
+
+programName:executableFile arguments:arrayOfStrings initialEnvironment:stringDictionary
+    "similar to command:, but with separate command and arguments"
+
+    ^ self new
+        command:(self commandStringForProgramName:executableFile arguments:arrayOfStrings);
+        environment:stringDictionary;
+        yourself
 ! !
 
 !OSProcess class methodsFor:'class initialization'!
 
 initialize
-    "Backward compatibility"
+    "Backward compatibility: Win32Process is an alias for OSProcess"
+
     Win32Process := self.
 ! !
 
+!OSProcess class methodsFor:'queries'!
+
+defaultShellPath
+    OperatingSystem isUNIXlike ifTrue:[
+        ^ OperatingSystem getEnvironment:'SHELL' 
+    ].
+    ^ 'cmd'
+! !
+
 !OSProcess methodsFor:'accessing'!
 
 auxStream