示例#1
0
 /*--- constructors ----------------------------------------------------*/
 protected socket()
 {
     input= bigloo.foreign.BFALSE;
       output= bigloo.foreign.BFALSE;
 }
示例#2
0
 protected void set_socket_io_ports( Socket _socket, byte[] inbuf, byte[] outbuf )
 {
     input= new input_socket_port( _socket, inbuf );
        // CARE must be buffered !?
        output= new output_socket_port( _socket );
 }
示例#3
0
        /*--- instance constructor --------------------------------------------*/
        public process( byte[]  host,
                    bool    fork,
                    bool    waiting, 
                    Object  binput,
                    Object  boutput,
                    Object  berror,
                    byte[]  bcommand,
                    Object  bargs,
                    Object  benv )
        {
            StringBuilder          cmd= new StringBuilder();
              ProcessStartInfo       process_start_info= new ProcessStartInfo();

              // we purge the process table and ask for a new index
              PURGE_PROCESS_TABLE();
              index= find_process();
              if (index < 0)
            foreign.fail( foreign.getbytes( "run-process" ),
                      foreign.getbytes( "too many processes" ),
                      unspecified._unspecified );

              // converting "null:" keywords to null file names
              if (   (boutput is keyword)
              && (foreign.KEYWORD_TO_STRING( (keyword)boutput ).ToString().Equals( "null:" )))
            boutput= foreign.getbytes( foreign.bigloo_strcmp( os.OS_CLASS, foreign.getbytes( "unix" ) )
                                   ? "/dev/null"
                                   : "NUL:" );
              if (   (berror is keyword)
              && (foreign.KEYWORD_TO_STRING( (keyword)berror ).ToString().Equals( "null:" )))
            berror= foreign.getbytes( foreign.bigloo_strcmp( os.OS_CLASS, foreign.getbytes( "unix" ) )
                                  ? "/dev/null"
                                  : "NUL:" );

              // sh or rsh ?
              if (host != null)
              {
            process_start_info.FileName= "rsh";
            cmd.Append( foreign.newstring( host ) );
            cmd.Append( " " );
              }
              else
              {
            String               comspec= Environment.GetEnvironmentVariable( "COMSPEC" );

            if (comspec != null)
            {
              process_start_info.FileName= comspec;
              cmd.Append( "/C " );
            }
            else
            {
              process_start_info.FileName= "sh";
              cmd.Append( "-c " );
            }
              }

              // command-line and arguments
              cmd.Append( foreign.newstring( bcommand ) );
              while (bargs is pair)
              {
            cmd.Append( " \"" );
            cmd.Append( foreign.newstring( (byte[])((pair)bargs).car ) );
            cmd.Append( "\"" );
            bargs= ((pair)bargs).cdr;
              }

              // the re-directions
              if (binput is keyword)
            process_start_info.RedirectStandardInput= true;
              else
              {
            input_port= bigloo.foreign.BFALSE;
            if (binput is byte[])
            {
              cmd.Append( " < " );
              cmd.Append( foreign.newstring( (byte[])binput ) );
            }
              }

              if (   (boutput is keyword)
              || (boutput == bigloo.foreign.BUNSPEC))
            process_start_info.RedirectStandardOutput= true;
              else
              {
            output_port= bigloo.foreign.BFALSE;
            if (boutput is byte[])
            {
              cmd.Append( " > " );
              cmd.Append( foreign.newstring( (byte[])boutput ) );
            }
              }

              if (   (berror is keyword)
              || (berror == bigloo.foreign.BUNSPEC))
            process_start_info.RedirectStandardError= true;
              else
              {
            error_port= bigloo.foreign.BFALSE;
            if (berror is byte[])
            {
              cmd.Append( " 2> " );
              cmd.Append( foreign.newstring( (byte[])berror ) );
            }
              }

              process_start_info.Arguments= cmd.ToString();

              if (!fork)
            Console.Error.WriteLine( "***WARNING: Can't run process without forking with the .NET back-end" );

              // Construct the process environment variable list
              while (benv is pair)
              {
            String               current_env_association= foreign.newstring( (byte[])((pair)benv).car );
            String[]             splitted_env= current_env_association.Split( '=' );

            if (splitted_env.Length != 2)
              Console.Error.WriteLine( "***WARNING: Can't split environment string [{0})", current_env_association );
            else
              process_start_info.EnvironmentVariables.Add( splitted_env[0], splitted_env[1] );

            benv= ((pair)benv).cdr;
              }

              // we create the system process
              process_start_info.CreateNoWindow= true;
              process_start_info.UseShellExecute= false;
              _process= Process.Start( process_start_info );

              // the re-directions
              if (binput is keyword)
            // !!!!! should check that direct access to the BaseStream does not mess everything !!!!!
            input_port= new output_stream_port( _process.StandardInput.BaseStream, bcommand );

              if (boutput is keyword)
            // !!!!! should check that direct access to the BaseStream does not mess everything !!!!!
            output_port= new input_pipe_port( _process.StandardOutput.BaseStream, bcommand );

              if (berror is keyword)
            // !!!!! should check that direct access to the BaseStream does not mess everything !!!!!
            error_port= new input_pipe_port( _process.StandardError.BaseStream, bcommand );

              // and we store the .NET object into the process table
              processes[index]= this;

              // if the output ports are not redirected to files or pipes, we
              // have to flush the input stream
              if (   (boutput == bigloo.foreign.BUNSPEC)
              || (berror == bigloo.foreign.BUNSPEC))
              {
            flushers= new ArrayList();
            if (boutput == bigloo.foreign.BUNSPEC)
              flushers.Add( new flusher( _process.StandardOutput, Console.Out ) );
            if (berror == bigloo.foreign.BUNSPEC)
              flushers.Add( new flusher( _process.StandardError, Console.Error ) );
              }

              // if requested, we wait for the process completion
              if (waiting)
            try
            {
              waitfor();
            }
            catch (Exception)
            {
            }
        }