KDECore
kmacroexpander_win.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kmacroexpander_p.h"
00023 #include "kshell_p.h"
00024
00025 #include "kshell.h"
00026
00027 #include <QString>
00028 #include <QStringList>
00029
00030 bool KMacroExpanderBase::expandMacrosShellQuote( QString &str, int &pos )
00031 {
00032 int len;
00033 int pos2;
00034 ushort uc;
00035 QChar ec( d->escapechar );
00036 bool shellQuote = false;
00037 bool crtQuote = false;
00038 bool escaped = false;
00039 int bslashes = 0;
00040 int parens = 0;
00041 QStringList rst;
00042 QString rsts;
00043
00044 while (pos < str.length()) {
00045 QChar cc( str.unicode()[pos] );
00046 if (escaped)
00047 goto notcf;
00048 if (ec != QLatin1Char(0)) {
00049 if (cc != ec)
00050 goto nohit;
00051 if (!(len = expandEscapedMacro( str, pos, rst )))
00052 goto nohit;
00053 } else {
00054 if (!(len = expandPlainMacro( str, pos, rst )))
00055 goto nohit;
00056 }
00057 if (len < 0) {
00058 pos -= len;
00059 continue;
00060 }
00061 if (shellQuote != crtQuote)
00062 return false;
00063 if (shellQuote) {
00064 rsts = KShell::quoteArgInternal( rst.join( QLatin1String(" ") ), true );
00065 } else {
00066 if (rst.isEmpty()) {
00067 str.remove( pos, len );
00068 continue;
00069 }
00070 rsts = KShell::joinArgs( rst );
00071 }
00072 pos2 = 0;
00073 while (pos2 < rsts.length() &&
00074 ((uc = rsts[pos2].unicode()) == '\\' || uc == '^'))
00075 pos2++;
00076 if (pos2 < rsts.length() && rsts[pos2].unicode() == '"') {
00077 QString bsl;
00078 bsl.reserve( bslashes );
00079 for (; bslashes; bslashes--)
00080 bsl.append( QLatin1String("\\") );
00081 rsts.prepend( bsl );
00082 }
00083 bslashes = 0;
00084 rst.clear();
00085 str.replace( pos, len, rsts );
00086 pos += rsts.length();
00087 continue;
00088 nohit:
00089 if (!escaped && !shellQuote && cc == QLatin1Char('^')) {
00090 escaped = true;
00091 } else {
00092 notcf:
00093 if (cc == QLatin1Char('\\')) {
00094 bslashes++;
00095 } else {
00096 if (cc == QLatin1Char('"')) {
00097 if (!escaped)
00098 shellQuote = !shellQuote;
00099 if (!(bslashes & 1))
00100 crtQuote = !crtQuote;
00101 } else if (!shellQuote) {
00102 if (cc == QLatin1Char('('))
00103 parens++;
00104 else if (cc == QLatin1Char(')'))
00105 if (--parens < 0)
00106 break;
00107 }
00108 bslashes = 0;
00109 }
00110 escaped = false;
00111 }
00112 pos++;
00113 }
00114 return true;
00115 }