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 void writefln( string frm, ... ){ 40 string frm2 = substitute( frm, "%s", "{}" ); 41 Stdout( Stdout.layout.convert( _arguments, _argptr, frm2 )).newline; 42 } 43 } 44 else{ 45 import std.string; 46 import std.stdio; 47 } 48 class SpawnWindow : MainWindow 49 { 50 51 TextView viewInput; 52 TextView viewOutput; 53 TextView viewError; 54 TextIter iterOut; 55 TextIter iterError; 56 57 this() 58 { 59 super("Spawn testing"); 60 setupWindow(); 61 setSizeRequest(400,400); 62 showAll(); 63 } 64 65 private void setupWindow() 66 { 67 Box main = new VBox(false, 2); 68 69 viewInput = new TextView(); 70 viewOutput = new TextView(); 71 viewError = new TextView(); 72 73 main.packStart(new ScrolledWindow(viewInput), false, false, 2); 74 Button button = new Button("exec", &execInput); 75 main.packStart(button, false, false, 4); 76 main.packStart(new ScrolledWindow(viewOutput), true, true, 2); 77 main.packStart(new ScrolledWindow(viewError), false, false, 2); 78 79 setBorderWidth(7); 80 add(main); 81 } 82 83 private void execInput(Button button) 84 { 85 version(Tango){ 86 string[] args = split( viewInput.getBuffer().getText(), " " ); 87 } 88 else{ 89 string[] args = std..string.split(viewInput.getBuffer().getText()); 90 } 91 exec(args); 92 } 93 94 private bool exec(string[] args) 95 { 96 foreach ( int i, string arg ; args) 97 { 98 writefln("[%s] >%s<", i, arg); 99 } 100 Spawn spawn = new Spawn(args[0]); 101 //spawn.addChildWatch(&childEnded); 102 //spawn.commandLineSync(&childEnded, &syncOutput, &syncError); 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 //spawn.addParm(null); 112 return exec(spawn); 113 } 114 115 /* 116 void childEnded(int process, int status) 117 { 118 writefln("process %s ended with status %s", process, status); 119 }*/ 120 bool childEnded(Spawn spawn) 121 { 122 123 writefln("process %s ended with status %s", viewInput.getBuffer().getText(),spawn.getExitStatus()); 124 return true; //gotta check this. 125 } 126 127 private bool exec(Spawn spawn) 128 { 129 130 viewOutput.getBuffer().setText(""); 131 viewError.getBuffer().setText(""); 132 133 //int result = spawn.execAsyncWithPipes(); 134 135 //int outCount; 136 //int errCount; 137 138 //TextBuffer bufferOutput = viewOutput.getBuffer(); 139 TextBuffer bufferError = viewError.getBuffer(); 140 //TextIter iterOut = new TextIter(); 141 TextIter iterError = new TextIter(); 142 143 /* Apparently the following code is obsolete 144 * and should not compile. Delegates and commandLineSync should 145 * be used instead. 146 */ 147 /*while ( !spawn.endOfOutput() ) 148 { 149 bufferOutput.getEndIter(iterOut); 150 //viewOutput.getBuffer().insert(iterOut, spawn.readLine()~"\n"); 151 viewOutput.getBuffer().insert(iterOut, spawn.getOutputString()~"\n"); 152 153 } 154 155 while ( !spawn.endOfError() ) 156 { 157 bufferError.getEndIter(iterError); 158 //viewError.getBuffer().insert(iterError, spawn.readLineError()~"\n"); 159 viewError.getBuffer().insert(iterError, spawn.getErrorString()~"\n"); 160 }*/ 161 162 int result = spawn.commandLineSync(&childEnded, &syncOutput, &syncError); 163 164 bufferError.getEndIter(iterError); 165 viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n"); 166 167 writefln("exit loop"); 168 169 spawn.close(); 170 return true; 171 } 172 173 public bool syncOutput(string line) 174 { 175 TextIter iter = new TextIter(); 176 viewOutput.getBuffer().getEndIter(iter); 177 viewOutput.getBuffer().insert(iter, line~"\n"); 178 return true; 179 } 180 181 public bool syncError(string line) 182 { 183 TextIter iter = new TextIter(); 184 viewError.getBuffer().getEndIter(iter); 185 viewError.getBuffer().insert(iter, line~"\n"); 186 return true; 187 } 188 189 public void setInput(string[] args) 190 { 191 TextBuffer inBuffer = viewInput.getBuffer(); 192 string t; 193 foreach ( int count, string arg; args) 194 { 195 if ( count > 0 ) t ~= " "; 196 t ~= arg; 197 } 198 inBuffer.setText(t); 199 } 200 201 public void setInput(string arg) 202 { 203 viewInput.getBuffer().setText(arg); 204 } 205 206 207 } 208 209 void main(string[] args) 210 { 211 Main.init(args); 212 213 SpawnWindow sw = new SpawnWindow(); 214 if ( args.length > 1 ) 215 { 216 sw.setInput(args[1..args.length]); 217 } 218 else 219 { 220 sw.setInput("/bin/ls"); 221 } 222 223 Main.run(); 224 }