File: CommandLine.cpp

package info (click to toggle)
pinot 0.85-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,524 kB
  • ctags: 3,868
  • sloc: cpp: 33,107; sh: 8,801; ansic: 3,049; makefile: 557; xml: 366; python: 250
file content (275 lines) | stat: -rw-r--r-- 6,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 *  Copyright 2005,2006 Fabrice Colin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <strings.h>
#include <glibmm/shell.h>
#include <glibmm/spawn.h>
#include <iostream>

#include "CommandLine.h"
#include "Url.h"

using std::cout;
using std::endl;
using std::string;
using std::vector;

CommandLine::CommandLine()
{
}

CommandLine::~CommandLine()
{
}

/// Quotes a string so that the shell will interpret the quoted string to mean str.
string CommandLine::quote(const string &str)
{
	return Glib::shell_quote(str);
}

/// Runs a command synchronously.
bool CommandLine::runSync(const string &commandLine, string &output)
{
	int exitStatus = 0;

	if (commandLine.empty() == true)
	{
		return false;
	}

	Glib::spawn_command_line_sync(commandLine, &output, NULL, &exitStatus);
	if (exitStatus == 0)
	{
		return true;
	}
#ifdef DEBUG
	cout << "CommandLine::runSync: exit status is " << exitStatus
		<< " for \"" << commandLine << "\"" << endl;
#endif

	return false;
}

/// Runs a command asynchronously.
bool CommandLine::runAsync(const MIMEAction &action, const vector<string> &arguments)
{
	string commandLine(action.m_exec);

	if (action.m_exec.empty() == true)
	{
		return false;
	}

	string::size_type equalPos = action.m_exec.find("=");
	if (equalPos != string::npos)
	{
		// The .desktop spec says that the program name or path in Exec may not contain it
		// but that's ignored by some .desktop files. So it could be either of :
		//  FOO=bar my program				- not valid
		//  sh -c "FOO=bar exec myprogram"		- valid
		//  myprogram --option FOO=bar			- valid
		// There's no easy way to find out which, so be lenient and use system()
		commandLine = "sh -c \"";
		commandLine += action.m_exec;
		commandLine += "\"";
	}

	if (commandLine.empty() == true)
	{
		return false;
	}
#ifdef DEBUG
	cout << "CommandLine::runAsync: " << arguments.size() << " arguments for application '"
		<< action.m_exec << "'" << endl;
#endif

	// We may have to spawn several copies of the same program if it doesn't support multiple arguments
	vector<string>::const_iterator firstArg = arguments.begin();
	while (firstArg != arguments.end())
	{
		Url firstUrl(*firstArg);
		bool foundParam = false;
		bool noArgument = true;
		bool oneArgumentOnly = true;

		// Expand parameters
		// See http://standards.freedesktop.org/desktop-entry-spec/latest/
		// We assume that all arguments are full-blown URLs
		string::size_type paramPos = commandLine.find('%');
		while ((paramPos != string::npos) &&
				(paramPos + 1 < commandLine.length()))
		{
			string shellReplacement, replacement;

			foundParam = true;
			switch (commandLine[paramPos + 1])
			{
				// Single parameter arguments
				case 'u':
					replacement = *firstArg;
					noArgument = false;
					break;
				case 'f':
					if (firstUrl.getProtocol() == "file")
					{
						replacement = firstUrl.getLocation();
						replacement += "/";
						replacement += firstUrl.getFile();
					}
					noArgument = false;
					break;
				case 'd':
					if (firstUrl.getProtocol() == "file")
					{
						replacement = firstUrl.getLocation();
					}
					noArgument = false;
					break;
				case 'n':
					if (firstUrl.getProtocol() == "file")
					{
						replacement = firstUrl.getFile();
					}
					noArgument = false;
					break;
				// Multiple parameters arguments
				case 'U':
					for (vector<string>::const_iterator argIter = firstArg; argIter != arguments.end(); ++argIter)
					{
						if (replacement.empty() == false)
						{
							replacement += " ";
						}
						replacement += *argIter;
					}
					noArgument = oneArgumentOnly = false;
					break;
				case 'F':
					for (vector<string>::const_iterator argIter = firstArg; argIter != arguments.end(); ++argIter)
					{
						Url argUrl(*argIter);

						if (argUrl.isLocal() == true)
						{
							if (replacement.empty() == false)
							{
								replacement += " ";
							}
							replacement += argUrl.getLocation();
							replacement += "/";
							replacement += argUrl.getFile();
						}
					}
					noArgument = oneArgumentOnly = false;
					break;
				case 'D':
					for (vector<string>::const_iterator argIter = firstArg; argIter != arguments.end(); ++argIter)
					{
						Url argUrl(*argIter);

						if (argUrl.isLocal() == true)
						{
							if (replacement.empty() == false)
							{
								replacement += " ";
							}
							replacement += argUrl.getLocation();
						}
					}
					noArgument = oneArgumentOnly = false;
					break;
				case 'N':
					for (vector<string>::const_iterator argIter = firstArg; argIter != arguments.end(); ++argIter)
					{
						Url argUrl(*argIter);

						if (argUrl.isLocal() == true)
						{
							if (replacement.empty() == false)
							{
								replacement += " ";
							}
							replacement += argUrl.getFile();
						}
					}
					noArgument = oneArgumentOnly = false;
					break;
				// Other parameters
				case 'i':
					replacement = action.m_icon;
					break;
				case 'c':
					// FIXME: this should be the "translated name"
					replacement = action.m_name;
					break;
				case 'k':
					replacement = action.m_location;
					break;
				case 'v':
					replacement = action.m_device;
					break;
				default:
					break;
			}

			if (replacement.empty() == false)
			{
				shellReplacement = Glib::shell_quote(replacement);
			}
			commandLine.replace(paramPos, 2, shellReplacement);

			// Next
			paramPos = commandLine.find('%', paramPos + shellReplacement.length());
		}

		if (foundParam == false)
		{
			// If no parameter was found, assume %f
			if (firstUrl.getProtocol() == "file")
			{
				commandLine += " ";
				commandLine += firstUrl.getLocation();
				commandLine += "/";
				commandLine += firstUrl.getFile();
			}
		}

#ifdef DEBUG
		cout << "CommandLine::runAsync: spawning '" << commandLine << "'" << endl;
#endif
		Glib::spawn_command_line_async(commandLine);

		if ((noArgument == true) ||
			(oneArgumentOnly == false))
		{
			// No or all arguments were processed
			break;
		}
		else
		{
			// Only the first argument was processed
			commandLine = action.m_exec;
			++firstArg;
		}

	}

	return true;
}