File: uservars.c

package info (click to toggle)
vdr-plugin-epgsearch 1.0.1~beta3-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,764 kB
  • ctags: 2,316
  • sloc: ansic: 23,300; perl: 1,330; sh: 608; makefile: 193
file content (427 lines) | stat: -rw-r--r-- 12,213 bytes parent folder | download | duplicates (3)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*                                                                  -*- c++ -*-
Copyright (C) 2004-2013 Christian Wieninger

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

The author can be reached at cwieninger@gmx.de

The project's page is at http://winni.vdr-developer.org/epgsearch
*/

#include <algorithm>
#ifdef __FreeBSD__
#include <netinet/in.h>
#endif
#include "uservars.h"
#include "epgsearchcats.h"
#include "epgsearchtools.h"
#include "log.h"
#include <sys/socket.h>
#include <netdb.h>

cUserVars UserVars;

string cPlugconfdirVar::dir = "";
string cExtEPGVar::nameSpace = "epg";
string cTimerVar::nameSpace = "timer";
string cSearchVar::nameSpace = "search";

// cUserVar

cUserVar::cUserVar()
{
   oldEvent = NULL;
   oldescapeStrings = false;
}

string cUserVar::Evaluate(const cEvent* e, bool escapeStrings)
{
   if (oldEvent && oldEvent == e && oldescapeStrings == escapeStrings)
    return oldResult;
   usedVars.clear();
   string result;
   if (IsShellCmd())
      result = EvaluateShellCmd(e);
   else if (IsConnectCmd())
     result = EvaluateConnectCmd(e);
   else if (IsLengthCmd())
     result = EvaluateLengthCmd(e);
   else if (IsCondExpr())
      result = EvaluateCondExpr(e);
   else
      result = EvaluateCompExpr(e);
   oldResult = result;
   oldEvent = e;
   oldescapeStrings = escapeStrings;

   // avoid double dir separators
   int pos = 0;
   while((pos = result.find("~~")) >= 0)
      result.replace(pos, 2, "~");

   return result;
}

string cUserVar::EvaluateShellCmd(const cEvent* e)
{
   if (!varparser.cmd) return "";
   string cmdArgs;
   if (varparser.cmdArgs != "")
   {
      string args = varparser.cmdArgs;
      varparser.compExpr = args; //handle the args as composed expr
      cmdArgs = EvaluateCompExpr(e, true);
   }
   const char* res = varparser.cmd->Execute(cmdArgs.c_str());
   string result = res?res:"";
   int crPos = 0; // remove any CR
   while((crPos = result.find("\n")) >= 0)
      result.replace(crPos, 1, "");

   return result;
}

#define MAX_LINE 1000
string cUserVar::EvaluateConnectCmd(const cEvent* e)
{
   if (varparser.connectAddr == "") return "";

   int       conn_s;                /*  connection socket         */
   struct    sockaddr_in servaddr;  /*  socket address structure  */
   char      buffer[MAX_LINE];      /*  character buffer          */

   if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
   {
     LogFile.eSysLog("Error creating listening socket");
     return "";
   }

   memset(&servaddr, 0, sizeof(varparser.connectAddr.c_str()));
   servaddr.sin_family      = AF_INET;
   servaddr.sin_port        = htons(varparser.connectPort);

   if (getAddrFromString(varparser.connectAddr.c_str(), &servaddr) != 0)
   {
     LogFile.eSysLog("Invalid remote address");
     return "";
   }

   if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) < 0 )
   {
     LogFile.eSysLog("Error calling connect()");
     return "";
   }

   varparser.compExpr = varparser.cmdArgs;
   string resexp = EvaluateCompExpr(e, true);
   sprintf(buffer, "%s\n", resexp.c_str());
   Writeline(conn_s, buffer, strlen(buffer));
   Readline(conn_s, buffer, MAX_LINE-1);

   close(conn_s);
   return buffer;
}

string cUserVar::EvaluateLengthCmd(const cEvent* e)
{
   return NumToString(EvaluateCompExpr(e, false).size());
}

string cUserVar::EvaluateCondExpr(const cEvent* e, bool escapeStrings)
{
   string condresult = "";
   cVarExpr leftExp(varparser.condEqLeft);
   string resLeft = leftExp.Evaluate(e);
   cVarExpr rightExp(varparser.condEqRight);
   string resRight = rightExp.Evaluate(e);
   if (varparser.condOp == condEq && resLeft == resRight)  condresult = "1"; // assign any value
   if (varparser.condOp == condNeq && resLeft != resRight) condresult = "1"; // assign any value

   cUserVar* condVarTrue = UserVars.GetFromName(varparser.condvarTrue);
   cUserVar* condVarFalse = UserVars.GetFromName(varparser.condvarFalse);
   if (!condVarTrue || !condVarFalse) return "";

   if (!AddDepVar(condVarTrue)) return "";
   if (!AddDepVar(condVarFalse)) return "";

   if (condresult != "")
   {
     LogFile.Log(3, "using case 'true'");
     return condVarTrue->Evaluate(e, escapeStrings);
   }
   else
   {
     LogFile.Log(3, "using case 'false'");
     return condVarFalse->Evaluate(e, escapeStrings);
   }
}

string cUserVar::EvaluateCompExpr(const cEvent* e, bool escapeStrings)
{
   string expr = varparser.compExpr;
   if (expr.find('%') == string::npos) return expr;

   // handle internal vars like title and subtitle
   expr = EvaluateInternalVars(expr, e, escapeStrings);

   // replace ext. EPG variables
   expr = EvaluateExtEPGVars(expr, e, escapeStrings);

   // replace other user vars
   expr = EvaluateUserVars(expr, e, escapeStrings);

   return expr;
}

string cUserVar::EvaluateInternalVars(const string& Expr, const cEvent* e, bool escapeStrings)
{
   string expr = Expr;
   if (expr.find('%') == string::npos) return expr;

   std::map<string, cInternalVar*>::iterator it;
   for (it = UserVars.internalVars.begin(); it != UserVars.internalVars.end(); ++it)
   {
      string varName = (it->second)->Name();
      int varPos = 0;
      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         usedVars.insert(it->second);
         expr.replace(varPos, varName.size(), (it->second)->Evaluate(e, escapeStrings));
      }
   }
   return expr;
}

string cUserVar::EvaluateExtEPGVars(const string& Expr, const cEvent* e, bool escapeStrings)
{
   string expr = Expr;
   if (expr.find('%') == string::npos) return expr;

   std::map<string, cExtEPGVar*>::iterator evar;
   for (evar = UserVars.extEPGVars.begin(); evar != UserVars.extEPGVars.end(); ++evar)
   {
      // replace ext. EPG variables with leading namespace 
      string varName = evar->second->Name(true);
      int varPos = 0;
      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         expr.replace(varPos, varName.size(), evar->second->Evaluate(e, escapeStrings));
         usedVars.insert(evar->second);
      }
      // replace ext. EPG variables without leading namespace 
      varName = evar->second->Name();
      varPos = 0;
      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         expr.replace(varPos, varName.size(), evar->second->Evaluate(e, escapeStrings));
         usedVars.insert(evar->second);
      }
   }
   return expr;
}

string cUserVar::EvaluateUserVars(const string& Expr, const cEvent* e, bool escapeStrings)
{
   string expr = Expr;
   if (expr.find('%') == string::npos) return expr;

   std::set<cUserVar*>::iterator it;
   for (it = UserVars.userVars.begin(); it != UserVars.userVars.end(); ++it)
   {
      string varName = (*it)->Name();
      int varPos = 0;
      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         if (!AddDepVar(*it)) return "";
         expr.replace(varPos, varName.size(), (*it)->Evaluate(e, escapeStrings));
      }
   }
   return expr;
}

string cUserVar::EvaluateInternalTimerVars(const string& Expr, const cTimer* t)
{
   string expr = Expr;
   if (expr.find('%') == string::npos) return expr;

   std::map<string, cTimerVar*>::iterator tvar;
   for (tvar = UserVars.internalTimerVars.begin(); tvar != UserVars.internalTimerVars.end(); ++tvar)
   {
      string varName = tvar->second->Name();
      int varPos = 0;

      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         expr.replace(varPos, varName.size(), tvar->second->Evaluate(t));
      }
   }
   return expr;
}

string cUserVar::EvaluateInternalSearchVars(const string& Expr, const cSearchExt* s)
{
   string expr = Expr;
   if (expr.find('%') == string::npos) return expr;

   std::map<string, cSearchVar*>::iterator svar;
   for (svar = UserVars.internalSearchVars.begin(); svar != UserVars.internalSearchVars.end(); ++svar)
   {
      string varName = svar->second->Name();
      int varPos = 0;
      while((varPos = FindIgnoreCase(expr, varName)) >= 0)
      {
         expr.replace(varPos, varName.size(), svar->second->Evaluate(s));
      }
   }
   return expr;
}

bool cUserVar::DependsOnVar(const string& varName)
{
   string VarName = Strip(varName);
   cUserVar* var = UserVars.GetFromName(VarName);
   if (!var) return false;
   return DependsOnVar(var);
}

bool cUserVar::DependsOnVar(cUserVar* var)
{
   if (!var) return false;
   if (usedVars.find(var) != usedVars.end()) return true;
   std::set<cUserVar*>::iterator it;
   for (it = usedVars.begin(); it != usedVars.end(); ++it)
      if ((*it)->DependsOnVar(var))
         return true;
   return false;
}

bool cUserVar::AddDepVar(cUserVar* var)
{
   if (var == this || var->DependsOnVar(this))
   {
      LogFile.eSysLog("ERROR - found cylic reference to var '%s' in var '%s'", var->Name().c_str(), Name().c_str());
      return false;
   }
   usedVars.insert(var);
   return true;
}

void cUserVar::ResetCache()
{
   oldEvent = NULL;
   oldResult = "";
}

// cUserVarLine
bool cUserVarLine::Parse(char *s)
{
   if (!s) return false;
   if (s[0] == '#')
      return true;
   char *p = strchr(s, '=');
   if (p)
   {
      cUserVar* userVar = new cUserVar;
      if (userVar->varparser.Parse(s))
      {
	cUserVar* oldVar = UserVars.GetFromName(userVar->Name(), false);
	if (oldVar) // allow redefintion of existing vars
	  {
	    LogFile.Log(1, "variable '%s' gets overwritten", oldVar->Name().c_str());
	    UserVars.userVars.erase(oldVar);
	    delete oldVar;
	  }
	UserVars.userVars.insert(userVar);
	return true;
      }
   }
   return false;
}

// cUserVars
cUserVar* cUserVars::GetFromName(const string& varName, bool log)
{
   string VarName = Strip(varName);
   std::transform(VarName.begin(), VarName.end(), VarName.begin(), tolower);

   std::map<string, cInternalVar*>::iterator ivar = internalVars.find(VarName);
   if (ivar != internalVars.end())
      return ivar->second;

   std::set<cUserVar*>::iterator uvar;
   for (uvar = userVars.begin(); uvar != userVars.end(); ++uvar)
      if (EqualsNoCase((*uvar)->Name(), VarName))
         return (*uvar);

   std::map<string, cExtEPGVar*>::iterator evar = extEPGVars.find(VarName);
   if (evar != extEPGVars.end())
      return evar->second;

   if (log)
     LogFile.eSysLog("var '%s' not defined!", VarName.c_str());
   return NULL;
}

// cVarExpr
string cVarExpr::Evaluate(const cEvent* e)
{
   // create a dummy user var
   cUserVar var;
   if (!var.varparser.ParseExp(expr))
      return "";

   LogFile.Log(3, "start evaluating expression '%s'", expr.c_str());
   string result = var.Evaluate(e);
   LogFile.Log(3, "stop evaluating expression '%s' - result: '%s'", expr.c_str(), result.c_str());
   usedVars = var.usedVars;
   return result;
}

string cVarExpr::Evaluate(const cTimer* t)
{
   // create a dummy user var
   cUserVar var;
   if (!var.varparser.ParseExp(expr))
      return "";

   usedVars = var.usedVars;
   return var.EvaluateInternalTimerVars(expr, t);
}

string cVarExpr::Evaluate(const cSearchExt* s)
{
   // create a dummy user var
   cUserVar var;
   if (!var.varparser.ParseExp(expr))
      return "";

   usedVars = var.usedVars;
   return var.EvaluateInternalSearchVars(expr, s);
}

bool cVarExpr::DependsOnVar(const string& varName, const cEvent* e)
{
   string VarName = Strip(varName);
   if (FindIgnoreCase(expr, VarName) >= 0)
      return true;
   // create a dummy user var
   cUserVar var;
   var.varparser.varName = expr;
   if (!var.varparser.ParseExp(expr))
      return false;
   var.Evaluate(e);
   return var.DependsOnVar(VarName);
}