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 import std.string; 37 import std.stdio; 38 39 class SpawnWindow : MainWindow 40 { 41 42 TextView viewInput; 43 TextView viewOutput; 44 TextView viewError; 45 TextIter iterOut; 46 TextIter iterError; 47 48 this() 49 { 50 super("Spawn testing"); 51 setupWindow(); 52 setSizeRequest(400,400); 53 showAll(); 54 } 55 56 private void setupWindow() 57 { 58 Box main = new VBox(false, 2); 59 60 viewInput = new TextView(); 61 viewOutput = new TextView(); 62 viewError = new TextView(); 63 64 main.packStart(new ScrolledWindow(viewInput), false, false, 2); 65 Button button = new Button("exec", &execInput); 66 main.packStart(button, false, false, 4); 67 main.packStart(new ScrolledWindow(viewOutput), true, true, 2); 68 main.packStart(new ScrolledWindow(viewError), false, false, 2); 69 70 setBorderWidth(7); 71 add(main); 72 } 73 74 private void execInput(Button button) 75 { 76 string[] args = std..string.split(viewInput.getBuffer().getText()); 77 exec(args); 78 } 79 80 private bool exec(string[] args) 81 { 82 foreach ( int i, string arg ; args) 83 { 84 writefln("[%s] >%s<", i, arg); 85 } 86 Spawn spawn = new Spawn(args[0]); 87 88 if (args.length > 1 ) 89 { 90 for( int i=1 ; i<args.length ; i++ ) 91 { 92 writefln("SpawnTests.exec adding parameter [%s] %s",i,args[i]); 93 spawn.addParm(args[i]); 94 } 95 } 96 return exec(spawn); 97 } 98 99 bool childEnded(Spawn spawn) 100 { 101 writefln("process %s ended with status %s", viewInput.getBuffer().getText(),spawn.getExitStatus()); 102 return true; //gotta check this. 103 } 104 105 private bool exec(Spawn spawn) 106 { 107 //TODO: These functions should accept a string. 108 viewOutput.getBuffer().setText(""); 109 viewError.getBuffer().setText(""); 110 111 TextBuffer bufferError = viewError.getBuffer(); 112 TextIter iterError = new TextIter(); 113 114 int result = spawn.commandLineSync(&childEnded, &syncOutput, &syncError); 115 116 bufferError.getEndIter(iterError); 117 viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n"); 118 119 writefln("exit loop"); 120 121 spawn.close(); 122 return true; 123 } 124 125 public bool syncOutput(string line) 126 { 127 TextIter iter = new TextIter(); 128 viewOutput.getBuffer().getEndIter(iter); 129 viewOutput.getBuffer().insert(iter, line~"\n"); 130 return true; 131 } 132 133 public bool syncError(string line) 134 { 135 TextIter iter = new TextIter(); 136 viewError.getBuffer().getEndIter(iter); 137 viewError.getBuffer().insert(iter, line~"\n"); 138 return true; 139 } 140 141 public void setInput(string[] args) 142 { 143 TextBuffer inBuffer = viewInput.getBuffer(); 144 string t; 145 foreach ( int count, string arg; args) 146 { 147 if ( count > 0 ) t ~= " "; 148 t ~= arg; 149 } 150 inBuffer.setText(t); 151 } 152 153 public void setInput(string arg) 154 { 155 viewInput.getBuffer().setText(arg); 156 } 157 } 158 159 void main(string[] args) 160 { 161 Main.init(args); 162 163 SpawnWindow sw = new SpawnWindow(); 164 if ( args.length > 1 ) 165 { 166 sw.setInput(args[1..args.length]); 167 } 168 else 169 { 170 sw.setInput("/bin/ls"); 171 } 172 173 Main.run(); 174 }