commandline.cpp
1 //------------------------------------------------------------------------------
2 // commandline.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "commandline.h"
24 
25 #include "apprunner.h"
26 #include "strings.h"
27 #include <QRegExp>
28 
29 void CommandLine::escapeArgs(QStringList& args)
30 {
31  QStringList::iterator it;
32  for (it = args.begin(); it != args.end(); ++it)
33  {
34  QString& str = *it;
35  escapeArg(str);
36  }
37 }
38 
39 static bool needsQuoteWrap(const QString &arg)
40 {
41  if (arg.isEmpty())
42  return true;
43  QRegExp reallySafestCharsIHope = QRegExp("[^a-z0-9/\\_-+]", Qt::CaseInsensitive);
44  return arg.contains(reallySafestCharsIHope);
45 }
46 
47 #if defined Q_OS_WIN
48 void CommandLine::escapeArg(QString& arg)
49 {
50  // Note: this may be game specific (oh, dear...)
51  arg.replace('"', "\\\"");
52  if (needsQuoteWrap(arg))
53  {
54  arg.prepend('"');
55  arg += '"';
56  }
57 }
58 
59 #else
60 // Since most other operating systems are Unix like we might as well make this a default.
61 void CommandLine::escapeArg(QString& arg)
62 {
63  arg.replace('\'', "'\\''"); // This does: ' -> '\''
64  if (needsQuoteWrap(arg))
65  {
66  arg.prepend('\'');
67  arg += '\'';
68  }
69 }
70 #endif
71 
73 {
74 #ifdef Q_OS_MAC
75  QFileInfo binary = arg;
76  if(binary.isBundle())
77  arg += AppRunner::findBundleBinary(binary);
78 #endif
79  return escapeArg(arg);
80 }
static void escapeArg(QString &arg)
Escapes all characters in the passed string.
Definition: commandline.cpp:61
static void escapeExecutable(QString &arg)
Escapes the executable path and handles OS X bundles.
Definition: commandline.cpp:72
static void escapeArgs(QStringList &args)
Escapes all characters in all strings on the list.
Definition: commandline.cpp:29