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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
kmacroexpander_win.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE libraries
3 
4  Copyright (c) 2008 Oswald Buddenhagen <ossi@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kmacroexpander_p.h"
23 #include "kshell_p.h"
24 
25 #include "kshell.h"
26 
27 #include <QString>
28 #include <QStringList>
29 
30 bool KMacroExpanderBase::expandMacrosShellQuote( QString &str, int &pos )
31 {
32  int len;
33  int pos2;
34  ushort uc;
35  ushort ec = d->escapechar.unicode();
36  bool shellQuote = false; // shell is in quoted state
37  bool crtQuote = false; // c runtime is in quoted state
38  bool escaped = false; // previous char was a circumflex
39  int bslashes = 0; // previous chars were backslashes
40  int parens = 0; // parentheses nesting level
41  QStringList rst;
42  QString rsts;
43 
44  while (pos < str.length()) {
45  ushort cc = str.unicode()[pos].unicode();
46  if (escaped) // prevent anomalies due to expansion
47  goto notcf;
48  if (ec != 0) {
49  if (cc != ec)
50  goto nohit;
51  if (!(len = expandEscapedMacro( str, pos, rst )))
52  goto nohit;
53  } else {
54  if (!(len = expandPlainMacro( str, pos, rst )))
55  goto nohit;
56  }
57  if (len < 0) {
58  pos -= len;
59  continue;
60  }
61  if (shellQuote != crtQuote) // Silly, isn't it? Ahoy to Redmond.
62  return false;
63  if (shellQuote) {
64  rsts = KShell::quoteArgInternal( rst.join( QLatin1String(" ") ), true );
65  } else {
66  if (rst.isEmpty()) {
67  str.remove( pos, len );
68  continue;
69  }
70  rsts = KShell::joinArgs( rst );
71  }
72  pos2 = 0;
73  while (pos2 < rsts.length() &&
74  ((uc = rsts.unicode()[pos2].unicode()) == '\\' || uc == '^'))
75  pos2++;
76  if (pos2 < rsts.length() && rsts.unicode()[pos2].unicode() == '"') {
77  QString bsl;
78  bsl.reserve( bslashes );
79  for (; bslashes; bslashes--)
80  bsl.append( QLatin1String("\\") );
81  rsts.prepend( bsl );
82  }
83  bslashes = 0;
84  rst.clear();
85  str.replace( pos, len, rsts );
86  pos += rsts.length();
87  continue;
88  nohit:
89  if (!escaped && !shellQuote && cc == '^') {
90  escaped = true;
91  } else {
92  notcf:
93  if (cc == '\\') {
94  bslashes++;
95  } else {
96  if (cc == '"') {
97  if (!escaped)
98  shellQuote = !shellQuote;
99  if (!(bslashes & 1))
100  crtQuote = !crtQuote;
101  } else if (!shellQuote) {
102  if (cc == '(')
103  parens++;
104  else if (cc == ')')
105  if (--parens < 0)
106  break;
107  }
108  bslashes = 0;
109  }
110  escaped = false;
111  }
112  pos++;
113  }
114  return true;
115 }
QList::clear
void clear()
kshell_p.h
QString::append
QString & append(QChar ch)
KMacroExpanderBase::expandMacrosShellQuote
bool expandMacrosShellQuote(QString &str, int &pos)
Perform safe macro expansion (substitution) on a string for use in shell commands.
Definition: kmacroexpander_unix.cpp:48
QString::prepend
QString & prepend(QChar ch)
kshell.h
QStringList::join
QString join(const QString &separator) const
QString::remove
QString & remove(int position, int n)
KMacroExpanderBase::expandEscapedMacro
virtual int expandEscapedMacro(const QString &str, int pos, QStringList &ret)
This function is called every time the escape char is found if it is not QChar::null.
Definition: kmacroexpander.cpp:91
QList::isEmpty
bool isEmpty() const
KMacroExpanderBase::expandPlainMacro
virtual int expandPlainMacro(const QString &str, int pos, QStringList &ret)
This function is called for every single char within the string if the escape char is QChar::null...
Definition: kmacroexpander.cpp:88
KMacroExpanderBasePrivate::escapechar
QChar escapechar
Definition: kmacroexpander_p.h:31
QString
QChar::unicode
ushort unicode() const
QStringList
QString::replace
QString & replace(int position, int n, QChar after)
QString::unicode
const QChar * unicode() const
QLatin1String
kmacroexpander_p.h
QString::length
int length() const
KShell::quoteArgInternal
QString quoteArgInternal(const QString &arg, bool _inquote)
Definition: kshell_win.cpp:186
QString::reserve
void reserve(int size)
KShell::joinArgs
QString joinArgs(const QStringList &args)
Quotes and joins args together according to system shell rules.
Definition: kshell.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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