1 /***************************************************************************
2 speech.cpp - description
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
21 #include <QStringList>
27 #include <QTextStream>
30 #include <kdeversion.h>
32 #include <kmacroexpander.h>
40 QString
Speech::prepareCommand (QString command
, const QString
&text
,
41 const QString
&filename
, const QString
&language
) {
43 QHash
<QChar
,QString
> map
;
47 return KMacroExpander::expandMacrosShellQuote (command
, map
);
49 QStack
<bool> stack
; // saved isdoublequote values during parsing of braces
50 bool issinglequote
=false; // inside '...' ?
51 bool isdoublequote
=false; // inside "..." ?
52 int noreplace
=0; // nested braces when within ${...}
53 QString escText
= KShellProcess::quote(text
);
55 // character sequences that change the state or need to be otherwise processed
56 QRegExp
re_singlequote("('|%%|%t|%f|%l)");
57 QRegExp
re_doublequote("(\"|\\\\|`|\\$\\(|\\$\\{|%%|%t|%f|%l)");
58 QRegExp
re_noquote ("('|\"|\\\\|`|\\$\\(|\\$\\{|\\(|\\{|\\)|\\}|%%|%t|%f|%l)");
61 for (int i
= re_noquote
.search(command
);
63 i
= (issinglequote
?re_singlequote
.search(command
,i
)
64 :isdoublequote
?re_doublequote
.search(command
,i
)
65 :re_noquote
.search(command
,i
))
67 // while there are character sequences that need to be processed
69 if ((command
[i
]=='(') || (command
[i
]=='{')) { // (...) or {...}
70 // assert(isdoublequote == false)
71 stack
.push(isdoublequote
);
73 // count nested braces when within ${...}
77 else if (command
[i
]=='$') { // $(...) or ${...}
78 stack
.push(isdoublequote
);
79 isdoublequote
= false;
80 if ((noreplace
> 0) || (command
[i
+1]=='{'))
81 // count nested braces when within ${...}
85 else if ((command
[i
]==')') || (command
[i
]=='}')) {
86 // $(...) or (...) or ${...} or {...}
88 isdoublequote
= stack
.pop();
90 qWarning("Parse error.");
92 // count nested braces when within ${...}
96 else if (command
[i
]=='\'') {
97 issinglequote
=!issinglequote
;
100 else if (command
[i
]=='"') {
101 isdoublequote
=!isdoublequote
;
104 else if (command
[i
]=='\\')
106 else if (command
[i
]=='`') {
107 // Replace all `...` with safer $(...)
108 command
.replace (i
, 1, "$(");
109 QRegExp
re_backticks("(`|\\\\`|\\\\\\\\|\\\\\\$)");
110 for (int i2
=re_backticks
.search(command
,i
+2);
112 i2
=re_backticks
.search(command
,i2
)
115 if (command
[i2
] == '`') {
116 command
.replace (i2
, 1, ")");
117 i2
=command
.length(); // leave loop
120 // remove backslash and ignore following character
121 command
.remove (i2
, 1);
125 // Leave i unchanged! We need to process "$("
127 else if (noreplace
> 0) { // do not replace macros within ${...}
129 i
+=re_singlequote
.matchedLength();
130 else if (isdoublequote
)
131 i
+=re_doublequote
.matchedLength();
133 i
+=re_noquote
.matchedLength();
135 else { // replace macro
140 match
=re_singlequote
.cap();
141 else if (isdoublequote
)
142 match
=re_doublequote
.cap();
144 match
=re_noquote
.cap();
146 // substitute %variables
149 else if (match
=="%f")
151 else if (match
=="%%")
153 else if (match
=="%l")
156 // %variable inside of a quote?
159 else if (issinglequote
)
162 command
.replace (i
, match
.length(), v
);
170 void Speech::speak(QString command
, bool stdIn
, const QString
&text
, const QString
&language
, int encoding
, QTextCodec
*codec
) {
171 if (text
.length () > 0) {
172 // 1. prepare the text:
173 // 1.a) encode the text
174 QTextStream
ts (encText
, QIODevice::WriteOnly
);
175 if (encoding
== Local
)
176 ts
.setEncoding (QTextStream::Locale
);
177 else if (encoding
== Latin1
)
178 ts
.setEncoding (QTextStream::Latin1
);
179 else if (encoding
== Unicode
)
180 ts
.setEncoding (QTextStream::Unicode
);
185 // 1.b) create a temporary file for the text
186 tempFile
.setAutoDelete(true);
187 QTextStream
* fs
= tempFile
.textStream();
188 if (encoding
== Local
)
189 fs
->setEncoding (QTextStream::Locale
);
190 else if (encoding
== Latin1
)
191 fs
->setEncoding (QTextStream::Latin1
);
192 else if (encoding
== Unicode
)
193 fs
->setEncoding (QTextStream::Unicode
);
195 fs
->setCodec (codec
);
198 QString filename
= tempFile
.file()->name();
201 // 2. prepare the command:
202 command
= prepareCommand (command
, encText
, filename
, language
);
205 // 3. create a new process
207 connect(&process
, SIGNAL(processExited(KProcess
*)), this, SLOT(processExited(KProcess
*)));
208 connect(&process
, SIGNAL(wroteStdin(KProcess
*)), this, SLOT(wroteStdin(KProcess
*)));
209 connect(&process
, SIGNAL(receivedStdout(KProcess
*, char *, int)), this, SLOT(receivedStdout(KProcess
*, char *, int)));
210 connect(&process
, SIGNAL(receivedStderr(KProcess
*, char *, int)), this, SLOT(receivedStderr(KProcess
*, char *, int)));
212 // 4. start the process
214 process
.start(KProcess::NotifyOnExit
, KProcess::All
);
215 if (encText
.size() > 0)
216 process
.writeStdin(encText
, encText
.size());
218 process
.closeStdin();
221 process
.start(KProcess::NotifyOnExit
, KProcess::AllOutput
);
225 void Speech::receivedStdout (KProcess
*, char *buffer
, int buflen
) {
226 kDebug() << QString::fromLatin1(buffer
, buflen
) + "\n";
228 void Speech::receivedStderr (KProcess
*, char *buffer
, int buflen
) {
229 kDebug() << QString::fromLatin1(buffer
, buflen
) + "\n";
232 void Speech::wroteStdin(KProcess
*) {
233 process
.closeStdin();
236 void Speech::processExited(KProcess
*) {
240 #include "speech.moc"