File: varparser.c

package info (click to toggle)
vdr-plugin-epgsearch 1.0.1~beta6+git20150211-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,780 kB
  • ctags: 2,325
  • sloc: ansic: 23,325; perl: 1,330; sh: 609; makefile: 197
file content (239 lines) | stat: -rw-r--r-- 6,532 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
/*                                                                  -*- 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 "varparser.h"
#include <vdr/plugin.h>
#include "log.h"
#include "epgsearchtools.h"
#include <sstream>

bool cVarParser::Parse(const string& input)
{
   return ParseAssign(input);
}

bool cVarParser::ParseAssign(const string& input)
{
   int assignPos = input.find("=");
   if (assignPos >= 0)
   {
      string var(input.begin(), input.begin() + assignPos);
      if (ParseVar(var))
      {
         varName = Strip(var);
         string assign(input.begin() + assignPos + 1, input.end());
         return ParseExp(assign);
      }
   }
   LogFile.eSysLog("error parsing '%s'", input.c_str());
   return false;
}

bool cVarParser::ParseExp(const string& input)
{
   // system call?
   int sysPos = input.find("system");
   if (sysPos == 0)
      return ParseShellCmd(input);
   // connect command?
   int conPos = input.find("connect");
   if (conPos == 0)
      return ParseConnectCmd(input);
   // length command?
   int lenPos = input.find("length");
   if (lenPos == 0)
      return ParseLengthCmd(input);
   // conditional expression?
   int varPos = Strip(input).find("%");
   if (varPos == 0)
     {
       int queryPos = input.find("?");
       if (queryPos >= 0)
	 {
	   int colonPos = input.find(":");
	   if (colonPos > queryPos)
	     return ParseCondExp(input);
	 }
     }
   // composed expression
   compExpr = input;
   return true;
}

bool cVarParser::ParseShellCmd(const string& input)
{
   int cmdPos = input.find("(");
   int cmdArgsBegin = input.find(",");
   int cmdArgsEnd = input.rfind(")");
   if (cmdPos == -1 || cmdArgsEnd == -1) return false;
   string shellcmd(input.begin() + cmdPos + 1, input.begin() + (cmdArgsBegin >= 0?cmdArgsBegin:cmdArgsEnd));
   shellcmd = Strip(shellcmd);

   cmdArgs = "";
   if (cmdArgsBegin >= 0)
      cmdArgs = string(input.begin() + cmdArgsBegin + 1, input.begin() + cmdArgsEnd);

   string cmdVDR = "varcmd: " + shellcmd;
   cmd = new cCommand;
   if (!cmd->Parse(cmdVDR.c_str()))
   {
      LogFile.eSysLog("error parsing command: %s", input.c_str());
      delete cmd;
      cmd = NULL;
      return false;
   }
   type = cVarParser::shellcmd;
   return true;
}

bool cVarParser::ParseConnectCmd(const string& input)
{
   int startCon = input.find("(");
   int endCon = input.find(")");
   if (startCon == -1 || endCon == -1) return false;
   string connect(input.begin() + startCon + 1, input.begin() + endCon);
   std::stringstream ss(connect);
   std::string item;
   if (std::getline(ss, item, ','))
     connectAddr = item;
   if (std::getline(ss, item, ','))
     connectPort = atoi(item.c_str());
   if (std::getline(ss, item))
     cmdArgs = item;

   connectAddr = Strip(connectAddr);
   cmdArgs = Strip(cmdArgs);

   if (connectAddr.size() == 0 || connectPort == -1)
   {
     LogFile.eSysLog("error parsing command: %s", input.c_str());
     return false;
   }
   type = cVarParser::connectcmd;
   return true;
}

bool cVarParser::ParseLengthCmd(const string& input)
{
   int startLen = input.find("(");
   int endLen = input.find(")");
   if (startLen == -1 || endLen == -1) return false;
   string arg(input.begin() + startLen + 1, input.begin() + endLen);
   compExpr = arg;
   type = cVarParser::lengthcmd;
   return true;
}

bool cVarParser::ParseCondExp(const string& input)
{
   int condEndPos = input.find("?");
   string cond(input.begin(), input.begin() + condEndPos);
   int condNeqPos = cond.find("!=");
   int condEqPos = cond.find("==");

   if (condEqPos == -1 && condNeqPos == -1)
   {
      cond += "!=";
      condNeqPos = cond.find("!=");
   }

   if (condEqPos >= 0 || condNeqPos >= 0)
   {
      if (!ParseEquality(cond))
      {
         LogFile.eSysLog("error parsing '%s'", input.c_str());
         return false;
      }
      condOp = (condEqPos >= 0)?condEq:condNeq;
   }
   else
   {
      LogFile.eSysLog("error parsing '%s'", input.c_str());
      return false;
   }

   string truefalse(input.begin() + condEndPos + 1, input.end());
   int elsePos = truefalse.find(":");
   if (elsePos >= 0)
   {
      string truePart(truefalse.begin(), truefalse.begin() + elsePos);
      string falsePart(truefalse.begin() + elsePos + 1, truefalse.end());
      if (ParseVar(truePart) && ParseVar(falsePart))
      {
         condvarTrue = Strip(truePart);
         condvarFalse = Strip(falsePart);
	 type = cVarParser::condition;
         return true;
      }
   }
   LogFile.eSysLog("error parsing '%s'", input.c_str());
   condEqLeft = condEqRight = "";
   return false;
}

bool cVarParser::ParseEquality(const string& input)
{
   int condEqPos = input.find("==");
   int condNeqPos = input.find("!=");
   int condOpPos = -1;
   if (condEqPos >= 0) condOpPos = condEqPos;
   if (condNeqPos >= 0) condOpPos = condNeqPos;
   if (condOpPos == -1) return false;
   string left(input.begin(), input.begin() + condOpPos);
   string right(input.begin() + condOpPos + 2, input.end());
   if (ParseExp(left) && ParseExp(right))
   {
      condEqLeft = Strip(left);
      condEqRight = Strip(right);
      return true;
   }
   return false;
}

bool cVarParser::ParseVar(const string& input)
{
   string str = Strip(input);
   if (str.size() > 2 && str[0] == '%' && str[str.size()-1] == '%')
      return true;
   return false;
}

bool cVarParser::IsCondExpr()
{
   return type == cVarParser::condition;
}

bool cVarParser::IsShellCmd()
{
   return type == cVarParser::shellcmd;
}

bool cVarParser::IsConnectCmd()
{
   return type == cVarParser::connectcmd;
}

bool cVarParser::IsLengthCmd()
{
   return type == cVarParser::lengthcmd;
}