27 #include <QtCore/QStringList>
28 #include <QtCore/QStack>
29 #include <QtCore/QRegExp>
31 namespace KMacroExpander {
46 using namespace KMacroExpander;
52 ushort ec = d->escapechar.unicode();
53 State state = {
noquote,
false };
59 while (pos < str.length()) {
60 ushort cc = str.unicode()[pos].unicode();
64 if (!(len = expandEscapedMacro( str, pos, rst )))
67 if (!(len = expandPlainMacro( str, pos, rst )))
75 rsts = rst.join( QLatin1String(
" ") );
76 rsts.replace( QRegExp(QLatin1String(
"([$`\"\\\\])")), QLatin1String(
"\\\\1") );
78 rsts = rst.join( QLatin1String(
" ") );
79 rsts.replace( QRegExp(QLatin1String(
"(['\\\\])")), QLatin1String(
"\\\\1") );
81 rsts = rst.join( QLatin1String(
" ") );
82 rsts.replace( QLatin1Char(
'\''), QLatin1String(
"'\\''") );
85 str.remove( pos, len );
92 str.replace( pos, len, rsts );
99 }
else if (cc ==
'\\') {
105 state = sstack.pop();
106 }
else if (cc ==
'$') {
107 cc = str.unicode()[++pos].unicode();
109 sstack.push( state );
110 if (str.unicode()[pos + 1].unicode() ==
'(') {
111 Save sav = { str, pos + 2 };
113 state.current =
math;
117 state.current =
paren;
118 state.dquote =
false;
120 }
else if (cc ==
'{') {
121 sstack.push( state );
122 state.current =
subst;
123 }
else if (!state.dquote) {
125 sstack.push( state );
127 }
else if (cc ==
'"') {
128 sstack.push( state );
134 }
else if (cc ==
'`') {
135 str.replace( pos, 1, QLatin1String(
"$( " ));
138 if (pos2 >= str.length()) {
142 cc = str.unicode()[pos2].unicode();
146 cc = str.unicode()[++pos2].unicode();
147 if (cc ==
'$' || cc ==
'`' || cc ==
'\\' ||
148 (cc ==
'"' && state.dquote))
150 str.remove( pos2 - 1, 1 );
156 str[pos2] = QLatin1Char(
')');
157 sstack.push( state );
158 state.current =
paren;
159 state.dquote =
false;
163 state = sstack.pop();
164 }
else if (cc ==
'\'') {
166 sstack.push( state );
169 }
else if (cc ==
'"') {
171 sstack.push( state );
175 }
else if (state.current ==
subst) {
177 state = sstack.pop();
178 }
else if (cc ==
')') {
179 if (state.current ==
math) {
180 if (str.unicode()[pos + 1].unicode() ==
')') {
181 state = sstack.pop();
186 pos = ostack.top().pos;
187 str = ostack.top().str;
189 state.current =
paren;
190 state.dquote =
false;
191 sstack.push( state );
194 }
else if (state.current ==
paren)
195 state = sstack.pop();
198 }
else if (cc ==
'}') {
200 state = sstack.pop();
203 }
else if (cc ==
'(') {
204 sstack.push( state );
205 state.current =
paren;
206 }
else if (cc ==
'{') {
207 sstack.push( state );
212 return sstack.empty();
bool expandMacrosShellQuote(QString &str, int &pos)
Perform safe macro expansion (substitution) on a string for use in shell commands.
QString joinArgs(const QStringList &args)
Quotes and joins args together according to system shell rules.