File: ExecString.cpp

package info (click to toggle)
kodi 2%3A21.2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 143,076 kB
  • sloc: cpp: 694,471; xml: 52,618; ansic: 38,300; python: 7,161; sh: 4,289; javascript: 2,325; makefile: 1,791; perl: 969; java: 513; cs: 390; objc: 340
file content (263 lines) | stat: -rw-r--r-- 8,382 bytes parent folder | download
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
/*
 *  Copyright (C) 2022 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "ExecString.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "music/tags/MusicInfoTag.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"
#include "video/VideoInfoTag.h"

CExecString::CExecString(const std::string& execString)
{
  m_valid = Parse(execString);
}

CExecString::CExecString(const std::string& function, const std::vector<std::string>& params)
  : m_function(function), m_params(params)
{
  m_valid = !m_function.empty();

  if (m_valid)
    SetExecString();
}

CExecString::CExecString(const std::string& function,
                         const CFileItem& target,
                         const std::string& param)
  : m_function(function)
{
  m_valid = !m_function.empty() && !target.GetPath().empty();

  m_params.emplace_back(StringUtils::Paramify(target.GetPath()));

  if (target.m_bIsFolder)
    m_params.emplace_back("isdir");

  if (!param.empty())
    m_params.emplace_back(param);

  if (m_valid)
    SetExecString();
}

CExecString::CExecString(const CFileItem& item, const std::string& contextWindow)
{
  m_valid = Parse(item, contextWindow);
}

namespace
{
void SplitParams(const std::string& paramString, std::vector<std::string>& parameters)
{
  bool inQuotes = false;
  bool lastEscaped = false; // only every second character can be escaped
  int inFunction = 0;
  size_t whiteSpacePos = 0;
  std::string parameter;
  parameters.clear();
  for (size_t pos = 0; pos < paramString.size(); pos++)
  {
    char ch = paramString[pos];
    bool escaped = (pos > 0 && paramString[pos - 1] == '\\' && !lastEscaped);
    lastEscaped = escaped;
    if (inQuotes)
    { // if we're in a quote, we accept everything until the closing quote
      if (ch == '"' && !escaped)
      { // finished a quote - no need to add the end quote to our string
        inQuotes = false;
      }
    }
    else
    { // not in a quote, so check if we should be starting one
      if (ch == '"' && !escaped)
      { // start of quote - no need to add the quote to our string
        inQuotes = true;
      }
      if (inFunction && ch == ')')
      { // end of a function
        inFunction--;
      }
      if (ch == '(')
      { // start of function
        inFunction++;
      }
      if (!inFunction && ch == ',')
      { // not in a function, so a comma signifies the end of this parameter
        if (whiteSpacePos)
          parameter.resize(whiteSpacePos);
        // trim off start and end quotes
        if (parameter.length() > 1 && parameter[0] == '"' &&
            parameter[parameter.length() - 1] == '"')
          parameter = parameter.substr(1, parameter.length() - 2);
        else if (parameter.length() > 3 && parameter[parameter.length() - 1] == '"')
        {
          // check name="value" style param.
          size_t quotaPos = parameter.find('"');
          if (quotaPos > 1 && quotaPos < parameter.length() - 1 && parameter[quotaPos - 1] == '=')
          {
            parameter.erase(parameter.length() - 1);
            parameter.erase(quotaPos);
          }
        }
        parameters.push_back(parameter);
        parameter.clear();
        whiteSpacePos = 0;
        continue;
      }
    }
    if ((ch == '"' || ch == '\\') && escaped)
    { // escaped quote or backslash
      parameter[parameter.size() - 1] = ch;
      continue;
    }
    // whitespace handling - we skip any whitespace at the left or right of an unquoted parameter
    if (ch == ' ' && !inQuotes)
    {
      if (parameter.empty()) // skip whitespace on left
        continue;
      if (!whiteSpacePos) // make a note of where whitespace starts on the right
        whiteSpacePos = parameter.size();
    }
    else
      whiteSpacePos = 0;
    parameter += ch;
  }
  if (inFunction || inQuotes)
    CLog::Log(LOGWARNING, "{}({}) - end of string while searching for ) or \"", __FUNCTION__,
              paramString);
  if (whiteSpacePos)
    parameter.erase(whiteSpacePos);
  // trim off start and end quotes
  if (parameter.size() > 1 && parameter[0] == '"' && parameter[parameter.size() - 1] == '"')
    parameter = parameter.substr(1, parameter.size() - 2);
  else if (parameter.size() > 3 && parameter[parameter.size() - 1] == '"')
  {
    // check name="value" style param.
    size_t quotaPos = parameter.find('"');
    if (quotaPos > 1 && quotaPos < parameter.length() - 1 && parameter[quotaPos - 1] == '=')
    {
      parameter.erase(parameter.length() - 1);
      parameter.erase(quotaPos);
    }
  }
  if (!parameter.empty() || parameters.size())
    parameters.push_back(parameter);
}

void SplitExecFunction(const std::string& execString,
                       std::string& function,
                       std::vector<std::string>& parameters)
{
  std::string paramString;

  size_t iPos = execString.find('(');
  size_t iPos2 = execString.rfind(')');
  if (iPos != std::string::npos && iPos2 != std::string::npos)
  {
    paramString = execString.substr(iPos + 1, iPos2 - iPos - 1);
    function = execString.substr(0, iPos);
  }
  else
    function = execString;

  // remove any whitespace, and the standard prefix (if it exists)
  StringUtils::Trim(function);

  SplitParams(paramString, parameters);
}
} // namespace

bool CExecString::Parse(const std::string& execString)
{
  m_execString = execString;
  SplitExecFunction(m_execString, m_function, m_params);

  // Keep original function case in execstring, lowercase it in function
  StringUtils::ToLower(m_function);
  return true;
}

bool CExecString::Parse(const CFileItem& item, const std::string& contextWindow)
{
  if (item.IsFavourite())
  {
    const CURL url(item.GetPath());
    Parse(CURL::Decode(url.GetHostName()));
  }
  else if (item.m_bIsFolder &&
           (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_playlistAsFolders ||
            !(item.IsSmartPlayList() || item.IsPlayList())))
  {
    if (!contextWindow.empty())
      Build("ActivateWindow", {contextWindow, StringUtils::Paramify(item.GetPath()), "return"});
  }
  else if (item.IsScript() && item.GetPath().size() > 9) // script://<foo>
    Build("RunScript", {StringUtils::Paramify(item.GetPath().substr(9))});
  else if (item.IsAddonsPath() && item.GetPath().size() > 9) // addons://<foo>
  {
    const CURL url(item.GetPath());
    if (url.GetHostName() == "install")
      Build("InstallFromZip", {});
    else if (url.GetHostName() == "check_for_updates")
      Build("UpdateAddonRepos", {"showProgress"});
    else
      Build("RunAddon", {StringUtils::Paramify(url.GetFileName())});
  }
  else if (item.IsAndroidApp() && item.GetPath().size() > 26) // androidapp://sources/apps/<foo>
    Build("StartAndroidActivity", {StringUtils::Paramify(item.GetPath().substr(26))});
  else // assume a media file
  {
    if (item.IsVideoDb() && item.HasVideoInfoTag())
      BuildPlayMedia(item, StringUtils::Paramify(item.GetVideoInfoTag()->m_strFileNameAndPath));
    else if (item.IsMusicDb() && item.HasMusicInfoTag())
      BuildPlayMedia(item, StringUtils::Paramify(item.GetMusicInfoTag()->GetURL()));
    else if (item.IsPicture())
      Build("ShowPicture", {StringUtils::Paramify(item.GetPath())});
    else
    {
      // Everything else will be treated as PlayMedia for item's path
      BuildPlayMedia(item, StringUtils::Paramify(item.GetPath()));
    }
  }
  return true;
}

void CExecString::Build(const std::string& function, const std::vector<std::string>& params)
{
  m_function = function;
  m_params = params;
  SetExecString();
}

void CExecString::BuildPlayMedia(const CFileItem& item, const std::string& target)
{
  std::vector<std::string> params{target};

  if (item.HasProperty("playlist_type_hint"))
    params.emplace_back("playlist_type_hint=" + item.GetProperty("playlist_type_hint").asString());

  Build("PlayMedia", params);
}

void CExecString::SetExecString()
{
  if (m_params.empty())
    m_execString = m_function;
  else
    m_execString = StringUtils::Format("{}({})", m_function, StringUtils::Join(m_params, ","));

  // Keep original function case in execstring, lowercase it in function
  StringUtils::ToLower(m_function);
}