view tango/example/system/process.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children
line wrap: on
line source

/*******************************************************************************
  copyright:   Copyright (c) 2006 Juan Jose Comellas. All rights reserved
  license:     BSD style: $(LICENSE)
  author:      Juan Jose Comellas <juanjo@comellas.com.ar>
*******************************************************************************/

private import tango.io.Stdout;
private import tango.sys.Process;
private import tango.core.Exception;

private import tango.text.stream.LineIterator;


/**
 * Example program for the tango.sys.Process class.
 */
void main()
{
    version (Windows)
        char[] command = "ping -n 4 localhost";
    else version (Posix)
        char[] command = "ping -c 4 localhost";
    else
        assert(false, "Unsupported platform");

    try
    {
        auto p = new Process(command, null);

        Stdout.formatln("Executing {0}", p.toString());
        p.execute();

        Stdout.formatln("Output from process: {0} (pid {1})\n---",
                        p.programName, p.pid);

        foreach (line; new LineIterator!(char)(p.stdout))
        {
            Stdout.formatln("{0}", line);
        }

        Stdout.print("---\n");

        auto result = p.wait();

        Stdout.formatln("Process '{0}' ({1}) finished: {2}",
                        p.programName, p.pid, result.toString());
    }
    catch (ProcessException e)
    {
        Stdout.formatln("Process execution failed: {0}", e.toString());
    }
    catch (IOException e)
    {
        Stdout.formatln("Input/output exception caught: {0}", e.toString());
    }
    catch (Exception e)
    {
        Stdout.formatln("Unexpected exception caught: {0}", e.toString());
    }
}