KDECore
kmacroexpander_win.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 00004 Copyright (c) 2008 Oswald Buddenhagen <ossi@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 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 ushort ec = d->escapechar.unicode(); 00036 bool shellQuote = false; // shell is in quoted state 00037 bool crtQuote = false; // c runtime is in quoted state 00038 bool escaped = false; // previous char was a circumflex 00039 int bslashes = 0; // previous chars were backslashes 00040 int parens = 0; // parentheses nesting level 00041 QStringList rst; 00042 QString rsts; 00043 00044 while (pos < str.length()) { 00045 ushort cc = str.unicode()[pos].unicode(); 00046 if (escaped) // prevent anomalies due to expansion 00047 goto notcf; 00048 if (ec != 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) // Silly, isn't it? Ahoy to Redmond. 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.unicode()[pos2].unicode()) == '\\' || uc == '^')) 00075 pos2++; 00076 if (pos2 < rsts.length() && rsts.unicode()[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 == '^') { 00090 escaped = true; 00091 } else { 00092 notcf: 00093 if (cc == '\\') { 00094 bslashes++; 00095 } else { 00096 if (cc == '"') { 00097 if (!escaped) 00098 shellQuote = !shellQuote; 00099 if (!(bslashes & 1)) 00100 crtQuote = !crtQuote; 00101 } else if (!shellQuote) { 00102 if (cc == '(') 00103 parens++; 00104 else if (cc == ')') 00105 if (--parens < 0) 00106 break; 00107 } 00108 bslashes = 0; 00109 } 00110 escaped = false; 00111 } 00112 pos++; 00113 } 00114 return true; 00115 }
KDE 4.6 API Reference