better getCommandOutputFrom:
authorClaus Gittinger <cg@exept.de>
Tue, 25 Jul 2000 12:47:43 +0200
changeset 5480 92eb8594f437
parent 5479 490e589f98d3
child 5481 680d6de80808
better getCommandOutputFrom:
AbstractOperatingSystem.st
--- a/AbstractOperatingSystem.st	Mon Jul 24 12:38:48 2000 +0200
+++ b/AbstractOperatingSystem.st	Tue Jul 25 12:47:43 2000 +0200
@@ -1473,38 +1473,88 @@
 
     |result|
 
+    result := self getCommandOutputFrom:aCommand maxNumberOfLines:1.
+    result notNil ifTrue:[
+        ^ result firstIfEmpty:['']
+    ].
+    ^ result
+
+    "
+     OperatingSystem getCommandOutputFrom:'hostname' 
+     OperatingSystem getCommandOutputFrom:'pwd'   
+     OperatingSystem getCommandOutputFrom:'sleep 1'   
+     OperatingSystem getCommandOutputFrom:'foo'   
+    "
+
+!
+
+getCommandOutputFrom:aCommand maxNumberOfLines:numLinesOrNil
+    "execute a simple command (such as hostname) and
+     return the commands output as a collection of strings,
+     but only up to the given number of lines (if non-nil).
+     If the command generates more output, only the first nLines are returned
+     (but the command is allowed to finish execution).
+     If the commands does not generate any output, an empty string is returned;
+     if the command fails, nil is returned."
+
+    |result|
+
     PipeFailed ~~ true ifTrue:[
         PipeStream openErrorSignal handle:[:ex |
             PipeFailed := true.
             'OperatingSystem [warning]: cannot fork/popen' errorPrintCR.
             ex return.
         ] do:[
-            |p|
+            |p line|
 
             p := PipeStream 
                     readingFrom:aCommand
                     errorDisposition:#discard
                     inDirectory:nil.
             p notNil ifTrue:[
-                p atEnd ifTrue:[
-                    result := ''
-                ] ifFalse:[
-                    result := p nextLine.
+                result := StringCollection new.
+                [p atEnd] whileFalse:[
+                    line := p nextLine.
+                    (numLinesOrNil isNil 
+                    or:[result size < numLinesOrNil]) ifTrue:[
+                        result add:line
+                    ].
                 ].
-                p close
+                p close.
+                p exitStatus success ifFalse:[
+                    result isEmpty ifTrue:[
+                        result := nil
+                    ]
+                ].
             ].
         ].
     ].
     ^ result
 
     "
-     OperatingSystem getCommandOutputFrom:'hostname'
-     OperatingSystem getCommandOutputFrom:'pwd' 
+     OperatingSystem getCommandOutputFrom:'ls' maxNumberOfLines:1
+     OperatingSystem getCommandOutputFrom:'ls' maxNumberOfLines:10
+     OperatingSystem getCommandOutputFrom:'ls' maxNumberOfLines:nil
+     OperatingSystem getCommandOutputFrom:'foo' maxNumberOfLines:nil
     "
 
     "Modified: / 19.5.1999 / 14:25:02 / cg"
 !
 
+getFullCommandOutputFrom:aCommand
+    "execute a simple command (such as hostname) and
+     return the commands output as a collection of strings.
+     If the commands does not generate any output, an empty string is returned;
+     if the command fails, nil is returned."
+
+    ^ self getCommandOutputFrom:aCommand maxNumberOfLines:nil
+
+    "
+     OperatingSystem getFullCommandOutputFrom:'mt status'
+    "
+
+!
+
 getVMSSymbol:aSymbolString
     "get a symbols value, or nil if there is none"
 
@@ -4182,6 +4232,6 @@
 !AbstractOperatingSystem class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/AbstractOperatingSystem.st,v 1.40 2000-06-28 07:53:30 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/AbstractOperatingSystem.st,v 1.41 2000-07-25 10:47:43 cg Exp $'
 ! !
 AbstractOperatingSystem initialize!