1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 module gtk.SpawnTests;
20 
21 private import glib.Spawn;
22 private import gtk.Main;
23 
24 private import gtk.TextView;
25 private import gtk.TextBuffer;
26 private import gtk.TextIter;
27 private import gtk.Box;
28 private import gtk.VBox;
29 private import gtk.ScrolledWindow;
30 
31 private import gtk.MainWindow;
32 
33 private import gtk.Button;
34 private import gtk.Image;
35 
36 version(Tango){
37     import tango.text.Util;
38     import tango.io.Stdout;
39 	import tango.core.Vararg;
40 
41     void writefln( string frm, ... ){
42         string frm2 = substitute( frm, "%s", "{}" );
43         Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 )).newline;
44     }
45 }
46 else{
47     import std.string;
48     import std.stdio;
49 }
50 class SpawnWindow : MainWindow
51 {
52 
53 	TextView viewInput;
54 	TextView viewOutput;
55 	TextView viewError;
56 	TextIter iterOut;
57 	TextIter iterError;
58 
59 	this()
60 	{
61 		super("Spawn testing");
62 		setupWindow();
63 		setSizeRequest(400,400);
64 		showAll();
65 	}
66 
67 	private void setupWindow()
68 	{
69 		Box main = new VBox(false, 2);
70 
71 		viewInput = new TextView();
72 		viewOutput = new TextView();
73 		viewError = new TextView();
74 
75 		main.packStart(new ScrolledWindow(viewInput), false, false, 2);
76 		Button button = new Button("exec", &execInput);
77 		main.packStart(button, false, false, 4);
78 		main.packStart(new ScrolledWindow(viewOutput), true, true, 2);
79 		main.packStart(new ScrolledWindow(viewError), false, false, 2);
80 
81 		setBorderWidth(7);
82 		add(main);
83 	}
84 
85 	private void execInput(Button button)
86 	{
87 		version(Tango)
88 			string[] args = split( viewInput.getBuffer().getText(), " " );
89         else
90 			string[] args = std..string.split(viewInput.getBuffer().getText());
91 
92 		exec(args);
93 	}
94 
95 	private bool exec(string[] args)
96 	{
97 		foreach ( int i, string arg ; args)
98 		{
99 			writefln("[%s] >%s<", i, arg);
100 		}
101 		Spawn spawn = new Spawn(args[0]);
102 
103 		if (args.length > 1 )
104 		{
105 			for( int i=1 ; i<args.length ; i++ )
106 			{
107 				writefln("SpawnTests.exec adding parameter [%s] %s",i,args[i]);
108 				spawn.addParm(args[i]);
109 			}
110 		}
111 		return exec(spawn);
112 	}
113 
114 	bool childEnded(Spawn spawn)
115 	{
116 		writefln("process %s ended with status %s", viewInput.getBuffer().getText(),spawn.getExitStatus());
117 		return true; //gotta check this.
118 	}
119 
120 	private bool exec(Spawn spawn)
121 	{
122 		//TODO: These functions should accept a string.
123 		viewOutput.getBuffer().setText("");
124 		viewError.getBuffer().setText("");
125 
126 		TextBuffer bufferError = viewError.getBuffer();
127 		TextIter iterError = new TextIter();
128 
129         int result = spawn.commandLineSync(&childEnded, &syncOutput, &syncError);
130 
131 		bufferError.getEndIter(iterError);
132 		viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n");
133 
134 		writefln("exit loop");
135 
136 		spawn.close();
137 		return true;
138 	}
139 
140 	public bool syncOutput(string line)
141 	{
142 		TextIter iter = new TextIter();
143 		viewOutput.getBuffer().getEndIter(iter);
144 		viewOutput.getBuffer().insert(iter, line~"\n");
145 		return true;
146 	}
147 
148 	public bool syncError(string line)
149 	{
150 		TextIter iter = new TextIter();
151 		viewError.getBuffer().getEndIter(iter);
152 		viewError.getBuffer().insert(iter, line~"\n");
153 		return true;
154 	}
155 
156 	public void setInput(string[] args)
157 	{
158 		TextBuffer inBuffer = viewInput.getBuffer();
159 		string t;
160 		foreach ( int count, string arg; args)
161 		{
162 			if ( count > 0 ) t ~= " ";
163 			t ~= arg;
164 		}
165 		inBuffer.setText(t);
166 	}
167 
168 	public void setInput(string arg)
169 	{
170 		viewInput.getBuffer().setText(arg);
171 	}
172 }
173 
174 void main(string[] args)
175 {
176 	Main.init(args);
177 
178 	SpawnWindow sw = new SpawnWindow();
179 	if ( args.length > 1 )
180 	{
181 		sw.setInput(args[1..args.length]);
182 	}
183 	else
184 	{
185 		sw.setInput("/bin/ls");
186 	}
187 
188 	Main.run();
189 }