?Executing shell command?

Ian Piumarta Ian.Piumarta at inria.fr
Thu Oct 21 16:01:55 UTC 1999


Couldn't resist... ;-)

> How can I execute shell command from Squeak

Starting from the squeak 2.6 top-level dir:

	myUnixBox$ mkdir src/System
	myUnixBox$ cd src/System
	myUnixBox$ ln -s ../Makefile .
	myUnixBox$ cat > system.c
	#include <stdlib.h>
	#include "sqVirtualMachine.h"
	
	struct VirtualMachine *interp;
	
	int primitiveSystem(void)
	{
	  int receiver= interp->stackValue(0);
	  int size= interp->stSizeOf(receiver);
	  char buf[1024];
	  int i;
	
	  if ((interp->methodArgumentCount() != 0) ||
	      (interp->fetchClassOf(receiver) != interp->classString()))
	    return interp->success(0);
	
	  for (i= 0; i < size; ++i)
	    buf[i]= ((char *)receiver)[i + 4];
	  buf[i]= '\0';
	
	  system(buf);
	  return 1;
	}
	
	int setInterpreter(struct VirtualMachine *proxy)
	{
	  interp= proxy;
	  return 1;
	}
EOF->	^D
	myUnixBox$ cd ../..
	myUnixBox$ make reconfig
	myUnixBox$ make
	myUnixBox$ cat > String-system.st
	'From Squeak2.6 of 11 October 1999 [latest update: #1559] on 21 October 1999 at 5:48:45 pm'!
	
	!String methodsFor: 'system commands' stamp: 'ikp 10/21/1999 17:48'!
	system		" 'ls' system "
	
		<primitive: 'primitiveSystem' module: 'System'>
		^self primitiveFailed! !
EOF->	^D
	myUnixBox$ ./<myUnixArch>/squeak

When Squeak pops up, open a file list and then file-in
"String-system.st".  Then execute:

	'ls' system

in a workspace somewhere.  If you want to collect the output of the
Unix command, then some variation of the above based on popen(3) is
probably indicated.

Of course, there is probably a much easier way of doing the same thing
using the OSProcess plugin.

A solution that avoids modifying the VM might be to use a Socket to
connect to port 514 on your local machine, and then talk to rshd (but
I guess that's not quite what you had in mind).

Regards,

Ian

(Jeez, work must be boring today. ;-)





More information about the Squeak-dev mailing list