commandline.cpp
1 //------------------------------------------------------------------------------
2 // commandline.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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.hpp"
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_DARWIN
75  QFileInfo binary = arg;
76  if (binary.isBundle())
77  arg += AppRunner::findBundleBinary(binary);
78  #endif
79  return escapeArg(arg);
80 }