kmacroexpander.h
/*
This file is part of the KDE libraries
Copyright (c) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef _KMACROEXPANDER_H
#define _KMACROEXPANDER_H
#include <qstringlist.h>
#include <qstring.h>
#include <qmap.h>
#include "kdelibs_export.h"
class KDECORE_EXPORT KMacroExpanderBase {
public:
KMacroExpanderBase( QChar c = '%' );
virtual ~KMacroExpanderBase();
void expandMacros( QString &str );
/*
* Perform safe macro expansion (substitution) on a string for use
* in shell commands.
*
* Explicitly supported shell constructs:
* \ '' "" $'' $"" {} () $(()) ${} $() ``
*
* Implicitly supported shell constructs:
* (())
*
* Unsupported shell constructs that will cause problems:
* @li Shortened "case $v in pat)" syntax. Use "case $v in (pat)" instead.
*
* The rest of the shell (incl. bash) syntax is simply ignored,
* as it is not expected to cause problems.
*
* Note that bash contains a bug which makes macro expansion within
* double quoted substitutions ("${VAR:-%macro}") inherently insecure.
*
* @param str the string in which macros are expanded in-place
* @param pos the position inside the string at which parsing/substitution
* should start, and upon exit where processing stopped
* @return false if the string could not be parsed and therefore no safe
* substitution was possible. Note that macros will have been processed
* up to the point where the error occurred. An unmatched closing paren
* or brace outside any shell construct is @em not an error (unlike in
* the function below), but still prematurely terminates processing.
*/
bool expandMacrosShellQuote( QString &str, uint &pos );
bool expandMacrosShellQuote( QString &str );
void setEscapeChar( QChar c );
QChar escapeChar() const;
protected:
virtual int expandPlainMacro( const QString &str, uint pos, QStringList &ret );
virtual int expandEscapedMacro( const QString &str, uint pos, QStringList &ret );
private:
QChar escapechar;
};
class KDECORE_EXPORT KWordMacroExpander : public KMacroExpanderBase {
public:
KWordMacroExpander( QChar c = '%' ) : KMacroExpanderBase( c ) {}
protected:
virtual int expandPlainMacro( const QString &str, uint pos, QStringList &ret );
virtual int expandEscapedMacro( const QString &str, uint pos, QStringList &ret );
virtual bool expandMacro( const QString &str, QStringList &ret ) = 0;
};
class KDECORE_EXPORT KCharMacroExpander : public KMacroExpanderBase {
public:
KCharMacroExpander( QChar c = '%' ) : KMacroExpanderBase( c ) {}
protected:
virtual int expandPlainMacro( const QString &str, uint pos, QStringList &ret );
virtual int expandEscapedMacro( const QString &str, uint pos, QStringList &ret );
virtual bool expandMacro( QChar chr, QStringList &ret ) = 0;
};
namespace KMacroExpander {
KDECORE_EXPORT QString expandMacros( const QString &str, const QMap<QChar,QString> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacrosShellQuote( const QString &str, const QMap<QChar,QString> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacros( const QString &str, const QMap<QString,QString> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacrosShellQuote( const QString &str, const QMap<QString,QString> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacros( const QString &str, const QMap<QChar,QStringList> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacros( const QString &str, const QMap<QString,QStringList> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacrosShellQuote( const QString &str, const QMap<QChar,QStringList> &map, QChar c = '%' );
KDECORE_EXPORT QString expandMacrosShellQuote( const QString &str, const QMap<QString,QStringList> &map, QChar c = '%' );
}
#endif /* _KMACROEXPANDER_H */
KMacroExpanderBase
Abstract base class for the worker classes behind the KMacroExpander namespace and the KCharMacroExpa...
Definition: kmacroexpander.h:37
KMacroExpander::expandMacros
QString expandMacros(const QString &ostr, const QMap< QChar, QString > &map, QChar c)
Perform safe macro expansion (substitution) on a string.
Definition: kmacroexpander.cpp:520
KWordMacroExpander::KWordMacroExpander
KWordMacroExpander(QChar c= '%')
Constructor.
Definition: kmacroexpander.h:198
KMacroExpanderBase::expandEscapedMacro
virtual int expandEscapedMacro(const QString &str, uint pos, QStringList &ret)
This function is called every time the escape char is found if it is not QChar::null.
Definition: kmacroexpander.cpp:290
KWordMacroExpander
Abstract base class for simple word macro substitutors.
Definition: kmacroexpander.h:191
KMacroExpander::expandMacrosShellQuote
QString expandMacrosShellQuote(const QString &ostr, const QMap< QChar, QString > &map, QChar c)
Perform safe macro expansion (substitution) on a string for use in shell commands.
Definition: kmacroexpander.cpp:521
KCharMacroExpander
Abstract base class for single char macro substitutors.
Definition: kmacroexpander.h:224
KMacroExpanderBase::expandPlainMacro
virtual int expandPlainMacro(const QString &str, uint pos, QStringList &ret)
This function is called for every single char within the string if the escape char is QChar::null...
Definition: kmacroexpander.cpp:287
KCharMacroExpander::KCharMacroExpander
KCharMacroExpander(QChar c= '%')
Constructor.
Definition: kmacroexpander.h:231

KDE Home | KDE Accessibility Home | Description of Access Keys