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 2.1 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18  
19 module ui.Exec;
20 
21 private import ui.Main;
22 
23 private import gtk.MainWindow;
24 private import gtk.Widget;
25 private import gtk.ProgressBar;
26 private import gtk.Label;
27 private import gtk.VBox;
28 
29 private import glib.Spawn;
30 
31 private import gdk.Event;
32 
33 private import compd.Compiler;
34 private import compd.Executor;
35 
36 private import std.stdio;
37 
38 /**
39  * Executes a command from the compiler
40  */
41 class Exec : Executor
42 {
43 	
44 	int exitStatus;
45 	
46 	char[][] output;
47 	char[][] error;
48 	
49 	void delegate(int) executionEnded;
50 	
51 	public bool execute(char[] command, void delegate(int) executionEnded)
52 	{
53 
54 		this.executionEnded = executionEnded;
55 		
56 		//writefln("\ncwd : ", std.file.getcwd());
57 		//writefln("exec command : %s", command);
58 		
59 		version(Win32)
60 		{
61 			command = std..string.replace(command, "\\", "\\\\");
62 		}
63 		
64 		Spawn spawn = new Spawn(command);
65 		
66 		int result = spawn.commandLineSync(
67 						&childEnd,
68 						&appendOutputLine, 
69 						&appendErrorLine
70 					);
71 		return result != 0;
72 	}
73 	
74 	public int getStatus()
75 	{
76 		return exitStatus;
77 	}
78 	
79 	public char[][] getOutput()
80 	{
81 		return output;
82 	}
83 	
84 	public char[][] getError()
85 	{
86 		return error;
87 	}
88 
89 	bool childEnd(Spawn spawn)
90 	{
91 		exitStatus = spawn.exitStatus;
92 		if ( executionEnded != null )
93 		{
94 			executionEnded(exitStatus);
95 		}
96 
97 		return false;
98 	}
99 
100 	/**
101 	 * Add the line to the output text view and adds the line to the outputQueue
102 	 * Params:
103 	 *    	line = 	The line received from the child output stream
104 	 * Returns: false
105 	 */
106 	bool appendOutputLine(char[] line)
107 	{
108 		//writefln("Output - %s", line);
109 		output ~= line;
110 		return false;
111 	}
112 	
113 	/**
114 	 * Add the line to the output text view and adds the line to the outputQueue
115 	 * Params:
116 	 *    	line = 	The line received from the child error stream
117 	 * Returns: false
118 	 */
119 	bool appendErrorLine(char[] line)
120 	{
121 		//writefln("Error - %s", line);
122 		error ~= line;
123 		return true;
124 	}
125 
126 	
127 }