AbstractOperatingSystem.st
changeset 5482 433983048ce8
parent 5480 92eb8594f437
child 5483 1d81b1ad42d0
equal deleted inserted replaced
5481:680d6de80808 5482:433983048ce8
  1464     ^ UnsupportedOperationSignal raise
  1464     ^ UnsupportedOperationSignal raise
  1465 !
  1465 !
  1466 
  1466 
  1467 getCommandOutputFrom:aCommand
  1467 getCommandOutputFrom:aCommand
  1468     "execute a simple command (such as hostname) and
  1468     "execute a simple command (such as hostname) and
  1469      return the commands first line of output as a string.
  1469      return the commands first line of output as a string (forget stdErr).
  1470      If the command generates multiple output lines, only the first line is returned.
  1470      If the command generates multiple output lines, only the first line is returned.
  1471      If the commands does not generate any output, an empty string is returned;
  1471      If the commands does not generate any output, an empty string is returned;
  1472      if the command fails, nil is returned."
  1472      if the command fails, nil is returned."
  1473 
  1473 
  1474     |result|
  1474     |result|
  1475 
  1475 
  1476     result := self getCommandOutputFrom:aCommand maxNumberOfLines:1.
  1476     result := self getCommandOutputFrom:aCommand maxNumberOfLines:1 errorDisposition:#discard.
  1477     result notNil ifTrue:[
  1477     result notNil ifTrue:[
  1478         ^ result firstIfEmpty:['']
  1478         ^ result firstIfEmpty:['']
  1479     ].
  1479     ].
  1480     ^ result
  1480     ^ result
  1481 
  1481 
  1486      OperatingSystem getCommandOutputFrom:'foo'   
  1486      OperatingSystem getCommandOutputFrom:'foo'   
  1487     "
  1487     "
  1488 
  1488 
  1489 !
  1489 !
  1490 
  1490 
  1491 getCommandOutputFrom:aCommand maxNumberOfLines:numLinesOrNil
  1491 getCommandOutputFrom:aCommand maxNumberOfLines:numLinesOrNil errorDisposition:errorDisposition
  1492     "execute a simple command (such as hostname) and
  1492     "execute a simple command (such as hostname) and
  1493      return the commands output as a collection of strings,
  1493      return the commands output as a collection of strings,
  1494      but only up to the given number of lines (if non-nil).
  1494      but only up to the given number of lines (if non-nil).
  1495      If the command generates more output, only the first nLines are returned
  1495      If the command generates more output, only the first nLines are returned
  1496      (but the command is allowed to finish execution).
  1496      (but the command is allowed to finish execution).
  1497      If the commands does not generate any output, an empty string is returned;
  1497      If the commands does not generate any output, an empty string is returned;
  1498      if the command fails, nil is returned."
  1498      if the command fails, nil is returned.
       
  1499      errorDisposition controls where the stdErr output should go,
       
  1500      and may be one of #discard, #inline or #stderr (default).
       
  1501      #discard causes stderr to be discarded (/dev/null), 
       
  1502      #inline causes it to be written to smalltalks own stdout and
       
  1503      #stderr causes it to be written to smalltalks own stderr.
       
  1504      Nil is treated like #stderr"
  1499 
  1505 
  1500     |result|
  1506     |result|
  1501 
  1507 
  1502     PipeFailed ~~ true ifTrue:[
  1508     PipeFailed ~~ true ifTrue:[
  1503         PipeStream openErrorSignal handle:[:ex |
  1509         PipeStream openErrorSignal handle:[:ex |
  1507         ] do:[
  1513         ] do:[
  1508             |p line|
  1514             |p line|
  1509 
  1515 
  1510             p := PipeStream 
  1516             p := PipeStream 
  1511                     readingFrom:aCommand
  1517                     readingFrom:aCommand
  1512                     errorDisposition:#discard
  1518                     errorDisposition:errorDisposition
  1513                     inDirectory:nil.
  1519                     inDirectory:nil.
  1514             p notNil ifTrue:[
  1520             p notNil ifTrue:[
  1515                 result := StringCollection new.
  1521                 result := StringCollection new.
  1516                 [p atEnd] whileFalse:[
  1522                 [p atEnd] whileFalse:[
  1517                     line := p nextLine.
  1523                     line := p nextLine.
  1540 
  1546 
  1541     "Modified: / 19.5.1999 / 14:25:02 / cg"
  1547     "Modified: / 19.5.1999 / 14:25:02 / cg"
  1542 !
  1548 !
  1543 
  1549 
  1544 getFullCommandOutputFrom:aCommand
  1550 getFullCommandOutputFrom:aCommand
  1545     "execute a simple command (such as hostname) and
  1551     "execute a command and
  1546      return the commands output as a collection of strings.
  1552      return the commands output as a collection of strings (including stdErr).
  1547      If the commands does not generate any output, an empty string is returned;
  1553      If the commands does not generate any output, an empty string is returned;
  1548      if the command fails, nil is returned."
  1554      if the command fails, nil is returned."
  1549 
  1555 
  1550     ^ self getCommandOutputFrom:aCommand maxNumberOfLines:nil
  1556     ^ self getCommandOutputFrom:aCommand maxNumberOfLines:nil errorDisposition:#inline
  1551 
  1557 
  1552     "
  1558     "
  1553      OperatingSystem getFullCommandOutputFrom:'mt status'
  1559      OperatingSystem getFullCommandOutputFrom:'mt status'
  1554     "
  1560     "
  1555 
  1561 
  4230 ! !
  4236 ! !
  4231 
  4237 
  4232 !AbstractOperatingSystem class methodsFor:'documentation'!
  4238 !AbstractOperatingSystem class methodsFor:'documentation'!
  4233 
  4239 
  4234 version
  4240 version
  4235     ^ '$Header: /cvs/stx/stx/libbasic/AbstractOperatingSystem.st,v 1.41 2000-07-25 10:47:43 cg Exp $'
  4241     ^ '$Header: /cvs/stx/stx/libbasic/AbstractOperatingSystem.st,v 1.42 2000-07-25 14:15:27 cg Exp $'
  4236 ! !
  4242 ! !
  4237 AbstractOperatingSystem initialize!
  4243 AbstractOperatingSystem initialize!