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

KDECore

  • 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
30bool 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}
KMacroExpanderBasePrivate::escapechar
QChar escapechar
Definition: kmacroexpander_p.h:31
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
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
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
QStringList
QString
kmacroexpander_p.h
kshell.h
kshell_p.h
KShell::quoteArgInternal
QString quoteArgInternal(const QString &arg, bool _inquote)
Definition: kshell_win.cpp:186
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-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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