summaryrefslogtreecommitdiffstats
path: root/kommander/widget/kommanderfunctions.cpp
blob: b36a725136f2474078742f1c548d12891434f386 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/***************************************************************************
                    kommanderfunctions.cpp - Text widget core functionality 
                             -------------------
    copyright          : (C) 2004      Michal Rudolf <mrudolf@kdewebdwev.org>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <iostream>
#include <stdlib.h> 

#include <tqfile.h>
#include <tqregexp.h>
#include <tqtextstream.h>
 
#include <dcopclient.h>
#include <kapplication.h>
#include <kconfig.h>
#include <klocale.h>
#include <kglobal.h>

#include "kommanderwidget.h"
#include "specials.h"
#include "specialinformation.h"
#include "expression.h"
#include "parser.h"

TQString KommanderWidget::evalFunction(const TQString& function, const TQStringList& args)
{ 
  switch (SpecialInformation::function(Group::Kommander, function)) {
    case Kommander::widgetText:
      return handleDCOP(DCOP::text);
    case Kommander::selectedWidgetText:
      return handleDCOP(DCOP::selection);
    case Kommander::dcopid:
      return kapp->dcopClient()->appId();
    case Kommander::pid:
      return TQString().setNum(getpid());
    case Kommander::null:
      return TQString();
    case Kommander::comment:
      return TQString("#");
    case Kommander::exec:
      return execCommand(args[0]);
    case Kommander::dcop:
      return DCOPQuery(args);
    case Kommander::parentPid:
      return global("_PARENTPID").isEmpty() ? TQString().setNum(getppid()) : global("_PARENTPID");
    case Kommander::env:
      return TQString(getenv(args[0].latin1())); 
    case Kommander::i18n:
      return TDEGlobal::locale()->translate(args[0]);
    case Kommander::global:
      return global(args[0]);
    case Kommander::setGlobal:
      setGlobal(args[0], args[1]); 
      return TQString();
    case Kommander::debug:
      tqDebug("%s", args[0].latin1());
      fflush(stderr);
      return TQString();
    case Kommander::echo:
      for (uint i=0; i<args.count(); i++)
         std::cout << args[i].latin1();
      fflush(stdout);
      return TQString();
    case Kommander::readSetting:
    {
      TQString fname = fileName();
      if (!fname.isEmpty())
      {
        TDEConfig cfg("kommanderrc", true);
        cfg.setGroup(fname);
        return cfg.readEntry(args[0], args[1]);
      }
      return TQString();
    }
    case Kommander::writeSetting:
    {
      TQString fname = fileName();
      if (!fname.isEmpty())
      {
        TDEConfig cfg("kommanderrc", false);
        cfg.setGroup(fname);
        cfg.writeEntry(args[0], args[1]);
      }
      return TQString();
    }
    case Kommander::dialog:
      if (args.count() > 1)
        return runDialog(args[0], args[1]); 
      else
        return runDialog(args[0]); 
    case Kommander::expr:
    {
      Expression expr(args[0]);
      bool ok;
      TQVariant value = expr.value(&ok);
      return ok ? value.toString() : TQString();
    }
    default:
      return TQString();
  }
}


TQString KommanderWidget::evalExecBlock(const TQStringList& args, const TQString& s, int& pos) 
{
  int f = s.find("@execEnd", pos);  
  if (f == -1)
  {
    printError(i18n("Unterminated @execBegin ... @execEnd block."));
    return TQString();
  } 
  else
  {
    TQString shell = args.count() ? args[0] : TQString();
    int start = pos;
    pos = f + TQString("@execEnd").length()+1;
    return execCommand(evalAssociatedText(s.mid(start, f - start)), shell);
  }
}
 
TQString KommanderWidget::evalForEachBlock(const TQStringList& args, const TQString& s, int& pos) 
{
  int f = s.find("@end", pos);  
//FIXME: better detection of block boundaries  
  if (f == -1)
  {
    printError(i18n("Unterminated @forEach ... @end block."));
    return TQString();
  }
  else
  {
    int start = pos;
    pos = f + TQString("@end").length()+1;
    TQString var = args[0];
    TQStringList loop = TQStringList::split("\n", args[1]);
    TQString output;
    TQString block = substituteVariable(s.mid(start, f - start), TQString("%1_count").arg(var),
      TQString::number(loop.count()));
    TQString varidx = TQString("%1_index").arg(var);
    for (uint i=0; i<loop.count(); i++)
      output += evalAssociatedText(substituteVariable(substituteVariable(block, varidx, TQString::number(i+1)),
        var, loop[i]));
    return output;
  }
}

TQString KommanderWidget::evalForBlock(const TQStringList& args, const TQString& s, int& pos) 
{
  int f = s.find("@end", pos);  
//FIXME: better detection of block boundaries  
  if (f == -1)
  {
    printError(i18n("Unterminated @forEach ... @end block."));
    return TQString();
  } 
  else
  {
    int start = pos;
    pos = f + TQString("@end").length()+1;
    TQString block = s.mid(start, f - start);
    TQString variable = args[0];

    Expression expr;
    int loopstart = expr.value(args[1]).toInt();
    int loopend = expr.value(args[2]).toInt();
    int loopstep = 1;
    if (args.count() > 3)
    {
      loopstep = expr.value(args[3]).toInt();  
      if (!loopstep)
        loopstep = 1;
    }

    TQString output;
    for (int i=loopstart; i<=loopend; i+=loopstep)
    {
      output += evalAssociatedText(substituteVariable(block, variable, TQString::number(i)));
    }
    return output;
  }
}

TQString KommanderWidget::evalIfBlock(const TQStringList& args, const TQString& s, int& pos) 
{
  int f = s.find("@endif", pos);
//FIXME: better detection of block boundaries; add error message
  if (f == -1)
  {
    pos = s.length()+1;
    printError(i18n("Unterminated @if ... @endif block."));
    return TQString();
  }
  else
  {
    TQString block = s.mid(pos, f - pos);
    pos = f + TQString("@endif").length()+1;
    Expression expr;
    if (expr.isTrue(args[0]))
      return evalAssociatedText(block);
    return TQString();
  }
}

TQString KommanderWidget::evalSwitchBlock(const TQStringList& args, const TQString& s, int& pos) 
{
  int f = s.find("@end", pos);
//FIXME: better detection of block boundaries; add error message
  if (f == -1)
  {
    printError(i18n("Unterminated @switch ... @end block."));
    return TQString();
  }
  else
  {
    TQString block = s.mid(pos, f - pos);
    pos = f + TQString("@end").length()+1;
    f = parseBlockBoundary(block, 0, "@case");
    bool finished = f == -1;
    while (!finished)
    {
      f += 5;
      int end = parseBlockBoundary(block, f, "@case");
      if (end == -1) 
      {
        end = block.length();
        finished = true;
      }
      bool ok;
      TQString value = parseBrackets(block, f, ok);
      if (!ok) 
        break;
      if (value == args[0] || value == "*")
        return evalAssociatedText(block.mid(f, end-f));
      f = end;
    }
    return TQString();
  }
}



TQString KommanderWidget::evalArrayFunction(const TQString& function, const TQStringList& args)
{
  Parser parser(internalParserData());
  int fname = SpecialInformation::function(Group::Array, function);
  TQString array = args[0].startsWith("_") ? args[0] : TQString("_")+ args[0];

  if (fname == Array::setValue)
    parser.setArray(array, args[1], args[2]);
  else if (fname == Array::fromString)
  {
    TQStringList lines = TQStringList::split("\n", args[1]);
    for (TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
    {
      TQString key = (*it).section('\t', 0, 0).stripWhiteSpace();
      if (!key.isEmpty())
        parser.setArray(array, key, (*it).section('\t', 1));
    }
  }
  else if (!parser.isArray(array))
    return TQString();
  else switch (fname) {
    case Array::value:
      return parser.arrayValue(array, args[1]).toString();
    case Array::keys:
    {
      const TQMap<TQString, ParseNode> map = parser.array(array);
      TQStringList keys;
      for (TQMap<TQString, ParseNode>::ConstIterator it = map.begin(); it != map.end(); ++it)
        keys.append(it.key());
      return keys.join("\n");
    }
    case Array::values:
    {
      const TQMap<TQString, ParseNode> map = parser.array(array);
      TQStringList values;
      for (TQMap<TQString, ParseNode>::ConstIterator it = map.begin(); it != map.end(); ++it)
        values.append(it.data().toString());
      return values.join("\n");
    }
    case Array::clear:
      parser.unsetArray(array);
      return TQString();
    case Array::remove:
      parser.unsetArray(array, args[1]);
      return TQString();
    case Array::count:
      return TQString::number(parser.array(array).count());
    case Array::toString:
    {
      const TQMap<TQString, ParseNode> map = parser.array(array);
      TQString arraystring;
      for (TQMap<TQString, ParseNode>::ConstIterator it = map.begin(); it != map.end(); ++it)
        arraystring += TQString("%1\t%2\n").arg(it.key()).arg(it.data().toString());
      return arraystring;
    }
    default:
      return TQString();
  }
  return TQString();
}


TQString KommanderWidget::evalWidgetFunction(const TQString& identifier, const TQString& s, int& pos)
{
  KommanderWidget* pWidget = parseWidget(identifier);
  if (!pWidget) 
  {
    printError(i18n("Unknown widget: @%1.").arg(identifier));
    return TQString();
  }
  if (s[pos] == '.')
  {
    pos++;
    bool ok = true;
    TQString function = parseIdentifier(s, pos);
    TQStringList args = parseFunction("DCOP", function, s, pos, ok);
    if (!ok)
      return TQString();
    args.prepend(pWidget->widgetName());
    TQString prototype = SpecialInformation::prototype(Group::DCOP,
      SpecialInformation::function(Group::DCOP, function));
    return localDCOPQuery(prototype, args);
  }
  else if (pWidget == this)
  {
    printError(i18n("Infinite loop: @%1 called inside @%2.").arg(pWidget->widgetName())
        .arg(pWidget->widgetName()));
    return TQString();
  }
  else if (!pWidget->hasAssociatedText())
  {
    printError(i18n("Script for @%1 is empty.").arg(pWidget->widgetName()));
    return TQString();
  }
  return pWidget->evalAssociatedText();
}