• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ShellCommand.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301 USA.
18 */
19 
20 // Own
21 #include "ShellCommand.h"
22 
23 // KDE
24 #include <KShell>
25 
26 using Konsole::ShellCommand;
27 
28 ShellCommand::ShellCommand(const QString& aCommand)
29 {
30  _arguments = KShell::splitArgs(aCommand);
31 }
32 ShellCommand::ShellCommand(const QString& aCommand, const QStringList& aArguments)
33 {
34  _arguments = aArguments;
35 
36  if (!_arguments.isEmpty())
37  _arguments[0] = aCommand;
38 }
39 QString ShellCommand::fullCommand() const
40 {
41  QStringList quotedArgs(_arguments);
42  for (int i = 0; i < quotedArgs.count(); i++) {
43  QString arg = quotedArgs.at(i);
44  bool hasSpace = false;
45  for (int j = 0; j < arg.count(); j++)
46  if (arg[j].isSpace())
47  hasSpace = true;
48  if (hasSpace)
49  quotedArgs[i] = '\"' + arg + '\"';
50  }
51  return quotedArgs.join(QChar(' '));
52 }
53 QString ShellCommand::command() const
54 {
55  if (!_arguments.isEmpty())
56  return _arguments[0];
57  else
58  return QString();
59 }
60 QStringList ShellCommand::arguments() const
61 {
62  return _arguments;
63 }
64 QStringList ShellCommand::expand(const QStringList& items)
65 {
66  QStringList result;
67 
68  foreach(const QString & item , items) {
69  result << expand(item);
70  }
71 
72  return result;
73 }
74 QString ShellCommand::expand(const QString& text)
75 {
76  QString result = text;
77  expandEnv(result);
78  return result;
79 }
80 
81 bool ShellCommand::isValidEnvCharacter(const QChar& ch)
82 {
83  const ushort code = ch.unicode();
84  return isValidLeadingEnvCharacter(ch) || ('0' <= code && code <= '9');
85 }
86 
87 bool ShellCommand::isValidLeadingEnvCharacter(const QChar& ch)
88 {
89  const ushort code = ch.unicode();
90  return (code == '_') || ('A' <= code && code <= 'Z');
91 }
92 
93 /*
94  * expandEnv
95  *
96  * Expand environment variables in text. Escaped '$' characters are ignored.
97  * Return true if any variables were expanded
98  */
99 bool ShellCommand::expandEnv(QString& text)
100 {
101  const QLatin1Char dollarChar('$');
102  const QLatin1Char backslashChar('\\');
103 
104  int dollarPos = 0;
105  bool expanded = false;
106 
107  // find and expand all environment variables beginning with '$'
108  while ((dollarPos = text.indexOf(dollarChar, dollarPos)) != -1) {
109  // if '$' is the last character, there is no way of expanding
110  if (dollarPos == text.length() - 1) {
111  break;
112  }
113 
114  // skip escaped '$'
115  if (dollarPos > 0 && text.at(dollarPos - 1) == backslashChar) {
116  dollarPos++;
117  continue;
118  }
119 
120  // if '$' is followed by an invalid leading character, skip this '$'
121  if (!isValidLeadingEnvCharacter(text.at(dollarPos + 1))) {
122  dollarPos++;
123  continue;
124  }
125 
126  int endPos = dollarPos + 1;
127  Q_ASSERT(endPos < text.length());
128  while (endPos < text.length() && isValidEnvCharacter(text.at(endPos))) {
129  endPos++;
130  }
131 
132  const int len = endPos - dollarPos;
133  const QString key = text.mid(dollarPos + 1, len - 1);
134  const QString value = QString::fromLocal8Bit(qgetenv(key.toLocal8Bit()));
135 
136  if (!value.isEmpty()) {
137  text.replace(dollarPos, len, value);
138  expanded = true;
139  dollarPos = dollarPos + value.length();
140  } else {
141  dollarPos = endPos;
142  }
143  }
144 
145  return expanded;
146 }
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
Konsole::ShellCommand::command
QString command() const
Returns the command.
Definition: ShellCommand.cpp:53
Konsole::ShellCommand::expand
static QString expand(const QString &text)
Expands environment variables in text .
Definition: ShellCommand.cpp:74
QChar
Konsole::ShellCommand::arguments
QStringList arguments() const
Returns the arguments.
Definition: ShellCommand.cpp:60
QList::at
const T & at(int i) const
QStringList::join
QString join(const QString &separator) const
Konsole::ShellCommand::isValidEnvCharacter
static bool isValidEnvCharacter(const QChar &ch)
Definition: ShellCommand.cpp:81
ShellCommand.h
Konsole::ShellCommand
A class to parse and extract information about shell commands.
Definition: ShellCommand.h:52
QList::count
int count(const T &value) const
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
Konsole::ShellCommand::ShellCommand
ShellCommand(const QString &aCommand)
Constructs a ShellCommand from a command line.
Definition: ShellCommand.cpp:28
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
Konsole::ShellCommand::fullCommand
QString fullCommand() const
Returns the full command line.
Definition: ShellCommand.cpp:39
QString
QChar::unicode
ushort unicode() const
QStringList
QString::toLocal8Bit
QByteArray toLocal8Bit() const
QLatin1Char
Konsole::ShellCommand::isValidLeadingEnvCharacter
static bool isValidLeadingEnvCharacter(const QChar &ch)
Definition: ShellCommand.cpp:87
QString::replace
QString & replace(int position, int n, QChar after)
QString::mid
QString mid(int position, int n) const
QString::count
int count() const
QString::at
const QChar at(int position) const
QString::length
int length() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal